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

result = *gconf;
if ( *gconf >= g_announces_expire )
{
announces_expire();
g_announces_expire = time_add_mins(1LL);
result = *gconf;
if ( g_announces_announce > *gconf )
return result;
}
else if ( g_announces_announce > result )
{
return result;
}
result = kad_count_nodes(0LL);
if ( (_DWORD)result )
{
announces_announce();
result = time_add_mins(1LL);
g_announces_announce = result;
}
return result;
}

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

result = *gconf;
if ( *gconf >= g_announces_expire )
{
func4();
g_announces_expire = time_add_mins(1LL);
result = *gconf;
if ( g_announces_announce > *gconf )
return result;
}
else if ( g_announces_announce > result )
{
return result;
}
result = kad_count_nodes(0LL);
if ( (_DWORD)result )
{
func0();
result = time_add_mins(1LL);
g_announces_announce = result;
}
return result;
}

original_code:
static void announces_handle(int _rc, int _sock)
{
 if (g_announces_expire <= (gconf->time_now)) {
  announces_expire();
  g_announces_expire = time_add_mins(1);
 }
 if (g_announces_announce <= (gconf->time_now) && kad_count_nodes(0) != 0) {
  announces_announce();
  g_announces_announce = time_add_mins(1);
 }
}


repo: mwarning/KadNode

decompiled_code:
__int64 announces_setup()
{
return net_add_handler(0xFFFFFFFFLL, announces_handle);
}

canonical_decompiled_code:
__int64 func6()
{
return net_add_handler(0xFFFFFFFFLL, announces_handle);
}

original_code:
void announces_setup(void)
{
 net_add_handler(-1, &announces_handle);
}

canonical_original_code:
void announces_setup()
{
 net_add_handler(-1, &announces_handle);
}

return_type: void


decompiled_code:
void announces_free()
{
_QWORD *v0; // rbx
void *v1; // rdi

v0 = g_values;
if ( g_values )
{
do
{
v1 = v0;
v0 = (_QWORD *)*v0;
value_free(v1);
}
while ( v0 );
}
g_values = 0LL;
}

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

v0 = g_values;
if ( g_values )
{
do
{
v1 = v0;
v0 = (_QWORD *)*v0;
func3(v1);
}
while ( v0 );
}
g_values = 0LL;
}

original_code:
void announces_free(void)
{
 struct value_t *cur;
 struct value_t *next;
 cur = g_values;
 while (cur) {
  next = cur->next;
  value_free(cur);
  cur = next;
 }
 g_values = ((void *)0);
}

canonical_original_code:
void announces_free()
{
 struct value_t *cur;
 struct value_t *next;
 cur = g_values;
 while (cur) {
  next = cur->next;
  value_free(cur);
  cur = next;
 }
 g_values = ((void *)0);
}

variable_types: struct value_t { struct value_t *next; unsigned char id[20]; char query[256]; int port; long lifetime; long refresh; } *


