decompiled_code:
__int64 __fastcall mqtt3_handle_subscribe(__int64 a1, __int64 a2)
{
unsigned int v2; // er13
__int64 v3; // rbx
__int64 v4; // rbp
__int64 v5; // rax
__int64 v6; // rdx
const char *v7; // rdi
unsigned __int8 v9; // [rsp+15h] [rbp-43h] BYREF
unsigned __int16 v10; // [rsp+16h] [rbp-42h] BYREF
const char *v11[8]; // [rsp+18h] [rbp-40h] BYREF

if ( !a2 )
return 1;
mqtt3_log_printf(16LL, "Received SUBSCRIBE from %s", *(const char **)(a2 + 8));
v2 = mosquitto_read_uint16(a2 + 64, &v10);
if ( v2 )
return 1;
if ( *(_DWORD *)(a2 + 76) > *(_DWORD *)(a2 + 84) )
{
v3 = 0LL;
v4 = 0LL;
while ( 1 )
{
v11[0] = 0LL;
if ( (unsigned int)mosquitto_read_string(a2 + 64, v11) )
break;
if ( v11[0] )
{
if ( (unsigned int)mosquitto_read_byte(a2 + 64, &v9) )
goto LABEL_19;
if ( v9 > 2u )
{
mqtt3_log_printf(1LL, "Invalid QoS in subscription command from %s, disconnecting.", *(_QWORD *)(a2 + 128));
LABEL_19:
v7 = v11[0];
goto LABEL_20;
}
if ( (unsigned int)mosquitto_fix_sub_topic(v11) )
goto LABEL_19;
if ( !*v11[0] )
{
mqtt3_log_printf(1LL, "Empty subscription string from %s, disconnecting.", *(_QWORD *)(a2 + 128));
goto LABEL_19;
}
mqtt3_log_printf(16LL, "\t%s (QoS %d)", v11[0], v9);
mqtt3_sub_add(a2, v11[0], v9, a1 + 8);
if ( (unsigned int)mqtt3_retain_queue(a1, a2, v11[0], v9) )
v2 = 1;
mosquitto_free(v11[0]);
}
v5 = mosquitto_realloc(v4);
v6 = (unsigned int)(v3 + 1);
v4 = v5;
*(_BYTE *)(v5 + v3) = v9;
if ( *(_DWORD *)(a2 + 84) >= *(_DWORD *)(a2 + 76) )
goto LABEL_26;
v3 = (unsigned int)v6;
}
v7 = v11[0];
if ( !v11[0] )
goto LABEL_21;
LABEL_20:
mosquitto_free(v7);
LABEL_21:
if ( v4 )
mosquitto_free(v4);
return 1;
}
v6 = 0LL;
v4 = 0LL;
LABEL_26:
if ( (unsigned int)mqtt3_raw_suback(a2, v10, v6, v4) )
v2 = 1;
mosquitto_free(v4);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2)
{
unsigned int v2; // er13
__int64 v3; // rbx
__int64 v4; // rbp
__int64 v5; // rax
__int64 v6; // rdx
const char *v7; // rdi
unsigned __int8 v9; // [rsp+15h] [rbp-43h] BYREF
unsigned __int16 v10; // [rsp+16h] [rbp-42h] BYREF
const char *v11[8]; // [rsp+18h] [rbp-40h] BYREF

if ( !a2 )
return 1;
mqtt3_log_printf(16LL, "Received SUBSCRIBE from %s", *(const char **)(a2 + 8));
v2 = mosquitto_read_uint16(a2 + 64, &v10);
if ( v2 )
return 1;
if ( *(_DWORD *)(a2 + 76) > *(_DWORD *)(a2 + 84) )
{
v3 = 0LL;
v4 = 0LL;
while ( 1 )
{
v11[0] = 0LL;
if ( (unsigned int)mosquitto_read_string(a2 + 64, v11) )
break;
if ( v11[0] )
{
if ( (unsigned int)mosquitto_read_byte(a2 + 64, &v9) )
goto LABEL_19;
if ( v9 > 2u )
{
mqtt3_log_printf(1LL, "Invalid QoS in subscription command from %s, disconnecting.", *(_QWORD *)(a2 + 128));
LABEL_19:
v7 = v11[0];
goto LABEL_20;
}
if ( (unsigned int)mosquitto_fix_sub_topic(v11) )
goto LABEL_19;
if ( !*v11[0] )
{
mqtt3_log_printf(1LL, "Empty subscription string from %s, disconnecting.", *(_QWORD *)(a2 + 128));
goto LABEL_19;
}
mqtt3_log_printf(16LL, "\t%s (QoS %d)", v11[0], v9);
mqtt3_sub_add(a2, v11[0], v9, a1 + 8);
if ( (unsigned int)mqtt3_retain_queue(a1, a2, v11[0], v9) )
v2 = 1;
mosquitto_free(v11[0]);
}
v5 = mosquitto_realloc(v4);
v6 = (unsigned int)(v3 + 1);
v4 = v5;
*(_BYTE *)(v5 + v3) = v9;
if ( *(_DWORD *)(a2 + 84) >= *(_DWORD *)(a2 + 76) )
goto LABEL_26;
v3 = (unsigned int)v6;
}
v7 = v11[0];
if ( !v11[0] )
goto LABEL_21;
LABEL_20:
mosquitto_free(v7);
LABEL_21:
if ( v4 )
mosquitto_free(v4);
return 1;
}
v6 = 0LL;
v4 = 0LL;
LABEL_26:
if ( (unsigned int)mqtt3_raw_suback(a2, v10, v6, v4) )
v2 = 1;
mosquitto_free(v4);
return v2;
}

original_code:
int mqtt3_handle_subscribe(mosquitto_db *db, mqtt3_context *context)
{
 int rc = 0;
 uint16_t mid;
 char *sub;
 uint8_t qos;
 uint8_t *payload = ((void *)0);
 uint32_t payloadlen = 0;
 if(!context) return 1;
 mqtt3_log_printf(0x10, "Received SUBSCRIBE from %s", context->core.id);
 if(_mosquitto_read_uint16(&context->core.in_packet, &mid)) return 1;
 while(context->core.in_packet.pos < context->core.in_packet.remaining_length){
  sub = ((void *)0);
  if(_mosquitto_read_string(&context->core.in_packet, &sub)){
   if(sub) _mosquitto_free(sub);
   if(payload) _mosquitto_free(payload);
   return 1;
  }
  if(sub){
   if(_mosquitto_read_byte(&context->core.in_packet, &qos)){
    _mosquitto_free(sub);
    if(payload) _mosquitto_free(payload);
    return 1;
   }
   if(qos > 2){
    mqtt3_log_printf(0x01, "Invalid QoS in subscription command from %s, disconnecting.",
     context->address);
    _mosquitto_free(sub);
    if(payload) _mosquitto_free(payload);
    return 1;
   }
   if(_mosquitto_fix_sub_topic(&sub)){
    _mosquitto_free(sub);
    if(payload) _mosquitto_free(payload);
    return 1;
   }
   if(!strlen(sub)){
    mqtt3_log_printf(0x01, "Empty subscription string from %s, disconnecting.",
     context->address);
    _mosquitto_free(sub);
    if(payload) _mosquitto_free(payload);
    return 1;
   }
   mqtt3_log_printf(0x10, "\t%s (QoS %d)", sub, qos);
   mqtt3_sub_add(context, sub, qos, &db->subs);
   if(mqtt3_retain_queue(db, context, sub, qos)) rc = 1;
   _mosquitto_free(sub);
  }
  payload = _mosquitto_realloc(payload, payloadlen + 1);
  payload[payloadlen] = qos;
  payloadlen++;
 }
 if(mqtt3_raw_suback(context, mid, payloadlen, payload)) rc = 1;
 _mosquitto_free(payload);
 return rc;
}

canonical_original_code:
int mqtt3_handle_subscribe(struct _mosquitto_db *db, struct _mqtt3_context *context)
{
 int rc = 0;
 unsigned short mid;
 char *sub;
 unsigned char qos;
 unsigned char *payload = ((void *)0);
 unsigned int payloadlen = 0;
 if(!context) return 1;
 mqtt3_log_printf(0x10, "Received SUBSCRIBE from %s", context->core.id);
 if(_mosquitto_read_uint16(&context->core.in_packet, &mid)) return 1;
 while(context->core.in_packet.pos < context->core.in_packet.remaining_length){
  sub = ((void *)0);
  if(_mosquitto_read_string(&context->core.in_packet, &sub)){
   if(sub) _mosquitto_free(sub);
   if(payload) _mosquitto_free(payload);
   return 1;
  }
  if(sub){
   if(_mosquitto_read_byte(&context->core.in_packet, &qos)){
    _mosquitto_free(sub);
    if(payload) _mosquitto_free(payload);
    return 1;
   }
   if(qos > 2){
    mqtt3_log_printf(0x01, "Invalid QoS in subscription command from %s, disconnecting.",
     context->address);
    _mosquitto_free(sub);
    if(payload) _mosquitto_free(payload);
    return 1;
   }
   if(_mosquitto_fix_sub_topic(&sub)){
    _mosquitto_free(sub);
    if(payload) _mosquitto_free(payload);
    return 1;
   }
   if(!strlen(sub)){
    mqtt3_log_printf(0x01, "Empty subscription string from %s, disconnecting.",
     context->address);
    _mosquitto_free(sub);
    if(payload) _mosquitto_free(payload);
    return 1;
   }
   mqtt3_log_printf(0x10, "\t%s (QoS %d)", sub, qos);
   mqtt3_sub_add(context, sub, qos, &db->subs);
   if(mqtt3_retain_queue(db, context, sub, qos)) rc = 1;
   _mosquitto_free(sub);
  }
  payload = _mosquitto_realloc(payload, payloadlen + 1);
  payload[payloadlen] = qos;
  payloadlen++;
 }
 if(mqtt3_raw_suback(context, mid, payloadlen, payload)) rc = 1;
 _mosquitto_free(payload);
 return rc;
}

variable_types:
mid
unsigned short

variable_types:
sub
char *

variable_types:
qos
unsigned char

variable_types:
payload
unsigned char *

variable_types:
payloadlen
unsigned int

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: enum mqtt3_bridge_direction { bd_out=0, bd_in=1, bd_both=2 }


repo: adamvr/mosquitto-homebrew-mods

decompiled_code:
_BOOL8 __fastcall mqtt3_handle_unsubscribe(__int64 a1, __int64 a2)
{
unsigned __int16 v3; // [rsp+0h] [rbp-22h] BYREF
const char *v4; // [rsp+2h] [rbp-20h] BYREF

if ( !a2 )
return 1LL;
mqtt3_log_printf(16LL, "Received UNSUBSCRIBE from %s", *(const char **)(a2 + 8));
if ( !(unsigned int)mosquitto_read_uint16(a2 + 64, &v3) )
{
while ( 1 )
{
if ( *(_DWORD *)(a2 + 84) >= *(_DWORD *)(a2 + 76) )
return (unsigned int)mqtt3_send_command_with_mid(a2, 176LL, v3, 0LL) != 0;
v4 = 0LL;
if ( (unsigned int)mosquitto_read_string(a2 + 64, &v4) )
break;
if ( v4 )
{
mqtt3_log_printf(16LL, "\t%s", v4);
mqtt3_sub_remove(a2, v4, a1 + 8);
mosquitto_free(v4);
}
}
if ( v4 )
mosquitto_free(v4);
}
return 1LL;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(__int64 a1, __int64 a2)
{
unsigned __int16 v3; // [rsp+0h] [rbp-22h] BYREF
const char *v4; // [rsp+2h] [rbp-20h] BYREF

if ( !a2 )
return 1LL;
mqtt3_log_printf(16LL, "Received UNSUBSCRIBE from %s", *(const char **)(a2 + 8));
if ( !(unsigned int)mosquitto_read_uint16(a2 + 64, &v3) )
{
while ( 1 )
{
if ( *(_DWORD *)(a2 + 84) >= *(_DWORD *)(a2 + 76) )
return (unsigned int)mqtt3_send_command_with_mid(a2, 176LL, v3, 0LL) != 0;
v4 = 0LL;
if ( (unsigned int)mosquitto_read_string(a2 + 64, &v4) )
break;
if ( v4 )
{
mqtt3_log_printf(16LL, "\t%s", v4);
mqtt3_sub_remove(a2, v4, a1 + 8);
mosquitto_free(v4);
}
}
if ( v4 )
mosquitto_free(v4);
}
return 1LL;
}

original_code:
int mqtt3_handle_unsubscribe(mosquitto_db *db, mqtt3_context *context)
{
 uint16_t mid;
 char *sub;
 if(!context) return 1;
 mqtt3_log_printf(0x10, "Received UNSUBSCRIBE from %s", context->core.id);
 if(_mosquitto_read_uint16(&context->core.in_packet, &mid)) return 1;
 while(context->core.in_packet.pos < context->core.in_packet.remaining_length){
  sub = ((void *)0);
  if(_mosquitto_read_string(&context->core.in_packet, &sub)){
   if(sub) _mosquitto_free(sub);
   return 1;
  }
  if(sub){
   mqtt3_log_printf(0x10, "\t%s", sub);
   mqtt3_sub_remove(context, sub, &db->subs);
   _mosquitto_free(sub);
  }
 }
 if(mqtt3_send_command_with_mid(context, 0xB0, mid, 0)) return 1;
 return 0;
}

canonical_original_code:
int mqtt3_handle_unsubscribe(struct _mosquitto_db *db, struct _mqtt3_context *context)
{
 unsigned short mid;
 char *sub;
 if(!context) return 1;
 mqtt3_log_printf(0x10, "Received UNSUBSCRIBE from %s", context->core.id);
 if(_mosquitto_read_uint16(&context->core.in_packet, &mid)) return 1;
 while(context->core.in_packet.pos < context->core.in_packet.remaining_length){
  sub = ((void *)0);
  if(_mosquitto_read_string(&context->core.in_packet, &sub)){
   if(sub) _mosquitto_free(sub);
   return 1;
  }
  if(sub){
   mqtt3_log_printf(0x10, "\t%s", sub);
   mqtt3_sub_remove(context, sub, &db->subs);
   _mosquitto_free(sub);
  }
 }
 if(mqtt3_send_command_with_mid(context, 0xB0, mid, 0)) 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 *

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; }

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; }


