decompiled_code:
char *__fastcall create_entry(char *src, char *a2)
{
char *v2; // rbx
void *v3; // rax

v2 = (char *)malloc(0x120uLL);
strcpy(v2 + 16, a2);
*(_QWORD *)v2 = 0LL;
*((_QWORD *)v2 + 1) = 0LL;
*((_DWORD *)v2 + 70) = 1;
v3 = malloc(0x118uLL);
*((_QWORD *)v2 + 34) = v3;
*(_QWORD *)v3 = v3;
*((_QWORD *)v3 + 1) = v3;
*((_DWORD *)v3 + 68) = 1;
strcpy((char *)v3 + 16, src);
return v2;
}

original_code:
struct entry * create_entry(const char * path, const char *dirname){
  struct entry *new_entry=malloc(sizeof(struct entry));
  strcpy(new_entry->dirname,dirname);
  new_entry->prev=((void *)0);
  new_entry->next=((void *)0);
  new_entry->state=1;
  new_entry->first_candidate=malloc(sizeof(struct candidate));
  new_entry->first_candidate->prev=new_entry->first_candidate;
  new_entry->first_candidate->next=new_entry->first_candidate;
  new_entry->first_candidate->score=1;
  strcpy(new_entry->first_candidate->path,path);
  return new_entry;
}

canonical_original_code:
struct entry *create_entry(char *path, char *dirname){
  struct entry *new_entry = malloc(sizeof(struct entry));
  strcpy(new_entry->dirname,dirname);
  new_entry->prev=((void *)0);
  new_entry->next=((void *)0);
  new_entry->state=1;
  new_entry->first_candidate=malloc(sizeof(struct candidate));
  new_entry->first_candidate->prev=new_entry->first_candidate;
  new_entry->first_candidate->next=new_entry->first_candidate;
  new_entry->first_candidate->score=1;
  strcpy(new_entry->first_candidate->path,path);
  return new_entry;
}

variable_types:
dirname
char *

return_type: struct entry { struct entry *prev; struct entry *next; char dirname[254]; struct candidate { struct candidate *prev; struct candidate *next; char path[254]; int score; } *first_candidate; int state; } *


hash

repo: rcatolino/gt

decompiled_code:
__int64 __fastcall hash(char *a1)
{
int v1; // eax
unsigned int v2; // edx
__int64 result; // rax

v1 = *a1;
if ( (unsigned int)(v1 - 65) <= 0x19 )
return (unsigned int)(v1 - 64);
v2 = v1 - 97;
result = (unsigned int)(v1 - 70);
if ( v2 >= 0x1A )
result = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *a1)
{
int v1; // eax
unsigned int v2; // edx
__int64 result; // rax

v1 = *a1;
if ( (unsigned int)(v1 - 65) <= 0x19 )
return (unsigned int)(v1 - 64);
v2 = v1 - 97;
result = (unsigned int)(v1 - 70);
if ( v2 >= 0x1A )
result = 0LL;
return result;
}

original_code:
int hash(const char * dirname){
  int letter = dirname[0];
  if(letter>64 && letter<91){
    return letter-64;
  }else if(letter>96 && letter<123){
    return letter-70;
  }else{
    return 0;
  }
  return 0;
}

canonical_original_code:
int hash(char *dirname){
  int letter = dirname[0];
  if(letter>64 && letter<91){
    return letter-64;
  }else if(letter>96 && letter<123){
    return letter-70;
  }else{
    return 0;
  }
  return 0;
}

variable_types: letter int

return_type: int


attach

decompiled_code:
int __fastcall attach(__int64 a1, unsigned int a2)
{
__int64 v2; // rax
__int64 v3; // rax
__int64 *v4; // rax

v2 = htable[a2];
if ( v2 )
{
fprintf(
stderr,
"attach entry %s, key %d. Existing entry for this key : %s\n",
(const char *)(a1 + 16),
a2,
(const char *)(v2 + 16));
*(_QWORD *)a1 = *(_QWORD *)htable[a2];
v3 = htable[a2];
*(_QWORD *)(a1 + 8) = v3;
*(_QWORD *)(*(_QWORD *)v3 + 8LL) = a1;
v4 = (__int64 *)htable[a2];
*v4 = a1;
}
else
{
LODWORD(v4) = fprintf(
stderr,
"attach entry %s, key %d. No previous entry for this key\n",
(const char *)(a1 + 16),
a2);
*(_QWORD *)a1 = a1;
*(_QWORD *)(a1 + 8) = a1;
}
htable[a2] = a1;
*(_DWORD *)(a1 + 280) = 0;
return (int)v4;
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1, unsigned int a2)
{
__int64 v2; // rax
__int64 v3; // rax
__int64 *v4; // rax

v2 = htable[a2];
if ( v2 )
{
fprintf(
stderr,
"attach entry %s, key %d. Existing entry for this key : %s\n",
(const char *)(a1 + 16),
a2,
(const char *)(v2 + 16));
*(_QWORD *)a1 = *(_QWORD *)htable[a2];
v3 = htable[a2];
*(_QWORD *)(a1 + 8) = v3;
*(_QWORD *)(*(_QWORD *)v3 + 8LL) = a1;
v4 = (__int64 *)htable[a2];
*v4 = a1;
}
else
{
LODWORD(v4) = fprintf(
stderr,
"attach entry %s, key %d. No previous entry for this key\n",
(const char *)(a1 + 16),
a2);
*(_QWORD *)a1 = a1;
*(_QWORD *)(a1 + 8) = a1;
}
htable[a2] = a1;
*(_DWORD *)(a1 + 280) = 0;
return (int)v4;
}

original_code:
void attach(struct entry * new_entry, int key){
  if(htable[key]==((void *)0)){
    fprintf(stderr,"attach entry %s, key %d. No previous entry for this key\n", new_entry->dirname, key);
    new_entry->prev=new_entry;
    new_entry->next=new_entry;
    htable[key]=new_entry;
  }else{
    fprintf(stderr,"attach entry %s, key %d. Existing entry for this key : %s\n", new_entry->dirname, key, htable[key]->dirname);
    new_entry->prev=htable[key]->prev;
    new_entry->next=htable[key];
    htable[key]->prev->next=new_entry;
    htable[key]->prev=new_entry;
    htable[key]=new_entry;
  }
  new_entry->state=0;
}

canonical_original_code:
void attach(struct entry *new_entry, int key){
  if(htable[key]==((void *)0)){
    fprintf(stderr,"attach entry %s, key %d. No previous entry for this key\n", new_entry->dirname, key);
    new_entry->prev=new_entry;
    new_entry->next=new_entry;
    htable[key]=new_entry;
  }else{
    fprintf(stderr,"attach entry %s, key %d. Existing entry for this key : %s\n", new_entry->dirname, key, htable[key]->dirname);
    new_entry->prev=htable[key]->prev;
    new_entry->next=htable[key];
    htable[key]->prev->next=new_entry;
    htable[key]->prev=new_entry;
    htable[key]=new_entry;
  }
  new_entry->state=0;
}

variable_types: struct entry { struct entry *prev; struct entry *next; char dirname[254]; struct candidate { struct candidate *prev; struct candidate *next; char path[254]; int score; } *first_candidate; int state; } *

variable_types: key int

return_type: void

user_defined_types: struct entry { struct entry *prev; struct entry *next; char dirname[254]; struct candidate *first_candidate; int state; }


decompiled_code:
__int64 __fastcall find_entry(char *s1, unsigned int a2)
{
__int64 v2; // r12
__int64 v3; // rbx

fprintf(stderr, "find_entry %s, key: %d\n", s1, a2);
v2 = htable[a2];
if ( v2 )
{
v3 = htable[a2];
while ( strcmp(s1, (const char *)(v3 + 16)) )
{
v3 = *(_QWORD *)(v3 + 8);
if ( v2 == v3 )
goto LABEL_5;
}
}
else
{
LABEL_5:
fwrite("find_entry: no such path\n", 1uLL, 0x19uLL, stderr);
v3 = 0LL;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *s1, unsigned int a2)
{
__int64 v2; // r12
__int64 v3; // rbx

fprintf(stderr, "find_entry %s, key: %d\n", s1, a2);
v2 = htable[a2];
if ( v2 )
{
v3 = htable[a2];
while ( strcmp(s1, (const char *)(v3 + 16)) )
{
v3 = *(_QWORD *)(v3 + 8);
if ( v2 == v3 )
goto LABEL_5;
}
}
else
{
LABEL_5:
fwrite("find_entry: no such path\n", 1uLL, 0x19uLL, stderr);
v3 = 0LL;
}
return v3;
}

original_code:
struct entry *find_entry(const char *dirname, int key){
  fprintf(stderr,"find_entry %s, key: %d\n",dirname, key);
  if (htable[key] != ((void *)0)){
    struct entry *current = htable[key];
    do {
      if (strcmp(dirname, current->dirname) == 0) {
        return current;
      }
      current = current->next;
    } while (current != htable[key]);
  }
  fprintf(stderr,"find_entry: no such path\n");
  return ((void *)0);
}

canonical_original_code:
struct entry *find_entry(char *dirname, int key){
  fprintf(stderr,"find_entry %s, key: %d\n",dirname, key);
  if (htable[key] != ((void *)0)){
    struct entry *current = htable[key];
    do {
      if (strcmp(dirname, current->dirname) == 0) {
        return current;
      }
      current = current->next;
    } while (current != htable[key]);
  }
  fprintf(stderr,"find_entry: no such path\n");
  return ((void *)0);
}

user_defined_types: struct candidate { struct candidate *prev; struct candidate *next; char path[254]; int score; }


decompiled_code:
__int64 __fastcall find_candidate(char *s1, __int64 a2)
{
__int64 v2; // rbp
__int64 v3; // rbx

v2 = *(_QWORD *)(a2 + 272);
if ( strcmp(s1, (const char *)(v2 + 16)) )
{
v3 = *(_QWORD *)(v2 + 8);
if ( v2 == v3 )
{
v2 = 0LL;
}
else
{
while ( strcmp(s1, (const char *)(v3 + 16)) )
{
v3 = *(_QWORD *)(v3 + 8);
if ( v2 == v3 )
return 0LL;
}
v2 = v3;
}
}
return v2;
}

original_code:
struct candidate *find_candidate(const char *path, struct entry *current_e) {
  struct candidate *current_c = current_e->first_candidate;
  if (strcmp(path, current_c->path) == 0){
    return current_c;
  }
  for (current_c = current_c->next; current_c != current_e->first_candidate; current_c = current_c->next){
    if(strcmp(path, current_c->path) == 0){
      return current_c;
    }
  }
  return ((void *)0);
}

canonical_original_code:
struct candidate *find_candidate(char *path, struct entry *current_e) {
  struct candidate *current_c = current_e->first_candidate;
  if (strcmp(path, current_c->path) == 0){
    return current_c;
  }
  for (current_c = current_c->next; current_c != current_e->first_candidate; current_c = current_c->next){
    if(strcmp(path, current_c->path) == 0){
      return current_c;
    }
  }
  return ((void *)0);
}

variable_types:
path
char *

variable_types: struct entry { struct entry *prev; struct entry *next; char dirname[254]; struct candidate *first_candidate; int state; } *

variable_types: struct candidate { struct candidate *prev; struct candidate *next; char path[254]; int score; } *


decompiled_code:
char *__fastcall update_entry_score(char *s1, char *a2, int a3)
{
unsigned int v4; // er14
__int64 v5; // rax
char *v6; // rbx
__int64 v7; // rax
int v8; // ebp

v4 = hash(a2);
v5 = find_entry(a2, v4);
if ( v5 )
{
v6 = (char *)v5;
v7 = find_candidate(s1, v5);
if ( v7 )
{
v8 = *(_DWORD *)(v7 + 272) + a3;
*(_DWORD *)(v7 + 272) = v8;
if ( *((_QWORD *)v6 + 34) != v7 && v8 > *(_DWORD *)(*(_QWORD *)v7 + 272LL) )
swap_left((__int64)v6, (_QWORD *)v7);
}
else
{
push_candidate((__int64)v6, s1);
*(_DWORD *)(**((_QWORD **)v6 + 34) + 272LL) = a3;
}
}
else
{
v6 = create_entry(s1, a2);
*(_DWORD *)(*((_QWORD *)v6 + 34) + 272LL) = a3;
attach((__int64)v6, v4);
}
return v6;
}

original_code:
struct entry *update_entry_score(const char *path, const char *dirname, unsigned long score) {
  struct entry *current;
  struct candidate *current_c;
  int key=hash(dirname);
  current=find_entry(dirname, key);
  if (!current){
    current=create_entry(path, dirname);
    current->first_candidate->score = score;
    attach(current, key);
    return current;
  }
  current_c=find_candidate(path, current);
  if(!current_c){
    push_candidate(current, path);
    current->first_candidate->prev->score = score;
  } else {
    current_c->score += score;
    if(current_c != current->first_candidate && current_c->score > current_c->prev->score){
      swap_left(current, current_c);
    }
  }
  return current;
}

canonical_original_code:
struct entry *update_entry_score(char *path, char *dirname, unsigned long score) {
  struct entry *current;
  struct candidate *current_c;
  int key = hash(dirname);
  current=find_entry(dirname, key);
  if (!current){
    current=create_entry(path, dirname);
    current->first_candidate->score = score;
    attach(current, key);
    return current;
  }
  current_c=find_candidate(path, current);
  if(!current_c){
    push_candidate(current, path);
    current->first_candidate->prev->score = score;
  } else {
    current_c->score += score;
    if(current_c != current->first_candidate && current_c->score > current_c->prev->score){
      swap_left(current, current_c);
    }
  }
  return current;
}

variable_types:
score
unsigned long


decompiled_code:
char *__fastcall update_entry(char *a1, char *a2)
{
return update_entry_score(a1, a2, 1);
}

canonical_decompiled_code:
char *__fastcall func8(char *a1, char *a2)
{
return func7(a1, a2, 1);
}

original_code:
struct entry *update_entry(const char *path, const char *dirname) {
  return update_entry_score(path, dirname, 1);
}

canonical_original_code:
struct entry *update_entry(char *path, char *dirname) {
  return update_entry_score(path, dirname, 1);
}


decompiled_code:
int end_serial()
{
int result; // eax

if ( hist )
{
result = fclose(hist);
if ( result )
{
perror("end_serial: fclose:");
result = -1;
}
else
{
hist = 0LL;
}
}
else
{
fwrite("end_serial: no histfile\n", 1uLL, 0x18uLL, stderr);
result = -1;
}
return result;
}

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

if ( hist )
{
result = fclose(hist);
if ( result )
{
perror("end_serial: fclose:");
result = -1;
}
else
{
hist = 0LL;
}
}
else
{
fwrite("end_serial: no histfile\n", 1uLL, 0x18uLL, stderr);
result = -1;
}
return result;
}

original_code:
int end_serial() {
  if(!hist) {
    fprintf(stderr,"end_serial: no histfile\n");
    return -1;
  }
  if(fclose(hist) != 0) {
    perror("end_serial: fclose:");
    return -1;
  }
  hist = ((void *)0);
  return 0;
}


