diff --git a/client/mysqlbinlog.cc b/client/mysqlbinlog.cc
index cfc05cbf794..9765fbbd470 100644
--- a/client/mysqlbinlog.cc
+++ b/client/mysqlbinlog.cc
@@ -1,6 +1,6 @@
 /*
    Copyright (c) 2000, 2014, Oracle and/or its affiliates.
-   Copyright (c) 2009, 2014, MariaDB
+   Copyright (c) 2009, 2019, MariaDB
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -58,7 +58,7 @@
 extern "C" unsigned char *mysql_net_store_length(unsigned char *packet, size_t length);
 #define net_store_length mysql_net_store_length
 
-Rpl_filter *binlog_filter= 0;
+Rpl_filter *binlog_filter;
 
 #define BIN_LOG_HEADER_SIZE	4
 #define PROBE_HEADER_LEN	(EVENT_LEN_OFFSET+4)
@@ -67,20 +67,17 @@ Rpl_filter *binlog_filter= 0;
 CHARSET_INFO* system_charset_info= &my_charset_utf8_general_ci;
 
 /* Needed for Flashback */
-DYNAMIC_ARRAY binlog_events; // Storing the events output string
-DYNAMIC_ARRAY events_in_stmt; // Storing the events that in one statement
-String stop_event_string; // Storing the STOP_EVENT output string
+static DYNAMIC_ARRAY binlog_events; // Storing the events output string
+static DYNAMIC_ARRAY events_in_stmt; // Storing the events that in one statement
+static String stop_event_string; // Storing the STOP_EVENT output string
 
 char server_version[SERVER_VERSION_LENGTH];
-ulong server_id = 0;
+ulong server_id;
 
-// needed by net_serv.c
-ulong bytes_sent = 0L, bytes_received = 0L;
-ulong mysqld_net_retry_count = 10L;
 ulong open_files_limit;
 ulong opt_binlog_rows_event_max_size;
-ulonglong test_flags = 0;
-ulong opt_binlog_rows_event_max_encoded_size= MAX_MAX_ALLOWED_PACKET;
+ulonglong test_flags;
+static ulong opt_binlog_rows_event_max_encoded_size= MAX_MAX_ALLOWED_PACKET;
 static uint opt_protocol= 0;
 static FILE *result_file;
 static char *result_file_name= 0;
@@ -151,7 +148,7 @@ static char *flashback_review_dbname, *flashback_review_tablename;
   This will be changed each time a new Format_description_log_event is
   found in the binlog. It is finally destroyed at program termination.
 */
-static Format_description_log_event* glob_description_event= NULL;
+static Format_description_log_event* glob_description_event;
 
 /**
   Exit status for functions in this file.
@@ -174,24 +171,21 @@ enum Exit_status {
   Also because of that when reading a remote Annotate event we have to keep
   its binary log representation in a separately allocated buffer.
 */
-static Annotate_rows_log_event *annotate_event= NULL;
+static Annotate_rows_log_event *annotate_event;
 
-void free_annotate_event()
+static void free_annotate_event()
 {
-  if (annotate_event)
-  {
-    delete annotate_event;
-    annotate_event= 0;
-  }
+  delete annotate_event;
+  annotate_event= NULL;
 }
 