decompiled_code:
_BOOL8 __fastcall tls_client_get_id(void *s, size_t n, char *a3)
{
unsigned int v4; // ebx
char *v5; // r8
_BOOL8 result; // rax
size_t v7; // rax
int v8; // er12
int v9; // ebp
unsigned __int64 v10; // rcx
unsigned int v11; // ebx
unsigned int v12; // ebx
unsigned int v13; // eax
__int64 v14; // rdx
__int64 v15[4]; // [rsp+0h] [rbp-B8h] BYREF
char v16[152]; // [rsp+20h] [rbp-98h] BYREF

v4 = n;
v5 = strchr(a3, 46);
result = 1LL;
if ( v5 )
{
mbedtls_sha256_init(v16);
v7 = strlen(a3);
v8 = mbedtls_sha256_update_ret(v16, a3, v7);
v9 = mbedtls_sha256_finish_ret(v16, v15);
memset(s, 0, n);
if ( n > 0x20 )
v4 = 32;
if ( v4 >= 8 )
{
v10 = ((unsigned __int64)s + 8) & 0xFFFFFFFFFFFFFFF8LL;
*(_QWORD *)s = v15[0];
*(_QWORD *)((char *)s + v4 - 8) = *(__int64 *)((char *)&v15[-1] + v4);
v11 = ((_DWORD)s - v10 + v4) & 0xFFFFFFF8;
if ( v11 >= 8 )
{
v12 = v11 & 0xFFFFFFF8;
v13 = 0;
do
{
v14 = v13;
v13 += 8;
*(_QWORD *)(v10 + v14) = *(_QWORD *)((char *)v15 - ((char *)s - v10) + v14);
}
while ( v13 < v12 );
}
}
else if ( (v4 & 4) != 0 )
{
*(_DWORD *)s = v15[0];
*(_DWORD *)((char *)s + v4 - 4) = *(_DWORD *)((char *)v15 + v4 - 4);
}
else if ( v4 )
{
*(_BYTE *)s = v15[0];
if ( (v4 & 2) != 0 )
*(_WORD *)((char *)s + v4 - 2) = *(_WORD *)((char *)v15 + v4 - 2);
}
result = (v9 | v8) != 0;
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func7(void *s, size_t n, char *a3)
{
unsigned int v4; // ebx
char *v5; // r8
_BOOL8 result; // rax
size_t v7; // rax
int v8; // er12
int v9; // ebp
unsigned __int64 v10; // rcx
unsigned int v11; // ebx
unsigned int v12; // ebx
unsigned int v13; // eax
__int64 v14; // rdx
__int64 v15[4]; // [rsp+0h] [rbp-B8h] BYREF
char v16[152]; // [rsp+20h] [rbp-98h] BYREF

v4 = n;
v5 = strchr(a3, 46);
result = 1LL;
if ( v5 )
{
mbedtls_sha256_init(v16);
v7 = strlen(a3);
v8 = mbedtls_sha256_update_ret(v16, a3, v7);
v9 = mbedtls_sha256_finish_ret(v16, v15);
memset(s, 0, n);
if ( n > 0x20 )
v4 = 32;
if ( v4 >= 8 )
{
v10 = ((unsigned __int64)s + 8) & 0xFFFFFFFFFFFFFFF8LL;
*(_QWORD *)s = v15[0];
*(_QWORD *)((char *)s + v4 - 8) = *(__int64 *)((char *)&v15[-1] + v4);
v11 = ((_DWORD)s - v10 + v4) & 0xFFFFFFF8;
if ( v11 >= 8 )
{
v12 = v11 & 0xFFFFFFF8;
v13 = 0;
do
{
v14 = v13;
v13 += 8;
*(_QWORD *)(v10 + v14) = *(_QWORD *)((char *)v15 - ((char *)s - v10) + v14);
}
while ( v13 < v12 );
}
}
else if ( (v4 & 4) != 0 )
{
*(_DWORD *)s = v15[0];
*(_DWORD *)((char *)s + v4 - 4) = *(_DWORD *)((char *)v15 + v4 - 4);
}
else if ( v4 )
{
*(_BYTE *)s = v15[0];
if ( (v4 & 2) != 0 )
*(_WORD *)((char *)s + v4 - 2) = *(_WORD *)((char *)v15 + v4 - 2);
}
result = (v9 | v8) != 0;
}
return result;
}

original_code:
int tls_client_get_id(uint8_t id[], size_t len, const char query[])
{
 uint8_t hash[32];
 int ret = 0;
 if (strchr(query, '.')) {
  mbedtls_sha256_context ctx;
  mbedtls_sha256_init(&ctx);
  ret |= mbedtls_sha256_update_ret(&ctx, (uint8_t*) &query[0], strlen(query));
  ret |= mbedtls_sha256_finish_ret(&ctx, hash);
  memset(id, 0, len);
  memcpy(id, hash, ((len) <= (sizeof(hash)) ? (len) : (sizeof(hash))));
  return (ret == 0) ? 0 : 1;
 }
 return 1;
}

canonical_original_code:
int tls_client_get_id(unsigned char id[], unsigned long len, char query[])
{
 unsigned char hash[32];
 int ret = 0;
 if (strchr(query, '.')) {
  struct mbedtls_sha256_context ctx;
  mbedtls_sha256_init(&ctx);
  ret |= mbedtls_sha256_update_ret(&ctx, (unsigned char *) &query[0], strlen(query));
  ret |= mbedtls_sha256_finish_ret(&ctx, hash);
  memset(id, 0, len);
  memcpy(id, hash, ((len) <= (sizeof(hash)) ? (len) : (sizeof(hash))));
  return (ret == 0) ? 0 : 1;
 }
 return 1;
}

variable_types:
id
unsigned char []

variable_types:
len
unsigned long

variable_types:
query
char []

variable_types:
hash
unsigned char [32]

variable_types: ret int

variable_types: struct mbedtls_sha256_context { unsigned int total[2]; unsigned int state[8]; unsigned char buffer[64]; int is224; }

return_type: int


decompiled_code:
__int64 __fastcall tls_client_add_ca(const char *a1)
{
int v1; // eax
char v3[120]; // [rsp+0h] [rbp-78h] BYREF

if ( !g_client_enable )
{
mbedtls_x509_crt_init(&g_cacert);
g_client_enable = 1;
}
v1 = mbedtls_x509_crt_parse_file(&g_cacert, a1);
if ( v1 < 0 )
{
v1 = mbedtls_x509_crt_parse_path(&g_cacert, a1);
if ( v1 < 0 )
{
mbedtls_strerror((unsigned int)v1, v3, 100LL);
if ( *(_DWORD *)(gconf + 60LL) != 2 )
log_print(4LL, "TLS-Client: Failed to load CA root certificates from %s (%s)", a1, v3);
return 0LL;
}
}
if ( *(_DWORD *)(gconf + 60LL) == 2 )
return 0LL;
log_print(6LL, "TLS-Client: Loaded certificates from %s (%d skipped)", a1, (unsigned int)v1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(const char *a1)
{
int v1; // eax
char v3[120]; // [rsp+0h] [rbp-78h] BYREF

if ( !g_client_enable )
{
mbedtls_x509_crt_init(&g_cacert);
g_client_enable = 1;
}
v1 = mbedtls_x509_crt_parse_file(&g_cacert, a1);
if ( v1 < 0 )
{
v1 = mbedtls_x509_crt_parse_path(&g_cacert, a1);
if ( v1 < 0 )
{
mbedtls_strerror((unsigned int)v1, v3, 100LL);
if ( *(_DWORD *)(gconf + 60LL) != 2 )
log_print(4LL, "TLS-Client: Failed to load CA root certificates from %s (%s)", a1, v3);
return 0LL;
}
}
if ( *(_DWORD *)(gconf + 60LL) == 2 )
return 0LL;
log_print(6LL, "TLS-Client: Loaded certificates from %s (%d skipped)", a1, (unsigned int)v1);
return 0LL;
}

original_code:
int tls_client_add_ca(const char path[])
{
 char error_buf[100];
 int ret;
 if (g_client_enable == 0) {
  mbedtls_x509_crt_init(&g_cacert);
  g_client_enable = 1;
 }
 if (((ret = mbedtls_x509_crt_parse_file(&g_cacert, path)) < 0) &&
  ((ret = mbedtls_x509_crt_parse_path(&g_cacert, path)) < 0)) {
  mbedtls_strerror(ret, error_buf, sizeof(error_buf));
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(4, "TLS-Client: Failed to load CA root certificates from %s (%s)", path, error_buf); } while (0);
  return 0;
 }
 do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(6, "TLS-Client: Loaded certificates from %s (%d skipped)", path, ret); } while (0);
 return 0;
}

canonical_original_code:
int tls_client_add_ca(char path[])
{
 char error_buf[100];
 int ret;
 if (g_client_enable == 0) {
  mbedtls_x509_crt_init(&g_cacert);
  g_client_enable = 1;
 }
 if (((ret = mbedtls_x509_crt_parse_file(&g_cacert, path)) < 0) &&
  ((ret = mbedtls_x509_crt_parse_path(&g_cacert, path)) < 0)) {
  mbedtls_strerror(ret, error_buf, sizeof(error_buf));
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(4, "TLS-Client: Failed to load CA root certificates from %s (%s)", path, error_buf); } while (0);
  return 0;
 }
 do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(6, "TLS-Client: Loaded certificates from %s (%d skipped)", path, ret); } while (0);
 return 0;
}