decompiled_code:
int __fastcall init_serial(char *modes)
{
char *v2; // rax
const char *v3; // r12
size_t v4; // r13
char *v5; // rbp
FILE *v6; // rbx

if ( hist )
return end_serial();
v2 = getenv("HOME");
v3 = v2;
if ( v2 )
{
v4 = (int)(strlen(v2) + 11);
v5 = (char *)malloc(v4);
snprintf(v5, v4, "%s/.%s", v3, "gtd.hist");
}
else
{
v5 = (char *)malloc(0xEuLL);
snprintf(v5, 0xEuLL, "%s/%s", "/etc", "gtd.hist");
}
fprintf(stderr, "using histfile : %s\n", v5);
v6 = fopen(v5, modes);
hist = v6;
free(v5);
if ( v6 )
return 0;
perror("init_serial: fopen");
return -1;
}

canonical_decompiled_code:
int __fastcall func1(char *modes)
{
char *v2; // rax
const char *v3; // r12
size_t v4; // r13
char *v5; // rbp
FILE *v6; // rbx

if ( hist )
return func0();
v2 = getenv("HOME");
v3 = v2;
if ( v2 )
{
v4 = (int)(strlen(v2) + 11);
v5 = (char *)malloc(v4);
snprintf(v5, v4, "%s/.%s", v3, "gtd.hist");
}
else
{
v5 = (char *)malloc(0xEuLL);
snprintf(v5, 0xEuLL, "%s/%s", "/etc", "gtd.hist");
}
fprintf(stderr, "using histfile : %s\n", v5);
v6 = fopen(v5, modes);
hist = v6;
free(v5);
if ( v6 )
return 0;
perror("init_serial: fopen");
return -1;
}

original_code:
int init_serial(char * mode) {
  char * filename;
  char * homepath;
  if(hist) {
    return end_serial();
  }
  homepath = getenv("HOME");
  if (homepath == ((void *)0)) {
    int namesize = strlen("/etc") + 1 + sizeof("gtd.hist");
    filename = malloc(namesize);
    snprintf(filename, namesize, "%s/%s", "/etc", "gtd.hist");
  } else {
    int namesize = strlen(homepath) + 2 + sizeof("gtd.hist");
    filename = malloc(namesize);
    snprintf(filename, namesize, "%s/.%s", homepath, "gtd.hist");
  }
  fprintf(stderr,"using histfile : %s\n", filename);
  hist = fopen(filename, mode);
  free(filename);
  if (!hist) {
    perror("init_serial: fopen");
    return -1;
  }
  return 0;
}

canonical_original_code:
int init_serial(char *mode) {
  char *filename;
  char *homepath;
  if(hist) {
    return end_serial();
  }
  homepath = getenv("HOME");
  if (homepath == ((void *)0)) {
    int namesize = strlen("/etc") + 1 + sizeof("gtd.hist");
    filename = malloc(namesize);
    snprintf(filename, namesize, "%s/%s", "/etc", "gtd.hist");
  } else {
    int namesize = strlen(homepath) + 2 + sizeof("gtd.hist");
    filename = malloc(namesize);
    snprintf(filename, namesize, "%s/.%s", homepath, "gtd.hist");
  }
  fprintf(stderr,"using histfile : %s\n", filename);
  hist = fopen(filename, mode);
  free(filename);
  if (!hist) {
    perror("init_serial: fopen");
    return -1;
  }
  return 0;
}

variable_types:
mode
char *

variable_types:
filename
char *

variable_types:
homepath
char *

variable_types: namesize int


decompiled_code:
char *__fastcall strtok_re(char *s, char *accept, char **a3)
{
char *v3; // rbx
const char *i; // rdi
char *v6; // rax

if ( !accept )
_assert_fail("delim", "serial.c", 0x40u, "strtok_re");
v3 = s;
if ( !s )
{
v3 = *a3;
if ( !*a3 )
return 0LL;
}
for ( i = v3; ; i = v6 + 2 )
{
v6 = strpbrk(i, accept);
if ( !v6 )
{
*a3 = 0LL;
return v3;
}
if ( *v6 != *accept )
break;
if ( !*accept )
{
*a3 = v6;
return v3;
}
}
*v6 = 0;
*a3 = v6 + 1;
return v3;
}

canonical_decompiled_code:
char *__fastcall func2(char *s, char *accept, char **a3)
{
char *v3; // rbx
const char *i; // rdi
char *v6; // rax

if ( !accept )
_assert_fail("delim", "serial.c", 0x40u, "strtok_re");
v3 = s;
if ( !s )
{
v3 = *a3;
if ( !*a3 )
return 0LL;
}
for ( i = v3; ; i = v6 + 2 )
{
v6 = strpbrk(i, accept);
if ( !v6 )
{
*a3 = 0LL;
return v3;
}
if ( *v6 != *accept )
break;
if ( !*accept )
{
*a3 = v6;
return v3;
}
}
*v6 = 0;
*a3 = v6 + 1;
return v3;
}

original_code:
char *strtok_re(char *str, const char *delim, char **saveptr) {
  char *token = str;
  char *ptr = str;
  ((void) sizeof ((delim) ? 1 : 0), __extension__ ({ if (delim) ; else __assert_fail ("delim", "serial.c", 64, __extension__ __PRETTY_FUNCTION__); }));
  if (!str) {
    if (!*saveptr) {
      return ((void *)0);
    }
    token = *saveptr;
    ptr = *saveptr;
  }
  while ((ptr = strpbrk(ptr, delim)) && ptr[0] == delim[0]) {
    if (delim[0] == '\0') {
      *saveptr = ptr;
      return token;
    }
    ptr += 2;
  }
  if (!ptr) {
    *saveptr = ((void *)0);
  } else {
    ptr[0] = '\0';
    *saveptr = ptr+1;
  }
  return token;
}

canonical_original_code:
char *strtok_re(char *str, char *delim, char **saveptr) {
  char *token = str;
  char *ptr = str;
  ((void) sizeof ((delim) ? 1 : 0), __extension__ ({ if (delim) ; else __assert_fail ("delim", "serial.c", 64, __extension__ __PRETTY_FUNCTION__); }));
  if (!str) {
    if (!*saveptr) {
      return ((void *)0);
    }
    token = *saveptr;
    ptr = *saveptr;
  }
  while ((ptr = strpbrk(ptr, delim)) && ptr[0] == delim[0]) {
    if (delim[0] == '\0') {
      *saveptr = ptr;
      return token;
    }
    ptr += 2;
  }
  if (!ptr) {
    *saveptr = ((void *)0);
  } else {
    ptr[0] = '\0';
    *saveptr = ptr+1;
  }
  return token;
}

variable_types:
str
char *

variable_types:
delim
char *

variable_types:
saveptr
char **

variable_types:
token
char *

variable_types:
ptr
char *

return_type: char *