-Log_event* read_remote_annotate_event(uchar* net_buf, ulong event_len,
-                                      const char **error_msg)
+static Log_event* read_remote_annotate_event(uchar* net_buf, ulong event_len,
+                                             const char **error_msg)
 {
-  uchar *event_buf;
+  char *event_buf;
   Log_event* event;
 
-  if (!(event_buf= (uchar*) my_malloc(event_len + 1, MYF(MY_WME))))
+  if (!(event_buf= (char*) my_malloc(event_len + 1, MYF(MY_WME))))
   {
     error("Out of memory");
     return 0;
@@ -200,7 +194,7 @@ Log_event* read_remote_annotate_event(uchar* net_buf, ulong event_len,
   memcpy(event_buf, net_buf, event_len);
   event_buf[event_len]= 0;
 
-  if (!(event= Log_event::read_log_event((const char*) event_buf, event_len,
+  if (!(event= Log_event::read_log_event(event_buf, event_len,
                                          error_msg, glob_description_event,
                                          opt_verify_binlog_checksum)))
   {
@@ -211,24 +205,23 @@ Log_event* read_remote_annotate_event(uchar* net_buf, ulong event_len,
     Ensure the event->temp_buf is pointing to the allocated buffer.
     (TRUE = free temp_buf on the event deletion)
   */
-  event->register_temp_buf((char*)event_buf, TRUE);
+  event->register_temp_buf(event_buf, TRUE);
 
   return event;
 }
 
-void keep_annotate_event(Annotate_rows_log_event* event)
+static void keep_annotate_event(Annotate_rows_log_event* event)
 {
-  free_annotate_event();
+  delete annotate_event;
   annotate_event= event;
 }
 
-void print_annotate_event(PRINT_EVENT_INFO *print_event_info)
+static void print_annotate_event(PRINT_EVENT_INFO *print_event_info)
 {
   if (annotate_event)
   {
     annotate_event->print(result_file, print_event_info);
-    delete annotate_event;  // the event should not be printed more than once
-    annotate_event= 0;
+    free_annotate_event();
   }
 }
 
@@ -896,7 +889,15 @@ static bool print_base64(PRINT_EVENT_INFO *print_event_info, Log_event *ev)
 }
 
 
-static bool print_row_event(PRINT_EVENT_INFO *print_event_info, Log_event *ev,
+static void delete_events(DYNAMIC_ARRAY* events)
+{
+  for (uint i= events->elements; i--; )
+    delete *dynamic_element(events, i, Log_event**);
+  reset_dynamic(events);
+}
+
+
+static bool print_row_event(PRINT_EVENT_INFO *print_event_info, Log_event *&ev,
                             ulonglong table_id, bool is_stmt_end)
 {
   Table_map_log_event *ignored_map= 
@@ -971,38 +972,35 @@ static bool print_row_event(PRINT_EVENT_INFO *print_event_info, Log_event *ev,
 
   /* skip the event check */
   if (skip_event)
-    return 0;
+  {
+    delete ev;
+    ev= NULL;
+    return false;
+  }
 
   if (!opt_flashback)
-    return print_base64(print_event_info, ev);
-  else
   {
-    if (is_stmt_end)
-    {
-      bool res= false;
-      Log_event *e= NULL;
+    bool res= print_base64(print_event_info, ev);
+    delete ev;
+    ev= NULL;
+    return res;
+  }
 
-      // Print the row_event from the last one to the first one
-      for (uint i= events_in_stmt.elements; i > 0; --i)
-      {
-        e= *(dynamic_element(&events_in_stmt, i - 1, Log_event**));
-        res= res || print_base64(print_event_info, e);
-      }
-      // Copy all output into the Log_event
-      ev->output_buf.copy(e->output_buf);
-      // Delete Log_event
-      for (uint i= 0; i < events_in_stmt.elements-1; ++i)
-      {
-        e= *(dynamic_element(&events_in_stmt, i, Log_event**));
-        delete e;
-      }
-      reset_dynamic(&events_in_stmt);
+  if (!is_stmt_end)
+    return false;
 
-      return res;
-    }
+  Log_event *e= NULL;
+  bool res= false;
+  // Print the row_event from the last one to the first one
+  for (uint i= events_in_stmt.elements; i > 0; --i)
+  {
+    e= *(dynamic_element(&events_in_stmt, i - 1, Log_event**));
+    res= res || print_base64(print_event_info, e);
   }
-
-  return 0;
+  // Copy all output into the Log_event
+  ev->output_buf.copy(e->output_buf);
+  delete_events(&events_in_stmt);
+  return res;
 }
 
 
@@ -1026,12 +1024,12 @@ static bool print_row_event(PRINT_EVENT_INFO *print_event_info, Log_event *ev,
   @retval OK_STOP No error, but the end of the specified range of
   events to process has been reached and the program should terminate.
 */
+static
 Exit_status process_event(PRINT_EVENT_INFO *print_event_info, Log_event *ev,
                           my_off_t pos, const char *logname)
 {
   char ll_buff[21];
   Log_event_type ev_type= ev->get_type_code();
-  my_bool destroy_evt= TRUE;
   DBUG_ENTER("process_event");
   print_event_info->short_form= short_form;
   Exit_status retval= OK_CONTINUE;
@@ -1299,7 +1297,7 @@ Exit_status process_event(PRINT_EVENT_INFO *print_event_info, Log_event *ev,
           away Table map or the last rbr will be processed.
         */
         keep_annotate_event((Annotate_rows_log_event*) ev);
-        destroy_evt= FALSE;
+        ev= NULL;
       }
       break;
     case TABLE_MAP_EVENT:
@@ -1309,7 +1307,7 @@ Exit_status process_event(PRINT_EVENT_INFO *print_event_info, Log_event *ev,
           shall_skip_table(map->get_table_name()))
       {
         print_event_info->m_table_map_ignored.set_table(map->get_table_id(), map);
-        destroy_evt= FALSE;
+        ev= NULL;
         goto end;
       }
 #ifdef WHEN_FLASHBACK_REVIEW_READY
@@ -1463,10 +1461,10 @@ Exit_status process_event(PRINT_EVENT_INFO *print_event_info, Log_event *ev,
       Rows_log_event *e= (Rows_log_event*) ev;
       bool is_stmt_end= e->get_flags(Rows_log_event::STMT_END_F);
       if (print_row_event(print_event_info, ev, e->get_table_id(),
-                          e->get_flags(Rows_log_event::STMT_END_F)))
+                          is_stmt_end))
         goto err;
       if (!is_stmt_end)
-        destroy_evt= FALSE;
+        ev= NULL;
       break;
     }
     case PRE_GA_WRITE_ROWS_EVENT:
@@ -1476,10 +1474,10 @@ Exit_status process_event(PRINT_EVENT_INFO *print_event_info, Log_event *ev,
       Old_rows_log_event *e= (Old_rows_log_event*) ev;
       bool is_stmt_end= e->get_flags(Rows_log_event::STMT_END_F);
       if (print_row_event(print_event_info, ev, e->get_table_id(),
-                          e->get_flags(Old_rows_log_event::STMT_END_F)))
+                          is_stmt_end))
         goto err;
       if (!is_stmt_end)
-        destroy_evt= FALSE;
+        ev= NULL;
       break;
     }
     case START_ENCRYPTION_EVENT:
@@ -1539,10 +1537,7 @@ Exit_status process_event(PRINT_EVENT_INFO *print_event_info, Log_event *ev,
       }
     }
 
-    if (remote_opt)
-      ev->temp_buf= 0;
-    if (destroy_evt) /* destroy it later if not set (ignored table map) */
-      delete ev;
+    delete ev;
   }
   DBUG_RETURN(retval);
 }
@@ -1854,7 +1849,14 @@ static void cleanup()
   my_free(stop_datetime_str);
 
   delete binlog_filter;
+  binlog_filter= NULL;
   delete glob_description_event;
+  glob_description_event= NULL;
+  free_annotate_event();
+  delete_events(&binlog_events);
+  delete_dynamic(&binlog_events);
+  delete_events(&events_in_stmt);
+  delete_dynamic(&events_in_stmt);
   if (mysql)
     mysql_close(mysql);
 }
@@ -2909,9 +2911,15 @@ static Exit_status dump_local_log_entries(PRINT_EVENT_INFO *print_event_info,
     char llbuff[21];
     my_off_t old_off = my_b_tell(file);
 
-    Log_event* ev = Log_event::read_log_event(file, 0, glob_description_event,
-                                              opt_verify_binlog_checksum);
-    if (!ev)
+    if (Log_event* ev= Log_event::read_log_event(file, 0,
+                                                 glob_description_event,
+                                                 opt_verify_binlog_checksum))
+    {
+      retval= process_event(print_event_info, ev, old_off, logname);
+      if (retval == OK_CONTINUE)
+        continue;
+    }
+    else
     {
       /*
         if binlog wasn't closed properly ("in use" flag is set) don't complain
@@ -2927,11 +2935,8 @@ static Exit_status dump_local_log_entries(PRINT_EVENT_INFO *print_event_info,
         goto err;
       }
       // file->error == 0 means EOF, that's OK, we break in this case
-      goto end;
     }
-    if ((retval= process_event(print_event_info, ev, old_off, logname)) !=
-        OK_CONTINUE)
-      goto end;
+    goto end;
   }
 
   /* NOTREACHED */
@@ -2948,6 +2953,8 @@ static Exit_status dump_local_log_entries(PRINT_EVENT_INFO *print_event_info,
    */
   if (end_io_cache(file))
     retval= ERROR_STOP;
+  delete glob_description_event;
+  glob_description_event= NULL;
 
   return retval;
 }
@@ -3117,8 +3124,6 @@ int main(int argc, char** argv)
       my_free(event_str->str);
     }
     fprintf(result_file, "COMMIT\n/*!*/;\n");
-    delete_dynamic(&binlog_events);
-    delete_dynamic(&events_in_stmt);
   }
 
   /* Set delimiter back to semicolon */
@@ -3153,7 +3158,6 @@ int main(int argc, char** argv)
   if (result_file && result_file != stdout)
     my_fclose(result_file, MYF(0));
   cleanup();
-  free_annotate_event();
   free_defaults(defaults_argv);
   my_free_open_file_info();
   load_processor.destroy();
