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


repo: adamvr/mosquitto-homebrew-mods

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

if ( !a1 )
return 1LL;
result = 1LL;
if ( a3 )
{
v7 = mosquitto_calloc(1LL, 40LL);
result = 1LL;
if ( v7 )
{
v8 = 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);
result = (unsigned int)mqtt3_net_packet_queue(a1, v7) != 0;
}
else
{
mosquitto_free(v7);
result = 1LL;
}
}
}
return result;
}

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

if ( !a1 )
return 1LL;
result = 1LL;
if ( a3 )
{
v7 = mosquitto_calloc(1LL, 40LL);
result = 1LL;
if ( v7 )
{
v8 = 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);
result = (unsigned int)mqtt3_net_packet_queue(a1, v7) != 0;
}
else
{
mosquitto_free(v7);
result = 1LL;
}
}
}
return result;
}

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: 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:
topic
char *

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

variable_types:
packetlen
unsigned int

variable_types:
mid
unsigned short

return_type: 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: 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_unsubscribe(__int64 a1, char a2, const char *a3)
{
_BOOL8 result; // rax
__int64 v5; // r13
unsigned int v6; // eax
__int64 v7; // rax
unsigned __int16 v8; // ax
unsigned __int16 v9; // ax

if ( !a1 )
return 1LL;
result = 1LL;
if ( a3 )
{
v5 = mosquitto_calloc(1LL, 40LL);
result = 1LL;
if ( v5 )
{
v6 = 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);
result = (unsigned int)mqtt3_net_packet_queue(a1, v5) != 0;
}
else
{
mosquitto_free(v5);
result = 1LL;
}
}
}
return result;
}

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

if ( !a1 )
return 1LL;
result = 1LL;
if ( a3 )
{
v5 = mosquitto_calloc(1LL, 40LL);
result = 1LL;
if ( v5 )
{
v6 = 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);
result = (unsigned int)mqtt3_net_packet_queue(a1, v5) != 0;
}
else
{
mosquitto_free(v5);
result = 1LL;
}
}
}
return result;
}

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

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

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

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

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

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

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

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


decompiled_code:
__int64 __fastcall mosquitto_log_init(__int64 a1, int a2, int a3)
{
if ( !a1 )
_assert_fail("mosq", "logging_mosq.c", 0x27u, "mosquitto_log_init");
*(_DWORD *)(a1 + 152) = a2;
*(_DWORD *)(a1 + 156) = a3;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2, int a3)
{
if ( !a1 )
_assert_fail("mosq", "logging_mosq.c", 0x27u, "mosquitto_log_init");
*(_DWORD *)(a1 + 152) = a2;
*(_DWORD *)(a1 + 156) = a3;
return 0LL;
}

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; // rbx
char v4[1036]; // [rsp+0h] [rbp-4A8h] BYREF
socklen_t len; // [rsp+40Ch] [rbp-9Ch] BYREF
struct sockaddr addr; // [rsp+410h] [rbp-98h] 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);
*(_QWORD *)(v2 + 48) = time(0LL);
*(_WORD *)(v2 + 32) = 60;
*(_BYTE *)(v2 + 120) = 1;
*(_QWORD *)(v2 + 8) = 0LL;
*(_WORD *)(v2 + 56) = 0;
*(_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 ( addr.sa_family != 10 || !inet_ntop(10, &addr.sa_data[6], v4, 0x400u) )
goto LABEL_5;
goto LABEL_8;
}
if ( inet_ntop(2, &addr.sa_data[2], v4, 0x400u) )
{
LABEL_8:
*(_QWORD *)(v2 + 128) = mosquitto_strdup(v4);
goto LABEL_5;
}
}
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 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:
void __fastcall mqtt3_context_cleanup(__int64 a1, __int64 a2, char a3)
{
__int64 i; // rdi
__int64 v5; // rax
_QWORD *v6; // rbp
_QWORD *v7; // rdi

if ( a2 )
{
if ( *(_DWORD *)a2 != -1 )
mqtt3_socket_close(a2);
if ( *(_BYTE *)(a2 + 120) && !*(_BYTE *)(a2 + 121) )
{
mqtt3_subs_clean_session(a2, a1 + 8);
mqtt3_db_messages_delete(a2);
}
if ( *(_QWORD *)(a2 + 128) )
((void (*)(void))mosquitto_free)();
if ( *(_QWORD *)(a2 + 8) )
((void (*)(void))mosquitto_free)();
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);
((void (*)(void))mosquitto_free)();
}
v5 = *(_QWORD *)(a2 + 112);
if ( v5 )
{
if ( *(_QWORD *)(v5 + 8) )
((void (*)(void))mosquitto_free)();
if ( *(_QWORD *)(*(_QWORD *)(a2 + 112) + 16LL) )
((void (*)(void))mosquitto_free)();
mosquitto_free(*(_QWORD *)(a2 + 112));
}
v6 = *(_QWORD **)(a2 + 144);
while ( v6 )
{
v7 = v6;
v6 = (_QWORD *)*v6;
--*(_DWORD *)(v7[1] + 16LL);
mosquitto_free(v7);
}
if ( a3 )
mosquitto_free(a2);
}
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1, __int64 a2, char a3)
{
__int64 i; // rdi
__int64 v5; // rax
_QWORD *v6; // rbp
_QWORD *v7; // rdi

if ( a2 )
{
if ( *(_DWORD *)a2 != -1 )
mqtt3_socket_close(a2);
if ( *(_BYTE *)(a2 + 120) && !*(_BYTE *)(a2 + 121) )
{
mqtt3_subs_clean_session(a2, a1 + 8);
mqtt3_db_messages_delete(a2);
}
if ( *(_QWORD *)(a2 + 128) )
((void (*)(void))mosquitto_free)();
if ( *(_QWORD *)(a2 + 8) )
((void (*)(void))mosquitto_free)();
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);
((void (*)(void))mosquitto_free)();
}
v5 = *(_QWORD *)(a2 + 112);
if ( v5 )
{
if ( *(_QWORD *)(v5 + 8) )
((void (*)(void))mosquitto_free)();
if ( *(_QWORD *)(*(_QWORD *)(a2 + 112) + 16LL) )
((void (*)(void))mosquitto_free)();
mosquitto_free(*(_QWORD *)(a2 + 112));
}
v6 = *(_QWORD **)(a2 + 144);
while ( v6 )
{
v7 = v6;
v6 = (_QWORD *)*v6;
--*(_DWORD *)(v7[1] + 16LL);
mosquitto_free(v7);
}
if ( a3 )
mosquitto_free(a2);
}
}

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


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

