senddgm

decompiled_code:
__int64 __fastcall senddgm(int fd, char *name, __int16 a3, const void *a4, int a5)
{
unsigned int v7; // eax
struct sockaddr v10; // [rsp+0h] [rbp-28h] BYREF

v10.sa_family = 2;
*(_WORD *)v10.sa_data = _bswap_16(a3);
*(_DWORD *)&v10.sa_data[2] = get_inaddr(name);
v7 = sendto(fd, a4, a5, 0, &v10, 0x10u);
if ( a5 == v7 )
return v7;
perror("sendto");
return (unsigned int)-1;
}

canonical_decompiled_code:
__int64 __fastcall func8(int fd, char *name, __int16 a3, const void *a4, int a5)
{
unsigned int v7; // eax
struct sockaddr v10; // [rsp+0h] [rbp-28h] BYREF

v10.sa_family = 2;
*(_WORD *)v10.sa_data = func0(a3);
*(_DWORD *)&v10.sa_data[2] = func5(name);
v7 = sendto(fd, a4, a5, 0, &v10, 0x10u);
if ( a5 == v7 )
return v7;
perror("sendto");
return (unsigned int)-1;
}

original_code:
int senddgm(int sock, char * peer, int port, char * pkt, int n){
  struct sockaddr_in addr;
  struct hostent * hp;
  addr.sin_family = 2;
  addr.sin_port = htons(port);
  addr.sin_addr.s_addr = get_inaddr(peer);
  if(addr.sin_addr.s_addr < 0){
    perror("get_inaddr");
    return(-1);
  }
  int m = 0;
  m = sendto(sock, pkt, n, 0, (struct sockaddr *)&addr, sizeof(addr));
  if(m != n){
    perror("sendto");
    return(-1);
  }
  return(m);
}

canonical_original_code:
int senddgm(int sock, char *peer, int port, char *pkt, int n){
  struct sockaddr_in addr;
  struct hostent *hp;
  addr.sin_family = 2;
  addr.sin_port = htons(port);
  addr.sin_addr.s_addr = get_inaddr(peer);
  if(addr.sin_addr.s_addr < 0){
    perror("get_inaddr");
    return(-1);
  }
  int m = 0;
  m = sendto(sock, pkt, n, 0, (struct sockaddr *)&addr, sizeof(addr));
  if(m != n){
    perror("sendto");
    return(-1);
  }
  return(m);
}

variable_types: port int

variable_types: n int

variable_types: 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]; }

variable_types: m int

user_defined_types: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; }


sendveip

repo: ocdet/veip

original_code:
int sendveip(int sock,char * peer, char * pkt, int n){
  struct vphdr * vp;
  struct ether_header * ehdr;
  char * p;
  u_short offs = 0;
  int ret;
  ehdr = malloc(sizeof(struct ether_header));
  if(ehdr == ((void *)0)) return(-1);
  memcpy(ehdr, (struct ether_header *)pkt, sizeof(struct ether_header));
  vp = malloc(sizeof(struct vphdr));
  if(vp == ((void *)0)){
    free(ehdr);
    return(-1);
  }
  if(ntohs(ehdr->ether_type) == 0x0806){
    vp->vp_frg = htons(0x8000);
  }else if(ntohs(ehdr->ether_type) == 0x8035){
    vp->vp_frg = htons(0x8000);
  }else{
    vp->vp_frg = htons(0x1000);
  }
  vp->vp_len = htons(n);
  vp->vp_id = cksum(pkt, n);
  vp->vp_pad = 0x0000;
  p = malloc(sizeof(struct vphdr) + n);
  if(p == ((void *)0)){
    free(vp); free(ehdr);
    return(-1);
  }
  memset(p, 0, sizeof(struct vphdr) + n);
  memcpy(p, vp, sizeof(struct vphdr));
  int m = 0;
  if(n > frag_size){
    m = frag_size;
    vp->vp_frg = htons(0x2000);
    vp->vp_len = htons(m);
    memcpy(p, vp, sizeof(struct vphdr));
    memcpy(p + sizeof(struct vphdr), pkt, m);
    ret = senddgm(sock, peer, 59999, p, sizeof(struct vphdr) + m);
    if(ret < 0){
      free(vp); free(ehdr); free(p);
      return(-1);
    }
    vp->vp_frg = htons(0x4000);
    vp->vp_len = htons(n - m);
    memcpy(p, vp, sizeof(struct vphdr));
  }
  free(vp); free(ehdr);
  memcpy(p + sizeof(struct vphdr), pkt + m, n - m);
  ret = senddgm(sock, peer, 59999, p, sizeof(struct vphdr) + n - m);
  free(p);
  if(ret < 0) return(-1);
  return(ret);
}

canonical_original_code:
int sendveip(int sock, char *peer, char *pkt, int n){
  struct vphdr *vp;
  struct ether_header *ehdr;
  char *p;
  unsigned short offs = 0;
  int ret;
  ehdr = malloc(sizeof(struct ether_header));
  if(ehdr == ((void *)0)) return(-1);
  memcpy(ehdr, (struct ether_header *)pkt, sizeof(struct ether_header));
  vp = malloc(sizeof(struct vphdr));
  if(vp == ((void *)0)){
    free(ehdr);
    return(-1);
  }
  if(ntohs(ehdr->ether_type) == 0x0806){
    vp->vp_frg = htons(0x8000);
  }else if(ntohs(ehdr->ether_type) == 0x8035){
    vp->vp_frg = htons(0x8000);
  }else{
    vp->vp_frg = htons(0x1000);
  }
  vp->vp_len = htons(n);
  vp->vp_id = cksum(pkt, n);
  vp->vp_pad = 0x0000;
  p = malloc(sizeof(struct vphdr) + n);
  if(p == ((void *)0)){
    free(vp); free(ehdr);
    return(-1);
  }
  memset(p, 0, sizeof(struct vphdr) + n);
  memcpy(p, vp, sizeof(struct vphdr));
  int m = 0;
  if(n > frag_size){
    m = frag_size;
    vp->vp_frg = htons(0x2000);
    vp->vp_len = htons(m);
    memcpy(p, vp, sizeof(struct vphdr));
    memcpy(p + sizeof(struct vphdr), pkt, m);
    ret = senddgm(sock, peer, 59999, p, sizeof(struct vphdr) + m);
    if(ret < 0){
      free(vp); free(ehdr); free(p);
      return(-1);
    }
    vp->vp_frg = htons(0x4000);
    vp->vp_len = htons(n - m);
    memcpy(p, vp, sizeof(struct vphdr));
  }
  free(vp); free(ehdr);
  memcpy(p + sizeof(struct vphdr), pkt + m, n - m);
  ret = senddgm(sock, peer, 59999, p, sizeof(struct vphdr) + n - m);
  free(p);
  if(ret < 0) return(-1);
  return(ret);
}

variable_types: sock int

variable_types:
peer
char *

variable_types:
pkt
char *

variable_types: struct vphdr { unsigned short vp_frg; unsigned short vp_len; unsigned short vp_id; unsigned short vp_pad; } *

variable_types: struct ether_header { unsigned char ether_dhost[6]; unsigned char ether_shost[6]; unsigned short ether_type; } *

variable_types:
p
char *

variable_types:
offs
unsigned short

variable_types: ret int

return_type: int


crecvsock