decompiled_code:
_BOOL8 __fastcall mqtt3_raw_connect(__int64 a1, const char *a2, char a3, char a4, char a5, const char *a6, char *s, unsigned __int16 a8, unsigned __int8 a9)
{
__int64 v13; // r12
int v14; // edx
char v15; // bl
int v16; // edx
const char *v17; // rdi
int v18; // eax
const char *v19; // rdi
int v20; // edx
__int64 v21; // rax
__int64 v22; // rsi
unsigned __int8 v23; // al
unsigned __int16 v24; // ax
__int64 v25; // r13
unsigned __int16 v26; // ax
__int64 v27; // r13
unsigned __int16 v28; // ax
char v30; // r8
unsigned __int16 v31; // ax
unsigned __int16 v32; // ax
unsigned __int16 v33; // ax
unsigned __int16 v34; // ax
int v37; // [rsp+10h] [rbp-48h]
int v38; // [rsp+10h] [rbp-48h]
int v39; // [rsp+18h] [rbp-40h]

if ( !a1 )
return 1LL;
if ( !a2 )
return 1LL;
v13 = mosquitto_calloc(1LL, 40LL);
if ( !v13 )
return 1LL;
v14 = strlen(a2);
v15 = (s != 0LL) & (a6 != 0LL) & a3;
if ( v15 )
{
v39 = v14;
v38 = strlen(a6);
v16 = strlen(s) + v39 + v38 + 6;
}
else
{
v16 = v14 + 2;
}
v17 = *(const char **)(a1 + 16);
if ( v17 )
{
v37 = v16;
v18 = strlen(v17);
v19 = *(const char **)(a1 + 24);
v16 = v37 + v18 + 2;
if ( v19 )
v16 = v37 + v18 + 2 + strlen(v19) + 2;
}
v20 = v16 + 12;
*(_BYTE *)v13 = 16;
*(_DWORD *)(v13 + 12) = v20;
v21 = mosquitto_malloc(v20);
*(_QWORD *)(v13 + 24) = v21;
if ( !v21 )
{
mosquitto_free(v13);
return 1LL;
}
mosquitto_write_string(v13, "MQIsdp", 6LL);
mosquitto_write_byte(v13, 3LL);
v22 = 2 * (unsigned int)a9;
if ( !v15 )
{
if ( !*(_QWORD *)(a1 + 16) )
{
mosquitto_write_byte(v13, v22);
mosquitto_write_uint16(v13, a8);
v34 = strlen(a2);
mosquitto_write_string(v13, a2, v34);
goto LABEL_15;
}
goto LABEL_12;
}
v30 = (2 * a9) | (8 * a4) & 0x18 | (32 * a5) | 4;
LOBYTE(v22) = v30;
if ( *(_QWORD *)(a1 + 16) )
{
LABEL_12:
v23 = v22 | 0xC0;
if ( !*(_QWORD *)(a1 + 24) )
v23 = v22 | 0x40;
mosquitto_write_byte(v13, v23);
mosquitto_write_uint16(v13, a8);
v24 = strlen(a2);
mosquitto_write_string(v13, a2, v24);
if ( !v15 )
goto LABEL_15;
goto LABEL_23;
}
mosquitto_write_byte(v13, (unsigned int)v30);
mosquitto_write_uint16(v13, a8);
v31 = strlen(a2);
mosquitto_write_string(v13, a2, v31);
LABEL_23:
v32 = strlen(a6);
mosquitto_write_string(v13, a6, v32);
v33 = strlen(s);
mosquitto_write_string(v13, s, v33);
LABEL_15:
v25 = *(_QWORD *)(a1 + 16);
if ( v25 )
{
v26 = strlen(*(const char **)(a1 + 16));
mosquitto_write_string(v13, v25, v26);
v27 = *(_QWORD *)(a1 + 24);
if ( v27 )
{
v28 = strlen(*(const char **)(a1 + 24));
mosquitto_write_string(v13, v27, v28);
}
}
*(_WORD *)(a1 + 32) = a8;
return (unsigned int)mqtt3_net_packet_queue(a1, v13) != 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(__int64 a1, const char *a2, char a3, char a4, char a5, const char *a6, char *s, unsigned __int16 a8, unsigned __int8 a9)
{
__int64 v13; // r12
int v14; // edx
char v15; // bl
int v16; // edx
const char *v17; // rdi
int v18; // eax
const char *v19; // rdi
int v20; // edx
__int64 v21; // rax
__int64 v22; // rsi
unsigned __int8 v23; // al
unsigned __int16 v24; // ax
__int64 v25; // r13
unsigned __int16 v26; // ax
__int64 v27; // r13
unsigned __int16 v28; // ax
char v30; // r8
unsigned __int16 v31; // ax
unsigned __int16 v32; // ax
unsigned __int16 v33; // ax
unsigned __int16 v34; // ax
int v37; // [rsp+10h] [rbp-48h]
int v38; // [rsp+10h] [rbp-48h]
int v39; // [rsp+18h] [rbp-40h]

if ( !a1 )
return 1LL;
if ( !a2 )
return 1LL;
v13 = mosquitto_calloc(1LL, 40LL);
if ( !v13 )
return 1LL;
v14 = strlen(a2);
v15 = (s != 0LL) & (a6 != 0LL) & a3;
if ( v15 )
{
v39 = v14;
v38 = strlen(a6);
v16 = strlen(s) + v39 + v38 + 6;
}
else
{
v16 = v14 + 2;
}
v17 = *(const char **)(a1 + 16);
if ( v17 )
{
v37 = v16;
v18 = strlen(v17);
v19 = *(const char **)(a1 + 24);
v16 = v37 + v18 + 2;
if ( v19 )
v16 = v37 + v18 + 2 + strlen(v19) + 2;
}
v20 = v16 + 12;
*(_BYTE *)v13 = 16;
*(_DWORD *)(v13 + 12) = v20;
v21 = mosquitto_malloc(v20);
*(_QWORD *)(v13 + 24) = v21;
if ( !v21 )
{
mosquitto_free(v13);
return 1LL;
}
mosquitto_write_string(v13, "MQIsdp", 6LL);
mosquitto_write_byte(v13, 3LL);
v22 = 2 * (unsigned int)a9;
if ( !v15 )
{
if ( !*(_QWORD *)(a1 + 16) )
{
mosquitto_write_byte(v13, v22);
mosquitto_write_uint16(v13, a8);
v34 = strlen(a2);
mosquitto_write_string(v13, a2, v34);
goto LABEL_15;
}
goto LABEL_12;
}
v30 = (2 * a9) | (8 * a4) & 0x18 | (32 * a5) | 4;
LOBYTE(v22) = v30;
if ( *(_QWORD *)(a1 + 16) )
{
LABEL_12:
v23 = v22 | 0xC0;
if ( !*(_QWORD *)(a1 + 24) )
v23 = v22 | 0x40;
mosquitto_write_byte(v13, v23);
mosquitto_write_uint16(v13, a8);
v24 = strlen(a2);
mosquitto_write_string(v13, a2, v24);
if ( !v15 )
goto LABEL_15;
goto LABEL_23;
}
mosquitto_write_byte(v13, (unsigned int)v30);
mosquitto_write_uint16(v13, a8);
v31 = strlen(a2);
mosquitto_write_string(v13, a2, v31);
LABEL_23:
v32 = strlen(a6);
mosquitto_write_string(v13, a6, v32);
v33 = strlen(s);
mosquitto_write_string(v13, s, v33);
LABEL_15:
v25 = *(_QWORD *)(a1 + 16);
if ( v25 )
{
v26 = strlen(*(const char **)(a1 + 16));
mosquitto_write_string(v13, v25, v26);
v27 = *(_QWORD *)(a1 + 24);
if ( v27 )
{
v28 = strlen(*(const char **)(a1 + 24));
mosquitto_write_string(v13, v27, v28);
}
}
*(_WORD *)(a1 + 32) = a8;
return (unsigned int)mqtt3_net_packet_queue(a1, v13) != 0;
}

original_code:
int mqtt3_raw_connect(mqtt3_context *context, const char *client_id, _Bool will, uint8_t will_qos, _Bool will_retain, const char *will_topic, const char *will_msg, uint16_t keepalive, _Bool clean_session)
{
 struct _mosquitto_packet *packet = ((void *)0);
 int payloadlen;
 uint8_t byte;
 if(!context || !client_id) return 1;
 packet = _mosquitto_calloc(1, sizeof(struct _mosquitto_packet));
 if(!packet) return 1;
 payloadlen = 2+strlen(client_id);
 if(will && will_topic && will_msg){
  payloadlen += 2+strlen(will_topic) + 2+strlen(will_msg);
 }else{
  will = 0;
 }
 if(context->core.username){
  payloadlen += 2+strlen(context->core.username);
  if(context->core.password){
   payloadlen += 2+strlen(context->core.password);
  }
 }
 packet->command = 0x10;
 packet->remaining_length = 12+payloadlen;
 packet->payload = _mosquitto_malloc(sizeof(uint8_t)*(12+payloadlen));
 if(!packet->payload){
  _mosquitto_free(packet);
  return 1;
 }
 _mosquitto_write_string(packet, "MQIsdp", strlen("MQIsdp"));
 _mosquitto_write_byte(packet, 3);
 byte = (clean_session&0x1)<<1;
 if(will){
  byte = byte | ((will_retain&0x1)<<5) | ((will_qos&0x3)<<3) | ((will&0x1)<<2);
 }
 if(context->core.username){
  byte = byte | 0x1<<6;
  if(context->core.password){
   byte = byte | 0x1<<7;
  }
 }
 _mosquitto_write_byte(packet, byte);
 _mosquitto_write_uint16(packet, keepalive);
 _mosquitto_write_string(packet, client_id, strlen(client_id));
 if(will){
  _mosquitto_write_string(packet, will_topic, strlen(will_topic));
  _mosquitto_write_string(packet, will_msg, strlen(will_msg));
 }
 if(context->core.username){
  _mosquitto_write_string(packet, context->core.username, strlen(context->core.username));
  if(context->core.password){
   _mosquitto_write_string(packet, context->core.password, strlen(context->core.password));
  }
 }
 context->core.keepalive = keepalive;
 if(mqtt3_net_packet_queue(context, packet)) return 1;
 return 0;
}

canonical_original_code:
int mqtt3_raw_connect(struct _mqtt3_context *context, char *client_id, _Bool will, unsigned char will_qos, _Bool will_retain, char *will_topic, char *will_msg, unsigned short keepalive, _Bool clean_session)
{
 struct _mosquitto_packet *packet = ((void *)0);
 int payloadlen;
 unsigned char byte;
 if(!context || !client_id) return 1;
 packet = _mosquitto_calloc(1, sizeof(struct _mosquitto_packet));
 if(!packet) return 1;
 payloadlen = 2+strlen(client_id);
 if(will && will_topic && will_msg){
  payloadlen += 2+strlen(will_topic) + 2+strlen(will_msg);
 }else{
  will = 0;
 }
 if(context->core.username){
  payloadlen += 2+strlen(context->core.username);
  if(context->core.password){
   payloadlen += 2+strlen(context->core.password);
  }
 }
 packet->command = 0x10;
 packet->remaining_length = 12+payloadlen;
 packet->payload = _mosquitto_malloc(sizeof(unsigned char)*(12+payloadlen));
 if(!packet->payload){
  _mosquitto_free(packet);
  return 1;
 }
 _mosquitto_write_string(packet, "MQIsdp", strlen("MQIsdp"));
 _mosquitto_write_byte(packet, 3);
 byte = (clean_session&0x1)<<1;
 if(will){
  byte = byte | ((will_retain&0x1)<<5) | ((will_qos&0x3)<<3) | ((will&0x1)<<2);
 }
 if(context->core.username){
  byte = byte | 0x1<<6;
  if(context->core.password){
   byte = byte | 0x1<<7;
  }
 }
 _mosquitto_write_byte(packet, byte);
 _mosquitto_write_uint16(packet, keepalive);
 _mosquitto_write_string(packet, client_id, strlen(client_id));
 if(will){
  _mosquitto_write_string(packet, will_topic, strlen(will_topic));
  _mosquitto_write_string(packet, will_msg, strlen(will_msg));
 }
 if(context->core.username){
  _mosquitto_write_string(packet, context->core.username, strlen(context->core.username));
  if(context->core.password){
   _mosquitto_write_string(packet, context->core.password, strlen(context->core.password));
  }
 }
 context->core.keepalive = keepalive;
 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:
keepalive
unsigned short

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; } *

variable_types: payloadlen int

variable_types:
byte
unsigned char


decompiled_code:
__int64 __fastcall mqtt3_raw_disconnect(__int64 a1)
{
return mqtt3_send_simple_command(a1, 224LL);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
return mqtt3_send_simple_command(a1, 224LL);
}

original_code:
int mqtt3_raw_disconnect(mqtt3_context *context)
{
 return mqtt3_send_simple_command(context, 0xE0);
}

canonical_original_code:
int mqtt3_raw_disconnect(struct _mqtt3_context *context)
{
 return mqtt3_send_simple_command(context, 0xE0);
}

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; }

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; }