decompiled_code:
__int64 read_next_entry()
{
unsigned int v0; // er14
__int64 result; // rax
char *v2; // rax
const char *v3; // rbx
char *v4; // rbp
int *v5; // r12
unsigned __int64 v6; // rbp
char *v7; // rax
__ssize_t v8; // rdx
char *v9; // r13
char *v10; // [rsp+0h] [rbp-48h] BYREF
size_t n; // [rsp+8h] [rbp-40h] BYREF
char *v12; // [rsp+10h] [rbp-38h] BYREF
char *lineptr; // [rsp+18h] [rbp-30h] BYREF

lineptr = 0LL;
n = 0LL;
if ( hist )
{
v0 = 0;
LABEL_14:
while ( 1 )
{
v8 = getline(&lineptr, &n, hist);
if ( v8 == -1 )
break;
++v0;
fprintf(stderr, "length : %lu, value : %s", v8, lineptr);
v9 = strtok_re(lineptr, "\\ ", &v12);
if ( v9 )
{
while ( 1 )
{
v2 = strtok_re(0LL, "\\ \n", &v12);
v3 = v2;
if ( !v2 || !*v2 )
break;
v4 = strtok_re(0LL, "\\ \n", &v12);
if ( !v4 )
{
fprintf(stderr, "Error line %d, no score for path '%s'\n", v0, v3);
goto LABEL_14;
}
v5 = _errno_location();
*v5 = 0;
v6 = strtoul(v4, &v10, 10);
if ( v10 && *v10 != 10 && *v10 || *v5 )
{
v7 = strerror(*v5);
fprintf(stderr, "Error line %d, invalid score for path '%s' : %s, %s\n", v0, v3, v10, v7);
goto LABEL_14;
}
fprintf(stderr, "path : %s, dirname : %s, score : %lu\n", v3, v9, v6);
update_entry_score(v3, v9, v6);
}
}
else
{
fprintf(stderr, "Invalid line %d, skipping\n", v0);
}
}
free(lineptr);
result = 0LL;
}
else
{
fwrite("read_next_entry: serial has not been initialized!\n", 1uLL, 0x32uLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 func3()
{
unsigned int v0; // er14
__int64 result; // rax
char *v2; // rax
const char *v3; // rbx
char *v4; // rbp
int *v5; // r12
unsigned __int64 v6; // rbp
char *v7; // rax
__ssize_t v8; // rdx
char *v9; // r13
char *v10; // [rsp+0h] [rbp-48h] BYREF
size_t n; // [rsp+8h] [rbp-40h] BYREF
char *v12; // [rsp+10h] [rbp-38h] BYREF
char *lineptr; // [rsp+18h] [rbp-30h] BYREF

lineptr = 0LL;
n = 0LL;
if ( hist )
{
v0 = 0;
LABEL_14:
while ( 1 )
{
v8 = getline(&lineptr, &n, hist);
if ( v8 == -1 )
break;
++v0;
fprintf(stderr, "length : %lu, value : %s", v8, lineptr);
v9 = func2(lineptr, "\\ ", &v12);
if ( v9 )
{
while ( 1 )
{
v2 = func2(0LL, "\\ \n", &v12);
v3 = v2;
if ( !v2 || !*v2 )
break;
v4 = func2(0LL, "\\ \n", &v12);
if ( !v4 )
{
fprintf(stderr, "Error line %d, no score for path '%s'\n", v0, v3);
goto LABEL_14;
}
v5 = _errno_location();
*v5 = 0;
v6 = strtoul(v4, &v10, 10);
if ( v10 && *v10 != 10 && *v10 || *v5 )
{
v7 = strerror(*v5);
fprintf(stderr, "Error line %d, invalid score for path '%s' : %s, %s\n", v0, v3, v10, v7);
goto LABEL_14;
}
fprintf(stderr, "path : %s, dirname : %s, score : %lu\n", v3, v9, v6);
update_entry_score(v3, v9, v6);
}
}
else
{
fprintf(stderr, "Invalid line %d, skipping\n", v0);
}
}
free(lineptr);
result = 0LL;
}
else
{
fwrite("read_next_entry: serial has not been initialized!\n", 1uLL, 0x32uLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int read_next_entry() {
  char *buff = ((void *)0);
  char *path;
  char *dirname;
  char *saveptr;
  ssize_t length;
  size_t len = 0;
  int linenb = 0;
  if (!hist){
    fprintf(stderr,"read_next_entry: serial has not been initialized!\n");
    return -1;
  }
  while (1) {
    length = getline(&buff, &len, hist);
    if (length == -1) {
      break;
    }
    linenb++;
    fprintf(stderr,"length : %lu, value : %s", length, buff);
    dirname = strtok_re(buff, "\\ ", &saveptr);
    if (!dirname) {
      fprintf(stderr,"Invalid line %d, skipping\n", linenb);
      continue;
    }
    while ((path = strtok_re(((void *)0), "\\ \n", &saveptr)) != ((void *)0) && path[0] != '\0') {
      unsigned long scorel = 0;
      char *score = strtok_re(((void *)0), "\\ \n", &saveptr);
      char *remains;
      if (!score) {
        fprintf(stderr,"Error line %d, no score for path '%s'\n", linenb, path);
        break;
      }
      (*__errno_location ()) = 0;
      scorel = strtoul(score, &remains, 10);
      if ((remains && remains[0] != '\0' && remains[0] != '\n') || (*__errno_location ()) != 0) {
        fprintf(stderr,"Error line %d, invalid score for path '%s' : %s, %s\n", linenb, path, remains, strerror((*__errno_location ())));
        break;
      }
      fprintf(stderr,"path : %s, dirname : %s, score : %lu\n", path, dirname, scorel);
      update_entry_score(path, dirname, scorel);
    }
  }
  free(buff);
  return 0;
}

canonical_original_code:
int read_next_entry() {
  char *buff = ((void *)0);
  char *path;
  char *dirname;
  char *saveptr;
  long length;
  unsigned long len = 0;
  int linenb = 0;
  if (!hist){
    fprintf(stderr,"read_next_entry: serial has not been initialized!\n");
    return -1;
  }
  while (1) {
    length = getline(&buff, &len, hist);
    if (length == -1) {
      break;
    }
    linenb++;
    fprintf(stderr,"length : %lu, value : %s", length, buff);
    dirname = strtok_re(buff, "\\ ", &saveptr);
    if (!dirname) {
      fprintf(stderr,"Invalid line %d, skipping\n", linenb);
      continue;
    }
    while ((path = strtok_re(((void *)0), "\\ \n", &saveptr)) != ((void *)0) && path[0] != '\0') {
      unsigned long scorel = 0;
      char *score = strtok_re(((void *)0), "\\ \n", &saveptr);
      char *remains;
      if (!score) {
        fprintf(stderr,"Error line %d, no score for path '%s'\n", linenb, path);
        break;
      }
      (*__errno_location ()) = 0;
      scorel = strtoul(score, &remains, 10);
      if ((remains && remains[0] != '\0' && remains[0] != '\n') || (*__errno_location ()) != 0) {
        fprintf(stderr,"Error line %d, invalid score for path '%s' : %s, %s\n", linenb, path, remains, strerror((*__errno_location ())));
        break;
      }
      fprintf(stderr,"path : %s, dirname : %s, score : %lu\n", path, dirname, scorel);
      update_entry_score(path, dirname, scorel);
    }
  }
  free(buff);
  return 0;
}

variable_types:
buff
char *

variable_types:
dirname
char *

variable_types:
saveptr
char *

variable_types: length long

variable_types:
len
unsigned long

variable_types: linenb int

variable_types:
scorel
unsigned long

variable_types:
score
char *

variable_types:
remains
char *


decompiled_code:
int __fastcall dump_entry(__int64 a1)
{
__int64 v1; // rbx
__int64 i; // rbx

if ( !hist )
return fwrite("dump_entry: serial has not been initialized!\n", 1uLL, 0x2DuLL, stderr);
fwrite("dumping entry\n", 1uLL, 0xEuLL, stderr);
v1 = *(_QWORD *)(a1 + 272);
if ( fprintf(hist, "%s %s %d ", (const char *)(a1 + 16), (const char *)(v1 + 16), *(unsigned int *)(v1 + 272)) <= 0 )
perror("fprintf:");
for ( i = *(_QWORD *)(v1 + 8); *(_QWORD *)(a1 + 272) != i; i = *(_QWORD *)(i + 8) )
fprintf(hist, "%s %d ", (const char *)(i + 16), *(unsigned int *)(i + 272));
return fputc(10, hist);
}

canonical_decompiled_code:
int __fastcall func4(__int64 a1)
{
__int64 v1; // rbx
__int64 i; // rbx

if ( !hist )
return fwrite("dump_entry: serial has not been initialized!\n", 1uLL, 0x2DuLL, stderr);
fwrite("dumping entry\n", 1uLL, 0xEuLL, stderr);
v1 = *(_QWORD *)(a1 + 272);
if ( fprintf(hist, "%s %s %d ", (const char *)(a1 + 16), (const char *)(v1 + 16), *(unsigned int *)(v1 + 272)) <= 0 )
perror("fprintf:");
for ( i = *(_QWORD *)(v1 + 8); *(_QWORD *)(a1 + 272) != i; i = *(_QWORD *)(i + 8) )
fprintf(hist, "%s %d ", (const char *)(i + 16), *(unsigned int *)(i + 272));
return fputc(10, hist);
}

original_code:
void dump_entry(const struct entry * to_dump) {
  struct candidate * c_dump;
  int ret = 0;
  if (!hist){
    fprintf(stderr,"dump_entry: serial has not been initialized!\n");
    return;
  }
  fprintf(stderr,"dumping entry\n");
  c_dump = to_dump->first_candidate;
  ret = fprintf(hist, "%s %s %d ", to_dump->dirname, c_dump->path, c_dump->score);
  if (ret <= 0){
    perror("fprintf:");
  }
  for (c_dump = c_dump->next; c_dump != to_dump->first_candidate; c_dump = c_dump->next) {
    fprintf(hist, "%s %d ", c_dump->path, c_dump->score);
  }
  fputc('\n', hist);
}

canonical_original_code:
void dump_entry(struct entry *to_dump) {
  struct candidate *c_dump;
  int ret = 0;
  if (!hist){
    fprintf(stderr,"dump_entry: serial has not been initialized!\n");
    return;
  }
  fprintf(stderr,"dumping entry\n");
  c_dump = to_dump->first_candidate;
  ret = fprintf(hist, "%s %s %d ", to_dump->dirname, c_dump->path, c_dump->score);
  if (ret <= 0){
    perror("fprintf:");
  }
  for (c_dump = c_dump->next; c_dump != to_dump->first_candidate; c_dump = c_dump->next) {
    fprintf(hist, "%s %d ", c_dump->path, c_dump->score);
  }
  fputc('\n', hist);
}

variable_types: struct entry { struct entry *prev; struct entry *next; char dirname[254]; struct candidate { struct candidate *prev; struct candidate *next; char path[254]; int score; } *first_candidate; int state; } *

variable_types: ret int


repo: rcatolino/tftfs

decompiled_code:
int print_help_head()
{
return puts(
"Usage : tftfs [CURL OPTIONS] url mountpoint [FUSE_OPTIONS]\n"
"Example: to mount http://thefiletree.com/example/ at ./tft use :\n"
"\n"
"tftfs http://thefiletree.com/example/ tft");
}

canonical_decompiled_code:
int func0()
{
return puts(
"Usage : tftfs [CURL OPTIONS] url mountpoint [FUSE_OPTIONS]\n"
"Example: to mount http://thefiletree.com/example/ at ./tft use :\n"
"\n"
"tftfs http://thefiletree.com/example/ tft");
}

original_code:
void print_help_head() {
  printf("Usage : tftfs [CURL OPTIONS] url mountpoint [FUSE_OPTIONS]\n"
         "Example: to mount http://thefiletree.com/example/ at ./tft use :\n"
         "\n"
         "tftfs http://thefiletree.com/example/ tft\n");
}


isdir

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

if ( stat64(a1, &v2) != -1 )
return (v2.st_mode & 0xF000) == 0x4000;
perror("stat ");
return 0LL;
}

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

if ( stat64(a1, &v2) != -1 )
return (v2.st_mode & 0xF000) == 0x4000;
perror("stat ");
return 0LL;
}

original_code:
int isdir(const char * path) {
  struct stat stats;
  if (stat(path, &stats) == -1) {
    perror("stat ");
    return 0;
  }
  if (((((stats.st_mode)) & 0170000) == (0040000))) {
    return 1;
  } else {
    return 0;
  }
}

canonical_original_code:
int isdir(char *path) {
  struct stat stats;
  if (stat(path, &stats) == -1) {
    perror("stat ");
    return 0;
  }
  if (((((stats.st_mode)) & 0170000) == (0040000))) {
    return 1;
  } else {
    return 0;
  }
}

variable_types:
path
char *

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

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


decompiled_code:
_BOOL8 __fastcall end_option(const char *a1)
{
return strcmp(a1, "--") == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(const char *a1)
{
return strcmp(a1, "--") == 0;
}

original_code:
int end_option(const char * opt) {
  if (strcmp(opt, "--") == 0) {
    return 1;
  }
  return 0;
}

canonical_original_code:
int end_option(char *opt) {
  if (strcmp(opt, "--") == 0) {
    return 1;
  }
  return 0;
}

variable_types:
opt
char *


option

decompiled_code:
_BOOL8 __fastcall option(const void *a1)
{
return memcmp(a1, "--", 2uLL) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func6(const void *a1)
{
return memcmp(a1, "--", 2uLL) == 0;
}

original_code:
int option(const char * opt) {
  if (strncmp(opt, "--", 2) == 0) {
    return 1;
  }
  return 0;
}

canonical_original_code:
int option(char *opt) {
  if (strncmp(opt, "--", 2) == 0) {
    return 1;
  }
  return 0;
}


decompiled_code:
void **__fastcall create_pool(const __m128i *a1)
{
void **v1; // rbx
void *v2; // rax
__int64 i; // rbp
void **v4; // r13
__m128i *v5; // rax
_QWORD *v6; // r13

v1 = (void **)malloc(0x70uLL);
if ( !v1 )
{
perror("Error initializing connection pool ");
return v1;
}
v2 = malloc(0x28uLL);
*v1 = v2;
if ( !v2 )
{
perror("Error initializing connection pool buffers");
LABEL_12:
free(v1);
return 0LL;
}
*((_DWORD *)v1 + 3) = 5;
*((_DWORD *)v1 + 2) = 5;
if ( pthread_cond_init((pthread_cond_t *)(v1 + 2), 0LL) )
{
perror("Error initializing connection pool condition ");
LABEL_11:
free(*v1);
goto LABEL_12;
}
if ( pthread_mutex_init((pthread_mutex_t *)(v1 + 8), 0LL) )
{
perror("Error initializing connection pool synchronization ");
pthread_cond_destroy((pthread_cond_t *)(v1 + 2));
goto LABEL_11;
}
*(_QWORD *)*v1 = a1;
for ( i = 8LL; i != 40; i += 8LL )
{
v4 = (void **)((char *)*v1 + i);
*v4 = malloc(0x48uLL);
v5 = *(__m128i **)((char *)*v1 + i);
*v5 = _mm_loadu_si128(a1);
v5[1] = _mm_loadu_si128(a1 + 1);
v5[2] = _mm_loadu_si128(a1 + 2);
v5[3] = _mm_loadu_si128(a1 + 3);
v5[4].m128i_i64[0] = a1[4].m128i_i64[0];
v6 = *(_QWORD **)((char *)*v1 + i);
*v6 = curl_easy_duphandle(a1->m128i_i64[0]);
*(_QWORD *)(*(_QWORD *)((char *)*v1 + i) + 40LL) = 0LL;
*(_QWORD *)(*(_QWORD *)((char *)*v1 + i) + 32LL) = 0LL;
}
return v1;
}

canonical_decompiled_code:
void **__fastcall func0(const __m128i *a1)
{
void **v1; // rbx
void *v2; // rax
__int64 i; // rbp
void **v4; // r13
__m128i *v5; // rax
_QWORD *v6; // r13

v1 = (void **)malloc(0x70uLL);
if ( !v1 )
{
perror("Error initializing connection pool ");
return v1;
}
v2 = malloc(0x28uLL);
*v1 = v2;
if ( !v2 )
{
perror("Error initializing connection pool buffers");
LABEL_12:
free(v1);
return 0LL;
}
*((_DWORD *)v1 + 3) = 5;
*((_DWORD *)v1 + 2) = 5;
if ( pthread_cond_init((pthread_cond_t *)(v1 + 2), 0LL) )
{
perror("Error initializing connection pool condition ");
LABEL_11:
free(*v1);
goto LABEL_12;
}
if ( pthread_mutex_init((pthread_mutex_t *)(v1 + 8), 0LL) )
{
perror("Error initializing connection pool synchronization ");
pthread_cond_destroy((pthread_cond_t *)(v1 + 2));
goto LABEL_11;
}
*(_QWORD *)*v1 = a1;
for ( i = 8LL; i != 40; i += 8LL )
{
v4 = (void **)((char *)*v1 + i);
*v4 = malloc(0x48uLL);
v5 = *(__m128i **)((char *)*v1 + i);
*v5 = _mm_loadu_si128(a1);
v5[1] = _mm_loadu_si128(a1 + 1);
v5[2] = _mm_loadu_si128(a1 + 2);
v5[3] = _mm_loadu_si128(a1 + 3);
v5[4].m128i_i64[0] = a1[4].m128i_i64[0];
v6 = *(_QWORD **)((char *)*v1 + i);
*v6 = curl_easy_duphandle(a1->m128i_i64[0]);
*(_QWORD *)(*(_QWORD *)((char *)*v1 + i) + 40LL) = 0LL;
*(_QWORD *)(*(_QWORD *)((char *)*v1 + i) + 32LL) = 0LL;
}
return v1;
}

original_code:
struct connection_pool *create_pool(struct http_connection *initial) {
  struct connection_pool *pool = malloc(sizeof(struct connection_pool));
  if (!pool) {
    perror("Error initializing connection pool ");
    goto error_malloc_pool;
  }
  pool->queue = malloc(5 * sizeof(struct http_connection*));
  if (!pool->queue) {
    perror("Error initializing connection pool buffers");
    goto error_malloc_queue;
  }
  pool->available_connections = 5;
  pool->pool_size = 5;
  if (pthread_cond_init(&pool->connection_available, ((void *)0)) != 0) {
    perror("Error initializing connection pool condition ");
    goto error_cond;
  }
  if (pthread_mutex_init(&pool->cond_mutex, ((void *)0)) != 0) {
    perror("Error initializing connection pool synchronization ");
    goto error_mutex;
  }
  pool->queue[0] = initial;
  for (int i = 1; i<5; i++) {
    pool->queue[i] = malloc(sizeof(struct http_connection));
    memcpy(pool->queue[i], initial, sizeof(struct http_connection));
    pool->queue[i]->curl = curl_easy_duphandle(initial->curl);
    pool->queue[i]->last_result.callback_userdata = ((void *)0);
    pool->queue[i]->last_result.effective_url = ((void *)0);
  }
  return pool;
error_mutex:
  pthread_cond_destroy(&pool->connection_available);
error_cond:
  free(pool->queue);
error_malloc_queue:
  free(pool);
error_malloc_pool:
  return ((void *)0);
}

canonical_original_code:
struct connection_pool *create_pool(struct http_connection *initial) {
  struct connection_pool *pool = malloc(sizeof(struct connection_pool));
  if (!pool) {
    perror("Error initializing connection pool ");
    goto error_malloc_pool;
  }
  pool->queue = malloc(5 * sizeof(struct http_connection *));
  if (!pool->queue) {
    perror("Error initializing connection pool buffers");
    goto error_malloc_queue;
  }
  pool->available_connections = 5;
  pool->pool_size = 5;
  if (pthread_cond_init(&pool->connection_available, ((void *)0)) != 0) {
    perror("Error initializing connection pool condition ");
    goto error_cond;
  }
  if (pthread_mutex_init(&pool->cond_mutex, ((void *)0)) != 0) {
    perror("Error initializing connection pool synchronization ");
    goto error_mutex;
  }
  pool->queue[0] = initial;
  for (int i = 1; i<5; i++) {
    pool->queue[i] = malloc(sizeof(struct http_connection));
    memcpy(pool->queue[i], initial, sizeof(struct http_connection));
    pool->queue[i]->curl = curl_easy_duphandle(initial->curl);
    pool->queue[i]->last_result.callback_userdata = ((void *)0);
    pool->queue[i]->last_result.effective_url = ((void *)0);
  }
  return pool;
error_mutex:
  pthread_cond_destroy(&pool->connection_available);
error_cond:
  free(pool->queue);
error_malloc_queue:
  free(pool);
error_malloc_pool:
  return ((void *)0);
}

variable_types:
initial
struct http_connection *

variable_types: struct connection_pool { struct http_connection { void *curl; char *root_url; char *host; char *root_path; struct http_result { char *effective_url; char *callback_userdata; enum CURLcode { CURLE_OK=0, CURLE_UNSUPPORTED_PROTOCOL=1, CURLE_FAILED_INIT=2, CURLE_URL_MALFORMAT=3, CURLE_NOT_BUILT_IN=4, CURLE_COULDNT_RESOLVE_PROXY=5, CURLE_COULDNT_RESOLVE_HOST=6, CURLE_COULDNT_CONNECT=7, CURLE_WEIRD_SERVER_REPLY=8, CURLE_REMOTE_ACCESS_DENIED=9, CURLE_FTP_ACCEPT_FAILED=10, CURLE_FTP_WEIRD_PASS_REPLY=11, CURLE_FTP_ACCEPT_TIMEOUT=12, CURLE_FTP_WEIRD_PASV_REPLY=13, CURLE_FTP_WEIRD_227_FORMAT=14, CURLE_FTP_CANT_GET_HOST=15, CURLE_HTTP2=16, CURLE_FTP_COULDNT_SET_TYPE=17, CURLE_PARTIAL_FILE=18, CURLE_FTP_COULDNT_RETR_FILE=19, CURLE_OBSOLETE20=20, CURLE_QUOTE_ERROR=21, CURLE_HTTP_RETURNED_ERROR=22, CURLE_WRITE_ERROR=23, CURLE_OBSOLETE24=24, CURLE_UPLOAD_FAILED=25, CURLE_READ_ERROR=26, CURLE_OUT_OF_MEMORY=27, CURLE_OPERATION_TIMEDOUT=28, CURLE_OBSOLETE29=29, CURLE_FTP_PORT_FAILED=30, CURLE_FTP_COULDNT_USE_REST=31, CURLE_OBSOLETE32=32, CURLE_RANGE_ERROR=33, CURLE_HTTP_POST_ERROR=34, CURLE_SSL_CONNECT_ERROR=35, CURLE_BAD_DOWNLOAD_RESUME=36, CURLE_FILE_COULDNT_READ_FILE=37, CURLE_LDAP_CANNOT_BIND=38, CURLE_LDAP_SEARCH_FAILED=39, CURLE_OBSOLETE40=40, CURLE_FUNCTION_NOT_FOUND=41, CURLE_ABORTED_BY_CALLBACK=42, CURLE_BAD_FUNCTION_ARGUMENT=43, CURLE_OBSOLETE44=44, CURLE_INTERFACE_FAILED=45, CURLE_OBSOLETE46=46, CURLE_TOO_MANY_REDIRECTS=47, CURLE_UNKNOWN_OPTION=48, CURLE_TELNET_OPTION_SYNTAX=49, CURLE_OBSOLETE50=50, CURLE_OBSOLETE51=51, CURLE_GOT_NOTHING=52, CURLE_SSL_ENGINE_NOTFOUND=53, CURLE_SSL_ENGINE_SETFAILED=54, CURLE_SEND_ERROR=55, CURLE_RECV_ERROR=56, CURLE_OBSOLETE57=57, CURLE_SSL_CERTPROBLEM=58, CURLE_SSL_CIPHER=59, CURLE_PEER_FAILED_VERIFICATION=60, CURLE_BAD_CONTENT_ENCODING=61, CURLE_LDAP_INVALID_URL=62, CURLE_FILESIZE_EXCEEDED=63, CURLE_USE_SSL_FAILED=64, CURLE_SEND_FAIL_REWIND=65, CURLE_SSL_ENGINE_INITFAILED=66, CURLE_LOGIN_DENIED=67, CURLE_TFTP_NOTFOUND=68, CURLE_TFTP_PERM=69, CURLE_REMOTE_DISK_FULL=70, CURLE_TFTP_ILLEGAL=71, CURLE_TFTP_UNKNOWNID=72, CURLE_REMOTE_FILE_EXISTS=73, CURLE_TFTP_NOSUCHUSER=74, CURLE_CONV_FAILED=75, CURLE_CONV_REQD=76, CURLE_SSL_CACERT_BADFILE=77, CURLE_REMOTE_FILE_NOT_FOUND=78, CURLE_SSH=79, CURLE_SSL_SHUTDOWN_FAILED=80, CURLE_AGAIN=81, CURLE_SSL_CRL_BADFILE=82, CURLE_SSL_ISSUER_ERROR=83, CURLE_FTP_PRET_FAILED=84, CURLE_RTSP_CSEQ_ERROR=85, CURLE_RTSP_SESSION_ERROR=86, CURLE_FTP_BAD_FILE_LIST=87, CURLE_CHUNK_FAILED=88, CURLE_NO_CONNECTION_AVAILABLE=89, CURLE_SSL_PINNEDPUBKEYNOTMATCH=90, CURLE_SSL_INVALIDCERTSTATUS=91, CURLE_HTTP2_STREAM=92, CURLE_RECURSIVE_API_CALL=93, CURL_LAST=94 } result; } last_result; enum con_mode { REGULAR=0, WS=1 } mode; char *ws_key; } **queue; int pool_size; int available_connections; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } connection_available; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } cond_mutex; void *shared_connection; } *

variable_types: i int

user_defined_types: struct http_connection { void *curl; char *root_url; char *host; char *root_path; struct http_result last_result; enum con_mode mode; char *ws_key; }

user_defined_types: struct connection_pool { struct http_connection **queue; int pool_size; int available_connections; union pthread_cond_t connection_available; union pthread_mutex_t cond_mutex; void *shared_connection; }

user_defined_types: struct http_result { char *effective_url; char *callback_userdata; enum CURLcode result; }

user_defined_types: union pthread_cond_t { struct __pthread_cond_s __data; char __size[48]; long long __align; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


decompiled_code:
void __fastcall free_pool(void *ptr)
{
__int64 v1; // rbx

if ( *((_DWORD *)ptr + 3) != *((_DWORD *)ptr + 2) )
_assert_fail("pool->available_connections == pool->pool_size", "src/connections.c", 0x3Eu, "free_pool");
free(*(void **)(**(_QWORD **)ptr + 16LL));
free(*(void **)(**(_QWORD **)ptr + 8LL));
if ( *((int *)ptr + 2) > 0 )
{
v1 = 0LL;
do
http_cleanup(*(_QWORD *)(*(_QWORD *)ptr + 8 * v1++));
while ( *((_DWORD *)ptr + 2) > (int)v1 );
}
curl_global_cleanup();
pthread_cond_destroy((pthread_cond_t *)((char *)ptr + 16));
pthread_mutex_destroy((pthread_mutex_t *)((char *)ptr + 64));
free(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
__int64 v1; // rbx

if ( *((_DWORD *)ptr + 3) != *((_DWORD *)ptr + 2) )
_assert_fail("pool->available_connections == pool->pool_size", "src/connections.c", 0x3Eu, "free_pool");
free(*(void **)(**(_QWORD **)ptr + 16LL));
free(*(void **)(**(_QWORD **)ptr + 8LL));
if ( *((int *)ptr + 2) > 0 )
{
v1 = 0LL;
do
http_cleanup(*(_QWORD *)(*(_QWORD *)ptr + 8 * v1++));
while ( *((_DWORD *)ptr + 2) > (int)v1 );
}
curl_global_cleanup();
pthread_cond_destroy((pthread_cond_t *)((char *)ptr + 16));
pthread_mutex_destroy((pthread_mutex_t *)((char *)ptr + 64));
free(*(void **)ptr);
free(ptr);
}

original_code:
void free_pool(struct connection_pool *pool) {
  ((void) sizeof ((pool->available_connections == pool->pool_size) ? 1 : 0), __extension__ ({ if (pool->available_connections == pool->pool_size) ; else __assert_fail ("pool->available_connections == pool->pool_size", "src/connections.c", 62, __extension__ __PRETTY_FUNCTION__); }));
  free(pool->queue[0]->host);
  free(pool->queue[0]->root_url);
  for (int i = 0; i<pool->pool_size; i++) {
    http_cleanup(pool->queue[i]);
  }
  curl_global_cleanup();
  pthread_cond_destroy(&pool->connection_available);
  pthread_mutex_destroy(&pool->cond_mutex);
  free(pool->queue);
  free(pool);
}

user_defined_types: struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; }

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

user_defined_types: enum CURLcode { CURLE_OK=0, CURLE_UNSUPPORTED_PROTOCOL=1, CURLE_FAILED_INIT=2, CURLE_URL_MALFORMAT=3, CURLE_NOT_BUILT_IN=4, CURLE_COULDNT_RESOLVE_PROXY=5, CURLE_COULDNT_RESOLVE_HOST=6, CURLE_COULDNT_CONNECT=7, CURLE_WEIRD_SERVER_REPLY=8, CURLE_REMOTE_ACCESS_DENIED=9, CURLE_FTP_ACCEPT_FAILED=10, CURLE_FTP_WEIRD_PASS_REPLY=11, CURLE_FTP_ACCEPT_TIMEOUT=12, CURLE_FTP_WEIRD_PASV_REPLY=13, CURLE_FTP_WEIRD_227_FORMAT=14, CURLE_FTP_CANT_GET_HOST=15, CURLE_HTTP2=16, CURLE_FTP_COULDNT_SET_TYPE=17, CURLE_PARTIAL_FILE=18, CURLE_FTP_COULDNT_RETR_FILE=19, CURLE_OBSOLETE20=20, CURLE_QUOTE_ERROR=21, CURLE_HTTP_RETURNED_ERROR=22, CURLE_WRITE_ERROR=23, CURLE_OBSOLETE24=24, CURLE_UPLOAD_FAILED=25, CURLE_READ_ERROR=26, CURLE_OUT_OF_MEMORY=27, CURLE_OPERATION_TIMEDOUT=28, CURLE_OBSOLETE29=29, CURLE_FTP_PORT_FAILED=30, CURLE_FTP_COULDNT_USE_REST=31, CURLE_OBSOLETE32=32, CURLE_RANGE_ERROR=33, CURLE_HTTP_POST_ERROR=34, CURLE_SSL_CONNECT_ERROR=35, CURLE_BAD_DOWNLOAD_RESUME=36, CURLE_FILE_COULDNT_READ_FILE=37, CURLE_LDAP_CANNOT_BIND=38, CURLE_LDAP_SEARCH_FAILED=39, CURLE_OBSOLETE40=40, CURLE_FUNCTION_NOT_FOUND=41, CURLE_ABORTED_BY_CALLBACK=42, CURLE_BAD_FUNCTION_ARGUMENT=43, CURLE_OBSOLETE44=44, CURLE_INTERFACE_FAILED=45, CURLE_OBSOLETE46=46, CURLE_TOO_MANY_REDIRECTS=47, CURLE_UNKNOWN_OPTION=48, CURLE_TELNET_OPTION_SYNTAX=49, CURLE_OBSOLETE50=50, CURLE_OBSOLETE51=51, CURLE_GOT_NOTHING=52, CURLE_SSL_ENGINE_NOTFOUND=53, CURLE_SSL_ENGINE_SETFAILED=54, CURLE_SEND_ERROR=55, CURLE_RECV_ERROR=56, CURLE_OBSOLETE57=57, CURLE_SSL_CERTPROBLEM=58, CURLE_SSL_CIPHER=59, CURLE_PEER_FAILED_VERIFICATION=60, CURLE_BAD_CONTENT_ENCODING=61, CURLE_LDAP_INVALID_URL=62, CURLE_FILESIZE_EXCEEDED=63, CURLE_USE_SSL_FAILED=64, CURLE_SEND_FAIL_REWIND=65, CURLE_SSL_ENGINE_INITFAILED=66, CURLE_LOGIN_DENIED=67, CURLE_TFTP_NOTFOUND=68, CURLE_TFTP_PERM=69, CURLE_REMOTE_DISK_FULL=70, CURLE_TFTP_ILLEGAL=71, CURLE_TFTP_UNKNOWNID=72, CURLE_REMOTE_FILE_EXISTS=73, CURLE_TFTP_NOSUCHUSER=74, CURLE_CONV_FAILED=75, CURLE_CONV_REQD=76, CURLE_SSL_CACERT_BADFILE=77, CURLE_REMOTE_FILE_NOT_FOUND=78, CURLE_SSH=79, CURLE_SSL_SHUTDOWN_FAILED=80, CURLE_AGAIN=81, CURLE_SSL_CRL_BADFILE=82, CURLE_SSL_ISSUER_ERROR=83, CURLE_FTP_PRET_FAILED=84, CURLE_RTSP_CSEQ_ERROR=85, CURLE_RTSP_SESSION_ERROR=86, CURLE_FTP_BAD_FILE_LIST=87, CURLE_CHUNK_FAILED=88, CURLE_NO_CONNECTION_AVAILABLE=89, CURLE_SSL_PINNEDPUBKEYNOTMATCH=90, CURLE_SSL_INVALIDCERTSTATUS=91, CURLE_HTTP2_STREAM=92, CURLE_RECURSIVE_API_CALL=93, CURL_LAST=94 }


decompiled_code:
__int64 __fastcall acquire_connection(__int64 a1)
{
pthread_mutex_t *v1; // rbp
int i; // eax
__int64 *v3; // rax
__int64 v4; // r12

v1 = (pthread_mutex_t *)(a1 + 64);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 64));
for ( i = *(_DWORD *)(a1 + 12); !i; i = *(_DWORD *)(a1 + 12) )
pthread_cond_wait((pthread_cond_t *)(a1 + 16), v1);
v3 = (__int64 *)(*(_QWORD *)a1 + 8LL * i - 8);
v4 = *v3;
*v3 = 0LL;
--*(_DWORD *)(a1 + 12);
if ( !v4 )
_assert_fail("con", "src/connections.c", 0x59u, "acquire_connection");
pthread_mutex_unlock(v1);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
pthread_mutex_t *v1; // rbp
int i; // eax
__int64 *v3; // rax
__int64 v4; // r12

v1 = (pthread_mutex_t *)(a1 + 64);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 64));
for ( i = *(_DWORD *)(a1 + 12); !i; i = *(_DWORD *)(a1 + 12) )
pthread_cond_wait((pthread_cond_t *)(a1 + 16), v1);
v3 = (__int64 *)(*(_QWORD *)a1 + 8LL * i - 8);
v4 = *v3;
*v3 = 0LL;
--*(_DWORD *)(a1 + 12);
if ( !v4 )
_assert_fail("con", "src/connections.c", 0x59u, "acquire_connection");
pthread_mutex_unlock(v1);
return v4;
}

original_code:
struct http_connection *acquire_connection(struct connection_pool *pool) {
  struct http_connection *con;
  pthread_mutex_lock(&pool->cond_mutex);
  while(pool->available_connections == 0) {
    pthread_cond_wait(&pool->connection_available, &pool->cond_mutex);
  }
  con = pool->queue[pool->available_connections - 1];
  pool->queue[pool->available_connections - 1] = ((void *)0);
  pool->available_connections -= 1;
  ((void) sizeof ((con) ? 1 : 0), __extension__ ({ if (con) ; else __assert_fail ("con", "src/connections.c", 89, __extension__ __PRETTY_FUNCTION__); }));
  pthread_mutex_unlock(&pool->cond_mutex);
  return con;
}

variable_types: struct connection_pool { struct http_connection **queue; int pool_size; int available_connections; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } connection_available; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } cond_mutex; void *shared_connection; } *