decompiled_code:
__int64 __fastcall crecvsock(__int16 a1)
{
int v1; // eax
unsigned int v2; // er12
socklen_t v3; // edx
struct sockaddr v5[2]; // [rsp+0h] [rbp-28h] BYREF

v1 = socket(2, 2, 0);
if ( v1 < 0
|| (v2 = v1,
v5[0].sa_family = 2,
*(_WORD *)v5[0].sa_data = _bswap_16(a1),
*(_DWORD *)&v5[0].sa_data[2] = _bswap_32_constprop_0(v2, v5),
bind(v2, v5, v3) < 0) )
{
v2 = -1;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int16 a1)
{
int v1; // eax
unsigned int v2; // er12
socklen_t v3; // edx
struct sockaddr v5[2]; // [rsp+0h] [rbp-28h] BYREF

v1 = socket(2, 2, 0);
if ( v1 < 0
|| (v2 = v1,
v5[0].sa_family = 2,
*(_WORD *)v5[0].sa_data = func0(a1),
*(_DWORD *)&v5[0].sa_data[2] = func1(v2, v5),
bind(v2, v5, v3) < 0) )
{
v2 = -1;
}
return v2;
}

original_code:
int crecvsock(int port){
  int rv;
  struct sockaddr_in saddr;
  rv = socket(2, SOCK_DGRAM, 0);
  if(rv < 0) return(-1);
  saddr.sin_family = 2;
  saddr.sin_port = htons(port);
  saddr.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
  if(bind(rv, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) return(-1);
  return(rv);
}

canonical_original_code:
int crecvsock(int port){
  int rv;
  struct sockaddr_in saddr;
  rv = socket(2, SOCK_DGRAM, 0);
  if(rv < 0) return(-1);
  saddr.sin_family = 2;
  saddr.sin_port = htons(port);
  saddr.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
  if(bind(rv, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) return(-1);
  return(rv);
}

variable_types: rv int

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

user_defined_types: struct sockaddr { unsigned short sa_family; char sa_data[14]; }


original_code:
int set_peer(struct vpeer * peer){
  peer->vp_gw = get_inaddr(peer->name);
  if(peer->vp_gw < 0){
    return(-1);
  }
  if((peer->sock = socket(2, SOCK_DGRAM, 0)) < 0){
    return(-1);
  }
  return(1);
}

canonical_original_code:
int set_peer(struct vpeer *peer){
  peer->vp_gw = get_inaddr(peer->name);
  if(peer->vp_gw < 0){
    return(-1);
  }
  if((peer->sock = socket(2, SOCK_DGRAM, 0)) < 0){
    return(-1);
  }
  return(1);
}

variable_types: struct vpeer { char *name; unsigned int vp_gw; int sock; } *


original_code:
int reg_eha(struct abuf * c, u_char eha[], in_addr_t vp_gw, in_addr_t ip){
  struct abuf * a;
  a = a_get();
  if ( a == ((void *)0)) return(-1);
  memcpy(a->eha, eha, 6);
  a->target.vp_gw = vp_gw;
  a->ip = ip;
  a_append(c, a);
  return(((void *)0));
}

canonical_original_code:
int reg_eha(struct abuf *c, unsigned char eha[], unsigned int vp_gw, unsigned int ip){
  struct abuf *a;
  a = a_get();
  if ( a == ((void *)0)) return(-1);
  memcpy(a->eha, eha, 6);
  a->target.vp_gw = vp_gw;
  a->ip = ip;
  a_append(c, a);
  return(((void *)0));
}

variable_types: struct abuf { struct abuf *a_next; struct abuf *nxtbuf; long tm; union { unsigned int vp_gw; char *device; } target; unsigned int ip; unsigned char eha[6]; unsigned char pad[2]; } *

variable_types:
eha
unsigned char []

variable_types:
ip
unsigned int


ehackzero

decompiled_code:
_BOOL8 __fastcall ehackzero(__int64 a1)
{
return !*(_DWORD *)a1 && !*(_WORD *)(a1 + 4);
}

canonical_decompiled_code:
_BOOL8 __fastcall func14(__int64 a1)
{
return !*(_DWORD *)a1 && !*(_WORD *)(a1 + 4);
}

original_code:
int ehackzero(u_char eha[]){
  u_char zero[6] = {};
  if(memcmp(eha, zero, 6) == 0)
    return(1);
  return(0);
}

canonical_original_code:
int ehackzero(unsigned char eha[]){
  unsigned char zero[6] = {};
  if(memcmp(eha, zero, 6) == 0)
    return(1);
  return(0);
}

variable_types:
zero
unsigned char [6]


ehackbcas

decompiled_code:
_BOOL8 __fastcall ehackbcas(__int64 a1)
{
return *(_DWORD *)a1 == -1 && *(_WORD *)(a1 + 4) == 0xFFFF;
}

canonical_decompiled_code:
_BOOL8 __fastcall func15(__int64 a1)
{
return *(_DWORD *)a1 == -1 && *(_WORD *)(a1 + 4) == 0xFFFF;
}

original_code:
int ehackbcas(u_char eha[]){
  u_char bcas[6] = {0xff,0xff,0xff,0xff,0xff,0xff};
  if(memcmp(eha, bcas, 6) == 0)
    return(1);
  return(0);
}

canonical_original_code:
int ehackbcas(unsigned char eha[]){
  unsigned char bcas[6] = {0xff,0xff,0xff,0xff,0xff,0xff};
  if(memcmp(eha, bcas, 6) == 0)
    return(1);
  return(0);
}

variable_types:
bcas
unsigned char [6]


decompiled_code:
_QWORD *a_get()
{
_QWORD *v0; // r12

v0 = calloc(0x30uLL, 1uLL);
if ( v0 )
v0[2] = time(0LL);
return v0;
}

canonical_decompiled_code:
_QWORD *func0()
{
_QWORD *v0; // r12

v0 = calloc(0x30uLL, 1uLL);
if ( v0 )
v0[2] = time(0LL);
return v0;
}

original_code:
struct abuf * a_get(){
  struct abuf * a;
  a = malloc(sizeof(struct abuf));
  if(a == ((void *)0)) return(((void *)0));
  memset(a, 0, sizeof(struct abuf));
  a->a_next= (struct abuf *)((void *)0);
  a->nxtbuf = (struct abuf *)((void *)0);
  a->tm = time(((void *)0));
  return(a);
}

canonical_original_code:
struct abuf *a_get(){
  struct abuf *a;
  a = malloc(sizeof(struct abuf));
  if(a == ((void *)0)) return(((void *)0));
  memset(a, 0, sizeof(struct abuf));
  a->a_next= (struct abuf *)((void *)0);
  a->nxtbuf = (struct abuf *)((void *)0);
  a->tm = time(((void *)0));
  return(a);
}

return_type: struct abuf { struct abuf *a_next; struct abuf *nxtbuf; long tm; union { unsigned int vp_gw; char *device; } target; unsigned int ip; unsigned char eha[6]; unsigned char pad[2]; } *


decompiled_code:
// attributes: thunk
void a_free(void *ptr)
{
free(ptr);
}

canonical_decompiled_code:
void func1(void *ptr)
{
free(ptr);
}

original_code:
void a_free(struct abuf * a){
  free(a);
  return;
}

canonical_original_code:
void a_free(struct abuf *a){
  free(a);
  return;
}

return_type: void

user_defined_types: struct abuf { struct abuf *a_next; struct abuf *nxtbuf; long tm; union { unsigned int vp_gw; char *device; } target; unsigned int ip; unsigned char eha[6]; unsigned char pad[2]; }


decompiled_code:
__int64 __fastcall a_cellcnt(_QWORD *a1)
{
__int64 result; // rax

for ( result = 0LL; a1; result = (unsigned int)(result + 1) )
a1 = (_QWORD *)*a1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(_QWORD *a1)
{
__int64 result; // rax

for ( result = 0LL; a1; result = (unsigned int)(result + 1) )
a1 = (_QWORD *)*a1;
return result;
}

original_code:
int a_cellcnt(struct abuf * c){
  int i = 0;
  while(c != ((void *)0)){
    c = c->a_next;
    i++;
  }
  return(i);
}

canonical_original_code:
int a_cellcnt(struct abuf *c){
  int i = 0;
  while(c != ((void *)0)){
    c = c->a_next;
    i++;
  }
  return(i);
}

variable_types: i int


decompiled_code:
time_t __fastcall a_append(_QWORD *a1, __int64 a2)
{
_QWORD *v2; // rax
time_t result; // rax

do
{
v2 = a1;
a1 = (_QWORD *)*a1;
}
while ( a1 );
*v2 = a2;
result = time(0LL);
*(_QWORD *)(a2 + 16) = result;
return result;
}

canonical_decompiled_code:
time_t __fastcall func4(_QWORD *a1, __int64 a2)
{
_QWORD *v2; // rax
time_t result; // rax

do
{
v2 = a1;
a1 = (_QWORD *)*a1;
}
while ( a1 );
*v2 = a2;
result = time(0LL);
*(_QWORD *)(a2 + 16) = result;
return result;
}

original_code:
void a_append(struct abuf * c, struct abuf * a){
  while(c->a_next) c = c->a_next;
  c->a_next= a;
  a->tm = time(((void *)0));
  return;
}

canonical_original_code:
void a_append(struct abuf *c, struct abuf *a){
  while(c->a_next) c = c->a_next;
  c->a_next= a;
  a->tm = time(((void *)0));
  return;
}

variable_types:
a
struct abuf *


decompiled_code:
__int64 __fastcall a_lookup_buf(_QWORD *a1, _QWORD *a2)
{
__int64 result; // rax

result = 0LL;
while ( a1 != a2 )
{
a1 = (_QWORD *)*a1;
result = (unsigned int)(result + 1);
if ( !a1 )
return 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(_QWORD *a1, _QWORD *a2)
{
__int64 result; // rax

result = 0LL;
while ( a1 != a2 )
{
a1 = (_QWORD *)*a1;
result = (unsigned int)(result + 1);
if ( !a1 )
return 0xFFFFFFFFLL;
}
return result;
}

original_code:
int a_lookup_buf(struct abuf * c, struct abuf * a){
  int i = 0;
  do{
    if(c == a) return(i);
    i++;
    c = c->a_next;
  }while(c != ((void *)0));
  return(-1);
}

canonical_original_code:
int a_lookup_buf(struct abuf *c, struct abuf *a){
  int i = 0;
  do{
    if(c == a) return(i);
    i++;
    c = c->a_next;
  }while(c != ((void *)0));
  return(-1);
}


decompiled_code:
__int64 __fastcall a_pullout(_QWORD *a1, _QWORD *a2)
{
_QWORD *v2; // rdx
int v3; // er8
__int64 result; // rax

if ( a1 == a2 )
{
result = 1LL;
if ( *a1 )
*a1 = 0LL;
}
else
{
v3 = a_lookup_buf(a1, a2);
result = 0xFFFFFFFFLL;
if ( v3 >= 0 )
{
while ( 1 )
{
result = (__int64)v2;
v2 = (_QWORD *)*v2;
if ( v2 == a2 )
break;
if ( !v2 )
return result;
}
*(_QWORD *)result = *v2;
result = 1LL;
*v2 = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(_QWORD *a1, _QWORD *a2)
{
_QWORD *v2; // rdx
int v3; // er8
__int64 result; // rax

if ( a1 == a2 )
{
result = 1LL;
if ( *a1 )
*a1 = 0LL;
}
else
{
v3 = func5(a1, a2);
result = 0xFFFFFFFFLL;
if ( v3 >= 0 )
{
while ( 1 )
{
result = (__int64)v2;
v2 = (_QWORD *)*v2;
if ( v2 == a2 )
break;
if ( !v2 )
return result;
}
*(_QWORD *)result = *v2;
result = 1LL;
*v2 = 0LL;
}
}
return result;
}

original_code:
int a_pullout(struct abuf * c, struct abuf * a){
  if(c == a){
    if(a->a_next){
      c->a_next = a->a_next;
      a->a_next= ((void *)0);
    }
    return(1);
  }
  if(a_lookup_buf(c,a) < 0 ) return(-1);
  do{
    if(c->a_next == a){
      c->a_next= a->a_next;
      a->a_next= ((void *)0);
      return(1);
    }
    c = c->a_next;
  }while(c != ((void *)0));
}

canonical_original_code:
int a_pullout(struct abuf *c, struct abuf *a){
  if(c == a){
    if(a->a_next){
      c->a_next = a->a_next;
      a->a_next= ((void *)0);
    }
    return(1);
  }
  if(a_lookup_buf(c,a) < 0 ) return(-1);
  do{
    if(c->a_next == a){
      c->a_next= a->a_next;
      a->a_next= ((void *)0);
      return(1);
    }
    c = c->a_next;
  }while(c != ((void *)0));
}


decompiled_code:
_QWORD *init_pbuf()
{
_QWORD *result; // rax

result = malloc(0x20uLL);
if ( result )
{
*result = 0LL;
result[1] = 0LL;
result[2] = 0LL;
}
return result;
}

canonical_decompiled_code:
_QWORD *func0()
{
_QWORD *result; // rax

result = malloc(0x20uLL);
if ( result )
{
*result = 0LL;
result[1] = 0LL;
result[2] = 0LL;
}
return result;
}

original_code:
struct pbuf * init_pbuf(){
  struct pbuf * pbuf;
  pbuf = malloc(sizeof(struct pbuf));
  if(pbuf == ((void *)0)) return ((void *)0);
  pbuf -> p_next = (struct pbuf *)((void *)0);
  pbuf -> vp_pkt = (char *)((void *)0);
  pbuf -> tm = (time_t)((void *)0);
  return pbuf;
}

canonical_original_code:
struct pbuf *init_pbuf(){
  struct pbuf *pbuf;
  pbuf = malloc(sizeof(struct pbuf));
  if(pbuf == ((void *)0)) return ((void *)0);
  pbuf -> p_next = (struct pbuf *)((void *)0);
  pbuf -> vp_pkt = (char *)((void *)0);
  pbuf -> tm = (time_t)((void *)0);
  return pbuf;
}

variable_types: struct pbuf { struct pbuf *p_next; char *vp_pkt; long tm; struct vphdr { unsigned short vp_frg; unsigned short vp_len; unsigned short vp_id; unsigned short vp_pad; } vphdr; } *

user_defined_types: struct pbuf { struct pbuf *p_next; char *vp_pkt; long tm; struct vphdr vphdr; }


decompiled_code:
__int64 __fastcall set_pbuf(_QWORD *a1, _QWORD *a2, const void *a3, int a4)
{
size_t v4; // r13
_QWORD *v6; // rbx
_QWORD *v7; // rbp
void *v8; // rax
_QWORD *v9; // rax

if ( !a1 )
return 0xFFFFFFFFLL;
v4 = a4;
v6 = a1;
v7 = init_pbuf();
if ( !v7 )
return 0xFFFFFFFFLL;
v7[3] = *a2;
v8 = malloc(v4);
v7[1] = v8;
if ( !v8 )
return 0xFFFFFFFFLL;
memcpy(v8, a3, v4);
do
{
v9 = v6;
v6 = (_QWORD *)*v6;
}
while ( v6 );
*v9 = v7;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(_QWORD *a1, _QWORD *a2, const void *a3, int a4)
{
size_t v4; // r13
_QWORD *v6; // rbx
_QWORD *v7; // rbp
void *v8; // rax
_QWORD *v9; // rax

if ( !a1 )
return 0xFFFFFFFFLL;
v4 = a4;
v6 = a1;
v7 = func0();
if ( !v7 )
return 0xFFFFFFFFLL;
v7[3] = *a2;
v8 = malloc(v4);
v7[1] = v8;
if ( !v8 )
return 0xFFFFFFFFLL;
memcpy(v8, a3, v4);
do
{
v9 = v6;
v6 = (_QWORD *)*v6;
}
while ( v6 );
*v9 = v7;
return 0LL;
}

original_code:
int set_pbuf(struct pbuf * pbuf, struct vphdr * vp, char * pkt, int len){
  struct pbuf * new_pbuf;
  if(pbuf == ((void *)0)) return(-1);
  if((new_pbuf = init_pbuf()) == ((void *)0)) return(-1);
  new_pbuf -> vphdr = *vp;
  new_pbuf -> vp_pkt = malloc(sizeof(u_char)*len);
  if(new_pbuf -> vp_pkt == ((void *)0)) return(-1);
  memcpy(new_pbuf -> vp_pkt, pkt, len);
  while(pbuf -> p_next != ((void *)0)){
    pbuf = pbuf -> p_next;
  }
  pbuf -> p_next = new_pbuf;
  return 0;
}

canonical_original_code:
int set_pbuf(struct pbuf *pbuf, struct vphdr *vp, char *pkt, int len){
  struct pbuf *new_pbuf;
  if(pbuf == ((void *)0)) return(-1);
  if((new_pbuf = init_pbuf()) == ((void *)0)) return(-1);
  new_pbuf -> vphdr = *vp;
  new_pbuf -> vp_pkt = malloc(sizeof(u_char)*len);
  if(new_pbuf -> vp_pkt == ((void *)0)) return(-1);
  memcpy(new_pbuf -> vp_pkt, pkt, len);
  while(pbuf -> p_next != ((void *)0)){
    pbuf = pbuf -> p_next;
  }
  pbuf -> p_next = new_pbuf;
  return 0;
}

variable_types:
vp
struct vphdr *

variable_types: len int

user_defined_types: struct vphdr { unsigned short vp_frg; unsigned short vp_len; unsigned short vp_id; unsigned short vp_pad; }


decompiled_code:
__int64 __fastcall free_pbuf(void *ptr)
{
free(*((void **)ptr + 1));
free(ptr);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(void *ptr)
{
free(*((void **)ptr + 1));
free(ptr);
return 0LL;
}

original_code:
int free_pbuf(struct pbuf * pbuf){
  free(pbuf -> vp_pkt);
  free(pbuf);
  return 0;
}

canonical_original_code:
int free_pbuf(struct pbuf *pbuf){
  free(pbuf -> vp_pkt);
  free(pbuf);
  return 0;
}


decompiled_code:
__int64 __fastcall freem_pbuf(_QWORD **a1)
{
_QWORD *v1; // rdi
_QWORD *v2; // rbx
void *v3; // rdi

if ( !a1 )
return 0xFFFFFFFFLL;
v1 = *a1;
if ( !v1 )
return 0LL;
v2 = (_QWORD *)*v1;
if ( (unsigned int)free_pbuf(v1) == -1 )
return 0xFFFFFFFFLL;
while ( 1 )
{
v3 = v2;
if ( !v2 )
break;
v2 = (_QWORD *)*v2;
if ( (unsigned int)free_pbuf(v3) == -1 )
return 0xFFFFFFFFLL;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(_QWORD **a1)
{
_QWORD *v1; // rdi
_QWORD *v2; // rbx
void *v3; // rdi

if ( !a1 )
return 0xFFFFFFFFLL;
v1 = *a1;
if ( !v1 )
return 0LL;
v2 = (_QWORD *)*v1;
if ( (unsigned int)func2(v1) == -1 )
return 0xFFFFFFFFLL;
while ( 1 )
{
v3 = v2;
if ( !v2 )
break;
v2 = (_QWORD *)*v2;
if ( (unsigned int)func2(v3) == -1 )
return 0xFFFFFFFFLL;
}
return 0LL;
}

original_code:
int freem_pbuf(struct pbuf * pbuf){
  struct pbuf * next_pbuf;
  if(pbuf == ((void *)0)) return(-1);
  pbuf = pbuf -> p_next;
  while(pbuf != ((void *)0)){
    next_pbuf = pbuf -> p_next;
    if(free_pbuf(pbuf) == -1) return(-1);
    pbuf = next_pbuf;
  }
  return 0;
}

canonical_original_code:
int freem_pbuf(struct pbuf *pbuf){
  struct pbuf *next_pbuf;
  if(pbuf == ((void *)0)) return(-1);
  pbuf = pbuf -> p_next;
  while(pbuf != ((void *)0)){
    next_pbuf = pbuf -> p_next;
    if(free_pbuf(pbuf) == -1) return(-1);
    pbuf = next_pbuf;
  }
  return 0;
}


decompiled_code:
__int64 __fastcall pbuf_cellcnt(_QWORD **a1)
{
_QWORD *v1; // rax
unsigned int i; // er8

if ( !a1 )
return (unsigned int)-1;
v1 = *a1;
for ( i = 0; v1; ++i )
v1 = (_QWORD *)*v1;
return i;
}

canonical_decompiled_code:
__int64 __fastcall func4(_QWORD **a1)
{
_QWORD *v1; // rax
unsigned int i; // er8

if ( !a1 )
return (unsigned int)-1;
v1 = *a1;
for ( i = 0; v1; ++i )
v1 = (_QWORD *)*v1;
return i;
}

original_code:
int pbuf_cellcnt(struct pbuf * pbuf){
  int n = 0;
  if(pbuf == ((void *)0)) return(-1);
  pbuf = pbuf -> p_next;
  while(pbuf != ((void *)0)){
    pbuf = pbuf -> p_next;
    n++;
  }
  return n;
}

canonical_original_code:
int pbuf_cellcnt(struct pbuf *pbuf){
  int n = 0;
  if(pbuf == ((void *)0)) return(-1);
  pbuf = pbuf -> p_next;
  while(pbuf != ((void *)0)){
    pbuf = pbuf -> p_next;
    n++;
  }
  return n;
}

variable_types: n int


usage

decompiled_code:
size_t usage()
{
return fwrite("usage:veip -a -D -i dev -L log -f frag_size peer1 peer2 peer3 .....\n", 1uLL, 0x45uLL, stderr);
}

canonical_decompiled_code:
size_t func1()
{
return fwrite("usage:veip -a -D -i dev -L log -f frag_size peer1 peer2 peer3 .....\n", 1uLL, 0x45uLL, stderr);
}

original_code:
void usage(){
  fprintf(stderr,"usage:veip  -a -D -i dev -L log -f frag_size peer1 peer2 peer3 .....\n");
  return;
}


main

canonical_name: main

repo: odanielson/Result

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int *v3; // rbp
int v4; // edx

if ( argc <= 2 )
{
printf("Usage: %s buffer pattern\n\nSearch for pattern in buffer and print position if found.\n", *argv);
}
else
{
v3 = (unsigned int *)findPattern(argv[1], argv[2], envp);
if ( v4 )
{
printf("Couldn't find \"%s\" in \"%s\"\n", argv[2], argv[1]);
}
else
{
printf("Found \"%s\" in \"%s\" at pos %d\n", argv[1], argv[2], *v3);
free(v3);
}
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int *v3; // rbp
int v4; // edx

if ( argc <= 2 )
{
printf("Usage: %s buffer pattern\n\nSearch for pattern in buffer and print position if found.\n", *argv);
}
else
{
v3 = (unsigned int *)func7(argv[1], argv[2], envp);
if ( v4 )
{
printf("Couldn't find \"%s\" in \"%s\"\n", argv[2], argv[1]);
}
else
{
printf("Found \"%s\" in \"%s\" at pos %d\n", argv[1], argv[2], *v3);
free(v3);
}
}
return 0;
}

original_code:
int main(int argc, char **argv) {
    Result res;
    if (argc > 2)
    {
        if ((res = findPattern(argv[1], argv[2])).status == OK)
        {
            printf("Found \"%s\" in \"%s\" at pos %d\n", argv[1], argv[2],
                   *(int *)res.handle);
            free(res.handle);
        }
        else
        {
            printf("Couldn't find \"%s\" in \"%s\"\n", argv[2], argv[1]);
        }
    }
    else
    {
        printf("Usage: %s buffer pattern\n\n"
               "Search for pattern in buffer and print position if found.\n",
               argv[0]);
    }
}

canonical_original_code:
int main(int argc, char **argv) {
    struct _Result res;
    if (argc > 2)
    {
        if ((res = findPattern(argv[1], argv[2])).status == OK)
        {
            printf("Found \"%s\" in \"%s\" at pos %d\n", argv[1], argv[2],
                   *(int *)res.handle);
            free(res.handle);
        }
        else
        {
            printf("Couldn't find \"%s\" in \"%s\"\n", argv[2], argv[1]);
        }
    }
    else
    {
        printf("Usage: %s buffer pattern\n\n"
               "Search for pattern in buffer and print position if found.\n",
               argv[0]);
    }
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct _Result { void *handle; enum _Status { OK=0, ERROR=1 } status; }

user_defined_types: struct _Result { void *handle; enum _Status status; }


repo: odanielson/TcpClient

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
__int64 v4; // rbp
const char *v5; // r13
size_t v6; // rax
char v8[296]; // [rsp+0h] [rbp-128h] BYREF

if ( argc <= 3 )
{
printf("Usage: %s host port message\n", *argv);
}
else
{
v3 = atoi(argv[2]);
v4 = MEMORY[0](argv[1], v3);
if ( v4 )
{
printf("\n-> %s\n", argv[3]);
v5 = argv[3];
v6 = strlen(v5);
MEMORY[0](v4, v5, v6);
MEMORY[0](v4, "\n", 1LL);
v8[MEMORY[0](v4, v8, 255LL)] = 0;
printf("\n<- %s\n", v8);
MEMORY[0](v4);
}
else
{
printf("Failed to connect to %s:%s\n", argv[1], argv[2]);
}
}
return 0;
}

original_code:
int main(int argc, char **argv) {
    char *host;
    int port;
    char response[256];
    size_t len;
    if (argc > 3)
    {
        TcpHandle *handle = TcpClient.Create(argv[1], atoi(argv[2]));
        if (handle)
        {
            printf("\n-> %s\n", argv[3]);
            TcpClient.Send(handle, argv[3], strlen(argv[3]));
            TcpClient.Send(handle, "\n", strlen("\n"));
            len = TcpClient.Read(handle, response, sizeof(response)-1);
            response[len] = 0;
            printf("\n<- %s\n", response);
            TcpClient.Close(handle);
        }
        else
        {
            printf("Failed to connect to %s:%s\n", argv[1], argv[2]);
        }
    }
    else
    {
        printf("Usage: %s host port message\n", argv[0]);
    }
}

canonical_original_code:
int main(int argc, char **argv) {
    char *host;
    int port;
    char response[256];
    unsigned long len;
    if (argc > 3)
    {
        struct _TcpHandle *handle = TcpClient.Create(argv[1], atoi(argv[2]));
        if (handle)
        {
            printf("\n-> %s\n", argv[3]);
            TcpClient.Send(handle, argv[3], strlen(argv[3]));
            TcpClient.Send(handle, "\n", strlen("\n"));
            len = TcpClient.Read(handle, response, sizeof(response)-1);
            response[len] = 0;
            printf("\n<- %s\n", response);
            TcpClient.Close(handle);
        }
        else
        {
            printf("Failed to connect to %s:%s\n", argv[1], argv[2]);
        }
    }
    else
    {
        printf("Usage: %s host port message\n", argv[0]);
    }
}

variable_types:
host
char *

variable_types: port int

variable_types:
response
char [256]

variable_types:
len
unsigned long

variable_types: struct _TcpHandle { int m_sockFD; char m_recvBuf[1024]; 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]; } m_servAddr; } *

user_defined_types: struct _TcpHandle { int m_sockFD; char m_recvBuf[1024]; struct sockaddr_in m_servAddr; }


Read

decompiled_code:
ssize_t __fastcall Read(int *a1, void *a2, size_t a3)
{
return read(*a1, a2, a3);
}

canonical_decompiled_code:
ssize_t __fastcall func0(int *a1, void *a2, size_t a3)
{
return read(*a1, a2, a3);
}

original_code:
static size_t Read(TcpHandle *a_handle, char *a_buffer, size_t a_maxLength)
{
    return read(a_handle->m_sockFD, a_buffer, a_maxLength);
}

canonical_original_code:
static unsigned long Read(struct _TcpHandle *a_handle, char *a_buffer, unsigned long a_maxLength)
{
    return read(a_handle->m_sockFD, a_buffer, a_maxLength);
}

return_type: unsigned long


Send

decompiled_code:
ssize_t __fastcall Send(int *a1, const void *a2, size_t a3)
{
return write(*a1, a2, a3);
}

canonical_decompiled_code:
ssize_t __fastcall func1(int *a1, const void *a2, size_t a3)
{
return write(*a1, a2, a3);
}

original_code:
static size_t Send(TcpHandle *a_handle, char *a_buffer, size_t a_length)
{
    return write(a_handle->m_sockFD, a_buffer, a_length);
}

canonical_original_code:
static unsigned long Send(struct _TcpHandle *a_handle, char *a_buffer, unsigned long a_length)
{
    return write(a_handle->m_sockFD, a_buffer, a_length);
}


Close

decompiled_code:
void __fastcall Close(void *ptr)
{
close(*(_DWORD *)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
close(*(_DWORD *)ptr);
free(ptr);
}

original_code:
static void Close(TcpHandle *a_handle) {
    close(a_handle->m_sockFD);
    free(a_handle);
}

canonical_original_code:
static void Close(struct _TcpHandle *a_handle) {
    close(a_handle->m_sockFD);
    free(a_handle);
}


Create

original_code:
static TcpHandle *Create(const char *a_host, int a_port)
{
    TcpHandle *handle = malloc(sizeof(TcpHandle));
    handle->m_sockFD = 0;
    memset(handle->m_recvBuf, 0, sizeof(handle->m_recvBuf));
    handle->m_sockFD = socket(2, SOCK_STREAM, 0);
    if (handle->m_sockFD < 0)
    {
        free(handle);
        return ((void *)0);
    }
    memset(&(handle->m_servAddr), '0', sizeof(handle->m_servAddr));
    handle->m_servAddr.sin_family = 2;
    handle->m_servAddr.sin_port = htons(a_port);
    if (inet_pton(2, a_host, &(handle->m_servAddr).sin_addr) <= 0)
    {
        free(handle);
        return ((void *)0);
    }
    if (connect(handle->m_sockFD, (struct sockaddr *)&(handle->m_servAddr),
                sizeof(handle->m_servAddr)) < 0)
    {
        free(handle);
        return ((void *)0);
    }
    return handle;
}

canonical_original_code:
static struct _TcpHandle *Create(char *a_host, int a_port)
{
    struct _TcpHandle *handle = malloc(sizeof(TcpHandle));
    handle->m_sockFD = 0;
    memset(handle->m_recvBuf, 0, sizeof(handle->m_recvBuf));
    handle->m_sockFD = socket(2, SOCK_STREAM, 0);
    if (handle->m_sockFD < 0)
    {
        free(handle);
        return ((void *)0);
    }
    memset(&(handle->m_servAddr), '0', sizeof(handle->m_servAddr));
    handle->m_servAddr.sin_family = 2;
    handle->m_servAddr.sin_port = htons(a_port);
    if (inet_pton(2, a_host, &(handle->m_servAddr).sin_addr) <= 0)
    {
        free(handle);
        return ((void *)0);
    }
    if (connect(handle->m_sockFD, (struct sockaddr *)&(handle->m_servAddr),
                sizeof(handle->m_servAddr)) < 0)
    {
        free(handle);
        return ((void *)0);
    }
    return handle;
}

return_type: struct _TcpHandle { int m_sockFD; char m_recvBuf[1024]; 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]; } m_servAddr; } *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
_DWORD *v3; // rax
_DWORD *v4; // r14
_DWORD *v5; // rax
_DWORD *v6; // r13
_DWORD *v7; // rax
_DWORD *v8; // r12
unsigned int *v9; // rbp
unsigned int **v10; // rbx

v3 = malloc(4uLL);
*v3 = 61;
v4 = v3;
v5 = malloc(4uLL);
*v5 = 23;
v6 = v5;
v7 = malloc(4uLL);
*v7 = 792;
v8 = v7;
v9 = (unsigned int *)malloc(0x28uLL);
list_init(v9, free);
list_ins_next(v9, *((_QWORD *)v9 + 4), v4);
list_ins_next(v9, *((_QWORD *)v9 + 4), v6);
list_ins_next(v9, *((_QWORD *)v9 + 4), v8);
printf("List size: %i\n", *v9);
v10 = (unsigned int **)*((_QWORD *)v9 + 3);
do
{
printf("%i\n", **v10);
v10 = (unsigned int **)v10[1];
}
while ( v10 );
list_destroy(v9);
free(v9);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
_DWORD *v3; // rax
_DWORD *v4; // r14
_DWORD *v5; // rax
_DWORD *v6; // r13
_DWORD *v7; // rax
_DWORD *v8; // r12
unsigned int *v9; // rbp
unsigned int **v10; // rbx

v3 = malloc(4uLL);
*v3 = 61;
v4 = v3;
v5 = malloc(4uLL);
*v5 = 23;
v6 = v5;
v7 = malloc(4uLL);
*v7 = 792;
v8 = v7;
v9 = (unsigned int *)malloc(0x28uLL);
func7(v9, free);
func8(v9, *((_QWORD *)v9 + 4), v4);
func8(v9, *((_QWORD *)v9 + 4), v6);
func8(v9, *((_QWORD *)v9 + 4), v8);
printf("List size: %i\n", *v9);
v10 = (unsigned int **)*((_QWORD *)v9 + 3);
do
{
printf("%i\n", **v10);
v10 = (unsigned int **)v10[1];
}
while ( v10 );
func10(v9);
free(v9);
return 0;
}

original_code:
int main(int argc, char **argv)
{
  int *n = (int*)malloc(sizeof(int));
  *n=61;
  int *b = (int*)malloc(sizeof(int));
  *b=23;
  int *c = (int*)malloc(sizeof(int));
  *c=792;
  List *testing;
  testing = (List*) malloc(sizeof(List));
  list_init(testing, (void*)free);
  list_ins_next(testing,((testing)->tail),(void*)n);
  list_ins_next(testing,((testing)->tail),(void*)b);
  list_ins_next(testing,((testing)->tail),(void*)c);
  printf("List size: %i\n", ((testing)->size));
  ListElmt *tmp = ((void *)0);
  tmp = ((testing)->head);
  do
    {
      printf("%i\n", *(int*)((tmp)->data));
    }while((tmp=((tmp)->next)));
  list_destroy(testing);
  free(testing);
  return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
  int *n = (int*)malloc(sizeof(int));
  *n=61;
  int *b = (int*)malloc(sizeof(int));
  *b=23;
  int *c = (int*)malloc(sizeof(int));
  *c=792;
  struct List_ *testing;
  testing = (struct List_ *) malloc(sizeof(List));
  list_init(testing, (void *)free);
  list_ins_next(testing,((testing)->tail),(void *)n);
  list_ins_next(testing,((testing)->tail),(void *)b);
  list_ins_next(testing,((testing)->tail),(void *)c);
  printf("List size: %i\n", ((testing)->size));
  struct ListElmt_ *tmp = ((void *)0);
  tmp = ((testing)->head);
  do
    {
      printf("%i\n", *(int *)((tmp)->data));
    }while((tmp=((tmp)->next)));
  list_destroy(testing);
  free(testing);
  return 0;
}

variable_types:
n
int *

variable_types:
b
int *

variable_types:
c
int *

variable_types: struct List_ { int size; int (*match)(void *key1, void *key2); void (*destroy)(void *data); struct ListElmt_ { void *data; struct ListElmt_ *next; } *head; struct ListElmt_ *tail; } *

user_defined_types: struct List_ { int size; int (*match)(void *key1, void *key2); void (*destroy)(void *data); struct ListElmt_ *head; struct ListElmt_ *tail; }


original_code:
void list_init(List *list, void (*destroy)(void *data))
{
  list->size = 0;
  list->destroy = destroy;
  list->head = ((void *)0);
  list->tail = ((void *)0);
  return;
}

canonical_original_code:
void list_init(struct List_ *list, void (*destroy)(void *data))
{
  list->size = 0;
  list->destroy = destroy;
  list->head = ((void *)0);
  list->tail = ((void *)0);
  return;
}

variable_types:
destroy
void (*)(void *data)

user_defined_types: struct ListElmt_ { void *data; struct ListElmt_ *next; }


original_code:
int list_ins_next(List *list, ListElmt *element, const void *data)
{
  ListElmt *new_element;
  if((new_element = (ListElmt *)malloc(sizeof(ListElmt))) == ((void *)0))
    {
      return -1;
    }
  new_element->data = (void*)data;
  if(element == ((void *)0))
    {
      if(((list)->size) == 0)
 {
   list->tail = new_element;
 }
      new_element->next = list->head;
      list->head = new_element;
    }else{
    if(element->next == ((void *)0))
      {
 list->tail = new_element;
      }
    new_element->next = element->next;
    element->next = new_element;
  }
  list->size++;
  return 0;
}

canonical_original_code:
int list_ins_next(struct List_ *list, struct ListElmt_ *element, void *data)
{
  struct ListElmt_ *new_element;
  if((new_element = (struct ListElmt_ *)malloc(sizeof(ListElmt))) == ((void *)0))
    {
      return -1;
    }
  new_element->data = (void *)data;
  if(element == ((void *)0))
    {
      if(((list)->size) == 0)
 {
   list->tail = new_element;
 }
      new_element->next = list->head;
      list->head = new_element;
    }else{
    if(element->next == ((void *)0))
      {
 list->tail = new_element;
      }
    new_element->next = element->next;
    element->next = new_element;
  }
  list->size++;
  return 0;
}

variable_types:
element
struct ListElmt_ *

variable_types:
data
void *

variable_types: struct ListElmt_ { void *data; struct ListElmt_ *next; } *


original_code:
int list_rem_next(List *list, ListElmt *element, void **data)
{
  ListElmt *old_element;
  if(((list)->size) == 0)
    return -1;
  if(element == ((void *)0))
    {
      *data = list->head->data;
      old_element = list->head;
      list->head = list->head->next;
      if(((list)->size) == 1)
 list->tail = ((void *)0);
    }else{
    if(element->next == ((void *)0))
      return -1;
    *data = element->next->data;
    old_element = element->next;
    element->next = element->next->next;
    if(element->next == ((void *)0))
      list->tail = element;
  }
  free(old_element);
  list->size--;
  return 0;
}

canonical_original_code:
int list_rem_next(struct List_ *list, struct ListElmt_ *element, void **data)
{
  struct ListElmt_ *old_element;
  if(((list)->size) == 0)
    return -1;
  if(element == ((void *)0))
    {
      *data = list->head->data;
      old_element = list->head;
      list->head = list->head->next;
      if(((list)->size) == 1)
 list->tail = ((void *)0);
    }else{
    if(element->next == ((void *)0))
      return -1;
    *data = element->next->data;
    old_element = element->next;
    element->next = element->next->next;
    if(element->next == ((void *)0))
      list->tail = element;
  }
  free(old_element);
  list->size--;
  return 0;
}

variable_types:
data
void **


IsEmpty

repo: oeichenwei/nettest

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

if ( a1 )
result = *(_QWORD *)(a1 + 8) == 0LL;
else
result = 1LL;
return result;
}

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

if ( a1 )
result = *(_QWORD *)(a1 + 8) == 0LL;
else
result = 1LL;
return result;
}

original_code:
int IsEmpty(List L)
{
 if(L == ((void *)0))
  return 1;
 return L->Next == ((void *)0);
}

canonical_original_code:
int IsEmpty(struct ListHeader *L)
{
 if(L == ((void *)0))
  return 1;
 return L->Next == ((void *)0);
}

variable_types: struct ListHeader { void *Element; struct Node { void *Element; struct Node *Next; } *Next; 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; } *

user_defined_types: struct ListHeader { void *Element; struct Node *Next; 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; }


IsLast

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

if ( a1 )
result = *(_QWORD *)(a1 + 8) == 0LL;
else
result = 1LL;
return result;
}

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

if ( a1 )
result = *(_QWORD *)(a1 + 8) == 0LL;
else
result = 1LL;
return result;
}

original_code:
int IsLast(Position P, List L)
{
 if(P == ((void *)0))
  return 1;
 return P->Next == ((void *)0);
}

canonical_original_code:
int IsLast(struct Node *P, struct ListHeader *L)
{
 if(P == ((void *)0))
  return 1;
 return P->Next == ((void *)0);
}

variable_types: struct Node { void *Element; struct Node *Next; } *

variable_types: struct ListHeader { void *Element; struct Node *Next; 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; } *


Delete

decompiled_code:
void __fastcall Delete(__int64 a1, _QWORD *a2)
{
_QWORD *v2; // rax
__int64 v3; // rdx
_QWORD *v4; // rdi

v2 = FindPrevious(a1, a2);
if ( !IsLast((__int64)v2) )
{
v4 = *(_QWORD **)(v3 + 8);
*(_QWORD *)(v3 + 8) = v4[1];
free(v4);
}
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1, _QWORD *a2)
{
_QWORD *v2; // rax
__int64 v3; // rdx
_QWORD *v4; // rdi

v2 = FindPrevious(a1, a2);
if ( !func1((__int64)v2) )
{
v4 = *(_QWORD **)(v3 + 8);
*(_QWORD *)(v3 + 8) = v4[1];
free(v4);
}
}

original_code:
void Delete(ElementType X, List L)
{
 Position P, TmpCell;
 P = FindPrevious(X, L);
 if(!IsLast(P, L))
 {
  TmpCell = P->Next;
  P->Next = TmpCell->Next;
  free(TmpCell);
 }
}

canonical_original_code:
void Delete(void *X, struct ListHeader *L)
{
 struct Node *P; struct Node *TmpCell;
 P = FindPrevious(X, L);
 if(!IsLast(P, L))
 {
  TmpCell = P->Next;
  P->Next = TmpCell->Next;
  free(TmpCell);
 }
}

variable_types:
X
void *

user_defined_types: struct Node { void *Element; struct Node *Next; }

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


DeleteList

decompiled_code:
void __fastcall DeleteList(void *ptr)
{
char *v1; // r12
_QWORD *v3; // rbx
void *v4; // rdi

if ( ptr )
{
v1 = (char *)ptr + 16;
Lock((char *)ptr + 16);
v3 = (_QWORD *)*((_QWORD *)ptr + 1);
*((_QWORD *)ptr + 1) = 0LL;
while ( v3 )
{
v4 = v3;
v3 = (_QWORD *)v3[1];
free(v4);
}
UnLock(v1);
UnInitLock(v1);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func3(void *ptr)
{
char *v1; // r12
_QWORD *v3; // rbx
void *v4; // rdi

if ( ptr )
{
v1 = (char *)ptr + 16;
Lock((char *)ptr + 16);
v3 = (_QWORD *)*((_QWORD *)ptr + 1);
*((_QWORD *)ptr + 1) = 0LL;
while ( v3 )
{
v4 = v3;
v3 = (_QWORD *)v3[1];
free(v4);
}
UnLock(v1);
UnInitLock(v1);
free(ptr);
}
}

original_code:
void DeleteList(List L)
{
 Position P, Tmp;
 if(L == ((void *)0))
  return;
 Lock(&L->lock);
 P = L->Next;
 L->Next = ((void *)0);
 while(P != ((void *)0))
 {
  Tmp = P->Next;
  free(P);
  P = Tmp;
 }
 UnLock(&L->lock);
 UnInitLock(&L->lock);
 free(L);
}

canonical_original_code:
void DeleteList(struct ListHeader *L)
{
 struct Node *P; struct Node *Tmp;
 if(L == ((void *)0))
  return;
 Lock(&L->lock);
 P = L->Next;
 L->Next = ((void *)0);
 while(P != ((void *)0))
 {
  Tmp = P->Next;
  free(P);
  P = Tmp;
 }
 UnLock(&L->lock);
 UnInitLock(&L->lock);
 free(L);
}


Header

decompiled_code:
__int64 __fastcall Header(__int64 a1)
{
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
return a1;
}

original_code:
Position Header(List L)
{
 return (Position)L;
}

canonical_original_code:
struct Node *Header(struct ListHeader *L)
{
 return (struct Node *)L;
}

return_type: struct Node { void *Element; struct Node *Next; } *


First

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

if ( a1 )
result = *(_QWORD *)(a1 + 8);
else
result = 0LL;
return result;
}

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

if ( a1 )
result = *(_QWORD *)(a1 + 8);
else
result = 0LL;
return result;
}

original_code:
Position First(List L)
{
 if(L == ((void *)0))
  return ((void *)0);
 return L->Next;
}

canonical_original_code:
struct Node *First(struct ListHeader *L)
{
 if(L == ((void *)0))
  return ((void *)0);
 return L->Next;
}


Advance

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

if ( a1 )
result = *(_QWORD *)(a1 + 8);
else
result = 0LL;
return result;
}

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

if ( a1 )
result = *(_QWORD *)(a1 + 8);
else
result = 0LL;
return result;
}

original_code:
Position Advance(Position P)
{
 if(P == ((void *)0))
  return ((void *)0);
 return P->Next;
}

canonical_original_code:
struct Node *Advance(struct Node *P)
{
 if(P == ((void *)0))
  return ((void *)0);
 return P->Next;
}

variable_types:
P
struct Node *


Retrieve

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

if ( a1 )
result = *a1;
else
result = 0LL;
return result;
}

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

if ( a1 )
result = *a1;
else
result = 0LL;
return result;
}

original_code:
ElementType Retrieve(Position P)
{
 if(P == ((void *)0))
  return ((void *)0);
 return P->Element;
}

canonical_original_code:
void *Retrieve(struct Node *P)
{
 if(P == ((void *)0))
  return ((void *)0);
 return P->Element;
}

return_type: void *


LockList

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

if ( a1 )
result = Lock(a1 + 16);
return result;
}

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

