decompiled_code:
void *__fastcall exit_1_svc(char *s1)
{
__int64 v1; // r12
const char **v2; // r14
__int64 v3; // rbx
const char **v4; // rbp

if ( !initialized )
init_data_structures();
v1 = *(unsigned int *)clients;
if ( (_DWORD)v1 )
{
v2 = (const char **)*((_QWORD *)clients + 1);
v3 = 0LL;
v4 = v2;
while ( strcmp(s1, *v4) || (int)v1 - 1 <= (unsigned int)v3 )
{
++v3;
v4 += 3;
if ( v3 == v1 )
goto LABEL_8;
}
memmove(v4, &v2[3 * v3 + 3], 24 * v1 - 1 - v3);
}
LABEL_8:
mcExit(s1);
return &result_1;
}

canonical_decompiled_code:
void *__fastcall func10(char *s1)
{
__int64 v1; // r12
const char **v2; // r14
__int64 v3; // rbx
const char **v4; // rbp

if ( !initialized )
func1();
v1 = *(unsigned int *)clients;
if ( (_DWORD)v1 )
{
v2 = (const char **)*((_QWORD *)clients + 1);
v3 = 0LL;
v4 = v2;
while ( strcmp(s1, *v4) || (int)v1 - 1 <= (unsigned int)v3 )
{
++v3;
v4 += 3;
if ( v3 == v1 )
goto LABEL_8;
}
memmove(v4, &v2[3 * v3 + 3], 24 * v1 - 1 - v3);
}
LABEL_8:
func6(s1);
return &result_1;
}

original_code:
int *exit_1_svc(uname *user, struct svc_req *rqstp) {
 static int result;
 int i;
 if (!initialized) {
  init_data_structures();
 }
 for (i=0; i<clients->clientlist.clientlist_len; i++) {
  if(strcmp(user, clients->clientlist.clientlist_val[i].userName) == 0) {
   if(i<(clients->clientlist.clientlist_len-1)) {
    memmove(&(clients->clientlist.clientlist_val[i]),
        &(clients->clientlist.clientlist_val[i+1]),
        sizeof(cname)*clients->clientlist.clientlist_len-1-i);
    break;
   }
  }
 }
 mcExit(user);
 return(&result);
}

canonical_original_code:
int *exit_1_svc(char **user, struct svc_req *rqstp) {
 static int result;
 int i;
 if (!initialized) {
  init_data_structures();
 }
 for (i=0; i<clients->clientlist.clientlist_len; i++) {
  if(strcmp(user, clients->clientlist.clientlist_val[i].userName) == 0) {
   if(i<(clients->clientlist.clientlist_len-1)) {
    memmove(&(clients->clientlist.clientlist_val[i]),
        &(clients->clientlist.clientlist_val[i+1]),
        sizeof(cname)*clients->clientlist.clientlist_len-1-i);
    break;
   }
  }
 }
 mcExit(user);
 return(&result);
}

variable_types:
user
char **

user_defined_types: struct xp_ops { int (*xp_recv)(struct SVCXPRT *__xprt, struct rpc_msg *__msg); enum xprt_stat (*xp_stat)(struct SVCXPRT *__xprt); int (*xp_getargs)(struct SVCXPRT *__xprt, int (*__xdr_args)(struct XDR *, void *, ...), char *__args_ptr); int (*xp_reply)(struct SVCXPRT *__xprt, struct rpc_msg *__msg); int (*xp_freeargs)(struct SVCXPRT *__xprt, int (*__xdr_args)(struct XDR *, void *, ...), char *__args_ptr); void (*xp_destroy)(struct SVCXPRT *__xprt); }

user_defined_types: struct rpc_msg { unsigned long rm_xid; enum msg_type rm_direction; union { struct call_body RM_cmb; struct reply_body RM_rmb; } ru; }

user_defined_types: struct reply_body { enum reply_stat rp_stat; union { struct accepted_reply RP_ar; struct rejected_reply RP_dr; } ru; }


repo: theqlabs/qchat

decompiled_code:
int *__fastcall heartbeat_1_svc(int *a1)
{
if ( !initialized )
init_data_structures();
result_0 = *a1;
return &result_0;
}

canonical_decompiled_code:
int *__fastcall func11(int *a1)
{
if ( !initialized )
func1();
result_0 = *a1;
return &result_0;
}

original_code:
uint32_t *heartbeat_1_svc(uint32_t *argp, struct svc_req *rqstp) {
 static uint32_t result = 0;
 if (!initialized) {
  init_data_structures();
 }
 result = *argp ++;
 return((&result));
}

canonical_original_code:
unsigned int *heartbeat_1_svc(unsigned int *argp, struct svc_req *rqstp) {
 static unsigned int result = 0;
 if (!initialized) {
  init_data_structures();
 }
 result = *argp ++;
 return((&result));
}

variable_types:
argp
unsigned int *

variable_types: struct svc_req { unsigned long rq_prog; unsigned long rq_vers; unsigned long rq_proc; struct opaque_auth { int oa_flavor; char *oa_base; unsigned int oa_length; } rq_cred; char *rq_clntcred; struct SVCXPRT { int xp_sock; unsigned short xp_port; struct xp_ops { int (*xp_recv)(struct SVCXPRT *__xprt, struct rpc_msg { unsigned long rm_xid; enum msg_type { CALL=0, REPLY=1 } rm_direction; union { struct call_body { unsigned long cb_rpcvers; unsigned long cb_prog; unsigned long cb_vers; unsigned long cb_proc; struct opaque_auth cb_cred; struct opaque_auth cb_verf; } RM_cmb; struct reply_body { enum reply_stat { MSG_ACCEPTED=0, MSG_DENIED=1 } rp_stat; union { struct accepted_reply { struct opaque_auth ar_verf; enum accept_stat { SUCCESS=0, PROG_UNAVAIL=1, PROG_MISMATCH=2, PROC_UNAVAIL=3, GARBAGE_ARGS=4, SYSTEM_ERR=5 } ar_stat; union { struct { unsigned long low; unsigned long high; } AR_versions; struct { char *where; int (*proc)(struct XDR { enum xdr_op { XDR_ENCODE=0, XDR_DECODE=1, XDR_FREE=2 } x_op; struct xdr_ops { int (*x_getlong)(struct XDR *__xdrs, long *__lp); int (*x_putlong)(struct XDR *__xdrs, long *__lp); int (*x_getbytes)(struct XDR *__xdrs, char *__addr, unsigned int __len); int (*x_putbytes)(struct XDR *__xdrs, char *__addr, unsigned int __len); unsigned int (*x_getpostn)(struct XDR *__xdrs); int (*x_setpostn)(struct XDR *__xdrs, unsigned int __pos); int *(*x_inline)(struct XDR *__xdrs, unsigned int __len); void (*x_destroy)(struct XDR *__xdrs); int (*x_getint32)(struct XDR *__xdrs, int *__ip); int (*x_putint32)(struct XDR *__xdrs, int *__ip); } *x_ops; char *x_public; char *x_private; char *x_base; unsigned int x_handy; } *, void *, ...); } AR_results; } ru; } RP_ar; struct rejected_reply { enum reject_stat { RPC_MISMATCH=0, AUTH_ERROR=1 } rj_stat; union { struct { unsigned long low; unsigned long high; } RJ_versions; enum auth_stat { AUTH_OK=0, AUTH_BADCRED=1, AUTH_REJECTEDCRED=2, AUTH_BADVERF=3, AUTH_REJECTEDVERF=4, AUTH_TOOWEAK=5, AUTH_INVALIDRESP=6, AUTH_FAILED=7 } RJ_why; } ru; } RP_dr; } ru; } RM_rmb; } ru; } *__msg); enum xprt_stat { XPRT_DIED=0, XPRT_MOREREQS=1, XPRT_IDLE=2 } (*xp_stat)(struct SVCXPRT *__xprt); int (*xp_getargs)(struct SVCXPRT *__xprt, int (*__xdr_args)(struct XDR *, void *, ...), char *__args_ptr); int (*xp_reply)(struct SVCXPRT *__xprt, struct rpc_msg *__msg); int (*xp_freeargs)(struct SVCXPRT *__xprt, int (*__xdr_args)(struct XDR *, void *, ...), char *__args_ptr); void (*xp_destroy)(struct SVCXPRT *__xprt); } *xp_ops; int xp_addrlen; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } xp_raddr; struct opaque_auth xp_verf; char *xp_p1; char *xp_p2; char xp_pad[256]; } *rq_xprt; } *

variable_types:
result
unsigned int

return_type: unsigned int *

user_defined_types: struct svc_req { unsigned long rq_prog; unsigned long rq_vers; unsigned long rq_proc; struct opaque_auth rq_cred; char *rq_clntcred; struct SVCXPRT *rq_xprt; }

user_defined_types: struct SVCXPRT { int xp_sock; unsigned short xp_port; struct xp_ops *xp_ops; int xp_addrlen; struct sockaddr_in xp_raddr; struct opaque_auth xp_verf; char *xp_p1; char *xp_p2; char xp_pad[256]; }

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

user_defined_types: struct accepted_reply { struct opaque_auth ar_verf; enum accept_stat ar_stat; union { struct { unsigned long low; unsigned long high; } AR_versions; struct { char *where; int (*proc)(struct XDR *, void *, ...); } AR_results; } ru; }

user_defined_types: struct rejected_reply { enum reject_stat rj_stat; union { struct { unsigned long low; unsigned long high; } RJ_versions; enum auth_stat RJ_why; } ru; }

user_defined_types: struct XDR { enum xdr_op x_op; struct xdr_ops *x_ops; char *x_public; char *x_private; char *x_base; unsigned int x_handy; }


decompiled_code:
void __noreturn shutdownserv_1_svc()
{
destroy_data_structures();
exit(0);
}

canonical_decompiled_code:
void __noreturn func12()
{
func2();
exit(0);
}

original_code:
void *shutdownserv_1_svc(void *rpc, struct svc_req *rqstp) {
  destroy_data_structures();
  exit(0);
  return ((void *)0);
}

variable_types:
rpc
void *

return_type: void *

user_defined_types: struct opaque_auth { int oa_flavor; char *oa_base; unsigned int oa_length; }

user_defined_types: enum xprt_stat { XPRT_DIED=0, XPRT_MOREREQS=1, XPRT_IDLE=2 }

user_defined_types: struct call_body { unsigned long cb_rpcvers; unsigned long cb_prog; unsigned long cb_vers; unsigned long cb_proc; struct opaque_auth cb_cred; struct opaque_auth cb_verf; }

user_defined_types: enum reply_stat { MSG_ACCEPTED=0, MSG_DENIED=1 }

user_defined_types: enum accept_stat { SUCCESS=0, PROG_UNAVAIL=1, PROG_MISMATCH=2, PROC_UNAVAIL=3, GARBAGE_ARGS=4, SYSTEM_ERR=5 }

user_defined_types: enum reject_stat { RPC_MISMATCH=0, AUTH_ERROR=1 }

user_defined_types: enum auth_stat { AUTH_OK=0, AUTH_BADCRED=1, AUTH_REJECTEDCRED=2, AUTH_BADVERF=3, AUTH_REJECTEDVERF=4, AUTH_TOOWEAK=5, AUTH_INVALIDRESP=6, AUTH_FAILED=7 }

user_defined_types: enum xdr_op { XDR_ENCODE=0, XDR_DECODE=1, XDR_FREE=2 }

user_defined_types: struct xdr_ops { int (*x_getlong)(struct XDR *__xdrs, long *__lp); int (*x_putlong)(struct XDR *__xdrs, long *__lp); int (*x_getbytes)(struct XDR *__xdrs, char *__addr, unsigned int __len); int (*x_putbytes)(struct XDR *__xdrs, char *__addr, unsigned int __len); unsigned int (*x_getpostn)(struct XDR *__xdrs); int (*x_setpostn)(struct XDR *__xdrs, unsigned int __pos); int *(*x_inline)(struct XDR *__xdrs, unsigned int __len); void (*x_destroy)(struct XDR *__xdrs); int (*x_getint32)(struct XDR *__xdrs, int *__ip); int (*x_putint32)(struct XDR *__xdrs, int *__ip); }


getParent

decompiled_code:
__int64 __fastcall getParent(int a1)
{
return (unsigned int)(a1 / 2);
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
return (unsigned int)(a1 / 2);
}

original_code:
int32_t getParent(int32_t index) {
  return index/2;
}

canonical_original_code:
int getParent(int index) {
  return index/2;
}