decompiled_code:
_BOOL8 __fastcall mqtt3_raw_subscribe(__int64 a1, char a2, const char *a3, unsigned __int8 a4)
{
__int64 v7; // r13
__int64 v8; // rdi
__int64 v9; // rax
unsigned __int16 v10; // ax
unsigned __int16 v11; // ax

if ( !a1 )
return 1LL;
if ( a3 )
{
v7 = mosquitto_calloc(1LL, 40LL);
if ( v7 )
{
v8 = (unsigned int)strlen(a3) + 5;
*(_BYTE *)v7 = (8 * a2) | 0x82;
*(_DWORD *)(v7 + 12) = v8;
v9 = mosquitto_malloc(v8);
*(_QWORD *)(v7 + 24) = v9;
if ( v9 )
{
v10 = mosquitto_mid_generate(a1);
mosquitto_write_uint16(v7, v10);
v11 = strlen(a3);
mosquitto_write_string(v7, a3, v11);
mosquitto_write_byte(v7, a4);
return (unsigned int)mqtt3_net_packet_queue(a1, v7) != 0;
}
mosquitto_free(v7);
}
}
return 1LL;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(__int64 a1, char a2, const char *a3, unsigned __int8 a4)
{
__int64 v7; // r13
__int64 v8; // rdi
__int64 v9; // rax
unsigned __int16 v10; // ax
unsigned __int16 v11; // ax

if ( !a1 )
return 1LL;
if ( a3 )
{
v7 = mosquitto_calloc(1LL, 40LL);
if ( v7 )
{
v8 = (unsigned int)strlen(a3) + 5;
*(_BYTE *)v7 = (8 * a2) | 0x82;
*(_DWORD *)(v7 + 12) = v8;
v9 = mosquitto_malloc(v8);
*(_QWORD *)(v7 + 24) = v9;
if ( v9 )
{
v10 = mosquitto_mid_generate(a1);
mosquitto_write_uint16(v7, v10);
v11 = strlen(a3);
mosquitto_write_string(v7, a3, v11);
mosquitto_write_byte(v7, a4);
return (unsigned int)mqtt3_net_packet_queue(a1, v7) != 0;
}
mosquitto_free(v7);
}
}
return 1LL;
}

original_code:
int mqtt3_raw_subscribe(mqtt3_context *context, _Bool dup, const char *topic, uint8_t topic_qos)
{
 struct _mosquitto_packet *packet = ((void *)0);
 uint32_t packetlen;
 uint16_t mid;
 if(!context || !topic) return 1;
 packet = _mosquitto_calloc(1, sizeof(struct _mosquitto_packet));
 if(!packet) return 1;
 packetlen = 2 + 2+strlen(topic) + 1;
 packet->command = 0x80 | (dup<<3) | (1<<1);
 packet->remaining_length = packetlen;
 packet->payload = _mosquitto_malloc(sizeof(uint8_t)*packetlen);
 if(!packet->payload){
  _mosquitto_free(packet);
  return 1;
 }
 mid = _mosquitto_mid_generate(&context->core);
 _mosquitto_write_uint16(packet, mid);
 _mosquitto_write_string(packet, topic, strlen(topic));
 _mosquitto_write_byte(packet, topic_qos);
 if(mqtt3_net_packet_queue(context, packet)) return 1;
 return 0;
}

canonical_original_code:
int mqtt3_raw_subscribe(struct _mqtt3_context *context, _Bool dup, char *topic, unsigned char topic_qos)
{
 struct _mosquitto_packet *packet = ((void *)0);
 unsigned int packetlen;
 unsigned short mid;
 if(!context || !topic) return 1;
 packet = _mosquitto_calloc(1, sizeof(struct _mosquitto_packet));
 if(!packet) return 1;
 packetlen = 2 + 2+strlen(topic) + 1;
 packet->command = 0x80 | (dup<<3) | (1<<1);
 packet->remaining_length = packetlen;
 packet->payload = _mosquitto_malloc(sizeof(unsigned char)*packetlen);
 if(!packet->payload){
  _mosquitto_free(packet);
  return 1;
 }
 mid = _mosquitto_mid_generate(&context->core);
 _mosquitto_write_uint16(packet, mid);
 _mosquitto_write_string(packet, topic, strlen(topic));
 _mosquitto_write_byte(packet, topic_qos);
 if(mqtt3_net_packet_queue(context, packet)) return 1;
 return 0;
}

variable_types:
topic
char *

variable_types:
packetlen
unsigned int


decompiled_code:
_BOOL8 __fastcall mqtt3_raw_unsubscribe(__int64 a1, char a2, const char *a3)
{
__int64 v5; // r12
__int64 v6; // rdi
__int64 v7; // rax
unsigned __int16 v8; // ax
unsigned __int16 v9; // ax

if ( !a1 )
return 1LL;
if ( a3 )
{
v5 = mosquitto_calloc(1LL, 40LL);
if ( v5 )
{
v6 = (unsigned int)strlen(a3) + 4;
*(_BYTE *)v5 = (8 * a2) | 0x82;
*(_DWORD *)(v5 + 12) = v6;
v7 = mosquitto_malloc(v6);
*(_QWORD *)(v5 + 24) = v7;
if ( v7 )
{
v8 = mosquitto_mid_generate(a1);
mosquitto_write_uint16(v5, v8);
v9 = strlen(a3);
mosquitto_write_string(v5, a3, v9);
return (unsigned int)mqtt3_net_packet_queue(a1, v5) != 0;
}
mosquitto_free(v5);
}
}
return 1LL;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(__int64 a1, char a2, const char *a3)
{
__int64 v5; // r12
__int64 v6; // rdi
__int64 v7; // rax
unsigned __int16 v8; // ax
unsigned __int16 v9; // ax

if ( !a1 )
return 1LL;
if ( a3 )
{
v5 = mosquitto_calloc(1LL, 40LL);
if ( v5 )
{
v6 = (unsigned int)strlen(a3) + 4;
*(_BYTE *)v5 = (8 * a2) | 0x82;
*(_DWORD *)(v5 + 12) = v6;
v7 = mosquitto_malloc(v6);
*(_QWORD *)(v5 + 24) = v7;
if ( v7 )
{
v8 = mosquitto_mid_generate(a1);
mosquitto_write_uint16(v5, v8);
v9 = strlen(a3);
mosquitto_write_string(v5, a3, v9);
return (unsigned int)mqtt3_net_packet_queue(a1, v5) != 0;
}
mosquitto_free(v5);
}
}
return 1LL;
}

original_code:
int mqtt3_raw_unsubscribe(mqtt3_context *context, _Bool dup, const char *topic)
{
 struct _mosquitto_packet *packet = ((void *)0);
 uint32_t packetlen;
 uint16_t mid;
 if(!context || !topic) return 1;
 packet = _mosquitto_calloc(1, sizeof(struct _mosquitto_packet));
 if(!packet) return 1;
 packetlen = 2 + 2+strlen(topic);
 packet->command = 0x80 | (dup<<3) | (1<<1);
 packet->remaining_length = packetlen;
 packet->payload = _mosquitto_malloc(sizeof(uint8_t)*packetlen);
 if(!packet->payload){
  _mosquitto_free(packet);
  return 1;
 }
 mid = _mosquitto_mid_generate(&context->core);
 _mosquitto_write_uint16(packet, mid);
 _mosquitto_write_string(packet, topic, strlen(topic));
 if(mqtt3_net_packet_queue(context, packet)) return 1;
 return 0;
}

canonical_original_code:
int mqtt3_raw_unsubscribe(struct _mqtt3_context *context, _Bool dup, char *topic)
{
 struct _mosquitto_packet *packet = ((void *)0);
 unsigned int packetlen;
 unsigned short mid;
 if(!context || !topic) return 1;
 packet = _mosquitto_calloc(1, sizeof(struct _mosquitto_packet));
 if(!packet) return 1;
 packetlen = 2 + 2+strlen(topic);
 packet->command = 0x80 | (dup<<3) | (1<<1);
 packet->remaining_length = packetlen;
 packet->payload = _mosquitto_malloc(sizeof(unsigned char)*packetlen);
 if(!packet->payload){
  _mosquitto_free(packet);
  return 1;
 }
 mid = _mosquitto_mid_generate(&context->core);
 _mosquitto_write_uint16(packet, mid);
 _mosquitto_write_string(packet, topic, strlen(topic));
 if(mqtt3_net_packet_queue(context, packet)) return 1;
 return 0;
}


decompiled_code:
__int64 __fastcall mosquitto_log_init(__int64 a1, int a2, int a3)
{
__int64 result; // rax

if ( !a1 )
_assert_fail("mosq", "logging_mosq.c", 0x27u, "mosquitto_log_init");
*(_DWORD *)(a1 + 152) = a2;
result = 0LL;
*(_DWORD *)(a1 + 156) = a3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2, int a3)
{
__int64 result; // rax

if ( !a1 )
_assert_fail("mosq", "logging_mosq.c", 0x27u, "mosquitto_log_init");
*(_DWORD *)(a1 + 152) = a2;
result = 0LL;
*(_DWORD *)(a1 + 156) = a3;
return result;
}

original_code:
int mosquitto_log_init(struct mosquitto *mosq, int priorities, int destinations)
{
 ((void) sizeof ((mosq) ? 1 : 0), __extension__ ({ if (mosq) ; else __assert_fail ("mosq", "logging_mosq.c", 39, __extension__ __PRETTY_FUNCTION__); }));
 mosq->log_priorities = priorities;
 mosq->log_destinations = destinations;
 return 0;
}

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); } *

variable_types: priorities int

variable_types: destinations int

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_context_init(int fd)
{
__int64 v1; // rax
__int64 v2; // r12
time_t v3; // rax
socklen_t len; // [rsp+Ch] [rbp-49Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-498h] BYREF
char buf[1048]; // [rsp+90h] [rbp-418h] BYREF

v1 = mosquitto_malloc(152LL);
v2 = v1;
if ( v1 )
{
*(_DWORD *)(v1 + 36) = 0;
*(_BYTE *)(v1 + 121) = 0;
*(_DWORD *)v1 = fd;
*(_QWORD *)(v1 + 40) = time(0LL);
v3 = time(0LL);
*(_BYTE *)(v2 + 120) = 1;
*(_QWORD *)(v2 + 48) = v3;
*(_WORD *)(v2 + 56) = 0;
*(_WORD *)(v2 + 32) = 60;
*(_QWORD *)(v2 + 8) = 0LL;
*(_QWORD *)(v2 + 112) = 0LL;
*(_QWORD *)(v2 + 16) = 0LL;
*(_QWORD *)(v2 + 24) = 0LL;
*(_QWORD *)(v2 + 88) = 0LL;
mosquitto_packet_cleanup(v2 + 64);
*(_QWORD *)(v2 + 104) = 0LL;
len = 128;
*(_QWORD *)(v2 + 128) = 0LL;
if ( !getpeername(fd, &addr, &len) )
{
if ( addr.sa_family == 2 )
{
if ( !inet_ntop(2, &addr.sa_data[2], buf, 0x400u) )
goto LABEL_5;
}
else if ( addr.sa_family != 10 || !inet_ntop(10, &addr.sa_data[6], buf, 0x400u) )
{
goto LABEL_5;
}
*(_QWORD *)(v2 + 128) = mosquitto_strdup(buf);
}
LABEL_5:
*(_QWORD *)(v2 + 136) = 0LL;
*(_QWORD *)(v2 + 144) = 0LL;
}
return v2;
}

