decompiled_code:
void __fastcall peerfile_import_static(__int64 **a1)
{
__int64 **v1; // rbx
int v2; // ebp

if ( a1 )
{
v1 = a1;
v2 = 0;
do
{
v2 += peerfile_import_peer((const char *)v1[1]);
v1 = (__int64 **)*v1;
}
while ( v1 );
if ( v2 > 0 && *(_DWORD *)(gconf + 60LL) != 2 )
log_print(6LL, "PEERFILE: Imported %d static peers.", (unsigned int)v2);
}
}

canonical_decompiled_code:
void __fastcall func2(__int64 **a1)
{
__int64 **v1; // rbx
int v2; // ebp

if ( a1 )
{
v1 = a1;
v2 = 0;
do
{
v2 += func0((const char *)v1[1]);
v1 = (__int64 **)*v1;
}
while ( v1 );
if ( v2 > 0 && *(_DWORD *)(gconf + 60LL) != 2 )
log_print(6LL, "PEERFILE: Imported %d static peers.", (unsigned int)v2);
}
}

original_code:
static void peerfile_import_static(const struct peer *peers)
{
 int num;
 num = 0;
 while (peers) {
  num += peerfile_import_peer(peers->addr_str);
  peers = peers->next;
 }
 if (num > 0) {
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(6, "PEERFILE: Imported %d static peers.", num); } while (0);
 }
}

canonical_original_code:
static void peerfile_import_static(struct peer *peers)
{
 int num;
 num = 0;
 while (peers) {
  num += peerfile_import_peer(peers->addr_str);
  peers = peers->next;
 }
 if (num > 0) {
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(6, "PEERFILE: Imported %d static peers.", num); } while (0);
 }
}

variable_types: struct peer { struct peer *next; char *addr_str; } *

variable_types: num int


repo: mwarning/KadNode

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

if ( *gconf >= peerfile_import_time && !(unsigned int)kad_count_nodes(0LL) )
{
peerfile_import();
peerfile_import_static((__int64 **)g_peers);
peerfile_import_time = time_add_mins(5LL);
}
result = gconf;
if ( *gconf >= peerfile_export_time )
{
result = kad_count_nodes(1LL);
if ( (_DWORD)result )
{
peerfile_export();
result = time_add_hours(24LL);
peerfile_export_time = result;
}
}
return result;
}

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

if ( *gconf >= peerfile_import_time && !(unsigned int)kad_count_nodes(0LL) )
{
func1();
func2((__int64 **)g_peers);
peerfile_import_time = time_add_mins(5LL);
}
result = gconf;
if ( *gconf >= peerfile_export_time )
{
result = kad_count_nodes(1LL);
if ( (_DWORD)result )
{
func3();
result = time_add_hours(24LL);
peerfile_export_time = result;
}
}
return result;
}

original_code:
static void peerfile_handle_peerfile(int _rc, int _sock)
{
 if (peerfile_import_time <= (gconf->time_now) && kad_count_nodes(0) == 0) {
  peerfile_import();
  peerfile_import_static(g_peers);
  peerfile_import_time = time_add_mins(5);
 }
 if (peerfile_export_time <= (gconf->time_now) && kad_count_nodes(1) != 0) {
  peerfile_export();
  peerfile_export_time = time_add_hours(24);
 }
}

return_type: void