variable_types: struct http_connection { void *curl; char *root_url; char *host; char *root_path; struct http_result { char *effective_url; char *callback_userdata; enum CURLcode { CURLE_OK=0, CURLE_UNSUPPORTED_PROTOCOL=1, CURLE_FAILED_INIT=2, CURLE_URL_MALFORMAT=3, CURLE_NOT_BUILT_IN=4, CURLE_COULDNT_RESOLVE_PROXY=5, CURLE_COULDNT_RESOLVE_HOST=6, CURLE_COULDNT_CONNECT=7, CURLE_WEIRD_SERVER_REPLY=8, CURLE_REMOTE_ACCESS_DENIED=9, CURLE_FTP_ACCEPT_FAILED=10, CURLE_FTP_WEIRD_PASS_REPLY=11, CURLE_FTP_ACCEPT_TIMEOUT=12, CURLE_FTP_WEIRD_PASV_REPLY=13, CURLE_FTP_WEIRD_227_FORMAT=14, CURLE_FTP_CANT_GET_HOST=15, CURLE_HTTP2=16, CURLE_FTP_COULDNT_SET_TYPE=17, CURLE_PARTIAL_FILE=18, CURLE_FTP_COULDNT_RETR_FILE=19, CURLE_OBSOLETE20=20, CURLE_QUOTE_ERROR=21, CURLE_HTTP_RETURNED_ERROR=22, CURLE_WRITE_ERROR=23, CURLE_OBSOLETE24=24, CURLE_UPLOAD_FAILED=25, CURLE_READ_ERROR=26, CURLE_OUT_OF_MEMORY=27, CURLE_OPERATION_TIMEDOUT=28, CURLE_OBSOLETE29=29, CURLE_FTP_PORT_FAILED=30, CURLE_FTP_COULDNT_USE_REST=31, CURLE_OBSOLETE32=32, CURLE_RANGE_ERROR=33, CURLE_HTTP_POST_ERROR=34, CURLE_SSL_CONNECT_ERROR=35, CURLE_BAD_DOWNLOAD_RESUME=36, CURLE_FILE_COULDNT_READ_FILE=37, CURLE_LDAP_CANNOT_BIND=38, CURLE_LDAP_SEARCH_FAILED=39, CURLE_OBSOLETE40=40, CURLE_FUNCTION_NOT_FOUND=41, CURLE_ABORTED_BY_CALLBACK=42, CURLE_BAD_FUNCTION_ARGUMENT=43, CURLE_OBSOLETE44=44, CURLE_INTERFACE_FAILED=45, CURLE_OBSOLETE46=46, CURLE_TOO_MANY_REDIRECTS=47, CURLE_UNKNOWN_OPTION=48, CURLE_TELNET_OPTION_SYNTAX=49, CURLE_OBSOLETE50=50, CURLE_OBSOLETE51=51, CURLE_GOT_NOTHING=52, CURLE_SSL_ENGINE_NOTFOUND=53, CURLE_SSL_ENGINE_SETFAILED=54, CURLE_SEND_ERROR=55, CURLE_RECV_ERROR=56, CURLE_OBSOLETE57=57, CURLE_SSL_CERTPROBLEM=58, CURLE_SSL_CIPHER=59, CURLE_PEER_FAILED_VERIFICATION=60, CURLE_BAD_CONTENT_ENCODING=61, CURLE_LDAP_INVALID_URL=62, CURLE_FILESIZE_EXCEEDED=63, CURLE_USE_SSL_FAILED=64, CURLE_SEND_FAIL_REWIND=65, CURLE_SSL_ENGINE_INITFAILED=66, CURLE_LOGIN_DENIED=67, CURLE_TFTP_NOTFOUND=68, CURLE_TFTP_PERM=69, CURLE_REMOTE_DISK_FULL=70, CURLE_TFTP_ILLEGAL=71, CURLE_TFTP_UNKNOWNID=72, CURLE_REMOTE_FILE_EXISTS=73, CURLE_TFTP_NOSUCHUSER=74, CURLE_CONV_FAILED=75, CURLE_CONV_REQD=76, CURLE_SSL_CACERT_BADFILE=77, CURLE_REMOTE_FILE_NOT_FOUND=78, CURLE_SSH=79, CURLE_SSL_SHUTDOWN_FAILED=80, CURLE_AGAIN=81, CURLE_SSL_CRL_BADFILE=82, CURLE_SSL_ISSUER_ERROR=83, CURLE_FTP_PRET_FAILED=84, CURLE_RTSP_CSEQ_ERROR=85, CURLE_RTSP_SESSION_ERROR=86, CURLE_FTP_BAD_FILE_LIST=87, CURLE_CHUNK_FAILED=88, CURLE_NO_CONNECTION_AVAILABLE=89, CURLE_SSL_PINNEDPUBKEYNOTMATCH=90, CURLE_SSL_INVALIDCERTSTATUS=91, CURLE_HTTP2_STREAM=92, CURLE_RECURSIVE_API_CALL=93, CURL_LAST=94 } result; } last_result; enum con_mode { REGULAR=0, WS=1 } mode; char *ws_key; } *