variable_types:
path
char []


decompiled_code:
__int64 tls_client_setup()
{
int v0; // eax
unsigned int v1; // eax
unsigned int v2; // er12
int v3; // eax
__int64 result; // rax

if ( !g_client_enable )
return 0;
if ( dword_A930 <= 0 )
{
log_print(3LL, "TLS-Client: No root CA certificates could be loaded.");
result = 1LL;
}
else
{
mbedtls_ctr_drbg_init(&g_drbg);
mbedtls_entropy_init(&g_entropy);
v0 = mbedtls_ctr_drbg_seed(&g_drbg, &mbedtls_entropy_func, &g_entropy, "kadnode", 7LL);
if ( v0 )
{
v2 = 1;
log_print(3LL, "TLS-Client: mbedtls_ctr_drbg_seed returned -0x%x", (unsigned int)-v0);
return v2;
}
mbedtls_ssl_init(&g_tls_resources);
mbedtls_net_init(&dword_C88);
mbedtls_ssl_init(&unk_E10);
mbedtls_net_init(&dword_FD8);
mbedtls_ssl_config_init(&g_conf);
v1 = mbedtls_ssl_config_defaults(&g_conf, 0LL, 0LL, 0LL);
v2 = v1;
if ( !v1 )
{
mbedtls_ssl_conf_verify(&g_conf, tls_conf_verify, 0LL);
mbedtls_ssl_conf_rng(&g_conf, &mbedtls_ctr_drbg_random, &g_drbg);
mbedtls_ssl_conf_read_timeout(&g_conf, 0LL);
mbedtls_ssl_conf_ca_chain(&g_conf, &g_cacert, 0LL);
v3 = mbedtls_ssl_setup(&g_tls_resources, &g_conf);
if ( v3 || (v3 = mbedtls_ssl_setup(&unk_E10, &g_conf)) != 0 )
{
log_print(3LL, "TLS-Client: mbedtls_ssl_setup returned -0x%x", (unsigned int)-v3);
return v2;
}
return 0;
}
log_print(3LL, "TLS-Client: mbedtls_ssl_config_defaults returned -0x%x", -v1);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 func9()
{
int v0; // eax
unsigned int v1; // eax
unsigned int v2; // er12
int v3; // eax
__int64 result; // rax

if ( !g_client_enable )
return 0;
if ( dword_A930 <= 0 )
{
log_print(3LL, "TLS-Client: No root CA certificates could be loaded.");
result = 1LL;
}
else
{
mbedtls_ctr_drbg_init(&g_drbg);
mbedtls_entropy_init(&g_entropy);
v0 = mbedtls_ctr_drbg_seed(&g_drbg, &mbedtls_entropy_func, &g_entropy, "kadnode", 7LL);
if ( v0 )
{
v2 = 1;
log_print(3LL, "TLS-Client: mbedtls_ctr_drbg_seed returned -0x%x", (unsigned int)-v0);
return v2;
}
mbedtls_ssl_init(&g_tls_resources);
mbedtls_net_init(&dword_C88);
mbedtls_ssl_init(&unk_E10);
mbedtls_net_init(&dword_FD8);
mbedtls_ssl_config_init(&g_conf);
v1 = mbedtls_ssl_config_defaults(&g_conf, 0LL, 0LL, 0LL);
v2 = v1;
if ( !v1 )
{
mbedtls_ssl_conf_verify(&g_conf, tls_conf_verify, 0LL);
mbedtls_ssl_conf_rng(&g_conf, &mbedtls_ctr_drbg_random, &g_drbg);
mbedtls_ssl_conf_read_timeout(&g_conf, 0LL);
mbedtls_ssl_conf_ca_chain(&g_conf, &g_cacert, 0LL);
v3 = mbedtls_ssl_setup(&g_tls_resources, &g_conf);
if ( v3 || (v3 = mbedtls_ssl_setup(&unk_E10, &g_conf)) != 0 )
{
log_print(3LL, "TLS-Client: mbedtls_ssl_setup returned -0x%x", (unsigned int)-v3);
return v2;
}
return 0;
}
log_print(3LL, "TLS-Client: mbedtls_ssl_config_defaults returned -0x%x", -v1);
result = 1LL;
}
return result;
}

original_code:
int tls_client_setup(void)
{
 const char *pers = "kadnode";
 int ret;
 int i;
 if (g_client_enable == 0) {
  return 0;
 }
 if (g_cacert.version <= 0) {
  log_print(3, "TLS-Client: No root CA certificates could be loaded.");;
  return 1;
 }
 mbedtls_ctr_drbg_init(&g_drbg);
 mbedtls_entropy_init(&g_entropy);
 if ((ret = mbedtls_ctr_drbg_seed(&g_drbg, mbedtls_entropy_func, &g_entropy,
  (const unsigned char *) pers, strlen(pers))) != 0) {
  log_print(3, "TLS-Client: mbedtls_ctr_drbg_seed returned -0x%x", -ret);;
  return 1;
 }
 for (i = 0; i < (sizeof(g_tls_resources) / sizeof((g_tls_resources)[0])); ++i) {
  mbedtls_ssl_init(&g_tls_resources[i].ssl);
  mbedtls_net_init(&g_tls_resources[i].fdc);
 }
 mbedtls_ssl_config_init(&g_conf);
 if ((ret = mbedtls_ssl_config_defaults(&g_conf,
  0,
  0,
  0)) != 0) {
  log_print(3, "TLS-Client: mbedtls_ssl_config_defaults returned -0x%x", -ret);;
  return 1;
 }
 mbedtls_ssl_conf_verify(&g_conf, tls_conf_verify, ((void *)0));
 mbedtls_ssl_conf_rng(&g_conf, mbedtls_ctr_drbg_random, &g_drbg);
 mbedtls_ssl_conf_read_timeout(&g_conf, 0);
 mbedtls_ssl_conf_ca_chain(&g_conf, &g_cacert, ((void *)0));
 for (i = 0; i < (sizeof(g_tls_resources) / sizeof((g_tls_resources)[0])); ++i) {
  if ((ret = mbedtls_ssl_setup(&g_tls_resources[i].ssl, &g_conf)) != 0) {
   log_print(3, "TLS-Client: mbedtls_ssl_setup returned -0x%x", -ret);;
   return 0;
  }
 }
 return 0;
}

canonical_original_code:
int tls_client_setup()
{
 char *pers = "kadnode";
 int ret;
 int i;
 if (g_client_enable == 0) {
  return 0;
 }
 if (g_cacert.version <= 0) {
  log_print(3, "TLS-Client: No root CA certificates could be loaded.");;
  return 1;
 }
 mbedtls_ctr_drbg_init(&g_drbg);
 mbedtls_entropy_init(&g_entropy);
 if ((ret = mbedtls_ctr_drbg_seed(&g_drbg, mbedtls_entropy_func, &g_entropy,
  (unsigned char *) pers, strlen(pers))) != 0) {
  log_print(3, "TLS-Client: mbedtls_ctr_drbg_seed returned -0x%x", -ret);;
  return 1;
 }
 for (i = 0; i < (sizeof(g_tls_resources) / sizeof((g_tls_resources)[0])); ++i) {
  mbedtls_ssl_init(&g_tls_resources[i].ssl);
  mbedtls_net_init(&g_tls_resources[i].fdc);
 }
 mbedtls_ssl_config_init(&g_conf);
 if ((ret = mbedtls_ssl_config_defaults(&g_conf,
  0,
  0,
  0)) != 0) {
  log_print(3, "TLS-Client: mbedtls_ssl_config_defaults returned -0x%x", -ret);;
  return 1;
 }
 mbedtls_ssl_conf_verify(&g_conf, tls_conf_verify, ((void *)0));
 mbedtls_ssl_conf_rng(&g_conf, mbedtls_ctr_drbg_random, &g_drbg);
 mbedtls_ssl_conf_read_timeout(&g_conf, 0);
 mbedtls_ssl_conf_ca_chain(&g_conf, &g_cacert, ((void *)0));
 for (i = 0; i < (sizeof(g_tls_resources) / sizeof((g_tls_resources)[0])); ++i) {
  if ((ret = mbedtls_ssl_setup(&g_tls_resources[i].ssl, &g_conf)) != 0) {
   log_print(3, "TLS-Client: mbedtls_ssl_setup returned -0x%x", -ret);;
   return 0;
  }
 }
 return 0;
}

variable_types:
pers
char *

variable_types: i int


decompiled_code:
__int64 tls_client_free()
{
mbedtls_ssl_free(&g_tls_resources);
mbedtls_net_free(&dword_C88);
mbedtls_ssl_free(&unk_E10);
mbedtls_net_free(&dword_FD8);
mbedtls_x509_crt_free(&g_cacert);
mbedtls_ssl_config_free(&g_conf);
mbedtls_entropy_free(&g_entropy);
return mbedtls_ctr_drbg_free(&g_drbg);
}

canonical_decompiled_code:
__int64 func10()
{
mbedtls_ssl_free(&g_tls_resources);
mbedtls_net_free(&dword_C88);
mbedtls_ssl_free(&unk_E10);
mbedtls_net_free(&dword_FD8);
mbedtls_x509_crt_free(&g_cacert);
mbedtls_ssl_config_free(&g_conf);
mbedtls_entropy_free(&g_entropy);
return mbedtls_ctr_drbg_free(&g_drbg);
}

original_code:
void tls_client_free(void)
{
 int i;
 for (i = 0; i < (sizeof(g_tls_resources) / sizeof((g_tls_resources)[0])); ++i) {
  mbedtls_ssl_free(&g_tls_resources[i].ssl);
  mbedtls_net_free(&g_tls_resources[i].fdc);
 }
 mbedtls_x509_crt_free(&g_cacert);
 mbedtls_ssl_config_free(&g_conf);
 mbedtls_entropy_free(&g_entropy);
 mbedtls_ctr_drbg_free(&g_drbg);
}

canonical_original_code:
void tls_client_free()
{
 int i;
 for (i = 0; i < (sizeof(g_tls_resources) / sizeof((g_tls_resources)[0])); ++i) {
  mbedtls_ssl_free(&g_tls_resources[i].ssl);
  mbedtls_net_free(&g_tls_resources[i].fdc);
 }
 mbedtls_x509_crt_free(&g_cacert);
 mbedtls_ssl_config_free(&g_conf);
 mbedtls_entropy_free(&g_entropy);
 mbedtls_ctr_drbg_free(&g_drbg);
}


decompiled_code:
char *log_time()
{
int v0; // eax
int v1; // edx
struct timespec v3; // [rsp+0h] [rbp-18h] BYREF

v3.tv_sec = 0LL;
v3.tv_nsec = 0LL;
clock_gettime(1, &v3);
v0 = log_start.tv_sec;
v1 = log_start.tv_nsec;
if ( log_start == 0uLL )
{
clock_gettime(1, &log_start);
v0 = log_start.tv_sec;
v1 = log_start.tv_nsec;
}
sprintf(
buf_0,
"[%8.2f] ",
(double)SLODWORD(v3.tv_nsec) * 0.000000001 + (double)SLODWORD(v3.tv_sec) - ((double)v1 * 0.000000001 + (double)v0));
return buf_0;
}

canonical_decompiled_code:
char *func0()
{
int v0; // eax
int v1; // edx
struct timespec v3; // [rsp+0h] [rbp-18h] BYREF

v3.tv_sec = 0LL;
v3.tv_nsec = 0LL;
clock_gettime(1, &v3);
v0 = log_start.tv_sec;
v1 = log_start.tv_nsec;
if ( log_start == 0uLL )
{
clock_gettime(1, &log_start);
v0 = log_start.tv_sec;
v1 = log_start.tv_nsec;
}
sprintf(
buf_0,
"[%8.2f] ",
(double)SLODWORD(v3.tv_nsec) * 0.000000001 + (double)SLODWORD(v3.tv_sec) - ((double)v1 * 0.000000001 + (double)v0));
return buf_0;
}

original_code:
const char *log_time()
{
 static char buf[16];
 struct timespec now = { 0, 0 };
 clock_gettime(1, &now);
 if (log_start.tv_sec == 0 && log_start.tv_nsec == 0) {
  clock_gettime(1, &log_start);
 }
 sprintf(buf, "[%8.2f] ",
  ((double) now.tv_sec + 1.0e-9 * (double)now.tv_nsec) -
  ((double) log_start.tv_sec + 1.0e-9 * (double)log_start.tv_nsec)
 );
 return buf;
}

variable_types:
buf
char [16]

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

return_type: char *


decompiled_code:
__int64 __fastcall peerfile_import_peer(const char *a1)
{
int v1; // eax
unsigned int v3; // ebx
int *v4; // rax
char *v5; // rax
int *v6; // rax
char *v7; // rax
_BYTE savedregs[152]; // [rsp+0h] [rbp+0h] BYREF

memset(savedregs, 0, 0x80uLL);
v1 = *(_DWORD *)(gconf + 68LL);
if ( v1 && v1 != 10 )
{
if ( v1 != 2 )
{
if ( *(_DWORD *)(gconf + 60LL) == 2 )
return 0LL;
LABEL_14:
v6 = _errno_location();
v7 = strerror(*v6);
log_print(4LL, "PEERFILE: Cannot parse/resolve address '%s': %s", a1, v7);
return 0LL;
}
LABEL_8:
if ( (unsigned __int8)addr_parse(savedregs, a1, "6881", 2LL) )
{
LABEL_9:
if ( (unsigned __int8)kad_ping(savedregs) )
return 1LL;
goto LABEL_12;
}
LABEL_15:
if ( *(_DWORD *)(gconf + 60LL) == 2 )
return 0LL;
goto LABEL_14;
}
v3 = v1 & 0xFFFFFFFD;
if ( !(unsigned __int8)addr_parse(savedregs, a1, "6881", 10LL) )
{
if ( v3 )
goto LABEL_15;
goto LABEL_8;
}
if ( (unsigned __int8)kad_ping(savedregs) )
{
if ( !v3 )
{
if ( (unsigned __int8)addr_parse(savedregs, a1, "6881", 2LL) )
kad_ping(savedregs);
}
return 1LL;
}
if ( !v3 && (unsigned __int8)addr_parse(savedregs, a1, "6881", 2LL) )
goto LABEL_9;
LABEL_12:
if ( *(_DWORD *)(gconf + 60LL) == 2 )
return 0LL;
v4 = _errno_location();
v5 = strerror(*v4);
log_print(4LL, "PEERFILE: Cannot ping address '%s': %s", a1, v5);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
int v1; // eax
unsigned int v3; // ebx
int *v4; // rax
char *v5; // rax
int *v6; // rax
char *v7; // rax
_BYTE savedregs[152]; // [rsp+0h] [rbp+0h] BYREF

memset(savedregs, 0, 0x80uLL);
v1 = *(_DWORD *)(gconf + 68LL);
if ( v1 && v1 != 10 )
{
if ( v1 != 2 )
{
if ( *(_DWORD *)(gconf + 60LL) == 2 )
return 0LL;
LABEL_14:
v6 = _errno_location();
v7 = strerror(*v6);
log_print(4LL, "PEERFILE: Cannot parse/resolve address '%s': %s", a1, v7);
return 0LL;
}
LABEL_8:
if ( (unsigned __int8)addr_parse(savedregs, a1, "6881", 2LL) )
{
LABEL_9:
if ( (unsigned __int8)kad_ping(savedregs) )
return 1LL;
goto LABEL_12;
}
LABEL_15:
if ( *(_DWORD *)(gconf + 60LL) == 2 )
return 0LL;
goto LABEL_14;
}
v3 = v1 & 0xFFFFFFFD;
if ( !(unsigned __int8)addr_parse(savedregs, a1, "6881", 10LL) )
{
if ( v3 )
goto LABEL_15;
goto LABEL_8;
}
if ( (unsigned __int8)kad_ping(savedregs) )
{
if ( !v3 )
{
if ( (unsigned __int8)addr_parse(savedregs, a1, "6881", 2LL) )
kad_ping(savedregs);
}
return 1LL;
}
if ( !v3 && (unsigned __int8)addr_parse(savedregs, a1, "6881", 2LL) )
goto LABEL_9;
LABEL_12:
if ( *(_DWORD *)(gconf + 60LL) == 2 )
return 0LL;
v4 = _errno_location();
v5 = strerror(*v4);
log_print(4LL, "PEERFILE: Cannot ping address '%s': %s", a1, v5);
return 0LL;
}

original_code:
static int peerfile_import_peer(const char addr_str[])
{
 const char *port_str = "6881";
 _Bool parsed = 0;
 _Bool pinged = 0;
 IP addr = {0};
 int af = gconf->af;
 if (af == 0 || af == 10) {
  if (addr_parse(&addr, addr_str, port_str, 10)) {
   parsed = 1;
   if (kad_ping(&addr)) {
    pinged = 1;
   }
  }
 }
 if (af == 0 || af == 2) {
  if (addr_parse(&addr, addr_str, port_str, 2)) {
   parsed = 1;
   if (kad_ping(&addr)) {
    pinged = 1;
   }
  }
 }
 if (!parsed) {
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(4, "PEERFILE: Cannot parse/resolve address '%s': %s", addr_str, strerror((*__errno_location ()))); } while (0);
  return 0;
 }
 if (!pinged) {
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(4, "PEERFILE: Cannot ping address '%s': %s", addr_str, strerror((*__errno_location ()))); } while (0);
  return 0;
 }
 return 1;
}