decompiled_code:
__int64 __fastcall peerfile_add_peer(char *s)
{
_QWORD *v1; // rbx

v1 = malloc(0x10uLL);
v1[1] = strdup(s);
*v1 = g_peers;
g_peers = v1;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(char *s)
{
_QWORD *v1; // rbx

v1 = malloc(0x10uLL);
v1[1] = strdup(s);
*v1 = g_peers;
g_peers = v1;
return 0LL;
}

original_code:
int peerfile_add_peer(const char addr_str[])
{
 struct peer *new;
 new = (struct peer *) malloc(sizeof(struct peer));
 new->addr_str = strdup(addr_str);
 new->next = g_peers;
 g_peers = new;
 return 0;
}

canonical_original_code:
int peerfile_add_peer(char addr_str[])
{
 struct peer *new;
 new = (struct peer *) malloc(sizeof(struct peer));
 new->addr_str = strdup(addr_str);
 new->next = g_peers;
 g_peers = new;
 return 0;
}

return_type: int

user_defined_types: struct peer { struct peer *next; char *addr_str; }


decompiled_code:
__int64 peerfile_setup()
{
peerfile_import_time = time_add_secs(10LL);
peerfile_export_time = time_add_hours(24LL);
return net_add_handler(0xFFFFFFFFLL, peerfile_handle_peerfile);
}

canonical_decompiled_code:
__int64 func6()
{
peerfile_import_time = time_add_secs(10LL);
peerfile_export_time = time_add_hours(24LL);
return net_add_handler(0xFFFFFFFFLL, peerfile_handle_peerfile);
}

original_code:
void peerfile_setup(void)
{
 peerfile_import_time = time_add_secs(10);
 peerfile_export_time = time_add_hours(24);
 net_add_handler(-1 , &peerfile_handle_peerfile);
}

canonical_original_code:
void peerfile_setup()
{
 peerfile_import_time = time_add_secs(10);
 peerfile_export_time = time_add_hours(24);
 net_add_handler(-1 , &peerfile_handle_peerfile);
}


decompiled_code:
void peerfile_free()
{
void **v0; // rbx
void **v1; // rbp

v0 = (void **)g_peers;
if ( g_peers )
{
do
{
v1 = v0;
v0 = (void **)*v0;
free(v1[1]);
free(v1);
}
while ( v0 );
}
}

canonical_decompiled_code:
void func7()
{
void **v0; // rbx
void **v1; // rbp

v0 = (void **)g_peers;
if ( g_peers )
{
do
{
v1 = v0;
v0 = (void **)*v0;
free(v1[1]);
free(v1);
}
while ( v0 );
}
}

original_code:
void peerfile_free(void)
{
 struct peer *next;
 struct peer *p;
 p = g_peers;
 while (p) {
  next = p->next;
  free(p->addr_str);
  free(p);
  p = next;
 }
}

canonical_original_code:
void peerfile_free()
{
 struct peer *next;
 struct peer *p;
 p = g_peers;
 while (p) {
  next = p->next;
  free(p->addr_str);
  free(p);
  p = next;
 }
}


decompiled_code:
__int64 __fastcall mbedtls_ecp_decompress(__int64 a1, _BYTE *a2, size_t a3, _BYTE *a4, unsigned __int64 *a5, unsigned __int64 a6)
{
__int64 v9; // r15
__int64 v10; // rbp
unsigned __int64 v11; // rax
unsigned int v12; // ebx
int v14; // ebx
char v16[32]; // [rsp+10h] [rbp-98h] BYREF
char v17[32]; // [rsp+30h] [rbp-78h] BYREF
char v18[88]; // [rsp+50h] [rbp-58h] BYREF

v9 = a1 + 8;
v10 = mbedtls_mpi_size(a1 + 8);
v11 = 2 * v10 + 1;
*a5 = v11;
if ( v11 > a6 )
return (unsigned int)-20224;
if ( v10 + 1 != a3 )
return (unsigned int)-20352;
if ( (unsigned __int8)(*a2 - 2) > 1u )
return (unsigned int)-20352;
memcpy(a4, a2, a3);
*a4 = 4;
mbedtls_mpi_init(v18);
mbedtls_mpi_init(v17);
mbedtls_mpi_init(v16);
v12 = mbedtls_mpi_read_binary(v17, a2 + 1, v10);
if ( !v12 )
{
v12 = mbedtls_mpi_mul_mpi(v18, v17);
if ( !v12 )
{
if ( *(_QWORD *)(a1 + 48) )
{
v12 = mbedtls_mpi_add_mpi(v18, v18, a1 + 32);
if ( !v12 )
{
LABEL_10:
v12 = mbedtls_mpi_mul_mpi(v18, v18);
if ( !v12 )
{
v12 = mbedtls_mpi_add_mpi(v18, v18, a1 + 56);
if ( !v12 )
{
v12 = mbedtls_mpi_add_int(v16, v9, 1LL);
if ( !v12 )
{
v12 = mbedtls_mpi_shift_r(v16, 2LL);
if ( !v12 )
{
v12 = mbedtls_mpi_exp_mod(v18, v18, v16, v9, 0LL);
if ( !v12 )
{
v14 = *a2 == 3;
if ( v14 == (unsigned int)mbedtls_mpi_get_bit(v18, 0LL) || (v12 = mbedtls_mpi_sub_mpi(v18, v9)) == 0 )
v12 = mbedtls_mpi_write_binary(v18, &a4[a3], v10);
}
}
}
}
}
goto LABEL_5;
}
}
else
{
v12 = mbedtls_mpi_sub_int(v18, v18, 3LL);
if ( !v12 )
goto LABEL_10;
}
}
}
LABEL_5:
mbedtls_mpi_free(v18);
mbedtls_mpi_free(v17);
mbedtls_mpi_free(v16);
return v12;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, _BYTE *a2, size_t a3, _BYTE *a4, unsigned __int64 *a5, unsigned __int64 a6)
{
__int64 v9; // r15
__int64 v10; // rbp
unsigned __int64 v11; // rax
unsigned int v12; // ebx
int v14; // ebx
char v16[32]; // [rsp+10h] [rbp-98h] BYREF
char v17[32]; // [rsp+30h] [rbp-78h] BYREF
char v18[88]; // [rsp+50h] [rbp-58h] BYREF

v9 = a1 + 8;
v10 = mbedtls_mpi_size(a1 + 8);
v11 = 2 * v10 + 1;
*a5 = v11;
if ( v11 > a6 )
return (unsigned int)-20224;
if ( v10 + 1 != a3 )
return (unsigned int)-20352;
if ( (unsigned __int8)(*a2 - 2) > 1u )
return (unsigned int)-20352;
memcpy(a4, a2, a3);
*a4 = 4;
mbedtls_mpi_init(v18);
mbedtls_mpi_init(v17);
mbedtls_mpi_init(v16);
v12 = mbedtls_mpi_read_binary(v17, a2 + 1, v10);
if ( !v12 )
{
v12 = mbedtls_mpi_mul_mpi(v18, v17);
if ( !v12 )
{
if ( *(_QWORD *)(a1 + 48) )
{
v12 = mbedtls_mpi_add_mpi(v18, v18, a1 + 32);
if ( !v12 )
{
LABEL_10:
v12 = mbedtls_mpi_mul_mpi(v18, v18);
if ( !v12 )
{
v12 = mbedtls_mpi_add_mpi(v18, v18, a1 + 56);
if ( !v12 )
{
v12 = mbedtls_mpi_add_int(v16, v9, 1LL);
if ( !v12 )
{
v12 = mbedtls_mpi_shift_r(v16, 2LL);
if ( !v12 )
{
v12 = mbedtls_mpi_exp_mod(v18, v18, v16, v9, 0LL);
if ( !v12 )
{
v14 = *a2 == 3;
if ( v14 == (unsigned int)mbedtls_mpi_get_bit(v18, 0LL) || (v12 = mbedtls_mpi_sub_mpi(v18, v9)) == 0 )
v12 = mbedtls_mpi_write_binary(v18, &a4[a3], v10);
}
}
}
}
}
goto LABEL_5;
}
}
else
{
v12 = mbedtls_mpi_sub_int(v18, v18, 3LL);
if ( !v12 )
goto LABEL_10;
}
}
}
LABEL_5:
mbedtls_mpi_free(v18);
mbedtls_mpi_free(v17);
mbedtls_mpi_free(v16);
return v12;
}

original_code:
int mbedtls_ecp_decompress(
  const mbedtls_ecp_group *grp,
  const unsigned char *input, size_t ilen,
  unsigned char *output, size_t *olen, size_t osize
) {
 int ret;
 size_t plen;
 mbedtls_mpi r;
 mbedtls_mpi x;
 mbedtls_mpi n;
 plen = mbedtls_mpi_size(&grp->P);
 *olen = 2 * plen + 1;
 if (osize < *olen)
  return(-0x4F00);
 if (ilen != plen + 1)
  return(-0x4F80);
 if (input[0] != 0x02 && input[0] != 0x03)
  return(-0x4F80);
 memcpy(output, input, ilen);
 output[0] = 0x04;
 mbedtls_mpi_init(&r);
 mbedtls_mpi_init(&x);
 mbedtls_mpi_init(&n);
 do { if( ( ret = (mbedtls_mpi_read_binary(&x, input + 1, plen)) ) != 0 ) goto cleanup; } while( 0 );
 do { if( ( ret = (mbedtls_mpi_mul_mpi(&r, &x, &x)) ) != 0 ) goto cleanup; } while( 0 );
 if (grp->A.p == ((void *)0)) {
  do { if( ( ret = (mbedtls_mpi_sub_int(&r, &r, 3)) ) != 0 ) goto cleanup; } while( 0 );
 } else {
  do { if( ( ret = (mbedtls_mpi_add_mpi(&r, &r, &grp->A)) ) != 0 ) goto cleanup; } while( 0 );
 }
 do { if( ( ret = (mbedtls_mpi_mul_mpi(&r, &r, &x)) ) != 0 ) goto cleanup; } while( 0 );
 do { if( ( ret = (mbedtls_mpi_add_mpi(&r, &r, &grp->B)) ) != 0 ) goto cleanup; } while( 0 );
 do { if( ( ret = (mbedtls_mpi_add_int(&n, &grp->P, 1)) ) != 0 ) goto cleanup; } while( 0 );
 do { if( ( ret = (mbedtls_mpi_shift_r(&n, 2)) ) != 0 ) goto cleanup; } while( 0 );
 do { if( ( ret = (mbedtls_mpi_exp_mod(&r, &r, &n, &grp->P, ((void *)0))) ) != 0 ) goto cleanup; } while( 0 );
 if ((input[0] == 0x03) != mbedtls_mpi_get_bit(&r, 0)) {
  do { if( ( ret = (mbedtls_mpi_sub_mpi(&r, &grp->P, &r)) ) != 0 ) goto cleanup; } while( 0 );
 }
 ret = mbedtls_mpi_write_binary(&r, output + 1 + plen, plen);
 cleanup:
 mbedtls_mpi_free(&r);
 mbedtls_mpi_free(&x);
 mbedtls_mpi_free(&n);
 return(ret);
}

canonical_original_code:
int mbedtls_ecp_decompress(struct mbedtls_ecp_group *grp, unsigned char *input, unsigned long ilen, unsigned char *output, unsigned long *olen, unsigned long osize) {
 int ret;
 unsigned long plen;
 struct mbedtls_mpi r;
 struct mbedtls_mpi x;
 struct mbedtls_mpi n;
 plen = mbedtls_mpi_size(&grp->P);
 *olen = 2 * plen + 1;
 if (osize < *olen)
  return(-0x4F00);
 if (ilen != plen + 1)
  return(-0x4F80);
 if (input[0] != 0x02 && input[0] != 0x03)
  return(-0x4F80);
 memcpy(output, input, ilen);
 output[0] = 0x04;
 mbedtls_mpi_init(&r);
 mbedtls_mpi_init(&x);
 mbedtls_mpi_init(&n);
 do { if( ( ret = (mbedtls_mpi_read_binary(&x, input + 1, plen)) ) != 0 ) goto cleanup; } while( 0 );
 do { if( ( ret = (mbedtls_mpi_mul_mpi(&r, &x, &x)) ) != 0 ) goto cleanup; } while( 0 );
 if (grp->A.p == ((void *)0)) {
  do { if( ( ret = (mbedtls_mpi_sub_int(&r, &r, 3)) ) != 0 ) goto cleanup; } while( 0 );
 } else {
  do { if( ( ret = (mbedtls_mpi_add_mpi(&r, &r, &grp->A)) ) != 0 ) goto cleanup; } while( 0 );
 }
 do { if( ( ret = (mbedtls_mpi_mul_mpi(&r, &r, &x)) ) != 0 ) goto cleanup; } while( 0 );
 do { if( ( ret = (mbedtls_mpi_add_mpi(&r, &r, &grp->B)) ) != 0 ) goto cleanup; } while( 0 );
 do { if( ( ret = (mbedtls_mpi_add_int(&n, &grp->P, 1)) ) != 0 ) goto cleanup; } while( 0 );
 do { if( ( ret = (mbedtls_mpi_shift_r(&n, 2)) ) != 0 ) goto cleanup; } while( 0 );
 do { if( ( ret = (mbedtls_mpi_exp_mod(&r, &r, &n, &grp->P, ((void *)0))) ) != 0 ) goto cleanup; } while( 0 );
 if ((input[0] == 0x03) != mbedtls_mpi_get_bit(&r, 0)) {
  do { if( ( ret = (mbedtls_mpi_sub_mpi(&r, &grp->P, &r)) ) != 0 ) goto cleanup; } while( 0 );
 }
 ret = mbedtls_mpi_write_binary(&r, output + 1 + plen, plen);
 cleanup:
 mbedtls_mpi_free(&r);
 mbedtls_mpi_free(&x);
 mbedtls_mpi_free(&n);
 return(ret);
}

variable_types: struct mbedtls_ecp_group { enum mbedtls_ecp_group_id { MBEDTLS_ECP_DP_NONE=0, MBEDTLS_ECP_DP_SECP192R1=1, MBEDTLS_ECP_DP_SECP224R1=2, MBEDTLS_ECP_DP_SECP256R1=3, MBEDTLS_ECP_DP_SECP384R1=4, MBEDTLS_ECP_DP_SECP521R1=5, MBEDTLS_ECP_DP_BP256R1=6, MBEDTLS_ECP_DP_BP384R1=7, MBEDTLS_ECP_DP_BP512R1=8, MBEDTLS_ECP_DP_CURVE25519=9, MBEDTLS_ECP_DP_SECP192K1=10, MBEDTLS_ECP_DP_SECP224K1=11, MBEDTLS_ECP_DP_SECP256K1=12, MBEDTLS_ECP_DP_CURVE448=13 } id; struct mbedtls_mpi { int s; unsigned long n; unsigned long *p; } P; struct mbedtls_mpi A; struct mbedtls_mpi B; struct mbedtls_ecp_point { struct mbedtls_mpi X; struct mbedtls_mpi Y; struct mbedtls_mpi Z; } G; struct mbedtls_mpi N; unsigned long pbits; unsigned long nbits; unsigned int h; int (*modp)(struct mbedtls_mpi *); int (*t_pre)(struct mbedtls_ecp_point *, void *); int (*t_post)(struct mbedtls_ecp_point *, void *); void *t_data; struct mbedtls_ecp_point *T; unsigned long T_size; } *

variable_types:
input
unsigned char *

variable_types:
ilen
unsigned long

variable_types:
output
unsigned char *

variable_types:
olen
unsigned long *

variable_types:
osize
unsigned long

variable_types: ret int

variable_types:
plen
unsigned long

user_defined_types: struct mbedtls_ecp_group { enum mbedtls_ecp_group_id id; struct mbedtls_mpi P; struct mbedtls_mpi A; struct mbedtls_mpi B; struct mbedtls_ecp_point G; struct mbedtls_mpi N; unsigned long pbits; unsigned long nbits; unsigned int h; int (*modp)(struct mbedtls_mpi *); int (*t_pre)(struct mbedtls_ecp_point *, void *); int (*t_post)(struct mbedtls_ecp_point *, void *); void *t_data; struct mbedtls_ecp_point *T; unsigned long T_size; }


original_code:
int mbedtls_ecp_compress(
  const mbedtls_ecp_group *grp,
  const unsigned char *input, size_t ilen,
  unsigned char *output, size_t *olen, size_t osize
) {
 size_t plen;
 plen = mbedtls_mpi_size(&grp->P);
 *olen = plen + 1;
 if (osize < *olen)
  return(-0x4F00);
 if (ilen != 2 * plen + 1)
  return (-0x4F80);
 if (input[0] != 0x04)
  return(-0x4F80);
 memcpy(output, input, *olen);
 output[0] = 0x02 + (input[2 * plen] & 1);
 return(0);
}

canonical_original_code:
int mbedtls_ecp_compress(struct mbedtls_ecp_group *grp, unsigned char *input, unsigned long ilen, unsigned char *output, unsigned long *olen, unsigned long osize) {
 unsigned long plen;
 plen = mbedtls_mpi_size(&grp->P);
 *olen = plen + 1;
 if (osize < *olen)
  return(-0x4F00);
 if (ilen != 2 * plen + 1)
  return (-0x4F80);
 if (input[0] != 0x04)
  return(-0x4F80);
 memcpy(output, input, *olen);
 output[0] = 0x02 + (input[2 * plen] & 1);
 return(0);
}

user_defined_types: enum mbedtls_ecp_group_id { MBEDTLS_ECP_DP_NONE=0, MBEDTLS_ECP_DP_SECP192R1=1, MBEDTLS_ECP_DP_SECP224R1=2, MBEDTLS_ECP_DP_SECP256R1=3, MBEDTLS_ECP_DP_SECP384R1=4, MBEDTLS_ECP_DP_SECP521R1=5, MBEDTLS_ECP_DP_BP256R1=6, MBEDTLS_ECP_DP_BP384R1=7, MBEDTLS_ECP_DP_BP512R1=8, MBEDTLS_ECP_DP_CURVE25519=9, MBEDTLS_ECP_DP_SECP192K1=10, MBEDTLS_ECP_DP_SECP224K1=11, MBEDTLS_ECP_DP_SECP256K1=12, MBEDTLS_ECP_DP_CURVE448=13 }

user_defined_types: struct mbedtls_mpi { int s; unsigned long n; unsigned long *p; }

user_defined_types: struct mbedtls_ecp_point { struct mbedtls_mpi X; struct mbedtls_mpi Y; struct mbedtls_mpi Z; }


sign

repo: mwarning/libsodium-example

decompiled_code:
__int64 sign()
{
__int64 result; // rax
unsigned int v1; // [rsp+8h] [rbp-10h]

if ( (unsigned int)crypto_sign() )
result = 0xFFFFFFFFLL;
else
result = v1;
return result;
}

canonical_decompiled_code:
__int64 func7()
{
__int64 result; // rax
unsigned int v1; // [rsp+8h] [rbp-10h]

if ( (unsigned int)crypto_sign() )
result = 0xFFFFFFFFLL;
else
result = v1;
return result;
}

original_code:
int sign(uint8_t sm[], const uint8_t m[], const int mlen, const uint8_t sk[]) {
 unsigned long long smlen;
 if( crypto_sign(sm,&smlen, m, mlen, sk) == 0) {
  return smlen;
 } else {
  return -1;
 }
}

canonical_original_code:
int sign(unsigned char sm[], unsigned char m[], int mlen, unsigned char sk[]) {
 unsigned long long smlen;
 if( crypto_sign(sm,&smlen, m, mlen, sk) == 0) {
  return smlen;
 } else {
  return -1;
 }
}

variable_types:
sm
unsigned char []

variable_types:
m
unsigned char []

variable_types: mlen int

variable_types:
sk
unsigned char []

variable_types:
smlen
unsigned long long


verify

decompiled_code:
__int64 verify()
{
__int64 result; // rax
unsigned int v1; // [rsp+8h] [rbp-10h]

if ( (unsigned int)crypto_sign_open() )
result = 0xFFFFFFFFLL;
else
result = v1;
return result;
}

canonical_decompiled_code:
__int64 func8()
{
__int64 result; // rax
unsigned int v1; // [rsp+8h] [rbp-10h]

if ( (unsigned int)crypto_sign_open() )
result = 0xFFFFFFFFLL;
else
result = v1;
return result;
}

original_code:
int verify(uint8_t m[], const uint8_t sm[], const int smlen, const uint8_t pk[]) {
 unsigned long long mlen;
 if( crypto_sign_open(m, &mlen, sm, smlen, pk) == 0) {
  return mlen;
 } else {
  return -1;
 }
}

canonical_original_code:
int verify(unsigned char m[], unsigned char sm[], int smlen, unsigned char pk[]) {
 unsigned long long mlen;
 if( crypto_sign_open(m, &mlen, sm, smlen, pk) == 0) {
  return mlen;
 } else {
  return -1;
 }
}

variable_types: smlen int

variable_types:
pk
unsigned char []

variable_types:
mlen
unsigned long long


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // edx
int result; // eax
__int64 v5; // rdx
int v6; // edx
char v7[16]; // [rsp+0h] [rbp-168h] BYREF
__int64 v8; // [rsp+10h] [rbp-158h]
__int64 v9; // [rsp+18h] [rbp-150h]
__int64 v10; // [rsp+20h] [rbp-148h]
__int64 v11; // [rsp+28h] [rbp-140h]
__int64 v12; // [rsp+30h] [rbp-138h]
__int64 v13; // [rsp+38h] [rbp-130h]
char v14[128]; // [rsp+80h] [rbp-E8h] BYREF
char v15[32]; // [rsp+100h] [rbp-68h] BYREF
char v16[72]; // [rsp+120h] [rbp-48h] BYREF

v8 = 0LL;
v9 = 0LL;
v10 = 0LL;
v11 = 0LL;
v12 = 0LL;
v13 = 0LL;
strcpy(v7, "Hello World!");
v3 = crypto_sign_keypair();
result = 1;
if ( v3 >= 0 )
{
v5 = (unsigned int)sign(v14, v7, 12LL, v16);
result = 1;
if ( (int)v5 >= 0 )
{
v6 = verify(v7, v14, v5, v15);
result = 1;
if ( v6 >= 0 )
{
puts("Verified!");
result = 0;
}
}
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // edx
int result; // eax
__int64 v5; // rdx
int v6; // edx
char v7[16]; // [rsp+0h] [rbp-168h] BYREF
__int64 v8; // [rsp+10h] [rbp-158h]
__int64 v9; // [rsp+18h] [rbp-150h]
__int64 v10; // [rsp+20h] [rbp-148h]
__int64 v11; // [rsp+28h] [rbp-140h]
__int64 v12; // [rsp+30h] [rbp-138h]
__int64 v13; // [rsp+38h] [rbp-130h]
char v14[128]; // [rsp+80h] [rbp-E8h] BYREF
char v15[32]; // [rsp+100h] [rbp-68h] BYREF
char v16[72]; // [rsp+120h] [rbp-48h] BYREF

v8 = 0LL;
v9 = 0LL;
v10 = 0LL;
v11 = 0LL;
v12 = 0LL;
v13 = 0LL;
strcpy(v7, "Hello World!");
v3 = crypto_sign_keypair();
result = 1;
if ( v3 >= 0 )
{
v5 = (unsigned int)func7(v14, v7, 12LL, v16);
result = 1;
if ( (int)v5 >= 0 )
{
v6 = func8(v7, v14, v5, v15);
result = 1;
if ( v6 >= 0 )
{
puts("Verified!");
result = 0;
}
}
}
return result;
}

original_code:
int main() {
 uint8_t sk[(32U + 32U)];
 uint8_t pk[32U];
 uint8_t sm[64 +64U];
 uint8_t m[64 +64U];
 memset(m, '\0', 64);
 int mlen = snprintf(m, 64, "%s", "Hello World!");
 int rc = crypto_sign_keypair(pk, sk);
 if(rc < 0) {
  return 1;
 }
 int smlen = sign(sm, m, mlen, sk);
 if(smlen < 0) {
  return 1;
 }
 mlen = verify(m, sm, smlen, pk);
 if(mlen < 0) {
  return 1;
 }
 printf("Verified!\n");
 return 0;
}

canonical_original_code:
int main() {
 unsigned char sk[-1];
 unsigned char pk[32];
 unsigned char sm[-1];
 unsigned char m[-1];
 memset(m, '\0', 64);
 int mlen = snprintf(m, 64, "%s", "Hello World!");
 int rc = crypto_sign_keypair(pk, sk);
 if(rc < 0) {
  return 1;
 }
 int smlen = sign(sm, m, mlen, sk);
 if(smlen < 0) {
  return 1;
 }
 mlen = verify(m, sm, smlen, pk);
 if(mlen < 0) {
  return 1;
 }
 printf("Verified!\n");
 return 0;
}

variable_types:
sk
unsigned char [-1]

variable_types:
pk
unsigned char [32]

variable_types:
sm
unsigned char [-1]

variable_types:
m
unsigned char [-1]

variable_types: rc int


decompiled_code:
char *__fastcall to_hex(char *s, unsigned __int8 *a2, __int64 a3)
{
unsigned __int8 *v3; // rbx
unsigned __int8 *v4; // r12
char *v5; // rbp

if ( a3 )
{
v3 = a2;
v4 = &a2[a3];
v5 = s;
do
{
snprintf(v5, 3uLL, "%02x", *v3++);
v5 += 2;
}
while ( v4 != v3 );
}
return s;
}

canonical_decompiled_code:
char *__fastcall func7(char *s, unsigned __int8 *a2, __int64 a3)
{
unsigned __int8 *v3; // rbx
unsigned __int8 *v4; // r12
char *v5; // rbp

if ( a3 )
{
v3 = a2;
v4 = &a2[a3];
v5 = s;
do
{
snprintf(v5, 3uLL, "%02x", *v3++);
v5 += 2;
}
while ( v4 != v3 );
}
return s;
}

original_code:
char* to_hex( char hex[], const uint8_t bin[], size_t length )
{
 int i;
 uint8_t *p0 = (uint8_t *)bin;
 char *p1 = hex;
 for( i = 0; i < length; i++ ) {
  snprintf( p1, 3, "%02x", *p0 );
  p0 += 1;
  p1 += 2;
 }
 return hex;
}

canonical_original_code:
char *to_hex(char hex[], unsigned char bin[], unsigned long length)
{
 int i;
 unsigned char *p0 = (uint8_t *)bin;
 char *p1 = hex;
 for( i = 0; i < length; i++ ) {
  snprintf( p1, 3, "%02x", *p0 );
  p0 += 1;
  p1 += 2;
 }
 return hex;
}

variable_types:
hex
char []

variable_types:
bin
unsigned char []

variable_types:
length
unsigned long

variable_types: i int

return_type: char *


decompiled_code:
int crypto_box_recover_public_key()
{
char *v0; // rax
char v2[80]; // [rsp+0h] [rbp-78h] BYREF
unsigned __int8 v3[40]; // [rsp+50h] [rbp-28h] BYREF

crypto_scalarmult_curve25519_base();
v0 = to_hex(v2, v3, 32LL);
return printf("recovered public_key: %s\n", v0);
}

canonical_decompiled_code:
int func8()
{
char *v0; // rax
char v2[80]; // [rsp+0h] [rbp-78h] BYREF
unsigned __int8 v3[40]; // [rsp+50h] [rbp-28h] BYREF

crypto_scalarmult_curve25519_base();
v0 = func7(v2, v3, 32LL);
return printf("recovered public_key: %s\n", v0);
}

original_code:
int crypto_box_recover_public_key(uint8_t secret_key[]) {
 uint8_t public_key[32U];
 char phexbuf[2*32U +1];
 crypto_scalarmult_curve25519_base( public_key, secret_key );
 printf("recovered public_key: %s\n", to_hex(phexbuf, public_key, 32U));
}

canonical_original_code:
int crypto_box_recover_public_key(unsigned char secret_key[]) {
 unsigned char public_key[32];
 char phexbuf[-1];
 crypto_scalarmult_curve25519_base( public_key, secret_key );
 printf("recovered public_key: %s\n", to_hex(phexbuf, public_key, 32U));
}

variable_types:
phexbuf
char [-1]


decompiled_code:
int crypto_box_example()
{
char *v0; // rax
char *v1; // rax
char v3[80]; // [rsp+0h] [rbp-E8h] BYREF
char s[80]; // [rsp+50h] [rbp-98h] BYREF
unsigned __int8 v5[32]; // [rsp+A0h] [rbp-48h] BYREF
unsigned __int8 v6[40]; // [rsp+C0h] [rbp-28h] BYREF

crypto_box_keypair();
v0 = to_hex(s, v6, 32LL);
printf("public_key: %s\n", v0);
v1 = to_hex(v3, v5, 32LL);
printf("secret_key: %s\n", v1);
return crypto_box_recover_public_key();
}

canonical_decompiled_code:
int func9()
{
char *v0; // rax
char *v1; // rax
char v3[80]; // [rsp+0h] [rbp-E8h] BYREF
char s[80]; // [rsp+50h] [rbp-98h] BYREF
unsigned __int8 v5[32]; // [rsp+A0h] [rbp-48h] BYREF
unsigned __int8 v6[40]; // [rsp+C0h] [rbp-28h] BYREF

crypto_box_keypair();
v0 = func7(s, v6, 32LL);
printf("public_key: %s\n", v0);
v1 = func7(v3, v5, 32LL);
printf("secret_key: %s\n", v1);
return func8();
}

original_code:
void crypto_box_example()
{
 uint8_t public_key[32U];
 uint8_t secret_key[32U];
 char phexbuf[2*32U +1];
 char shexbuf[2*32U +1];
 crypto_box_keypair(public_key, secret_key);
 printf("public_key: %s\n", to_hex(phexbuf, public_key, 32U));
 printf("secret_key: %s\n", to_hex(shexbuf, secret_key, 32U));
 crypto_box_recover_public_key(secret_key);
}

canonical_original_code:
void crypto_box_example()
{
 unsigned char public_key[32];
 unsigned char secret_key[32];
 char phexbuf[-1];
 char shexbuf[-1];
 crypto_box_keypair(public_key, secret_key);
 printf("public_key: %s\n", to_hex(phexbuf, public_key, 32U));
 printf("secret_key: %s\n", to_hex(shexbuf, secret_key, 32U));
 crypto_box_recover_public_key(secret_key);
}

variable_types:
shexbuf
char [-1]


decompiled_code:
int __fastcall crypto_sign_recover_public_key(const __m128i *a1)
{
char *v1; // rax
char v3[80]; // [rsp+0h] [rbp-78h] BYREF
__int128 v4[2]; // [rsp+50h] [rbp-28h] BYREF

v4[0] = (__int128)_mm_loadu_si128(a1 + 2);
v4[1] = (__int128)_mm_loadu_si128(a1 + 3);
v1 = to_hex(v3, (unsigned __int8 *)v4, 32LL);
return printf("recovered public_key: %s\n", v1);
}

canonical_decompiled_code:
int __fastcall func10(const __m128i *a1)
{
char *v1; // rax
char v3[80]; // [rsp+0h] [rbp-78h] BYREF
__int128 v4[2]; // [rsp+50h] [rbp-28h] BYREF

v4[0] = (__int128)_mm_loadu_si128(a1 + 2);
v4[1] = (__int128)_mm_loadu_si128(a1 + 3);
v1 = func7(v3, (unsigned __int8 *)v4, 32LL);
return printf("recovered public_key: %s\n", v1);
}

original_code:
int crypto_sign_recover_public_key(uint8_t secret_key[]) {
 uint8_t public_key[32U];
 char phexbuf[2*32U +1];
 memcpy(public_key, secret_key+32U, 32U);
 printf("recovered public_key: %s\n", to_hex(phexbuf, public_key, 32U));
}

canonical_original_code:
int crypto_sign_recover_public_key(unsigned char secret_key[]) {
 unsigned char public_key[32];
 char phexbuf[-1];
 memcpy(public_key, secret_key+32U, 32U);
 printf("recovered public_key: %s\n", to_hex(phexbuf, public_key, 32U));
}


decompiled_code:
int crypto_sign_example()
{
char *v0; // rax
char *v1; // rax
char v3[144]; // [rsp+0h] [rbp-148h] BYREF
char s[80]; // [rsp+90h] [rbp-B8h] BYREF
__m128i v5[4]; // [rsp+E0h] [rbp-68h] BYREF
unsigned __int8 v6[40]; // [rsp+120h] [rbp-28h] BYREF

crypto_sign_keypair();
v0 = to_hex(s, v6, 32LL);
printf("public_key: %s\n", v0);
v1 = to_hex(v3, (unsigned __int8 *)v5, 64LL);
printf("secret_key: %s\n", v1);
return crypto_sign_recover_public_key(v5);
}

canonical_decompiled_code:
int func11()
{
char *v0; // rax
char *v1; // rax
char v3[144]; // [rsp+0h] [rbp-148h] BYREF
char s[80]; // [rsp+90h] [rbp-B8h] BYREF
__m128i v5[4]; // [rsp+E0h] [rbp-68h] BYREF
unsigned __int8 v6[40]; // [rsp+120h] [rbp-28h] BYREF

crypto_sign_keypair();
v0 = func7(s, v6, 32LL);
printf("public_key: %s\n", v0);
v1 = func7(v3, (unsigned __int8 *)v5, 64LL);
printf("secret_key: %s\n", v1);
return func10(v5);
}

original_code:
void crypto_sign_example()
{
 uint8_t public_key[32U];
 uint8_t secret_key[(32U + 32U)];
 char phexbuf[2*32U +1];
 char shexbuf[2*(32U + 32U)+1];
 crypto_sign_keypair(public_key, secret_key);
 printf("public_key: %s\n", to_hex(phexbuf, public_key, 32U));
 printf("secret_key: %s\n", to_hex(shexbuf, secret_key, (32U + 32U)));
 crypto_sign_recover_public_key(secret_key);
}

canonical_original_code:
void crypto_sign_example()
{
 unsigned char public_key[32];
 unsigned char secret_key[-1];
 char phexbuf[-1];
 char shexbuf[-1];
 crypto_sign_keypair(public_key, secret_key);
 printf("public_key: %s\n", to_hex(phexbuf, public_key, 32U));
 printf("secret_key: %s\n", to_hex(shexbuf, secret_key, (32U + 32U)));
 crypto_sign_recover_public_key(secret_key);
}


randombytes

decompiled_code:
int __fastcall randombytes(void *buf, size_t nbytes)
{
int v2; // eax
int v3; // ebx
int result; // eax

v2 = open("/dev/urandom", 0);
if ( v2 < 0 )
{
fwrite("Failed to open /dev/urandom\n", 1uLL, 0x1CuLL, stderr);
exit(1);
}
v3 = v2;
result = read(v2, buf, nbytes);
if ( result >= 0 )
result = close(v3);
return result;
}

canonical_decompiled_code:
int __fastcall func7(void *buf, size_t nbytes)
{
int v2; // eax
int v3; // ebx
int result; // eax

v2 = open("/dev/urandom", 0);
if ( v2 < 0 )
{
fwrite("Failed to open /dev/urandom\n", 1uLL, 0x1CuLL, stderr);
exit(1);
}
v3 = v2;
result = read(v2, buf, nbytes);
if ( result >= 0 )
result = close(v3);
return result;
}

original_code:
void randombytes(uint8_t buffer[], unsigned long long size)
{
 int fd;
 fd = open( "/dev/urandom", 00 );
 if( fd < 0 ) {
  fprintf( stderr, "Failed to open /dev/urandom\n" );
  exit(1);
 }
 int rc;
 if( (rc = read( fd, buffer, size )) >= 0 ) {
  close( fd );
 }
}

canonical_original_code:
void randombytes(unsigned char buffer[], unsigned long long size)
{
 int fd;
 fd = open( "/dev/urandom", 00 );
 if( fd < 0 ) {
  fprintf( stderr, "Failed to open /dev/urandom\n" );
  exit(1);
 }
 int rc;
 if( (rc = read( fd, buffer, size )) >= 0 ) {
  close( fd );
 }
}

variable_types:
buffer
unsigned char []

variable_types:
size
unsigned long long

variable_types: fd int


canonical_decompiled_code:
char *__fastcall func8(char *s, unsigned __int8 *a2, __int64 a3)
{
unsigned __int8 *v3; // rbx
unsigned __int8 *v4; // r12
char *v5; // rbp

if ( a3 )
{
v3 = a2;
v4 = &a2[a3];
v5 = s;
do
{
snprintf(v5, 3uLL, "%02x", *v3++);
v5 += 2;
}
while ( v4 != v3 );
}
return s;
}


decompiled_code:
__int64 __fastcall is_zero(unsigned __int8 *a1, int a2)
{
unsigned __int8 *v2; // rax
__int64 v3; // rsi
unsigned int v4; // edx

if ( a2 <= 0 )
return 0;
v2 = a1;
v3 = (__int64)&a1[a2 - 1 + 1];
v4 = 0;
do
v4 |= *v2++;
while ( v2 != (unsigned __int8 *)v3 );
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned __int8 *a1, int a2)
{
unsigned __int8 *v2; // rax
__int64 v3; // rsi
unsigned int v4; // edx

if ( a2 <= 0 )
return 0;
v2 = a1;
v3 = (__int64)&a1[a2 - 1 + 1];
v4 = 0;
do
v4 |= *v2++;
while ( v2 != (unsigned __int8 *)v3 );
return v4;
}

original_code:
int is_zero( const uint8_t *data, int len )
{
 int i;
 int rc;
 rc = 0;
 for(i = 0; i < len; ++i) {
  rc |= data[i];
 }
 return rc;
}

canonical_original_code:
int is_zero(unsigned char *data, int len)
{
 int i;
 int rc;
 rc = 0;
 for(i = 0; i < len; ++i) {
  rc |= data[i];
 }
 return rc;
}

variable_types:
data
unsigned char *

variable_types: len int


encrypt

original_code:
int encrypt(uint8_t encrypted[], const uint8_t pk[], const uint8_t sk[], const uint8_t nonce[], const uint8_t plain[], int length)
{
 uint8_t temp_plain[1400];
 uint8_t temp_encrypted[1400];
 int rc;
 printf("encrypt\n", length);
 if(length+(16U + 16U) >= 1400) {
  return -2;
 }
 memset(temp_plain, '\0', (16U + 16U));
 memcpy(temp_plain + (16U + 16U), plain, length);
 rc = crypto_box(temp_encrypted, temp_plain, (16U + 16U) + length, nonce, pk, sk);
 if( rc != 0 ) {
  return -1;
 }
 if( is_zero(temp_plain, 16U) != 0 ) {
  return -3;
 }
 memcpy(encrypted, temp_encrypted + 16U, (16U + 16U) + length);
 return (16U + 16U) + length - 16U;
}

canonical_original_code:
int encrypt(unsigned char encrypted[], unsigned char pk[], unsigned char sk[], unsigned char nonce[], unsigned char plain[], int length)
{
 unsigned char temp_plain[1400];
 unsigned char temp_encrypted[1400];
 int rc;
 printf("encrypt\n", length);
 if(length+(16U + 16U) >= 1400) {
  return -2;
 }
 memset(temp_plain, '\0', (16U + 16U));
 memcpy(temp_plain + (16U + 16U), plain, length);
 rc = crypto_box(temp_encrypted, temp_plain, (16U + 16U) + length, nonce, pk, sk);
 if( rc != 0 ) {
  return -1;
 }
 if( is_zero(temp_plain, 16U) != 0 ) {
  return -3;
 }
 memcpy(encrypted, temp_encrypted + 16U, (16U + 16U) + length);
 return (16U + 16U) + length - 16U;
}

variable_types:
encrypted
unsigned char []

variable_types:
pk
unsigned char []

variable_types:
sk
unsigned char []

variable_types:
nonce
unsigned char []

variable_types:
plain
unsigned char []

variable_types: length int


decrypt

original_code:
int decrypt(uint8_t plain[], const uint8_t pk[], const uint8_t sk[], const uint8_t nonce[], const uint8_t encrypted[], int length)
{
 uint8_t temp_encrypted[1400];
 uint8_t temp_plain[1400];
 int rc;
 printf("decrypt\n");
 if(length+16U >= 1400) {
  return -2;
 }
 memset(temp_encrypted, '\0', 16U);
 memcpy(temp_encrypted + 16U, encrypted, length);
 rc = crypto_box_open(temp_plain, temp_encrypted, 16U + length, nonce, pk, sk);
 if( rc != 0 ) {
  return -1;
 }
 if( is_zero(temp_plain, (16U + 16U)) != 0 ) {
  return -3;
 }
 memcpy(plain, temp_plain + (16U + 16U), 16U + length);
 return 16U + length - (16U + 16U);
}

canonical_original_code:
int decrypt(unsigned char plain[], unsigned char pk[], unsigned char sk[], unsigned char nonce[], unsigned char encrypted[], int length)
{
 unsigned char temp_encrypted[1400];
 unsigned char temp_plain[1400];
 int rc;
 printf("decrypt\n");
 if(length+16U >= 1400) {
  return -2;
 }
 memset(temp_encrypted, '\0', 16U);
 memcpy(temp_encrypted + 16U, encrypted, length);
 rc = crypto_box_open(temp_plain, temp_encrypted, 16U + length, nonce, pk, sk);
 if( rc != 0 ) {
  return -1;
 }
 if( is_zero(temp_plain, (16U + 16U)) != 0 ) {
  return -3;
 }
 memcpy(plain, temp_plain + (16U + 16U), 16U + length);
 return 16U + length - (16U + 16U);
}

variable_types: rc int


decompiled_code:
int __fastcall print_user(__int64 a1)
{
char *v1; // rax
char *v2; // rax
char v4[80]; // [rsp+0h] [rbp-A8h] BYREF
char s[88]; // [rsp+50h] [rbp-58h] BYREF

printf("username: %s\n", *(const char **)a1);
v1 = to_hex(s, (unsigned __int8 *)(a1 + 8), 32LL);
printf("public key: %s\n", v1);
v2 = to_hex(v4, (unsigned __int8 *)(a1 + 40), 32LL);
return printf("secret key: %s\n\n", v2);
}

canonical_decompiled_code:
int __fastcall func12(__int64 a1)
{
char *v1; // rax
char *v2; // rax
char v4[80]; // [rsp+0h] [rbp-A8h] BYREF
char s[88]; // [rsp+50h] [rbp-58h] BYREF

printf("username: %s\n", *(const char **)a1);
v1 = func8(s, (unsigned __int8 *)(a1 + 8), 32LL);
printf("public key: %s\n", v1);
v2 = func8(v4, (unsigned __int8 *)(a1 + 40), 32LL);
return printf("secret key: %s\n\n", v2);
}

original_code:
void print_user(User *user)
{
 char phexbuf[2*32U +1];
 char shexbuf[2*32U +1];
 printf("username: %s\n", user->name);
 printf("public key: %s\n", to_hex(phexbuf, user->public_key, 32U ));
 printf("secret key: %s\n\n", to_hex(shexbuf, user->secret_key, 32U ));
}

canonical_original_code:
void print_user(struct User *user)
{
 char phexbuf[-1];
 char shexbuf[-1];
 printf("username: %s\n", user->name);
 printf("public key: %s\n", to_hex(phexbuf, user->public_key, 32U ));
 printf("secret key: %s\n\n", to_hex(shexbuf, user->secret_key, 32U ));
}

variable_types: struct User { char *name; unsigned char public_key[32]; unsigned char secret_key[32]; } *


decompiled_code:
__int64 __fastcall tagfs_destroy(_QWORD *a1)
{
log_msg1(0LL, "tagfs.c", 278LL, "tagfs_destroy user_data=%p", a1);
return destroy_tagfs_data(a1);
}

canonical_decompiled_code:
__int64 __fastcall func17(_QWORD *a1)
{
log_msg1(0LL, "tagfs.c", 278LL, "tagfs_destroy user_data=%p", a1);
return func16(a1);
}

original_code:
void tagfs_destroy (void *user_data)
{
    debug("tagfs_destroy user_data=%p", user_data);
    destroy_tagfs_data(user_data);
}

canonical_original_code:
void tagfs_destroy(void *user_data)
{
    debug("tagfs_destroy user_data=%p", user_data);
    destroy_tagfs_data(user_data);
}


absolutize

decompiled_code:
__int64 __fastcall absolutize(__int64 a1, _QWORD *a2)
{
__int64 v3; // r12

if ( !(unsigned int)g_path_is_absolute(*a2) )
{
v3 = *a2;
*a2 = g_build_filename(a1, *a2, 0LL);
g_free(v3);
}
return *a2;
}

canonical_decompiled_code:
__int64 __fastcall func19(__int64 a1, _QWORD *a2)
{
__int64 v3; // r12

if ( !(unsigned int)g_path_is_absolute(*a2) )
{
v3 = *a2;
*a2 = g_build_filename(a1, *a2, 0LL);
g_free(v3);
}
return *a2;
}

original_code:
char *absolutize(const char *cwd, char **path)
{
    if (!g_path_is_absolute(*path))
    {
        char *tmp = *path;
        *path = g_build_filename(cwd, *path, NULL);
        g_free(tmp);
    }
    return *path;
}

canonical_original_code:
char *absolutize(char *cwd, char **path)
{
    if (!g_path_is_absolute(*path))
    {
        char *tmp = *path;
        *path = g_build_filename(cwd, *path, NULL);
        g_free(tmp);
    }
    return *path;
}

variable_types:
cwd
char *

variable_types:
path
char **

variable_types:
tmp
char *


spawn

decompiled_code:
__pid_t __fastcall spawn(char *file, char **argv)
{
__pid_t result; // eax

result = fork();
if ( !result )
{
execvp(file, argv);
fwrite("an error occurred in execvp\n", 1uLL, 0x1CuLL, stderr);
abort();
}
return result;
}

canonical_decompiled_code:
__pid_t __fastcall func7(char *file, char **argv)
{
__pid_t result; // eax

result = fork();
if ( !result )
{
execvp(file, argv);
fwrite("an error occurred in execvp\n", 1uLL, 0x1CuLL, stderr);
abort();
}
return result;
}

original_code:
int spawn (char* program, char** arg_list)
{
  pid_t child_pid;
  child_pid = fork ();
  if (child_pid != 0)
    return child_pid;
  else {
    execvp (program, arg_list);
    fprintf (stderr, "an error occurred in execvp\n");
    abort ();
  }
}

canonical_original_code:
int spawn(char *program, char **arg_list)
{
  int child_pid;
  child_pid = fork ();
  if (child_pid != 0)
    return child_pid;
  else {
    execvp (program, arg_list);
    fprintf (stderr, "an error occurred in execvp\n");
    abort ();
  }
}

variable_types:
program
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v4[5]; // [rsp+0h] [rbp-28h] BYREF

v4[0] = "ls";
v4[1] = "-l";
v4[2] = "/";
v4[3] = 0LL;
spawn("ls", v4);
puts("done with main program");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v4[5]; // [rsp+0h] [rbp-28h] BYREF

v4[0] = "ls";
v4[1] = "-l";
v4[2] = "/";
v4[3] = 0LL;
func7("ls", v4);
puts("done with main program");
return 0;
}