variable_types: index int

return_type: int


getLeftChild

decompiled_code:
__int64 __fastcall getLeftChild(int a1)
{
return (unsigned int)(2 * a1 + 1);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
return (unsigned int)(2 * a1 + 1);
}

original_code:
int32_t getLeftChild(int32_t index) {
  return 2*index + 1;
}

canonical_original_code:
int getLeftChild(int index) {
  return 2*index + 1;
}


getRightChild

decompiled_code:
__int64 __fastcall getRightChild(int a1)
{
return (unsigned int)(2 * a1 + 2);
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
return (unsigned int)(2 * a1 + 2);
}

original_code:
int32_t getRightChild(int32_t index) {
  return 2*index + 2;
}

canonical_original_code:
int getRightChild(int index) {
  return 2*index + 2;
}


original_code:
void hq_push(HoldbackQueue *hq, const void *msg) {
    if(hq == ((void *)0)) {
      return;
    }
    if (hq->num_elements >= hq->capacity) {
    }
    hq->messages[hq->num_elements] = (void*) msg;
    int32_t idx = hq->num_elements;
    void *temp = ((void *)0);
    while(idx > 0 && hq->cmp(hq->messages[idx], hq->messages[getParent(idx)]) > 0) {
        temp = hq->messages[idx];
        hq->messages[idx] = hq->messages[getParent(idx)];
        hq->messages[getParent(idx)] = temp;
        idx = getParent(idx);
    }
    hq->num_elements++;
}

canonical_original_code:
void hq_push(struct HQ *hq, void *msg) {
    if(hq == ((void *)0)) {
      return;
    }
    if (hq->num_elements >= hq->capacity) {
    }
    hq->messages[hq->num_elements] = (void *) msg;
    int idx = hq->num_elements;
    void *temp = ((void *)0);
    while(idx > 0 && hq->cmp(hq->messages[idx], hq->messages[getParent(idx)]) > 0) {
        temp = hq->messages[idx];
        hq->messages[idx] = hq->messages[getParent(idx)];
        hq->messages[getParent(idx)] = temp;
        idx = getParent(idx);
    }
    hq->num_elements++;
}

variable_types: struct HQ { int num_elements; int capacity; void **messages; int (*cmp)(void *m1, void *m2); } *

variable_types:
msg
void *

variable_types: idx int

variable_types:
temp
void *

return_type: void


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

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

original_code:
void hq_delete(HoldbackQueue *hq) {
    if (hq != ((void *)0)) {
      free(hq->messages);
      free(hq);
    }
}

canonical_original_code:
void hq_delete(struct HQ *hq) {
    if (hq != ((void *)0)) {
      free(hq->messages);
      free(hq);
    }
}

user_defined_types: struct HQ { int num_elements; int capacity; void **messages; int (*cmp)(void *m1, void *m2); }


heapify

original_code:
void heapify(HoldbackQueue *hq, int32_t index) {
    if(hq == ((void *)0)) {
      return;
    }
    int32_t max_index;
    int32_t l_child = getLeftChild(index);
    int32_t r_child = getRightChild(index);
    if (l_child < hq->num_elements && hq->cmp(hq->messages[l_child], hq->messages[index]) > 0) {
      max_index = l_child;
    } else {
      max_index = index;
    }
    if (r_child < hq->num_elements && hq->cmp(hq->messages[r_child], hq->messages[max_index]) > 0) {
      max_index = r_child;
    }
    if (max_index != index) {
      void *temp= hq->messages[max_index];
      hq->messages[max_index] = hq->messages[index];
      hq->messages[index] = temp;
      heapify(hq, max_index);
    }
}

canonical_original_code:
void heapify(struct HQ *hq, int index) {
    if(hq == ((void *)0)) {
      return;
    }
    int max_index;
    int l_child = getLeftChild(index);
    int r_child = getRightChild(index);
    if (l_child < hq->num_elements && hq->cmp(hq->messages[l_child], hq->messages[index]) > 0) {
      max_index = l_child;
    } else {
      max_index = index;
    }
    if (r_child < hq->num_elements && hq->cmp(hq->messages[r_child], hq->messages[max_index]) > 0) {
      max_index = r_child;
    }
    if (max_index != index) {
      void *temp = hq->messages[max_index];
      hq->messages[max_index] = hq->messages[index];
      hq->messages[index] = temp;
      heapify(hq, max_index);
    }
}


original_code:
void *hq_pop(HoldbackQueue *hq) {
    if(hq == ((void *)0)) {
      return ((void *)0);
    }
    if (hq->num_elements < 1) {
      return ((void *)0);
    }
    void *item = hq->messages[0];
    hq->messages[0] = hq->messages[hq->num_elements-1];
    hq->num_elements--;
    heapify(hq, 0);
    return (item);
}

canonical_original_code:
void *hq_pop(struct HQ *hq) {
    if(hq == ((void *)0)) {
      return ((void *)0);
    }
    if (hq->num_elements < 1) {
      return ((void *)0);
    }
    void *item = hq->messages[0];
    hq->messages[0] = hq->messages[hq->num_elements-1];
    hq->num_elements--;
    heapify(hq, 0);
    return (item);
}

variable_types:
item
void *


terminate

repo: therealmuffin/synchronator