decompiled_code:
int __fastcall release_connection(__int64 a1, __int64 a2)
{
_QWORD *v2; // rax
int v3; // eax

if ( !a1 )
_assert_fail("con", "src/connections.c", 0x60u, "release_connection");
pthread_mutex_lock((pthread_mutex_t *)(a2 + 64));
v2 = (_QWORD *)(*(_QWORD *)a2 + 8LL * *(int *)(a2 + 12));
if ( *v2 )
_assert_fail("!pool->queue[pool->available_connections]", "src/connections.c", 0x64u, "release_connection");
*v2 = a1;
v3 = *(_DWORD *)(a2 + 12) + 1;
*(_DWORD *)(a2 + 12) = v3;
if ( v3 == 1 )
pthread_cond_signal((pthread_cond_t *)(a2 + 16));
return pthread_mutex_unlock((pthread_mutex_t *)(a2 + 64));
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1, __int64 a2)
{
_QWORD *v2; // rax
int v3; // eax

if ( !a1 )
_assert_fail("con", "src/connections.c", 0x60u, "release_connection");
pthread_mutex_lock((pthread_mutex_t *)(a2 + 64));
v2 = (_QWORD *)(*(_QWORD *)a2 + 8LL * *(int *)(a2 + 12));
if ( *v2 )
_assert_fail("!pool->queue[pool->available_connections]", "src/connections.c", 0x64u, "release_connection");
*v2 = a1;
v3 = *(_DWORD *)(a2 + 12) + 1;
*(_DWORD *)(a2 + 12) = v3;
if ( v3 == 1 )
pthread_cond_signal((pthread_cond_t *)(a2 + 16));
return pthread_mutex_unlock((pthread_mutex_t *)(a2 + 64));
}

original_code:
void release_connection(struct http_connection *con, struct connection_pool *pool) {
  ((void) sizeof ((con) ? 1 : 0), __extension__ ({ if (con) ; else __assert_fail ("con", "src/connections.c", 96, __extension__ __PRETTY_FUNCTION__); }));
  pthread_mutex_lock(&pool->cond_mutex);
  ((void) sizeof ((!pool->queue[pool->available_connections]) ? 1 : 0), __extension__ ({ if (!pool->queue[pool->available_connections]) ; else __assert_fail ("!pool->queue[pool->available_connections]", "src/connections.c", 100, __extension__ __PRETTY_FUNCTION__); }));
  pool->queue[pool->available_connections] = con;
  pool->available_connections += 1;
  if (pool->available_connections == 1) {
    pthread_cond_signal(&pool->connection_available);
  }
  pthread_mutex_unlock(&pool->cond_mutex);
}


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

if ( a1 )
{
result = *(_QWORD *)(a1 + 8);
if ( result != -1 )
{
result = _InterlockedExchangeAdd64((volatile signed __int64 *)(a1 + 8), 0xFFFFFFFFFFFFFFFFLL);
if ( result == 1 )
result = json_delete();
}
}
return result;
}

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

if ( a1 )
{
result = *(_QWORD *)(a1 + 8);
if ( result != -1 )
{
result = _InterlockedExchangeAdd64((volatile signed __int64 *)(a1 + 8), 0xFFFFFFFFFFFFFFFFLL);
if ( result == 1 )
result = json_delete();
}
}
return result;
}

original_code:
static inline
void json_decref(json_t *json)
{
    if(json && json->refcount != (size_t)-1 && __atomic_sub_fetch(&json->refcount, 1, 3) == 0)
        json_delete(json);
}

canonical_original_code:
static inline void json_decref(struct json_t *json)
{
    if(json && json->refcount != (unsigned long)-1 && __atomic_sub_fetch(&json->refcount, 1, 3) == 0)
        json_delete(json);
}

variable_types: struct json_t { enum json_type { JSON_OBJECT=0, JSON_ARRAY=1, JSON_STRING=2, JSON_INTEGER=3, JSON_REAL=4, JSON_TRUE=5, JSON_FALSE=6, JSON_NULL=7 } type; unsigned long refcount; } *

user_defined_types: struct json_t { enum json_type type; unsigned long refcount; }


reverseString

repo: rcharlet/school

decompiled_code:
const char *__fastcall reverseString(const char *a1)
{
int v2; // eax
int v3; // edi
char *v4; // rdx
__int64 v5; // rax
char v6; // cl

v2 = strlen(a1);
v3 = v2 / 2;
if ( v2 > 1 )
{
v4 = (char *)&a1[v2 - 1];
v5 = 0LL;
do
{
v6 = a1[v5];
a1[v5] = *v4;
*v4 = v6;
++v5;
--v4;
}
while ( v3 > (int)v5 );
}
return a1;
}

canonical_decompiled_code:
const char *__fastcall func7(const char *a1)
{
int v2; // eax
int v3; // edi
char *v4; // rdx
__int64 v5; // rax
char v6; // cl

v2 = strlen(a1);
v3 = v2 / 2;
if ( v2 > 1 )
{
v4 = (char *)&a1[v2 - 1];
v5 = 0LL;
do
{
v6 = a1[v5];
a1[v5] = *v4;
*v4 = v6;
++v5;
--v4;
}
while ( v3 > (int)v5 );
}
return a1;
}

original_code:
char * reverseString(char * s) {
 int len = strlen(s);
 int end = len-1;
 int half = len/2;
 char temp;
 int i;
 for (i=0; i<half; i++) {
  temp = s[i];
  s[i] = s[end-i];
  s[end-i] = temp;
 }
 return s;
}

canonical_original_code:
char *reverseString(char *s) {
 int len = strlen(s);
 int end = len-1;
 int half = len/2;
 char temp;
 int i;
 for (i=0; i<half; i++) {
  temp = s[i];
  s[i] = s[end-i];
  s[end-i] = temp;
 }
 return s;
}

variable_types:
s
char *

variable_types: len int

variable_types: end int

variable_types: half int

variable_types: temp char


main

canonical_name: main

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

strcpy(v4, "hi");
reverseString(v4);
puts(v4);
strcpy(v4, "howdy");
reverseString(v4);
puts(v4);
strcpy(v4, "howdy yall");
reverseString(v4);
puts(v4);
strcpy(v4, "supercalifragilistic-expialadocious");
reverseString(v4);
puts(v4);
return 0;
}

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

strcpy(v4, "hi");
func7(v4);
puts(v4);
strcpy(v4, "howdy");
func7(v4);
puts(v4);
strcpy(v4, "howdy yall");
func7(v4);
puts(v4);
strcpy(v4, "supercalifragilistic-expialadocious");
func7(v4);
puts(v4);
return 0;
}

original_code:
int main() {
 char s[500];
 sprintf(s, "%s", "hi");
 reverseString(s);
 printf("%s\n", s);
 sprintf(s, "%s", "howdy");
 reverseString(s);
 printf("%s\n", s);
 sprintf(s, "%s", "howdy yall");
 reverseString(s);
 printf("%s\n", s);
 sprintf(s, "%s", "supercalifragilistic-expialadocious");
 reverseString(s);
 printf("%s\n", s);
 return 0;
}

variable_types:
s
char [500]


reverseList

decompiled_code:
__int64 __fastcall reverseList(__int64 a1)
{
__int64 v1; // rdx
__int64 v2; // rax

if ( !a1 )
return 0xFFFFFFFFLL;
v1 = 0LL;
do
{
v2 = a1;
a1 = *(_QWORD *)(a1 + 8);
*(_QWORD *)(v2 + 8) = v1;
v1 = v2;
}
while ( a1 );
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
__int64 v1; // rdx
__int64 v2; // rax

if ( !a1 )
return 0xFFFFFFFFLL;
v1 = 0LL;
do
{
v2 = a1;
a1 = *(_QWORD *)(a1 + 8);
*(_QWORD *)(v2 + 8) = v1;
v1 = v2;
}
while ( a1 );
return 0LL;
}

original_code:
int reverseList(data *head) {
 data *prev,*node,*next;
 if (!head) return -1;
 prev = ((void *)0);
 node = head;
 while (node) {
  next = node->next;
  node->next = prev;
  prev = node;
  node = next;
 }
 return 0;
}

canonical_original_code:
int reverseList(struct data *head) {
 struct data *prev; struct data *node; struct data *next;
 if (!head) return -1;
 prev = ((void *)0);
 node = head;
 while (node) {
  next = node->next;
  node->next = prev;
  prev = node;
  node = next;
 }
 return 0;
}

variable_types: struct data { int thing; struct data *next; } *


printList

decompiled_code:
__int64 __fastcall printList(unsigned int *a1)
{
unsigned int *i; // rbx

for ( i = a1; i; i = (unsigned int *)*((_QWORD *)i + 1) )
printf("this.thing=%d\n", *i);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned int *a1)
{
unsigned int *i; // rbx

for ( i = a1; i; i = (unsigned int *)*((_QWORD *)i + 1) )
printf("this.thing=%d\n", *i);
return 0LL;
}

original_code:
int printList(data *head) {
 data *this=head;
 while(this) {
  printf("this.thing=%d\n", this->thing);
  this=this->next;
 }
 return 0;
}

canonical_original_code:
int printList(struct data *head) {
 struct data *this = head;
 while(this) {
  printf("this.thing=%d\n", this->thing);
  this=this->next;
 }
 return 0;
}

user_defined_types: struct data { int thing; struct data *next; }


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v4; // [rsp+0h] [rbp-58h] BYREF
int *v5; // [rsp+8h] [rbp-50h]
int v6; // [rsp+10h] [rbp-48h] BYREF
int *v7; // [rsp+18h] [rbp-40h]
int v8; // [rsp+20h] [rbp-38h] BYREF
int *v9; // [rsp+28h] [rbp-30h]
int v10; // [rsp+30h] [rbp-28h] BYREF
unsigned int *v11; // [rsp+38h] [rbp-20h]
unsigned int v12; // [rsp+40h] [rbp-18h] BYREF
__int64 v13; // [rsp+48h] [rbp-10h]

v12 = 5;
v13 = 0LL;
v10 = 4;
v11 = &v12;
v8 = 3;
v9 = &v10;
v6 = 2;
v7 = &v8;
v4 = 1;
v5 = &v6;
printList(&v4);
puts("\n");
reverseList((__int64)&v4);
printList(&v12);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v4; // [rsp+0h] [rbp-58h] BYREF
int *v5; // [rsp+8h] [rbp-50h]
int v6; // [rsp+10h] [rbp-48h] BYREF
int *v7; // [rsp+18h] [rbp-40h]
int v8; // [rsp+20h] [rbp-38h] BYREF
int *v9; // [rsp+28h] [rbp-30h]
int v10; // [rsp+30h] [rbp-28h] BYREF
unsigned int *v11; // [rsp+38h] [rbp-20h]
unsigned int v12; // [rsp+40h] [rbp-18h] BYREF
__int64 v13; // [rsp+48h] [rbp-10h]

v12 = 5;
v13 = 0LL;
v10 = 4;
v11 = &v12;
v8 = 3;
v9 = &v10;
v6 = 2;
v7 = &v8;
v4 = 1;
v5 = &v6;
func8(&v4);
puts("\n");
func7((__int64)&v4);
func8(&v12);
return 0;
}

original_code:
int main() {
 data d5 = {5,((void *)0)};
 data d4 = {4,&d5};
 data d3 = {3,&d4};
 data d2 = {2,&d3};
 data d1 = {1,&d2};
 printList(&d1);
 printf("\n\n");
 reverseList(&d1);
 printList(&d5);
 return 0;
}

canonical_original_code:
int main() {
 struct data d5 = {5,((void *)0)};
 struct data d4 = {4,&d5};
 struct data d3 = {3,&d4};
 struct data d2 = {2,&d3};
 struct data d1 = {1,&d2};
 printList(&d1);
 printf("\n\n");
 reverseList(&d1);
 printList(&d5);
 return 0;
}

variable_types: struct data { int thing; struct data *next; }


repo: rcr/rirc

decompiled_code:
__int64 __fastcall mbedtls_mpi_core_montmul_working_limbs(__int64 a1)
{
return 2 * a1 + 1;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return 2 * a1 + 1;
}

original_code:
static inline size_t mbedtls_mpi_core_montmul_working_limbs(size_t AN_limbs)
{
    return 2 * AN_limbs + 1;
}

canonical_original_code:
static inline unsigned long mbedtls_mpi_core_montmul_working_limbs(unsigned long AN_limbs)
{
    return 2 * AN_limbs + 1;
}

return_type: unsigned long


decompiled_code:
__int64 __fastcall mbedtls_mpi_mod_raw_cond_assign(__int64 a1, __int64 a2, __int64 a3, unsigned __int8 a4)
{
return mbedtls_mpi_core_cond_assign(a1, a2, *(_QWORD *)(a3 + 8), a4);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, __int64 a3, unsigned __int8 a4)
{
return mbedtls_mpi_core_cond_assign(a1, a2, *(_QWORD *)(a3 + 8), a4);
}

original_code:
void mbedtls_mpi_mod_raw_cond_assign(mbedtls_mpi_uint *X,
                                     const mbedtls_mpi_uint *A,
                                     const mbedtls_mpi_mod_modulus *N,
                                     unsigned char assign)
{
    mbedtls_mpi_core_cond_assign(X, A, N->limbs, assign);
}

canonical_original_code:
void mbedtls_mpi_mod_raw_cond_assign(unsigned long *X, unsigned long *A, struct mbedtls_mpi_mod_modulus *N, unsigned char assign)
{
    mbedtls_mpi_core_cond_assign(X, A, N->limbs, assign);
}