canonical_original_code:
static int peerfile_import_peer(char addr_str[])
{
 char *port_str = "6881";
 _Bool parsed = 0;
 _Bool pinged = 0;
 struct sockaddr_storage addr = {0};
 int af = gconf->af;
 if (af == 0 || af == 10) {
  if (addr_parse(&addr, addr_str, port_str, 10)) {
   parsed = 1;
   if (kad_ping(&addr)) {
    pinged = 1;
   }
  }
 }
 if (af == 0 || af == 2) {
  if (addr_parse(&addr, addr_str, port_str, 2)) {
   parsed = 1;
   if (kad_ping(&addr)) {
    pinged = 1;
   }
  }
 }
 if (!parsed) {
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(4, "PEERFILE: Cannot parse/resolve address '%s': %s", addr_str, strerror((*__errno_location ()))); } while (0);
  return 0;
 }
 if (!pinged) {
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(4, "PEERFILE: Cannot ping address '%s': %s", addr_str, strerror((*__errno_location ()))); } while (0);
  return 0;
 }
 return 1;
}

variable_types: struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; }

variable_types: af int


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

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

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

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

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


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

result = *gconf;
if ( *gconf < peerfile_import_time )
{
if ( peerfile_export_time > result )
return result;
goto LABEL_7;
}
if ( !(unsigned int)kad_count_nodes(0LL) )
{
peerfile_import();
peerfile_import_static((__int64 **)g_peers);
peerfile_import_time = time_add_mins(5LL);
}
result = *gconf;
if ( peerfile_export_time <= *gconf )
{
LABEL_7:
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

result = *gconf;
if ( *gconf < peerfile_import_time )
{
if ( peerfile_export_time > result )
return result;
goto LABEL_7;
}
if ( !(unsigned int)kad_count_nodes(0LL) )
{
func1();
func2((__int64 **)g_peers);
peerfile_import_time = time_add_mins(5LL);
}
result = *gconf;
if ( peerfile_export_time <= *gconf )
{
LABEL_7:
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);
 }
}


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

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

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