if ( a1 )
result = Lock(a1 + 16);
return result;
}

original_code:
void LockList(List L)
{
 if(L == ((void *)0))
  return;
 Lock(&L->lock);
}

canonical_original_code:
void LockList(struct ListHeader *L)
{
 if(L == ((void *)0))
  return;
 Lock(&L->lock);
}


UnLockList

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

if ( a1 )
result = UnLock(a1 + 16);
return result;
}

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

if ( a1 )
result = UnLock(a1 + 16);
return result;
}

original_code:
void UnLockList(List L)
{
 if(L == ((void *)0))
  return;
 UnLock(&L->lock);
}

canonical_original_code:
void UnLockList(struct ListHeader *L)
{
 if(L == ((void *)0))
  return;
 UnLock(&L->lock);
}


InitTimerQue

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

result = MakeEmpty(0LL);
g_timer = result;
return result;
}

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

result = MakeEmpty(0LL);
g_timer = result;
return result;
}

original_code:
void InitTimerQue()
{
 g_timer = MakeEmpty(((void *)0));
}


UnInitTimerQue

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

result = DeleteList(g_timer);
g_timer = 0LL;
return result;
}

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

result = DeleteList(g_timer);
g_timer = 0LL;
return result;
}

original_code:
void UnInitTimerQue()
{
 DeleteList(g_timer);
 g_timer = ((void *)0);
}