decompiled_code:
void __fastcall __noreturn terminate(int sig)
{
pthread_t v1; // r13
pthread_t v2; // rbp
pthread_t v3; // r13
pthread_t v4; // r13
__int64 v5; // rax
__int64 v6; // rax
__int64 v7; // rax
__int64 v8; // rax
char *v9; // rax
int v10; // er13
int *v11; // rbp
int v12; // eax
char *v13; // rax
int v14; // er13
int *v15; // rbp
int v16; // eax
int v17; // er13
int *v18; // rbp
int v19; // eax
char *v20; // rax
char *v21; // rax
char *v22; // rax
char *v23; // rax
char *v24; // rax

v1 = watchMixerThread;
v2 = mainThread;
if ( !pthread_equal(watchMixerThread, mainThread) )
{
v17 = pthread_kill(v1, 0);
v18 = _errno_location();
if ( !v17 && pthread_cancel(watchMixerThread) )
{
v22 = strerror(*v18);
syslog(4, "Unable request cancelation for mixer thread: %s", v22);
}
v19 = pthread_join(watchMixerThread, 0LL);
*v18 = v19;
if ( v19 )
{
v21 = strerror(v19);
syslog(4, "Unable to join mixer thread: %s\n", v21);
}
v2 = mainThread;
}
v3 = interfaceListenThread;
if ( !pthread_equal(interfaceListenThread, v2) )
{
v14 = pthread_kill(v3, 0);
v15 = _errno_location();
if ( !v14 && pthread_cancel(interfaceListenThread) )
{
v24 = strerror(*v15);
syslog(4, "Unable request cancelation for serial thread: %s", v24);
}
v16 = pthread_join(interfaceListenThread, 0LL);
*v15 = v16;
if ( v16 )
{
v23 = strerror(v16);
syslog(4, "Unable to join serial thread: %s\n", v23);
}
v2 = mainThread;
}
v4 = statusQueryThread;
if ( !pthread_equal(statusQueryThread, v2) )
{
v10 = pthread_kill(v4, 0);
v11 = _errno_location();
if ( !v10 && pthread_cancel(statusQueryThread) )
{
v20 = strerror(*v11);
syslog(4, "Unable request cancelation for query thread: %s", v20);
}
v12 = pthread_join(statusQueryThread, 0LL);
*v11 = v12;
if ( v12 )
{
v13 = strerror(v12);
syslog(4, "Unable to join query thread: %s\n", v13);
}
}
v5 = *((_QWORD *)&common_data + 16);
if ( v5 )
(*(void (**)(void))(v5 + 24))();
v6 = *((_QWORD *)&common_data + 17);
if ( v6 )
(*(void (**)(void))(v6 + 24))();
v7 = *((_QWORD *)&common_data + 19);
if ( v7 )
(*(void (**)(void))(v7 + 24))();
deinitMixer();
v8 = *((_QWORD *)&common_data + 18);
if ( v8 )
(*(void (**)(void))(v8 + 16))();
statusInfo[2]();
config_destroy(&config);
pthread_mutex_destroy(&lockConfig);
pthread_mutex_destroy(&lockProcess);
deinitMsQ();
if ( lock_file > 0 )
{
lockf(lock_file, 0, 0LL);
close(lock_file);
unlink("/var/run/synchronator/synchronator.pid");
}
v9 = strsignal(sig);
syslog(5, "Program terminated: %s (%i)", v9, (unsigned int)sig);
closelog();
exit(0);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(int sig)
{
pthread_t v1; // r13
pthread_t v2; // rbp
pthread_t v3; // r13
pthread_t v4; // r13
__int64 v5; // rax
__int64 v6; // rax
__int64 v7; // rax
__int64 v8; // rax
char *v9; // rax
int v10; // er13
int *v11; // rbp
int v12; // eax
char *v13; // rax
int v14; // er13
int *v15; // rbp
int v16; // eax
int v17; // er13
int *v18; // rbp
int v19; // eax
char *v20; // rax
char *v21; // rax
char *v22; // rax
char *v23; // rax
char *v24; // rax

v1 = watchMixerThread;
v2 = mainThread;
if ( !pthread_equal(watchMixerThread, mainThread) )
{
v17 = pthread_kill(v1, 0);
v18 = _errno_location();
if ( !v17 && pthread_cancel(watchMixerThread) )
{
v22 = strerror(*v18);
syslog(4, "Unable request cancelation for mixer thread: %s", v22);
}
v19 = pthread_join(watchMixerThread, 0LL);
*v18 = v19;
if ( v19 )
{
v21 = strerror(v19);
syslog(4, "Unable to join mixer thread: %s\n", v21);
}
v2 = mainThread;
}
v3 = interfaceListenThread;
if ( !pthread_equal(interfaceListenThread, v2) )
{
v14 = pthread_kill(v3, 0);
v15 = _errno_location();
if ( !v14 && pthread_cancel(interfaceListenThread) )
{
v24 = strerror(*v15);
syslog(4, "Unable request cancelation for serial thread: %s", v24);
}
v16 = pthread_join(interfaceListenThread, 0LL);
*v15 = v16;
if ( v16 )
{
v23 = strerror(v16);
syslog(4, "Unable to join serial thread: %s\n", v23);
}
v2 = mainThread;
}
v4 = statusQueryThread;
if ( !pthread_equal(statusQueryThread, v2) )
{
v10 = pthread_kill(v4, 0);
v11 = _errno_location();
if ( !v10 && pthread_cancel(statusQueryThread) )
{
v20 = strerror(*v11);
syslog(4, "Unable request cancelation for query thread: %s", v20);
}
v12 = pthread_join(statusQueryThread, 0LL);
*v11 = v12;
if ( v12 )
{
v13 = strerror(v12);
syslog(4, "Unable to join query thread: %s\n", v13);
}
}
v5 = *((_QWORD *)&common_data + 16);
if ( v5 )
(*(void (**)(void))(v5 + 24))();
v6 = *((_QWORD *)&common_data + 17);
if ( v6 )
(*(void (**)(void))(v6 + 24))();
v7 = *((_QWORD *)&common_data + 19);
if ( v7 )
(*(void (**)(void))(v7 + 24))();
deinitMixer();
v8 = *((_QWORD *)&common_data + 18);
if ( v8 )
(*(void (**)(void))(v8 + 16))();
statusInfo[2]();
config_destroy(&config);
pthread_mutex_destroy(&lockConfig);
pthread_mutex_destroy(&lockProcess);
deinitMsQ();
if ( lock_file > 0 )
{
lockf(lock_file, 0, 0LL);
close(lock_file);
unlink("/var/run/synchronator/synchronator.pid");
}
v9 = strsignal(sig);
syslog(5, "Program terminated: %s (%i)", v9, (unsigned int)sig);
closelog();
exit(0);
}

original_code:
static void terminate(int signum) {
    if(!pthread_equal(watchMixerThread, mainThread)) {
        if(pthread_kill(watchMixerThread, 0) == 0 && pthread_cancel(watchMixerThread) != 0)
            syslog(4, "Unable request cancelation for mixer thread: %s", strerror((*__errno_location ())));
        if(((*__errno_location ()) = pthread_join(watchMixerThread, ((void *)0))) != 0)
            syslog(4, "Unable to join mixer thread: %s\n", strerror((*__errno_location ())));
    }
    if(!pthread_equal(interfaceListenThread, mainThread)) {
        if(pthread_kill(interfaceListenThread, 0) == 0 && pthread_cancel(interfaceListenThread) != 0)
            syslog(4, "Unable request cancelation for serial thread: %s", strerror((*__errno_location ())));
        if(((*__errno_location ()) = pthread_join(interfaceListenThread, ((void *)0))) != 0)
            syslog(4, "Unable to join serial thread: %s\n", strerror((*__errno_location ())));
    }
    if(!pthread_equal(statusQueryThread, mainThread)) {
        if(pthread_kill(statusQueryThread, 0) == 0 && pthread_cancel(statusQueryThread) != 0)
            syslog(4, "Unable request cancelation for query thread: %s", strerror((*__errno_location ())));
        if(((*__errno_location ()) = pthread_join(statusQueryThread, ((void *)0))) != 0)
            syslog(4, "Unable to join query thread: %s\n", strerror((*__errno_location ())));
    }
    if(common_data.process)
        common_data.process->deinit();
    if(common_data.interface)
        common_data.interface->deinit();
    if(common_data.mod)
        common_data.mod->deinit();
    deinitMixer();
    if(common_data.volume)
        common_data.volume->deinit();
    statusInfo.deinit();
    config_destroy(&config);
    pthread_mutex_destroy(&lockConfig);
    pthread_mutex_destroy(&lockProcess);
    deinitMsQ();
    if(lock_file > 0) {
        lockf(lock_file, 0, 0);
        close(lock_file);
        unlink("/var/run/synchronator/" "synchronator" ".pid");
    }
    syslog(5, "Program terminated: %s (%i)", strsignal(signum), signum);
    closelog();
    exit(0);
}

variable_types: signum int


daemonize

decompiled_code:
__pid_t daemonize()
{
rlim_t v0; // rbx
int v1; // edi
__uid_t v2; // eax
__pid_t v3; // eax
__pid_t result; // eax
int *v5; // rax
char *v6; // rax
int *v7; // rax
char *v8; // rax
int *v9; // rax
char *v10; // rdx
int *v11; // rax
char *v12; // rdx
struct rlimit v13; // [rsp+0h] [rbp-B8h] BYREF
struct sigaction v14; // [rsp+10h] [rbp-A8h] BYREF

if ( getrlimit(RLIMIT_NOFILE, &v13) < 0 )
{
v5 = _errno_location();
v6 = strerror(*v5);
fprintf(stderr, "Unable to retrieve resource limit: %s\n", v6);
}
if ( v13.rlim_max == -1LL )
{
v13.rlim_max = 1024LL;
}
else if ( !v13.rlim_max )
{
goto LABEL_7;
}
v0 = 0LL;
do
{
v1 = v0++;
close(v1);
}
while ( v13.rlim_max > v0 );
LABEL_7:
open("/dev/null", 2);
dup(0);
openlog("synchronator", 9, 24);
v2 = getuid();
syslog(5, "Program started by User %d", v2);
v3 = fork();
if ( v3 < 0 )
{
v7 = _errno_location();
v8 = strerror(*v7);
syslog(3, "Failed to detach from TTY: %s", v8);
return 1;
}
if ( v3 )
goto LABEL_12;
setsid();
v14.sa_handler = (__sighandler_t)(terminate + 1);
sigemptyset(&v14.sa_mask);
v14.sa_flags = 0;
if ( sigaction(1, &v14, 0LL) < 0 )
{
v9 = _errno_location();
v10 = strerror(*v9);
syslog(3, "Failed to ignore SIGHUP: %s", v10);
LABEL_16:
closelog();
return 1;
}
result = fork();
if ( result < 0 )
{
v11 = _errno_location();
v12 = strerror(*v11);
syslog(3, "Failed to attach to init: %s", v12);
goto LABEL_16;
}
if ( result )
{
LABEL_12:
closelog();
exit(0);
}
return result;
}

canonical_decompiled_code:
__pid_t func1()
{
rlim_t v0; // rbx
int v1; // edi
__uid_t v2; // eax
__pid_t v3; // eax
__pid_t result; // eax
int *v5; // rax
char *v6; // rax
int *v7; // rax
char *v8; // rax
int *v9; // rax
char *v10; // rdx
int *v11; // rax
char *v12; // rdx
struct rlimit v13; // [rsp+0h] [rbp-B8h] BYREF
struct sigaction v14; // [rsp+10h] [rbp-A8h] BYREF

if ( getrlimit(RLIMIT_NOFILE, &v13) < 0 )
{
v5 = _errno_location();
v6 = strerror(*v5);
fprintf(stderr, "Unable to retrieve resource limit: %s\n", v6);
}
if ( v13.rlim_max == -1LL )
{
v13.rlim_max = 1024LL;
}
else if ( !v13.rlim_max )
{
goto LABEL_7;
}
v0 = 0LL;
do
{
v1 = v0++;
close(v1);
}
while ( v13.rlim_max > v0 );
LABEL_7:
open("/dev/null", 2);
dup(0);
openlog("synchronator", 9, 24);
v2 = getuid();
syslog(5, "Program started by User %d", v2);
v3 = fork();
if ( v3 < 0 )
{
v7 = _errno_location();
v8 = strerror(*v7);
syslog(3, "Failed to detach from TTY: %s", v8);
return 1;
}
if ( v3 )
goto LABEL_12;
setsid();
v14.sa_handler = (__sighandler_t)(terminate + 1);
sigemptyset(&v14.sa_mask);
v14.sa_flags = 0;
if ( sigaction(1, &v14, 0LL) < 0 )
{
v9 = _errno_location();
v10 = strerror(*v9);
syslog(3, "Failed to ignore SIGHUP: %s", v10);
LABEL_16:
closelog();
return 1;
}
result = fork();
if ( result < 0 )
{
v11 = _errno_location();
v12 = strerror(*v11);
syslog(3, "Failed to attach to init: %s", v12);
goto LABEL_16;
}
if ( result )
{
LABEL_12:
closelog();
exit(0);
}
return result;
}

original_code:
static int daemonize(void) {
    int count;
    pid_t synchronatord_pid;
    struct rlimit resource_limit;
    int fd0, fd1, fd2;
    if(getrlimit(RLIMIT_NOFILE, &resource_limit) < 0)
        fprintf(stderr, "Unable to retrieve resource limit: %s\n", strerror((*__errno_location ())));
    if(resource_limit.rlim_max == ((__rlim_t) -1))
        resource_limit.rlim_max = 1024;
    for(count = 0; count < resource_limit.rlim_max; count++)
        close(count);
    fd0 = open("/dev/null", 02);
    fd1 = fd2 = dup(0);
    openlog( "synchronator" , 0x08 | 0x01, (3<<3));
    syslog(5, "Program started by User %d", getuid());
    if((synchronatord_pid = fork()) < 0) {
        syslog(3, "Failed to detach from TTY: %s", strerror((*__errno_location ())));
        return 1;
    }
    else if(synchronatord_pid > 0) {
        closelog();
        exit(0);
    }
    setsid();
    struct sigaction signal_action;
    signal_action.__sigaction_handler.sa_handler = ((__sighandler_t) 1);
    sigemptyset(&signal_action.sa_mask);
    signal_action.sa_flags = 0;
    if(sigaction(1, &signal_action, ((void *)0)) < 0) {
        syslog(3, "Failed to ignore SIGHUP: %s", strerror((*__errno_location ())));
        closelog();
        return 1;
    }
    if((synchronatord_pid = fork()) < 0) {
        syslog(3, "Failed to attach to init: %s", strerror((*__errno_location ())));
        closelog();
        return 1;
    }
    else if(synchronatord_pid > 0) {
        closelog();
        exit(0);
    }
    return 0;
}

canonical_original_code:
static int daemonize() {
    int count;
    int synchronatord_pid;
    struct rlimit resource_limit;
    int fd0; int fd1; int fd2;
    if(getrlimit(RLIMIT_NOFILE, &resource_limit) < 0)
        fprintf(stderr, "Unable to retrieve resource limit: %s\n", strerror((*__errno_location ())));
    if(resource_limit.rlim_max == ((__rlim_t) -1))
        resource_limit.rlim_max = 1024;
    for(count = 0; count < resource_limit.rlim_max; count++)
        close(count);
    fd0 = open("/dev/null", 02);
    fd1 = fd2 = dup(0);
    openlog( "synchronator" , 0x08 | 0x01, (3<<3));
    syslog(5, "Program started by User %d", getuid());
    if((synchronatord_pid = fork()) < 0) {
        syslog(3, "Failed to detach from TTY: %s", strerror((*__errno_location ())));
        return 1;
    }
    else if(synchronatord_pid > 0) {
        closelog();
        exit(0);
    }
    setsid();
    struct sigaction signal_action;
    signal_action.__sigaction_handler.sa_handler = ((void (*)(int)) 1);
    sigemptyset(&signal_action.sa_mask);
    signal_action.sa_flags = 0;
    if(sigaction(1, &signal_action, ((void *)0)) < 0) {
        syslog(3, "Failed to ignore SIGHUP: %s", strerror((*__errno_location ())));
        closelog();
        return 1;
    }
    if((synchronatord_pid = fork()) < 0) {
        syslog(3, "Failed to attach to init: %s", strerror((*__errno_location ())));
        closelog();
        return 1;
    }
    else if(synchronatord_pid > 0) {
        closelog();
        exit(0);
    }
    return 0;
}

variable_types: count int

variable_types: struct rlimit { unsigned long rlim_cur; unsigned long rlim_max; }

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }


queryStatus