v1 = malloc(0x10uLL);
v1[1] = strdup(s);
v2 = g_peers;
g_peers = v1;
*v1 = v2;
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;
}

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 v6; // r15
__int64 v9; // r12
unsigned __int64 v10; // rax
unsigned int v11; // eax
int v13; // ebx
unsigned int v15; // [rsp+0h] [rbp-A8h]
char v17[32]; // [rsp+10h] [rbp-98h] BYREF
char v18[32]; // [rsp+30h] [rbp-78h] BYREF
char v19[88]; // [rsp+50h] [rbp-58h] BYREF

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

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

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

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:
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; // eax
__int64 v5; // [rsp+0h] [rbp-168h] BYREF
char v6[64]; // [rsp+20h] [rbp-148h] BYREF
char v7[128]; // [rsp+60h] [rbp-108h] BYREF
__int64 v8; // [rsp+E0h] [rbp-88h] BYREF
__int128 v9; // [rsp+E8h] [rbp-80h]
__int128 v10; // [rsp+F8h] [rbp-70h]
__int128 v11; // [rsp+108h] [rbp-60h]
__int64 v12; // [rsp+118h] [rbp-50h]

v9 = 0LL;
v12 = 0LL;
qmemcpy(&v8, "Hello World!", 12);
v10 = 0LL;
v11 = 0LL;
if ( (int)crypto_sign_keypair() < 0 )
return 1;
v3 = sign(v7, &v8, 12LL, v6);
if ( v3 < 0 || (int)verify(&v8, v7, (unsigned int)v3, &v5) < 0 )
return 1;
puts("Verified!");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
__int64 v5; // [rsp+0h] [rbp-168h] BYREF
char v6[64]; // [rsp+20h] [rbp-148h] BYREF
char v7[128]; // [rsp+60h] [rbp-108h] BYREF
__int64 v8; // [rsp+E0h] [rbp-88h] BYREF
__int128 v9; // [rsp+E8h] [rbp-80h]
__int128 v10; // [rsp+F8h] [rbp-70h]
__int128 v11; // [rsp+108h] [rbp-60h]
__int64 v12; // [rsp+118h] [rbp-50h]

