main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
_QWORD *v4; // r13
__int64 v5; // r12
__int64 v6; // rbp
__int64 v7; // r14
unsigned int v8; // ebx
unsigned int v9; // er15
unsigned int v10; // er9
int v11; // ebx
__int64 v12; // rdx
int v13; // eax
int v14; // ebx
__int64 i; // rsi
__int64 v17; // [rsp+8h] [rbp-40h]

v3 = wtssynth_create(argc, argv, envp);
if ( !v3 )
_assert_fail("w != NULL", "wtssynth_test.c", 0x7Fu, "main");
v4 = (_QWORD *)v3;
v5 = audiobuf_create(352800LL, 2LL, 44100LL, 16LL);
if ( !v5 )
_assert_fail("a != NULL", "wtssynth_test.c", 0x82u, "main");
v6 = nrsynth_create();
if ( !v6 )
_assert_fail("n != NULL", "wtssynth_test.c", 0x85u, "main");
v7 = sdelay_create();
if ( !v7 )
_assert_fail("s != NULL", "wtssynth_test.c", 0x88u, "main");
wtsconfig_programchange(*v4, 8LL);
g_current_time = 0;
seventh_midi((__int64)v4, v5, key_C4, key_E5, key_G5, key_B5, 0xAC44u);
v8 = key_C6;
v9 = key_F5;
seventh_midi((__int64)v4, v5, key_D4, key_F5, key_A5, key_C6, 0xAC44u);
seventh_midi((__int64)v4, v5, key_E4, key_G5, key_B5, key_D6, 0xAC44u);
seventh_midi((__int64)v4, v5, key_Dp4, key_G5, key_Ap5, key_D6, 0xAC44u);
seventh_midi((__int64)v4, v5, key_D4, v9, key_A5, v8, 0x15888u);
v10 = v8;
v11 = 0;
seventh_midi((__int64)v4, v5, key_Cp4, v9, key_Gp5, v10, 0x15888u);
v12 = v17;
g_current_time = 0;
do
{
v13 = v11++ & 1;
nrsynth_play(v6, v13 == 0 ? 35 : 38, v12, 1.0);
nrsynth_render(v6, v5, (unsigned int)g_current_time, 22050LL);
g_current_time += 22050;
}
while ( v11 != 16 );
g_current_time = 0;
v14 = 0;
for ( i = 42LL; ; i = 4 * (unsigned int)((v14 & 3) == 2) + 42 )
{
++v14;
nrsynth_play(v6, i, v12, 1.0);
nrsynth_render(v6, v5, (unsigned int)g_current_time, 11025LL);
g_current_time += 11025;
if ( v14 == 32 )
break;
}
sdelay_set_drywet(v7, 0.80000001, 0.5);
sdelay_render(v7, v5, 0LL, 352800LL);
audiobuf_save_header(v5, "test.wav");
audiobuf_save_append(v5, "test.wav");
sdelay_destroy(v7);
nrsynth_destroy(v6);
audiobuf_destroy(v5);
wtssynth_destroy(v4);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
_QWORD *v4; // r13
__int64 v5; // r12
__int64 v6; // rbp
__int64 v7; // r14
unsigned int v8; // ebx
unsigned int v9; // er15
unsigned int v10; // er9
int v11; // ebx
__int64 v12; // rdx
int v13; // eax
int v14; // ebx
__int64 i; // rsi
__int64 v17; // [rsp+8h] [rbp-40h]

v3 = wtssynth_create(argc, argv, envp);
if ( !v3 )
_assert_fail("w != NULL", "wtssynth_test.c", 0x7Fu, "main");
v4 = (_QWORD *)v3;
v5 = audiobuf_create(352800LL, 2LL, 44100LL, 16LL);
if ( !v5 )
_assert_fail("a != NULL", "wtssynth_test.c", 0x82u, "main");
v6 = nrsynth_create();
if ( !v6 )
_assert_fail("n != NULL", "wtssynth_test.c", 0x85u, "main");
v7 = sdelay_create();
if ( !v7 )
_assert_fail("s != NULL", "wtssynth_test.c", 0x88u, "main");
wtsconfig_programchange(*v4, 8LL);
g_current_time = 0;
func1((__int64)v4, v5, key_C4, key_E5, key_G5, key_B5, 0xAC44u);
v8 = key_C6;
v9 = key_F5;
func1((__int64)v4, v5, key_D4, key_F5, key_A5, key_C6, 0xAC44u);
func1((__int64)v4, v5, key_E4, key_G5, key_B5, key_D6, 0xAC44u);
func1((__int64)v4, v5, key_Dp4, key_G5, key_Ap5, key_D6, 0xAC44u);
func1((__int64)v4, v5, key_D4, v9, key_A5, v8, 0x15888u);
v10 = v8;
v11 = 0;
func1((__int64)v4, v5, key_Cp4, v9, key_Gp5, v10, 0x15888u);
v12 = v17;
g_current_time = 0;
do
{
v13 = v11++ & 1;
nrsynth_play(v6, v13 == 0 ? 35 : 38, v12, 1.0);
nrsynth_render(v6, v5, (unsigned int)g_current_time, 22050LL);
g_current_time += 22050;
}
while ( v11 != 16 );
g_current_time = 0;
v14 = 0;
for ( i = 42LL; ; i = 4 * (unsigned int)((v14 & 3) == 2) + 42 )
{
++v14;
nrsynth_play(v6, i, v12, 1.0);
nrsynth_render(v6, v5, (unsigned int)g_current_time, 11025LL);
g_current_time += 11025;
if ( v14 == 32 )
break;
}
sdelay_set_drywet(v7, 0.80000001, 0.5);
sdelay_render(v7, v5, 0LL, 352800LL);
audiobuf_save_header(v5, "test.wav");
audiobuf_save_append(v5, "test.wav");
sdelay_destroy(v7);
nrsynth_destroy(v6);
audiobuf_destroy(v5);
wtssynth_destroy(v4);
return 0;
}