variable_types:
X
unsigned long *

variable_types:
A
unsigned long *

variable_types: struct mbedtls_mpi_mod_modulus { unsigned long *p; unsigned long limbs; unsigned long bits; enum mbedtls_mpi_mod_rep_selector { MBEDTLS_MPI_MOD_REP_INVALID=0, MBEDTLS_MPI_MOD_REP_MONTGOMERY=2, MBEDTLS_MPI_MOD_REP_OPT_RED=3 } int_rep; union rep { struct mbedtls_mpi_mont_struct { unsigned long *rr; unsigned long mm; } mont; void *ored; } rep; } *

variable_types:
assign
unsigned char

user_defined_types: struct mbedtls_mpi_mod_modulus { unsigned long *p; unsigned long limbs; unsigned long bits; enum mbedtls_mpi_mod_rep_selector int_rep; union rep rep; }

user_defined_types: union rep { struct mbedtls_mpi_mont_struct mont; void *ored; }


decompiled_code:
__int64 __fastcall mbedtls_mpi_mod_raw_cond_swap(__int64 a1, __int64 a2, __int64 a3, unsigned __int8 a4)
{
return mbedtls_mpi_core_cond_swap(a1, a2, *(_QWORD *)(a3 + 8), a4);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, __int64 a3, unsigned __int8 a4)
{
return mbedtls_mpi_core_cond_swap(a1, a2, *(_QWORD *)(a3 + 8), a4);
}

original_code:
void mbedtls_mpi_mod_raw_cond_swap(mbedtls_mpi_uint *X,
                                   mbedtls_mpi_uint *Y,
                                   const mbedtls_mpi_mod_modulus *N,
                                   unsigned char swap)
{
    mbedtls_mpi_core_cond_swap(X, Y, N->limbs, swap);
}

canonical_original_code:
void mbedtls_mpi_mod_raw_cond_swap(unsigned long *X, unsigned long *Y, struct mbedtls_mpi_mod_modulus *N, unsigned char swap)
{
    mbedtls_mpi_core_cond_swap(X, Y, N->limbs, swap);
}

variable_types:
Y
unsigned long *

variable_types:
swap
unsigned char

user_defined_types: enum mbedtls_mpi_mod_rep_selector { MBEDTLS_MPI_MOD_REP_INVALID=0, MBEDTLS_MPI_MOD_REP_MONTGOMERY=2, MBEDTLS_MPI_MOD_REP_OPT_RED=3 }

user_defined_types: struct mbedtls_mpi_mont_struct { unsigned long *rr; unsigned long mm; }


decompiled_code:
__int64 __fastcall mbedtls_mpi_mod_raw_read(__int64 a1, _QWORD *a2, __int64 a3, __int64 a4, int a5)
{
unsigned int v5; // ebx

if ( a5 == 8 )
{
v5 = mbedtls_mpi_core_read_le(a1, a2[1]);
}
else
{
v5 = -4;
if ( a5 != 9 )
return v5;
v5 = mbedtls_mpi_core_read_be(a1, a2[1]);
}
if ( !v5 && !(unsigned int)mbedtls_mpi_core_lt_ct(a1, *a2, a2[1]) )
v5 = -4;
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, _QWORD *a2, __int64 a3, __int64 a4, int a5)
{
unsigned int v5; // ebx

if ( a5 == 8 )
{
v5 = mbedtls_mpi_core_read_le(a1, a2[1]);
}
else
{
v5 = -4;
if ( a5 != 9 )
return v5;
v5 = mbedtls_mpi_core_read_be(a1, a2[1]);
}
if ( !v5 && !(unsigned int)mbedtls_mpi_core_lt_ct(a1, *a2, a2[1]) )
v5 = -4;
return v5;
}

original_code:
int mbedtls_mpi_mod_raw_read(mbedtls_mpi_uint *X,
                             const mbedtls_mpi_mod_modulus *N,
                             const unsigned char *input,
                             size_t input_length,
                             mbedtls_mpi_mod_ext_rep ext_rep)
{
    int ret = -0x006E;
    switch (ext_rep) {
        case MBEDTLS_MPI_MOD_EXT_REP_LE:
            ret = mbedtls_mpi_core_read_le(X, N->limbs,
                                           input, input_length);
            break;
        case MBEDTLS_MPI_MOD_EXT_REP_BE:
            ret = mbedtls_mpi_core_read_be(X, N->limbs,
                                           input, input_length);
            break;
        default:
            return -0x0004;
    }
    if (ret != 0) {
        goto cleanup;
    }
    if (!mbedtls_mpi_core_lt_ct(X, N->p, N->limbs)) {
        ret = -0x0004;
        goto cleanup;
    }
cleanup:
    return ret;
}

canonical_original_code:
int mbedtls_mpi_mod_raw_read(unsigned long *X, struct mbedtls_mpi_mod_modulus *N, unsigned char *input, unsigned long input_length, enum mbedtls_mpi_mod_ext_rep ext_rep)
{
    int ret = -0x006E;
    switch (ext_rep) {
        case MBEDTLS_MPI_MOD_EXT_REP_LE:
            ret = mbedtls_mpi_core_read_le(X, N->limbs,
                                           input, input_length);
            break;
        case MBEDTLS_MPI_MOD_EXT_REP_BE:
            ret = mbedtls_mpi_core_read_be(X, N->limbs,
                                           input, input_length);
            break;
        default:
            return -0x0004;
    }
    if (ret != 0) {
        goto cleanup;
    }
    if (!mbedtls_mpi_core_lt_ct(X, N->p, N->limbs)) {
        ret = -0x0004;
        goto cleanup;
    }
cleanup:
    return ret;
}

variable_types:
input
unsigned char *

variable_types: enum mbedtls_mpi_mod_ext_rep { MBEDTLS_MPI_MOD_EXT_REP_INVALID=0, MBEDTLS_MPI_MOD_EXT_REP_LE=8, MBEDTLS_MPI_MOD_EXT_REP_BE=9 }

variable_types: ret int


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

if ( a5 == 8 )
return mbedtls_mpi_core_write_le(a1, *(_QWORD *)(a2 + 8));
result = 4294967292LL;
if ( a5 == 9 )
result = mbedtls_mpi_core_write_be(a1, *(_QWORD *)(a2 + 8));
return result;
}

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

if ( a5 == 8 )
return mbedtls_mpi_core_write_le(a1, *(_QWORD *)(a2 + 8));
result = 4294967292LL;
if ( a5 == 9 )
result = mbedtls_mpi_core_write_be(a1, *(_QWORD *)(a2 + 8));
return result;
}

original_code:
int mbedtls_mpi_mod_raw_write(const mbedtls_mpi_uint *A,
                              const mbedtls_mpi_mod_modulus *N,
                              unsigned char *output,
                              size_t output_length,
                              mbedtls_mpi_mod_ext_rep ext_rep)
{
    switch (ext_rep) {
        case MBEDTLS_MPI_MOD_EXT_REP_LE:
            return mbedtls_mpi_core_write_le(A, N->limbs,
                                             output, output_length);
        case MBEDTLS_MPI_MOD_EXT_REP_BE:
            return mbedtls_mpi_core_write_be(A, N->limbs,
                                             output, output_length);
        default:
            return -0x0004;
    }
}

canonical_original_code:
int mbedtls_mpi_mod_raw_write(unsigned long *A, struct mbedtls_mpi_mod_modulus *N, unsigned char *output, unsigned long output_length, enum mbedtls_mpi_mod_ext_rep ext_rep)
{
    switch (ext_rep) {
        case MBEDTLS_MPI_MOD_EXT_REP_LE:
            return mbedtls_mpi_core_write_le(A, N->limbs,
                                             output, output_length);
        case MBEDTLS_MPI_MOD_EXT_REP_BE:
            return mbedtls_mpi_core_write_be(A, N->limbs,
                                             output, output_length);
        default:
            return -0x0004;
    }
}

variable_types:
output
unsigned char *

user_defined_types: enum mbedtls_mpi_mod_ext_rep { MBEDTLS_MPI_MOD_EXT_REP_INVALID=0, MBEDTLS_MPI_MOD_EXT_REP_LE=8, MBEDTLS_MPI_MOD_EXT_REP_BE=9 }


decompiled_code:
__int64 __fastcall mbedtls_mpi_mod_raw_sub(__int64 a1, __int64 a2, __int64 a3, _QWORD *a4)
{
__int64 v5; // rax

v5 = mbedtls_mpi_core_sub(a1, a2, a3, a4[1]);
return mbedtls_mpi_core_add_if(a1, *a4, a4[1], v5);
}

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

v5 = mbedtls_mpi_core_sub(a1, a2, a3, a4[1]);
return mbedtls_mpi_core_add_if(a1, *a4, a4[1], v5);
}

original_code:
void mbedtls_mpi_mod_raw_sub(mbedtls_mpi_uint *X,
                             const mbedtls_mpi_uint *A,
                             const mbedtls_mpi_uint *B,
                             const mbedtls_mpi_mod_modulus *N)
{
    mbedtls_mpi_uint c = mbedtls_mpi_core_sub(X, A, B, N->limbs);
    (void) mbedtls_mpi_core_add_if(X, N->p, N->limbs, (unsigned) c);
}

canonical_original_code:
void mbedtls_mpi_mod_raw_sub(unsigned long *X, unsigned long *A, unsigned long *B, struct mbedtls_mpi_mod_modulus *N)
{
    unsigned long c = mbedtls_mpi_core_sub(X, A, B, N->limbs);
    (void) mbedtls_mpi_core_add_if(X, N->p, N->limbs, (unsigned int) c);
}

variable_types:
B
unsigned long *

variable_types:
c
unsigned long


decompiled_code:
__int64 __fastcall mbedtls_mpi_mod_raw_mul(__int64 a1, __int64 a2, __int64 a3, _QWORD *a4, __int64 a5)
{
return mbedtls_mpi_core_montmul(a1, a2, a3, a4[1], *a4, a4[1], a4[5], a5);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2, __int64 a3, _QWORD *a4, __int64 a5)
{
return mbedtls_mpi_core_montmul(a1, a2, a3, a4[1], *a4, a4[1], a4[5], a5);
}

original_code:
void mbedtls_mpi_mod_raw_mul(mbedtls_mpi_uint *X,
                             const mbedtls_mpi_uint *A,
                             const mbedtls_mpi_uint *B,
                             const mbedtls_mpi_mod_modulus *N,
                             mbedtls_mpi_uint *T)
{
    mbedtls_mpi_core_montmul(X, A, B, N->limbs, N->p, N->limbs,
                             N->rep.mont.mm, T);
}

canonical_original_code:
void mbedtls_mpi_mod_raw_mul(unsigned long *X, unsigned long *A, unsigned long *B, struct mbedtls_mpi_mod_modulus *N, unsigned long *T)
{
    mbedtls_mpi_core_montmul(X, A, B, N->limbs, N->p, N->limbs,
                             N->rep.mont.mm, T);
}

variable_types:
T
unsigned long *


decompiled_code:
__int64 __fastcall mbedtls_mpi_mod_raw_inv_prime_working_limbs(__int64 a1)
{
return a1 + mbedtls_mpi_core_exp_mod_working_limbs(a1, a1);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
return a1 + mbedtls_mpi_core_exp_mod_working_limbs(a1, a1);
}

original_code:
size_t mbedtls_mpi_mod_raw_inv_prime_working_limbs(size_t AN_limbs)
{
    return AN_limbs +
           mbedtls_mpi_core_exp_mod_working_limbs(AN_limbs, AN_limbs);
}

canonical_original_code:
unsigned long mbedtls_mpi_mod_raw_inv_prime_working_limbs(unsigned long AN_limbs)
{
    return AN_limbs +
           mbedtls_mpi_core_exp_mod_working_limbs(AN_limbs, AN_limbs);
}


decompiled_code:
__int64 __fastcall mbedtls_mpi_mod_raw_inv_prime(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6)
{
mbedtls_mpi_core_sub_int(a6, a3, 2LL);
return mbedtls_mpi_core_exp_mod(a1, a2, a3, a4, a6, a4, a5, a6 + 8 * a4);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6)
{
mbedtls_mpi_core_sub_int(a6, a3, 2LL);
return mbedtls_mpi_core_exp_mod(a1, a2, a3, a4, a6, a4, a5, a6 + 8 * a4);
}

original_code:
void mbedtls_mpi_mod_raw_inv_prime(mbedtls_mpi_uint *X,
                                   const mbedtls_mpi_uint *A,
                                   const mbedtls_mpi_uint *N,
                                   size_t AN_limbs,
                                   const mbedtls_mpi_uint *RR,
                                   mbedtls_mpi_uint *T)
{
    mbedtls_mpi_uint *Nminus2 = T;
    (void) mbedtls_mpi_core_sub_int(Nminus2, N, 2, AN_limbs);
    mbedtls_mpi_core_exp_mod(X,
                             A, N, AN_limbs, Nminus2, AN_limbs,
                             RR, T + AN_limbs);
}

canonical_original_code:
void mbedtls_mpi_mod_raw_inv_prime(unsigned long *X, unsigned long *A, unsigned long *N, unsigned long AN_limbs, unsigned long *RR, unsigned long *T)
{
    unsigned long *Nminus2 = T;
    (void) mbedtls_mpi_core_sub_int(Nminus2, N, 2, AN_limbs);
    mbedtls_mpi_core_exp_mod(X,
                             A, N, AN_limbs, Nminus2, AN_limbs,
                             RR, T + AN_limbs);
}

variable_types:
N
unsigned long *

variable_types:
RR
unsigned long *


decompiled_code:
__int64 __fastcall mbedtls_mpi_mod_raw_add(__int64 a1, __int64 a2, __int64 a3, _QWORD *a4)
{
unsigned int v5; // er12
int v6; // eax

v5 = mbedtls_mpi_core_add(a1, a2, a3, a4[1]);
v6 = mbedtls_mpi_core_sub(a1, a1, *a4, a4[1]);
return mbedtls_mpi_core_add_if(a1, *a4, a4[1], v6 ^ v5);
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, __int64 a2, __int64 a3, _QWORD *a4)
{
unsigned int v5; // er12
int v6; // eax

v5 = mbedtls_mpi_core_add(a1, a2, a3, a4[1]);
v6 = mbedtls_mpi_core_sub(a1, a1, *a4, a4[1]);
return mbedtls_mpi_core_add_if(a1, *a4, a4[1], v6 ^ v5);
}

original_code:
void mbedtls_mpi_mod_raw_add(mbedtls_mpi_uint *X,
                             const mbedtls_mpi_uint *A,
                             const mbedtls_mpi_uint *B,
                             const mbedtls_mpi_mod_modulus *N)
{
    mbedtls_mpi_uint carry, borrow;
    carry = mbedtls_mpi_core_add(X, A, B, N->limbs);
    borrow = mbedtls_mpi_core_sub(X, X, N->p, N->limbs);
    (void) mbedtls_mpi_core_add_if(X, N->p, N->limbs, (unsigned) (carry ^ borrow));
}

