repo: adamvr/mosquitto-homebrew-mods

decompiled_code:
__int64 __fastcall mqtt3_bridge_packet_cleanup(__int64 a1)
{
__int64 result; // rax

if ( a1 )
{
while ( *(_QWORD *)(a1 + 104) )
{
((void (*)(void))mosquitto_packet_cleanup)();
*(_QWORD *)(a1 + 104) = *(_QWORD *)(*(_QWORD *)(a1 + 104) + 32LL);
mosquitto_free();
}
result = mosquitto_packet_cleanup(a1 + 64);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
__int64 result; // rax

if ( a1 )
{
while ( *(_QWORD *)(a1 + 104) )
{
((void (*)(void))mosquitto_packet_cleanup)();
*(_QWORD *)(a1 + 104) = *(_QWORD *)(*(_QWORD *)(a1 + 104) + 32LL);
mosquitto_free();
}
result = mosquitto_packet_cleanup(a1 + 64);
}
return result;
}

original_code:
void mqtt3_bridge_packet_cleanup(mqtt3_context *context)
{
 struct _mosquitto_packet *packet;
 if(!context) return;
    while(context->core.out_packet){
  _mosquitto_packet_cleanup(context->core.out_packet);
  packet = context->core.out_packet;
  context->core.out_packet = context->core.out_packet->next;
  _mosquitto_free(packet);
 }
 _mosquitto_packet_cleanup(&(context->core.in_packet));
}

canonical_original_code:
void mqtt3_bridge_packet_cleanup(struct _mqtt3_context *context)
{
 struct _mosquitto_packet *packet;
 if(!context) return;
    while(context->core.out_packet){
  _mosquitto_packet_cleanup(context->core.out_packet);
  packet = context->core.out_packet;
  context->core.out_packet = context->core.out_packet->next;
  _mosquitto_free(packet);
 }
 _mosquitto_packet_cleanup(&(context->core.in_packet));
}

variable_types: struct _mqtt3_context { struct _mosquitto_core { int sock; char *id; char *username; char *password; unsigned short keepalive; enum mosquitto_client_state { mosq_cs_new=0, mosq_cs_connected=1, mosq_cs_disconnecting=2 } state; long last_msg_in; long last_msg_out; unsigned short last_mid; struct _mosquitto_packet { unsigned char command; unsigned char command_saved; unsigned char have_remaining; unsigned char remaining_count; unsigned short mid; unsigned int remaining_mult; unsigned int remaining_length; unsigned int to_process; unsigned int pos; unsigned char *payload; struct _mosquitto_packet *next; } in_packet; struct _mosquitto_packet *out_packet; struct mosquitto_message { unsigned short mid; char *topic; unsigned char *payload; unsigned int payloadlen; int qos; _Bool retain; } *will; } core; _Bool clean_session; _Bool duplicate; char *address; struct _mqtt3_bridge { char *name; char *address; unsigned short port; int keepalive; _Bool clean_session; struct _mqtt3_bridge_topic { char *topic; enum mqtt3_bridge_direction { bd_out=0, bd_in=1, bd_both=2 } direction; } *topics; int topic_count; long restart_t; char *username; char *password; } *bridge; struct _mosquitto_client_msg { struct _mosquitto_client_msg *next; struct mosquitto_msg_store { struct mosquitto_msg_store *next; unsigned long db_id; int ref_count; char *source_id; unsigned short source_mid; struct mosquitto_message msg; } *store; unsigned short mid; int qos; _Bool retain; long timestamp; enum mosquitto_msg_direction { mosq_md_in=0, mosq_md_out=1 } direction; enum mqtt3_msg_state { ms_invalid=0, ms_publish=1, ms_publish_puback=2, ms_wait_puback=3, ms_publish_pubrec=4, ms_wait_pubrec=5, ms_resend_pubrel=6, ms_wait_pubrel=7, ms_resend_pubcomp=8, ms_wait_pubcomp=9, ms_resend_pubrec=10, ms_queued=11 } state; _Bool dup; } *msgs; } *

variable_types: struct _mosquitto_packet { unsigned char command; unsigned char command_saved; unsigned char have_remaining; unsigned char remaining_count; unsigned short mid; unsigned int remaining_mult; unsigned int remaining_length; unsigned int to_process; unsigned int pos; unsigned char *payload; struct _mosquitto_packet *next; } *

user_defined_types: struct _mqtt3_context { struct _mosquitto_core core; _Bool clean_session; _Bool duplicate; char *address; struct _mqtt3_bridge *bridge; struct _mosquitto_client_msg *msgs; }

user_defined_types: struct _mosquitto_core { int sock; char *id; char *username; char *password; unsigned short keepalive; enum mosquitto_client_state state; long last_msg_in; long last_msg_out; unsigned short last_mid; struct _mosquitto_packet in_packet; struct _mosquitto_packet *out_packet; struct mosquitto_message *will; }

user_defined_types: struct _mqtt3_bridge { char *name; char *address; unsigned short port; int keepalive; _Bool clean_session; struct _mqtt3_bridge_topic *topics; int topic_count; long restart_t; char *username; char *password; }

user_defined_types: struct _mosquitto_client_msg { struct _mosquitto_client_msg *next; struct mosquitto_msg_store *store; unsigned short mid; int qos; _Bool retain; long timestamp; enum mosquitto_msg_direction direction; enum mqtt3_msg_state state; _Bool dup; }

user_defined_types: struct _mqtt3_bridge_topic { char *topic; enum mqtt3_bridge_direction direction; }


original_code:
int mqtt3_bridge_connect(mosquitto_db *db, mqtt3_context *context)
{
 int new_sock = -1;
 int i;
 if(!context || !context->bridge) return 1;
 context->core.state = mosq_cs_new;
 context->duplicate = 0;
 context->core.sock = -1;
 context->core.last_msg_in = time(((void *)0));
 context->core.last_msg_out = time(((void *)0));
 context->core.keepalive = context->bridge->keepalive;
 context->clean_session = context->bridge->clean_session;
 context->core.in_packet.payload = ((void *)0);
 mqtt3_bridge_packet_cleanup(context);
 mqtt3_log_printf(0x02, "Connecting bridge %s", context->bridge->name);
 new_sock = _mosquitto_socket_connect(context->bridge->address, context->bridge->port);
 if(new_sock == -1){
  mqtt3_log_printf(0x08, "Error creating bridge.");
  return 1;
 }
 context->core.sock = new_sock;
 context->core.last_msg_in = time(((void *)0));
 if(mqtt3_raw_connect(context, context->core.id,
            0, 0, 0, ((void *)0), ((void *)0),
   context->core.keepalive, context->clean_session)){
  return 1;
 }
 for(i=0; i<context->bridge->topic_count; i++){
  if(context->bridge->topics[i].direction == bd_out || context->bridge->topics[i].direction == bd_both){
   if(mqtt3_sub_add(context, context->bridge->topics[i].topic, 2, &db->subs)) return 1;
  }
 }
 return 0;
}

canonical_original_code:
int mqtt3_bridge_connect(struct _mosquitto_db *db, struct _mqtt3_context *context)
{
 int new_sock = -1;
 int i;
 if(!context || !context->bridge) return 1;
 context->core.state = mosq_cs_new;
 context->duplicate = 0;
 context->core.sock = -1;
 context->core.last_msg_in = time(((void *)0));
 context->core.last_msg_out = time(((void *)0));
 context->core.keepalive = context->bridge->keepalive;
 context->clean_session = context->bridge->clean_session;
 context->core.in_packet.payload = ((void *)0);
 mqtt3_bridge_packet_cleanup(context);
 mqtt3_log_printf(0x02, "Connecting bridge %s", context->bridge->name);
 new_sock = _mosquitto_socket_connect(context->bridge->address, context->bridge->port);
 if(new_sock == -1){
  mqtt3_log_printf(0x08, "Error creating bridge.");
  return 1;
 }
 context->core.sock = new_sock;
 context->core.last_msg_in = time(((void *)0));
 if(mqtt3_raw_connect(context, context->core.id,
            0, 0, 0, ((void *)0), ((void *)0),
   context->core.keepalive, context->clean_session)){
  return 1;
 }
 for(i=0; i<context->bridge->topic_count; i++){
  if(context->bridge->topics[i].direction == bd_out || context->bridge->topics[i].direction == bd_both){
   if(mqtt3_sub_add(context, context->bridge->topics[i].topic, 2, &db->subs)) return 1;
  }
 }
 return 0;
}

variable_types: struct _mosquitto_db { unsigned long last_db_id; struct _mosquitto_subhier { struct _mosquitto_subhier *children; struct _mosquitto_subhier *next; struct _mosquitto_subleaf { struct _mosquitto_subleaf *prev; struct _mosquitto_subleaf *next; struct _mqtt3_context { struct _mosquitto_core { int sock; char *id; char *username; char *password; unsigned short keepalive; enum mosquitto_client_state { mosq_cs_new=0, mosq_cs_connected=1, mosq_cs_disconnecting=2 } state; long last_msg_in; long last_msg_out; unsigned short last_mid; struct _mosquitto_packet { unsigned char command; unsigned char command_saved; unsigned char have_remaining; unsigned char remaining_count; unsigned short mid; unsigned int remaining_mult; unsigned int remaining_length; unsigned int to_process; unsigned int pos; unsigned char *payload; struct _mosquitto_packet *next; } in_packet; struct _mosquitto_packet *out_packet; struct mosquitto_message { unsigned short mid; char *topic; unsigned char *payload; unsigned int payloadlen; int qos; _Bool retain; } *will; } core; _Bool clean_session; _Bool duplicate; char *address; struct _mqtt3_bridge { char *name; char *address; unsigned short port; int keepalive; _Bool clean_session; struct _mqtt3_bridge_topic { char *topic; enum mqtt3_bridge_direction { bd_out=0, bd_in=1, bd_both=2 } direction; } *topics; int topic_count; long restart_t; char *username; char *password; } *bridge; struct _mosquitto_client_msg { struct _mosquitto_client_msg *next; struct mosquitto_msg_store { struct mosquitto_msg_store *next; unsigned long db_id; int ref_count; char *source_id; unsigned short source_mid; struct mosquitto_message msg; } *store; unsigned short mid; int qos; _Bool retain; long timestamp; enum mosquitto_msg_direction { mosq_md_in=0, mosq_md_out=1 } direction; enum mqtt3_msg_state { ms_invalid=0, ms_publish=1, ms_publish_puback=2, ms_wait_puback=3, ms_publish_pubrec=4, ms_wait_pubrec=5, ms_resend_pubrel=6, ms_wait_pubrel=7, ms_resend_pubcomp=8, ms_wait_pubcomp=9, ms_resend_pubrec=10, ms_queued=11 } state; _Bool dup; } *msgs; } *context; int qos; } *subs; char *topic; struct mosquitto_msg_store *retained; } subs; struct _mqtt3_context **contexts; int context_count; struct mosquitto_msg_store *msg_store; int msg_store_count; char *filepath; } *

variable_types:
context
struct _mqtt3_context *

variable_types: i int

return_type: int

user_defined_types: struct _mosquitto_db { unsigned long last_db_id; struct _mosquitto_subhier subs; struct _mqtt3_context **contexts; int context_count; struct mosquitto_msg_store *msg_store; int msg_store_count; char *filepath; }

user_defined_types: struct _mosquitto_subhier { struct _mosquitto_subhier *children; struct _mosquitto_subhier *next; struct _mosquitto_subleaf *subs; char *topic; struct mosquitto_msg_store *retained; }

user_defined_types: struct _mosquitto_subleaf { struct _mosquitto_subleaf *prev; struct _mosquitto_subleaf *next; struct _mqtt3_context *context; int qos; }


original_code:
int mqtt3_bridge_new(mosquitto_db *db, struct _mqtt3_bridge *bridge)
{
 int i;
 mqtt3_context *new_context = ((void *)0);
 mqtt3_context **tmp_contexts;
 ((void) sizeof ((db) ? 1 : 0), __extension__ ({ if (db) ; else __assert_fail ("db", "bridge.c", 44, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof ((bridge) ? 1 : 0), __extension__ ({ if (bridge) ; else __assert_fail ("bridge", "bridge.c", 45, __extension__ __PRETTY_FUNCTION__); }));
 new_context = mqtt3_context_init(-1);
 if(!new_context){
  return 1;
 }
 new_context->bridge = bridge;
 for(i=0; i<db->context_count; i++){
  if(db->contexts[i] == ((void *)0)){
   db->contexts[i] = new_context;
   break;
  }
 }
 if(i==db->context_count){
  db->context_count++;
  tmp_contexts = _mosquitto_realloc(db->contexts, sizeof(mqtt3_context*)*db->context_count);
  if(tmp_contexts){
   db->contexts = tmp_contexts;
   db->contexts[db->context_count-1] = new_context;
  }else{
   return 1;
  }
 }
 new_context->core.id = _mosquitto_strdup(bridge->name);
 if(!new_context->core.id){
  return 1;
 }
 new_context->core.username = new_context->bridge->username;
 new_context->core.password = new_context->bridge->password;
 return mqtt3_bridge_connect(db, new_context);
}

canonical_original_code:
int mqtt3_bridge_new(struct _mosquitto_db *db, struct _mqtt3_bridge *bridge)
{
 int i;
 struct _mqtt3_context *new_context = ((void *)0);
 struct _mqtt3_context **tmp_contexts;
 ((void) sizeof ((db) ? 1 : 0), __extension__ ({ if (db) ; else __assert_fail ("db", "bridge.c", 44, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof ((bridge) ? 1 : 0), __extension__ ({ if (bridge) ; else __assert_fail ("bridge", "bridge.c", 45, __extension__ __PRETTY_FUNCTION__); }));
 new_context = mqtt3_context_init(-1);
 if(!new_context){
  return 1;
 }
 new_context->bridge = bridge;
 for(i=0; i<db->context_count; i++){
  if(db->contexts[i] == ((void *)0)){
   db->contexts[i] = new_context;
   break;
  }
 }
 if(i==db->context_count){
  db->context_count++;
  tmp_contexts = _mosquitto_realloc(db->contexts, sizeof(struct _mqtt3_context *)*db->context_count);
  if(tmp_contexts){
   db->contexts = tmp_contexts;
   db->contexts[db->context_count-1] = new_context;
  }else{
   return 1;
  }
 }
 new_context->core.id = _mosquitto_strdup(bridge->name);
 if(!new_context->core.id){
  return 1;
 }
 new_context->core.username = new_context->bridge->username;
 new_context->core.password = new_context->bridge->password;
 return mqtt3_bridge_connect(db, new_context);
}

variable_types:
bridge
struct _mqtt3_bridge *

variable_types: struct _mqtt3_context { struct _mosquitto_core { int sock; char *id; char *username; char *password; unsigned short keepalive; enum mosquitto_client_state { mosq_cs_new=0, mosq_cs_connected=1, mosq_cs_disconnecting=2 } state; long last_msg_in; long last_msg_out; unsigned short last_mid; struct _mosquitto_packet { unsigned char command; unsigned char command_saved; unsigned char have_remaining; unsigned char remaining_count; unsigned short mid; unsigned int remaining_mult; unsigned int remaining_length; unsigned int to_process; unsigned int pos; unsigned char *payload; struct _mosquitto_packet *next; } in_packet; struct _mosquitto_packet *out_packet; struct mosquitto_message { unsigned short mid; char *topic; unsigned char *payload; unsigned int payloadlen; int qos; _Bool retain; } *will; } core; _Bool clean_session; _Bool duplicate; char *address; struct _mqtt3_bridge { char *name; char *address; unsigned short port; int keepalive; _Bool clean_session; struct _mqtt3_bridge_topic { char *topic; enum mqtt3_bridge_direction { bd_out=0, bd_in=1, bd_both=2 } direction; } *topics; int topic_count; long restart_t; char *username; char *password; } *bridge; struct _mosquitto_client_msg { struct _mosquitto_client_msg *next; struct mosquitto_msg_store { struct mosquitto_msg_store *next; unsigned long db_id; int ref_count; char *source_id; unsigned short source_mid; struct mosquitto_message msg; } *store; unsigned short mid; int qos; _Bool retain; long timestamp; enum mosquitto_msg_direction { mosq_md_in=0, mosq_md_out=1 } direction; enum mqtt3_msg_state { ms_invalid=0, ms_publish=1, ms_publish_puback=2, ms_wait_puback=3, ms_publish_pubrec=4, ms_wait_pubrec=5, ms_resend_pubrel=6, ms_wait_pubrel=7, ms_resend_pubcomp=8, ms_wait_pubcomp=9, ms_resend_pubrec=10, ms_queued=11 } state; _Bool dup; } *msgs; } **


decompiled_code:
__int64 __fastcall mosquitto_message_copy(__int64 a1, __int64 a2)
{
__int64 result; // rax
__int64 v4; // rax
int v5; // edi
void *v6; // rax

if ( !a1 )
return 3LL;
result = 3LL;
if ( a2 )
{
*(_WORD *)a1 = *(_WORD *)a2;
v4 = mosquitto_strdup(*(_QWORD *)(a2 + 8));
*(_QWORD *)(a1 + 8) = v4;
if ( v4 )
{
v5 = *(_DWORD *)(a2 + 24);
*(_DWORD *)(a1 + 28) = *(_DWORD *)(a2 + 28);
*(_BYTE *)(a1 + 32) = *(_BYTE *)(a2 + 32);
if ( v5 )
{
v6 = (void *)mosquitto_malloc();
*(_QWORD *)(a1 + 16) = v6;
if ( v6 )
{
memcpy(v6, *(const void **)(a2 + 16), *(unsigned int *)(a2 + 24));
*(_DWORD *)(a1 + 24) = *(_DWORD *)(a2 + 24);
result = 0LL;
}
else
{
mosquitto_free(*(_QWORD *)(a1 + 8));
result = 1LL;
}
}
else
{
*(_DWORD *)(a1 + 24) = 0;
result = 0LL;
*(_QWORD *)(a1 + 16) = 0LL;
}
}
else
{
result = 1LL;
}
}
return result;
}

original_code:
int mosquitto_message_copy(struct mosquitto_message *dst, const struct mosquitto_message *src)
{
 if(!dst || !src) return 3;
 dst->mid = src->mid;
 dst->topic = _mosquitto_strdup(src->topic);
 if(!dst->topic) return 1;
 dst->qos = src->qos;
 dst->retain = src->retain;
 if(src->payloadlen){
  dst->payload = _mosquitto_malloc(src->payloadlen);
  if(!dst->payload){
   _mosquitto_free(dst->topic);
   return 1;
  }
  memcpy(dst->payload, src->payload, src->payloadlen);
  dst->payloadlen = src->payloadlen;
 }else{
  dst->payloadlen = 0;
  dst->payload = ((void *)0);
 }
 return 0;
}

canonical_original_code:
int mosquitto_message_copy(struct mosquitto_message *dst, struct mosquitto_message *src)
{
 if(!dst || !src) return 3;
 dst->mid = src->mid;
 dst->topic = _mosquitto_strdup(src->topic);
 if(!dst->topic) return 1;
 dst->qos = src->qos;
 dst->retain = src->retain;
 if(src->payloadlen){
  dst->payload = _mosquitto_malloc(src->payloadlen);
  if(!dst->payload){
   _mosquitto_free(dst->topic);
   return 1;
  }
  memcpy(dst->payload, src->payload, src->payloadlen);
  dst->payloadlen = src->payloadlen;
 }else{
  dst->payloadlen = 0;
  dst->payload = ((void *)0);
 }
 return 0;
}

variable_types: struct mosquitto_message { unsigned short mid; char *topic; unsigned char *payload; unsigned int payloadlen; int qos; _Bool retain; } *

variable_types:
src
struct mosquitto_message *


decompiled_code:
__int64 __fastcall mosquitto_message_free(__int64 *a1)
{
__int64 v1; // rbp
__int64 v2; // rdi
__int64 v3; // rdi
__int64 result; // rax

if ( a1 )
{
v1 = *a1;
if ( *a1 )
{
v2 = *(_QWORD *)(v1 + 8);
if ( v2 )
mosquitto_free(v2);
v3 = *(_QWORD *)(v1 + 16);
if ( v3 )
mosquitto_free(v3);
result = mosquitto_free(v1);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 *a1)
{
__int64 v1; // rbp
__int64 v2; // rdi
__int64 v3; // rdi
__int64 result; // rax

if ( a1 )
{
v1 = *a1;
if ( *a1 )
{
v2 = *(_QWORD *)(v1 + 8);
if ( v2 )
mosquitto_free(v2);
v3 = *(_QWORD *)(v1 + 16);
if ( v3 )
mosquitto_free(v3);
result = mosquitto_free(v1);
}
}
return result;
}

original_code:
void mosquitto_message_free(struct mosquitto_message **message)
{
 struct mosquitto_message *msg;
 if(!message || !*message) return;
 msg = *message;
 if(msg->topic) _mosquitto_free(msg->topic);
 if(msg->payload) _mosquitto_free(msg->payload);
 _mosquitto_free(msg);
}

variable_types: struct mosquitto_message { unsigned short mid; char *topic; unsigned char *payload; unsigned int payloadlen; int qos; _Bool retain; } **

return_type: void


decompiled_code:
void __fastcall mosquitto_message_retry_set(__int64 a1, int a2)
{
if ( !a1 )
_assert_fail("mosq", "messages_mosq.c", 0xC0u, "mosquitto_message_retry_set");
*(_DWORD *)(a1 + 128) = a2;
}

canonical_decompiled_code:
void __fastcall func7(__int64 a1, int a2)
{
if ( !a1 )
_assert_fail("mosq", "messages_mosq.c", 0xC0u, "mosquitto_message_retry_set");
*(_DWORD *)(a1 + 128) = a2;
}

original_code:
void mosquitto_message_retry_set(struct mosquitto *mosq, unsigned int message_retry)
{
 ((void) sizeof ((mosq) ? 1 : 0), __extension__ ({ if (mosq) ; else __assert_fail ("mosq", "messages_mosq.c", 192, __extension__ __PRETTY_FUNCTION__); }));
 if(mosq) mosq->message_retry = message_retry;
}

variable_types: struct mosquitto { struct _mosquitto_core { int sock; char *id; char *username; char *password; unsigned short keepalive; enum mosquitto_client_state { mosq_cs_new=0, mosq_cs_connected=1, mosq_cs_disconnecting=2 } state; long last_msg_in; long last_msg_out; unsigned short last_mid; struct _mosquitto_packet { unsigned char command; unsigned char command_saved; unsigned char have_remaining; unsigned char remaining_count; unsigned short mid; unsigned int remaining_mult; unsigned int remaining_length; unsigned int to_process; unsigned int pos; unsigned char *payload; struct _mosquitto_packet *next; } in_packet; struct _mosquitto_packet *out_packet; struct mosquitto_message { unsigned short mid; char *topic; unsigned char *payload; unsigned int payloadlen; int qos; _Bool retain; } *will; } core; void *obj; unsigned int message_retry; long last_retry_check; struct mosquitto_message_all { struct mosquitto_message_all *next; long timestamp; enum mosquitto_msg_direction { mosq_md_in=0, mosq_md_out=1 } direction; enum mosquitto_msg_state { mosq_ms_invalid=0, mosq_ms_wait_puback=1, mosq_ms_wait_pubrec=2, mosq_ms_wait_pubrel=3, mosq_ms_wait_pubcomp=4 } state; _Bool dup; struct mosquitto_message msg; } *messages; int log_priorities; int log_destinations; void (*on_connect)(void *obj, int rc); void (*on_disconnect)(void *obj); void (*on_publish)(void *obj, unsigned short mid); void (*on_message)(void *obj, struct mosquitto_message *message); void (*on_subscribe)(void *obj, unsigned short mid, int qos_count, unsigned char *granted_qos); void (*on_unsubscribe)(void *obj, unsigned short mid); } *

user_defined_types: struct mosquitto { struct _mosquitto_core core; void *obj; unsigned int message_retry; long last_retry_check; struct mosquitto_message_all *messages; int log_priorities; int log_destinations; void (*on_connect)(void *obj, int rc); void (*on_disconnect)(void *obj); void (*on_publish)(void *obj, unsigned short mid); void (*on_message)(void *obj, struct mosquitto_message *message); void (*on_subscribe)(void *obj, unsigned short mid, int qos_count, unsigned char *granted_qos); void (*on_unsubscribe)(void *obj, unsigned short mid); }

user_defined_types: struct mosquitto_message_all { struct mosquitto_message_all *next; long timestamp; enum mosquitto_msg_direction direction; enum mosquitto_msg_state state; _Bool dup; struct mosquitto_message msg; }


decompiled_code:
__int64 __fastcall mqtt3_raw_publish(__int64 a1, unsigned int a2, unsigned __int8 a3, unsigned __int8 a4, unsigned __int16 a5, const char *a6, unsigned int a7, __int64 a8)
{
int v11; // eax
unsigned int v12; // edx
unsigned int v13; // eax
__int64 v14; // rax
__int64 v15; // rcx
__int64 v16; // r12
__int64 v17; // rax
unsigned __int16 v18; // ax
__int64 result; // rax
unsigned int v20; // [rsp+0h] [rbp-44h]
unsigned int v21; // [rsp+4h] [rbp-40h]
unsigned int v22; // [rsp+8h] [rbp-3Ch]

if ( !a1 )
return 1LL;
if ( *(_DWORD *)a1 == -1 || !a6 )
return 1LL;
v22 = a5;
v21 = a3;
mqtt3_log_printf(
16LL,
"Sending PUBLISH to %s (d%d, q%d, r%d, m%d, '%s', ... (%ld bytes))",
*(const char **)(a1 + 8),
a2,
a3,
a4,
a5,
a6,
a7);
v11 = a7 + strlen(a6);
v12 = v11 + 2;
v13 = v11 + 4;
if ( a3 )
v12 = v13;
v20 = v12;
v14 = mosquitto_calloc(1LL, 40LL);
v16 = v14;
if ( v14 )
{
*(_DWORD *)(v14 + 12) = v20;
*(_BYTE *)v14 = (2 * v21) | a4 | (8 * a2) & 8 | 0x30;
v17 = mosquitto_malloc((int)v20);
*(_QWORD *)(v16 + 24) = v17;
if ( v17 )
{
v18 = strlen(a6);
mosquitto_write_string(v16, a6, v18);
if ( a3 )
mosquitto_write_uint16(v16, v22);
if ( a7 )
mosquitto_write_bytes(v16, a8, a7);
result = mqtt3_net_packet_queue(a1, v16);
if ( (_DWORD)result )
{
mqtt3_log_printf(16LL, "PUBLISH failed queuing packet.");
mosquitto_free(v16);
result = 1LL;
}
}
else
{
mqtt3_log_printf(16LL, "PUBLISH failed allocating payload memory.");
mosquitto_free(v16);
result = 1LL;
}
}
else
{
mqtt3_log_printf(16LL, "PUBLISH failed allocating packet memory.", v20, v15, v21);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, unsigned int a2, unsigned __int8 a3, unsigned __int8 a4, unsigned __int16 a5, const char *a6, unsigned int a7, __int64 a8)
{
int v11; // eax
unsigned int v12; // edx
unsigned int v13; // eax
__int64 v14; // rax
__int64 v15; // rcx
__int64 v16; // r12
__int64 v17; // rax
unsigned __int16 v18; // ax
__int64 result; // rax
unsigned int v20; // [rsp+0h] [rbp-44h]
unsigned int v21; // [rsp+4h] [rbp-40h]
unsigned int v22; // [rsp+8h] [rbp-3Ch]

if ( !a1 )
return 1LL;
if ( *(_DWORD *)a1 == -1 || !a6 )
return 1LL;
v22 = a5;
v21 = a3;
mqtt3_log_printf(
16LL,
"Sending PUBLISH to %s (d%d, q%d, r%d, m%d, '%s', ... (%ld bytes))",
*(const char **)(a1 + 8),
a2,
a3,
a4,
a5,
a6,
a7);
v11 = a7 + strlen(a6);
v12 = v11 + 2;
v13 = v11 + 4;
if ( a3 )
v12 = v13;
v20 = v12;
v14 = mosquitto_calloc(1LL, 40LL);
v16 = v14;
if ( v14 )
{
*(_DWORD *)(v14 + 12) = v20;
*(_BYTE *)v14 = (2 * v21) | a4 | (8 * a2) & 8 | 0x30;
v17 = mosquitto_malloc((int)v20);
*(_QWORD *)(v16 + 24) = v17;
if ( v17 )
{
v18 = strlen(a6);
mosquitto_write_string(v16, a6, v18);
if ( a3 )
mosquitto_write_uint16(v16, v22);
if ( a7 )
mosquitto_write_bytes(v16, a8, a7);
result = mqtt3_net_packet_queue(a1, v16);
if ( (_DWORD)result )
{
mqtt3_log_printf(16LL, "PUBLISH failed queuing packet.");
mosquitto_free(v16);
result = 1LL;
}
}
else
{
mqtt3_log_printf(16LL, "PUBLISH failed allocating payload memory.");
mosquitto_free(v16);
result = 1LL;
}
}
else
{
mqtt3_log_printf(16LL, "PUBLISH failed allocating packet memory.", v20, v15, v21);
result = 1LL;
}
return result;
}

original_code:
int mqtt3_raw_publish(mqtt3_context *context, int dup, uint8_t qos, _Bool retain, uint16_t mid, const char *topic, uint32_t payloadlen, const uint8_t *payload)
{
 struct _mosquitto_packet *packet = ((void *)0);
 int packetlen;
 if(!context || context->core.sock == -1 || !topic) return 1;
 if(context) mqtt3_log_printf(0x10, "Sending PUBLISH to %s (d%d, q%d, r%d, m%d, '%s', ... (%ld bytes))", context->core.id, dup, qos, retain, mid, topic, (long)payloadlen);
 packetlen = 2+strlen(topic) + payloadlen;
 if(qos > 0) packetlen += 2;
 packet = _mosquitto_calloc(1, sizeof(struct _mosquitto_packet));
 if(!packet){
  mqtt3_log_printf(0x10, "PUBLISH failed allocating packet memory.");
  return 1;
 }
 packet->command = 0x30 | ((dup&0x1)<<3) | (qos<<1) | retain;
 packet->remaining_length = packetlen;
 packet->payload = _mosquitto_malloc(sizeof(uint8_t)*packetlen);
 if(!packet->payload){
  mqtt3_log_printf(0x10, "PUBLISH failed allocating payload memory.");
  _mosquitto_free(packet);
  return 1;
 }
 _mosquitto_write_string(packet, topic, strlen(topic));
 if(qos > 0){
  _mosquitto_write_uint16(packet, mid);
 }
 if(payloadlen){
  _mosquitto_write_bytes(packet, payload, payloadlen);
 }
 if(mqtt3_net_packet_queue(context, packet)){
  mqtt3_log_printf(0x10, "PUBLISH failed queuing packet.");
  _mosquitto_free(packet);
  return 1;
 }
 return 0;
}

canonical_original_code:
int mqtt3_raw_publish(struct _mqtt3_context *context, int dup, unsigned char qos, _Bool retain, unsigned short mid, char *topic, unsigned int payloadlen, unsigned char *payload)
{
 struct _mosquitto_packet *packet = ((void *)0);
 int packetlen;
 if(!context || context->core.sock == -1 || !topic) return 1;
 if(context) mqtt3_log_printf(0x10, "Sending PUBLISH to %s (d%d, q%d, r%d, m%d, '%s', ... (%ld bytes))", context->core.id, dup, qos, retain, mid, topic, (long)payloadlen);
 packetlen = 2+strlen(topic) + payloadlen;
 if(qos > 0) packetlen += 2;
 packet = _mosquitto_calloc(1, sizeof(struct _mosquitto_packet));
 if(!packet){
  mqtt3_log_printf(0x10, "PUBLISH failed allocating packet memory.");
  return 1;
 }
 packet->command = 0x30 | ((dup&0x1)<<3) | (qos<<1) | retain;
 packet->remaining_length = packetlen;
 packet->payload = _mosquitto_malloc(sizeof(unsigned char)*packetlen);
 if(!packet->payload){
  mqtt3_log_printf(0x10, "PUBLISH failed allocating payload memory.");
  _mosquitto_free(packet);
  return 1;
 }
 _mosquitto_write_string(packet, topic, strlen(topic));
 if(qos > 0){
  _mosquitto_write_uint16(packet, mid);
 }
 if(payloadlen){
  _mosquitto_write_bytes(packet, payload, payloadlen);
 }
 if(mqtt3_net_packet_queue(context, packet)){
  mqtt3_log_printf(0x10, "PUBLISH failed queuing packet.");
  _mosquitto_free(packet);
  return 1;
 }
 return 0;
}

variable_types: dup int

variable_types:
qos
unsigned char

variable_types:
mid
unsigned short

variable_types:
topic
char *

variable_types:
payloadlen
unsigned int

variable_types:
payload
unsigned char *

variable_types: packetlen int

user_defined_types: struct _mosquitto_packet { unsigned char command; unsigned char command_saved; unsigned char have_remaining; unsigned char remaining_count; unsigned short mid; unsigned int remaining_mult; unsigned int remaining_length; unsigned int to_process; unsigned int pos; unsigned char *payload; struct _mosquitto_packet *next; }

user_defined_types: enum mosquitto_client_state { mosq_cs_new=0, mosq_cs_connected=1, mosq_cs_disconnecting=2 }

user_defined_types: struct mosquitto_message { unsigned short mid; char *topic; unsigned char *payload; unsigned int payloadlen; int qos; _Bool retain; }

user_defined_types: enum mosquitto_msg_direction { mosq_md_in=0, mosq_md_out=1 }

user_defined_types: enum mqtt3_msg_state { ms_invalid=0, ms_publish=1, ms_publish_puback=2, ms_wait_puback=3, ms_publish_pubrec=4, ms_wait_pubrec=5, ms_resend_pubrel=6, ms_wait_pubrel=7, ms_resend_pubcomp=8, ms_wait_pubcomp=9, ms_resend_pubrec=10, ms_queued=11 }

user_defined_types: struct mosquitto_msg_store { struct mosquitto_msg_store *next; unsigned long db_id; int ref_count; char *source_id; unsigned short source_mid; struct mosquitto_message msg; }

user_defined_types: enum mqtt3_bridge_direction { bd_out=0, bd_in=1, bd_both=2 }


decompiled_code:
_BOOL8 __fastcall mqtt3_send_command_with_mid(__int64 a1, char a2, __int16 a3, char a4)
{
char v6; // bl
__int64 v7; // rax
__int64 v8; // rbp
char v9; // si
_BYTE *v10; // rax
_BOOL8 result; // rax

v6 = a2;
v7 = mosquitto_calloc(1LL, 40LL);
if ( !v7 )
return 1LL;
*(_DWORD *)(v7 + 12) = 2;
v8 = v7;
v9 = a2 | 8;
if ( a4 )
v6 = v9;
*(_BYTE *)v7 = v6;
v10 = (_BYTE *)mosquitto_malloc(2LL);
*(_QWORD *)(v8 + 24) = v10;
if ( v10 )
{
*v10 = HIBYTE(a3);
*(_BYTE *)(*(_QWORD *)(v8 + 24) + 1LL) = a3;
result = (unsigned int)mqtt3_net_packet_queue(a1, v8) != 0;
}
else
{
mosquitto_free(v8);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(__int64 a1, char a2, __int16 a3, char a4)
{
char v6; // bl
__int64 v7; // rax
__int64 v8; // rbp
char v9; // si
_BYTE *v10; // rax
_BOOL8 result; // rax

v6 = a2;
v7 = mosquitto_calloc(1LL, 40LL);
if ( !v7 )
return 1LL;
*(_DWORD *)(v7 + 12) = 2;
v8 = v7;
v9 = a2 | 8;
if ( a4 )
v6 = v9;
*(_BYTE *)v7 = v6;
v10 = (_BYTE *)mosquitto_malloc(2LL);
*(_QWORD *)(v8 + 24) = v10;
if ( v10 )
{
*v10 = HIBYTE(a3);
*(_BYTE *)(*(_QWORD *)(v8 + 24) + 1LL) = a3;
result = (unsigned int)mqtt3_net_packet_queue(a1, v8) != 0;
}
else
{
mosquitto_free(v8);
result = 1LL;
}
return result;
}

original_code:
int mqtt3_send_command_with_mid(mqtt3_context *context, uint8_t command, uint16_t mid, _Bool dup)
{
 struct _mosquitto_packet *packet = ((void *)0);
 packet = _mosquitto_calloc(1, sizeof(struct _mosquitto_packet));
 if(!packet) return 1;
 packet->command = command;
 if(dup){
  packet->command |= 8;
 }
 packet->remaining_length = 2;
 packet->payload = _mosquitto_malloc(sizeof(uint8_t)*2);
 if(!packet->payload){
  _mosquitto_free(packet);
  return 1;
 }
 packet->payload[0] = (uint8_t)((mid & 0xFF00) >> 8);
 packet->payload[1] = (uint8_t)(mid & 0x00FF);
 if(mqtt3_net_packet_queue(context, packet)) return 1;
 return 0;
}

canonical_original_code:
int mqtt3_send_command_with_mid(struct _mqtt3_context *context, unsigned char command, unsigned short mid, _Bool dup)
{
 struct _mosquitto_packet *packet = ((void *)0);
 packet = _mosquitto_calloc(1, sizeof(struct _mosquitto_packet));
 if(!packet) return 1;
 packet->command = command;
 if(dup){
  packet->command |= 8;
 }
 packet->remaining_length = 2;
 packet->payload = _mosquitto_malloc(sizeof(unsigned char)*2);
 if(!packet->payload){
  _mosquitto_free(packet);
  return 1;
 }
 packet->payload[0] = (unsigned char)((mid & 0xFF00) >> 8);
 packet->payload[1] = (unsigned char)(mid & 0x00FF);
 if(mqtt3_net_packet_queue(context, packet)) return 1;
 return 0;
}

variable_types: struct _mqtt3_context { struct _mosquitto_core { int sock; char *id; char *username; char *password; unsigned short keepalive; enum mosquitto_client_state { mosq_cs_new=0, mosq_cs_connected=1, mosq_cs_disconnecting=2 } state; long last_msg_in; long last_msg_out; unsigned short last_mid; struct _mosquitto_packet { unsigned char command; unsigned char command_saved; unsigned char have_remaining; unsigned char remaining_count; unsigned short mid; unsigned int remaining_mult; unsigned int remaining_length; unsigned int to_process; unsigned int pos; unsigned char *payload; struct _mosquitto_packet *next; } in_packet; struct _mosquitto_packet *out_packet; struct mosquitto_message { unsigned short mid; char *topic; unsigned char *payload; unsigned int payloadlen; int qos; _Bool retain; } *will; } core; _Bool clean_session; _Bool duplicate; char *address; struct _mqtt3_bridge { char *name; char *address; unsigned short port; int keepalive; _Bool clean_session; struct _mqtt3_bridge_topic { char *topic; enum mqtt3_bridge_direction { bd_out=0, bd_in=1, bd_both=2 } direction; } *topics; int topic_count; long restart_t; char *username; char *password; } *bridge; struct _mosquitto_client_msg { struct _mosquitto_client_msg *next; struct mosquitto_msg_store { struct mosquitto_msg_store *next; unsigned long db_id; int ref_count; char *source_id; unsigned short source_mid; struct mosquitto_message msg; } *store; unsigned short mid; int qos; _Bool retain; long timestamp; enum mosquitto_msg_direction { mosq_md_in=0, mosq_md_out=1 } direction; enum mqtt3_msg_state { ms_invalid=0, ms_publish=1, ms_publish_puback=2, ms_wait_puback=3, ms_publish_pubrec=4, ms_wait_pubrec=5, ms_resend_pubrel=6, ms_wait_pubrel=7, ms_resend_pubcomp=8, ms_wait_pubcomp=9, ms_resend_pubrec=10, ms_queued=11 } state; _Bool dup; } *msgs; } *

variable_types:
command
unsigned char

variable_types: struct _mosquitto_packet { unsigned char command; unsigned char command_saved; unsigned char have_remaining; unsigned char remaining_count; unsigned short mid; unsigned int remaining_mult; unsigned int remaining_length; unsigned int to_process; unsigned int pos; unsigned char *payload; struct _mosquitto_packet *next; } *

user_defined_types: struct _mqtt3_context { struct _mosquitto_core core; _Bool clean_session; _Bool duplicate; char *address; struct _mqtt3_bridge *bridge; struct _mosquitto_client_msg *msgs; }


decompiled_code:
_BOOL8 __fastcall mqtt3_raw_puback(__int64 a1, unsigned __int16 a2)
{
if ( a1 )
mqtt3_log_printf(16LL, "Sending PUBACK to %s (Mid: %d)", *(const char **)(a1 + 8), a2);
return mqtt3_send_command_with_mid(a1, 64, a2, 0);
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(__int64 a1, unsigned __int16 a2)
{
if ( a1 )
mqtt3_log_printf(16LL, "Sending PUBACK to %s (Mid: %d)", *(const char **)(a1 + 8), a2);
return func1(a1, 64, a2, 0);
}

original_code:
int mqtt3_raw_puback(mqtt3_context *context, uint16_t mid)
{
 if(context) mqtt3_log_printf(0x10, "Sending PUBACK to %s (Mid: %d)", context->core.id, mid);
 return mqtt3_send_command_with_mid(context, 0x40, mid, 0);
}

canonical_original_code:
int mqtt3_raw_puback(struct _mqtt3_context *context, unsigned short mid)
{
 if(context) mqtt3_log_printf(0x10, "Sending PUBACK to %s (Mid: %d)", context->core.id, mid);
 return mqtt3_send_command_with_mid(context, 0x40, mid, 0);
}

user_defined_types: struct _mosquitto_core { int sock; char *id; char *username; char *password; unsigned short keepalive; enum mosquitto_client_state state; long last_msg_in; long last_msg_out; unsigned short last_mid; struct _mosquitto_packet in_packet; struct _mosquitto_packet *out_packet; struct mosquitto_message *will; }


decompiled_code:
_BOOL8 __fastcall mqtt3_raw_pubcomp(__int64 a1, unsigned __int16 a2)
{
if ( a1 )
mqtt3_log_printf(16LL, "Sending PUBCOMP to %s (Mid: %d)", *(const char **)(a1 + 8), a2);
return mqtt3_send_command_with_mid(a1, 112, a2, 0);
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(__int64 a1, unsigned __int16 a2)
{
if ( a1 )
mqtt3_log_printf(16LL, "Sending PUBCOMP to %s (Mid: %d)", *(const char **)(a1 + 8), a2);
return func1(a1, 112, a2, 0);
}

original_code:
int mqtt3_raw_pubcomp(mqtt3_context *context, uint16_t mid)
{
 if(context) mqtt3_log_printf(0x10, "Sending PUBCOMP to %s (Mid: %d)", context->core.id, mid);
 return mqtt3_send_command_with_mid(context, 0x70, mid, 0);
}

canonical_original_code:
int mqtt3_raw_pubcomp(struct _mqtt3_context *context, unsigned short mid)
{
 if(context) mqtt3_log_printf(0x10, "Sending PUBCOMP to %s (Mid: %d)", context->core.id, mid);
 return mqtt3_send_command_with_mid(context, 0x70, mid, 0);
}

user_defined_types: struct _mqtt3_bridge { char *name; char *address; unsigned short port; int keepalive; _Bool clean_session; struct _mqtt3_bridge_topic *topics; int topic_count; long restart_t; char *username; char *password; }


decompiled_code:
_BOOL8 __fastcall mqtt3_raw_pubrec(__int64 a1, unsigned __int16 a2)
{
if ( a1 )
mqtt3_log_printf(16LL, "Sending PUBREC to %s (Mid: %d)", *(const char **)(a1 + 8), a2);
return mqtt3_send_command_with_mid(a1, 80, a2, 0);
}

canonical_decompiled_code:
_BOOL8 __fastcall func4(__int64 a1, unsigned __int16 a2)
{
if ( a1 )
mqtt3_log_printf(16LL, "Sending PUBREC to %s (Mid: %d)", *(const char **)(a1 + 8), a2);
return func1(a1, 80, a2, 0);
}

original_code:
int mqtt3_raw_pubrec(mqtt3_context *context, uint16_t mid)
{
 if(context) mqtt3_log_printf(0x10, "Sending PUBREC to %s (Mid: %d)", context->core.id, mid);
 return mqtt3_send_command_with_mid(context, 0x50, mid, 0);
}

canonical_original_code:
int mqtt3_raw_pubrec(struct _mqtt3_context *context, unsigned short mid)
{
 if(context) mqtt3_log_printf(0x10, "Sending PUBREC to %s (Mid: %d)", context->core.id, mid);
 return mqtt3_send_command_with_mid(context, 0x50, mid, 0);
}

user_defined_types: struct _mosquitto_client_msg { struct _mosquitto_client_msg *next; struct mosquitto_msg_store *store; unsigned short mid; int qos; _Bool retain; long timestamp; enum mosquitto_msg_direction direction; enum mqtt3_msg_state state; _Bool dup; }


decompiled_code:
_BOOL8 __fastcall mqtt3_raw_pubrel(__int64 a1, unsigned __int16 a2, char a3)
{
if ( a1 )
mqtt3_log_printf(16LL, "Sending PUBREL to %s (Mid: %d)", *(const char **)(a1 + 8), a2);
return mqtt3_send_command_with_mid(a1, 96, a2, a3);
}

canonical_decompiled_code:
_BOOL8 __fastcall func5(__int64 a1, unsigned __int16 a2, char a3)
{
if ( a1 )
mqtt3_log_printf(16LL, "Sending PUBREL to %s (Mid: %d)", *(const char **)(a1 + 8), a2);
return func1(a1, 96, a2, a3);
}

original_code:
int mqtt3_raw_pubrel(mqtt3_context *context, uint16_t mid, _Bool dup)
{
 if(context) mqtt3_log_printf(0x10, "Sending PUBREL to %s (Mid: %d)", context->core.id, mid);
 return mqtt3_send_command_with_mid(context, 0x60, mid, dup);
}

canonical_original_code:
int mqtt3_raw_pubrel(struct _mqtt3_context *context, unsigned short mid, _Bool dup)
{
 if(context) mqtt3_log_printf(0x10, "Sending PUBREL to %s (Mid: %d)", context->core.id, mid);
 return mqtt3_send_command_with_mid(context, 0x60, mid, dup);
}

user_defined_types: struct _mqtt3_bridge_topic { char *topic; enum mqtt3_bridge_direction direction; }


decompiled_code:
__int64 __fastcall mqtt3_send_simple_command(__int64 a1, char a2)
{
__int64 v2; // rbp
__int64 result; // rax

v2 = mosquitto_calloc(1LL, 40LL);
result = 1LL;
if ( v2 )
{
*(_BYTE *)v2 = a2;
*(_DWORD *)(v2 + 12) = 0;
result = mqtt3_net_packet_queue(a1, v2);
if ( (_DWORD)result )
{
mosquitto_free(v2);
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, char a2)
{
__int64 v2; // rbp
__int64 result; // rax

v2 = mosquitto_calloc(1LL, 40LL);
result = 1LL;
if ( v2 )
{
*(_BYTE *)v2 = a2;
*(_DWORD *)(v2 + 12) = 0;
result = mqtt3_net_packet_queue(a1, v2);
if ( (_DWORD)result )
{
mosquitto_free(v2);
result = 1LL;
}
}
return result;
}

original_code:
int mqtt3_send_simple_command(mqtt3_context *context, uint8_t command)
{
 struct _mosquitto_packet *packet = ((void *)0);
 packet = _mosquitto_calloc(1, sizeof(struct _mosquitto_packet));
 if(!packet) return 1;
 packet->command = command;
 packet->remaining_length = 0;
 if(mqtt3_net_packet_queue(context, packet)){
  _mosquitto_free(packet);
  return 1;
 }
 return 0;
}

canonical_original_code:
int mqtt3_send_simple_command(struct _mqtt3_context *context, unsigned char command)
{
 struct _mosquitto_packet *packet = ((void *)0);
 packet = _mosquitto_calloc(1, sizeof(struct _mosquitto_packet));
 if(!packet) return 1;
 packet->command = command;
 packet->remaining_length = 0;
 if(mqtt3_net_packet_queue(context, packet)){
  _mosquitto_free(packet);
  return 1;
 }
 return 0;
}


decompiled_code:
__int64 __fastcall mqtt3_raw_pingreq(__int64 a1)
{
if ( a1 )
mqtt3_log_printf(16LL, "Sending PINGREQ to %s", *(const char **)(a1 + 8));
return mqtt3_send_simple_command(a1, 192);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
if ( a1 )
mqtt3_log_printf(16LL, "Sending PINGREQ to %s", *(const char **)(a1 + 8));
return func6(a1, 192);
}

original_code:
int mqtt3_raw_pingreq(mqtt3_context *context)
{
 if(context) mqtt3_log_printf(0x10, "Sending PINGREQ to %s", context->core.id);
 return mqtt3_send_simple_command(context, 0xC0);
}

canonical_original_code:
int mqtt3_raw_pingreq(struct _mqtt3_context *context)
{
 if(context) mqtt3_log_printf(0x10, "Sending PINGREQ to %s", context->core.id);
 return mqtt3_send_simple_command(context, 0xC0);
}


decompiled_code:
__int64 __fastcall mqtt3_raw_pingresp(__int64 a1)
{
if ( a1 )
mqtt3_log_printf(16LL, "Sending PINGRESP to %s", *(const char **)(a1 + 8));
return mqtt3_send_simple_command(a1, 208);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
if ( a1 )
mqtt3_log_printf(16LL, "Sending PINGRESP to %s", *(const char **)(a1 + 8));
return func6(a1, 208);
}

original_code:
int mqtt3_raw_pingresp(mqtt3_context *context)
{
 if(context) mqtt3_log_printf(0x10, "Sending PINGRESP to %s", context->core.id);
 return mqtt3_send_simple_command(context, 0xD0);
}

canonical_original_code:
int mqtt3_raw_pingresp(struct _mqtt3_context *context)
{
 if(context) mqtt3_log_printf(0x10, "Sending PINGRESP to %s", context->core.id);
 return mqtt3_send_simple_command(context, 0xD0);
}


decompiled_code:
__int64 __fastcall mqtt3_raw_connack(__int64 a1, unsigned __int8 a2)
{
__int64 v2; // r13
__int64 v3; // rax
__int64 v4; // rbp
_BYTE *v5; // rax

if ( a1 )
mqtt3_log_printf(16LL, "Sending CONNACK to %s (%d)", *(const char **)(a1 + 8), a2);
LODWORD(v2) = 1;
v3 = mosquitto_calloc(1LL, 40LL);
v4 = v3;
if ( !v3 )
return (unsigned int)v2;
*(_BYTE *)v3 = 32;
*(_DWORD *)(v3 + 12) = 2;
v5 = (_BYTE *)mosquitto_malloc(2LL);
*(_QWORD *)(v4 + 24) = v5;
if ( v5 )
{
*v5 = 0;
*(_BYTE *)(*(_QWORD *)(v4 + 24) + 1LL) = a2;
return (unsigned int)mqtt3_net_packet_queue(a1, v4) != 0;
}
mosquitto_free(v4);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, unsigned __int8 a2)
{
__int64 v2; // r13
__int64 v3; // rax
__int64 v4; // rbp
_BYTE *v5; // rax

if ( a1 )
mqtt3_log_printf(16LL, "Sending CONNACK to %s (%d)", *(const char **)(a1 + 8), a2);
LODWORD(v2) = 1;
v3 = mosquitto_calloc(1LL, 40LL);
v4 = v3;
if ( !v3 )
return (unsigned int)v2;
*(_BYTE *)v3 = 32;
*(_DWORD *)(v3 + 12) = 2;
v5 = (_BYTE *)mosquitto_malloc(2LL);
*(_QWORD *)(v4 + 24) = v5;
if ( v5 )
{
*v5 = 0;
*(_BYTE *)(*(_QWORD *)(v4 + 24) + 1LL) = a2;
return (unsigned int)mqtt3_net_packet_queue(a1, v4) != 0;
}
mosquitto_free(v4);
return 1LL;
}

original_code:
int mqtt3_raw_connack(mqtt3_context *context, uint8_t result)
{
 struct _mosquitto_packet *packet = ((void *)0);
 if(context) mqtt3_log_printf(0x10, "Sending CONNACK to %s (%d)", context->core.id, result);
 packet = _mosquitto_calloc(1, sizeof(struct _mosquitto_packet));
 if(!packet) return 1;
 packet->command = 0x20;
 packet->remaining_length = 2;
 packet->payload = _mosquitto_malloc(sizeof(uint8_t)*2);
 if(!packet->payload){
  _mosquitto_free(packet);
  return 1;
 }
 packet->payload[0] = 0;
 packet->payload[1] = result;
 if(mqtt3_net_packet_queue(context, packet)) return 1;
 return 0;
}

canonical_original_code:
int mqtt3_raw_connack(struct _mqtt3_context *context, unsigned char result)
{
 struct _mosquitto_packet *packet = ((void *)0);
 if(context) mqtt3_log_printf(0x10, "Sending CONNACK to %s (%d)", context->core.id, result);
 packet = _mosquitto_calloc(1, sizeof(struct _mosquitto_packet));
 if(!packet) return 1;
 packet->command = 0x20;
 packet->remaining_length = 2;
 packet->payload = _mosquitto_malloc(sizeof(unsigned char)*2);
 if(!packet->payload){
  _mosquitto_free(packet);
  return 1;
 }
 packet->payload[0] = 0;
 packet->payload[1] = result;
 if(mqtt3_net_packet_queue(context, packet)) return 1;
 return 0;
}

variable_types:
result
unsigned char


decompiled_code:
_BOOL8 __fastcall mqtt3_raw_suback(__int64 a1, unsigned __int16 a2, unsigned int a3, __int64 a4)
{
_BOOL4 v4; // er15
__int64 v8; // rax
__int64 v9; // rdi
__int64 v10; // rbp
__int64 v11; // rax

v4 = 1;
mqtt3_log_printf(16LL, "Sending SUBACK to %s", *(const char **)(a1 + 8));
v8 = mosquitto_calloc(1LL, 40LL);
if ( v8 )
{
v9 = a3 + 2;
*(_BYTE *)v8 = -112;
v10 = v8;
*(_DWORD *)(v8 + 12) = v9;
v11 = mosquitto_malloc(v9);
*(_QWORD *)(v10 + 24) = v11;
if ( v11 )
{
mosquitto_write_uint16(v10, a2);
if ( a3 )
mosquitto_write_bytes(v10, a4, a3);
v4 = mqtt3_net_packet_queue(a1, v10) != 0;
}
else
{
mosquitto_free(v10);
}
}
return v4;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(__int64 a1, unsigned __int16 a2, unsigned int a3, __int64 a4)
{
_BOOL4 v4; // er15
__int64 v8; // rax
__int64 v9; // rdi
__int64 v10; // rbp
__int64 v11; // rax

v4 = 1;
mqtt3_log_printf(16LL, "Sending SUBACK to %s", *(const char **)(a1 + 8));
v8 = mosquitto_calloc(1LL, 40LL);
if ( v8 )
{
v9 = a3 + 2;
*(_BYTE *)v8 = -112;
v10 = v8;
*(_DWORD *)(v8 + 12) = v9;
v11 = mosquitto_malloc(v9);
*(_QWORD *)(v10 + 24) = v11;
if ( v11 )
{
mosquitto_write_uint16(v10, a2);
if ( a3 )
mosquitto_write_bytes(v10, a4, a3);
v4 = mqtt3_net_packet_queue(a1, v10) != 0;
}
else
{
mosquitto_free(v10);
}
}
return v4;
}

original_code:
int mqtt3_raw_suback(mqtt3_context *context, uint16_t mid, uint32_t payloadlen, const uint8_t *payload)
{
 struct _mosquitto_packet *packet = ((void *)0);
 mqtt3_log_printf(0x10, "Sending SUBACK to %s", context->core.id);
 packet = _mosquitto_calloc(1, sizeof(struct _mosquitto_packet));
 if(!packet) return 1;
 packet->command = 0x90;
 packet->remaining_length = 2+payloadlen;
 packet->payload = _mosquitto_malloc(sizeof(uint8_t)*(2+payloadlen));
 if(!packet->payload){
  _mosquitto_free(packet);
  return 1;
 }
 _mosquitto_write_uint16(packet, mid);
 if(payloadlen){
  _mosquitto_write_bytes(packet, payload, payloadlen);
 }
 if(mqtt3_net_packet_queue(context, packet)) return 1;
 return 0;
}

canonical_original_code:
int mqtt3_raw_suback(struct _mqtt3_context *context, unsigned short mid, unsigned int payloadlen, unsigned char *payload)
{
 struct _mosquitto_packet *packet = ((void *)0);
 mqtt3_log_printf(0x10, "Sending SUBACK to %s", context->core.id);
 packet = _mosquitto_calloc(1, sizeof(struct _mosquitto_packet));
 if(!packet) return 1;
 packet->command = 0x90;
 packet->remaining_length = 2+payloadlen;
 packet->payload = _mosquitto_malloc(sizeof(unsigned char)*(2+payloadlen));
 if(!packet->payload){
  _mosquitto_free(packet);
  return 1;
 }
 _mosquitto_write_uint16(packet, mid);
 if(payloadlen){
  _mosquitto_write_bytes(packet, payload, payloadlen);
 }
 if(mqtt3_net_packet_queue(context, packet)) return 1;
 return 0;
}


decompiled_code:
void __fastcall mqtt3_net_set_max_connections(int a1)
{
max_connections = a1;
}

canonical_decompiled_code:
void __fastcall func1(int a1)
{
max_connections = a1;
}

original_code:
void mqtt3_net_set_max_connections(int max)
{
 max_connections = max;
}

variable_types: max int


decompiled_code:
__int64 __fastcall mqtt3_socket_accept(__int64 *a1, int *a2, int a3)
{
int v4; // eax
unsigned int v5; // er12
unsigned int v6; // eax
__int64 v7; // rax
__int64 v8; // r13
int v9; // ecx
_QWORD *v10; // rax
int v11; // edx
int v13; // ecx
_QWORD *v14; // rdi
__int64 v15; // rax
__int64 v16; // rdx

v4 = accept(a3, 0LL, 0LL);
if ( v4 < 0 )
return (unsigned int)-1;
v5 = v4;
if ( max_connections > 0 && max_connections <= *a2
|| (v6 = fcntl(v4, 3, 0LL), v6 == -1)
|| (BYTE1(v6) |= 8u, fcntl(v5, 4, v6) == -1) )
{
close(v5);
return (unsigned int)-1;
}
v7 = mqtt3_context_init(v5);
v8 = v7;
if ( !v7 )
return (unsigned int)-1;
mqtt3_log_printf(2LL, "New client connected from %s.", *(const char **)(v7 + 128));
v9 = *a2;
if ( *a2 <= 0 )
{
v11 = 0;
}
else
{
v10 = (_QWORD *)*a1;
v11 = 0;
while ( *v10 )
{
++v11;
++v10;
if ( v9 == v11 )
goto LABEL_14;
}
*v10 = v8;
}
if ( v9 != v11 )
return v5;
LABEL_14:
v13 = v9 + 1;
v14 = (_QWORD *)*a1;
*a2 = v13;
v15 = mosquitto_realloc(v14, 8LL * v13);
if ( !v15 )
return v5;
v16 = *a2;
*a1 = v15;
*(_QWORD *)(v15 + 8 * v16 - 8) = v8;
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 *a1, int *a2, int a3)
{
int v4; // eax
unsigned int v5; // er12
unsigned int v6; // eax
__int64 v7; // rax
__int64 v8; // r13
int v9; // ecx
_QWORD *v10; // rax
int v11; // edx
int v13; // ecx
_QWORD *v14; // rdi
__int64 v15; // rax
__int64 v16; // rdx

v4 = accept(a3, 0LL, 0LL);
if ( v4 < 0 )
return (unsigned int)-1;
v5 = v4;
if ( max_connections > 0 && max_connections <= *a2
|| (v6 = fcntl(v4, 3, 0LL), v6 == -1)
|| (BYTE1(v6) |= 8u, fcntl(v5, 4, v6) == -1) )
{
close(v5);
return (unsigned int)-1;
}
v7 = mqtt3_context_init(v5);
v8 = v7;
if ( !v7 )
return (unsigned int)-1;
mqtt3_log_printf(2LL, "New client connected from %s.", *(const char **)(v7 + 128));
v9 = *a2;
if ( *a2 <= 0 )
{
v11 = 0;
}
else
{
v10 = (_QWORD *)*a1;
v11 = 0;
while ( *v10 )
{
++v11;
++v10;
if ( v9 == v11 )
goto LABEL_14;
}
*v10 = v8;
}
if ( v9 != v11 )
return v5;
LABEL_14:
v13 = v9 + 1;
v14 = (_QWORD *)*a1;
*a2 = v13;
v15 = mosquitto_realloc(v14, 8LL * v13);
if ( !v15 )
return v5;
v16 = *a2;
*a1 = v15;
*(_QWORD *)(v15 + 8 * v16 - 8) = v8;
return v5;
}

original_code:
int mqtt3_socket_accept(mqtt3_context ***contexts, int *context_count, int listensock)
{
 int i;
 int new_sock = -1;
 mqtt3_context **tmp_contexts = ((void *)0);
 mqtt3_context *new_context;
 int opt = 1;
 new_sock = accept(listensock, ((void *)0), 0);
 if(new_sock < 0) return -1;
 if(max_connections > 0 && (*context_count) >= max_connections){
  close(new_sock);
  return -1;
 }
 opt = fcntl(new_sock, 3, 0);
 if(opt == -1 || fcntl(new_sock, 4, opt | 04000) == -1){
  close(new_sock);
  return -1;
 }
  new_context = mqtt3_context_init(new_sock);
  if(!new_context) return -1;
  mqtt3_log_printf(0x02, "New client connected from %s.", new_context->address);
  for(i=0; i<(*context_count); i++){
   if((*contexts)[i] == ((void *)0)){
    (*contexts)[i] = new_context;
    break;
   }
  }
  if(i==(*context_count)){
   (*context_count)++;
   tmp_contexts = _mosquitto_realloc(*contexts, sizeof(mqtt3_context*)*(*context_count));
   if(tmp_contexts){
    *contexts = tmp_contexts;
    (*contexts)[(*context_count)-1] = new_context;
   }
  }
 return new_sock;
}

canonical_original_code:
int mqtt3_socket_accept(struct _mqtt3_context ***contexts, int *context_count, int listensock)
{
 int i;
 int new_sock = -1;
 struct _mqtt3_context **tmp_contexts = ((void *)0);
 struct _mqtt3_context *new_context;
 int opt = 1;
 new_sock = accept(listensock, ((void *)0), 0);
 if(new_sock < 0) return -1;
 if(max_connections > 0 && (*context_count) >= max_connections){
  close(new_sock);
  return -1;
 }
 opt = fcntl(new_sock, 3, 0);
 if(opt == -1 || fcntl(new_sock, 4, opt | 04000) == -1){
  close(new_sock);
  return -1;
 }
  new_context = mqtt3_context_init(new_sock);
  if(!new_context) return -1;
  mqtt3_log_printf(0x02, "New client connected from %s.", new_context->address);
  for(i=0; i<(*context_count); i++){
   if((*contexts)[i] == ((void *)0)){
    (*contexts)[i] = new_context;
    break;
   }
  }
  if(i==(*context_count)){
   (*context_count)++;
   tmp_contexts = _mosquitto_realloc(*contexts, sizeof(struct _mqtt3_context *)*(*context_count));
   if(tmp_contexts){
    *contexts = tmp_contexts;
    (*contexts)[(*context_count)-1] = new_context;
   }
  }
 return new_sock;
}

variable_types: struct _mqtt3_context { struct _mosquitto_core { int sock; char *id; char *username; char *password; unsigned short keepalive; enum mosquitto_client_state { mosq_cs_new=0, mosq_cs_connected=1, mosq_cs_disconnecting=2 } state; long last_msg_in; long last_msg_out; unsigned short last_mid; struct _mosquitto_packet { unsigned char command; unsigned char command_saved; unsigned char have_remaining; unsigned char remaining_count; unsigned short mid; unsigned int remaining_mult; unsigned int remaining_length; unsigned int to_process; unsigned int pos; unsigned char *payload; struct _mosquitto_packet *next; } in_packet; struct _mosquitto_packet *out_packet; struct mosquitto_message { unsigned short mid; char *topic; unsigned char *payload; unsigned int payloadlen; int qos; _Bool retain; } *will; } core; _Bool clean_session; _Bool duplicate; char *address; struct _mqtt3_bridge { char *name; char *address; unsigned short port; int keepalive; _Bool clean_session; struct _mqtt3_bridge_topic { char *topic; enum mqtt3_bridge_direction { bd_out=0, bd_in=1, bd_both=2 } direction; } *topics; int topic_count; long restart_t; char *username; char *password; } *bridge; struct _mosquitto_client_msg { struct _mosquitto_client_msg *next; struct mosquitto_msg_store { struct mosquitto_msg_store *next; unsigned long db_id; int ref_count; char *source_id; unsigned short source_mid; struct mosquitto_message msg; } *store; unsigned short mid; int qos; _Bool retain; long timestamp; enum mosquitto_msg_direction { mosq_md_in=0, mosq_md_out=1 } direction; enum mqtt3_msg_state { ms_invalid=0, ms_publish=1, ms_publish_puback=2, ms_wait_puback=3, ms_publish_pubrec=4, ms_wait_pubrec=5, ms_resend_pubrel=6, ms_wait_pubrel=7, ms_resend_pubcomp=8, ms_wait_pubcomp=9, ms_resend_pubrec=10, ms_queued=11 } state; _Bool dup; } *msgs; } ***

variable_types: listensock int

variable_types: i int

variable_types: opt int


decompiled_code:
int __fastcall mqtt3_socket_close(int *a1)
{
int v2; // edi
int result; // eax

if ( !a1 )
_assert_fail("context", "net.c", 0x91u, "mqtt3_socket_close");
v2 = *a1;
if ( v2 != -1 )
{
result = close(v2);
*a1 = -1;
}
return result;
}

canonical_decompiled_code:
int __fastcall func3(int *a1)
{
int v2; // edi
int result; // eax

if ( !a1 )
_assert_fail("context", "net.c", 0x91u, "mqtt3_socket_close");
v2 = *a1;
if ( v2 != -1 )
{
result = close(v2);
*a1 = -1;
}
return result;
}

original_code:
void mqtt3_socket_close(mqtt3_context *context)
{
 int rc = 0;
 ((void) sizeof ((context) ? 1 : 0), __extension__ ({ if (context) ; else __assert_fail ("context", "net.c", 145, __extension__ __PRETTY_FUNCTION__); }));
 if(context->core.sock != -1){
  rc = close(context->core.sock);
  context->core.sock = -1;
 }
}

canonical_original_code:
void mqtt3_socket_close(struct _mqtt3_context *context)
{
 int rc = 0;
 ((void) sizeof ((context) ? 1 : 0), __extension__ ({ if (context) ; else __assert_fail ("context", "net.c", 145, __extension__ __PRETTY_FUNCTION__); }));
 if(context->core.sock != -1){
  rc = close(context->core.sock);
  context->core.sock = -1;
 }
}

variable_types: rc int


decompiled_code:
__int64 __fastcall mqtt3_socket_listen(char *name, unsigned __int16 a2, __int64 *a3, int *a4)
{
unsigned int v4; // er14
struct addrinfo *v7; // rbp
int v8; // eax
unsigned __int16 v9; // ax
int v10; // er12
int v11; // eax
__int64 v12; // rdi
__int64 v13; // rax
__int64 v14; // rdx
unsigned int v15; // eax
int *v16; // rax
char *v17; // rax
unsigned __int16 v19; // ax
int *v20; // rax
char *v21; // rax
int optval; // [rsp+4h] [rbp-74h] BYREF
struct addrinfo *pai; // [rsp+8h] [rbp-70h] BYREF
char s[10]; // [rsp+16h] [rbp-62h] BYREF
struct addrinfo req; // [rsp+20h] [rbp-58h] BYREF

v4 = -1;
optval = 1;
snprintf(s, 0xAuLL, "%d", a2);
*(_OWORD *)&req.ai_flags = 0LL;
*(_OWORD *)&req.ai_addrlen = 0LL;
req.ai_flags = 1;
req.ai_socktype = 1;
*(_OWORD *)&req.ai_canonname = 0LL;
if ( getaddrinfo(name, s, &req, &pai) )
return v4;
v7 = pai;
*a4 = 0;
*a3 = 0LL;
if ( v7 )
{
while ( 1 )
{
v8 = v7->ai_family;
if ( v8 == 2 )
{
v19 = _bswap_16(*(_WORD *)v7->ai_addr->sa_data);
mqtt3_log_printf(1LL, "Opening ipv4 listen socket on port %d.", v19);
}
else
{
if ( v8 != 10 )
goto LABEL_4;
v9 = _bswap_16(*(_WORD *)v7->ai_addr->sa_data);
mqtt3_log_printf(1LL, "Opening ipv6 listen socket on port %d.", v9);
}
v10 = socket(v7->ai_family, v7->ai_socktype, v7->ai_protocol);
if ( v10 == -1 )
{
v20 = _errno_location();
v4 = 1;
v21 = strerror(*v20);
mqtt3_log_printf(8LL, "Error: %s", v21);
return v4;
}
v11 = *a4;
v12 = *a3;
++*a4;
v13 = mosquitto_realloc(v12, 4LL * (v11 + 1));
v14 = *a4;
*a3 = v13;
*(_DWORD *)(v13 + 4 * v14 - 4) = v10;
optval = 1;
setsockopt(v10, 1, 2, &optval, 4u);
optval = 1;
setsockopt(v10, 41, 26, &optval, 4u);
v15 = fcntl(v10, 3, 0LL);
if ( v15 == -1 )
goto LABEL_14;
BYTE1(v15) |= 8u;
if ( fcntl(v10, 4, v15) == -1 )
goto LABEL_14;
if ( bind(v10, v7->ai_addr, v7->ai_addrlen) == -1 || listen(v10, 100) == -1 )
{
v16 = _errno_location();
v17 = strerror(*v16);
mqtt3_log_printf(8LL, "Error: %s", v17);
LABEL_14:
v4 = 1;
close(v10);
return v4;
}
LABEL_4:
v7 = v7->ai_next;
if ( !v7 )
{
v7 = pai;
break;
}
}
}
freeaddrinfo(v7);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *name, unsigned __int16 a2, __int64 *a3, int *a4)
{
unsigned int v4; // er14
struct addrinfo *v7; // rbp
int v8; // eax
unsigned __int16 v9; // ax
int v10; // er12
int v11; // eax
__int64 v12; // rdi
__int64 v13; // rax
__int64 v14; // rdx
unsigned int v15; // eax
int *v16; // rax
char *v17; // rax
unsigned __int16 v19; // ax
int *v20; // rax
char *v21; // rax
int optval; // [rsp+4h] [rbp-74h] BYREF
struct addrinfo *pai; // [rsp+8h] [rbp-70h] BYREF
char s[10]; // [rsp+16h] [rbp-62h] BYREF
struct addrinfo req; // [rsp+20h] [rbp-58h] BYREF

v4 = -1;
optval = 1;
snprintf(s, 0xAuLL, "%d", a2);
*(_OWORD *)&req.ai_flags = 0LL;
*(_OWORD *)&req.ai_addrlen = 0LL;
req.ai_flags = 1;
req.ai_socktype = 1;
*(_OWORD *)&req.ai_canonname = 0LL;
if ( getaddrinfo(name, s, &req, &pai) )
return v4;
v7 = pai;
*a4 = 0;
*a3 = 0LL;
if ( v7 )
{
while ( 1 )
{
v8 = v7->ai_family;
if ( v8 == 2 )
{
v19 = func0(*(_WORD *)v7->ai_addr->sa_data);
mqtt3_log_printf(1LL, "Opening ipv4 listen socket on port %d.", v19);
}
else
{
if ( v8 != 10 )
goto LABEL_4;
v9 = func0(*(_WORD *)v7->ai_addr->sa_data);
mqtt3_log_printf(1LL, "Opening ipv6 listen socket on port %d.", v9);
}
v10 = socket(v7->ai_family, v7->ai_socktype, v7->ai_protocol);
if ( v10 == -1 )
{
v20 = _errno_location();
v4 = 1;
v21 = strerror(*v20);
mqtt3_log_printf(8LL, "Error: %s", v21);
return v4;
}
v11 = *a4;
v12 = *a3;
++*a4;
v13 = mosquitto_realloc(v12, 4LL * (v11 + 1));
v14 = *a4;
*a3 = v13;
*(_DWORD *)(v13 + 4 * v14 - 4) = v10;
optval = 1;
setsockopt(v10, 1, 2, &optval, 4u);
optval = 1;
setsockopt(v10, 41, 26, &optval, 4u);
v15 = fcntl(v10, 3, 0LL);
if ( v15 == -1 )
goto LABEL_14;
BYTE1(v15) |= 8u;
if ( fcntl(v10, 4, v15) == -1 )
goto LABEL_14;
if ( bind(v10, v7->ai_addr, v7->ai_addrlen) == -1 || listen(v10, 100) == -1 )
{
v16 = _errno_location();
v17 = strerror(*v16);
mqtt3_log_printf(8LL, "Error: %s", v17);
LABEL_14:
v4 = 1;
close(v10);
return v4;
}
LABEL_4:
v7 = v7->ai_next;
if ( !v7 )
{
v7 = pai;
break;
}
}
}
freeaddrinfo(v7);
return 0LL;
}

original_code:
int mqtt3_socket_listen(const char *host, uint16_t port, int **socks, int *sock_count)
{
 int sock = -1;
 struct addrinfo hints;
 struct addrinfo *ainfo, *rp;
 char service[10];
 int opt = 1;
 int ss_opt = 1;
 snprintf(service, 10, "%d", port);
 memset(&hints, 0, sizeof(struct addrinfo));
 hints.ai_family = 0;
 hints.ai_flags = 0x0001;
 hints.ai_socktype = SOCK_STREAM;
 if(getaddrinfo(host, service, &hints, &ainfo)) return -1;
 *sock_count = 0;
 *socks = ((void *)0);
 for(rp = ainfo; rp; rp = rp->ai_next){
  if(rp->ai_family == 2){
   mqtt3_log_printf(0x01, "Opening ipv4 listen socket on port %d.", ntohs(((struct sockaddr_in *)rp->ai_addr)->sin_port));
  }else if(rp->ai_family == 10){
   mqtt3_log_printf(0x01, "Opening ipv6 listen socket on port %d.", ntohs(((struct sockaddr_in6 *)rp->ai_addr)->sin6_port));
  }else{
   continue;
  }
  sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  if(sock == -1){
   mqtt3_log_printf(0x08, "Error: %s", strerror((*__errno_location ())));
   return 1;
  }
  (*sock_count)++;
  *socks = _mosquitto_realloc(*socks, sizeof(int)*(*sock_count));
  (*socks)[(*sock_count)-1] = sock;
  ss_opt = 1;
  setsockopt(sock, 1, 2, &ss_opt, sizeof(ss_opt));
  ss_opt = 1;
  setsockopt(sock, IPPROTO_IPV6, 26, &ss_opt, sizeof(ss_opt));
  opt = fcntl(sock, 3, 0);
  if(opt == -1 || fcntl(sock, 4, opt | 04000) == -1){
   close(sock);
   return 1;
  }
  if(bind(sock, rp->ai_addr, rp->ai_addrlen) == -1){
   mqtt3_log_printf(0x08, "Error: %s", strerror((*__errno_location ())));
   close(sock);
   return 1;
  }
  if(listen(sock, 100) == -1){
   mqtt3_log_printf(0x08, "Error: %s", strerror((*__errno_location ())));
   close(sock);
   return 1;
  }
 }
 freeaddrinfo(ainfo);
 return 0;
}

canonical_original_code:
int mqtt3_socket_listen(char *host, unsigned short port, int **socks, int *sock_count)
{
 int sock = -1;
 struct addrinfo hints;
 struct addrinfo *ainfo; struct addrinfo *rp;
 char service[10];
 int opt = 1;
 int ss_opt = 1;
 snprintf(service, 10, "%d", port);
 memset(&hints, 0, sizeof(struct addrinfo));
 hints.ai_family = 0;
 hints.ai_flags = 0x0001;
 hints.ai_socktype = SOCK_STREAM;
 if(getaddrinfo(host, service, &hints, &ainfo)) return -1;
 *sock_count = 0;
 *socks = ((void *)0);
 for(rp = ainfo; rp; rp = rp->ai_next){
  if(rp->ai_family == 2){
   mqtt3_log_printf(0x01, "Opening ipv4 listen socket on port %d.", ntohs(((struct sockaddr_in *)rp->ai_addr)->sin_port));
  }else if(rp->ai_family == 10){
   mqtt3_log_printf(0x01, "Opening ipv6 listen socket on port %d.", ntohs(((struct sockaddr_in6 *)rp->ai_addr)->sin6_port));
  }else{
   continue;
  }
  sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  if(sock == -1){
   mqtt3_log_printf(0x08, "Error: %s", strerror((*__errno_location ())));
   return 1;
  }
  (*sock_count)++;
  *socks = _mosquitto_realloc(*socks, sizeof(int)*(*sock_count));
  (*socks)[(*sock_count)-1] = sock;
  ss_opt = 1;
  setsockopt(sock, 1, 2, &ss_opt, sizeof(ss_opt));
  ss_opt = 1;
  setsockopt(sock, IPPROTO_IPV6, 26, &ss_opt, sizeof(ss_opt));
  opt = fcntl(sock, 3, 0);
  if(opt == -1 || fcntl(sock, 4, opt | 04000) == -1){
   close(sock);
   return 1;
  }
  if(bind(sock, rp->ai_addr, rp->ai_addrlen) == -1){
   mqtt3_log_printf(0x08, "Error: %s", strerror((*__errno_location ())));
   close(sock);
   return 1;
  }
  if(listen(sock, 100) == -1){
   mqtt3_log_printf(0x08, "Error: %s", strerror((*__errno_location ())));
   close(sock);
   return 1;
  }
 }
 freeaddrinfo(ainfo);
 return 0;
}

variable_types:
host
char *

variable_types:
port
unsigned short

variable_types:
socks
int **

variable_types: sock int

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; } *

variable_types:
service
char [10]

user_defined_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }

user_defined_types: struct sockaddr_in6 { unsigned short sin6_family; unsigned short sin6_port; unsigned int sin6_flowinfo; struct in6_addr sin6_addr; unsigned int sin6_scope_id; }

user_defined_types: struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; }


decompiled_code:
__int64 __fastcall mqtt3_net_packet_queue(__int64 a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // rdx

if ( !a1 || !a2 )
return 1LL;
result = *(_QWORD *)(a1 + 104);
*(_QWORD *)(a2 + 32) = 0LL;
if ( result )
{
do
{
v3 = result;
result = *(_QWORD *)(result + 32);
}
while ( result );
*(_QWORD *)(v3 + 32) = a2;
}
else
{
*(_QWORD *)(a1 + 104) = a2;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // rdx

if ( !a1 || !a2 )
return 1LL;
result = *(_QWORD *)(a1 + 104);
*(_QWORD *)(a2 + 32) = 0LL;
if ( result )
{
do
{
v3 = result;
result = *(_QWORD *)(result + 32);
}
while ( result );
*(_QWORD *)(v3 + 32) = a2;
}
else
{
*(_QWORD *)(a1 + 104) = a2;
result = 0LL;
}
return result;
}

original_code:
int mqtt3_net_packet_queue(mqtt3_context *context, struct _mosquitto_packet *packet)
{
 struct _mosquitto_packet *tail;
 if(!context || !packet) return 1;
 packet->next = ((void *)0);
 if(context->core.out_packet){
  tail = context->core.out_packet;
  while(tail->next){
   tail = tail->next;
  }
  tail->next = packet;
 }else{
  context->core.out_packet = packet;
 }
 return 0;
}

canonical_original_code:
int mqtt3_net_packet_queue(struct _mqtt3_context *context, struct _mosquitto_packet *packet)
{
 struct _mosquitto_packet *tail;
 if(!context || !packet) return 1;
 packet->next = ((void *)0);
 if(context->core.out_packet){
  tail = context->core.out_packet;
  while(tail->next){
   tail = tail->next;
  }
  tail->next = packet;
 }else{
  context->core.out_packet = packet;
 }
 return 0;
}

variable_types:
packet
struct _mosquitto_packet *


original_code:
int mqtt3_net_read(mosquitto_db *db, mqtt3_context *context)
{
 uint8_t byte;
 ssize_t read_length;
 int rc = 0;
 if(!context || context->core.sock == -1) return 1;
 if(!context->core.in_packet.command){
  read_length = _mosquitto_net_read(&context->core, &byte, 1);
  if(read_length == 1){
   bytes_received++;
   context->core.in_packet.command = byte;
   if(!(context->bridge) && context->core.state == mosq_cs_new && (byte&0xF0) != 0x10) return 1;
  }else{
   if(read_length == 0) return 1;
   if((*__errno_location ()) == 11 || (*__errno_location ()) == 11){
    return 0;
   }else{
    return 1;
   }
  }
 }
 if(!context->core.in_packet.have_remaining){
  do{
   read_length = _mosquitto_net_read(&context->core, &byte, 1);
   if(read_length == 1){
    context->core.in_packet.remaining_count++;
    if(context->core.in_packet.remaining_count > 4) return 2;
    bytes_received++;
    context->core.in_packet.remaining_length += (byte & 127) * context->core.in_packet.remaining_mult;
    context->core.in_packet.remaining_mult *= 128;
   }else{
    if(read_length == 0) return 1;
    if((*__errno_location ()) == 11 || (*__errno_location ()) == 11){
     return 0;
    }else{
     return 1;
    }
   }
  }while((byte & 128) != 0);
  if(context->core.in_packet.remaining_length > 0){
   context->core.in_packet.payload = _mosquitto_malloc(context->core.in_packet.remaining_length*sizeof(uint8_t));
   if(!context->core.in_packet.payload) return 1;
   context->core.in_packet.to_process = context->core.in_packet.remaining_length;
  }
  context->core.in_packet.have_remaining = 1;
 }
 while(context->core.in_packet.to_process>0){
  read_length = _mosquitto_net_read(&context->core, &(context->core.in_packet.payload[context->core.in_packet.pos]), context->core.in_packet.to_process);
  if(read_length > 0){
   bytes_received += read_length;
   context->core.in_packet.to_process -= read_length;
   context->core.in_packet.pos += read_length;
  }else{
   if((*__errno_location ()) == 11 || (*__errno_location ()) == 11){
    return 0;
   }else{
    return 1;
   }
  }
 }
 msgs_received++;
 context->core.in_packet.pos = 0;
 rc = mqtt3_packet_handle(db, context);
 _mosquitto_packet_cleanup(&context->core.in_packet);
 context->core.last_msg_in = time(((void *)0));
 return rc;
}

canonical_original_code:
int mqtt3_net_read(struct _mosquitto_db *db, struct _mqtt3_context *context)
{
 unsigned char byte;
 long read_length;
 int rc = 0;
 if(!context || context->core.sock == -1) return 1;
 if(!context->core.in_packet.command){
  read_length = _mosquitto_net_read(&context->core, &byte, 1);
  if(read_length == 1){
   bytes_received++;
   context->core.in_packet.command = byte;
   if(!(context->bridge) && context->core.state == mosq_cs_new && (byte&0xF0) != 0x10) return 1;
  }else{
   if(read_length == 0) return 1;
   if((*__errno_location ()) == 11 || (*__errno_location ()) == 11){
    return 0;
   }else{
    return 1;
   }
  }
 }
 if(!context->core.in_packet.have_remaining){
  do{
   read_length = _mosquitto_net_read(&context->core, &byte, 1);
   if(read_length == 1){
    context->core.in_packet.remaining_count++;
    if(context->core.in_packet.remaining_count > 4) return 2;
    bytes_received++;
    context->core.in_packet.remaining_length += (byte & 127) * context->core.in_packet.remaining_mult;
    context->core.in_packet.remaining_mult *= 128;
   }else{
    if(read_length == 0) return 1;
    if((*__errno_location ()) == 11 || (*__errno_location ()) == 11){
     return 0;
    }else{
     return 1;
    }
   }
  }while((byte & 128) != 0);
  if(context->core.in_packet.remaining_length > 0){
   context->core.in_packet.payload = _mosquitto_malloc(context->core.in_packet.remaining_length*sizeof(unsigned char));
   if(!context->core.in_packet.payload) return 1;
   context->core.in_packet.to_process = context->core.in_packet.remaining_length;
  }
  context->core.in_packet.have_remaining = 1;
 }
 while(context->core.in_packet.to_process>0){
  read_length = _mosquitto_net_read(&context->core, &(context->core.in_packet.payload[context->core.in_packet.pos]), context->core.in_packet.to_process);
  if(read_length > 0){
   bytes_received += read_length;
   context->core.in_packet.to_process -= read_length;
   context->core.in_packet.pos += read_length;
  }else{
   if((*__errno_location ()) == 11 || (*__errno_location ()) == 11){
    return 0;
   }else{
    return 1;
   }
  }
 }
 msgs_received++;
 context->core.in_packet.pos = 0;
 rc = mqtt3_packet_handle(db, context);
 _mosquitto_packet_cleanup(&context->core.in_packet);
 context->core.last_msg_in = time(((void *)0));
 return rc;
}

variable_types: struct _mosquitto_db { unsigned long last_db_id; struct _mosquitto_subhier { struct _mosquitto_subhier *children; struct _mosquitto_subhier *next; struct _mosquitto_subleaf { struct _mosquitto_subleaf *prev; struct _mosquitto_subleaf *next; struct _mqtt3_context { struct _mosquitto_core { int sock; char *id; char *username; char *password; unsigned short keepalive; enum mosquitto_client_state { mosq_cs_new=0, mosq_cs_connected=1, mosq_cs_disconnecting=2 } state; long last_msg_in; long last_msg_out; unsigned short last_mid; struct _mosquitto_packet { unsigned char command; unsigned char command_saved; unsigned char have_remaining; unsigned char remaining_count; unsigned short mid; unsigned int remaining_mult; unsigned int remaining_length; unsigned int to_process; unsigned int pos; unsigned char *payload; struct _mosquitto_packet *next; } in_packet; struct _mosquitto_packet *out_packet; struct mosquitto_message { unsigned short mid; char *topic; unsigned char *payload; unsigned int payloadlen; int qos; _Bool retain; } *will; } core; _Bool clean_session; _Bool duplicate; char *address; struct _mqtt3_bridge { char *name; char *address; unsigned short port; int keepalive; _Bool clean_session; struct _mqtt3_bridge_topic { char *topic; enum mqtt3_bridge_direction { bd_out=0, bd_in=1, bd_both=2 } direction; } *topics; int topic_count; long restart_t; char *username; char *password; } *bridge; struct _mosquitto_client_msg { struct _mosquitto_client_msg *next; struct mosquitto_msg_store { struct mosquitto_msg_store *next; unsigned long db_id; int ref_count; char *source_id; unsigned short source_mid; struct mosquitto_message msg; } *store; unsigned short mid; int qos; _Bool retain; long timestamp; enum mosquitto_msg_direction { mosq_md_in=0, mosq_md_out=1 } direction; enum mqtt3_msg_state { ms_invalid=0, ms_publish=1, ms_publish_puback=2, ms_wait_puback=3, ms_publish_pubrec=4, ms_wait_pubrec=5, ms_resend_pubrel=6, ms_wait_pubrel=7, ms_resend_pubcomp=8, ms_wait_pubcomp=9, ms_resend_pubrec=10, ms_queued=11 } state; _Bool dup; } *msgs; } *context; int qos; } *subs; char *topic; struct mosquitto_msg_store *retained; } subs; struct _mqtt3_context **contexts; int context_count; struct mosquitto_msg_store *msg_store; int msg_store_count; char *filepath; } *

variable_types:
context
struct _mqtt3_context *

variable_types:
byte
unsigned char

user_defined_types: struct _mosquitto_db { unsigned long last_db_id; struct _mosquitto_subhier subs; struct _mqtt3_context **contexts; int context_count; struct mosquitto_msg_store *msg_store; int msg_store_count; char *filepath; }

user_defined_types: struct _mosquitto_subhier { struct _mosquitto_subhier *children; struct _mosquitto_subhier *next; struct _mosquitto_subleaf *subs; char *topic; struct mosquitto_msg_store *retained; }

user_defined_types: struct _mosquitto_subleaf { struct _mosquitto_subleaf *prev; struct _mosquitto_subleaf *next; struct _mqtt3_context *context; int qos; }


original_code:
int mqtt3_net_write(mqtt3_context *context)
{
 uint8_t byte;
 ssize_t write_length;
 struct _mosquitto_packet *packet;
 if(!context || context->core.sock == -1) return 1;
 while(context->core.out_packet){
  packet = context->core.out_packet;
  if(packet->command){
   packet->to_process = packet->remaining_length;
   packet->pos = 0;
   write_length = _mosquitto_net_write(&context->core, &packet->command, 1);
   if(write_length == 1){
    bytes_sent++;
    packet->command = 0;
   }else{
    if(write_length == 0) return 1;
    if((*__errno_location ()) == 11 || (*__errno_location ()) == 11){
     return 0;
    }else{
     return 1;
    }
   }
  }
  if(!packet->have_remaining){
   do{
    byte = packet->remaining_length % 128;
    packet->remaining_length = packet->remaining_length / 128;
    if(packet->remaining_length>0){
     byte = byte | 0x80;
    }
    write_length = _mosquitto_net_write(&context->core, &byte, 1);
    if(write_length == 1){
     packet->remaining_count++;
     if(packet->remaining_count > 4) return 2;
     bytes_sent++;
    }else{
     if(write_length == 0) return 1;
     if((*__errno_location ()) == 11 || (*__errno_location ()) == 11){
      return 0;
     }else{
      return 1;
     }
    }
   }while(packet->remaining_length > 0);
   packet->have_remaining = 1;
  }
  while(packet->to_process > 0){
   write_length = _mosquitto_net_write(&context->core, &(packet->payload[packet->pos]), packet->to_process);
   if(write_length > 0){
    bytes_sent += write_length;
    packet->to_process -= write_length;
    packet->pos += write_length;
   }else{
    if((*__errno_location ()) == 11 || (*__errno_location ()) == 11){
     return 0;
    }else{
     return 1;
    }
   }
  }
  msgs_sent++;
  context->core.out_packet = packet->next;
  _mosquitto_packet_cleanup(packet);
  _mosquitto_free(packet);
  context->core.last_msg_out = time(((void *)0));
 }
 return 0;
}

canonical_original_code:
int mqtt3_net_write(struct _mqtt3_context *context)
{
 unsigned char byte;
 long write_length;
 struct _mosquitto_packet *packet;
 if(!context || context->core.sock == -1) return 1;
 while(context->core.out_packet){
  packet = context->core.out_packet;
  if(packet->command){
   packet->to_process = packet->remaining_length;
   packet->pos = 0;
   write_length = _mosquitto_net_write(&context->core, &packet->command, 1);
   if(write_length == 1){
    bytes_sent++;
    packet->command = 0;
   }else{
    if(write_length == 0) return 1;
    if((*__errno_location ()) == 11 || (*__errno_location ()) == 11){
     return 0;
    }else{
     return 1;
    }
   }
  }
  if(!packet->have_remaining){
   do{
    byte = packet->remaining_length % 128;
    packet->remaining_length = packet->remaining_length / 128;
    if(packet->remaining_length>0){
     byte = byte | 0x80;
    }
    write_length = _mosquitto_net_write(&context->core, &byte, 1);
    if(write_length == 1){
     packet->remaining_count++;
     if(packet->remaining_count > 4) return 2;
     bytes_sent++;
    }else{
     if(write_length == 0) return 1;
     if((*__errno_location ()) == 11 || (*__errno_location ()) == 11){
      return 0;
     }else{
      return 1;
     }
    }
   }while(packet->remaining_length > 0);
   packet->have_remaining = 1;
  }
  while(packet->to_process > 0){
   write_length = _mosquitto_net_write(&context->core, &(packet->payload[packet->pos]), packet->to_process);
   if(write_length > 0){
    bytes_sent += write_length;
    packet->to_process -= write_length;
    packet->pos += write_length;
   }else{
    if((*__errno_location ()) == 11 || (*__errno_location ()) == 11){
     return 0;
    }else{
     return 1;
    }
   }
  }
  msgs_sent++;
  context->core.out_packet = packet->next;
  _mosquitto_packet_cleanup(packet);
  _mosquitto_free(packet);
  context->core.last_msg_out = time(((void *)0));
 }
 return 0;
}

variable_types: struct _mqtt3_context { struct _mosquitto_core { int sock; char *id; char *username; char *password; unsigned short keepalive; enum mosquitto_client_state { mosq_cs_new=0, mosq_cs_connected=1, mosq_cs_disconnecting=2 } state; long last_msg_in; long last_msg_out; unsigned short last_mid; struct _mosquitto_packet { unsigned char command; unsigned char command_saved; unsigned char have_remaining; unsigned char remaining_count; unsigned short mid; unsigned int remaining_mult; unsigned int remaining_length; unsigned int to_process; unsigned int pos; unsigned char *payload; struct _mosquitto_packet *next; } in_packet; struct _mosquitto_packet *out_packet; struct mosquitto_message { unsigned short mid; char *topic; unsigned char *payload; unsigned int payloadlen; int qos; _Bool retain; } *will; } core; _Bool clean_session; _Bool duplicate; char *address; struct _mqtt3_bridge { char *name; char *address; unsigned short port; int keepalive; _Bool clean_session; struct _mqtt3_bridge_topic { char *topic; enum mqtt3_bridge_direction { bd_out=0, bd_in=1, bd_both=2 } direction; } *topics; int topic_count; long restart_t; char *username; char *password; } *bridge; struct _mosquitto_client_msg { struct _mosquitto_client_msg *next; struct mosquitto_msg_store { struct mosquitto_msg_store *next; unsigned long db_id; int ref_count; char *source_id; unsigned short source_mid; struct mosquitto_message msg; } *store; unsigned short mid; int qos; _Bool retain; long timestamp; enum mosquitto_msg_direction { mosq_md_in=0, mosq_md_out=1 } direction; enum mqtt3_msg_state { ms_invalid=0, ms_publish=1, ms_publish_puback=2, ms_wait_puback=3, ms_publish_pubrec=4, ms_wait_pubrec=5, ms_resend_pubrel=6, ms_wait_pubrel=7, ms_resend_pubcomp=8, ms_wait_pubcomp=9, ms_resend_pubrec=10, ms_queued=11 } state; _Bool dup; } *msgs; } *

variable_types: struct _mosquitto_packet { unsigned char command; unsigned char command_saved; unsigned char have_remaining; unsigned char remaining_count; unsigned short mid; unsigned int remaining_mult; unsigned int remaining_length; unsigned int to_process; unsigned int pos; unsigned char *payload; struct _mosquitto_packet *next; } *


decompiled_code:
__int64 mqtt3_net_bytes_total_received()
{
return bytes_received;
}

canonical_decompiled_code:
__int64 func8()
{
return bytes_received;
}

original_code:
uint64_t mqtt3_net_bytes_total_received(void)
{
 return bytes_received;
}

canonical_original_code:
unsigned long mqtt3_net_bytes_total_received()
{
 return bytes_received;
}

return_type: unsigned long


decompiled_code:
__int64 mqtt3_net_bytes_total_sent()
{
return bytes_sent;
}

canonical_decompiled_code:
__int64 func9()
{
return bytes_sent;
}

original_code:
uint64_t mqtt3_net_bytes_total_sent(void)
{
 return bytes_sent;
}

canonical_original_code:
unsigned long mqtt3_net_bytes_total_sent()
{
 return bytes_sent;
}


decompiled_code:
__int64 mqtt3_net_msgs_total_received()
{
return msgs_received;
}

canonical_decompiled_code:
__int64 func10()
{
return msgs_received;
}

original_code:
unsigned long mqtt3_net_msgs_total_received(void)
{
 return msgs_received;
}

canonical_original_code:
unsigned long mqtt3_net_msgs_total_received()
{
 return msgs_received;
}


decompiled_code:
__int64 mqtt3_net_msgs_total_sent()
{
return msgs_sent;
}

canonical_decompiled_code:
__int64 func11()
{
return msgs_sent;
}

original_code:
unsigned long mqtt3_net_msgs_total_sent(void)
{
 return msgs_sent;
}

canonical_original_code:
unsigned long mqtt3_net_msgs_total_sent()
{
 return msgs_sent;
}


decompiled_code:
__int64 __fastcall mqtt3_handle_connect(__int64 a1, int *a2)
{
unsigned int v2; // er12
int v3; // eax
int *v4; // r13
_DWORD *v6; // rbp
char v7; // r14
__int64 v9; // r13
__int64 v10; // rdx
char *v11; // r14
__int64 v12; // r13
__int64 v13; // r12
__int64 v14; // r15
const char *v15; // rdi
int v16; // eax
int v17; // eax
char *v18; // rdi
char *v19; // rax
char *v20; // rdi
__int64 v21; // rbx
__int64 v22; // rdi
__int64 v23; // r13
__int64 v24; // rax
int v25; // edx
_DWORD *v26; // rax
__int64 v27; // [rsp+0h] [rbp-78h]
unsigned __int8 v28; // [rsp+Eh] [rbp-6Ah]
char v29; // [rsp+Fh] [rbp-69h]
unsigned __int8 v30; // [rsp+16h] [rbp-62h] BYREF
char v31; // [rsp+17h] [rbp-61h] BYREF
const char *v32; // [rsp+18h] [rbp-60h] BYREF
char *s2; // [rsp+20h] [rbp-58h] BYREF
char *s; // [rsp+28h] [rbp-50h] BYREF
char v35[8]; // [rsp+30h] [rbp-48h] BYREF
char v36[64]; // [rsp+38h] [rbp-40h] BYREF

v2 = 2;
v3 = a2[9];
s = 0LL;
if ( !v3 )
{
v4 = a2 + 16;
v6 = a2;
if ( (unsigned int)mosquitto_read_string(a2 + 16, &v32) )
return 1;
if ( !v32 )
{
v2 = 3;
mqtt3_socket_close(a2);
return v2;
}
if ( strcmp(v32, "MQIsdp") )
{
mqtt3_log_printf(1LL, "Invalid protocol \"%s\" in CONNECT from %s.", v32, *((const char **)a2 + 16));
mosquitto_free(v32);
mqtt3_socket_close(a2);
return v2;
}
if ( (unsigned int)mosquitto_read_byte(v4, &v30) )
return 1;
if ( v30 != 3 )
{
mqtt3_log_printf(1LL, "Invalid protocol version %d in CONNECT from %s.", v30, *((const char **)a2 + 16));
mosquitto_free(v32);
mqtt3_raw_connack(a2, 1LL);
mqtt3_socket_close(a2);
return v2;
}
mosquitto_free(v32);
if ( (unsigned int)mosquitto_read_byte(v4, &v31) )
return 1;
v7 = v31;
v28 = v31;
if ( (unsigned int)mosquitto_read_uint16(v4, a2 + 8) || (unsigned int)mosquitto_read_string(v4, &s2) )
return 1;
v9 = *(int *)(a1 + 56);
v29 = v7 & 2;
if ( (int)v9 > 0 )
{
v10 = *(_QWORD *)(a1 + 48);
v11 = s2;
v12 = 8 * v9;
v13 = 0LL;
while ( 1 )
{
v14 = *(_QWORD *)(v10 + v13);
if ( v14 )
{
v15 = *(const char **)(v14 + 8);
if ( v15 )
{
v27 = v10;
v16 = strcmp(v15, v11);
v10 = v27;
if ( !v16 )
break;
}
}
v13 += 8LL;
if ( v12 == v13 )
goto LABEL_20;
}
if ( *(_DWORD *)v14 != -1 )
{
mqtt3_log_printf(8LL, "Client %s already connected, closing old connection.", v11);
v14 = *(_QWORD *)(*(_QWORD *)(a1 + 48) + v13);
}
if ( v29 )
*(_BYTE *)(v14 + 120) = 1;
mqtt3_context_cleanup(a1, v14, 0LL);
v22 = *((_QWORD *)a2 + 16);
v23 = *(_QWORD *)(*(_QWORD *)(a1 + 48) + v13);
*(_DWORD *)(v23 + 36) = 1;
v24 = mosquitto_strdup(v22);
v25 = *a2;
*(_QWORD *)(v23 + 128) = v24;
v26 = *(_DWORD **)(*(_QWORD *)(a1 + 48) + v13);
*v26 = v25;
*a2 = -1;
a2[9] = 2;
v6 = v26;
}
LABEL_20:
if ( (v31 & 4) != 0 )
{
v19 = (char *)malloc(0x28uLL);
*((_QWORD *)v6 + 14) = v19;
if ( !v19
|| (unsigned int)mosquitto_read_string(v6 + 16, v19 + 8)
|| (unsigned int)mosquitto_read_string(v6 + 16, &s) )
{
return 1;
}
v20 = s;
v21 = *((_QWORD *)v6 + 14);
if ( s )
{
*(_QWORD *)(v21 + 16) = s;
*(_DWORD *)(v21 + 24) = strlen(v20);
}
else
{
*(_QWORD *)(v21 + 16) = 0LL;
*(_DWORD *)(v21 + 24) = 0;
}
*(_DWORD *)(v21 + 28) = (v28 >> 3) & 3;
*(_BYTE *)(v21 + 32) = (v28 & 0x20) != 0;
}
if ( (v28 & 0x40) != 0 )
{
v17 = mosquitto_read_string(v6 + 16, v35);
if ( v17 )
{
if ( v17 == 1 )
return 1;
}
else if ( (v28 & 0x80u) != 0 && (unsigned int)mosquitto_read_string(v6 + 16, v36) == 1 )
{
return 1;
}
}
mqtt3_log_printf(16LL, "Received CONNECT from %s as %s", *((const char **)v6 + 16), s2);
v18 = s;
*((_QWORD *)v6 + 1) = s2;
*((_BYTE *)v6 + 120) = v29 != 0;
if ( v18 )
mosquitto_free(v18);
v6[9] = 1;
v2 = mqtt3_raw_connack(v6, 0LL);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int *a2)
{
unsigned int v2; // er12
int v3; // eax
int *v4; // r13
_DWORD *v6; // rbp
char v7; // r14
__int64 v9; // r13
__int64 v10; // rdx
char *v11; // r14
__int64 v12; // r13
__int64 v13; // r12
__int64 v14; // r15
const char *v15; // rdi
int v16; // eax
int v17; // eax
char *v18; // rdi
char *v19; // rax
char *v20; // rdi
__int64 v21; // rbx
__int64 v22; // rdi
__int64 v23; // r13
__int64 v24; // rax
int v25; // edx
_DWORD *v26; // rax
__int64 v27; // [rsp+0h] [rbp-78h]
unsigned __int8 v28; // [rsp+Eh] [rbp-6Ah]
char v29; // [rsp+Fh] [rbp-69h]
unsigned __int8 v30; // [rsp+16h] [rbp-62h] BYREF
char v31; // [rsp+17h] [rbp-61h] BYREF
const char *v32; // [rsp+18h] [rbp-60h] BYREF
char *s2; // [rsp+20h] [rbp-58h] BYREF
char *s; // [rsp+28h] [rbp-50h] BYREF
char v35[8]; // [rsp+30h] [rbp-48h] BYREF
char v36[64]; // [rsp+38h] [rbp-40h] BYREF

v2 = 2;
v3 = a2[9];
s = 0LL;
if ( !v3 )
{
v4 = a2 + 16;
v6 = a2;
if ( (unsigned int)mosquitto_read_string(a2 + 16, &v32) )
return 1;
if ( !v32 )
{
v2 = 3;
mqtt3_socket_close(a2);
return v2;
}
if ( strcmp(v32, "MQIsdp") )
{
mqtt3_log_printf(1LL, "Invalid protocol \"%s\" in CONNECT from %s.", v32, *((const char **)a2 + 16));
mosquitto_free(v32);
mqtt3_socket_close(a2);
return v2;
}
if ( (unsigned int)mosquitto_read_byte(v4, &v30) )
return 1;
if ( v30 != 3 )
{
mqtt3_log_printf(1LL, "Invalid protocol version %d in CONNECT from %s.", v30, *((const char **)a2 + 16));
mosquitto_free(v32);
mqtt3_raw_connack(a2, 1LL);
mqtt3_socket_close(a2);
return v2;
}
mosquitto_free(v32);
if ( (unsigned int)mosquitto_read_byte(v4, &v31) )
return 1;
v7 = v31;
v28 = v31;
if ( (unsigned int)mosquitto_read_uint16(v4, a2 + 8) || (unsigned int)mosquitto_read_string(v4, &s2) )
return 1;
v9 = *(int *)(a1 + 56);
v29 = v7 & 2;
if ( (int)v9 > 0 )
{
v10 = *(_QWORD *)(a1 + 48);
v11 = s2;
v12 = 8 * v9;
v13 = 0LL;
while ( 1 )
{
v14 = *(_QWORD *)(v10 + v13);
if ( v14 )
{
v15 = *(const char **)(v14 + 8);
if ( v15 )
{
v27 = v10;
v16 = strcmp(v15, v11);
v10 = v27;
if ( !v16 )
break;
}
}
v13 += 8LL;
if ( v12 == v13 )
goto LABEL_20;
}
if ( *(_DWORD *)v14 != -1 )
{
mqtt3_log_printf(8LL, "Client %s already connected, closing old connection.", v11);
v14 = *(_QWORD *)(*(_QWORD *)(a1 + 48) + v13);
}
if ( v29 )
*(_BYTE *)(v14 + 120) = 1;
mqtt3_context_cleanup(a1, v14, 0LL);
v22 = *((_QWORD *)a2 + 16);
v23 = *(_QWORD *)(*(_QWORD *)(a1 + 48) + v13);
*(_DWORD *)(v23 + 36) = 1;
v24 = mosquitto_strdup(v22);
v25 = *a2;
*(_QWORD *)(v23 + 128) = v24;
v26 = *(_DWORD **)(*(_QWORD *)(a1 + 48) + v13);
*v26 = v25;
*a2 = -1;
a2[9] = 2;
v6 = v26;
}
LABEL_20:
if ( (v31 & 4) != 0 )
{
v19 = (char *)malloc(0x28uLL);
*((_QWORD *)v6 + 14) = v19;
if ( !v19
|| (unsigned int)mosquitto_read_string(v6 + 16, v19 + 8)
|| (unsigned int)mosquitto_read_string(v6 + 16, &s) )
{
return 1;
}
v20 = s;
v21 = *((_QWORD *)v6 + 14);
if ( s )
{
*(_QWORD *)(v21 + 16) = s;
*(_DWORD *)(v21 + 24) = strlen(v20);
}
else
{
*(_QWORD *)(v21 + 16) = 0LL;
*(_DWORD *)(v21 + 24) = 0;
}
*(_DWORD *)(v21 + 28) = (v28 >> 3) & 3;
*(_BYTE *)(v21 + 32) = (v28 & 0x20) != 0;
}
if ( (v28 & 0x40) != 0 )
{
v17 = mosquitto_read_string(v6 + 16, v35);
if ( v17 )
{
if ( v17 == 1 )
return 1;
}
else if ( (v28 & 0x80u) != 0 && (unsigned int)mosquitto_read_string(v6 + 16, v36) == 1 )
{
return 1;
}
}
mqtt3_log_printf(16LL, "Received CONNECT from %s as %s", *((const char **)v6 + 16), s2);
v18 = s;
*((_QWORD *)v6 + 1) = s2;
*((_BYTE *)v6 + 120) = v29 != 0;
if ( v18 )
mosquitto_free(v18);
v6[9] = 1;
v2 = mqtt3_raw_connack(v6, 0LL);
}
return v2;
}

original_code:
int mqtt3_handle_connect(mosquitto_db *db, mqtt3_context *context)
{
 char *protocol_name;
 uint8_t protocol_version;
 uint8_t connect_flags;
 char *client_id;
 char *will_topic = ((void *)0), *will_message = ((void *)0);
 uint8_t will, will_retain, will_qos, clean_session;
 uint8_t username_flag, password_flag;
 char *username, *password;
 int i;
 int rc;
 if(context->core.state != mosq_cs_new) return 2;
 if(_mosquitto_read_string(&context->core.in_packet, &protocol_name)) return 1;
 if(!protocol_name){
  mqtt3_socket_close(context);
  return 3;
 }
 if(strcmp(protocol_name, "MQIsdp")){
  mqtt3_log_printf(0x01, "Invalid protocol \"%s\" in CONNECT from %s.",
    protocol_name, context->address);
  _mosquitto_free(protocol_name);
  mqtt3_socket_close(context);
  return 2;
 }
 if(_mosquitto_read_byte(&context->core.in_packet, &protocol_version)) return 1;
 if(protocol_version != 3){
  mqtt3_log_printf(0x01, "Invalid protocol version %d in CONNECT from %s.",
    protocol_version, context->address);
  _mosquitto_free(protocol_name);
  mqtt3_raw_connack(context, 1);
  mqtt3_socket_close(context);
  return 2;
 }
 _mosquitto_free(protocol_name);
 if(_mosquitto_read_byte(&context->core.in_packet, &connect_flags)) return 1;
 clean_session = connect_flags & 0x02;
 will = connect_flags & 0x04;
 will_qos = (connect_flags & 0x18) >> 3;
 will_retain = connect_flags & 0x20;
 username_flag = connect_flags & 0x40;
 password_flag = connect_flags & 0x80;
 if(_mosquitto_read_uint16(&context->core.in_packet, &(context->core.keepalive))) return 1;
 if(_mosquitto_read_string(&context->core.in_packet, &client_id)) return 1;
 for(i=0; i<db->context_count; i++){
  if(db->contexts[i] && db->contexts[i]->core.id && !strcmp(db->contexts[i]->core.id, client_id)){
   if(db->contexts[i]->core.sock == -1){
   }else{
    mqtt3_log_printf(0x08, "Client %s already connected, closing old connection.", client_id);
   }
   if(clean_session){
    db->contexts[i]->clean_session = 1;
   }
   mqtt3_context_cleanup(db, db->contexts[i], 0);
   db->contexts[i]->core.state = mosq_cs_connected;
   db->contexts[i]->address = _mosquitto_strdup(context->address);
   db->contexts[i]->core.sock = context->core.sock;
   context->core.sock = -1;
   context->core.state = mosq_cs_disconnecting;
   context = db->contexts[i];
   break;
  }
 }
 if(connect_flags & 0x04){
  context->core.will = malloc(sizeof(struct mosquitto_message));
  if(!context->core.will) return 1;
  if(_mosquitto_read_string(&context->core.in_packet, &context->core.will->topic)) return 1;
  if(_mosquitto_read_string(&context->core.in_packet, &will_message)) return 1;
  if(will_message){
   context->core.will->payload = (uint8_t *)will_message;
   context->core.will->payloadlen = strlen(will_message);
  }else{
   context->core.will->payload = ((void *)0);
   context->core.will->payloadlen = 0;
  }
  context->core.will->qos = will_qos;
  context->core.will->retain = will_retain;
 }
 if(username_flag){
  rc = _mosquitto_read_string(&context->core.in_packet, &username);
  if(rc == 0){
   if(password_flag){
    rc = _mosquitto_read_string(&context->core.in_packet, &password);
    if(rc == 1){
     return 1;
    }else if(rc == 2){
     password_flag = 0;
    }
   }
  }else if(rc == 1){
   return 1;
  }else{
   username_flag = 0;
  }
 }
 mqtt3_log_printf(0x10, "Received CONNECT from %s as %s", context->address, client_id);
 context->core.id = client_id;
 context->clean_session = clean_session;
 if(will_topic) _mosquitto_free(will_topic);
 if(will_message) _mosquitto_free(will_message);
 context->core.state = mosq_cs_connected;
 return mqtt3_raw_connack(context, 0);
}

canonical_original_code:
int mqtt3_handle_connect(struct _mosquitto_db *db, struct _mqtt3_context *context)
{
 char *protocol_name;
 unsigned char protocol_version;
 unsigned char connect_flags;
 char *client_id;
 char *will_topic = ((void *)0); char *will_message = ((void *)0);
 unsigned char will; unsigned char will_retain; unsigned char will_qos; unsigned char clean_session;
 unsigned char username_flag; unsigned char password_flag;
 char *username; char *password;
 int i;
 int rc;
 if(context->core.state != mosq_cs_new) return 2;
 if(_mosquitto_read_string(&context->core.in_packet, &protocol_name)) return 1;
 if(!protocol_name){
  mqtt3_socket_close(context);
  return 3;
 }
 if(strcmp(protocol_name, "MQIsdp")){
  mqtt3_log_printf(0x01, "Invalid protocol \"%s\" in CONNECT from %s.",
    protocol_name, context->address);
  _mosquitto_free(protocol_name);
  mqtt3_socket_close(context);
  return 2;
 }
 if(_mosquitto_read_byte(&context->core.in_packet, &protocol_version)) return 1;
 if(protocol_version != 3){
  mqtt3_log_printf(0x01, "Invalid protocol version %d in CONNECT from %s.",
    protocol_version, context->address);
  _mosquitto_free(protocol_name);
  mqtt3_raw_connack(context, 1);
  mqtt3_socket_close(context);
  return 2;
 }
 _mosquitto_free(protocol_name);
 if(_mosquitto_read_byte(&context->core.in_packet, &connect_flags)) return 1;
 clean_session = connect_flags & 0x02;
 will = connect_flags & 0x04;
 will_qos = (connect_flags & 0x18) >> 3;
 will_retain = connect_flags & 0x20;
 username_flag = connect_flags & 0x40;
 password_flag = connect_flags & 0x80;
 if(_mosquitto_read_uint16(&context->core.in_packet, &(context->core.keepalive))) return 1;
 if(_mosquitto_read_string(&context->core.in_packet, &client_id)) return 1;
 for(i=0; i<db->context_count; i++){
  if(db->contexts[i] && db->contexts[i]->core.id && !strcmp(db->contexts[i]->core.id, client_id)){
   if(db->contexts[i]->core.sock == -1){
   }else{
    mqtt3_log_printf(0x08, "Client %s already connected, closing old connection.", client_id);
   }
   if(clean_session){
    db->contexts[i]->clean_session = 1;
   }
   mqtt3_context_cleanup(db, db->contexts[i], 0);
   db->contexts[i]->core.state = mosq_cs_connected;
   db->contexts[i]->address = _mosquitto_strdup(context->address);
   db->contexts[i]->core.sock = context->core.sock;
   context->core.sock = -1;
   context->core.state = mosq_cs_disconnecting;
   context = db->contexts[i];
   break;
  }
 }
 if(connect_flags & 0x04){
  context->core.will = malloc(sizeof(struct mosquitto_message));
  if(!context->core.will) return 1;
  if(_mosquitto_read_string(&context->core.in_packet, &context->core.will->topic)) return 1;
  if(_mosquitto_read_string(&context->core.in_packet, &will_message)) return 1;
  if(will_message){
   context->core.will->payload = (unsigned char *)will_message;
   context->core.will->payloadlen = strlen(will_message);
  }else{
   context->core.will->payload = ((void *)0);
   context->core.will->payloadlen = 0;
  }
  context->core.will->qos = will_qos;
  context->core.will->retain = will_retain;
 }
 if(username_flag){
  rc = _mosquitto_read_string(&context->core.in_packet, &username);
  if(rc == 0){
   if(password_flag){
    rc = _mosquitto_read_string(&context->core.in_packet, &password);
    if(rc == 1){
     return 1;
    }else if(rc == 2){
     password_flag = 0;
    }
   }
  }else if(rc == 1){
   return 1;
  }else{
   username_flag = 0;
  }
 }
 mqtt3_log_printf(0x10, "Received CONNECT from %s as %s", context->address, client_id);
 context->core.id = client_id;
 context->clean_session = clean_session;
 if(will_topic) _mosquitto_free(will_topic);
 if(will_message) _mosquitto_free(will_message);
 context->core.state = mosq_cs_connected;
 return mqtt3_raw_connack(context, 0);
}

variable_types:
will
unsigned char

variable_types:
username
char *

variable_types:
password
char *

user_defined_types: struct mosquitto_message { unsigned short mid; char *topic; unsigned char *payload; unsigned int payloadlen; int qos; _Bool retain; }

user_defined_types: enum mosquitto_client_state { mosq_cs_new=0, mosq_cs_connected=1, mosq_cs_disconnecting=2 }

user_defined_types: struct _mosquitto_packet { unsigned char command; unsigned char command_saved; unsigned char have_remaining; unsigned char remaining_count; unsigned short mid; unsigned int remaining_mult; unsigned int remaining_length; unsigned int to_process; unsigned int pos; unsigned char *payload; struct _mosquitto_packet *next; }


decompiled_code:
__int64 __fastcall mqtt3_handle_disconnect(__int64 a1)
{
__int64 result; // rax

if ( !a1 )
return 1LL;
result = 2LL;
if ( !*(_DWORD *)(a1 + 76) )
{
mqtt3_log_printf(16LL, "Received DISCONNECT from %s", *(const char **)(a1 + 8));
*(_DWORD *)(a1 + 36) = 2;
mqtt3_socket_close(a1);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
__int64 result; // rax

if ( !a1 )
return 1LL;
result = 2LL;
if ( !*(_DWORD *)(a1 + 76) )
{
mqtt3_log_printf(16LL, "Received DISCONNECT from %s", *(const char **)(a1 + 8));
*(_DWORD *)(a1 + 36) = 2;
mqtt3_socket_close(a1);
result = 0LL;
}
return result;
}

original_code:
int mqtt3_handle_disconnect(mqtt3_context *context)
{
 if(!context){
  return 1;
 }
 if(context->core.in_packet.remaining_length != 0){
  return 2;
 }
 mqtt3_log_printf(0x10, "Received DISCONNECT from %s", context->core.id);
 context->core.state = mosq_cs_disconnecting;
 mqtt3_socket_close(context);
 return 0;
}

canonical_original_code:
int mqtt3_handle_disconnect(struct _mqtt3_context *context)
{
 if(!context){
  return 1;
 }
 if(context->core.in_packet.remaining_length != 0){
  return 2;
 }
 mqtt3_log_printf(0x10, "Received DISCONNECT from %s", context->core.id);
 context->core.state = mosq_cs_disconnecting;
 mqtt3_socket_close(context);
 return 0;
}

user_defined_types: enum mosquitto_msg_direction { mosq_md_in=0, mosq_md_out=1 }

user_defined_types: enum mqtt3_msg_state { ms_invalid=0, ms_publish=1, ms_publish_puback=2, ms_wait_puback=3, ms_publish_pubrec=4, ms_wait_pubrec=5, ms_resend_pubrel=6, ms_wait_pubrel=7, ms_resend_pubcomp=8, ms_wait_pubcomp=9, ms_resend_pubrec=10, ms_queued=11 }

user_defined_types: struct mosquitto_msg_store { struct mosquitto_msg_store *next; unsigned long db_id; int ref_count; char *source_id; unsigned short source_mid; struct mosquitto_message msg; }