Rearrange

decompiled_code:
__int64 __fastcall Rearrange(__int64 a1, __int64 a2)
{
__int64 v2; // r14
_QWORD *v3; // rbp
__int64 v4; // rbx
__int64 v5; // rax

v2 = a2;
v3 = (_QWORD *)Retrieve(a2);
v4 = Advance(a2);
if ( !v4 )
return a2;
while ( 1 )
{
if ( *(_QWORD *)Retrieve(v4) > *v3 )
{
if ( a2 == v2 )
return a2;
goto LABEL_7;
}
v2 = v4;
v5 = Advance(v4);
if ( !v5 )
break;
v4 = v5;
}
v4 = 0LL;
if ( a2 != v2 )
{
LABEL_7:
*(_QWORD *)(a1 + 8) = *(_QWORD *)(a2 + 8);
*(_QWORD *)(a2 + 8) = v4;
*(_QWORD *)(v2 + 8) = a2;
return *(_QWORD *)(a1 + 8);
}
return a2;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2)
{
__int64 v2; // r14
_QWORD *v3; // rbp
__int64 v4; // rbx
__int64 v5; // rax

v2 = a2;
v3 = (_QWORD *)Retrieve(a2);
v4 = Advance(a2);
if ( !v4 )
return a2;
while ( 1 )
{
if ( *(_QWORD *)Retrieve(v4) > *v3 )
{
if ( a2 == v2 )
return a2;
goto LABEL_7;
}
v2 = v4;
v5 = Advance(v4);
if ( !v5 )
break;
v4 = v5;
}
v4 = 0LL;
if ( a2 != v2 )
{
LABEL_7:
*(_QWORD *)(a1 + 8) = *(_QWORD *)(a2 + 8);
*(_QWORD *)(a2 + 8) = v4;
*(_QWORD *)(v2 + 8) = a2;
return *(_QWORD *)(a1 + 8);
}
return a2;
}

original_code:
Position Rearrange(Position itPrevOld, Position itOld)
{
 Position it, itPrev;
 time_node* pTemp;
 time_node* pTimerNode;
 pTimerNode = (time_node*)Retrieve(itOld);
 itPrev = itOld;
 it = Advance(itOld);
 while(it != ((void *)0))
 {
  pTemp = (time_node*)Retrieve(it);
  if(pTemp->ulNextTime > pTimerNode->ulNextTime)
   break;
  itPrev = it;
  it = Advance(it);
 }
 if(itPrev != itOld)
 {
  itPrevOld->Next = itOld->Next;
  itOld->Next = it;
  itPrev->Next = itOld;
  return itPrevOld->Next;
 }
 return itOld;
}

canonical_original_code:
struct Node *Rearrange(struct Node *itPrevOld, struct Node *itOld)
{
 struct Node *it; struct Node *itPrev;
 struct time_node *pTemp;
 struct time_node *pTimerNode;
 pTimerNode = (struct time_node *)Retrieve(itOld);
 itPrev = itOld;
 it = Advance(itOld);
 while(it != ((void *)0))
 {
  pTemp = (struct time_node *)Retrieve(it);
  if(pTemp->ulNextTime > pTimerNode->ulNextTime)
   break;
  itPrev = it;
  it = Advance(it);
 }
 if(itPrev != itOld)
 {
  itPrevOld->Next = itOld->Next;
  itOld->Next = it;
  itPrev->Next = itOld;
  return itPrevOld->Next;
 }
 return itOld;
}

variable_types:
itPrevOld
struct Node *

variable_types:
itOld
struct Node *

variable_types: struct time_node { unsigned long ulNextTime; int nInterval; void *pPtr; int nRemainCount; void (*OnTimer)(void *pPtr, unsigned long uID); unsigned long ulID; } *


CancelTimer

decompiled_code:
__int64 __fastcall CancelTimer(__int64 a1)
{
__int64 v1; // rbp
__int64 v2; // r13
__int64 v3; // rax
_QWORD *v4; // r12

LockList(g_timer);
v1 = First(g_timer);
v2 = Header(g_timer);
if ( v1 )
{
while ( 1 )
{
v4 = (_QWORD *)Retrieve(v1);
if ( v4[5] == a1 )
break;
v3 = Advance(v1);
v2 = v1;
if ( !v3 )
return UnLockList(g_timer);
v1 = v3;
}
*(_QWORD *)(v2 + 8) = Advance(v1);
free(v4);
}
return UnLockList(g_timer);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
__int64 v1; // rbp
__int64 v2; // r13
__int64 v3; // rax
_QWORD *v4; // r12

LockList(g_timer);
v1 = First(g_timer);
v2 = Header(g_timer);
if ( v1 )
{
while ( 1 )
{
v4 = (_QWORD *)Retrieve(v1);
if ( v4[5] == a1 )
break;
v3 = Advance(v1);
v2 = v1;
if ( !v3 )
return UnLockList(g_timer);
v1 = v3;
}
*(_QWORD *)(v2 + 8) = Advance(v1);
free(v4);
}
return UnLockList(g_timer);
}

original_code:
void CancelTimer(unsigned long ulTimerID)
{
 Position it, itPrev;
 struct time_node* pTemp;
 LockList(g_timer);
 it = First(g_timer);
 itPrev = Header(g_timer);
 while(it != ((void *)0))
 {
  pTemp = (struct time_node*)Retrieve(it);
  if(pTemp->ulID == ulTimerID)
  {
   it = Advance(it);
   itPrev->Next = it;
   free(pTemp);
   break;
  }
  itPrev = it;
  it = Advance(it);
 }
 UnLockList(g_timer);
}

canonical_original_code:
void CancelTimer(unsigned long ulTimerID)
{
 struct Node *it; struct Node *itPrev;
 struct time_node *pTemp;
 LockList(g_timer);
 it = First(g_timer);
 itPrev = Header(g_timer);
 while(it != ((void *)0))
 {
  pTemp = (struct time_node *)Retrieve(it);
  if(pTemp->ulID == ulTimerID)
  {
   it = Advance(it);
   itPrev->Next = it;
   free(pTemp);
   break;
  }
  itPrev = it;
  it = Advance(it);
 }
 UnLockList(g_timer);
}

variable_types:
ulTimerID
unsigned long

user_defined_types: struct time_node { unsigned long ulNextTime; int nInterval; void *pPtr; int nRemainCount; void (*OnTimer)(void *pPtr, unsigned long uID); unsigned long ulID; }


decompiled_code:
__time_t get_tick_count()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v1, 0LL);
return 1000 * v1.tv_sec + v1.tv_usec / 1000;
}

canonical_decompiled_code:
__time_t func4()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v1, 0LL);
return 1000 * v1.tv_sec + v1.tv_usec / 1000;
}

original_code:
unsigned long get_tick_count()
{
 unsigned long ret;
 struct timeval time_val;
 gettimeofday(&time_val, ((void *)0));
 ret = time_val.tv_sec * 1000 + time_val.tv_usec / 1000;
 return ret;
}

variable_types:
ret
unsigned long

variable_types: struct timeval { long tv_sec; long tv_usec; }


ProcessTimer

decompiled_code:
__int64 ProcessTimer()
{
__time_t v0; // rax
unsigned __int64 v1; // r13
__int64 v2; // r12
__int64 v3; // rbx
int *v4; // rbp
__int64 v5; // rax
__int64 v6; // r12
int v8; // eax
__int64 v9; // rax

v0 = get_tick_count();
if ( !g_timer )
return -1LL;
v1 = v0;
LockList(g_timer);
v2 = First(g_timer);
v3 = Header(g_timer);
while ( v2 )
{
while ( 1 )
{
v4 = (int *)Retrieve(v2);
if ( *(_QWORD *)v4 > v1 )
{
v6 = *(_QWORD *)v4 - v1;
goto LABEL_8;
}
(*((void (__fastcall **)(_QWORD, _QWORD))v4 + 4))(*((_QWORD *)v4 + 2), *((_QWORD *)v4 + 5));
v5 = *(_QWORD *)(v3 + 8);
if ( v5 == v2 )
break;
v2 = *(_QWORD *)(v3 + 8);
if ( !v5 )
goto LABEL_7;
}
v8 = v4[6];
if ( v8 )
{
if ( v8 > 0 )
v4[6] = v8 - 1;
*(_QWORD *)v4 += v4[2];
v2 = Rearrange(v3, v2);
}
else
{
free(v4);
v9 = Advance(v2);
*(_QWORD *)(v3 + 8) = v9;
v2 = v9;
}
}
LABEL_7:
v6 = -1LL;
LABEL_8:
UnLockList(g_timer);
return v6;
}