canonical_original_code:
void mbedtls_mpi_mod_raw_add(unsigned long *X, unsigned long *A, unsigned long *B, struct mbedtls_mpi_mod_modulus *N)
{
    unsigned long carry; unsigned long borrow;
    carry = mbedtls_mpi_core_add(X, A, B, N->limbs);
    borrow = mbedtls_mpi_core_sub(X, X, N->p, N->limbs);
    (void) mbedtls_mpi_core_add_if(X, N->p, N->limbs, (unsigned int) (carry ^ borrow));
}

variable_types:
carry
unsigned long

variable_types:
borrow
unsigned long


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

v2 = a2[1];
v3 = mbedtls_mpi_core_montmul_working_limbs(v2);
v4 = calloc(v3, 8uLL);
if ( !v4 )
return 4294967280LL;
v5 = v4;
mbedtls_mpi_core_to_mont_rep(a1, a1, *a2, v2, a2[5], a2[4], v4);
mbedtls_platform_zeroize(v5, 8 * v3);
free(v5);
return 0LL;
}

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

v2 = a2[1];
v3 = func0(v2);
v4 = calloc(v3, 8uLL);
if ( !v4 )
return 4294967280LL;
v5 = v4;
mbedtls_mpi_core_to_mont_rep(a1, a1, *a2, v2, a2[5], a2[4], v4);
mbedtls_platform_zeroize(v5, 8 * v3);
free(v5);
return 0LL;
}

original_code:
int mbedtls_mpi_mod_raw_to_mont_rep(mbedtls_mpi_uint *X,
                                    const mbedtls_mpi_mod_modulus *N)
{
    mbedtls_mpi_uint *T;
    const size_t t_limbs = mbedtls_mpi_core_montmul_working_limbs(N->limbs);
    if ((T = (mbedtls_mpi_uint *) calloc(t_limbs, (sizeof(mbedtls_mpi_uint)))) == ((void *)0)) {
        return -0x0010;
    }
    mbedtls_mpi_core_to_mont_rep(X, X, N->p, N->limbs,
                                 N->rep.mont.mm, N->rep.mont.rr, T);
    mbedtls_platform_zeroize(T, t_limbs * (sizeof(mbedtls_mpi_uint)));
    free(T);
    return 0;
}

canonical_original_code:
int mbedtls_mpi_mod_raw_to_mont_rep(unsigned long *X, struct mbedtls_mpi_mod_modulus *N)
{
    unsigned long *T;
    unsigned long t_limbs = mbedtls_mpi_core_montmul_working_limbs(N->limbs);
    if ((T = (unsigned long *) calloc(t_limbs, (sizeof(mbedtls_mpi_uint)))) == ((void *)0)) {
        return -0x0010;
    }
    mbedtls_mpi_core_to_mont_rep(X, X, N->p, N->limbs,
                                 N->rep.mont.mm, N->rep.mont.rr, T);
    mbedtls_platform_zeroize(T, t_limbs * (sizeof(mbedtls_mpi_uint)));
    free(T);
    return 0;
}

variable_types:
X
unsigned long *


decompiled_code:
__int64 __fastcall mbedtls_mpi_mod_raw_canonical_to_modulus_rep(__int64 a1, __int64 a2)
{
int v2; // eax
__int64 result; // rax

v2 = *(_DWORD *)(a2 + 24);
if ( v2 == 2 )
result = mbedtls_mpi_mod_raw_to_mont_rep(a1, (_QWORD *)a2);
else
result = 4 * (unsigned int)(v2 == 3) - 4;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, __int64 a2)
{
int v2; // eax
__int64 result; // rax

v2 = *(_DWORD *)(a2 + 24);
if ( v2 == 2 )
result = func10(a1, (_QWORD *)a2);
else
result = 4 * (unsigned int)(v2 == 3) - 4;
return result;
}

original_code:
int mbedtls_mpi_mod_raw_canonical_to_modulus_rep(
    mbedtls_mpi_uint *X,
    const mbedtls_mpi_mod_modulus *N)
{
    switch (N->int_rep) {
        case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
            return mbedtls_mpi_mod_raw_to_mont_rep(X, N);
        case MBEDTLS_MPI_MOD_REP_OPT_RED:
            return 0;
        default:
            return -0x0004;
    }
}

canonical_original_code:
int mbedtls_mpi_mod_raw_canonical_to_modulus_rep(unsigned long *X, struct mbedtls_mpi_mod_modulus *N)
{
    switch (N->int_rep) {
        case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
            return mbedtls_mpi_mod_raw_to_mont_rep(X, N);
        case MBEDTLS_MPI_MOD_REP_OPT_RED:
            return 0;
        default:
            return -0x0004;
    }
}


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

result = mbedtls_mpi_core_random(a1, a2, *a3, a3[1], a4, a5);
if ( !(_DWORD)result )
result = mbedtls_mpi_mod_raw_canonical_to_modulus_rep(a1, (__int64)a3);
return result;
}

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

result = mbedtls_mpi_core_random(a1, a2, *a3, a3[1], a4, a5);
if ( !(_DWORD)result )
result = func11(a1, (__int64)a3);
return result;
}

original_code:
int mbedtls_mpi_mod_raw_random(mbedtls_mpi_uint *X,
                               mbedtls_mpi_uint min,
                               const mbedtls_mpi_mod_modulus *N,
                               int (*f_rng)(void *, unsigned char *, size_t),
                               void *p_rng)
{
    int ret = mbedtls_mpi_core_random(X, min, N->p, N->limbs, f_rng, p_rng);
    if (ret != 0) {
        return ret;
    }
    return mbedtls_mpi_mod_raw_canonical_to_modulus_rep(X, N);
}

canonical_original_code:
int mbedtls_mpi_mod_raw_random(unsigned long *X, unsigned long min, struct mbedtls_mpi_mod_modulus *N, int (*f_rng)(void *, unsigned char *, unsigned long), void *p_rng)
{
    int ret = mbedtls_mpi_core_random(X, min, N->p, N->limbs, f_rng, p_rng);
    if (ret != 0) {
        return ret;
    }
    return mbedtls_mpi_mod_raw_canonical_to_modulus_rep(X, N);
}

variable_types:
min
unsigned long

variable_types: int (*)(void *, unsigned char *, unsigned long)


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

v2 = a2[1];
v3 = mbedtls_mpi_core_montmul_working_limbs(v2);
v4 = calloc(v3, 8uLL);
if ( !v4 )
return 4294967280LL;
v5 = v4;
mbedtls_mpi_core_from_mont_rep(a1, a1, *a2, v2, a2[5], v4);
mbedtls_platform_zeroize(v5, 8 * v3);
free(v5);
return 0LL;
}

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

v2 = a2[1];
v3 = func0(v2);
v4 = calloc(v3, 8uLL);
if ( !v4 )
return 4294967280LL;
v5 = v4;
mbedtls_mpi_core_from_mont_rep(a1, a1, *a2, v2, a2[5], v4);
mbedtls_platform_zeroize(v5, 8 * v3);
free(v5);
return 0LL;
}

original_code:
int mbedtls_mpi_mod_raw_from_mont_rep(mbedtls_mpi_uint *X,
                                      const mbedtls_mpi_mod_modulus *N)
{
    const size_t t_limbs = mbedtls_mpi_core_montmul_working_limbs(N->limbs);
    mbedtls_mpi_uint *T;
    if ((T = (mbedtls_mpi_uint *) calloc(t_limbs, (sizeof(mbedtls_mpi_uint)))) == ((void *)0)) {
        return -0x0010;
    }
    mbedtls_mpi_core_from_mont_rep(X, X, N->p, N->limbs, N->rep.mont.mm, T);
    mbedtls_platform_zeroize(T, t_limbs * (sizeof(mbedtls_mpi_uint)));
    free(T);
    return 0;
}

canonical_original_code:
int mbedtls_mpi_mod_raw_from_mont_rep(unsigned long *X, struct mbedtls_mpi_mod_modulus *N)
{
    unsigned long t_limbs = mbedtls_mpi_core_montmul_working_limbs(N->limbs);
    unsigned long *T;
    if ((T = (unsigned long *) calloc(t_limbs, (sizeof(mbedtls_mpi_uint)))) == ((void *)0)) {
        return -0x0010;
    }
    mbedtls_mpi_core_from_mont_rep(X, X, N->p, N->limbs, N->rep.mont.mm, T);
    mbedtls_platform_zeroize(T, t_limbs * (sizeof(mbedtls_mpi_uint)));
    free(T);
    return 0;
}

variable_types: struct mbedtls_mpi_mod_modulus { unsigned long *p; unsigned long limbs; unsigned long bits; enum mbedtls_mpi_mod_rep_selector { MBEDTLS_MPI_MOD_REP_INVALID=0, MBEDTLS_MPI_MOD_REP_MONTGOMERY=2, MBEDTLS_MPI_MOD_REP_OPT_RED=3 } int_rep; union rep { struct mbedtls_mpi_mont_struct { unsigned long *rr; unsigned long mm; } mont; void *ored; } rep; } *


decompiled_code:
__int64 __fastcall mbedtls_mpi_mod_raw_modulus_to_canonical_rep(__int64 a1, __int64 a2)
{
int v2; // eax
__int64 result; // rax

v2 = *(_DWORD *)(a2 + 24);
if ( v2 == 2 )
result = mbedtls_mpi_mod_raw_from_mont_rep(a1, (_QWORD *)a2);
else
result = 4 * (unsigned int)(v2 == 3) - 4;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 a1, __int64 a2)
{
int v2; // eax
__int64 result; // rax

v2 = *(_DWORD *)(a2 + 24);
if ( v2 == 2 )
result = func13(a1, (_QWORD *)a2);
else
result = 4 * (unsigned int)(v2 == 3) - 4;
return result;
}

original_code:
int mbedtls_mpi_mod_raw_modulus_to_canonical_rep(
    mbedtls_mpi_uint *X,
    const mbedtls_mpi_mod_modulus *N)
{
    switch (N->int_rep) {
        case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
            return mbedtls_mpi_mod_raw_from_mont_rep(X, N);
        case MBEDTLS_MPI_MOD_REP_OPT_RED:
            return 0;
        default:
            return -0x0004;
    }
}

canonical_original_code:
int mbedtls_mpi_mod_raw_modulus_to_canonical_rep(unsigned long *X, struct mbedtls_mpi_mod_modulus *N)
{
    switch (N->int_rep) {
        case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
            return mbedtls_mpi_mod_raw_from_mont_rep(X, N);
        case MBEDTLS_MPI_MOD_REP_OPT_RED:
            return 0;
        default:
            return -0x0004;
    }
}


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

mbedtls_mpi_core_sub(a1, *a3, a2, a3[1]);
v4 = mbedtls_mpi_core_sub(a1, a1, *a3, a3[1]);
return mbedtls_mpi_core_add_if(a1, *a3, a3[1], v4);
}

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

mbedtls_mpi_core_sub(a1, *a3, a2, a3[1]);
v4 = mbedtls_mpi_core_sub(a1, a1, *a3, a3[1]);
return mbedtls_mpi_core_add_if(a1, *a3, a3[1], v4);
}

original_code:
void mbedtls_mpi_mod_raw_neg(mbedtls_mpi_uint *X,
                             const mbedtls_mpi_uint *A,
                             const mbedtls_mpi_mod_modulus *N)
{
    mbedtls_mpi_core_sub(X, N->p, A, N->limbs);
    mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub(X, X, N->p, N->limbs);
    (void) mbedtls_mpi_core_add_if(X, N->p, N->limbs, (unsigned) borrow);
}

canonical_original_code:
void mbedtls_mpi_mod_raw_neg(unsigned long *X, unsigned long *A, struct mbedtls_mpi_mod_modulus *N)
{
    mbedtls_mpi_core_sub(X, N->p, A, N->limbs);
    unsigned long borrow = mbedtls_mpi_core_sub(X, X, N->p, N->limbs);
    (void) mbedtls_mpi_core_add_if(X, N->p, N->limbs, (unsigned int) borrow);
}

variable_types:
A
unsigned long *


decompiled_code:
__int64 __fastcall ircv3_cap_get(__int64 a1, const char *a2)
{
__int64 result; // rax

if ( !strcmp(a2, "account-notify") )
return a1;
if ( !strcmp(a2, "away-notify") )
return a1 + 16;
if ( !strcmp(a2, "chghost") )
return a1 + 32;
if ( !strcmp(a2, "extended-join") )
return a1 + 48;
if ( !strcmp(a2, "invite-notify") )
return a1 + 64;
if ( !strcmp(a2, "multi-prefix") )
return a1 + 80;
result = a1 + 96;
if ( strcmp(a2, "sasl") )
result = 0LL;
return result;
}

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

if ( !strcmp(a2, "account-notify") )
return a1;
if ( !strcmp(a2, "away-notify") )
return a1 + 16;
if ( !strcmp(a2, "chghost") )
return a1 + 32;
if ( !strcmp(a2, "extended-join") )
return a1 + 48;
if ( !strcmp(a2, "invite-notify") )
return a1 + 64;
if ( !strcmp(a2, "multi-prefix") )
return a1 + 80;
result = a1 + 96;
if ( strcmp(a2, "sasl") )
result = 0LL;
return result;
}

original_code:
struct ircv3_cap*
ircv3_cap_get(struct ircv3_caps *caps, const char *cap_str)
{
 if (!strcmp(cap_str, "account-notify")) return &(caps->account_notify); if (!strcmp(cap_str, "away-notify")) return &(caps->away_notify); if (!strcmp(cap_str, "chghost")) return &(caps->chghost); if (!strcmp(cap_str, "extended-join")) return &(caps->extended_join); if (!strcmp(cap_str, "invite-notify")) return &(caps->invite_notify); if (!strcmp(cap_str, "multi-prefix")) return &(caps->multi_prefix); if (!strcmp(cap_str, "sasl")) return &(caps->sasl);
 return ((void *)0);
}

canonical_original_code:
struct ircv3_cap *ircv3_cap_get(struct ircv3_caps *caps, char *cap_str)
{
 if (!strcmp(cap_str, "account-notify")) return &(caps->account_notify); if (!strcmp(cap_str, "away-notify")) return &(caps->away_notify); if (!strcmp(cap_str, "chghost")) return &(caps->chghost); if (!strcmp(cap_str, "extended-join")) return &(caps->extended_join); if (!strcmp(cap_str, "invite-notify")) return &(caps->invite_notify); if (!strcmp(cap_str, "multi-prefix")) return &(caps->multi_prefix); if (!strcmp(cap_str, "sasl")) return &(caps->sasl);
 return ((void *)0);
}

variable_types: struct ircv3_caps { struct ircv3_cap account_notify; struct ircv3_cap away_notify; struct ircv3_cap chghost; struct ircv3_cap extended_join; struct ircv3_cap invite_notify; struct ircv3_cap multi_prefix; struct ircv3_cap sasl; } *

return_type: struct ircv3_cap { char *val; unsigned int req; unsigned int req_auto; unsigned int set; unsigned int supported; unsigned int supports_del; unsigned int supports_req; } *


