ChangeLogging

decompiled_code:
void ChangeLogging()
{
Syslog(6, "klogd %s.%s, ---------- state change ----------\n", "1.5", "0");
if ( reload_symbols > 0 )
{
ReloadSymbols();
}
else if ( caught_TSTP == 1 )
{
CloseLogSrc();
logsrc = 0;
change_state = 0;
}
else
{
if ( logsrc )
Syslog(6, "Kernel logging re-started after SIGSTOP.");
else
logsrc = GetKernelLogSrc();
change_state = 0;
}
}

canonical_decompiled_code:
void func12()
{
func7(6, "klogd %s.%s, ---------- state change ----------\n", "1.5", "0");
if ( reload_symbols > 0 )
{
func5();
}
else if ( caught_TSTP == 1 )
{
func8();
logsrc = 0;
change_state = 0;
}
else
{
if ( logsrc )
func7(6, "Kernel logging re-started after SIGSTOP.");
else
logsrc = func11();
change_state = 0;
}
}

original_code:
static void ChangeLogging(void)
{
 if ( terminate == 1 )
  Terminate();
 Syslog(6, "klogd %s.%s, ---------- state change ----------\n", "1.5", "0");
 if ( reload_symbols > 0 )
 {
  ReloadSymbols();
  return;
 }
 if ( caught_TSTP == 1 )
 {
  CloseLogSrc();
  logsrc = none;
  change_state = 0;
  return;
 }
 if ( logsrc != none )
 {
  Syslog(6, "Kernel logging re-started after SIGSTOP.");
  change_state = 0;
  return;
 }
 logsrc = GetKernelLogSrc();
 change_state = 0;
 return;
}

canonical_original_code:
static void ChangeLogging()
{
 if ( terminate == 1 )
  Terminate();
 Syslog(6, "klogd %s.%s, ---------- state change ----------\n", "1.5", "0");
 if ( reload_symbols > 0 )
 {
  ReloadSymbols();
  return;
 }
 if ( caught_TSTP == 1 )
 {
  CloseLogSrc();
  logsrc = none;
  change_state = 0;
  return;
 }
 if ( logsrc != none )
 {
  Syslog(6, "Kernel logging re-started after SIGSTOP.");
  change_state = 0;
  return;
 }
 logsrc = GetKernelLogSrc();
 change_state = 0;
 return;
}


LogKernelLine

repo: Distrotech/sysklogd

decompiled_code:
void LogKernelLine()
{
int v0; // eax
int *v1; // rbx
char *v2; // rax

memset(log_buffer, 0, sizeof(log_buffer));
v0 = klogctl(2, log_buffer, 4095);
if ( v0 >= 0 )
{
LogLine(log_buffer, v0);
}
else
{
v1 = _errno_location();
if ( *v1 != 4 )
{
v2 = strerror(*v1);
fprintf(stderr, "klogd: Error return from sys_sycall: %d - %s\n", (unsigned int)*v1, v2);
}
}
}

canonical_decompiled_code:
void func14()
{
int v0; // eax
int *v1; // rbx
char *v2; // rax

memset(log_buffer, 0, sizeof(log_buffer));
v0 = klogctl(2, log_buffer, 4095);
if ( v0 >= 0 )
{
func13(log_buffer, v0);
}
else
{
v1 = _errno_location();
if ( *v1 != 4 )
{
v2 = strerror(*v1);
fprintf(stderr, "klogd: Error return from sys_sycall: %d - %s\n", (unsigned int)*v1, v2);
}
}
}

original_code:
static void LogKernelLine(void)
{
 auto int rdcnt;
 memset(log_buffer, '\0', sizeof(log_buffer));
 if ( (rdcnt = klogctl(2, log_buffer, sizeof(log_buffer)-1)) < 0 )
 {
  if ( (*__errno_location ()) == 4 )
   return;
  fprintf(stderr, "klogd: Error return from sys_sycall: " "%d - %s\n", (*__errno_location ()), strerror((*__errno_location ())));
 }
 else
  LogLine(log_buffer, rdcnt);
 return;
}

canonical_original_code:
static void LogKernelLine()
{
 auto int rdcnt;
 memset(log_buffer, '\0', sizeof(log_buffer));
 if ( (rdcnt = klogctl(2, log_buffer, sizeof(log_buffer)-1)) < 0 )
 {
  if ( (*__errno_location ()) == 4 )
   return;
  fprintf(stderr, "klogd: Error return from sys_sycall: " "%d - %s\n", (*__errno_location ()), strerror((*__errno_location ())));
 }
 else
  LogLine(log_buffer, rdcnt);
 return;
}

variable_types: rdcnt int

return_type: void


LogProcLine

decompiled_code:
void LogProcLine()
{
int v0; // eax
int *v1; // rbx
char *v2; // rax

memset(log_buffer, 0, sizeof(log_buffer));
v0 = read(kmsg, log_buffer, 0xFFFuLL);
if ( v0 >= 0 )
{
LogLine(log_buffer, v0);
}
else
{
v1 = _errno_location();
if ( *v1 != 4 )
{
v2 = strerror(*v1);
Syslog(3, "Cannot read proc file system: %d - %s.", (unsigned int)*v1, v2);
}
}
}

canonical_decompiled_code:
void func15()
{
int v0; // eax
int *v1; // rbx
char *v2; // rax

memset(log_buffer, 0, sizeof(log_buffer));
v0 = read(kmsg, log_buffer, 0xFFFuLL);
if ( v0 >= 0 )
{
func13(log_buffer, v0);
}
else
{
v1 = _errno_location();
if ( *v1 != 4 )
{
v2 = strerror(*v1);
func7(3, "Cannot read proc file system: %d - %s.", (unsigned int)*v1, v2);
}
}
}

original_code:
static void LogProcLine(void)
{
 auto int rdcnt;
 memset(log_buffer, '\0', sizeof(log_buffer));
 if ( (rdcnt = read(kmsg, log_buffer, sizeof(log_buffer)-1)) < 0 )
 {
  if ( (*__errno_location ()) == 4 )
   return;
  Syslog(3, "Cannot read proc file system: %d - %s.", (*__errno_location ()), strerror((*__errno_location ())));
 }
 else
  LogLine(log_buffer, rdcnt);
 return;
}

canonical_original_code:
static void LogProcLine()
{
 auto int rdcnt;
 memset(log_buffer, '\0', sizeof(log_buffer));
 if ( (rdcnt = read(kmsg, log_buffer, sizeof(log_buffer)-1)) < 0 )
 {
  if ( (*__errno_location ()) == 4 )
   return;
  Syslog(3, "Cannot read proc file system: %d - %s.", (*__errno_location ()), strerror((*__errno_location ())));
 }
 else
  LogLine(log_buffer, rdcnt);
 return;
}


FreeSymbols

decompiled_code:
void FreeSymbols()
{
__int64 v0; // rbx

if ( num_syms > 0 )
{
v0 = 0LL;
do
free(*((void **)sym_array + 2 * v0++ + 1));
while ( num_syms > (int)v0 );
}
free(sym_array);
sym_array = 0LL;
num_syms = 0;
}

canonical_decompiled_code:
void func0()
{
__int64 v0; // rbx

if ( num_syms > 0 )
{
v0 = 0LL;
do
free(*((void **)sym_array + 2 * v0++ + 1));
while ( num_syms > (int)v0 );
}
free(sym_array);
sym_array = 0LL;
num_syms = 0;
}

original_code:
static void FreeSymbols()
{
 auto int lp;
 for(lp = 0; lp < num_syms; ++lp)
  free(sym_array[lp].name);
 free(sym_array);
 sym_array = (struct sym_table *) 0;
 num_syms = 0;
 return;
}

variable_types: lp int

user_defined_types: struct sym_table { unsigned long value; char *name; }


closelog

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

result = close(LogFile);
LogFile = -1;
connected = 0;
return result;
}

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

result = close(LogFile);
LogFile = -1;
connected = 0;
return result;
}

original_code:
void
closelog()
{
 (void) close(LogFile);
 LogFile = -1;
 connected = 0;
}


syslog

decompiled_code:
void syslog(unsigned int a1, _BYTE *a2, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a2);
vsyslog(a1, a2, va);
}

canonical_decompiled_code:
void func3(unsigned int a1, _BYTE *a2, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a2);
func2(a1, a2, va);
}

original_code:
void
syslog(int pri, const char *fmt, ...)
{
 va_list ap;
 __builtin_va_start(ap,fmt);
 vsyslog(pri, fmt, ap);
 __builtin_va_end(ap);
}

canonical_original_code:
void syslog(int pri, char *fmt, ...)
{
 __builtin_va_list ap;
 __builtin_va_start(ap,fmt);
 vsyslog(pri, fmt, ap);
 __builtin_va_end(ap);
}

variable_types: pri int

variable_types:
fmt
char *


FreeModules

decompiled_code:
void FreeModules()
{
__int64 v0; // r13
__int64 v1; // r12
char *v2; // rbp
int v3; // eax
__int64 v4; // rbx
void *v5; // rdi

if ( num_modules && sym_array_modules )
{
if ( num_modules > 0 )
{
v0 = 24LL * (unsigned int)num_modules;
v1 = 0LL;
do
{
v2 = (char *)sym_array_modules + v1;
v3 = *(_DWORD *)((char *)sym_array_modules + v1 + 8);
if ( v3 )
{
if ( v3 > 0 )
{
v4 = 0LL;
do
free(*(void **)(*(_QWORD *)v2 + 16 * v4++ + 8));
while ( *((_DWORD *)v2 + 2) > (int)v4 );
}
free(*(void **)v2);
v5 = (void *)*((_QWORD *)v2 + 2);
if ( v5 )
free(v5);
}
v1 += 24LL;
}
while ( v1 != v0 );
}
free(sym_array_modules);
sym_array_modules = 0LL;
num_modules = 0;
}
}

canonical_decompiled_code:
void func1()
{
__int64 v0; // r13
__int64 v1; // r12
char *v2; // rbp
int v3; // eax
__int64 v4; // rbx
void *v5; // rdi

if ( num_modules && sym_array_modules )
{
if ( num_modules > 0 )
{
v0 = 24LL * (unsigned int)num_modules;
v1 = 0LL;
do
{
v2 = (char *)sym_array_modules + v1;
v3 = *(_DWORD *)((char *)sym_array_modules + v1 + 8);
if ( v3 )
{
if ( v3 > 0 )
{
v4 = 0LL;
do
free(*(void **)(*(_QWORD *)v2 + 16 * v4++ + 8));
while ( *((_DWORD *)v2 + 2) > (int)v4 );
}
free(*(void **)v2);
v5 = (void *)*((_QWORD *)v2 + 2);
if ( v5 )
free(v5);
}
v1 += 24LL;
}
while ( v1 != v0 );
}
free(sym_array_modules);
sym_array_modules = 0LL;
num_modules = 0;
}
}

original_code:
static void FreeModules()
{
 auto int nmods,
   nsyms;
 auto struct Module *mp;
 have_modules = 0;
 if ( num_modules == 0 )
  return;
 if ( sym_array_modules == ((void *)0) )
  return;
 for (nmods = 0; nmods < num_modules; ++nmods)
 {
  mp = &sym_array_modules[nmods];
  if ( mp->num_syms == 0 )
   continue;
  for (nsyms= 0; nsyms < mp->num_syms; ++nsyms)
   free(mp->sym_array[nsyms].name);
  free(mp->sym_array);
  if ( mp->name != ((void *)0) )
   free(mp->name);
 }
 free(sym_array_modules);
 sym_array_modules = (struct Module *) 0;
 num_modules = 0;
 return;
}

canonical_original_code:
static void FreeModules()
{
 auto int nmods; auto int nsyms;
 auto struct Module *mp;
 have_modules = 0;
 if ( num_modules == 0 )
  return;
 if ( sym_array_modules == ((void *)0) )
  return;
 for (nmods = 0; nmods < num_modules; ++nmods)
 {
  mp = &sym_array_modules[nmods];
  if ( mp->num_syms == 0 )
   continue;
  for (nsyms= 0; nsyms < mp->num_syms; ++nsyms)
   free(mp->sym_array[nsyms].name);
  free(mp->sym_array);
  if ( mp->name != ((void *)0) )
   free(mp->name);
 }
 free(sym_array_modules);
 sym_array_modules = (struct Module *) 0;
 num_modules = 0;
 return;
}

variable_types: nmods int

variable_types: nsyms int

variable_types: struct Module { struct sym_table { unsigned long value; char *name; } *sym_array; int num_syms; char *name; } *

user_defined_types: struct Module { struct sym_table *sym_array; int num_syms; char *name; }


isVoid

repo: Djeef/Indexation