original_code:
int main(int argc, char *argv[]){
  int i;
  wtssynth_t *w;
  audiobuf_t *a;
  nrsynth_t *n;
  sdelay_t *s;
  int delta = 44100;
  w = wtssynth_create();
  ((void) sizeof ((w != ((void *)0)) ? 1 : 0), __extension__ ({ if (w != ((void *)0)) ; else __assert_fail ("w != NULL", "wtssynth_test.c", 127, __extension__ __PRETTY_FUNCTION__); }));
  a = audiobuf_create(44100 * 8, 2, 44100, 16);;
  ((void) sizeof ((a != ((void *)0)) ? 1 : 0), __extension__ ({ if (a != ((void *)0)) ; else __assert_fail ("a != NULL", "wtssynth_test.c", 130, __extension__ __PRETTY_FUNCTION__); }));
  n = nrsynth_create();
  ((void) sizeof ((n != ((void *)0)) ? 1 : 0), __extension__ ({ if (n != ((void *)0)) ; else __assert_fail ("n != NULL", "wtssynth_test.c", 133, __extension__ __PRETTY_FUNCTION__); }));
  s = sdelay_create();
  ((void) sizeof ((s != ((void *)0)) ? 1 : 0), __extension__ ({ if (s != ((void *)0)) ; else __assert_fail ("s != NULL", "wtssynth_test.c", 136, __extension__ __PRETTY_FUNCTION__); }));
  wtsconfig_programchange(w->config, 8);
  g_current_time = 0;
  seventh_midi(w, a, key_C4, key_E5, key_G5, key_B5, delta);
  seventh_midi(w, a, key_D4, key_F5, key_A5, key_C6, delta);
  seventh_midi(w, a, key_E4, key_G5, key_B5, key_D6, delta);
  seventh_midi(w, a, key_Dp4, key_G5, key_Ap5, key_D6, delta);
  seventh_midi(w, a, key_D4, key_F5, key_A5, key_C6, delta*2);
  seventh_midi(w, a, key_Cp4, key_F5, key_Gp5, key_C6, delta*2);
  g_current_time = 0;
  for(i = 0; i < 16; i++){
    nrsynth_play(n, i%2 == 0 ? 35 : 38, 1.0);
    nrsynth_render(n, a, g_current_time, 44100 * 8/16);
    g_current_time += 44100 * 8/16;
  }
  g_current_time = 0;
  for(i = 0; i < 32; i++){
    nrsynth_play(n, i%4 == 2 ? 46 : 42, 1.0);
    nrsynth_render(n, a, g_current_time, 44100 * 8/32);
    g_current_time += 44100 * 8/32;
  }
  sdelay_set_drywet(s, 0.8, 0.5);
  sdelay_render(s, a, 0, 44100 * 8);
  audiobuf_save_header(a, "test.wav");audiobuf_save_append(a, "test.wav");
  sdelay_destroy(s);
  nrsynth_destroy(n);
  audiobuf_destroy(a);
  wtssynth_destroy(w);
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[]){
  int i;
  struct wtssynth_t *w;
  struct audiobuf_t *a;
  struct nrsynth_t *n;
  struct sdelay_t *s;
  int delta = 44100;
  w = wtssynth_create();
  ((void) sizeof ((w != ((void *)0)) ? 1 : 0), __extension__ ({ if (w != ((void *)0)) ; else __assert_fail ("w != NULL", "wtssynth_test.c", 127, __extension__ __PRETTY_FUNCTION__); }));
  a = audiobuf_create(44100 * 8, 2, 44100, 16);;
  ((void) sizeof ((a != ((void *)0)) ? 1 : 0), __extension__ ({ if (a != ((void *)0)) ; else __assert_fail ("a != NULL", "wtssynth_test.c", 130, __extension__ __PRETTY_FUNCTION__); }));
  n = nrsynth_create();
  ((void) sizeof ((n != ((void *)0)) ? 1 : 0), __extension__ ({ if (n != ((void *)0)) ; else __assert_fail ("n != NULL", "wtssynth_test.c", 133, __extension__ __PRETTY_FUNCTION__); }));
  s = sdelay_create();
  ((void) sizeof ((s != ((void *)0)) ? 1 : 0), __extension__ ({ if (s != ((void *)0)) ; else __assert_fail ("s != NULL", "wtssynth_test.c", 136, __extension__ __PRETTY_FUNCTION__); }));
  wtsconfig_programchange(w->config, 8);
  g_current_time = 0;
  seventh_midi(w, a, key_C4, key_E5, key_G5, key_B5, delta);
  seventh_midi(w, a, key_D4, key_F5, key_A5, key_C6, delta);
  seventh_midi(w, a, key_E4, key_G5, key_B5, key_D6, delta);
  seventh_midi(w, a, key_Dp4, key_G5, key_Ap5, key_D6, delta);
  seventh_midi(w, a, key_D4, key_F5, key_A5, key_C6, delta*2);
  seventh_midi(w, a, key_Cp4, key_F5, key_Gp5, key_C6, delta*2);
  g_current_time = 0;
  for(i = 0; i < 16; i++){
    nrsynth_play(n, i%2 == 0 ? 35 : 38, 1.0);
    nrsynth_render(n, a, g_current_time, 44100 * 8/16);
    g_current_time += 44100 * 8/16;
  }
  g_current_time = 0;
  for(i = 0; i < 32; i++){
    nrsynth_play(n, i%4 == 2 ? 46 : 42, 1.0);
    nrsynth_render(n, a, g_current_time, 44100 * 8/32);
    g_current_time += 44100 * 8/32;
  }
  sdelay_set_drywet(s, 0.8, 0.5);
  sdelay_render(s, a, 0, 44100 * 8);
  audiobuf_save_header(a, "test.wav");audiobuf_save_append(a, "test.wav");
  sdelay_destroy(s);
  nrsynth_destroy(n);
  audiobuf_destroy(a);
  wtssynth_destroy(w);
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct nrsynth_t { struct nrsynth_inst_t { int status; unsigned int elapsed; float velocity; unsigned int attack_time; unsigned int sustain_time; unsigned int release_time; int pitchmode; float rnd; int key; } inst[4]; int keymap[128]; float expression; float panpot; float volume; float volume_total_l; float volume_total_r; } *

variable_types: struct sdelay_t { struct sdelay_allpass_t { struct audiobuf_t { int n_channels; int sampling_rate; int qbit; float **data; int *index; int size; } *tap; float gain; float current_output_l; float current_output_r; } *apfilter[3]; float gain; float dry; float wet; float current_output_l; float current_output_r; } *

variable_types: delta int

user_defined_types: struct sdelay_t { struct sdelay_allpass_t *apfilter[3]; float gain; float dry; float wet; float current_output_l; float current_output_r; }

user_defined_types: struct sdelay_allpass_t { struct audiobuf_t *tap; float gain; float current_output_l; float current_output_r; }


decompiled_code:
void __fastcall audiobuf_destroy(void *ptr)
{
_QWORD *v2; // rdi
void *v3; // r8
__int64 v4; // rbx
void *v5; // rdi

if ( ptr )
{
v2 = (_QWORD *)*((_QWORD *)ptr + 2);
if ( v2 )
{
v3 = (void *)*((_QWORD *)ptr + 3);
if ( v3 )
{
if ( *(int *)ptr > 0 )
{
v4 = 0LL;
while ( 1 )
{
v5 = (void *)v2[v4];
if ( !v5 )
break;
free(v5);
if ( *(_DWORD *)ptr <= (int)++v4 )
break;
v2 = (_QWORD *)*((_QWORD *)ptr + 2);
}
v3 = (void *)*((_QWORD *)ptr + 3);
}
free(v3);
v2 = (_QWORD *)*((_QWORD *)ptr + 2);
}
free(v2);
}
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func0(void *ptr)
{
_QWORD *v2; // rdi
void *v3; // r8
__int64 v4; // rbx
void *v5; // rdi

if ( ptr )
{
v2 = (_QWORD *)*((_QWORD *)ptr + 2);
if ( v2 )
{
v3 = (void *)*((_QWORD *)ptr + 3);
if ( v3 )
{
if ( *(int *)ptr > 0 )
{
v4 = 0LL;
while ( 1 )
{
v5 = (void *)v2[v4];
if ( !v5 )
break;
free(v5);
if ( *(_DWORD *)ptr <= (int)++v4 )
break;
v2 = (_QWORD *)*((_QWORD *)ptr + 2);
}
v3 = (void *)*((_QWORD *)ptr + 3);
}
free(v3);
v2 = (_QWORD *)*((_QWORD *)ptr + 2);
}
free(v2);
}
free(ptr);
}
}

original_code:
void audiobuf_destroy(audiobuf_t *a){
  int i;
  if(a == ((void *)0)) return;
  if(a->data == ((void *)0)) goto free1;
  if(a->index == ((void *)0)) goto free2;
  for(i = 0; i < a->n_channels; i ++){
    if(a->data[i] == ((void *)0)){
      goto free3;
    }else{
      free(a->data[i]);
    }
  }
 free3:
  free(a->index);
 free2:
  free(a->data);
 free1:
  free(a);
}

canonical_original_code:
void audiobuf_destroy(struct audiobuf_t *a){
  int i;
  if(a == ((void *)0)) return;
  if(a->data == ((void *)0)) goto free1;
  if(a->index == ((void *)0)) goto free2;
  for(i = 0; i < a->n_channels; i ++){
    if(a->data[i] == ((void *)0)){
      goto free3;
    }else{
      free(a->data[i]);
    }
  }
 free3:
  free(a->index);
 free2:
  free(a->data);
 free1:
  free(a);
}

variable_types: struct audiobuf_t { int n_channels; int sampling_rate; int qbit; float **data; int *index; int size; } *

variable_types: i int

return_type: void


decompiled_code:
__int64 __fastcall audiobuf_copy(unsigned int *a1, unsigned int *a2)
{
int v2; // er11
__int64 result; // rax
__int64 v5; // rdi
__int64 v6; // r8
__int64 v7; // rcx
__int64 v8; // rdx

v2 = a1[8];
result = *a1;
if ( (int)a2[8] <= v2 )
v2 = a2[8];
if ( (int)*a2 <= (int)result )
result = *a2;
if ( (int)result > 0 )
{
result = (int)result;
v5 = 0LL;
v6 = 8LL * (int)result;
do
{
if ( v2 > 0 )
{
v7 = *(_QWORD *)(*((_QWORD *)a1 + 2) + v5);
v8 = *(_QWORD *)(*((_QWORD *)a2 + 2) + v5);
for ( result = 0LL; result != v2; ++result )
*(_DWORD *)(v8 + 4 * result) = *(_DWORD *)(v7 + 4 * result);
}
v5 += 8LL;
}
while ( v5 != v6 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int *a1, unsigned int *a2)
{
int v2; // er11
__int64 result; // rax
__int64 v5; // rdi
__int64 v6; // r8
__int64 v7; // rcx
__int64 v8; // rdx

v2 = a1[8];
result = *a1;
if ( (int)a2[8] <= v2 )
v2 = a2[8];
if ( (int)*a2 <= (int)result )
result = *a2;
if ( (int)result > 0 )
{
result = (int)result;
v5 = 0LL;
v6 = 8LL * (int)result;
do
{
if ( v2 > 0 )
{
v7 = *(_QWORD *)(*((_QWORD *)a1 + 2) + v5);
v8 = *(_QWORD *)(*((_QWORD *)a2 + 2) + v5);
for ( result = 0LL; result != v2; ++result )
*(_DWORD *)(v8 + 4 * result) = *(_DWORD *)(v7 + 4 * result);
}
v5 += 8LL;
}
while ( v5 != v6 );
}
return result;
}

original_code:
void audiobuf_copy(audiobuf_t *src, audiobuf_t *dst){
  int i,j;
  int maxdata = src->size < dst->size ? src->size : dst->size;
  int maxchannel = src->n_channels < dst->n_channels ? src->n_channels : dst->n_channels;
  for(i = 0; i < maxchannel; i ++){
    for(j = 0; j < maxdata; j ++){
      dst->data[i][j] = src->data[i][j];
    }
  }
}

canonical_original_code:
void audiobuf_copy(struct audiobuf_t *src, struct audiobuf_t *dst){
  int i; int j;
  int maxdata = src->size < dst->size ? src->size : dst->size;
  int maxchannel = src->n_channels < dst->n_channels ? src->n_channels : dst->n_channels;
  for(i = 0; i < maxchannel; i ++){
    for(j = 0; j < maxdata; j ++){
      dst->data[i][j] = src->data[i][j];
    }
  }
}

variable_types:
dst
struct audiobuf_t *

variable_types: j int

variable_types: maxdata int

variable_types: maxchannel int

user_defined_types: struct audiobuf_t { int n_channels; int sampling_rate; int qbit; float **data; int *index; int size; }


decompiled_code:
__int64 __fastcall audoibuf_merge(unsigned int *a1, unsigned int *a2)
{
int v2; // er11
__int64 result; // rax
__int64 v5; // rdi
__int64 v6; // r8
__int64 v7; // rdx
__int64 v8; // rcx

v2 = a1[8];
result = *a1;
if ( (int)a2[8] <= v2 )
v2 = a2[8];
if ( (int)*a2 <= (int)result )
result = *a2;
if ( (int)result > 0 )
{
result = (int)result;
v5 = 0LL;
v6 = 8LL * (int)result;
do
{
if ( v2 > 0 )
{
v7 = *(_QWORD *)(*((_QWORD *)a2 + 2) + v5);
v8 = *(_QWORD *)(*((_QWORD *)a1 + 2) + v5);
for ( result = 0LL; result != v2; ++result )
*(float *)(v7 + 4 * result) = *(float *)(v7 + 4 * result) + *(float *)(v8 + 4 * result);
}
v5 += 8LL;
}
while ( v5 != v6 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int *a1, unsigned int *a2)
{
int v2; // er11
__int64 result; // rax
__int64 v5; // rdi
__int64 v6; // r8
__int64 v7; // rdx
__int64 v8; // rcx

v2 = a1[8];
result = *a1;
if ( (int)a2[8] <= v2 )
v2 = a2[8];
if ( (int)*a2 <= (int)result )
result = *a2;
if ( (int)result > 0 )
{
result = (int)result;
v5 = 0LL;
v6 = 8LL * (int)result;
do
{
if ( v2 > 0 )
{
v7 = *(_QWORD *)(*((_QWORD *)a2 + 2) + v5);
v8 = *(_QWORD *)(*((_QWORD *)a1 + 2) + v5);
for ( result = 0LL; result != v2; ++result )
*(float *)(v7 + 4 * result) = *(float *)(v7 + 4 * result) + *(float *)(v8 + 4 * result);
}
v5 += 8LL;
}
while ( v5 != v6 );
}
return result;
}

original_code:
void audoibuf_merge(audiobuf_t *src, audiobuf_t *dst){
  int i,j;
  int maxdata = src->size < dst->size ? src->size : dst->size;
  int maxchannel = src->n_channels < dst->n_channels ? src->n_channels : dst->n_channels;
  for(i = 0; i < maxchannel; i ++){
    for(j = 0; j < maxdata; j ++){
      dst->data[i][j] += src->data[i][j];
    }
  }
}

canonical_original_code:
void audoibuf_merge(struct audiobuf_t *src, struct audiobuf_t *dst){
  int i; int j;
  int maxdata = src->size < dst->size ? src->size : dst->size;
  int maxchannel = src->n_channels < dst->n_channels ? src->n_channels : dst->n_channels;
  for(i = 0; i < maxchannel; i ++){
    for(j = 0; j < maxdata; j ++){
      dst->data[i][j] += src->data[i][j];
    }
  }
}


decompiled_code:
__int64 __fastcall audiobuf_clear(unsigned int *a1)
{
__int64 result; // rax
__int64 v2; // rbx

result = *a1;
if ( (int)result > 0 )
{
v2 = 0LL;
do
{
memset(*(void **)(*((_QWORD *)a1 + 2) + 8 * v2), 0, 4LL * (int)a1[8]);
result = *((_QWORD *)a1 + 3);
*(_DWORD *)(result + 4 * v2++) = 0;
}
while ( (int)*a1 > (int)v2 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int *a1)
{
__int64 result; // rax
__int64 v2; // rbx

result = *a1;
if ( (int)result > 0 )
{
v2 = 0LL;
do
{
memset(*(void **)(*((_QWORD *)a1 + 2) + 8 * v2), 0, 4LL * (int)a1[8]);
result = *((_QWORD *)a1 + 3);
*(_DWORD *)(result + 4 * v2++) = 0;
}
while ( (int)*a1 > (int)v2 );
}
return result;
}

original_code:
void audiobuf_clear(audiobuf_t *a){
  int i;
  for(i = 0; i < a->n_channels; i++){
    memset(a->data[i], 0, sizeof(float) * a->size);
    a->index[i] = 0;
  }
}

canonical_original_code:
void audiobuf_clear(struct audiobuf_t *a){
  int i;
  for(i = 0; i < a->n_channels; i++){
    memset(a->data[i], 0, sizeof(float) * a->size);
    a->index[i] = 0;
  }
}


decompiled_code:
unsigned int *__fastcall audiobuf_duplicate(int *a1)
{
unsigned int *v1; // r12

v1 = audiobuf_create(a1[8], *a1, a1[1], a1[2]);
audiobuf_copy((unsigned int *)a1, v1);
return v1;
}

canonical_decompiled_code:
unsigned int *__fastcall func4(int *a1)
{
unsigned int *v1; // r12

v1 = audiobuf_create(a1[8], *a1, a1[1], a1[2]);
func1((unsigned int *)a1, v1);
return v1;
}

original_code:
audiobuf_t *audiobuf_duplicate(audiobuf_t *a){
  audiobuf_t *b;
  b = audiobuf_create(a->size, a->n_channels, a->sampling_rate, a->qbit);
  audiobuf_copy(a, b);
  return b;
}

canonical_original_code:
struct audiobuf_t *audiobuf_duplicate(struct audiobuf_t *a){
  struct audiobuf_t *b;
  b = audiobuf_create(a->size, a->n_channels, a->sampling_rate, a->qbit);
  audiobuf_copy(a, b);
  return b;
}

variable_types:
a
struct audiobuf_t *

return_type: struct audiobuf_t { int n_channels; int sampling_rate; int qbit; float **data; int *index; int size; } *


decompiled_code:
__int64 __fastcall audiobuf_rewind(unsigned int *a1)
{
__int64 result; // rax
__int64 v2; // rdx

result = *a1;
if ( (int)result > 0 )
{
v2 = *((_QWORD *)a1 + 3);
result = 0LL;
do
*(_DWORD *)(v2 + 4 * result++) = 0;
while ( (int)*a1 > (int)result );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int *a1)
{
__int64 result; // rax
__int64 v2; // rdx

result = *a1;
if ( (int)result > 0 )
{
v2 = *((_QWORD *)a1 + 3);
result = 0LL;
do
*(_DWORD *)(v2 + 4 * result++) = 0;
while ( (int)*a1 > (int)result );
}
return result;
}

original_code:
void audiobuf_rewind(audiobuf_t *a){
  int i;
  for(i = 0; i < a->n_channels; i++){
    a->index[i] = 0;
  }
}

canonical_original_code:
void audiobuf_rewind(struct audiobuf_t *a){
  int i;
  for(i = 0; i < a->n_channels; i++){
    a->index[i] = 0;
  }
}


decompiled_code:
__int64 __fastcall audiobuf_seek(unsigned int *a1, int a2)
{
__int64 result; // rax
__int64 v3; // rdx

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

canonical_decompiled_code:
__int64 __fastcall func6(unsigned int *a1, int a2)
{
__int64 result; // rax
__int64 v3; // rdx

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

original_code:
void audiobuf_seek(audiobuf_t *a, int pos){
  int i;
  for(i = 0; i < a->n_channels; i++){
    a->index[i] = pos;
  }
}

canonical_original_code:
void audiobuf_seek(struct audiobuf_t *a, int pos){
  int i;
  for(i = 0; i < a->n_channels; i++){
    a->index[i] = pos;
  }
}

variable_types: pos int


decompiled_code:
float __fastcall audiobuf_get(__int64 a1, int a2)
{
return *(float *)(*(_QWORD *)(*(_QWORD *)(a1 + 16) + 8LL * a2) + 4LL * *(int *)(*(_QWORD *)(a1 + 24) + 4LL * a2));
}

original_code:
float audiobuf_get(audiobuf_t *a, int channel){
  return a->data[channel][a->index[channel]];
}

canonical_original_code:
float audiobuf_get(struct audiobuf_t *a, int channel){
  return a->data[channel][a->index[channel]];
}

variable_types: channel int

return_type: float


decompiled_code:
__int64 __fastcall audiobuf_wrap(unsigned int *a1)
{
__int64 result; // rax
__int64 v2; // rdx

result = *a1;
if ( (int)result > 0 )
{
v2 = *((_QWORD *)a1 + 3);
result = 0LL;
do
{
if ( *(_DWORD *)(v2 + 4 * result) >= (signed int)a1[8] )
*(_DWORD *)(v2 + 4 * result) = 0;
++result;
}
while ( (int)*a1 > (int)result );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned int *a1)
{
__int64 result; // rax
__int64 v2; // rdx

result = *a1;
if ( (int)result > 0 )
{
v2 = *((_QWORD *)a1 + 3);
result = 0LL;
do
{
if ( *(_DWORD *)(v2 + 4 * result) >= (signed int)a1[8] )
*(_DWORD *)(v2 + 4 * result) = 0;
++result;
}
while ( (int)*a1 > (int)result );
}
return result;
}

original_code:
void audiobuf_wrap(audiobuf_t *a){
  int i;
  for(i = 0 ; i < a->n_channels; i ++){
    if(a->index[i] >= a->size) a->index[i] = 0;
  }
}

canonical_original_code:
void audiobuf_wrap(struct audiobuf_t *a){
  int i;
  for(i = 0 ; i < a->n_channels; i ++){
    if(a->index[i] >= a->size) a->index[i] = 0;
  }
}


decompiled_code:
__int64 __fastcall sdelay_allpass_process(float *a1, float a2, float a3)
{
double v4; // xmm1_8
double v5; // xmm0_8
__int64 v6; // rdi
float v8; // [rsp+8h] [rbp-20h]
float v9; // [rsp+Ch] [rbp-1Ch]
float v10; // [rsp+10h] [rbp-18h]

v8 = a1[2];
v9 = audiobuf_get(*(_QWORD *)a1, 0LL);
*(_QWORD *)&v4 = LODWORD(a2);
v10 = audiobuf_get(*(_QWORD *)a1, 1LL);
*(float *)&v4 = a2 + (float)(v8 * v9);
audiobuf_write(*(_QWORD *)a1, 0LL, v4);
*(_QWORD *)&v5 = LODWORD(a3);
*(float *)&v5 = a3 + (float)(v8 * v10);
audiobuf_write(*(_QWORD *)a1, 1LL, v5);
v6 = *(_QWORD *)a1;
a1[3] = v9 - (float)(*(float *)&v4 * v8);
a1[4] = v10 - (float)(*(float *)&v5 * v8);
return audiobuf_wrap(v6);
}

canonical_decompiled_code:
__int64 __fastcall func0(float *a1, float a2, float a3)
{
double v4; // xmm1_8
double v5; // xmm0_8
__int64 v6; // rdi
float v8; // [rsp+8h] [rbp-20h]
float v9; // [rsp+Ch] [rbp-1Ch]
float v10; // [rsp+10h] [rbp-18h]

v8 = a1[2];
v9 = audiobuf_get(*(_QWORD *)a1, 0LL);
*(_QWORD *)&v4 = LODWORD(a2);
v10 = audiobuf_get(*(_QWORD *)a1, 1LL);
*(float *)&v4 = a2 + (float)(v8 * v9);
audiobuf_write(*(_QWORD *)a1, 0LL, v4);
*(_QWORD *)&v5 = LODWORD(a3);
*(float *)&v5 = a3 + (float)(v8 * v10);
audiobuf_write(*(_QWORD *)a1, 1LL, v5);
v6 = *(_QWORD *)a1;
a1[3] = v9 - (float)(*(float *)&v4 * v8);
a1[4] = v10 - (float)(*(float *)&v5 * v8);
return audiobuf_wrap(v6);
}

original_code:
static void sdelay_allpass_process(sdelay_allpass_t *a, float l, float r){
  float g = a->gain;
  float dl,dr;
  dl = audiobuf_get(a->tap, 0);
  dr = audiobuf_get(a->tap, 1);
  audiobuf_write(a->tap, 0, l+dl*g);
  audiobuf_write(a->tap, 1, r+dr*g);
  a->current_output_l = dl - (l+dl*g)*g;
  a->current_output_r = dr - (r+dr*g)*g;
  audiobuf_wrap(a->tap);
}

canonical_original_code:
static void sdelay_allpass_process(struct sdelay_allpass_t *a, float l, float r){
  float g = a->gain;
  float dl; float dr;
  dl = audiobuf_get(a->tap, 0);
  dr = audiobuf_get(a->tap, 1);
  audiobuf_write(a->tap, 0, l+dl*g);
  audiobuf_write(a->tap, 1, r+dr*g);
  a->current_output_l = dl - (l+dl*g)*g;
  a->current_output_r = dr - (r+dr*g)*g;
  audiobuf_wrap(a->tap);
}

variable_types: struct sdelay_allpass_t { struct audiobuf_t { int n_channels; int sampling_rate; int qbit; float **data; int *index; int size; } *tap; float gain; float current_output_l; float current_output_r; } *

variable_types: l float

variable_types: r float

variable_types: g float

variable_types: dl float

variable_types: dr float


decompiled_code:
__int64 __fastcall sdelay_process(float **a1, float a2, float a3)
{
sdelay_allpass_process(*a1, a2, a3);
sdelay_allpass_process(a1[1], (*a1)[3], (*a1)[4]);
return sdelay_allpass_process(a1[2], a1[1][3], a1[1][4]);
}

original_code:
static void sdelay_process(sdelay_t *s, float l, float r){
  int i;
  sdelay_allpass_process(s->apfilter[0], l, r);
  for(i = 1; i < 3; i++){
    sdelay_allpass_process(s->apfilter[i],
      s->apfilter[i-1]->current_output_l,
      s->apfilter[i-1]->current_output_r);
  }
}

canonical_original_code:
static void sdelay_process(struct sdelay_t *s, float l, float r){
  int i;
  sdelay_allpass_process(s->apfilter[0], l, r);
  for(i = 1; i < 3; i++){
    sdelay_allpass_process(s->apfilter[i],
      s->apfilter[i-1]->current_output_l,
      s->apfilter[i-1]->current_output_r);
  }
}

variable_types: struct sdelay_t { struct sdelay_allpass_t { struct audiobuf_t { int n_channels; int sampling_rate; int qbit; float **data; int *index; int size; } *tap; float gain; float current_output_l; float current_output_r; } *apfilter[3]; float gain; float dry; float wet; float current_output_l; float current_output_r; } *


decompiled_code:
void __fastcall sdelay_allpass_destroy(void *ptr)
{
audiobuf_destroy(*(_QWORD *)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func5(void *ptr)
{
audiobuf_destroy(*(_QWORD *)ptr);
free(ptr);
}

original_code:
void sdelay_allpass_destroy(sdelay_allpass_t *a){
  audiobuf_destroy(a->tap);
  free(a);
}

canonical_original_code:
void sdelay_allpass_destroy(struct sdelay_allpass_t *a){
  audiobuf_destroy(a->tap);
  free(a);
}


original_code:
sdelay_t *sdelay_create(){
  sdelay_t *s;
  int i;
  s = (sdelay_t *)malloc(sizeof(sdelay_t));
  memset(s, 0, sizeof(sdelay_t));
  for(i = 0 ; i < 3; i ++){
    s->apfilter[i] = sdelay_allpass_create(tapdefs[i], gaindefs[i]);
  }
  s->dry = 0.0;
  s->wet = 1.0;
  return s;
}

canonical_original_code:
struct sdelay_t *sdelay_create(){
  struct sdelay_t *s;
  int i;
  s = (struct sdelay_t *)malloc(sizeof(sdelay_t));
  memset(s, 0, sizeof(sdelay_t));
  for(i = 0 ; i < 3; i ++){
    s->apfilter[i] = sdelay_allpass_create(tapdefs[i], gaindefs[i]);
  }
  s->dry = 0.0;
  s->wet = 1.0;
  return s;
}

return_type: struct sdelay_t { struct sdelay_allpass_t { struct audiobuf_t { int n_channels; int sampling_rate; int qbit; float **data; int *index; int size; } *tap; float gain; float current_output_l; float current_output_r; } *apfilter[3]; float gain; float dry; float wet; float current_output_l; float current_output_r; } *


decompiled_code:
void __fastcall sdelay_destroy(void *ptr)
{
sdelay_allpass_destroy(*(void **)ptr);
sdelay_allpass_destroy(*((void **)ptr + 1));
sdelay_allpass_destroy(*((void **)ptr + 2));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func7(void *ptr)
{
func5(*(void **)ptr);
func5(*((void **)ptr + 1));
func5(*((void **)ptr + 2));
free(ptr);
}

original_code:
void sdelay_destroy(sdelay_t *s){
  int i;
  for(i = 0 ; i < 3; i ++){
    sdelay_allpass_destroy(s->apfilter[i]);
  }
  free(s);
}

canonical_original_code:
void sdelay_destroy(struct sdelay_t *s){
  int i;
  for(i = 0 ; i < 3; i ++){
    sdelay_allpass_destroy(s->apfilter[i]);
  }
  free(s);
}


decompiled_code:
void __fastcall sdelay_set_drywet(__int64 a1, float a2, float a3)
{
*(float *)(a1 + 28) = a2;
*(float *)(a1 + 32) = a3;
}

canonical_decompiled_code:
void __fastcall func8(__int64 a1, float a2, float a3)
{
*(float *)(a1 + 28) = a2;
*(float *)(a1 + 32) = a3;
}

original_code:
void sdelay_set_drywet(sdelay_t *s, float dry, float wet){
  s->dry = dry;
  s->wet = wet;
}

canonical_original_code:
void sdelay_set_drywet(struct sdelay_t *s, float dry, float wet){
  s->dry = dry;
  s->wet = wet;
}

variable_types: dry float

variable_types: wet float


decompiled_code:
__int64 __fastcall sdelay_render(__int64 a1, __int64 a2, unsigned int a3, int a4, __m128i a5)
{
__int64 result; // rax
int v8; // er13
float v9; // er14
double v10; // xmm0_8
__int64 v11; // rdi
double v12; // xmm3_8
__m128i v13; // xmm2
float v14; // [rsp+8h] [rbp-30h]
float v15; // [rsp+Ch] [rbp-2Ch]

result = audiobuf_seek(a2, a3);
if ( a4 > 0 )
{
v8 = 0;
do
{
++v8;
*(float *)a5.m128i_i32 = audiobuf_get(a2, 0LL);
v9 = COERCE_FLOAT(_mm_cvtsi128_si32(a5));
v15 = audiobuf_get(a2, 1LL);
*(_QWORD *)&v10 = _mm_cvtsi32_si128(LODWORD(v9)).m128i_u64[0];
sdelay_process((float **)a1, *(float *)&v10, v15);
v11 = *(_QWORD *)(a1 + 16);
*(float *)&v10 = sdelay_current_output_l_isra_0(v11);
v12 = v10;
v14 = sdelay_current_output_r_isra_0(v11);
HIDWORD(v10) = HIDWORD(v12);
*(float *)&v10 = (float)(*(float *)&v12 * *(float *)(a1 + 32)) + (float)(v9 * *(float *)(a1 + 28));
audiobuf_write(a2, 0LL, v10);
v13 = (__m128i)LODWORD(v14);
*(float *)v13.m128i_i32 = v14 * *(float *)(a1 + 32);
a5 = v13;
*(float *)a5.m128i_i32 = *(float *)v13.m128i_i32 + (float)(v15 * *(float *)(a1 + 28));
result = audiobuf_write(a2, 1LL, *(double *)a5.m128i_i64);
}
while ( a4 != v8 );
}
return result;
}

original_code:
void sdelay_render(sdelay_t *s, audiobuf_t *a, int start, int size){
  int i;
  float il, ir;
  float ol, or;
  audiobuf_seek(a, start);
  for(i = 0; i < size; i ++){
    il = audiobuf_get(a, 0);
    ir = audiobuf_get(a, 1);
    sdelay_process(s, il, ir);
    ol = sdelay_current_output_l(s);
    or = sdelay_current_output_r(s);
    audiobuf_write(a, 0, ol*s->wet + il*s->dry);
    audiobuf_write(a, 1, or*s->wet + ir*s->dry);
  }
}

canonical_original_code:
void sdelay_render(struct sdelay_t *s, struct audiobuf_t *a, int start, int size){
  int i;
  float il; float ir;
  float ol; float or;
  audiobuf_seek(a, start);
  for(i = 0; i < size; i ++){
    il = audiobuf_get(a, 0);
    ir = audiobuf_get(a, 1);
    sdelay_process(s, il, ir);
    ol = sdelay_current_output_l(s);
    or = sdelay_current_output_r(s);
    audiobuf_write(a, 0, ol*s->wet + il*s->dry);
    audiobuf_write(a, 1, or*s->wet + ir*s->dry);
  }
}

variable_types: start int

variable_types: size int

variable_types: i int

variable_types: il float

variable_types: ir float

variable_types: ol float

variable_types: or float


decompiled_code:
void __fastcall soundmodule_chamber_init(__int64 a1, int a2)
{
*(_DWORD *)a1 = a2;
if ( a2 )
{
if ( a2 == 1 )
*(_QWORD *)(a1 + 8) = nrsynth_create();
}
else
{
*(_QWORD *)(a1 + 8) = wtssynth_create();
}
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1, int a2)
{
*(_DWORD *)a1 = a2;
if ( a2 )
{
if ( a2 == 1 )
*(_QWORD *)(a1 + 8) = nrsynth_create();
}
else
{
*(_QWORD *)(a1 + 8) = wtssynth_create();
}
}

original_code:
void soundmodule_chamber_init(soundmodule_chamber_t *c, int type){
  c->type = type;
  switch(type){
  case SOUNDMODULE_CHAMBER_TYPE_WTS:
    c->synth = wtssynth_create();
    break;
  case SOUNDMODULE_CHAMBER_TYPE_NR:
    c->synth = nrsynth_create();
    break;
  default:
    ;
  }
}

canonical_original_code:
void soundmodule_chamber_init(struct soundmodule_chamber_t *c, int type){
  c->type = type;
  switch(type){
  case SOUNDMODULE_CHAMBER_TYPE_WTS:
    c->synth = wtssynth_create();
    break;
  case SOUNDMODULE_CHAMBER_TYPE_NR:
    c->synth = nrsynth_create();
    break;
  default:
    ;
  }
}

variable_types: struct soundmodule_chamber_t { int type; void *synth; int is_muted; } *

variable_types: type int


decompiled_code:
char *soundmodule_create()
{
int v0; // ebx
char *v1; // r12
char *v2; // rbp
__int64 v3; // rdi
int v4; // esi

v0 = 0;
v1 = (char *)calloc(0x180uLL, 1uLL);
v2 = v1;
do
{
v3 = (__int64)v2;
v4 = v0++ == 9;
v2 += 24;
soundmodule_chamber_init(v3, v4);
}
while ( v0 != 16 );
return v1;
}

original_code:
soundmodule_t *soundmodule_create(){
  soundmodule_t *s;
  int i;
  s = (soundmodule_t *)malloc(sizeof(soundmodule_t));
  memset(s, 0, sizeof(soundmodule_t));
  for(i = 0; i < 16; i ++){
    soundmodule_chamber_init(&s->chambers[i],
        i != 9 ? SOUNDMODULE_CHAMBER_TYPE_WTS : SOUNDMODULE_CHAMBER_TYPE_NR);
  }
  return s;
}

canonical_original_code:
struct soundmodule_t *soundmodule_create(){
  struct soundmodule_t *s;
  int i;
  s = (struct soundmodule_t *)malloc(sizeof(soundmodule_t));
  memset(s, 0, sizeof(soundmodule_t));
  for(i = 0; i < 16; i ++){
    soundmodule_chamber_init(&s->chambers[i],
        i != 9 ? SOUNDMODULE_CHAMBER_TYPE_WTS : SOUNDMODULE_CHAMBER_TYPE_NR);
  }
  return s;
}

variable_types: struct soundmodule_t { struct soundmodule_chamber_t { int type; void *synth; int is_muted; } chambers[16]; } *

user_defined_types: struct soundmodule_t { struct soundmodule_chamber_t chambers[16]; }


decompiled_code:
void __fastcall soundmodule_destroy(void *ptr)
{
_QWORD *v1; // rbp
int v2; // ebx

if ( ptr )
{
v1 = (char *)ptr + 8;
v2 = 0;
while ( 1 )
{
++v2;
if ( *v1 )
{
if ( v2 == 10 )
{
nrsynth_destroy();
goto LABEL_5;
}
wtssynth_destroy();
}
if ( v2 == 16 )
{
free(ptr);
return;
}
LABEL_5:
v1 += 3;
}
}
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
_QWORD *v1; // rbp
int v2; // ebx

if ( ptr )
{
v1 = (char *)ptr + 8;
v2 = 0;
while ( 1 )
{
++v2;
if ( *v1 )
{
if ( v2 == 10 )
{
nrsynth_destroy();
goto LABEL_5;
}
wtssynth_destroy();
}
if ( v2 == 16 )
{
free(ptr);
return;
}
LABEL_5:
v1 += 3;
}
}
}

original_code:
void soundmodule_destroy(soundmodule_t *s){
  int i;
  if(s == ((void *)0)) return;
  for(i = 0; i < 16; i ++){
    if(s->chambers[i].synth == ((void *)0)) continue;
    if(i != 9){
      wtssynth_destroy(s->chambers[i].synth);
    }else{
      nrsynth_destroy(s->chambers[i].synth);
    }
  }
  free(s);
}

canonical_original_code:
void soundmodule_destroy(struct soundmodule_t *s){
  int i;
  if(s == ((void *)0)) return;
  for(i = 0; i < 16; i ++){
    if(s->chambers[i].synth == ((void *)0)) continue;
    if(i != 9){
      wtssynth_destroy(s->chambers[i].synth);
    }else{
      nrsynth_destroy(s->chambers[i].synth);
    }
  }
  free(s);
}

user_defined_types: struct soundmodule_chamber_t { int type; void *synth; int is_muted; }


decompiled_code:
__int64 __fastcall soundmodule_dispatch_midi(__int64 a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // rsi
unsigned int *v4; // rax
__int64 v5; // rdi

result = midifile_current_event(a2);
if ( !*(_DWORD *)(result + 8) )
{
v3 = result;
v4 = (unsigned int *)(a1 + 24LL * *(int *)(result + 12));
v5 = *((_QWORD *)v4 + 1);
result = *v4;
if ( (_DWORD)result )
{
if ( (_DWORD)result == 1 )
result = nrsynth_midi(v5, v3);
}
else
{
result = wtssynth_midi(v5, v3);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // rsi
unsigned int *v4; // rax
__int64 v5; // rdi

result = midifile_current_event(a2);
if ( !*(_DWORD *)(result + 8) )
{
v3 = result;
v4 = (unsigned int *)(a1 + 24LL * *(int *)(result + 12));
v5 = *((_QWORD *)v4 + 1);
result = *v4;
if ( (_DWORD)result )
{
if ( (_DWORD)result == 1 )
result = nrsynth_midi(v5, v3);
}
else
{
result = wtssynth_midi(v5, v3);
}
}
return result;
}

original_code:
void soundmodule_dispatch_midi(soundmodule_t *s, midifile_t *mf){
  void *synth;
  midievent_t *e = midifile_current_event(mf);
  if(e->is_meta){
    ;
  }else{
    synth = s->chambers[e->channel].synth;
    switch(s->chambers[e->channel].type){
    case SOUNDMODULE_CHAMBER_TYPE_WTS:
      wtssynth_midi(synth, e);
      break;
    case SOUNDMODULE_CHAMBER_TYPE_NR:
      nrsynth_midi(synth, e);
      break;
    default:
      ;
    }
  }
}

canonical_original_code:
void soundmodule_dispatch_midi(struct soundmodule_t *s, struct midifile_t *mf){
  void *synth;
  struct midievent_t *e = midifile_current_event(mf);
  if(e->is_meta){
    ;
  }else{
    synth = s->chambers[e->channel].synth;
    switch(s->chambers[e->channel].type){
    case SOUNDMODULE_CHAMBER_TYPE_WTS:
      wtssynth_midi(synth, e);
      break;
    case SOUNDMODULE_CHAMBER_TYPE_NR:
      nrsynth_midi(synth, e);
      break;
    default:
      ;
    }
  }
}

variable_types: struct midifile_t { int size; struct midievent_t { unsigned int absolute_time; unsigned int ticks; int is_meta; int channel; int type; int shortparam[4]; int longparam_size; char *longparam_char; int longparam_int; } **events; int current_index; int record_index; struct midiparam_t { unsigned int header_length; unsigned short format; unsigned short num_tracks; unsigned short deltatime; unsigned int track_length; unsigned int current_time; unsigned char command; unsigned char last_command; unsigned char channel; unsigned char last_channel; char author[129]; char title[129]; } midiparam; int tempo; int current_ticks; int verbose; } *

variable_types:
synth
void *

user_defined_types: struct midifile_t { int size; struct midievent_t **events; int current_index; int record_index; struct midiparam_t midiparam; int tempo; int current_ticks; int verbose; }


decompiled_code:
__int64 __fastcall soundmodule_dispatch_wave(unsigned int *a1, __int64 a2, unsigned int a3, unsigned int a4)
{
unsigned int *v4; // r14
unsigned int *v7; // rbx
__int64 result; // rax
__int64 v9; // rdi

v4 = a1 + 96;
v7 = a1;
do
{
while ( 1 )
{
result = v7[4];
if ( !(_DWORD)result )
break;
LABEL_4:
v7 += 6;
if ( v7 == v4 )
return result;
}
result = *v7;
if ( (_DWORD)result )
{
if ( (_DWORD)result == 1 )
result = nrsynth_render(*((_QWORD *)v7 + 1), a2, a3, a4);
goto LABEL_4;
}
v9 = *((_QWORD *)v7 + 1);
v7 += 6;
result = wtssynth_render(v9, a2, a3, a4);
}
while ( v7 != v4 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int *a1, __int64 a2, unsigned int a3, unsigned int a4)
{
unsigned int *v4; // r14
unsigned int *v7; // rbx
__int64 result; // rax
__int64 v9; // rdi

v4 = a1 + 96;
v7 = a1;
do
{
while ( 1 )
{
result = v7[4];
if ( !(_DWORD)result )
break;
LABEL_4:
v7 += 6;
if ( v7 == v4 )
return result;
}
result = *v7;
if ( (_DWORD)result )
{
if ( (_DWORD)result == 1 )
result = nrsynth_render(*((_QWORD *)v7 + 1), a2, a3, a4);
goto LABEL_4;
}
v9 = *((_QWORD *)v7 + 1);
v7 += 6;
result = wtssynth_render(v9, a2, a3, a4);
}
while ( v7 != v4 );
return result;
}

original_code:
void soundmodule_dispatch_wave(soundmodule_t *s, audiobuf_t *a, int current_bufpos, int length){
  int i;
  for(i = 0; i < 16; i ++){
    if(s->chambers[i].is_muted) continue;
    switch(s->chambers[i].type){
    case SOUNDMODULE_CHAMBER_TYPE_WTS:
      wtssynth_render(s->chambers[i].synth, a, current_bufpos, length);
      break;
    case SOUNDMODULE_CHAMBER_TYPE_NR:
      nrsynth_render(s->chambers[i].synth, a, current_bufpos, length);
      break;
    default:
      ;
    }
  }
}

canonical_original_code:
void soundmodule_dispatch_wave(struct soundmodule_t *s, struct audiobuf_t *a, int current_bufpos, int length){
  int i;
  for(i = 0; i < 16; i ++){
    if(s->chambers[i].is_muted) continue;
    switch(s->chambers[i].type){
    case SOUNDMODULE_CHAMBER_TYPE_WTS:
      wtssynth_render(s->chambers[i].synth, a, current_bufpos, length);
      break;
    case SOUNDMODULE_CHAMBER_TYPE_NR:
      nrsynth_render(s->chambers[i].synth, a, current_bufpos, length);
      break;
    default:
      ;
    }
  }
}

variable_types: struct audiobuf_t { int n_channels; int sampling_rate; int qbit; float **data; int *index; int size; } *

variable_types: length int


decompiled_code:
__int64 __fastcall soundmodule_render(unsigned int *a1, __int64 a2, __int64 a3)
{
unsigned int v3; // er13
__int64 result; // rax
int v6; // eax
signed int v7; // er14
unsigned int v8; // er15

v3 = 0;
result = *(unsigned int *)(a2 + 32);
if ( (int)result > 0 )
{
while ( 1 )
{
while ( (unsigned int)midifile_has_waiting_event(a3) )
{
soundmodule_dispatch_midi((__int64)a1, a3);
midifile_next_event(a3);
}
v6 = midifile_get_nextevent_time(a3);
v7 = *(_DWORD *)(a2 + 32);
v8 = v7 - v3;
if ( v6 > 0 && (int)v8 >= v6 )
{
v7 = v6 + v3;
v8 = v6;
}
soundmodule_dispatch_wave(a1, a2, v3, v8);
result = midifile_ticks_proceed(a3, v8);
if ( *(_DWORD *)(a2 + 32) <= v7 )
break;
v3 = v7;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int *a1, __int64 a2, __int64 a3)
{
unsigned int v3; // er13
__int64 result; // rax
int v6; // eax
signed int v7; // er14
unsigned int v8; // er15

v3 = 0;
result = *(unsigned int *)(a2 + 32);
if ( (int)result > 0 )
{
while ( 1 )
{
while ( (unsigned int)midifile_has_waiting_event(a3) )
{
func3((__int64)a1, a3);
midifile_next_event(a3);
}
v6 = midifile_get_nextevent_time(a3);
v7 = *(_DWORD *)(a2 + 32);
v8 = v7 - v3;
if ( v6 > 0 && (int)v8 >= v6 )
{
v7 = v6 + v3;
v8 = v6;
}
func4(a1, a2, v3, v8);
result = midifile_ticks_proceed(a3, v8);
if ( *(_DWORD *)(a2 + 32) <= v7 )
break;
v3 = v7;
}
}
return result;
}

original_code:
void soundmodule_render(soundmodule_t *s, audiobuf_t *a, midifile_t *mf){
  int samples_to_render;
  int current_bufpos = 0;
  while(current_bufpos < a->size){
    while(midifile_has_waiting_event(mf)){
      soundmodule_dispatch_midi(s, mf);
      midifile_next_event(mf);
    }
    samples_to_render = midifile_get_nextevent_time(mf);
    if(samples_to_render <= 0 ||
       a->size - current_bufpos < samples_to_render){
      samples_to_render = a->size - current_bufpos;
    }
    soundmodule_dispatch_wave(s, a, current_bufpos, samples_to_render);
    midifile_ticks_proceed(mf, samples_to_render);
    current_bufpos += samples_to_render;
  }
}

canonical_original_code:
void soundmodule_render(struct soundmodule_t *s, struct audiobuf_t *a, struct midifile_t *mf){
  int samples_to_render;
  int current_bufpos = 0;
  while(current_bufpos < a->size){
    while(midifile_has_waiting_event(mf)){
      soundmodule_dispatch_midi(s, mf);
      midifile_next_event(mf);
    }
    samples_to_render = midifile_get_nextevent_time(mf);
    if(samples_to_render <= 0 ||
       a->size - current_bufpos < samples_to_render){
      samples_to_render = a->size - current_bufpos;
    }
    soundmodule_dispatch_wave(s, a, current_bufpos, samples_to_render);
    midifile_ticks_proceed(mf, samples_to_render);
    current_bufpos += samples_to_render;
  }
}

user_defined_types: struct midiparam_t { unsigned int header_length; unsigned short format; unsigned short num_tracks; unsigned short deltatime; unsigned int track_length; unsigned int current_time; unsigned char command; unsigned char last_command; unsigned char channel; unsigned char last_channel; char author[129]; char title[129]; }

user_defined_types: struct midievent_t { unsigned int absolute_time; unsigned int ticks; int is_meta; int channel; int type; int shortparam[4]; int longparam_size; char *longparam_char; int longparam_int; }


decompiled_code:
_BOOL8 __fastcall soundmodule_is_muted(__int64 a1, int a2)
{
return *(_DWORD *)(a1 + 24LL * a2 + 16) == 1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func6(__int64 a1, int a2)
{
return *(_DWORD *)(a1 + 24LL * a2 + 16) == 1;
}

original_code:
int soundmodule_is_muted(soundmodule_t *s, int channel){
  return s->chambers[channel].is_muted == 1;
}

canonical_original_code:
int soundmodule_is_muted(struct soundmodule_t *s, int channel){
  return s->chambers[channel].is_muted == 1;
}

variable_types: channel int

return_type: int


decompiled_code:
__int64 __fastcall soundmodule_unmute(__int64 a1, int a2)
{
__int64 result; // rax
int v3; // edx

result = a1 + 24LL * a2;
v3 = *(_DWORD *)result;
*(_DWORD *)(result + 16) = 0;
if ( !v3 )
result = wtssynth_all_note_off(*(_QWORD *)(result + 8));
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, int a2)
{
__int64 result; // rax
int v3; // edx

result = a1 + 24LL * a2;
v3 = *(_DWORD *)result;
*(_DWORD *)(result + 16) = 0;
if ( !v3 )
result = wtssynth_all_note_off(*(_QWORD *)(result + 8));
return result;
}

original_code:
void soundmodule_unmute(soundmodule_t *s, int channel){
  void *synth;
  s->chambers[channel].is_muted = 0;
  synth = s->chambers[channel].synth;
  switch(s->chambers[channel].type){
  case SOUNDMODULE_CHAMBER_TYPE_WTS:
    return wtssynth_all_note_off(synth);
  case SOUNDMODULE_CHAMBER_TYPE_NR:
    return ;
  default:
    return ;
  }
}

canonical_original_code:
void soundmodule_unmute(struct soundmodule_t *s, int channel){
  void *synth;
  s->chambers[channel].is_muted = 0;
  synth = s->chambers[channel].synth;
  switch(s->chambers[channel].type){
  case SOUNDMODULE_CHAMBER_TYPE_WTS:
    return wtssynth_all_note_off(synth);
  case SOUNDMODULE_CHAMBER_TYPE_NR:
    return ;
  default:
    return ;
  }
}


decompiled_code:
__int64 __fastcall soundmodule_midi_key(unsigned int *a1, unsigned int a2)
{
unsigned int *v2; // rbp
unsigned int *v3; // rbx
__int64 result; // rax
__int64 v5; // rdi

v2 = a1 + 96;
v3 = a1;
do
{
while ( 1 )
{
result = *v3;
if ( !(_DWORD)result )
break;
v3 += 6;
if ( v3 == v2 )
return result;
}
v5 = *((_QWORD *)v3 + 1);
v3 += 6;
result = wtssynth_midi_key(v5, a2);
}
while ( v3 != v2 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned int *a1, unsigned int a2)
{
unsigned int *v2; // rbp
unsigned int *v3; // rbx
__int64 result; // rax
__int64 v5; // rdi

v2 = a1 + 96;
v3 = a1;
do
{
while ( 1 )
{
result = *v3;
if ( !(_DWORD)result )
break;
v3 += 6;
if ( v3 == v2 )
return result;
}
v5 = *((_QWORD *)v3 + 1);
v3 += 6;
result = wtssynth_midi_key(v5, a2);
}
while ( v3 != v2 );
return result;
}

original_code:
void soundmodule_midi_key(soundmodule_t *s, int midi_key){
  int i;
  void *synth;
  for(i = 0; i < 16; i ++){
    synth = s->chambers[i].synth;
    switch(s->chambers[i].type){
    case SOUNDMODULE_CHAMBER_TYPE_WTS:
      wtssynth_midi_key(synth, midi_key);
      break;
    case SOUNDMODULE_CHAMBER_TYPE_NR:
      break;
    default:
      ;
    }
  }
}

canonical_original_code:
void soundmodule_midi_key(struct soundmodule_t *s, int midi_key){
  int i;
  void *synth;
  for(i = 0; i < 16; i ++){
    synth = s->chambers[i].synth;
    switch(s->chambers[i].type){
    case SOUNDMODULE_CHAMBER_TYPE_WTS:
      wtssynth_midi_key(synth, midi_key);
      break;
    case SOUNDMODULE_CHAMBER_TYPE_NR:
      break;
    default:
      ;
    }
  }
}


decompiled_code:
__int64 __fastcall soundmodule_get_current_key(__int64 a1, int a2, unsigned int a3)
{
int *v3; // rax
__int64 v4; // rdi
int v5; // eax

v3 = (int *)(a1 + 24LL * a2);
v4 = *((_QWORD *)v3 + 1);
v5 = *v3;
if ( !v5 )
return wtssynth_get_current_key(v4, a3);
if ( v5 == 1 )
return nrsynth_get_current_key(v4, a3);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, int a2, unsigned int a3)
{
int *v3; // rax
__int64 v4; // rdi
int v5; // eax

v3 = (int *)(a1 + 24LL * a2);
v4 = *((_QWORD *)v3 + 1);
v5 = *v3;
if ( !v5 )
return wtssynth_get_current_key(v4, a3);
if ( v5 == 1 )
return nrsynth_get_current_key(v4, a3);
return 0xFFFFFFFFLL;
}

original_code:
int soundmodule_get_current_key(soundmodule_t *s, int channel, int voice_index){
  void *synth;
  synth = s->chambers[channel].synth;
  switch(s->chambers[channel].type){
  case SOUNDMODULE_CHAMBER_TYPE_WTS:
    return wtssynth_get_current_key(synth, voice_index);
  case SOUNDMODULE_CHAMBER_TYPE_NR:
    return nrsynth_get_current_key(synth, voice_index);
  default:
    return -1;
  }
}

canonical_original_code:
int soundmodule_get_current_key(struct soundmodule_t *s, int channel, int voice_index){
  void *synth;
  synth = s->chambers[channel].synth;
  switch(s->chambers[channel].type){
  case SOUNDMODULE_CHAMBER_TYPE_WTS:
    return wtssynth_get_current_key(synth, voice_index);
  case SOUNDMODULE_CHAMBER_TYPE_NR:
    return nrsynth_get_current_key(synth, voice_index);
  default:
    return -1;
  }
}


decompiled_code:
__int64 __fastcall soundmodule_is_key_offing(__int64 a1, int a2, unsigned int a3)
{
__int64 v3; // rax
__int64 result; // rax

v3 = a1 + 24LL * a2;
if ( *(_DWORD *)v3 )
result = 0LL;
else
result = wtssynth_is_key_offing(*(_QWORD *)(v3 + 8), a3);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, int a2, unsigned int a3)
{
__int64 v3; // rax
__int64 result; // rax

v3 = a1 + 24LL * a2;
if ( *(_DWORD *)v3 )
result = 0LL;
else
result = wtssynth_is_key_offing(*(_QWORD *)(v3 + 8), a3);
return result;
}

original_code:
int soundmodule_is_key_offing(soundmodule_t *s, int channel, int voice_index){
  void *synth;
  synth = s->chambers[channel].synth;
  switch(s->chambers[channel].type){
  case SOUNDMODULE_CHAMBER_TYPE_WTS:
    return wtssynth_is_key_offing(synth, voice_index);
  case SOUNDMODULE_CHAMBER_TYPE_NR:
    return 0;
  default:
    return 0;
    ;
  }
}

canonical_original_code:
int soundmodule_is_key_offing(struct soundmodule_t *s, int channel, int voice_index){
  void *synth;
  synth = s->chambers[channel].synth;
  switch(s->chambers[channel].type){
  case SOUNDMODULE_CHAMBER_TYPE_WTS:
    return wtssynth_is_key_offing(synth, voice_index);
  case SOUNDMODULE_CHAMBER_TYPE_NR:
    return 0;
  default:
    return 0;
    ;
  }
}


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

v2 = a1 + 24LL * a2;
if ( *(_DWORD *)v2 )
result = 0LL;
else
result = wtssynth_get_program(*(_QWORD *)(v2 + 8));
return result;
}

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

v2 = a1 + 24LL * a2;
if ( *(_DWORD *)v2 )
result = 0LL;
else
result = wtssynth_get_program(*(_QWORD *)(v2 + 8));
return result;
}

original_code:
int soundmodule_get_program(soundmodule_t *s, int channel){
  void *synth;
  synth = s->chambers[channel].synth;
  switch(s->chambers[channel].type){
  case SOUNDMODULE_CHAMBER_TYPE_WTS:
    return wtssynth_get_program(synth);
  case SOUNDMODULE_CHAMBER_TYPE_NR:
    return 0;
  default:
    return 0;
  }
}

canonical_original_code:
int soundmodule_get_program(struct soundmodule_t *s, int channel){
  void *synth;
  synth = s->chambers[channel].synth;
  switch(s->chambers[channel].type){
  case SOUNDMODULE_CHAMBER_TYPE_WTS:
    return wtssynth_get_program(synth);
  case SOUNDMODULE_CHAMBER_TYPE_NR:
    return 0;
  default:
    return 0;
  }
}


decompiled_code:
void __fastcall xor128_init(__int64 a1)
{
v = a1;
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1)
{
v = a1;
}

original_code: void xor128_init(unsigned long s){v[0] = s;}

variable_types:
s
unsigned long


decompiled_code:
unsigned __int64 xor128()
{
unsigned __int64 v0; // rdx
__int64 v1; // rax
unsigned __int64 result; // rax

v0 = v ^ (v << 11);
v = qword_148;
v1 = qword_150;
qword_150 = qword_158;
qword_148 = v1;
result = (v0 >> 8) ^ v0 ^ qword_158 ^ ((unsigned __int64)qword_158 >> 19);
qword_158 = result;
return result;
}

canonical_decompiled_code:
unsigned __int64 func1()
{
unsigned __int64 v0; // rdx
__int64 v1; // rax
unsigned __int64 result; // rax

v0 = v ^ (v << 11);
v = qword_148;
v1 = qword_150;
qword_150 = qword_158;
qword_148 = v1;
result = (v0 >> 8) ^ v0 ^ qword_158 ^ ((unsigned __int64)qword_158 >> 19);
qword_158 = result;
return result;
}

variable_types:
t
unsigned long

return_type: unsigned long


decompiled_code:
void __fastcall midievent_destroy(void *ptr)
{
void *v2; // rdi

v2 = (void *)*((_QWORD *)ptr + 5);
if ( v2 )
free(v2);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func0(void *ptr)
{
void *v2; // rdi

v2 = (void *)*((_QWORD *)ptr + 5);
if ( v2 )
free(v2);
free(ptr);
}

original_code:
void midievent_destroy(midievent_t *e){
  if(e->longparam_char != ((void *)0)){
    free(e->longparam_char);
  }
  free(e);
}

canonical_original_code:
void midievent_destroy(struct midievent_t *e){
  if(e->longparam_char != ((void *)0)){
    free(e->longparam_char);
  }
  free(e);
}

variable_types: struct midievent_t { unsigned int absolute_time; unsigned int ticks; int is_meta; int channel; int type; int shortparam[4]; int longparam_size; char *longparam_char; int longparam_int; } *


decompiled_code:
int __fastcall midievent_dump(unsigned int *a1)
{
__int64 v1; // rcx
__int64 v2; // rdx
__int64 v3; // rsi
int result; // eax

v1 = a1[4];
v2 = a1[3];
v3 = *a1;
if ( a1[2] )
result = printf(" %010d %010d [%02d]: Long %02x %d\n", v3, a1[1], (unsigned int)v2, (unsigned int)v1, a1[12]);
else
result = printf(
" Ticks[%010d] Ch[%02d]: Type[%02x] Param[%d %d %d %d]\n",
v3,
v2,
v1,
a1[5],
a1[6],
a1[7],
a1[8]);
return result;
}

canonical_decompiled_code:
int __fastcall func1(unsigned int *a1)
{
__int64 v1; // rcx
__int64 v2; // rdx
__int64 v3; // rsi
int result; // eax

v1 = a1[4];
v2 = a1[3];
v3 = *a1;
if ( a1[2] )
result = printf(" %010d %010d [%02d]: Long %02x %d\n", v3, a1[1], (unsigned int)v2, (unsigned int)v1, a1[12]);
else
result = printf(
" Ticks[%010d] Ch[%02d]: Type[%02x] Param[%d %d %d %d]\n",
v3,
v2,
v1,
a1[5],
a1[6],
a1[7],
a1[8]);
return result;
}

original_code:
void midievent_dump(midievent_t *e){
  if(e->is_meta == 0){
    printf("    Ticks[%010d] Ch[%02d]: Type[%02x] Param[%d %d %d %d]\n",
    e->absolute_time, e->channel, e->type,
    e->shortparam[0], e->shortparam[1], e->shortparam[2], e->shortparam[3]);
  }else{
    printf(" %010d %010d [%02d]: Long  %02x %d\n",
    e->absolute_time, e->ticks, e->channel, e->type, e->longparam_int);
  }
}

canonical_original_code:
void midievent_dump(struct midievent_t *e){
  if(e->is_meta == 0){
    printf("    Ticks[%010d] Ch[%02d]: Type[%02x] Param[%d %d %d %d]\n",
    e->absolute_time, e->channel, e->type,
    e->shortparam[0], e->shortparam[1], e->shortparam[2], e->shortparam[3]);
  }else{
    printf(" %010d %010d [%02d]: Long  %02x %d\n",
    e->absolute_time, e->ticks, e->channel, e->type, e->longparam_int);
  }
}


decompiled_code:
__int64 __fastcall midievent_compare(_DWORD **a1, _DWORD **a2)
{
return (unsigned int)(**a1 - **a2);
}

canonical_decompiled_code:
__int64 __fastcall func2(_DWORD **a1, _DWORD **a2)
{
return (unsigned int)(**a1 - **a2);
}

original_code:
int midievent_compare(const void *p, const void *q){
  midievent_t **_p = (midievent_t **)p;
  midievent_t **_q = (midievent_t **)q;
  return (*_p)->absolute_time - (*_q)->absolute_time;
}

canonical_original_code:
int midievent_compare(void *p, void *q){
  struct midievent_t **_p = (midievent_t **)p;
  struct midievent_t **_q = (midievent_t **)q;
  return (*_p)->absolute_time - (*_q)->absolute_time;
}

variable_types:
p
void *

variable_types:
q
void *

variable_types: struct midievent_t { unsigned int absolute_time; unsigned int ticks; int is_meta; int channel; int type; int shortparam[4]; int longparam_size; char *longparam_char; int longparam_int; } **


GCMFileEntryStructToRaw

repo: sadistech/gcmtool

original_code:
void GCMFileEntryStructToRaw(GCMFileEntryStruct *e, char *buf) {
 if (!e || !buf) return;
 bzero(buf, 0x000c);
 char *start = buf;
 if (e->isDir) {
  buf[0] = 1;
 }
 *buf++;
 u32 *tempFilenameOffset = (u32*)malloc(sizeof(u32));
 *tempFilenameOffset = (e->filenameOffset << 8);
 memcpy(buf, tempFilenameOffset, 3);
 buf += 3;
 u32 *offset = (u32*)malloc(sizeof(u32));
 *offset = htonl(e->offset);
 memcpy(buf, offset, sizeof(u32));
 buf += sizeof(u32);
 u32 *length = (u32*)malloc(sizeof(u32));
 *length = htonl(e->length);
 memcpy(buf, length, sizeof(u32));
 buf += sizeof(u32);
 buf = start;
}

canonical_original_code:
void GCMFileEntryStructToRaw(struct gcm_file_entry_struct *e, char *buf) {
 if (!e || !buf) return;
 bzero(buf, 0x000c);
 char *start = buf;
 if (e->isDir) {
  buf[0] = 1;
 }
 *buf++;
 unsigned long *tempFilenameOffset = (u32*)malloc(sizeof(u32));
 *tempFilenameOffset = (e->filenameOffset << 8);
 memcpy(buf, tempFilenameOffset, 3);
 buf += 3;
 unsigned long *offset = (u32*)malloc(sizeof(u32));
 *offset = htonl(e->offset);
 memcpy(buf, offset, sizeof(u32));
 buf += sizeof(u32);
 unsigned long *length = (u32*)malloc(sizeof(u32));
 *length = htonl(e->length);
 memcpy(buf, length, sizeof(u32));
 buf += sizeof(u32);
 buf = start;
}

variable_types: struct gcm_file_entry_struct { int index; char isDir; unsigned long filenameOffset; unsigned long offset; unsigned long length; char *data; char *filename; } *

variable_types:
buf
char *

variable_types:
start
char *

variable_types:
tempFilenameOffset
unsigned long *

variable_types:
offset
unsigned long *

variable_types:
length
unsigned long *


GCMFreeFileEntryStruct

decompiled_code:
void __fastcall GCMFreeFileEntryStruct(void *ptr)
{
void *v2; // rdi
void *v3; // rdi

if ( ptr )
{
v2 = (void *)*((_QWORD *)ptr + 4);
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 5);
if ( v3 )
free(v3);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func7(void *ptr)
{
void *v2; // rdi
void *v3; // rdi

if ( ptr )
{
v2 = (void *)*((_QWORD *)ptr + 4);
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 5);
if ( v3 )
free(v3);
free(ptr);
}
}

original_code:
void GCMFreeFileEntryStruct(GCMFileEntryStruct *fe) {
 if (!fe) return;
 if (fe->data != ((void *)0))
  free(fe->data);
 if (fe->filename != ((void *)0))
  free(fe->filename);
 free(fe);
}

canonical_original_code:
void GCMFreeFileEntryStruct(struct gcm_file_entry_struct *fe) {
 if (!fe) return;
 if (fe->data != ((void *)0))
  free(fe->data);
 if (fe->filename != ((void *)0))
  free(fe->filename);
 free(fe);
}

user_defined_types: struct gcm_file_entry_struct { int index; char isDir; unsigned long filenameOffset; unsigned long offset; unsigned long length; char *data; char *filename; }


printBnrInfo

decompiled_code:
int __fastcall printBnrInfo(const char *a1)
{
printf("Name: %s\n", a1);
printf("Developer: %s\n", a1 + 32);
printf("Full Name: %s\n", a1 + 64);
printf("Full Developer: %s\n", a1 + 128);
return printf("Description: %s\n", a1 + 192);
}

canonical_decompiled_code:
int __fastcall func0(const char *a1)
{
printf("Name: %s\n", a1);
printf("Developer: %s\n", a1 + 32);
printf("Full Name: %s\n", a1 + 64);
printf("Full Developer: %s\n", a1 + 128);
return printf("Description: %s\n", a1 + 192);
}

original_code:
void printBnrInfo(GCMBnrInfoStruct *r) {
 printf("Name:           %s\n", r->name);
 printf("Developer:      %s\n", r->developer);
 printf("Full Name:      %s\n", r->fullName);
 printf("Full Developer: %s\n", r->fullDeveloper);
 printf("Description:    %s\n", r->description);
}

canonical_original_code:
void printBnrInfo(struct gcm_bnr_info_record_struct *r) {
 printf("Name:           %s\n", r->name);
 printf("Developer:      %s\n", r->developer);
 printf("Full Name:      %s\n", r->fullName);
 printf("Full Developer: %s\n", r->fullDeveloper);
 printf("Description:    %s\n", r->description);
}

variable_types: struct gcm_bnr_info_record_struct { char name[32]; char developer[32]; char fullName[64]; char fullDeveloper[64]; char description[128]; struct gcm_bnr_info_record_struct *next; } *


printBnr

decompiled_code:
int __fastcall printBnr(char *a1)
{
unsigned int v1; // eax
__int64 v2; // rbx
int result; // eax

printf("Version: %c\n", (unsigned int)*a1);
if ( (int)GCMBnrInfoCount(*((_QWORD *)a1 + 769)) > 1 )
{
v1 = GCMBnrInfoCount(*((_QWORD *)a1 + 769));
printf(" Count: %d\n", v1);
putchar(10);
}
v2 = *((_QWORD *)a1 + 769);
do
{
printBnrInfo((const char *)v2);
result = putchar(10);
v2 = *(_QWORD *)(v2 + 320);
}
while ( v2 );
return result;
}

canonical_decompiled_code:
int __fastcall func1(char *a1)
{
unsigned int v1; // eax
__int64 v2; // rbx
int result; // eax

printf("Version: %c\n", (unsigned int)*a1);
if ( (int)GCMBnrInfoCount(*((_QWORD *)a1 + 769)) > 1 )
{
v1 = GCMBnrInfoCount(*((_QWORD *)a1 + 769));
printf(" Count: %d\n", v1);
putchar(10);
}
v2 = *((_QWORD *)a1 + 769);
do
{
func0((const char *)v2);
result = putchar(10);
v2 = *(_QWORD *)(v2 + 320);
}
while ( v2 );
return result;
}

original_code:
void printBnr(GCMBnrStruct *b) {
 printf("Version:        %c\n", b->version);
 if (GCMBnrInfoCount(b->info) > 1) {
  printf(" Count:   %d\n", GCMBnrInfoCount(b->info));
  printf("\n");
 }
 GCMBnrInfoStruct *info = b->info;
 do {
  printBnrInfo(info);
  printf("\n");
 } while (info = info->next);
}

canonical_original_code:
void printBnr(struct gcm_bnr_struct *b) {
 printf("Version:        %c\n", b->version);
 if (GCMBnrInfoCount(b->info) > 1) {
  printf(" Count:   %d\n", GCMBnrInfoCount(b->info));
  printf("\n");
 }
 struct gcm_bnr_info_record_struct *info = b->info;
 do {
  printBnrInfo(info);
  printf("\n");
 } while (info = info->next);
}

variable_types: struct gcm_bnr_struct { char version; char graphic[6144]; struct gcm_bnr_info_record_struct { char name[32]; char developer[32]; char fullName[64]; char fullDeveloper[64]; char description[128]; struct gcm_bnr_info_record_struct *next; } *info; } *

user_defined_types: struct gcm_bnr_struct { char version; char graphic[6144]; struct gcm_bnr_info_record_struct *info; }


openBnr

decompiled_code:
FILE *openBnr()
{
FILE *result; // rax

result = fopen(filename, "r+");
bnrFile = result;
if ( !result )
{
printf("Error opening %s\n", filename);
exit(1);
}
return result;
}

canonical_decompiled_code:
FILE *func2()
{
FILE *result; // rax

result = fopen(filename, "r+");
bnrFile = result;
if ( !result )
{
printf("Error opening %s\n", filename);
exit(1);
}
return result;
}

original_code:
void openBnr() {
 if (!(bnrFile = fopen(filename, "r+"))) {
  printf("Error opening %s\n", filename);
  exit(1);
 }
}


closeBnr

decompiled_code:
int closeBnr()
{
return fclose(bnrFile);
}

canonical_decompiled_code:
int func3()
{
return fclose(bnrFile);
}

original_code:
void closeBnr() {
 fclose(bnrFile);
}

canonical_original_code:
void closeBnr() {
 fclose(bnrFile);
}


printUsage

decompiled_code:
int printUsage()
{
printf("gcmbnrtool %s- Utility for working with .bnr files from GameCube DVD Images (GCMs)\n", "0.3.3d");
puts("http://gcmtool.sourceforge.net\n");
printf("Usage:\t");
puts("gcmbnrtool [options] <bnr_file>\n");
return puts("Use -? to view extended usage.\n");
}

canonical_decompiled_code:
int func4()
{
printf("gcmbnrtool %s- Utility for working with .bnr files from GameCube DVD Images (GCMs)\n", "0.3.3d");
puts("http://gcmtool.sourceforge.net\n");
printf("Usage:\t");
puts("gcmbnrtool [options] <bnr_file>\n");
return puts("Use -? to view extended usage.\n");
}

original_code:
void printUsage() {
 printf("gcmbnrtool %s- Utility for working with .bnr files from GameCube DVD Images (GCMs)\n", "0.3.3d");
 printf("http://gcmtool.sourceforge.net\n\n");
 printf("Usage:\t");
 printf("gcmbnrtool [options] <bnr_file>\n\n");
 printf("Use -? to view extended usage.\n\n");
}


printExtendedUsage

decompiled_code:
int printExtendedUsage()
{
printUsage();
printf("\t\x1B[1m-?%s--help \x1B[m\n\t\tDisplays this help.\n\n", ", ");
printf(
"\t\x1B[1m-i%s--icon [ -raw | -ppm ] <pathname>\x1B[m\n"
"\t\tSets the icon from <pathname> and injects it into the bnr. -raw is the default\n"
"\n",
", ");
printf(
"\t\x1B[1m-e%s--extract-icon [ -raw | -ppm ] <pathname>\x1B[m\n"
"\t\tExtracts the icon from the bnr in the desired format. -raw is the default\n"
"\n",
", ");
printf("\t\x1B[1m-m%s--modify-nth <index>\x1B[m\n\t\tModify the nth record (only for multi-lingual BNRs)\n\n", ", ");
printf("When editing BNR2s, you must use %s to declare which field you'd like to edit.\n", "-m");
printf("\t\x1B[1m-n%s--name <name>\x1B[m\n\t\tSets the name field to <name>\n\n", ", ");
printf("\t\x1B[1m-d%s--developer <developer>\x1B[m\n\t\tSets the developer field to <developer>\n\n", ", ");
printf("\t\x1B[1m-N%s--full-name <full_name>\x1B[m\n\t\tSets the full name field to <full_name>\n\n", ", ");
printf(
"\t\x1B[1m-D%s--full-developer <full_developer>\x1B[m\n\t\tSets the full developer field to <full_developer>\n\n",
", ");
return printf(
"\t\x1B[1m-s%s--description <full_description>\x1B[m\n"
"\t\tSets the description field to <full_description>\n"
"\n",
", ");
}

canonical_decompiled_code:
int func5()
{
func4();
printf("\t\x1B[1m-?%s--help \x1B[m\n\t\tDisplays this help.\n\n", ", ");
printf(
"\t\x1B[1m-i%s--icon [ -raw | -ppm ] <pathname>\x1B[m\n"
"\t\tSets the icon from <pathname> and injects it into the bnr. -raw is the default\n"
"\n",
", ");
printf(
"\t\x1B[1m-e%s--extract-icon [ -raw | -ppm ] <pathname>\x1B[m\n"
"\t\tExtracts the icon from the bnr in the desired format. -raw is the default\n"
"\n",
", ");
printf("\t\x1B[1m-m%s--modify-nth <index>\x1B[m\n\t\tModify the nth record (only for multi-lingual BNRs)\n\n", ", ");
printf("When editing BNR2s, you must use %s to declare which field you'd like to edit.\n", "-m");
printf("\t\x1B[1m-n%s--name <name>\x1B[m\n\t\tSets the name field to <name>\n\n", ", ");
printf("\t\x1B[1m-d%s--developer <developer>\x1B[m\n\t\tSets the developer field to <developer>\n\n", ", ");
printf("\t\x1B[1m-N%s--full-name <full_name>\x1B[m\n\t\tSets the full name field to <full_name>\n\n", ", ");
printf(
"\t\x1B[1m-D%s--full-developer <full_developer>\x1B[m\n\t\tSets the full developer field to <full_developer>\n\n",
", ");
return printf(
"\t\x1B[1m-s%s--description <full_description>\x1B[m\n"
"\t\tSets the description field to <full_description>\n"
"\n",
", ");
}

original_code:
void printExtendedUsage() {
 printUsage();
 printf("\t\033[1m" "-?" "%s" "--help" " " "" "\033[m\n\t\t" "Displays this help." "\n\n", strcmp("", "--help") == 0 ? "" : ", ");;
 printf("\t\033[1m" "-i" "%s" "--icon" " " "[ " "-raw" " | " "-ppm" " ] <pathname>" "\033[m\n\t\t" "Sets the icon from <pathname> and injects it into the bnr. " "-raw" " is the default" "\n\n", strcmp("", "--icon") == 0 ? "" : ", ");;
 printf("\t\033[1m" "-e" "%s" "--extract-icon" " " "[ " "-raw" " | " "-ppm" " ] <pathname>" "\033[m\n\t\t" "Extracts the icon from the bnr in the desired format. " "-raw" " is the default" "\n\n", strcmp("", "--extract-icon") == 0 ? "" : ", ");;
 printf("\t\033[1m" "-m" "%s" "--modify-nth" " " "<index>" "\033[m\n\t\t" "Modify the nth record (only for multi-lingual BNRs)" "\n\n", strcmp("", "--modify-nth") == 0 ? "" : ", ");;
 printf("When editing BNR2s, you must use %s to declare which field you'd like to edit.\n", "-m");
 printf("\t\033[1m" "-n" "%s" "--name" " " "<name>" "\033[m\n\t\t" "Sets the name field to <name>" "\n\n", strcmp("", "--name") == 0 ? "" : ", ");;
 printf("\t\033[1m" "-d" "%s" "--developer" " " "<developer>" "\033[m\n\t\t" "Sets the developer field to <developer>" "\n\n", strcmp("", "--developer") == 0 ? "" : ", ");;
 printf("\t\033[1m" "-N" "%s" "--full-name" " " "<full_name>" "\033[m\n\t\t" "Sets the full name field to <full_name>" "\n\n", strcmp("", "--full-name") == 0 ? "" : ", ");;
 printf("\t\033[1m" "-D" "%s" "--full-developer" " " "<full_developer>" "\033[m\n\t\t" "Sets the full developer field to <full_developer>" "\n\n", strcmp("", "--full-developer") == 0 ? "" : ", ");;
 printf("\t\033[1m" "-s" "%s" "--description" " " "<full_description>" "\033[m\n\t\t" "Sets the description field to <full_description>" "\n\n", strcmp("", "--description") == 0 ? "" : ", ");;
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er15
const char **v5; // r12
int v6; // eax
const char *v7; // rbp
int v8; // edx
const char *v9; // rbp
const char *v10; // rbp
size_t v11; // rbx
void *v12; // rbp
__int64 v13; // rsi
char *v14; // rax
char *v15; // rbx
char *v16; // rbp
int v17; // er12
__int64 v18; // rcx
char *v19; // rdi
__int64 v20; // rcx
_DWORD *v21; // rdi
__int64 v22; // rcx
_DWORD *v23; // rdi
__int64 v24; // rcx
_DWORD *v25; // rdi
char *v26; // rbp
__int64 v27; // rcx
char *v28; // rdi
void *v29; // rbp
__int64 v30; // rsi
size_t v31; // rbp
void *v32; // rax
void *v33; // r12
char *v34; // rbp
size_t v35; // rbp
void *v36; // r12
char *src; // [rsp+0h] [rbp-78h]
char *v39; // [rsp+8h] [rbp-70h]
const char *v40; // [rsp+10h] [rbp-68h]
char *v41; // [rsp+18h] [rbp-60h]
char *v42; // [rsp+20h] [rbp-58h]
char *s; // [rsp+28h] [rbp-50h]
char *v44; // [rsp+30h] [rbp-48h]
int v45; // [rsp+38h] [rbp-40h]
int v46; // [rsp+3Ch] [rbp-3Ch]

v3 = 0;
v46 = 0;
v45 = 0;
s = 0LL;
v40 = 0LL;
v44 = 0LL;
v42 = 0LL;
v41 = 0LL;
v39 = 0LL;
src = 0LL;
while ( 1 )
{
v7 = argv[1];
if ( !v7 )
{
printUsage();
exit(1);
}
v8 = *(unsigned __int8 *)v7;
if ( v8 == 45 && v7[1] == 63 && !v7[2] || !strcmp("--help", argv[1]) )
{
printExtendedUsage();
exit(1);
}
if ( v8 == 45 && v7[1] == 109 && !v7[2] || !strcmp("--modify-nth", argv[1]) )
{
v5 = argv + 2;
v6 = atoi(argv[2]);
v3 = v6;
if ( v6 <= 0 )
{
printf(" index is out of bounds! (%d)\n", (unsigned int)v6);
exit(1);
}
goto LABEL_5;
}
v5 = argv + 1;
if ( v8 == 45 && v7[1] == 110 && !v7[2] || !strcmp("--name", argv[1]) )
{
if ( argv[2] )
{
src = (char *)argv[2];
v5 = argv + 2;
}
goto LABEL_5;
}
if ( v8 == 45 && v7[1] == 100 && !v7[2] || !strcmp("--developer", argv[1]) )
{
if ( argv[2] )
{
v39 = (char *)argv[2];
v5 = argv + 2;
}
goto LABEL_5;
}
if ( v8 == 45 && v7[1] == 78 && !v7[2] || !strcmp("--full-name", argv[1]) )
{
if ( argv[2] )
{
v41 = (char *)argv[2];
v5 = argv + 2;
}
goto LABEL_5;
}
if ( !strcmp("-D", v7) || !strcmp("--full-developer", v7) )
{
if ( argv[2] )
{
v42 = (char *)argv[2];
v5 = argv + 2;
}
goto LABEL_5;
}
if ( !strcmp("-s", v7) || !strcmp("--description", v7) )
{
if ( argv[2] )
{
v44 = (char *)argv[2];
v5 = argv + 2;
}
goto LABEL_5;
}
if ( !strcmp("-e", v7) || !strcmp("--extract-icon", v7) )
{
v10 = argv[2];
if ( !v10 )
goto LABEL_5;
if ( !strcmp(argv[2], "-raw") )
{
v45 = 0;
v5 = argv + 2;
}
else if ( !strcmp(v10, "-ppm") )
{
v45 = 1;
v5 = argv + 2;
}
if ( v5[1] )
{
v40 = v5[1];
++v5;
goto LABEL_5;
}
goto LABEL_57;
}
if ( strcmp("-i", v7) && strcmp("--icon", v7) )
break;
v9 = argv[2];
if ( !v9 )
goto LABEL_5;
if ( !strcmp(argv[2], "-raw") )
{
v46 = 0;
v5 = argv + 2;
}
else if ( !strcmp(v9, "-ppm") )
{
v46 = 1;
v5 = argv + 2;
}
if ( v5[1] )
{
s = (char *)v5[1];
++v5;
goto LABEL_5;
}
LABEL_57:
if ( !*v5 )
goto LABEL_58;
LABEL_5:
argv = v5;
}
filename = (char *)v7;
LABEL_58:
openBnr();
v11 = GetFilesizeFromStream(bnrFile);
v12 = malloc(v11);
v13 = ReadDataFromFile(v12, filename);
if ( v13 != v11 )
goto LABEL_106;
v14 = (char *)GCMRawBnrToStruct(v12, v13);
v15 = v14;
if ( !v14 )
{
puts("error opening banner!");
exit(1);
}
printBnr(v14);
if ( *v15 == 49 )
v16 = (char *)*((_QWORD *)v15 + 769);
else
v16 = (char *)GCMBnrGetNthInfo(*((_QWORD *)v15 + 769), (unsigned int)(v3 - 1));
if ( !v16 )
{
puts("ERROR! mod == NULL (this shouldn't happen!)");
exit(1);
}
v17 = 0;
if ( src )
{
v18 = 8LL;
v19 = v16;
v17 = 1;
while ( v18 )
{
*(_DWORD *)v19 = 0;
v19 += 4;
--v18;
}
strcpy(v16, src);
}
if ( v39 )
{
v20 = 8LL;
v17 = 1;
v21 = v16 + 32;
while ( v20 )
{
*v21++ = 0;
--v20;
}
strcpy(v16 + 32, v39);
}
if ( v41 )
{
v22 = 16LL;
v17 = 1;
v23 = v16 + 64;
while ( v22 )
{
*v23++ = 0;
--v22;
}
strcpy(v16 + 64, v41);
}
if ( v42 )
{
v24 = 16LL;
v17 = 1;
v25 = v16 + 128;
while ( v24 )
{
*v25++ = 0;
--v24;
}
strcpy(v16 + 128, v42);
}
if ( v44 )
{
v26 = v16 + 192;
v27 = 32LL;
v17 = 1;
v28 = v26;
while ( v27 )
{
*(_DWORD *)v28 = 0;
v28 += 4;
--v27;
}
strcpy(v26, v44);
}
if ( v40 )
{
if ( v45 )
{
v29 = malloc(0x240DuLL);
GCMBnrGetImagePPM(v15, v29);
v30 = 9229LL;
}
else
{
v29 = malloc(0x2400uLL);
GCMBnrGetImageRaw(v15, v29);
v30 = 9216LL;
}
WriteDataToFile(v29, v30, v40);
}
if ( !s )
{
if ( v17 )
goto LABEL_96;
goto LABEL_97;
}
v31 = GetFilesizeFromPath(s);
v32 = malloc(v31);
v33 = v32;
if ( v46 )
{
puts("PPM IMPORT IS NOT IMPLEMENTED! (file not affected)\n");
exit(1);
}
if ( ReadDataFromFile(v32, s) != v31 )
{
perror(s);
exit(1);
}
v34 = (char *)malloc(0x1800uLL);
GCMBnrRawImageToGraphic(v33, v34);
*(_QWORD *)(v15 + 1) = *(_QWORD *)v34;
*(_QWORD *)(v15 + 6137) = *((_QWORD *)v34 + 767);
qmemcpy(
(void *)((unsigned __int64)(v15 + 9) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v34 - &v15[-((unsigned __int64)(v15 + 9) & 0xFFFFFFFFFFFFFFF8LL) + 1]),
8LL * (((_DWORD)v15 + 1 - (((_DWORD)v15 + 9) & 0xFFFFFFF8) + 6144) >> 3));
free(v34);
LABEL_96:
rewind(bnrFile);
v35 = GCMBnrRawSize(v15);
v36 = malloc(v35);
GCMBnrStructToRaw(v15, v36);
if ( fwrite(v36, 1uLL, v35, bnrFile) != v35 )
{
LABEL_106:
perror(filename);
exit(1);
}
LABEL_97:
GCMFreeBnrInfoStruct(*((_QWORD *)v15 + 769));
closeBnr();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er15
const char **v5; // r12
int v6; // eax
const char *v7; // rbp
int v8; // edx
const char *v9; // rbp
const char *v10; // rbp
size_t v11; // rbx
void *v12; // rbp
__int64 v13; // rsi
char *v14; // rax
char *v15; // rbx
char *v16; // rbp
int v17; // er12
__int64 v18; // rcx
char *v19; // rdi
__int64 v20; // rcx
_DWORD *v21; // rdi
__int64 v22; // rcx
_DWORD *v23; // rdi
__int64 v24; // rcx
_DWORD *v25; // rdi
char *v26; // rbp
__int64 v27; // rcx
char *v28; // rdi
void *v29; // rbp
__int64 v30; // rsi
size_t v31; // rbp
void *v32; // rax
void *v33; // r12
char *v34; // rbp
size_t v35; // rbp
void *v36; // r12
char *src; // [rsp+0h] [rbp-78h]
char *v39; // [rsp+8h] [rbp-70h]
const char *v40; // [rsp+10h] [rbp-68h]
char *v41; // [rsp+18h] [rbp-60h]
char *v42; // [rsp+20h] [rbp-58h]
char *s; // [rsp+28h] [rbp-50h]
char *v44; // [rsp+30h] [rbp-48h]
int v45; // [rsp+38h] [rbp-40h]
int v46; // [rsp+3Ch] [rbp-3Ch]

v3 = 0;
v46 = 0;
v45 = 0;
s = 0LL;
v40 = 0LL;
v44 = 0LL;
v42 = 0LL;
v41 = 0LL;
v39 = 0LL;
src = 0LL;
while ( 1 )
{
v7 = argv[1];
if ( !v7 )
{
func4();
exit(1);
}
v8 = *(unsigned __int8 *)v7;
if ( v8 == 45 && v7[1] == 63 && !v7[2] || !strcmp("--help", argv[1]) )
{
func5();
exit(1);
}
if ( v8 == 45 && v7[1] == 109 && !v7[2] || !strcmp("--modify-nth", argv[1]) )
{
v5 = argv + 2;
v6 = atoi(argv[2]);
v3 = v6;
if ( v6 <= 0 )
{
printf(" index is out of bounds! (%d)\n", (unsigned int)v6);
exit(1);
}
goto LABEL_5;
}
v5 = argv + 1;
if ( v8 == 45 && v7[1] == 110 && !v7[2] || !strcmp("--name", argv[1]) )
{
if ( argv[2] )
{
src = (char *)argv[2];
v5 = argv + 2;
}
goto LABEL_5;
}
if ( v8 == 45 && v7[1] == 100 && !v7[2] || !strcmp("--developer", argv[1]) )
{
if ( argv[2] )
{
v39 = (char *)argv[2];
v5 = argv + 2;
}
goto LABEL_5;
}
if ( v8 == 45 && v7[1] == 78 && !v7[2] || !strcmp("--full-name", argv[1]) )
{
if ( argv[2] )
{
v41 = (char *)argv[2];
v5 = argv + 2;
}
goto LABEL_5;
}
if ( !strcmp("-D", v7) || !strcmp("--full-developer", v7) )
{
if ( argv[2] )
{
v42 = (char *)argv[2];
v5 = argv + 2;
}
goto LABEL_5;
}
if ( !strcmp("-s", v7) || !strcmp("--description", v7) )
{
if ( argv[2] )
{
v44 = (char *)argv[2];
v5 = argv + 2;
}
goto LABEL_5;
}
if ( !strcmp("-e", v7) || !strcmp("--extract-icon", v7) )
{
v10 = argv[2];
if ( !v10 )
goto LABEL_5;
if ( !strcmp(argv[2], "-raw") )
{
v45 = 0;
v5 = argv + 2;
}
else if ( !strcmp(v10, "-ppm") )
{
v45 = 1;
v5 = argv + 2;
}
if ( v5[1] )
{
v40 = v5[1];
++v5;
goto LABEL_5;
}
goto LABEL_57;
}
if ( strcmp("-i", v7) && strcmp("--icon", v7) )
break;
v9 = argv[2];
if ( !v9 )
goto LABEL_5;
if ( !strcmp(argv[2], "-raw") )
{
v46 = 0;
v5 = argv + 2;
}
else if ( !strcmp(v9, "-ppm") )
{
v46 = 1;
v5 = argv + 2;
}
if ( v5[1] )
{
s = (char *)v5[1];
++v5;
goto LABEL_5;
}
LABEL_57:
if ( !*v5 )
goto LABEL_58;
LABEL_5:
argv = v5;
}
filename = (char *)v7;
LABEL_58:
func2();
v11 = GetFilesizeFromStream(bnrFile);
v12 = malloc(v11);
v13 = ReadDataFromFile(v12, filename);
if ( v13 != v11 )
goto LABEL_106;
v14 = (char *)GCMRawBnrToStruct(v12, v13);
v15 = v14;
if ( !v14 )
{
puts("error opening banner!");
exit(1);
}
func1(v14);
if ( *v15 == 49 )
v16 = (char *)*((_QWORD *)v15 + 769);
else
v16 = (char *)GCMBnrGetNthInfo(*((_QWORD *)v15 + 769), (unsigned int)(v3 - 1));
if ( !v16 )
{
puts("ERROR! mod == NULL (this shouldn't happen!)");
exit(1);
}
v17 = 0;
if ( src )
{
v18 = 8LL;
v19 = v16;
v17 = 1;
while ( v18 )
{
*(_DWORD *)v19 = 0;
v19 += 4;
--v18;
}
strcpy(v16, src);
}
if ( v39 )
{
v20 = 8LL;
v17 = 1;
v21 = v16 + 32;
while ( v20 )
{
*v21++ = 0;
--v20;
}
strcpy(v16 + 32, v39);
}
if ( v41 )
{
v22 = 16LL;
v17 = 1;
v23 = v16 + 64;
while ( v22 )
{
*v23++ = 0;
--v22;
}
strcpy(v16 + 64, v41);
}
if ( v42 )
{
v24 = 16LL;
v17 = 1;
v25 = v16 + 128;
while ( v24 )
{
*v25++ = 0;
--v24;
}
strcpy(v16 + 128, v42);
}
if ( v44 )
{
v26 = v16 + 192;
v27 = 32LL;
v17 = 1;
v28 = v26;
while ( v27 )
{
*(_DWORD *)v28 = 0;
v28 += 4;
--v27;
}
strcpy(v26, v44);
}
if ( v40 )
{
if ( v45 )
{
v29 = malloc(0x240DuLL);
GCMBnrGetImagePPM(v15, v29);
v30 = 9229LL;
}
else
{
v29 = malloc(0x2400uLL);
GCMBnrGetImageRaw(v15, v29);
v30 = 9216LL;
}
WriteDataToFile(v29, v30, v40);
}
if ( !s )
{
if ( v17 )
goto LABEL_96;
goto LABEL_97;
}
v31 = GetFilesizeFromPath(s);
v32 = malloc(v31);
v33 = v32;
if ( v46 )
{
puts("PPM IMPORT IS NOT IMPLEMENTED! (file not affected)\n");
exit(1);
}
if ( ReadDataFromFile(v32, s) != v31 )
{
perror(s);
exit(1);
}
v34 = (char *)malloc(0x1800uLL);
GCMBnrRawImageToGraphic(v33, v34);
*(_QWORD *)(v15 + 1) = *(_QWORD *)v34;
*(_QWORD *)(v15 + 6137) = *((_QWORD *)v34 + 767);
qmemcpy(
(void *)((unsigned __int64)(v15 + 9) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v34 - &v15[-((unsigned __int64)(v15 + 9) & 0xFFFFFFFFFFFFFFF8LL) + 1]),
8LL * (((_DWORD)v15 + 1 - (((_DWORD)v15 + 9) & 0xFFFFFFF8) + 6144) >> 3));
free(v34);
LABEL_96:
rewind(bnrFile);
v35 = GCMBnrRawSize(v15);
v36 = malloc(v35);
GCMBnrStructToRaw(v15, v36);
if ( fwrite(v36, 1uLL, v35, bnrFile) != v35 )
{
LABEL_106:
perror(filename);
exit(1);
}
LABEL_97:
GCMFreeBnrInfoStruct(*((_QWORD *)v15 + 769));
func3();
return 0;
}

original_code:
int main(int argc, char **argv) {
 char *newName = ((void *)0);
 char *newDeveloper = ((void *)0);
 char *newFullName = ((void *)0);
 char *newFullDeveloper = ((void *)0);
 char *newDescription = ((void *)0);
 char *extractIconPath = ((void *)0);
 char *injectIconPath = ((void *)0);
 int modIndex = 0;
 int extractFormat = 0;
 int injectFormat = 0;
 char *currentArg = ((void *)0);
 do {
  currentArg = *(++argv);
  if (!currentArg) {
   printUsage();
   exit(1);
  } else if (strcmp("-?", currentArg) == 0 || strcmp("--help", currentArg) == 0) {
   printExtendedUsage();
   exit(1);
  } else if (strcmp("-m", currentArg) == 0 || strcmp("--modify-nth", currentArg) == 0) {
   modIndex = atoi(*(++argv));
   if (modIndex < 1) {
    printf(" index is out of bounds! (%d)\n", modIndex);
    exit(1);
   }
  } else if (strcmp("-n", currentArg) == 0 || strcmp("--name", currentArg) == 0) {
   if (*(argv + 1)) {
    newName = *(++argv);
   }
  } else if (strcmp("-d", currentArg) == 0 || strcmp("--developer", currentArg) == 0) {
   if (*(argv + 1)) {
    newDeveloper = *(++argv);
   }
  } else if (strcmp("-N", currentArg) == 0 || strcmp("--full-name", currentArg) == 0) {
   if (*(argv + 1)) {
    newFullName = *(++argv);
   }
  } else if (strcmp("-D", currentArg) == 0 || strcmp("--full-developer", currentArg) == 0) {
   if (*(argv + 1)) {
    newFullDeveloper = *(++argv);
   }
  } else if (strcmp("-s", currentArg) == 0 || strcmp("--description", currentArg) == 0) {
   if (*(argv + 1)) {
    newDescription = *(++argv);
   }
  } else if (strcmp("-e", currentArg) == 0 || strcmp("--extract-icon", currentArg) == 0) {
   if (*(argv + 1)) {
    if (strcmp(*(argv + 1), "-raw") == 0) {
     extractFormat = 0;
     *(argv += 1);
    } else if (strcmp(*(argv + 1), "-ppm") == 0) {
     extractFormat = 1;
     *(argv += 1);
    }
    if (*(argv + 1)) {
     extractIconPath = *(++argv);
    }
   }
  } else if (strcmp("-i", currentArg) == 0 || strcmp("--icon", currentArg) == 0) {
   if (*(argv + 1)) {
    if (strcmp(*(argv + 1), "-raw") == 0) {
     injectFormat = 0;
     *(argv += 1);
    } else if (strcmp(*(argv + 1), "-ppm") == 0) {
     injectFormat = 1;
     *(argv += 1);
    }
    if (*(argv + 1)) {
     injectIconPath = *(++argv);
    }
   }
  } else {
   filename = currentArg;
   break;
  }
 }while (*argv);
 openBnr();
 unsigned long len = GetFilesizeFromStream(bnrFile);
 char *data = (char*)malloc(len);
 if (ReadDataFromFile(data, filename) != len) {
  perror(filename);
  exit(1);
 }
 GCMBnrStruct *b = GCMRawBnrToStruct(data, len);
 if (!b) {
  printf("error opening banner!\n");
  exit(1);
 }
 printBnr(b);
 int fileChanged = 0;
 GCMBnrInfoStruct *mod = ((void *)0);
 if (b->version != '1') {
  mod = GCMBnrGetNthInfo(b->info, modIndex - 1);
 } else {
  mod = b->info;
 }
 if (!mod) {
  printf("ERROR! mod == NULL (this shouldn't happen!)\n");
  exit(1);
 }
 if (newName != ((void *)0)) {
  bzero(mod->name, 0x0020);
  strcpy(mod->name, newName);
  fileChanged = 1;
 }
 if (newDeveloper != ((void *)0)) {
  bzero(mod->developer, 0x0020);
  strcpy(mod->developer, newDeveloper);
  fileChanged = 1;
 }
 if (newFullName != ((void *)0)) {
  bzero(mod->fullName, 0x0040);
  strcpy(mod->fullName, newFullName);
  fileChanged = 1;
 }
 if (newFullDeveloper != ((void *)0)) {
  bzero(mod->fullDeveloper, 0x0040);
  strcpy(mod->fullDeveloper, newFullDeveloper);
  fileChanged = 1;
 }
 if (newDescription != ((void *)0)) {
  bzero(mod->description, 0x0080);
  strcpy(mod->description, newDescription);
  fileChanged = 1;
 }
 if (extractIconPath != ((void *)0)) {
  char *imageData = ((void *)0);
  u32 len;
  if (extractFormat == 0) {
   len = 3 * 96 * 32;
   imageData = (char*)malloc(len);
   GCMBnrGetImageRaw(b, imageData);
  } else {
   len = 3 * 96 * 32 + 13;
   imageData = (char*)malloc(len);
   GCMBnrGetImagePPM(b, imageData);
  }
  WriteDataToFile(imageData, len, extractIconPath);
 }
 if (injectIconPath != ((void *)0)) {
  u32 len = GetFilesizeFromPath(injectIconPath);
  char *imageData = (char*)malloc(len);
  if (injectFormat == 0) {
   if (ReadDataFromFile(imageData, injectIconPath) != len) {
    perror(injectIconPath);
    exit(1);
   }
  } else {
   printf("PPM IMPORT IS NOT IMPLEMENTED! (file not affected)\n\n");
   exit(1);
  }
  char *iconData = (char*)malloc(0x1800);
  GCMBnrRawImageToGraphic(imageData, iconData);
  memcpy(b->graphic, iconData, 0x1800);
  free(iconData);
  fileChanged = 1;
 }
 if (fileChanged) {
  rewind(bnrFile);
  u32 bnrSize = GCMBnrRawSize(b);
  char *buf = (char*)malloc(bnrSize);
  GCMBnrStructToRaw(b, buf);
  if (fwrite(buf, 1, bnrSize, bnrFile) != bnrSize) {
   perror(filename);
   exit(1);
  }
 }
 GCMFreeBnrInfoStruct(b->info);
 closeBnr();
 return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
 char *newName = ((void *)0);
 char *newDeveloper = ((void *)0);
 char *newFullName = ((void *)0);
 char *newFullDeveloper = ((void *)0);
 char *newDescription = ((void *)0);
 char *extractIconPath = ((void *)0);
 char *injectIconPath = ((void *)0);
 int modIndex = 0;
 int extractFormat = 0;
 int injectFormat = 0;
 char *currentArg = ((void *)0);
 do {
  currentArg = *(++argv);
  if (!currentArg) {
   printUsage();
   exit(1);
  } else if (strcmp("-?", currentArg) == 0 || strcmp("--help", currentArg) == 0) {
   printExtendedUsage();
   exit(1);
  } else if (strcmp("-m", currentArg) == 0 || strcmp("--modify-nth", currentArg) == 0) {
   modIndex = atoi(*(++argv));
   if (modIndex < 1) {
    printf(" index is out of bounds! (%d)\n", modIndex);
    exit(1);
   }
  } else if (strcmp("-n", currentArg) == 0 || strcmp("--name", currentArg) == 0) {
   if (*(argv + 1)) {
    newName = *(++argv);
   }
  } else if (strcmp("-d", currentArg) == 0 || strcmp("--developer", currentArg) == 0) {
   if (*(argv + 1)) {
    newDeveloper = *(++argv);
   }
  } else if (strcmp("-N", currentArg) == 0 || strcmp("--full-name", currentArg) == 0) {
   if (*(argv + 1)) {
    newFullName = *(++argv);
   }
  } else if (strcmp("-D", currentArg) == 0 || strcmp("--full-developer", currentArg) == 0) {
   if (*(argv + 1)) {
    newFullDeveloper = *(++argv);
   }
  } else if (strcmp("-s", currentArg) == 0 || strcmp("--description", currentArg) == 0) {
   if (*(argv + 1)) {
    newDescription = *(++argv);
   }
  } else if (strcmp("-e", currentArg) == 0 || strcmp("--extract-icon", currentArg) == 0) {
   if (*(argv + 1)) {
    if (strcmp(*(argv + 1), "-raw") == 0) {
     extractFormat = 0;
     *(argv += 1);
    } else if (strcmp(*(argv + 1), "-ppm") == 0) {
     extractFormat = 1;
     *(argv += 1);
    }
    if (*(argv + 1)) {
     extractIconPath = *(++argv);
    }
   }
  } else if (strcmp("-i", currentArg) == 0 || strcmp("--icon", currentArg) == 0) {
   if (*(argv + 1)) {
    if (strcmp(*(argv + 1), "-raw") == 0) {
     injectFormat = 0;
     *(argv += 1);
    } else if (strcmp(*(argv + 1), "-ppm") == 0) {
     injectFormat = 1;
     *(argv += 1);
    }
    if (*(argv + 1)) {
     injectIconPath = *(++argv);
    }
   }
  } else {
   filename = currentArg;
   break;
  }
 }while (*argv);
 openBnr();
 unsigned long len = GetFilesizeFromStream(bnrFile);
 char *data = (char*)malloc(len);
 if (ReadDataFromFile(data, filename) != len) {
  perror(filename);
  exit(1);
 }
 struct gcm_bnr_struct *b = GCMRawBnrToStruct(data, len);
 if (!b) {
  printf("error opening banner!\n");
  exit(1);
 }
 printBnr(b);
 int fileChanged = 0;
 struct gcm_bnr_info_record_struct *mod = ((void *)0);
 if (b->version != '1') {
  mod = GCMBnrGetNthInfo(b->info, modIndex - 1);
 } else {
  mod = b->info;
 }
 if (!mod) {
  printf("ERROR! mod == NULL (this shouldn't happen!)\n");
  exit(1);
 }
 if (newName != ((void *)0)) {
  bzero(mod->name, 0x0020);
  strcpy(mod->name, newName);
  fileChanged = 1;
 }
 if (newDeveloper != ((void *)0)) {
  bzero(mod->developer, 0x0020);
  strcpy(mod->developer, newDeveloper);
  fileChanged = 1;
 }
 if (newFullName != ((void *)0)) {
  bzero(mod->fullName, 0x0040);
  strcpy(mod->fullName, newFullName);
  fileChanged = 1;
 }
 if (newFullDeveloper != ((void *)0)) {
  bzero(mod->fullDeveloper, 0x0040);
  strcpy(mod->fullDeveloper, newFullDeveloper);
  fileChanged = 1;
 }
 if (newDescription != ((void *)0)) {
  bzero(mod->description, 0x0080);
  strcpy(mod->description, newDescription);
  fileChanged = 1;
 }
 if (extractIconPath != ((void *)0)) {
  char *imageData = ((void *)0);
  unsigned long len;
  if (extractFormat == 0) {
   len = 3 * 96 * 32;
   imageData = (char *)malloc(len);
   GCMBnrGetImageRaw(b, imageData);
  } else {
   len = 3 * 96 * 32 + 13;
   imageData = (char *)malloc(len);
   GCMBnrGetImagePPM(b, imageData);
  }
  WriteDataToFile(imageData, len, extractIconPath);
 }
 if (injectIconPath != ((void *)0)) {
  unsigned long len = GetFilesizeFromPath(injectIconPath);
  char *imageData = (char*)malloc(len);
  if (injectFormat == 0) {
   if (ReadDataFromFile(imageData, injectIconPath) != len) {
    perror(injectIconPath);
    exit(1);
   }
  } else {
   printf("PPM IMPORT IS NOT IMPLEMENTED! (file not affected)\n\n");
   exit(1);
  }
  char *iconData = (char*)malloc(0x1800);
  GCMBnrRawImageToGraphic(imageData, iconData);
  memcpy(b->graphic, iconData, 0x1800);
  free(iconData);
  fileChanged = 1;
 }
 if (fileChanged) {
  rewind(bnrFile);
  unsigned long bnrSize = GCMBnrRawSize(b);
  char *buf = (char*)malloc(bnrSize);
  GCMBnrStructToRaw(b, buf);
  if (fwrite(buf, 1, bnrSize, bnrFile) != bnrSize) {
   perror(filename);
   exit(1);
  }
 }
 GCMFreeBnrInfoStruct(b->info);
 closeBnr();
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
newName
char *

variable_types:
newDeveloper
char *

variable_types:
newFullName
char *

variable_types:
newFullDeveloper
char *

variable_types:
newDescription
char *

variable_types:
extractIconPath
char *

variable_types:
injectIconPath
char *

variable_types: modIndex int

variable_types: extractFormat int

variable_types: injectFormat int

variable_types:
currentArg
char *

variable_types:
len
unsigned long

variable_types:
data
char *

variable_types: fileChanged int

variable_types:
imageData
char *

variable_types:
iconData
char *

variable_types:
bnrSize
unsigned long

user_defined_types: struct gcm_bnr_info_record_struct { char name[32]; char developer[32]; char fullName[64]; char fullDeveloper[64]; char description[128]; struct gcm_bnr_info_record_struct *next; }


GCMNewApploaderStruct

decompiled_code:
_QWORD *GCMNewApploaderStruct()
{
_QWORD *result; // rax

result = malloc(0x30uLL);
result[2] = 0LL;
result[3] = 0LL;
result[4] = 0LL;
result[5] = 0LL;
*(_OWORD *)result = 0LL;
return result;
}

canonical_decompiled_code:
_QWORD *func1()
{
_QWORD *result; // rax

result = malloc(0x30uLL);
result[2] = 0LL;
result[3] = 0LL;
result[4] = 0LL;
result[5] = 0LL;
*(_OWORD *)result = 0LL;
return result;
}

original_code:
GCMApploaderStruct *GCMNewApploaderStruct() {
 GCMApploaderStruct *a = (GCMApploaderStruct*)malloc(sizeof(GCMApploaderStruct));
 bzero(a->date, 0x0010);
 a->entrypoint = 0;
 a->size = 0;
 a->unknown = 0;
 a->code = ((void *)0);
 return a;
}

canonical_original_code:
struct gcm_apploader_struct *GCMNewApploaderStruct() {
 struct gcm_apploader_struct *a = (GCMApploaderStruct*)malloc(sizeof(GCMApploaderStruct));
 bzero(a->date, 0x0010);
 a->entrypoint = 0;
 a->size = 0;
 a->unknown = 0;
 a->code = ((void *)0);
 return a;
}

variable_types: struct gcm_apploader_struct { char date[16]; unsigned long entrypoint; unsigned long size; unsigned long unknown; char *code; } *


GCMApploaderStructToRaw

original_code:
void GCMApploaderStructToRaw(GCMApploaderStruct *a, char *buf) {
 if (!a) return;
 char *start = buf;
 memcpy(buf, a->date, 0x0010);
 buf += 0x0010;
 u32 *entrypoint = (u32*)malloc(sizeof(u32));
 *entrypoint = htonl(a->entrypoint);
 memcpy(buf, entrypoint, 0x0004);
 free(entrypoint);
 buf += 0x0004;
 u32 *size = (u32*)malloc(sizeof(u32));
 *size = htonl(a->size);
 memcpy(buf, size, 0x0004);
 free(size);
 buf += 0x0004;
 u32 *unknown = (u32*)malloc(sizeof(u32));
 *unknown = htonl(a->unknown);
 memcpy(buf, unknown, 0x0004);
 free(unknown);
 buf += 0x0004;
 buf += 4;
 memcpy(buf, a->code, (a->size));
 buf = start;
}

canonical_original_code:
void GCMApploaderStructToRaw(struct gcm_apploader_struct *a, char *buf) {
 if (!a) return;
 char *start = buf;
 memcpy(buf, a->date, 0x0010);
 buf += 0x0010;
 unsigned long *entrypoint = (u32*)malloc(sizeof(u32));
 *entrypoint = htonl(a->entrypoint);
 memcpy(buf, entrypoint, 0x0004);
 free(entrypoint);
 buf += 0x0004;
 unsigned long *size = (u32*)malloc(sizeof(u32));
 *size = htonl(a->size);
 memcpy(buf, size, 0x0004);
 free(size);
 buf += 0x0004;
 unsigned long *unknown = (u32*)malloc(sizeof(u32));
 *unknown = htonl(a->unknown);
 memcpy(buf, unknown, 0x0004);
 free(unknown);
 buf += 0x0004;
 buf += 4;
 memcpy(buf, a->code, (a->size));
 buf = start;
}

variable_types:
buf
char *

variable_types:
start
char *

variable_types:
entrypoint
unsigned long *

variable_types:
size
unsigned long *

variable_types:
unknown
unsigned long *

user_defined_types: struct gcm_apploader_struct { char date[16]; unsigned long entrypoint; unsigned long size; unsigned long unknown; char *code; }


GCMFreeApploaderStruct

decompiled_code:
void __fastcall GCMFreeApploaderStruct(void *ptr)
{
void *v2; // rdi

if ( ptr )
{
v2 = (void *)*((_QWORD *)ptr + 5);
if ( v2 )
free(v2);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func3(void *ptr)
{
void *v2; // rdi

if ( ptr )
{
v2 = (void *)*((_QWORD *)ptr + 5);
if ( v2 )
free(v2);
free(ptr);
}
}

original_code:
void GCMFreeApploaderStruct(GCMApploaderStruct *a) {
 if (!a) return;
 if (a->code != ((void *)0))
  free(a->code);
 free(a);
}

canonical_original_code:
void GCMFreeApploaderStruct(struct gcm_apploader_struct *a) {
 if (!a) return;
 if (a->code != ((void *)0))
  free(a->code);
 free(a);
}


GCMFreeBnrInfoStruct

decompiled_code:
void __fastcall GCMFreeBnrInfoStruct(_QWORD *a1)
{
if ( a1[40] )
GCMFreeBnrInfoStruct();
free(a1);
}

canonical_decompiled_code:
void __fastcall func0(_QWORD *a1)
{
if ( a1[40] )
func0();
free(a1);
}

original_code:
void GCMFreeBnrInfoStruct(GCMBnrInfoStruct *r) {
 if (r->next) {
  GCMFreeBnrInfoStruct(r->next);
 }
 free(r);
}

canonical_original_code:
void GCMFreeBnrInfoStruct(struct gcm_bnr_info_record_struct *r) {
 if (r->next) {
  GCMFreeBnrInfoStruct(r->next);
 }
 free(r);
}

variable_types: struct gcm_bnr_info_record_struct { char name[32]; char developer[32]; char fullName[64]; char fullDeveloper[64]; char description[128]; struct gcm_bnr_info_record_struct *next; } *


GCMBnrInfoCount

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

v1 = *(_QWORD *)(a1 + 320);
if ( !v1 )
return 1LL;
v2 = 1;
do
{
v1 = *(_QWORD *)(v1 + 320);
++v2;
}
while ( v1 );
return v2;
}

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

v1 = *(_QWORD *)(a1 + 320);
if ( !v1 )
return 1LL;
v2 = 1;
do
{
v1 = *(_QWORD *)(v1 + 320);
++v2;
}
while ( v1 );
return v2;
}

original_code:
int GCMBnrInfoCount(GCMBnrInfoStruct *r) {
 if (r->next) {
  return 1 + GCMBnrInfoCount(r->next);
 } else {
  return 1;
 }
}

canonical_original_code:
int GCMBnrInfoCount(struct gcm_bnr_info_record_struct *r) {
 if (r->next) {
  return 1 + GCMBnrInfoCount(r->next);
 } else {
  return 1;
 }
}


GCMBnrGetNthInfo

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

for ( result = a1; a2; --a2 )
{
result = *(_QWORD *)(result + 320);
if ( !result )
break;
}
return result;
}

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

for ( result = a1; a2; --a2 )
{
result = *(_QWORD *)(result + 320);
if ( !result )
break;
}
return result;
}

original_code:
GCMBnrInfoStruct *GCMBnrGetNthInfo(GCMBnrInfoStruct *r, int n) {
 if (n == 0) {
  return r;
 } else if (r->next) {
  return GCMBnrGetNthInfo(r->next, n - 1);
 } else {
  return ((void *)0);
 }
}

canonical_original_code:
struct gcm_bnr_info_record_struct *GCMBnrGetNthInfo(struct gcm_bnr_info_record_struct *r, int n) {
 if (n == 0) {
  return r;
 } else if (r->next) {
  return GCMBnrGetNthInfo(r->next, n - 1);
 } else {
  return ((void *)0);
 }
}

variable_types:
r
struct gcm_bnr_info_record_struct *

variable_types: n int

return_type: struct gcm_bnr_info_record_struct { char name[32]; char developer[32]; char fullName[64]; char fullDeveloper[64]; char description[128]; struct gcm_bnr_info_record_struct *next; } *


initRecursion

decompiled_code:
FILE *initRecursion()
{
int v0; // eax
int v1; // edi
FILE *result; // rax

strcpy((char *)&word_FD0, "XX");
*(__m128i *)fstTempFilename = _mm_load_si128((const __m128i *)&LC3);
v0 = mkstemp(fstTempFilename);
if ( v0 == -1 || (fstTempFile = fdopen(v0, "w+")) == 0LL )
{
perror("ERROR OPENING TEMP FILE");
exit(1);
}
strcpy((char *)&dword_F90, "XXXXXX");
*(__m128i *)stringTableTempFilename = _mm_load_si128((const __m128i *)&LC4);
v1 = mkstemp(stringTableTempFilename);
if ( v1 == -1 || (result = fdopen(v1, "w+"), (stringTableTempFile = result) == 0LL) )
{
perror("ERROR OPENING TEMP FILE!");
exit(1);
}
return result;
}

canonical_decompiled_code:
FILE *func1()
{
int v0; // eax
int v1; // edi
FILE *result; // rax

strcpy((char *)&word_FD0, "XX");
*(__m128i *)fstTempFilename = _mm_load_si128((const __m128i *)&LC3);
v0 = mkstemp(fstTempFilename);
if ( v0 == -1 || (fstTempFile = fdopen(v0, "w+")) == 0LL )
{
perror("ERROR OPENING TEMP FILE");
exit(1);
}
strcpy((char *)&dword_F90, "XXXXXX");
*(__m128i *)stringTableTempFilename = _mm_load_si128((const __m128i *)&LC4);
v1 = mkstemp(stringTableTempFilename);
if ( v1 == -1 || (result = fdopen(v1, "w+"), (stringTableTempFile = result) == 0LL) )
{
perror("ERROR OPENING TEMP FILE!");
exit(1);
}
return result;
}

original_code:
static void initRecursion() {
 int fst_fd = -1;
 int stringTable_fd = -1;
 strcpy(fstTempFilename, "fstData.tmp.XXXXXX");
 if (((fst_fd = mkstemp(fstTempFilename)) == -1) || !(fstTempFile = fdopen(fst_fd, "w+"))) {
  perror("ERROR OPENING TEMP FILE");
  exit(1);
 }
 strcpy(stringTableTempFilename, "stringtable.tmp.XXXXXX");
 if (((stringTable_fd = mkstemp(stringTableTempFilename)) == -1) || !(stringTableTempFile = fdopen(stringTable_fd, "w+"))) {
  perror("ERROR OPENING TEMP FILE!");
  exit(1);
 }
}


writeStringToTempFile

decompiled_code:
size_t __fastcall writeStringToTempFile(void *ptr)
{
size_t v1; // rbx

v1 = (int)(strlen((const char *)ptr) + 1);
if ( v1 != fwrite(ptr, 1uLL, v1, stringTableTempFile) )
{
perror("Error writing to string table!\n");
exit(1);
}
return ftell(stringTableTempFile) - v1;
}

canonical_decompiled_code:
size_t __fastcall func2(void *ptr)
{
size_t v1; // rbx

v1 = (int)(strlen((const char *)ptr) + 1);
if ( v1 != fwrite(ptr, 1uLL, v1, stringTableTempFile) )
{
perror("Error writing to string table!\n");
exit(1);
}
return ftell(stringTableTempFile) - v1;
}

original_code:
static u32 writeStringToTempFile(char *string) {
 int len = strlen(string) + 1;
 if (fwrite(string, 1, len, stringTableTempFile) != len) {
  perror("Error writing to string table!\n");
  exit(1);
 }
 return ftell(stringTableTempFile) - len;
}

canonical_original_code:
static unsigned long writeStringToTempFile(char *string) {
 int len = strlen(string) + 1;
 if (fwrite(string, 1, len, stringTableTempFile) != len) {
  perror("Error writing to string table!\n");
  exit(1);
 }
 return ftell(stringTableTempFile) - len;
}

variable_types:
string
char *

variable_types: len int


lastPathComponent

decompiled_code:
const char *__fastcall lastPathComponent(const char *a1)
{
char *v2; // rax
char *v3; // rdi
bool v4; // zf
const char *result; // rax

v2 = rindex(a1, 47);
v3 = v2 + 1;
v4 = v2 == 0LL;
result = a1;
if ( !v4 )
result = v3;
return result;
}

canonical_decompiled_code:
const char *__fastcall func1(const char *a1)
{
char *v2; // rax
char *v3; // rdi
bool v4; // zf
const char *result; // rax

v2 = rindex(a1, 47);
v3 = v2 + 1;
v4 = v2 == 0LL;
result = a1;
if ( !v4 )
result = v3;
return result;
}

original_code:
char *lastPathComponent(char *source) {
 char *last = rindex(source, '/');
 if (last && last != '\0') {
  *last++;
  return last;
 } else {
  return source;
 }
}

variable_types:
source
char *

variable_types:
last
char *

return_type: char *


nthPathComponent

decompiled_code:
char *__fastcall nthPathComponent(char *s, int a2, char *a3)
{
char *v4; // r12
int v5; // er15
size_t v6; // rax
void *v7; // r13
char *v8; // rax
char *v9; // r12
_BYTE *v10; // rdx
char v11; // al

if ( !s || !a3 )
return 0LL;
v4 = s;
v5 = 0;
v6 = strlen(s);
v7 = malloc(v6 + 1);
if ( a2 > 0 )
{
while ( 1 )
{
v8 = index(v4, 47);
v9 = v8;
if ( !v8 )
break;
++v5;
v4 = v8 + 1;
if ( a2 == v5 )
goto LABEL_8;
}
puts("not enough path components!!!!!");
free(v7);
}
else
{
LABEL_8:
v10 = v7;
do
{
v11 = *v4;
if ( *v4 == 47 )
break;
++v10;
++v4;
*(v10 - 1) = v11;
}
while ( v11 );
*v10 = 0;
v9 = a3;
strcpy(a3, (const char *)v7);
free(v7);
}
return v9;
}

canonical_decompiled_code:
char *__fastcall func2(char *s, int a2, char *a3)
{
char *v4; // r12
int v5; // er15
size_t v6; // rax
void *v7; // r13
char *v8; // rax
char *v9; // r12
_BYTE *v10; // rdx
char v11; // al

if ( !s || !a3 )
return 0LL;
v4 = s;
v5 = 0;
v6 = strlen(s);
v7 = malloc(v6 + 1);
if ( a2 > 0 )
{
while ( 1 )
{
v8 = index(v4, 47);
v9 = v8;
if ( !v8 )
break;
++v5;
v4 = v8 + 1;
if ( a2 == v5 )
goto LABEL_8;
}
puts("not enough path components!!!!!");
free(v7);
}
else
{
LABEL_8:
v10 = v7;
do
{
v11 = *v4;
if ( *v4 == 47 )
break;
++v10;
++v4;
*(v10 - 1) = v11;
}
while ( v11 );
*v10 = 0;
v9 = a3;
strcpy(a3, (const char *)v7);
free(v7);
}
return v9;
}

original_code:
char *nthPathComponent(char *source, int n, char *buf) {
 if (!source || !buf) return ((void *)0);
 int i = 0;
 char *pathItem = (char*)malloc(strlen(source) + 1);
 char *os1 = pathItem;
 char *s = source;
 for (i = 0; (i < n) && (s = index(s, '/')); i++, *s++);
 if (!s) {
  printf("not enough path components!!!!!\n");
  free(pathItem);
  return ((void *)0);
 }
 while (*s != '/' && (*os1++ = *s++));
 *os1++ = 0;
 strcpy(buf, pathItem);
 free(pathItem);
 return buf;
}

variable_types: i int

variable_types:
pathItem
char *

variable_types:
s
char *


pathComponentCount

decompiled_code:
__int64 __fastcall pathComponentCount(char *a1)
{
char v1; // al
unsigned int i; // er8

v1 = *a1;
for ( i = 0; *a1; v1 = *a1 )
{
++a1;
i += v1 == 47;
}
return i;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *a1)
{
char v1; // al
unsigned int i; // er8

v1 = *a1;
for ( i = 0; *a1; v1 = *a1 )
{
++a1;
i += v1 == 47;
}
return i;
}

original_code:
int pathComponentCount(char *source) {
 int count = 0;
 while(*source) {
  if (*source++ == '/') {
   count++;
  }
 }
 return count;
}

variable_types: count int


GCMSystemIDToStr

decompiled_code:
const char *__fastcall GCMSystemIDToStr(char a1)
{
const char *result; // rax

result = "Gamecube";
if ( a1 != 71 )
{
result = "Other";
if ( a1 == 85 )
result = "GBA Player";
}
return result;
}

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

result = "Gamecube";
if ( a1 != 71 )
{
result = "Other";
if ( a1 == 85 )
result = "GBA Player";
}
return result;
}

original_code:
char *GCMSystemIDToStr(char sysID) {
 switch (sysID) {
 case 'G':
  return "Gamecube";
 case 'U':
  return "GBA Player";
 default:
  return "Other";
 }
}

variable_types: sysID char