main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int buf[3]; // [rsp+Ch] [rbp-Ch] BYREF

setbuf(_bss_start, 0LL);
while ( 1 )
{
read(0, buf, 1uLL);
playShit(buf[0]);
}
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int buf[3]; // [rsp+Ch] [rbp-Ch] BYREF

setbuf(_bss_start, 0LL);
while ( 1 )
{
read(0, buf, 1uLL);
func8(buf[0]);
}
}

original_code:
int main() {
  setbuf(stdout, ((void *)0));
  while(1) {
    int c;
    read(0,&c,1);
    playShit(c);
  }
}

variable_types: c int


decompiled_code:
__int64 timeout_cb()
{
(*(void (__fastcall **)(__int64, __int64))(ph + 40))(ph, timeout_hook);
minimized = 1;
return 3LL;
}

canonical_decompiled_code:
__int64 func1()
{
(*(void (__fastcall **)(__int64, __int64))(ph + 40))(ph, timeout_hook);
minimized = 1;
return 3LL;
}

original_code:
static int timeout_cb(void *user_data)
{
  (((ph))->xchat_unhook)(ph, timeout_hook);
  minimized = 1;
  return (1|2);
}

return_type: int


repo: jojje/command-wrapper

decompiled_code:
void *__fastcall malloc_string(size_t a1, int a2)
{
const char *v2; // rdx
void *v3; // rbx

v3 = malloc(a1);
if ( !v3 )
error((int)"Failed to allocate memory\n", a2, v2);
return v3;
}

canonical_decompiled_code:
void *__fastcall func0(size_t a1, int a2)
{
const char *v2; // rdx
void *v3; // rbx

v3 = malloc(a1);
if ( !v3 )
error((int)"Failed to allocate memory\n", a2, v2);
return v3;
}

original_code:
char* malloc_string(size_t size) {
  char* s = (char*) malloc(size);
  if(!s) {
    error("Failed to allocate memory\n");
  }
  return s;
}

canonical_original_code:
char *malloc_string(unsigned long size) {
  char *s = (char*) malloc(size);
  if(!s) {
    error("Failed to allocate memory\n");
  }
  return s;
}

variable_types:
size
unsigned long

variable_types:
s
char *

return_type: char *


decompiled_code:
int __fastcall join_path(char *a1, const char *a2, const char *a3)
{
return snprintf(a1, 0x7FFFuLL, "%s%s%s", a2, "//", a3);
}

canonical_decompiled_code:
int __fastcall func1(char *a1, const char *a2, const char *a3)
{
return snprintf(a1, 0x7FFFuLL, "%s%s%s", a2, "//", a3);
}

original_code:
void join_path(char* dst, const char* path1, const char* path2) {
  snprintf(dst, 32767, "%s%s%s", path1, "//", path2);
}

canonical_original_code:
void join_path(char *dst, char *path1, char *path2) {
  snprintf(dst, 32767, "%s%s%s", path1, "//", path2);
}

variable_types:
dst
char *

return_type: void


basename

decompiled_code:
size_t __fastcall basename(const char **a1)
{
const char *v1; // rbx
size_t result; // rax
__int64 v3; // rcx
int v4; // esi
char v5; // dl

v1 = *a1;
result = strlen(*a1);
if ( (int)result <= 0 )
{
v4 = 0;
}
else
{
v3 = (unsigned int)(result - 1) + 2LL;
result = 1LL;
v4 = 0;
do
{
v5 = v1[result - 1];
if ( v5 == 47 || v5 == 92 )
v4 = result;
++result;
}
while ( result != v3 );
}
*a1 = &v1[v4];
return result;
}

canonical_decompiled_code:
size_t __fastcall func2(const char **a1)
{
const char *v1; // rbx
size_t result; // rax
__int64 v3; // rcx
int v4; // esi
char v5; // dl

v1 = *a1;
result = strlen(*a1);
if ( (int)result <= 0 )
{
v4 = 0;
}
else
{
v3 = (unsigned int)(result - 1) + 2LL;
result = 1LL;
v4 = 0;
do
{
v5 = v1[result - 1];
if ( v5 == 47 || v5 == 92 )
v4 = result;
++result;
}
while ( result != v3 );
}
*a1 = &v1[v4];
return result;
}

original_code:
void basename(char** ppath) {
  char* path = *ppath;
  int i, last_sep, len=strlen(path);
  for(i=0, last_sep=0; i<len; i++) {
    if(path[i] == '/' || path[i] == '\\') {
      last_sep = i+1;
    }
  }
  path += last_sep;
  *ppath = path;
  len = strlen(path);
}

canonical_original_code:
void basename(char **ppath) {
  char *path = *ppath;
  int i; int last_sep; int len = strlen(path);
  for(i=0, last_sep=0; i<len; i++) {
    if(path[i] == '/' || path[i] == '\\') {
      last_sep = i+1;
    }
  }
  path += last_sep;
  *ppath = path;
  len = strlen(path);
}

variable_types:
ppath
char **

variable_types:
path
char *

variable_types: i int

variable_types: len int