decompiled_code:
_BOOL8 __fastcall isVoid(_QWORD *a1)
{
return *a1 == 0LL;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(_QWORD *a1)
{
return *a1 == 0LL;
}

original_code:
int isVoid(HistogramSound *tab) {
 return(tab->begin == ((void *)0));
}

canonical_original_code:
int isVoid(struct HistogramSound *tab) {
 return(tab->begin == ((void *)0));
}

variable_types: struct HistogramSound { struct HistogramSoundCell { int key; int value; struct HistogramSoundCell *nextCell; } *begin; struct HistogramSoundCell *end; } *

return_type: int

user_defined_types: struct HistogramSound { struct HistogramSoundCell *begin; struct HistogramSoundCell *end; }


getHistogramSoundCell

decompiled_code:
__int64 __fastcall getHistogramSoundCell(__int64 *a1, int a2)
{
_BOOL4 v2; // edx
__int64 result; // rax

v2 = isVoid(a1);
result = 0LL;
if ( !v2 )
{
for ( result = *a1; result; result = *(_QWORD *)(result + 8) )
{
if ( *(_DWORD *)result == a2 )
break;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 *a1, int a2)
{
_BOOL4 v2; // edx
__int64 result; // rax

v2 = func0(a1);
result = 0LL;
if ( !v2 )
{
for ( result = *a1; result; result = *(_QWORD *)(result + 8) )
{
if ( *(_DWORD *)result == a2 )
break;
}
}
return result;
}

original_code:
HistogramSoundCell* getHistogramSoundCell(HistogramSound *tab, int key) {
 HistogramSoundCell *currentCell = ((void *)0);
 if(!isVoid(tab)) {
  currentCell = tab->begin;
  while(currentCell != ((void *)0)) {
   if(currentCell->key == key) {
    break;
   }
   currentCell = currentCell->nextCell;
  }
 }
 return(currentCell);
}

canonical_original_code:
struct HistogramSoundCell *getHistogramSoundCell(struct HistogramSound *tab, int key) {
 struct HistogramSoundCell *currentCell = ((void *)0);
 if(!isVoid(tab)) {
  currentCell = tab->begin;
  while(currentCell != ((void *)0)) {
   if(currentCell->key == key) {
    break;
   }
   currentCell = currentCell->nextCell;
  }
 }
 return(currentCell);
}

variable_types: struct HistogramSound { struct HistogramSoundCell *begin; struct HistogramSoundCell *end; } *

variable_types: key int

variable_types: struct HistogramSoundCell { int key; int value; struct HistogramSoundCell *nextCell; } *


initTab

decompiled_code:
__int64 __fastcall initTab(__int64 *a1, int a2)
{
int i; // ebx
__int64 result; // rax

*a1 = 0LL;
a1[1] = 0LL;
if ( a2 > 0 )
{
for ( i = 0; i != a2; ++i )
result = changeValueAt(a1, i, 0);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 *a1, int a2)
{
int i; // ebx
__int64 result; // rax

*a1 = 0LL;
a1[1] = 0LL;
if ( a2 > 0 )
{
for ( i = 0; i != a2; ++i )
result = func2(a1, i, 0);
}
return result;
}

original_code:
void initTab(HistogramSound *tab, int nbValues) {
 int i;
 tab->begin = ((void *)0);
 tab->end = ((void *)0);
 for(i = 0; i < nbValues; i++) {
  changeValueAt(tab, i, 0);
 }
}

canonical_original_code:
void initTab(struct HistogramSound *tab, int nbValues) {
 int i;
 tab->begin = ((void *)0);
 tab->end = ((void *)0);
 for(i = 0; i < nbValues; i++) {
  changeValueAt(tab, i, 0);
 }
}

variable_types: nbValues int

variable_types: i int

user_defined_types: struct HistogramSoundCell { int key; int value; struct HistogramSoundCell *nextCell; }


addValue

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

result = getHistogramSoundCell(a1, a2);
if ( !result )
return changeValueAt(a1, a2, 1);
++*(_DWORD *)(result + 4);
return result;
}

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

result = func1(a1, a2);
if ( !result )
return func2(a1, a2, 1);
++*(_DWORD *)(result + 4);
return result;
}

original_code:
void addValue(HistogramSound *tab, int key) {
 HistogramSoundCell *currentCell = getHistogramSoundCell(tab, key);
 if(currentCell == ((void *)0)) {
  changeValueAt(tab, key, 1);
 } else {
  currentCell->value++;
 }
}

canonical_original_code:
void addValue(struct HistogramSound *tab, int key) {
 struct HistogramSoundCell *currentCell = getHistogramSoundCell(tab, key);
 if(currentCell == ((void *)0)) {
  changeValueAt(tab, key, 1);
 } else {
  currentCell->value++;
 }
}


decompiled_code:
__int64 __fastcall init_descriptor(__int64 a1)
{
_BYTE *v1; // rax
void *v2; // rax
__int64 result; // rax

v1 = calloc(0x1EuLL, 1uLL);
*(_QWORD *)a1 = v1;
*v1 = 0;
v2 = malloc(0x10uLL);
*(_QWORD *)(a1 + 8) = v2;
result = init_histogram(v2);
*(_DWORD *)(a1 + 16) = 0;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
_BYTE *v1; // rax
void *v2; // rax
__int64 result; // rax

v1 = calloc(0x1EuLL, 1uLL);
*(_QWORD *)a1 = v1;
*v1 = 0;
v2 = malloc(0x10uLL);
*(_QWORD *)(a1 + 8) = v2;
result = init_histogram(v2);
*(_DWORD *)(a1 + 16) = 0;
return result;
}

original_code:
void init_descriptor(Descriptor *d) {
 d->id = (char*)calloc(30, sizeof(char));
 strcpy(d->id, "\0");
 d->ptr_histo = (Histogram*)malloc(sizeof(Histogram));
 init_histogram(d->ptr_histo);
 d->nb_words = 0;
}

canonical_original_code:
void init_descriptor(struct Descriptor *d) {
 d->id = (char *)calloc(30, sizeof(char));
 strcpy(d->id, "\0");
 d->ptr_histo = (struct Histogram *)malloc(sizeof(Histogram));
 init_histogram(d->ptr_histo);
 d->nb_words = 0;
}

variable_types: struct Descriptor { char *id; struct Histogram { struct Cel { char key[1000]; int value; struct Cel *ptr_next_cel; } *begin; struct Cel *end; } *ptr_histo; int nb_words; } *

user_defined_types: struct Descriptor { char *id; struct Histogram *ptr_histo; int nb_words; }

user_defined_types: struct Histogram { struct Cel *begin; struct Cel *end; }


decompiled_code:
void __fastcall free_descriptor(__int64 a1)
{
free_histogram(*(_QWORD *)(a1 + 8));
free(*(void **)a1);
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1)
{
free_histogram(*(_QWORD *)(a1 + 8));
free(*(void **)a1);
}

original_code:
void free_descriptor(Descriptor *d) {
 free_histogram(d->ptr_histo);
 free(d->id);
}

canonical_original_code:
void free_descriptor(struct Descriptor *d) {
 free_histogram(d->ptr_histo);
 free(d->id);
}

user_defined_types: struct Cel { char key[1000]; int value; struct Cel *ptr_next_cel; }


original_code:
void add_value(char *key, Descriptor *d) {
 Cel *current_cel;
 Cel *newc;
 if(is_void(d->ptr_histo)) {
  newc = (Cel*)malloc(sizeof(Cel));
  strcpy(newc->key, key);
  newc->value = 1;
  newc->ptr_next_cel = ((void *)0);
  d->ptr_histo->begin = newc;
  d->ptr_histo->end = newc;
 } else {
  current_cel = d->ptr_histo->begin;
  while (current_cel != ((void *)0)) {
   if (strcmp(current_cel->key, key) == 0) {
    current_cel->value++;
    break;
   } else {
    current_cel = current_cel->ptr_next_cel;
   }
  }
  if (current_cel == ((void *)0)) {
   newc = (Cel*)malloc(sizeof(Cel));
   strcpy(newc->key, key);
   newc->value = 1;
   newc->ptr_next_cel = ((void *)0);
   d->ptr_histo->end->ptr_next_cel = newc;
   d->ptr_histo->end = d->ptr_histo->end->ptr_next_cel;
  }
 }
}

canonical_original_code:
void add_value(char *key, struct Descriptor *d) {
 struct Cel *current_cel;
 struct Cel *newc;
 if(is_void(d->ptr_histo)) {
  newc = (struct Cel *)malloc(sizeof(Cel));
  strcpy(newc->key, key);
  newc->value = 1;
  newc->ptr_next_cel = ((void *)0);
  d->ptr_histo->begin = newc;
  d->ptr_histo->end = newc;
 } else {
  current_cel = d->ptr_histo->begin;
  while (current_cel != ((void *)0)) {
   if (strcmp(current_cel->key, key) == 0) {
    current_cel->value++;
    break;
   } else {
    current_cel = current_cel->ptr_next_cel;
   }
  }
  if (current_cel == ((void *)0)) {
   newc = (struct Cel *)malloc(sizeof(Cel));
   strcpy(newc->key, key);
   newc->value = 1;
   newc->ptr_next_cel = ((void *)0);
   d->ptr_histo->end->ptr_next_cel = newc;
   d->ptr_histo->end = d->ptr_histo->end->ptr_next_cel;
  }
 }
}

variable_types:
key
char *

variable_types: struct Cel { char key[1000]; int value; struct Cel *ptr_next_cel; } *


initWavHeader

original_code:
void initWavHeader(WavHeader *wavHeader) {
 memset(wavHeader, 0, sizeof(WavHeader));
}

canonical_original_code:
void initWavHeader(struct WavHeader *wavHeader) {
 memset(wavHeader, 0, sizeof(WavHeader));
}

variable_types: struct WavHeader { char chunkId[4]; int chunkSize; char format[4]; char subChunk1Id[4]; int subChunk1Size; short audioFormat; short numChannels; int sampleRate; int byteRate; short blockAlign; short bitsPerSample; char subChunk2Id[4]; int subChunk2Size; } *


calculateScore

original_code:
int calculateScore(Histogram* hist) {
 int i;
 for(i = 0; i < hist->nbValue; i++) {
  hist->score += (i+1)*hist->matrixHisto[1][i];
 }
}

canonical_original_code:
int calculateScore(struct Histogram *hist) {
 int i;
 for(i = 0; i < hist->nbValue; i++) {
  hist->score += (i+1)*hist->matrixHisto[1][i];
 }
}

variable_types: struct Histogram { int nbValue; int score; int **matrixHisto; } *


decompiled_code:
_BOOL8 __fastcall is_void(_QWORD *a1)
{
return *a1 == 0LL;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(_QWORD *a1)
{
return *a1 == 0LL;
}

original_code:
int is_void(Histogram *h) {
 return(h->begin == ((void *)0));
}

canonical_original_code:
int is_void(struct Histogram *h) {
 return(h->begin == ((void *)0));
}

variable_types: struct Histogram { struct Cel { char key[1000]; int value; struct Cel *ptr_next_cel; } *begin; struct Cel *end; } *


decompiled_code:
__int64 __fastcall get_nb_words(__int64 *a1)
{
__int64 v1; // rax
unsigned int v2; // edx

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

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

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

original_code:
int get_nb_words(Histogram *h) {
 Cel *current_cel = h->begin;
 int nb_words = 0;
 while (current_cel != ((void *)0)) {
  nb_words++;
  current_cel = current_cel->ptr_next_cel;
 }
 return nb_words;
}

canonical_original_code:
int get_nb_words(struct Histogram *h) {
 struct Cel *current_cel = h->begin;
 int nb_words = 0;
 while (current_cel != ((void *)0)) {
  nb_words++;
  current_cel = current_cel->ptr_next_cel;
 }
 return nb_words;
}


decompiled_code:
int __fastcall add_key_value(char **a1, const char *a2, int a3)
{
char *v4; // rbx
char *v5; // rax
char *v6; // rbx
char *v7; // rbx

if ( is_void(a1) )
{
v7 = (char *)malloc(0x3F8uLL);
LODWORD(v5) = (unsigned int)strcpy(v7, a2);
*((_DWORD *)v7 + 250) = a3;
*((_QWORD *)v7 + 126) = 0LL;
*a1 = v7;
a1[1] = v7;
}
else
{
v4 = *a1;
if ( *a1 )
{
while ( 1 )
{
LODWORD(v5) = strcmp(v4, a2);
if ( !(_DWORD)v5 )
break;
v4 = (char *)*((_QWORD *)v4 + 126);
if ( !v4 )
goto LABEL_5;
}
*((_DWORD *)v4 + 250) = a3;
}
else
{
LABEL_5:
v6 = (char *)malloc(0x3F8uLL);
strcpy(v6, a2);
*((_DWORD *)v6 + 250) = a3;
*((_QWORD *)v6 + 126) = 0LL;
*((_QWORD *)a1[1] + 126) = v6;
v5 = (char *)*((_QWORD *)a1[1] + 126);
a1[1] = v5;
}
}
return (int)v5;
}

canonical_decompiled_code:
int __fastcall func4(char **a1, const char *a2, int a3)
{
char *v4; // rbx
char *v5; // rax
char *v6; // rbx
char *v7; // rbx

if ( func1(a1) )
{
v7 = (char *)malloc(0x3F8uLL);
LODWORD(v5) = (unsigned int)strcpy(v7, a2);
*((_DWORD *)v7 + 250) = a3;
*((_QWORD *)v7 + 126) = 0LL;
*a1 = v7;
a1[1] = v7;
}
else
{
v4 = *a1;
if ( *a1 )
{
while ( 1 )
{
LODWORD(v5) = strcmp(v4, a2);
if ( !(_DWORD)v5 )
break;
v4 = (char *)*((_QWORD *)v4 + 126);
if ( !v4 )
goto LABEL_5;
}
*((_DWORD *)v4 + 250) = a3;
}
else
{
LABEL_5:
v6 = (char *)malloc(0x3F8uLL);
strcpy(v6, a2);
*((_DWORD *)v6 + 250) = a3;
*((_QWORD *)v6 + 126) = 0LL;
*((_QWORD *)a1[1] + 126) = v6;
v5 = (char *)*((_QWORD *)a1[1] + 126);
a1[1] = v5;
}
}
return (int)v5;
}

original_code:
void add_key_value(Histogram *h, char *key, int value) {
 Cel *current_cel;
 Cel *newc;
 if(is_void(h)) {
  newc = (Cel*)malloc(sizeof(Cel));
  strcpy(newc->key, key);
  newc->value = value;
  newc->ptr_next_cel = ((void *)0);
  h->begin = newc;
  h->end = newc;
 } else {
  current_cel = h->begin;
  while (current_cel != ((void *)0)) {
   if (strcmp(current_cel->key, key) == 0) {
    current_cel->value = value;
    break;
   } else {
    current_cel = current_cel->ptr_next_cel;
   }
  }
  if (current_cel == ((void *)0)) {
   newc = (Cel*)malloc(sizeof(Cel));
   strcpy(newc->key, key);
   newc->value = value;
   newc->ptr_next_cel = ((void *)0);
   h->end->ptr_next_cel = newc;
   h->end = h->end->ptr_next_cel;
  }
 }
}

canonical_original_code:
void add_key_value(struct Histogram *h, char *key, int value) {
 struct Cel *current_cel;
 struct Cel *newc;
 if(is_void(h)) {
  newc = (struct Cel *)malloc(sizeof(Cel));
  strcpy(newc->key, key);
  newc->value = value;
  newc->ptr_next_cel = ((void *)0);
  h->begin = newc;
  h->end = newc;
 } else {
  current_cel = h->begin;
  while (current_cel != ((void *)0)) {
   if (strcmp(current_cel->key, key) == 0) {
    current_cel->value = value;
    break;
   } else {
    current_cel = current_cel->ptr_next_cel;
   }
  }
  if (current_cel == ((void *)0)) {
   newc = (struct Cel *)malloc(sizeof(Cel));
   strcpy(newc->key, key);
   newc->value = value;
   newc->ptr_next_cel = ((void *)0);
   h->end->ptr_next_cel = newc;
   h->end = h->end->ptr_next_cel;
  }
 }
}

variable_types: value int


decompiled_code:
int __fastcall get_value(__int64 *a1, const char *a2, _DWORD *a3)
{
__int64 v4; // rbx
int result; // eax

if ( is_void(a1) )
return -1;
v4 = *a1;
if ( !*a1 )
return -1;
while ( 1 )
{
result = strcmp((const char *)v4, a2);
if ( !result )
break;
v4 = *(_QWORD *)(v4 + 1008);
if ( !v4 )
return -1;
}
*a3 = *(_DWORD *)(v4 + 1000);
return result;
}

canonical_decompiled_code:
int __fastcall func5(__int64 *a1, const char *a2, _DWORD *a3)
{
__int64 v4; // rbx
int result; // eax

if ( func1(a1) )
return -1;
v4 = *a1;
if ( !*a1 )
return -1;
while ( 1 )
{
result = strcmp((const char *)v4, a2);
if ( !result )
break;
v4 = *(_QWORD *)(v4 + 1008);
if ( !v4 )
return -1;
}
*a3 = *(_DWORD *)(v4 + 1000);
return result;
}

original_code:
int get_value(Histogram *h, char *key, int *value) {
 Cel *current_cel;
 if(is_void(h)) {
  return(-1);
 } else {
  current_cel = h->begin;
  while(current_cel != ((void *)0)) {
   if(strcmp(current_cel->key, key) == 0) {
    *value = current_cel->value;
    return(0);
    break;
   } else {
    current_cel = current_cel->ptr_next_cel;
   }
  }
 }
 return(-1);
}

canonical_original_code:
int get_value(struct Histogram *h, char *key, int *value) {
 struct Cel *current_cel;
 if(is_void(h)) {
  return(-1);
 } else {
  current_cel = h->begin;
  while(current_cel != ((void *)0)) {
   if(strcmp(current_cel->key, key) == 0) {
    *value = current_cel->value;
    return(0);
    break;
   } else {
    current_cel = current_cel->ptr_next_cel;
   }
  }
 }
 return(-1);
}

variable_types:
value
int *


initWavData

decompiled_code:
void __fastcall initWavData(__int64 a1)
{
*(_QWORD *)(a1 + 8) = 0LL;
*(_QWORD *)(a1 + 16) = 0LL;
*(_DWORD *)a1 = 0;
}

original_code:
void initWavData(WavData *wavData) {
        wavData->begin = ((void *)0);
        wavData->end = ((void *)0);
        wavData->size = 0;
}

canonical_original_code:
void initWavData(struct WavData *wavData) {
        wavData->begin = ((void *)0);
        wavData->end = ((void *)0);
        wavData->size = 0;
}

variable_types: struct WavData { int size; struct CelWavData { short sample; struct CelWavData *nextCell; } *begin; struct CelWavData *end; } *

user_defined_types: struct WavData { int size; struct CelWavData *begin; struct CelWavData *end; }


quantifyRGB

decompiled_code:
__int64 __fastcall quantifyRGB(__int64 a1, int a2, int a3, int a4, const char *a5)
{
int v5; // er14
int v6; // er12
int v7; // er13
int v9; // ebp
__int64 result; // rax
__int64 v11; // r15
int v12; // ebp
int v13; // ecx
int v14; // edx
int v15; // ecx
__int64 v16; // r14
int v17; // ebp
int v18; // er15
int v19; // edx
int v20; // ecx
__int64 v21; // rbp
int v22; // er12
int v23; // ecx

v5 = a2;
v6 = a3;
v7 = a4;
if ( a4 <= 255 && a3 <= 255 && a2 <= 255 )
{
v9 = *(_DWORD *)(a1 + 8);
*(_QWORD *)a1 = malloc(12LL * v9);
result = (unsigned int)(8 - v9);
if ( (int)result <= 7 )
{
v11 = 0LL;
v12 = 7;
do
{
v15 = (int)pow(2.0, (double)v12);
if ( v5 % v15 >= v5 )
{
*(_DWORD *)(*(_QWORD *)a1 + v11) = 0;
}
else
{
*(_DWORD *)(*(_QWORD *)a1 + v11) = 1;
v5 -= v15;
}
v13 = v12;
v14 = 8 - v12--;
result = (unsigned int)(8 - *(_DWORD *)(a1 + 8));
v11 += 4LL;
}
while ( (int)result <= v12 );
if ( (int)result <= 7 )
{
v16 = 4LL * v14;
v17 = 7;
v18 = 16 - v13;
do
{
v20 = (int)pow(2.0, (double)v17);
if ( v6 % v20 >= v6 )
{
*(_DWORD *)(*(_QWORD *)a1 + v16) = 0;
}
else
{
*(_DWORD *)(*(_QWORD *)a1 + v16) = 1;
v6 -= v20;
}
v19 = v18 - v17--;
result = (unsigned int)(8 - *(_DWORD *)(a1 + 8));
v16 += 4LL;
}
while ( (int)result <= v17 );
if ( (int)result <= 7 )
{
v21 = 4LL * v19;
v22 = 7;
do
{
v23 = (int)pow(2.0, (double)v22);
if ( v7 % v23 >= v7 )
{
*(_DWORD *)(*(_QWORD *)a1 + v21) = 0;
}
else
{
*(_DWORD *)(*(_QWORD *)a1 + v21) = 1;
v7 -= v23;
}
--v22;
v21 += 4LL;
result = (unsigned int)(8 - *(_DWORD *)(a1 + 8));
}
while ( (int)result <= v22 );
}
}
}
}
else
{
fwrite("\x1B[31m", 1uLL, 5uLL, stderr);
fprintf(
stderr,
"> ERROR: RGB level higher than 255: value = R: %d / G: %d / B: %d (in file: %s)\n",
(unsigned int)a2,
(unsigned int)v6,
(unsigned int)v7,
a5);
fwrite("\x1B[00m", 1uLL, 5uLL, stderr);
fflush(stdout);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2, int a3, int a4, const char *a5)
{
int v5; // er14
int v6; // er12
int v7; // er13
int v9; // ebp
__int64 result; // rax
__int64 v11; // r15
int v12; // ebp
int v13; // ecx
int v14; // edx
int v15; // ecx
__int64 v16; // r14
int v17; // ebp
int v18; // er15
int v19; // edx
int v20; // ecx
__int64 v21; // rbp
int v22; // er12
int v23; // ecx

v5 = a2;
v6 = a3;
v7 = a4;
if ( a4 <= 255 && a3 <= 255 && a2 <= 255 )
{
v9 = *(_DWORD *)(a1 + 8);
*(_QWORD *)a1 = malloc(12LL * v9);
result = (unsigned int)(8 - v9);
if ( (int)result <= 7 )
{
v11 = 0LL;
v12 = 7;
do
{
v15 = (int)pow(2.0, (double)v12);
if ( v5 % v15 >= v5 )
{
*(_DWORD *)(*(_QWORD *)a1 + v11) = 0;
}
else
{
*(_DWORD *)(*(_QWORD *)a1 + v11) = 1;
v5 -= v15;
}
v13 = v12;
v14 = 8 - v12--;
result = (unsigned int)(8 - *(_DWORD *)(a1 + 8));
v11 += 4LL;
}
while ( (int)result <= v12 );
if ( (int)result <= 7 )
{
v16 = 4LL * v14;
v17 = 7;
v18 = 16 - v13;
do
{
v20 = (int)pow(2.0, (double)v17);
if ( v6 % v20 >= v6 )
{
*(_DWORD *)(*(_QWORD *)a1 + v16) = 0;
}
else
{
*(_DWORD *)(*(_QWORD *)a1 + v16) = 1;
v6 -= v20;
}
v19 = v18 - v17--;
result = (unsigned int)(8 - *(_DWORD *)(a1 + 8));
v16 += 4LL;
}
while ( (int)result <= v17 );
if ( (int)result <= 7 )
{
v21 = 4LL * v19;
v22 = 7;
do
{
v23 = (int)pow(2.0, (double)v22);
if ( v7 % v23 >= v7 )
{
*(_DWORD *)(*(_QWORD *)a1 + v21) = 0;
}
else
{
*(_DWORD *)(*(_QWORD *)a1 + v21) = 1;
v7 -= v23;
}
--v22;
v21 += 4LL;
result = (unsigned int)(8 - *(_DWORD *)(a1 + 8));
}
while ( (int)result <= v22 );
}
}
}
}
else
{
fwrite("\x1B[31m", 1uLL, 5uLL, stderr);
fprintf(
stderr,
"> ERROR: RGB level higher than 255: value = R: %d / G: %d / B: %d (in file: %s)\n",
(unsigned int)a2,
(unsigned int)v6,
(unsigned int)v7,
a5);
fwrite("\x1B[00m", 1uLL, 5uLL, stderr);
fflush(stdout);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int quantifyRGB(Quantification* quant, int redValue, int greenValue, int blueValue, char* filename) {
 int powerOfTwo, exponent, positionQuantWord;
 if(redValue <= 255 && greenValue <= 255 && blueValue <= 255) {
  quant->quantifyingNumber = (int*)malloc(((quant->nbBit)*3)*sizeof(int));
  positionQuantWord = 0;
  for(exponent = 7; exponent >= (8-quant->nbBit); exponent--) {
   powerOfTwo = pow(2, exponent);
   if((redValue%powerOfTwo) <= (redValue-1)) {
    quant->quantifyingNumber[positionQuantWord] = 1;
    redValue -= powerOfTwo;
   } else {
    quant->quantifyingNumber[positionQuantWord] = 0;
   }
   positionQuantWord++;
  }
  for(exponent = 7; exponent >= (8-quant->nbBit); exponent--) {
   powerOfTwo = pow(2, exponent);
   if((greenValue%powerOfTwo) <= (greenValue-1)) {
    quant->quantifyingNumber[positionQuantWord] = 1;
    greenValue -= powerOfTwo;
   } else {
    quant->quantifyingNumber[positionQuantWord] = 0;
   }
   positionQuantWord++;
  }
  for(exponent = 7; exponent >= (8-quant->nbBit); exponent--) {
   powerOfTwo = pow(2, exponent);
   if((blueValue%powerOfTwo) <= (blueValue-1)) {
    quant->quantifyingNumber[positionQuantWord] = 1;
    blueValue -= powerOfTwo;
   } else {
    quant->quantifyingNumber[positionQuantWord] = 0;
   }
   positionQuantWord++;
  }
 } else {
  fprintf(stderr, "\033[31m");
  fprintf(stderr, "> ERROR: RGB level higher than 255: value = R: %d / G: %d / B: %d (in file: %s)\n", redValue, greenValue, blueValue, filename);
  fprintf(stderr, "\033[00m");
  fflush(stdout);
  return -1;
 }
}

canonical_original_code:
int quantifyRGB(struct Quantification *quant, int redValue, int greenValue, int blueValue, char *filename) {
 int powerOfTwo; int exponent; int positionQuantWord;
 if(redValue <= 255 && greenValue <= 255 && blueValue <= 255) {
  quant->quantifyingNumber = (int *)malloc(((quant->nbBit)*3)*sizeof(int));
  positionQuantWord = 0;
  for(exponent = 7; exponent >= (8-quant->nbBit); exponent--) {
   powerOfTwo = pow(2, exponent);
   if((redValue%powerOfTwo) <= (redValue-1)) {
    quant->quantifyingNumber[positionQuantWord] = 1;
    redValue -= powerOfTwo;
   } else {
    quant->quantifyingNumber[positionQuantWord] = 0;
   }
   positionQuantWord++;
  }
  for(exponent = 7; exponent >= (8-quant->nbBit); exponent--) {
   powerOfTwo = pow(2, exponent);
   if((greenValue%powerOfTwo) <= (greenValue-1)) {
    quant->quantifyingNumber[positionQuantWord] = 1;
    greenValue -= powerOfTwo;
   } else {
    quant->quantifyingNumber[positionQuantWord] = 0;
   }
   positionQuantWord++;
  }
  for(exponent = 7; exponent >= (8-quant->nbBit); exponent--) {
   powerOfTwo = pow(2, exponent);
   if((blueValue%powerOfTwo) <= (blueValue-1)) {
    quant->quantifyingNumber[positionQuantWord] = 1;
    blueValue -= powerOfTwo;
   } else {
    quant->quantifyingNumber[positionQuantWord] = 0;
   }
   positionQuantWord++;
  }
 } else {
  fprintf(stderr, "\033[31m");
  fprintf(stderr, "> ERROR: RGB level higher than 255: value = R: %d / G: %d / B: %d (in file: %s)\n", redValue, greenValue, blueValue, filename);
  fprintf(stderr, "\033[00m");
  fflush(stdout);
  return -1;
 }
}

variable_types: struct Quantification { int *quantifyingNumber; int nbBit; } *

variable_types: redValue int

variable_types: greenValue int

variable_types: blueValue int

variable_types:
filename
char *

variable_types: powerOfTwo int

variable_types: exponent int

variable_types: positionQuantWord int


quantifyBW

decompiled_code:
__int64 __fastcall quantifyBW(__int64 a1, int a2, const char *a3)
{
int v3; // ebx
int v4; // ebp
__int64 result; // rax
__int64 v6; // r12
int v7; // ebp
int v8; // ecx

v3 = a2;
if ( a2 > 255 )
{
fwrite("\x1B[31m", 1uLL, 5uLL, stderr);
fprintf(stderr, "> ERROR: Grey level higher than 255: value = %d (in file: %s)\n", (unsigned int)a2, a3);
fwrite("\x1B[00m", 1uLL, 5uLL, stderr);
fflush(stdout);
result = 0xFFFFFFFFLL;
}
else
{
v4 = *(_DWORD *)(a1 + 8);
*(_QWORD *)a1 = malloc(4LL * v4);
result = (unsigned int)(8 - v4);
if ( (int)result <= 7 )
{
v6 = 0LL;
v7 = 7;
do
{
v8 = (int)pow(2.0, (double)v7);
if ( v3 % v8 >= v3 )
{
*(_DWORD *)(*(_QWORD *)a1 + v6) = 0;
}
else
{
*(_DWORD *)(*(_QWORD *)a1 + v6) = 1;
v3 -= v8;
}
--v7;
v6 += 4LL;
result = (unsigned int)(8 - *(_DWORD *)(a1 + 8));
}
while ( (int)result <= v7 );
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int a2, const char *a3)
{
int v3; // ebx
int v4; // ebp
__int64 result; // rax
__int64 v6; // r12
int v7; // ebp
int v8; // ecx

v3 = a2;
if ( a2 > 255 )
{
fwrite("\x1B[31m", 1uLL, 5uLL, stderr);
fprintf(stderr, "> ERROR: Grey level higher than 255: value = %d (in file: %s)\n", (unsigned int)a2, a3);
fwrite("\x1B[00m", 1uLL, 5uLL, stderr);
fflush(stdout);
result = 0xFFFFFFFFLL;
}
else
{
v4 = *(_DWORD *)(a1 + 8);
*(_QWORD *)a1 = malloc(4LL * v4);
result = (unsigned int)(8 - v4);
if ( (int)result <= 7 )
{
v6 = 0LL;
v7 = 7;
do
{
v8 = (int)pow(2.0, (double)v7);
if ( v3 % v8 >= v3 )
{
*(_DWORD *)(*(_QWORD *)a1 + v6) = 0;
}
else
{
*(_DWORD *)(*(_QWORD *)a1 + v6) = 1;
v3 -= v8;
}
--v7;
v6 += 4LL;
result = (unsigned int)(8 - *(_DWORD *)(a1 + 8));
}
while ( (int)result <= v7 );
}
}
return result;
}

original_code:
int quantifyBW(Quantification* quant, int greyValue, char* filename) {
 int powerOfTwo, exponent, positionQuantWord;
 if(greyValue <= 255) {
  quant->quantifyingNumber = (int*)malloc((quant->nbBit)*sizeof(int));
  positionQuantWord = 0;
  for(exponent = 7; exponent >= (8-quant->nbBit); exponent--) {
   powerOfTwo = pow(2, exponent);
   if((greyValue%powerOfTwo) <= (greyValue-1)) {
    quant->quantifyingNumber[positionQuantWord] = 1;
    greyValue -= powerOfTwo;
   } else {
    quant->quantifyingNumber[positionQuantWord] = 0;
   }
   positionQuantWord++;
  }
 } else {
  fprintf(stderr, "\033[31m");
  fprintf(stderr, "> ERROR: Grey level higher than 255: value = %d (in file: %s)\n", greyValue, filename);
  fprintf(stderr, "\033[00m");
  fflush(stdout);
  return -1;
 }
}

canonical_original_code:
int quantifyBW(struct Quantification *quant, int greyValue, char *filename) {
 int powerOfTwo; int exponent; int positionQuantWord;
 if(greyValue <= 255) {
  quant->quantifyingNumber = (int *)malloc((quant->nbBit)*sizeof(int));
  positionQuantWord = 0;
  for(exponent = 7; exponent >= (8-quant->nbBit); exponent--) {
   powerOfTwo = pow(2, exponent);
   if((greyValue%powerOfTwo) <= (greyValue-1)) {
    quant->quantifyingNumber[positionQuantWord] = 1;
    greyValue -= powerOfTwo;
   } else {
    quant->quantifyingNumber[positionQuantWord] = 0;
   }
   positionQuantWord++;
  }
 } else {
  fprintf(stderr, "\033[31m");
  fprintf(stderr, "> ERROR: Grey level higher than 255: value = %d (in file: %s)\n", greyValue, filename);
  fprintf(stderr, "\033[00m");
  fflush(stdout);
  return -1;
 }
}

variable_types: greyValue int

user_defined_types: struct Quantification { int *quantifyingNumber; int nbBit; }


calculateMatrixRGBQuantification

original_code:
int calculateMatrixRGBQuantification(PictureRGB* pictRGB, Quantification* quant, Histogram* hist, char* filename) {
 int i, j, k, exponent, quantifyingLevel;
 int redValue, blueValue, greenValue;
 for(i = 0; i < pictRGB->sizeY; i++) {
  for(j = 0; j < pictRGB->sizeX; j++) {
   redValue = pictRGB->matrixRed[i][j];
   greenValue = pictRGB->matrixGreen[i][j];
   blueValue = pictRGB->matrixBlue[i][j];
   quantifyRGB(&(*quant), redValue, greenValue, blueValue, filename);
   exponent = ((quant->nbBit)*(pictRGB->component)) - 1;
   quantifyingLevel = 0;
   for(k = 0; k < ((quant->nbBit)*(pictRGB->component)); k++) {
    if(quant->quantifyingNumber[k] == 1) {
     quantifyingLevel += pow(2, exponent);
    }
    exponent--;
   }
   hist->matrixHisto[1][quantifyingLevel]++;
  }
 }
}

canonical_original_code:
int calculateMatrixRGBQuantification(struct PictureRGB *pictRGB, struct Quantification *quant, struct Histogram *hist, char *filename) {
 int i; int j; int k; int exponent; int quantifyingLevel;
 int redValue; int blueValue; int greenValue;
 for(i = 0; i < pictRGB->sizeY; i++) {
  for(j = 0; j < pictRGB->sizeX; j++) {
   redValue = pictRGB->matrixRed[i][j];
   greenValue = pictRGB->matrixGreen[i][j];
   blueValue = pictRGB->matrixBlue[i][j];
   quantifyRGB(&(*quant), redValue, greenValue, blueValue, filename);
   exponent = ((quant->nbBit)*(pictRGB->component)) - 1;
   quantifyingLevel = 0;
   for(k = 0; k < ((quant->nbBit)*(pictRGB->component)); k++) {
    if(quant->quantifyingNumber[k] == 1) {
     quantifyingLevel += pow(2, exponent);
    }
    exponent--;
   }
   hist->matrixHisto[1][quantifyingLevel]++;
  }
 }
}

variable_types: struct PictureRGB { char *filename; int sizeX; int sizeY; int component; int **matrixRed; int **matrixGreen; int **matrixBlue; } *

variable_types: i int

variable_types: j int

variable_types: k int

variable_types: quantifyingLevel int

user_defined_types: struct Histogram { int nbValue; int score; int **matrixHisto; }


calculateMatrixBWQuantification

original_code:
int calculateMatrixBWQuantification(PictureBW* pictBW, Quantification* quant, Histogram* hist, char* filename) {
 int i, j, k, exponent, quantifyingLevel;
 int greyValue;
 for(i = 0; i < pictBW->sizeY; i++) {
  for(j = 0; j < pictBW->sizeX; j++) {
   greyValue = pictBW->matrixGrey[i][j];
   quantifyBW(&(*quant), greyValue, filename);
   exponent = quant->nbBit - 1;
   quantifyingLevel = 0;
   for(k = 0; k < ((quant->nbBit)*(pictBW->component)); k++) {
    if(quant->quantifyingNumber[k] == 1) {
     quantifyingLevel += pow(2, exponent);
    }
    exponent--;
   }
   hist->matrixHisto[1][quantifyingLevel]++;
  }
 }
}

canonical_original_code:
int calculateMatrixBWQuantification(struct PictureBW *pictBW, struct Quantification *quant, struct Histogram *hist, char *filename) {
 int i; int j; int k; int exponent; int quantifyingLevel;
 int greyValue;
 for(i = 0; i < pictBW->sizeY; i++) {
  for(j = 0; j < pictBW->sizeX; j++) {
   greyValue = pictBW->matrixGrey[i][j];
   quantifyBW(&(*quant), greyValue, filename);
   exponent = quant->nbBit - 1;
   quantifyingLevel = 0;
   for(k = 0; k < ((quant->nbBit)*(pictBW->component)); k++) {
    if(quant->quantifyingNumber[k] == 1) {
     quantifyingLevel += pow(2, exponent);
    }
    exponent--;
   }
   hist->matrixHisto[1][quantifyingLevel]++;
  }
 }
}

variable_types: struct PictureBW { char *filename; int sizeX; int sizeY; int component; int **matrixGrey; } *

variable_types: struct Histogram { int nbValue; int score; int **matrixHisto; } *


decompiled_code:
__int64 __fastcall rl_max_size(__int64 a1)
{
if ( !a1 )
_assert_fail("rl != NULL", "readlines.c", 0xFu, "rl_max_size");
return *(_QWORD *)(a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
if ( !a1 )
_assert_fail("rl != NULL", "readlines.c", 0xFu, "rl_max_size");
return *(_QWORD *)(a1 + 8);
}

original_code:
size_t rl_max_size(struct RL* rl){
    ((void) sizeof ((rl != ((void *)0)) ? 1 : 0), __extension__ ({ if (rl != ((void *)0)) ; else __assert_fail ("rl != NULL", "readlines.c", 15, __extension__ __PRETTY_FUNCTION__); }));
    return rl->max_size;
}

canonical_original_code:
unsigned long rl_max_size(struct RL *rl){
    ((void) sizeof ((rl != ((void *)0)) ? 1 : 0), __extension__ ({ if (rl != ((void *)0)) ; else __assert_fail ("rl != NULL", "readlines.c", 15, __extension__ __PRETTY_FUNCTION__); }));
    return rl->max_size;
}

variable_types: struct RL { int fd; unsigned long max_size; } *

return_type: unsigned long


decompiled_code:
__int64 __fastcall rl_close(void *ptr)
{
unsigned int v1; // ebp

if ( !ptr )
_assert_fail("rl != NULL", "readlines.c", 0x14u, "rl_close");
v1 = close(*(_DWORD *)ptr);
free(ptr);
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func1(void *ptr)
{
unsigned int v1; // ebp

if ( !ptr )
_assert_fail("rl != NULL", "readlines.c", 0x14u, "rl_close");
v1 = close(*(_DWORD *)ptr);
free(ptr);
return v1;
}

original_code:
int rl_close(struct RL* rl){
    ((void) sizeof ((rl != ((void *)0)) ? 1 : 0), __extension__ ({ if (rl != ((void *)0)) ; else __assert_fail ("rl != NULL", "readlines.c", 20, __extension__ __PRETTY_FUNCTION__); }));
    int ret = close(rl->fd);
    free(rl);
    return ret;
}

canonical_original_code:
int rl_close(struct RL *rl){
    ((void) sizeof ((rl != ((void *)0)) ? 1 : 0), __extension__ ({ if (rl != ((void *)0)) ; else __assert_fail ("rl != NULL", "readlines.c", 20, __extension__ __PRETTY_FUNCTION__); }));
    int ret = close(rl->fd);
    free(rl);
    return ret;
}

variable_types: ret int

user_defined_types: struct RL { int fd; unsigned long max_size; }


decompiled_code:
__int64 __fastcall rl_readline(__int64 a1, char *a2, size_t a3)
{
unsigned __int64 v4; // rbx
unsigned int v5; // edx
char buf[41]; // [rsp+Fh] [rbp-29h] BYREF

if ( !a1 )
_assert_fail("rl != NULL", "readlines.c", 0x1Bu, "rl_readline");
memset(a2, 0, a3);
v4 = read(*(_DWORD *)a1, a2, 1uLL);
v5 = 0;
if ( v4 )
{
while ( 1 )
{
if ( a2[v4 - 1] == 10 )
return (unsigned int)v4;
if ( a3 + 1 <= v4 || *(_QWORD *)(a1 + 8) <= v4 )
break;
v4 += read(*(_DWORD *)a1, &a2[v4], 1uLL);
if ( !v4 )
return 0;
}
if ( *(_QWORD *)(a1 + 8) + 1LL == v4 )
{
v5 = -3;
}
else if ( v4 == a3 )
{
while ( 1 )
{
v5 = read(*(_DWORD *)a1, buf, 1uLL);
if ( !v5 )
break;
if ( buf[0] == 10 )
return (unsigned int)-2;
}
}
else
{
v5 = -1337;
}
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, char *a2, size_t a3)
{
unsigned __int64 v4; // rbx
unsigned int v5; // edx
char buf[41]; // [rsp+Fh] [rbp-29h] BYREF

if ( !a1 )
_assert_fail("rl != NULL", "readlines.c", 0x1Bu, "rl_readline");
memset(a2, 0, a3);
v4 = read(*(_DWORD *)a1, a2, 1uLL);
v5 = 0;
if ( v4 )
{
while ( 1 )
{
if ( a2[v4 - 1] == 10 )
return (unsigned int)v4;
if ( a3 + 1 <= v4 || *(_QWORD *)(a1 + 8) <= v4 )
break;
v4 += read(*(_DWORD *)a1, &a2[v4], 1uLL);
if ( !v4 )
return 0;
}
if ( *(_QWORD *)(a1 + 8) + 1LL == v4 )
{
v5 = -3;
}
else if ( v4 == a3 )
{
while ( 1 )
{
v5 = read(*(_DWORD *)a1, buf, 1uLL);
if ( !v5 )
break;
if ( buf[0] == 10 )
return (unsigned int)-2;
}
}
else
{
v5 = -1337;
}
}
return v5;
}

original_code:
int rl_readline(struct RL* rl, char* buf, size_t buf_size){
    ((void) sizeof ((rl != ((void *)0)) ? 1 : 0), __extension__ ({ if (rl != ((void *)0)) ; else __assert_fail ("rl != NULL", "readlines.c", 27, __extension__ __PRETTY_FUNCTION__); }));
    size_t pos = 0;
    memset(buf,0,buf_size);
    if( (pos = read(rl->fd, buf, sizeof(char))) == 0)
        return 0;
    while (buf[pos-1] != '\n' && pos < buf_size+1 && pos < rl->max_size)
    {
        pos += read(rl->fd, &buf[pos], sizeof(char));
        if(pos == 0)
            return 0;
    }
    if(buf[pos-1] == '\n')
        return pos;
    if(pos == rl->max_size+1)
        return -3;
    if(pos == buf_size){
        char tmp;
        do{
            int has_read = read(rl->fd, &tmp, sizeof(char));
            if(has_read == 0)
                return 0;
        } while(tmp != '\n');
        return -2;
    }
    return -1337;
}

canonical_original_code:
int rl_readline(struct RL *rl, char *buf, unsigned long buf_size){
    ((void) sizeof ((rl != ((void *)0)) ? 1 : 0), __extension__ ({ if (rl != ((void *)0)) ; else __assert_fail ("rl != NULL", "readlines.c", 27, __extension__ __PRETTY_FUNCTION__); }));
    unsigned long pos = 0;
    memset(buf,0,buf_size);
    if( (pos = read(rl->fd, buf, sizeof(char))) == 0)
        return 0;
    while (buf[pos-1] != '\n' && pos < buf_size+1 && pos < rl->max_size)
    {
        pos += read(rl->fd, &buf[pos], sizeof(char));
        if(pos == 0)
            return 0;
    }
    if(buf[pos-1] == '\n')
        return pos;
    if(pos == rl->max_size+1)
        return -3;
    if(pos == buf_size){
        char tmp;
        do{
            int has_read = read(rl->fd, &tmp, sizeof(char));
            if(has_read == 0)
                return 0;
        } while(tmp != '\n');
        return -2;
    }
    return -1337;
}

variable_types:
buf
char *

variable_types:
pos
unsigned long

variable_types: tmp char


decompiled_code:
_BOOL8 __fastcall match_source(__int64 a1, __int64 a2, unsigned __int16 a3)
{
_BOOL8 result; // rax

result = 0LL;
if ( *(_QWORD *)a1 == a2 )
result = *(_WORD *)(a1 + 8) == a3;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func8(__int64 a1, __int64 a2, unsigned __int16 a3)
{
_BOOL8 result; // rax

result = 0LL;
if ( *(_QWORD *)a1 == a2 )
result = *(_WORD *)(a1 + 8) == a3;
return result;
}

original_code:
int match_source(source_t* source, u_long ip, u_short port)
{
    if (source->ip == ip && source->port == port) {
        return 1;
    } else {
        return 0;
    }
}

canonical_original_code:
int match_source(struct source_s *source, unsigned long ip, unsigned short port)
{
    if (source->ip == ip && source->port == port) {
        return 1;
    } else {
        return 0;
    }
}

variable_types: struct source_s { unsigned long ip; unsigned short port; unsigned char mac[6]; unsigned long seq; struct packet_s { unsigned long seq; unsigned char *data; int len; struct packet_s *next; } *packets; } *

variable_types:
ip
unsigned long

variable_types:
port
unsigned short

user_defined_types: struct source_s { unsigned long ip; unsigned short port; unsigned char mac[6]; unsigned long seq; struct packet_s *packets; }


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

v2 = *a2;
if ( *a2 >= *(_QWORD *)(a1 + 16) )
{
v3 = *(unsigned __int64 **)(a1 + 24);
if ( v3 )
{
if ( v2 >= *v3 )
{
if ( v2 > *v3 )
{
while ( 1 )
{
v4 = v3;
v3 = (unsigned __int64 *)v3[3];
if ( !v3 )
break;
if ( v2 <= *v3 )
{
if ( v2 >= *v3 )
return;
break;
}
}
a2[3] = (unsigned __int64)v3;
v4[3] = (unsigned __int64)a2;
}
}
else
{
a2[3] = (unsigned __int64)v3;
*(_QWORD *)(a1 + 24) = a2;
}
}
else
{
*(_QWORD *)(a1 + 24) = a2;
}
}
}

original_code:
void insert_packet(source_t* source, packet_t* packet)
{
    if (packet->seq < source->seq) return;
    if (source->packets == ((void *)0)) {
        source->packets = packet;
    } else {
        if (packet->seq < source->packets->seq) {
            packet->next = source->packets;
            source->packets = packet;
        }
        else if (packet->seq > source->packets->seq) {
            packet_t* current = source->packets;
            while (current->next != ((void *)0) && current->next->seq < packet->seq) {
                current = current->next;
            }
            if (current->next == ((void *)0) || current->next->seq > packet->seq) {
                packet->next = current->next;
                current->next = packet;
            }
        }
    }
}

canonical_original_code:
void insert_packet(struct source_s *source, struct packet_s *packet)
{
    if (packet->seq < source->seq) return;
    if (source->packets == ((void *)0)) {
        source->packets = packet;
    } else {
        if (packet->seq < source->packets->seq) {
            packet->next = source->packets;
            source->packets = packet;
        }
        else if (packet->seq > source->packets->seq) {
            struct packet_s *current = source->packets;
            while (current->next != ((void *)0) && current->next->seq < packet->seq) {
                current = current->next;
            }
            if (current->next == ((void *)0) || current->next->seq > packet->seq) {
                packet->next = current->next;
                current->next = packet;
            }
        }
    }
}

variable_types:
packet
struct packet_s *

variable_types: struct packet_s { unsigned long seq; unsigned char *data; int len; struct packet_s *next; } *


decompiled_code:
void __fastcall free_packet(void *ptr)
{
free(*((void **)ptr + 1));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func10(void *ptr)
{
free(*((void **)ptr + 1));
free(ptr);
}

original_code:
void free_packet(packet_t* p)
{
    free(p->data);
    free(p);
}

canonical_original_code:
void free_packet(struct packet_s *p)
{
    free(p->data);
    free(p);
}

user_defined_types: struct packet_s { unsigned long seq; unsigned char *data; int len; struct packet_s *next; }


decompiled_code:
int __fastcall save_packet(__int64 a1)
{
fwrite((const void *)(*(_QWORD *)(a1 + 8) + 10LL), *(_DWORD *)(a1 + 16) - 10, 1uLL, file);
return fflush(file);
}

canonical_decompiled_code:
int __fastcall func11(__int64 a1)
{
fwrite((const void *)(*(_QWORD *)(a1 + 8) + 10LL), *(_DWORD *)(a1 + 16) - 10, 1uLL, file);
return fflush(file);
}

original_code:
void save_packet(packet_t* p)
{
    fwrite(p->data+10, p->len - 10, 1, file);
    fflush(file);
}

canonical_original_code:
void save_packet(struct packet_s *p)
{
    fwrite(p->data+10, p->len - 10, 1, file);
    fflush(file);
}


decompiled_code:
int __fastcall send_ack(__int64 a1)
{
_QWORD *i; // rdi
_QWORD *v3; // rdi
int result; // eax
const char *v5; // rax
const char *v6; // rax
const char *v7; // rax

for ( i = *(_QWORD **)(a1 + 24); i; i = *(_QWORD **)(a1 + 24) )
{
if ( *(_QWORD *)(a1 + 16) != *i )
break;
save_packet((__int64)i);
v3 = *(_QWORD **)(a1 + 24);
*(_QWORD *)(a1 + 24) = v3[3];
free_packet(v3);
++*(_QWORD *)(a1 + 16);
}
udp_ptag = libnet_build_udp();
if ( udp_ptag == -1 )
{
v5 = (const char *)libnet_geterror();
result = fprintf(_bss_start, "Can't build UDP header: %s\n", v5);
}
else
{
ipv4_ptag = libnet_build_ipv4();
if ( ipv4_ptag == -1 )
{
v6 = (const char *)libnet_geterror();
result = fprintf(_bss_start, "Can't build IP header: %s\n", v6);
}
else
{
eth_ptag = libnet_build_ethernet();
if ( eth_ptag == -1 )
{
v7 = (const char *)libnet_geterror();
result = fprintf(_bss_start, "Can't build ethernet header: %s\n", v7);
}
else
{
result = libnet_write();
}
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func12(__int64 a1)
{
_QWORD *i; // rdi
_QWORD *v3; // rdi
int result; // eax
const char *v5; // rax
const char *v6; // rax
const char *v7; // rax

for ( i = *(_QWORD **)(a1 + 24); i; i = *(_QWORD **)(a1 + 24) )
{
if ( *(_QWORD *)(a1 + 16) != *i )
break;
func11((__int64)i);
v3 = *(_QWORD **)(a1 + 24);
*(_QWORD *)(a1 + 24) = v3[3];
func10(v3);
++*(_QWORD *)(a1 + 16);
}
udp_ptag = libnet_build_udp();
if ( udp_ptag == -1 )
{
v5 = (const char *)libnet_geterror();
result = fprintf(_bss_start, "Can't build UDP header: %s\n", v5);
}
else
{
ipv4_ptag = libnet_build_ipv4();
if ( ipv4_ptag == -1 )
{
v6 = (const char *)libnet_geterror();
result = fprintf(_bss_start, "Can't build IP header: %s\n", v6);
}
else
{
eth_ptag = libnet_build_ethernet();
if ( eth_ptag == -1 )
{
v7 = (const char *)libnet_geterror();
result = fprintf(_bss_start, "Can't build ethernet header: %s\n", v7);
}
else
{
result = libnet_write();
}
}
}
return result;
}

original_code:
void send_ack(source_t* source)
{
    while (source->packets != ((void *)0) && source->seq == source->packets->seq) {
        save_packet(source->packets);
        packet_t* p = source->packets;
        source->packets = source->packets->next;
        free_packet(p);
        ++source->seq;
    }
    char payload[10];
    memset(payload, 0, sizeof(payload));
    memcpy(payload + 0, &source->seq, sizeof(u_long));
    udp_ptag = libnet_build_udp(
            local_port,
            source->port,
            0x08 + 10,
            0,
            payload,
            10,
            lnet,
            udp_ptag);
    if (udp_ptag == -1)
    {
        fprintf(stderr, "Can't build UDP header: %s\n", libnet_geterror(lnet));
        return;
    }
    ipv4_ptag = libnet_build_ipv4(
            0x14 + 0x08,
            0,
            242,
            0,
            64,
            IPPROTO_UDP,
            0,
            local_ip,
            source->ip,
            ((void *)0),
            0,
            lnet,
            ipv4_ptag);
    if (ipv4_ptag == -1)
    {
        fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(lnet));
        return;
    }
    eth_ptag = libnet_build_ethernet(
            source->mac,
            local_mac,
            0x0800,
            ((void *)0),
            0,
            lnet,
            eth_ptag);
    if (eth_ptag == -1)
    {
        fprintf(stderr, "Can't build ethernet header: %s\n", libnet_geterror(lnet));
        return;
    }
    libnet_write(lnet);
}

canonical_original_code:
void send_ack(struct source_s *source)
{
    while (source->packets != ((void *)0) && source->seq == source->packets->seq) {
        save_packet(source->packets);
        struct packet_s *p = source->packets;
        source->packets = source->packets->next;
        free_packet(p);
        ++source->seq;
    }
    char payload[10];
    memset(payload, 0, sizeof(payload));
    memcpy(payload + 0, &source->seq, sizeof(u_long));
    udp_ptag = libnet_build_udp(
            local_port,
            source->port,
            0x08 + 10,
            0,
            payload,
            10,
            lnet,
            udp_ptag);
    if (udp_ptag == -1)
    {
        fprintf(stderr, "Can't build UDP header: %s\n", libnet_geterror(lnet));
        return;
    }
    ipv4_ptag = libnet_build_ipv4(
            0x14 + 0x08,
            0,
            242,
            0,
            64,
            IPPROTO_UDP,
            0,
            local_ip,
            source->ip,
            ((void *)0),
            0,
            lnet,
            ipv4_ptag);
    if (ipv4_ptag == -1)
    {
        fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(lnet));
        return;
    }
    eth_ptag = libnet_build_ethernet(
            source->mac,
            local_mac,
            0x0800,
            ((void *)0),
            0,
            lnet,
            eth_ptag);
    if (eth_ptag == -1)
    {
        fprintf(stderr, "Can't build ethernet header: %s\n", libnet_geterror(lnet));
        return;
    }
    libnet_write(lnet);
}

variable_types:
payload
char [10]


original_code:
void parse_packet(u_char* args, const struct pcap_pkthdr* pkthdr, const u_char* packet)
{
    if (pkthdr->caplen < pkthdr->len) return;
    u_long src_ip;
    u_long seq;
    u_short src_port;
    u_char flags;
    u_char src_mac[6];
    memcpy(&src_mac, packet+6, 6);
    memcpy(&src_ip, packet+0x0e +12, 4);
    memcpy(&src_port, packet+0x0e +0x14 +0, 2);
    memcpy(&seq, packet+0x0e +0x14 +0x08 +0, 4);
    memcpy(&flags, packet+0x0e +0x14 +0x08 +8, sizeof(u_char));
    src_port = ntohs(src_port);
    packet_t* newp = malloc(sizeof(packet_t));
    int offset = 0x0e + 0x14 + 0x08;
    int len = pkthdr->len - offset;
    newp->seq = seq;
    newp->data = malloc(len);
    memcpy(newp->data, packet + offset, len);
    newp->len = len;
    newp->next = ((void *)0);
    int s = 0;
    while (s < src_num && !match_source(&sources[s], src_ip, src_port)) ++s;
    if (s >= src_num && (flags & 0x02)) {
        s = src_num++;
        sources[s].ip = src_ip;
        sources[s].port = src_port;
        sources[s].seq = seq;
        sources[s].packets = newp;
    } else {
        insert_packet(&sources[s], newp);
    }
    memcpy(sources[s].mac, src_mac, 6);
    send_ack(&sources[s]);
}

canonical_original_code:
void parse_packet(unsigned char *args, struct pcap_pkthdr *pkthdr, unsigned char *packet)
{
    if (pkthdr->caplen < pkthdr->len) return;
    unsigned long src_ip;
    unsigned long seq;
    unsigned short src_port;
    unsigned char flags;
    unsigned char src_mac[6];
    memcpy(&src_mac, packet+6, 6);
    memcpy(&src_ip, packet+0x0e +12, 4);
    memcpy(&src_port, packet+0x0e +0x14 +0, 2);
    memcpy(&seq, packet+0x0e +0x14 +0x08 +0, 4);
    memcpy(&flags, packet+0x0e +0x14 +0x08 +8, sizeof(u_char));
    src_port = ntohs(src_port);
    struct packet_s *newp = malloc(sizeof(packet_t));
    int offset = 0x0e + 0x14 + 0x08;
    int len = pkthdr->len - offset;
    newp->seq = seq;
    newp->data = malloc(len);
    memcpy(newp->data, packet + offset, len);
    newp->len = len;
    newp->next = ((void *)0);
    int s = 0;
    while (s < src_num && !match_source(&sources[s], src_ip, src_port)) ++s;
    if (s >= src_num && (flags & 0x02)) {
        s = src_num++;
        sources[s].ip = src_ip;
        sources[s].port = src_port;
        sources[s].seq = seq;
        sources[s].packets = newp;
    } else {
        insert_packet(&sources[s], newp);
    }
    memcpy(sources[s].mac, src_mac, 6);
    send_ack(&sources[s]);
}

variable_types:
args
unsigned char *

variable_types: struct pcap_pkthdr { struct timeval { long tv_sec; long tv_usec; } ts; unsigned int caplen; unsigned int len; } *

variable_types:
packet
unsigned char *

variable_types:
seq
unsigned long

variable_types:
flags
unsigned char

variable_types: offset int

variable_types: len int

variable_types: s int

user_defined_types: struct pcap_pkthdr { struct timeval ts; unsigned int caplen; unsigned int len; }


decompiled_code:
__int64 set_local_fields()
{
__int64 v0; // rax
struct in_addr *v1; // rbx
__int64 result; // rax
const char *v3; // rax

v0 = libnet_get_hwaddr();
if ( v0 )
{
local_mac = *(_DWORD *)v0;
word_40C164 = *(_WORD *)(v0 + 4);
v1 = (struct in_addr *)malloc(4uLL);
if ( inet_aton("192.168.10.10", v1) )
{
local_ip = *(_QWORD *)&v1->s_addr;
free(v1);
local_port = 13579LL;
result = 0LL;
}
else
{
perror("Failed to set local ip");
result = 1LL;
}
}
else
{
v3 = (const char *)libnet_geterror();
perror(v3);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 func14()
{
__int64 v0; // rax
struct in_addr *v1; // rbx
__int64 result; // rax
const char *v3; // rax

v0 = libnet_get_hwaddr();
if ( v0 )
{
local_mac = *(_DWORD *)v0;
word_40C164 = *(_WORD *)(v0 + 4);
v1 = (struct in_addr *)malloc(4uLL);
if ( inet_aton("192.168.10.10", v1) )
{
local_ip = *(_QWORD *)&v1->s_addr;
free(v1);
local_port = 13579LL;
result = 0LL;
}
else
{
perror("Failed to set local ip");
result = 1LL;
}
}
else
{
v3 = (const char *)libnet_geterror();
perror(v3);
result = 1LL;
}
return result;
}

original_code:
int set_local_fields() {
    struct libnet_ether_addr* src_hwaddr = libnet_get_hwaddr(lnet);
    if (src_hwaddr == ((void *)0)) {
        perror(libnet_geterror(lnet));
        return 1;
    }
    memcpy(local_mac, src_hwaddr->ether_addr_octet, 6);
    struct in_addr* inp = malloc(sizeof(struct in_addr));
    if (inet_aton("192.168.10.10", inp) == 0) {
        perror("Failed to set local ip");
        return 1;
    }
    memcpy(&local_ip, &(inp->s_addr), sizeof(u_long));
    free(inp);
    local_port = 13579;
    return 0;
}

canonical_original_code:
int set_local_fields() {
    struct libnet_ether_addr *src_hwaddr = libnet_get_hwaddr(lnet);
    if (src_hwaddr == ((void *)0)) {
        perror(libnet_geterror(lnet));
        return 1;
    }
    memcpy(local_mac, src_hwaddr->ether_addr_octet, 6);
    struct in_addr *inp = malloc(sizeof(struct in_addr));
    if (inet_aton("192.168.10.10", inp) == 0) {
        perror("Failed to set local ip");
        return 1;
    }
    memcpy(&local_ip, &(inp->s_addr), sizeof(u_long));
    free(inp);
    local_port = 13579;
    return 0;
}

variable_types: struct libnet_ether_addr { unsigned char ether_addr_octet[6]; } *

variable_types: struct in_addr { unsigned int s_addr; } *


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbx
int v4; // ebx
char s[64]; // [rsp+10h] [rbp-448h] BYREF
char v7[1032]; // [rsp+50h] [rbp-408h] BYREF

v3 = argv[1];
file = fopen(argv[2], "w+");
pcap = pcap_open_live();
if ( pcap )
{
lnet = libnet_init();
if ( lnet )
{
v4 = set_local_fields(0LL, v3);
if ( v4 )
{
puts("set_local_fields failed");
v4 = 1;
}
else
{
sprintf(s, "udp and dst port %d", 13579LL);
pcap_compile();
pcap_setfilter();
memset(sources, 0, sizeof(sources));
pcap_loop();
libnet_destroy();
pcap_close();
fclose(file);
}
}
else
{
puts("libnet init error!");
fputs(v7, _bss_start);
pcap_close();
v4 = 1;
}
}
else
{
puts("pcap init error!");
fputs(v7, _bss_start);
v4 = 1;
}
return v4;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbx
int v4; // ebx
char s[64]; // [rsp+10h] [rbp-448h] BYREF
char v7[1032]; // [rsp+50h] [rbp-408h] BYREF

v3 = argv[1];
file = fopen(argv[2], "w+");
pcap = pcap_open_live();
if ( pcap )
{
lnet = libnet_init();
if ( lnet )
{
v4 = func14(0LL, v3);
if ( v4 )
{
puts("set_local_fields failed");
v4 = 1;
}
else
{
sprintf(s, "udp and dst port %d", 13579LL);
pcap_compile();
pcap_setfilter();
memset(sources, 0, sizeof(sources));
pcap_loop();
libnet_destroy();
pcap_close();
fclose(file);
}
}
else
{
puts("libnet init error!");
fputs(v7, _bss_start);
pcap_close();
v4 = 1;
}
}
else
{
puts("pcap init error!");
fputs(v7, _bss_start);
v4 = 1;
}
return v4;
}

original_code:
int main(int agrc, char** argv)
{
    char err_buf[1024];
    char* dev = argv[1];
    file = fopen(argv[2], "w+");
    pcap = pcap_open_live(dev, 65536, 1, 0, err_buf);
    if (pcap == ((void *)0)) {
        printf("pcap init error!\n");
        fprintf(stderr, "%s", err_buf);
        return 1;
    }
    lnet = libnet_init(0x00, dev, err_buf);
    if (lnet == ((void *)0)) {
        printf("libnet init error!\n");
        fprintf(stderr, "%s", err_buf);
        pcap_close(pcap);
        return 1;
    }
    if (set_local_fields() != 0) {
        printf("set_local_fields failed\n");
        return 1;
    }
    char pattern[64];
    sprintf(pattern, "udp and dst port %d", 13579);
    struct bpf_program filter;
    pcap_compile(pcap, &filter, pattern, 1, 0);
    pcap_setfilter(pcap, &filter);
    memset(sources, 0, sizeof(sources));
    pcap_loop(pcap, -1, parse_packet, ((void *)0));
    libnet_destroy(lnet);
    pcap_close(pcap);
    fclose(file);
    return 0;
}

canonical_original_code:
int main(int agrc, char **argv)
{
    char err_buf[1024];
    char *dev = argv[1];
    file = fopen(argv[2], "w+");
    pcap = pcap_open_live(dev, 65536, 1, 0, err_buf);
    if (pcap == ((void *)0)) {
        printf("pcap init error!\n");
        fprintf(stderr, "%s", err_buf);
        return 1;
    }
    lnet = libnet_init(0x00, dev, err_buf);
    if (lnet == ((void *)0)) {
        printf("libnet init error!\n");
        fprintf(stderr, "%s", err_buf);
        pcap_close(pcap);
        return 1;
    }
    if (set_local_fields() != 0) {
        printf("set_local_fields failed\n");
        return 1;
    }
    char pattern[64];
    sprintf(pattern, "udp and dst port %d", 13579);
    struct bpf_program filter;
    pcap_compile(pcap, &filter, pattern, 1, 0);
    pcap_setfilter(pcap, &filter);
    memset(sources, 0, sizeof(sources));
    pcap_loop(pcap, -1, parse_packet, ((void *)0));
    libnet_destroy(lnet);
    pcap_close(pcap);
    fclose(file);
    return 0;
}

variable_types: agrc int

variable_types:
argv
char **

variable_types:
dev
char *

variable_types:
pattern
char [64]

variable_types: struct bpf_program { unsigned int bf_len; struct bpf_insn { unsigned short code; unsigned char jt; unsigned char jf; unsigned int k; } *bf_insns; }

user_defined_types: struct bpf_program { unsigned int bf_len; struct bpf_insn *bf_insns; }


list

decompiled_code:
__int64 __fastcall list(char *dir)
{
size_t v1; // rax
char *v2; // r14
__int64 v3; // r12
_QWORD *v4; // r13
__int64 i; // rbx
size_t v6; // rax
__int64 v7; // rbp
__int64 j; // rbx
struct dirent **namelist; // [rsp+8h] [rbp-40h] BYREF

puts("List operation");
v1 = strlen(dir);
v2 = (char *)malloc(v1 + 12);
v3 = scandir(
dir,
&namelist,
(int (*)(const struct dirent *))&filterFiles,
(int (*)(const struct dirent **, const struct dirent **))&alphasort);
v4 = malloc(48 * v3);
if ( v3 )
{
for ( i = 0LL; i != v3; ++i )
{
v6 = strlen(dir);
snprintf(v2, v6 + 12, "%s/%s", dir, namelist[i]->d_name);
v7 = getEntryFromFile(v2);
v4[i] = v7;
free(namelist[i]);
if ( v7 )
printEntry(v7);
}
for ( j = 0LL; j != v3; ++j )
freeEntry(v4[j]);
}
free(v2);
free(v4);
free(namelist);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *dir)
{
size_t v1; // rax
char *v2; // r14
__int64 v3; // r12
_QWORD *v4; // r13
__int64 i; // rbx
size_t v6; // rax
__int64 v7; // rbp
__int64 j; // rbx
struct dirent **namelist; // [rsp+8h] [rbp-40h] BYREF

puts("List operation");
v1 = strlen(dir);
v2 = (char *)malloc(v1 + 12);
v3 = scandir(
dir,
&namelist,
(int (*)(const struct dirent *))&filterFiles,
(int (*)(const struct dirent **, const struct dirent **))&alphasort);
v4 = malloc(48 * v3);
if ( v3 )
{
for ( i = 0LL; i != v3; ++i )
{
v6 = strlen(dir);
snprintf(v2, v6 + 12, "%s/%s", dir, namelist[i]->d_name);
v7 = getEntryFromFile(v2);
v4[i] = v7;
free(namelist[i]);
if ( v7 )
printEntry(v7);
}
for ( j = 0LL; j != v3; ++j )
freeEntry(v4[j]);
}
free(v2);
free(v4);
free(namelist);
return 0LL;
}

original_code:
int
list(const char* storageFolder)
{
    printf ("List operation\n");
    struct dirent **nameList;
    size_t numberFiles;
    struct LogEntry **entries;
    char* fileNameBuffer = malloc (strlen (storageFolder) + 12);
    int i = 0;
    numberFiles = scandir (storageFolder, &nameList, filterFiles, alphasort);
    entries = malloc (numberFiles * sizeof (struct LogEntry));
    for (i = 0; i < numberFiles; i++)
    {
        snprintf (fileNameBuffer, strlen (storageFolder) + 12, "%s/%s",
                    storageFolder, nameList[i]->d_name);
        entries[i] = getEntryFromFile (fileNameBuffer);
        free (nameList[i]);
        if (entries[i] != ((void *)0))
        {
            printEntry (entries[i]);
        }
    }
    for (i = 0; i < numberFiles; i++)
    {
        freeEntry (entries[i]);
    }
    free (fileNameBuffer);
    free (entries);
    free (nameList);
    return 0;
}

canonical_original_code:
int list(char *storageFolder)
{
    printf ("List operation\n");
    struct dirent **nameList;
    unsigned long numberFiles;
    struct LogEntry **entries;
    char *fileNameBuffer = malloc (strlen (storageFolder) + 12);
    int i = 0;
    numberFiles = scandir (storageFolder, &nameList, filterFiles, alphasort);
    entries = malloc (numberFiles * sizeof (struct LogEntry));
    for (i = 0; i < numberFiles; i++)
    {
        snprintf (fileNameBuffer, strlen (storageFolder) + 12, "%s/%s",
                    storageFolder, nameList[i]->d_name);
        entries[i] = getEntryFromFile (fileNameBuffer);
        free (nameList[i]);
        if (entries[i] != ((void *)0))
        {
            printEntry (entries[i]);
        }
    }
    for (i = 0; i < numberFiles; i++)
    {
        freeEntry (entries[i]);
    }
    free (fileNameBuffer);
    free (entries);
    free (nameList);
    return 0;
}

variable_types:
storageFolder
char *

variable_types: struct dirent { unsigned long d_ino; long d_off; unsigned short d_reclen; unsigned char d_type; char d_name[256]; } **

variable_types:
numberFiles
unsigned long

variable_types: struct LogEntry { int id; long time; char *timezone; char *location; char *project; char *content; } **

variable_types:
fileNameBuffer
char *

variable_types: i int


usage

decompiled_code:
size_t usage()
{
return fwrite("usage: exlog [command] [-h]\n", 1uLL, 0x1CuLL, stderr);
}

canonical_decompiled_code:
size_t func0()
{
return fwrite("usage: exlog [command] [-h]\n", 1uLL, 0x1CuLL, stderr);
}

original_code:
void
usage (void)
{
    fprintf (stderr, "usage: exlog [command] [-h]\n");
}

canonical_original_code:
void usage()
{
    fprintf (stderr, "usage: exlog [command] [-h]\n");
}


setup

decompiled_code:
void setup()
{
char *v0; // rax
const char *v1; // r12
size_t v2; // rbp
char *v3; // rbx
size_t v4; // rax
void *v5; // rbp
size_t v6; // rax
int *v7; // rax
char *v8; // rax
struct stat v9; // [rsp+0h] [rbp-A8h] BYREF

v0 = getenv("HOME");
if ( !v0 )
{
fwrite("HOME not set", 1uLL, 0xCuLL, stderr);
exit(1);
}
v1 = v0;
v2 = snprintf(0LL, 0LL, "%s/.exlog", v0) + 1;
v3 = (char *)malloc(v2);
snprintf(v3, v2, "%s/.exlog", v1);
if ( stat(v3, &v9) == -1 || (v9.st_mode & 0xF000) != 0x4000 )
{
printf("Storage folder not found.\nCreating folder \"%s\" now...\n", v3);
if ( mkdir(v3, 0x1C0u) )
{
v7 = _errno_location();
v8 = strerror(*v7);
fprintf(stderr, "Failed to create storage folder.\nERROR: %s\n", v8);
}
}
v4 = strlen(v3);
v5 = malloc(v4 + 1);
gStorageFolder = v5;
v6 = strlen(v3);
strncpy((char *)v5, v3, v6 + 1);
free(v3);
}

canonical_decompiled_code:
void func1()
{
char *v0; // rax
const char *v1; // r12
size_t v2; // rbp
char *v3; // rbx
size_t v4; // rax
void *v5; // rbp
size_t v6; // rax
int *v7; // rax
char *v8; // rax
struct stat v9; // [rsp+0h] [rbp-A8h] BYREF

v0 = getenv("HOME");
if ( !v0 )
{
fwrite("HOME not set", 1uLL, 0xCuLL, stderr);
exit(1);
}
v1 = v0;
v2 = snprintf(0LL, 0LL, "%s/.exlog", v0) + 1;
v3 = (char *)malloc(v2);
snprintf(v3, v2, "%s/.exlog", v1);
if ( stat(v3, &v9) == -1 || (v9.st_mode & 0xF000) != 0x4000 )
{
printf("Storage folder not found.\nCreating folder \"%s\" now...\n", v3);
if ( mkdir(v3, 0x1C0u) )
{
v7 = _errno_location();
v8 = strerror(*v7);
fprintf(stderr, "Failed to create storage folder.\nERROR: %s\n", v8);
}
}
v4 = strlen(v3);
v5 = malloc(v4 + 1);
gStorageFolder = v5;
v6 = strlen(v3);
strncpy((char *)v5, v3, v6 + 1);
free(v3);
}

original_code:
void
setup (void)
{
    struct stat sts;
    char *home = getenv ("HOME");
    if (home != ((void *)0))
    {
        size_t sizeNeeded = snprintf (((void *)0), 0, "%s/.exlog", home) + 1;
        char *path = malloc (sizeNeeded);
        snprintf (path, sizeNeeded, "%s/.exlog", home);
        size_t ret = stat(path, &sts);
        if ((ret == -1) || &sts != ((void *)0) && !((((sts.st_mode)) & 0170000) == (0040000)))
        {
            printf ("Storage folder not found.\n"
                    "Creating folder \"%s\" now...\n", path);
            if (mkdir (path, 0700) != 0)
            {
                fprintf (stderr, "Failed to create storage folder.\n"
                            "ERROR: %s\n", strerror((*__errno_location ())));
            }
        }
        gStorageFolder = malloc (strlen (path) + 1);
        strncpy (gStorageFolder, path, strlen (path) + 1);
        free (path);
    }else
    {
        fprintf (stderr, "HOME not set");
        exit (1);
    }
}

canonical_original_code:
void setup()
{
    struct stat sts;
    char *home = getenv ("HOME");
    if (home != ((void *)0))
    {
        unsigned long sizeNeeded = snprintf (((void *)0), 0, "%s/.exlog", home) + 1;
        char *path = malloc (sizeNeeded);
        snprintf (path, sizeNeeded, "%s/.exlog", home);
        unsigned long ret = stat(path, &sts);
        if ((ret == -1) || &sts != ((void *)0) && !((((sts.st_mode)) & 0170000) == (0040000)))
        {
            printf ("Storage folder not found.\n"
                    "Creating folder \"%s\" now...\n", path);
            if (mkdir (path, 0700) != 0)
            {
                fprintf (stderr, "Failed to create storage folder.\n"
                            "ERROR: %s\n", strerror((*__errno_location ())));
            }
        }
        gStorageFolder = malloc (strlen (path) + 1);
        strncpy (gStorageFolder, path, strlen (path) + 1);
        free (path);
    }else
    {
        fprintf (stderr, "HOME not set");
        exit (1);
    }
}

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

variable_types:
home
char *

variable_types:
sizeNeeded
unsigned long

variable_types:
path
char *

variable_types:
ret
unsigned long

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


teardown

decompiled_code:
void teardown()
{
free(gStorageFolder);
gStorageFolder = 0LL;
}

canonical_decompiled_code:
void func2()
{
free(gStorageFolder);
gStorageFolder = 0LL;
}

original_code:
void
teardown (void)
{
    free (gStorageFolder);
    gStorageFolder = ((void *)0);
}

canonical_original_code:
void teardown()
{
    free (gStorageFolder);
    gStorageFolder = ((void *)0);
}


callAdd

decompiled_code:
__int64 __fastcall callAdd(int argc, char **argv)
{
char *v2; // r13
char *v3; // r12
int v4; // eax
struct option v6; // [rsp+0h] [rbp-A8h] BYREF
const char *v7; // [rsp+20h] [rbp-88h]
int v8; // [rsp+28h] [rbp-80h]
__int64 v9; // [rsp+30h] [rbp-78h]
int v10; // [rsp+38h] [rbp-70h]
const char *v11; // [rsp+40h] [rbp-68h]
int v12; // [rsp+48h] [rbp-60h]
__int64 v13; // [rsp+50h] [rbp-58h]
int v14; // [rsp+58h] [rbp-50h]
__int64 v15; // [rsp+60h] [rbp-48h]
int v16; // [rsp+68h] [rbp-40h]
__int64 v17; // [rsp+70h] [rbp-38h]
int v18; // [rsp+78h] [rbp-30h]

v6.name = "help";
v6.has_arg = 0;
v6.flag = 0LL;
v6.val = 104;
v7 = "location";
v8 = 1;
v9 = 0LL;
v10 = 108;
v11 = "project";
v12 = 1;
v13 = 0LL;
v14 = 112;
v15 = 0LL;
v16 = 0;
v17 = 0LL;
v18 = 0;
v2 = 0LL;
v3 = 0LL;
while ( 1 )
{
v4 = getopt_long(argc, argv, "h", &v6, 0LL);
if ( v4 == -1 )
break;
switch ( v4 )
{
case 'l':
v3 = optarg;
break;
case 'p':
v2 = optarg;
break;
case 'h':
usage();
teardown();
exit(0);
default:
usage();
teardown();
exit(1);
}
}
return add(gStorageFolder, v3, v2);
}

canonical_decompiled_code:
__int64 __fastcall func3(int argc, char **argv)
{
char *v2; // r13
char *v3; // r12
int v4; // eax
struct option v6; // [rsp+0h] [rbp-A8h] BYREF
const char *v7; // [rsp+20h] [rbp-88h]
int v8; // [rsp+28h] [rbp-80h]
__int64 v9; // [rsp+30h] [rbp-78h]
int v10; // [rsp+38h] [rbp-70h]
const char *v11; // [rsp+40h] [rbp-68h]
int v12; // [rsp+48h] [rbp-60h]
__int64 v13; // [rsp+50h] [rbp-58h]
int v14; // [rsp+58h] [rbp-50h]
__int64 v15; // [rsp+60h] [rbp-48h]
int v16; // [rsp+68h] [rbp-40h]
__int64 v17; // [rsp+70h] [rbp-38h]
int v18; // [rsp+78h] [rbp-30h]

v6.name = "help";
v6.has_arg = 0;
v6.flag = 0LL;
v6.val = 104;
v7 = "location";
v8 = 1;
v9 = 0LL;
v10 = 108;
v11 = "project";
v12 = 1;
v13 = 0LL;
v14 = 112;
v15 = 0LL;
v16 = 0;
v17 = 0LL;
v18 = 0;
v2 = 0LL;
v3 = 0LL;
while ( 1 )
{
v4 = getopt_long(argc, argv, "h", &v6, 0LL);
if ( v4 == -1 )
break;
switch ( v4 )
{
case 'l':
v3 = optarg;
break;
case 'p':
v2 = optarg;
break;
case 'h':
func0();
func2();
exit(0);
default:
func0();
func2();
exit(1);
}
}
return add(gStorageFolder, v3, v2);
}

original_code:
void
callAdd (int argc, char* argv[])
{
    const struct option long_options[] =
    {
        {"help", 0, ((void *)0), 'h'},
        {"location", 1, ((void *)0), 'l'},
        {"project", 1, ((void *)0), 'p'},
        {((void *)0), 0, ((void *)0), 0}
    };
    const char* location = ((void *)0);
    const char* project = ((void *)0);
    int next_option;
    while ((next_option = getopt_long (argc, argv, "h", long_options, ((void *)0)))
            != -1)
    {
        switch (next_option)
        {
            case 'h':
                usage ();
                teardown ();
                exit (0);
            case 'l':
                location = optarg;
                break;
            case 'p':
                project = optarg;
                break;
            default:
                usage ();
                teardown ();
                exit (1);
         }
    }
    add (gStorageFolder, location, project);
}

canonical_original_code:
void callAdd(int argc, char *argv[])
{
    struct option long_options[] = {
        {"help", 0, ((void *)0), 'h'},
        {"location", 1, ((void *)0), 'l'},
        {"project", 1, ((void *)0), 'p'},
        {((void *)0), 0, ((void *)0), 0}
    };
    char *location = ((void *)0);
    char *project = ((void *)0);
    int next_option;
    while ((next_option = getopt_long (argc, argv, "h", long_options, ((void *)0)))
            != -1)
    {
        switch (next_option)
        {
            case 'h':
                usage ();
                teardown ();
                exit (0);
            case 'l':
                location = optarg;
                break;
            case 'p':
                project = optarg;
                break;
            default:
                usage ();
                teardown ();
                exit (1);
         }
    }
    add (gStorageFolder, location, project);
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct option { char *name; int has_arg; int *flag; int val; } []

variable_types:
location
char *

variable_types:
project
char *


callRm

decompiled_code:
int __fastcall callRm(int a1, __int64 a2)
{
int result; // eax
unsigned int v3; // [rsp+Ch] [rbp-Ch] BYREF

if ( a1 <= 1 )
{
puts("You need to specify what to delete.");
teardown();
exit(1);
}
result = sscanf(*(const char **)(a2 + 16), "%u", &v3);
if ( result == 1 )
result = rm(v3);
return result;
}

canonical_decompiled_code:
int __fastcall func4(int a1, __int64 a2)
{
int result; // eax
unsigned int v3; // [rsp+Ch] [rbp-Ch] BYREF

if ( a1 <= 1 )
{
puts("You need to specify what to delete.");
func2();
exit(1);
}
result = sscanf(*(const char **)(a2 + 16), "%u", &v3);
if ( result == 1 )
result = rm(v3);
return result;
}

original_code:
void
callRm (int argc, char* argv[])
{
   uint id;
   if (argc < 2)
   {
       printf ("You need to specify what to delete.\n");
       teardown ();
       exit (1);
   }else
   {
       if (sscanf (argv[2], "%u", &id) == 1)
       {
           rm (id);
       }
   }
}

canonical_original_code:
void callRm(int argc, char *argv[])
{
   unsigned int id;
   if (argc < 2)
   {
       printf ("You need to specify what to delete.\n");
       teardown ();
       exit (1);
   }else
   {
       if (sscanf (argv[2], "%u", &id) == 1)
       {
           rm (id);
       }
   }
}

variable_types:
id
unsigned int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v4; // rbp
__int64 v5; // rbx
const char *v6; // rsi
__int64 v8[9]; // [rsp+0h] [rbp-48h]

v8[0] = (__int64)"add";
v8[1] = (__int64)"rm";
v8[2] = (__int64)"list";
v8[3] = 0LL;
setup();
if ( argc <= 1 )
{
list(gStorageFolder);
teardown();
exit(0);
}
v4 = argv[1];
v5 = 0LL;
v6 = "add";
do
{
if ( !strcasecmp(v4, v6) )
break;
v6 = (const char *)v8[++v5];
}
while ( v6 );
if ( v5 == 1 )
{
callRm(argc, (__int64)argv);
}
else if ( v5 == 2 )
{
list(gStorageFolder);
}
else
{
if ( v5 )
{
fwrite("Unknown command...\n", 1uLL, 0x13uLL, stderr);
exit(1);
}
callAdd(argc, (char **)argv);
}
teardown();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v4; // rbp
__int64 v5; // rbx
const char *v6; // rsi
__int64 v8[9]; // [rsp+0h] [rbp-48h]

v8[0] = (__int64)"add";
v8[1] = (__int64)"rm";
v8[2] = (__int64)"list";
v8[3] = 0LL;
func1();
if ( argc <= 1 )
{
list(gStorageFolder);
func2();
exit(0);
}
v4 = argv[1];
v5 = 0LL;
v6 = "add";
do
{
if ( !strcasecmp(v4, v6) )
break;
v6 = (const char *)v8[++v5];
}
while ( v6 );
if ( v5 == 1 )
{
func4(argc, (__int64)argv);
}
else if ( v5 == 2 )
{
list(gStorageFolder);
}
else
{
if ( v5 )
{
fwrite("Unknown command...\n", 1uLL, 0x13uLL, stderr);
exit(1);
}
func3(argc, (char **)argv);
}
func2();
return 0;
}

original_code:
int
main (int argc, char *argv[])
{
    size_t cmd;
    const char *commands[]={"add", "rm", "list", ((void *)0)};
    enum {ADD,RM,LIST};
    setup ();
    if (argc <= 1)
    {
        list (gStorageFolder);
        teardown ();
        exit (0);
    }
    for (cmd=0;commands[cmd] && strcasecmp(argv[1],commands[cmd]);cmd++);
    switch (cmd)
    {
        case ADD:
            callAdd (argc, argv);
            break;
        case RM:
            callRm (argc, argv);
            break;
        case LIST:
            list (gStorageFolder);
            break;
        default:
            fprintf (stderr, "Unknown command...\n");
            exit (1);
            break;
    }
    teardown ();
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    unsigned long cmd;
    char *commands[] = {"add", "rm", "list", ((void *)0)};
    enum {ADD,RM,LIST};
    setup ();
    if (argc <= 1)
    {
        list (gStorageFolder);
        teardown ();
        exit (0);
    }
    for (cmd=0;commands[cmd] && strcasecmp(argv[1],commands[cmd]);cmd++);
    switch (cmd)
    {
        case ADD:
            callAdd (argc, argv);
            break;
        case RM:
            callRm (argc, argv);
            break;
        case LIST:
            list (gStorageFolder);
            break;
        default:
            fprintf (stderr, "Unknown command...\n");
            exit (1);
            break;
    }
    teardown ();
    return 0;
}

variable_types:
cmd
unsigned long

variable_types:
commands
char *[]


freeEntry

decompiled_code:
void __fastcall freeEntry(void *ptr)
{
free(*((void **)ptr + 2));
free(*((void **)ptr + 3));
free(*((void **)ptr + 4));
free(*((void **)ptr + 5));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func0(void *ptr)
{
free(*((void **)ptr + 2));
free(*((void **)ptr + 3));
free(*((void **)ptr + 4));
free(*((void **)ptr + 5));
free(ptr);
}

original_code:
void
freeEntry (struct LogEntry* entry)
{
    free (entry->timezone);
    free (entry->location);
    free (entry->project);
    free (entry->content);
    free (entry);
}

canonical_original_code:
void freeEntry(struct LogEntry *entry)
{
    free (entry->timezone);
    free (entry->location);
    free (entry->project);
    free (entry->content);
    free (entry);
}

variable_types: struct LogEntry { int id; long time; char *timezone; char *location; char *project; char *content; } *


getTimeFromFile

decompiled_code:
__int64 __fastcall getTimeFromFile(const char *a1)
{
char *v1; // rax
__int64 result; // rax
int v3; // [rsp+Ch] [rbp-Ch] BYREF

v3 = -1;
v1 = strrchr(a1, 47);
sscanf(v1 + 1, "%d", &v3);
LODWORD(result) = v3;
if ( v3 == -1 )
LODWORD(result) = 0;
return (int)result;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1)
{
char *v1; // rax
__int64 result; // rax
int v3; // [rsp+Ch] [rbp-Ch] BYREF

v3 = -1;
v1 = strrchr(a1, 47);
sscanf(v1 + 1, "%d", &v3);
LODWORD(result) = v3;
if ( v3 == -1 )
LODWORD(result) = 0;
return (int)result;
}

original_code:
time_t
getTimeFromFile (const char* fileName)
{
    struct tm* cal;
    time_t timestamp = -1;
    int i = -1;
    sscanf (strrchr (fileName, '/') + 1, "%d", &i);
    if (i != -1)
    {
        timestamp = i;
        return timestamp;
    } else
    {
        return 0;
    }
}

canonical_original_code:
long getTimeFromFile(char *fileName)
{
    struct tm *cal;
    long timestamp = -1;
    int i = -1;
    sscanf (strrchr (fileName, '/') + 1, "%d", &i);
    if (i != -1)
    {
        timestamp = i;
        return timestamp;
    } else
    {
        return 0;
    }
}

variable_types:
fileName
char *

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

variable_types: timestamp long

return_type: long


getCorrectedTime

decompiled_code:
struct tm *__fastcall getCorrectedTime(__int64 a1)
{
char *v1; // rbp
struct tm *v2; // rbx

v1 = getenv("TZ");
setenv("TZ", *(const char **)(a1 + 16), 1);
v2 = localtime((const time_t *)(a1 + 8));
setenv("TZ", v1, 1);
return v2;
}

canonical_decompiled_code:
struct tm *__fastcall func2(__int64 a1)
{
char *v1; // rbp
struct tm *v2; // rbx

v1 = getenv("TZ");
setenv("TZ", *(const char **)(a1 + 16), 1);
v2 = localtime((const time_t *)(a1 + 8));
setenv("TZ", v1, 1);
return v2;
}

original_code:
struct tm*
getCorrectedTime (const struct LogEntry* entry)
{
    char* curTZ = getenv ("TZ");
    setenv ("TZ", entry->timezone, 1);
    struct tm* t = localtime (&entry->time);
    setenv ("TZ", curTZ, 1);
    return t;
}

canonical_original_code:
struct tm *getCorrectedTime(struct LogEntry *entry)
{
    char *curTZ = getenv ("TZ");
    setenv ("TZ", entry->timezone, 1);
    struct tm *t = localtime (&entry->time);
    setenv ("TZ", curTZ, 1);
    return t;
}

variable_types:
curTZ
char *

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

user_defined_types: struct LogEntry { int id; long time; char *timezone; char *location; char *project; char *content; }


printEntry

decompiled_code:
void __fastcall printEntry(unsigned int *a1)
{
struct tm *v1; // r12
char *v2; // rax
char *v3; // rbp
const char *v4; // rsi
const char *v5; // rsi

v1 = getCorrectedTime((__int64)a1);
v2 = (char *)malloc(0x1AuLL);
v3 = v2;
if ( v1 )
strftime(v2, 0x1AuLL, "%F %T %z", v1);
printf("Entry %d at %s\n", *a1, v3);
v4 = (const char *)*((_QWORD *)a1 + 4);
if ( v4 && !*v4 )
printf("Entry for project %s ", v4);
else
printf("Entry for project default ");
v5 = (const char *)*((_QWORD *)a1 + 3);
if ( v5 )
printf("at location %s\n", v5);
else
puts("at location default");
printf("Content: %s\n", *((const char **)a1 + 5));
free(v3);
}

canonical_decompiled_code:
void __fastcall func3(unsigned int *a1)
{
struct tm *v1; // r12
char *v2; // rax
char *v3; // rbp
const char *v4; // rsi
const char *v5; // rsi

v1 = func2((__int64)a1);
v2 = (char *)malloc(0x1AuLL);
v3 = v2;
if ( v1 )
strftime(v2, 0x1AuLL, "%F %T %z", v1);
printf("Entry %d at %s\n", *a1, v3);
v4 = (const char *)*((_QWORD *)a1 + 4);
if ( v4 && !*v4 )
printf("Entry for project %s ", v4);
else
printf("Entry for project default ");
v5 = (const char *)*((_QWORD *)a1 + 3);
if ( v5 )
printf("at location %s\n", v5);
else
puts("at location default");
printf("Content: %s\n", *((const char **)a1 + 5));
free(v3);
}

original_code:
void
printEntry (const struct LogEntry* entry)
{
    struct tm* t = getCorrectedTime (entry);
    char* date = malloc (26);
    if (t != ((void *)0))
    {
        strftime (date, 26, "%F %T %z\0", t);
    }
    printf ("Entry %d at %s\n",
            entry->id,
            date);
    if (entry->project == ((void *)0) || strcmp(entry->project, ""))
    {
        printf ("Entry for project default ");
    }else
    {
        printf ("Entry for project %s ", entry->project);
    }
    if (entry->location == ((void *)0))
    {
        printf ("at location default\n");
    }else
    {
        printf ("at location %s\n", entry->location);
    }
    printf ("Content: %s\n", entry->content);
    free (date);
}

canonical_original_code:
void printEntry(struct LogEntry *entry)
{
    struct tm *t = getCorrectedTime (entry);
    char *date = malloc (26);
    if (t != ((void *)0))
    {
        strftime (date, 26, "%F %T %z\0", t);
    }
    printf ("Entry %d at %s\n",
            entry->id,
            date);
    if (entry->project == ((void *)0) || strcmp(entry->project, ""))
    {
        printf ("Entry for project default ");
    }else
    {
        printf ("Entry for project %s ", entry->project);
    }
    if (entry->location == ((void *)0))
    {
        printf ("at location default\n");
    }else
    {
        printf ("at location %s\n", entry->location);
    }
    printf ("Content: %s\n", entry->content);
    free (date);
}

variable_types:
date
char *

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


getFileName

decompiled_code:
char *__fastcall getFileName(const char *a1, __int64 a2)
{
__int64 v2; // r13
size_t v3; // rbp
char *v4; // rbx

v2 = *(_QWORD *)(a2 + 8);
v3 = strlen(a1) + 12;
v4 = (char *)malloc(v3);
snprintf(v4, v3, "%s/%010d", a1, v2);
return v4;
}

canonical_decompiled_code:
char *__fastcall func4(const char *a1, __int64 a2)
{
__int64 v2; // r13
size_t v3; // rbp
char *v4; // rbx

v2 = *(_QWORD *)(a2 + 8);
v3 = strlen(a1) + 12;
v4 = (char *)malloc(v3);
snprintf(v4, v3, "%s/%010d", a1, v2);
return v4;
}

original_code:
char*
getFileName(const char* storageFolder, const struct LogEntry* entry)
{
    const char *fileNameFormat = "%s/%010d";
    const char* timeFormat = "%FT%T%z";
    time_t t = entry->time;
    size_t sizeNeeded = strlen (storageFolder) + 12;
    char* buf = malloc (sizeNeeded);
    snprintf (buf, sizeNeeded, fileNameFormat, storageFolder, t);
    return buf;
}

canonical_original_code:
char *getFileName(char *storageFolder, struct LogEntry *entry)
{
    char *fileNameFormat = "%s/%010d";
    char *timeFormat = "%FT%T%z";
    long t = entry->time;
    unsigned long sizeNeeded = strlen (storageFolder) + 12;
    char *buf = malloc (sizeNeeded);
    snprintf (buf, sizeNeeded, fileNameFormat, storageFolder, t);
    return buf;
}

variable_types:
storageFolder
char *

variable_types:
fileNameFormat
char *

variable_types:
timeFormat
char *

variable_types: t long

variable_types:
sizeNeeded
unsigned long

variable_types:
buf
char *

return_type: char *


getTimezone

decompiled_code:
char *getTimezone()
{
char *v0; // rax
const char *v1; // rbx
size_t v2; // rax
char *v3; // rbp
size_t v4; // rax

v0 = getenv("TZ");
if ( v0 )
{
v1 = v0;
v2 = strlen(v0);
v3 = (char *)malloc(v2 + 1);
v4 = strlen(v1);
strncpy(v3, v1, v4 + 1);
}
else
{
v3 = (char *)malloc(4uLL);
*(_DWORD *)v3 = 4412501;
}
return v3;
}

canonical_decompiled_code:
char *func1()
{
char *v0; // rax
const char *v1; // rbx
size_t v2; // rax
char *v3; // rbp
size_t v4; // rax

v0 = getenv("TZ");
if ( v0 )
{
v1 = v0;
v2 = strlen(v0);
v3 = (char *)malloc(v2 + 1);
v4 = strlen(v1);
strncpy(v3, v1, v4 + 1);
}
else
{
v3 = (char *)malloc(4uLL);
*(_DWORD *)v3 = 4412501;
}
return v3;
}

original_code:
char*
getTimezone ()
{
    char* env;
    char* tz;
    env = getenv ("TZ");
    if (env == ((void *)0))
    {
        tz = malloc (4);
        strncpy (tz, "UTC\0", 4);
    }else
    {
        tz = malloc (strlen (env) + 1);
        strncpy (tz, env, strlen (env) + 1);
    }
    return tz;
}

canonical_original_code:
char *getTimezone()
{
    char *env;
    char *tz;
    env = getenv ("TZ");
    if (env == ((void *)0))
    {
        tz = malloc (4);
        strncpy (tz, "UTC\0", 4);
    }else
    {
        tz = malloc (strlen (env) + 1);
        strncpy (tz, env, strlen (env) + 1);
    }
    return tz;
}

variable_types:
env
char *

variable_types:
tz
char *


getNextID

decompiled_code:
__int64 __fastcall getNextID(char *s)
{
int v1; // eax
__int64 v2; // rbp
unsigned int v3; // er12
size_t v4; // r13
unsigned __int64 v5; // rbp
char *v6; // r12
size_t v7; // rax
char *v8; // r13
size_t v9; // r14
size_t v10; // rax
_DWORD *v11; // rax
unsigned __int64 v12; // rbx
struct dirent **namelist; // [rsp+8h] [rbp-30h] BYREF

v1 = scandir(
s,
&namelist,
(int (*)(const struct dirent *))&filterFiles,
(int (*)(const struct dirent **, const struct dirent **))&alphasort);
v2 = v1;
v3 = 0;
if ( v1 )
{
v4 = strlen(s);
v5 = 8 * v2;
v6 = namelist[v5 / 8 - 1]->d_name;
v7 = strlen(v6);
v8 = (char *)malloc(v4 + v7 + 2);
v9 = strlen(s);
v10 = strlen(v6);
snprintf(v8, v9 + v10 + 2, "%s/%s", s, v6);
v11 = (_DWORD *)getEntryFromFile(v8);
v3 = *v11 + 1;
freeEntry(v11);
free(v8);
v12 = 0LL;
do
{
free(namelist[v12 / 8]);
v12 += 8LL;
}
while ( v5 != v12 );
}
free(namelist);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *s)
{
int v1; // eax
__int64 v2; // rbp
unsigned int v3; // er12
size_t v4; // r13
unsigned __int64 v5; // rbp
char *v6; // r12
size_t v7; // rax
char *v8; // r13
size_t v9; // r14
size_t v10; // rax
_DWORD *v11; // rax
unsigned __int64 v12; // rbx
struct dirent **namelist; // [rsp+8h] [rbp-30h] BYREF

v1 = scandir(
s,
&namelist,
(int (*)(const struct dirent *))&filterFiles,
(int (*)(const struct dirent **, const struct dirent **))&alphasort);
v2 = v1;
v3 = 0;
if ( v1 )
{
v4 = strlen(s);
v5 = 8 * v2;
v6 = namelist[v5 / 8 - 1]->d_name;
v7 = strlen(v6);
v8 = (char *)malloc(v4 + v7 + 2);
v9 = strlen(s);
v10 = strlen(v6);
snprintf(v8, v9 + v10 + 2, "%s/%s", s, v6);
v11 = (_DWORD *)getEntryFromFile(v8);
v3 = *v11 + 1;
freeEntry(v11);
free(v8);
v12 = 0LL;
do
{
free(namelist[v12 / 8]);
v12 += 8LL;
}
while ( v5 != v12 );
}
free(namelist);
return v3;
}

original_code:
int
getNextID (const char* storageFolder)
{
    struct dirent **nameList;
    size_t numberFiles;
    int id = -1;
    int i = 0;
    numberFiles = scandir (storageFolder, &nameList, filterFiles, alphasort);
    if (numberFiles < 0)
    {
        fprintf (stderr, "Could not open storage folder: %s\n",
                strerror ((*__errno_location ())));
        exit(2);
    }else if (numberFiles == 0)
    {
        id = 0;
    }else
    {
        char* fileName = malloc ( strlen (storageFolder) + strlen (
                    nameList[numberFiles - 1]->d_name) + 2);
        snprintf (fileName, strlen (storageFolder) + strlen (
                    nameList[numberFiles - 1]->d_name) + 2,
                    "%s/%s", storageFolder, nameList[numberFiles - 1]->d_name);
        struct LogEntry* entry = getEntryFromFile (fileName);
        id = entry->id + 1;
        freeEntry (entry);
        free (fileName);
    }
    for (i = 0; i < numberFiles; i++)
    {
        free(nameList[i]);
    }
    free (nameList);
    return id;
}

canonical_original_code:
int getNextID(char *storageFolder)
{
    struct dirent **nameList;
    unsigned long numberFiles;
    int id = -1;
    int i = 0;
    numberFiles = scandir (storageFolder, &nameList, filterFiles, alphasort);
    if (numberFiles < 0)
    {
        fprintf (stderr, "Could not open storage folder: %s\n",
                strerror ((*__errno_location ())));
        exit(2);
    }else if (numberFiles == 0)
    {
        id = 0;
    }else
    {
        char *fileName = malloc ( strlen (storageFolder) + strlen (
                    nameList[numberFiles - 1]->d_name) + 2);
        snprintf (fileName, strlen (storageFolder) + strlen (
                    nameList[numberFiles - 1]->d_name) + 2,
                    "%s/%s", storageFolder, nameList[numberFiles - 1]->d_name);
        struct LogEntry *entry = getEntryFromFile (fileName);
        id = entry->id + 1;
        freeEntry (entry);
        free (fileName);
    }
    for (i = 0; i < numberFiles; i++)
    {
        free(nameList[i]);
    }
    free (nameList);
    return id;
}

variable_types: struct dirent { unsigned long d_ino; long d_off; unsigned short d_reclen; unsigned char d_type; char d_name[256]; } **

variable_types:
numberFiles
unsigned long

variable_types: id int

variable_types: i int


rm

decompiled_code:
__int64 __fastcall rm(unsigned int a1)
{
printf("Rm operation, deleting log id %d\n", a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1)
{
printf("Rm operation, deleting log id %d\n", a1);
return 0LL;
}

original_code:
int
rm (uint id)
{
    printf ("Rm operation, deleting log id %d\n", id);
    return 0;
}

canonical_original_code:
int rm(unsigned int id)
{
    printf ("Rm operation, deleting log id %d\n", id);
    return 0;
}

variable_types:
id
unsigned int