canonical_decompiled_code:
__int64 func5()
{
__time_t v0; // rax
unsigned __int64 v1; // r13
__int64 v2; // r12
__int64 v3; // rbx
int *v4; // rbp
__int64 v5; // rax
__int64 v6; // r12
int v8; // eax
__int64 v9; // rax

v0 = func4();
if ( !g_timer )
return -1LL;
v1 = v0;
LockList(g_timer);
v2 = First(g_timer);
v3 = Header(g_timer);
while ( v2 )
{
while ( 1 )
{
v4 = (int *)Retrieve(v2);
if ( *(_QWORD *)v4 > v1 )
{
v6 = *(_QWORD *)v4 - v1;
goto LABEL_8;
}
(*((void (__fastcall **)(_QWORD, _QWORD))v4 + 4))(*((_QWORD *)v4 + 2), *((_QWORD *)v4 + 5));
v5 = *(_QWORD *)(v3 + 8);
if ( v5 == v2 )
break;
v2 = *(_QWORD *)(v3 + 8);
if ( !v5 )
goto LABEL_7;
}
v8 = v4[6];
if ( v8 )
{
if ( v8 > 0 )
v4[6] = v8 - 1;
*(_QWORD *)v4 += v4[2];
v2 = func2(v3, v2);
}
else
{
free(v4);
v9 = Advance(v2);
*(_QWORD *)(v3 + 8) = v9;
v2 = v9;
}
}
LABEL_7:
v6 = -1LL;
LABEL_8:
UnLockList(g_timer);
return v6;
}

original_code:
long ProcessTimer()
{
 Position it;
 Position itPrev;
 struct time_node* tvTimer;
 unsigned long ulNow;
 long ulRet = -1;
 ulNow = get_tick_count();
 if(g_timer == ((void *)0))
  return -1;
 LockList(g_timer);
 it = First(g_timer);
 itPrev = Header(g_timer);
 while(it != ((void *)0))
 {
  tvTimer = (struct time_node*)Retrieve(it);
  if(ulNow >= tvTimer->ulNextTime)
  {
   tvTimer->OnTimer(tvTimer->pPtr, tvTimer->ulID);
   if(itPrev->Next != it)
   {
    it = itPrev->Next;
    continue;
   }
   if(tvTimer->nRemainCount == 0)
   {
    free(tvTimer);
    it = Advance(it);
    itPrev->Next = it;
   }
   else
   {
    if(tvTimer->nRemainCount > 0)
     tvTimer->nRemainCount--;
    tvTimer->ulNextTime += tvTimer->nInterval;
    it = Rearrange(itPrev, it);
   }
  }
  else
  {
   ulRet = tvTimer->ulNextTime - ulNow;
   break;
  }
 }
 UnLockList(g_timer);
 return ulRet;
}

canonical_original_code:
long ProcessTimer()
{
 struct Node *it;
 struct Node *itPrev;
 struct time_node *tvTimer;
 unsigned long ulNow;
 long ulRet = -1;
 ulNow = get_tick_count();
 if(g_timer == ((void *)0))
  return -1;
 LockList(g_timer);
 it = First(g_timer);
 itPrev = Header(g_timer);
 while(it != ((void *)0))
 {
  tvTimer = (struct time_node *)Retrieve(it);
  if(ulNow >= tvTimer->ulNextTime)
  {
   tvTimer->OnTimer(tvTimer->pPtr, tvTimer->ulID);
   if(itPrev->Next != it)
   {
    it = itPrev->Next;
    continue;
   }
   if(tvTimer->nRemainCount == 0)
   {
    free(tvTimer);
    it = Advance(it);
    itPrev->Next = it;
   }
   else
   {
    if(tvTimer->nRemainCount > 0)
     tvTimer->nRemainCount--;
    tvTimer->ulNextTime += tvTimer->nInterval;
    it = Rearrange(itPrev, it);
   }
  }
  else
  {
   ulRet = tvTimer->ulNextTime - ulNow;
   break;
  }
 }
 UnLockList(g_timer);
 return ulRet;
}

variable_types:
ulNow
unsigned long

variable_types: ulRet long

return_type: long


RegisterTimer

decompiled_code:
__int64 __fastcall RegisterTimer(int a1, __int64 a2, __int64 a3, int a4)
{
_QWORD *v7; // rax
_QWORD *v8; // rbp
__time_t v9; // rax
__int64 v10; // rdi
__int64 v11; // rdx
__int64 v12; // rbx
__int64 v13; // r12
__int64 v14; // rax

v7 = malloc(0x30uLL);
v7[2] = a3;
v8 = v7;
v7[4] = a2;
v9 = get_tick_count();
v10 = g_timer;
*((_DWORD *)v8 + 6) = a4;
*((_DWORD *)v8 + 2) = a1;
*v8 = v9 + a1;
v11 = g_ulTimerSeq + 1;
v8[5] = g_ulTimerSeq;
g_ulTimerSeq = v11;
LockList(v10);
v12 = First(g_timer);
v13 = Header(g_timer);
if ( v12 )
{
while ( *(_QWORD *)Retrieve(v12) <= *v8 )
{
v13 = v12;
v14 = Advance(v12);
if ( !v14 )
break;
v12 = v14;
}
}
Insert(v8, g_timer, v13);
UnLockList(g_timer);
NotifyHandler();
return v8[5];
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1, __int64 a2, __int64 a3, int a4)
{
_QWORD *v7; // rax
_QWORD *v8; // rbp
__time_t v9; // rax
__int64 v10; // rdi
__int64 v11; // rdx
__int64 v12; // rbx
__int64 v13; // r12
__int64 v14; // rax

v7 = malloc(0x30uLL);
v7[2] = a3;
v8 = v7;
v7[4] = a2;
v9 = func4();
v10 = g_timer;
*((_DWORD *)v8 + 6) = a4;
*((_DWORD *)v8 + 2) = a1;
*v8 = v9 + a1;
v11 = g_ulTimerSeq + 1;
v8[5] = g_ulTimerSeq;
g_ulTimerSeq = v11;
LockList(v10);
v12 = First(g_timer);
v13 = Header(g_timer);
if ( v12 )
{
while ( *(_QWORD *)Retrieve(v12) <= *v8 )
{
v13 = v12;
v14 = Advance(v12);
if ( !v14 )
break;
v12 = v14;
}
}
Insert(v8, g_timer, v13);
UnLockList(g_timer);
NotifyHandler();
return v8[5];
}

original_code:
unsigned long RegisterTimer(int milisecond, P2PTimerProc callback, void* pParam, int nRepeatTime)
{
 struct time_node* pTimerNode;
 struct time_node* pTemp;
 Position it, itPrev;
 pTimerNode = (struct time_node*)malloc(sizeof(struct time_node));
 pTimerNode->pPtr = pParam;
 pTimerNode->OnTimer = callback;
 pTimerNode->ulNextTime = get_tick_count() + milisecond;
 pTimerNode->nRemainCount = nRepeatTime;
 pTimerNode->ulID = g_ulTimerSeq++;
 pTimerNode->nInterval = milisecond;
 LockList(g_timer);
 it = First(g_timer);
 itPrev = Header(g_timer);
 while(it != ((void *)0))
 {
  pTemp = (struct time_node*)Retrieve(it);
  if(pTemp->ulNextTime > pTimerNode->ulNextTime)
   break;
  itPrev = it;
  it = Advance(it);
 }
 Insert(pTimerNode, g_timer, itPrev);
 UnLockList(g_timer);
 NotifyHandler();
 return pTimerNode->ulID;
}

canonical_original_code:
unsigned long RegisterTimer(int milisecond, void (*callback)(void *pPtr, unsigned long uID), void *pParam, int nRepeatTime)
{
 struct time_node *pTimerNode;
 struct time_node *pTemp;
 struct Node *it; struct Node *itPrev;
 pTimerNode = (struct time_node *)malloc(sizeof(struct time_node));
 pTimerNode->pPtr = pParam;
 pTimerNode->OnTimer = callback;
 pTimerNode->ulNextTime = get_tick_count() + milisecond;
 pTimerNode->nRemainCount = nRepeatTime;
 pTimerNode->ulID = g_ulTimerSeq++;
 pTimerNode->nInterval = milisecond;
 LockList(g_timer);
 it = First(g_timer);
 itPrev = Header(g_timer);
 while(it != ((void *)0))
 {
  pTemp = (struct time_node *)Retrieve(it);
  if(pTemp->ulNextTime > pTimerNode->ulNextTime)
   break;
  itPrev = it;
  it = Advance(it);
 }
 Insert(pTimerNode, g_timer, itPrev);
 UnLockList(g_timer);
 NotifyHandler();
 return pTimerNode->ulID;
}

variable_types: milisecond int

variable_types:
callback
void (*)(void *pPtr, unsigned long uID)

variable_types:
pParam
void *

variable_types: nRepeatTime int


InitLock

decompiled_code:
int __fastcall InitLock(pthread_mutex_t *mutex)
{
pthread_mutexattr_t attr; // [rsp+Ch] [rbp-Ch] BYREF

pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, 1);
pthread_mutex_init(mutex, &attr);
return pthread_mutexattr_destroy(&attr);
}

canonical_decompiled_code:
int __fastcall func0(pthread_mutex_t *mutex)
{
pthread_mutexattr_t attr; // [rsp+Ch] [rbp-Ch] BYREF

pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, 1);
pthread_mutex_init(mutex, &attr);
return pthread_mutexattr_destroy(&attr);
}

original_code:
void InitLock(THREAD_MUTEX_T* pLock)
{
 pthread_mutexattr_t mutexattr;
 pthread_mutexattr_init(&mutexattr);
 pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE_NP);
 pthread_mutex_init(pLock, &mutexattr);
 pthread_mutexattr_destroy(&mutexattr);
}

canonical_original_code:
void InitLock(union pthread_mutex_t *pLock)
{
 union pthread_mutexattr_t mutexattr;
 pthread_mutexattr_init(&mutexattr);
 pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE_NP);
 pthread_mutex_init(pLock, &mutexattr);
 pthread_mutexattr_destroy(&mutexattr);
}

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

variable_types: union pthread_mutexattr_t { char __size[4]; int __align; }


UnInitLock

decompiled_code:
// attributes: thunk
int UnInitLock(pthread_mutex_t *mutex)
{
return pthread_mutex_destroy(mutex);
}

canonical_decompiled_code:
int func1(pthread_mutex_t *mutex)
{
return pthread_mutex_destroy(mutex);
}

original_code:
void UnInitLock(THREAD_MUTEX_T* pLock)
{
 pthread_mutex_destroy(pLock);
}

canonical_original_code:
void UnInitLock(union pthread_mutex_t *pLock)
{
 pthread_mutex_destroy(pLock);
}


Lock

decompiled_code:
// attributes: thunk
int Lock(pthread_mutex_t *mutex)
{
return pthread_mutex_lock(mutex);
}

canonical_decompiled_code:
int func2(pthread_mutex_t *mutex)
{
return pthread_mutex_lock(mutex);
}

original_code:
void Lock(THREAD_MUTEX_T* pLock)
{
 pthread_mutex_lock(pLock);
}

canonical_original_code:
void Lock(union pthread_mutex_t *pLock)
{
 pthread_mutex_lock(pLock);
}


UnLock

decompiled_code:
// attributes: thunk
int UnLock(pthread_mutex_t *mutex)
{
return pthread_mutex_unlock(mutex);
}

canonical_decompiled_code:
int func3(pthread_mutex_t *mutex)
{
return pthread_mutex_unlock(mutex);
}

original_code:
void UnLock(THREAD_MUTEX_T* pLock)
{
 pthread_mutex_unlock(pLock);
}

canonical_original_code:
void UnLock(union pthread_mutex_t *pLock)
{
 pthread_mutex_unlock(pLock);
}


UnInitLog

decompiled_code:
int UnInitLog()
{
if ( g_hLogFile )
{
fclose(g_hLogFile);
g_hLogFile = 0LL;
}
return UnInitLock(&g_LockLog);
}

canonical_decompiled_code:
int func4()
{
if ( g_hLogFile )
{
fclose(g_hLogFile);
g_hLogFile = 0LL;
}
return func1(&g_LockLog);
}

original_code:
void UnInitLog()
{
 if(g_hLogFile != ((void *)0))
 {
  fclose(g_hLogFile);
  g_hLogFile = ((void *)0);
 }
 UnInitLock(&g_LockLog);
}


logMessage

decompiled_code:
int logMessage(__int64 a1, const char *a2, ...)
{
pthread_t v2; // r12
__int64 v3; // rax
struct timeval v5; // [rsp+0h] [rbp-128h] BYREF
gcc_va_list arg; // [rsp+18h] [rbp-110h] BYREF
struct tm tp; // [rsp+30h] [rbp-F8h] BYREF

va_start(arg, a2);
Lock(&g_LockLog);
if ( g_hLogFile )
{
vsnprintf(s_buffer_0, 0x7FFuLL, a2, arg);
gettimeofday(&v5, 0LL);
localtime_r(&v5.tv_sec, &tp);
v2 = pthread_self();
LODWORD(v3) = getpid();
fprintf(
g_hLogFile,
"[%02d/%02d/%04d %02d:%02d:%02d.%03lu pid=%d tid=%d] %s\r\n",
(unsigned int)(tp.tm_mon + 1),
(unsigned int)tp.tm_mday,
(unsigned int)(tp.tm_year + 1900),
(unsigned int)tp.tm_hour,
(unsigned int)tp.tm_min,
(unsigned int)tp.tm_sec,
v5.tv_usec / 1000,
v3,
v2,
s_buffer_0);
fflush(g_hLogFile);
}
else
{
vprintf(a2, arg);
puts("\r");
}
return UnLock(&g_LockLog);
}

canonical_decompiled_code:
int func5(__int64 a1, const char *a2, ...)
{
pthread_t v2; // r12
__int64 v3; // rax
struct timeval v5; // [rsp+0h] [rbp-128h] BYREF
gcc_va_list arg; // [rsp+18h] [rbp-110h] BYREF
struct tm tp; // [rsp+30h] [rbp-F8h] BYREF

va_start(arg, a2);
func2(&g_LockLog);
if ( g_hLogFile )
{
vsnprintf(s_buffer_0, 0x7FFuLL, a2, arg);
gettimeofday(&v5, 0LL);
localtime_r(&v5.tv_sec, &tp);
v2 = pthread_self();
LODWORD(v3) = getpid();
fprintf(
g_hLogFile,
"[%02d/%02d/%04d %02d:%02d:%02d.%03lu pid=%d tid=%d] %s\r\n",
(unsigned int)(tp.tm_mon + 1),
(unsigned int)tp.tm_mday,
(unsigned int)(tp.tm_year + 1900),
(unsigned int)tp.tm_hour,
(unsigned int)tp.tm_min,
(unsigned int)tp.tm_sec,
v5.tv_usec / 1000,
v3,
v2,
s_buffer_0);
fflush(g_hLogFile);
}
else
{
vprintf(a2, arg);
puts("\r");
}
return func3(&g_LockLog);
}

