decompiled_code:
void __fastcall zw_node_wakeup_handler(__int64 a1, unsigned __int8 a2)
{
void **v2; // rbx
int v3; // [rsp+8h] [rbp-20h] BYREF
int v4[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v3 = -1;
v2 = (void **)zw_nodes;
v4[0] = -1;
if ( zw_nodes != &zw_nodes )
{
while ( *((_DWORD *)v2 + 12) != a2 )
{
v2 = (void **)*v2;
if ( v2 == &zw_nodes )
return;
}
if ( (unsigned int)cc_get(a1, a2, 128LL, &v3) )
syslog(2, "Get Battery state command failed for node %d", a2);
if ( (unsigned int)cc_get(a1, a2, *((unsigned __int8 *)v2 + 57), v4) )
syslog(2, "Get state command failed for node %d", a2);
}
}

canonical_decompiled_code:
void __fastcall func6(__int64 a1, unsigned __int8 a2)
{
void **v2; // rbx
int v3; // [rsp+8h] [rbp-20h] BYREF
int v4[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v3 = -1;
v2 = (void **)zw_nodes;
v4[0] = -1;
if ( zw_nodes != &zw_nodes )
{
while ( *((_DWORD *)v2 + 12) != a2 )
{
v2 = (void **)*v2;
if ( v2 == &zw_nodes )
return;
}
if ( (unsigned int)cc_get(a1, a2, 128LL, &v3) )
syslog(2, "Get Battery state command failed for node %d", a2);
if ( (unsigned int)cc_get(a1, a2, *((unsigned __int8 *)v2 + 57), v4) )
syslog(2, "Get state command failed for node %d", a2);
}
}

original_code:
void
zw_node_wakeup_handler( zw_api_ctx_S *ctx, u8 id )
{
 list_node *node = ((void *)0);
 struct zw_node *zwnode;
 int batt = -1;
 int state = -1;
 for(node = (&zw_nodes)->next; node != (&zw_nodes); node = node->next) {
  zwnode = (struct zw_node *)node;
  if ( zwnode->id == id ) {
   if ( 0 != cc_get( ctx, id, 0x80, (void *)&batt ) )
    syslog( 2, "Get Battery state command failed for node %d", id );;
   if ( 0 != cc_get( ctx, id, zwnode->cclass, (void *)&state ) )
    syslog( 2, "Get state command failed for node %d", id );;
   break;
  }
 }
}

canonical_original_code:
void zw_node_wakeup_handler(struct zw_api_ctx *ctx, unsigned char id)
{
 struct _node *node = ((void *)0);
 struct zw_node *zwnode;
 int batt = -1;
 int state = -1;
 for(node = (&zw_nodes)->next; node != (&zw_nodes); node = node->next) {
  zwnode = (struct zw_node *)node;
  if ( zwnode->id == id ) {
   if ( 0 != cc_get( ctx, id, 0x80, (void *)&batt ) )
    syslog( 2, "Get Battery state command failed for node %d", id );;
   if ( 0 != cc_get( ctx, id, zwnode->cclass, (void *)&state ) )
    syslog( 2, "Get state command failed for node %d", id );;
   break;
  }
 }
}

variable_types: struct zw_api_ctx { int port; int node_id; } *

variable_types: batt int

variable_types: state int


repo: pmnair/zwave-remote

decompiled_code:
__int64 __fastcall zw_node_get_version(__int64 a1, __int64 a2, __int64 a3)
{
void **v3; // rax

v3 = (void **)zw_nodes;
if ( zw_nodes == &zw_nodes )
return 0xFFFFFFFFLL;
a2 = (unsigned __int8)a2;
while ( *((_DWORD *)v3 + 12) != (_DWORD)a2 )
{
v3 = (void **)*v3;
if ( v3 == &zw_nodes )
return 0xFFFFFFFFLL;
}
return cc_version(a1, a2, *((unsigned __int8 *)v3 + 57), a3);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2, __int64 a3)
{
void **v3; // rax

v3 = (void **)zw_nodes;
if ( zw_nodes == &zw_nodes )
return 0xFFFFFFFFLL;
a2 = (unsigned __int8)a2;
while ( *((_DWORD *)v3 + 12) != (_DWORD)a2 )
{
v3 = (void **)*v3;
if ( v3 == &zw_nodes )
return 0xFFFFFFFFLL;
}
return cc_version(a1, a2, *((unsigned __int8 *)v3 + 57), a3);
}

original_code:
int
zw_node_get_version( zw_api_ctx_S *ctx, u8 id, void *resp )
{
 list_node *node = ((void *)0);
 struct zw_node *zwnode;
 int rc = -1;
 for(node = (&zw_nodes)->next; node != (&zw_nodes); node = node->next) {
  zwnode = (struct zw_node *)node;
  if ( zwnode->id == id ) {
   rc = cc_version( ctx, id, zwnode->cclass, resp );
   break;
  }
 }
 return rc;
}

canonical_original_code:
int zw_node_get_version(struct zw_api_ctx *ctx, unsigned char id, void *resp)
{
 struct _node *node = ((void *)0);
 struct zw_node *zwnode;
 int rc = -1;
 for(node = (&zw_nodes)->next; node != (&zw_nodes); node = node->next) {
  zwnode = (struct zw_node *)node;
  if ( zwnode->id == id ) {
   rc = cc_version( ctx, id, zwnode->cclass, resp );
   break;
  }
 }
 return rc;
}

variable_types:
id
unsigned char

variable_types:
resp
void *

variable_types: struct _node { struct _node *next; struct _node *prev; } *

variable_types: struct zw_node { struct _node { struct _node *next; struct _node *prev; } list; char name[32]; int id; unsigned char mode; unsigned char func; unsigned char btype; unsigned char gtype; unsigned char stype; unsigned char cclass; unsigned char batt_level; unsigned char state; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } lock; } *

variable_types: rc int

return_type: int

user_defined_types: struct zw_api_ctx { int port; int node_id; }

user_defined_types: struct zw_node { struct _node list; char name[32]; int id; unsigned char mode; unsigned char func; unsigned char btype; unsigned char gtype; unsigned char stype; unsigned char cclass; unsigned char batt_level; unsigned char state; union pthread_mutex_t lock; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


decompiled_code:
__int64 __fastcall zw_node_get_value(__int64 a1, __int64 a2, __int64 a3)
{
void **v3; // rax

v3 = (void **)zw_nodes;
if ( zw_nodes == &zw_nodes )
return 0xFFFFFFFFLL;
a2 = (unsigned __int8)a2;
while ( *((_DWORD *)v3 + 12) != (_DWORD)a2 )
{
v3 = (void **)*v3;
if ( v3 == &zw_nodes )
return 0xFFFFFFFFLL;
}
return cc_get(a1, a2, *((unsigned __int8 *)v3 + 57), a3);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, __int64 a2, __int64 a3)
{
void **v3; // rax

v3 = (void **)zw_nodes;
if ( zw_nodes == &zw_nodes )
return 0xFFFFFFFFLL;
a2 = (unsigned __int8)a2;
while ( *((_DWORD *)v3 + 12) != (_DWORD)a2 )
{
v3 = (void **)*v3;
if ( v3 == &zw_nodes )
return 0xFFFFFFFFLL;
}
return cc_get(a1, a2, *((unsigned __int8 *)v3 + 57), a3);
}

original_code:
int
zw_node_get_value( zw_api_ctx_S *ctx, u8 id, void *resp )
{
 list_node *node = ((void *)0);
 struct zw_node *zwnode;
 int rc = -1;
 for(node = (&zw_nodes)->next; node != (&zw_nodes); node = node->next) {
  zwnode = (struct zw_node *)node;
  if ( zwnode->id == id ) {
   rc = cc_get( ctx, id, zwnode->cclass, resp );
   break;
  }
 }
 return rc;
}

canonical_original_code:
int zw_node_get_value(struct zw_api_ctx *ctx, unsigned char id, void *resp)
{
 struct _node *node = ((void *)0);
 struct zw_node *zwnode;
 int rc = -1;
 for(node = (&zw_nodes)->next; node != (&zw_nodes); node = node->next) {
  zwnode = (struct zw_node *)node;
  if ( zwnode->id == id ) {
   rc = cc_get( ctx, id, zwnode->cclass, resp );
   break;
  }
 }
 return rc;
}

user_defined_types: struct _node { struct _node *next; struct _node *prev; }

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }


decompiled_code:
__int64 __fastcall zw_node_set_value(__int64 a1, __int64 a2, __int64 a3)
{
void **v3; // rax

v3 = (void **)zw_nodes;
if ( zw_nodes == &zw_nodes )
return 0xFFFFFFFFLL;
a2 = (unsigned __int8)a2;
while ( *((_DWORD *)v3 + 12) != (_DWORD)a2 )
{
v3 = (void **)*v3;
if ( v3 == &zw_nodes )
return 0xFFFFFFFFLL;
}
return cc_set(a1, a2, *((unsigned __int8 *)v3 + 57), a3);
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, __int64 a2, __int64 a3)
{
void **v3; // rax

v3 = (void **)zw_nodes;
if ( zw_nodes == &zw_nodes )
return 0xFFFFFFFFLL;
a2 = (unsigned __int8)a2;
while ( *((_DWORD *)v3 + 12) != (_DWORD)a2 )
{
v3 = (void **)*v3;
if ( v3 == &zw_nodes )
return 0xFFFFFFFFLL;
}
return cc_set(a1, a2, *((unsigned __int8 *)v3 + 57), a3);
}

original_code:
int
zw_node_set_value( zw_api_ctx_S *ctx, u8 id, void *value )
{
 list_node *node = ((void *)0);
 struct zw_node *zwnode;
 int rc = -1;
 for(node = (&zw_nodes)->next; node != (&zw_nodes); node = node->next) {
  zwnode = (struct zw_node *)node;
  if ( zwnode->id == id ) {
   rc = cc_set( ctx, id, zwnode->cclass, value );
   break;
  }
 }
 return rc;
}

canonical_original_code:
int zw_node_set_value(struct zw_api_ctx *ctx, unsigned char id, void *value)
{
 struct _node *node = ((void *)0);
 struct zw_node *zwnode;
 int rc = -1;
 for(node = (&zw_nodes)->next; node != (&zw_nodes); node = node->next) {
  zwnode = (struct zw_node *)node;
  if ( zwnode->id == id ) {
   rc = cc_set( ctx, id, zwnode->cclass, value );
   break;
  }
 }
 return rc;
}

variable_types:
value
void *


decompiled_code:
__int64 __fastcall zw_node_get_report(__int64 a1, __int64 a2, __int64 a3)
{
void **v3; // rax

v3 = (void **)zw_nodes;
if ( zw_nodes == &zw_nodes )
return 0xFFFFFFFFLL;
a2 = (unsigned __int8)a2;
while ( *((_DWORD *)v3 + 12) != (_DWORD)a2 )
{
v3 = (void **)*v3;
if ( v3 == &zw_nodes )
return 0xFFFFFFFFLL;
}
return cc_report(a1, a2, *((unsigned __int8 *)v3 + 57), a3);
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, __int64 a2, __int64 a3)
{
void **v3; // rax

v3 = (void **)zw_nodes;
if ( zw_nodes == &zw_nodes )
return 0xFFFFFFFFLL;
a2 = (unsigned __int8)a2;
while ( *((_DWORD *)v3 + 12) != (_DWORD)a2 )
{
v3 = (void **)*v3;
if ( v3 == &zw_nodes )
return 0xFFFFFFFFLL;
}
return cc_report(a1, a2, *((unsigned __int8 *)v3 + 57), a3);
}

original_code:
int
zw_node_get_report( zw_api_ctx_S *ctx, u8 id, void *resp )
{
 list_node *node = ((void *)0);
 struct zw_node *zwnode;
 int rc = -1;
 for(node = (&zw_nodes)->next; node != (&zw_nodes); node = node->next) {
  zwnode = (struct zw_node *)node;
  if ( zwnode->id == id ) {
   rc = cc_report( ctx, id, zwnode->cclass, resp );
   break;
  }
 }
 return rc;
}

canonical_original_code:
int zw_node_get_report(struct zw_api_ctx *ctx, unsigned char id, void *resp)
{
 struct _node *node = ((void *)0);
 struct zw_node *zwnode;
 int rc = -1;
 for(node = (&zw_nodes)->next; node != (&zw_nodes); node = node->next) {
  zwnode = (struct zw_node *)node;
  if ( zwnode->id == id ) {
   rc = cc_report( ctx, id, zwnode->cclass, resp );
   break;
  }
 }
 return rc;
}


decompiled_code:
__int64 __fastcall zw_node_get_battery_status(__int64 a1, __int64 a2, __int64 a3)
{
void **v3; // rax

v3 = (void **)zw_nodes;
if ( zw_nodes == &zw_nodes )
return 0xFFFFFFFFLL;
a2 = (unsigned __int8)a2;
while ( *((_DWORD *)v3 + 12) != (_DWORD)a2 )
{
v3 = (void **)*v3;
if ( v3 == &zw_nodes )
return 0xFFFFFFFFLL;
}
return cc_get(a1, a2, 128LL, a3);
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, __int64 a2, __int64 a3)
{
void **v3; // rax

v3 = (void **)zw_nodes;
if ( zw_nodes == &zw_nodes )
return 0xFFFFFFFFLL;
a2 = (unsigned __int8)a2;
while ( *((_DWORD *)v3 + 12) != (_DWORD)a2 )
{
v3 = (void **)*v3;
if ( v3 == &zw_nodes )
return 0xFFFFFFFFLL;
}
return cc_get(a1, a2, 128LL, a3);
}

original_code:
int
zw_node_get_battery_status( zw_api_ctx_S *ctx, u8 id, void *resp )
{
 list_node *node = ((void *)0);
 struct zw_node *zwnode;
 int rc = -1;
 for(node = (&zw_nodes)->next; node != (&zw_nodes); node = node->next) {
  zwnode = (struct zw_node *)node;
  if ( zwnode->id == id ) {
   rc = cc_get( ctx, id, 0x80, resp );
   break;
  }
 }
 return rc;
}

canonical_original_code:
int zw_node_get_battery_status(struct zw_api_ctx *ctx, unsigned char id, void *resp)
{
 struct _node *node = ((void *)0);
 struct zw_node *zwnode;
 int rc = -1;
 for(node = (&zw_nodes)->next; node != (&zw_nodes); node = node->next) {
  zwnode = (struct zw_node *)node;
  if ( zwnode->id == id ) {
   rc = cc_get( ctx, id, 0x80, resp );
   break;
  }
 }
 return rc;
}


decompiled_code:
__int64 __fastcall zw_node_set_wakeup_interval(__int64 a1, __int64 a2, int a3)
{
void **v3; // rax
int v5[3]; // [rsp+Ch] [rbp-Ch] BYREF

v5[0] = a3;
v3 = (void **)zw_nodes;
if ( zw_nodes == &zw_nodes )
return 0xFFFFFFFFLL;
a2 = (unsigned __int8)a2;
while ( *((_DWORD *)v3 + 12) != (_DWORD)a2 )
{
v3 = (void **)*v3;
if ( v3 == &zw_nodes )
return 0xFFFFFFFFLL;
}
return cc_set(a1, a2, 132LL, v5);
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, __int64 a2, int a3)
{
void **v3; // rax
int v5[3]; // [rsp+Ch] [rbp-Ch] BYREF

v5[0] = a3;
v3 = (void **)zw_nodes;
if ( zw_nodes == &zw_nodes )
return 0xFFFFFFFFLL;
a2 = (unsigned __int8)a2;
while ( *((_DWORD *)v3 + 12) != (_DWORD)a2 )
{
v3 = (void **)*v3;
if ( v3 == &zw_nodes )
return 0xFFFFFFFFLL;
}
return cc_set(a1, a2, 132LL, v5);
}

original_code:
int
zw_node_set_wakeup_interval( zw_api_ctx_S *ctx, u8 id, int intvl )
{
 list_node *node = ((void *)0);
 struct zw_node *zwnode;
 int rc = -1;
 int val = intvl;
 for(node = (&zw_nodes)->next; node != (&zw_nodes); node = node->next) {
  zwnode = (struct zw_node *)node;
  if ( zwnode->id == id ) {
   rc = cc_set( ctx, id, 0x84, (void *)&val );
   break;
  }
 }
 return rc;
}

canonical_original_code:
int zw_node_set_wakeup_interval(struct zw_api_ctx *ctx, unsigned char id, int intvl)
{
 struct _node *node = ((void *)0);
 struct zw_node *zwnode;
 int rc = -1;
 int val = intvl;
 for(node = (&zw_nodes)->next; node != (&zw_nodes); node = node->next) {
  zwnode = (struct zw_node *)node;
  if ( zwnode->id == id ) {
   rc = cc_set( ctx, id, 0x84, (void *)&val );
   break;
  }
 }
 return rc;
}

variable_types: intvl int

variable_types: val int


decompiled_code:
__int64 __fastcall zw_node_get_wakeup_interval(__int64 a1, __int64 a2, __int64 a3)
{
void **v3; // rax

v3 = (void **)zw_nodes;
if ( zw_nodes == &zw_nodes )
return 0xFFFFFFFFLL;
a2 = (unsigned __int8)a2;
while ( *((_DWORD *)v3 + 12) != (_DWORD)a2 )
{
v3 = (void **)*v3;
if ( v3 == &zw_nodes )
return 0xFFFFFFFFLL;
}
return cc_get(a1, a2, 132LL, a3);
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1, __int64 a2, __int64 a3)
{
void **v3; // rax

v3 = (void **)zw_nodes;
if ( zw_nodes == &zw_nodes )
return 0xFFFFFFFFLL;
a2 = (unsigned __int8)a2;
while ( *((_DWORD *)v3 + 12) != (_DWORD)a2 )
{
v3 = (void **)*v3;
if ( v3 == &zw_nodes )
return 0xFFFFFFFFLL;
}
return cc_get(a1, a2, 132LL, a3);
}

original_code:
int
zw_node_get_wakeup_interval( zw_api_ctx_S *ctx, u8 id, void *resp )
{
 list_node *node = ((void *)0);
 struct zw_node *zwnode;
 int rc = -1;
 for(node = (&zw_nodes)->next; node != (&zw_nodes); node = node->next) {
  zwnode = (struct zw_node *)node;
  if ( zwnode->id == id ) {
   rc = cc_get( ctx, id, 0x84, resp );
   break;
  }
 }
 return rc;
}

canonical_original_code:
int zw_node_get_wakeup_interval(struct zw_api_ctx *ctx, unsigned char id, void *resp)
{
 struct _node *node = ((void *)0);
 struct zw_node *zwnode;
 int rc = -1;
 for(node = (&zw_nodes)->next; node != (&zw_nodes); node = node->next) {
  zwnode = (struct zw_node *)node;
  if ( zwnode->id == id ) {
   rc = cc_get( ctx, id, 0x84, resp );
   break;
  }
 }
 return rc;
}


decompiled_code:
void zw_list_nodes()
{
const char *v0; // rbx

v0 = (const char *)zw_nodes;
if ( zw_nodes != &zw_nodes )
{
do
{
syslog(5, "ZW_NODE: %s - %d\n", v0 + 16, *((unsigned int *)v0 + 12));
v0 = *(const char **)v0;
}
while ( v0 != (const char *)&zw_nodes );
}
}

canonical_decompiled_code:
void func14()
{
const char *v0; // rbx

v0 = (const char *)zw_nodes;
if ( zw_nodes != &zw_nodes )
{
do
{
syslog(5, "ZW_NODE: %s - %d\n", v0 + 16, *((unsigned int *)v0 + 12));
v0 = *(const char **)v0;
}
while ( v0 != (const char *)&zw_nodes );
}
}

original_code:
void
zw_list_nodes()
{
 list_node *node = ((void *)0);
 struct zw_node *zwnode;
 for(node = (&zw_nodes)->next; node != (&zw_nodes); node = node->next) {
  zwnode = (struct zw_node *)node;
  syslog( 5, "ZW_NODE: %s - %d\n", zwnode->name, zwnode->id );;
 }
}

canonical_original_code:
void zw_list_nodes()
{
 struct _node *node = ((void *)0);
 struct zw_node *zwnode;
 for(node = (&zw_nodes)->next; node != (&zw_nodes); node = node->next) {
  zwnode = (struct zw_node *)node;
  syslog( 5, "ZW_NODE: %s - %d\n", zwnode->name, zwnode->id );;
 }
}

return_type: void


decompiled_code:
__int64 __fastcall register_zw_node(__int64 a1, _BYTE *a2, __int64 a3)
{
unsigned int v4; // er12
void **v5; // rbx
char v6; // di
unsigned __int8 v7; // al
__int64 v8; // rcx

v4 = a3;
syslog(5, "register_zw_node: %d\n", a3);
fflush(stdout);
v5 = (void **)zw_nodes;
if ( zw_nodes == &zw_nodes )
return 0xFFFFFFFFLL;
while ( *((_DWORD *)v5 + 12) != v4 )
{
v5 = (void **)*v5;
if ( v5 == &zw_nodes )
return 0xFFFFFFFFLL;
}
*((_BYTE *)v5 + 52) = a2[2];
*((_BYTE *)v5 + 53) = a2[3];
*((_BYTE *)v5 + 54) = a2[5];
v6 = a2[6];
*((_BYTE *)v5 + 55) = v6;
*((_BYTE *)v5 + 56) = a2[7];
v7 = get_cmd_class(v6);
*((_BYTE *)v5 + 57) = v7;
cc_get(a1, (unsigned __int8)v4, v7, v8);
syslog(
5,
"register_zw_node: %d func=%d, bt=%d, gt=%d, st=%d\n",
v4,
*((unsigned __int8 *)v5 + 53),
*((unsigned __int8 *)v5 + 54),
*((unsigned __int8 *)v5 + 55),
*((unsigned __int8 *)v5 + 56));
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func16(__int64 a1, _BYTE *a2, __int64 a3)
{
unsigned int v4; // er12
void **v5; // rbx
char v6; // di
unsigned __int8 v7; // al
__int64 v8; // rcx

v4 = a3;
syslog(5, "register_zw_node: %d\n", a3);
fflush(stdout);
v5 = (void **)zw_nodes;
if ( zw_nodes == &zw_nodes )
return 0xFFFFFFFFLL;
while ( *((_DWORD *)v5 + 12) != v4 )
{
v5 = (void **)*v5;
if ( v5 == &zw_nodes )
return 0xFFFFFFFFLL;
}
*((_BYTE *)v5 + 52) = a2[2];
*((_BYTE *)v5 + 53) = a2[3];
*((_BYTE *)v5 + 54) = a2[5];
v6 = a2[6];
*((_BYTE *)v5 + 55) = v6;
*((_BYTE *)v5 + 56) = a2[7];
v7 = func0(v6);
*((_BYTE *)v5 + 57) = v7;
cc_get(a1, (unsigned __int8)v4, v7, v8);
syslog(
5,
"register_zw_node: %d func=%d, bt=%d, gt=%d, st=%d\n",
v4,
*((unsigned __int8 *)v5 + 53),
*((unsigned __int8 *)v5 + 54),
*((unsigned __int8 *)v5 + 55),
*((unsigned __int8 *)v5 + 56));
return 0LL;
}

original_code:
int
register_zw_node( zw_api_ctx_S *ctx, const u8 *frame, int id )
{
 list_node *node = ((void *)0);
 struct zw_node *zwnode;
 int rc = -1;
 syslog( 5, "register_zw_node: %d\n", id );;
 fflush(stdout);
 for(node = (&zw_nodes)->next; node != (&zw_nodes); node = node->next) {
  zwnode = (struct zw_node *)node;
  if ( zwnode->id == id ) {
   zwnode->mode = frame[ 2 ];
   zwnode->func = frame[ 3 ];
   zwnode->btype = frame[ 5 ];
   zwnode->gtype = frame[ 6 ];
   zwnode->stype = frame[ 7 ];
   zwnode->cclass = get_cmd_class( zwnode->gtype );
   cc_get( ctx, id, zwnode->cclass, (void *)&zwnode->state );
   rc = 0;
                        syslog( 5, "register_zw_node: %d func=%d, bt=%d, gt=%d, st=%d\n", id, zwnode->func, zwnode->btype, zwnode->gtype, zwnode->stype );;
   break;
  }
 }
 return rc;
}

canonical_original_code:
int register_zw_node(struct zw_api_ctx *ctx, unsigned char *frame, int id)
{
 struct _node *node = ((void *)0);
 struct zw_node *zwnode;
 int rc = -1;
 syslog( 5, "register_zw_node: %d\n", id );;
 fflush(stdout);
 for(node = (&zw_nodes)->next; node != (&zw_nodes); node = node->next) {
  zwnode = (struct zw_node *)node;
  if ( zwnode->id == id ) {
   zwnode->mode = frame[ 2 ];
   zwnode->func = frame[ 3 ];
   zwnode->btype = frame[ 5 ];
   zwnode->gtype = frame[ 6 ];
   zwnode->stype = frame[ 7 ];
   zwnode->cclass = get_cmd_class( zwnode->gtype );
   cc_get( ctx, id, zwnode->cclass, (void *)&zwnode->state );
   rc = 0;
                        syslog( 5, "register_zw_node: %d func=%d, bt=%d, gt=%d, st=%d\n", id, zwnode->func, zwnode->btype, zwnode->gtype, zwnode->stype );;
   break;
  }
 }
 return rc;
}

variable_types:
frame
unsigned char *

variable_types: id int


decompiled_code:
__int64 __fastcall unregister_zw_node(int a1)
{
void **v2; // rdi

v2 = (void **)zw_nodes;
if ( zw_nodes == &zw_nodes )
return 0xFFFFFFFFLL;
while ( *((_DWORD *)v2 + 12) != a1 )
{
v2 = (void **)*v2;
if ( v2 == &zw_nodes )
return 0xFFFFFFFFLL;
}
list_remove_constprop_0(v2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func17(int a1)
{
void **v2; // rdi

v2 = (void **)zw_nodes;
if ( zw_nodes == &zw_nodes )
return 0xFFFFFFFFLL;
while ( *((_DWORD *)v2 + 12) != a1 )
{
v2 = (void **)*v2;
if ( v2 == &zw_nodes )
return 0xFFFFFFFFLL;
}
func1(v2);
return 0LL;
}

original_code:
int
unregister_zw_node( int id )
{
 list_node *node = ((void *)0);
 struct zw_node *zwnode;
 int rc = -1;
 for(node = (&zw_nodes)->next; node != (&zw_nodes); node = node->next) {
  zwnode = (struct zw_node *)node;
  if ( zwnode->id == id ) {
   rc = 0;
   break;
  }
 }
 if ( 0 == rc )
  list_remove((list_node *)&zw_nodes, (list_node *)zwnode);
 return rc;
}

canonical_original_code:
int unregister_zw_node(int id)
{
 struct _node *node = ((void *)0);
 struct zw_node *zwnode;
 int rc = -1;
 for(node = (&zw_nodes)->next; node != (&zw_nodes); node = node->next) {
  zwnode = (struct zw_node *)node;
  if ( zwnode->id == id ) {
   rc = 0;
   break;
  }
 }
 if ( 0 == rc )
  list_remove((struct _node *)&zw_nodes, (struct _node *)zwnode);
 return rc;
}


original_code:
static int
toggle_sw_set( zw_api_ctx_S *ctx, u8 nodeid, void *value )
{
 u8 buff[1024];
 int level = *(int *)value;
 buff[0] = 0x13;
 buff[1] = nodeid;
 buff[2] = 3;
 buff[3] = 0x28;
 buff[4] = 0x01;
 buff[5] = level;
 buff[6] = 0x01 | 0x04;
 return zw_send_request( ctx, buff, 7, 3, 1, 0x13 );
}

canonical_original_code:
static int toggle_sw_set(struct zw_api_ctx *ctx, unsigned char nodeid, void *value)
{
 unsigned char buff[1024];
 int level = *(int *)value;
 buff[0] = 0x13;
 buff[1] = nodeid;
 buff[2] = 3;
 buff[3] = 0x28;
 buff[4] = 0x01;
 buff[5] = level;
 buff[6] = 0x01 | 0x04;
 return zw_send_request( ctx, buff, 7, 3, 1, 0x13 );
}

variable_types:
nodeid
unsigned char

variable_types:
buff
unsigned char [1024]

variable_types: level int


decompiled_code:
__int64 __fastcall toggle_sw_get(__int64 a1, char a2, _DWORD *a3)
{
unsigned int v4; // er12
struct timespec v6; // [rsp+0h] [rbp-428h] BYREF
char v7[2]; // [rsp+10h] [rbp-418h] BYREF
int v8; // [rsp+12h] [rbp-416h]
char v9; // [rsp+16h] [rbp-412h]

v7[1] = a2;
v7[0] = 19;
v8 = 84027394;
v9 = 3;
zw_send_request(a1, v7, 7LL, 3LL, 1LL, 19LL, v6.tv_sec);
pthread_mutex_lock(&val_lock);
clock_gettime(0, &v6);
v6.tv_sec += 5LL;
v4 = pthread_cond_timedwait(&tsw_value, &val_lock, &v6);
*a3 = tsw_val;
pthread_mutex_unlock(&val_lock);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, char a2, _DWORD *a3)
{
unsigned int v4; // er12
struct timespec v6; // [rsp+0h] [rbp-428h] BYREF
char v7[2]; // [rsp+10h] [rbp-418h] BYREF
int v8; // [rsp+12h] [rbp-416h]
char v9; // [rsp+16h] [rbp-412h]

v7[1] = a2;
v7[0] = 19;
v8 = 84027394;
v9 = 3;
zw_send_request(a1, v7, 7LL, 3LL, 1LL, 19LL, v6.tv_sec);
pthread_mutex_lock(&val_lock);
clock_gettime(0, &v6);
v6.tv_sec += 5LL;
v4 = pthread_cond_timedwait(&tsw_value, &val_lock, &v6);
*a3 = tsw_val;
pthread_mutex_unlock(&val_lock);
return v4;
}

original_code:
static int
toggle_sw_get( zw_api_ctx_S *ctx, u8 nodeid, void *resp )
{
 u8 buff[1024];
 int rc;
 int *value = (int *)resp;
 struct timespec ts;
 buff[0] = 0x13;
 buff[1] = nodeid;
 buff[2] = 2;
 buff[3] = 0x28;
 buff[4] = 0x02;
 buff[5] = 0x01 | 0x04;
 buff[6] = 3;
 rc = zw_send_request( ctx, buff, 7, 3, 1, 0x13 );
 pthread_mutex_lock( &val_lock );
 clock_gettime(0, &ts);
 ts.tv_sec += 5;
 rc = pthread_cond_timedwait(&tsw_value, &val_lock, &ts);
 *value = tsw_val;
 pthread_mutex_unlock( &val_lock );
 return rc;
}

canonical_original_code:
static int toggle_sw_get(struct zw_api_ctx *ctx, unsigned char nodeid, void *resp)
{
 unsigned char buff[1024];
 int rc;
 int *value = (int *)resp;
 struct timespec ts;
 buff[0] = 0x13;
 buff[1] = nodeid;
 buff[2] = 2;
 buff[3] = 0x28;
 buff[4] = 0x02;
 buff[5] = 0x01 | 0x04;
 buff[6] = 3;
 rc = zw_send_request( ctx, buff, 7, 3, 1, 0x13 );
 pthread_mutex_lock( &val_lock );
 clock_gettime(0, &ts);
 ts.tv_sec += 5;
 rc = pthread_cond_timedwait(&tsw_value, &val_lock, &ts);
 *value = tsw_val;
 pthread_mutex_unlock( &val_lock );
 return rc;
}

variable_types:
value
int *

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


decompiled_code:
__int64 __fastcall toggle_sw_report(__int64 a1, char a2, _DWORD *a3)
{
return toggle_sw_get(a1, a2, a3);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, char a2, _DWORD *a3)
{
return func1(a1, a2, a3);
}

original_code:
static int
toggle_sw_report( zw_api_ctx_S *ctx, u8 nodeid, void *resp )
{
 return toggle_sw_get( ctx, nodeid, resp );
}

canonical_original_code:
static int toggle_sw_report(struct zw_api_ctx *ctx, unsigned char nodeid, void *resp)
{
 return toggle_sw_get( ctx, nodeid, resp );
}

variable_types: struct zw_api_ctx { int port; int node_id; } *


decompiled_code:
__int64 __fastcall toggle_sw_proc_msg(__int64 a1, __int64 a2, unsigned __int8 a3)
{
unsigned __int8 v4; // al
int v5; // ebx

syslog(7, "COMMAND_CLASS_BINARY_TOGGLE_SWITCH - processing message");
v4 = *(_BYTE *)(a2 + 6);
switch ( v4 )
{
case 1u:
syslog(7, "SWITCH_TOGGLE_BINARY_SET received from node %d value %d", a3, *(unsigned __int8 *)(a2 + 7));
v5 = *(unsigned __int8 *)(a2 + 7);
goto LABEL_5;
case 2u:
syslog(7, "SWITCH_TOGGLE_BINARY_GET received from node %d value %d", a3, *(unsigned __int8 *)(a2 + 7));
v5 = *(unsigned __int8 *)(a2 + 7);
goto LABEL_5;
case 3u:
syslog(7, "SWITCH_TOGGLE_BINARY_REPORT received from node %d value %d", a3, *(unsigned __int8 *)(a2 + 7));
v5 = *(unsigned __int8 *)(a2 + 7);
LABEL_5:
pthread_mutex_lock(&val_lock);
tsw_val = v5;
pthread_cond_broadcast(&tsw_value);
pthread_mutex_unlock(&val_lock);
return 0LL;
}
syslog(5, "%i received from node %d", v4, a3);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2, unsigned __int8 a3)
{
unsigned __int8 v4; // al
int v5; // ebx

syslog(7, "COMMAND_CLASS_BINARY_TOGGLE_SWITCH - processing message");
v4 = *(_BYTE *)(a2 + 6);
switch ( v4 )
{
case 1u:
syslog(7, "SWITCH_TOGGLE_BINARY_SET received from node %d value %d", a3, *(unsigned __int8 *)(a2 + 7));
v5 = *(unsigned __int8 *)(a2 + 7);
goto LABEL_5;
case 2u:
syslog(7, "SWITCH_TOGGLE_BINARY_GET received from node %d value %d", a3, *(unsigned __int8 *)(a2 + 7));
v5 = *(unsigned __int8 *)(a2 + 7);
goto LABEL_5;
case 3u:
syslog(7, "SWITCH_TOGGLE_BINARY_REPORT received from node %d value %d", a3, *(unsigned __int8 *)(a2 + 7));
v5 = *(unsigned __int8 *)(a2 + 7);
LABEL_5:
pthread_mutex_lock(&val_lock);
tsw_val = v5;
pthread_cond_broadcast(&tsw_value);
pthread_mutex_unlock(&val_lock);
return 0LL;
}
syslog(5, "%i received from node %d", v4, a3);
return 0LL;
}

original_code:
static int
toggle_sw_proc_msg( zw_api_ctx_S *ctx, const u8* frame, u8 nodeid )
{
 int val = -1;
 syslog( 7, "COMMAND_CLASS_BINARY_TOGGLE_SWITCH - processing message" );;
 if ((unsigned char)frame[6] == 0x01) {
  syslog( 7, "SWITCH_TOGGLE_BINARY_SET received from node %d value %d", nodeid,(unsigned char)frame[7] );;
  val = frame[7];
 }
 else if ((unsigned char)frame[6] == 0x02) {
  syslog( 7, "SWITCH_TOGGLE_BINARY_GET received from node %d value %d", nodeid,(unsigned char)frame[7] );;
  val = frame[7];
 }
 else if ((unsigned char)frame[6] == 0x03) {
  syslog( 7, "SWITCH_TOGGLE_BINARY_REPORT received from node %d value %d", nodeid,(unsigned char)frame[7] );;
  val = frame[7];
 }
 else {
  syslog( 5, "%i received from node %d", frame[6], nodeid );;
  goto out;
 }
 pthread_mutex_lock( &val_lock );
 tsw_val = val;
 pthread_cond_broadcast( &tsw_value );
 pthread_mutex_unlock( &val_lock );
out:
 return 0;
}

canonical_original_code:
static int toggle_sw_proc_msg(struct zw_api_ctx *ctx, unsigned char *frame, unsigned char nodeid)
{
 int val = -1;
 syslog( 7, "COMMAND_CLASS_BINARY_TOGGLE_SWITCH - processing message" );;
 if ((unsigned char)frame[6] == 0x01) {
  syslog( 7, "SWITCH_TOGGLE_BINARY_SET received from node %d value %d", nodeid,(unsigned char)frame[7] );;
  val = frame[7];
 }
 else if ((unsigned char)frame[6] == 0x02) {
  syslog( 7, "SWITCH_TOGGLE_BINARY_GET received from node %d value %d", nodeid,(unsigned char)frame[7] );;
  val = frame[7];
 }
 else if ((unsigned char)frame[6] == 0x03) {
  syslog( 7, "SWITCH_TOGGLE_BINARY_REPORT received from node %d value %d", nodeid,(unsigned char)frame[7] );;
  val = frame[7];
 }
 else {
  syslog( 5, "%i received from node %d", frame[6], nodeid );;
  goto out;
 }
 pthread_mutex_lock( &val_lock );
 tsw_val = val;
 pthread_cond_broadcast( &tsw_value );
 pthread_mutex_unlock( &val_lock );
out:
 return 0;
}


decompiled_code:
__int64 toggle_sw_init()
{
return register_cmd_class(&toggle_sw);
}

canonical_decompiled_code:
__int64 func4()
{
return register_cmd_class(&toggle_sw);
}

original_code:
static void __attribute__ ((constructor)) toggle_sw_init( void )
{
 register_cmd_class( &toggle_sw );
}

canonical_original_code:
static void toggle_sw_init()
{
 register_cmd_class( &toggle_sw );
}


decompiled_code:
int toggle_sw_exit()
{
unregister_cmd_class(&toggle_sw);
pthread_cond_destroy(&tsw_value);
return pthread_mutex_destroy(&val_lock);
}

canonical_decompiled_code:
int func5()
{
unregister_cmd_class(&toggle_sw);
pthread_cond_destroy(&tsw_value);
return pthread_mutex_destroy(&val_lock);
}

original_code:
static void __attribute__ ((destructor)) toggle_sw_exit( void )
{
 unregister_cmd_class( &toggle_sw );
 pthread_cond_destroy( &tsw_value );
 pthread_mutex_destroy( &val_lock );
}

canonical_original_code:
static void toggle_sw_exit()
{
 unregister_cmd_class( &toggle_sw );
 pthread_cond_destroy( &tsw_value );
 pthread_mutex_destroy( &val_lock );
}


decompiled_code:
void print_cmd_classes()
{
const char **v0; // rbx

v0 = (const char **)cmd_classes;
if ( cmd_classes != &cmd_classes )
{
do
{
syslog(7, "CmdClass: %s", v0[2]);
v0 = (const char **)*v0;
}
while ( v0 != (const char **)&cmd_classes );
}
}

canonical_decompiled_code:
void func2()
{
const char **v0; // rbx

v0 = (const char **)cmd_classes;
if ( cmd_classes != &cmd_classes )
{
do
{
syslog(7, "CmdClass: %s", v0[2]);
v0 = (const char **)*v0;
}
while ( v0 != (const char **)&cmd_classes );
}
}

original_code:
void print_cmd_classes()
{
 list_node *node = ((void *)0);
 for(node = (&cmd_classes)->next; node != (&cmd_classes); node = node->next) {
  syslog( 7, "CmdClass: %s", ((struct cmd_class *)node)->name );;
 }
}

canonical_original_code:
void print_cmd_classes()
{
 struct _node *node = ((void *)0);
 for(node = (&cmd_classes)->next; node != (&cmd_classes); node = node->next) {
  syslog( 7, "CmdClass: %s", ((struct cmd_class *)node)->name );;
 }
}

user_defined_types: struct cmd_class { struct _node list; char *name; unsigned char type; int (*process_msg)(struct zw_api_ctx *ctx, unsigned char *frame, unsigned char nodeid); int (*version)(struct zw_api_ctx *ctx, unsigned char nodeid, void *resp); int (*get)(struct zw_api_ctx *ctx, unsigned char nodeid, void *resp); int (*set)(struct zw_api_ctx *ctx, unsigned char nodeid, void *resp); int (*report)(struct zw_api_ctx *ctx, unsigned char nodeid, void *resp); }


decompiled_code:
__int64 __fastcall cc_process_generic_msg(__int64 a1, unsigned __int8 *a2)
{
__int64 v2; // rdx
__int64 result; // rax
unsigned __int8 v4; // al
unsigned __int8 v5; // al
unsigned __int8 v6; // al
char v7[536]; // [rsp+0h] [rbp-218h] BYREF

v2 = a2[5];
switch ( (_BYTE)v2 )
{
case 0x60:
syslog(7, "COMMAND_CLASS_MULTI_INSTANCE");
v4 = a2[6];
if ( v4 == 5 )
{
syslog(7, "Got MULTI_INSTANCE_REPORT from node %i: Command Class 0x%x, instance count: %i", a2[3], a2[7], a2[8]);
return 0LL;
}
if ( v4 == 6 )
{
syslog(
7,
"Got MULTI_INSTANCE_CMD_ENCAP from node %i: instance %i Command Class 0x%x type 0x%x",
a2[3],
a2[7],
a2[8],
a2[9]);
v5 = a2[8];
if ( v5 == 49 )
{
syslog(
7,
"CommandSensorMultilevelReport: node=%i, inst=%i, type=%i, valuem=%i, value=%i",
a2[3],
a2[7],
a2[10],
a2[11],
a2[12]);
return 0LL;
}
if ( v5 == 32 )
{
v6 = a2[9];
if ( v6 == 3 )
{
syslog(7, "Got basic report from node %i, instance %i, value: %i", a2[3], a2[7], a2[10]);
return 0LL;
}
if ( v6 == 1 )
{
syslog(7, "Got basic set from node %i, instance %i, value: %i", a2[3], a2[7], a2[10]);
return 0LL;
}
}
}
return 0LL;
case 0x86:
syslog(7, "\nCOMMAND_CLASS_VERSION");
if ( a2[6] == 18 )
{
syslog(
7,
"REPORT: Lib.typ: 0x%x, Prot.Ver: 0x%x, Sub: 0x%x App.Ver: 0x%x, Sub: 0x%x",
a2[7],
a2[8],
a2[9],
a2[10],
a2[11]);
return 0LL;
}
return 0LL;
case 0x21:
syslog(7, "COMMAND_CLASS_CONTROLLER_REPLICATION");
v7[0] = 68;
((void (__fastcall *)(__int64, char *, __int64, _QWORD, _QWORD, _QWORD))zw_send_request)(
a1,
v7,
1LL,
0LL,
0LL,
0LL);
result = 0LL;
break;
default:
syslog(7, "Function not implemented - unhandled command class: %x", v2);
result = 0xFFFFFFFFLL;
break;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, unsigned __int8 *a2)
{
__int64 v2; // rdx
__int64 result; // rax
unsigned __int8 v4; // al
unsigned __int8 v5; // al
unsigned __int8 v6; // al
char v7[536]; // [rsp+0h] [rbp-218h] BYREF

v2 = a2[5];
switch ( (_BYTE)v2 )
{
case 0x60:
syslog(7, "COMMAND_CLASS_MULTI_INSTANCE");
v4 = a2[6];
if ( v4 == 5 )
{
syslog(7, "Got MULTI_INSTANCE_REPORT from node %i: Command Class 0x%x, instance count: %i", a2[3], a2[7], a2[8]);
return 0LL;
}
if ( v4 == 6 )
{
syslog(
7,
"Got MULTI_INSTANCE_CMD_ENCAP from node %i: instance %i Command Class 0x%x type 0x%x",
a2[3],
a2[7],
a2[8],
a2[9]);
v5 = a2[8];
if ( v5 == 49 )
{
syslog(
7,
"CommandSensorMultilevelReport: node=%i, inst=%i, type=%i, valuem=%i, value=%i",
a2[3],
a2[7],
a2[10],
a2[11],
a2[12]);
return 0LL;
}
if ( v5 == 32 )
{
v6 = a2[9];
if ( v6 == 3 )
{
syslog(7, "Got basic report from node %i, instance %i, value: %i", a2[3], a2[7], a2[10]);
return 0LL;
}
if ( v6 == 1 )
{
syslog(7, "Got basic set from node %i, instance %i, value: %i", a2[3], a2[7], a2[10]);
return 0LL;
}
}
}
return 0LL;
case 0x86:
syslog(7, "\nCOMMAND_CLASS_VERSION");
if ( a2[6] == 18 )
{
syslog(
7,
"REPORT: Lib.typ: 0x%x, Prot.Ver: 0x%x, Sub: 0x%x App.Ver: 0x%x, Sub: 0x%x",
a2[7],
a2[8],
a2[9],
a2[10],
a2[11]);
return 0LL;
}
return 0LL;
case 0x21:
syslog(7, "COMMAND_CLASS_CONTROLLER_REPLICATION");
v7[0] = 68;
((void (__fastcall *)(__int64, char *, __int64, _QWORD, _QWORD, _QWORD))zw_send_request)(
a1,
v7,
1LL,
0LL,
0LL,
0LL);
result = 0LL;
break;
default:
syslog(7, "Function not implemented - unhandled command class: %x", v2);
result = 0xFFFFFFFFLL;
break;
}
return result;
}

original_code:
int
cc_process_generic_msg( zw_api_ctx_S *ctx, const u8* frame )
{
 u8 tempbuf[512];
 int rc = -1;
 switch ((unsigned char)frame[5]) {
  case 0x21:
   syslog( 7, "COMMAND_CLASS_CONTROLLER_REPLICATION" );;
   if (frame[6] == 0x31) {
    tempbuf[0]=0x44;
    zw_send_request( ctx, tempbuf , 1, 0, 0, 0 );
   } else {
    tempbuf[0]=0x44;
    zw_send_request( ctx, tempbuf , 1, 0, 0, 0 );
   }
   rc = 0;
   break;
  case 0x60:
   syslog( 7, "COMMAND_CLASS_MULTI_INSTANCE" );;
   if (frame[6] == 0x05) {
    int instanceCount = (unsigned char)frame[8];
    syslog( 7, "Got MULTI_INSTANCE_REPORT from node %i: Command Class 0x%x, instance count: %i",(unsigned char)frame[3],(unsigned char)frame[7], instanceCount );;
   } else if (frame[6] == 0x6) {
    syslog( 7, "Got MULTI_INSTANCE_CMD_ENCAP from node %i: instance %i Command Class 0x%x type 0x%x",(unsigned char)frame[3],(unsigned char)frame[7],(unsigned char)frame[8],(unsigned char)frame[9] );;
    if (frame[8] == 0x31) {
     syslog( 7, "CommandSensorMultilevelReport: node=%i, inst=%i, type=%i, valuem=%i, value=%i", frame[3], frame[7], frame[10], frame[11], frame[12] );;
    } else if ((frame[8] == 0x20) && (frame[9] == 0x03)) {
     syslog( 7, "Got basic report from node %i, instance %i, value: %i",(unsigned char)frame[3],(unsigned char)frame[7], (unsigned char) frame[10] );;
    } else if ((frame[8] == 0x20) && (frame[9] == 0x01)) {
     syslog( 7, "Got basic set from node %i, instance %i, value: %i",(unsigned char)frame[3],(unsigned char)frame[7], (unsigned char) frame[10] );;
    }
   }
   rc = 0;
   break;
  case 0x86:
   syslog( 7, "\nCOMMAND_CLASS_VERSION" );;
   if (frame[6] == 0x12) {
    syslog( 7, "REPORT: Lib.typ: 0x%x, Prot.Ver: 0x%x, Sub: 0x%x App.Ver: 0x%x, Sub: 0x%x", (unsigned char)frame[7], (unsigned char)frame[8], (unsigned char)frame[9], (unsigned char)frame[10], (unsigned char)frame[11] );;
   }
   rc = 0;
   break;
  default:
   syslog( 7, "Function not implemented - unhandled command class: %x",(unsigned char)frame[5] );;
   break;
  ;;
 }
 return rc;
}

canonical_original_code:
int cc_process_generic_msg(struct zw_api_ctx *ctx, unsigned char *frame)
{
 unsigned char tempbuf[512];
 int rc = -1;
 switch ((unsigned char)frame[5]) {
  case 0x21:
   syslog( 7, "COMMAND_CLASS_CONTROLLER_REPLICATION" );;
   if (frame[6] == 0x31) {
    tempbuf[0]=0x44;
    zw_send_request( ctx, tempbuf , 1, 0, 0, 0 );
   } else {
    tempbuf[0]=0x44;
    zw_send_request( ctx, tempbuf , 1, 0, 0, 0 );
   }
   rc = 0;
   break;
  case 0x60:
   syslog( 7, "COMMAND_CLASS_MULTI_INSTANCE" );;
   if (frame[6] == 0x05) {
    int instanceCount = (unsigned char)frame[8];
    syslog( 7, "Got MULTI_INSTANCE_REPORT from node %i: Command Class 0x%x, instance count: %i",(unsigned char)frame[3],(unsigned char)frame[7], instanceCount );;
   } else if (frame[6] == 0x6) {
    syslog( 7, "Got MULTI_INSTANCE_CMD_ENCAP from node %i: instance %i Command Class 0x%x type 0x%x",(unsigned char)frame[3],(unsigned char)frame[7],(unsigned char)frame[8],(unsigned char)frame[9] );;
    if (frame[8] == 0x31) {
     syslog( 7, "CommandSensorMultilevelReport: node=%i, inst=%i, type=%i, valuem=%i, value=%i", frame[3], frame[7], frame[10], frame[11], frame[12] );;
    } else if ((frame[8] == 0x20) && (frame[9] == 0x03)) {
     syslog( 7, "Got basic report from node %i, instance %i, value: %i",(unsigned char)frame[3],(unsigned char)frame[7], (unsigned char) frame[10] );;
    } else if ((frame[8] == 0x20) && (frame[9] == 0x01)) {
     syslog( 7, "Got basic set from node %i, instance %i, value: %i",(unsigned char)frame[3],(unsigned char)frame[7], (unsigned char) frame[10] );;
    }
   }
   rc = 0;
   break;
  case 0x86:
   syslog( 7, "\nCOMMAND_CLASS_VERSION" );;
   if (frame[6] == 0x12) {
    syslog( 7, "REPORT: Lib.typ: 0x%x, Prot.Ver: 0x%x, Sub: 0x%x App.Ver: 0x%x, Sub: 0x%x", (unsigned char)frame[7], (unsigned char)frame[8], (unsigned char)frame[9], (unsigned char)frame[10], (unsigned char)frame[11] );;
   }
   rc = 0;
   break;
  default:
   syslog( 7, "Function not implemented - unhandled command class: %x",(unsigned char)frame[5] );;
   break;
  ;;
 }
 return rc;
}

variable_types:
tempbuf
unsigned char [512]

variable_types: instanceCount int


decompiled_code:
__int64 __fastcall cc_process_unimplemented_msg(unsigned __int8 *a1)
{
__int64 v1; // rdx
unsigned __int8 v3; // al
unsigned __int8 v4; // al

v1 = a1[5];
if ( (unsigned __int8)v1 > 0x46u )
{
switch ( (char)v1 )
{
case 113:
syslog(7, "\nCOMMAND_CLASS_ALARM - ");
if ( a1[6] == 5 )
{
syslog(7, "Got ALARM from node %i, type: %i, level: %i", a1[3], a1[7], a1[8]);
syslog(7, "Send sensor tripped changed event");
}
break;
case 114:
syslog(7, "\nCOMMAND_CLASS_MANUFACTURER_SPECIFIC");
break;
case -127:
syslog(7, "COMMAND_CLASS_CLOCK - ");
break;
case -124:
syslog(7, "\nCOMMAND_CLASS_WAKE_UP - ");
break;
case -123:
syslog(7, "\nCOMMAND_CLASS_ASSOCIATION - ");
break;
case -113:
syslog(7, "COMMAND_CLASS_MULTI_CMD - ");
break;
case -100:
syslog(7, "\nCOMMAND_CLASS_SENSOR_ALARM - ");
break;
default:
goto LABEL_5;
}
}
else if ( (unsigned __int8)v1 <= 0x1Fu )
{
LABEL_5:
syslog(4, "Function not implemented - unhandled command class: %x", v1);
}
else
{
switch ( (char)v1 )
{
case ' ':
syslog(7, "\nCOMMAND_CLASS_BASIC - ");
return 0LL;
case '&':
syslog(7, "\nCOMMAND_CLASS_SWITCH_MULTILEVEL - ");
v3 = a1[6];
if ( v3 == 3 )
{
syslog(7, "Got switch multilevel report from node %i, level: %i", a1[3], a1[7]);
}
else
{
if ( v3 != 1 )
return 0LL;
syslog(7, "Got switch multilevel set from node %i, level: %i", a1[3], a1[7]);
}
syslog(7, "Send light changed event");
break;
case '\'':
syslog(7, "\nCOMMAND_CLASS_SWITCH_ALL - ");
v4 = a1[6];
if ( v4 == 4 )
{
syslog(7, "Got switch all ON from node %i", a1[3]);
syslog(7, "Send light changed event");
v4 = a1[6];
}
if ( v4 == 5 )
{
syslog(7, "Got switch all OFF from node %i", a1[3]);
syslog(7, "Send light changed event");
}
return 0LL;
case '1':
syslog(7, "\nCOMMAND_CLASS_SENSOR_MULTILEVEL - ");
return 0LL;
case '2':
syslog(7, "\nCOMMAND_CLASS_METER");
return 0LL;
case '@':
syslog(7, "COMMAND_CLASS_THERMOSTAT_MODE - ");
return 0LL;
case 'C':
syslog(7, "COMMAND_CLASS_THERMOSTAT_SETPOINT - ");
return 0LL;
case 'F':
syslog(7, "\nCOMMAND_CLASS_CLIMATE_CONTROL_SCHEDULE: - ");
if ( a1[6] == 2 )
syslog(7, "Got SCHEDULE_GET from node %i for day: %i", a1[3], a1[7]);
return 0LL;
default:
goto LABEL_5;
}
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned __int8 *a1)
{
__int64 v1; // rdx
unsigned __int8 v3; // al
unsigned __int8 v4; // al

v1 = a1[5];
if ( (unsigned __int8)v1 > 0x46u )
{
switch ( (char)v1 )
{
case 113:
syslog(7, "\nCOMMAND_CLASS_ALARM - ");
if ( a1[6] == 5 )
{
syslog(7, "Got ALARM from node %i, type: %i, level: %i", a1[3], a1[7], a1[8]);
syslog(7, "Send sensor tripped changed event");
}
break;
case 114:
syslog(7, "\nCOMMAND_CLASS_MANUFACTURER_SPECIFIC");
break;
case -127:
syslog(7, "COMMAND_CLASS_CLOCK - ");
break;
case -124:
syslog(7, "\nCOMMAND_CLASS_WAKE_UP - ");
break;
case -123:
syslog(7, "\nCOMMAND_CLASS_ASSOCIATION - ");
break;
case -113:
syslog(7, "COMMAND_CLASS_MULTI_CMD - ");
break;
case -100:
syslog(7, "\nCOMMAND_CLASS_SENSOR_ALARM - ");
break;
default:
goto LABEL_5;
}
}
else if ( (unsigned __int8)v1 <= 0x1Fu )
{
LABEL_5:
syslog(4, "Function not implemented - unhandled command class: %x", v1);
}
else
{
switch ( (char)v1 )
{
case ' ':
syslog(7, "\nCOMMAND_CLASS_BASIC - ");
return 0LL;
case '&':
syslog(7, "\nCOMMAND_CLASS_SWITCH_MULTILEVEL - ");
v3 = a1[6];
if ( v3 == 3 )
{
syslog(7, "Got switch multilevel report from node %i, level: %i", a1[3], a1[7]);
}
else
{
if ( v3 != 1 )
return 0LL;
syslog(7, "Got switch multilevel set from node %i, level: %i", a1[3], a1[7]);
}
syslog(7, "Send light changed event");
break;
case '\'':
syslog(7, "\nCOMMAND_CLASS_SWITCH_ALL - ");
v4 = a1[6];
if ( v4 == 4 )
{
syslog(7, "Got switch all ON from node %i", a1[3]);
syslog(7, "Send light changed event");
v4 = a1[6];
}
if ( v4 == 5 )
{
syslog(7, "Got switch all OFF from node %i", a1[3]);
syslog(7, "Send light changed event");
}
return 0LL;
case '1':
syslog(7, "\nCOMMAND_CLASS_SENSOR_MULTILEVEL - ");
return 0LL;
case '2':
syslog(7, "\nCOMMAND_CLASS_METER");
return 0LL;
case '@':
syslog(7, "COMMAND_CLASS_THERMOSTAT_MODE - ");
return 0LL;
case 'C':
syslog(7, "COMMAND_CLASS_THERMOSTAT_SETPOINT - ");
return 0LL;
case 'F':
syslog(7, "\nCOMMAND_CLASS_CLIMATE_CONTROL_SCHEDULE: - ");
if ( a1[6] == 2 )
syslog(7, "Got SCHEDULE_GET from node %i for day: %i", a1[3], a1[7]);
return 0LL;
default:
goto LABEL_5;
}
}
return 0LL;
}

original_code:
int
cc_process_unimplemented_msg( const u8 *frame )
{
 switch ((unsigned char)frame[5]) {
  case 0x32:
   syslog( 7, "\nCOMMAND_CLASS_METER" );;
   break;
  case 0x72:
   syslog( 7, "\nCOMMAND_CLASS_MANUFACTURER_SPECIFIC" );;
   break;
  case 0x84:
   syslog( 7, "\nCOMMAND_CLASS_WAKE_UP - " );;
   break;
  case 0x31:
   syslog( 7, "\nCOMMAND_CLASS_SENSOR_MULTILEVEL - " );;
   break;
  case 0x9c:
   syslog( 7, "\nCOMMAND_CLASS_SENSOR_ALARM - " );;
   break;
  case 0x26:
   syslog( 7, "\nCOMMAND_CLASS_SWITCH_MULTILEVEL - " );;
   if (frame[6] == 0x03) {
    syslog( 7, "Got switch multilevel report from node %i, level: %i",(unsigned char)frame[3],(unsigned char)frame[7] );;
    syslog( 7, "Send light changed event" );;
   } else if ((unsigned char)frame[6] == 0x01) {
    syslog( 7, "Got switch multilevel set from node %i, level: %i",(unsigned char)frame[3],(unsigned char)frame[7] );;
    syslog( 7, "Send light changed event" );;
   }
   break;
  case 0x27:
   syslog( 7, "\nCOMMAND_CLASS_SWITCH_ALL - " );;
   if (frame[6] == 0x04) {
    syslog( 7, "Got switch all ON from node %i",(unsigned char)frame[3] );;
    syslog( 7, "Send light changed event" );;
   }
   if (frame[6] == 0x05) {
    syslog( 7, "Got switch all OFF from node %i",(unsigned char)frame[3] );;
    syslog( 7, "Send light changed event" );;
   }
   break;
  case 0x71:
   syslog( 7, "\nCOMMAND_CLASS_ALARM - " );;
   if (frame[6] == 0x05) {
    syslog( 7, "Got ALARM from node %i, type: %i, level: %i",(unsigned char)frame[3],(unsigned char)frame[7],(unsigned char)frame[8] );;
    syslog( 7, "Send sensor tripped changed event" );;
   }
   break;
  case 0x20:
   syslog( 7, "\nCOMMAND_CLASS_BASIC - " );;
   break;
  case 0x46:
   syslog( 7, "\nCOMMAND_CLASS_CLIMATE_CONTROL_SCHEDULE: - " );;
   if (frame[6] == 0x02) {
    syslog( 7, "Got SCHEDULE_GET from node %i for day: %i",frame[3],frame[7] );;
   }
   break;
  case 0x85:
   syslog( 7, "\nCOMMAND_CLASS_ASSOCIATION - " );;
   break;
  case 0x43:
   syslog( 7, "COMMAND_CLASS_THERMOSTAT_SETPOINT - " );;
   break;
  case 0x81:
   syslog( 7, "COMMAND_CLASS_CLOCK - " );;
   break;
   ;;
  case 0x40:
   syslog( 7, "COMMAND_CLASS_THERMOSTAT_MODE - " );;
   break;
   ;;
  case 0x8F:
   syslog( 7, "COMMAND_CLASS_MULTI_CMD - " );;
   break;
  default:
   syslog( 4, "Function not implemented - unhandled command class: %x",(unsigned char)frame[5] );;
   break;
  ;;
 }
 return 0;
}

canonical_original_code:
int cc_process_unimplemented_msg(unsigned char *frame)
{
 switch ((unsigned char)frame[5]) {
  case 0x32:
   syslog( 7, "\nCOMMAND_CLASS_METER" );;
   break;
  case 0x72:
   syslog( 7, "\nCOMMAND_CLASS_MANUFACTURER_SPECIFIC" );;
   break;
  case 0x84:
   syslog( 7, "\nCOMMAND_CLASS_WAKE_UP - " );;
   break;
  case 0x31:
   syslog( 7, "\nCOMMAND_CLASS_SENSOR_MULTILEVEL - " );;
   break;
  case 0x9c:
   syslog( 7, "\nCOMMAND_CLASS_SENSOR_ALARM - " );;
   break;
  case 0x26:
   syslog( 7, "\nCOMMAND_CLASS_SWITCH_MULTILEVEL - " );;
   if (frame[6] == 0x03) {
    syslog( 7, "Got switch multilevel report from node %i, level: %i",(unsigned char)frame[3],(unsigned char)frame[7] );;
    syslog( 7, "Send light changed event" );;
   } else if ((unsigned char)frame[6] == 0x01) {
    syslog( 7, "Got switch multilevel set from node %i, level: %i",(unsigned char)frame[3],(unsigned char)frame[7] );;
    syslog( 7, "Send light changed event" );;
   }
   break;
  case 0x27:
   syslog( 7, "\nCOMMAND_CLASS_SWITCH_ALL - " );;
   if (frame[6] == 0x04) {
    syslog( 7, "Got switch all ON from node %i",(unsigned char)frame[3] );;
    syslog( 7, "Send light changed event" );;
   }
   if (frame[6] == 0x05) {
    syslog( 7, "Got switch all OFF from node %i",(unsigned char)frame[3] );;
    syslog( 7, "Send light changed event" );;
   }
   break;
  case 0x71:
   syslog( 7, "\nCOMMAND_CLASS_ALARM - " );;
   if (frame[6] == 0x05) {
    syslog( 7, "Got ALARM from node %i, type: %i, level: %i",(unsigned char)frame[3],(unsigned char)frame[7],(unsigned char)frame[8] );;
    syslog( 7, "Send sensor tripped changed event" );;
   }
   break;
  case 0x20:
   syslog( 7, "\nCOMMAND_CLASS_BASIC - " );;
   break;
  case 0x46:
   syslog( 7, "\nCOMMAND_CLASS_CLIMATE_CONTROL_SCHEDULE: - " );;
   if (frame[6] == 0x02) {
    syslog( 7, "Got SCHEDULE_GET from node %i for day: %i",frame[3],frame[7] );;
   }
   break;
  case 0x85:
   syslog( 7, "\nCOMMAND_CLASS_ASSOCIATION - " );;
   break;
  case 0x43:
   syslog( 7, "COMMAND_CLASS_THERMOSTAT_SETPOINT - " );;
   break;
  case 0x81:
   syslog( 7, "COMMAND_CLASS_CLOCK - " );;
   break;
   ;;
  case 0x40:
   syslog( 7, "COMMAND_CLASS_THERMOSTAT_MODE - " );;
   break;
   ;;
  case 0x8F:
   syslog( 7, "COMMAND_CLASS_MULTI_CMD - " );;
   break;
  default:
   syslog( 4, "Function not implemented - unhandled command class: %x",(unsigned char)frame[5] );;
   break;
  ;;
 }
 return 0;
}


decompiled_code:
__int64 __fastcall cc_process_msg(__int64 a1, unsigned __int8 *a2, unsigned __int8 a3)
{
unsigned __int8 v5; // dl
void **v6; // rax
__int64 (__fastcall *v7)(__int64, unsigned __int8 *, _QWORD); // rcx

v5 = a2[5];
if ( v5 == 96 || v5 == 0x86 || v5 == 33 )
return cc_process_generic_msg(a1, a2);
v6 = (void **)cmd_classes;
if ( cmd_classes == &cmd_classes )
{
LABEL_10:
cc_process_unimplemented_msg(a2);
}
else
{
while ( v5 != *((_BYTE *)v6 + 24) )
{
v6 = (void **)*v6;
if ( v6 == &cmd_classes )
goto LABEL_10;
}
v7 = (__int64 (__fastcall *)(__int64, unsigned __int8 *, _QWORD))v6[4];
if ( v7 )
return v7(a1, a2, a3);
syslog(4, "CmdCLass: %s does not register process msg", (const char *)v6[2]);
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, unsigned __int8 *a2, unsigned __int8 a3)
{
unsigned __int8 v5; // dl
void **v6; // rax
__int64 (__fastcall *v7)(__int64, unsigned __int8 *, _QWORD); // rcx

v5 = a2[5];
if ( v5 == 96 || v5 == 0x86 || v5 == 33 )
return func3(a1, a2);
v6 = (void **)cmd_classes;
if ( cmd_classes == &cmd_classes )
{
LABEL_10:
func4(a2);
}
else
{
while ( v5 != *((_BYTE *)v6 + 24) )
{
v6 = (void **)*v6;
if ( v6 == &cmd_classes )
goto LABEL_10;
}
v7 = (__int64 (__fastcall *)(__int64, unsigned __int8 *, _QWORD))v6[4];
if ( v7 )
return v7(a1, a2, a3);
syslog(4, "CmdCLass: %s does not register process msg", (const char *)v6[2]);
}
return 0xFFFFFFFFLL;
}

original_code:
int
cc_process_msg( zw_api_ctx_S *ctx, const u8* frame, u8 nodeid )
{
 list_node *node = ((void *)0);
 struct cmd_class *cmd_cls = ((void *)0);
 int rc = -1;
 int cls_found = 0;
 switch ( frame[ 5 ] ) {
  case 0x21:
  case 0x60:
  case 0x86:
   rc = cc_process_generic_msg( ctx, frame );
   goto out;
  default:
   break;
 }
 for(node = (&cmd_classes)->next; node != (&cmd_classes); node = node->next) {
  cmd_cls = (struct cmd_class *)node;
  if ( cmd_cls->type == frame[ 5 ] ) {
   if ( cmd_cls->process_msg )
    rc = cmd_cls->process_msg( ctx, frame, nodeid );
   else
    syslog( 4, "CmdCLass: %s does not register process msg", cmd_cls->name );;
   cls_found = 1;
   break;
  }
 }
 if( !cls_found ) cc_process_unimplemented_msg( frame );
out:
 return rc;
}

canonical_original_code:
int cc_process_msg(struct zw_api_ctx *ctx, unsigned char *frame, unsigned char nodeid)
{
 struct _node *node = ((void *)0);
 struct cmd_class *cmd_cls = ((void *)0);
 int rc = -1;
 int cls_found = 0;
 switch ( frame[ 5 ] ) {
  case 0x21:
  case 0x60:
  case 0x86:
   rc = cc_process_generic_msg( ctx, frame );
   goto out;
  default:
   break;
 }
 for(node = (&cmd_classes)->next; node != (&cmd_classes); node = node->next) {
  cmd_cls = (struct cmd_class *)node;
  if ( cmd_cls->type == frame[ 5 ] ) {
   if ( cmd_cls->process_msg )
    rc = cmd_cls->process_msg( ctx, frame, nodeid );
   else
    syslog( 4, "CmdCLass: %s does not register process msg", cmd_cls->name );;
   cls_found = 1;
   break;
  }
 }
 if( !cls_found ) cc_process_unimplemented_msg( frame );
out:
 return rc;
}

variable_types: struct cmd_class { struct _node { struct _node *next; struct _node *prev; } list; char *name; unsigned char type; int (*process_msg)(struct zw_api_ctx { int port; int node_id; } *ctx, unsigned char *frame, unsigned char nodeid); int (*version)(struct zw_api_ctx *ctx, unsigned char nodeid, void *resp); int (*get)(struct zw_api_ctx *ctx, unsigned char nodeid, void *resp); int (*set)(struct zw_api_ctx *ctx, unsigned char nodeid, void *resp); int (*report)(struct zw_api_ctx *ctx, unsigned char nodeid, void *resp); } *


decompiled_code:
__int64 __fastcall cc_version(__int64 a1, unsigned __int8 a2, char a3, __int64 a4)
{
void **v5; // rax
__int64 (__fastcall *v6)(__int64, _QWORD, __int64); // rcx

v5 = (void **)cmd_classes;
if ( cmd_classes == &cmd_classes )
return 0xFFFFFFFFLL;
while ( *((_BYTE *)v5 + 24) != a3 )
{
v5 = (void **)*v5;
if ( v5 == &cmd_classes )
return 0xFFFFFFFFLL;
}
v6 = (__int64 (__fastcall *)(__int64, _QWORD, __int64))v5[5];
if ( v6 )
return v6(a1, a2, a4);
syslog(4, "CmdCLass: %s does not register version", (const char *)v5[2]);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, unsigned __int8 a2, char a3, __int64 a4)
{
void **v5; // rax
__int64 (__fastcall *v6)(__int64, _QWORD, __int64); // rcx

v5 = (void **)cmd_classes;
if ( cmd_classes == &cmd_classes )
return 0xFFFFFFFFLL;
while ( *((_BYTE *)v5 + 24) != a3 )
{
v5 = (void **)*v5;
if ( v5 == &cmd_classes )
return 0xFFFFFFFFLL;
}
v6 = (__int64 (__fastcall *)(__int64, _QWORD, __int64))v5[5];
if ( v6 )
return v6(a1, a2, a4);
syslog(4, "CmdCLass: %s does not register version", (const char *)v5[2]);
return 0xFFFFFFFFLL;
}

original_code:
int
cc_version( zw_api_ctx_S *ctx, u8 nodeid, const u8 cls_type, void *resp )
{
 list_node *node = ((void *)0);
 struct cmd_class *cmd_cls = ((void *)0);
 int rc = -1;
 for(node = (&cmd_classes)->next; node != (&cmd_classes); node = node->next) {
  cmd_cls = (struct cmd_class *)node;
  if ( cmd_cls->type == cls_type ) {
   if ( cmd_cls->version )
    rc = cmd_cls->version( ctx, nodeid, resp );
   else
    syslog( 4, "CmdCLass: %s does not register version", cmd_cls->name );;
   break;
  }
 }
 return rc;
}

canonical_original_code:
int cc_version(struct zw_api_ctx *ctx, unsigned char nodeid, unsigned char cls_type, void *resp)
{
 struct _node *node = ((void *)0);
 struct cmd_class *cmd_cls = ((void *)0);
 int rc = -1;
 for(node = (&cmd_classes)->next; node != (&cmd_classes); node = node->next) {
  cmd_cls = (struct cmd_class *)node;
  if ( cmd_cls->type == cls_type ) {
   if ( cmd_cls->version )
    rc = cmd_cls->version( ctx, nodeid, resp );
   else
    syslog( 4, "CmdCLass: %s does not register version", cmd_cls->name );;
   break;
  }
 }
 return rc;
}

variable_types:
resp
void *

variable_types: struct _node { struct _node *next; struct _node *prev; } *


decompiled_code:
__int64 __fastcall cc_get(__int64 a1, unsigned __int8 a2, char a3, __int64 a4)
{
void **v5; // rax
__int64 (__fastcall *v6)(__int64, _QWORD, __int64); // rcx

v5 = (void **)cmd_classes;
if ( cmd_classes == &cmd_classes )
return 0xFFFFFFFFLL;
while ( *((_BYTE *)v5 + 24) != a3 )
{
v5 = (void **)*v5;
if ( v5 == &cmd_classes )
return 0xFFFFFFFFLL;
}
v6 = (__int64 (__fastcall *)(__int64, _QWORD, __int64))v5[6];
if ( v6 )
return v6(a1, a2, a4);
syslog(4, "CmdCLass: %s does not register get", (const char *)v5[2]);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, unsigned __int8 a2, char a3, __int64 a4)
{
void **v5; // rax
__int64 (__fastcall *v6)(__int64, _QWORD, __int64); // rcx

v5 = (void **)cmd_classes;
if ( cmd_classes == &cmd_classes )
return 0xFFFFFFFFLL;
while ( *((_BYTE *)v5 + 24) != a3 )
{
v5 = (void **)*v5;
if ( v5 == &cmd_classes )
return 0xFFFFFFFFLL;
}
v6 = (__int64 (__fastcall *)(__int64, _QWORD, __int64))v5[6];
if ( v6 )
return v6(a1, a2, a4);
syslog(4, "CmdCLass: %s does not register get", (const char *)v5[2]);
return 0xFFFFFFFFLL;
}

original_code:
int
cc_get( zw_api_ctx_S *ctx, u8 nodeid, const u8 cls_type, void *resp )
{
 list_node *node = ((void *)0);
 struct cmd_class *cmd_cls = ((void *)0);
 int rc = -1;
 for(node = (&cmd_classes)->next; node != (&cmd_classes); node = node->next) {
  cmd_cls = (struct cmd_class *)node;
  if ( cmd_cls->type == cls_type ) {
   if ( cmd_cls->get )
    rc = cmd_cls->get( ctx, nodeid, resp );
   else
    syslog( 4, "CmdCLass: %s does not register get", cmd_cls->name );;
   break;
  }
 }
 return rc;
}

canonical_original_code:
int cc_get(struct zw_api_ctx *ctx, unsigned char nodeid, unsigned char cls_type, void *resp)
{
 struct _node *node = ((void *)0);
 struct cmd_class *cmd_cls = ((void *)0);
 int rc = -1;
 for(node = (&cmd_classes)->next; node != (&cmd_classes); node = node->next) {
  cmd_cls = (struct cmd_class *)node;
  if ( cmd_cls->type == cls_type ) {
   if ( cmd_cls->get )
    rc = cmd_cls->get( ctx, nodeid, resp );
   else
    syslog( 4, "CmdCLass: %s does not register get", cmd_cls->name );;
   break;
  }
 }
 return rc;
}

variable_types: rc int


decompiled_code:
__int64 __fastcall cc_set(__int64 a1, unsigned __int8 a2, char a3, __int64 a4)
{
void **v5; // rax
__int64 (__fastcall *v6)(__int64, _QWORD, __int64); // rcx

v5 = (void **)cmd_classes;
if ( cmd_classes == &cmd_classes )
return 0xFFFFFFFFLL;
while ( *((_BYTE *)v5 + 24) != a3 )
{
v5 = (void **)*v5;
if ( v5 == &cmd_classes )
return 0xFFFFFFFFLL;
}
v6 = (__int64 (__fastcall *)(__int64, _QWORD, __int64))v5[7];
if ( v6 )
return v6(a1, a2, a4);
syslog(4, "CmdCLass: %s does not register set", (const char *)v5[2]);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, unsigned __int8 a2, char a3, __int64 a4)
{
void **v5; // rax
__int64 (__fastcall *v6)(__int64, _QWORD, __int64); // rcx

v5 = (void **)cmd_classes;
if ( cmd_classes == &cmd_classes )
return 0xFFFFFFFFLL;
while ( *((_BYTE *)v5 + 24) != a3 )
{
v5 = (void **)*v5;
if ( v5 == &cmd_classes )
return 0xFFFFFFFFLL;
}
v6 = (__int64 (__fastcall *)(__int64, _QWORD, __int64))v5[7];
if ( v6 )
return v6(a1, a2, a4);
syslog(4, "CmdCLass: %s does not register set", (const char *)v5[2]);
return 0xFFFFFFFFLL;
}

original_code:
int
cc_set( zw_api_ctx_S *ctx, u8 nodeid, const u8 cls_type, void *val )
{
 list_node *node = ((void *)0);
 struct cmd_class *cmd_cls = ((void *)0);
 int rc = -1;
 for(node = (&cmd_classes)->next; node != (&cmd_classes); node = node->next) {
  cmd_cls = (struct cmd_class *)node;
  if ( cmd_cls->type == cls_type ) {
   if ( cmd_cls->set )
    rc = cmd_cls->set( ctx, nodeid, val );
   else
    syslog( 4, "CmdCLass: %s does not register set", cmd_cls->name );;
   break;
  }
 }
 return rc;
}

canonical_original_code:
int cc_set(struct zw_api_ctx *ctx, unsigned char nodeid, unsigned char cls_type, void *val)
{
 struct _node *node = ((void *)0);
 struct cmd_class *cmd_cls = ((void *)0);
 int rc = -1;
 for(node = (&cmd_classes)->next; node != (&cmd_classes); node = node->next) {
  cmd_cls = (struct cmd_class *)node;
  if ( cmd_cls->type == cls_type ) {
   if ( cmd_cls->set )
    rc = cmd_cls->set( ctx, nodeid, val );
   else
    syslog( 4, "CmdCLass: %s does not register set", cmd_cls->name );;
   break;
  }
 }
 return rc;
}

variable_types:
val
void *


decompiled_code:
__int64 __fastcall cc_report(__int64 a1, unsigned __int8 a2, char a3, __int64 a4)
{
void **v5; // rax
__int64 (__fastcall *v6)(__int64, _QWORD, __int64); // rcx

v5 = (void **)cmd_classes;
if ( cmd_classes == &cmd_classes )
return 0xFFFFFFFFLL;
while ( *((_BYTE *)v5 + 24) != a3 )
{
v5 = (void **)*v5;
if ( v5 == &cmd_classes )
return 0xFFFFFFFFLL;
}
v6 = (__int64 (__fastcall *)(__int64, _QWORD, __int64))v5[8];
if ( v6 )
return v6(a1, a2, a4);
syslog(4, "CmdCLass: %s does not register report", (const char *)v5[2]);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, unsigned __int8 a2, char a3, __int64 a4)
{
void **v5; // rax
__int64 (__fastcall *v6)(__int64, _QWORD, __int64); // rcx

v5 = (void **)cmd_classes;
if ( cmd_classes == &cmd_classes )
return 0xFFFFFFFFLL;
while ( *((_BYTE *)v5 + 24) != a3 )
{
v5 = (void **)*v5;
if ( v5 == &cmd_classes )
return 0xFFFFFFFFLL;
}
v6 = (__int64 (__fastcall *)(__int64, _QWORD, __int64))v5[8];
if ( v6 )
return v6(a1, a2, a4);
syslog(4, "CmdCLass: %s does not register report", (const char *)v5[2]);
return 0xFFFFFFFFLL;
}

original_code:
int
cc_report( zw_api_ctx_S *ctx, u8 nodeid, const u8 cls_type, void *resp )
{
 list_node *node = ((void *)0);
 struct cmd_class *cmd_cls = ((void *)0);
 int rc = -1;
 for(node = (&cmd_classes)->next; node != (&cmd_classes); node = node->next) {
  cmd_cls = (struct cmd_class *)node;
  if ( cmd_cls->type == cls_type ) {
   if ( cmd_cls->report )
    rc = cmd_cls->report( ctx, nodeid, resp );
   else
    syslog( 4, "CmdCLass: %s does not register report", cmd_cls->name );;
   break;
  }
 }
 return rc;
}

canonical_original_code:
int cc_report(struct zw_api_ctx *ctx, unsigned char nodeid, unsigned char cls_type, void *resp)
{
 struct _node *node = ((void *)0);
 struct cmd_class *cmd_cls = ((void *)0);
 int rc = -1;
 for(node = (&cmd_classes)->next; node != (&cmd_classes); node = node->next) {
  cmd_cls = (struct cmd_class *)node;
  if ( cmd_cls->type == cls_type ) {
   if ( cmd_cls->report )
    rc = cmd_cls->report( ctx, nodeid, resp );
   else
    syslog( 4, "CmdCLass: %s does not register report", cmd_cls->name );;
   break;
  }
 }
 return rc;
}


decompiled_code:
__int64 __fastcall register_cmd_class(_QWORD *a1)
{
list_add_constprop_0(a1);
print_cmd_classes();
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func10(_QWORD *a1)
{
func1(a1);
func2();
return 1LL;
}

original_code:
int
register_cmd_class( struct cmd_class *ccls )
{
 int rc = 1;
 list_add((list_node *)&cmd_classes, (list_node *)ccls);
 print_cmd_classes();
 return rc;
}

canonical_original_code:
int register_cmd_class(struct cmd_class *ccls)
{
 int rc = 1;
 list_add((struct _node *)&cmd_classes, (struct _node *)ccls);
 print_cmd_classes();
 return rc;
}


decompiled_code:
__int64 __fastcall unregister_cmd_class(void **a1)
{
list_remove_constprop_0(a1);
print_cmd_classes();
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func11(void **a1)
{
func0(a1);
func2();
return 1LL;
}

original_code:
int
unregister_cmd_class( struct cmd_class *ccls)
{
 int rc = 1;
 list_remove((list_node *)&cmd_classes, (list_node *)ccls);
 print_cmd_classes();
 return rc;
}

canonical_original_code:
int unregister_cmd_class(struct cmd_class *ccls)
{
 int rc = 1;
 list_remove((struct _node *)&cmd_classes, (struct _node *)ccls);
 print_cmd_classes();
 return rc;
}


original_code:
static int
batt_get( zw_api_ctx_S *ctx, u8 nodeid, void *resp )
{
 u8 buff[1024];
 int rc;
 buff[0] = 0x13;
 buff[1] = nodeid;
 buff[2] = 2;
 buff[3] = 0x80;
 buff[4] = 0x02;
 buff[5] = 0x01 | 0x04;
 buff[6] = 3;
 rc = zw_send_request( ctx, buff, 7, nodeid, 1, 0x13 );
 return rc;
}

canonical_original_code:
static int batt_get(struct zw_api_ctx *ctx, unsigned char nodeid, void *resp)
{
 unsigned char buff[1024];
 int rc;
 buff[0] = 0x13;
 buff[1] = nodeid;
 buff[2] = 2;
 buff[3] = 0x80;
 buff[4] = 0x02;
 buff[5] = 0x01 | 0x04;
 buff[6] = 3;
 rc = zw_send_request( ctx, buff, 7, nodeid, 1, 0x13 );
 return rc;
}


decompiled_code:
__int64 __fastcall batt_proc_msg(__int64 a1, __int64 a2, unsigned __int8 a3)
{
unsigned int v5; // er12

syslog(7, "COMMAND_CLASS_BATTERY - processing message");
if ( *(_BYTE *)(a2 + 6) == 3 )
{
syslog(7, "BATTERY_REPORT: Battery level: %d", *(unsigned __int8 *)(a2 + 7));
v5 = *(unsigned __int8 *)(a2 + 7);
if ( (_BYTE)v5 == 0xFF )
syslog(7, "Battery low warning from node %d", a3);
if ( (unsigned int)zw_node_set_batt_level(a3, v5) )
syslog(7, "Setting node battery level failed");
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, unsigned __int8 a3)
{
unsigned int v5; // er12

syslog(7, "COMMAND_CLASS_BATTERY - processing message");
if ( *(_BYTE *)(a2 + 6) == 3 )
{
syslog(7, "BATTERY_REPORT: Battery level: %d", *(unsigned __int8 *)(a2 + 7));
v5 = *(unsigned __int8 *)(a2 + 7);
if ( (_BYTE)v5 == 0xFF )
syslog(7, "Battery low warning from node %d", a3);
if ( (unsigned int)zw_node_set_batt_level(a3, v5) )
syslog(7, "Setting node battery level failed");
}
return 0LL;
}

original_code:
static int
batt_proc_msg( zw_api_ctx_S *ctx, const u8* frame, u8 nodeid )
{
 int val = -1;
 syslog( 7, "COMMAND_CLASS_BATTERY - processing message" );;
 if ((unsigned char)frame[6] == 0x03) {
  syslog( 7, "BATTERY_REPORT: Battery level: %d",(unsigned char)frame[7] );;
  val = frame[ 7 ];
  if ((unsigned char)frame[7] == 0xff) {
   syslog( 7, "Battery low warning from node %d", nodeid );;
  }
  if ( 0 != zw_node_set_batt_level( nodeid, val ) )
   syslog( 7, "Setting node battery level failed" );;
 }
 return 0;
}

canonical_original_code:
static int batt_proc_msg(struct zw_api_ctx *ctx, unsigned char *frame, unsigned char nodeid)
{
 int val = -1;
 syslog( 7, "COMMAND_CLASS_BATTERY - processing message" );;
 if ((unsigned char)frame[6] == 0x03) {
  syslog( 7, "BATTERY_REPORT: Battery level: %d",(unsigned char)frame[7] );;
  val = frame[ 7 ];
  if ((unsigned char)frame[7] == 0xff) {
   syslog( 7, "Battery low warning from node %d", nodeid );;
  }
  if ( 0 != zw_node_set_batt_level( nodeid, val ) )
   syslog( 7, "Setting node battery level failed" );;
 }
 return 0;
}


decompiled_code:
__int64 batt_init()
{
return register_cmd_class(&batt);
}

canonical_decompiled_code:
__int64 func2()
{
return register_cmd_class(&batt);
}

original_code:
static void __attribute__ ((constructor)) batt_init( void )
{
 register_cmd_class( &batt );
}

canonical_original_code:
static void batt_init()
{
 register_cmd_class( &batt );
}


decompiled_code:
__int64 batt_exit()
{
return unregister_cmd_class(&batt);
}

canonical_decompiled_code:
__int64 func3()
{
return unregister_cmd_class(&batt);
}

original_code:
static void __attribute__ ((destructor)) batt_exit( void )
{
 unregister_cmd_class( &batt );
}

canonical_original_code:
static void batt_exit()
{
 unregister_cmd_class( &batt );
}


callback

original_code:
static int callback( void *udata, int argc, char **argv, char **col )
{
        int i;
 db_row_S *rows = (db_row_S *)udata;
 int len = 0;
 int rowidx = rows->nrows;
 rows->row = realloc( rows->row, (++rows->nrows * sizeof(char *)) );
 if ( rows->row == ((void *)0) ) {
  perror( "rows" );
  return 1;
 }
        for(i=0; i<argc; i++){
  len += strlen( col[i] ) + (argv[i] ? strlen(argv[i]) : strlen("NULL")) + 2;
                printf("%s = %s\n", col[i], argv[i] ? argv[i] : "NULL");
        }
 len++;
 rows->row[ rowidx ] = calloc( len, sizeof( char ));
 if ( rows->row[ rowidx ] == ((void *)0) ) {
  perror( "row" );
  return 1;
 }
        for(i=0; i<argc; i++){
  int slen = strlen( rows->row[ rowidx ] );
  snprintf( rows->row[ rowidx ] + slen, len - slen, "%s=%s|", col[i], argv[i] ? argv[i] : "NULL");
                printf("%s = %s\n", col[i], argv[i] ? argv[i] : "NULL");
        }
        printf("\n");
        return 0;
}

canonical_original_code:
static int callback(void *udata, int argc, char **argv, char **col)
{
        int i;
 struct _db_row *rows = (db_row_S *)udata;
 int len = 0;
 int rowidx = rows->nrows;
 rows->row = realloc( rows->row, (++rows->nrows * sizeof(char *)) );
 if ( rows->row == ((void *)0) ) {
  perror( "rows" );
  return 1;
 }
        for(i=0; i<argc; i++){
  len += strlen( col[i] ) + (argv[i] ? strlen(argv[i]) : strlen("NULL")) + 2;
                printf("%s = %s\n", col[i], argv[i] ? argv[i] : "NULL");
        }
 len++;
 rows->row[ rowidx ] = calloc( len, sizeof( char ));
 if ( rows->row[ rowidx ] == ((void *)0) ) {
  perror( "row" );
  return 1;
 }
        for(i=0; i<argc; i++){
  int slen = strlen( rows->row[ rowidx ] );
  snprintf( rows->row[ rowidx ] + slen, len - slen, "%s=%s|", col[i], argv[i] ? argv[i] : "NULL");
                printf("%s = %s\n", col[i], argv[i] ? argv[i] : "NULL");
        }
        printf("\n");
        return 0;
}

variable_types:
udata
void *

variable_types: argc int

variable_types:
argv
char **

variable_types:
col
char **

variable_types: i int

variable_types: struct _db_row { char **row; int nrows; } *

variable_types: len int

variable_types: rowidx int

variable_types: slen int


decompiled_code:
__int64 __fastcall db_exec(__int64 a1, __int64 a2)
{
unsigned int v2; // er12
const char *v4; // [rsp+8h] [rbp-10h] BYREF

v4 = 0LL;
v2 = sqlite3_exec(pHzrDb, a1, callback, a2, &v4);
if ( v2 )
{
syslog(2, "SQL error: %sn", v4);
sqlite3_free(v4);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
unsigned int v2; // er12
const char *v4; // [rsp+8h] [rbp-10h] BYREF

v4 = 0LL;
v2 = sqlite3_exec(pHzrDb, a1, callback, a2, &v4);
if ( v2 )
{
syslog(2, "SQL error: %sn", v4);
sqlite3_free(v4);
}
return v2;
}

original_code:
int db_exec( const char *query, db_row_S *result )
{
 char *err = 0;
 int rc;
 rc = sqlite3_exec( pHzrDb, query, callback, result, &err );
 if( 0 != rc ){
  syslog( 2, "SQL error: %sn", err );;
  sqlite3_free( err );
 }
 return rc;
}

canonical_original_code:
int db_exec(char *query, struct _db_row *result)
{
 char *err = 0;
 int rc;
 rc = sqlite3_exec( pHzrDb, query, callback, result, &err );
 if( 0 != rc ){
  syslog( 2, "SQL error: %sn", err );;
  sqlite3_free( err );
 }
 return rc;
}

variable_types:
query
char *

variable_types:
err
char *

user_defined_types: struct _db_row { char **row; int nrows; }


decompiled_code:
__int64 db_init()
{
__int64 result; // rax
const char *v1; // rax

result = sqlite3_open("/opt/zwave/bin/hzr.db", &pHzrDb);
if ( (_DWORD)result )
{
v1 = (const char *)sqlite3_errmsg(pHzrDb);
syslog(2, "Can't open database: %s", v1);
result = sqlite3_close(pHzrDb);
}
return result;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 result; // rax
const char *v1; // rax

result = sqlite3_open("/opt/zwave/bin/hzr.db", &pHzrDb);
if ( (_DWORD)result )
{
v1 = (const char *)sqlite3_errmsg(pHzrDb);
syslog(2, "Can't open database: %s", v1);
result = sqlite3_close(pHzrDb);
}
return result;
}

original_code:
static void __attribute__ ((constructor)) db_init( void )
{
 if( sqlite3_open( "/opt/zwave/bin/hzr.db", &pHzrDb ) ){
  syslog( 2, "Can't open database: %s", sqlite3_errmsg( pHzrDb ) );;
  sqlite3_close( pHzrDb );
 }
}

canonical_original_code:
static void db_init()
{
 if( sqlite3_open( "/opt/zwave/bin/hzr.db", &pHzrDb ) ){
  syslog( 2, "Can't open database: %s", sqlite3_errmsg( pHzrDb ) );;
  sqlite3_close( pHzrDb );
 }
}


decompiled_code:
__int64 db_exit()
{
return sqlite3_close(pHzrDb);
}

canonical_decompiled_code:
__int64 func3()
{
return sqlite3_close(pHzrDb);
}

original_code:
static void __attribute__ ((destructor)) db_exit( void )
{
 sqlite3_close( pHzrDb );
}

canonical_original_code:
static void db_exit()
{
 sqlite3_close( pHzrDb );
}


original_code:
static int
bin_sensor_get( zw_api_ctx_S *ctx, u8 nodeid, void *resp )
{
 u8 buff[1024];
 int rc;
 buff[0] = 0x13;
 buff[1] = nodeid;
 buff[2] = 2;
 buff[3] = 0x30;
 buff[4] = 0x02;
 buff[5] = 0x01 | 0x04;
 buff[6] = 3;
 rc = zw_send_request( ctx, buff, 7, nodeid, 1, 0x13 );
 return rc;
}

canonical_original_code:
static int bin_sensor_get(struct zw_api_ctx *ctx, unsigned char nodeid, void *resp)
{
 unsigned char buff[1024];
 int rc;
 buff[0] = 0x13;
 buff[1] = nodeid;
 buff[2] = 2;
 buff[3] = 0x30;
 buff[4] = 0x02;
 buff[5] = 0x01 | 0x04;
 buff[6] = 3;
 rc = zw_send_request( ctx, buff, 7, nodeid, 1, 0x13 );
 return rc;
}

variable_types: struct zw_api_ctx { int port; int node_id; } *

variable_types:
nodeid
unsigned char

variable_types:
buff
unsigned char [1024]


decompiled_code:
__int64 __fastcall bin_sensor_proc_msg(__int64 a1, unsigned __int8 *a2, unsigned __int8 a3)
{
syslog(7, "COMMAND_CLASS_SENSOR_BINARY - processing message");
if ( a2[6] == 3 )
{
syslog(7, "Got sensor report from node %i, level: %i", a2[3], a2[7]);
if ( (unsigned int)zw_node_set_state(a3, a2[7]) )
syslog(7, "Setting node bin sensor state failed");
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, unsigned __int8 *a2, unsigned __int8 a3)
{
syslog(7, "COMMAND_CLASS_SENSOR_BINARY - processing message");
if ( a2[6] == 3 )
{
syslog(7, "Got sensor report from node %i, level: %i", a2[3], a2[7]);
if ( (unsigned int)zw_node_set_state(a3, a2[7]) )
syslog(7, "Setting node bin sensor state failed");
}
return 0LL;
}

original_code:
static int
bin_sensor_proc_msg( zw_api_ctx_S *ctx, const u8* frame, u8 nodeid )
{
 int val = -1;
 syslog( 7, "COMMAND_CLASS_SENSOR_BINARY - processing message" );;
 if (frame[6] == 0x03) {
  syslog( 7, "Got sensor report from node %i, level: %i",(unsigned char)frame[3],(unsigned char)frame[7] );;
  val = frame[ 7 ];
  if ( 0 != zw_node_set_state( nodeid, val ) )
   syslog( 7, "Setting node bin sensor state failed" );;
 }
 return 0;
}

canonical_original_code:
static int bin_sensor_proc_msg(struct zw_api_ctx *ctx, unsigned char *frame, unsigned char nodeid)
{
 int val = -1;
 syslog( 7, "COMMAND_CLASS_SENSOR_BINARY - processing message" );;
 if (frame[6] == 0x03) {
  syslog( 7, "Got sensor report from node %i, level: %i",(unsigned char)frame[3],(unsigned char)frame[7] );;
  val = frame[ 7 ];
  if ( 0 != zw_node_set_state( nodeid, val ) )
   syslog( 7, "Setting node bin sensor state failed" );;
 }
 return 0;
}

variable_types:
frame
unsigned char *

variable_types: val int


decompiled_code:
__int64 bin_sensor_init()
{
return register_cmd_class(&bin_sensor);
}

canonical_decompiled_code:
__int64 func2()
{
return register_cmd_class(&bin_sensor);
}

original_code:
static void __attribute__ ((constructor)) bin_sensor_init( void )
{
 register_cmd_class( &bin_sensor );
}

canonical_original_code:
static void bin_sensor_init()
{
 register_cmd_class( &bin_sensor );
}


decompiled_code:
__int64 bin_sensor_exit()
{
return unregister_cmd_class(&bin_sensor);
}

canonical_decompiled_code:
__int64 func3()
{
return unregister_cmd_class(&bin_sensor);
}

original_code:
static void __attribute__ ((destructor)) bin_sensor_exit( void )
{
 unregister_cmd_class( &bin_sensor );
}

canonical_original_code:
static void bin_sensor_exit()
{
 unregister_cmd_class( &bin_sensor );
}


calculate

original_code:
double
calculate(const struct rusage *b, const struct rusage *a)
{
    if (b == ((void *)0) || a == ((void *)0))
        return 0;
    else
        return ((((a->ru_utime.tv_sec * 1000000 + a->ru_utime.tv_usec) -
                 (b->ru_utime.tv_sec * 1000000 + b->ru_utime.tv_usec)) +
                ((a->ru_stime.tv_sec * 1000000 + a->ru_stime.tv_usec) -
                 (b->ru_stime.tv_sec * 1000000 + b->ru_stime.tv_usec)))
                / 1000000.0);
}

canonical_original_code:
double calculate(struct rusage *b, struct rusage *a)
{
    if (b == ((void *)0) || a == ((void *)0))
        return 0;
    else
        return ((((a->ru_utime.tv_sec * 1000000 + a->ru_utime.tv_usec) -
                 (b->ru_utime.tv_sec * 1000000 + b->ru_utime.tv_usec)) +
                ((a->ru_stime.tv_sec * 1000000 + a->ru_stime.tv_usec) -
                 (b->ru_stime.tv_sec * 1000000 + b->ru_stime.tv_usec)))
                / 1000000.0);
}

variable_types: struct rusage { struct timeval { long tv_sec; long tv_usec; } ru_utime; struct timeval ru_stime; union { long ru_maxrss; long __ru_maxrss_word; }; union { long ru_ixrss; long __ru_ixrss_word; }; union { long ru_idrss; long __ru_idrss_word; }; union { long ru_isrss; long __ru_isrss_word; }; union { long ru_minflt; long __ru_minflt_word; }; union { long ru_majflt; long __ru_majflt_word; }; union { long ru_nswap; long __ru_nswap_word; }; union { long ru_inblock; long __ru_inblock_word; }; union { long ru_oublock; long __ru_oublock_word; }; union { long ru_msgsnd; long __ru_msgsnd_word; }; union { long ru_msgrcv; long __ru_msgrcv_word; }; union { long ru_nsignals; long __ru_nsignals_word; }; union { long ru_nvcsw; long __ru_nvcsw_word; }; union { long ru_nivcsw; long __ru_nivcsw_word; }; } *

variable_types:
a
struct rusage *

return_type: double

user_defined_types: struct rusage { struct timeval ru_utime; struct timeval ru_stime; union { long ru_maxrss; long __ru_maxrss_word; }; union { long ru_ixrss; long __ru_ixrss_word; }; union { long ru_idrss; long __ru_idrss_word; }; union { long ru_isrss; long __ru_isrss_word; }; union { long ru_minflt; long __ru_minflt_word; }; union { long ru_majflt; long __ru_majflt_word; }; union { long ru_nswap; long __ru_nswap_word; }; union { long ru_inblock; long __ru_inblock_word; }; union { long ru_oublock; long __ru_oublock_word; }; union { long ru_msgsnd; long __ru_msgsnd_word; }; union { long ru_msgrcv; long __ru_msgrcv_word; }; union { long ru_nsignals; long __ru_nsignals_word; }; union { long ru_nvcsw; long __ru_nvcsw_word; }; union { long ru_nivcsw; long __ru_nivcsw_word; }; }


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
char v5[9]; // [rsp+Fh] [rbp-9h] BYREF

v3 = time(0LL);
srand(v3);
pdeckInit(520LL, argv);
pdeckShuffle();
while ( (unsigned __int8)pdeckGet(v5) )
printf("%d\n", (unsigned __int8)v5[0]);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
char v5[9]; // [rsp+Fh] [rbp-9h] BYREF

v3 = time(0LL);
srand(v3);
func7(520LL, argv);
func9();
while ( (unsigned __int8)func10(v5) )
printf("%d\n", (unsigned __int8)v5[0]);
return 0;
}

original_code:
int main ( int argc , char *argv[] ){
 pdeckCardType card;
 srand( (unsigned)time(((void *)0)) );
 pdeckInit( );
 pdeckShuffle( 52 * 10 );
 for( ; pdeckGet( &card ) ; printf( "%d\n" , (unsigned int)card ) );
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[]){
 unsigned char card;
 srand( (unsigned int)time(((void *)0)) );
 pdeckInit( );
 pdeckShuffle( 52 * 10 );
 for( ; pdeckGet( &card ) ; printf( "%d\n" , (unsigned int)card ) );
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
card
unsigned char


pdeckInit

decompiled_code:
__int64 pdeckInit()
{
__int64 result; // rax
char v1; // dl

for ( result = 0LL; result != -52; rest[result] = v1 )
v1 = result-- + 51;
rest[0] = 52;
return result;
}

canonical_decompiled_code:
__int64 func7()
{
__int64 result; // rax
char v1; // dl

for ( result = 0LL; result != -52; rest[result] = v1 )
v1 = result-- + 51;
rest[0] = 52;
return result;
}

original_code:
void pdeckInit( void ){
 pdeckCardType i;
 for( i=52 ; i-- ; deck[i] = i );
 rest = 52;
}

canonical_original_code:
void pdeckInit(){
 unsigned char i;
 for( i=52 ; i-- ; deck[i] = i );
 rest = 52;
}

variable_types:
i
unsigned char


pdeckSwap

decompiled_code:
__int64 __fastcall pdeckSwap(_BYTE *a1, _BYTE *a2)
{
__int64 result; // rax

result = (unsigned __int8)*a1;
*a1 = *a2;
*a2 = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(_BYTE *a1, _BYTE *a2)
{
__int64 result; // rax

result = (unsigned __int8)*a1;
*a1 = *a2;
*a2 = result;
return result;
}

original_code:
void pdeckSwap( pdeckCardType *pCard1 , pdeckCardType *pCard2 ){
 pdeckCardType cardTemp = *pCard1;
 *pCard1 = *pCard2;
 *pCard2 = cardTemp;
}

canonical_original_code:
void pdeckSwap(unsigned char *pCard1, unsigned char *pCard2){
 unsigned char cardTemp = *pCard1;
 *pCard1 = *pCard2;
 *pCard2 = cardTemp;
}

variable_types:
cardTemp
unsigned char


pdeckShuffle

decompiled_code:
void __fastcall pdeckShuffle(int a1)
{
int v1; // ebp
int v2; // er12
int v3; // ebx
int v4; // eax

v1 = (unsigned __int8)rest[0];
if ( (_WORD)a1 )
{
v2 = a1 - 1;
do
{
--v2;
v3 = rand() % v1;
v4 = rand();
pdeckSwap((_BYTE *)((unsigned __int8)v3 + 4210816LL), &deck[(unsigned __int8)(v4 % v1)]);
}
while ( (_WORD)v2 != 0xFFFF );
}
}

canonical_decompiled_code:
void __fastcall func9(int a1)
{
int v1; // ebp
int v2; // er12
int v3; // ebx
int v4; // eax

v1 = (unsigned __int8)rest[0];
if ( (_WORD)a1 )
{
v2 = a1 - 1;
do
{
--v2;
v3 = rand() % v1;
v4 = rand();
func8((_BYTE *)((unsigned __int8)v3 + 4210816LL), &deck[(unsigned __int8)(v4 % v1)]);
}
while ( (_WORD)v2 != 0xFFFF );
}
}

original_code:
void pdeckShuffle( unsigned short shuffle ){
 pdeckCardType i , j;
 for( ; shuffle-- ; pdeckSwap( &deck[i] , &deck[j] ) ) {
  i = rand() % rest;
  j = rand() % rest;
 }
}

canonical_original_code:
void pdeckShuffle(unsigned short shuffle){
 unsigned char i; unsigned char j;
 for( ; shuffle-- ; pdeckSwap( &deck[i] , &deck[j] ) ) {
  i = rand() % rest;
  j = rand() % rest;
 }
}

variable_types:
shuffle
unsigned short

variable_types:
j
unsigned char


pdeckGet

decompiled_code:
__int64 __fastcall pdeckGet(_BYTE *a1)
{
__int64 result; // rax

result = (unsigned __int8)rest[0];
if ( rest[0] )
{
--rest[0];
*a1 = deck[(unsigned __int8)(result - 1)];
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(_BYTE *a1)
{
__int64 result; // rax

result = (unsigned __int8)rest[0];
if ( rest[0] )
{
--rest[0];
*a1 = deck[(unsigned __int8)(result - 1)];
}
return result;
}

original_code:
pdeckCardType pdeckGet( pdeckCardType *pCard ){
 pdeckCardType res = rest;
 res = rest;
 if( res ) {
  *pCard = deck[--rest];
 }
 return res;
}

canonical_original_code:
unsigned char pdeckGet(unsigned char *pCard){
 unsigned char res = rest;
 res = rest;
 if( res ) {
  *pCard = deck[--rest];
 }
 return res;
}

variable_types:
pCard
unsigned char *

variable_types:
res
unsigned char

return_type: unsigned char


init

repo: pollow/Dict

decompiled_code:
__int64 init()
{
readWords();
trieInit();
layoutInit();
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
func0();
trieInit();
layoutInit();
return 0LL;
}

original_code:
int init() {
    readWords();
    trieInit();
    layoutInit();
    return 0;
}


test

decompiled_code:
unsigned int **test()
{
unsigned int **result; // rax

readWords();
trieInit();
wordsNum = 20;
lookupProcess("ap*l?");
result = (unsigned int **)trielist;
if ( trielist )
{
do
{
printf("%s %d\n", *((const char **)*result + 1), **result);
result = *(unsigned int ***)(trielist + 8LL);
trielist = result;
}
while ( result );
}
return result;
}

canonical_decompiled_code:
unsigned int **func2()
{
unsigned int **result; // rax

func0();
trieInit();
wordsNum = 20;
lookupProcess("ap*l?");
result = (unsigned int **)trielist;
if ( trielist )
{
do
{
printf("%s %d\n", *((const char **)*result + 1), **result);
result = *(unsigned int ***)(trielist + 8LL);
trielist = result;
}
while ( result );
}
return result;
}

original_code:
void test() {
    readWords();
    trieInit();
    wordsNum = 20;
    lookupProcess("ap*l?");
    while(trielist) {
        int _index_ = trielist->key->index;
        printf("%s %d\n",(trielist->key)->word,_index_);
        trielist = trielist->next;
    }
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
init(argc, argv, envp);
layoutProcess();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func1(argc, argv, envp);
layoutProcess();
return 0;
}

original_code:
int main() {
    if(0) {
        test();
    } else {
    init();
    layoutProcess();
    }
    return 0;
}


trieNodeInit

decompiled_code:
__int64 __fastcall trieNodeInit(_DWORD *a1)
{
int v1; // eax
unsigned __int64 v2; // rdi
unsigned int v3; // ecx
__int64 result; // rax

*a1 = -1;
v1 = (int)a1;
v2 = (unsigned __int64)(a1 + 6);
*(_QWORD *)(v2 - 16) = 0LL;
*(_QWORD *)(v2 - 8) = 0LL;
*(_QWORD *)(v2 + 112) = 0LL;
v2 &= 0xFFFFFFFFFFFFFFF8LL;
v3 = v1 - v2 + 144;
result = 0LL;
memset((void *)v2, 0, 8LL * (v3 >> 3));
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(_DWORD *a1)
{
int v1; // eax
unsigned __int64 v2; // rdi
unsigned int v3; // ecx
__int64 result; // rax

*a1 = -1;
v1 = (int)a1;
v2 = (unsigned __int64)(a1 + 6);
*(_QWORD *)(v2 - 16) = 0LL;
*(_QWORD *)(v2 - 8) = 0LL;
*(_QWORD *)(v2 + 112) = 0LL;
v2 &= 0xFFFFFFFFFFFFFFF8LL;
v3 = v1 - v2 + 144;
result = 0LL;
memset((void *)v2, 0, 8LL * (v3 >> 3));
return result;
}

original_code:
void trieNodeInit(struct trieNode *a) {
    a->index = -1;
    a->word = ((void *)0);
    memset(a->son,0, sizeof(a->son));
}

variable_types: struct trieNode { int index; char *word; struct trieNode *son[16]; } *


newNode

decompiled_code:
__int64 newNode()
{
_DWORD *v0; // rax
__int64 v1; // r8

v0 = malloc(0x90uLL);
trieNodeInit(v0);
return v1;
}

canonical_decompiled_code:
__int64 func1()
{
_DWORD *v0; // rax
__int64 v1; // r8

v0 = malloc(0x90uLL);
func0(v0);
return v1;
}

original_code:
struct trieNode *newNode() {
    struct trieNode *tmp = (struct trieNode *)malloc(sizeof(struct trieNode));
    trieNodeInit(tmp);
    return tmp;
}

return_type: struct trieNode { int index; char *word; struct trieNode *son[16]; } *


trieInsert

original_code:
struct trieNode *trieInsert(struct trieNode *u, int *q, const int index) {
    while (*q != -1) {
        if(u->son[*q] == ((void *)0)) u->son[*q] = newNode();
        u = u->son[*q];
        q++;
    }
    u->index = index;
    u->word = words[index];
    return u;
}

canonical_original_code:
struct trieNode *trieInsert(struct trieNode *u, int *q, int index) {
    while (*q != -1) {
        if(u->son[*q] == ((void *)0)) u->son[*q] = newNode();
        u = u->son[*q];
        q++;
    }
    u->index = index;
    u->word = words[index];
    return u;
}

variable_types:
u
struct trieNode *

variable_types:
q
int *

variable_types: index int

user_defined_types: struct trieNode { int index; char *word; struct trieNode *son[16]; }


trieSearch

original_code:
struct trieNode *trieSearch(struct trieNode *u, int *ch) {
    if(*ch == -1) {
        return u;
    } else {
        if(u->son[*ch] == ((void *)0)) {
            return ((void *)0);
        } else {
            return trieSearch(u->son[*ch],ch+1);
        }
    }
}

variable_types:
ch
int *


delTrieList

decompiled_code:
void __fastcall delTrieList(void *ptr)
{
if ( ptr )
{
delTrieList(*((void **)ptr + 1));
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func5(void *ptr)
{
if ( ptr )
{
func5(*((void **)ptr + 1));
free(ptr);
}
}

original_code:
void delTrieList(struct trieList *u) {
    if(u == ((void *)0)) {
        return;
    } else {
        delTrieList(u->next);
        free(u);
    }
}

variable_types: struct trieList { struct trieNode { int index; char *word; struct trieNode *son[16]; } *key; struct trieList *next; } *

user_defined_types: struct trieList { struct trieNode *key; struct trieList *next; }


emptyTrieList

decompiled_code:
void emptyTrieList()
{
delTrieList(trielist);
trielist = 0LL;
}

canonical_decompiled_code:
void func6()
{
func5(trielist);
trielist = 0LL;
}

original_code:
void emptyTrieList() {
    delTrieList(trielist);
    trielist = ((void *)0);
}


trieGetList

original_code:
void trieGetList(struct trieNode *u, int *num) {
    if(u->index != -1) {
        struct trieList *tmp = (struct trieList *) malloc(sizeof(struct trieList));
        tmp->key = u;
        tmp->next = trielist;
        trielist = tmp;
        (*num)--;
        if(!*num) return;
    }
    for(int i=0;i<16;i++) {
            if(u->son[i] && *num) trieGetList(u->son[i],num);
    }
}

canonical_original_code:
void trieGetList(struct trieNode *u, int *num) {
    if(u->index != -1) {
        struct trieList *tmp = (struct trieList *) malloc(sizeof(struct trieList));
        tmp->key = u;
        tmp->next = trielist;
        trielist = tmp;
        (*num)--;
        if(!*num) return;
    }
    for(int i = 0;i<16;i++) {
            if(u->son[i] && *num) trieGetList(u->son[i],num);
    }
}

variable_types:
num
int *

variable_types: i int


trieInit

decompiled_code:
void trieInit()
{
__int64 i; // rbx
const char *v1; // rsi
int v2[202]; // [rsp+0h] [rbp-328h] BYREF

for ( i = 0LL; i != 91286; ++i )
{
v1 = (const char *)*((_QWORD *)&words + i);
if ( v1 )
{
hexConvert((__int64)v2, v1);
*((_QWORD *)&wordsNode + i) = trieInsert((__int64)&trieRoot, v2, i);
}
}
}

canonical_decompiled_code:
void func8()
{
__int64 i; // rbx
const char *v1; // rsi
int v2[202]; // [rsp+0h] [rbp-328h] BYREF

for ( i = 0LL; i != 91286; ++i )
{
v1 = (const char *)*((_QWORD *)&words + i);
if ( v1 )
{
func2((__int64)v2, v1);
*((_QWORD *)&wordsNode + i) = func3((__int64)&trieRoot, v2, i);
}
}
}

original_code:
void trieInit() {
    int hexStr[200];
    for(int i=0;i<91286;i++) {
        if(words[i] == ((void *)0)) {
            continue;
        }
        hexConvert(hexStr,words[i]);
        wordsNode[i] = trieInsert(&trieRoot,hexStr,i);
    }
}

canonical_original_code:
void trieInit() {
    int hexStr[200];
    for(int i = 0;i<91286;i++) {
        if(words[i] == ((void *)0)) {
            continue;
        }
        hexConvert(hexStr,words[i]);
        wordsNode[i] = trieInsert(&trieRoot,hexStr,i);
    }
}

variable_types:
hexStr
int [200]


min

decompiled_code:
__int64 __fastcall min(unsigned int a1, unsigned int a2)
{
__int64 result; // rax

result = a2;
if ( (int)a1 <= (int)a2 )
result = a1;
return result;
}

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

result = a2;
if ( (int)a1 <= (int)a2 )
result = a1;
return result;
}

original_code:
int min(int a, int b) {
    if ( a < b ) return a;
    else return b;
}

variable_types: a int

variable_types: b int


cleanHeap

decompiled_code:
__int64 cleanHeap()
{
__int64 result; // rax

result = 0LL;
memset(&heap, 0, 0x640uLL);
return result;
}

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

result = 0LL;
memset(&heap, 0, 0x640uLL);
return result;
}

original_code:
void cleanHeap() {
    memset(heap,0,sizeof(heap));
    heapSize = 1;
}


popHeap

decompiled_code:
__int64 popHeap()
{
unsigned int *v0; // rdi
unsigned int v1; // er12
int v2; // edi
__int64 v3; // r8
__int64 v4; // r9
int i; // eax
__int64 v6; // rcx
int v7; // esi
__int64 v8; // rdx
int v9; // ecx

v0 = (unsigned int *)heap[1];
v1 = *v0;
free(v0);
LODWORD(heapSize) = (_DWORD)heapSize - 1;
v2 = (int)heapSize;
v3 = heap[(int)heapSize];
heap[1] = v3;
if ( v2 > 2 )
{
v4 = 1LL;
for ( i = 2; ; i *= 2 )
{
v7 = i + 1;
v8 = heap[i];
v9 = *(_DWORD *)(v8 + 4);
if ( v2 > i + 1 && *(_DWORD *)(heap[v7] + 4LL) > v9 )
{
v9 = *(_DWORD *)(heap[v7] + 4LL);
v8 = heap[v7];
++i;
}
if ( *(_DWORD *)(v3 + 4) <= v9 )
{
heap[v4] = v8;
heap[i] = v3;
}
if ( v2 <= 2 * i )
break;
v6 = i;
v4 = i;
v3 = heap[v6];
}
}
return v1;
}

canonical_decompiled_code:
__int64 func2()
{
unsigned int *v0; // rdi
unsigned int v1; // er12
int v2; // edi
__int64 v3; // r8
__int64 v4; // r9
int i; // eax
__int64 v6; // rcx
int v7; // esi
__int64 v8; // rdx
int v9; // ecx

v0 = (unsigned int *)heap[1];
v1 = *v0;
free(v0);
LODWORD(heapSize) = (_DWORD)heapSize - 1;
v2 = (int)heapSize;
v3 = heap[(int)heapSize];
heap[1] = v3;
if ( v2 > 2 )
{
v4 = 1LL;
for ( i = 2; ; i *= 2 )
{
v7 = i + 1;
v8 = heap[i];
v9 = *(_DWORD *)(v8 + 4);
if ( v2 > i + 1 && *(_DWORD *)(heap[v7] + 4LL) > v9 )
{
v9 = *(_DWORD *)(heap[v7] + 4LL);
v8 = heap[v7];
++i;
}
if ( *(_DWORD *)(v3 + 4) <= v9 )
{
heap[v4] = v8;
heap[i] = v3;
}
if ( v2 <= 2 * i )
break;
v6 = i;
v4 = i;
v3 = heap[v6];
}
}
return v1;
}

original_code:
int popHeap() {
    int ans = heap[1]->key, t=1;
    struct heapStruct *tmp;
    free(heap[1]);
    heap[1] = heap[--heapSize];
    while( t<<1 < heapSize ) {
        int k = t << 1;
        if(( k+1 < heapSize ) && (heap[ k ]->value < heap[ k + 1 ]->value)) k++;
        if(heap[ t ]->value <= heap[ k ]->value) {
            tmp = heap[ t ];
            heap[ t ] = heap[ k ];
            heap[ k ] = tmp;
        }
        t = k;
    }
    return ans;
}

canonical_original_code:
int popHeap() {
    int ans = heap[1]->key; int t = 1;
    struct heapStruct *tmp;
    free(heap[1]);
    heap[1] = heap[--heapSize];
    while( t<<1 < heapSize ) {
        int k = t << 1;
        if(( k+1 < heapSize ) && (heap[ k ]->value < heap[ k + 1 ]->value)) k++;
        if(heap[ t ]->value <= heap[ k ]->value) {
            tmp = heap[ t ];
            heap[ t ] = heap[ k ];
            heap[ k ] = tmp;
        }
        t = k;
    }
    return ans;
}

variable_types: ans int

variable_types: t int

variable_types:
tmp
struct heapStruct { int key; } *

variable_types: k int


addHeap

decompiled_code:
__int64 __fastcall addHeap(int a1, int a2)
{
int v3; // ebx
__int64 result; // rax
_DWORD *v5; // rsi
__int64 v6; // rax
__int64 v7; // rcx

v3 = (int)heapSize;
if ( wordsNum + 1 >= (int)heapSize || (result = heap[1], *(_DWORD *)(result + 4) > a2) )
{
LODWORD(heapSize) = (_DWORD)heapSize + 1;
v5 = malloc(8uLL);
heap[v3] = v5;
*v5 = a1;
v5[1] = a2;
if ( v3 > 1 )
{
do
{
v6 = v3;
v3 >>= 1;
v7 = heap[v3];
if ( *(_DWORD *)(v7 + 4) >= a2 )
break;
heap[v6] = v7;
heap[v3] = v5;
}
while ( v3 != 1 );
}
result = (unsigned int)(wordsNum + 2);
if ( (int)result < (int)heapSize )
result = popHeap();
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, int a2)
{
int v3; // ebx
__int64 result; // rax
_DWORD *v5; // rsi
__int64 v6; // rax
__int64 v7; // rcx

v3 = (int)heapSize;
if ( wordsNum + 1 >= (int)heapSize || (result = heap[1], *(_DWORD *)(result + 4) > a2) )
{
LODWORD(heapSize) = (_DWORD)heapSize + 1;
v5 = malloc(8uLL);
heap[v3] = v5;
*v5 = a1;
v5[1] = a2;
if ( v3 > 1 )
{
do
{
v6 = v3;
v3 >>= 1;
v7 = heap[v3];
if ( *(_DWORD *)(v7 + 4) >= a2 )
break;
heap[v6] = v7;
heap[v3] = v5;
}
while ( v3 != 1 );
}
result = (unsigned int)(wordsNum + 2);
if ( (int)result < (int)heapSize )
result = func2();
}
return result;
}

original_code:
void addHeap(int key, int value) {
    if(heapSize > wordsNum+1 && value >= heap[1]->value) return;
    int t = heapSize++;
    struct heapStruct *tmp;
    heap[t] = (struct heapStruct *) malloc(sizeof(struct heapStruct));
    heap[t]->key = key;
    heap[t]->value = value;
    while(t > 1) {
        if(heap[ t ]->value > heap[ t >> 1 ]->value) {
            tmp = heap[t];
            heap[t] = heap[t >> 1];
            heap[t >> 1] = tmp;
        } else break;
        t = t >> 1;
    }
    if( heapSize > wordsNum+2 ) {
        popHeap();
    }
    return;
}

variable_types: key int

variable_types: value int


MED

original_code:
void MED(char *a) {
    int f[100][100];
    cleanHeap();
    for( int t = 0; t < 91286; t++ ) {
        memset(f,0,sizeof(f));
        char *b = words[t];
        if( !b ) continue;
        size_t lena = strlen(a), lenb = strlen(b);
        for(size_t i = 1; i <= lena; i++) f[i][0] = f[i-1][0]+5;
        for(size_t i = 1; i <= lenb; i++) f[0][i] = f[0][i-1]+3;
        for(size_t i = 1; i <= lena; i++)
            for(size_t j=1; j <= lenb; j++) {
                f[i][j] = min(f[i-1][j]+5,f[i][j-1]+3);
                if( toupper(a[i-1]) != toupper(b[j-1]) ) {
                    f[i][j] = min(f[i][j],f[i-1][j-1]+1);
                } else {
                    f[i][j] = min(f[i][j],f[i-1][j-1]);
                }
            }
        addHeap(t,f[lena][lenb]);
    }
    return;
}

canonical_original_code:
void MED(char *a) {
    int f[100][100];
    cleanHeap();
    for( int t = 0; t < 91286; t++ ) {
        memset(f,0,sizeof(f));
        char *b = words[t];
        if( !b ) continue;
        unsigned long lena = strlen(a); unsigned long lenb = strlen(b);
        for(unsigned long i = 1; i <= lena; i++) f[i][0] = f[i-1][0]+5;
        for(unsigned long i = 1; i <= lenb; i++) f[0][i] = f[0][i-1]+3;
        for(unsigned long i = 1; i <= lena; i++)
            for(unsigned long j = 1; j <= lenb; j++) {
                f[i][j] = min(f[i-1][j]+5,f[i][j-1]+3);
                if( toupper(a[i-1]) != toupper(b[j-1]) ) {
                    f[i][j] = min(f[i][j],f[i-1][j-1]+1);
                } else {
                    f[i][j] = min(f[i][j],f[i-1][j-1]);
                }
            }
        addHeap(t,f[lena][lenb]);
    }
    return;
}

variable_types:
a
char *

variable_types:
b
char *

variable_types:
lena
unsigned long

variable_types:
lenb
unsigned long

variable_types:
i
unsigned long

variable_types:
j
unsigned long


trieValid

decompiled_code:
__int64 __fastcall trieValid(const char *a1)
{
int v1; // ecx
__int64 v2; // rax
char v3; // dl

v1 = strlen(a1);
v2 = 0LL;
do
{
if ( v1 < (int)v2 )
return 1LL;
v3 = a1[v2++];
}
while ( v3 != 42 && v3 != 63 );
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
int v1; // ecx
__int64 v2; // rax
char v3; // dl

v1 = strlen(a1);
v2 = 0LL;
do
{
if ( v1 < (int)v2 )
return 1LL;
v3 = a1[v2++];
}
while ( v3 != 42 && v3 != 63 );
return 0xFFFFFFFFLL;
}

original_code:
int trieValid(char *str) {
    for (int i = 0; i <= (int)strlen(str); i++) {
        if(str[i] == '*' || str[i] == '?') {
            return -1;
        }
    }
    return 1;
}

variable_types:
str
char *


isCapable

decompiled_code:
__int64 __fastcall isCapable(const char *a1, const char *a2)
{
int v3; // ebp
__int64 v4; // rbx
int v5; // esi
char v6; // al
__int64 v7; // rbx
int v8; // eax
unsigned int v9; // edx
int v10; // er13
const char *v11; // r14
int v12; // ebp
const char *v13; // rsi
unsigned int v14; // eax
unsigned __int8 v16; // [rsp+1h] [rbp-29h]

if ( a2 )
{
v3 = 0;
v4 = 0LL;
v5 = strlen(a1);
while ( 1 )
{
if ( v5 < (int)v4 )
{
LOBYTE(v9) = (unsigned int)strlen(a2) + 1 == v3;
return v9;
}
v6 = a1[v4];
if ( v6 != 42 && v6 != 63 )
break;
if ( v6 == 63 )
goto LABEL_4;
if ( v6 == 42 )
{
v7 = (__int64)&a1[v4 + 1];
v8 = strlen(a2);
v9 = 0;
v10 = v8;
v11 = &a2[v3];
v12 = v3 - (_DWORD)v11;
while ( v10 >= v12 + (int)v11 )
{
v13 = v11;
v16 = v9;
++v11;
v14 = isCapable(v7, v13);
v9 = v16;
if ( (_BYTE)v14 )
return v14;
}
return v9;
}
LABEL_5:
++v4;
}
if ( a2[v3] != v6 )
return 0LL;
LABEL_4:
++v3;
goto LABEL_5;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1, const char *a2)
{
int v3; // ebp
__int64 v4; // rbx
int v5; // esi
char v6; // al
__int64 v7; // rbx
int v8; // eax
unsigned int v9; // edx
int v10; // er13
const char *v11; // r14
int v12; // ebp
const char *v13; // rsi
unsigned int v14; // eax
unsigned __int8 v16; // [rsp+1h] [rbp-29h]

if ( a2 )
{
v3 = 0;
v4 = 0LL;
v5 = strlen(a1);
while ( 1 )
{
if ( v5 < (int)v4 )
{
LOBYTE(v9) = (unsigned int)strlen(a2) + 1 == v3;
return v9;
}
v6 = a1[v4];
if ( v6 != 42 && v6 != 63 )
break;
if ( v6 == 63 )
goto LABEL_4;
if ( v6 == 42 )
{
v7 = (__int64)&a1[v4 + 1];
v8 = strlen(a2);
v9 = 0;
v10 = v8;
v11 = &a2[v3];
v12 = v3 - (_DWORD)v11;
while ( v10 >= v12 + (int)v11 )
{
v13 = v11;
v16 = v9;
++v11;
v14 = func1(v7, v13);
v9 = v16;
if ( (_BYTE)v14 )
return v14;
}
return v9;
}
LABEL_5:
++v4;
}
if ( a2[v3] != v6 )
return 0LL;
LABEL_4:
++v3;
goto LABEL_5;
}
return 0LL;
}

original_code:
_Bool isCapable(char *a, char *b) {
    if(!b) return 0;
    int j = 0;
    for (int i = 0; i <= (int)strlen(a); i++) {
        if(a[i] != '*' && a[i] != '?') {
            if(a[i] != b[j]) return 0;
            j++;
        } else if(a[i] == '?') {
            j++;
        } else if(a[i] == '*') {
            for (int k = j; k <= (int)strlen(b); k++) {
                if (isCapable(a+(i+1),b+k)) return 1;
            }
            return 0;
        }
    }
    if (j == (int)strlen(b) + 1) return 1;
    else return 0;
}

variable_types: j int

variable_types: i int