original_code:
int main ()
{
  char* arg_list[] = {
    "ls",
    "-l",
    "/",
    ((void *)0)
  };
  spawn ("ls", arg_list);
  printf ("done with main program\n");
  return 0;
}

canonical_original_code:
int main()
{
  char *arg_list[] = {
    "ls",
    "-l",
    "/",
    ((void *)0)
  };
  spawn ("ls", arg_list);
  printf ("done with main program\n");
  return 0;
}


decompiled_code:
void sig_handler()
{
g_continue = 0;
}

canonical_decompiled_code:
void func0()
{
g_continue = 0;
}

original_code:
void
sig_handler(int sig)
{
 g_continue = 0;
}

variable_types: sig int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
unsigned int v4; // ebp
unsigned int v5; // eax
unsigned int v7; // ebx
unsigned int v8; // eax
__int64 v9; // [rsp+8h] [rbp-80h] BYREF
__int64 v10[2]; // [rsp+10h] [rbp-78h] BYREF
__int64 v11[8]; // [rsp+20h] [rbp-68h] BYREF
char v12[34]; // [rsp+66h] [rbp-22h] BYREF

strcpy(v12, "/mailbox1");
v11[1] = 6LL;
v11[2] = 8LL;
v11[0] = 0LL;
v3 = mq_open(v12, 0LL, 384LL, v11);
if ( v3 == -1 )
{
perror("mq_open()");
exit(1);
}
v4 = v3;
signal(2, (__sighandler_t)sig_handler);
v5 = time(0LL);
srand(v5);
while ( g_continue )
{
v10[0] = time(0LL) + 5;
v10[1] = 0LL;
if ( mq_timedreceive(v4, &v9, 8LL, 0LL, v10) == -1 )
{
perror("mq_timedreceive() failed");
puts("Type Ctrl-C and wait for 5 seconds to terminate.");
}
else
{
v7 = get_y_coord(v9);
v8 = get_x_coord(v9);
printf("Received a random point at (%d, %d)\n", v8, v7);
}
}
if ( (unsigned int)mq_close(v4) == -1 )
{
perror("mq_close() failed");
exit(2);
}
return 0;
}