original_code:
void logMessage(int level, const char *format, ...)
{
 va_list ap;
 static char s_buffer[2048];
 struct timeval timeVal;
 struct tm tmVar;
 Lock(&g_LockLog);
 __builtin_va_start(ap,format);
 if(g_hLogFile == ((void *)0))
 {
  vprintf(format, ap);
  printf("\r\n");
 }
 else
 {
  vsnprintf(s_buffer, 2047, format, ap);
  gettimeofday(&timeVal, ((void *)0));
  localtime_r((const time_t*)&timeVal.tv_sec, &tmVar);
  fprintf(g_hLogFile, "[%02d/%02d/%04d %02d:%02d:%02d.%03lu pid=%d tid=%d] %s\r\n",
   tmVar.tm_mon + 1, tmVar.tm_mday, tmVar.tm_year + 1900,
   tmVar.tm_hour, tmVar.tm_min, tmVar.tm_sec,
   timeVal.tv_usec / 1000,
   getpid(), (int)pthread_self(),
   s_buffer);
  fflush(g_hLogFile);
 }
 __builtin_va_end(ap);
 UnLock(&g_LockLog);
}

canonical_original_code:
void logMessage(int level, char *format, ...)
{
 __builtin_va_list ap;
 static char s_buffer[2048];
 struct timeval timeVal;
 struct tm tmVar;
 Lock(&g_LockLog);
 __builtin_va_start(ap,format);
 if(g_hLogFile == ((void *)0))
 {
  vprintf(format, ap);
  printf("\r\n");
 }
 else
 {
  vsnprintf(s_buffer, 2047, format, ap);
  gettimeofday(&timeVal, ((void *)0));
  localtime_r((long *)&timeVal.tv_sec, &tmVar);
  fprintf(g_hLogFile, "[%02d/%02d/%04d %02d:%02d:%02d.%03lu pid=%d tid=%d] %s\r\n",
   tmVar.tm_mon + 1, tmVar.tm_mday, tmVar.tm_year + 1900,
   tmVar.tm_hour, tmVar.tm_min, tmVar.tm_sec,
   timeVal.tv_usec / 1000,
   getpid(), (int)pthread_self(),
   s_buffer);
  fflush(g_hLogFile);
 }
 __builtin_va_end(ap);
 UnLock(&g_LockLog);
}

variable_types: level int

variable_types:
format
char *

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; }

user_defined_types: struct timeval { long tv_sec; long tv_usec; }


ParseAddr

decompiled_code:
void __fastcall ParseAddr(void *src, _WORD *a2, void *a3)
{
char *v4; // rax
int v5; // ebx

if ( a3 && a2 )
{
v4 = strchr((const char *)src, 58);
if ( v4 )
{
v5 = (_DWORD)v4 - (_DWORD)src;
*a2 = atoi(v4 + 1);
if ( v5 > 0 )
{
memcpy(a3, src, v5);
*((_BYTE *)a3 + v5) = 0;
}
}
else
{
logMessage(3LL, "ParseAddr, unknow aIpAddrAndPort=%s", (const char *)src);
*a2 = 0;
}
}
}

canonical_decompiled_code:
void __fastcall func6(void *src, _WORD *a2, void *a3)
{
char *v4; // rax
int v5; // ebx

if ( a3 && a2 )
{
v4 = strchr((const char *)src, 58);
if ( v4 )
{
v5 = (_DWORD)v4 - (_DWORD)src;
*a2 = atoi(v4 + 1);
if ( v5 > 0 )
{
memcpy(a3, src, v5);
*((_BYTE *)a3 + v5) = 0;
}
}
else
{
func5(3LL, "ParseAddr, unknow aIpAddrAndPort=%s", (const char *)src);
*a2 = 0;
}
}
}

original_code:
void ParseAddr(unsigned char* szIn, unsigned short* pPort, char* pIP, int nIPLen)
{
 char *szFind = strchr((char*)szIn, ':');
 int nAddrLen = 0;
 if(szIn == ((void *)0) || pPort == ((void *)0) || pIP == ((void *)0))
  return;
 if(!szFind)
 {
  logMessage(logError, "ParseAddr, unknow aIpAddrAndPort=%s", szIn);
  *pPort = 0;
  szFind = (char*)szIn;
 }
 else
 {
  *pPort = (unsigned short)atoi(szFind + 1);
 }
 nAddrLen = szFind - (char*)szIn;
 if(nAddrLen > 0)
 {
  memcpy(pIP, szIn, nAddrLen);
  pIP[nAddrLen] = '\0';
 }
}

canonical_original_code:
void ParseAddr(unsigned char *szIn, unsigned short *pPort, char *pIP, int nIPLen)
{
 char *szFind = strchr((char*)szIn, ':');
 int nAddrLen = 0;
 if(szIn == ((void *)0) || pPort == ((void *)0) || pIP == ((void *)0))
  return;
 if(!szFind)
 {
  logMessage(logError, "ParseAddr, unknow aIpAddrAndPort=%s", szIn);
  *pPort = 0;
  szFind = (char *)szIn;
 }
 else
 {
  *pPort = (unsigned short)atoi(szFind + 1);
 }
 nAddrLen = szFind - (char *)szIn;
 if(nAddrLen > 0)
 {
  memcpy(pIP, szIn, nAddrLen);
  pIP[nAddrLen] = '\0';
 }
}

variable_types:
szIn
unsigned char *

variable_types:
pPort
unsigned short *

variable_types:
pIP
char *

variable_types: nIPLen int

variable_types:
szFind
char *

variable_types: nAddrLen int


main

repo: oftn/melody

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
unsigned int v4; // er14
const char *v5; // r12
__int64 v6; // rdx
const char *v7; // rax
char *v8; // r8
int result; // eax
__int64 v10; // rbp
char v11[4136]; // [rsp+0h] [rbp-1028h] BYREF

if ( argc <= 1 )
goto LABEL_13;
v3 = 1;
v4 = 0;
v5 = 0LL;
do
{
while ( 1 )
{
v6 = v3;
v7 = argv[v6];
if ( *(_WORD *)v7 != 26925 || v7[2] )
break;
v3 += 2;
v4 = atoi(argv[v6 + 1]);
if ( argc <= v3 )
goto LABEL_7;
}
++v3;
v5 = argv[v6];
}
while ( argc > v3 );
LABEL_7:
if ( v5 )
{
v8 = fgets(v11, 4096, stdin);
result = 0;
if ( v8 )
{
v10 = music_parse(v11);
if ( !(unsigned __int8)music_generate(v5, 120LL, v10, v4) )
fwrite("Could not do music generation\n", 1uLL, 0x1EuLL, stderr);
music_item_destroy(v10);
result = 0;
}
}
else
{
LABEL_13:
fprintf(stderr, "Usage: %s [-i <instrument_number>] filename.mid\n\n", *argv);
result = 1;
}
return result;
}