original_code:
mqtt3_context *mqtt3_context_init(int sock)
{
 mqtt3_context *context;
 struct sockaddr_storage addr;
 socklen_t addrlen;
 char address[1024];
 context = _mosquitto_malloc(sizeof(mqtt3_context));
 if(!context) return ((void *)0);
 context->core.state = mosq_cs_new;
 context->duplicate = 0;
 context->core.sock = sock;
 context->core.last_msg_in = time(((void *)0));
 context->core.last_msg_out = time(((void *)0));
 context->core.keepalive = 60;
 context->clean_session = 1;
 context->core.id = ((void *)0);
 context->core.last_mid = 0;
 context->core.will = ((void *)0);
 context->core.username = ((void *)0);
 context->core.password = ((void *)0);
 context->core.in_packet.payload = ((void *)0);
 _mosquitto_packet_cleanup(&context->core.in_packet);
 context->core.out_packet = ((void *)0);
 addrlen = sizeof(addr);
 context->address = ((void *)0);
 if(!getpeername(sock, (struct sockaddr *)&addr, &addrlen)){
  if(addr.ss_family == 2){
   if(inet_ntop(2, &((struct sockaddr_in *)&addr)->sin_addr.s_addr, address, 1024)){
    context->address = _mosquitto_strdup(address);
   }
  }else if(addr.ss_family == 10){
   if(inet_ntop(10, &((struct sockaddr_in6 *)&addr)->sin6_addr.__in6_u.__u6_addr8, address, 1024)){
    context->address = _mosquitto_strdup(address);
   }
  }
 }
 context->bridge = ((void *)0);
 context->msgs = ((void *)0);
 return context;
}

canonical_original_code:
struct _mqtt3_context *mqtt3_context_init(int sock)
{
 struct _mqtt3_context *context;
 struct sockaddr_storage addr;
 unsigned int addrlen;
 char address[1024];
 context = _mosquitto_malloc(sizeof(mqtt3_context));
 if(!context) return ((void *)0);
 context->core.state = mosq_cs_new;
 context->duplicate = 0;
 context->core.sock = sock;
 context->core.last_msg_in = time(((void *)0));
 context->core.last_msg_out = time(((void *)0));
 context->core.keepalive = 60;
 context->clean_session = 1;
 context->core.id = ((void *)0);
 context->core.last_mid = 0;
 context->core.will = ((void *)0);
 context->core.username = ((void *)0);
 context->core.password = ((void *)0);
 context->core.in_packet.payload = ((void *)0);
 _mosquitto_packet_cleanup(&context->core.in_packet);
 context->core.out_packet = ((void *)0);
 addrlen = sizeof(addr);
 context->address = ((void *)0);
 if(!getpeername(sock, (struct sockaddr *)&addr, &addrlen)){
  if(addr.ss_family == 2){
   if(inet_ntop(2, &((struct sockaddr_in *)&addr)->sin_addr.s_addr, address, 1024)){
    context->address = _mosquitto_strdup(address);
   }
  }else if(addr.ss_family == 10){
   if(inet_ntop(10, &((struct sockaddr_in6 *)&addr)->sin6_addr.__in6_u.__u6_addr8, address, 1024)){
    context->address = _mosquitto_strdup(address);
   }
  }
 }
 context->bridge = ((void *)0);
 context->msgs = ((void *)0);
 return context;
}

variable_types: sock int

variable_types: struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; }

variable_types:
addrlen
unsigned int

variable_types:
address
char [1024]

return_type: 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; } *

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 sockaddr { unsigned short sa_family; char sa_data[14]; }

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_context_cleanup(__int64 a1, __int64 a2, char a3)
{
__int64 result; // rax
__int64 i; // rdi
__int64 v6; // rdi
_QWORD *j; // rbx
_QWORD *v8; // rdi

if ( !a2 )
return result;
if ( *(_DWORD *)a2 == -1 )
{
if ( !*(_BYTE *)(a2 + 120) )
goto LABEL_5;
}
else
{
mqtt3_socket_close(a2);
if ( !*(_BYTE *)(a2 + 120) )
goto LABEL_5;
}
if ( !*(_BYTE *)(a2 + 121) )
{
mqtt3_subs_clean_session(a2, a1 + 8);
mqtt3_db_messages_delete(a2);
if ( !*(_QWORD *)(a2 + 128) )
goto LABEL_7;
goto LABEL_6;
}
LABEL_5:
if ( *(_QWORD *)(a2 + 128) )
LABEL_6:
((void (*)(void))mosquitto_free)();
LABEL_7:
if ( *(_QWORD *)(a2 + 8) )
((void (*)(void))mosquitto_free)();
result = mosquitto_packet_cleanup(a2 + 64);
for ( i = *(_QWORD *)(a2 + 104); i; i = *(_QWORD *)(a2 + 104) )
{
mosquitto_packet_cleanup(i);
*(_QWORD *)(a2 + 104) = *(_QWORD *)(*(_QWORD *)(a2 + 104) + 32LL);
result = ((__int64 (*)(void))mosquitto_free)();
}
v6 = *(_QWORD *)(a2 + 112);
if ( v6 )
{
if ( *(_QWORD *)(v6 + 8) )
{
mosquitto_free(*(_QWORD *)(v6 + 8));
v6 = *(_QWORD *)(a2 + 112);
}
if ( *(_QWORD *)(v6 + 16) )
{
mosquitto_free(*(_QWORD *)(v6 + 16));
v6 = *(_QWORD *)(a2 + 112);
}
result = mosquitto_free(v6);
}
for ( j = *(_QWORD **)(a2 + 144); j; result = mosquitto_free(v8) )
{
v8 = j;
j = (_QWORD *)*j;
--*(_DWORD *)(v8[1] + 16LL);
}
if ( a3 )
result = mosquitto_free(a2);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, char a3)
{
__int64 result; // rax
__int64 i; // rdi
__int64 v6; // rdi
_QWORD *j; // rbx
_QWORD *v8; // rdi

if ( !a2 )
return result;
if ( *(_DWORD *)a2 == -1 )
{
if ( !*(_BYTE *)(a2 + 120) )
goto LABEL_5;
}
else
{
mqtt3_socket_close(a2);
if ( !*(_BYTE *)(a2 + 120) )
goto LABEL_5;
}
if ( !*(_BYTE *)(a2 + 121) )
{
mqtt3_subs_clean_session(a2, a1 + 8);
mqtt3_db_messages_delete(a2);
if ( !*(_QWORD *)(a2 + 128) )
goto LABEL_7;
goto LABEL_6;
}
LABEL_5:
if ( *(_QWORD *)(a2 + 128) )
LABEL_6:
((void (*)(void))mosquitto_free)();
LABEL_7:
if ( *(_QWORD *)(a2 + 8) )
((void (*)(void))mosquitto_free)();
result = mosquitto_packet_cleanup(a2 + 64);
for ( i = *(_QWORD *)(a2 + 104); i; i = *(_QWORD *)(a2 + 104) )
{
mosquitto_packet_cleanup(i);
*(_QWORD *)(a2 + 104) = *(_QWORD *)(*(_QWORD *)(a2 + 104) + 32LL);
result = ((__int64 (*)(void))mosquitto_free)();
}
v6 = *(_QWORD *)(a2 + 112);
if ( v6 )
{
if ( *(_QWORD *)(v6 + 8) )
{
mosquitto_free(*(_QWORD *)(v6 + 8));
v6 = *(_QWORD *)(a2 + 112);
}
if ( *(_QWORD *)(v6 + 16) )
{
mosquitto_free(*(_QWORD *)(v6 + 16));
v6 = *(_QWORD *)(a2 + 112);
}
result = mosquitto_free(v6);
}
for ( j = *(_QWORD **)(a2 + 144); j; result = mosquitto_free(v8) )
{
v8 = j;
j = (_QWORD *)*j;
--*(_DWORD *)(v8[1] + 16LL);
}
if ( a3 )
result = mosquitto_free(a2);
return result;
}

original_code:
void mqtt3_context_cleanup(mosquitto_db *db, mqtt3_context *context, _Bool do_free)
{
 struct _mosquitto_packet *packet;
 mosquitto_client_msg *msg, *next;
 if(!context) return;
 if(context->core.sock != -1){
  mqtt3_socket_close(context);
 }
 if(context->clean_session && !context->duplicate){
  mqtt3_subs_clean_session(context, &db->subs);
  mqtt3_db_messages_delete(context);
 }
 if(context->address) _mosquitto_free(context->address);
 if(context->core.id) _mosquitto_free(context->core.id);
 _mosquitto_packet_cleanup(&(context->core.in_packet));
 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);
 }
 if(context->core.will){
  if(context->core.will->topic) _mosquitto_free(context->core.will->topic);
  if(context->core.will->payload) _mosquitto_free(context->core.will->payload);
  _mosquitto_free(context->core.will);
 }
 msg = context->msgs;
 while(msg){
  next = msg->next;
  msg->store->ref_count--;
  _mosquitto_free(msg);
  msg = next;
 }
 if(do_free){
  _mosquitto_free(context);
 }
}

canonical_original_code:
void mqtt3_context_cleanup(struct _mosquitto_db *db, struct _mqtt3_context *context, _Bool do_free)
{
 struct _mosquitto_packet *packet;
 struct _mosquitto_client_msg *msg; struct _mosquitto_client_msg *next;
 if(!context) return;
 if(context->core.sock != -1){
  mqtt3_socket_close(context);
 }
 if(context->clean_session && !context->duplicate){
  mqtt3_subs_clean_session(context, &db->subs);
  mqtt3_db_messages_delete(context);
 }
 if(context->address) _mosquitto_free(context->address);
 if(context->core.id) _mosquitto_free(context->core.id);
 _mosquitto_packet_cleanup(&(context->core.in_packet));
 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);
 }
 if(context->core.will){
  if(context->core.will->topic) _mosquitto_free(context->core.will->topic);
  if(context->core.will->payload) _mosquitto_free(context->core.will->payload);
  _mosquitto_free(context->core.will);
 }
 msg = context->msgs;
 while(msg){
  next = msg->next;
  msg->store->ref_count--;
  _mosquitto_free(msg);
  msg = next;
 }
 if(do_free){
  _mosquitto_free(context);
 }
}

variable_types: 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 { unsigned short mid; char *topic; unsigned char *payload; unsigned int payloadlen; int qos; _Bool retain; } 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; } *

return_type: void

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; }

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: enum mqtt3_bridge_direction { bd_out=0, bd_in=1, bd_both=2 }


decompiled_code:
__int64 __fastcall mqtt3_handle_puback(__int64 a1)
{
__int64 v1; // r12
unsigned __int16 v3[13]; // [rsp+Eh] [rbp-1Ah] BYREF

if ( a1 )
{
LODWORD(v1) = 2;
if ( *(_DWORD *)(a1 + 76) != 2 )
return (unsigned int)v1;
LODWORD(v1) = mosquitto_read_uint16(a1 + 64, v3);
if ( !(_DWORD)v1 )
{
mqtt3_log_printf(16LL, "Received PUBACK from %s (Mid: %d)", *(const char **)(a1 + 8), v3[0]);
if ( v3[0] )
v1 = (unsigned int)mqtt3_db_message_delete(a1, v3[0], 1LL) != 0;
return (unsigned int)v1;
}
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
__int64 v1; // r12
unsigned __int16 v3[13]; // [rsp+Eh] [rbp-1Ah] BYREF

if ( a1 )
{
LODWORD(v1) = 2;
if ( *(_DWORD *)(a1 + 76) != 2 )
return (unsigned int)v1;
LODWORD(v1) = mosquitto_read_uint16(a1 + 64, v3);
if ( !(_DWORD)v1 )
{
mqtt3_log_printf(16LL, "Received PUBACK from %s (Mid: %d)", *(const char **)(a1 + 8), v3[0]);
if ( v3[0] )
v1 = (unsigned int)mqtt3_db_message_delete(a1, v3[0], 1LL) != 0;
return (unsigned int)v1;
}
}
return 1LL;
}

original_code:
int mqtt3_handle_puback(mqtt3_context *context)
{
 uint16_t mid;
 if(!context){
  return 1;
 }
 if(context->core.in_packet.remaining_length != 2){
  return 2;
 }
 if(_mosquitto_read_uint16(&context->core.in_packet, &mid)) return 1;
 mqtt3_log_printf(0x10, "Received PUBACK from %s (Mid: %d)", context->core.id, mid);
 if(mid){
  if(mqtt3_db_message_delete(context, mid, mosq_md_out)) return 1;
 }
 return 0;
}

canonical_original_code:
int mqtt3_handle_puback(struct _mqtt3_context *context)
{
 unsigned short mid;
 if(!context){
  return 1;
 }
 if(context->core.in_packet.remaining_length != 2){
  return 2;
 }
 if(_mosquitto_read_uint16(&context->core.in_packet, &mid)) return 1;
 mqtt3_log_printf(0x10, "Received PUBACK from %s (Mid: %d)", context->core.id, mid);
 if(mid){
  if(mqtt3_db_message_delete(context, mid, mosq_md_out)) return 1;
 }
 return 0;
}

variable_types:
mid
unsigned short

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; }

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; }