decompiled_code:
__int64 __fastcall get_name_value(char *s, _QWORD *a2, _QWORD *a3)
{
size_t v4; // r14
size_t v5; // rbp
__int64 result; // rax
const char *v7; // rbx
size_t v8; // rax

v4 = strlen(s);
v5 = strcspn(s, "=");
result = 0LL;
if ( v4 != v5 )
{
strncpy_null(*a2, s, v5);
trim(a2);
v7 = &s[v5 + 1];
v8 = strlen(v7);
strncpy_null(*a3, v7, v8);
trim(a3);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(char *s, _QWORD *a2, _QWORD *a3)
{
size_t v4; // r14
size_t v5; // rbp
__int64 result; // rax
const char *v7; // rbx
size_t v8; // rax

v4 = strlen(s);
v5 = strcspn(s, "=");
result = 0LL;
if ( v4 != v5 )
{
strncpy_null(*a2, s, v5);
trim(a2);
v7 = &s[v5 + 1];
v8 = strlen(v7);
strncpy_null(*a3, v7, v8);
trim(a3);
result = 1LL;
}
return result;
}

original_code:
int get_name_value(const char* line, char** pname, char** pvalue){
  size_t pos = 0, len = strlen(line);
  pos = strcspn(line, "=");
  if(pos == len)
    return 0;
  strncpy_null(*pname, line, pos);
  trim(pname);
  line += pos+1;
  strncpy_null(*pvalue, line, strlen(line));
  trim(pvalue);
  return 1;
}

canonical_original_code:
int get_name_value(char *line, char **pname, char **pvalue){
  unsigned long pos = 0; unsigned long len = strlen(line);
  pos = strcspn(line, "=");
  if(pos == len)
    return 0;
  strncpy_null(*pname, line, pos);
  trim(pname);
  line += pos+1;
  strncpy_null(*pvalue, line, strlen(line));
  trim(pvalue);
  return 1;
}

variable_types:
line
char *

variable_types:
pname
char **

variable_types:
pvalue
char **

variable_types:
pos
unsigned long

variable_types:
len
unsigned long


original_code:
int parse_env_vars(config_entry* entry, const char* line, char delim) {
  char* s = malloc_string(strlen(line)+1);
  size_t i, j, len = strlen(line);
  char* start;
  strcpy(s,line);
  for(i=0, j=0; i<len; i++) {
    if( s[i] == delim ) j++;
  }
  entry->c_envvars = j;
  entry->envvars = (char**) malloc(entry->c_envvars * sizeof(char**));
  if(!entry->envvars)
    error("Failed to allocate memory");
  s++;
  start = s;
  for(i=0; *s; s++) {
    if( *s == '|' ) {
      *s = 0;
      entry->envvars[i] = (char*) malloc(strlen(start) + 1);
      strcpy(entry->envvars[i], start);
      trim(&entry->envvars[i]);
      i++;
      start = s+1;
    }
  }
  if(strlen(start) > 3) {
    entry->envvars[i] = (char*) malloc(strlen(start) + 1);
    strcpy(entry->envvars[i], start);
    trim(&entry->envvars[i]);
  }
  return 1;
}

canonical_original_code:
int parse_env_vars(struct config_entry *entry, char *line, char delim) {
  char *s = malloc_string(strlen(line)+1);
  unsigned long i; unsigned long j; unsigned long len = strlen(line);
  char *start;
  strcpy(s,line);
  for(i=0, j=0; i<len; i++) {
    if( s[i] == delim ) j++;
  }
  entry->c_envvars = j;
  entry->envvars = (char **) malloc(entry->c_envvars * sizeof(char **));
  if(!entry->envvars)
    error("Failed to allocate memory");
  s++;
  start = s;
  for(i=0; *s; s++) {
    if( *s == '|' ) {
      *s = 0;
      entry->envvars[i] = (char *) malloc(strlen(start) + 1);
      strcpy(entry->envvars[i], start);
      trim(&entry->envvars[i]);
      i++;
      start = s+1;
    }
  }
  if(strlen(start) > 3) {
    entry->envvars[i] = (char *) malloc(strlen(start) + 1);
    strcpy(entry->envvars[i], start);
    trim(&entry->envvars[i]);
  }
  return 1;
}

variable_types: struct config_entry { char *cmd; char *abscmd; char **envvars; int c_envvars; } *

variable_types: delim char

variable_types:
i
unsigned long

variable_types:
j
unsigned long

variable_types:
start
char *


decompiled_code:
__int64 __fastcall parse_config_entry(_QWORD *a1, char *a2)
{
size_t v2; // r12
size_t v3; // rbp
__int64 result; // rax
void *v5; // rax
size_t v6; // rbp
void *v7; // rax
char *v8; // rbp
size_t v9; // rdx
char *s; // [rsp+8h] [rbp-20h] BYREF

s = a2;
v2 = strlen(a2);
v3 = strcspn(a2, "=");
result = 0LL;
if ( v2 != v3 )
{
v5 = malloc_string(v3 + 1, (int)"=");
*a1 = v5;
strncpy_null(v5, s, v3);
trim(a1);
s += v3 + 1;
v6 = strcspn(s, "|");
v7 = malloc_string(v6 + 1, (int)"|");
a1[1] = v7;
strncpy_null(v7, s, v6);
trim(a1 + 1);
s += v6;
trim(&s);
v8 = s;
v9 = strlen(s);
result = 1LL;
if ( v9 > 4 )
{
parse_env_vars((__int64)a1, v8, 124);
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(_QWORD *a1, char *a2)
{
size_t v2; // r12
size_t v3; // rbp
__int64 result; // rax
void *v5; // rax
size_t v6; // rbp
void *v7; // rax
char *v8; // rbp
size_t v9; // rdx
char *s; // [rsp+8h] [rbp-20h] BYREF

s = a2;
v2 = strlen(a2);
v3 = strcspn(a2, "=");
result = 0LL;
if ( v2 != v3 )
{
v5 = func0(v3 + 1, (int)"=");
*a1 = v5;
strncpy_null(v5, s, v3);
trim(a1);
s += v3 + 1;
v6 = strcspn(s, "|");
v7 = func0(v6 + 1, (int)"|");
a1[1] = v7;
strncpy_null(v7, s, v6);
trim(a1 + 1);
s += v6;
trim(&s);
v8 = s;
v9 = strlen(s);
result = 1LL;
if ( v9 > 4 )
{
func6((__int64)a1, v8, 124);
result = 1LL;
}
}
return result;
}

original_code:
int parse_config_entry(config_entry* entry, char* line){
  size_t pos = 0, len = strlen(line);
  pos = strcspn(line, "=");
  if(pos == len)
    return 0;
  entry->cmd = malloc_string( pos+1 );
  strncpy_null(entry->cmd, line, pos);
  trim(&entry->cmd);
  line += pos+1;
  pos = strcspn(line, "|");
  entry->abscmd = malloc_string( pos+1 );
  strncpy_null(entry->abscmd, line, pos);
  trim(&entry->abscmd);
  line += pos;
  trim(&line);
  len = strlen(line);
  if(len > 4) {
    parse_env_vars(entry, line, '|');
  }
  return 1;
}

canonical_original_code:
int parse_config_entry(struct config_entry *entry, char *line){
  unsigned long pos = 0; unsigned long len = strlen(line);
  pos = strcspn(line, "=");
  if(pos == len)
    return 0;
  entry->cmd = malloc_string( pos+1 );
  strncpy_null(entry->cmd, line, pos);
  trim(&entry->cmd);
  line += pos+1;
  pos = strcspn(line, "|");
  entry->abscmd = malloc_string( pos+1 );
  strncpy_null(entry->abscmd, line, pos);
  trim(&entry->abscmd);
  line += pos;
  trim(&line);
  len = strlen(line);
  if(len > 4) {
    parse_env_vars(entry, line, '|');
  }
  return 1;
}

user_defined_types: struct config_entry { char *cmd; char *abscmd; char **envvars; int c_envvars; }


decompiled_code:
__int64 __fastcall split_pair(const char **a1, const char *a2)
{
int v2; // eax
__int64 result; // rax

v2 = strcspn(a2, "=");
a2[v2] = 0;
*a1 = a2;
result = (__int64)&a2[v2 + 1];
a1[1] = (const char *)result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char **a1, const char *a2)
{
int v2; // eax
__int64 result; // rax

v2 = strcspn(a2, "=");
a2[v2] = 0;
*a1 = a2;
result = (__int64)&a2[v2 + 1];
a1[1] = (const char *)result;
return result;
}

original_code:
void split_pair(pair* kv, char* s ) {
  int pos = strcspn(s, "=");
  *(s + pos) = 0;
  kv->key = s;
  s += pos+1;
  kv->value = s;
}

canonical_original_code:
void split_pair(struct pair *kv, char *s) {
  int pos = strcspn(s, "=");
  *(s + pos) = 0;
  kv->key = s;
  s += pos+1;
  kv->value = s;
}

variable_types: struct pair { char *key; char *value; } *

variable_types: pos int


setEnv

decompiled_code:
const char **__fastcall setEnv(char *s, char *a2, const char **a3)
{
const char **v3; // r15
const char *v4; // rbx
const char **v5; // rbp
int v6; // er13
size_t v7; // rax
size_t v8; // rbx
size_t v9; // rax
char *v10; // rax
_QWORD *v12; // rbx
const char *v13; // rdx
__int64 v14; // rax
__int64 v15; // rbp
__int64 v16; // r13
size_t v17; // r15
size_t v18; // rax
const char **v19; // rbp
char *v20; // rax
int v21; // [rsp+Ch] [rbp-803Ch]
char s2[32824]; // [rsp+10h] [rbp-8038h] BYREF

v3 = a3;
v4 = *a3;
if ( !*a3 )
{
v6 = 0;
LABEL_9:
v12 = malloc(8LL * (v6 + 2));
v13 = *v3;
if ( *v3 )
{
v14 = 1LL;
do
{
v12[v14 - 1] = v13;
v15 = v14;
v16 = v14++;
v13 = v3[v14 - 1];
}
while ( v13 );
}
else
{
v15 = 0LL;
v16 = 0LL;
}
v17 = strlen(s);
v18 = strlen(a2);
v19 = (const char **)&v12[v15];
v20 = (char *)malloc(v18 * v17 + 2);
*v19 = v20;
sprintf(v20, "%s=%s", s, a2);
info("Adding ENV: %s\n", *v19);
v12[v16 + 1] = 0LL;
return (const char **)v12;
}
v5 = a3;
v21 = 0;
v6 = 0;
do
{
v7 = strcspn(v4, "=");
strncpy_null(s2, v4, v7);
if ( !strcmp(s, s2) )
{
info("Replacing ENV: %s -> %s=%s\n", *v5, s, a2);
v8 = strlen(s);
v9 = strlen(a2);
v10 = (char *)malloc(v9 * v8 + 2);
*v5 = v10;
sprintf(v10, "%s=%s", s, a2);
v21 = 1;
}
++v6;
v4 = *++v5;
}
while ( *v5 );
if ( !v21 )
goto LABEL_9;
return v3;
}

canonical_decompiled_code:
const char **__fastcall func2(char *s, char *a2, const char **a3)
{
const char **v3; // r15
const char *v4; // rbx
const char **v5; // rbp
int v6; // er13
size_t v7; // rax
size_t v8; // rbx
size_t v9; // rax
char *v10; // rax
_QWORD *v12; // rbx
const char *v13; // rdx
__int64 v14; // rax
__int64 v15; // rbp
__int64 v16; // r13
size_t v17; // r15
size_t v18; // rax
const char **v19; // rbp
char *v20; // rax
int v21; // [rsp+Ch] [rbp-803Ch]
char s2[32824]; // [rsp+10h] [rbp-8038h] BYREF

v3 = a3;
v4 = *a3;
if ( !*a3 )
{
v6 = 0;
LABEL_9:
v12 = malloc(8LL * (v6 + 2));
v13 = *v3;
if ( *v3 )
{
v14 = 1LL;
do
{
v12[v14 - 1] = v13;
v15 = v14;
v16 = v14++;
v13 = v3[v14 - 1];
}
while ( v13 );
}
else
{
v15 = 0LL;
v16 = 0LL;
}
v17 = strlen(s);
v18 = strlen(a2);
v19 = (const char **)&v12[v15];
v20 = (char *)malloc(v18 * v17 + 2);
*v19 = v20;
sprintf(v20, "%s=%s", s, a2);
info("Adding ENV: %s\n", *v19);
v12[v16 + 1] = 0LL;
return (const char **)v12;
}
v5 = a3;
v21 = 0;
v6 = 0;
do
{
v7 = strcspn(v4, "=");
strncpy_null(s2, v4, v7);
if ( !strcmp(s, s2) )
{
info("Replacing ENV: %s -> %s=%s\n", *v5, s, a2);
v8 = strlen(s);
v9 = strlen(a2);
v10 = (char *)malloc(v9 * v8 + 2);
*v5 = v10;
sprintf(v10, "%s=%s", s, a2);
v21 = 1;
}
++v6;
v4 = *++v5;
}
while ( *v5 );
if ( !v21 )
goto LABEL_9;
return v3;
}

original_code:
char** setEnv(char* key, char* value, char* envp[]) {
  int i, found = 0;
  char** new_envp;
  char lkey[32767 +1];
  size_t pos;
  for(i=0; envp[i] != 0; i++) {
    pos = strcspn(envp[i], "=");
    strncpy_null(lkey, envp[i], pos);
    if( strcmp(key, lkey) == 0 ) {
      found = 1;
      info("Replacing ENV: %s -> %s=%s\n", envp[i], key, value);
      envp[i] = (char*) malloc( strlen(key) * strlen(value) + 2 );
      sprintf(envp[i], "%s=%s", key, value);
    }
  }
  if(!found) {
    new_envp = (char**) malloc( (i+2) * sizeof(char**) );
    for(i=0; envp[i] != 0; i++) {
      new_envp[i] = envp[i];
    }
    new_envp[i] = (char*) malloc( strlen(key) * strlen(value) + 2 );
    sprintf(new_envp[i], "%s=%s", key, value);
    info("Adding ENV: %s\n", new_envp[i]);
    new_envp[i+1] = 0;
    envp = new_envp;
  }
  return envp;
}

canonical_original_code:
char **setEnv(char *key, char *value, char *envp[]) {
  int i; int found = 0;
  char **new_envp;
  char lkey[-1];
  unsigned long pos;
  for(i=0; envp[i] != 0; i++) {
    pos = strcspn(envp[i], "=");
    strncpy_null(lkey, envp[i], pos);
    if( strcmp(key, lkey) == 0 ) {
      found = 1;
      info("Replacing ENV: %s -> %s=%s\n", envp[i], key, value);
      envp[i] = (char *) malloc( strlen(key) * strlen(value) + 2 );
      sprintf(envp[i], "%s=%s", key, value);
    }
  }
  if(!found) {
    new_envp = (char **) malloc( (i+2) * sizeof(char **) );
    for(i=0; envp[i] != 0; i++) {
      new_envp[i] = envp[i];
    }
    new_envp[i] = (char *) malloc( strlen(key) * strlen(value) + 2 );
    sprintf(new_envp[i], "%s=%s", key, value);
    info("Adding ENV: %s\n", new_envp[i]);
    new_envp[i+1] = 0;
    envp = new_envp;
  }
  return envp;
}

variable_types:
key
char *

variable_types:
value
char *

variable_types:
envp
char *[]

variable_types: found int

variable_types:
lkey
char [-1]

return_type: char **


launch

decompiled_code:
__int64 __fastcall launch(int a1, const char **a2, const char **a3)
{
const char *v5; // rdx
__int64 v6; // rbx
char *v8; // r13
const char *v9; // rsi
const char **v10; // rbx
__int64 i; // rbx

if ( !(unsigned int)file_exists(*a2) )
error((int)"File command found: %s\n", (int)*a2, v5);
if ( verbose )
{
v8 = (char *)malloc(0x10000uLL);
*v8 = 0;
info("Launching using environment variables:");
v9 = *a3;
if ( *a3 )
{
v10 = a3 + 1;
do
{
info(" %s", v9);
v9 = *v10++;
}
while ( v9 );
}
info((const char *)&unk_560);
info("Command arguments: ");
if ( a1 > 0 )
{
for ( i = 0LL; i != a1; ++i )
{
strcat(v8, a2[i]);
if ( a1 - 1 > (int)i )
*(_WORD *)&v8[strlen(v8)] = 32;
}
}
info(" %s\n", v8);
}
v6 = (unsigned int)execv(*a2, (char *const *)a2);
info("Return code: %d", v6);
return (unsigned int)v6;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, const char **a2, const char **a3)
{
const char *v5; // rdx
__int64 v6; // rbx
char *v8; // r13
const char *v9; // rsi
const char **v10; // rbx
__int64 i; // rbx

if ( !(unsigned int)file_exists(*a2) )
error((int)"File command found: %s\n", (int)*a2, v5);
if ( verbose )
{
v8 = (char *)malloc(0x10000uLL);
*v8 = 0;
info("Launching using environment variables:");
v9 = *a3;
if ( *a3 )
{
v10 = a3 + 1;
do
{
info(" %s", v9);
v9 = *v10++;
}
while ( v9 );
}
info((const char *)&unk_560);
info("Command arguments: ");
if ( a1 > 0 )
{
for ( i = 0LL; i != a1; ++i )
{
strcat(v8, a2[i]);
if ( a1 - 1 > (int)i )
*(_WORD *)&v8[strlen(v8)] = 32;
}
}
info(" %s\n", v8);
}
v6 = (unsigned int)execv(*a2, (char *const *)a2);
info("Return code: %d", v6);
return (unsigned int)v6;
}

original_code:
int launch(int argc, char* argv[], char* envp[]) {
  int i, rc;
  char* infomsg;
  if(! file_exists(argv[0]) )
    error("File command found: %s\n",argv[0]);
  if(verbose) {
    infomsg = (char*) malloc(1<<16);
    infomsg[0] = 0;
    if(infomsg) {
      info("Launching using environment variables:");
      for(i=0; envp[i] != 0; i++) info("  %s", envp[i] );
      info("");
      info("Command arguments: ");
      for(i=0; i<argc ; i++) {
        strcat(infomsg, argv[i] );
        if(i<argc-1) strcat(infomsg, " ");
      }
      info("  %s\n", infomsg);
    }
  }
    rc = execv(argv[0],(char * const *)argv);
  info("Return code: %d", rc);
  return rc;
}

canonical_original_code:
int launch(int argc, char *argv[], char *envp[]) {
  int i; int rc;
  char *infomsg;
  if(! file_exists(argv[0]) )
    error("File command found: %s\n",argv[0]);
  if(verbose) {
    infomsg = (char *) malloc(1<<16);
    infomsg[0] = 0;
    if(infomsg) {
      info("Launching using environment variables:");
      for(i=0; envp[i] != 0; i++) info("  %s", envp[i] );
      info("");
      info("Command arguments: ");
      for(i=0; i<argc ; i++) {
        strcat(infomsg, argv[i] );
        if(i<argc-1) strcat(infomsg, " ");
      }
      info("  %s\n", infomsg);
    }
  }
    rc = execv(argv[0],(char **)argv);
  info("Return code: %d", rc);
  return rc;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: i int

variable_types: rc int

variable_types:
infomsg
char *


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
_QWORD *v4; // r12
__int64 v5; // rbp
int v7; // [rsp+Ch] [rbp-4Ch] BYREF
char *s[3]; // [rsp+10h] [rbp-48h] BYREF
__int64 filename[6]; // [rsp+28h] [rbp-30h] BYREF

v7 = argc;
filename[0] = (__int64)*argv;
parse_verbose(&v7, (__int64)argv);
basename((const char *)filename);
v4 = find_config(filename[0], (int)argv);
*argv = (const char *)v4[1];
if ( *((int *)v4 + 6) > 0 )
{
v5 = 0LL;
do
{
split_pair((const char **)s, *(const char **)(v4[2] + 8 * v5));
envp = setEnv(s[0], s[1], envp);
++v5;
}
while ( *((_DWORD *)v4 + 6) > (int)v5 );
}
free(v4);
return launch(v7, argv, envp);
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
_QWORD *v4; // r12
__int64 v5; // rbp
int v7; // [rsp+Ch] [rbp-4Ch] BYREF
char *s[3]; // [rsp+10h] [rbp-48h] BYREF
__int64 filename[6]; // [rsp+28h] [rbp-30h] BYREF

v7 = argc;
filename[0] = (__int64)*argv;
func1(&v7, (__int64)argv);
basename((const char *)filename);
v4 = find_config(filename[0], (int)argv);
*argv = (const char *)v4[1];
if ( *((int *)v4 + 6) > 0 )
{
v5 = 0LL;
do
{
func0((const char **)s, *(const char **)(v4[2] + 8 * v5));
envp = func2(s[0], s[1], envp);
++v5;
}
while ( *((_DWORD *)v4 + 6) > (int)v5 );
}
free(v4);
return func3(v7, argv, envp);
}

original_code:
int main(int argc, char* argv[], char* envp[]) {
  int i;
  char* cmd = argv[0];
  config_entry* entry;
  pair kv;
  parse_verbose(&argc, argv);
  basename(&cmd);
  entry = find_config(cmd);
  argv[0] = entry->abscmd;
  for(i=0; i<entry->c_envvars; i++){
    split_pair(&kv, entry->envvars[i] );
    envp = setEnv(kv.key, kv.value, envp);
  }
  free(entry);
  return launch(argc, argv, envp);
}

canonical_original_code:
int main(int argc, char *argv[], char *envp[]) {
  int i;
  char *cmd = argv[0];
  struct config_entry *entry;
  struct pair kv;
  parse_verbose(&argc, argv);
  basename(&cmd);
  entry = find_config(cmd);
  argv[0] = entry->abscmd;
  for(i=0; i<entry->c_envvars; i++){
    split_pair(&kv, entry->envvars[i] );
    envp = setEnv(kv.key, kv.value, envp);
  }
  free(entry);
  return launch(argc, argv, envp);
}

variable_types:
cmd
char *

variable_types: struct config_entry { char *cmd; char *abscmd; char **envvars; int c_envvars; } *

variable_types:
kv
struct pair { char *key; char *value; }


error

decompiled_code:
void __noreturn error(const char *a1, ...)
{
char *v1; // rax
char *v2; // rbx
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

v1 = (char *)malloc(0x10000uLL);
v2 = v1;
if ( v1 )
{
strcpy(v1, "ERROR ");
strcat(v1, a1);
*(_WORD *)&v2[strlen(v2)] = 10;
va_start(arg, a1);
vfprintf(stderr, v2, arg);
}
free(v2);
exit(1);
}

canonical_decompiled_code:
void __noreturn func0(const char *a1, ...)
{
char *v1; // rax
char *v2; // rbx
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

v1 = (char *)malloc(0x10000uLL);
v2 = v1;
if ( v1 )
{
strcpy(v1, "ERROR ");
strcat(v1, a1);
*(_WORD *)&v2[strlen(v2)] = 10;
va_start(arg, a1);
vfprintf(stderr, v2, arg);
}
free(v2);
exit(1);
}

original_code:
void error(const char* fmt, ...) {
  va_list args;
  int max = 1<<16;
  char* msg = (char*) malloc(max);
  if(msg) {
    strcpy(msg,"ERROR ");
    strcat(msg,fmt);
    strcat(msg,"\n");
    __builtin_va_start(args,fmt);
    vfprintf(stderr, msg, args);
    __builtin_va_end(args);
  }
  free(msg);
  exit(1);
}

canonical_original_code:
void error(char *fmt, ...) {
  __builtin_va_list args;
  int max = 1<<16;
  char *msg = (char*) malloc(max);
  if(msg) {
    strcpy(msg,"ERROR ");
    strcat(msg,fmt);
    strcat(msg,"\n");
    __builtin_va_start(args,fmt);
    vfprintf(stderr, msg, args);
    __builtin_va_end(args);
  }
  free(msg);
  exit(1);
}

variable_types:
fmt
char *

variable_types: max int

variable_types:
msg
char *


info

decompiled_code:
void info(char *src, ...)
{
char *v1; // rax
char *v2; // rbx
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

if ( verbose )
{
v1 = (char *)malloc(0x10000uLL);
v2 = v1;
if ( v1 )
{
strcpy(v1, "INFO ");
strcat(v1, src);
*(_WORD *)&v2[strlen(v2)] = 10;
va_start(arg, src);
vfprintf(stdout, v2, arg);
}
free(v2);
}
}

canonical_decompiled_code:
void func1(char *src, ...)
{
char *v1; // rax
char *v2; // rbx
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

if ( verbose )
{
v1 = (char *)malloc(0x10000uLL);
v2 = v1;
if ( v1 )
{
strcpy(v1, "INFO ");
strcat(v1, src);
*(_WORD *)&v2[strlen(v2)] = 10;
va_start(arg, src);
vfprintf(stdout, v2, arg);
}
free(v2);
}
}

original_code:
void info(const char* fmt, ...) {
  va_list args;
  char* msg;
  if(verbose) {
    msg = (char*) malloc(1<<16);
    if(msg) {
      strcpy(msg,"INFO ");
      strcat(msg,fmt);
      strcat(msg,"\n");
      __builtin_va_start(args,fmt);
      vfprintf(stdout, msg, args);
      __builtin_va_end(args);
    }
    free(msg);
  }
}

canonical_original_code:
void info(char *fmt, ...) {
  __builtin_va_list args;
  char *msg;
  if(verbose) {
    msg = (char *) malloc(1<<16);
    if(msg) {
      strcpy(msg,"INFO ");
      strcat(msg,fmt);
      strcat(msg,"\n");
      __builtin_va_start(args,fmt);
      vfprintf(stdout, msg, args);
      __builtin_va_end(args);
    }
    free(msg);
  }
}


trim

decompiled_code:
__int64 __fastcall trim(const char **a1)
{
const char *v1; // rbx
const char *v2; // rbp
const unsigned __int16 **v3; // rcx
__int64 result; // rax

v1 = *a1;
v2 = &v1[strlen(*a1) - 1];
v3 = _ctype_b_loc();
while ( ((*v3)[*v2] & 0x2000) != 0 )
--v2;
*((_BYTE *)v2 + 1) = 0;
for ( result = *v1; ((*v3)[result] & 0x2000) != 0; result = *v1 )
++v1;
*a1 = v1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char **a1)
{
const char *v1; // rbx
const char *v2; // rbp
const unsigned __int16 **v3; // rcx
__int64 result; // rax

v1 = *a1;
v2 = &v1[strlen(*a1) - 1];
v3 = _ctype_b_loc();
while ( ((*v3)[*v2] & 0x2000) != 0 )
--v2;
*((_BYTE *)v2 + 1) = 0;
for ( result = *v1; ((*v3)[result] & 0x2000) != 0; result = *v1 )
++v1;
*a1 = v1;
return result;
}

original_code:
void trim(char** sptr) {
  char* s = *sptr;
  char* ptr = s;
  ptr = s + strlen(s)-1;
  while(((*__ctype_b_loc ())[(int) (((int)*ptr))] & (unsigned short int) _ISspace)) ptr--;
  *(ptr+1) = '\0';
  ptr = s;
  while(((*__ctype_b_loc ())[(int) (((int)*ptr))] & (unsigned short int) _ISspace)) ptr++;
  *sptr = ptr;
}

canonical_original_code:
void trim(char **sptr) {
  char *s = *sptr;
  char *ptr = s;
  ptr = s + strlen(s)-1;
  while(((*__ctype_b_loc ())[(int) (((int)*ptr))] & (unsigned short) _ISspace)) ptr--;
  *(ptr+1) = '\0';
  ptr = s;
  while(((*__ctype_b_loc ())[(int) (((int)*ptr))] & (unsigned short) _ISspace)) ptr++;
  *sptr = ptr;
}

variable_types:
sptr
char **

variable_types:
s
char *

variable_types:
ptr
char *


decompiled_code:
char *__fastcall strncpy_null(char *a1, const char *a2, size_t a3)
{
char *result; // rax

result = strncpy(a1, a2, a3);
a1[a3] = 0;
return result;
}

canonical_decompiled_code:
char *__fastcall func1(char *a1, const char *a2, size_t a3)
{
char *result; // rax

result = strncpy(a1, a2, a3);
a1[a3] = 0;
return result;
}

original_code:
char* strncpy_null(char* dst, const char* src, size_t num) {
  strncpy(dst, src, num);
  dst[num] = 0;
  return dst;
}

canonical_original_code:
char *strncpy_null(char *dst, char *src, unsigned long num) {
  strncpy(dst, src, num);
  dst[num] = 0;
  return dst;
}

variable_types:
dst
char *

variable_types:
src
char *

variable_types:
num
unsigned long


quote

decompiled_code:
int __fastcall quote(char *a1, const char *a2)
{
return sprintf(a1, "\"%s\"", a2);
}

canonical_decompiled_code:
int __fastcall func2(char *a1, const char *a2)
{
return sprintf(a1, "\"%s\"", a2);
}

original_code:
void quote(char* dst, const char* src) {
  sprintf(dst, "\"%s\"", src);
}

canonical_original_code:
void quote(char *dst, char *src) {
  sprintf(dst, "\"%s\"", src);
}


repo: jokkebk/JZipVIew

original_code:
JImage *make_letter(JImage *font, int x, int y, int w, int h, void ** mempool) {
    int i, j, offset = 0;
    JImage *image = (JImage *)(*mempool);
    *mempool += sizeof(JImage);
    image->w = w;
    image->h = h;
    image->data = (Uint32 *)(*mempool);
    *mempool += w*h * sizeof(Uint32);
    for(j=0; j<h; j++)
        for(i=0; i<w; i++)
            image->data[offset++] = ((font)->data[(y+j) * (font)->w + (x+i)]);
    return image;
}

canonical_original_code:
struct JImage *make_letter(struct JImage *font, int x, int y, int w, int h, void **mempool) {
    int i; int j; int offset = 0;
    struct JImage *image = (JImage *)(*mempool);
    *mempool += sizeof(JImage);
    image->w = w;
    image->h = h;
    image->data = (unsigned int *)(*mempool);
    *mempool += w*h * sizeof(Uint32);
    for(j=0; j<h; j++)
        for(i=0; i<w; i++)
            image->data[offset++] = ((font)->data[(y+j) * (font)->w + (x+i)]);
    return image;
}

variable_types:
font
struct JImage *

variable_types: x int

variable_types: y int

variable_types: w int

variable_types: h int

variable_types:
mempool
void **

variable_types: j int

variable_types: offset int

variable_types: struct JImage { unsigned int *data; int w; int h; } *


decompiled_code:
void __fastcall destroy_font(void *a1)
{
free(a1);
}

canonical_decompiled_code:
void __fastcall func2(void *a1)
{
free(a1);
}

original_code:
void destroy_font(JFont *fontPtr) {
    free(fontPtr);
}

canonical_original_code:
void destroy_font(struct JFont *fontPtr) {
    free(fontPtr);
}

variable_types: struct JFont { struct JImage { unsigned int *data; int w; int h; } **letter; char convert[128]; int letter_spacing; int space_width; } *

user_defined_types: struct JFont { struct JImage **letter; char convert[128]; int letter_spacing; int space_width; }


original_code:
void write_font(JImage *image, JFont *font, Uint32 c, const char *message, int x, int y, int align, int spacing) {
    int total_width = 0, len = strlen(message), i, ch, n;
    for(i=0; i<len; i++) {
        if((ch = message[i]) == 32 || (n = font->convert[ch]) == -1)
            total_width += font->space_width;
        else
            total_width += font->letter[n]->w;
    }
    total_width += spacing * (len-1);
    if((align & 0x0F) == 0x01)
        y -= font->letter[0]->h / 2;
    else if((align & 0x0F) == 0x02)
        y -= font->letter[0]->h;
    if((align & 0xF0) == 0x10)
        x -= total_width / 2;
    else if((align & 0xF0) == 0x20)
        x -= total_width;
    for(i=0; i<len; i++) {
        if((ch = message[i]) == 32 || (n = font->convert[ch]) == -1) {
            x += font->space_width + spacing;
        } else {
            blit_font(image, font->letter[n], x, y, c);
            x += font->letter[n]->w + spacing;
        }
    }
}

canonical_original_code:
void write_font(struct JImage *image, struct JFont *font, unsigned int c, char *message, int x, int y, int align, int spacing) {
    int total_width = 0; int len = strlen(message); int i; int ch; int n;
    for(i=0; i<len; i++) {
        if((ch = message[i]) == 32 || (n = font->convert[ch]) == -1)
            total_width += font->space_width;
        else
            total_width += font->letter[n]->w;
    }
    total_width += spacing * (len-1);
    if((align & 0x0F) == 0x01)
        y -= font->letter[0]->h / 2;
    else if((align & 0x0F) == 0x02)
        y -= font->letter[0]->h;
    if((align & 0xF0) == 0x10)
        x -= total_width / 2;
    else if((align & 0xF0) == 0x20)
        x -= total_width;
    for(i=0; i<len; i++) {
        if((ch = message[i]) == 32 || (n = font->convert[ch]) == -1) {
            x += font->space_width + spacing;
        } else {
            blit_font(image, font->letter[n], x, y, c);
            x += font->letter[n]->w + spacing;
        }
    }
}

variable_types: struct JFont { struct JImage **letter; char convert[128]; int letter_spacing; int space_width; } *

variable_types:
c
unsigned int

variable_types:
message
char *

variable_types: align int

variable_types: spacing int

variable_types: len int

variable_types: i int

variable_types: ch int

variable_types: n int

user_defined_types: struct JImage { unsigned int *data; int w; int h; }


decompiled_code:
void __fastcall destroy_image(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func0(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

original_code:
void destroy_image(JImage *img) {
    free(img->data);
    free(img);
}

canonical_original_code:
void destroy_image(struct JImage *img) {
    free(img->data);
    free(img);
}


decompiled_code:
void *__fastcall copy_image(void **a1, __int64 a2)
{
void *result; // rax

result = *(void **)(a2 + 8);
if ( a1[1] == result )
result = memcpy(*a1, *(const void **)a2, 4LL * *(_DWORD *)(a2 + 12) * *(_DWORD *)(a2 + 8));
return result;
}

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

result = *(void **)(a2 + 8);
if ( a1[1] == result )
result = memcpy(*a1, *(const void **)a2, 4LL * *(_DWORD *)(a2 + 12) * *(_DWORD *)(a2 + 8));
return result;
}

original_code:
void copy_image(JImage *dest, JImage *src) {
    if(dest->w != src->w || dest->h != src->h)
        return;
    memcpy(dest->data, src->data, src->w * src->h * sizeof(Uint32));
}

canonical_original_code:
void copy_image(struct JImage *dest, struct JImage *src) {
    if(dest->w != src->w || dest->h != src->h)
        return;
    memcpy(dest->data, src->data, src->w * src->h * sizeof(Uint32));
}

variable_types:
src
struct JImage *


decompiled_code:
unsigned __int64 __fastcall rotate_image(__int64 a1, __int64 a2, int a3)
{
int v3; // ecx
unsigned __int64 result; // rax
int k; // er8
int i; // er9
int m; // ecx
int n; // edx
int j; // er8
int v10; // edx
int l; // ecx

v3 = *(_DWORD *)(a2 + 12);
result = (unsigned int)(*(_DWORD *)(a1 + 12) * *(_DWORD *)(a1 + 8));
if ( (_DWORD)result == *(_DWORD *)(a2 + 8) * v3 )
{
if ( (a3 & 0xFFFFFFFD) == 1 )
{
*(_DWORD *)(a1 + 8) = v3;
result = *(unsigned int *)(a2 + 8);
*(_DWORD *)(a1 + 12) = result;
}
else
{
*(_DWORD *)(a1 + 12) = v3;
result = *(unsigned int *)(a2 + 8);
*(_DWORD *)(a1 + 8) = result;
}
switch ( a3 )
{
case 2:
for ( i = 0; *(_DWORD *)(a2 + 12) > i; ++i )
{
v10 = *(_DWORD *)(a2 + 8);
for ( j = 0; v10 > j; v10 = *(_DWORD *)(a2 + 8) )
{
result = *(_DWORD *)(a1 + 8) * (*(_DWORD *)(a1 + 12) + ~i) + *(_DWORD *)(a1 + 8) - 1 - j;
*(_DWORD *)(*(_QWORD *)a1 + 4 * result) = *(_DWORD *)(*(_QWORD *)a2 + 4LL * (j + i * v10));
++j;
}
}
break;
case 3:
for ( k = 0; *(_DWORD *)(a2 + 12) > k; ++k )
{
result = *(unsigned int *)(a2 + 8);
for ( l = 0; (int)result > l; result = *(unsigned int *)(a2 + 8) )
{
*(_DWORD *)(*(_QWORD *)a1 + 4LL * (k + *(_DWORD *)(a1 + 8) * ((int)result - 1 - l))) = *(_DWORD *)(*(_QWORD *)a2 + 4LL * (l + k * (int)result));
++l;
}
}
break;
case 1:
for ( m = 0; *(_DWORD *)(a2 + 12) > m; ++m )
{
result = *(unsigned int *)(a2 + 8);
for ( n = 0; (int)result > n; result = *(unsigned int *)(a2 + 8) )
{
*(_DWORD *)(*(_QWORD *)a1 + 4LL * (*(_DWORD *)(a1 + 8) * n + *(_DWORD *)(a2 + 12) + ~m)) = *(_DWORD *)(*(_QWORD *)a2 + 4LL * (n + m * (int)result));
++n;
}
}
break;
default:
result = (unsigned __int64)copy_image((void **)a1, a2);
break;
}
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func2(__int64 a1, __int64 a2, int a3)
{
int v3; // ecx
unsigned __int64 result; // rax
int k; // er8
int i; // er9
int m; // ecx
int n; // edx
int j; // er8
int v10; // edx
int l; // ecx

v3 = *(_DWORD *)(a2 + 12);
result = (unsigned int)(*(_DWORD *)(a1 + 12) * *(_DWORD *)(a1 + 8));
if ( (_DWORD)result == *(_DWORD *)(a2 + 8) * v3 )
{
if ( (a3 & 0xFFFFFFFD) == 1 )
{
*(_DWORD *)(a1 + 8) = v3;
result = *(unsigned int *)(a2 + 8);
*(_DWORD *)(a1 + 12) = result;
}
else
{
*(_DWORD *)(a1 + 12) = v3;
result = *(unsigned int *)(a2 + 8);
*(_DWORD *)(a1 + 8) = result;
}
switch ( a3 )
{
case 2:
for ( i = 0; *(_DWORD *)(a2 + 12) > i; ++i )
{
v10 = *(_DWORD *)(a2 + 8);
for ( j = 0; v10 > j; v10 = *(_DWORD *)(a2 + 8) )
{
result = *(_DWORD *)(a1 + 8) * (*(_DWORD *)(a1 + 12) + ~i) + *(_DWORD *)(a1 + 8) - 1 - j;
*(_DWORD *)(*(_QWORD *)a1 + 4 * result) = *(_DWORD *)(*(_QWORD *)a2 + 4LL * (j + i * v10));
++j;
}
}
break;
case 3:
for ( k = 0; *(_DWORD *)(a2 + 12) > k; ++k )
{
result = *(unsigned int *)(a2 + 8);
for ( l = 0; (int)result > l; result = *(unsigned int *)(a2 + 8) )
{
*(_DWORD *)(*(_QWORD *)a1 + 4LL * (k + *(_DWORD *)(a1 + 8) * ((int)result - 1 - l))) = *(_DWORD *)(*(_QWORD *)a2 + 4LL * (l + k * (int)result));
++l;
}
}
break;
case 1:
for ( m = 0; *(_DWORD *)(a2 + 12) > m; ++m )
{
result = *(unsigned int *)(a2 + 8);
for ( n = 0; (int)result > n; result = *(unsigned int *)(a2 + 8) )
{
*(_DWORD *)(*(_QWORD *)a1 + 4LL * (*(_DWORD *)(a1 + 8) * n + *(_DWORD *)(a2 + 12) + ~m)) = *(_DWORD *)(*(_QWORD *)a2 + 4LL * (n + m * (int)result));
++n;
}
}
break;
default:
result = (unsigned __int64)func1((void **)a1, a2);
break;
}
}
return result;
}

original_code:
void rotate_image(JImage *dest, JImage *src, int angle) {
    int x, y;
    if(dest->w * dest->h != src->w * src->h)
        return;
    if(angle == 1 || angle == 3) {
        dest->w = src->h;
        dest->h = src->w;
    } else {
        dest->h = src->h;
        dest->w = src->w;
    }
    switch(angle) {
    case 1:
        for(y=0; y<src->h; y++)
        for(x=0; x<src->w; x++)
            { (dest)->data[(x) * (dest)->w + (src->h - 1 - y)] = (((src)->data[(y) * (src)->w + (x)])); };
        break;
    case 2:
        for(y=0; y<src->h; y++)
        for(x=0; x<src->w; x++)
            { (dest)->data[(dest->h - 1 - y) * (dest)->w + (dest->w - 1 - x)] = (((src)->data[(y) * (src)->w + (x)])); };
        break;
    case 3:
        for(y=0; y<src->h; y++)
        for(x=0; x<src->w; x++)
            { (dest)->data[(src->w - 1 - x) * (dest)->w + (y)] = (((src)->data[(y) * (src)->w + (x)])); };
        break;
    default:
        copy_image(dest, src);
        break;
    }
}

canonical_original_code:
void rotate_image(struct JImage *dest, struct JImage *src, int angle) {
    int x; int y;
    if(dest->w * dest->h != src->w * src->h)
        return;
    if(angle == 1 || angle == 3) {
        dest->w = src->h;
        dest->h = src->w;
    } else {
        dest->h = src->h;
        dest->w = src->w;
    }
    switch(angle) {
    case 1:
        for(y=0; y<src->h; y++)
        for(x=0; x<src->w; x++)
            { (dest)->data[(x) * (dest)->w + (src->h - 1 - y)] = (((src)->data[(y) * (src)->w + (x)])); };
        break;
    case 2:
        for(y=0; y<src->h; y++)
        for(x=0; x<src->w; x++)
            { (dest)->data[(dest->h - 1 - y) * (dest)->w + (dest->w - 1 - x)] = (((src)->data[(y) * (src)->w + (x)])); };
        break;
    case 3:
        for(y=0; y<src->h; y++)
        for(x=0; x<src->w; x++)
            { (dest)->data[(src->w - 1 - x) * (dest)->w + (y)] = (((src)->data[(y) * (src)->w + (x)])); };
        break;
    default:
        copy_image(dest, src);
        break;
    }
}

variable_types: angle int

variable_types: x int

variable_types: y int


variable_types: avg int


original_code:
void invert_image(JImage *img) {
    int x, y;
    for(y=0; y<img->h; y++) {
        for(x=0; x<img->w; x++) {
            { (img)->data[(y) * (img)->w + (x)] = (0xFFFFFF - ((img)->data[(y) * (img)->w + (x)])); };
        }
    }
}

canonical_original_code:
void invert_image(struct JImage *img) {
    int x; int y;
    for(y=0; y<img->h; y++) {
        for(x=0; x<img->w; x++) {
            { (img)->data[(y) * (img)->w + (x)] = (0xFFFFFF - ((img)->data[(y) * (img)->w + (x)])); };
        }
    }
}


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

result = (unsigned int)(*(_DWORD *)(a1 + 12) * *(_DWORD *)(a1 + 8));
if ( (int)result > 0 )
{
result = 0LL;
do
*(_DWORD *)(*(_QWORD *)a1 + 4 * result++) = a2;
while ( *(_DWORD *)(a1 + 12) * *(_DWORD *)(a1 + 8) > (int)result );
}
return result;
}

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

result = (unsigned int)(*(_DWORD *)(a1 + 12) * *(_DWORD *)(a1 + 8));
if ( (int)result > 0 )
{
result = 0LL;
do
*(_DWORD *)(*(_QWORD *)a1 + 4 * result++) = a2;
while ( *(_DWORD *)(a1 + 12) * *(_DWORD *)(a1 + 8) > (int)result );
}
return result;
}

original_code:
void fill_image(JImage *img, Uint32 c) {
    int i;
    for(i=0; i<img->w * img->h; i++)
        img->data[i] = c;
}

canonical_original_code:
void fill_image(struct JImage *img, unsigned int c) {
    int i;
    for(i=0; i<img->w * img->h; i++)
        img->data[i] = c;
}


original_code:
void blit_image(JImage *dest, int dx, int dy, JImage *src, int sx, int sy, int w, int h) {
    int x, y;
    if(dx < 0) {
        w += dx;
        sx += dx;
        dx = 0;
    }
    if(dy < 0) {
        h += dy;
        sy += dy;
        dy = 0;
    }
    if(sx < 0) {
        w += sx;
        dx += sx;
        sx = 0;
    }
    if(sy < 0) {
        h += sy;
        dy += sy;
        sy = 0;
    }
    if(dx + w > dest->w)
        w = dest->w - dx;
    if(dy + h > dest->h)
        h = dest->h - dy;
    if(sx + w > src->w)
        w = src->w - sx;
    if(sy + h > src->h)
        h = src->h - sy;
    if(dx >= dest->w || dy >= dest->h || w <= 0 || h <= 0)
        return;
    for(y=0; y<h; y++)
        for(x=0; x<w; x++)
            { (dest)->data[(dy+y) * (dest)->w + (dx+x)] = (((src)->data[(sy+y) * (src)->w + (sx+x)])); };
}

canonical_original_code:
void blit_image(struct JImage *dest, int dx, int dy, struct JImage *src, int sx, int sy, int w, int h) {
    int x; int y;
    if(dx < 0) {
        w += dx;
        sx += dx;
        dx = 0;
    }
    if(dy < 0) {
        h += dy;
        sy += dy;
        dy = 0;
    }
    if(sx < 0) {
        w += sx;
        dx += sx;
        sx = 0;
    }
    if(sy < 0) {
        h += sy;
        dy += sy;
        sy = 0;
    }
    if(dx + w > dest->w)
        w = dest->w - dx;
    if(dy + h > dest->h)
        h = dest->h - dy;
    if(sx + w > src->w)
        w = src->w - sx;
    if(sy + h > src->h)
        h = src->h - sy;
    if(dx >= dest->w || dy >= dest->h || w <= 0 || h <= 0)
        return;
    for(y=0; y<h; y++)
        for(x=0; x<w; x++)
            { (dest)->data[(dy+y) * (dest)->w + (dx+x)] = (((src)->data[(sy+y) * (src)->w + (sx+x)])); };
}

variable_types: dx int

variable_types: dy int

variable_types: sx int

variable_types: sy int

variable_types: w int

variable_types: h int


decompiled_code:
__int64 __fastcall blit_sprite(__int64 a1, int a2, int a3, __int64 a4)
{
return blit_image(a1, a2, a3, a4, 0, 0, *(_DWORD *)(a4 + 8), *(_DWORD *)(a4 + 12));
}

original_code:
void blit_sprite(JImage *dest, int dx, int dy, JImage *sprite) {
    blit_image(dest, dx, dy, sprite, 0, 0, sprite->w, sprite->h);
}

canonical_original_code:
void blit_sprite(struct JImage *dest, int dx, int dy, struct JImage *sprite) {
    blit_image(dest, dx, dy, sprite, 0, 0, sprite->w, sprite->h);
}

variable_types: struct JImage { unsigned int *data; int w; int h; } *

variable_types:
sprite
struct JImage *


repo: jon/libjs

decompiled_code:
void *js_alloc()
{
void *v0; // rdx

v0 = malloc(0x908uLL);
if ( v0 )
memset(v0, 0, 0x908uLL);
return v0;
}

canonical_decompiled_code:
void *func6()
{
void *v0; // rdx

v0 = malloc(0x908uLL);
if ( v0 )
memset(v0, 0, 0x908uLL);
return v0;
}

original_code:
static js_t *js_alloc() {
    js_t *js;
    js = (js_t *)malloc(sizeof(js_t));
    if (js == ((void *)0))
        return ((void *)0);
    memset(js, 0, sizeof(js_t));
    return js;
}

canonical_original_code:
static struct js *js_alloc() {
    struct js *js;
    js = (struct js *)malloc(sizeof(js_t));
    if (js == ((void *)0))
        return ((void *)0);
    memset(js, 0, sizeof(js_t));
    return js;
}

variable_types: struct js { int fd; struct js_state { unsigned char buttons[256]; double axes[256]; } state; } *

user_defined_types: struct js { int fd; struct js_state state; }


decompiled_code:
void *__fastcall js_poll_event(int *a1)
{
bool v1; // cc
void *result; // rax

if ( !a1 )
__assert_fail("js != NULL", "js.c", 0x56u, "js_poll_event");
v1 = (int)read(*a1, &event_1, 8uLL) <= 7;
result = &event_1;
if ( v1 )
result = 0LL;
return result;
}

canonical_decompiled_code:
void *__fastcall func8(int *a1)
{
bool v1; // cc
void *result; // rax

if ( !a1 )
__assert_fail("js != NULL", "js.c", 0x56u, "js_poll_event");
v1 = (int)read(*a1, &event_1, 8uLL) <= 7;
result = &event_1;
if ( v1 )
result = 0LL;
return result;
}

original_code:
js_event_t *js_poll_event(js_t *js) {
    static js_event_t event;
    ((void) sizeof ((js != ((void *)0)) ? 1 : 0), __extension__ ({ if (js != ((void *)0)) ; else __assert_fail ("js != NULL", "js.c", 86, __extension__ __PRETTY_FUNCTION__); }));
    int len = read(js->fd, &event, sizeof(event));
    if (len < (int)sizeof(event))
        return ((void *)0);
    return &event;
}

canonical_original_code:
struct js_event *js_poll_event(struct js *js) {
    static struct js_event event;
    ((void) sizeof ((js != ((void *)0)) ? 1 : 0), __extension__ ({ if (js != ((void *)0)) ; else __assert_fail ("js != NULL", "js.c", 86, __extension__ __PRETTY_FUNCTION__); }));
    int len = read(js->fd, &event, sizeof(event));
    if (len < (int)sizeof(event))
        return ((void *)0);
    return &event;
}

variable_types: len int

return_type: struct js_event { unsigned int time; short value; unsigned char type; unsigned char number; } *

user_defined_types: struct js_state { unsigned char buttons[256]; double axes[256]; }


original_code:
int js_poll_state(js_t *js) {
    js_event_t *event = js_poll_event(js);
    if (event == ((void *)0))
        return -1;
    switch (event->type) {
        case 0x02:
            js->state.axes[event->number] = (double)event->value / 32768.0;
            break;
        case 0x01:
            js->state.buttons[event->number] = event->value;
            break;
        default:
            break;
    }
    return 0;
}

canonical_original_code:
int js_poll_state(struct js *js) {
    struct js_event *event = js_poll_event(js);
    if (event == ((void *)0))
        return -1;
    switch (event->type) {
        case 0x02:
            js->state.axes[event->number] = (double)event->value / 32768.0;
            break;
        case 0x01:
            js->state.buttons[event->number] = event->value;
            break;
        default:
            break;
    }
    return 0;
}

variable_types: struct js_event { unsigned int time; short value; unsigned char type; unsigned char number; } *


decompiled_code:
void __fastcall js_close(void *ptr)
{
if ( !ptr )
__assert_fail("js != NULL", "js.c", 0x75u, "js_close");
close(*(_DWORD *)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func10(void *ptr)
{
if ( !ptr )
__assert_fail("js != NULL", "js.c", 0x75u, "js_close");
close(*(_DWORD *)ptr);
free(ptr);
}

original_code:
void js_close(js_t *js) {
    ((void) sizeof ((js != ((void *)0)) ? 1 : 0), __extension__ ({ if (js != ((void *)0)) ; else __assert_fail ("js != NULL", "js.c", 117, __extension__ __PRETTY_FUNCTION__); }));
    close(js->fd);
    free(js);
}

canonical_original_code:
void js_close(struct js *js) {
    ((void) sizeof ((js != ((void *)0)) ? 1 : 0), __extension__ ({ if (js != ((void *)0)) ; else __assert_fail ("js != NULL", "js.c", 117, __extension__ __PRETTY_FUNCTION__); }));
    close(js->fd);
    free(js);
}


decompiled_code:
void __fastcall ldap_free_friendlymap(void **a1)
{
void **v1; // rbx
void *i; // rdi

v1 = (void **)*a1;
if ( *a1 )
{
for ( i = *v1; *v1; i = *v1 )
{
free(i);
free(v1[1]);
v1 += 2;
}
free(*a1);
*a1 = 0LL;
}
}

original_code:
void
ldap_free_friendlymap( LDAPFriendlyMap **map )
{
 LDAPFriendlyMap* pF = *map;
 if ( pF == ((void *)0) )
  return;
 while ( pF->lf_unfriendly )
 {
  free( pF->lf_unfriendly );
  free( pF->lf_friendly );
  pF++;
 }
 free( *map );
 *map = ((void *)0);
}

canonical_original_code:
void ldap_free_friendlymap(struct ldap_friendly **map)
{
 struct ldap_friendly *pF = *map;
 if ( pF == ((void *)0) )
  return;
 while ( pF->lf_unfriendly )
 {
  free( pF->lf_unfriendly );
  free( pF->lf_friendly );
  pF++;
 }
 free( *map );
 *map = ((void *)0);
}

variable_types: struct ldap_friendly { char *lf_unfriendly; char *lf_friendly; } **


decompiled_code:
__int64 __fastcall break_into_words(__int64 a1, __int64 a2, _QWORD *a3)
{
void *v4; // rax
void *v5; // rbx
__int64 v6; // rbp
__int64 v7; // r12
unsigned int v8; // er13
char *v9; // rax
char v11[64]; // [rsp+8h] [rbp-40h] BYREF

v4 = calloc(1uLL, 8uLL);
if ( !v4 )
return (unsigned int)-1;
v5 = v4;
v6 = ldap_pvt_strtok(a1, a2, v11);
if ( v6 )
{
v7 = 0LL;
v8 = 0;
while ( 1 )
{
v9 = (char *)realloc(v5, v7 + 16);
v5 = v9;
if ( !v9 )
break;
*(_QWORD *)&v9[v7] = v6;
++v8;
v7 += 8LL;
*(_QWORD *)&v9[v7] = 0LL;
v6 = ldap_pvt_strtok(0LL, a2, v11);
if ( !v6 )
goto LABEL_6;
}
v8 = -1;
}
else
{
v8 = 0;
LABEL_6:
*a3 = v5;
}
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2, _QWORD *a3)
{
void *v4; // rax
void *v5; // rbx
__int64 v6; // rbp
__int64 v7; // r12
unsigned int v8; // er13
char *v9; // rax
char v11[64]; // [rsp+8h] [rbp-40h] BYREF

v4 = calloc(1uLL, 8uLL);
if ( !v4 )
return (unsigned int)-1;
v5 = v4;
v6 = ldap_pvt_strtok(a1, a2, v11);
if ( v6 )
{
v7 = 0LL;
v8 = 0;
while ( 1 )
{
v9 = (char *)realloc(v5, v7 + 16);
v5 = v9;
if ( !v9 )
break;
*(_QWORD *)&v9[v7] = v6;
++v8;
v7 += 8LL;
*(_QWORD *)&v9[v7] = 0LL;
v6 = ldap_pvt_strtok(0LL, a2, v11);
if ( !v6 )
goto LABEL_6;
}
v8 = -1;
}
else
{
v8 = 0;
LABEL_6:
*a3 = v5;
}
return v8;
}

original_code:
static int
break_into_words( char *str, const char *delims, char ***wordsp )
{
    char *word, **words;
    int count;
    char *tok_r;
    if (( words = (char **)(calloc((1),(sizeof( char * ))))) == ((void *)0) ) {
 return( -1 );
    }
    count = 0;
    words[ count ] = ((void *)0);
    word = ldap_pvt_strtok( str, delims, &tok_r );
    while ( word != ((void *)0) ) {
 if (( words = (char **)(realloc((words),(( count + 2 ) * sizeof( char * ))))) == ((void *)0) ) {
     return( -1 );
 }
 words[ count ] = word;
 words[ ++count ] = ((void *)0);
 word = ldap_pvt_strtok( ((void *)0), delims, &tok_r );
    }
    *wordsp = words;
    return( count );
}

canonical_original_code:
static int break_into_words(char *str, char *delims, char ***wordsp)
{
    char *word; char **words;
    int count;
    char *tok_r;
    if (( words = (char **)(calloc((1),(sizeof( char * ))))) == ((void *)0) ) {
 return( -1 );
    }
    count = 0;
    words[ count ] = ((void *)0);
    word = ldap_pvt_strtok( str, delims, &tok_r );
    while ( word != ((void *)0) ) {
 if (( words = (char **)(realloc((words),(( count + 2 ) * sizeof( char * ))))) == ((void *)0) ) {
     return( -1 );
 }
 words[ count ] = word;
 words[ ++count ] = ((void *)0);
 word = ldap_pvt_strtok( ((void *)0), delims, &tok_r );
    }
    *wordsp = words;
    return( count );
}

variable_types:
str
char *

variable_types:
delims
char *

variable_types:
wordsp
char ***

variable_types:
word
char *

variable_types:
words
char **

variable_types: count int


decompiled_code:
void __fastcall ldap_setfilteraffixes(__int64 a1, const char *a2, char *a3)
{
char *v4; // rbp
void *v6; // rdi
void *v7; // rdi

v4 = (char *)a2;
v6 = *(void **)(a1 + 1096);
if ( v6 )
free(v6);
if ( a2 )
v4 = strdup(a2);
*(_QWORD *)(a1 + 1096) = v4;
v7 = *(void **)(a1 + 1104);
if ( v7 )
free(v7);
if ( a3 )
a3 = strdup(a3);
*(_QWORD *)(a1 + 1104) = a3;
}

original_code:
void
ldap_setfilteraffixes( LDAPFiltDesc *lfdp, const char *prefix, const char *suffix )
{
    if ( lfdp->lfd_filtprefix != ((void *)0) ) {
 (free(lfdp->lfd_filtprefix));
    }
    lfdp->lfd_filtprefix = ( prefix == ((void *)0) ) ? ((void *)0) : (strdup(prefix));
    if ( lfdp->lfd_filtsuffix != ((void *)0) ) {
 (free(lfdp->lfd_filtsuffix));
    }
    lfdp->lfd_filtsuffix = ( suffix == ((void *)0) ) ? ((void *)0) : (strdup(suffix));
}

canonical_original_code:
void ldap_setfilteraffixes(struct ldap_filt_desc *lfdp, char *prefix, char *suffix)
{
    if ( lfdp->lfd_filtprefix != ((void *)0) ) {
 (free(lfdp->lfd_filtprefix));
    }
    lfdp->lfd_filtprefix = ( prefix == ((void *)0) ) ? ((void *)0) : (strdup(prefix));
    if ( lfdp->lfd_filtsuffix != ((void *)0) ) {
 (free(lfdp->lfd_filtsuffix));
    }
    lfdp->lfd_filtsuffix = ( suffix == ((void *)0) ) ? ((void *)0) : (strdup(suffix));
}

variable_types: struct ldap_filt_desc { struct ldap_filt_list { char *lfl_tag; char *lfl_pattern; char *lfl_delims; struct ldap_filt_info { char *lfi_filter; char *lfi_desc; int lfi_scope; int lfi_isexact; struct ldap_filt_info *lfi_next; } *lfl_ilist; struct ldap_filt_list *lfl_next; } *lfd_filtlist; struct ldap_filt_info *lfd_curfip; struct ldap_filt_info lfd_retfi; char lfd_filter[1024]; char *lfd_curval; char *lfd_curvalcopy; char **lfd_curvalwords; char *lfd_filtprefix; char *lfd_filtsuffix; } *

variable_types:
prefix
char *

variable_types:
suffix
char *

user_defined_types: struct ldap_filt_desc { struct ldap_filt_list *lfd_filtlist; struct ldap_filt_info *lfd_curfip; struct ldap_filt_info lfd_retfi; char lfd_filter[1024]; char *lfd_curval; char *lfd_curvalcopy; char **lfd_curvalwords; char *lfd_filtprefix; char *lfd_filtsuffix; }

user_defined_types: struct ldap_filt_list { char *lfl_tag; char *lfl_pattern; char *lfl_delims; struct ldap_filt_info *lfl_ilist; struct ldap_filt_list *lfl_next; }


original_code:
void
ldap_build_filter(
 char *filtbuf,
 ber_len_t buflen,
 const char *pattern,
 const char *prefix,
 const char *suffix,
 const char *attr,
 const char *value,
 char **valwords )
{
 const char *p;
 char *f;
 size_t slen;
 int i, wordcount, wordnum, endwordnum;
 if ( valwords == ((void *)0) ) {
     wordcount = 0;
 } else {
     for ( wordcount = 0; valwords[ wordcount ] != ((void *)0); ++wordcount ) {
  ;
     }
 }
 f = filtbuf;
 if ( prefix != ((void *)0) ) {
     strcpy( f, prefix );
     f += strlen( prefix );
 }
 for ( p = pattern; *p != '\0'; ++p ) {
     if ( *p == '%' ) {
  ++p;
  if ( *p == 'v' ) {
      if ( ( ((unsigned char) p[1]) >= '0' && ((unsigned char) p[1]) <= '9' )) {
   ++p;
   wordnum = *p - '1';
   if ( *(p+1) == '-' ) {
       ++p;
       if ( ( ((unsigned char) p[1]) >= '0' && ((unsigned char) p[1]) <= '9' )) {
    ++p;
    endwordnum = *p - '1';
    if ( endwordnum > wordcount - 1 ) {
        endwordnum = wordcount - 1;
    }
       } else {
    endwordnum = wordcount - 1;
       }
   } else {
       endwordnum = wordnum;
   }
   if ( wordcount > 0 ) {
       for ( i = wordnum; i <= endwordnum; ++i ) {
    if ( i > wordnum ) {
        *f++ = ' ';
    }
    slen = strlen( valwords[ i ] );
    memcpy((f), (valwords[ i ]), (slen));
    f += slen;
       }
   }
      } else if ( *(p+1) == '$' ) {
   ++p;
   if ( wordcount > 0 ) {
       wordnum = wordcount - 1;
       slen = strlen( valwords[ wordnum ] );
       memcpy((f), (valwords[ wordnum ]), (slen));
       f += slen;
   }
      } else if ( value != ((void *)0) ) {
   slen = strlen( value );
   memcpy((f), (value), (slen));
   f += slen;
      }
  } else if ( *p == 'a' && attr != ((void *)0) ) {
      slen = strlen( attr );
      memcpy((f), (attr), (slen));
      f += slen;
  } else {
      *f++ = *p;
  }
     } else {
  *f++ = *p;
     }
     if ( (size_t) (f - filtbuf) > buflen ) {
  --f;
  break;
     }
 }
 if ( suffix != ((void *)0) && ( (size_t) (f - filtbuf) < buflen ) )
 {
     strcpy( f, suffix );
 } else {
     *f = '\0';
 }
}

canonical_original_code:
void ldap_build_filter(char *filtbuf, unsigned long buflen, char *pattern, char *prefix, char *suffix, char *attr, char *value, char **valwords)
{
 char *p;
 char *f;
 unsigned long slen;
 int i; int wordcount; int wordnum; int endwordnum;
 if ( valwords == ((void *)0) ) {
     wordcount = 0;
 } else {
     for ( wordcount = 0; valwords[ wordcount ] != ((void *)0); ++wordcount ) {
  ;
     }
 }
 f = filtbuf;
 if ( prefix != ((void *)0) ) {
     strcpy( f, prefix );
     f += strlen( prefix );
 }
 for ( p = pattern; *p != '\0'; ++p ) {
     if ( *p == '%' ) {
  ++p;
  if ( *p == 'v' ) {
      if ( ( ((unsigned char) p[1]) >= '0' && ((unsigned char) p[1]) <= '9' )) {
   ++p;
   wordnum = *p - '1';
   if ( *(p+1) == '-' ) {
       ++p;
       if ( ( ((unsigned char) p[1]) >= '0' && ((unsigned char) p[1]) <= '9' )) {
    ++p;
    endwordnum = *p - '1';
    if ( endwordnum > wordcount - 1 ) {
        endwordnum = wordcount - 1;
    }
       } else {
    endwordnum = wordcount - 1;
       }
   } else {
       endwordnum = wordnum;
   }
   if ( wordcount > 0 ) {
       for ( i = wordnum; i <= endwordnum; ++i ) {
    if ( i > wordnum ) {
        *f++ = ' ';
    }
    slen = strlen( valwords[ i ] );
    memcpy((f), (valwords[ i ]), (slen));
    f += slen;
       }
   }
      } else if ( *(p+1) == '$' ) {
   ++p;
   if ( wordcount > 0 ) {
       wordnum = wordcount - 1;
       slen = strlen( valwords[ wordnum ] );
       memcpy((f), (valwords[ wordnum ]), (slen));
       f += slen;
   }
      } else if ( value != ((void *)0) ) {
   slen = strlen( value );
   memcpy((f), (value), (slen));
   f += slen;
      }
  } else if ( *p == 'a' && attr != ((void *)0) ) {
      slen = strlen( attr );
      memcpy((f), (attr), (slen));
      f += slen;
  } else {
      *f++ = *p;
  }
     } else {
  *f++ = *p;
     }
     if ( (unsigned long) (f - filtbuf) > buflen ) {
  --f;
  break;
     }
 }
 if ( suffix != ((void *)0) && ( (unsigned long) (f - filtbuf) < buflen ) )
 {
     strcpy( f, suffix );
 } else {
     *f = '\0';
 }
}

variable_types:
filtbuf
char *

variable_types:
buflen
unsigned long

variable_types:
pattern
char *

variable_types:
attr
char *

variable_types:
value
char *

variable_types:
valwords
char **

variable_types:
p
char *

variable_types:
f
char *

variable_types:
slen
unsigned long

variable_types: i int

variable_types: wordcount int

variable_types: wordnum int

variable_types: endwordnum int


original_code:
LDAPFiltInfo *
ldap_getnextfilter( LDAPFiltDesc *lfdp )
{
    LDAPFiltInfo *fip;
    fip = lfdp->lfd_curfip;
    if ( fip == ((void *)0) ) {
 return( ((void *)0) );
    }
    lfdp->lfd_curfip = fip->lfi_next;
    ldap_build_filter( lfdp->lfd_filter, 1024, fip->lfi_filter,
     lfdp->lfd_filtprefix, lfdp->lfd_filtsuffix, ((void *)0),
     lfdp->lfd_curval, lfdp->lfd_curvalwords );
    lfdp->lfd_retfi.lfi_filter = lfdp->lfd_filter;
    lfdp->lfd_retfi.lfi_desc = fip->lfi_desc;
    lfdp->lfd_retfi.lfi_scope = fip->lfi_scope;
    lfdp->lfd_retfi.lfi_isexact = fip->lfi_isexact;
    return( &lfdp->lfd_retfi );
}

canonical_original_code:
struct ldap_filt_info *ldap_getnextfilter(struct ldap_filt_desc *lfdp)
{
    struct ldap_filt_info *fip;
    fip = lfdp->lfd_curfip;
    if ( fip == ((void *)0) ) {
 return( ((void *)0) );
    }
    lfdp->lfd_curfip = fip->lfi_next;
    ldap_build_filter( lfdp->lfd_filter, 1024, fip->lfi_filter,
     lfdp->lfd_filtprefix, lfdp->lfd_filtsuffix, ((void *)0),
     lfdp->lfd_curval, lfdp->lfd_curvalwords );
    lfdp->lfd_retfi.lfi_filter = lfdp->lfd_filter;
    lfdp->lfd_retfi.lfi_desc = fip->lfi_desc;
    lfdp->lfd_retfi.lfi_scope = fip->lfi_scope;
    lfdp->lfd_retfi.lfi_isexact = fip->lfi_isexact;
    return( &lfdp->lfd_retfi );
}

variable_types: struct ldap_filt_desc { struct ldap_filt_list { char *lfl_tag; char *lfl_pattern; char *lfl_delims; struct ldap_filt_info *lfl_ilist; struct ldap_filt_list *lfl_next; } *lfd_filtlist; struct ldap_filt_info *lfd_curfip; struct ldap_filt_info lfd_retfi; char lfd_filter[1024]; char *lfd_curval; char *lfd_curvalcopy; char **lfd_curvalwords; char *lfd_filtprefix; char *lfd_filtsuffix; } *

variable_types: struct ldap_filt_info { char *lfi_filter; char *lfi_desc; int lfi_scope; int lfi_isexact; struct ldap_filt_info *lfi_next; } *


original_code:
static int
next_line( char **bufp, ber_len_t *blenp, char **linep )
{
    char *linestart, *line, *p;
    ber_slen_t plen;
    linestart = *bufp;
    p = *bufp;
    plen = *blenp;
    do {
 for ( linestart = p; plen > 0; ++p, --plen ) {
     if ( *p == '\r' ) {
  if ( plen > 1 && *(p+1) == '\n' ) {
      ++p;
      --plen;
  }
  break;
     }
     if ( *p == '\n' ) {
  if ( plen > 1 && *(p+1) == '\r' ) {
      ++p;
      --plen;
  }
  break;
     }
 }
 ++p;
 --plen;
    } while ( plen > 0 && ( *linestart == '#' || linestart + 1 == p ));
    *bufp = p;
    *blenp = plen;
    if ( plen <= 0 ) {
 *linep = ((void *)0);
 return( 0 );
    }
    if (( line = (malloc(p - linestart))) == ((void *)0) ) {
 *linep = ((void *)0);
 return( -1 );
    }
    memcpy((line), (linestart), (p - linestart));
    line[ p - linestart - 1 ] = '\0';
    *linep = line;
    return( strlen( line ));
}

canonical_original_code:
static int next_line(char **bufp, unsigned long *blenp, char **linep)
{
    char *linestart; char *line; char *p;
    long plen;
    linestart = *bufp;
    p = *bufp;
    plen = *blenp;
    do {
 for ( linestart = p; plen > 0; ++p, --plen ) {
     if ( *p == '\r' ) {
  if ( plen > 1 && *(p+1) == '\n' ) {
      ++p;
      --plen;
  }
  break;
     }
     if ( *p == '\n' ) {
  if ( plen > 1 && *(p+1) == '\r' ) {
      ++p;
      --plen;
  }
  break;
     }
 }
 ++p;
 --plen;
    } while ( plen > 0 && ( *linestart == '#' || linestart + 1 == p ));
    *bufp = p;
    *blenp = plen;
    if ( plen <= 0 ) {
 *linep = ((void *)0);
 return( 0 );
    }
    if (( line = (malloc(p - linestart))) == ((void *)0) ) {
 *linep = ((void *)0);
 return( -1 );
    }
    memcpy((line), (linestart), (p - linestart));
    line[ p - linestart - 1 ] = '\0';
    *linep = line;
    return( strlen( line ));
}

variable_types:
bufp
char **

variable_types:
blenp
unsigned long *

variable_types:
linep
char **

variable_types:
linestart
char *

variable_types:
line
char *

variable_types: plen long


decompiled_code:
char *__fastcall next_token(const char **a1)
{
const char *v2; // rdi
char v3; // al
char v4; // al
int v5; // er9
char v6; // al
const char *v7; // rdx
char *v8; // rsi
const char *v9; // rcx
char *result; // rax

v2 = *a1;
v3 = *v2;
if ( !*v2 )
return 0LL;
if ( (unsigned __int8)(v3 - 9) > 1u && v3 != 32 )
{
if ( v3 != 34 )
{
v6 = *v2;
v5 = 0;
LABEL_9:
v7 = v2 + 1;
v8 = (char *)v2;
while ( (unsigned __int8)(v6 - 9) > 1u && v6 != 32 || v5 )
{
if ( v6 == 34 )
{
v5 ^= 1u;
v9 = v7;
}
else
{
v9 = v7;
*v8++ = v6;
}
v6 = *v9;
++v7;
if ( !*v9 )
goto LABEL_19;
}
v9 = &v7[-(v6 == 0)];
goto LABEL_19;
}
goto LABEL_10;
}
do
{
do
v4 = *++v2;
while ( (unsigned __int8)(*v2 - 9) <= 1u );
}
while ( v4 == 32 );
if ( !v4 )
return 0LL;
v5 = 0;
if ( v4 == 34 )
{
LABEL_10:
++v2;
v5 = 1;
}
v6 = *v2;
if ( *v2 )
goto LABEL_9;
v8 = (char *)v2;
v9 = v2;
LABEL_19:
*v8 = 0;
*a1 = v9;
if ( v2 == v8 + 1 )
result = 0LL;
else
result = strdup(v2);
return result;
}

canonical_decompiled_code:
char *__fastcall func1(const char **a1)
{
const char *v2; // rdi
char v3; // al
char v4; // al
int v5; // er9
char v6; // al
const char *v7; // rdx
char *v8; // rsi
const char *v9; // rcx
char *result; // rax

v2 = *a1;
v3 = *v2;
if ( !*v2 )
return 0LL;
if ( (unsigned __int8)(v3 - 9) > 1u && v3 != 32 )
{
if ( v3 != 34 )
{
v6 = *v2;
v5 = 0;
LABEL_9:
v7 = v2 + 1;
v8 = (char *)v2;
while ( (unsigned __int8)(v6 - 9) > 1u && v6 != 32 || v5 )
{
if ( v6 == 34 )
{
v5 ^= 1u;
v9 = v7;
}
else
{
v9 = v7;
*v8++ = v6;
}
v6 = *v9;
++v7;
if ( !*v9 )
goto LABEL_19;
}
v9 = &v7[-(v6 == 0)];
goto LABEL_19;
}
goto LABEL_10;
}
do
{
do
v4 = *++v2;
while ( (unsigned __int8)(*v2 - 9) <= 1u );
}
while ( v4 == 32 );
if ( !v4 )
return 0LL;
v5 = 0;
if ( v4 == 34 )
{
LABEL_10:
++v2;
v5 = 1;
}
v6 = *v2;
if ( *v2 )
goto LABEL_9;
v8 = (char *)v2;
v9 = v2;
LABEL_19:
*v8 = 0;
*a1 = v9;
if ( v2 == v8 + 1 )
result = 0LL;
else
result = strdup(v2);
return result;
}

original_code:
static char *
next_token( char **sp )
{
    int in_quote = 0;
    char *p, *tokstart, *t;
    if ( **sp == '\0' ) {
 return( ((void *)0) );
    }
    p = *sp;
    while ( (((unsigned char) *p) == ' ' || ((unsigned char) *p) == '\t' || ((unsigned char) *p) == '\n')) {
 ++p;
    }
    if ( *p == '\0' ) {
 return( ((void *)0) );
    }
    if ( *p == '\"' ) {
 in_quote = 1;
 ++p;
    }
    t = tokstart = p;
    for ( ;; ) {
 if ( *p == '\0' || ( (((unsigned char) *p) == ' ' || ((unsigned char) *p) == '\t' || ((unsigned char) *p) == '\n') && !in_quote )) {
     if ( *p != '\0' ) {
  ++p;
     }
     *t++ = '\0';
     break;
 }
 if ( *p == '\"' ) {
     in_quote = !in_quote;
     ++p;
 } else {
     *t++ = *p++;
 }
    }
    *sp = p;
    if ( t == tokstart ) {
 return( ((void *)0) );
    }
    return( (strdup(tokstart)));
}

canonical_original_code:
static char *next_token(char **sp)
{
    int in_quote = 0;
    char *p; char *tokstart; char *t;
    if ( **sp == '\0' ) {
 return( ((void *)0) );
    }
    p = *sp;
    while ( (((unsigned char) *p) == ' ' || ((unsigned char) *p) == '\t' || ((unsigned char) *p) == '\n')) {
 ++p;
    }
    if ( *p == '\0' ) {
 return( ((void *)0) );
    }
    if ( *p == '\"' ) {
 in_quote = 1;
 ++p;
    }
    t = tokstart = p;
    for ( ;; ) {
 if ( *p == '\0' || ( (((unsigned char) *p) == ' ' || ((unsigned char) *p) == '\t' || ((unsigned char) *p) == '\n') && !in_quote )) {
     if ( *p != '\0' ) {
  ++p;
     }
     *t++ = '\0';
     break;
 }
 if ( *p == '\"' ) {
     in_quote = !in_quote;
     ++p;
 } else {
     *t++ = *p++;
 }
    }
    *sp = p;
    if ( t == tokstart ) {
 return( ((void *)0) );
    }
    return( (strdup(tokstart)));
}

variable_types:
sp
char **

variable_types:
tokstart
char *

variable_types:
t
char *


decompiled_code:
__int64 __fastcall ldap_int_next_line_tokens(_BYTE **a1, __int64 *a2, const char ***a3)
{
unsigned int i; // er13
const char **v5; // rbx
__int64 v6; // rbp
char *v7; // r12
void *ptr; // [rsp+0h] [rbp-38h] BYREF
const char *v10; // [rsp+8h] [rbp-30h] BYREF

*a3 = 0LL;
i = next_line(a1, a2, (const char **)&ptr);
if ( (int)i > 0 )
{
v5 = (const char **)calloc(1uLL, 8uLL);
if ( v5 )
{
v10 = (const char *)ptr;
v6 = 0LL;
for ( i = 0; ; ++i )
{
v7 = next_token(&v10);
if ( !v7 )
break;
v5 = (const char **)realloc(v5, v6 * 8 + 16);
if ( !v5 )
goto LABEL_7;
v5[v6] = v7;
v5[++v6] = 0LL;
}
if ( i == 1 )
{
if ( !strcasecmp(*v5, "END") )
{
free(v5);
free(ptr);
return 0;
}
free(ptr);
}
else
{
free(ptr);
if ( !i )
{
free(v5);
return i;
}
}
*a3 = v5;
return i;
}
LABEL_7:
free(ptr);
i = -1;
}
return i;
}

canonical_decompiled_code:
__int64 __fastcall func2(_BYTE **a1, __int64 *a2, const char ***a3)
{
unsigned int i; // er13
const char **v5; // rbx
__int64 v6; // rbp
char *v7; // r12
void *ptr; // [rsp+0h] [rbp-38h] BYREF
const char *v10; // [rsp+8h] [rbp-30h] BYREF

*a3 = 0LL;
i = func0(a1, a2, (const char **)&ptr);
if ( (int)i > 0 )
{
v5 = (const char **)calloc(1uLL, 8uLL);
if ( v5 )
{
v10 = (const char *)ptr;
v6 = 0LL;
for ( i = 0; ; ++i )
{
v7 = func1(&v10);
if ( !v7 )
break;
v5 = (const char **)realloc(v5, v6 * 8 + 16);
if ( !v5 )
goto LABEL_7;
v5[v6] = v7;
v5[++v6] = 0LL;
}
if ( i == 1 )
{
if ( !strcasecmp(*v5, "END") )
{
free(v5);
free(ptr);
return 0;
}
free(ptr);
}
else
{
free(ptr);
if ( !i )
{
free(v5);
return i;
}
}
*a3 = v5;
return i;
}
LABEL_7:
free(ptr);
i = -1;
}
return i;
}

original_code:
int
ldap_int_next_line_tokens( char **bufp, ber_len_t *blenp, char ***toksp )
{
    char *p, *line, *token, **toks;
    int rc, tokcnt;
    *toksp = ((void *)0);
    if (( rc = next_line( bufp, blenp, &line )) <= 0 ) {
 return( rc );
    }
    if (( toks = (char **)(calloc((1),(sizeof( char * ))))) == ((void *)0) ) {
 (free(line));
 return( -1 );
    }
    tokcnt = 0;
    p = line;
    while (( token = next_token( &p )) != ((void *)0) ) {
 if (( toks = (char **)(realloc((toks),(( tokcnt + 2 ) * sizeof( char * ))))) == ((void *)0) ) {
     (free((char *)toks));
     (free(line));
     return( -1 );
 }
 toks[ tokcnt ] = token;
 toks[ ++tokcnt ] = ((void *)0);
    }
    if ( tokcnt == 1 && strcasecmp( toks[ 0 ], "END" ) == 0 ) {
 tokcnt = 0;
 (free(toks));
 toks = ((void *)0);
    }
    (free(line));
    if ( tokcnt == 0 ) {
 if ( toks != ((void *)0) ) {
     (free((char *)toks));
 }
    } else {
 *toksp = toks;
    }
    return( tokcnt );
}

canonical_original_code:
int ldap_int_next_line_tokens(char **bufp, unsigned long *blenp, char ***toksp)
{
    char *p; char *line; char *token; char **toks;
    int rc; int tokcnt;
    *toksp = ((void *)0);
    if (( rc = next_line( bufp, blenp, &line )) <= 0 ) {
 return( rc );
    }
    if (( toks = (char **)(calloc((1),(sizeof( char * ))))) == ((void *)0) ) {
 (free(line));
 return( -1 );
    }
    tokcnt = 0;
    p = line;
    while (( token = next_token( &p )) != ((void *)0) ) {
 if (( toks = (char **)(realloc((toks),(( tokcnt + 2 ) * sizeof( char * ))))) == ((void *)0) ) {
     (free((char *)toks));
     (free(line));
     return( -1 );
 }
 toks[ tokcnt ] = token;
 toks[ ++tokcnt ] = ((void *)0);
    }
    if ( tokcnt == 1 && strcasecmp( toks[ 0 ], "END" ) == 0 ) {
 tokcnt = 0;
 (free(toks));
 toks = ((void *)0);
    }
    (free(line));
    if ( tokcnt == 0 ) {
 if ( toks != ((void *)0) ) {
     (free((char *)toks));
 }
    } else {
 *toksp = toks;
    }
    return( tokcnt );
}

variable_types:
toksp
char ***

variable_types:
p
char *

variable_types:
token
char *

variable_types:
toks
char **

variable_types: rc int

variable_types: tokcnt int


decompiled_code:
void __fastcall ldap_memfree(void *a1)
{
free(a1);
}

canonical_decompiled_code:
void __fastcall func0(void *a1)
{
free(a1);
}

original_code:
void
ldap_memfree( void *p )
{
 (free(p));
}

canonical_original_code:
void ldap_memfree(void *p)
{
 (free(p));
}

variable_types:
p
void *


decompiled_code:
void __fastcall ldap_memvfree(void *a1)
{
free(a1);
}

canonical_decompiled_code:
void __fastcall func1(void *a1)
{
free(a1);
}

original_code:
void
ldap_memvfree( void **v )
{
 (free(v));
}

canonical_original_code:
void ldap_memvfree(void **v)
{
 (free(v));
}

variable_types:
v
void **


decompiled_code:
void *__fastcall ldap_memalloc(size_t a1)
{
return malloc(a1);
}

canonical_decompiled_code:
void *__fastcall func2(size_t a1)
{
return malloc(a1);
}

original_code:
void *
ldap_memalloc( ber_len_t s )
{
 return (malloc(s));
}

canonical_original_code:
void *ldap_memalloc(unsigned long s)
{
 return (malloc(s));
}

variable_types:
s
unsigned long

return_type: void *


decompiled_code:
void *__fastcall ldap_memcalloc(size_t a1, size_t a2)
{
return calloc(a1, a2);
}

canonical_decompiled_code:
void *__fastcall func3(size_t a1, size_t a2)
{
return calloc(a1, a2);
}

original_code:
void *
ldap_memcalloc( ber_len_t n, ber_len_t s )
{
 return (calloc((n),(s)));
}

canonical_original_code:
void *ldap_memcalloc(unsigned long n, unsigned long s)
{
 return (calloc((n),(s)));
}

variable_types:
n
unsigned long


decompiled_code:
void *__fastcall ldap_memrealloc(void *a1, size_t a2)
{
return realloc(a1, a2);
}

canonical_decompiled_code:
void *__fastcall func4(void *a1, size_t a2)
{
return realloc(a1, a2);
}

original_code:
void *
ldap_memrealloc( void* p, ber_len_t s )
{
 return (realloc((p),(s)));
}

canonical_original_code:
void *ldap_memrealloc(void *p, unsigned long s)
{
 return (realloc((p),(s)));
}


decompiled_code:
char *__fastcall ldap_strdup(const char *a1)
{
return strdup(a1);
}

canonical_decompiled_code:
char *__fastcall func5(const char *a1)
{
return strdup(a1);
}

original_code:
char *
ldap_strdup( const char *p )
{
 return (strdup(p));
}

canonical_original_code:
char *ldap_strdup(char *p)
{
 return (strdup(p));
}


original_code:
void
ldap_getfilter_free( LDAPFiltDesc *lfdp )
{
    LDAPFiltList *flp, *nextflp;
    LDAPFiltInfo *fip, *nextfip;
    for ( flp = lfdp->lfd_filtlist; flp != ((void *)0); flp = nextflp ) {
 for ( fip = flp->lfl_ilist; fip != ((void *)0); fip = nextfip ) {
     nextfip = fip->lfi_next;
     (free(fip->lfi_filter));
     (free(fip->lfi_desc));
     (free(fip));
 }
 nextflp = flp->lfl_next;
 (free(flp->lfl_pattern));
 (free(flp->lfl_delims));
 (free(flp->lfl_tag));
 (free(flp));
    }
    if ( lfdp->lfd_curvalcopy != ((void *)0) ) {
 (free(lfdp->lfd_curvalcopy));
    }
    if ( lfdp->lfd_curvalwords != ((void *)0) ) {
 (free(lfdp->lfd_curvalwords));
    }
    if ( lfdp->lfd_filtprefix != ((void *)0) ) {
 (free(lfdp->lfd_filtprefix));
    }
    if ( lfdp->lfd_filtsuffix != ((void *)0) ) {
 (free(lfdp->lfd_filtsuffix));
    }
    (free(lfdp));
}

canonical_original_code:
void ldap_getfilter_free(struct ldap_filt_desc *lfdp)
{
    struct ldap_filt_list *flp; struct ldap_filt_list *nextflp;
    struct ldap_filt_info *fip; struct ldap_filt_info *nextfip;
    for ( flp = lfdp->lfd_filtlist; flp != ((void *)0); flp = nextflp ) {
 for ( fip = flp->lfl_ilist; fip != ((void *)0); fip = nextfip ) {
     nextfip = fip->lfi_next;
     (free(fip->lfi_filter));
     (free(fip->lfi_desc));
     (free(fip));
 }
 nextflp = flp->lfl_next;
 (free(flp->lfl_pattern));
 (free(flp->lfl_delims));
 (free(flp->lfl_tag));
 (free(flp));
    }
    if ( lfdp->lfd_curvalcopy != ((void *)0) ) {
 (free(lfdp->lfd_curvalcopy));
    }
    if ( lfdp->lfd_curvalwords != ((void *)0) ) {
 (free(lfdp->lfd_curvalwords));
    }
    if ( lfdp->lfd_filtprefix != ((void *)0) ) {
 (free(lfdp->lfd_filtprefix));
    }
    if ( lfdp->lfd_filtsuffix != ((void *)0) ) {
 (free(lfdp->lfd_filtsuffix));
    }
    (free(lfdp));
}

variable_types: struct ldap_filt_desc { struct ldap_filt_list { char *lfl_tag; char *lfl_pattern; char *lfl_delims; struct ldap_filt_info { char *lfi_filter; char *lfi_desc; int lfi_scope; int lfi_isexact; struct ldap_filt_info *lfi_next; } *lfl_ilist; struct ldap_filt_list *lfl_next; } *lfd_filtlist; struct ldap_filt_info *lfd_curfip; struct ldap_filt_info lfd_retfi; char lfd_filter[1024]; char *lfd_curval; char *lfd_curvalcopy; char **lfd_curvalwords; char *lfd_filtprefix; char *lfd_filtsuffix; } *


decompiled_code:
void __fastcall ldap_mods_free(void *ptr, int a2)
{
void *v2; // rbp
char *v3; // rax
void **v4; // rbx
void *v5; // rdi
void *v6; // rdi

if ( ptr )
{
v2 = ptr;
v3 = *(char **)ptr;
if ( *(_QWORD *)ptr )
{
v4 = (void **)ptr;
do
{
if ( *v3 >= 0 )
{
v5 = (void *)*((_QWORD *)v3 + 2);
if ( v5 )
free(v5);
}
else if ( *((_QWORD *)v3 + 2) )
{
ber_bvecfree();
}
v6 = (void *)*((_QWORD *)*v4 + 1);
if ( v6 )
free(v6);
free(*v4++);
v3 = (char *)*v4;
}
while ( *v4 );
}
if ( a2 )
free(v2);
}
}

canonical_decompiled_code:
void __fastcall func7(void *ptr, int a2)
{
void *v2; // rbp
char *v3; // rax
void **v4; // rbx
void *v5; // rdi
void *v6; // rdi

if ( ptr )
{
v2 = ptr;
v3 = *(char **)ptr;
if ( *(_QWORD *)ptr )
{
v4 = (void **)ptr;
do
{
if ( *v3 >= 0 )
{
v5 = (void *)*((_QWORD *)v3 + 2);
if ( v5 )
free(v5);
}
else if ( *((_QWORD *)v3 + 2) )
{
ber_bvecfree();
}
v6 = (void *)*((_QWORD *)*v4 + 1);
if ( v6 )
free(v6);
free(*v4++);
v3 = (char *)*v4;
}
while ( *v4 );
}
if ( a2 )
free(v2);
}
}

original_code:
void
ldap_mods_free( LDAPMod **mods, int freemods )
{
 int i;
 if ( mods == ((void *)0) )
  return;
 for ( i = 0; mods[i] != ((void *)0); i++ ) {
  if ( mods[i]->mod_op & (0x0080) ) {
   if( mods[i]->mod_vals.modv_bvals != ((void *)0) )
    ber_bvecfree( mods[i]->mod_vals.modv_bvals );
  } else if( mods[i]->mod_vals.modv_strvals != ((void *)0) ) {
   (free(mods[i]->mod_vals.modv_strvals));
  }
  if ( mods[i]->mod_type != ((void *)0) ) {
   (free(mods[i]->mod_type));
  }
  (free((char *) mods[i]));
 }
 if ( freemods ) {
  (free((char *) mods));
 }
}

canonical_original_code:
void ldap_mods_free(struct ldapmod **mods, int freemods)
{
 int i;
 if ( mods == ((void *)0) )
  return;
 for ( i = 0; mods[i] != ((void *)0); i++ ) {
  if ( mods[i]->mod_op & (0x0080) ) {
   if( mods[i]->mod_vals.modv_bvals != ((void *)0) )
    ber_bvecfree( mods[i]->mod_vals.modv_bvals );
  } else if( mods[i]->mod_vals.modv_strvals != ((void *)0) ) {
   (free(mods[i]->mod_vals.modv_strvals));
  }
  if ( mods[i]->mod_type != ((void *)0) ) {
   (free(mods[i]->mod_type));
  }
  (free((char *) mods[i]));
 }
 if ( freemods ) {
  (free((char *) mods));
 }
}

variable_types: struct ldapmod { int mod_op; char *mod_type; union mod_vals_u { char **modv_strvals; struct berval { unsigned long bv_len; char *bv_val; } **modv_bvals; } mod_vals; } **

variable_types: freemods int

variable_types: i int

user_defined_types: struct ldapmod { int mod_op; char *mod_type; union mod_vals_u mod_vals; }

user_defined_types: union mod_vals_u { char **modv_strvals; struct berval **modv_bvals; }


original_code:
static int int_strspn( const char *str, const char *delim )
{
 int pos;
 const char *p=delim;
 for( pos=0; (*str) ; pos++,str++) {
  if (*str!=*p) {
   for( p=delim; (*p) ; p++ ) {
    if (*str==*p) {
     break;
    }
     }
  }
  if (*p=='\0') {
   return pos;
  }
 }
 return pos;
}

canonical_original_code:
static int int_strspn(char *str, char *delim)
{
 int pos;
 char *p = delim;
 for( pos=0; (*str) ; pos++,str++) {
  if (*str!=*p) {
   for( p=delim; (*p) ; p++ ) {
    if (*str==*p) {
     break;
    }
     }
  }
  if (*p=='\0') {
   return pos;
  }
 }
 return pos;
}

variable_types:
str
char *

variable_types:
delim
char *

variable_types: pos int


setproctitle

decompiled_code:
char *__fastcall setproctitle(char *s, unsigned int a2)
{
const char *v2; // r13
char *v3; // rax
char *v4; // rbx
size_t v5; // r12
size_t v6; // rax
char *v7; // r12
int v8; // er13
char *v9; // rbx
unsigned __int64 v10; // rbp
__int64 v11; // rdx
char *result; // rax
const char *v13; // rbx
size_t v14; // rbx
size_t v15; // rax

if ( !endargv_0 )
{
v13 = *(const char **)(Argv + 8LL * Argc - 8);
endargv_0 = (__int64)&v13[strlen(v13)];
}
v2 = *(const char **)Argv;
v3 = strrchr(*(const char **)Argv, 47);
v4 = v3;
if ( v3 )
{
v5 = strlen(v3);
v6 = strlen(s);
v7 = (char *)calloc(v5 + v6 + 10, 1uLL);
v2 = v4 + 1;
}
else
{
v14 = strlen(v2);
v15 = strlen(s);
v7 = (char *)calloc(v14 + v15 + 10, 1uLL);
}
sprintf(v7, "-%s: %s %d", v2, s, a2);
v8 = strlen(v7);
v9 = *(char **)Argv;
v10 = endargv_0;
v11 = endargv_0 - *(_QWORD *)Argv;
if ( v11 <= v8 )
{
v8 = v11 - 1;
v7[(int)v11 - 1] = 0;
}
strcpy(v9, v7);
free(v7);
result = &v9[v8];
if ( v10 > (unsigned __int64)result )
{
do
*result++ = 32;
while ( (char *)v10 != result );
}
return result;
}

canonical_decompiled_code:
char *__fastcall func0(char *s, unsigned int a2)
{
const char *v2; // r13
char *v3; // rax
char *v4; // rbx
size_t v5; // r12
size_t v6; // rax
char *v7; // r12
int v8; // er13
char *v9; // rbx
unsigned __int64 v10; // rbp
__int64 v11; // rdx
char *result; // rax
const char *v13; // rbx
size_t v14; // rbx
size_t v15; // rax

if ( !endargv_0 )
{
v13 = *(const char **)(Argv + 8LL * Argc - 8);
endargv_0 = (__int64)&v13[strlen(v13)];
}
v2 = *(const char **)Argv;
v3 = strrchr(*(const char **)Argv, 47);
v4 = v3;
if ( v3 )
{
v5 = strlen(v3);
v6 = strlen(s);
v7 = (char *)calloc(v5 + v6 + 10, 1uLL);
v2 = v4 + 1;
}
else
{
v14 = strlen(v2);
v15 = strlen(s);
v7 = (char *)calloc(v14 + v15 + 10, 1uLL);
}
sprintf(v7, "-%s: %s %d", v2, s, a2);
v8 = strlen(v7);
v9 = *(char **)Argv;
v10 = endargv_0;
v11 = endargv_0 - *(_QWORD *)Argv;
if ( v11 <= v8 )
{
v8 = v11 - 1;
v7[(int)v11 - 1] = 0;
}
strcpy(v9, v7);
free(v7);
result = &v9[v8];
if ( v10 > (unsigned __int64)result )
{
do
*result++ = 32;
while ( (char *)v10 != result );
}
return result;
}

original_code:
void setproctitle(char *host, int port)
{
 static char *endargv = (char *)0;
 char *s, *buf;
 int i;
 if ( endargv == (char *)0 ) {
  endargv = Argv[Argc-1] + strlen(Argv[Argc-1]);
 }
    s = (char *)strrchr(Argv[0], '/');
    buf = (char *)calloc(strlen(s ? s : Argv[0]) + strlen(host) + 10, sizeof(char));
    sprintf(buf, "-%s: %s %d", s ? s + 1 : Argv[0], host, port);
 i = strlen(buf);
 s = Argv[0];
 if (i > endargv - s - 1) {
  i = endargv - s - 1;
  buf[i] = '\0';
 }
 strcpy(s, buf);
    free(buf);
 s += i;
 while (s < endargv) *s++ = ' ';
}

canonical_original_code:
void setproctitle(char *host, int port)
{
 static char *endargv = (char *)0;
 char *s; char *buf;
 int i;
 if ( endargv == (char *)0 ) {
  endargv = Argv[Argc-1] + strlen(Argv[Argc-1]);
 }
    s = (char *)strrchr(Argv[0], '/');
    buf = (char *)calloc(strlen(s ? s : Argv[0]) + strlen(host) + 10, sizeof(char));
    sprintf(buf, "-%s: %s %d", s ? s + 1 : Argv[0], host, port);
 i = strlen(buf);
 s = Argv[0];
 if (i > endargv - s - 1) {
  i = endargv - s - 1;
  buf[i] = '\0';
 }
 strcpy(s, buf);
    free(buf);
 s += i;
 while (s < endargv) *s++ = ' ';
}

variable_types:
host
char *

variable_types: port int

variable_types:
endargv
char *

variable_types:
s
char *

variable_types:
buf
char *


gettid

repo: jonatanlinden/PR

decompiled_code:
__int64 gettid()
{
return syscall(186LL);
}

canonical_decompiled_code:
__int64 func0()
{
return syscall(186LL);
}

original_code:
pid_t
gettid(void)
{
    return (pid_t) syscall(186);
}

canonical_original_code:
int gettid()
{
    return (int) syscall(186);
}


pin

decompiled_code:
int __fastcall pin(__pid_t pid, int a2)
{
int result; // eax
char *v3; // rax
cpu_set_t v4; // [rsp+0h] [rbp-88h] BYREF

memset(&v4, 0, sizeof(v4));
if ( (unsigned __int64)a2 <= 0x3FF )
v4.__bits[(unsigned __int64)a2 >> 6] |= 1LL << a2;
result = sched_setaffinity(pid, 0x80uLL, &v4);
if ( result )
{
v3 = strerror(result);
result = fputs(v3, stderr);
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(__pid_t pid, int a2)
{
int result; // eax
char *v3; // rax
cpu_set_t v4; // [rsp+0h] [rbp-88h] BYREF

memset(&v4, 0, sizeof(v4));
if ( (unsigned __int64)a2 <= 0x3FF )
v4.__bits[(unsigned __int64)a2 >> 6] |= 1LL << a2;
result = sched_setaffinity(pid, 0x80uLL, &v4);
if ( result )
{
v3 = strerror(result);
result = fputs(v3, stderr);
}
return result;
}

original_code:
void
pin(pid_t t, int cpu)
{
    cpu_set_t cpuset;
    do __builtin_memset (&cpuset, '\0', sizeof (cpu_set_t)); while (0);
    (__extension__ ({ size_t __cpu = (cpu); __cpu / 8 < (sizeof (cpu_set_t)) ? (((__cpu_mask *) ((&cpuset)->__bits))[((__cpu) / (8 * sizeof (__cpu_mask)))] |= ((__cpu_mask) 1 << ((__cpu) % (8 * sizeof (__cpu_mask))))) : 0; }));
    do { int _c = (sched_setaffinity(t, sizeof(cpu_set_t), &cpuset)); if (_c != 0) { fprintf(stderr, "%s", strerror(_c)); } } while (0);
}

canonical_original_code:
void pin(int t, int cpu)
{
    struct cpu_set_t cpuset;
    do __builtin_memset (&cpuset, '\0', sizeof (cpu_set_t)); while (0);
    (__extension__ ({ unsigned long __cpu = (cpu); __cpu / 8 < (sizeof (cpu_set_t)) ? (((unsigned long *) ((&cpuset)->__bits))[((__cpu) / (8 * sizeof (__cpu_mask)))] |= ((unsigned long) 1 << ((__cpu) % (8 * sizeof (__cpu_mask))))) : 0; }));
    do { int _c = (sched_setaffinity(t, sizeof(cpu_set_t), &cpuset)); if (_c != 0) { fprintf(stderr, "%s", strerror(_c)); } } while (0);
}

variable_types: t int

variable_types: cpu int

variable_types: struct cpu_set_t { unsigned long __bits[-1]; }


gettime

decompiled_code:
int __fastcall gettime(struct timespec *tp)
{
int result; // eax

result = clock_gettime(1, tp);
if ( result < 0 )
result = fprintf(stderr, "E: %s: %d: %s\n", "common.c", 23LL, "clock_gettime(CLOCK_MONOTONIC, ts)");
return result;
}

canonical_decompiled_code:
int __fastcall func2(struct timespec *tp)
{
int result; // eax

result = clock_gettime(1, tp);
if ( result < 0 )
result = fprintf(stderr, "E: %s: %d: %s\n", "common.c", 23LL, "clock_gettime(CLOCK_MONOTONIC, ts)");
return result;
}

original_code:
void
gettime(struct timespec *ts)
{
    do { int _c = (clock_gettime(1, ts)); if (_c < 0) { fprintf(stderr, "E: %s: %d: %s\n", "common.c", 23, "clock_gettime(CLOCK_MONOTONIC, ts)"); } } while (0);
}

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


timediff

decompiled_code:
__int64 __fastcall timediff(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
__int64 result; // rax

result = a3 - a1;
if ( a4 - a2 < 0 )
--result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
__int64 result; // rax

result = a3 - a1;
if ( a4 - a2 < 0 )
--result;
return result;
}

original_code:
struct timespec
timediff (struct timespec begin, struct timespec end)
{
    struct timespec tmp;
    if ((end.tv_nsec - begin.tv_nsec) < 0) {
 tmp.tv_sec = end.tv_sec - begin.tv_sec - 1;
 tmp.tv_nsec = 1000000000 + end.tv_nsec - begin.tv_nsec;
    } else {
 tmp.tv_sec = end.tv_sec - begin.tv_sec;
 tmp.tv_nsec = end.tv_nsec - begin.tv_nsec;
    }
    return tmp;
}

canonical_original_code:
struct timespec timediff(struct timespec begin, struct timespec end)
{
    struct timespec tmp;
    if ((end.tv_nsec - begin.tv_nsec) < 0) {
 tmp.tv_sec = end.tv_sec - begin.tv_sec - 1;
 tmp.tv_nsec = 1000000000 + end.tv_nsec - begin.tv_nsec;
    } else {
 tmp.tv_sec = end.tv_sec - begin.tv_sec;
 tmp.tv_nsec = end.tv_nsec - begin.tv_nsec;
    }
    return tmp;
}

variable_types:
begin
struct timespec

variable_types:
end
struct timespec

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


decompiled_code:
__int64 __fastcall rng_init(__int64 a1)
{
__syscall_slong_t v1; // rax
__int64 result; // rax
struct timespec v3; // [rsp+0h] [rbp-18h] BYREF

clock_gettime(0, &v3);
v1 = v3.tv_nsec;
*(_DWORD *)a1 = v3.tv_nsec;
result = v1 >> 32;
*(_WORD *)(a1 + 4) = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
__syscall_slong_t v1; // rax
__int64 result; // rax
struct timespec v3; // [rsp+0h] [rbp-18h] BYREF

clock_gettime(0, &v3);
v1 = v3.tv_nsec;
*(_DWORD *)a1 = v3.tv_nsec;
result = v1 >> 32;
*(_WORD *)(a1 + 4) = result;
return result;
}

original_code:
void
rng_init (unsigned short rng[3])
{
    struct timespec time;
    clock_gettime(0, &time);
    rng[0] = time.tv_nsec;
    rng[1] = time.tv_nsec >> 16;
    rng[2] = time.tv_nsec >> 32;
}

canonical_original_code:
void rng_init(unsigned short rng[3])
{
    struct timespec time;
    clock_gettime(0, &time);
    rng[0] = time.tv_nsec;
    rng[1] = time.tv_nsec >> 16;
    rng[2] = time.tv_nsec >> 32;
}

variable_types:
rng
unsigned short [3]

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


restructure

original_code:
static void
restructure(pq_t *pq)
{
    node_t *pred, *cur, *h;
    int i = 32 - 1;
    pred = pq->head;
    while (i > 0) {
        h = pq->head->next[i];
        __asm__ __volatile__ ("" : : : "memory");
        cur = pred->next[i];
        if (!(((uintptr_t)(h->next[0])) & 1)) {
            i--;
            continue;
        }
        while((((uintptr_t)(cur->next[0])) & 1)) {
            pred = cur;
            cur = pred->next[i];
        }
        ((void) (0));
        if (__sync_bool_compare_and_swap(&pq->head->next[i],h,cur))
            i--;
    }
}

canonical_original_code:
static void restructure(struct pq_t *pq)
{
    struct node_s *pred; struct node_s *cur; struct node_s *h;
    int i = 32 - 1;
    pred = pq->head;
    while (i > 0) {
        h = pq->head->next[i];
        __asm__ __volatile__ ("" : : : "memory");
        cur = pred->next[i];
        if (!(((unsigned long)(h->next[0])) & 1)) {
            i--;
            continue;
        }
        while((((unsigned long)(cur->next[0])) & 1)) {
            pred = cur;
            cur = pred->next[i];
        }
        ((void) (0));
        if (__sync_bool_compare_and_swap(&pq->head->next[i],h,cur))
            i--;
    }
}

variable_types: struct pq_t { int max_offset; int max_level; int nthreads; struct node_s { unsigned long k; int level; int inserting; void *v; struct node_s *next[1]; } *head; struct node_s *tail; char pad[128]; } *

variable_types: i int

user_defined_types: struct pq_t { int max_offset; int max_level; int nthreads; struct node_s *head; struct node_s *tail; char pad[128]; }


original_code:
static node_t *
alloc_node()
{
    node_t *n;
    unsigned int r = ptst->rand;
    ptst->rand = r * 1103515245 + 12345;
    r &= (1u << (32 - 1)) - 1;
    int level = __builtin_ctz(r) + 1;
    ((void) (0));
    n = gc_alloc(ptst, gc_id[level - 1]);
    n->level = level;
    n->inserting = 1;
    memset(n->next, 0, level * sizeof(node_t *));
    return n;
}

canonical_original_code:
static struct node_s *alloc_node()
{
    struct node_s *n;
    unsigned int r = ptst->rand;
    ptst->rand = r * 1103515245 + 12345;
    r &= (1u << (32 - 1)) - 1;
    int level = __builtin_ctz(r) + 1;
    ((void) (0));
    n = gc_alloc(ptst, gc_id[level - 1]);
    n->level = level;
    n->inserting = 1;
    memset(n->next, 0, level * sizeof(struct node_s *));
    return n;
}

variable_types: struct node_s { unsigned long k; int level; int inserting; void *v; struct node_s *next[1]; } *

variable_types:
r
unsigned int

variable_types: level int


decompiled_code:
__int64 __fastcall free_node(__int64 a1)
{
return gc_free(ptst, a1, (unsigned int)gc_id[*(_DWORD *)(a1 + 8) - 1]);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
return gc_free(ptst, a1, (unsigned int)gc_id[*(_DWORD *)(a1 + 8) - 1]);
}

original_code:
static void
free_node(node_t *n)
{
    gc_free(ptst, (void *)n, gc_id[(n->level) - 1]);
}

canonical_original_code:
static void free_node(struct node_s *n)
{
    gc_free(ptst, (void *)n, gc_id[(n->level) - 1]);
}

user_defined_types: struct node_s { unsigned long k; int level; int inserting; void *v; struct node_s *next[1]; }


insert

original_code:
void
insert(pq_t *pq, pkey_t k, pval_t v)
{
    node_t *preds[32], *succs[32];
    node_t *new = ((void *)0), *del = ((void *)0);
    ((void) (0));
    critical_enter();
    new = alloc_node();
    new->k = k;
    new->v = v;
 retry:
    del = locate_preds(pq, k, preds, succs);
    if (succs[0]->k == k && !(((uintptr_t)(preds[0]->next[0])) & 1) && preds[0]->next[0] == succs[0]) {
        new->inserting = 0;
        free_node(new);
        goto out;
    }
    new->next[0] = succs[0];
    if (!__sync_bool_compare_and_swap(&preds[0]->next[0], succs[0], new)) {
        goto retry;
    }
    int i = 1;
    while ( i < new->level)
    {
        if ((((uintptr_t)(new->next[0])) & 1) ||
            (((uintptr_t)(succs[i]->next[0])) & 1) ||
            del == succs[i])
            goto success;
        new->next[i] = succs[i];
        if (!__sync_bool_compare_and_swap(&preds[i]->next[i], succs[i], new))
        {
            del = locate_preds(pq, k, preds, succs);
            if (succs[0] != new) goto success;
        } else {
            i++;
        }
    }
 success:
    if (new) {
        new->inserting = 0;
    }
 out:
    gc_exit(ptst);
}

canonical_original_code:
void insert(struct pq_t *pq, unsigned long k, void *v)
{
    struct node_s *preds[32]; struct node_s *succs[32];
    struct node_s *new = ((void *)0); struct node_s *del = ((void *)0);
    ((void) (0));
    critical_enter();
    new = alloc_node();
    new->k = k;
    new->v = v;
 retry:
    del = locate_preds(pq, k, preds, succs);
    if (succs[0]->k == k && !(((unsigned long)(preds[0]->next[0])) & 1) && preds[0]->next[0] == succs[0]) {
        new->inserting = 0;
        free_node(new);
        goto out;
    }
    new->next[0] = succs[0];
    if (!__sync_bool_compare_and_swap(&preds[0]->next[0], succs[0], new)) {
        goto retry;
    }
    int i = 1;
    while ( i < new->level)
    {
        if ((((unsigned long)(new->next[0])) & 1) ||
            (((unsigned long)(succs[i]->next[0])) & 1) ||
            del == succs[i])
            goto success;
        new->next[i] = succs[i];
        if (!__sync_bool_compare_and_swap(&preds[i]->next[i], succs[i], new))
        {
            del = locate_preds(pq, k, preds, succs);
            if (succs[0] != new) goto success;
        } else {
            i++;
        }
    }
 success:
    if (new) {
        new->inserting = 0;
    }
 out:
    gc_exit(ptst);
}

variable_types:
k
unsigned long

variable_types:
v
void *

variable_types: struct node_s { unsigned long k; int level; int inserting; void *v; struct node_s *next[1]; } *[32]


deletemin

decompiled_code:
__int64 __fastcall deletemin(__int64 a1)
{
unsigned __int64 v2; // rdx
signed __int64 v3; // r12
int v4; // edi
unsigned __int64 v5; // rbx
unsigned __int64 v6; // rcx
__int64 v7; // r13
signed __int64 v9; // rsi
signed __int64 v10; // rax
signed __int64 v11; // rtt
__int64 v12; // rdx
unsigned __int64 v13; // r12
unsigned __int64 v14; // rbx
__int64 v15; // rdi

critical_enter();
v2 = *(_QWORD *)(a1 + 16);
v3 = *(_QWORD *)(v2 + 24);
v4 = 0;
v5 = 0LL;
while ( 1 )
{
++v4;
v9 = *(_QWORD *)(v2 + 24);
v6 = v9 & 0xFFFFFFFFFFFFFFFELL;
if ( (v9 & 0xFFFFFFFFFFFFFFFELL) == *(_QWORD *)(a1 + 24) )
{
v7 = 0LL;
goto LABEL_7;
}
if ( !v5 && *(_DWORD *)(v2 + 12) )
v5 = v2;
if ( (v9 & 1) != 0 )
break;
v10 = *(_QWORD *)(v2 + 24);
do
{
v9 = v10;
v11 = v10;
v10 = _InterlockedCompareExchange64((volatile signed __int64 *)(v2 + 24), v10 | 1, v10);
}
while ( v11 != v10 );
v6 = v9 & 0xFFFFFFFFFFFFFFFELL;
if ( (v9 & 0xFFFFFFFFFFFFFFFELL) == 0 )
{
v6 = 0LL;
goto LABEL_17;
}
if ( (v9 & 1) == 0 )
goto LABEL_17;
LABEL_9:
v2 = v6;
}
if ( (v9 & 0xFFFFFFFFFFFFFFFELL) != 0 )
goto LABEL_9;
v6 = 0LL;
LABEL_17:
v7 = *(_QWORD *)(v6 + 16);
if ( !v5 )
v5 = v6;
if ( *(_DWORD *)a1 < v4 )
{
v12 = *(_QWORD *)(a1 + 16);
if ( *(_QWORD *)(v12 + 24) == v3
&& v3 == _InterlockedCompareExchange64((volatile signed __int64 *)(v12 + 24), v5 | 1, v3) )
{
restructure(a1);
v13 = v3 & 0xFFFFFFFFFFFFFFFELL;
v14 = v5 & 0xFFFFFFFFFFFFFFFELL;
while ( v13 != v14 )
{
v15 = v13;
v13 = *(_QWORD *)(v13 + 24) & 0xFFFFFFFFFFFFFFFELL;
free_node(v15);
}
}
}
LABEL_7:
gc_exit(ptst, v9);
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
unsigned __int64 v2; // rdx
signed __int64 v3; // r12
int v4; // edi
unsigned __int64 v5; // rbx
unsigned __int64 v6; // rcx
__int64 v7; // r13
signed __int64 v9; // rsi
signed __int64 v10; // rax
signed __int64 v11; // rtt
__int64 v12; // rdx
unsigned __int64 v13; // r12
unsigned __int64 v14; // rbx
__int64 v15; // rdi

critical_enter();
v2 = *(_QWORD *)(a1 + 16);
v3 = *(_QWORD *)(v2 + 24);
v4 = 0;
v5 = 0LL;
while ( 1 )
{
++v4;
v9 = *(_QWORD *)(v2 + 24);
v6 = v9 & 0xFFFFFFFFFFFFFFFELL;
if ( (v9 & 0xFFFFFFFFFFFFFFFELL) == *(_QWORD *)(a1 + 24) )
{
v7 = 0LL;
goto LABEL_7;
}
if ( !v5 && *(_DWORD *)(v2 + 12) )
v5 = v2;
if ( (v9 & 1) != 0 )
break;
v10 = *(_QWORD *)(v2 + 24);
do
{
v9 = v10;
v11 = v10;
v10 = _InterlockedCompareExchange64((volatile signed __int64 *)(v2 + 24), v10 | 1, v10);
}
while ( v11 != v10 );
v6 = v9 & 0xFFFFFFFFFFFFFFFELL;
if ( (v9 & 0xFFFFFFFFFFFFFFFELL) == 0 )
{
v6 = 0LL;
goto LABEL_17;
}
if ( (v9 & 1) == 0 )
goto LABEL_17;
LABEL_9:
v2 = v6;
}
if ( (v9 & 0xFFFFFFFFFFFFFFFELL) != 0 )
goto LABEL_9;
v6 = 0LL;
LABEL_17:
v7 = *(_QWORD *)(v6 + 16);
if ( !v5 )
v5 = v6;
if ( *(_DWORD *)a1 < v4 )
{
v12 = *(_QWORD *)(a1 + 16);
if ( *(_QWORD *)(v12 + 24) == v3
&& v3 == _InterlockedCompareExchange64((volatile signed __int64 *)(v12 + 24), v5 | 1, v3) )
{
func1(a1);
v13 = v3 & 0xFFFFFFFFFFFFFFFELL;
v14 = v5 & 0xFFFFFFFFFFFFFFFELL;
while ( v13 != v14 )
{
v15 = v13;
v13 = *(_QWORD *)(v13 + 24) & 0xFFFFFFFFFFFFFFFELL;
func3(v15);
}
}
}
LABEL_7:
gc_exit(ptst, v9);
return v7;
}

original_code:
pval_t
deletemin(pq_t *pq)
{
    pval_t v = ((void *)0);
    node_t *x, *nxt, *obs_head = ((void *)0), *newhead, *cur;
    int offset, lvl;
    newhead = ((void *)0);
    offset = lvl = 0;
    critical_enter();
    x = pq->head;
    obs_head = x->next[0];
    do {
        offset++;
        nxt = x->next[0];
        if (((void *)(((uintptr_t)(nxt)) & ~1)) == pq->tail) {
            goto out;
        }
        if (newhead == ((void *)0) && x->inserting) newhead = x;
        if ((((uintptr_t)(nxt)) & 1)) continue;
        nxt = __sync_fetch_and_or(&x->next[0], 1);
    }
    while ( (x = ((void *)(((uintptr_t)(nxt)) & ~1))) && (((uintptr_t)(nxt)) & 1) );
    ((void) (0));
    v = x->v;
    if (newhead == ((void *)0)) newhead = x;
    if (offset <= pq->max_offset) goto out;
    if (pq->head->next[0] != obs_head) goto out;
    if (__sync_bool_compare_and_swap(&pq->head->next[0], obs_head, ((void *)(((uintptr_t)(newhead)) | 1))))
    {
        restructure(pq);
        cur = ((void *)(((uintptr_t)(obs_head)) & ~1));
        while (cur != ((void *)(((uintptr_t)(newhead)) & ~1))) {
            nxt = ((void *)(((uintptr_t)(cur->next[0])) & ~1));
            ((void) (0));
            free_node(cur);
            cur = nxt;
        }
    }
 out:
    gc_exit(ptst);
    return v;
}

canonical_original_code:
void *deletemin(struct pq_t *pq)
{
    void *v = ((void *)0);
    struct node_s *x; struct node_s *nxt; struct node_s *obs_head = ((void *)0); struct node_s *newhead; struct node_s *cur;
    int offset; int lvl;
    newhead = ((void *)0);
    offset = lvl = 0;
    critical_enter();
    x = pq->head;
    obs_head = x->next[0];
    do {
        offset++;
        nxt = x->next[0];
        if (((void *)(((unsigned long)(nxt)) & ~1)) == pq->tail) {
            goto out;
        }
        if (newhead == ((void *)0) && x->inserting) newhead = x;
        if ((((unsigned long)(nxt)) & 1)) continue;
        nxt = __sync_fetch_and_or(&x->next[0], 1);
    }
    while ( (x = ((void *)(((unsigned long)(nxt)) & ~1))) && (((unsigned long)(nxt)) & 1) );
    ((void) (0));
    v = x->v;
    if (newhead == ((void *)0)) newhead = x;
    if (offset <= pq->max_offset) goto out;
    if (pq->head->next[0] != obs_head) goto out;
    if (__sync_bool_compare_and_swap(&pq->head->next[0], obs_head, ((void *)(((unsigned long)(newhead)) | 1))))
    {
        restructure(pq);
        cur = ((void *)(((unsigned long)(obs_head)) & ~1));
        while (cur != ((void *)(((unsigned long)(newhead)) & ~1))) {
            nxt = ((void *)(((unsigned long)(cur->next[0])) & ~1));
            ((void) (0));
            free_node(cur);
            cur = nxt;
        }
    }
 out:
    gc_exit(ptst);
    return v;
}

variable_types: offset int

variable_types: lvl int


decompiled_code:
void __fastcall pq_destroy(void *ptr)
{
_QWORD *v2; // rbp
__int64 v3; // rdi

v2 = (_QWORD *)*((_QWORD *)ptr + 2);
if ( v2 == *((_QWORD **)ptr + 3) )
{
v2 = (_QWORD *)*((_QWORD *)ptr + 3);
}
else
{
do
{
v3 = (__int64)v2;
v2 = (_QWORD *)(v2[3] & 0xFFFFFFFFFFFFFFFELL);
free_node(v3);
}
while ( *((_QWORD **)ptr + 3) != v2 );
}
free(v2);
free(*((void **)ptr + 2));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func6(void *ptr)
{
_QWORD *v2; // rbp
__int64 v3; // rdi

v2 = (_QWORD *)*((_QWORD *)ptr + 2);
if ( v2 == *((_QWORD **)ptr + 3) )
{
v2 = (_QWORD *)*((_QWORD *)ptr + 3);
}
else
{
do
{
v3 = (__int64)v2;
v2 = (_QWORD *)(v2[3] & 0xFFFFFFFFFFFFFFFELL);
func3(v3);
}
while ( *((_QWORD **)ptr + 3) != v2 );
}
free(v2);
free(*((void **)ptr + 2));
free(ptr);
}

original_code:
void
pq_destroy(pq_t *pq)
{
    node_t *cur, *pred;
    cur = pq->head;
    while (cur != pq->tail) {
        pred = cur;
        cur = ((void *)(((uintptr_t)(pred->next[0])) & ~1));
        free_node(pred);
    }
    free(pq->tail);
    free(pq->head);
    free(pq);
}

canonical_original_code:
void pq_destroy(struct pq_t *pq)
{
    struct node_s *cur; struct node_s *pred;
    cur = pq->head;
    while (cur != pq->tail) {
        pred = cur;
        cur = ((void *)(((unsigned long)(pred->next[0])) & ~1));
        free_node(pred);
    }
    free(pq->tail);
    free(pq->head);
    free(pq);
}


decompiled_code:
void *__fastcall removemin_thread(void *a1)
{
int v1; // ebx
unsigned __int64 v2; // rbp
unsigned __int64 v3; // rax
unsigned __int64 v4; // rdx

v1 = 30;
v2 = 0LL;
do
{
v3 = deletemin(pq);
v4 = v2;
v2 = v3;
if ( v3 <= v4 )
_assert_fail("v > ov", "unittests.c", 0xF3u, "removemin_thread");
--v1;
}
while ( v1 );
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func0(void *a1)
{
int v1; // ebx
unsigned __int64 v2; // rbp
unsigned __int64 v3; // rax
unsigned __int64 v4; // rdx

v1 = 30;
v2 = 0LL;
do
{
v3 = deletemin(pq);
v4 = v2;
v2 = v3;
if ( v3 <= v4 )
_assert_fail("v > ov", "unittests.c", 0xF3u, "removemin_thread");
--v1;
}
while ( v1 );
return 0LL;
}

original_code:
void *
removemin_thread(void *id)
{
    unsigned long v, ov = 0;
    for(int i = 0; i < 30; i++) {
 v = (unsigned long) deletemin(pq);
 ((v > ov) ? (void) (0) : __assert_fail ("v > ov", "unittests.c", 243, __extension__ __PRETTY_FUNCTION__));
 ov = v;
    }
    return ((void *)0);
}

canonical_original_code:
void *removemin_thread(void *id)
{
    unsigned long v; unsigned long ov = 0;
    for(int i = 0; i < 30; i++) {
 v = (unsigned long) deletemin(pq);
 ((v > ov) ? (void) (0) : __assert_fail ("v > ov", "unittests.c", 243, __extension__ __PRETTY_FUNCTION__));
 ov = v;
    }
    return ((void *)0);
}

variable_types:
id
void *

variable_types:
v
unsigned long

variable_types:
ov
unsigned long