original_code:
int
main(void)
{
 mqd_t qdes;
 char qname[] = "/mailbox1";
 mode_t mode = 0400 | 0200;
 struct mq_attr attr;
 attr.mq_maxmsg = 6;
 attr.mq_msgsize = sizeof(struct point);
 attr.mq_flags = 0;
 qdes = mq_open(qname, 00, mode, &attr);
 if (qdes == -1 ) {
  perror("mq_open()");
  exit(1);
 }
 signal(2, sig_handler);
 srand(time(0));
 while (g_continue) {
  struct point pt;
  struct timespec ts = {time(0) + 5, 0};
  if (mq_timedreceive(qdes, (char *) &pt, sizeof(struct point), 0, &ts) == -1) {
   perror("mq_timedreceive() failed");
   printf("Type Ctrl-C and wait for 5 seconds to terminate.\n");
  } else {
   printf("Received a random point at (%d, %d)\n", get_x_coord(pt), get_y_coord(pt));
  }
 };
 if (mq_close(qdes) == -1) {
  perror("mq_close() failed");
  exit(2);
 }
 return 0;
}

canonical_original_code:
int main()
{
 int qdes;
 char qname[] = "/mailbox1";
 unsigned int mode = 0400 | 0200;
 struct mq_attr attr;
 attr.mq_maxmsg = 6;
 attr.mq_msgsize = sizeof(struct point);
 attr.mq_flags = 0;
 qdes = mq_open(qname, 00, mode, &attr);
 if (qdes == -1 ) {
  perror("mq_open()");
  exit(1);
 }
 signal(2, sig_handler);
 srand(time(0));
 while (g_continue) {
  struct point pt;
  struct timespec ts = {time(0) + 5, 0};
  if (mq_timedreceive(qdes, (char *) &pt, sizeof(struct point), 0, &ts) == -1) {
   perror("mq_timedreceive() failed");
   printf("Type Ctrl-C and wait for 5 seconds to terminate.\n");
  } else {
   printf("Received a random point at (%d, %d)\n", get_x_coord(pt), get_y_coord(pt));
  }
 };
 if (mq_close(qdes) == -1) {
  perror("mq_close() failed");
  exit(2);
 }
 return 0;
}

variable_types: qdes int

variable_types:
qname
char []

variable_types:
mode
unsigned int

variable_types: struct mq_attr { long mq_flags; long mq_maxmsg; long mq_msgsize; long mq_curmsgs; long __pad[4]; }

variable_types:
pt
struct point { int x; int y; }

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


repo: mwilliamson/whack-run

decompiled_code:
__int64 unshare_mount_namespace()
{
return syscall(272LL, 0x20000LL);
}

canonical_decompiled_code:
__int64 func7()
{
return syscall(272LL, 0x20000LL);
}

original_code:
int unshare_mount_namespace() {
    return syscall(272, 0x00020000);
}