decompiled_code:
__int64 __fastcall mqtt3_handle_pingreq(__int64 a1)
{
if ( !a1 )
return 1LL;
if ( *(_DWORD *)(a1 + 76) )
return 2LL;
mqtt3_log_printf(16LL, "Received PINGREQ from %s", *(const char **)(a1 + 8));
return mqtt3_raw_pingresp(a1);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
if ( !a1 )
return 1LL;
if ( *(_DWORD *)(a1 + 76) )
return 2LL;
mqtt3_log_printf(16LL, "Received PINGREQ from %s", *(const char **)(a1 + 8));
return mqtt3_raw_pingresp(a1);
}

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

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

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; }


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

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

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

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

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

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

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:
__int64 __fastcall mqtt3_handle_pubcomp(__int64 a1)
{
unsigned int v1; // er12
unsigned __int16 v3[13]; // [rsp+Eh] [rbp-1Ah] BYREF

if ( a1 )
{
v1 = 2;
if ( *(_DWORD *)(a1 + 76) != 2 )
return v1;
v1 = mosquitto_read_uint16(a1 + 64, v3);
if ( !v1 )
{
mqtt3_log_printf(16LL, "Received PUBCOMP from %s (Mid: %d)", *(const char **)(a1 + 8), v3[0]);
if ( v3[0] )
v1 = mqtt3_db_message_delete(a1, v3[0], 1LL) != 0;
return v1;
}
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
unsigned int v1; // er12
unsigned __int16 v3[13]; // [rsp+Eh] [rbp-1Ah] BYREF

if ( a1 )
{
v1 = 2;
if ( *(_DWORD *)(a1 + 76) != 2 )
return v1;
v1 = mosquitto_read_uint16(a1 + 64, v3);
if ( !v1 )
{
mqtt3_log_printf(16LL, "Received PUBCOMP from %s (Mid: %d)", *(const char **)(a1 + 8), v3[0]);
if ( v3[0] )
v1 = mqtt3_db_message_delete(a1, v3[0], 1LL) != 0;
return v1;
}
}
return 1LL;
}

original_code:
int mqtt3_handle_pubcomp(mqtt3_context *context)
{
 uint16_t mid;
 if(!context){
  return 1;
 }
 if(context->core.in_packet.remaining_length != 2){
  return 2;
 }
 if(_mosquitto_read_uint16(&context->core.in_packet, &mid)) return 1;
 mqtt3_log_printf(0x10, "Received PUBCOMP from %s (Mid: %d)", context->core.id, mid);
 if(mid){
  if(mqtt3_db_message_delete(context, mid, mosq_md_out)) return 1;
 }
 return 0;
}

canonical_original_code:
int mqtt3_handle_pubcomp(struct _mqtt3_context *context)
{
 unsigned short mid;
 if(!context){
  return 1;
 }
 if(context->core.in_packet.remaining_length != 2){
  return 2;
 }
 if(_mosquitto_read_uint16(&context->core.in_packet, &mid)) return 1;
 mqtt3_log_printf(0x10, "Received PUBCOMP from %s (Mid: %d)", context->core.id, mid);
 if(mid){
  if(mqtt3_db_message_delete(context, mid, mosq_md_out)) return 1;
 }
 return 0;
}


decompiled_code:
__int64 __fastcall mqtt3_handle_publish(__int64 a1, __int64 a2)
{
__int64 v2; // r14
unsigned __int8 v3; // bl
__int64 v4; // r12
const char *v6; // rax
int v7; // er15
__int64 v8; // rbx
__int64 v9; // rcx
__int64 v10; // r15
unsigned int v11; // er10
__int64 v12; // r9
int v13; // eax
int v14; // ebx
int v15; // eax
unsigned __int8 v16; // [rsp+7h] [rbp-61h]
unsigned int v17; // [rsp+8h] [rbp-60h]
unsigned int v18; // [rsp+8h] [rbp-60h]
unsigned __int16 v19; // [rsp+1Eh] [rbp-4Ah] BYREF
const char *v20; // [rsp+20h] [rbp-48h] BYREF
__int64 v21[8]; // [rsp+28h] [rbp-40h] BYREF

v2 = a2 + 64;
v3 = *(_BYTE *)(a2 + 64);
v19 = 0;
v21[0] = 0LL;
if ( (unsigned int)mosquitto_read_string(a2 + 64, &v20)
|| (LODWORD(v4) = mosquitto_fix_sub_topic(&v20), (_DWORD)v4)
|| (v6 = v20, !*v20) )
{
LABEL_2:
LODWORD(v4) = 1;
return (unsigned int)v4;
}
v7 = v3;
v16 = ((int)v3 >> 1) & 3;
if ( (((int)v3 >> 1) & 3) != 0 )
{
if ( (unsigned int)mosquitto_read_uint16(v2, &v19) )
{
LABEL_18:
LODWORD(v4) = 1;
mosquitto_free(v20);
return (unsigned int)v4;
}
v6 = v20;
}
v8 = v3 & 1;
v9 = (v7 >> 3) & 1;
v17 = *(_DWORD *)(a2 + 76) - *(_DWORD *)(a2 + 84);
v10 = 0LL;
mqtt3_log_printf(
16LL,
"Received PUBLISH from %s (d%d, q%d, r%d, m%d, '%s', ... (%ld bytes))",
*(const char **)(a2 + 8),
v9,
v16,
(unsigned int)v8,
v19,
v6,
v17);
v11 = v17;
if ( v17 )
{
v10 = mosquitto_calloc(v17 + 1, 1LL);
v13 = mosquitto_read_bytes(v2, v10, v17);
v11 = v17;
if ( v13 )
goto LABEL_18;
}
if ( !v16 )
{
v12 = v21[0];
if ( v21[0] )
{
LABEL_12:
v4 = (unsigned int)mqtt3_db_messages_queue(a1, *(_QWORD *)(a2 + 8), v20, 0LL, (unsigned int)v8, v12) != 0;
goto LABEL_13;
}
if ( !(unsigned int)mqtt3_db_message_store(a1, *(_QWORD *)(a2 + 8), v19, v20, 0LL, v11, v10, v8, v21, 0LL) )
{
v12 = v21[0];
goto LABEL_12;
}
goto LABEL_23;
}
v18 = v11;
mqtt3_db_message_store_find(a1, *(_QWORD *)(a2 + 8), v19, v21);
if ( v21[0] )
{
if ( v16 != 2 )
{
if ( v16 == 3 )
goto LABEL_13;
goto LABEL_22;
}
goto LABEL_25;
}
if ( (unsigned int)mqtt3_db_message_store(a1, *(_QWORD *)(a2 + 8), v19, v20, v16, v18, v10, v8, v21, 0LL) )
{
LABEL_23:
mosquitto_free(v20);
if ( v10 )
mosquitto_free(v10);
goto LABEL_2;
}
if ( v16 == 2 )
{
v15 = mqtt3_db_message_insert(a2, v19, 0LL, 2LL, 0LL, v21[0]);
if ( !v15 )
{
LABEL_25:
v4 = (unsigned int)mqtt3_raw_pubrec(a2, v19) != 0;
goto LABEL_13;
}
LODWORD(v4) = v15 == 1;
}
else
{
if ( v16 != 3 )
{
LABEL_22:
v14 = mqtt3_db_messages_queue(a1, *(_QWORD *)(a2 + 8), v20, 1LL, (unsigned int)v8, v21[0]);
v4 = ((unsigned int)mqtt3_raw_puback(a2, v19) | v14) != 0;
goto LABEL_13;
}
LODWORD(v4) = 0;
}
LABEL_13:
mosquitto_free(v20);
if ( v10 )
mosquitto_free(v10);
return (unsigned int)v4;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2)
{
__int64 v2; // r14
unsigned __int8 v3; // bl
__int64 v4; // r12
const char *v6; // rax
int v7; // er15
__int64 v8; // rbx
__int64 v9; // rcx
__int64 v10; // r15
unsigned int v11; // er10
__int64 v12; // r9
int v13; // eax
int v14; // ebx
int v15; // eax
unsigned __int8 v16; // [rsp+7h] [rbp-61h]
unsigned int v17; // [rsp+8h] [rbp-60h]
unsigned int v18; // [rsp+8h] [rbp-60h]
unsigned __int16 v19; // [rsp+1Eh] [rbp-4Ah] BYREF
const char *v20; // [rsp+20h] [rbp-48h] BYREF
__int64 v21[8]; // [rsp+28h] [rbp-40h] BYREF

v2 = a2 + 64;
v3 = *(_BYTE *)(a2 + 64);
v19 = 0;
v21[0] = 0LL;
if ( (unsigned int)mosquitto_read_string(a2 + 64, &v20)
|| (LODWORD(v4) = mosquitto_fix_sub_topic(&v20), (_DWORD)v4)
|| (v6 = v20, !*v20) )
{
LABEL_2:
LODWORD(v4) = 1;
return (unsigned int)v4;
}
v7 = v3;
v16 = ((int)v3 >> 1) & 3;
if ( (((int)v3 >> 1) & 3) != 0 )
{
if ( (unsigned int)mosquitto_read_uint16(v2, &v19) )
{
LABEL_18:
LODWORD(v4) = 1;
mosquitto_free(v20);
return (unsigned int)v4;
}
v6 = v20;
}
v8 = v3 & 1;
v9 = (v7 >> 3) & 1;
v17 = *(_DWORD *)(a2 + 76) - *(_DWORD *)(a2 + 84);
v10 = 0LL;
mqtt3_log_printf(
16LL,
"Received PUBLISH from %s (d%d, q%d, r%d, m%d, '%s', ... (%ld bytes))",
*(const char **)(a2 + 8),
v9,
v16,
(unsigned int)v8,
v19,
v6,
v17);
v11 = v17;
if ( v17 )
{
v10 = mosquitto_calloc(v17 + 1, 1LL);
v13 = mosquitto_read_bytes(v2, v10, v17);
v11 = v17;
if ( v13 )
goto LABEL_18;
}
if ( !v16 )
{
v12 = v21[0];
if ( v21[0] )
{
LABEL_12:
v4 = (unsigned int)mqtt3_db_messages_queue(a1, *(_QWORD *)(a2 + 8), v20, 0LL, (unsigned int)v8, v12) != 0;
goto LABEL_13;
}
if ( !(unsigned int)mqtt3_db_message_store(a1, *(_QWORD *)(a2 + 8), v19, v20, 0LL, v11, v10, v8, v21, 0LL) )
{
v12 = v21[0];
goto LABEL_12;
}
goto LABEL_23;
}
v18 = v11;
mqtt3_db_message_store_find(a1, *(_QWORD *)(a2 + 8), v19, v21);
if ( v21[0] )
{
if ( v16 != 2 )
{
if ( v16 == 3 )
goto LABEL_13;
goto LABEL_22;
}
goto LABEL_25;
}
if ( (unsigned int)mqtt3_db_message_store(a1, *(_QWORD *)(a2 + 8), v19, v20, v16, v18, v10, v8, v21, 0LL) )
{
LABEL_23:
mosquitto_free(v20);
if ( v10 )
mosquitto_free(v10);
goto LABEL_2;
}
if ( v16 == 2 )
{
v15 = mqtt3_db_message_insert(a2, v19, 0LL, 2LL, 0LL, v21[0]);
if ( !v15 )
{
LABEL_25:
v4 = (unsigned int)mqtt3_raw_pubrec(a2, v19) != 0;
goto LABEL_13;
}
LODWORD(v4) = v15 == 1;
}
else
{
if ( v16 != 3 )
{
LABEL_22:
v14 = mqtt3_db_messages_queue(a1, *(_QWORD *)(a2 + 8), v20, 1LL, (unsigned int)v8, v21[0]);
v4 = ((unsigned int)mqtt3_raw_puback(a2, v19) | v14) != 0;
goto LABEL_13;
}
LODWORD(v4) = 0;
}
LABEL_13:
mosquitto_free(v20);
if ( v10 )
mosquitto_free(v10);
return (unsigned int)v4;
}

original_code:
int mqtt3_handle_publish(mosquitto_db *db, mqtt3_context *context)
{
 char *topic;
 uint8_t *payload = ((void *)0);
 uint32_t payloadlen;
 uint8_t dup, qos, retain;
 uint16_t mid = 0;
 int rc = 0;
 uint8_t header = context->core.in_packet.command;
 int res = 0;
 struct mosquitto_msg_store *stored = ((void *)0);
 dup = (header & 0x08)>>3;
 qos = (header & 0x06)>>1;
 retain = (header & 0x01);
 if(_mosquitto_read_string(&context->core.in_packet, &topic)) return 1;
 if(_mosquitto_fix_sub_topic(&topic)) return 1;
 if(!strlen(topic)){
  return 1;
 }
 if(qos > 0){
  if(_mosquitto_read_uint16(&context->core.in_packet, &mid)){
   _mosquitto_free(topic);
   return 1;
  }
 }
 payloadlen = context->core.in_packet.remaining_length - context->core.in_packet.pos;
 mqtt3_log_printf(0x10, "Received PUBLISH from %s (d%d, q%d, r%d, m%d, '%s', ... (%ld bytes))", context->core.id, dup, qos, retain, mid, topic, (long)payloadlen);
 if(payloadlen){
  payload = _mosquitto_calloc(payloadlen+1, sizeof(uint8_t));
  if(_mosquitto_read_bytes(&context->core.in_packet, payload, payloadlen)){
   _mosquitto_free(topic);
   return 1;
  }
 }
 if(qos > 0){
  mqtt3_db_message_store_find(db, context->core.id, mid, &stored);
 }
 if(!stored){
  dup = 0;
  if(mqtt3_db_message_store(db, context->core.id, mid, topic, qos, payloadlen, payload, retain, &stored, 0)){
   _mosquitto_free(topic);
   if(payload) _mosquitto_free(payload);
   return 1;
  }
 }else{
  dup = 1;
 }
 switch(qos){
  case 0:
   if(mqtt3_db_messages_queue(db, context->core.id, topic, qos, retain, stored)) rc = 1;
   break;
  case 1:
   if(mqtt3_db_messages_queue(db, context->core.id, topic, qos, retain, stored)) rc = 1;
   if(mqtt3_raw_puback(context, mid)) rc = 1;
   break;
  case 2:
   if(!dup){
    res = mqtt3_db_message_insert(context, mid, mosq_md_in, qos, 0, stored);
   }else{
    res = 0;
   }
   if(!res){
    if(mqtt3_raw_pubrec(context, mid)) rc = 1;
   }else if(res == 1){
    rc = 1;
   }
   break;
 }
 _mosquitto_free(topic);
 if(payload) _mosquitto_free(payload);
 return rc;
}

canonical_original_code:
int mqtt3_handle_publish(struct _mosquitto_db *db, struct _mqtt3_context *context)
{
 char *topic;
 unsigned char *payload = ((void *)0);
 unsigned int payloadlen;
 unsigned char dup; unsigned char qos; unsigned char retain;
 unsigned short mid = 0;
 int rc = 0;
 unsigned char header = context->core.in_packet.command;
 int res = 0;
 struct mosquitto_msg_store *stored = ((void *)0);
 dup = (header & 0x08)>>3;
 qos = (header & 0x06)>>1;
 retain = (header & 0x01);
 if(_mosquitto_read_string(&context->core.in_packet, &topic)) return 1;
 if(_mosquitto_fix_sub_topic(&topic)) return 1;
 if(!strlen(topic)){
  return 1;
 }
 if(qos > 0){
  if(_mosquitto_read_uint16(&context->core.in_packet, &mid)){
   _mosquitto_free(topic);
   return 1;
  }
 }
 payloadlen = context->core.in_packet.remaining_length - context->core.in_packet.pos;
 mqtt3_log_printf(0x10, "Received PUBLISH from %s (d%d, q%d, r%d, m%d, '%s', ... (%ld bytes))", context->core.id, dup, qos, retain, mid, topic, (long)payloadlen);
 if(payloadlen){
  payload = _mosquitto_calloc(payloadlen+1, sizeof(unsigned char));
  if(_mosquitto_read_bytes(&context->core.in_packet, payload, payloadlen)){
   _mosquitto_free(topic);
   return 1;
  }
 }
 if(qos > 0){
  mqtt3_db_message_store_find(db, context->core.id, mid, &stored);
 }
 if(!stored){
  dup = 0;
  if(mqtt3_db_message_store(db, context->core.id, mid, topic, qos, payloadlen, payload, retain, &stored, 0)){
   _mosquitto_free(topic);
   if(payload) _mosquitto_free(payload);
   return 1;
  }
 }else{
  dup = 1;
 }
 switch(qos){
  case 0:
   if(mqtt3_db_messages_queue(db, context->core.id, topic, qos, retain, stored)) rc = 1;
   break;
  case 1:
   if(mqtt3_db_messages_queue(db, context->core.id, topic, qos, retain, stored)) rc = 1;
   if(mqtt3_raw_puback(context, mid)) rc = 1;
   break;
  case 2:
   if(!dup){
    res = mqtt3_db_message_insert(context, mid, mosq_md_in, qos, 0, stored);
   }else{
    res = 0;
   }
   if(!res){
    if(mqtt3_raw_pubrec(context, mid)) rc = 1;
   }else if(res == 1){
    rc = 1;
   }
   break;
 }
 _mosquitto_free(topic);
 if(payload) _mosquitto_free(payload);
 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:
payload
unsigned char *

variable_types:
payloadlen
unsigned int

variable_types:
dup
unsigned char

variable_types:
qos
unsigned char

variable_types:
retain
unsigned char

variable_types: rc int

variable_types:
header
unsigned char

variable_types: res int

variable_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 { unsigned short mid; char *topic; unsigned char *payload; unsigned int payloadlen; int qos; _Bool retain; } msg; } *


decompiled_code:
_BOOL8 __fastcall mqtt3_handle_pubrec(__int64 a1)
{
unsigned __int16 v2[5]; // [rsp+0h] [rbp-Ah] BYREF

if ( !a1 )
return 1LL;
if ( *(_DWORD *)(a1 + 76) != 2 )
return 1LL;
if ( (unsigned int)mosquitto_read_uint16(a1 + 64, v2) )
return 1LL;
mqtt3_log_printf(16LL, "Received PUBREC from %s (Mid: %d)", *(const char **)(a1 + 8), v2[0]);
if ( (unsigned int)mqtt3_db_message_update(a1, v2[0], 1LL, 9LL) )
return 1LL;
return (unsigned int)mqtt3_raw_pubrel(a1, v2[0], 0LL) != 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func5(__int64 a1)
{
unsigned __int16 v2[5]; // [rsp+0h] [rbp-Ah] BYREF

if ( !a1 )
return 1LL;
if ( *(_DWORD *)(a1 + 76) != 2 )
return 1LL;
if ( (unsigned int)mosquitto_read_uint16(a1 + 64, v2) )
return 1LL;
mqtt3_log_printf(16LL, "Received PUBREC from %s (Mid: %d)", *(const char **)(a1 + 8), v2[0]);
if ( (unsigned int)mqtt3_db_message_update(a1, v2[0], 1LL, 9LL) )
return 1LL;
return (unsigned int)mqtt3_raw_pubrel(a1, v2[0], 0LL) != 0;
}

original_code:
int mqtt3_handle_pubrec(mqtt3_context *context)
{
 uint16_t mid;
 if(!context){
  return 1;
 }
 if(context->core.in_packet.remaining_length != 2){
  return 1;
 }
 if(_mosquitto_read_uint16(&context->core.in_packet, &mid)) return 1;
 mqtt3_log_printf(0x10, "Received PUBREC from %s (Mid: %d)", context->core.id, mid);
 if(mqtt3_db_message_update(context, mid, mosq_md_out, ms_wait_pubcomp)) return 1;
 if(mqtt3_raw_pubrel(context, mid, 0)) return 1;
 return 0;
}

canonical_original_code:
int mqtt3_handle_pubrec(struct _mqtt3_context *context)
{
 unsigned short mid;
 if(!context){
  return 1;
 }
 if(context->core.in_packet.remaining_length != 2){
  return 1;
 }
 if(_mosquitto_read_uint16(&context->core.in_packet, &mid)) return 1;
 mqtt3_log_printf(0x10, "Received PUBREC from %s (Mid: %d)", context->core.id, mid);
 if(mqtt3_db_message_update(context, mid, mosq_md_out, ms_wait_pubcomp)) return 1;
 if(mqtt3_raw_pubrel(context, mid, 0)) 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; } *


decompiled_code:
_BOOL8 __fastcall mqtt3_handle_pubrel(__int64 a1, __int64 a2)
{
unsigned __int16 v3[13]; // [rsp+0h] [rbp-1Ah] BYREF

if ( !a2 )
return 1LL;
if ( *(_DWORD *)(a2 + 76) != 2 || (unsigned int)mosquitto_read_uint16(a2 + 64, v3) )
return 1LL;
mqtt3_log_printf(16LL, "Received PUBREL from %s (Mid: %d)", *(const char **)(a2 + 8), v3[0]);
mqtt3_db_message_release(a1, a2, v3[0], 0LL);
return (unsigned int)mqtt3_raw_pubcomp(a2, v3[0]) != 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func6(__int64 a1, __int64 a2)
{
unsigned __int16 v3[13]; // [rsp+0h] [rbp-1Ah] BYREF

if ( !a2 )
return 1LL;
if ( *(_DWORD *)(a2 + 76) != 2 || (unsigned int)mosquitto_read_uint16(a2 + 64, v3) )
return 1LL;
mqtt3_log_printf(16LL, "Received PUBREL from %s (Mid: %d)", *(const char **)(a2 + 8), v3[0]);
mqtt3_db_message_release(a1, a2, v3[0], 0LL);
return (unsigned int)mqtt3_raw_pubcomp(a2, v3[0]) != 0;
}

original_code:
int mqtt3_handle_pubrel(mosquitto_db *db, mqtt3_context *context)
{
 uint16_t mid;
 if(!context){
  return 1;
 }
 if(context->core.in_packet.remaining_length != 2){
  return 1;
 }
 if(_mosquitto_read_uint16(&context->core.in_packet, &mid)) return 1;
 mqtt3_log_printf(0x10, "Received PUBREL from %s (Mid: %d)", context->core.id, mid);
 mqtt3_db_message_release(db, context, mid, mosq_md_in);
 if(mqtt3_raw_pubcomp(context, mid)) return 1;
 return 0;
}

canonical_original_code:
int mqtt3_handle_pubrel(struct _mosquitto_db *db, struct _mqtt3_context *context)
{
 unsigned short mid;
 if(!context){
  return 1;
 }
 if(context->core.in_packet.remaining_length != 2){
  return 1;
 }
 if(_mosquitto_read_uint16(&context->core.in_packet, &mid)) return 1;
 mqtt3_log_printf(0x10, "Received PUBREL from %s (Mid: %d)", context->core.id, mid);
 mqtt3_db_message_release(db, context, mid, mosq_md_in);
 if(mqtt3_raw_pubcomp(context, mid)) return 1;
 return 0;
}


decompiled_code:
__int64 __fastcall mqtt3_packet_handle(__int64 a1, __int64 a2)
{
unsigned __int8 v2; // al

if ( !a2 )
return 1LL;
v2 = *(_BYTE *)(a2 + 64) & 0xF0;
if ( v2 == 0x80 )
return mqtt3_handle_subscribe();
if ( (*(_BYTE *)(a2 + 64) & 0xF0u) > 0x80 )
{
if ( v2 == 0xC0 )
return mqtt3_handle_pingreq(a2);
if ( v2 <= 0xC0u )
{
switch ( v2 )
{
case 0xA0:
return mqtt3_handle_unsubscribe();
case 0xB0:
return mqtt3_handle_unsuback(a2);
case 0x90:
return mqtt3_handle_suback(a2);
}
}
else
{
if ( v2 == 0xD0 )
return mqtt3_handle_pingresp(a2);
if ( v2 == 0xE0 )
return mqtt3_handle_disconnect(a2);
}
}
else
{
if ( v2 == 64 )
return mqtt3_handle_puback(a2);
if ( v2 <= 0x40u )
{
switch ( v2 )
{
case 0x20u:
return mqtt3_handle_connack(a2);
case 0x30u:
return mqtt3_handle_publish(a1, a2);
case 0x10u:
return mqtt3_handle_connect();
}
}
else
{
switch ( v2 )
{
case '`':
return mqtt3_handle_pubrel(a1, a2);
case 'p':
return mqtt3_handle_pubcomp(a2);
case 'P':
return mqtt3_handle_pubrec(a2);
}
}
}
return 2LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2)
{
unsigned __int8 v2; // al

if ( !a2 )
return 1LL;
v2 = *(_BYTE *)(a2 + 64) & 0xF0;
if ( v2 == 0x80 )
return mqtt3_handle_subscribe();
if ( (*(_BYTE *)(a2 + 64) & 0xF0u) > 0x80 )
{
if ( v2 == 0xC0 )
return func1(a2);
if ( v2 <= 0xC0u )
{
switch ( v2 )
{
case 0xA0:
return mqtt3_handle_unsubscribe();
case 0xB0:
return mqtt3_handle_unsuback(a2);
case 0x90:
return mqtt3_handle_suback(a2);
}
}
else
{
if ( v2 == 0xD0 )
return func2(a2);
if ( v2 == 0xE0 )
return mqtt3_handle_disconnect(a2);
}
}
else
{
if ( v2 == 64 )
return func0(a2);
if ( v2 <= 0x40u )
{
switch ( v2 )
{
case 0x20u:
return mqtt3_handle_connack(a2);
case 0x30u:
return func4(a1, a2);
case 0x10u:
return mqtt3_handle_connect();
}
}
else
{
switch ( v2 )
{
case '`':
return func6(a1, a2);
case 'p':
return func3(a2);
case 'P':
return func5(a2);
}
}
}
return 2LL;
}

original_code:
int mqtt3_packet_handle(mosquitto_db *db, mqtt3_context *context)
{
 if(!context) return 1;
 switch((context->core.in_packet.command)&0xF0){
  case 0xC0:
   return mqtt3_handle_pingreq(context);
  case 0xD0:
   return mqtt3_handle_pingresp(context);
  case 0x40:
   return mqtt3_handle_puback(context);
  case 0x70:
   return mqtt3_handle_pubcomp(context);
  case 0x30:
   return mqtt3_handle_publish(db, context);
  case 0x50:
   return mqtt3_handle_pubrec(context);
  case 0x60:
   return mqtt3_handle_pubrel(db, context);
  case 0x10:
   return mqtt3_handle_connect(db, context);
  case 0xE0:
   return mqtt3_handle_disconnect(context);
  case 0x80:
   return mqtt3_handle_subscribe(db, context);
  case 0xA0:
   return mqtt3_handle_unsubscribe(db, context);
  case 0x20:
   return mqtt3_handle_connack(context);
  case 0x90:
   return mqtt3_handle_suback(context);
  case 0xB0:
   return mqtt3_handle_unsuback(context);
  default:
   return 2;
 }
}

canonical_original_code:
int mqtt3_packet_handle(struct _mosquitto_db *db, struct _mqtt3_context *context)
{
 if(!context) return 1;
 switch((context->core.in_packet.command)&0xF0){
  case 0xC0:
   return mqtt3_handle_pingreq(context);
  case 0xD0:
   return mqtt3_handle_pingresp(context);
  case 0x40:
   return mqtt3_handle_puback(context);
  case 0x70:
   return mqtt3_handle_pubcomp(context);
  case 0x30:
   return mqtt3_handle_publish(db, context);
  case 0x50:
   return mqtt3_handle_pubrec(context);
  case 0x60:
   return mqtt3_handle_pubrel(db, context);
  case 0x10:
   return mqtt3_handle_connect(db, context);
  case 0xE0:
   return mqtt3_handle_disconnect(context);
  case 0x80:
   return mqtt3_handle_subscribe(db, context);
  case 0xA0:
   return mqtt3_handle_unsubscribe(db, context);
  case 0x20:
   return mqtt3_handle_connack(context);
  case 0x90:
   return mqtt3_handle_suback(context);
  case 0xB0:
   return mqtt3_handle_unsuback(context);
  default:
   return 2;
 }
}


decompiled_code:
__int64 __fastcall mqtt3_handle_connack(__int64 a1)
{
unsigned int v1; // er12
__int64 v3; // rcx
__int64 i; // rbp
__int64 v5; // rax
char v6; // [rsp+Eh] [rbp-1Ah] BYREF
char v7[25]; // [rsp+Fh] [rbp-19h] BYREF

if ( a1 )
{
v1 = 2;
if ( *(_DWORD *)(a1 + 76) != 2 )
return v1;
mqtt3_log_printf(16LL, "Received CONNACK");
if ( !(unsigned int)mosquitto_read_byte(a1 + 64, &v6) )
{
v1 = mosquitto_read_byte(a1 + 64, v7);
if ( !v1 )
{
if ( client_connack_callback )
client_connack_callback((unsigned __int8)v7[0]);
if ( v7[0] == 2 )
{
v1 = 1;
mqtt3_log_printf(8LL, "Connection Refused: identifier rejected");
return v1;
}
if ( v7[0] <= 2u )
{
if ( v7[0] )
{
v1 = 1;
mqtt3_log_printf(8LL, "Connection Refused: unacceptable protocol version");
}
else
{
v3 = *(_QWORD *)(a1 + 136);
if ( v3 && *(int *)(v3 + 40) > 0 )
{
for ( i = 0LL; *(_DWORD *)(v3 + 40) > (int)i; ++i )
{
v5 = *(_QWORD *)(v3 + 32) + 16 * i;
if ( (unsigned int)(*(_DWORD *)(v5 + 8) - 1) <= 1 )
{
if ( (unsigned int)mqtt3_raw_subscribe(a1, 0LL, *(_QWORD *)v5, 2LL) )
return 1LL;
v3 = *(_QWORD *)(a1 + 136);
}
}
}
*(_DWORD *)(a1 + 36) = 1;
}
return v1;
}
if ( v7[0] == 3 )
mqtt3_log_printf(8LL, "Connection Refused: broker unavailable");
}
}
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
unsigned int v1; // er12
__int64 v3; // rcx
__int64 i; // rbp
__int64 v5; // rax
char v6; // [rsp+Eh] [rbp-1Ah] BYREF
char v7[25]; // [rsp+Fh] [rbp-19h] BYREF

if ( a1 )
{
v1 = 2;
if ( *(_DWORD *)(a1 + 76) != 2 )
return v1;
mqtt3_log_printf(16LL, "Received CONNACK");
if ( !(unsigned int)mosquitto_read_byte(a1 + 64, &v6) )
{
v1 = mosquitto_read_byte(a1 + 64, v7);
if ( !v1 )
{
if ( client_connack_callback )
client_connack_callback((unsigned __int8)v7[0]);
if ( v7[0] == 2 )
{
v1 = 1;
mqtt3_log_printf(8LL, "Connection Refused: identifier rejected");
return v1;
}
if ( v7[0] <= 2u )
{
if ( v7[0] )
{
v1 = 1;
mqtt3_log_printf(8LL, "Connection Refused: unacceptable protocol version");
}
else
{
v3 = *(_QWORD *)(a1 + 136);
if ( v3 && *(int *)(v3 + 40) > 0 )
{
for ( i = 0LL; *(_DWORD *)(v3 + 40) > (int)i; ++i )
{
v5 = *(_QWORD *)(v3 + 32) + 16 * i;
if ( (unsigned int)(*(_DWORD *)(v5 + 8) - 1) <= 1 )
{
if ( (unsigned int)mqtt3_raw_subscribe(a1, 0LL, *(_QWORD *)v5, 2LL) )
return 1LL;
v3 = *(_QWORD *)(a1 + 136);
}
}
}
*(_DWORD *)(a1 + 36) = 1;
}
return v1;
}
if ( v7[0] == 3 )
mqtt3_log_printf(8LL, "Connection Refused: broker unavailable");
}
}
}
return 1LL;
}

original_code:
int mqtt3_handle_connack(mqtt3_context *context)
{
 uint8_t byte;
 uint8_t rc;
 int i;
 if(!context){
  return 1;
 }
 if(context->core.in_packet.remaining_length != 2){
  return 2;
 }
 mqtt3_log_printf(0x10, "Received CONNACK");
 if(_mosquitto_read_byte(&context->core.in_packet, &byte)) return 1;
 if(_mosquitto_read_byte(&context->core.in_packet, &rc)) return 1;
 if(client_connack_callback){
  client_connack_callback(rc);
 }
 switch(rc){
  case 0:
   if(context->bridge){
    for(i=0; i<context->bridge->topic_count; i++){
     if(context->bridge->topics[i].direction == bd_in || context->bridge->topics[i].direction == bd_both){
      if(mqtt3_raw_subscribe(context, 0, context->bridge->topics[i].topic, 2)){
       return 1;
      }
     }
    }
   }
   context->core.state = mosq_cs_connected;
   return 0;
  case 1:
   mqtt3_log_printf(0x08, "Connection Refused: unacceptable protocol version");
   return 1;
  case 2:
   mqtt3_log_printf(0x08, "Connection Refused: identifier rejected");
   return 1;
  case 3:
   mqtt3_log_printf(0x08, "Connection Refused: broker unavailable");
   return 1;
 }
 return 1;
}

canonical_original_code:
int mqtt3_handle_connack(struct _mqtt3_context *context)
{
 unsigned char byte;
 unsigned char rc;
 int i;
 if(!context){
  return 1;
 }
 if(context->core.in_packet.remaining_length != 2){
  return 2;
 }
 mqtt3_log_printf(0x10, "Received CONNACK");
 if(_mosquitto_read_byte(&context->core.in_packet, &byte)) return 1;
 if(_mosquitto_read_byte(&context->core.in_packet, &rc)) return 1;
 if(client_connack_callback){
  client_connack_callback(rc);
 }
 switch(rc){
  case 0:
   if(context->bridge){
    for(i=0; i<context->bridge->topic_count; i++){
     if(context->bridge->topics[i].direction == bd_in || context->bridge->topics[i].direction == bd_both){
      if(mqtt3_raw_subscribe(context, 0, context->bridge->topics[i].topic, 2)){
       return 1;
      }
     }
    }
   }
   context->core.state = mosq_cs_connected;
   return 0;
  case 1:
   mqtt3_log_printf(0x08, "Connection Refused: unacceptable protocol version");
   return 1;
  case 2:
   mqtt3_log_printf(0x08, "Connection Refused: identifier rejected");
   return 1;
  case 3:
   mqtt3_log_printf(0x08, "Connection Refused: broker unavailable");
   return 1;
 }
 return 1;
}

variable_types:
byte
unsigned char

variable_types:
rc
unsigned char

variable_types: i int


decompiled_code:
__int64 __fastcall mqtt3_handle_suback(__int64 a1)
{
unsigned int v1; // er12
char v3; // [rsp+Dh] [rbp-1Bh] BYREF
char v4[26]; // [rsp+Eh] [rbp-1Ah] BYREF

mqtt3_log_printf(16LL, "Received SUBACK");
v1 = mosquitto_read_uint16(a1 + 64, v4);
if ( v1 )
return 1LL;
while ( *(_DWORD *)(a1 + 84) < *(_DWORD *)(a1 + 76) )
{
if ( (unsigned int)mosquitto_read_byte(a1 + 64, &v3) )
return 1LL;
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
unsigned int v1; // er12
char v3; // [rsp+Dh] [rbp-1Bh] BYREF
char v4[26]; // [rsp+Eh] [rbp-1Ah] BYREF

mqtt3_log_printf(16LL, "Received SUBACK");
v1 = mosquitto_read_uint16(a1 + 64, v4);
if ( v1 )
return 1LL;
while ( *(_DWORD *)(a1 + 84) < *(_DWORD *)(a1 + 76) )
{
if ( (unsigned int)mosquitto_read_byte(a1 + 64, &v3) )
return 1LL;
}
return v1;
}

original_code:
int mqtt3_handle_suback(mqtt3_context *context)
{
 uint16_t mid;
 uint8_t granted_qos;
 mqtt3_log_printf(0x10, "Received SUBACK");
 if(_mosquitto_read_uint16(&context->core.in_packet, &mid)) return 1;
 while(context->core.in_packet.pos < context->core.in_packet.remaining_length){
  if(_mosquitto_read_byte(&context->core.in_packet, &granted_qos)) return 1;
 }
 return 0;
}

canonical_original_code:
int mqtt3_handle_suback(struct _mqtt3_context *context)
{
 unsigned short mid;
 unsigned char granted_qos;
 mqtt3_log_printf(0x10, "Received SUBACK");
 if(_mosquitto_read_uint16(&context->core.in_packet, &mid)) return 1;
 while(context->core.in_packet.pos < context->core.in_packet.remaining_length){
  if(_mosquitto_read_byte(&context->core.in_packet, &granted_qos)) return 1;
 }
 return 0;
}


decompiled_code:
__int64 __fastcall mqtt3_handle_unsuback(__int64 a1)
{
__int64 result; // rax
char v2[10]; // [rsp+0h] [rbp-Ah] BYREF

if ( !a1 )
return 1LL;
result = 2LL;
if ( *(_DWORD *)(a1 + 76) == 2 )
{
mqtt3_log_printf(16LL, "Received UNSUBACK");
result = (unsigned int)mosquitto_read_uint16(a1 + 64, v2) != 0;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
__int64 result; // rax
char v2[10]; // [rsp+0h] [rbp-Ah] BYREF

if ( !a1 )
return 1LL;
result = 2LL;
if ( *(_DWORD *)(a1 + 76) == 2 )
{
mqtt3_log_printf(16LL, "Received UNSUBACK");
result = (unsigned int)mosquitto_read_uint16(a1 + 64, v2) != 0;
}
return result;
}

original_code:
int mqtt3_handle_unsuback(mqtt3_context *context)
{
 uint16_t mid;
 if(!context){
  return 1;
 }
 if(context->core.in_packet.remaining_length != 2){
  return 2;
 }
 mqtt3_log_printf(0x10, "Received UNSUBACK");
 if(_mosquitto_read_uint16(&context->core.in_packet, &mid)) return 1;
 return 0;
}

canonical_original_code:
int mqtt3_handle_unsuback(struct _mqtt3_context *context)
{
 unsigned short mid;
 if(!context){
  return 1;
 }
 if(context->core.in_packet.remaining_length != 2){
  return 2;
 }
 mqtt3_log_printf(0x10, "Received UNSUBACK");
 if(_mosquitto_read_uint16(&context->core.in_packet, &mid)) return 1;
 return 0;
}


decompiled_code:
__int64 __fastcall mqtt3_log_init(int a1, int a2)
{
log_destinations = a2;
log_priorities = a1;
if ( (a2 & 1) != 0 )
openlog("mosquitto", 1, 24);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, int a2)
{
log_destinations = a2;
log_priorities = a1;
if ( (a2 & 1) != 0 )
openlog("mosquitto", 1, 24);
return 0LL;
}

original_code:
int mqtt3_log_init(int priorities, int destinations)
{
 int rc = 0;
 log_priorities = priorities;
 log_destinations = destinations;
 if(log_destinations & 0x01){
  openlog("mosquitto", 0x01, (3<<3));
 }
 return rc;
}

variable_types: priorities int

variable_types: destinations int


decompiled_code:
__int64 mqtt3_log_close()
{
if ( (log_destinations & 1) != 0 )
closelog();
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
if ( (log_destinations & 1) != 0 )
closelog();
return 0LL;
}

original_code:
int mqtt3_log_close(void)
{
 if(log_destinations & 0x01){
  closelog();
 }
 return 0;
}

canonical_original_code:
int mqtt3_log_close()
{
 if(log_destinations & 0x01){
  closelog();
 }
 return 0;
}


decompiled_code:
__int64 mqtt3_log_printf(int a1, const char *a2, ...)
{
__int64 v2; // rax
const char *v3; // r12
int v4; // ebp
char v5; // al
unsigned int v6; // eax
gcc_va_list arg; // [rsp+8h] [rbp-2E0h] BYREF
char s[512]; // [rsp+20h] [rbp-2C8h] BYREF

if ( (log_priorities & a1) != 0 && log_destinations )
{
v2 = (unsigned int)(a1 - 1);
if ( (unsigned int)v2 > 0xF )
{
v4 = 3;
v3 = "$SYS/broker/log/E";
}
else
{
v3 = CSWTCH_15[v2];
v4 = CSWTCH_16[v2];
}
va_start(arg, a2);
vsnprintf(s, 0x1F4uLL, a2, arg);
v5 = log_destinations;
if ( (log_destinations & 4) != 0 )
{
fprintf(stdout, "%s\n", s);
fflush(stdout);
v5 = log_destinations;
}
if ( (v5 & 8) != 0 )
{
fprintf(stderr, "%s\n", s);
fflush(stderr);
v5 = log_destinations;
}
if ( (v5 & 1) != 0 )
{
syslog(v4, "%s", s);
v5 = log_destinations;
}
if ( (v5 & 0x10) != 0 && a1 != 16 )
{
v6 = strlen(s);
mqtt3_db_messages_easy_queue(&int_db, 0LL, v3, 2LL, v6, s, 0LL);
}
}
return 0LL;
}

canonical_decompiled_code:
__int64 func2(int a1, const char *a2, ...)
{
__int64 v2; // rax
const char *v3; // r12
int v4; // ebp
char v5; // al
unsigned int v6; // eax
gcc_va_list arg; // [rsp+8h] [rbp-2E0h] BYREF
char s[512]; // [rsp+20h] [rbp-2C8h] BYREF

if ( (log_priorities & a1) != 0 && log_destinations )
{
v2 = (unsigned int)(a1 - 1);
if ( (unsigned int)v2 > 0xF )
{
v4 = 3;
v3 = "$SYS/broker/log/E";
}
else
{
v3 = CSWTCH_15[v2];
v4 = CSWTCH_16[v2];
}
va_start(arg, a2);
vsnprintf(s, 0x1F4uLL, a2, arg);
v5 = log_destinations;
if ( (log_destinations & 4) != 0 )
{
fprintf(stdout, "%s\n", s);
fflush(stdout);
v5 = log_destinations;
}
if ( (v5 & 8) != 0 )
{
fprintf(stderr, "%s\n", s);
fflush(stderr);
v5 = log_destinations;
}
if ( (v5 & 1) != 0 )
{
syslog(v4, "%s", s);
v5 = log_destinations;
}
if ( (v5 & 0x10) != 0 && a1 != 16 )
{
v6 = strlen(s);
mqtt3_db_messages_easy_queue(&int_db, 0LL, v3, 2LL, v6, s, 0LL);
}
}
return 0LL;
}

original_code:
int mqtt3_log_printf(int priority, const char *fmt, ...)
{
 va_list va;
 char s[500];
 const char *topic;
 int syslog_priority;
 if((log_priorities & priority) && log_destinations != 0x00){
  switch(priority){
   case 0x10:
    topic = "$SYS/broker/log/D";
    syslog_priority = 7;
    break;
   case 0x08:
    topic = "$SYS/broker/log/E";
    syslog_priority = 3;
    break;
   case 0x04:
    topic = "$SYS/broker/log/W";
    syslog_priority = 4;
    break;
   case 0x02:
    topic = "$SYS/broker/log/N";
    syslog_priority = 5;
    break;
   case 0x01:
    topic = "$SYS/broker/log/I";
    syslog_priority = 6;
    break;
   default:
    topic = "$SYS/broker/log/E";
    syslog_priority = 3;
  }
  __builtin_va_start(va,fmt);
  vsnprintf(s, 500, fmt, va);
  __builtin_va_end(va);
  if(log_destinations & 0x04){
   fprintf(stdout, "%s\n", s);
   fflush(stdout);
  }
  if(log_destinations & 0x08){
   fprintf(stderr, "%s\n", s);
   fflush(stderr);
  }
  if(log_destinations & 0x01){
   syslog(syslog_priority, "%s", s);
  }
  if(log_destinations & 0x10 && priority != 0x10){
   mqtt3_db_messages_easy_queue(&int_db, ((void *)0), topic, 2, strlen(s), (uint8_t *)s, 0);
  }
 }
 return 0;
}

canonical_original_code:
int mqtt3_log_printf(int priority, char *fmt, ...)
{
 __builtin_va_list va;
 char s[500];
 char *topic;
 int syslog_priority;
 if((log_priorities & priority) && log_destinations != 0x00){
  switch(priority){
   case 0x10:
    topic = "$SYS/broker/log/D";
    syslog_priority = 7;
    break;
   case 0x08:
    topic = "$SYS/broker/log/E";
    syslog_priority = 3;
    break;
   case 0x04:
    topic = "$SYS/broker/log/W";
    syslog_priority = 4;
    break;
   case 0x02:
    topic = "$SYS/broker/log/N";
    syslog_priority = 5;
    break;
   case 0x01:
    topic = "$SYS/broker/log/I";
    syslog_priority = 6;
    break;
   default:
    topic = "$SYS/broker/log/E";
    syslog_priority = 3;
  }
  __builtin_va_start(va,fmt);
  vsnprintf(s, 500, fmt, va);
  __builtin_va_end(va);
  if(log_destinations & 0x04){
   fprintf(stdout, "%s\n", s);
   fflush(stdout);
  }
  if(log_destinations & 0x08){
   fprintf(stderr, "%s\n", s);
   fflush(stderr);
  }
  if(log_destinations & 0x01){
   syslog(syslog_priority, "%s", s);
  }
  if(log_destinations & 0x10 && priority != 0x10){
   mqtt3_db_messages_easy_queue(&int_db, ((void *)0), topic, 2, strlen(s), (unsigned char *)s, 0);
  }
 }
 return 0;
}

variable_types: priority int

variable_types:
fmt
char *

variable_types:
s
char [500]

variable_types:
topic
char *


decompiled_code:
int __fastcall my_message_callback(__int64 a1, __int64 a2)
{
int result; // eax

result = *(_DWORD *)(a2 + 24);
if ( verbose )
{
if ( result )
printf("%s %s\n", *(const char **)(a2 + 8), *(const char **)(a2 + 16));
else
printf("%s (null)\n", *(const char **)(a2 + 8));
result = fflush(stdout);
}
else if ( result )
{
puts(*(const char **)(a2 + 16));
result = fflush(stdout);
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1, __int64 a2)
{
int result; // eax

result = *(_DWORD *)(a2 + 24);
if ( verbose )
{
if ( result )
printf("%s %s\n", *(const char **)(a2 + 8), *(const char **)(a2 + 16));
else
printf("%s (null)\n", *(const char **)(a2 + 8));
result = fflush(stdout);
}
else if ( result )
{
puts(*(const char **)(a2 + 16));
result = fflush(stdout);
}
return result;
}

original_code:
void my_message_callback(void *obj, const struct mosquitto_message *message)
{
 if(verbose){
  if(message->payloadlen){
   printf("%s %s\n", message->topic, message->payload);
  }else{
   printf("%s (null)\n", message->topic);
  }
  fflush(stdout);
 }else{
  if(message->payloadlen){
   printf("%s\n", message->payload);
   fflush(stdout);
  }
 }
}

canonical_original_code:
void my_message_callback(void *obj, struct mosquitto_message *message)
{
 if(verbose){
  if(message->payloadlen){
   printf("%s %s\n", message->topic, message->payload);
  }else{
   printf("%s (null)\n", message->topic);
  }
  fflush(stdout);
 }else{
  if(message->payloadlen){
   printf("%s\n", message->payload);
   fflush(stdout);
  }
 }
}

variable_types:
obj
void *

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


decompiled_code:
int __fastcall my_subscribe_callback(__int64 a1, unsigned __int16 a2, int a3, unsigned __int8 *a4)
{
unsigned __int8 *v6; // rbx
__int64 v7; // rbp
__int64 v8; // rsi

printf("Subscribed (mid: %d): %d", a2, *a4);
if ( a3 > 1 )
{
v6 = a4 + 1;
v7 = (__int64)&a4[a3 - 2 + 2];
do
{
v8 = *v6++;
printf(", %d", v8);
}
while ( v6 != (unsigned __int8 *)v7 );
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1, unsigned __int16 a2, int a3, unsigned __int8 *a4)
{
unsigned __int8 *v6; // rbx
__int64 v7; // rbp
__int64 v8; // rsi

printf("Subscribed (mid: %d): %d", a2, *a4);
if ( a3 > 1 )
{
v6 = a4 + 1;
v7 = (__int64)&a4[a3 - 2 + 2];
do
{
v8 = *v6++;
printf(", %d", v8);
}
while ( v6 != (unsigned __int8 *)v7 );
}
return putchar(10);
}

original_code:
void my_subscribe_callback(void *obj, uint16_t mid, int qos_count, const uint8_t *granted_qos)
{
 int i;
 printf("Subscribed (mid: %d): %d", mid, granted_qos[0]);
 for(i=1; i<qos_count; i++){
  printf(", %d", granted_qos[i]);
 }
 printf("\n");
}

canonical_original_code:
void my_subscribe_callback(void *obj, unsigned short mid, int qos_count, unsigned char *granted_qos)
{
 int i;
 printf("Subscribed (mid: %d): %d", mid, granted_qos[0]);
 for(i=1; i<qos_count; i++){
  printf(", %d", granted_qos[i]);
 }
 printf("\n");
}

variable_types:
mid
unsigned short