if ( !a1 )
return 1;
v1 = 2;
if ( *(_DWORD *)(a1 + 76) == 2 )
{
v1 = mosquitto_read_uint16(a1 + 64, v3);
if ( v1 )
{
v1 = 1;
}
else
{
mqtt3_log_printf(16LL, "Received PUBACK 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;
}

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

if ( !a1 )
return 1;
v1 = 2;
if ( *(_DWORD *)(a1 + 76) == 2 )
{
v1 = mosquitto_read_uint16(a1 + 64, v3);
if ( v1 )
{
v1 = 1;
}
else
{
mqtt3_log_printf(16LL, "Received PUBACK 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;
}

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

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)
{
__int64 result; // rax

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

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

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

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

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


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


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

if ( !a1 )
return 1;
v1 = 2;
if ( *(_DWORD *)(a1 + 76) == 2 )
{
v1 = mosquitto_read_uint16(a1 + 64, v3);
if ( v1 )
{
v1 = 1;
}
else
{
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;
}

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

if ( !a1 )
return 1;
v1 = 2;
if ( *(_DWORD *)(a1 + 76) == 2 )
{
v1 = mosquitto_read_uint16(a1 + 64, v3);
if ( v1 )
{
v1 = 1;
}
else
{
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;
}

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)
{
unsigned __int8 v2; // r14
__int64 v3; // r12
unsigned int v4; // ebp
int v6; // er13
unsigned int v7; // er15
__int64 v8; // r14
int v9; // eax
int v10; // ebp
__int64 v11; // rdi
int v12; // [rsp+10h] [rbp-68h]
__int64 v13; // [rsp+10h] [rbp-68h]
__int64 v14; // [rsp+28h] [rbp-50h] BYREF
unsigned __int16 v15; // [rsp+36h] [rbp-42h] BYREF
const char *v16[8]; // [rsp+38h] [rbp-40h] BYREF

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

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2)
{
unsigned __int8 v2; // r14
__int64 v3; // r12
unsigned int v4; // ebp
int v6; // er13
unsigned int v7; // er15
__int64 v8; // r14
int v9; // eax
int v10; // ebp
__int64 v11; // rdi
int v12; // [rsp+10h] [rbp-68h]
__int64 v13; // [rsp+10h] [rbp-68h]
__int64 v14; // [rsp+28h] [rbp-50h] BYREF
unsigned __int16 v15; // [rsp+36h] [rbp-42h] BYREF
const char *v16[8]; // [rsp+38h] [rbp-40h] BYREF

v15 = 0;
v2 = *(_BYTE *)(a2 + 64);
v14 = 0LL;
v3 = a2 + 64;
v4 = 1;
if ( !(unsigned int)mosquitto_read_string(a2 + 64, v16) )
{
v4 = mosquitto_fix_sub_topic(v16);
if ( v4 )
return 1;
if ( !*v16[0] )
return 1;
v12 = v2;
v6 = ((int)v2 >> 1) & 3;
if ( v6 && (unsigned int)mosquitto_read_uint16(v3, &v15)
|| (v7 = *(_DWORD *)(a2 + 76) - *(_DWORD *)(a2 + 84),
v8 = v2 & 1,
mqtt3_log_printf(
16LL,
"Received PUBLISH from %s (d%d, q%d, r%d, m%d, '%s', ... (%ld bytes))",
*(const char **)(a2 + 8),
(v12 >> 3) & 1,
(unsigned __int8)v6,
(unsigned int)v8,
v15,
v16[0],
v7),
v13 = 0LL,
v7)
&& (v13 = mosquitto_calloc(v7 + 1, 1LL), (unsigned int)mosquitto_read_bytes(v3, v13, v7)) )
{
mosquitto_free(v16[0]);
return 1;
}
if ( (_BYTE)v6 && (mqtt3_db_message_store_find(a1, *(_QWORD *)(a2 + 8), v15, &v14), v14) )
{
if ( (_BYTE)v6 != 2 )
{
LABEL_18:
if ( (unsigned __int8)v6 <= 2u )
{
if ( (_BYTE)v6 )
{
v10 = mqtt3_db_messages_queue(a1, *(_QWORD *)(a2 + 8), v16[0], 1LL, (unsigned int)v8, v14);
v4 = ((unsigned int)mqtt3_raw_puback(a2, v15) | v10) != 0;
}
else
{
v4 = mqtt3_db_messages_queue(a1, *(_QWORD *)(a2 + 8), v16[0], 0LL, (unsigned int)v8, v14) != 0;
}
}
LABEL_24:
mosquitto_free(v16[0]);
v11 = v13;
if ( !v13 )
return v4;
goto LABEL_22;
}
}
else
{
if ( (unsigned int)mqtt3_db_message_store(
a1,
*(_QWORD *)(a2 + 8),
v15,
v16[0],
(unsigned __int8)v6,
v7,
v13,
v8,
&v14,
0LL) )
{
mosquitto_free(v16[0]);
v4 = 1;
v11 = v13;
if ( !v13 )
return v4;
LABEL_22:
mosquitto_free(v11);
return v4;
}
if ( (_BYTE)v6 != 2 )
goto LABEL_18;
v9 = mqtt3_db_message_insert(a2, v15, 0LL, 2LL, 0LL, v14);
if ( v9 )
{
v4 = v9 == 1;
goto LABEL_24;
}
}
v4 = mqtt3_raw_pubrec(a2, v15) != 0;
goto LABEL_24;
}
return 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:
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)
{
_BOOL8 result; // rax
int v2; // edx
int v3; // edx
unsigned __int16 v4[5]; // [rsp+0h] [rbp-Ah] BYREF

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

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

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

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

variable_types:
mid
unsigned short


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

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

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

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

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
__int64 result; // rax

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 )
{
result = mqtt3_handle_pingreq(a2);
}
else if ( v2 <= 0xC0u )
{
switch ( v2 )
{
case 0xA0:
result = mqtt3_handle_unsubscribe();
break;
case 0xB0:
result = mqtt3_handle_unsuback(a2);
break;
case 0x90:
result = mqtt3_handle_suback(a2);
break;
default:
result = 2LL;
break;
}
}
else if ( v2 == 0xD0 )
{
result = mqtt3_handle_pingresp(a2);
}
else if ( v2 == 0xE0 )
{
result = mqtt3_handle_disconnect(a2);
}
else
{
result = 2LL;
}
}
else if ( v2 == 64 )
{
result = mqtt3_handle_puback(a2);
}
else if ( v2 <= 0x40u )
{
switch ( v2 )
{
case 0x20u:
result = mqtt3_handle_connack(a2);
break;
case 0x30u:
result = mqtt3_handle_publish(a1, a2);
break;
case 0x10u:
result = mqtt3_handle_connect();
break;
default:
result = 2LL;
break;
}
}
else
{
switch ( v2 )
{
case '`':
result = mqtt3_handle_pubrel(a1, a2);
break;
case 'p':
result = mqtt3_handle_pubcomp(a2);
break;
case 'P':
result = mqtt3_handle_pubrec(a2);
break;
default:
result = 2LL;
break;
}
}
return result;
}

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

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 )
{
result = func1(a2);
}
else if ( v2 <= 0xC0u )
{
switch ( v2 )
{
case 0xA0:
result = mqtt3_handle_unsubscribe();
break;
case 0xB0:
result = mqtt3_handle_unsuback(a2);
break;
case 0x90:
result = mqtt3_handle_suback(a2);
break;
default:
result = 2LL;
break;
}
}
else if ( v2 == 0xD0 )
{
result = func2(a2);
}
else if ( v2 == 0xE0 )
{
result = mqtt3_handle_disconnect(a2);
}
else
{
result = 2LL;
}
}
else if ( v2 == 64 )
{
result = func0(a2);
}
else if ( v2 <= 0x40u )
{
switch ( v2 )
{
case 0x20u:
result = mqtt3_handle_connack(a2);
break;
case 0x30u:
result = func4(a1, a2);
break;
case 0x10u:
result = mqtt3_handle_connect();
break;
default:
result = 2LL;
break;
}
}
else
{
switch ( v2 )
{
case '`':
result = func6(a1, a2);
break;
case 'p':
result = func3(a2);
break;
case 'P':
result = func5(a2);
break;
default:
result = 2LL;
break;
}
}
return result;
}

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; // ebp
__int64 v3; // rdx
__int64 v4; // r12
__int64 v5; // rax
unsigned __int8 v6; // [rsp+Eh] [rbp-1Ah] BYREF
char v7[25]; // [rsp+Fh] [rbp-19h] BYREF

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

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

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

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 1;
while ( *(_DWORD *)(a1 + 84) < *(_DWORD *)(a1 + 76) )
{
if ( (unsigned int)mosquitto_read_byte(a1 + 64, &v3) )
return 1;
}
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 1;
while ( *(_DWORD *)(a1 + 84) < *(_DWORD *)(a1 + 76) )
{
if ( (unsigned int)mosquitto_read_byte(a1 + 64, &v3) )
return 1;
}
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_priorities = a1;
log_destinations = a2;
if ( (a2 & 1) != 0 )
openlog("mosquitto", 1, 24);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, int a2)
{
log_priorities = a1;
log_destinations = a2;
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, ...)
{
int v2; // ebp
const char *v3; // r12
unsigned int v5; // eax
char v6[504]; // [rsp+0h] [rbp-2D8h] BYREF
gcc_va_list arg; // [rsp+1F8h] [rbp-E0h] BYREF

if ( (log_priorities & a1) != 0 && log_destinations )
{
if ( a1 == 4 )
{
v2 = 4;
v3 = "$SYS/broker/log/W";
}
else if ( a1 > 4 )
{
v2 = 4 * (a1 == 16) + 3;
v3 = "$SYS/broker/log/E";
if ( a1 == 16 )
v3 = "$SYS/broker/log/D";
}
else if ( a1 == 1 )
{
v2 = 6;
v3 = "$SYS/broker/log/I";
}
else
{
v2 = 2 * (a1 == 2) + 3;
v3 = "$SYS/broker/log/E";
if ( a1 == 2 )
v3 = "$SYS/broker/log/N";
}
va_start(arg, a2);
vsnprintf(v6, 0x1F4uLL, a2, arg);
if ( (log_destinations & 4) != 0 )
{
fprintf(stdout, "%s\n", v6);
fflush(stdout);
}
if ( (log_destinations & 8) != 0 )
{
fprintf(stderr, "%s\n", v6);
fflush(stderr);
}
if ( (log_destinations & 1) != 0 )
syslog(v2, "%s", v6);
if ( (log_destinations & 0x10) != 0 && a1 != 16 )
{
v5 = strlen(v6);
mqtt3_db_messages_easy_queue(&int_db, 0LL, v3, 2LL, v5, v6, 0LL);
}
}
return 0LL;
}