decompiled_code:
void __fastcall __noreturn queryStatus(void *a1)
{
unsigned int v1; // edi
int *v2; // rax

if ( pthread_sigmask(0, (const __sigset_t *)a1, 0LL) < 0 )
{
v2 = _errno_location();
syslog(3, "Failed to ignore signals in statusQuery: %i", (unsigned int)*v2);
pthread_kill(mainThread, 15);
pause();
}
while ( 1 )
{
if ( (*(unsigned int (__fastcall **)(_QWORD, _QWORD))(*((_QWORD *)&common_data + 17) + 40LL))(
*((_QWORD *)&common_data + 15),
*(&common_data + 28)) == -1 )
{
syslog(3, "Failed to write trigger to serial port");
pthread_kill(mainThread, 15);
pause();
}
syslog(7, "Trigger written to serial port");
v1 = *(&common_data + 29);
if ( !v1 )
pthread_exit(0LL);
sleep(v1);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func2(void *a1)
{
unsigned int v1; // edi
int *v2; // rax

if ( pthread_sigmask(0, (const __sigset_t *)a1, 0LL) < 0 )
{
v2 = _errno_location();
syslog(3, "Failed to ignore signals in statusQuery: %i", (unsigned int)*v2);
pthread_kill(mainThread, 15);
pause();
}
while ( 1 )
{
if ( (*(unsigned int (__fastcall **)(_QWORD, _QWORD))(*((_QWORD *)&common_data + 17) + 40LL))(
*((_QWORD *)&common_data + 15),
*(&common_data + 28)) == -1 )
{
syslog(3, "Failed to write trigger to serial port");
pthread_kill(mainThread, 15);
pause();
}
syslog(7, "Trigger written to serial port");
v1 = *(&common_data + 29);
if ( !v1 )
pthread_exit(0LL);
sleep(v1);
}
}

original_code:
static void *queryStatus(void *arg) {
    sigset_t *thread_sigmask = (sigset_t *)arg;
    if(pthread_sigmask(0, thread_sigmask, ((void *)0)) < 0) {
        syslog(3, "Failed to ignore signals in statusQuery: %i", (*__errno_location ()));
        pthread_kill(mainThread, 15);
        pause();
    }
    while(1) {
        if(common_data.interface->send(common_data.statusQuery, common_data.statusQueryLength) == -1) {
            syslog(3, "Failed to write trigger to serial port");
            pthread_kill(mainThread, 15);
            pause();
        }
        syslog(7, "Trigger written to serial port");
        if(common_data.statusQueryInterval == 0)
            break;
        sleep(common_data.statusQueryInterval);
    }
    pthread_exit(0);
}

canonical_original_code:
static void *queryStatus(void *arg) {
    struct __sigset_t *thread_sigmask = (sigset_t *)arg;
    if(pthread_sigmask(0, thread_sigmask, ((void *)0)) < 0) {
        syslog(3, "Failed to ignore signals in statusQuery: %i", (*__errno_location ()));
        pthread_kill(mainThread, 15);
        pause();
    }
    while(1) {
        if(common_data.interface->send(common_data.statusQuery, common_data.statusQueryLength) == -1) {
            syslog(3, "Failed to write trigger to serial port");
            pthread_kill(mainThread, 15);
            pause();
        }
        syslog(7, "Trigger written to serial port");
        if(common_data.statusQueryInterval == 0)
            break;
        sleep(common_data.statusQueryInterval);
    }
    pthread_exit(0);
}

variable_types:
arg
void *

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


synchronatord

decompiled_code:
__int64 __fastcall synchronatord(const char *a1)
{
__int64 v1; // rbx
const char *v2; // r12
unsigned int v3; // eax
unsigned int v4; // eax
pthread_attr_t *v5; // rsi
unsigned int v6; // er12
__int64 v7; // rdx
unsigned int v8; // eax
unsigned int v10; // er12
unsigned int v11; // er12
__int64 v12[4]; // [rsp+0h] [rbp-F8h]
pthread_attr_t attr; // [rsp+20h] [rbp-D8h] BYREF
sigset_t set; // [rsp+60h] [rbp-98h] BYREF

v12[0] = (__int64)a1;
v1 = a1 == 0LL;
v12[1] = (__int64)"/usr/local/etc/synchronator.conf";
v12[2] = (__int64)"/etc/synchronator.conf";
while ( 1 )
{
config_init(&config);
v2 = (const char *)v12[v1];
if ( (unsigned int)config_read_file(&config, v2) )
break;
if ( (v1 & 0xFFFFFFFD) != 0 )
{
if ( (int)++v1 > 2 )
goto LABEL_6;
}
else
{
++v1;
syslog(
4,
"Configuration file was not found or is invalid: %s\n%s:%d - %s",
v2,
*((const char **)&config + 5),
*((unsigned int *)&config + 12),
*((const char **)&config + 4));
raise(15);
if ( (int)v1 > 2 )
goto LABEL_6;
}
}
syslog(5, "Configuration file has been read: %s", a1);
LABEL_6:
if ( init() == 1 )
{
syslog(3, "Initialisation failed: %i", 1LL);
raise(15);
}
sigemptyset(&set);
sigaddset(&set, 2);
sigaddset(&set, 15);
v3 = pthread_mutex_init(&lockProcess, 0LL);
if ( v3 )
{
syslog(3, "Failed to create process mutex: %i", v3);
raise(15);
}
v4 = pthread_attr_init(&attr);
if ( v4 )
{
syslog(3, "Failed to initialize thread attributes: %i", v4);
raise(15);
}
pthread_attr_setdetachstate(&attr, 0);
v5 = &attr;
v6 = pthread_create(&watchMixerThread, &attr, (void *(*)(void *))&watchMixer, &set);
if ( v6 )
{
pthread_attr_destroy(&attr);
v5 = (pthread_attr_t *)"Failed to open mixer thread: %i";
syslog(3, "Failed to open mixer thread: %i", v6);
raise(15);
}
if ( common_data )
{
v5 = &attr;
v11 = pthread_create(
&interfaceListenThread,
&attr,
*(void *(**)(void *))(*((_QWORD *)&common_data + 17) + 48LL),
&set);
if ( v11 )
{
pthread_attr_destroy(&attr);
v5 = (pthread_attr_t *)"Failed to open interface listen thread: %i";
syslog(3, "Failed to open interface listen thread: %i", v11);
raise(15);
}
}
if ( *(&common_data + 3) )
{
v5 = &attr;
v10 = pthread_create(&statusQueryThread, &attr, (void *(*)(void *))queryStatus, &set);
if ( v10 )
{
pthread_attr_destroy(&attr);
v5 = (pthread_attr_t *)"Failed to open interface query thread: %i";
syslog(3, "Failed to open interface query thread: %i", v10);
raise(15);
}
}
pthread_attr_destroy(&attr);
v8 = watchMsQ(&attr, v5, v7);
if ( v8 )
{
syslog(3, "Message queue failure: %i", v8);
raise(15);
}
raise(15);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(const char *a1)
{
__int64 v1; // rbx
const char *v2; // r12
unsigned int v3; // eax
unsigned int v4; // eax
pthread_attr_t *v5; // rsi
unsigned int v6; // er12
__int64 v7; // rdx
unsigned int v8; // eax
unsigned int v10; // er12
unsigned int v11; // er12
__int64 v12[4]; // [rsp+0h] [rbp-F8h]
pthread_attr_t attr; // [rsp+20h] [rbp-D8h] BYREF
sigset_t set; // [rsp+60h] [rbp-98h] BYREF

v12[0] = (__int64)a1;
v1 = a1 == 0LL;
v12[1] = (__int64)"/usr/local/etc/synchronator.conf";
v12[2] = (__int64)"/etc/synchronator.conf";
while ( 1 )
{
config_init(&config);
v2 = (const char *)v12[v1];
if ( (unsigned int)config_read_file(&config, v2) )
break;
if ( (v1 & 0xFFFFFFFD) != 0 )
{
if ( (int)++v1 > 2 )
goto LABEL_6;
}
else
{
++v1;
syslog(
4,
"Configuration file was not found or is invalid: %s\n%s:%d - %s",
v2,
*((const char **)&config + 5),
*((unsigned int *)&config + 12),
*((const char **)&config + 4));
raise(15);
if ( (int)v1 > 2 )
goto LABEL_6;
}
}
syslog(5, "Configuration file has been read: %s", a1);
LABEL_6:
if ( func3() == 1 )
{
syslog(3, "Initialisation failed: %i", 1LL);
raise(15);
}
sigemptyset(&set);
sigaddset(&set, 2);
sigaddset(&set, 15);
v3 = pthread_mutex_init(&lockProcess, 0LL);
if ( v3 )
{
syslog(3, "Failed to create process mutex: %i", v3);
raise(15);
}
v4 = pthread_attr_init(&attr);
if ( v4 )
{
syslog(3, "Failed to initialize thread attributes: %i", v4);
raise(15);
}
pthread_attr_setdetachstate(&attr, 0);
v5 = &attr;
v6 = pthread_create(&watchMixerThread, &attr, (void *(*)(void *))&watchMixer, &set);
if ( v6 )
{
pthread_attr_destroy(&attr);
v5 = (pthread_attr_t *)"Failed to open mixer thread: %i";
syslog(3, "Failed to open mixer thread: %i", v6);
raise(15);
}
if ( common_data )
{
v5 = &attr;
v11 = pthread_create(
&interfaceListenThread,
&attr,
*(void *(**)(void *))(*((_QWORD *)&common_data + 17) + 48LL),
&set);
if ( v11 )
{
pthread_attr_destroy(&attr);
v5 = (pthread_attr_t *)"Failed to open interface listen thread: %i";
syslog(3, "Failed to open interface listen thread: %i", v11);
raise(15);
}
}
if ( *(&common_data + 3) )
{
v5 = &attr;
v10 = pthread_create(&statusQueryThread, &attr, (void *(*)(void *))queryStatus, &set);
if ( v10 )
{
pthread_attr_destroy(&attr);
v5 = (pthread_attr_t *)"Failed to open interface query thread: %i";
syslog(3, "Failed to open interface query thread: %i", v10);
raise(15);
}
}
pthread_attr_destroy(&attr);
v8 = watchMsQ(&attr, v5, v7);
if ( v8 )
{
syslog(3, "Message queue failure: %i", v8);
raise(15);
}
raise(15);
return 1LL;
}

original_code:
static int synchronatord(char *customConfigLocation) {
    int status = 0;
    int count = 0;
    char *configLocations[] = {customConfigLocation, "/usr/local" "/etc/" "synchronator" ".conf", "/etc/" "synchronator" ".conf"};
    for(count = (configLocations[0] == ((void *)0)) ? 1:0; count < 3; count++) {
        config_init(&config);
        if(!config_read_file(&config, configLocations[count])) {
            if(count == 2 || count == 0) {
                syslog(4, "Configuration file was not found or is invalid: %s\n%s:%d - %s",
                    configLocations[count], ((&config)->error_file), ((&config)->error_line),
                    ((&config)->error_text));
                raise(15);
            }
        }
        else {
            syslog(5, "Configuration file has been read: %s", configLocations[0]);
            break;
        }
    }
    if((status = init()) == 1) {
        syslog(3, "Initialisation failed: %i", status);
        raise(15);
    }
    sigset_t thread_sigmask;
    sigemptyset(&thread_sigmask);
    sigaddset(&thread_sigmask, 2);
    sigaddset(&thread_sigmask, 15);
    if((status = pthread_mutex_init(&lockProcess, ((void *)0))) != 0) {
        syslog(3, "Failed to create process mutex: %i", status);
        raise(15);
    }
    pthread_attr_t thread_attr;
    if((status = pthread_attr_init(&thread_attr)) != 0) {
        syslog(3, "Failed to initialize thread attributes: %i", status);
        raise(15);
    }
    pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
    if(status = pthread_create(&watchMixerThread, &thread_attr, watchMixer, &thread_sigmask)) {
        pthread_attr_destroy(&thread_attr);
        syslog(3, "Failed to open mixer thread: %i", status);
        raise(15);
    }
    if(common_data.sync_2way && (status = pthread_create(&interfaceListenThread, &thread_attr,
                                    common_data.interface->listen, &thread_sigmask))) {
        pthread_attr_destroy(&thread_attr);
        syslog(3, "Failed to open interface listen thread: %i", status);
        raise(15);
    }
    if(common_data.send_query && (status = pthread_create(&statusQueryThread, &thread_attr,
    queryStatus, &thread_sigmask))) {
        pthread_attr_destroy(&thread_attr);
        syslog(3, "Failed to open interface query thread: %i", status);
        raise(15);
    }
    pthread_attr_destroy(&thread_attr);
        if(status = watchMsQ()) {
            syslog(3, "Message queue failure: %i", status);
            raise(15);
        }
    raise(15);
    return 1;
}

canonical_original_code:
static int synchronatord(char *customConfigLocation) {
    int status = 0;
    int count = 0;
    char *configLocations[] = {customConfigLocation, "/usr/local" "/etc/" "synchronator" ".conf", "/etc/" "synchronator" ".conf"};
    for(count = (configLocations[0] == ((void *)0)) ? 1:0; count < 3; count++) {
        config_init(&config);
        if(!config_read_file(&config, configLocations[count])) {
            if(count == 2 || count == 0) {
                syslog(4, "Configuration file was not found or is invalid: %s\n%s:%d - %s",
                    configLocations[count], ((&config)->error_file), ((&config)->error_line),
                    ((&config)->error_text));
                raise(15);
            }
        }
        else {
            syslog(5, "Configuration file has been read: %s", configLocations[0]);
            break;
        }
    }
    if((status = init()) == 1) {
        syslog(3, "Initialisation failed: %i", status);
        raise(15);
    }
    struct __sigset_t thread_sigmask;
    sigemptyset(&thread_sigmask);
    sigaddset(&thread_sigmask, 2);
    sigaddset(&thread_sigmask, 15);
    if((status = pthread_mutex_init(&lockProcess, ((void *)0))) != 0) {
        syslog(3, "Failed to create process mutex: %i", status);
        raise(15);
    }
    union pthread_attr_t thread_attr;
    if((status = pthread_attr_init(&thread_attr)) != 0) {
        syslog(3, "Failed to initialize thread attributes: %i", status);
        raise(15);
    }
    pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
    if(status = pthread_create(&watchMixerThread, &thread_attr, watchMixer, &thread_sigmask)) {
        pthread_attr_destroy(&thread_attr);
        syslog(3, "Failed to open mixer thread: %i", status);
        raise(15);
    }
    if(common_data.sync_2way && (status = pthread_create(&interfaceListenThread, &thread_attr,
                                    common_data.interface->listen, &thread_sigmask))) {
        pthread_attr_destroy(&thread_attr);
        syslog(3, "Failed to open interface listen thread: %i", status);
        raise(15);
    }
    if(common_data.send_query && (status = pthread_create(&statusQueryThread, &thread_attr,
    queryStatus, &thread_sigmask))) {
        pthread_attr_destroy(&thread_attr);
        syslog(3, "Failed to open interface query thread: %i", status);
        raise(15);
    }
    pthread_attr_destroy(&thread_attr);
        if(status = watchMsQ()) {
            syslog(3, "Message queue failure: %i", status);
            raise(15);
        }
    raise(15);
    return 1;
}

variable_types:
customConfigLocation
char *

variable_types: status int

variable_types:
configLocations
char *[]

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

variable_types: union pthread_attr_t { char __size[56]; long __align; }


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er13
char *v4; // r12
int v5; // eax
unsigned int v6; // er12
char *v7; // r14
int v8; // ebp
unsigned int v9; // eax
pthread_t v10; // rax
int *v11; // rax
char *v12; // rax
__uid_t v13; // eax
int *v14; // rax
char *v15; // rax
int *v16; // rax
char *v17; // rax
__int64 v18; // rbp
struct sigaction v20; // [rsp+0h] [rbp-C8h] BYREF

v3 = 0;
v4 = 0LL;
setlogmask(63);
opterr = 0;
while ( 2 )
{
v5 = getopt(argc, (char *const *)argv, "v:Vhdi:");
if ( v5 == -1 )
{
umask(0);
if ( v3 )
{
if ( daemonize() == 1 )
break;
}
else
{
openlog("synchronator", 41, 24);
v13 = getuid();
syslog(5, "Program started by User %d", v13);
}
lock_file = open("/var/run/synchronator/synchronator.pid", 524354, 420LL);
if ( lock_file >= 0 )
{
if ( lockf(lock_file, 2, 0LL) >= 0 )
{
v8 = lock_file;
ftruncate(lock_file, 0LL);
v9 = getpid();
dprintf(v8, "%d\n", v9);
v10 = pthread_self();
*((_QWORD *)&common_data + 16) = 0LL;
interfaceListenThread = v10;
watchMixerThread = v10;
statusQueryThread = v10;
mainThread = v10;
*((_QWORD *)&common_data + 17) = 0LL;
*((_QWORD *)&common_data + 18) = 0LL;
*((_QWORD *)&common_data + 19) = 0LL;
v20.sa_handler = (__sighandler_t)terminate;
sigfillset(&v20.sa_mask);
v20.sa_flags = 0;
if ( sigaction(2, &v20, 0LL) < 0 || sigaction(15, &v20, 0LL) < 0 )
{
v11 = _errno_location();
v12 = strerror(*v11);
syslog(3, "Failed to ignore signals: %s", v12);
closelog();
exit(1);
}
v18 = (unsigned int)synchronatord(v4);
syslog(3, "Exiting: %i", v18);
closelog();
exit(v18);
}
v16 = _errno_location();
v17 = strerror(*v16);
syslog(4, "Exiting: only one instance of this application can run: %s", v17);
closelog();
exit(0);
}
v14 = _errno_location();
v15 = strerror(*v14);
syslog(3, "Failed to open lock file: %s (%s)", "/var/run/synchronator/synchronator.pid", v15);
closelog();
}
else
{
if ( v5 != 63 )
{
switch ( v5 )
{
case 'V':
fprintf(
stdout,
"%s %s\n\n%s\n",
"synchronator",
" (5762e34)",
"Copyright (C) 2013-2017 Muffinman <synchronator.hifi@gmail.com>\n"
"This is free software; see the source for copying conditions. There is NO\n"
"warranty; not even MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
exit(0);
case 'd':
v3 = 1;
continue;
case 'h':
fprintf(
stdout,
"Usage: %s %s",
*argv,
"[options...]\n"
" -V print version information\n"
" -h print this help message\n"
" -d daemonize application\n"
" -v0 set level of verbose to a minimum\n"
" -v1 set level of verbose to include informational messages\n"
" -v2 set level of verbose to include debug messages\n"
" -i specify path to a configuration file\n"
"\n"
"If no location for the configuration file is given, the application will look in:\n"
" primary /usr/local/etc/synchronator.conf\n"
" secondary /etc/synchronator.conf\n"
"\n"
"Synchronator is compiled with support for:\n"
" Serial interface\n"
" Message Queue (basic local control)\n");
exit(0);
case 'i':
v4 = optarg;
continue;
case 'v':
v7 = optarg;
if ( !strcmp(optarg, "0") )
{
setlogmask(15);
}
else if ( !strcmp(v7, "1") )
{
setlogmask(127);
}
else if ( !strcmp(v7, "2") )
{
setlogmask(255);
}
continue;
default:
goto LABEL_9;
}
}
v6 = optopt;
if ( optopt == 105 )
{
fprintf(stderr, "You forgot to include a path with option '-%c'.\n", 105LL);
}
else if ( optopt == 118 )
{
setlogmask(127);
}
else if ( ((*_ctype_b_loc())[v6] & 0x4000) != 0 )
{
fprintf(stderr, "Unknown option `-%c'.\n", v6);
}
else
{
fprintf(stderr, "Unknown option character `\\x%x'.\n", v6);
}
}
break;
}
LABEL_9:
exit(1);
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er13
char *v4; // r12
int v5; // eax
unsigned int v6; // er12
char *v7; // r14
int v8; // ebp
unsigned int v9; // eax
pthread_t v10; // rax
int *v11; // rax
char *v12; // rax
__uid_t v13; // eax
int *v14; // rax
char *v15; // rax
int *v16; // rax
char *v17; // rax
__int64 v18; // rbp
struct sigaction v20; // [rsp+0h] [rbp-C8h] BYREF

v3 = 0;
v4 = 0LL;
setlogmask(63);
opterr = 0;
while ( 2 )
{
v5 = getopt(argc, (char *const *)argv, "v:Vhdi:");
if ( v5 == -1 )
{
umask(0);
if ( v3 )
{
if ( func1() == 1 )
break;
}
else
{
openlog("synchronator", 41, 24);
v13 = getuid();
syslog(5, "Program started by User %d", v13);
}
lock_file = open("/var/run/synchronator/synchronator.pid", 524354, 420LL);
if ( lock_file >= 0 )
{
if ( lockf(lock_file, 2, 0LL) >= 0 )
{
v8 = lock_file;
ftruncate(lock_file, 0LL);
v9 = getpid();
dprintf(v8, "%d\n", v9);
v10 = pthread_self();
*((_QWORD *)&common_data + 16) = 0LL;
interfaceListenThread = v10;
watchMixerThread = v10;
statusQueryThread = v10;
mainThread = v10;
*((_QWORD *)&common_data + 17) = 0LL;
*((_QWORD *)&common_data + 18) = 0LL;
*((_QWORD *)&common_data + 19) = 0LL;
v20.sa_handler = (__sighandler_t)terminate;
sigfillset(&v20.sa_mask);
v20.sa_flags = 0;
if ( sigaction(2, &v20, 0LL) < 0 || sigaction(15, &v20, 0LL) < 0 )
{
v11 = _errno_location();
v12 = strerror(*v11);
syslog(3, "Failed to ignore signals: %s", v12);
closelog();
exit(1);
}
v18 = (unsigned int)func4(v4);
syslog(3, "Exiting: %i", v18);
closelog();
exit(v18);
}
v16 = _errno_location();
v17 = strerror(*v16);
syslog(4, "Exiting: only one instance of this application can run: %s", v17);
closelog();
exit(0);
}
v14 = _errno_location();
v15 = strerror(*v14);
syslog(3, "Failed to open lock file: %s (%s)", "/var/run/synchronator/synchronator.pid", v15);
closelog();
}
else
{
if ( v5 != 63 )
{
switch ( v5 )
{
case 'V':
fprintf(
stdout,
"%s %s\n\n%s\n",
"synchronator",
" (5762e34)",
"Copyright (C) 2013-2017 Muffinman <synchronator.hifi@gmail.com>\n"
"This is free software; see the source for copying conditions. There is NO\n"
"warranty; not even MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
exit(0);
case 'd':
v3 = 1;
continue;
case 'h':
fprintf(
stdout,
"Usage: %s %s",
*argv,
"[options...]\n"
" -V print version information\n"
" -h print this help message\n"
" -d daemonize application\n"
" -v0 set level of verbose to a minimum\n"
" -v1 set level of verbose to include informational messages\n"
" -v2 set level of verbose to include debug messages\n"
" -i specify path to a configuration file\n"
"\n"
"If no location for the configuration file is given, the application will look in:\n"
" primary /usr/local/etc/synchronator.conf\n"
" secondary /etc/synchronator.conf\n"
"\n"
"Synchronator is compiled with support for:\n"
" Serial interface\n"
" Message Queue (basic local control)\n");
exit(0);
case 'i':
v4 = optarg;
continue;
case 'v':
v7 = optarg;
if ( !strcmp(optarg, "0") )
{
setlogmask(15);
}
else if ( !strcmp(v7, "1") )
{
setlogmask(127);
}
else if ( !strcmp(v7, "2") )
{
setlogmask(255);
}
continue;
default:
goto LABEL_9;
}
}
v6 = optopt;
if ( optopt == 105 )
{
fprintf(stderr, "You forgot to include a path with option '-%c'.\n", 105LL);
}
else if ( optopt == 118 )
{
setlogmask(127);
}
else if ( ((*_ctype_b_loc())[v6] & 0x4000) != 0 )
{
fprintf(stderr, "Unknown option `-%c'.\n", v6);
}
else
{
fprintf(stderr, "Unknown option character `\\x%x'.\n", v6);
}
}
break;
}
LABEL_9:
exit(1);
}

original_code:
int main(int argc, char *argv[]) {
    int status = 0;
    int daemon = 0;
    char *customConfigLocation = ((void *)0);
    setlogmask(((1 << ((5)+1)) - 1));
    int option = 0;
    opterr = 0;
    while((option = getopt(argc, argv, "v:Vhdi:")) != -1)
        switch(option) {
            case 'v':
                if(strcmp(optarg, "0") == 0)
                    setlogmask(((1 << ((3)+1)) - 1));
                else if(strcmp(optarg, "1") == 0)
                    setlogmask(((1 << ((6)+1)) - 1));
                else if(strcmp(optarg, "2") == 0)
                    setlogmask(((1 << ((7)+1)) - 1));
                break;
            case 'V':
                fprintf(stdout, "%s %s\n\n%s\n", "synchronator" , " (5762e34)", "Copyright (C) 2013-2017 Muffinman <synchronator.hifi@gmail.com>\n" "This is free software; see the source for copying conditions. There is NO\n" "warranty; not even MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
                exit(0);
            case 'h':
                fprintf(stdout, "Usage: %s %s", argv[0], "[options...]\n" "   -V          print version information\n" "   -h          print this help message\n" "   -d          daemonize application\n" "   -v0         set level of verbose to a minimum\n" "   -v1         set level of verbose to include informational messages\n" "   -v2         set level of verbose to include debug messages\n" "   -i          specify path to a configuration file\n\n" "If no location for the configuration file is given, the application will look in:\n" "   primary     ""/usr/local" "/etc/" "synchronator" ".conf""\n" "   secondary   ""/etc/" "synchronator" ".conf""\n" "\n" "Synchronator is compiled with support for:\n" " Serial interface\n" " Message Queue (basic local control)\n");
                exit(0);
            case 'd':
                daemon = 1;
                break;
            case 'i':
                customConfigLocation = optarg;
                break;
            case '?':
                if(optopt == 'i')
                    fprintf(stderr, "You forgot to include a path with option '-%c'.\n", optopt);
                else if(optopt == 'v')
                    setlogmask(((1 << ((6)+1)) - 1));
                else if(((*__ctype_b_loc ())[(int) ((optopt))] & (unsigned short int) _ISprint))
                    fprintf(stderr, "Unknown option `-%c'.\n", optopt);
                else
                    fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
                exit(1);
            default:
                exit(1);
        }
    umask(0);
    if(daemon) {
        if((status = daemonize()) == 1)
            exit(1);
    }
    else {
        openlog( "synchronator" , 0x08 | 0x01 | 0x20, (3<<3));
        syslog(5, "Program started by User %d", getuid());
    }
    if((lock_file = open("/var/run/synchronator/" "synchronator" ".pid", 02|0100|02000000, (0400|0200|(0400 >> 3)|((0400 >> 3) >> 3)))) < 0) {
        syslog(3, "Failed to open lock file: %s (%s)", "/var/run/synchronator/" "synchronator" ".pid", strerror((*__errno_location ())));
        closelog();
        exit(1);
    }
    if(lockf(lock_file, 2, 0) < 0) {
        syslog(4, "Exiting: only one instance of this application can run: %s", strerror((*__errno_location ())));
        closelog();
        exit(0);
    }
    ftruncate(lock_file, 0);
    dprintf(lock_file, "%d\n", getpid());
    mainThread = statusQueryThread = watchMixerThread = interfaceListenThread = pthread_self();
    common_data.process = ((void *)0);
    common_data.interface = ((void *)0);
    common_data.volume = ((void *)0);
    common_data.mod = ((void *)0);
    struct sigaction signal_action;
    signal_action.__sigaction_handler.sa_handler = terminate;
    sigfillset(&signal_action.sa_mask);
    signal_action.sa_flags = 0;
    if(sigaction(2, &signal_action, ((void *)0)) < 0 || sigaction(15, &signal_action, ((void *)0)) < 0) {
        syslog(3, "Failed to ignore signals: %s", strerror((*__errno_location ())));
        closelog();
        exit(1);
    }
    status = synchronatord(customConfigLocation);
    syslog(3, "Exiting: %i", status);
    closelog();
    exit(status);
}

canonical_original_code:
int main(int argc, char *argv[]) {
    int status = 0;
    int daemon = 0;
    char *customConfigLocation = ((void *)0);
    setlogmask(((1 << ((5)+1)) - 1));
    int option = 0;
    opterr = 0;
    while((option = getopt(argc, argv, "v:Vhdi:")) != -1)
        switch(option) {
            case 'v':
                if(strcmp(optarg, "0") == 0)
                    setlogmask(((1 << ((3)+1)) - 1));
                else if(strcmp(optarg, "1") == 0)
                    setlogmask(((1 << ((6)+1)) - 1));
                else if(strcmp(optarg, "2") == 0)
                    setlogmask(((1 << ((7)+1)) - 1));
                break;
            case 'V':
                fprintf(stdout, "%s %s\n\n%s\n", "synchronator" , " (5762e34)", "Copyright (C) 2013-2017 Muffinman <synchronator.hifi@gmail.com>\n" "This is free software; see the source for copying conditions. There is NO\n" "warranty; not even MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
                exit(0);
            case 'h':
                fprintf(stdout, "Usage: %s %s", argv[0], "[options...]\n" "   -V          print version information\n" "   -h          print this help message\n" "   -d          daemonize application\n" "   -v0         set level of verbose to a minimum\n" "   -v1         set level of verbose to include informational messages\n" "   -v2         set level of verbose to include debug messages\n" "   -i          specify path to a configuration file\n\n" "If no location for the configuration file is given, the application will look in:\n" "   primary     ""/usr/local" "/etc/" "synchronator" ".conf""\n" "   secondary   ""/etc/" "synchronator" ".conf""\n" "\n" "Synchronator is compiled with support for:\n" " Serial interface\n" " Message Queue (basic local control)\n");
                exit(0);
            case 'd':
                daemon = 1;
                break;
            case 'i':
                customConfigLocation = optarg;
                break;
            case '?':
                if(optopt == 'i')
                    fprintf(stderr, "You forgot to include a path with option '-%c'.\n", optopt);
                else if(optopt == 'v')
                    setlogmask(((1 << ((6)+1)) - 1));
                else if(((*__ctype_b_loc ())[(int) ((optopt))] & (unsigned short) _ISprint))
                    fprintf(stderr, "Unknown option `-%c'.\n", optopt);
                else
                    fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
                exit(1);
            default:
                exit(1);
        }
    umask(0);
    if(daemon) {
        if((status = daemonize()) == 1)
            exit(1);
    }
    else {
        openlog( "synchronator" , 0x08 | 0x01 | 0x20, (3<<3));
        syslog(5, "Program started by User %d", getuid());
    }
    if((lock_file = open("/var/run/synchronator/" "synchronator" ".pid", 02|0100|02000000, (0400|0200|(0400 >> 3)|((0400 >> 3) >> 3)))) < 0) {
        syslog(3, "Failed to open lock file: %s (%s)", "/var/run/synchronator/" "synchronator" ".pid", strerror((*__errno_location ())));
        closelog();
        exit(1);
    }
    if(lockf(lock_file, 2, 0) < 0) {
        syslog(4, "Exiting: only one instance of this application can run: %s", strerror((*__errno_location ())));
        closelog();
        exit(0);
    }
    ftruncate(lock_file, 0);
    dprintf(lock_file, "%d\n", getpid());
    mainThread = statusQueryThread = watchMixerThread = interfaceListenThread = pthread_self();
    common_data.process = ((void *)0);
    common_data.interface = ((void *)0);
    common_data.volume = ((void *)0);
    common_data.mod = ((void *)0);
    struct sigaction signal_action;
    signal_action.__sigaction_handler.sa_handler = terminate;
    sigfillset(&signal_action.sa_mask);
    signal_action.sa_flags = 0;
    if(sigaction(2, &signal_action, ((void *)0)) < 0 || sigaction(15, &signal_action, ((void *)0)) < 0) {
        syslog(3, "Failed to ignore signals: %s", strerror((*__errno_location ())));
        closelog();
        exit(1);
    }
    status = synchronatord(customConfigLocation);
    syslog(3, "Exiting: %i", status);
    closelog();
    exit(status);
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: daemon int

variable_types: option int

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

user_defined_types: union sigval { int sival_int; void *sival_ptr; }


init

decompiled_code:
unsigned int init()
{
__int64 v0; // rax
__int64 v1; // rax
const char *v2; // rdx
__int64 v3; // rax
const char *v4; // rdx
__int64 v5; // rax
unsigned int result; // eax
__int64 v7; // rax
int *v8; // rax
char *v9; // rax
__int64 v10; // rax
const char *v11[2]; // [rsp+8h] [rbp-10h] BYREF

v11[0] = 0LL;
if ( chdir("/") < 0 )
{
v8 = _errno_location();
v9 = strerror(*v8);
syslog(3, "Unable to change working directory to root: %s", v9);
return 1;
}
syslog(6, "Checking presence and validity of required variables:");
config_lookup_string(&config, "data_type", (unsigned int)((_DWORD)&common_data + 16));
validateConfigBool(&config, "sync_2way", &common_data, 0LL);
validateConfigBool(&config, "diff_commands", (unsigned int)((_DWORD)&common_data + 8), 0LL);
v0 = config_lookup(&config, "query");
*(&common_data + 3) = v0 != 0;
if ( !v0 )
goto LABEL_29;
v7 = config_lookup(&config, "query.trigger");
if ( !v7 || *(_WORD *)(v7 + 8) != 7 || (v10 = config_setting_length(v7), (*(&common_data + 28) = v10) == 0) )
{
syslog(3, "[ERROR] Setting is not present or not formatted as array: query.trigger");
return 1;
}
if ( !*(&common_data + 3)
|| (unsigned int)validateConfigInt(
&config,
"query.interval",
(unsigned int)((_DWORD)&common_data + 116),
0xFFFFFFFFLL,
0LL,
300LL,
-1LL,
v10) != 1 )
{
LABEL_29:
*((_QWORD *)&common_data + 19) = &mod;
if ( (unsigned int)mod() != 1 )
{
v11[0] = 0LL;
config_lookup_string(&config, "volume.curve", v11);
v1 = getVolume(v11);
v2 = v11[0];
*((_QWORD *)&common_data + 18) = v1;
if ( !v1 )
{
syslog(3, "[Error] Volume curve is not recognised: %s", v2);
return 1;
}
syslog(6, "[OK] volume.curve: %s", v2);
if ( (**((unsigned int (***)(void))&common_data + 18))() != 1 )
{
v11[0] = 0LL;
config_lookup_string(&config, "interface", v11);
v3 = getInterface(v11);
v4 = v11[0];
*((_QWORD *)&common_data + 17) = v3;
if ( !v3 )
{
syslog(3, "[Error] Setting 'interface' is not recognised: %s", v4);
return 1;
}
syslog(6, "[OK] interface: %s", v4);
if ( (*(unsigned int (**)(void))(*((_QWORD *)&common_data + 17) + 16LL))() != 1
&& (unsigned int)initMixer() != 1 )
{
v5 = getProcessMethod((unsigned int)((_DWORD)&common_data + 16));
*((_QWORD *)&common_data + 16) = v5;
if ( !v5 )
{
syslog(3, "[Error] Setting 'data_type' is not recognised: %s", *((const char **)&common_data + 2));
return 1;
}
syslog(6, "[OK] data_type: %s", v11[0]);
if ( (*(unsigned int (**)(void))(*((_QWORD *)&common_data + 16) + 16LL))() != 1 )
{
(*(void (**)(void))(*((_QWORD *)&common_data + 18) + 32LL))();
if ( (unsigned int)initMsQ() != 1 )
{
result = pthread_mutex_init(&lockConfig, 0LL);
if ( !result )
return result;
syslog(3, "Failed to create config mutex: %i", result);
}
}
}
}
}
}
return 1;
}

canonical_decompiled_code:
unsigned int func3()
{
__int64 v0; // rax
__int64 v1; // rax
const char *v2; // rdx
__int64 v3; // rax
const char *v4; // rdx
__int64 v5; // rax
unsigned int result; // eax
__int64 v7; // rax
int *v8; // rax
char *v9; // rax
__int64 v10; // rax
const char *v11[2]; // [rsp+8h] [rbp-10h] BYREF

v11[0] = 0LL;
if ( chdir("/") < 0 )
{
v8 = _errno_location();
v9 = strerror(*v8);
syslog(3, "Unable to change working directory to root: %s", v9);
return 1;
}
syslog(6, "Checking presence and validity of required variables:");
config_lookup_string(&config, "data_type", (unsigned int)((_DWORD)&common_data + 16));
validateConfigBool(&config, "sync_2way", &common_data, 0LL);
validateConfigBool(&config, "diff_commands", (unsigned int)((_DWORD)&common_data + 8), 0LL);
v0 = config_lookup(&config, "query");
*(&common_data + 3) = v0 != 0;
if ( !v0 )
goto LABEL_29;
v7 = config_lookup(&config, "query.trigger");
if ( !v7 || *(_WORD *)(v7 + 8) != 7 || (v10 = config_setting_length(v7), (*(&common_data + 28) = v10) == 0) )
{
syslog(3, "[ERROR] Setting is not present or not formatted as array: query.trigger");
return 1;
}
if ( !*(&common_data + 3)
|| (unsigned int)validateConfigInt(
&config,
"query.interval",
(unsigned int)((_DWORD)&common_data + 116),
0xFFFFFFFFLL,
0LL,
300LL,
-1LL,
v10) != 1 )
{
LABEL_29:
*((_QWORD *)&common_data + 19) = &mod;
if ( (unsigned int)mod() != 1 )
{
v11[0] = 0LL;
config_lookup_string(&config, "volume.curve", v11);
v1 = getVolume(v11);
v2 = v11[0];
*((_QWORD *)&common_data + 18) = v1;
if ( !v1 )
{
syslog(3, "[Error] Volume curve is not recognised: %s", v2);
return 1;
}
syslog(6, "[OK] volume.curve: %s", v2);
if ( (**((unsigned int (***)(void))&common_data + 18))() != 1 )
{
v11[0] = 0LL;
config_lookup_string(&config, "interface", v11);
v3 = getInterface(v11);
v4 = v11[0];
*((_QWORD *)&common_data + 17) = v3;
if ( !v3 )
{
syslog(3, "[Error] Setting 'interface' is not recognised: %s", v4);
return 1;
}
syslog(6, "[OK] interface: %s", v4);
if ( (*(unsigned int (**)(void))(*((_QWORD *)&common_data + 17) + 16LL))() != 1
&& (unsigned int)initMixer() != 1 )
{
v5 = getProcessMethod((unsigned int)((_DWORD)&common_data + 16));
*((_QWORD *)&common_data + 16) = v5;
if ( !v5 )
{
syslog(3, "[Error] Setting 'data_type' is not recognised: %s", *((const char **)&common_data + 2));
return 1;
}
syslog(6, "[OK] data_type: %s", v11[0]);
if ( (*(unsigned int (**)(void))(*((_QWORD *)&common_data + 16) + 16LL))() != 1 )
{
(*(void (**)(void))(*((_QWORD *)&common_data + 18) + 32LL))();
if ( (unsigned int)initMsQ() != 1 )
{
result = pthread_mutex_init(&lockConfig, 0LL);
if ( !result )
return result;
syslog(3, "Failed to create config mutex: %i", result);
}
}
}
}
}
}
return 1;
}

original_code:
static int init(void) {
    int status;
    config_setting_t *conf_setting = ((void *)0);
    const char *conf_value = ((void *)0);
    if(chdir("/") < 0) {
        syslog(3, "Unable to change working directory to root: %s", strerror((*__errno_location ())));
        return 1;
    }
    syslog(6, "Checking presence and validity of required variables:");
    config_lookup_string(&config, "data_type", &common_data.dataType);
    validateConfigBool(&config, "sync_2way", &common_data.sync_2way, 0);
    validateConfigBool(&config, "diff_commands", &common_data.diff_commands, 0);
    common_data.send_query = config_lookup(&config, "query") ? 1:0;
    if(common_data.send_query && ((conf_setting = config_lookup(&config, "query.trigger")) == ((void *)0) ||
    ((conf_setting)->type == 7) == (0) || (common_data.statusQueryLength =
    config_setting_length(conf_setting)) == 0)) {
        syslog(3, "[ERROR] Setting is not present or not formatted as array: query.trigger");
        return 1;
    }
    if(common_data.send_query && validateConfigInt(&config, "query.interval",
    &common_data.statusQueryInterval, -1, 0, 300, -1) == 1)
        return 1;
    common_data.mod = &mod;
    if(common_data.mod->init() == 1)
        return 1;
    conf_value = ((void *)0);
    config_lookup_string(&config, "volume.curve", &conf_value);
    if(!(common_data.volume = getVolume(&conf_value))) {
        syslog(3, "[Error] Volume curve is not recognised: %s", conf_value);
        return 1;
    }
    else {
        syslog(6, "[OK] volume.curve: %s", conf_value);
    }
    if(common_data.volume->init() == 1)
        return 1;
    conf_value = ((void *)0);
    config_lookup_string(&config, "interface", &conf_value);
    if(!(common_data.interface = getInterface(&conf_value))) {
        syslog(3, "[Error] Setting 'interface' is not recognised: %s", conf_value);
        return 1;
    }
    else {
        syslog(6, "[OK] interface: %s", conf_value);
    }
    if(common_data.interface->init() == 1)
        return 1;
    if(initMixer() == 1)
        return 1;
    if(!(common_data.process = getProcessMethod(&common_data.dataType))) {
        syslog(3, "[Error] Setting 'data_type' is not recognised: %s", common_data.dataType);
        return 1;
    }
    else {
        syslog(6, "[OK] data_type: %s", conf_value);
    }
    if(common_data.process->init() == 1)
        return 1;
    common_data.volume->regenerateMultipliers();
    if(initMsQ() == 1)
        return 1;
    if((status = pthread_mutex_init(&lockConfig, ((void *)0))) != 0) {
        syslog(3, "Failed to create config mutex: %i", status);
        return 1;
    }
    return 0;
}

canonical_original_code:
static int init() {
    int status;
    struct config_setting_t *conf_setting = ((void *)0);
    char *conf_value = ((void *)0);
    if(chdir("/") < 0) {
        syslog(3, "Unable to change working directory to root: %s", strerror((*__errno_location ())));
        return 1;
    }
    syslog(6, "Checking presence and validity of required variables:");
    config_lookup_string(&config, "data_type", &common_data.dataType);
    validateConfigBool(&config, "sync_2way", &common_data.sync_2way, 0);
    validateConfigBool(&config, "diff_commands", &common_data.diff_commands, 0);
    common_data.send_query = config_lookup(&config, "query") ? 1:0;
    if(common_data.send_query && ((conf_setting = config_lookup(&config, "query.trigger")) == ((void *)0) ||
    ((conf_setting)->type == 7) == (0) || (common_data.statusQueryLength =
    config_setting_length(conf_setting)) == 0)) {
        syslog(3, "[ERROR] Setting is not present or not formatted as array: query.trigger");
        return 1;
    }
    if(common_data.send_query && validateConfigInt(&config, "query.interval",
    &common_data.statusQueryInterval, -1, 0, 300, -1) == 1)
        return 1;
    common_data.mod = &mod;
    if(common_data.mod->init() == 1)
        return 1;
    conf_value = ((void *)0);
    config_lookup_string(&config, "volume.curve", &conf_value);
    if(!(common_data.volume = getVolume(&conf_value))) {
        syslog(3, "[Error] Volume curve is not recognised: %s", conf_value);
        return 1;
    }
    else {
        syslog(6, "[OK] volume.curve: %s", conf_value);
    }
    if(common_data.volume->init() == 1)
        return 1;
    conf_value = ((void *)0);
    config_lookup_string(&config, "interface", &conf_value);
    if(!(common_data.interface = getInterface(&conf_value))) {
        syslog(3, "[Error] Setting 'interface' is not recognised: %s", conf_value);
        return 1;
    }
    else {
        syslog(6, "[OK] interface: %s", conf_value);
    }
    if(common_data.interface->init() == 1)
        return 1;
    if(initMixer() == 1)
        return 1;
    if(!(common_data.process = getProcessMethod(&common_data.dataType))) {
        syslog(3, "[Error] Setting 'data_type' is not recognised: %s", common_data.dataType);
        return 1;
    }
    else {
        syslog(6, "[OK] data_type: %s", conf_value);
    }
    if(common_data.process->init() == 1)
        return 1;
    common_data.volume->regenerateMultipliers();
    if(initMsQ() == 1)
        return 1;
    if((status = pthread_mutex_init(&lockConfig, ((void *)0))) != 0) {
        syslog(3, "Failed to create config mutex: %i", status);
        return 1;
    }
    return 0;
}

variable_types: struct config_setting_t { char *name; short type; short format; union config_value_t { int ival; long long llval; double fval; char *sval; struct config_list_t { unsigned int length; struct config_setting_t **elements; } *list; } value; struct config_setting_t *parent; struct config_t { struct config_setting_t *root; void (*destructor)(void *); int options; unsigned short tab_width; short default_format; char *include_dir; char *error_text; char *error_file; int error_line; enum config_error_t { CONFIG_ERR_NONE=0, CONFIG_ERR_FILE_IO=1, CONFIG_ERR_PARSE=2 } error_type; char **filenames; unsigned int num_filenames; } *config; void *hook; unsigned int line; char *file; } *

user_defined_types: struct config_setting_t { char *name; short type; short format; union config_value_t value; struct config_setting_t *parent; struct config_t *config; void *hook; unsigned int line; char *file; }

user_defined_types: union config_value_t { int ival; long long llval; double fval; char *sval; struct config_list_t *list; }

user_defined_types: struct config_t { struct config_setting_t *root; void (*destructor)(void *); int options; unsigned short tab_width; short default_format; char *include_dir; char *error_text; char *error_file; int error_line; enum config_error_t error_type; char **filenames; unsigned int num_filenames; }


getModVolumeResponse

decompiled_code:
const char **__fastcall getModVolumeResponse(char *s1)
{
const char **v1; // r12
void **v2; // rbx

if ( !s1 )
return 0LL;
v1 = (const char **)modVolumeResponseList;
if ( modVolumeResponseList )
{
v2 = &modVolumeResponseList;
while ( strcasecmp(s1, *v1) )
{
v1 = (const char **)v2[1];
++v2;
if ( !v1 )
goto LABEL_7;
}
return v1;
}
LABEL_7:
syslog(4, "[NOTICE] Setting 'volume.response.mode' is not recognised, ignoring: %s", s1);
return 0LL;
}

canonical_decompiled_code:
const char **__fastcall func1(char *s1)
{
const char **v1; // r12
void **v2; // rbx

if ( !s1 )
return 0LL;
v1 = (const char **)modVolumeResponseList;
if ( modVolumeResponseList )
{
v2 = &modVolumeResponseList;
while ( strcasecmp(s1, *v1) )
{
v1 = (const char **)v2[1];
++v2;
if ( !v1 )
goto LABEL_7;
}
return v1;
}
LABEL_7:
syslog(4, "[NOTICE] Setting 'volume.response.mode' is not recognised, ignoring: %s", s1);
return 0LL;
}

original_code:
static modVolumeResponse_t *getModVolumeResponse(const char *name) {
    modVolumeResponse_t **modVolumeResponse;
    if (!name)
        return ((void *)0);
    for (modVolumeResponse=modVolumeResponseList; *modVolumeResponse; modVolumeResponse++)
        if (!strcasecmp(name, (*modVolumeResponse)->name))
            return *modVolumeResponse;
    syslog(4, "[NOTICE] Setting 'volume.response.mode' is not recognised, ignoring: %s", name);
    return ((void *)0);
}

canonical_original_code:
static struct modVolumeResponse_t *getModVolumeResponse(char *name) {
    struct modVolumeResponse_t **modVolumeResponse;
    if (!name)
        return ((void *)0);
    for (modVolumeResponse=modVolumeResponseList; *modVolumeResponse; modVolumeResponse++)
        if (!strcasecmp(name, (*modVolumeResponse)->name))
            return *modVolumeResponse;
    syslog(4, "[NOTICE] Setting 'volume.response.mode' is not recognised, ignoring: %s", name);
    return ((void *)0);
}

variable_types:
name
char *

variable_types: struct modVolumeResponse_t { char *name; void (*help)(); int (*init)(); void (*process)(double *volume_level); void (*deinit)(); } **


getmodCommand

decompiled_code:
const char **__fastcall getmodCommand(char *s1)
{
const char **v1; // r12
void **v2; // rbx

if ( !s1 )
return 0LL;
v1 = (const char **)modCommandList;
if ( modCommandList )
{
v2 = &modCommandList;
while ( strcasecmp(s1, *v1) )
{
v1 = (const char **)v2[1];
++v2;
if ( !v1 )
goto LABEL_7;
}
return v1;
}
LABEL_7:
syslog(4, "[NOTICE] Setting 'command_mod.type' is not recognised, ignoring: %s", s1);
return 0LL;
}

canonical_decompiled_code:
const char **__fastcall func2(char *s1)
{
const char **v1; // r12
void **v2; // rbx

if ( !s1 )
return 0LL;
v1 = (const char **)modCommandList;
if ( modCommandList )
{
v2 = &modCommandList;
while ( strcasecmp(s1, *v1) )
{
v1 = (const char **)v2[1];
++v2;
if ( !v1 )
goto LABEL_7;
}
return v1;
}
LABEL_7:
syslog(4, "[NOTICE] Setting 'command_mod.type' is not recognised, ignoring: %s", s1);
return 0LL;
}

original_code:
static modCommand_t *getmodCommand(const char *name) {
    modCommand_t **modCommand;
    if (!name)
        return ((void *)0);
    for (modCommand=modCommandList; *modCommand; modCommand++)
        if (!strcasecmp(name, (*modCommand)->name))
            return *modCommand;
    syslog(4, "[NOTICE] Setting 'command_mod.type' is not recognised, ignoring: %s", name);
    return ((void *)0);
}

canonical_original_code:
static struct modCommand_t *getmodCommand(char *name) {
    struct modCommand_t **modCommand;
    if (!name)
        return ((void *)0);
    for (modCommand=modCommandList; *modCommand; modCommand++)
        if (!strcasecmp(name, (*modCommand)->name))
            return *modCommand;
    syslog(4, "[NOTICE] Setting 'command_mod.type' is not recognised, ignoring: %s", name);
    return ((void *)0);
}

variable_types: struct modCommand_t { char *name; void (*help)(); int (*init)(int x, ...); void (*produce)(void *message); int (*process)(void *command, int command_length); void (*deinit)(); } **


deinit

decompiled_code:
void deinit()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
static void deinit(void) {
}

canonical_original_code:
static void deinit() {
}


init

decompiled_code:
__int64 init()
{
__int64 v1; // rbx
const char **v2; // rax
__int64 v3; // rbx
const char **v4; // rax
char *s1[2]; // [rsp+8h] [rbp-10h] BYREF

s1[0] = 0LL;
if ( (unsigned int)config_lookup_string(&config, "volume.response.mode", s1) )
{
v1 = common_data[19];
v2 = getModVolumeResponse(s1[0]);
*(_QWORD *)(v1 + 8) = v2;
if ( v2 )
syslog(6, "[OK] volume.response.mode: %s", s1[0]);
}
if ( !(unsigned int)config_lookup_string(&config, "command_mod.type", s1) )
return 0LL;
v3 = common_data[19];
v4 = getmodCommand(s1[0]);
*(_QWORD *)(v3 + 16) = v4;
if ( !v4 )
return 0LL;
syslog(6, "[OK] command_mod.type: %s", s1[0]);
return 0LL;
}

canonical_decompiled_code:
__int64 func3()
{
__int64 v1; // rbx
const char **v2; // rax
__int64 v3; // rbx
const char **v4; // rax
char *s1[2]; // [rsp+8h] [rbp-10h] BYREF

s1[0] = 0LL;
if ( (unsigned int)config_lookup_string(&config, "volume.response.mode", s1) )
{
v1 = common_data[19];
v2 = func1(s1[0]);
*(_QWORD *)(v1 + 8) = v2;
if ( v2 )
syslog(6, "[OK] volume.response.mode: %s", s1[0]);
}
if ( !(unsigned int)config_lookup_string(&config, "command_mod.type", s1) )
return 0LL;
v3 = common_data[19];
v4 = func2(s1[0]);
*(_QWORD *)(v3 + 16) = v4;
if ( !v4 )
return 0LL;
syslog(6, "[OK] command_mod.type: %s", s1[0]);
return 0LL;
}

original_code:
static int init(void) {
    const char* conf_value = ((void *)0);
    if(config_lookup_string(&config, "volume.response.mode", &conf_value) != (0) &&
    (common_data.mod->volumeResponse = getModVolumeResponse(conf_value))) {
        syslog(6, "[OK] volume.response.mode: %s", conf_value);
    }
    if(config_lookup_string(&config, "command_mod.type", &conf_value) != (0) &&
    (common_data.mod->command = getmodCommand(conf_value))) {
        syslog(6, "[OK] command_mod.type: %s", conf_value);
    }
    return 0;
}

canonical_original_code:
static int init() {
    char *conf_value = ((void *)0);
    if(config_lookup_string(&config, "volume.response.mode", &conf_value) != (0) &&
    (common_data.mod->volumeResponse = getModVolumeResponse(conf_value))) {
        syslog(6, "[OK] volume.response.mode: %s", conf_value);
    }
    if(config_lookup_string(&config, "command_mod.type", &conf_value) != (0) &&
    (common_data.mod->command = getmodCommand(conf_value))) {
        syslog(6, "[OK] command_mod.type: %s", conf_value);
    }
    return 0;
}


listen

decompiled_code:
void __fastcall __noreturn listen(__sigset_t *newmask)
{
__int64 v1; // rbx
char *v2; // rbp
int v3; // eax
ssize_t v4; // rax
ssize_t v5; // r12
ssize_t v6; // rsi
__int64 v7; // rax
int *v8; // rax
char *v9; // rax
int *v10; // rax
struct pollfd fds; // [rsp+8h] [rbp-90h] BYREF
char v12[136]; // [rsp+10h] [rbp-88h] BYREF

if ( pthread_sigmask(0, newmask, 0LL) < 0 )
{
v10 = _errno_location();
syslog(3, "Failed to ignore signals in listen: %i", (unsigned int)*v10);
pthread_kill(mainThread, 15);
pause();
}
LODWORD(v1) = 0;
fds.fd = interfaceFD;
fds.events = 1;
while ( 1 )
{
LABEL_4:
v2 = &v12[(int)v1];
while ( 1 )
{
v3 = poll(&fds, 1uLL, -1);
if ( v3 < 0 )
{
v8 = _errno_location();
v9 = strerror(*v8);
syslog(3, "Failed to poll serial interface: %s", v9);
pthread_kill(mainThread, 15);
pause();
}
else if ( !v3 )
{
goto LABEL_4;
}
if ( (fds.revents & 1) == 0 )
break;
v4 = read(interfaceFD, v2, 99 - (int)v1);
v5 = v4;
if ( v4 <= 0 )
break;
v6 = v4 + (int)v1;
v7 = common_data[16];
v12[v6] = 0;
v1 = (*(unsigned int (__fastcall **)(char *))(v7 + 56))(v12);
syslog(7, "Leftovers in buffer (bytes read, #leftovers): %s (%i, %i)", v12, v5, v1);
if ( (int)v1 <= 49 )
break;
v2 = v12;
LODWORD(v1) = 0;
}
}
}

canonical_decompiled_code:
void __fastcall __noreturn func0(__sigset_t *newmask)
{
__int64 v1; // rbx
char *v2; // rbp
int v3; // eax
ssize_t v4; // rax
ssize_t v5; // r12
ssize_t v6; // rsi
__int64 v7; // rax
int *v8; // rax
char *v9; // rax
int *v10; // rax
struct pollfd fds; // [rsp+8h] [rbp-90h] BYREF
char v12[136]; // [rsp+10h] [rbp-88h] BYREF

if ( pthread_sigmask(0, newmask, 0LL) < 0 )
{
v10 = _errno_location();
syslog(3, "Failed to ignore signals in listen: %i", (unsigned int)*v10);
pthread_kill(mainThread, 15);
pause();
}
LODWORD(v1) = 0;
fds.fd = interfaceFD;
fds.events = 1;
while ( 1 )
{
LABEL_4:
v2 = &v12[(int)v1];
while ( 1 )
{
v3 = poll(&fds, 1uLL, -1);
if ( v3 < 0 )
{
v8 = _errno_location();
v9 = strerror(*v8);
syslog(3, "Failed to poll serial interface: %s", v9);
pthread_kill(mainThread, 15);
pause();
}
else if ( !v3 )
{
goto LABEL_4;
}
if ( (fds.revents & 1) == 0 )
break;
v4 = read(interfaceFD, v2, 99 - (int)v1);
v5 = v4;
if ( v4 <= 0 )
break;
v6 = v4 + (int)v1;
v7 = common_data[16];
v12[v6] = 0;
v1 = (*(unsigned int (__fastcall **)(char *))(v7 + 56))(v12);
syslog(7, "Leftovers in buffer (bytes read, #leftovers): %s (%i, %i)", v12, v5, v1);
if ( (int)v1 <= 49 )
break;
v2 = v12;
LODWORD(v1) = 0;
}
}
}

original_code:
static void *listen(void *arg) {
    sigset_t *thread_sigmask = (sigset_t *)arg;
    if(pthread_sigmask(0, thread_sigmask, ((void *)0)) < 0) {
        syslog(3, "Failed to ignore signals in listen: %i", (*__errno_location ()));
        pthread_kill(mainThread, 15);
        pause();
    }
    struct pollfd pollFD[1];
    pollFD[0].fd = interfaceFD;
    pollFD[0].events = 0x001;
    ssize_t bytesRead;
    unsigned char interfaceRxBuffer[100];
    unsigned char *interfaceRxPtr;
    int leftovers = 0;
    int status = 0;
    while(1) {
        interfaceRxPtr = interfaceRxBuffer;
        if(leftovers >= (100/2))
            leftovers = 0;
        interfaceRxPtr += leftovers;
        status = poll(pollFD, 1, -1);
        if(status < 0) {
            syslog(3, "Failed to poll serial interface: %s", strerror((*__errno_location ())));
            pthread_kill(mainThread, 15);
            pause();
        }
        else if(status == 0)
            continue;
        if(pollFD[0].revents & 0x001) {
            bytesRead = read(interfaceFD, interfaceRxPtr, 100 -1-leftovers);
            if(bytesRead <= 0)
                continue;
            interfaceRxBuffer[bytesRead+leftovers] = '\0';
            leftovers = common_data.process->strip_raw_input(interfaceRxBuffer, bytesRead+leftovers);
            syslog(7, "Leftovers in buffer (bytes read, #leftovers): %s (%i, %i)", interfaceRxBuffer, bytesRead, leftovers);
        }
    }
}

canonical_original_code:
static void *listen(void *arg) {
    struct __sigset_t *thread_sigmask = (sigset_t *)arg;
    if(pthread_sigmask(0, thread_sigmask, ((void *)0)) < 0) {
        syslog(3, "Failed to ignore signals in listen: %i", (*__errno_location ()));
        pthread_kill(mainThread, 15);
        pause();
    }
    struct pollfd pollFD[1];
    pollFD[0].fd = interfaceFD;
    pollFD[0].events = 0x001;
    long bytesRead;
    unsigned char interfaceRxBuffer[100];
    unsigned char *interfaceRxPtr;
    int leftovers = 0;
    int status = 0;
    while(1) {
        interfaceRxPtr = interfaceRxBuffer;
        if(leftovers >= (100/2))
            leftovers = 0;
        interfaceRxPtr += leftovers;
        status = poll(pollFD, 1, -1);
        if(status < 0) {
            syslog(3, "Failed to poll serial interface: %s", strerror((*__errno_location ())));
            pthread_kill(mainThread, 15);
            pause();
        }
        else if(status == 0)
            continue;
        if(pollFD[0].revents & 0x001) {
            bytesRead = read(interfaceFD, interfaceRxPtr, 100 -1-leftovers);
            if(bytesRead <= 0)
                continue;
            interfaceRxBuffer[bytesRead+leftovers] = '\0';
            leftovers = common_data.process->strip_raw_input(interfaceRxBuffer, bytesRead+leftovers);
            syslog(7, "Leftovers in buffer (bytes read, #leftovers): %s (%i, %i)", interfaceRxBuffer, bytesRead, leftovers);
        }
    }
}

variable_types: struct pollfd { int fd; short events; short revents; } [1]

variable_types: bytesRead long

variable_types:
interfaceRxBuffer
unsigned char [100]

variable_types:
interfaceRxPtr
unsigned char *

variable_types: leftovers int


send

decompiled_code:
__int64 __fastcall send(void *buf, size_t n)
{
__int64 result; // rax
int *v3; // rax
char *v4; // rax

pthread_setcancelstate(1, 0LL);
pthread_mutex_lock(&interfaceLock);
if ( write(interfaceFD, buf, n) == -1 )
{
pthread_mutex_unlock(&interfaceLock);
pthread_setcancelstate(0, 0LL);
v3 = _errno_location();
v4 = strerror(*v3);
syslog(3, "Failed to write to serial port: %s", v4);
result = 1LL;
}
else
{
pthread_mutex_unlock(&interfaceLock);
pthread_setcancelstate(0, 0LL);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(void *buf, size_t n)
{
__int64 result; // rax
int *v3; // rax
char *v4; // rax

pthread_setcancelstate(1, 0LL);
pthread_mutex_lock(&interfaceLock);
if ( write(interfaceFD, buf, n) == -1 )
{
pthread_mutex_unlock(&interfaceLock);
pthread_setcancelstate(0, 0LL);
v3 = _errno_location();
v4 = strerror(*v3);
syslog(3, "Failed to write to serial port: %s", v4);
result = 1LL;
}
else
{
pthread_mutex_unlock(&interfaceLock);
pthread_setcancelstate(0, 0LL);
result = 0LL;
}
return result;
}

original_code:
static int send(const void *message, size_t messageLength) {
    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, ((void *)0));
    pthread_mutex_lock(&interfaceLock);
    if(write(interfaceFD, message, messageLength) == -1) {
        pthread_mutex_unlock(&interfaceLock);
        pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, ((void *)0));
        syslog(3, "Failed to write to serial port: %s", strerror((*__errno_location ())));
        return 1;
    }
    pthread_mutex_unlock(&interfaceLock);
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, ((void *)0));
    return 0;
}

canonical_original_code:
static int send(void *message, unsigned long messageLength) {
    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, ((void *)0));
    pthread_mutex_lock(&interfaceLock);
    if(write(interfaceFD, message, messageLength) == -1) {
        pthread_mutex_unlock(&interfaceLock);
        pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, ((void *)0));
        syslog(3, "Failed to write to serial port: %s", strerror((*__errno_location ())));
        return 1;
    }
    pthread_mutex_unlock(&interfaceLock);
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, ((void *)0));
    return 0;
}

variable_types:
message
void *

variable_types:
messageLength
unsigned long


deinit

decompiled_code:
__int64 deinit()
{
int v0; // ebp

v0 = interfaceFD;
if ( interfaceFD > 0 )
{
tcsetattr(interfaceFD, 0, &termiosOriginalAttr);
close(v0);
}
pthread_mutex_destroy(&interfaceLock);
return 0LL;
}

canonical_decompiled_code:
__int64 func2()
{
int v0; // ebp

v0 = interfaceFD;
if ( interfaceFD > 0 )
{
tcsetattr(interfaceFD, 0, &termiosOriginalAttr);
close(v0);
}
pthread_mutex_destroy(&interfaceLock);
return 0LL;
}

original_code:
static int deinit(void) {
    if(interfaceFD > 0) {
        tcsetattr(interfaceFD, 0, &termiosOriginalAttr);
        close(interfaceFD);
    }
    pthread_mutex_destroy(&interfaceLock);
    return 0;
}

canonical_original_code:
static int deinit() {
    if(interfaceFD > 0) {
        tcsetattr(interfaceFD, 0, &termiosOriginalAttr);
        close(interfaceFD);
    }
    pthread_mutex_destroy(&interfaceLock);
    return 0;
}