original_code:
int main(int argc, char *argv[]) {
 char buffer[4096];
 char *filename = ((void *)0);
 int instr_id = 0;
 for (int i = 1; i < argc; i++) {
  if (strcmp (argv[i], "-i") == 0) {
   i++;
   instr_id = atoi (argv[i]);
  } else {
   filename = argv[i];
  }
 }
 if (!filename) {
  fprintf (stderr, "Usage: %s [-i <instrument_number>] filename.mid\n\n", argv[0]);
  return 1;
 }
 if (fgets (buffer, 4096, stdin)) {
  music_item_t *music = music_parse (buffer);
  if (!music_generate (filename, 120, music, instr_id))
   fprintf (stderr, "Could not do music generation\n");
  music_item_destroy (music);
 }
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
 char buffer[4096];
 char *filename = ((void *)0);
 int instr_id = 0;
 for (int i = 1; i < argc; i++) {
  if (strcmp (argv[i], "-i") == 0) {
   i++;
   instr_id = atoi (argv[i]);
  } else {
   filename = argv[i];
  }
 }
 if (!filename) {
  fprintf (stderr, "Usage: %s [-i <instrument_number>] filename.mid\n\n", argv[0]);
  return 1;
 }
 if (fgets (buffer, 4096, stdin)) {
  struct music_item *music = music_parse (buffer);
  if (!music_generate (filename, 120, music, instr_id))
   fprintf (stderr, "Could not do music generation\n");
  music_item_destroy (music);
 }
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
buffer
char [4096]

variable_types:
filename
char *

variable_types: i int

variable_types: struct music_item { enum music_item_type { MUSIC_ITEM_NOTE=0, MUSIC_ITEM_REST=1, MUSIC_ITEM_TEMPO=2, MUSIC_ITEM_SUB=3 } type; union { struct music_item_note { unsigned int id; unsigned int frac; unsigned int beats; } note; unsigned int tempo; struct music_item_sub { unsigned int beats; struct music_item *first; } sub; } value; struct music_item *next; } *

user_defined_types: struct music_item { enum music_item_type type; union { struct music_item_note note; unsigned int tempo; struct music_item_sub sub; } value; struct music_item *next; }


decompiled_code:
bool __fastcall music_create_note(unsigned int *a1, unsigned int a2, char a3, char a4)
{
int v4; // esi
__int64 v5; // rdx
int v6; // edx
int v7; // eax
unsigned int v8; // eax

if ( (unsigned __int8)(a3 - 65) > 6u || a2 > 9 )
return 0;
v4 = 4 * (3 * a2 + 3);
if ( a3 <= 66 )
v5 = (char)(a3 - 60);
else
v5 = (char)(a3 - 67);
v6 = (unsigned __int16)lookup_0[v5] + v4;
v7 = 1;
if ( a4 != 43 )
v7 = -(a4 == 45);
v8 = v6 + v7;
*a1 = v8;
return v8 <= 0x7F;
}

canonical_decompiled_code:
bool __fastcall func0(unsigned int *a1, unsigned int a2, char a3, char a4)
{
int v4; // esi
__int64 v5; // rdx
int v6; // edx
int v7; // eax
unsigned int v8; // eax

if ( (unsigned __int8)(a3 - 65) > 6u || a2 > 9 )
return 0;
v4 = 4 * (3 * a2 + 3);
if ( a3 <= 66 )
v5 = (char)(a3 - 60);
else
v5 = (char)(a3 - 67);
v6 = (unsigned __int16)lookup_0[v5] + v4;
v7 = 1;
if ( a4 != 43 )
v7 = -(a4 == 45);
v8 = v6 + v7;
*a1 = v8;
return v8 <= 0x7F;
}

original_code:
static _Bool music_create_note (music_item_note_t *note, unsigned int octave, char letter, char modifier) {
 static unsigned short int lookup[] = { 0, 2, 4, 5, 7, 9, 11 };
 if (octave > 9 || letter < 'A' || letter > 'G')
  return 0;
 note->id =
                       (octave + 1) * 12 +
                       lookup[(letter >= 'C') ? (letter - 'C') : (letter + 5 - 'A')] +
                       ((modifier == '+') ? 1 : (modifier == '-' ? -1 : 0));
 if (note->id > 127)
  return 0;
 return 1;
}

canonical_original_code:
static _Bool music_create_note(struct music_item_note *note, unsigned int octave, char letter, char modifier) {
 static unsigned short lookup[] = { 0, 2, 4, 5, 7, 9, 11 };
 if (octave > 9 || letter < 'A' || letter > 'G')
  return 0;
 note->id =
                       (octave + 1) * 12 +
                       lookup[(letter >= 'C') ? (letter - 'C') : (letter + 5 - 'A')] +
                       ((modifier == '+') ? 1 : (modifier == '-' ? -1 : 0));
 if (note->id > 127)
  return 0;
 return 1;
}

variable_types: struct music_item_note { unsigned int id; unsigned int frac; unsigned int beats; } *

variable_types:
octave
unsigned int

variable_types: letter char

variable_types: modifier char

variable_types:
lookup
unsigned short []


decompiled_code:
__int64 __fastcall music_item_push(_QWORD *a1, __int64 a2, _QWORD *a3)
{
__int64 result; // rax

if ( *(_QWORD *)a2 )
*(_QWORD *)(*(_QWORD *)a2 + 24LL) = *a3;
if ( !*a1 )
*a1 = *a3;
result = *a3;
*(_QWORD *)a2 = *a3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(_QWORD *a1, __int64 a2, _QWORD *a3)
{
__int64 result; // rax

if ( *(_QWORD *)a2 )
*(_QWORD *)(*(_QWORD *)a2 + 24LL) = *a3;
if ( !*a1 )
*a1 = *a3;
result = *a3;
*(_QWORD *)a2 = *a3;
return result;
}

original_code:
static void music_item_push (music_item_t **head, music_item_t **tail, music_item_t **item) {
 if (!head || !tail || !item)
  return;
 if (*tail)
  (*tail)->next = *item;
 if (!(*head))
  *head = *item;
 *tail = *item;
}

canonical_original_code:
static void music_item_push(struct music_item **head, struct music_item **tail, struct music_item **item) {
 if (!head || !tail || !item)
  return;
 if (*tail)
  (*tail)->next = *item;
 if (!(*head))
  *head = *item;
 *tail = *item;
}

variable_types: struct music_item { enum music_item_type { MUSIC_ITEM_NOTE=0, MUSIC_ITEM_REST=1, MUSIC_ITEM_TEMPO=2, MUSIC_ITEM_SUB=3 } type; union { struct music_item_note { unsigned int id; unsigned int frac; unsigned int beats; } note; unsigned int tempo; struct music_item_sub { unsigned int beats; struct music_item *first; } sub; } value; struct music_item *next; } **

variable_types:
tail
struct music_item **

variable_types:
item
struct music_item **


decompiled_code:
__int64 __fastcall music_parse_unsigned_int(int *a1, char **a2)
{
const unsigned __int16 **v3; // rax
unsigned int v4; // er8
const unsigned __int16 *v5; // rsi
char *v6; // rax
int v7; // edx
int v8; // edx

v3 = _ctype_b_loc();
v4 = 0;
v5 = *v3;
v6 = *a2;
if ( (v5[**a2] & 0x800) != 0 )
{
v7 = 0;
do
{
++v6;
v8 = 10 * v7;
*a1 = v8;
v7 = v8 + *(v6 - 1) - 48;
*a1 = v7;
*a2 = v6;
}
while ( (v5[*v6] & 0x800) != 0 );
v4 = 1;
}
return v4;
}

original_code:
static _Bool music_parse_unsigned_int (unsigned int *out, char **input) {
 if (!((*__ctype_b_loc ())[(int) ((**input))] & (unsigned short int) _ISdigit))
  return 0;
 *out = 0;
 do {
  *out *= 10;
  *out += (**input - '0');
  (*input)++;
 } while (((*__ctype_b_loc ())[(int) ((**input))] & (unsigned short int) _ISdigit));
 return 1;
}

canonical_original_code:
static _Bool music_parse_unsigned_int(unsigned int *out, char **input) {
 if (!((*__ctype_b_loc ())[(int) ((**input))] & (unsigned short) _ISdigit))
  return 0;
 *out = 0;
 do {
  *out *= 10;
  *out += (**input - '0');
  (*input)++;
 } while (((*__ctype_b_loc ())[(int) ((**input))] & (unsigned short) _ISdigit));
 return 1;
}

variable_types:
out
unsigned int *

variable_types:
input
char **


decompiled_code:
__int64 __fastcall music_parse(char *a1)
{
char *v2; // [rsp+8h] [rbp-30h] BYREF
int v3; // [rsp+14h] [rbp-24h] BYREF
__int64 v4; // [rsp+18h] [rbp-20h] BYREF
_QWORD *v5; // [rsp+20h] [rbp-18h] BYREF
__int64 v6; // [rsp+28h] [rbp-10h] BYREF

v2 = a1;
v4 = 0LL;
v5 = 0LL;
v6 = 0LL;
v3 = 4;
if ( !a1 )
return 0LL;
do
{
v5 = music_item_parse(&v2, &v3);
music_item_push(&v4, (__int64)&v6, &v5);
}
while ( v5 );
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *a1)
{
char *v2; // [rsp+8h] [rbp-30h] BYREF
int v3; // [rsp+14h] [rbp-24h] BYREF
__int64 v4; // [rsp+18h] [rbp-20h] BYREF
_QWORD *v5; // [rsp+20h] [rbp-18h] BYREF
__int64 v6; // [rsp+28h] [rbp-10h] BYREF

v2 = a1;
v4 = 0LL;
v5 = 0LL;
v6 = 0LL;
v3 = 4;
if ( !a1 )
return 0LL;
do
{
v5 = music_item_parse(&v2, &v3);
func1(&v4, (__int64)&v6, &v5);
}
while ( v5 );
return v4;
}

original_code:
music_item_t * music_parse (char *input) {
 music_item_t *head = ((void *)0);
 music_item_t *item = ((void *)0);
 music_item_t *tail = ((void *)0);
 unsigned int default_octave = 4;
 if (!input)
  return ((void *)0);
 do {
  item = music_item_parse (&input, &default_octave);
  music_item_push (&head, &tail, &item);
 } while (item);
 return head;
}

canonical_original_code:
struct music_item *music_parse(char *input) {
 struct music_item *head = ((void *)0);
 struct music_item *item = ((void *)0);
 struct music_item *tail = ((void *)0);
 unsigned int default_octave = 4;
 if (!input)
  return ((void *)0);
 do {
  item = music_item_parse (&input, &default_octave);
  music_item_push (&head, &tail, &item);
 } while (item);
 return head;
}

variable_types:
input
char *

return_type: struct music_item { enum music_item_type { MUSIC_ITEM_NOTE=0, MUSIC_ITEM_REST=1, MUSIC_ITEM_TEMPO=2, MUSIC_ITEM_SUB=3 } type; union { struct music_item_note { unsigned int id; unsigned int frac; unsigned int beats; } note; unsigned int tempo; struct music_item_sub { unsigned int beats; struct music_item *first; } sub; } value; struct music_item *next; } *


repo: oguyon/Cfits

original_code:
void test_gtd7(void) {
   struct nrlmsise_output output[17];
 struct nrlmsise_input input[17];
   struct nrlmsise_flags flags;
 struct ap_array aph;
 int i;
 int j;
   for (i=0;i<7;i++)
  aph.a[i]=100;
 flags.switches[0]=0;
   for (i=1;i<24;i++)
    flags.switches[i]=1;
 for (i=0;i<17;i++) {
  input[i].doy=172;
  input[i].year=0;
    input[i].sec=29000;
  input[i].alt=400;
  input[i].g_lat=60;
  input[i].g_long=-70;
  input[i].lst=16;
  input[i].f107A=150;
  input[i].f107=150;
  input[i].ap=4;
 }
 input[1].doy=81;
 input[2].sec=75000;
 input[2].alt=1000;
 input[3].alt=100;
 input[10].alt=0;
 input[11].alt=10;
 input[12].alt=30;
 input[13].alt=50;
 input[14].alt=70;
 input[16].alt=100;
 input[4].g_lat=0;
 input[5].g_long=0;
 input[6].lst=4;
 input[7].f107A=70;
 input[8].f107=180;
 input[9].ap=40;
 input[15].ap_a=&aph;
 input[16].ap_a=&aph;
   for (i=0;i<15;i++)
    gtd7(&input[i], &flags, &output[i]);
   flags.switches[9]=-1;
   for (i=15;i<17;i++)
    gtd7(&input[i], &flags, &output[i]);
   for (i=0;i<17;i++) {
  printf("\n");
  for (j=0;j<9;j++)
   printf("%E ",output[i].d[j]);
  printf("%E ",output[i].t[0]);
  printf("%E \n",output[i].t[1]);
   }
 for (i=0;i<3;i++) {
  printf("\n");
  printf("\nDAY   ");
  for (j=0;j<5;j++)
   printf("         %3i",input[i*5+j].doy);
  printf("\nUT    ");
  for (j=0;j<5;j++)
   printf("       %5.0f",input[i*5+j].sec);
  printf("\nALT   ");
  for (j=0;j<5;j++)
   printf("        %4.0f",input[i*5+j].alt);
  printf("\nLAT   ");
  for (j=0;j<5;j++)
   printf("         %3.0f",input[i*5+j].g_lat);
  printf("\nLONG  ");
  for (j=0;j<5;j++)
   printf("         %3.0f",input[i*5+j].g_long);
  printf("\nLST   ");
  for (j=0;j<5;j++)
   printf("       %5.0f",input[i*5+j].lst);
  printf("\nF107A ");
  for (j=0;j<5;j++)
   printf("         %3.0f",input[i*5+j].f107A);
  printf("\nF107  ");
  for (j=0;j<5;j++)
   printf("         %3.0f",input[i*5+j].f107);
  printf("\n\n");
    printf("\nTINF  ");
  for (j=0;j<5;j++)
   printf("     %7.2f",output[i*5+j].t[0]);
  printf("\nTG    ");
  for (j=0;j<5;j++)
   printf("     %7.2f",output[i*5+j].t[1]);
  printf("\nHE    ");
  for (j=0;j<5;j++)
   printf("   %1.3e",output[i*5+j].d[0]);
  printf("\nO     ");
  for (j=0;j<5;j++)
   printf("   %1.3e",output[i*5+j].d[1]);
  printf("\nN2    ");
  for (j=0;j<5;j++)
   printf("   %1.3e",output[i*5+j].d[2]);
  printf("\nO2    ");
  for (j=0;j<5;j++)
   printf("   %1.3e",output[i*5+j].d[3]);
  printf("\nAR    ");
  for (j=0;j<5;j++)
   printf("   %1.3e",output[i*5+j].d[4]);
  printf("\nH     ");
  for (j=0;j<5;j++)
   printf("   %1.3e",output[i*5+j].d[6]);
  printf("\nN     ");
  for (j=0;j<5;j++)
   printf("   %1.3e",output[i*5+j].d[7]);
  printf("\nANM 0 ");
  for (j=0;j<5;j++)
   printf("   %1.3e",output[i*5+j].d[8]);
  printf("\nRHO   ");
  for (j=0;j<5;j++)
   printf("   %1.3e",output[i*5+j].d[5]);
   printf("\n");
 }
 printf("\n");
}

canonical_original_code:
void test_gtd7() {
   struct nrlmsise_output output[17];
 struct nrlmsise_input input[17];
   struct nrlmsise_flags flags;
 struct ap_array aph;
 int i;
 int j;
   for (i=0;i<7;i++)
  aph.a[i]=100;
 flags.switches[0]=0;
   for (i=1;i<24;i++)
    flags.switches[i]=1;
 for (i=0;i<17;i++) {
  input[i].doy=172;
  input[i].year=0;
    input[i].sec=29000;
  input[i].alt=400;
  input[i].g_lat=60;
  input[i].g_long=-70;
  input[i].lst=16;
  input[i].f107A=150;
  input[i].f107=150;
  input[i].ap=4;
 }
 input[1].doy=81;
 input[2].sec=75000;
 input[2].alt=1000;
 input[3].alt=100;
 input[10].alt=0;
 input[11].alt=10;
 input[12].alt=30;
 input[13].alt=50;
 input[14].alt=70;
 input[16].alt=100;
 input[4].g_lat=0;
 input[5].g_long=0;
 input[6].lst=4;
 input[7].f107A=70;
 input[8].f107=180;
 input[9].ap=40;
 input[15].ap_a=&aph;
 input[16].ap_a=&aph;
   for (i=0;i<15;i++)
    gtd7(&input[i], &flags, &output[i]);
   flags.switches[9]=-1;
   for (i=15;i<17;i++)
    gtd7(&input[i], &flags, &output[i]);
   for (i=0;i<17;i++) {
  printf("\n");
  for (j=0;j<9;j++)
   printf("%E ",output[i].d[j]);
  printf("%E ",output[i].t[0]);
  printf("%E \n",output[i].t[1]);
   }
 for (i=0;i<3;i++) {
  printf("\n");
  printf("\nDAY   ");
  for (j=0;j<5;j++)
   printf("         %3i",input[i*5+j].doy);
  printf("\nUT    ");
  for (j=0;j<5;j++)
   printf("       %5.0f",input[i*5+j].sec);
  printf("\nALT   ");
  for (j=0;j<5;j++)
   printf("        %4.0f",input[i*5+j].alt);
  printf("\nLAT   ");
  for (j=0;j<5;j++)
   printf("         %3.0f",input[i*5+j].g_lat);
  printf("\nLONG  ");
  for (j=0;j<5;j++)
   printf("         %3.0f",input[i*5+j].g_long);
  printf("\nLST   ");
  for (j=0;j<5;j++)
   printf("       %5.0f",input[i*5+j].lst);
  printf("\nF107A ");
  for (j=0;j<5;j++)
   printf("         %3.0f",input[i*5+j].f107A);
  printf("\nF107  ");
  for (j=0;j<5;j++)
   printf("         %3.0f",input[i*5+j].f107);
  printf("\n\n");
    printf("\nTINF  ");
  for (j=0;j<5;j++)
   printf("     %7.2f",output[i*5+j].t[0]);
  printf("\nTG    ");
  for (j=0;j<5;j++)
   printf("     %7.2f",output[i*5+j].t[1]);
  printf("\nHE    ");
  for (j=0;j<5;j++)
   printf("   %1.3e",output[i*5+j].d[0]);
  printf("\nO     ");
  for (j=0;j<5;j++)
   printf("   %1.3e",output[i*5+j].d[1]);
  printf("\nN2    ");
  for (j=0;j<5;j++)
   printf("   %1.3e",output[i*5+j].d[2]);
  printf("\nO2    ");
  for (j=0;j<5;j++)
   printf("   %1.3e",output[i*5+j].d[3]);
  printf("\nAR    ");
  for (j=0;j<5;j++)
   printf("   %1.3e",output[i*5+j].d[4]);
  printf("\nH     ");
  for (j=0;j<5;j++)
   printf("   %1.3e",output[i*5+j].d[6]);
  printf("\nN     ");
  for (j=0;j<5;j++)
   printf("   %1.3e",output[i*5+j].d[7]);
  printf("\nANM 0 ");
  for (j=0;j<5;j++)
   printf("   %1.3e",output[i*5+j].d[8]);
  printf("\nRHO   ");
  for (j=0;j<5;j++)
   printf("   %1.3e",output[i*5+j].d[5]);
   printf("\n");
 }
 printf("\n");
}

variable_types: struct nrlmsise_output { double d[9]; double t[2]; } [17]

variable_types: struct nrlmsise_input { int year; int doy; double sec; double alt; double g_lat; double g_long; double lst; double f107A; double f107; double ap; struct ap_array { double a[7]; } *ap_a; } [17]

variable_types: struct nrlmsise_flags { int switches[24]; double sw[24]; double swc[24]; }

variable_types:
aph
struct ap_array { double a[7]; }

variable_types: j int

user_defined_types: struct nrlmsise_input { int year; int doy; double sec; double alt; double g_lat; double g_long; double lst; double f107A; double f107; double ap; struct ap_array *ap_a; }


decompiled_code:
// local variable allocation has failed, the output may be wrong!
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rcx
__int64 v4; // r8
__int64 v5; // r9
__int64 v7; // [rsp+0h] [rbp-8h]

test_gtd7(*(__int64 *)&argc, (__int64)argv, (__int64)envp, v3, v4, v5, v7);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rcx
__int64 v4; // r8
__int64 v5; // r9
__int64 v7; // [rsp+0h] [rbp-8h]

func0(*(__int64 *)&argc, (__int64)argv, (__int64)envp, v3, v4, v5, v7);
return 0;
}

original_code:
int main(void) {
 test_gtd7();
 return 0;
}

canonical_original_code:
int main() {
 test_gtd7();
 return 0;
}


decompiled_code:
_BOOL8 __fastcall nip_heap_less_than(__int64 a1, __int64 a2)
{
_BOOL8 result; // rax
int v3; // edx

result = 0LL;
if ( a1 )
{
result = 1LL;
if ( a2 )
{
v3 = *(_DWORD *)(a2 + 16);
if ( *(_DWORD *)(a1 + 16) >= v3 )
{
result = 0LL;
if ( *(_DWORD *)(a1 + 16) == v3 )
result = *(_DWORD *)(a1 + 20) < *(_DWORD *)(a2 + 20);
}
}
}
return result;
}

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

result = 0LL;
if ( a1 )
{
result = 1LL;
if ( a2 )
{
v3 = *(_DWORD *)(a2 + 16);
if ( *(_DWORD *)(a1 + 16) >= v3 )
{
result = 0LL;
if ( *(_DWORD *)(a1 + 16) == v3 )
result = *(_DWORD *)(a1 + 20) < *(_DWORD *)(a2 + 20);
}
}
}
return result;
}

original_code:
static int nip_heap_less_than(nip_heap_item h1, nip_heap_item h2) {
  if(h1!=((void *)0)){
    if(h2!=((void *)0))
      return ((h1->primary_key < h2->primary_key) ||
       (h1->primary_key == h2->primary_key &&
        h1->secondary_key < h2->secondary_key));
    else
      return 1;
  }
  return 0;
}

canonical_original_code:
static int nip_heap_less_than(struct nip_heap_item_struct *h1, struct nip_heap_item_struct *h2) {
  if(h1!=((void *)0)){
    if(h2!=((void *)0))
      return ((h1->primary_key < h2->primary_key) ||
       (h1->primary_key == h2->primary_key &&
        h1->secondary_key < h2->secondary_key));
    else
      return 1;
  }
  return 0;
}

variable_types: struct nip_heap_item_struct { int content_size; void *content; int primary_key; int secondary_key; } *


original_code:
int nip_heap_insert(nip_heap h, void* content, int size) {
  int i;
  nip_heap_item hi;
  nip_heap_item* bigger;
  hi = (nip_heap_item) malloc(sizeof(nip_heap_item_struct));
  if(!hi)
    return nip_report_error("src/nipheap.c", 90, 12, 1);
  hi->content = content;
  hi->content_size = size;
  hi->primary_key = (*h->primary_key)(hi->content, hi->content_size);
  hi->secondary_key = (*h->secondary_key)(hi->content, hi->content_size);
  if(h->heap_size == h->allocated_size){
    i = 2 * h->allocated_size;
    bigger = (nip_heap_item*) realloc(h->heap_items, i);
    if(bigger != ((void *)0)){
      h->heap_items = bigger;
      h->allocated_size = i;
      for(i = h->allocated_size-1; i >= h->heap_size; i--)
 h->heap_items[i] = ((void *)0);
    }
    else
      return nip_report_error("src/nipheap.c", 108, 12, 1);
  }
  h->heap_items[h->heap_size] = hi;
  h->heap_size++;
  h->heapified = 0;
  return 0;
}