original_code:
void
ircv3_caps(struct ircv3_caps *caps)
{
 caps->account_notify.val = ((void *)0); caps->account_notify.req = 0; caps->account_notify.set = 0; caps->account_notify.supported = 0; caps->account_notify.supports_del = !((1 << 0) & (1 << 1)); caps->account_notify.supports_req = !((1 << 0) & (1 << 2)); caps->account_notify.req_auto = ((1 << 0) & (1 << 0)); caps->away_notify.val = ((void *)0); caps->away_notify.req = 0; caps->away_notify.set = 0; caps->away_notify.supported = 0; caps->away_notify.supports_del = !((1 << 0) & (1 << 1)); caps->away_notify.supports_req = !((1 << 0) & (1 << 2)); caps->away_notify.req_auto = ((1 << 0) & (1 << 0)); caps->chghost.val = ((void *)0); caps->chghost.req = 0; caps->chghost.set = 0; caps->chghost.supported = 0; caps->chghost.supports_del = !((1 << 0) & (1 << 1)); caps->chghost.supports_req = !((1 << 0) & (1 << 2)); caps->chghost.req_auto = ((1 << 0) & (1 << 0)); caps->extended_join.val = ((void *)0); caps->extended_join.req = 0; caps->extended_join.set = 0; caps->extended_join.supported = 0; caps->extended_join.supports_del = !((1 << 0) & (1 << 1)); caps->extended_join.supports_req = !((1 << 0) & (1 << 2)); caps->extended_join.req_auto = ((1 << 0) & (1 << 0)); caps->invite_notify.val = ((void *)0); caps->invite_notify.req = 0; caps->invite_notify.set = 0; caps->invite_notify.supported = 0; caps->invite_notify.supports_del = !((1 << 0) & (1 << 1)); caps->invite_notify.supports_req = !((1 << 0) & (1 << 2)); caps->invite_notify.req_auto = ((1 << 0) & (1 << 0)); caps->multi_prefix.val = ((void *)0); caps->multi_prefix.req = 0; caps->multi_prefix.set = 0; caps->multi_prefix.supported = 0; caps->multi_prefix.supports_del = !((1 << 0) & (1 << 1)); caps->multi_prefix.supports_req = !((1 << 0) & (1 << 2)); caps->multi_prefix.req_auto = ((1 << 0) & (1 << 0)); caps->sasl.val = ((void *)0); caps->sasl.req = 0; caps->sasl.set = 0; caps->sasl.supported = 0; caps->sasl.supports_del = !((1 << 0) & (1 << 1)); caps->sasl.supports_req = !((1 << 0) & (1 << 2)); caps->sasl.req_auto = ((1 << 0) & (1 << 0));
}

variable_types: struct ircv3_caps { struct ircv3_cap { char *val; unsigned int req; unsigned int req_auto; unsigned int set; unsigned int supported; unsigned int supports_del; unsigned int supports_req; } account_notify; struct ircv3_cap away_notify; struct ircv3_cap chghost; struct ircv3_cap extended_join; struct ircv3_cap invite_notify; struct ircv3_cap multi_prefix; struct ircv3_cap sasl; } *

user_defined_types: struct ircv3_caps { struct ircv3_cap account_notify; struct ircv3_cap away_notify; struct ircv3_cap chghost; struct ircv3_cap extended_join; struct ircv3_cap invite_notify; struct ircv3_cap multi_prefix; struct ircv3_cap sasl; }


original_code:
void
ircv3_caps_reset(struct ircv3_caps *caps)
{
 free((void *)caps->account_notify.val); caps->account_notify.val = ((void *)0); caps->account_notify.req = 0; caps->account_notify.set = 0; caps->account_notify.supported = 0; free((void *)caps->away_notify.val); caps->away_notify.val = ((void *)0); caps->away_notify.req = 0; caps->away_notify.set = 0; caps->away_notify.supported = 0; free((void *)caps->chghost.val); caps->chghost.val = ((void *)0); caps->chghost.req = 0; caps->chghost.set = 0; caps->chghost.supported = 0; free((void *)caps->extended_join.val); caps->extended_join.val = ((void *)0); caps->extended_join.req = 0; caps->extended_join.set = 0; caps->extended_join.supported = 0; free((void *)caps->invite_notify.val); caps->invite_notify.val = ((void *)0); caps->invite_notify.req = 0; caps->invite_notify.set = 0; caps->invite_notify.supported = 0; free((void *)caps->multi_prefix.val); caps->multi_prefix.val = ((void *)0); caps->multi_prefix.req = 0; caps->multi_prefix.set = 0; caps->multi_prefix.supported = 0; free((void *)caps->sasl.val); caps->sasl.val = ((void *)0); caps->sasl.req = 0; caps->sasl.set = 0; caps->sasl.supported = 0;
}

user_defined_types: struct ircv3_cap { char *val; unsigned int req; unsigned int req_auto; unsigned int set; unsigned int supported; unsigned int supports_del; unsigned int supports_req; }


decompiled_code:
void __fastcall ircv3_sasl_reset(__int64 a1)
{
*(_DWORD *)(a1 + 4) = 0;
}

canonical_decompiled_code:
void __fastcall func4(__int64 a1)
{
*(_DWORD *)(a1 + 4) = 0;
}

original_code:
void
ircv3_sasl_reset(struct ircv3_sasl *sasl)
{
 sasl->state = IRCV3_SASL_STATE_NONE;
}

variable_types: struct ircv3_sasl { enum { IRCV3_SASL_MECH_NONE=0, IRCV3_SASL_MECH_EXTERNAL=1, IRCV3_SASL_MECH_PLAIN=2 } mech; enum { IRCV3_SASL_STATE_NONE=0, IRCV3_SASL_STATE_REQ_MECH=1, IRCV3_SASL_STATE_AUTHENTICATED=2 } state; char *user; char *pass; } *


decompiled_code:
void __fastcall mbedtls_pem_init(_QWORD *a1)
{
*a1 = 0LL;
a1[1] = 0LL;
a1[2] = 0LL;
}

canonical_decompiled_code:
void __fastcall func1(_QWORD *a1)
{
*a1 = 0LL;
a1[1] = 0LL;
a1[2] = 0LL;
}

original_code:
void mbedtls_pem_init(mbedtls_pem_context *ctx)
{
    memset(ctx, 0, sizeof(mbedtls_pem_context));
}

canonical_original_code:
void mbedtls_pem_init(struct mbedtls_pem_context *ctx)
{
    memset(ctx, 0, sizeof(mbedtls_pem_context));
}

variable_types: struct mbedtls_pem_context { unsigned char *buf; unsigned long buflen; unsigned char *info; } *


decompiled_code:
__int64 __fastcall mbedtls_pem_read_buffer(_QWORD *a1, const char *a2, const char *a3, const char *a4, __int64 a5, __int64 a6, _QWORD *a7)
{
char *v9; // rbx
char *v10; // rax
char *v11; // rbp
size_t v12; // rax
char *v13; // rbx
char *v14; // rbx
char *v15; // rbx
size_t v16; // rax
char *v17; // rax
signed __int64 v18; // r12
size_t v19; // r14
void *v20; // rax
void *v21; // rbp
unsigned int v22; // ebx
size_t size[8]; // [rsp+8h] [rbp-40h] BYREF

if ( !a1 )
return (unsigned int)-5248;
v9 = strstr(a4, a2);
if ( !v9 )
return (unsigned int)-4224;
v10 = strstr(a4, a3);
v11 = v10;
if ( !v10 || v9 >= v10 )
return (unsigned int)-4224;
v12 = strlen(a2);
v13 = &v9[v12 + (v9[v12] == 32)];
v14 = &v13[*v13 == 13];
if ( *v14 != 10 )
return (unsigned int)-4224;
v15 = v14 + 1;
v16 = strlen(a3);
v17 = &v11[v16 + (v11[v16] == 32)];
*a7 = &v17[(*v17 == 13) + (v17[*v17 == 13] == 10)] - a4;
v18 = v11 - v15;
if ( v11 - v15 > 21 && !memcmp(v15, "Proc-Type: 4,ENCRYPTED", 0x16uLL) )
return (unsigned int)-5120;
if ( v11 <= v15 )
return (unsigned int)-4352;
if ( (unsigned int)mbedtls_base64_decode(0LL, 0LL, size, v15, v11 - v15) == -44 )
return (unsigned int)mbedtls_error_add(-4352, -44);
v19 = size[0];
v20 = calloc(1uLL, size[0]);
v21 = v20;
if ( !v20 )
return (unsigned int)-4480;
v22 = mbedtls_base64_decode(v20, v19, size, v15, v18);
if ( v22 )
{
mbedtls_platform_zeroize(v21, size[0]);
free(v21);
v22 = mbedtls_error_add(-4352, v22);
}
else
{
*a1 = v21;
a1[1] = size[0];
}
return v22;
}

canonical_decompiled_code:
__int64 __fastcall func2(_QWORD *a1, const char *a2, const char *a3, const char *a4, __int64 a5, __int64 a6, _QWORD *a7)
{
char *v9; // rbx
char *v10; // rax
char *v11; // rbp
size_t v12; // rax
char *v13; // rbx
char *v14; // rbx
char *v15; // rbx
size_t v16; // rax
char *v17; // rax
signed __int64 v18; // r12
size_t v19; // r14
void *v20; // rax
void *v21; // rbp
unsigned int v22; // ebx
size_t size[8]; // [rsp+8h] [rbp-40h] BYREF

if ( !a1 )
return (unsigned int)-5248;
v9 = strstr(a4, a2);
if ( !v9 )
return (unsigned int)-4224;
v10 = strstr(a4, a3);
v11 = v10;
if ( !v10 || v9 >= v10 )
return (unsigned int)-4224;
v12 = strlen(a2);
v13 = &v9[v12 + (v9[v12] == 32)];
v14 = &v13[*v13 == 13];
if ( *v14 != 10 )
return (unsigned int)-4224;
v15 = v14 + 1;
v16 = strlen(a3);
v17 = &v11[v16 + (v11[v16] == 32)];
*a7 = &v17[(*v17 == 13) + (v17[*v17 == 13] == 10)] - a4;
v18 = v11 - v15;
if ( v11 - v15 > 21 && !memcmp(v15, "Proc-Type: 4,ENCRYPTED", 0x16uLL) )
return (unsigned int)-5120;
if ( v11 <= v15 )
return (unsigned int)-4352;
if ( (unsigned int)mbedtls_base64_decode(0LL, 0LL, size, v15, v11 - v15) == -44 )
return (unsigned int)func0(-4352, -44);
v19 = size[0];
v20 = calloc(1uLL, size[0]);
v21 = v20;
if ( !v20 )
return (unsigned int)-4480;
v22 = mbedtls_base64_decode(v20, v19, size, v15, v18);
if ( v22 )
{
mbedtls_platform_zeroize(v21, size[0]);
free(v21);
v22 = func0(-4352, v22);
}
else
{
*a1 = v21;
a1[1] = size[0];
}
return v22;
}

original_code:
int mbedtls_pem_read_buffer(mbedtls_pem_context *ctx, const char *header, const char *footer,
                            const unsigned char *data, const unsigned char *pwd,
                            size_t pwdlen, size_t *use_len)
{
    int ret, enc;
    size_t len;
    unsigned char *buf;
    const unsigned char *s1, *s2, *end;
    ((void) pwd);
    ((void) pwdlen);
    if (ctx == ((void *)0)) {
        return -0x1480;
    }
    s1 = (unsigned char *) strstr((const char *) data, header);
    if (s1 == ((void *)0)) {
        return -0x1080;
    }
    s2 = (unsigned char *) strstr((const char *) data, footer);
    if (s2 == ((void *)0) || s2 <= s1) {
        return -0x1080;
    }
    s1 += strlen(header);
    if (*s1 == ' ') {
        s1++;
    }
    if (*s1 == '\r') {
        s1++;
    }
    if (*s1 == '\n') {
        s1++;
    } else {
        return -0x1080;
    }
    end = s2;
    end += strlen(footer);
    if (*end == ' ') {
        end++;
    }
    if (*end == '\r') {
        end++;
    }
    if (*end == '\n') {
        end++;
    }
    *use_len = end - data;
    enc = 0;
    if (s2 - s1 >= 22 && memcmp(s1, "Proc-Type: 4,ENCRYPTED", 22) == 0) {
        return -0x1400;
    }
    if (s1 >= s2) {
        return -0x1100;
    }
    ret = mbedtls_base64_decode(((void *)0), 0, &len, s1, s2 - s1);
    if (ret == -0x002C) {
        return mbedtls_error_add(-0x1100, ret, "pem.c", 497);
    }
    if ((buf = calloc(1, len)) == ((void *)0)) {
        return -0x1180;
    }
    if ((ret = mbedtls_base64_decode(buf, len, &len, s1, s2 - s1)) != 0) {
        mbedtls_platform_zeroize(buf, len);
        free(buf);
        return mbedtls_error_add(-0x1100, ret, "pem.c", 507);
    }
    if (enc != 0) {
        mbedtls_platform_zeroize(buf, len);
        free(buf);
        return -0x1400;
    }
    ctx->buf = buf;
    ctx->buflen = len;
    return 0;
}

canonical_original_code:
int mbedtls_pem_read_buffer(struct mbedtls_pem_context *ctx, char *header, char *footer, unsigned char *data, unsigned char *pwd, unsigned long pwdlen, unsigned long *use_len)
{
    int ret; int enc;
    unsigned long len;
    unsigned char *buf;
    unsigned char *s1; unsigned char *s2; unsigned char *end;
    ((void) pwd);
    ((void) pwdlen);
    if (ctx == ((void *)0)) {
        return -0x1480;
    }
    s1 = (unsigned char *) strstr((char *) data, header);
    if (s1 == ((void *)0)) {
        return -0x1080;
    }
    s2 = (unsigned char *) strstr((char *) data, footer);
    if (s2 == ((void *)0) || s2 <= s1) {
        return -0x1080;
    }
    s1 += strlen(header);
    if (*s1 == ' ') {
        s1++;
    }
    if (*s1 == '\r') {
        s1++;
    }
    if (*s1 == '\n') {
        s1++;
    } else {
        return -0x1080;
    }
    end = s2;
    end += strlen(footer);
    if (*end == ' ') {
        end++;
    }
    if (*end == '\r') {
        end++;
    }
    if (*end == '\n') {
        end++;
    }
    *use_len = end - data;
    enc = 0;
    if (s2 - s1 >= 22 && memcmp(s1, "Proc-Type: 4,ENCRYPTED", 22) == 0) {
        return -0x1400;
    }
    if (s1 >= s2) {
        return -0x1100;
    }
    ret = mbedtls_base64_decode(((void *)0), 0, &len, s1, s2 - s1);
    if (ret == -0x002C) {
        return mbedtls_error_add(-0x1100, ret, "pem.c", 497);
    }
    if ((buf = calloc(1, len)) == ((void *)0)) {
        return -0x1180;
    }
    if ((ret = mbedtls_base64_decode(buf, len, &len, s1, s2 - s1)) != 0) {
        mbedtls_platform_zeroize(buf, len);
        free(buf);
        return mbedtls_error_add(-0x1100, ret, "pem.c", 507);
    }
    if (enc != 0) {
        mbedtls_platform_zeroize(buf, len);
        free(buf);
        return -0x1400;
    }
    ctx->buf = buf;
    ctx->buflen = len;
    return 0;
}

variable_types:
header
char *

variable_types:
footer
char *

variable_types:
data
unsigned char *

variable_types:
pwd
unsigned char *

variable_types:
pwdlen
unsigned long

variable_types: ret int

variable_types: enc int

variable_types:
len
unsigned long

variable_types:
buf
unsigned char *

variable_types:
end
unsigned char *

user_defined_types: struct mbedtls_pem_context { unsigned char *buf; unsigned long buflen; unsigned char *info; }