canonical_decompiled_code:
__int64 func2(int a1, const char *a2, ...)
{
int v2; // ebp
const char *v3; // r12
unsigned int v5; // eax
char v6[504]; // [rsp+0h] [rbp-2D8h] BYREF
gcc_va_list arg; // [rsp+1F8h] [rbp-E0h] BYREF

if ( (log_priorities & a1) != 0 && log_destinations )
{
if ( a1 == 4 )
{
v2 = 4;
v3 = "$SYS/broker/log/W";
}
else if ( a1 > 4 )
{
v2 = 4 * (a1 == 16) + 3;
v3 = "$SYS/broker/log/E";
if ( a1 == 16 )
v3 = "$SYS/broker/log/D";
}
else if ( a1 == 1 )
{
v2 = 6;
v3 = "$SYS/broker/log/I";
}
else
{
v2 = 2 * (a1 == 2) + 3;
v3 = "$SYS/broker/log/E";
if ( a1 == 2 )
v3 = "$SYS/broker/log/N";
}
va_start(arg, a2);
vsnprintf(v6, 0x1F4uLL, a2, arg);
if ( (log_destinations & 4) != 0 )
{
fprintf(stdout, "%s\n", v6);
fflush(stdout);
}
if ( (log_destinations & 8) != 0 )
{
fprintf(stderr, "%s\n", v6);
fflush(stderr);
}
if ( (log_destinations & 1) != 0 )
syslog(v2, "%s", v6);
if ( (log_destinations & 0x10) != 0 && a1 != 16 )
{
v5 = strlen(v6);
mqtt3_db_messages_easy_queue(&int_db, 0LL, v3, 2LL, v5, v6, 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

if ( verbose )
{
if ( *(_DWORD *)(a2 + 24) )
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 ( *(_DWORD *)(a2 + 24) )
{
puts(*(const char **)(a2 + 16));
result = fflush(stdout);
}
return result;
}

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

if ( verbose )
{
if ( *(_DWORD *)(a2 + 24) )
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 ( *(_DWORD *)(a2 + 24) )
{
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

printf("Subscribed (mid: %d): %d", a2, *a4);
if ( a3 > 1 )
{
v6 = a4 + 1;
v7 = (__int64)&a4[a3 - 2 + 2];
do
printf(", %d", *v6++);
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

printf("Subscribed (mid: %d): %d", a2, *a4);
if ( a3 > 1 )
{
v6 = a4 + 1;
v7 = (__int64)&a4[a3 - 2 + 2];
do
printf(", %d", *v6++);
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");
}


decompiled_code:
int print_usage()
{
puts("mosquitto_sub is a simple mqtt client that will subscribe to a single topic and print all messages it receives.\n");
puts("Usage: mosquitto_sub [-c] [-i id] [-k keepalive] [-p port] [-q qos] [-v] -t topic ...");
puts(" [-u username [--pw password]]");
puts(" [--will-topic [--will-payload payload] [--will-qos qos] [--will-retain]]\n");
puts(" -c : disable 'clean session' (store subscription and pending messages when client disconnects).");
puts(" -d : enable debug messages.");
puts(" -h : mqtt host to connect to. Defaults to localhost.");
puts(" -i : id to use for this client. Defaults to mosquitto_sub_ appended with the process id.");
puts(" -k : keep alive in seconds for this client. Defaults to 60.");
puts(" -p : network port to connect to. Defaults to 1883.");
puts(" -q : quality of service level to use for the subscription. Defaults to 0.");
puts(" -t : mqtt topic to subscribe to. May be repeated multiple times.");
puts(" -u : provide a username (requires MQTT 3.1 broker)");
puts(" -v : print published messages verbosely.");
puts(" --pw : provide a password (requires MQTT 3.1 broker)");
puts(" --will-payload : payload for the client Will, which is sent by the broker in case of");
puts(" unexpected disconnection. If not given and will-topic is set, a zero");
puts(" length message will be sent.");
puts(" --will-qos : QoS level for the client Will.");
puts(" --will-retain : if given, make the client Will retained.");
return puts(" --will-topic : the topic on which to publish the client Will.");
}

canonical_decompiled_code:
int func3()
{
puts("mosquitto_sub is a simple mqtt client that will subscribe to a single topic and print all messages it receives.\n");
puts("Usage: mosquitto_sub [-c] [-i id] [-k keepalive] [-p port] [-q qos] [-v] -t topic ...");
puts(" [-u username [--pw password]]");
puts(" [--will-topic [--will-payload payload] [--will-qos qos] [--will-retain]]\n");
puts(" -c : disable 'clean session' (store subscription and pending messages when client disconnects).");
puts(" -d : enable debug messages.");
puts(" -h : mqtt host to connect to. Defaults to localhost.");
puts(" -i : id to use for this client. Defaults to mosquitto_sub_ appended with the process id.");
puts(" -k : keep alive in seconds for this client. Defaults to 60.");
puts(" -p : network port to connect to. Defaults to 1883.");
puts(" -q : quality of service level to use for the subscription. Defaults to 0.");
puts(" -t : mqtt topic to subscribe to. May be repeated multiple times.");
puts(" -u : provide a username (requires MQTT 3.1 broker)");
puts(" -v : print published messages verbosely.");
puts(" --pw : provide a password (requires MQTT 3.1 broker)");
puts(" --will-payload : payload for the client Will, which is sent by the broker in case of");
puts(" unexpected disconnection. If not given and will-topic is set, a zero");
puts(" length message will be sent.");
puts(" --will-qos : QoS level for the client Will.");
puts(" --will-retain : if given, make the client Will retained.");
return puts(" --will-topic : the topic on which to publish the client Will.");
}

original_code:
void print_usage(void)
{
 printf("mosquitto_sub is a simple mqtt client that will subscribe to a single topic and print all messages it receives.\n\n");
 printf("Usage: mosquitto_sub [-c] [-i id] [-k keepalive] [-p port] [-q qos] [-v] -t topic ...\n");
 printf("                     [-u username [--pw password]]\n");
 printf("                     [--will-topic [--will-payload payload] [--will-qos qos] [--will-retain]]\n\n");
 printf(" -c : disable 'clean session' (store subscription and pending messages when client disconnects).\n");
 printf(" -d : enable debug messages.\n");
 printf(" -h : mqtt host to connect to. Defaults to localhost.\n");
 printf(" -i : id to use for this client. Defaults to mosquitto_sub_ appended with the process id.\n");
 printf(" -k : keep alive in seconds for this client. Defaults to 60.\n");
 printf(" -p : network port to connect to. Defaults to 1883.\n");
 printf(" -q : quality of service level to use for the subscription. Defaults to 0.\n");
 printf(" -t : mqtt topic to subscribe to. May be repeated multiple times.\n");
 printf(" -u : provide a username (requires MQTT 3.1 broker)\n");
 printf(" -v : print published messages verbosely.\n");
 printf(" --pw : provide a password (requires MQTT 3.1 broker)\n");
 printf(" --will-payload : payload for the client Will, which is sent by the broker in case of\n");
 printf("                  unexpected disconnection. If not given and will-topic is set, a zero\n");
 printf("                  length message will be sent.\n");
 printf(" --will-qos : QoS level for the client Will.\n");
 printf(" --will-retain : if given, make the client Will retained.\n");
 printf(" --will-topic : the topic on which to publish the client Will.\n");
}

canonical_original_code:
void print_usage()
{
 printf("mosquitto_sub is a simple mqtt client that will subscribe to a single topic and print all messages it receives.\n\n");
 printf("Usage: mosquitto_sub [-c] [-i id] [-k keepalive] [-p port] [-q qos] [-v] -t topic ...\n");
 printf("                     [-u username [--pw password]]\n");
 printf("                     [--will-topic [--will-payload payload] [--will-qos qos] [--will-retain]]\n\n");
 printf(" -c : disable 'clean session' (store subscription and pending messages when client disconnects).\n");
 printf(" -d : enable debug messages.\n");
 printf(" -h : mqtt host to connect to. Defaults to localhost.\n");
 printf(" -i : id to use for this client. Defaults to mosquitto_sub_ appended with the process id.\n");
 printf(" -k : keep alive in seconds for this client. Defaults to 60.\n");
 printf(" -p : network port to connect to. Defaults to 1883.\n");
 printf(" -q : quality of service level to use for the subscription. Defaults to 0.\n");
 printf(" -t : mqtt topic to subscribe to. May be repeated multiple times.\n");
 printf(" -u : provide a username (requires MQTT 3.1 broker)\n");
 printf(" -v : print published messages verbosely.\n");
 printf(" --pw : provide a password (requires MQTT 3.1 broker)\n");
 printf(" --will-payload : payload for the client Will, which is sent by the broker in case of\n");
 printf("                  unexpected disconnection. If not given and will-topic is set, a zero\n");
 printf("                  length message will be sent.\n");
 printf(" --will-qos : QoS level for the client Will.\n");
 printf(" --will-retain : if given, make the client Will retained.\n");
 printf(" --will-topic : the topic on which to publish the client Will.\n");
}


decompiled_code:
const char *__fastcall mqtt3_command_to_string(unsigned __int8 a1)
{
const char *result; // rax

if ( a1 == 0x80 )
return "SUBSCRIBE";
if ( a1 > 0x80u )
{
if ( a1 == 0xC0 )
{
result = "PINGREQ";
}
else if ( a1 <= 0xC0u )
{
result = "UNSUBSCRIBE";
if ( a1 != 0xA0 )
{
if ( a1 == 0xB0 )
{
result = "UNSUBACK";
}
else
{
result = "UNKNOWN";
if ( a1 == 0x90 )
result = "SUBACK";
}
}
}
else
{
result = "PINGRESP";
if ( a1 != 0xD0 )
{
result = "UNKNOWN";
if ( a1 == 0xE0 )
result = "DISCONNECT";
}
}
}
else if ( a1 == 64 )
{
result = "PUBACK";
}
else if ( a1 <= 0x40u )
{
result = "CONNACK";
if ( a1 != 32 )
{
if ( a1 == 48 )
{
result = "PUBLISH";
}
else
{
result = "UNKNOWN";
if ( a1 == 16 )
result = "CONNECT";
}
}
}
else
{
result = "PUBREL";
if ( a1 != 96 )
{
if ( a1 == 112 )
{
result = "PUBCOMP";
}
else
{
result = "UNKNOWN";
if ( a1 == 80 )
result = "PUBREC";
}
}
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func0(unsigned __int8 a1)
{
const char *result; // rax

if ( a1 == 0x80 )
return "SUBSCRIBE";
if ( a1 > 0x80u )
{
if ( a1 == 0xC0 )
{
result = "PINGREQ";
}
else if ( a1 <= 0xC0u )
{
result = "UNSUBSCRIBE";
if ( a1 != 0xA0 )
{
if ( a1 == 0xB0 )
{
result = "UNSUBACK";
}
else
{
result = "UNKNOWN";
if ( a1 == 0x90 )
result = "SUBACK";
}
}
}
else
{
result = "PINGRESP";
if ( a1 != 0xD0 )
{
result = "UNKNOWN";
if ( a1 == 0xE0 )
result = "DISCONNECT";
}
}
}
else if ( a1 == 64 )
{
result = "PUBACK";
}
else if ( a1 <= 0x40u )
{
result = "CONNACK";
if ( a1 != 32 )
{
if ( a1 == 48 )
{
result = "PUBLISH";
}
else
{
result = "UNKNOWN";
if ( a1 == 16 )
result = "CONNECT";
}
}
}
else
{
result = "PUBREL";
if ( a1 != 96 )
{
if ( a1 == 112 )
{
result = "PUBCOMP";
}
else
{
result = "UNKNOWN";
if ( a1 == 80 )
result = "PUBREC";
}
}
}
return result;
}

original_code:
const char *mqtt3_command_to_string(uint8_t command)
{
 switch(command){
  case 0x20:
   return "CONNACK";
  case 0x10:
   return "CONNECT";
  case 0xE0:
   return "DISCONNECT";
  case 0xC0:
   return "PINGREQ";
  case 0xD0:
   return "PINGRESP";
  case 0x40:
   return "PUBACK";
  case 0x70:
   return "PUBCOMP";
  case 0x30:
   return "PUBLISH";
  case 0x50:
   return "PUBREC";
  case 0x60:
   return "PUBREL";
  case 0x90:
   return "SUBACK";
  case 0x80:
   return "SUBSCRIBE";
  case 0xB0:
   return "UNSUBACK";
  case 0xA0:
   return "UNSUBSCRIBE";
 }
 return "UNKNOWN";
}

canonical_original_code:
char *mqtt3_command_to_string(unsigned char command)
{
 switch(command){
  case 0x20:
   return "CONNACK";
  case 0x10:
   return "CONNECT";
  case 0xE0:
   return "DISCONNECT";
  case 0xC0:
   return "PINGREQ";
  case 0xD0:
   return "PINGRESP";
  case 0x40:
   return "PUBACK";
  case 0x70:
   return "PUBCOMP";
  case 0x30:
   return "PUBLISH";
  case 0x50:
   return "PUBREC";
  case 0x60:
   return "PUBREL";
  case 0x90:
   return "SUBACK";
  case 0x80:
   return "SUBSCRIBE";
  case 0xB0:
   return "UNSUBACK";
  case 0xA0:
   return "UNSUBSCRIBE";
 }
 return "UNKNOWN";
}

variable_types:
command
unsigned char

return_type: char *


decompiled_code:
void __fastcall mqtt3_check_keepalive(__int64 a1)
{
if ( a1 && *(_DWORD *)a1 != -1 && time(0LL) - *(_QWORD *)(a1 + 48) >= *(unsigned __int16 *)(a1 + 32) )
{
if ( *(_DWORD *)(a1 + 36) == 1 )
mqtt3_raw_pingreq(a1);
else
mqtt3_socket_close(a1);
}
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1)
{
if ( a1 && *(_DWORD *)a1 != -1 && time(0LL) - *(_QWORD *)(a1 + 48) >= *(unsigned __int16 *)(a1 + 32) )
{
if ( *(_DWORD *)(a1 + 36) == 1 )
mqtt3_raw_pingreq(a1);
else
mqtt3_socket_close(a1);
}
}

original_code:
void mqtt3_check_keepalive(mqtt3_context *context)
{
 if(context && context->core.sock != -1 && time(((void *)0)) - context->core.last_msg_out >= context->core.keepalive){
  if(context->core.state == mosq_cs_connected){
   mqtt3_raw_pingreq(context);
  }else{
   mqtt3_socket_close(context);
  }
 }
}

canonical_original_code:
void mqtt3_check_keepalive(struct _mqtt3_context *context)
{
 if(context && context->core.sock != -1 && time(((void *)0)) - context->core.last_msg_out >= context->core.keepalive){
  if(context->core.state == mosq_cs_connected){
   mqtt3_raw_pingreq(context);
  }else{
   mqtt3_socket_close(context);
  }
 }
}


loop

decompiled_code:
__int64 __fastcall mosquittopp::loop(mosquittopp *this)
{
return mosquitto_loop();
}

canonical_decompiled_code:
__int64 __fastcall mosquittopp::func20(mosquittopp *this)
{
return mosquitto_loop();
}

original_code:
int loop(mqtt3_config *config, int *listensock, int listensock_count, int listener_max)
{
 time_t start_time = time(((void *)0));
 time_t last_backup = time(((void *)0));
 time_t last_store_clean = time(((void *)0));
 time_t now;
 int fdcount;
 sigset_t sigblock, origsig;
 int i;
 struct pollfd *pollfds = ((void *)0);
 unsigned int pollfd_count = 0;
 int new_clients = 1;
 int client_max = 0;
 unsigned int sock_max = 0;
 sigemptyset(&sigblock);
 sigaddset(&sigblock, 2);
 while(run){
  mqtt3_db_sys_update(&int_db, config->sys_interval, start_time);
  if(new_clients){
   client_max = -1;
   for(i=0; i<int_db.context_count; i++){
    if(int_db.contexts[i] && int_db.contexts[i]->core.sock != -1 && int_db.contexts[i]->core.sock > sock_max){
     client_max = int_db.contexts[i]->core.sock;
    }
   }
   new_clients = 0;
  }
  if(client_max > listener_max){
   sock_max = client_max;
  }else{
   sock_max = listener_max;
  }
  if(sock_max+1 > pollfd_count){
   pollfd_count = sock_max+1;
   pollfds = _mosquitto_realloc(pollfds, sizeof(struct pollfd)*pollfd_count);
   if(!pollfds){
    mqtt3_log_printf(0x08, "Error: Out of memory.");
    return 1;
   }
  }
  memset(pollfds, -1, sizeof(struct pollfd)*pollfd_count);
  for(i=0; i<listensock_count; i++){
   pollfds[listensock[i]].fd = listensock[i];
   pollfds[listensock[i]].events = 0x001;
   pollfds[listensock[i]].revents = 0;
  }
  now = time(((void *)0));
  for(i=0; i<int_db.context_count; i++){
   if(int_db.contexts[i]){
    if(int_db.contexts[i]->core.sock != -1){
     if(int_db.contexts[i]->bridge){
      mqtt3_check_keepalive(int_db.contexts[i]);
     }
     if(!(int_db.contexts[i]->core.keepalive) || now - int_db.contexts[i]->core.last_msg_in < (time_t)(int_db.contexts[i]->core.keepalive)*3/2){
      if(mqtt3_db_message_write(int_db.contexts[i])){
      }
      pollfds[int_db.contexts[i]->core.sock].fd = int_db.contexts[i]->core.sock;
      pollfds[int_db.contexts[i]->core.sock].events = 0x001;
      pollfds[int_db.contexts[i]->core.sock].revents = 0;
      if(int_db.contexts[i]->core.out_packet){
       pollfds[int_db.contexts[i]->core.sock].events |= 0x004;
      }
     }else{
      mqtt3_log_printf(0x02, "Client %s has exceeded timeout, disconnecting.", int_db.contexts[i]->core.id);
      if(int_db.contexts[i]->bridge || int_db.contexts[i]->clean_session == 0){
       mqtt3_socket_close(int_db.contexts[i]);
      }else{
       mqtt3_context_cleanup(&int_db, int_db.contexts[i], 1);
       int_db.contexts[i] = ((void *)0);
      }
     }
    }else{
     if(int_db.contexts[i]->bridge){
      if(!int_db.contexts[i]->bridge->restart_t){
       int_db.contexts[i]->bridge->restart_t = time(((void *)0))+30;
      }else{
       if(time(((void *)0)) > int_db.contexts[i]->bridge->restart_t){
        int_db.contexts[i]->bridge->restart_t = 0;
        mqtt3_bridge_connect(&int_db, int_db.contexts[i]);
       }
      }
     }else{
      if(int_db.contexts[i]->clean_session == 1){
       mqtt3_context_cleanup(&int_db, int_db.contexts[i], 1);
       int_db.contexts[i] = ((void *)0);
      }
     }
    }
   }
  }
  mqtt3_db_message_timeout_check(&int_db, config->retry_interval);
  sigprocmask(2, &sigblock, &origsig);
  fdcount = poll(pollfds, pollfd_count, 1000);
  sigprocmask(2, &origsig, ((void *)0));
  if(fdcount == -1){
   loop_handle_errors(pollfds);
  }else{
   loop_handle_reads_writes(pollfds);
   for(i=0; i<listensock_count; i++){
    if(pollfds[listensock[i]].revents & (0x001 | 0x002)){
     new_clients = 1;
     while(mqtt3_socket_accept(&int_db.contexts, &int_db.context_count, listensock[i]) != -1){
     }
    }
   }
  }
  if(config->persistence && config->autosave_interval){
   if(last_backup + config->autosave_interval < now){
    mqtt3_db_backup(&int_db, 0, 0);
    last_backup = time(((void *)0));
   }
  }
  if(!config->store_clean_interval || last_store_clean + config->store_clean_interval < now){
   mqtt3_db_store_clean(&int_db);
   last_store_clean = time(((void *)0));
  }
 }
 if(pollfds) _mosquitto_free(pollfds);
 return 0;
}

canonical_original_code:
int loop(struct mqtt3_config *config, int *listensock, int listensock_count, int listener_max)
{
 long start_time = time(((void *)0));
 long last_backup = time(((void *)0));
 long last_store_clean = time(((void *)0));
 long now;
 int fdcount;
 struct __sigset_t sigblock; struct __sigset_t origsig;
 int i;
 struct pollfd *pollfds = ((void *)0);
 unsigned int pollfd_count = 0;
 int new_clients = 1;
 int client_max = 0;
 unsigned int sock_max = 0;
 sigemptyset(&sigblock);
 sigaddset(&sigblock, 2);
 while(run){
  mqtt3_db_sys_update(&int_db, config->sys_interval, start_time);
  if(new_clients){
   client_max = -1;
   for(i=0; i<int_db.context_count; i++){
    if(int_db.contexts[i] && int_db.contexts[i]->core.sock != -1 && int_db.contexts[i]->core.sock > sock_max){
     client_max = int_db.contexts[i]->core.sock;
    }
   }
   new_clients = 0;
  }
  if(client_max > listener_max){
   sock_max = client_max;
  }else{
   sock_max = listener_max;
  }
  if(sock_max+1 > pollfd_count){
   pollfd_count = sock_max+1;
   pollfds = _mosquitto_realloc(pollfds, sizeof(struct pollfd)*pollfd_count);
   if(!pollfds){
    mqtt3_log_printf(0x08, "Error: Out of memory.");
    return 1;
   }
  }
  memset(pollfds, -1, sizeof(struct pollfd)*pollfd_count);
  for(i=0; i<listensock_count; i++){
   pollfds[listensock[i]].fd = listensock[i];
   pollfds[listensock[i]].events = 0x001;
   pollfds[listensock[i]].revents = 0;
  }
  now = time(((void *)0));
  for(i=0; i<int_db.context_count; i++){
   if(int_db.contexts[i]){
    if(int_db.contexts[i]->core.sock != -1){
     if(int_db.contexts[i]->bridge){
      mqtt3_check_keepalive(int_db.contexts[i]);
     }
     if(!(int_db.contexts[i]->core.keepalive) || now - int_db.contexts[i]->core.last_msg_in < (time_t)(int_db.contexts[i]->core.keepalive)*3/2){
      if(mqtt3_db_message_write(int_db.contexts[i])){
      }
      pollfds[int_db.contexts[i]->core.sock].fd = int_db.contexts[i]->core.sock;
      pollfds[int_db.contexts[i]->core.sock].events = 0x001;
      pollfds[int_db.contexts[i]->core.sock].revents = 0;
      if(int_db.contexts[i]->core.out_packet){
       pollfds[int_db.contexts[i]->core.sock].events |= 0x004;
      }
     }else{
      mqtt3_log_printf(0x02, "Client %s has exceeded timeout, disconnecting.", int_db.contexts[i]->core.id);
      if(int_db.contexts[i]->bridge || int_db.contexts[i]->clean_session == 0){
       mqtt3_socket_close(int_db.contexts[i]);
      }else{
       mqtt3_context_cleanup(&int_db, int_db.contexts[i], 1);
       int_db.contexts[i] = ((void *)0);
      }
     }
    }else{
     if(int_db.contexts[i]->bridge){
      if(!int_db.contexts[i]->bridge->restart_t){
       int_db.contexts[i]->bridge->restart_t = time(((void *)0))+30;
      }else{
       if(time(((void *)0)) > int_db.contexts[i]->bridge->restart_t){
        int_db.contexts[i]->bridge->restart_t = 0;
        mqtt3_bridge_connect(&int_db, int_db.contexts[i]);
       }
      }
     }else{
      if(int_db.contexts[i]->clean_session == 1){
       mqtt3_context_cleanup(&int_db, int_db.contexts[i], 1);
       int_db.contexts[i] = ((void *)0);
      }
     }
    }
   }
  }
  mqtt3_db_message_timeout_check(&int_db, config->retry_interval);
  sigprocmask(2, &sigblock, &origsig);
  fdcount = poll(pollfds, pollfd_count, 1000);
  sigprocmask(2, &origsig, ((void *)0));
  if(fdcount == -1){
   loop_handle_errors(pollfds);
  }else{
   loop_handle_reads_writes(pollfds);
   for(i=0; i<listensock_count; i++){
    if(pollfds[listensock[i]].revents & (0x001 | 0x002)){
     new_clients = 1;
     while(mqtt3_socket_accept(&int_db.contexts, &int_db.context_count, listensock[i]) != -1){
     }
    }
   }
  }
  if(config->persistence && config->autosave_interval){
   if(last_backup + config->autosave_interval < now){
    mqtt3_db_backup(&int_db, 0, 0);
    last_backup = time(((void *)0));
   }
  }
  if(!config->store_clean_interval || last_store_clean + config->store_clean_interval < now){
   mqtt3_db_store_clean(&int_db);
   last_store_clean = time(((void *)0));
  }
 }
 if(pollfds) _mosquitto_free(pollfds);
 return 0;
}

variable_types: struct mqtt3_config { int autosave_interval; _Bool daemon; struct _mqtt3_listener { int fd; char *host; unsigned short port; int max_connections; char *mount_point; } default_listener; struct _mqtt3_listener *listeners; int listener_count; int log_dest; int log_type; int max_connections; _Bool persistence; char *persistence_location; char *persistence_file; int retry_interval; int store_clean_interval; int sys_interval; char *pid_file; char *user; 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; } *bridges; int bridge_count; } *

variable_types:
listensock
int *

variable_types: now long

variable_types: fdcount int

variable_types: struct __sigset_t { unsigned long __val[-1]; }

variable_types: struct pollfd { int fd; short events; short revents; } *

user_defined_types: struct mqtt3_config { int autosave_interval; _Bool daemon; struct _mqtt3_listener default_listener; struct _mqtt3_listener *listeners; int listener_count; int log_dest; int log_type; int max_connections; _Bool persistence; char *persistence_location; char *persistence_file; int retry_interval; int store_clean_interval; int sys_interval; char *pid_file; char *user; struct _mqtt3_bridge *bridges; int bridge_count; }


repo: adanaila/ctrlinux

decompiled_code:
void timer_on_handler()
{
char buf; // [rsp+Fh] [rbp-69h] BYREF
char s[80]; // [rsp+10h] [rbp-68h] BYREF
struct timespec tp; // [rsp+60h] [rbp-18h] BYREF

buf = 0;
if ( pthread_mutex_lock(&mtx) )
perror("pthread_mutex_lock");
if ( clock_gettime(1, &tp) == -1 )
perror("clock_gettime");
buf = gpio_val + 48;
sprintf(s, "TIMER_ON %ld s %ld ns \n", tp.tv_sec, tp.tv_nsec);
if ( (unsigned int)write(fd_gpio, &buf, 1uLL) == -1 )
perror("write");
if ( gpio_val )
{
new_time.it_interval.tv_nsec = time_on;
new_time.it_value.tv_nsec = time_on;
}
else
{
new_time.it_interval.tv_nsec = time_off;
new_time.it_value.tv_nsec = time_off;
}
if ( timer_settime(on_timer, 0, &new_time, &old_time) == -1 )
perror("timer_setttime");
gpio_val ^= 1u;
if ( pthread_mutex_unlock(&mtx) )
perror("pthread_mutex_unlock");
}

canonical_decompiled_code:
void func7()
{
char buf; // [rsp+Fh] [rbp-69h] BYREF
char s[80]; // [rsp+10h] [rbp-68h] BYREF
struct timespec tp; // [rsp+60h] [rbp-18h] BYREF

buf = 0;
if ( pthread_mutex_lock(&mtx) )
perror("pthread_mutex_lock");
if ( clock_gettime(1, &tp) == -1 )
perror("clock_gettime");
buf = gpio_val + 48;
sprintf(s, "TIMER_ON %ld s %ld ns \n", tp.tv_sec, tp.tv_nsec);
if ( (unsigned int)write(fd_gpio, &buf, 1uLL) == -1 )
perror("write");
if ( gpio_val )
{
new_time.it_interval.tv_nsec = time_on;
new_time.it_value.tv_nsec = time_on;
}
else
{
new_time.it_interval.tv_nsec = time_off;
new_time.it_value.tv_nsec = time_off;
}
if ( timer_settime(on_timer, 0, &new_time, &old_time) == -1 )
perror("timer_setttime");
gpio_val ^= 1u;
if ( pthread_mutex_unlock(&mtx) )
perror("pthread_mutex_unlock");
}

original_code:
void timer_on_handler (int signum)
{
    struct timespec tp;
    char buffer [80];
    char gpio_out_val = 0;
    int ret_val = 0;
    ret_val = pthread_mutex_lock(&mtx);
    if (0 != ret_val)
        perror("pthread_mutex_lock");
    if (clock_gettime (1, &tp) == -1)
        perror ("clock_gettime");
    gpio_out_val = gpio_val + '0';
    sprintf (buffer, "TIMER_ON  %ld s %ld ns \n", tp.tv_sec, tp.tv_nsec );
    if(0)printf("%s", buffer);
    ret_val = write(fd_gpio,&gpio_out_val,1);
    if (ret_val == -1)
        perror("write");
    if (gpio_val)
    {
        if(0)printf("ON Cycle setting off interval to %lu\n",time_off);
        new_time.it_interval.tv_nsec = time_on;
        new_time.it_value.tv_nsec = time_on;
    }
    else
    {
        if(0)printf("OFF Cycle setting on interval to %lu\n",time_on);
        new_time.it_interval.tv_nsec = time_off;
        new_time.it_value.tv_nsec = time_off;
    }
    if (-1 == timer_settime(on_timer, 0, &new_time,&old_time))
        perror("timer_setttime");
    gpio_val ^= 1;
    ret_val = pthread_mutex_unlock(&mtx);
    if (0 != ret_val)
        perror("pthread_mutex_unlock");
}

canonical_original_code:
void timer_on_handler(int signum)
{
    struct timespec tp;
    char buffer[80];
    char gpio_out_val = 0;
    int ret_val = 0;
    ret_val = pthread_mutex_lock(&mtx);
    if (0 != ret_val)
        perror("pthread_mutex_lock");
    if (clock_gettime (1, &tp) == -1)
        perror ("clock_gettime");
    gpio_out_val = gpio_val + '0';
    sprintf (buffer, "TIMER_ON  %ld s %ld ns \n", tp.tv_sec, tp.tv_nsec );
    if(0)printf("%s", buffer);
    ret_val = write(fd_gpio,&gpio_out_val,1);
    if (ret_val == -1)
        perror("write");
    if (gpio_val)
    {
        if(0)printf("ON Cycle setting off interval to %lu\n",time_off);
        new_time.it_interval.tv_nsec = time_on;
        new_time.it_value.tv_nsec = time_on;
    }
    else
    {
        if(0)printf("OFF Cycle setting on interval to %lu\n",time_on);
        new_time.it_interval.tv_nsec = time_off;
        new_time.it_value.tv_nsec = time_off;
    }
    if (-1 == timer_settime(on_timer, 0, &new_time,&old_time))
        perror("timer_setttime");
    gpio_val ^= 1;
    ret_val = pthread_mutex_unlock(&mtx);
    if (0 != ret_val)
        perror("pthread_mutex_unlock");
}

variable_types: signum int

variable_types: struct timespec { long tv_sec; long tv_nsec; }

variable_types:
buffer
char [80]