canonical_original_code:
int nip_heap_insert(struct nip_heap_struct *h, void *content, int size) {
  int i;
  struct nip_heap_item_struct *hi;
  struct nip_heap_item_struct **bigger;
  hi = (struct nip_heap_item_struct *) malloc(sizeof(nip_heap_item_struct));
  if(!hi)
    return nip_report_error("src/nipheap.c", 90, 12, 1);
  hi->content = content;
  hi->content_size = size;
  hi->primary_key = (*h->primary_key)(hi->content, hi->content_size);
  hi->secondary_key = (*h->secondary_key)(hi->content, hi->content_size);
  if(h->heap_size == h->allocated_size){
    i = 2 * h->allocated_size;
    bigger = (struct nip_heap_item_struct **) realloc(h->heap_items, i);
    if(bigger != ((void *)0)){
      h->heap_items = bigger;
      h->allocated_size = i;
      for(i = h->allocated_size-1; i >= h->heap_size; i--)
 h->heap_items[i] = ((void *)0);
    }
    else
      return nip_report_error("src/nipheap.c", 108, 12, 1);
  }
  h->heap_items[h->heap_size] = hi;
  h->heap_size++;
  h->heapified = 0;
  return 0;
}

variable_types: struct nip_heap_struct { int allocated_size; struct nip_heap_item_struct { int content_size; void *content; int primary_key; int secondary_key; } **heap_items; int heap_size; int (*primary_key)(void *item, int size); int (*secondary_key)(void *item, int size); int heapified; struct nip_int_list_type { int length; struct nip_int_link_type { int data; struct nip_int_link_type *fwd; struct nip_int_link_type *bwd; } *first; struct nip_int_link_type *last; } *updated_items; } *

variable_types:
content
void *

variable_types: size int

variable_types: i int

user_defined_types: struct nip_heap_struct { int allocated_size; struct nip_heap_item_struct **heap_items; int heap_size; int (*primary_key)(void *item, int size); int (*secondary_key)(void *item, int size); int heapified; struct nip_int_list_type *updated_items; }

user_defined_types: struct nip_int_list_type { int length; struct nip_int_link_type *first; struct nip_int_link_type *last; }


decompiled_code:
__int64 __fastcall nip_search_heap_item(__int64 a1, unsigned int (__fastcall *a2)(_QWORD, _QWORD, __int64, _QWORD), __int64 a3, unsigned int a4)
{
__int64 v6; // rbx
unsigned int v7; // er14
unsigned int *v8; // rax

if ( *(int *)(a1 + 16) <= 0 )
return (unsigned int)-1;
v6 = 0LL;
while ( 1 )
{
v7 = v6;
v8 = *(unsigned int **)(*(_QWORD *)(a1 + 8) + 8 * v6);
if ( a2(*((_QWORD *)v8 + 1), *v8, a3, a4) )
break;
if ( *(_DWORD *)(a1 + 16) <= (int)++v6 )
return (unsigned int)-1;
}
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, unsigned int (__fastcall *a2)(_QWORD, _QWORD, __int64, _QWORD), __int64 a3, unsigned int a4)
{
__int64 v6; // rbx
unsigned int v7; // er14
unsigned int *v8; // rax

if ( *(int *)(a1 + 16) <= 0 )
return (unsigned int)-1;
v6 = 0LL;
while ( 1 )
{
v7 = v6;
v8 = *(unsigned int **)(*(_QWORD *)(a1 + 8) + 8 * v6);
if ( a2(*((_QWORD *)v8 + 1), *v8, a3, a4) )
break;
if ( *(_DWORD *)(a1 + 16) <= (int)++v6 )
return (unsigned int)-1;
}
return v7;
}

original_code:
int nip_search_heap_item(nip_heap h,
    int (*comparison)(void* i, int isize,
        void* r, int rsize),
    void* ref, int refsize){
    int n;
    nip_heap_item hi;
    for (n = 0; n < h->heap_size; n++) {
      hi = h->heap_items[n];
      if ((*comparison)(hi->content, hi->content_size, ref, refsize))
 return n;
    }
    return -1;
}

canonical_original_code:
int nip_search_heap_item(struct nip_heap_struct *h, int (*comparison)(void *i, int isize, void *r, int rsize), void *ref, int refsize){
    int n;
    struct nip_heap_item_struct *hi;
    for (n = 0; n < h->heap_size; n++) {
      hi = h->heap_items[n];
      if ((*comparison)(hi->content, hi->content_size, ref, refsize))
 return n;
    }
    return -1;
}

variable_types: int (*)(void *i, int isize, void *r, int rsize)

variable_types:
ref
void *

variable_types: refsize int

variable_types: n int

user_defined_types: struct nip_heap_item_struct { int content_size; void *content; int primary_key; int secondary_key; }

user_defined_types: struct nip_int_link_type { int data; struct nip_int_link_type *fwd; struct nip_int_link_type *bwd; }


original_code:
int nip_set_heap_item(nip_heap h, int index,
        void* content, int size) {
  nip_heap_item hi;
  if (h == ((void *)0) || index < 0 || index >= h->heap_size || size <= 0)
    return nip_report_error("src/nipheap.c", 159, 22, 1);
  hi = h->heap_items[index];
  hi->content = content;
  hi->content_size = size;
  hi->primary_key = (*h->primary_key)(hi->content, hi->content_size);
  hi->secondary_key = (*h->secondary_key)(hi->content, hi->content_size);
  nip_append_int(h->updated_items, index);
  return 0;
}

canonical_original_code:
int nip_set_heap_item(struct nip_heap_struct *h, int index, void *content, int size) {
  struct nip_heap_item_struct *hi;
  if (h == ((void *)0) || index < 0 || index >= h->heap_size || size <= 0)
    return nip_report_error("src/nipheap.c", 159, 22, 1);
  hi = h->heap_items[index];
  hi->content = content;
  hi->content_size = size;
  hi->primary_key = (*h->primary_key)(hi->content, hi->content_size);
  hi->secondary_key = (*h->secondary_key)(hi->content, hi->content_size);
  nip_append_int(h->updated_items, index);
  return 0;
}

variable_types: index int


original_code:
void nip_build_min_heap(nip_heap h) {
  int i, index;
  nip_int_link il;
  if(!h)
    return;
  if(!h->heapified){
    for (i = ((h->heap_size-1 -1)/2); i >= 0; i--)
      nip_min_heapify(h, i);
  }
  else {
    il = ( (h->updated_items)->first );
    while (il != ((void *)0)){
      index = ( (il)->data );
      nip_min_heapify(h, index);
      il = ( (il)->fwd );
    }
  }
  nip_empty_int_list(h->updated_items);
  h->heapified = 1;
}

canonical_original_code:
void nip_build_min_heap(struct nip_heap_struct *h) {
  int i; int index;
  struct nip_int_link_type *il;
  if(!h)
    return;
  if(!h->heapified){
    for (i = ((h->heap_size-1 -1)/2); i >= 0; i--)
      nip_min_heapify(h, i);
  }
  else {
    il = ( (h->updated_items)->first );
    while (il != ((void *)0)){
      index = ( (il)->data );
      nip_min_heapify(h, index);
      il = ( (il)->fwd );
    }
  }
  nip_empty_int_list(h->updated_items);
  h->heapified = 1;
}

variable_types: struct nip_int_link_type { int data; struct nip_int_link_type *fwd; struct nip_int_link_type *bwd; } *


decompiled_code:
void __fastcall nip_free_heap(void *ptr)
{
_QWORD *v2; // r8
__int64 v3; // rbx
void *v4; // rdi

if ( ptr )
{
v2 = (_QWORD *)*((_QWORD *)ptr + 1);
if ( *((int *)ptr + 4) > 0 )
{
v3 = 0LL;
do
{
v4 = (void *)v2[v3];
if ( v4 )
{
free(v4);
v2 = (_QWORD *)*((_QWORD *)ptr + 1);
}
++v3;
}
while ( *((_DWORD *)ptr + 4) > (int)v3 );
}
free(v2);
nip_empty_int_list(*((_QWORD *)ptr + 6));
free(*((void **)ptr + 6));
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func9(void *ptr)
{
_QWORD *v2; // r8
__int64 v3; // rbx
void *v4; // rdi

if ( ptr )
{
v2 = (_QWORD *)*((_QWORD *)ptr + 1);
if ( *((int *)ptr + 4) > 0 )
{
v3 = 0LL;
do
{
v4 = (void *)v2[v3];
if ( v4 )
{
free(v4);
v2 = (_QWORD *)*((_QWORD *)ptr + 1);
}
++v3;
}
while ( *((_DWORD *)ptr + 4) > (int)v3 );
}
free(v2);
nip_empty_int_list(*((_QWORD *)ptr + 6));
free(*((void **)ptr + 6));
free(ptr);
}
}

original_code:
void nip_free_heap(nip_heap h) {
  int i;
  nip_heap_item hi;
  if(!h)
    return;
  for(i = 0; i < h->heap_size; i++){
    hi = h->heap_items[i];
    if(hi){
      free(hi);
    }
  }
  free(h->heap_items);
  nip_empty_int_list(h->updated_items);
  free(h->updated_items);
  free(h);
  return;
}

canonical_original_code:
void nip_free_heap(struct nip_heap_struct *h) {
  int i;
  struct nip_heap_item_struct *hi;
  if(!h)
    return;
  for(i = 0; i < h->heap_size; i++){
    hi = h->heap_items[i];
    if(hi){
      free(hi);
    }
  }
  free(h->heap_items);
  nip_empty_int_list(h->updated_items);
  free(h->updated_items);
  free(h);
  return;
}


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

result = 0LL;
if ( a1 )
result = *(unsigned int *)(a1 + 16);
return result;
}

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

result = 0LL;
if ( a1 )
result = *(unsigned int *)(a1 + 16);
return result;
}

original_code:
int nip_heap_size(nip_heap h) {
  if(h)
    return h->heap_size;
  return 0;
}

canonical_original_code:
int nip_heap_size(struct nip_heap_struct *h) {
  if(h)
    return h->heap_size;
  return 0;
}


decompiled_code:
__int64 __fastcall nip_report_error(const char *a1, unsigned int a2, unsigned int a3, int a4)
{
NIP_ERROR_CODE = a3;
++NIP_ERROR_COUNTER;
if ( a4 )
{
fprintf(stderr, "In %s (%d): ", a1, a2);
switch ( a3 )
{
case 0u:
fwrite("O.K.\n", 1uLL, 5uLL, stderr);
break;
case 2u:
fwrite("Requested file not found.\n", 1uLL, 0x1AuLL, stderr);
break;
case 5u:
fwrite("I/O failure.\n", 1uLL, 0xDuLL, stderr);
break;
case 0xCu:
fwrite("Failed to allocate memory.\n", 1uLL, 0x1BuLL, stderr);
break;
case 0xEu:
fwrite("Nullpointer given.\n", 1uLL, 0x13uLL, stderr);
break;
case 0x16u:
fwrite("Invalid argument given.\n", 1uLL, 0x18uLL, stderr);
break;
case 0x21u:
fwrite("Argument outside the defined domain.\n", 1uLL, 0x25uLL, stderr);
break;
default:
fwrite("Something went wrong.\n", 1uLL, 0x16uLL, stderr);
break;
}
}
return a3;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1, unsigned int a2, unsigned int a3, int a4)
{
NIP_ERROR_CODE = a3;
++NIP_ERROR_COUNTER;
if ( a4 )
{
fprintf(stderr, "In %s (%d): ", a1, a2);
switch ( a3 )
{
case 0u:
fwrite("O.K.\n", 1uLL, 5uLL, stderr);
break;
case 2u:
fwrite("Requested file not found.\n", 1uLL, 0x1AuLL, stderr);
break;
case 5u:
fwrite("I/O failure.\n", 1uLL, 0xDuLL, stderr);
break;
case 0xCu:
fwrite("Failed to allocate memory.\n", 1uLL, 0x1BuLL, stderr);
break;
case 0xEu:
fwrite("Nullpointer given.\n", 1uLL, 0x13uLL, stderr);
break;
case 0x16u:
fwrite("Invalid argument given.\n", 1uLL, 0x18uLL, stderr);
break;
case 0x21u:
fwrite("Argument outside the defined domain.\n", 1uLL, 0x25uLL, stderr);
break;
default:
fwrite("Something went wrong.\n", 1uLL, 0x16uLL, stderr);
break;
}
}
return a3;
}

original_code:
int nip_report_error(char *srcFile, int line, int error, int verbose){
  NIP_ERROR_CODE = error;
  NIP_ERROR_COUNTER++;
  if(verbose){
    fprintf(stderr, "In %s (%d): ", srcFile, line);
    switch (error) {
    case 0 :
      fprintf(stderr, "O.K.\n"); break;
    case 14 :
      fprintf(stderr, "Nullpointer given.\n"); break;
    case 33 :
      fprintf(stderr, "Argument outside the defined domain.\n"); break;
    case 22 :
      fprintf(stderr, "Invalid argument given.\n"); break;
    case 12 :
      fprintf(stderr, "Failed to allocate memory.\n"); break;
    case 5 :
      fprintf(stderr, "I/O failure.\n"); break;
    case 2 :
      fprintf(stderr, "Requested file not found.\n"); break;
    default : fprintf(stderr, "Something went wrong.\n");
    }
  }
  return error;
}

variable_types:
srcFile
char *

variable_types: line int

variable_types: error int

variable_types: verbose int


decompiled_code:
void nip_reset_error_handler()
{
NIP_ERROR_CODE = 0;
NIP_ERROR_COUNTER = 0;
}

canonical_decompiled_code:
void func1()
{
NIP_ERROR_CODE = 0;
NIP_ERROR_COUNTER = 0;
}

original_code:
void nip_reset_error_handler(){
  NIP_ERROR_CODE = 0;
  NIP_ERROR_COUNTER = 0;
}


decompiled_code:
__int64 nip_check_error_type()
{
return (unsigned int)NIP_ERROR_CODE;
}

canonical_decompiled_code:
__int64 func2()
{
return (unsigned int)NIP_ERROR_CODE;
}

original_code:
int nip_check_error_type(){
  return NIP_ERROR_CODE;
}


decompiled_code:
__int64 nip_check_error_counter()
{
return (unsigned int)NIP_ERROR_COUNTER;
}

canonical_decompiled_code:
__int64 func3()
{
return (unsigned int)NIP_ERROR_COUNTER;
}

original_code:
int nip_check_error_counter(){
  return NIP_ERROR_COUNTER;
}


decompiled_code:
__int64 read_motorpos()
{
__int64 v0; // r12

write_cmd(64LL, 0LL, 0LL);
if ( (unsigned int)write_cmd(64LL, 0LL, 0LL) != 1 )
{
syslog(27, "Failed to read PIC AN0");
goto LABEL_5;
}
v0 = (unsigned int)read_data(2LL);
sprintf(message, "Motor position at %d", v0);
syslog(30, "%s", message);
if ( (unsigned int)v0 > 0x3FF )
LABEL_5:
LODWORD(v0) = -1;
return (unsigned int)v0;
}

canonical_decompiled_code:
__int64 func7()
{
__int64 v0; // r12

write_cmd(64LL, 0LL, 0LL);
if ( (unsigned int)write_cmd(64LL, 0LL, 0LL) != 1 )
{
syslog(27, "Failed to read PIC AN0");
goto LABEL_5;
}
v0 = (unsigned int)read_data(2LL);
sprintf(message, "Motor position at %d", v0);
syslog(30, "%s", message);
if ( (unsigned int)v0 > 0x3FF )
LABEL_5:
LODWORD(v0) = -1;
return (unsigned int)v0;
}

original_code:
int read_motorpos()
{
  int ok = -1;
  int pos = -1;
  ok = write_cmd( 0x40, 0, 0 );
  ok = write_cmd( 0x40, 0, 0 );
  if( ok == 1 )
  {
    pos = read_data( 2 );
    sprintf( message, "Motor position at %d", pos );
    syslog( 6 | (3<<3), "%s", message );
    if( pos < 0 || pos > 1023 ) pos = -1;
  }
  else syslog( 3 | (3<<3), "Failed to read PIC AN0" );
  return pos;
}

variable_types: ok int

variable_types: pos int