decompiled_code:
_BOOL8 __fastcall directory_exists(char *a1)
{
struct stat v2; // [rsp+0h] [rbp-98h] BYREF

return (unsigned int)stat(a1, &v2) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func8(char *a1)
{
struct stat v2; // [rsp+0h] [rbp-98h] BYREF

return (unsigned int)func15(a1, &v2) == 0;
}

original_code:
int directory_exists(char* path) {
    struct stat st;
    return stat(path, &st) == 0;
}

canonical_original_code:
int directory_exists(char *path) {
    struct stat st;
    return stat(path, &st) == 0;
}

variable_types:
path
char *

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


decompiled_code:
int ensure_apps_dest_dir_exists()
{
_BOOL4 v0; // edx
int result; // eax

v0 = directory_exists(apps_dest_dir);
result = 0;
if ( !v0 )
result = mkdir(apps_dest_dir, 0x1EDu);
return result;
}

canonical_decompiled_code:
int func9()
{
_BOOL4 v0; // edx
int result; // eax

v0 = func8(apps_dest_dir);
result = 0;
if ( !v0 )
result = mkdir(apps_dest_dir, 0x1EDu);
return result;
}

original_code:
int ensure_apps_dest_dir_exists() {
    if (directory_exists(apps_dest_dir)) {
        return 0;
    } else {
        return mkdir(apps_dest_dir, 0755);
    }
}


decompiled_code:
int __fastcall mount_bind(const char *a1, const char *a2)
{
return mount(a1, a2, 0LL, 0x1000uLL, 0LL);
}

canonical_decompiled_code:
int __fastcall func10(const char *a1, const char *a2)
{
return mount(a1, a2, 0LL, 0x1000uLL, 0LL);
}

original_code:
int mount_bind(char* src_path, char* dest_path) {
    return mount(src_path, dest_path, ((void *)0), MS_BIND, ((void *)0));
}

canonical_original_code:
int mount_bind(char *src_path, char *dest_path) {
    return mount(src_path, dest_path, ((void *)0), MS_BIND, ((void *)0));
}


decompiled_code:
_BOOL8 __fastcall str_starts_with(char *s2, char *s1)
{
size_t v2; // r12
size_t v3; // rdx
_BOOL8 result; // rax

v2 = strlen(s1);
v3 = strlen(s2);
result = 0LL;
if ( v2 <= v3 )
result = strncmp(s1, s2, v2) == 0;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func11(char *s2, char *s1)
{
size_t v2; // r12
size_t v3; // rdx
_BOOL8 result; // rax

v2 = strlen(s1);
v3 = strlen(s2);
result = 0LL;
if ( v2 <= v3 )
result = strncmp(s1, s2, v2) == 0;
return result;
}

original_code:
int str_starts_with(const char *str, const char *prefix) {
    size_t prefix_len = strlen(prefix);
    size_t str_len = strlen(str);
    return str_len < prefix_len ? 0 : strncmp(prefix, str, prefix_len) == 0;
}

canonical_original_code:
int str_starts_with(char *str, char *prefix) {
    unsigned long prefix_len = strlen(prefix);
    unsigned long str_len = strlen(str);
    return str_len < prefix_len ? 0 : strncmp(prefix, str, prefix_len) == 0;
}

variable_types:
str
char *

variable_types:
prefix
char *


decompiled_code:
__int64 __fastcall should_fork(int a1, const char **a2)
{
const char **v2; // rdx

if ( a1 <= 0 )
return 0LL;
v2 = a2;
while ( strcmp(*v2, "--fork") )
{
if ( ++v2 == &a2[(unsigned int)(a1 - 1) + 1] )
return 0LL;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func12(int a1, const char **a2)
{
const char **v2; // rdx

if ( a1 <= 0 )
return 0LL;
v2 = a2;
while ( strcmp(*v2, "--fork") )
{
if ( ++v2 == &a2[(unsigned int)(a1 - 1) + 1] )
return 0LL;
}
return 1LL;
}

original_code:
int should_fork(int argc, char **argv) {
    for (int i = 0; i < argc; i++) {
        if (strcmp(argv[i], "--fork") == 0) {
            return 1;
        }
    }
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: i int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbx
int v4; // er12
int v5; // ebx
const char *v7; // r12
const char *v8; // rbx
__gid_t v9; // eax
__uid_t v10; // eax
const char **v11; // r13
const char *v12; // r15
char *const *v13; // rbx
__int64 v14; // rax
int v15; // er14
int v16; // eax
int v17; // eax
int stat_loc; // [rsp+Ch] [rbp-3Ch] BYREF

if ( argc == 2 )
{
v7 = argv[1];
v5 = strcmp(v7, "--source-hash");
if ( !v5 )
{
puts("22bb8ced44fb757bdd25ca9a3b364e45ee9c5e93");
return v5;
}
v5 = strcmp(v7, "--version");
if ( !v5 )
{
puts("1.1.0-dev");
return v5;
}
goto LABEL_6;
}
if ( argc <= 2 )
goto LABEL_6;
v3 = 2LL;
while ( 1 )
{
v4 = v3;
if ( !str_starts_with((char *)argv[v3], "--") )
break;
if ( ++v3 == argc )
goto LABEL_6;
}
if ( argc <= (int)v3 )
{
LABEL_6:
printf("Usage: %s <apps-dir> [--fork] <app> <args>\n", *argv);
return 1;
}
if ( (unsigned int)unshare_mount_namespace() )
{
puts("ERROR: Could not unshare mount namespace");
v5 = 1;
}
else if ( ensure_apps_dest_dir_exists() )
{
printf("ERROR: Could not create %s\n", apps_dest_dir);
v5 = 1;
}
else
{
umount(apps_dest_dir);
v8 = argv[1];
if ( mount_bind(v8, apps_dest_dir) )
{
printf("ERROR: Could not mount %s\n", v8);
v5 = 1;
}
else
{
v9 = getgid();
if ( setgid(v9) )
{
printf("ERROR: Could not drop group privileges");
v5 = 1;
}
else
{
v10 = getuid();
if ( setuid(v10) )
{
printf("ERROR: Could not drop user privileges");
v5 = 1;
}
else
{
v11 = &argv[v4];
v12 = *v11;
v13 = (char *const *)malloc(8LL * (argc - 1));
v14 = 0LL;
v15 = argc - v4;
do
{
v13[v14] = (char *const)v11[v14];
++v14;
}
while ( v15 > (int)v14 );
v13[v15] = 0LL;
if ( !(unsigned int)should_fork(v4, argv) )
goto LABEL_23;
v16 = fork();
if ( v16 < 0 )
{
puts("ERROR: failed to fork");
return 1;
}
if ( v16 > 0 )
{
stat_loc = 0;
waitpid(v16, &stat_loc, 0);
umount2(apps_dest_dir, 2);
if ( (stat_loc & 0x7F) != 0 )
{
v5 = 255;
if ( (char)((stat_loc & 0x7F) + 1) > 1 )
v5 = (stat_loc & 0x7F) + 128;
}
else
{
v5 = BYTE1(stat_loc);
}
}
else
{
LABEL_23:
v17 = execvp(v12, v13);
v5 = 2;
if ( v17 )
{
printf("ERROR: failed to exec %s\n", v12);
v5 = 1;
}
}
}
}
}
}
return v5;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbx
int v4; // er12
int v5; // ebx
const char *v7; // r12
const char *v8; // rbx
__gid_t v9; // eax
__uid_t v10; // eax
const char **v11; // r13
const char *v12; // r15
char *const *v13; // rbx
__int64 v14; // rax
int v15; // er14
int v16; // eax
int v17; // eax
int stat_loc; // [rsp+Ch] [rbp-3Ch] BYREF

if ( argc == 2 )
{
v7 = argv[1];
v5 = strcmp(v7, "--source-hash");
if ( !v5 )
{
puts("22bb8ced44fb757bdd25ca9a3b364e45ee9c5e93");
return v5;
}
v5 = strcmp(v7, "--version");
if ( !v5 )
{
puts("1.1.0-dev");
return v5;
}
goto LABEL_6;
}
if ( argc <= 2 )
goto LABEL_6;
v3 = 2LL;
while ( 1 )
{
v4 = v3;
if ( !func11((char *)argv[v3], "--") )
break;
if ( ++v3 == argc )
goto LABEL_6;
}
if ( argc <= (int)v3 )
{
LABEL_6:
printf("Usage: %s <apps-dir> [--fork] <app> <args>\n", *argv);
return 1;
}
if ( (unsigned int)func7() )
{
puts("ERROR: Could not unshare mount namespace");
v5 = 1;
}
else if ( func9() )
{
printf("ERROR: Could not create %s\n", apps_dest_dir);
v5 = 1;
}
else
{
umount(apps_dest_dir);
v8 = argv[1];
if ( func10(v8, apps_dest_dir) )
{
printf("ERROR: Could not mount %s\n", v8);
v5 = 1;
}
else
{
v9 = getgid();
if ( setgid(v9) )
{
printf("ERROR: Could not drop group privileges");
v5 = 1;
}
else
{
v10 = getuid();
if ( setuid(v10) )
{
printf("ERROR: Could not drop user privileges");
v5 = 1;
}
else
{
v11 = &argv[v4];
v12 = *v11;
v13 = (char *const *)malloc(8LL * (argc - 1));
v14 = 0LL;
v15 = argc - v4;
do
{
v13[v14] = (char *const)v11[v14];
++v14;
}
while ( v15 > (int)v14 );
v13[v15] = 0LL;
if ( !(unsigned int)func12(v4, argv) )
goto LABEL_23;
v16 = fork();
if ( v16 < 0 )
{
puts("ERROR: failed to fork");
return 1;
}
if ( v16 > 0 )
{
stat_loc = 0;
waitpid(v16, &stat_loc, 0);
umount2(apps_dest_dir, 2);
if ( (stat_loc & 0x7F) != 0 )
{
v5 = 255;
if ( (char)((stat_loc & 0x7F) + 1) > 1 )
v5 = (stat_loc & 0x7F) + 128;
}
else
{
v5 = BYTE1(stat_loc);
}
}
else
{
LABEL_23:
v17 = execvp(v12, v13);
v5 = 2;
if ( v17 )
{
printf("ERROR: failed to exec %s\n", v12);
v5 = 1;
}
}
}
}
}
}
return v5;
}

original_code:
int main(int argc, char **argv) {
    if (argc == 2 && strcmp(argv[1], "--source-hash") == 0) {
        printf("%s\n", "22bb8ced44fb757bdd25ca9a3b364e45ee9c5e93");
        return 0;
    }
    if (argc == 2 && strcmp(argv[1], "--version") == 0) {
        printf("%s\n", "1.1.0-dev");
        return 0;
    }
    int app_index = 2;
    while (app_index < argc && str_starts_with(argv[app_index], "--")) {
        app_index++;
    }
    if (app_index >= argc) {
        printf("Usage: %s <apps-dir> [--fork] <app> <args>\n", argv[0]);
        return 1;
    }
    if (unshare_mount_namespace() != 0) {
        printf("ERROR: Could not unshare mount namespace\n");
        return 1;
    }
    if (ensure_apps_dest_dir_exists() != 0) {
        printf("ERROR: Could not create %s\n", apps_dest_dir);
        return 1;
    }
    umount(apps_dest_dir);
    char* apps_src_dir = argv[1];
    if (mount_bind(apps_src_dir, apps_dest_dir) != 0) {
        printf("ERROR: Could not mount %s\n", apps_src_dir);
        return 1;
    }
    if (setgid(getgid()) != 0) {
        printf("ERROR: Could not drop group privileges");
        return 1;
    }
    if (setuid(getuid()) != 0) {
        printf("ERROR: Could not drop user privileges");
        return 1;
    }
    char* app = argv[app_index];
    char** app_args = (char**)malloc(sizeof(char*) * (argc - 1));
    for (int i = 0; i < argc - app_index; i++) {
        app_args[i] = argv[i + app_index];
    }
    app_args[argc - app_index] = 0;
    if (should_fork(app_index, argv)) {
        int pid = fork();
        if (pid < 0) {
            printf("ERROR: failed to fork\n");
            return 1;
        }
        if (pid > 0) {
            int status = 0;
            waitpid(pid, &status, 0);
            umount2(apps_dest_dir, MNT_DETACH);
            if ((((status) & 0x7f) == 0)) {
                return (((status) & 0xff00) >> 8);
            } else if ((((signed char) (((status) & 0x7f) + 1) >> 1) > 0)) {
                return 128 + ((status) & 0x7f);
            } else {
                return 255;
            }
        }
    }
    if (execvp(app, app_args) != 0) {
        printf("ERROR: failed to exec %s\n", app);
        return 1;
    }
    return 2;
}

canonical_original_code:
int main(int argc, char **argv) {
    if (argc == 2 && strcmp(argv[1], "--source-hash") == 0) {
        printf("%s\n", "22bb8ced44fb757bdd25ca9a3b364e45ee9c5e93");
        return 0;
    }
    if (argc == 2 && strcmp(argv[1], "--version") == 0) {
        printf("%s\n", "1.1.0-dev");
        return 0;
    }
    int app_index = 2;
    while (app_index < argc && str_starts_with(argv[app_index], "--")) {
        app_index++;
    }
    if (app_index >= argc) {
        printf("Usage: %s <apps-dir> [--fork] <app> <args>\n", argv[0]);
        return 1;
    }
    if (unshare_mount_namespace() != 0) {
        printf("ERROR: Could not unshare mount namespace\n");
        return 1;
    }
    if (ensure_apps_dest_dir_exists() != 0) {
        printf("ERROR: Could not create %s\n", apps_dest_dir);
        return 1;
    }
    umount(apps_dest_dir);
    char *apps_src_dir = argv[1];
    if (mount_bind(apps_src_dir, apps_dest_dir) != 0) {
        printf("ERROR: Could not mount %s\n", apps_src_dir);
        return 1;
    }
    if (setgid(getgid()) != 0) {
        printf("ERROR: Could not drop group privileges");
        return 1;
    }
    if (setuid(getuid()) != 0) {
        printf("ERROR: Could not drop user privileges");
        return 1;
    }
    char *app = argv[app_index];
    char **app_args = (char**)malloc(sizeof(char*) * (argc - 1));
    for (int i = 0; i < argc - app_index; i++) {
        app_args[i] = argv[i + app_index];
    }
    app_args[argc - app_index] = 0;
    if (should_fork(app_index, argv)) {
        int pid = fork();
        if (pid < 0) {
            printf("ERROR: failed to fork\n");
            return 1;
        }
        if (pid > 0) {
            int status = 0;
            waitpid(pid, &status, 0);
            umount2(apps_dest_dir, MNT_DETACH);
            if ((((status) & 0x7f) == 0)) {
                return (((status) & 0xff00) >> 8);
            } else if ((((char) (((status) & 0x7f) + 1) >> 1) > 0)) {
                return 128 + ((status) & 0x7f);
            } else {
                return 255;
            }
        }
    }
    if (execvp(app, app_args) != 0) {
        printf("ERROR: failed to exec %s\n", app);
        return 1;
    }
    return 2;
}

variable_types:
app
char *

variable_types: pid int

variable_types: status int


repo: mwillsey/NES

decompiled_code:
__int64 __fastcall mem_read(_QWORD *a1, unsigned __int16 a2)
{
__int64 v2; // rdx
__int64 (__fastcall *v3)(_QWORD); // rax
__int64 result; // rax

v2 = *(unsigned __int16 *)(a1[2] + 2LL * a2);
v3 = *(__int64 (__fastcall **)(_QWORD))(a1[4] + 8 * v2);
if ( v3 )
result = v3(*a1);
else
result = *(unsigned __int8 *)(a1[1] + v2);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(_QWORD *a1, unsigned __int16 a2)
{
__int64 v2; // rdx
__int64 (__fastcall *v3)(_QWORD); // rax
__int64 result; // rax

v2 = *(unsigned __int16 *)(a1[2] + 2LL * a2);
v3 = *(__int64 (__fastcall **)(_QWORD))(a1[4] + 8 * v2);
if ( v3 )
result = v3(*a1);
else
result = *(unsigned __int8 *)(a1[1] + v2);
return result;
}

original_code:
byte mem_read (memory *mem, addr a) {
  addr a1 = mem->mirrors[a];
  if (mem->read_cbs[a1])
    return (mem->read_cbs[a1])(mem->n);
  return mem->ram[a1];
}

canonical_original_code:
unsigned char mem_read(struct memory *mem, unsigned short a) {
  unsigned short a1 = mem->mirrors[a];
  if (mem->read_cbs[a1])
    return (mem->read_cbs[a1])(mem->n);
  return mem->ram[a1];
}

variable_types: struct memory { struct nes { struct cpu_s { union sem_t { char __size[32]; long __align; } clock; struct memory *mem; unsigned char A; unsigned char X; unsigned char SP; unsigned short PC; unsigned char P; } *c; struct ppu_s { union sem_t clock; union sem_t render_clock; struct memory *mem; int cycle; int scanline; _Bool even_frame; _Bool first_write; _Bool rendering; unsigned char nt_entry; unsigned char at_latch; unsigned char pt_latch_lo; unsigned short at_shift; unsigned short pt_shift_lo; unsigned char oam1[64][4]; unsigned char oam2[8][4]; unsigned char spr_shift[8][2]; unsigned char spr_latch[8]; unsigned char spr_count[8]; unsigned char ctrl; unsigned char mask; unsigned char status; unsigned char oam_addr; unsigned char oam_data; unsigned char scrollx; unsigned short addr; unsigned char frame_buffer[240][256]; } *p; unsigned char *lower_bank; unsigned char *upper_bank; unsigned char *chr_rom; } *n; unsigned char *ram; unsigned short *mirrors; void (**write_cbs)(struct nes *, unsigned char); unsigned char (**read_cbs)(struct nes *); } *

variable_types:
a
unsigned short

return_type: unsigned char

user_defined_types: struct memory { struct nes *n; unsigned char *ram; unsigned short *mirrors; void (**write_cbs)(struct nes *, unsigned char); unsigned char (**read_cbs)(struct nes *); }

user_defined_types: struct nes { struct cpu_s *c; struct ppu_s *p; unsigned char *lower_bank; unsigned char *upper_bank; unsigned char *chr_rom; }

user_defined_types: struct cpu_s { union sem_t clock; struct memory *mem; unsigned char A; unsigned char X; unsigned char SP; unsigned short PC; unsigned char P; }


decompiled_code:
void __fastcall mem_destroy(__int64 a1)
{
free(*(void **)(a1 + 8));
free(*(void **)(a1 + 24));
}

original_code:
void mem_destroy (memory *mem) {
  free(mem->ram);
  free(mem->write_cbs);
}

canonical_original_code:
void mem_destroy(struct memory *mem) {
  free(mem->ram);
  free(mem->write_cbs);
}

user_defined_types: struct ppu_s { union sem_t clock; union sem_t render_clock; struct memory *mem; int cycle; int scanline; _Bool even_frame; _Bool first_write; _Bool rendering; unsigned char nt_entry; unsigned char at_latch; unsigned char pt_latch_lo; unsigned short at_shift; unsigned short pt_shift_lo; unsigned char oam1[64][4]; unsigned char oam2[8][4]; unsigned char spr_shift[8][2]; unsigned char spr_latch[8]; unsigned char spr_count[8]; unsigned char ctrl; unsigned char mask; unsigned char status; unsigned char oam_addr; unsigned char oam_data; unsigned char scrollx; unsigned short addr; unsigned char frame_buffer[240][256]; }

user_defined_types: union sem_t { char __size[32]; long __align; }


decompiled_code:
__int64 __fastcall mem_mirror(__int64 a1, int a2, unsigned __int16 a3, int a4)
{
__int16 i; // r10
__int64 result; // rax

for ( i = a2; a3 >= (unsigned __int16)a2; ++a2 )
{
result = (unsigned int)((unsigned __int16)a2 / a4);
*(_WORD *)(*(_QWORD *)(a1 + 16) + 2LL * (unsigned __int16)a2) = i + (unsigned __int16)a2 % a4;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int a2, unsigned __int16 a3, int a4)
{
__int16 i; // r10
__int64 result; // rax

for ( i = a2; a3 >= (unsigned __int16)a2; ++a2 )
{
result = (unsigned int)((unsigned __int16)a2 / a4);
*(_WORD *)(*(_QWORD *)(a1 + 16) + 2LL * (unsigned __int16)a2) = i + (unsigned __int16)a2 % a4;
}
return result;
}

original_code:
void mem_mirror (memory *mem, addr start, addr end, int size) {
  addr a;
  for (a = start; a <= end; a++) {
    mem->mirrors[a] = start + a % size;
  }
}

canonical_original_code:
void mem_mirror(struct memory *mem, unsigned short start, unsigned short end, int size) {
  unsigned short a;
  for (a = start; a <= end; a++) {
    mem->mirrors[a] = start + a % size;
  }
}

variable_types:
start
unsigned short

variable_types:
end
unsigned short

variable_types: size int


repo: mwpher/btar-fbsd

decompiled_code:
void __fastcall add_filter(char **argv, int fd, _DWORD *a3, int a4)
{
char **v4; // rbx
const char *v7; // rdx
unsigned int v8; // eax
int v9; // eax
const char *v10; // rdx
int v11; // eax
const char *v12; // rdx
const char *v13; // rdx
char *v14; // rdx
int pipedes[2]; // [rsp+8h] [rbp-30h] BYREF

v4 = argv;
if ( pipe(pipedes) == -1 )
error((int)"Error making pipe", fd, v7);
v8 = fork();
if ( !v8 )
{
if ( a4 != -1 )
close(a4);
close(0);
v9 = dup(fd);
if ( v9 == -1 )
{
error((int)"Cannot dup", fd, v10);
}
else if ( !v9 )
{
close(fd);
close(1);
v11 = dup(pipedes[1]);
if ( v11 == -1 )
{
error((int)"Cannot dup", fd, v12);
}
else if ( v11 == 1 )
{
close(pipedes[1]);
close(pipedes[0]);
execvp(*argv, argv);
error((int)"Cannot exec filter", (int)argv, v13);
return;
}
_assert_fail("res == 1", "filters.c", 0x3Au, "add_filter");
}
_assert_fail("res == 0", "filters.c", 0x33u, "add_filter");
}
if ( command_line[1] )
{
fprintf(stderr, "Running filter PID %i: ", v8);
v14 = *argv;
if ( *argv )
{
do
{
fprintf(stderr, "%s ", v14);
v14 = *++v4;
}
while ( *v4 );
}
fputc(10, stderr);
}
close(fd);
close(pipedes[1]);
*a3 = pipedes[0];
}

canonical_decompiled_code:
void __fastcall func0(char **argv, int fd, _DWORD *a3, int a4)
{
char **v4; // rbx
const char *v7; // rdx
unsigned int v8; // eax
int v9; // eax
const char *v10; // rdx
int v11; // eax
const char *v12; // rdx
const char *v13; // rdx
char *v14; // rdx
int pipedes[2]; // [rsp+8h] [rbp-30h] BYREF

v4 = argv;
if ( pipe(pipedes) == -1 )
error((int)"Error making pipe", fd, v7);
v8 = fork();
if ( !v8 )
{
if ( a4 != -1 )
close(a4);
close(0);
v9 = dup(fd);
if ( v9 == -1 )
{
error((int)"Cannot dup", fd, v10);
}
else if ( !v9 )
{
close(fd);
close(1);
v11 = dup(pipedes[1]);
if ( v11 == -1 )
{
error((int)"Cannot dup", fd, v12);
}
else if ( v11 == 1 )
{
close(pipedes[1]);
close(pipedes[0]);
execvp(*argv, argv);
error((int)"Cannot exec filter", (int)argv, v13);
return;
}
_assert_fail("res == 1", "filters.c", 0x3Au, "add_filter");
}
_assert_fail("res == 0", "filters.c", 0x33u, "add_filter");
}
if ( command_line[1] )
{
fprintf(stderr, "Running filter PID %i: ", v8);
v14 = *argv;
if ( *argv )
{
do
{
fprintf(stderr, "%s ", v14);
v14 = *++v4;
}
while ( *v4 );
}
fputc(10, stderr);
}
close(fd);
close(pipedes[1]);
*a3 = pipedes[0];
}

original_code:
void
add_filter(char * const *args, int fdin, int *fdout, int alsoclose)
{
    int filter_output[2];
    int res;
    res = pipe(filter_output);
    if (res == -1)
        error("Error making pipe");
    int pid = fork();
    if (pid == 0)
    {
        int res;
        if (alsoclose != -1)
            close(alsoclose);
        close(0);
        res = dup(fdin);
        if (res == -1)
            error("Cannot dup");
        ((void) sizeof ((res == 0) ? 1 : 0), __extension__ ({ if (res == 0) ; else __assert_fail ("res == 0", "filters.c", 51, __extension__ __PRETTY_FUNCTION__); }));
        close(fdin);
        close(1);
        res = dup(filter_output[1]);
        if (res == -1)
            error("Cannot dup");
        ((void) sizeof ((res == 1) ? 1 : 0), __extension__ ({ if (res == 1) ; else __assert_fail ("res == 1", "filters.c", 58, __extension__ __PRETTY_FUNCTION__); }));
        close(filter_output[1]);
        close(filter_output[0]);
        res = execvp(args[0], args);
        error("Cannot exec filter");
    }
    else
    {
        if (command_line.debug)
        {
            char * const *ptr = args;
            fprintf(stderr, "Running filter PID %i: ", pid);
            while(*ptr)
            {
                fprintf(stderr, "%s ", *ptr);
                ++ptr;
            }
            fprintf(stderr, "\n");
        }
        close(fdin);
        close(filter_output[1]);
        *fdout = filter_output[0];
    }
}

canonical_original_code:
void add_filter(char **args, int fdin, int *fdout, int alsoclose)
{
    int filter_output[2];
    int res;
    res = pipe(filter_output);
    if (res == -1)
        error("Error making pipe");
    int pid = fork();
    if (pid == 0)
    {
        int res;
        if (alsoclose != -1)
            close(alsoclose);
        close(0);
        res = dup(fdin);
        if (res == -1)
            error("Cannot dup");
        ((void) sizeof ((res == 0) ? 1 : 0), __extension__ ({ if (res == 0) ; else __assert_fail ("res == 0", "filters.c", 51, __extension__ __PRETTY_FUNCTION__); }));
        close(fdin);
        close(1);
        res = dup(filter_output[1]);
        if (res == -1)
            error("Cannot dup");
        ((void) sizeof ((res == 1) ? 1 : 0), __extension__ ({ if (res == 1) ; else __assert_fail ("res == 1", "filters.c", 58, __extension__ __PRETTY_FUNCTION__); }));
        close(filter_output[1]);
        close(filter_output[0]);
        res = execvp(args[0], args);
        error("Cannot exec filter");
    }
    else
    {
        if (command_line.debug)
        {
            char **ptr = args;
            fprintf(stderr, "Running filter PID %i: ", pid);
            while(*ptr)
            {
                fprintf(stderr, "%s ", *ptr);
                ++ptr;
            }
            fprintf(stderr, "\n");
        }
        close(fdin);
        close(filter_output[1]);
        *fdout = filter_output[0];
    }
}

variable_types:
args
char **

variable_types: fdin int

variable_types:
fdout
int *

variable_types: alsoclose int

variable_types: res int

variable_types:
ptr
char **


decompiled_code:
void __fastcall first_filter(char **argv, _DWORD *a2, _DWORD *a3)
{
const char *v4; // rdx
int v5; // ecx
int pipedes[2]; // [rsp+8h] [rbp-20h] BYREF

if ( pipe(pipedes) == -1 )
error((int)"Error making pipe", (int)a2, v4);
v5 = pipedes[1];
*a2 = pipedes[1];
add_filter(argv, pipedes[0], a3, v5);
}

canonical_decompiled_code:
void __fastcall func1(char **argv, _DWORD *a2, _DWORD *a3)
{
const char *v4; // rdx
int v5; // ecx
int pipedes[2]; // [rsp+8h] [rbp-20h] BYREF

if ( pipe(pipedes) == -1 )
error((int)"Error making pipe", (int)a2, v4);
v5 = pipedes[1];
*a2 = pipedes[1];
func0(argv, pipedes[0], a3, v5);
}

original_code:
void
first_filter(char * const *args, int *fdin, int *fdout)
{
    int filter_input[2];
    int res;
    res = pipe(filter_input);
    if (res == -1)
        error("Error making pipe");
    *fdin = filter_input[1];
    add_filter(args, filter_input[0], fdout, *fdin);
}

canonical_original_code:
void first_filter(char **args, int *fdin, int *fdout)
{
    int filter_input[2];
    int res;
    res = pipe(filter_input);
    if (res == -1)
        error("Error making pipe");
    *fdin = filter_input[1];
    add_filter(args, filter_input[0], fdout, *fdin);
}

variable_types:
fdin
int *


decompiled_code:
void __fastcall run_filters_given_fdin(__int64 a1, unsigned int a2, unsigned int *a3)
{
__int64 i; // rbx

if ( a1 )
{
add_filter(*(char ***)a1, a2, a3, -1);
for ( i = *(_QWORD *)(a1 + 8); i; i = *(_QWORD *)(i + 8) )
add_filter(*(char ***)i, *a3, a3, a2);
}
else
{
*a3 = a2;
}
if ( command_line[1] )
fprintf(stderr, " Filter started, reading from given fd %i, outputing on fd %i.\n", a2, *a3);
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1, unsigned int a2, unsigned int *a3)
{
__int64 i; // rbx

if ( a1 )
{
func0(*(char ***)a1, a2, a3, -1);
for ( i = *(_QWORD *)(a1 + 8); i; i = *(_QWORD *)(i + 8) )
func0(*(char ***)i, *a3, a3, a2);
}
else
{
*a3 = a2;
}
if ( command_line[1] )
fprintf(stderr, " Filter started, reading from given fd %i, outputing on fd %i.\n", a2, *a3);
}

original_code:
void
run_filters_given_fdin(struct filter *filter, int fdin, int *fdout)
{
    struct filter *f = filter;
    if (f != 0)
    {
        add_filter(f->args, fdin, fdout, -1);
        f = f->next;
        while(f)
        {
            add_filter(f->args, *fdout, fdout, fdin);
            f = f->next;
        }
    }
    else
    {
        *fdout = fdin;
    }
    if (command_line.debug)
        fprintf(stderr, "  Filter started, reading from given fd %i, outputing on fd %i.\n",
                fdin, *fdout);
}

variable_types: struct filter { char **args; struct filter *next; char extensions[4096]; } *


decompiled_code:
void __fastcall run_filters(__int64 a1, unsigned int *a2, unsigned int *a3)
{
__int64 i; // rbx
const char *v5; // rdx
int pipedes[2]; // [rsp+8h] [rbp-20h] BYREF

if ( a1 )
{
first_filter(*(char ***)a1, a2, a3);
for ( i = *(_QWORD *)(a1 + 8); i; i = *(_QWORD *)(i + 8) )
add_filter(*(char ***)i, *a3, a3, *a2);
}
else
{
if ( pipe(pipedes) == -1 )
error((int)"Cannot create pipe", (int)a2, v5);
*a2 = pipedes[1];
*a3 = pipedes[0];
}
if ( command_line[1] )
fprintf(stderr, " Filter started, accepting on fd %i, outputing on fd %i.\n", *a2, *a3);
}

canonical_decompiled_code:
void __fastcall func3(__int64 a1, unsigned int *a2, unsigned int *a3)
{
__int64 i; // rbx
const char *v5; // rdx
int pipedes[2]; // [rsp+8h] [rbp-20h] BYREF

if ( a1 )
{
func1(*(char ***)a1, a2, a3);
for ( i = *(_QWORD *)(a1 + 8); i; i = *(_QWORD *)(i + 8) )
func0(*(char ***)i, *a3, a3, *a2);
}
else
{
if ( pipe(pipedes) == -1 )
error((int)"Cannot create pipe", (int)a2, v5);
*a2 = pipedes[1];
*a3 = pipedes[0];
}
if ( command_line[1] )
fprintf(stderr, " Filter started, accepting on fd %i, outputing on fd %i.\n", *a2, *a3);
}

original_code:
void
run_filters(struct filter *filter, int *fdin, int *fdout)
{
    struct filter *f = filter;
    if (f != 0)
    {
        first_filter(f->args, fdin, fdout);
        f = f->next;
        while(f)
        {
            add_filter(f->args, *fdout, fdout, *fdin);
            f = f->next;
        }
    }
    else
    {
        int mypipe[2];
        int res;
        res = pipe(mypipe);
        if (res == -1)
            error("Cannot create pipe");
        *fdin = mypipe[1];
        *fdout = mypipe[0];
    }
    if (command_line.debug)
        fprintf(stderr, "  Filter started, accepting on fd %i, outputing on fd %i.\n",
                *fdin, *fdout);
}

variable_types:
mypipe
int [2]

user_defined_types: struct filter { char **args; struct filter *next; char extensions[4096]; }


decompiled_code:
void *__fastcall append_filter(__int64 a1, const char **a2)
{
void *v2; // rbx
const char *v3; // r12
char *v4; // rax
char *v5; // r13

v2 = malloc(0x1010uLL);
if ( !v2 )
fatal_error("Cannot allocate");
*((_QWORD *)v2 + 1) = 0LL;
*(_QWORD *)v2 = a2;
v3 = *a2;
v4 = strrchr(*a2, 47);
if ( v4 )
v3 = v4 + 1;
if ( a1 )
{
v5 = strdup((const char *)(a1 + 16));
snprintf((char *)v2 + 16, 0x1000uLL, "%s.%s", v5, v3);
free(v5);
*(_QWORD *)(a1 + 8) = v2;
v2 = (void *)a1;
}
else if ( !strcmp(v3, "cat") )
{
*((_BYTE *)v2 + 16) = 0;
}
else
{
snprintf((char *)v2 + 16, 0x1000uLL, ".%s", v3);
}
return v2;
}

canonical_decompiled_code:
void *__fastcall func4(__int64 a1, const char **a2)
{
void *v2; // rbx
const char *v3; // r12
char *v4; // rax
char *v5; // r13

v2 = malloc(0x1010uLL);
if ( !v2 )
fatal_error("Cannot allocate");
*((_QWORD *)v2 + 1) = 0LL;
*(_QWORD *)v2 = a2;
v3 = *a2;
v4 = strrchr(*a2, 47);
if ( v4 )
v3 = v4 + 1;
if ( a1 )
{
v5 = strdup((const char *)(a1 + 16));
snprintf((char *)v2 + 16, 0x1000uLL, "%s.%s", v5, v3);
free(v5);
*(_QWORD *)(a1 + 8) = v2;
v2 = (void *)a1;
}
else if ( !strcmp(v3, "cat") )
{
*((_BYTE *)v2 + 16) = 0;
}
else
{
snprintf((char *)v2 + 16, 0x1000uLL, ".%s", v3);
}
return v2;
}

original_code:
struct filter *
append_filter(struct filter *f, char **args)
{
    struct filter *fnew;
    fnew = malloc(sizeof(*fnew));
    if (!fnew)
        fatal_error("Cannot allocate");
    fnew->next = 0;
    fnew->args = args;
    {
        const char *pos, *npos;
        pos = args[0];
        if ((npos = strrchr(pos, '/')) != 0)
            pos = npos+1;
        if(f)
        {
            char *tmp;
            tmp = strdup(f->extensions);
            snprintf(fnew->extensions, sizeof fnew->extensions,
                    "%s.%s", tmp, pos);
            free(tmp);
        }
        else
        {
            if (!strcmp(pos, "cat"))
                fnew->extensions[0] = '\0';
            else
                snprintf(fnew->extensions, sizeof fnew->extensions,
                        ".%s", pos);
        }
    }
    if (f)
    {
        f->next = fnew;
        return f;
    }
    return fnew;
}

canonical_original_code:
struct filter *append_filter(struct filter *f, char **args)
{
    struct filter *fnew;
    fnew = malloc(sizeof(*fnew));
    if (!fnew)
        fatal_error("Cannot allocate");
    fnew->next = 0;
    fnew->args = args;
    {
        char *pos; char *npos;
        pos = args[0];
        if ((npos = strrchr(pos, '/')) != 0)
            pos = npos+1;
        if(f)
        {
            char *tmp;
            tmp = strdup(f->extensions);
            snprintf(fnew->extensions, sizeof fnew->extensions,
                    "%s.%s", tmp, pos);
            free(tmp);
        }
        else
        {
            if (!strcmp(pos, "cat"))
                fnew->extensions[0] = '\0';
            else
                snprintf(fnew->extensions, sizeof fnew->extensions,
                        ".%s", pos);
        }
    }
    if (f)
    {
        f->next = fnew;
        return f;
    }
    return fnew;
}

variable_types:
f
struct filter *

variable_types:
pos
char *

variable_types:
npos
char *

variable_types:
tmp
char *

return_type: struct filter { char **args; struct filter *next; char extensions[4096]; } *


decompiled_code:
void *__fastcall append_filter_spaces(__int64 a1, const char *a2)
{
char *v3; // rax
const char *v4; // rbx
char *v5; // rdi
int i; // ebp
const char **v7; // r13
__int64 j; // rbp
char *v9; // rax

v3 = strdup(a2);
v4 = v3;
if ( *v3 == 32 )
{
v5 = v3;
do
++v5;
while ( *v5 == 32 );
}
else
{
v5 = v3;
}
for ( i = 0; ; ++i )
{
v5 = strchr(v5, 32);
if ( !v5 )
break;
do
++v5;
while ( *v5 == 32 );
}
v7 = (const char **)malloc(8LL * (i + 2));
if ( !v7 )
fatal_error("Cannot allocate");
for ( j = 0LL; ; ++j )
{
v9 = strchr(v4, 32);
if ( !v9 )
break;
*v9 = 0;
v7[j] = v4;
do
++v9;
while ( *v9 == 32 && *v4 );
v4 = v9;
}
v7[(int)j] = v4;
v7[(int)j + 1] = 0LL;
return append_filter(a1, v7);
}

canonical_decompiled_code:
void *__fastcall func5(__int64 a1, const char *a2)
{
char *v3; // rax
const char *v4; // rbx
char *v5; // rdi
int i; // ebp
const char **v7; // r13
__int64 j; // rbp
char *v9; // rax

v3 = strdup(a2);
v4 = v3;
if ( *v3 == 32 )
{
v5 = v3;
do
++v5;
while ( *v5 == 32 );
}
else
{
v5 = v3;
}
for ( i = 0; ; ++i )
{
v5 = strchr(v5, 32);
if ( !v5 )
break;
do
++v5;
while ( *v5 == 32 );
}
v7 = (const char **)malloc(8LL * (i + 2));
if ( !v7 )
fatal_error("Cannot allocate");
for ( j = 0LL; ; ++j )
{
v9 = strchr(v4, 32);
if ( !v9 )
break;
*v9 = 0;
v7[j] = v4;
do
++v9;
while ( *v9 == 32 && *v4 );
v4 = v9;
}
v7[(int)j] = v4;
v7[(int)j + 1] = 0LL;
return func4(a1, v7);
}

original_code:
struct filter *
append_filter_spaces(struct filter *f, const char *arg)
{
    int spaces = 0;
    char **args;
    char *tmp = strdup(arg);
    char *pos = tmp;
    char *nextpos;
    while(*pos == ' ')
        ++pos;
    while((pos = strchr(pos, ' ')) != 0)
    {
        do
            ++pos;
        while(*pos == ' ' && *pos != '\0');
        ++spaces;
    }
    ++spaces;
    ++spaces;
    args = malloc(sizeof(*args) * spaces);
    if (!args)
        fatal_error("Cannot allocate");
    spaces = 0;
    pos = tmp;
    while((nextpos = strchr(pos,' ')) != 0)
    {
        *nextpos = '\0';
        args[spaces++] = pos;
        do
            ++nextpos;
        while(*nextpos == ' ' && *pos != '\0');
        pos = nextpos;
    }
    args[spaces++] = pos;
    args[spaces] = 0;
    return append_filter(f, args);
}

canonical_original_code:
struct filter *append_filter_spaces(struct filter *f, char *arg)
{
    int spaces = 0;
    char **args;
    char *tmp = strdup(arg);
    char *pos = tmp;
    char *nextpos;
    while(*pos == ' ')
        ++pos;
    while((pos = strchr(pos, ' ')) != 0)
    {
        do
            ++pos;
        while(*pos == ' ' && *pos != '\0');
        ++spaces;
    }
    ++spaces;
    ++spaces;
    args = malloc(sizeof(*args) * spaces);
    if (!args)
        fatal_error("Cannot allocate");
    spaces = 0;
    pos = tmp;
    while((nextpos = strchr(pos,' ')) != 0)
    {
        *nextpos = '\0';
        args[spaces++] = pos;
        do
            ++nextpos;
        while(*nextpos == ' ' && *pos != '\0');
        pos = nextpos;
    }
    args[spaces++] = pos;
    args[spaces] = 0;
    return append_filter(f, args);
}

variable_types:
arg
char *

variable_types: spaces int

variable_types:
nextpos
char *


decompiled_code:
void *__fastcall get_filter_extensions(__int64 a1)
{
while ( a1 )
{
if ( !*(_QWORD *)(a1 + 8) )
return (void *)(a1 + 16);
a1 = *(_QWORD *)(a1 + 8);
}
return &unk_620;
}

canonical_decompiled_code:
void *__fastcall func6(__int64 a1)
{
while ( a1 )
{
if ( !*(_QWORD *)(a1 + 8) )
return (void *)(a1 + 16);
a1 = *(_QWORD *)(a1 + 8);
}
return &unk_620;
}

original_code:
const char *
get_filter_extensions(struct filter *f)
{
    while(f != 0)
    {
        if (!f->next)
            return f->extensions;
        f = f->next;
    }
    return "";
}

canonical_original_code:
char *get_filter_extensions(struct filter *f)
{
    while(f != 0)
    {
        if (!f->next)
            return f->extensions;
        f = f->next;
    }
    return "";
}


decompiled_code:
void *__fastcall defilters_from_extensions(const char *a1)
{
char *v1; // r12
void *v2; // rbp
char *v3; // rax
char *v4; // rbx
char v6[4120]; // [rsp+0h] [rbp-1018h] BYREF

v1 = strdup(a1);
v2 = 0LL;
while ( 1 )
{
v3 = strrchr(v1, 46);
v4 = v3;
if ( !v3 || !strcmp(v3 + 1, "tar") )
break;
snprintf(v6, 0x1000uLL, "%s -d", v3 + 1);
v2 = append_filter_spaces((__int64)v2, v6);
*v4 = 0;
}
free(v1);
return v2;
}

canonical_decompiled_code:
void *__fastcall func7(const char *a1)
{
char *v1; // r12
void *v2; // rbp
char *v3; // rax
char *v4; // rbx
char v6[4120]; // [rsp+0h] [rbp-1018h] BYREF

v1 = strdup(a1);
v2 = 0LL;
while ( 1 )
{
v3 = strrchr(v1, 46);
v4 = v3;
if ( !v3 || !strcmp(v3 + 1, "tar") )
break;
snprintf(v6, 0x1000uLL, "%s -d", v3 + 1);
v2 = func5((__int64)v2, v6);
*v4 = 0;
}
free(v1);
return v2;
}

original_code:
struct filter *
defilters_from_extensions(const char *filename)
{
    struct filter *f = 0;
    char *tmp = strdup(filename);
    char *pos = tmp;
    while((pos = strrchr(tmp, '.')) != 0)
    {
        pos = pos+1;
        if (!strcmp(pos, "tar"))
            break;
        else
        {
            char command[4096];
            snprintf(command, sizeof command, "%s -d", pos);
            f = append_filter_spaces(f, command);
        }
        --pos;
        *pos = '\0';
        --pos;
    }
    free(tmp);
    return f;
}

canonical_original_code:
struct filter *defilters_from_extensions(char *filename)
{
    struct filter *f = 0;
    char *tmp = strdup(filename);
    char *pos = tmp;
    while((pos = strrchr(tmp, '.')) != 0)
    {
        pos = pos+1;
        if (!strcmp(pos, "tar"))
            break;
        else
        {
            char command[4096];
            snprintf(command, sizeof command, "%s -d", pos);
            f = append_filter_spaces(f, command);
        }
        --pos;
        *pos = '\0';
        --pos;
    }
    free(tmp);
    return f;
}

variable_types:
filename
char *

variable_types:
command
char [4096]


decompiled_code:
void __fastcall free_filters(void *ptr)
{
void ***v1; // rbx
void ***v2; // rbp

v1 = (void ***)ptr;
if ( ptr )
{
do
{
v2 = v1;
v1 = (void ***)v1[1];
free(**v2);
free(*v2);
free(v2);
}
while ( v1 );
}
}

canonical_decompiled_code:
void __fastcall func8(void *ptr)
{
void ***v1; // rbx
void ***v2; // rbp

v1 = (void ***)ptr;
if ( ptr )
{
do
{
v2 = v1;
v1 = (void ***)v1[1];
free(**v2);
free(*v2);
free(v2);
}
while ( v1 );
}
}

original_code:
void
free_filters(struct filter *f)
{
    while(f)
    {
        struct filter *next = f->next;
        free(f->args[0]);
        free(f->args);
        free(f);
        f = next;
    }
}


decompiled_code:
__int64 __fastcall block_process_can_read(__int64 *a1)
{
__int64 v1; // rbx
__int64 result; // rax

v1 = a1[3];
if ( filter )
v1 = *a1;
result = block_can_accept(v1);
if ( (_DWORD)result )
result = *(_QWORD *)(v1 + 40) < command_line[1];
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 *a1)
{
__int64 v1; // rbx
__int64 result; // rax

v1 = a1[3];
if ( filter )
v1 = *a1;
result = block_can_accept(v1);
if ( (_DWORD)result )
result = *(_QWORD *)(v1 + 40) < command_line[1];
return result;
}

original_code:
int
block_process_can_read(const struct block_process *bp)
{
    struct block *b = bp->bo;
    if (filter)
        b = bp->bi;
    if (block_can_accept(b) &&
            b->total_written < command_line.blocksize)
        return 1;
    return 0;
}

canonical_original_code:
int block_process_can_read(struct block_process *bp)
{
    struct block *b = bp->bo;
    if (filter)
        b = bp->bi;
    if (block_can_accept(b) &&
            b->total_written < command_line.blocksize)
        return 1;
    return 0;
}

variable_types: struct block_process { struct block { char *data; struct block_reader { struct block *b; unsigned long pos; } **readers; unsigned long nreaders; unsigned long writer_pos; unsigned long allocated; unsigned long total_written; struct block *nextblock; struct block *lastblock; int go_back; } *bi; struct block_reader *br_to_filter; int fd_filterin; int fd_filterout; struct block *bo; unsigned long left; int closed_in; int block_finished; int nblock; int has_read; int finished_read_ack; } *

user_defined_types: struct block_process { struct block *bi; struct block_reader *br_to_filter; int fd_filterin; int fd_filterout; struct block *bo; unsigned long left; int closed_in; int block_finished; int nblock; int has_read; int finished_read_ack; }

user_defined_types: struct block { char *data; struct block_reader **readers; unsigned long nreaders; unsigned long writer_pos; unsigned long allocated; unsigned long total_written; struct block *nextblock; struct block *lastblock; int go_back; }


decompiled_code:
__int64 __fastcall block_process_total_read(__int64 *a1)
{
__int64 v1; // rax

v1 = a1[3];
if ( filter )
v1 = *a1;
return *(_QWORD *)(v1 + 40);
}

original_code:
size_t
block_process_total_read(const struct block_process *bp)
{
    struct block *b = bp->bo;
    if (filter)
        b = bp->bi;
    return b->total_written;
}

canonical_original_code:
unsigned long block_process_total_read(struct block_process *bp)
{
    struct block *b = bp->bo;
    if (filter)
        b = bp->bi;
    return b->total_written;
}

variable_types: struct block { char *data; struct block_reader { struct block *b; unsigned long pos; } **readers; unsigned long nreaders; unsigned long writer_pos; unsigned long allocated; unsigned long total_written; struct block *nextblock; struct block *lastblock; int go_back; } *

return_type: unsigned long


decompiled_code:
_BOOL8 __fastcall block_process_finished_reading(__int64 *a1)
{
__int64 v1; // rdx
_BOOL8 result; // rax

v1 = a1[3];
if ( filter )
v1 = *a1;
result = 1LL;
if ( !*((_DWORD *)a1 + 10) )
result = *(_QWORD *)(v1 + 40) == command_line[1];
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(__int64 *a1)
{
__int64 v1; // rdx
_BOOL8 result; // rax

v1 = a1[3];
if ( filter )
v1 = *a1;
result = 1LL;
if ( !*((_DWORD *)a1 + 10) )
result = *(_QWORD *)(v1 + 40) == command_line[1];
return result;
}

original_code:
int
block_process_finished_reading(struct block_process *bp)
{
    struct block *b = bp->bo;
    if (filter)
        b = bp->bi;
    if ((bp->closed_in || b->total_written == command_line.blocksize))
        return 1;
    return 0;
}

user_defined_types: struct block_reader { struct block *b; unsigned long pos; }


decompiled_code:
__int64 __fastcall block_process_finished(__int64 *a1)
{
__int64 v1; // rdx
__int64 result; // rax

v1 = a1[3];
if ( filter )
v1 = *a1;
result = *((unsigned int *)a1 + 11);
if ( (_DWORD)result )
result = (unsigned int)block_are_readers_done(v1) != 0;
return result;
}

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

v1 = a1[3];
if ( filter )
v1 = *a1;
result = *((unsigned int *)a1 + 11);
if ( (_DWORD)result )
result = (unsigned int)block_are_readers_done(v1) != 0;
return result;
}

original_code:
int
block_process_finished(struct block_process *bp)
{
    struct block *b = bp->bo;
    if (filter)
        b = bp->bi;
    return bp->block_finished && block_are_readers_done(b);
}


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

if ( filter )
{
*(_QWORD *)(*(_QWORD *)a1 + 40LL) = 0LL;
if ( *(_QWORD *)(*(_QWORD *)a1 + 24LL) )
_assert_fail("bp->bi->writer_pos == 0", "blockprocess.c", 0x76u, "block_process_reset");
if ( *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL) )
_assert_fail("bp->br_to_filter->pos == 0", "blockprocess.c", 0x77u, "block_process_reset");
}
result = *(_QWORD *)(a1 + 24);
if ( *(_QWORD *)(result + 24) )
_assert_fail("bp->bo->writer_pos == 0", "blockprocess.c", 0x7Au, "block_process_reset");
*(_QWORD *)(result + 40) = 0LL;
*(_DWORD *)(a1 + 48) = a2;
*(_DWORD *)(a1 + 44) = 1;
*(_DWORD *)(a1 + 52) = 0;
*(_DWORD *)(a1 + 56) = 0;
return result;
}

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

if ( filter )
{
*(_QWORD *)(*(_QWORD *)a1 + 40LL) = 0LL;
if ( *(_QWORD *)(*(_QWORD *)a1 + 24LL) )
_assert_fail("bp->bi->writer_pos == 0", "blockprocess.c", 0x76u, "block_process_reset");
if ( *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL) )
_assert_fail("bp->br_to_filter->pos == 0", "blockprocess.c", 0x77u, "block_process_reset");
}
result = *(_QWORD *)(a1 + 24);
if ( *(_QWORD *)(result + 24) )
_assert_fail("bp->bo->writer_pos == 0", "blockprocess.c", 0x7Au, "block_process_reset");
*(_QWORD *)(result + 40) = 0LL;
*(_DWORD *)(a1 + 48) = a2;
*(_DWORD *)(a1 + 44) = 1;
*(_DWORD *)(a1 + 52) = 0;
*(_DWORD *)(a1 + 56) = 0;
return result;
}

original_code:
void
block_process_reset(struct block_process *bp, int nblock)
{
    if (filter)
    {
        bp->bi->total_written = 0;
        ((void) sizeof ((bp->bi->writer_pos == 0) ? 1 : 0), __extension__ ({ if (bp->bi->writer_pos == 0) ; else __assert_fail ("bp->bi->writer_pos == 0", "blockprocess.c", 118, __extension__ __PRETTY_FUNCTION__); }));
        ((void) sizeof ((bp->br_to_filter->pos == 0) ? 1 : 0), __extension__ ({ if (bp->br_to_filter->pos == 0) ; else __assert_fail ("bp->br_to_filter->pos == 0", "blockprocess.c", 119, __extension__ __PRETTY_FUNCTION__); }));
    }
    ((void) sizeof ((bp->bo->writer_pos == 0) ? 1 : 0), __extension__ ({ if (bp->bo->writer_pos == 0) ; else __assert_fail ("bp->bo->writer_pos == 0", "blockprocess.c", 122, __extension__ __PRETTY_FUNCTION__); }));
    bp->bo->total_written = 0;
    bp->nblock = nblock;
    bp->block_finished = 1;
    bp->has_read = 0;
    bp->finished_read_ack = 0;
}

variable_types: nblock int


decompiled_code:
void __fastcall prepare_readfds(__int64 a1, __int64 a2, __int64 a3, int a4)
{
if ( filter && *(int *)(a1 + 20) >= 0 && (unsigned int)block_can_accept(*(_QWORD *)(a1 + 24)) )
addfd(a2, *(unsigned int *)(a1 + 20), a3);
if ( a4 && !*(_DWORD *)(a1 + 40) )
{
if ( (unsigned int)block_process_can_read((__int64 *)a1) )
addfd(a2, 0LL, a3);
}
}

canonical_decompiled_code:
void __fastcall func5(__int64 a1, __int64 a2, __int64 a3, int a4)
{
if ( filter && *(int *)(a1 + 20) >= 0 && (unsigned int)block_can_accept(*(_QWORD *)(a1 + 24)) )
addfd(a2, *(unsigned int *)(a1 + 20), a3);
if ( a4 && !*(_DWORD *)(a1 + 40) )
{
if ( (unsigned int)func0((__int64 *)a1) )
addfd(a2, 0LL, a3);
}
}

original_code:
void
prepare_readfds(struct block_process *bp, fd_set *fdset, int *nfds, int should_read)
{
    struct block *b = bp->bo;
    if (filter)
    {
        if (bp->fd_filterout >= 0 && block_can_accept(b))
        {
            addfd(fdset, bp->fd_filterout, nfds);
        }
        b = bp->bi;
    }
    if (should_read && !bp->closed_in && block_process_can_read(bp))
        addfd(fdset, 0, nfds);
}

canonical_original_code:
void prepare_readfds(struct block_process *bp, struct fd_set *fdset, int *nfds, int should_read)
{
    struct block *b = bp->bo;
    if (filter)
    {
        if (bp->fd_filterout >= 0 && block_can_accept(b))
        {
            addfd(fdset, bp->fd_filterout, nfds);
        }
        b = bp->bi;
    }
    if (should_read && !bp->closed_in && block_process_can_read(bp))
        addfd(fdset, 0, nfds);
}

variable_types: struct fd_set { long __fds_bits[-1]; } *

variable_types:
nfds
int *


decompiled_code:
void __fastcall prepare_writefds(__int64 a1, __int64 a2, __int64 a3)
{
if ( *(int *)(a1 + 16) >= 0 )
{
if ( (unsigned int)block_reader_can_read(*(_QWORD *)(a1 + 8)) )
addfd(a2, *(unsigned int *)(a1 + 16), a3);
}
}

canonical_decompiled_code:
void __fastcall func6(__int64 a1, __int64 a2, __int64 a3)
{
if ( *(int *)(a1 + 16) >= 0 )
{
if ( (unsigned int)block_reader_can_read(*(_QWORD *)(a1 + 8)) )
addfd(a2, *(unsigned int *)(a1 + 16), a3);
}
}

original_code:
void
prepare_writefds(struct block_process *bp, fd_set *fdset, int *nfds)
{
    if (bp->fd_filterin >= 0 && block_reader_can_read(bp->br_to_filter))
    {
        addfd(fdset, bp->fd_filterin, nfds);
    }
}

canonical_original_code:
void prepare_writefds(struct block_process *bp, struct fd_set *fdset, int *nfds)
{
    if (bp->fd_filterin >= 0 && block_reader_can_read(bp->br_to_filter))
    {
        addfd(fdset, bp->fd_filterin, nfds);
    }
}


decompiled_code:
void __fastcall check_write_fds(__int64 a1, __int64 a2)
{
int v3; // esi

v3 = *(_DWORD *)(a1 + 16);
if ( v3 >= 0 && (*(_QWORD *)(a2 + 8LL * (v3 >> 6)) & (1LL << (v3 % 64))) != 0 )
{
if ( (unsigned int)block_reader_to_fd(*(_QWORD *)(a1 + 8)) == -1 && *_errno_location() != 4 )
fatal_errno("Failed write to filter");
if ( *(_QWORD *)(*(_QWORD *)a1 + 40LL) == command_line[1]
&& !(unsigned int)block_reader_can_read(*(_QWORD *)(a1 + 8))
|| *(_DWORD *)(a1 + 40) && !(unsigned int)block_reader_can_read(*(_QWORD *)(a1 + 8)) )
{
close(*(_DWORD *)(a1 + 16));
*(_DWORD *)(a1 + 16) = -1;
}
}
}

canonical_decompiled_code:
void __fastcall func8(__int64 a1, __int64 a2)
{
int v3; // esi

v3 = *(_DWORD *)(a1 + 16);
if ( v3 >= 0 && (*(_QWORD *)(a2 + 8LL * (v3 >> 6)) & (1LL << (v3 % 64))) != 0 )
{
if ( (unsigned int)block_reader_to_fd(*(_QWORD *)(a1 + 8)) == -1 && *_errno_location() != 4 )
fatal_errno("Failed write to filter");
if ( *(_QWORD *)(*(_QWORD *)a1 + 40LL) == command_line[1]
&& !(unsigned int)block_reader_can_read(*(_QWORD *)(a1 + 8))
|| *(_DWORD *)(a1 + 40) && !(unsigned int)block_reader_can_read(*(_QWORD *)(a1 + 8)) )
{
close(*(_DWORD *)(a1 + 16));
*(_DWORD *)(a1 + 16) = -1;
}
}
}

original_code:
void
check_write_fds(struct block_process *bp, fd_set *writefds)
{
    if (bp->fd_filterin >= 0 && ((((writefds)->__fds_bits)[((bp->fd_filterin) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((bp->fd_filterin) % (8 * (int) sizeof (__fd_mask)))))) != 0))
    {
        int nwritten = block_reader_to_fd(bp->br_to_filter, bp->fd_filterin);
        if (nwritten == -1 && (*__errno_location ()) != 4)
            fatal_errno("Failed write to filter");
        if (bp->bi->total_written == command_line.blocksize &&
                !block_reader_can_read(bp->br_to_filter))
        {
            close(bp->fd_filterin);
            bp->fd_filterin = -1;
        }
        else if(bp->closed_in && !block_reader_can_read(bp->br_to_filter))
        {
            close(bp->fd_filterin);
            bp->fd_filterin = -1;
        }
    }
}

canonical_original_code:
void check_write_fds(struct block_process *bp, struct fd_set *writefds)
{
    if (bp->fd_filterin >= 0 && ((((writefds)->__fds_bits)[((bp->fd_filterin) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((bp->fd_filterin) % (8 * (int) sizeof (__fd_mask)))))) != 0))
    {
        int nwritten = block_reader_to_fd(bp->br_to_filter, bp->fd_filterin);
        if (nwritten == -1 && (*__errno_location ()) != 4)
            fatal_errno("Failed write to filter");
        if (bp->bi->total_written == command_line.blocksize &&
                !block_reader_can_read(bp->br_to_filter))
        {
            close(bp->fd_filterin);
            bp->fd_filterin = -1;
        }
        else if(bp->closed_in && !block_reader_can_read(bp->br_to_filter))
        {
            close(bp->fd_filterin);
            bp->fd_filterin = -1;
        }
    }
}

variable_types: nwritten int


decompiled_code:
unsigned __int64 __fastcall xor_to_xorblock(__int64 a1, _QWORD *a2)
{
unsigned __int64 v2; // rbx
__int64 v3; // rax
__int64 i; // rax
unsigned __int64 result; // rax

v2 = *(_QWORD *)(*(_QWORD *)(a1 + 24) + 24LL);
v3 = a2[4];
if ( !v3 )
_assert_fail("oldsize > 0", "blockprocess.c", 0x115u, "xor_to_xorblock");
block_realloc_set(a2, v2 + 1, (unsigned int)*(char *)(*a2 + v3 - 1));
if ( v2 )
{
for ( i = 0LL; i != v2; ++i )
*(_BYTE *)(*a2 + i) ^= *(_BYTE *)(**(_QWORD **)(a1 + 24) + i);
}
result = a2[4];
if ( v2 < result )
{
do
++v2;
while ( v2 != result );
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func9(__int64 a1, _QWORD *a2)
{
unsigned __int64 v2; // rbx
__int64 v3; // rax
__int64 i; // rax
unsigned __int64 result; // rax

v2 = *(_QWORD *)(*(_QWORD *)(a1 + 24) + 24LL);
v3 = a2[4];
if ( !v3 )
_assert_fail("oldsize > 0", "blockprocess.c", 0x115u, "xor_to_xorblock");
block_realloc_set(a2, v2 + 1, (unsigned int)*(char *)(*a2 + v3 - 1));
if ( v2 )
{
for ( i = 0LL; i != v2; ++i )
*(_BYTE *)(*a2 + i) ^= *(_BYTE *)(**(_QWORD **)(a1 + 24) + i);
}
result = a2[4];
if ( v2 < result )
{
do
++v2;
while ( v2 != result );
}
return result;
}

original_code:
void
xor_to_xorblock(struct block_process *bp, struct block *xorblock)
{
    size_t mysize = bp->bo->writer_pos;
    size_t xorsize = mysize + 1 ;
    size_t oldsize = xorblock->allocated;
    size_t i;
    char expand_byte;
    ((void) sizeof ((oldsize > 0) ? 1 : 0), __extension__ ({ if (oldsize > 0) ; else __assert_fail ("oldsize > 0", "blockprocess.c", 277, __extension__ __PRETTY_FUNCTION__); }));
    expand_byte = xorblock->data[oldsize-1];
    block_realloc_set(xorblock, xorsize, expand_byte);
    for(i=0; i < mysize; ++i)
        xorblock->data[i] ^= bp->bo->data[i];
    for(i=mysize; i < xorblock->allocated; ++i)
        xorblock->data[i] ^= 0;
}

canonical_original_code:
void xor_to_xorblock(struct block_process *bp, struct block *xorblock)
{
    unsigned long mysize = bp->bo->writer_pos;
    unsigned long xorsize = mysize + 1;
    unsigned long oldsize = xorblock->allocated;
    unsigned long i;
    char expand_byte;
    ((void) sizeof ((oldsize > 0) ? 1 : 0), __extension__ ({ if (oldsize > 0) ; else __assert_fail ("oldsize > 0", "blockprocess.c", 277, __extension__ __PRETTY_FUNCTION__); }));
    expand_byte = xorblock->data[oldsize-1];
    block_realloc_set(xorblock, xorsize, expand_byte);
    for(i=0; i < mysize; ++i)
        xorblock->data[i] ^= bp->bo->data[i];
    for(i=mysize; i < xorblock->allocated; ++i)
        xorblock->data[i] ^= 0;
}

variable_types:
xorblock
struct block *

variable_types:
mysize
unsigned long

variable_types:
xorsize
unsigned long

variable_types:
oldsize
unsigned long

variable_types:
i
unsigned long