v9 = 0LL;
v12 = 0LL;
qmemcpy(&v8, "Hello World!", 12);
v10 = 0LL;
v11 = 0LL;
if ( (int)crypto_sign_keypair() < 0 )
return 1;
v3 = func7(v7, &v8, 12LL, v6);
if ( v3 < 0 || (int)func8(&v8, v7, (unsigned int)v3, &v5) < 0 )
return 1;
puts("Verified!");
return 0;
}

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)
{
char *v3; // r13
unsigned __int8 *v4; // rbx
unsigned __int8 *v5; // r12
char *v6; // rbp
__int64 v7; // rcx
char *v8; // rdi

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

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

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

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


decompiled_code:
int crypto_box_recover_public_key()
{
char *v0; // rax
unsigned __int8 v2[32]; // [rsp+0h] [rbp-78h] BYREF
char s[88]; // [rsp+20h] [rbp-58h] BYREF

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

canonical_decompiled_code:
int func8()
{
char *v0; // rax
unsigned __int8 v2[32]; // [rsp+0h] [rbp-78h] BYREF
char s[88]; // [rsp+20h] [rbp-58h] BYREF

crypto_scalarmult_curve25519_base();
v0 = func7(s, v2, 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
unsigned __int8 v3[32]; // [rsp+0h] [rbp-E8h] BYREF
unsigned __int8 v4[32]; // [rsp+20h] [rbp-C8h] BYREF
char s[80]; // [rsp+40h] [rbp-A8h] BYREF
char v6[88]; // [rsp+90h] [rbp-58h] BYREF

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

canonical_decompiled_code:
int func9()
{
char *v0; // rax
char *v1; // rax
unsigned __int8 v3[32]; // [rsp+0h] [rbp-E8h] BYREF
unsigned __int8 v4[32]; // [rsp+20h] [rbp-C8h] BYREF
char s[80]; // [rsp+40h] [rbp-A8h] BYREF
char v6[88]; // [rsp+90h] [rbp-58h] BYREF

crypto_box_keypair();
v0 = func7(s, v3, 32LL);
printf("public_key: %s\n", v0);
v1 = func7(v6, v4, 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)
{
__m128i v1; // xmm1
char *v2; // rax
__int128 v4[2]; // [rsp+0h] [rbp-78h] BYREF
char s[88]; // [rsp+20h] [rbp-58h] BYREF

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

canonical_decompiled_code:
int __fastcall func10(const __m128i *a1)
{
__m128i v1; // xmm1
char *v2; // rax
__int128 v4[2]; // [rsp+0h] [rbp-78h] BYREF
char s[88]; // [rsp+20h] [rbp-58h] BYREF

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

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
unsigned __int8 v3[32]; // [rsp+0h] [rbp-148h] BYREF
__m128i v4[4]; // [rsp+20h] [rbp-128h] BYREF
char s[80]; // [rsp+60h] [rbp-E8h] BYREF
char v6[152]; // [rsp+B0h] [rbp-98h] BYREF

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

canonical_decompiled_code:
int func11()
{
char *v0; // rax
char *v1; // rax
unsigned __int8 v3[32]; // [rsp+0h] [rbp-148h] BYREF
__m128i v4[4]; // [rsp+20h] [rbp-128h] BYREF
char s[80]; // [rsp+60h] [rbp-E8h] BYREF
char v6[152]; // [rsp+B0h] [rbp-98h] BYREF

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

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; // ebp
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; // ebp
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)
{
char *v3; // r13
unsigned __int8 *v4; // rbx
unsigned __int8 *v5; // r12
char *v6; // rbp
__int64 v7; // rcx
char *v8; // rdi

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


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

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

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

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

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(v4, (unsigned __int8 *)(a1 + 8), 32LL);
printf("public key: %s\n", v1);
v2 = to_hex(s, (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(v4, (unsigned __int8 *)(a1 + 8), 32LL);
printf("public key: %s\n", v1);
v2 = func8(s, (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
time_t v6; // rax
unsigned int v7; // er12
unsigned int v8; // eax
__int64 v10; // [rsp+Ch] [rbp-7Ch] BYREF
char v11[10]; // [rsp+16h] [rbp-72h] BYREF
_QWORD v12[2]; // [rsp+20h] [rbp-68h] BYREF
__int64 v13[11]; // [rsp+30h] [rbp-58h] BYREF

strcpy(v11, "/mailbox1");
v13[1] = 6LL;
v13[2] = 8LL;
v13[0] = 0LL;
v3 = mq_open(v11, 0LL, 384LL, v13);
if ( v3 == -1 )
{
perror("mq_open()");
exit(1);
}
v4 = v3;
signal(2, (__sighandler_t)sig_handler);
v5 = time(0LL);
srand(v5);
while ( g_continue )
{
while ( 1 )
{
v6 = time(0LL);
v12[1] = 0LL;
v12[0] = v6 + 5;
if ( mq_timedreceive(v4, &v10, 8LL, 0LL, v12) == -1 )
break;
v7 = get_y_coord(v10);
v8 = get_x_coord(v10);
printf("Received a random point at (%d, %d)\n", v8, v7);
if ( !g_continue )
goto LABEL_6;
}
perror("mq_timedreceive() failed");
puts("Type Ctrl-C and wait for 5 seconds to terminate.");
}
LABEL_6:
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:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char **v3; // rbx
unsigned int v4; // ebp
const char *v5; // r14
__gid_t v6; // eax
__uid_t v7; // eax
const char *v8; // r15
char **v9; // rax
signed int v10; // edx
char *const *v11; // rbx
int v12; // eax
int v13; // er12
const char *v14; // rbp
int stat_loc; // [rsp+Ch] [rbp-3Ch] BYREF

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

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char **v3; // rbx
unsigned int v4; // ebp
const char *v5; // r14
__gid_t v6; // eax
__uid_t v7; // eax
const char *v8; // r15
char **v9; // rax
signed int v10; // edx
char *const *v11; // rbx
int v12; // eax
int v13; // er12
const char *v14; // rbp
int stat_loc; // [rsp+Ch] [rbp-3Ch] BYREF

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

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: argc int

variable_types:
argv
char **

variable_types:
app
char *

variable_types: i int

variable_types: pid int

variable_types: status int


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()
{
int result; // eax

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

canonical_decompiled_code:
int func9()
{
int result; // eax

if ( func8(apps_dest_dir) )
result = 0;
else
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; // r13
size_t v3; // r8
_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; // r13
size_t v3; // r8
_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; // rax

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

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


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 v4; // r8
__int64 v5; // r10
int v6; // eax
__int64 v7; // rdi
__int64 result; // rax
int v9; // et2

v4 = a2;
if ( (unsigned __int16)a2 <= a3 )
{
v5 = *(_QWORD *)(a1 + 16);
do
{
v6 = (unsigned __int16)a2;
v7 = (unsigned __int16)a2++;
v9 = v6 % a4;
result = (unsigned int)(v6 / a4);
*(_WORD *)(v5 + 2 * v7) = v4 + v9;
}
while ( a3 >= (unsigned __int16)a2 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int a2, unsigned __int16 a3, int a4)
{
__int16 v4; // r8
__int64 v5; // r10
int v6; // eax
__int64 v7; // rdi
__int64 result; // rax
int v9; // et2

v4 = a2;
if ( (unsigned __int16)a2 <= a3 )
{
v5 = *(_QWORD *)(a1 + 16);
do
{
v6 = (unsigned __int16)a2;
v7 = (unsigned __int16)a2++;
v9 = v6 % a4;
result = (unsigned int)(v6 / a4);
*(_WORD *)(v5 + 2 * v7) = v4 + v9;
}
while ( a3 >= (unsigned __int16)a2 );
}
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 **v6; // 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

v6 = 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
{
++v6;
fprintf(stderr, "%s ", v14);
v14 = *v6;
}
while ( *v6 );
}
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 **v6; // 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

v6 = 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
{
++v6;
fprintf(stderr, "%s ", v14);
v14 = *v6;
}
while ( *v6 );
}
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 *v5; // rdx
int v6; // ecx
int v7; // esi
int pipedes[2]; // [rsp+8h] [rbp-20h] BYREF

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

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

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

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:
int __fastcall run_filters_given_fdin(__int64 a1, int a2, unsigned int *a3)
{
unsigned int v3; // er12
unsigned int *v4; // rbp
__int64 v5; // rbx
int v6; // ecx
int result; // eax

v3 = a2;
v4 = a3;
if ( !a1 )
{
*a3 = a2;
result = command_line[1];
if ( !result )
return result;
return fprintf(stderr, " Filter started, reading from given fd %i, outputing on fd %i.\n", v3, *v4);
}
v5 = a1;
v6 = -1;
while ( 1 )
{
add_filter(*(char ***)v5, a2, a3, v6);
v5 = *(_QWORD *)(v5 + 8);
if ( !v5 )
break;
a2 = *v4;
v6 = v3;
a3 = v4;
}
result = command_line[1];
if ( result )
return fprintf(stderr, " Filter started, reading from given fd %i, outputing on fd %i.\n", v3, *v4);
return result;
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1, int a2, unsigned int *a3)
{
unsigned int v3; // er12
unsigned int *v4; // rbp
__int64 v5; // rbx
int v6; // ecx
int result; // eax

v3 = a2;
v4 = a3;
if ( !a1 )
{
*a3 = a2;
result = command_line[1];
if ( !result )
return result;
return fprintf(stderr, " Filter started, reading from given fd %i, outputing on fd %i.\n", v3, *v4);
}
v5 = a1;
v6 = -1;
while ( 1 )
{
func0(*(char ***)v5, a2, a3, v6);
v5 = *(_QWORD *)(v5 + 8);
if ( !v5 )
break;
a2 = *v4;
v6 = v3;
a3 = v4;
}
result = command_line[1];
if ( result )
return fprintf(stderr, " Filter started, reading from given fd %i, outputing on fd %i.\n", v3, *v4);
return result;
}

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:
int __fastcall run_filters(__int64 a1, unsigned int *a2, unsigned int *a3)
{
__int64 i; // rbx
int result; // eax
const char *v6; // 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, v6);
*a2 = pipedes[1];
*a3 = pipedes[0];
}
result = command_line[1];
if ( result )
result = fprintf(stderr, " Filter started, accepting on fd %i, outputing on fd %i.\n", *a2, *a3);
return result;
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1, unsigned int *a2, unsigned int *a3)
{
__int64 i; // rbx
int result; // eax
const char *v6; // 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, v6);
*a2 = pipedes[1];
*a3 = pipedes[0];
}
result = command_line[1];
if ( result )
result = fprintf(stderr, " Filter started, accepting on fd %i, outputing on fd %i.\n", *a2, *a3);
return result;
}

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; // r12
const char *v3; // rbp
char *v4; // rax
char *v5; // r13
void *result; // rax

v2 = malloc(0x1010uLL);
if ( !v2 )
fatal_error("Cannot allocate");
*(_QWORD *)v2 = a2;
v3 = *a2;
*((_QWORD *)v2 + 1) = 0LL;
v4 = strrchr(v3, 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;
result = (void *)a1;
}
else
{
if ( !strcmp(v3, "cat") )
*((_BYTE *)v2 + 16) = 0;
else
snprintf((char *)v2 + 16, 0x1000uLL, ".%s", v3);
result = v2;
}
return result;
}

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

v2 = malloc(0x1010uLL);
if ( !v2 )
fatal_error("Cannot allocate");
*(_QWORD *)v2 = a2;
v3 = *a2;
*((_QWORD *)v2 + 1) = 0LL;
v4 = strrchr(v3, 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;
result = (void *)a1;
}
else
{
if ( !strcmp(v3, "cat") )
*((_BYTE *)v2 + 16) = 0;
else
snprintf((char *)v2 + 16, 0x1000uLL, ".%s", v3);
result = v2;
}
return result;
}

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; // rbx
char *i; // rdi
int j; // er12
const char **v6; // r13
_QWORD *v7; // r14
int k; // er12
char *v9; // rax

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

canonical_decompiled_code:
void *__fastcall func5(__int64 a1, const char *a2)
{
char *v3; // rbx
char *i; // rdi
int j; // er12
const char **v6; // r13
_QWORD *v7; // r14
int k; // er12
char *v9; // rax

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

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 ( 1 )
{
if ( !a1 )
return &unk_6FC;
if ( !*(_QWORD *)(a1 + 8) )
break;
a1 = *(_QWORD *)(a1 + 8);
}
return (void *)(a1 + 16);
}

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

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