main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
int result; // eax
struct sigaction v5; // [rsp+0h] [rbp-A8h] BYREF

if ( argc == 1 )
{
v3 = getpid();
if ( v3 == -1 )
{
result = error("Error on getpid\n");
}
else
{
ft_putstr("Le PID du serveur est : ");
ft_putnbr(v3);
ft_putchar(10);
v5.sa_flags = 4;
v5.sa_handler = (__sighandler_t)receive_signal;
if ( sigaction(10, &v5, 0LL) != -1 && sigaction(12, &v5, 0LL) != -1 )
{
while ( 1 )
pause();
}
result = error("Error on sigaction\n");
}
}
else
{
show_usage((void *)*argv);
result = 1;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
int result; // eax
struct sigaction v5; // [rsp+0h] [rbp-A8h] BYREF

if ( argc == 1 )
{
v3 = getpid();
if ( v3 == -1 )
{
result = func10("Error on getpid\n");
}
else
{
func9("Le PID du serveur est : ");
func11(v3);
func7(10);
v5.sa_flags = 4;
v5.sa_handler = (__sighandler_t)receive_signal;
if ( sigaction(10, &v5, 0LL) != -1 && sigaction(12, &v5, 0LL) != -1 )
{
while ( 1 )
pause();
}
result = func10("Error on sigaction\n");
}
}
else
{
func13((void *)*argv);
result = 1;
}
return result;
}

original_code:
int main(int argc, char **argv)
{
 int pid;
 struct sigaction catch;
 if (argc == 1)
 {
  if ((pid = getpid()) == -1)
   return (error("Error on getpid\n"));
  ft_putstr("Le PID du serveur est : ");
  ft_putnbr(pid);
  ft_putchar('\n');
  catch.sa_flags = 4;
  catch.__sigaction_handler.sa_sigaction = receive_signal;
  if ((sigaction(10, &catch, 0)) == -1)
   return (error("Error on sigaction\n"));
  if ((sigaction(12, &catch, 0)) == -1)
   return (error("Error on sigaction\n"));
  while (42)
   pause();
 }
 else
 {
  show_usage(argv[0]);
  return (1);
 }
 return (0);
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }


repo: villavanschaeck/schaeckeling

decompiled_code:
__int64 __fastcall midi_to_input_index(unsigned int a1)
{
if ( a1 > 0x7F )
_assert_fail("channel >= 0 && channel < MIDI_CHANNELS", "schaeckeling.h", 0xBu, "midi_to_input_index");
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1)
{
if ( a1 > 0x7F )
_assert_fail("channel >= 0 && channel < MIDI_CHANNELS", "schaeckeling.h", 0xBu, "midi_to_input_index");
return a1;
}

original_code:
static inline inputidx_t
midi_to_input_index(midichannel_t channel) {
 ((channel >= 0 && channel < 128) ? (void) (0) : __assert_fail ("channel >= 0 && channel < MIDI_CHANNELS", "schaeckeling.h", 11, __extension__ __PRETTY_FUNCTION__));
 return channel;
}

canonical_original_code:
static inline int midi_to_input_index(int channel) {
 ((channel >= 0 && channel < 128) ? (void) (0) : __assert_fail ("channel >= 0 && channel < MIDI_CHANNELS", "schaeckeling.h", 11, __extension__ __PRETTY_FUNCTION__));
 return channel;
}

variable_types: channel int

return_type: int


decompiled_code:
__int64 __fastcall dmx_to_input_index(int a1)
{
if ( (unsigned int)(a1 - 1) > 0x1FF )
_assert_fail("channel > 0 && channel <= DMX_CHANNELS", "schaeckeling.h", 0x11u, "dmx_to_input_index");
return (unsigned int)(a1 + 127);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
if ( (unsigned int)(a1 - 1) > 0x1FF )
_assert_fail("channel > 0 && channel <= DMX_CHANNELS", "schaeckeling.h", 0x11u, "dmx_to_input_index");
return (unsigned int)(a1 + 127);
}

original_code:
static inline inputidx_t
dmx_to_input_index(dmxchannel_t channel) {
 ((channel > 0 && channel <= 512) ? (void) (0) : __assert_fail ("channel > 0 && channel <= DMX_CHANNELS", "schaeckeling.h", 17, __extension__ __PRETTY_FUNCTION__));
 return channel + 128 - 1;
}

canonical_original_code:
static inline int dmx_to_input_index(int channel) {
 ((channel > 0 && channel <= 512) ? (void) (0) : __assert_fail ("channel > 0 && channel <= DMX_CHANNELS", "schaeckeling.h", 17, __extension__ __PRETTY_FUNCTION__));
 return channel + 128 - 1;
}


decompiled_code:
__int64 __fastcall dmx_changed(unsigned int a1, unsigned __int8 a2, unsigned __int8 a3)
{
unsigned int v3; // ebp
unsigned int v4; // eax

if ( a1 > 0x1FF )
_assert_fail("channel >= 0 && channel < DMX_CHANNELS", "input.c", 0x2Eu, "dmx_changed");
v3 = a3;
fprintf(stdout, "dmx_changed(%d, %d, %d)\n", a1 + 1, a2, a3);
receiving_changes = 1;
v4 = dmx_to_input_index(a1 + 1);
return update_input(v4, v3);
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1, unsigned __int8 a2, unsigned __int8 a3)
{
unsigned int v3; // ebp
unsigned int v4; // eax

if ( a1 > 0x1FF )
_assert_fail("channel >= 0 && channel < DMX_CHANNELS", "input.c", 0x2Eu, "dmx_changed");
v3 = a3;
fprintf(stdout, "dmx_changed(%d, %d, %d)\n", a1 + 1, a2, a3);
receiving_changes = 1;
v4 = func1(a1 + 1);
return update_input(v4, v3);
}

original_code:
void
dmx_changed(dmxchannel_t channel, unsigned char old, unsigned char new) {
 ((channel >= 0 && channel < 512) ? (void) (0) : __assert_fail ("channel >= 0 && channel < DMX_CHANNELS", "input.c", 46, __extension__ __PRETTY_FUNCTION__));
 ++channel;
 fprintf(stdout, "dmx_changed(%d, %d, %d)\n", channel, (int)old, (int)new);
 receiving_changes = 1;
 update_input(dmx_to_input_index(channel), new);
}

canonical_original_code:
void dmx_changed(int channel, unsigned char old, unsigned char new) {
 ((channel >= 0 && channel < 512) ? (void) (0) : __assert_fail ("channel >= 0 && channel < DMX_CHANNELS", "input.c", 46, __extension__ __PRETTY_FUNCTION__));
 ++channel;
 fprintf(stdout, "dmx_changed(%d, %d, %d)\n", channel, (int)old, (int)new);
 receiving_changes = 1;
 update_input(dmx_to_input_index(channel), new);
}

variable_types:
old
unsigned char

variable_types:
new
unsigned char

return_type: void


decompiled_code:
__int64 dmx_input_completed()
{
receiving_changes = 0;
flush_dmxout_sendbuf();
return update_websockets(0LL, 1LL);
}

canonical_decompiled_code:
__int64 func3()
{
receiving_changes = 0;
flush_dmxout_sendbuf();
return update_websockets(0LL, 1LL);
}

original_code:
void
dmx_input_completed(void) {
 receiving_changes = 0;
 flush_dmxout_sendbuf();
 update_websockets(0, 1);
}

canonical_original_code:
void dmx_input_completed() {
 receiving_changes = 0;
 flush_dmxout_sendbuf();
 update_websockets(0, 1);
}


decompiled_code:
__int64 __fastcall mk2c_error(unsigned int a1)
{
fprintf(stderr, "mk2c_error: %d\n", a1);
mk2c_lost = 1;
return error_step();
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1)
{
fprintf(stderr, "mk2c_error: %d\n", a1);
mk2c_lost = 1;
return error_step();
}

original_code:
void
mk2c_error(int error) {
 fprintf(stderr, "mk2c_error: %d\n", error);
 mk2c_lost = 1;
 error_step();
}

variable_types: error int


decompiled_code:
__int64 __fastcall midi_changed(unsigned int a1, char a2)
{
unsigned int v2; // eax

if ( a1 > 0x7F )
_assert_fail("channel >= 0 && channel < MIDI_CHANNELS", "input.c", 0x1Bu, "midi_changed");
if ( a2 < 0 )
_assert_fail("value <= 127", "input.c", 0x1Cu, "midi_changed");
fprintf(stdout, "midi_changed(%d, %d)\n", a1, (unsigned __int8)(2 * a2));
receiving_changes = 1;
v2 = midi_to_input_index(a1);
return update_input(v2, (unsigned __int8)(2 * a2));
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1, char a2)
{
unsigned int v2; // eax

if ( a1 > 0x7F )
_assert_fail("channel >= 0 && channel < MIDI_CHANNELS", "input.c", 0x1Bu, "midi_changed");
if ( a2 < 0 )
_assert_fail("value <= 127", "input.c", 0x1Cu, "midi_changed");
fprintf(stdout, "midi_changed(%d, %d)\n", a1, (unsigned __int8)(2 * a2));
receiving_changes = 1;
v2 = func0(a1);
return update_input(v2, (unsigned __int8)(2 * a2));
}

original_code:
void
midi_changed(midichannel_t channel, unsigned char value) {
 ((channel >= 0 && channel < 128) ? (void) (0) : __assert_fail ("channel >= 0 && channel < MIDI_CHANNELS", "input.c", 27, __extension__ __PRETTY_FUNCTION__));
 ((value <= 127) ? (void) (0) : __assert_fail ("value <= 127", "input.c", 28, __extension__ __PRETTY_FUNCTION__));
 value *= 2;
 fprintf(stdout, "midi_changed(%d, %d)\n", channel, (int)value);
 receiving_changes = 1;
 update_input(midi_to_input_index(channel), value);
}

canonical_original_code:
void midi_changed(int channel, unsigned char value) {
 ((channel >= 0 && channel < 128) ? (void) (0) : __assert_fail ("channel >= 0 && channel < MIDI_CHANNELS", "input.c", 27, __extension__ __PRETTY_FUNCTION__));
 ((value <= 127) ? (void) (0) : __assert_fail ("value <= 127", "input.c", 28, __extension__ __PRETTY_FUNCTION__));
 value *= 2;
 fprintf(stdout, "midi_changed(%d, %d)\n", channel, (int)value);
 receiving_changes = 1;
 update_input(midi_to_input_index(channel), value);
}

variable_types:
value
unsigned char


decompiled_code:
__int64 midi_input_completed()
{
receiving_changes = 0;
return flush_dmxout_sendbuf();
}

canonical_decompiled_code:
__int64 func6()
{
receiving_changes = 0;
return flush_dmxout_sendbuf();
}

original_code:
void
midi_input_completed(void) {
 receiving_changes = 0;
 flush_dmxout_sendbuf();
}

canonical_original_code:
void midi_input_completed() {
 receiving_changes = 0;
 flush_dmxout_sendbuf();
}


decompiled_code:
__int64 __fastcall nanokontrol_error(unsigned int a1)
{
fprintf(stderr, "nanokontrol_error: %d\n", a1);
nanokontrol_lost = 1;
return error_step();
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int a1)
{
fprintf(stderr, "nanokontrol_error: %d\n", a1);
nanokontrol_lost = 1;
return error_step();
}

original_code:
void
nanokontrol_error(int error) {
 fprintf(stderr, "nanokontrol_error: %d\n", error);
 nanokontrol_lost = 1;
 error_step();
}


decompiled_code:
__int64 __fastcall generic_midi_error(unsigned int a1)
{
fprintf(stderr, "generic_midi_error: %d\n", a1);
midi_lost = 1;
return error_step();
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned int a1)
{
fprintf(stderr, "generic_midi_error: %d\n", a1);
midi_lost = 1;
return error_step();
}

original_code:
void
generic_midi_error(int error) {
 fprintf(stderr, "generic_midi_error: %d\n", error);
 midi_lost = 1;
 error_step();
}


decompiled_code:
__int64 reconnect_if_needed()
{
if ( !mk2c_lost )
return (unsigned int)nanokontrol_lost;
if ( mk2c )
teardown_dmx_usb_mk2_pro();
mk2c = init_dmx_usb_mk2_pro(dmx_changed, dmx_input_completed, mk2c_error);
if ( mk2c )
{
mk2c_lost = 0;
flush_dmxout_sendbuf();
}
return (unsigned int)nanokontrol_lost;
}

canonical_decompiled_code:
__int64 func9()
{
if ( !mk2c_lost )
return (unsigned int)nanokontrol_lost;
if ( mk2c )
teardown_dmx_usb_mk2_pro();
mk2c = init_dmx_usb_mk2_pro(dmx_changed, dmx_input_completed, mk2c_error);
if ( mk2c )
{
mk2c_lost = 0;
flush_dmxout_sendbuf();
}
return (unsigned int)nanokontrol_lost;
}

original_code:
void
reconnect_if_needed(void) {
 if (mk2c_lost) {
  if (mk2c != ((void *)0)) {
   teardown_dmx_usb_mk2_pro(mk2c);
  }
  mk2c = init_dmx_usb_mk2_pro(dmx_changed, dmx_input_completed, mk2c_error);
  if (mk2c != ((void *)0)) {
   mk2c_lost = 0;
   flush_dmxout_sendbuf();
  }
 }
 if (midi_lost) {
 }
 if (nanokontrol_lost) {
 }
}

canonical_original_code:
void reconnect_if_needed() {
 if (mk2c_lost) {
  if (mk2c != ((void *)0)) {
   teardown_dmx_usb_mk2_pro(mk2c);
  }
  mk2c = init_dmx_usb_mk2_pro(dmx_changed, dmx_input_completed, mk2c_error);
  if (mk2c != ((void *)0)) {
   mk2c_lost = 0;
   flush_dmxout_sendbuf();
  }
 }
 if (midi_lost) {
 }
 if (nanokontrol_lost) {
 }
}


decompiled_code:
__int64 init_communications()
{
mk2c = init_dmx_usb_mk2_pro(dmx_changed, dmx_input_completed, mk2c_error);
if ( !mk2c )
abort();
nanokontrol2 = init_nanokontrol2("/dev/snd/midiC1D0");
if ( nanokontrol2 )
{
usbmidi = init_usbmidi("/dev/snd/midiC1D0");
if ( !usbmidi )
fwrite("init_communications: init_usbmidi failed.", 1uLL, 0x29uLL, stderr);
}
else
{
fwrite("init_communications: init_nanokontrol2 failed.", 1uLL, 0x2EuLL, stderr);
usbmidi = init_usbmidi("/dev/snd/midiC1D0");
if ( !usbmidi )
{
fwrite("init_communications: init_usbmidi failed.", 1uLL, 0x29uLL, stderr);
fwrite("No input devices available. Defaulting to preprogrammed output.\n", 1uLL, 0x40uLL, stderr);
}
}
return 0LL;
}

canonical_decompiled_code:
__int64 func10()
{
mk2c = init_dmx_usb_mk2_pro(dmx_changed, dmx_input_completed, mk2c_error);
if ( !mk2c )
abort();
nanokontrol2 = init_nanokontrol2("/dev/snd/midiC1D0");
if ( nanokontrol2 )
{
usbmidi = init_usbmidi("/dev/snd/midiC1D0");
if ( !usbmidi )
fwrite("init_communications: init_usbmidi failed.", 1uLL, 0x29uLL, stderr);
}
else
{
fwrite("init_communications: init_nanokontrol2 failed.", 1uLL, 0x2EuLL, stderr);
usbmidi = init_usbmidi("/dev/snd/midiC1D0");
if ( !usbmidi )
{
fwrite("init_communications: init_usbmidi failed.", 1uLL, 0x29uLL, stderr);
fwrite("No input devices available. Defaulting to preprogrammed output.\n", 1uLL, 0x40uLL, stderr);
}
}
return 0LL;
}

original_code:
int
init_communications(void) {
 int has_any_input = 0;
 mk2c = init_dmx_usb_mk2_pro(dmx_changed, dmx_input_completed, mk2c_error);
 if (mk2c == ((void *)0)) {
  abort();
 }
 nanokontrol2 = init_nanokontrol2("/dev/snd/midiC1D0");
 if (nanokontrol2 != ((void *)0)) {
  has_any_input = 1;
 } else {
  fprintf(stderr, "init_communications: init_nanokontrol2 failed.");
 }
 usbmidi = init_usbmidi("/dev/snd/midiC1D0");
 if (usbmidi != ((void *)0)) {
  has_any_input = 1;
 } else {
  fprintf(stderr, "init_communications: init_usbmidi failed.");
 }
 if (!has_any_input) {
  fprintf(stderr, "No input devices available. Defaulting to preprogrammed output.\n");
 }
 return 0;
}

canonical_original_code:
int init_communications() {
 int has_any_input = 0;
 mk2c = init_dmx_usb_mk2_pro(dmx_changed, dmx_input_completed, mk2c_error);
 if (mk2c == ((void *)0)) {
  abort();
 }
 nanokontrol2 = init_nanokontrol2("/dev/snd/midiC1D0");
 if (nanokontrol2 != ((void *)0)) {
  has_any_input = 1;
 } else {
  fprintf(stderr, "init_communications: init_nanokontrol2 failed.");
 }
 usbmidi = init_usbmidi("/dev/snd/midiC1D0");
 if (usbmidi != ((void *)0)) {
  has_any_input = 1;
 } else {
  fprintf(stderr, "init_communications: init_usbmidi failed.");
 }
 if (!has_any_input) {
  fprintf(stderr, "No input devices available. Defaulting to preprogrammed output.\n");
 }
 return 0;
}


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

if ( mk2c )
result = mk2_send_dmx(mk2c, a1);
else
result = 4294967294LL;
return result;
}

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

if ( mk2c )
result = mk2_send_dmx(mk2c, a1);
else
result = 4294967294LL;
return result;
}

original_code:
int
send_dmx(unsigned char *dmxbytes) {
 int ret = -2;
 if (mk2c != ((void *)0)) {
  ret = mk2_send_dmx(mk2c, dmxbytes);
 }
 return ret;
}

variable_types:
dmxbytes
unsigned char *

variable_types: ret int


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

if ( nanokontrol2 )
{
result = nanokontrol2_set_led(nanokontrol2, 41LL, (unsigned int)(127 * a1));
if ( !a1 )
result = nanokontrol2_set_led(nanokontrol2, 46LL, 0LL);
}
return result;
}

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

if ( nanokontrol2 )
{
result = nanokontrol2_set_led(nanokontrol2, 41LL, (unsigned int)(127 * a1));
if ( !a1 )
result = nanokontrol2_set_led(nanokontrol2, 46LL, 0LL);
}
return result;
}

original_code:
void
set_feedback_running(int running) {
 if(nanokontrol2 != ((void *)0)) {
  nanokontrol2_set_led(nanokontrol2, 0x29, running * 127);
  if(!running) {
   nanokontrol2_set_led(nanokontrol2, 0x2E, 0);
  }
 }
}

variable_types: running int


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

result = nanokontrol2;
if ( nanokontrol2 )
result = nanokontrol2_set_led(nanokontrol2, 45LL, (unsigned int)(127 * a1));
return result;
}

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

result = nanokontrol2;
if ( nanokontrol2 )
result = nanokontrol2_set_led(nanokontrol2, 45LL, (unsigned int)(127 * a1));
return result;
}

original_code:
void
set_feedback_blackout(int blackout) {
 if(nanokontrol2 != ((void *)0)) {
  nanokontrol2_set_led(nanokontrol2, 0x2D, blackout * 127);
 }
}

variable_types: blackout int


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

if ( nanokontrol2 )
{
prev_0 = 127 - prev_0;
result = nanokontrol2_set_led(nanokontrol2, 46LL, (unsigned int)prev_0);
}
return result;
}

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

if ( nanokontrol2 )
{
prev_0 = 127 - prev_0;
result = nanokontrol2_set_led(nanokontrol2, 46LL, (unsigned int)prev_0);
}
return result;
}

original_code:
void
set_feedback_step() {
 if(nanokontrol2 != ((void *)0)) {
  static int prev = 0;
  prev = 127 - prev;
  nanokontrol2_set_led(nanokontrol2, 0x2E, prev);
 }
}

variable_types: prev int


decompiled_code:
void __fastcall handle_sigwinch(int a1)
{
if ( a1 != 28 )
__assert_fail("sig == SIGWINCH", "dmxdog.c", 0x1Bu, "handle_sigwinch");
got_sigwinch = 1;
}

canonical_decompiled_code:
void __fastcall func7(int a1)
{
if ( a1 != 28 )
__assert_fail("sig == SIGWINCH", "dmxdog.c", 0x1Bu, "handle_sigwinch");
got_sigwinch = 1;
}

original_code:
void
handle_sigwinch(int sig) {
 ((sig == 28) ? (void) (0) : __assert_fail ("sig == SIGWINCH", "dmxdog.c", 27, __extension__ __PRETTY_FUNCTION__));
 got_sigwinch = 1;
}

variable_types: sig int


decompiled_code:
void __fastcall __noreturn handle_sigterm(int a1)
{
if ( a1 == 15 )
{
if ( pid > 0 )
{
kill(pid, 15);
sleep(3u);
kill(pid, 9);
}
exit(0);
}
__assert_fail("sig == SIGTERM", "dmxdog.c", 0x21u, "handle_sigterm");
}

canonical_decompiled_code:
void __fastcall __noreturn func8(int a1)
{
if ( a1 == 15 )
{
if ( pid > 0 )
{
kill(pid, 15);
sleep(3u);
kill(pid, 9);
}
exit(0);
}
__assert_fail("sig == SIGTERM", "dmxdog.c", 0x21u, "handle_sigterm");
}

original_code:
void
handle_sigterm(int sig) {
 ((sig == 15) ? (void) (0) : __assert_fail ("sig == SIGTERM", "dmxdog.c", 33, __extension__ __PRETTY_FUNCTION__));
 if(pid > 0) {
  kill(pid, sig);
  sleep(3);
  kill(pid, 9);
 }
 exit(0);
}


canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // er12
int v4; // eax
char *const *v5; // rbp
int v6; // ebx
__pid_t v7; // eax
__pid_t v8; // eax
time_t timer; // [rsp+8h] [rbp-40h] BYREF
time_t v10; // [rsp+10h] [rbp-38h] BYREF
int stat_loc; // [rsp+1Ch] [rbp-2Ch] BYREF

v10 = 0LL;
signal(28, handle_sigwinch);
signal(15, (__sighandler_t)handle_sigterm);
v3 = 0;
while ( 1 )
{
v4 = __posix_getopt();
if ( v4 == -1 )
break;
if ( v4 == 68 )
{
v3 = 1;
}
else
{
if ( v4 != 99 )
{
fprintf(stderr, "Usage: %s [-c chdir] [-D]\n", *argv);
exit(64);
}
if ( chdir(optarg) == -1 )
warn("chdir", argv);
}
}
v5 = (char *const *)&argv[optind];
if ( v3 && daemon(1, 0) == -1 )
warn("daemon");
v6 = 0;
while ( 1 )
{
if ( pid > 0 )
goto LABEL_28;
v7 = fork();
pid = v7;
if ( !v7 )
{
execve("./dmxmain", v5, environ);
err(1, "execve");
}
if ( v7 == -1 )
{
pid = 0;
warn("fork");
++v6;
time(&v10);
}
else
{
LABEL_28:
v8 = waitpid(pid, &stat_loc, 1);
if ( v8 == -1 )
{
warn("waitpid");
++v6;
time(&v10);
}
else if ( pid == v8 )
{
if ( (stat_loc & 0x7F) != 0 )
{
if ( (char)((stat_loc & 0x7F) + 1) > 1 )
fprintf(stderr, "dmxd was killed by signal %d\n", stat_loc & 0x7F);
}
else
{
fprintf(stderr, "dmxd exited with exit code %d\n", BYTE1(stat_loc));
}
pid = 0;
++v6;
time(&v10);
}
else
{
got_sigwinch = 0;
sleep(0x1Eu);
if ( !got_sigwinch )
{
kill(pid, 15);
sleep(3u);
kill(pid, 9);
}
}
}
if ( v6 > 0 )
{
time(&timer);
if ( timer - v10 > 300 )
{
v6 = 0;
}
else if ( v6 > 4 )
{
system("reboot");
}
}
}
}

original_code:
int
main(int argc, char **argv) {
 pid_t wpid;
 int opt, status;
 int failures = 0;
 time_t last_fail = 0;
 int daemonize = 0;
 signal(28, handle_sigwinch);
 signal(15, handle_sigterm);
 while((opt = getopt(argc, argv, "Dc:")) != -1) {
  switch(opt) {
   case 'D':
    daemonize = 1;
    break;
   case 'c':
    if(chdir(optarg) == -1) {
     warn("chdir");
    }
    break;
   default:
    fprintf(stderr, "Usage: %s [-c chdir] [-D]\n", argv[0]);
    exit(64);
  }
 }
 argc -= optind;
 argv += optind;
 if(daemonize) {
  if(daemon(1, 0) == -1) {
   warn("daemon");
  }
 }
 while(1) {
  if(failures > 0) {
   time_t now;
   time(&now);
   if(now - last_fail > 300) {
    failures = 0;
   }
   if(failures >= 5) {
    system("reboot");
   }
  }
  if(pid <= 0) {
   pid = fork();
   if(pid == 0) {
    execve("./dmxmain", argv, environ);
    err(1, "execve");
   } else if(pid == -1) {
    pid = 0;
    warn("fork"); failures++; time(&last_fail); continue;
   }
  }
  wpid = waitpid(pid, &status, 1);
  if(wpid == -1) {
   warn("waitpid"); failures++; time(&last_fail); continue;
  } else if(wpid == pid) {
   if((((status) & 0x7f) == 0)) {
    fprintf(stderr, "dmxd exited with exit code %d\n", (((status) & 0xff00) >> 8));
   } else if((((signed char) (((status) & 0x7f) + 1) >> 1) > 0)) {
    fprintf(stderr, "dmxd was killed by signal %d\n", ((status) & 0x7f));
   }
   pid = 0;
   failures++; time(&last_fail); continue;
  }
  got_sigwinch = 0;
  sleep(30);
  if(!got_sigwinch) {
   kill(pid, 15);
   sleep(3);
   kill(pid, 9);
  }
 }
}

canonical_original_code:
int main(int argc, char **argv) {
 int wpid;
 int opt; int status;
 int failures = 0;
 long last_fail = 0;
 int daemonize = 0;
 signal(28, handle_sigwinch);
 signal(15, handle_sigterm);
 while((opt = getopt(argc, argv, "Dc:")) != -1) {
  switch(opt) {
   case 'D':
    daemonize = 1;
    break;
   case 'c':
    if(chdir(optarg) == -1) {
     warn("chdir");
    }
    break;
   default:
    fprintf(stderr, "Usage: %s [-c chdir] [-D]\n", argv[0]);
    exit(64);
  }
 }
 argc -= optind;
 argv += optind;
 if(daemonize) {
  if(daemon(1, 0) == -1) {
   warn("daemon");
  }
 }
 while(1) {
  if(failures > 0) {
   long now;
   time(&now);
   if(now - last_fail > 300) {
    failures = 0;
   }
   if(failures >= 5) {
    system("reboot");
   }
  }
  if(pid <= 0) {
   pid = fork();
   if(pid == 0) {
    execve("./dmxmain", argv, environ);
    err(1, "execve");
   } else if(pid == -1) {
    pid = 0;
    warn("fork"); failures++; time(&last_fail); continue;
   }
  }
  wpid = waitpid(pid, &status, 1);
  if(wpid == -1) {
   warn("waitpid"); failures++; time(&last_fail); continue;
  } else if(wpid == pid) {
   if((((status) & 0x7f) == 0)) {
    fprintf(stderr, "dmxd exited with exit code %d\n", (((status) & 0xff00) >> 8));
   } else if((((char) (((status) & 0x7f) + 1) >> 1) > 0)) {
    fprintf(stderr, "dmxd was killed by signal %d\n", ((status) & 0x7f));
   }
   pid = 0;
   failures++; time(&last_fail); continue;
  }
  got_sigwinch = 0;
  sleep(30);
  if(!got_sigwinch) {
   kill(pid, 15);
   sleep(3);
   kill(pid, 9);
  }
 }
}

variable_types: wpid int

variable_types: opt int

variable_types: status int

variable_types: failures int

variable_types: daemonize int

variable_types: now long


decompiled_code:
__int64 __fastcall usbmidi_read_data(__int64 a1, unsigned __int8 *a2, int a3)
{
unsigned __int8 v5; // al
__int64 v6; // rsi
__int64 v7; // rdx
__int64 v8; // rax
unsigned int v9; // er12
unsigned __int8 v10; // si

if ( a3 <= 0 )
return 0;
v5 = *a2;
v6 = *a2;
if ( v5 == 0xB0 )
{
v9 = 3;
}
else
{
if ( v5 != 0xF0 )
{
printf("Wat de fuck is %02x?\n", v5);
midi_print_buf("buffer: ", a2, (unsigned int)a3);
abort();
}
if ( a3 <= 1 )
return 0;
if ( (a2[1] & 0x80u) != 0 )
{
v9 = 2;
goto LABEL_14;
}
if ( a3 <= 2 )
return 0;
v7 = (unsigned int)(a3 + 1);
v8 = 3LL;
while ( 1 )
{
v9 = v8;
if ( (a2[v8 - 1] & 0x80u) != 0 )
break;
if ( ++v8 == v7 )
return 0;
}
}
if ( (int)v9 > a3 )
{
printf("Not enough data for %02x: %d of %d\n", v6, (unsigned int)a3, v9);
return 0;
}
LABEL_14:
midi_print_buf("< ", a2, v9);
if ( *a2 == 0xB0 )
{
v10 = a2[2];
if ( v10 != 0xB0 )
midi_changed(a2[1], v10);
}
else if ( *a2 == 0xF0 && a2[v9 - 1] != 0xF7 )
{
_assert_fail("buffer[cmdlen-1] == 0xf7", "usbmididriver.c", 0x38u, "usbmidi_read_data");
}
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, unsigned __int8 *a2, int a3)
{
unsigned __int8 v5; // al
__int64 v6; // rsi
__int64 v7; // rdx
__int64 v8; // rax
unsigned int v9; // er12
unsigned __int8 v10; // si

if ( a3 <= 0 )
return 0;
v5 = *a2;
v6 = *a2;
if ( v5 == 0xB0 )
{
v9 = 3;
}
else
{
if ( v5 != 0xF0 )
{
printf("Wat de fuck is %02x?\n", v5);
midi_print_buf("buffer: ", a2, (unsigned int)a3);
abort();
}
if ( a3 <= 1 )
return 0;
if ( (a2[1] & 0x80u) != 0 )
{
v9 = 2;
goto LABEL_14;
}
if ( a3 <= 2 )
return 0;
v7 = (unsigned int)(a3 + 1);
v8 = 3LL;
while ( 1 )
{
v9 = v8;
if ( (a2[v8 - 1] & 0x80u) != 0 )
break;
if ( ++v8 == v7 )
return 0;
}
}
if ( (int)v9 > a3 )
{
printf("Not enough data for %02x: %d of %d\n", v6, (unsigned int)a3, v9);
return 0;
}
LABEL_14:
midi_print_buf("< ", a2, v9);
if ( *a2 == 0xB0 )
{
v10 = a2[2];
if ( v10 != 0xB0 )
midi_changed(a2[1], v10);
}
else if ( *a2 == 0xF0 && a2[v9 - 1] != 0xF7 )
{
_assert_fail("buffer[cmdlen-1] == 0xf7", "usbmididriver.c", 0x38u, "usbmidi_read_data");
}
return v9;
}

original_code:
static int
usbmidi_read_data(void *_ctx, const unsigned char *buffer, int len) {
 struct usbmidi_context *ctx = _ctx;
 if(len < 1) {
  return 0;
 }
 int cmdlen = 0;
 int dumpoffset = -1;
 switch(buffer[0]) {
  case 0xb0:
   cmdlen = 3;
   break;
  case 0xf0:
   if(len < 2) {
    return 0;
   }
   for(cmdlen = 2; buffer[cmdlen-1] <= 0x7f; cmdlen++) {
    if(cmdlen >= len) {
     return 0;
    }
   }
   break;
  default:
   printf("Wat de fuck is %02x?\n", buffer[0]);
   midi_print_buf("buffer: ", buffer, len);
   abort();
 }
 if(cmdlen > len) {
  printf("Not enough data for %02x: %d of %d\n", buffer[0], len, cmdlen);
  return 0;
 }
 midi_print_buf("< ", buffer, cmdlen);
 switch(buffer[0]) {
  case 0xb0:
   if(buffer[2] == 0xb0) {
    break;
   }
   midi_changed(buffer[1], buffer[2]);
   break;
  case 0xf0:
   ((buffer[cmdlen-1] == 0xf7) ? (void) (0) : __assert_fail ("buffer[cmdlen-1] == 0xf7", "usbmididriver.c", 56, __extension__ __PRETTY_FUNCTION__));
   break;
 }
 return cmdlen;
}

canonical_original_code:
static int usbmidi_read_data(void *_ctx, unsigned char *buffer, int len) {
 struct usbmidi_context *ctx = _ctx;
 if(len < 1) {
  return 0;
 }
 int cmdlen = 0;
 int dumpoffset = -1;
 switch(buffer[0]) {
  case 0xb0:
   cmdlen = 3;
   break;
  case 0xf0:
   if(len < 2) {
    return 0;
   }
   for(cmdlen = 2; buffer[cmdlen-1] <= 0x7f; cmdlen++) {
    if(cmdlen >= len) {
     return 0;
    }
   }
   break;
  default:
   printf("Wat de fuck is %02x?\n", buffer[0]);
   midi_print_buf("buffer: ", buffer, len);
   abort();
 }
 if(cmdlen > len) {
  printf("Not enough data for %02x: %d of %d\n", buffer[0], len, cmdlen);
  return 0;
 }
 midi_print_buf("< ", buffer, cmdlen);
 switch(buffer[0]) {
  case 0xb0:
   if(buffer[2] == 0xb0) {
    break;
   }
   midi_changed(buffer[1], buffer[2]);
   break;
  case 0xf0:
   ((buffer[cmdlen-1] == 0xf7) ? (void) (0) : __assert_fail ("buffer[cmdlen-1] == 0xf7", "usbmididriver.c", 56, __extension__ __PRETTY_FUNCTION__));
   break;
 }
 return cmdlen;
}

variable_types:
buffer
unsigned char *

variable_types: len int

variable_types: struct usbmidi_context { struct midi_context { int fd; unsigned long thr; int running; int (*eater)(void *ctx, unsigned char *buffer, int len); } midictx; } *

variable_types: cmdlen int

variable_types: dumpoffset int

user_defined_types: struct usbmidi_context { struct midi_context midictx; }


decompiled_code:
void *__fastcall init_usbmidi(__int64 a1)
{
void *v1; // rax
void *v2; // rbx

v1 = malloc(0x20uLL);
if ( !v1 )
_assert_fail("ctx != NULL", "usbmididriver.c", 0x43u, "init_usbmidi");
v2 = v1;
if ( !init_midi(v1, a1, usbmidi_read_data) )
{
free(v2);
v2 = 0LL;
}
return v2;
}

canonical_decompiled_code:
void *__fastcall func1(__int64 a1)
{
void *v1; // rax
void *v2; // rbx

v1 = malloc(0x20uLL);
if ( !v1 )
_assert_fail("ctx != NULL", "usbmididriver.c", 0x43u, "init_usbmidi");
v2 = v1;
if ( !init_midi(v1, a1, usbmidi_read_data) )
{
free(v2);
v2 = 0LL;
}
return v2;
}

original_code:
struct usbmidi_context *
init_usbmidi(char *path) {
 struct usbmidi_context *ctx;
 ctx = malloc(sizeof(struct usbmidi_context));
 ((ctx != ((void *)0)) ? (void) (0) : __assert_fail ("ctx != NULL", "usbmididriver.c", 67, __extension__ __PRETTY_FUNCTION__));
 if(init_midi(&ctx->midictx, path, usbmidi_read_data) == ((void *)0)) {
  free(ctx);
  return ((void *)0);
 }
 return ctx;
}

canonical_original_code:
struct usbmidi_context *init_usbmidi(char *path) {
 struct usbmidi_context *ctx;
 ctx = malloc(sizeof(struct usbmidi_context));
 ((ctx != ((void *)0)) ? (void) (0) : __assert_fail ("ctx != NULL", "usbmididriver.c", 67, __extension__ __PRETTY_FUNCTION__));
 if(init_midi(&ctx->midictx, path, usbmidi_read_data) == ((void *)0)) {
  free(ctx);
  return ((void *)0);
 }
 return ctx;
}

variable_types:
path
char *

return_type: struct usbmidi_context { struct midi_context { int fd; unsigned long thr; int running; int (*eater)(void *ctx, unsigned char *buffer, int len); } midictx; } *


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

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

original_code:
void
teardown_usbmidi(struct usbmidi_context *ctx) {
 teardown_midi(&ctx->midictx);
 free(ctx);
}

user_defined_types: struct midi_context { int fd; unsigned long thr; int running; int (*eater)(void *ctx, unsigned char *buffer, int len); }


decompiled_code:
__int64 __fastcall nanokontrol2_read_data(__int64 a1, unsigned __int8 *a2, int a3)
{
unsigned __int8 v4; // al
int v5; // er14
unsigned int v6; // er12
unsigned __int8 v7; // al
__int64 result; // rax
unsigned __int8 v9; // r12
unsigned __int8 v10; // al

if ( a3 <= 0 )
return 0;
v4 = *a2;
if ( *a2 == 0xF0 )
{
if ( a3 <= 10 )
return 0;
v5 = -1;
v6 = 11;
if ( a2[7] != 127 )
goto LABEL_8;
v9 = a2[8];
if ( v9 == 127 )
{
if ( a3 <= 11 )
return 0;
if ( a2[9] != 2 )
_assert_fail("buffer[9] == 0x02", "nanokontroldriver.c", 0x32u, "nanokontrol2_read_data");
v6 = (a2[10] << 7) + a2[11] + 13;
v5 = 12;
}
else
{
v6 = v9 + 11;
v5 = 9;
}
printf("supercmdlen: %d\n", v6);
}
else
{
if ( v4 > 0xF0u )
goto LABEL_21;
if ( v4 != 0xB0 )
{
if ( v4 == 0xBF )
{
v5 = -1;
v6 = 3;
goto LABEL_7;
}
LABEL_21:
printf("Wat de fuck is %02x?\n", v4);
midi_print_buf("buffer: ", a2, (unsigned int)a3);
abort();
}
v5 = -1;
v6 = 3;
}
LABEL_7:
if ( (int)v6 > a3 )
{
printf("Not enough data for %02x: %d of %d\n", *a2, (unsigned int)a3, v6);
return 0;
}
LABEL_8:
midi_print_buf("< ", a2, v6);
v7 = *a2;
if ( *a2 == 0xBF )
goto LABEL_24;
if ( v7 != 0xF0 )
{
if ( v7 != 0xB0 )
return v6;
LABEL_24:
midi_changed(a2[1], a2[2]);
return v6;
}
if ( a2[v6 - 1] != 0xF7 )
_assert_fail("buffer[cmdlen-1] == 0xf7", "nanokontroldriver.c", 0x56u, "nanokontrol2_read_data");
v10 = a2[7];
switch ( v10 )
{
case '_':
switch ( a2[8] )
{
case '!':
if ( a2[9] )
_assert_fail("buffer[9] == 0x00", "nanokontroldriver.c", 0x5Fu, "nanokontrol2_read_data");
return result;
case '"':
if ( a2[9] )
_assert_fail("buffer[9] == 0x00", "nanokontroldriver.c", 0x62u, "nanokontrol2_read_data");
return result;
case '#':
if ( a2[9] )
_assert_fail("buffer[9] == 0x00", "nanokontroldriver.c", 0x64u, "nanokontrol2_read_data");
return result;
case '$':
if ( a2[9] )
_assert_fail("buffer[9] == 0x00", "nanokontroldriver.c", 0x67u, "nanokontrol2_read_data");
return result;
case 'B':
*(_DWORD *)(a1 + 32) = a2[9];
break;
default:
return v6;
}
break;
case '\x7F':
if ( a2[v5] == 64 )
midi_print_buf("group 1: ", &a2[v5 + 4], 30LL);
break;
case '@':
if ( a2[8] )
_assert_fail("buffer[8] == 0x00", "nanokontroldriver.c", 0x59u, "nanokontrol2_read_data");
*(_DWORD *)(a1 + 32) = a2[9] != 2;
break;
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, unsigned __int8 *a2, int a3)
{
unsigned __int8 v4; // al
int v5; // er14
unsigned int v6; // er12
unsigned __int8 v7; // al
__int64 result; // rax
unsigned __int8 v9; // r12
unsigned __int8 v10; // al

if ( a3 <= 0 )
return 0;
v4 = *a2;
if ( *a2 == 0xF0 )
{
if ( a3 <= 10 )
return 0;
v5 = -1;
v6 = 11;
if ( a2[7] != 127 )
goto LABEL_8;
v9 = a2[8];
if ( v9 == 127 )
{
if ( a3 <= 11 )
return 0;
if ( a2[9] != 2 )
_assert_fail("buffer[9] == 0x02", "nanokontroldriver.c", 0x32u, "nanokontrol2_read_data");
v6 = (a2[10] << 7) + a2[11] + 13;
v5 = 12;
}
else
{
v6 = v9 + 11;
v5 = 9;
}
printf("supercmdlen: %d\n", v6);
}
else
{
if ( v4 > 0xF0u )
goto LABEL_21;
if ( v4 != 0xB0 )
{
if ( v4 == 0xBF )
{
v5 = -1;
v6 = 3;
goto LABEL_7;
}
LABEL_21:
printf("Wat de fuck is %02x?\n", v4);
midi_print_buf("buffer: ", a2, (unsigned int)a3);
abort();
}
v5 = -1;
v6 = 3;
}
LABEL_7:
if ( (int)v6 > a3 )
{
printf("Not enough data for %02x: %d of %d\n", *a2, (unsigned int)a3, v6);
return 0;
}
LABEL_8:
midi_print_buf("< ", a2, v6);
v7 = *a2;
if ( *a2 == 0xBF )
goto LABEL_24;
if ( v7 != 0xF0 )
{
if ( v7 != 0xB0 )
return v6;
LABEL_24:
midi_changed(a2[1], a2[2]);
return v6;
}
if ( a2[v6 - 1] != 0xF7 )
_assert_fail("buffer[cmdlen-1] == 0xf7", "nanokontroldriver.c", 0x56u, "nanokontrol2_read_data");
v10 = a2[7];
switch ( v10 )
{
case '_':
switch ( a2[8] )
{
case '!':
if ( a2[9] )
_assert_fail("buffer[9] == 0x00", "nanokontroldriver.c", 0x5Fu, "nanokontrol2_read_data");
return result;
case '"':
if ( a2[9] )
_assert_fail("buffer[9] == 0x00", "nanokontroldriver.c", 0x62u, "nanokontrol2_read_data");
return result;
case '#':
if ( a2[9] )
_assert_fail("buffer[9] == 0x00", "nanokontroldriver.c", 0x64u, "nanokontrol2_read_data");
return result;
case '$':
if ( a2[9] )
_assert_fail("buffer[9] == 0x00", "nanokontroldriver.c", 0x67u, "nanokontrol2_read_data");
return result;
case 'B':
*(_DWORD *)(a1 + 32) = a2[9];
break;
default:
return v6;
}
break;
case '\x7F':
if ( a2[v5] == 64 )
midi_print_buf("group 1: ", &a2[v5 + 4], 30LL);
break;
case '@':
if ( a2[8] )
_assert_fail("buffer[8] == 0x00", "nanokontroldriver.c", 0x59u, "nanokontrol2_read_data");
*(_DWORD *)(a1 + 32) = a2[9] != 2;
break;
}
return v6;
}

original_code:
static int
nanokontrol2_read_data(void *_ctx, const unsigned char *buffer, int len) {
 struct nanokontrol2_context *ctx = _ctx;
 if(len < 1) {
  return 0;
 }
 int cmdlen = 0;
 int dumpoffset = -1;
 switch(buffer[0]) {
  case 0xb0:
  case 0xbf:
   cmdlen = 3;
   break;
  case 0xf0:
   if(len < 11) {
    return 0;
   }
   if(buffer[7] == 0x7f) {
    if(buffer[8] == 0x7f) {
     if(len < 12) {
      return 0;
     }
     ((buffer[9] == 0x02) ? (void) (0) : __assert_fail ("buffer[9] == 0x02", "nanokontroldriver.c", 50, __extension__ __PRETTY_FUNCTION__));
     cmdlen = 11 + buffer[10]*128 + buffer[11] + 2;
     dumpoffset = 12;
    } else {
     cmdlen = 11 + buffer[8];
     dumpoffset = 9;
    }
    printf("supercmdlen: %d\n", cmdlen);
   } else {
    cmdlen = 11;
   }
   break;
  default:
   printf("Wat de fuck is %02x?\n", buffer[0]);
   midi_print_buf("buffer: ", buffer, len);
   abort();
 }
 if(cmdlen > len) {
  printf("Not enough data for %02x: %d of %d\n", buffer[0], len, cmdlen);
  return 0;
 }
 midi_print_buf("< ", buffer, cmdlen);
 switch(buffer[0]) {
  case 0xb0:
  case 0xbf:
   midi_changed(buffer[1], buffer[2]);
   break;
  case 0xf0:
   ((buffer[cmdlen-1] == 0xf7) ? (void) (0) : __assert_fail ("buffer[cmdlen-1] == 0xf7", "nanokontroldriver.c", 86, __extension__ __PRETTY_FUNCTION__));
   switch(buffer[7]) {
    case 0x40:
     ((buffer[8] == 0x00) ? (void) (0) : __assert_fail ("buffer[8] == 0x00", "nanokontroldriver.c", 89, __extension__ __PRETTY_FUNCTION__));
     ctx->active_mode = (buffer[9] == 02 ? 0 : 1);
     break;
    case 0x5f:
     switch(buffer[8]) {
      case 0x21:
       ((buffer[9] == 0x00) ? (void) (0) : __assert_fail ("buffer[9] == 0x00", "nanokontroldriver.c", 95, __extension__ __PRETTY_FUNCTION__));
       break;
      case 0x22:
       ((buffer[9] == 0x00) ? (void) (0) : __assert_fail ("buffer[9] == 0x00", "nanokontroldriver.c", 98, __extension__ __PRETTY_FUNCTION__));
      case 0x23:
       ((buffer[9] == 0x00) ? (void) (0) : __assert_fail ("buffer[9] == 0x00", "nanokontroldriver.c", 100, __extension__ __PRETTY_FUNCTION__));
       break;
      case 0x24:
       ((buffer[9] == 0x00) ? (void) (0) : __assert_fail ("buffer[9] == 0x00", "nanokontroldriver.c", 103, __extension__ __PRETTY_FUNCTION__));
       break;
      case 0x42:
       ctx->active_mode = buffer[9];
       break;
     }
     break;
    case 0x7f:
     switch(buffer[dumpoffset]) {
      case 0x40:
       midi_print_buf("group 1: ", buffer + dumpoffset + 1 + 3, 30);
       break;
     }
     break;
   }
   break;
 }
 return cmdlen;
}

canonical_original_code:
static int nanokontrol2_read_data(void *_ctx, unsigned char *buffer, int len) {
 struct nanokontrol2_context *ctx = _ctx;
 if(len < 1) {
  return 0;
 }
 int cmdlen = 0;
 int dumpoffset = -1;
 switch(buffer[0]) {
  case 0xb0:
  case 0xbf:
   cmdlen = 3;
   break;
  case 0xf0:
   if(len < 11) {
    return 0;
   }
   if(buffer[7] == 0x7f) {
    if(buffer[8] == 0x7f) {
     if(len < 12) {
      return 0;
     }
     ((buffer[9] == 0x02) ? (void) (0) : __assert_fail ("buffer[9] == 0x02", "nanokontroldriver.c", 50, __extension__ __PRETTY_FUNCTION__));
     cmdlen = 11 + buffer[10]*128 + buffer[11] + 2;
     dumpoffset = 12;
    } else {
     cmdlen = 11 + buffer[8];
     dumpoffset = 9;
    }
    printf("supercmdlen: %d\n", cmdlen);
   } else {
    cmdlen = 11;
   }
   break;
  default:
   printf("Wat de fuck is %02x?\n", buffer[0]);
   midi_print_buf("buffer: ", buffer, len);
   abort();
 }
 if(cmdlen > len) {
  printf("Not enough data for %02x: %d of %d\n", buffer[0], len, cmdlen);
  return 0;
 }
 midi_print_buf("< ", buffer, cmdlen);
 switch(buffer[0]) {
  case 0xb0:
  case 0xbf:
   midi_changed(buffer[1], buffer[2]);
   break;
  case 0xf0:
   ((buffer[cmdlen-1] == 0xf7) ? (void) (0) : __assert_fail ("buffer[cmdlen-1] == 0xf7", "nanokontroldriver.c", 86, __extension__ __PRETTY_FUNCTION__));
   switch(buffer[7]) {
    case 0x40:
     ((buffer[8] == 0x00) ? (void) (0) : __assert_fail ("buffer[8] == 0x00", "nanokontroldriver.c", 89, __extension__ __PRETTY_FUNCTION__));
     ctx->active_mode = (buffer[9] == 02 ? 0 : 1);
     break;
    case 0x5f:
     switch(buffer[8]) {
      case 0x21:
       ((buffer[9] == 0x00) ? (void) (0) : __assert_fail ("buffer[9] == 0x00", "nanokontroldriver.c", 95, __extension__ __PRETTY_FUNCTION__));
       break;
      case 0x22:
       ((buffer[9] == 0x00) ? (void) (0) : __assert_fail ("buffer[9] == 0x00", "nanokontroldriver.c", 98, __extension__ __PRETTY_FUNCTION__));
      case 0x23:
       ((buffer[9] == 0x00) ? (void) (0) : __assert_fail ("buffer[9] == 0x00", "nanokontroldriver.c", 100, __extension__ __PRETTY_FUNCTION__));
       break;
      case 0x24:
       ((buffer[9] == 0x00) ? (void) (0) : __assert_fail ("buffer[9] == 0x00", "nanokontroldriver.c", 103, __extension__ __PRETTY_FUNCTION__));
       break;
      case 0x42:
       ctx->active_mode = buffer[9];
       break;
     }
     break;
    case 0x7f:
     switch(buffer[dumpoffset]) {
      case 0x40:
       midi_print_buf("group 1: ", buffer + dumpoffset + 1 + 3, 30);
       break;
     }
     break;
   }
   break;
 }
 return cmdlen;
}

variable_types: struct nanokontrol2_context { struct midi_context { int fd; unsigned long thr; int running; int (*eater)(void *ctx, unsigned char *buffer, int len); } midictx; int active_mode; int requested_mode; } *

user_defined_types: struct nanokontrol2_context { struct midi_context midictx; int active_mode; int requested_mode; }


decompiled_code:
__int64 __fastcall nanokontrol2_set_led(__int64 a1, char a2, int a3)
{
char v4[11]; // [rsp+Dh] [rbp-Bh] BYREF

v4[0] = -65;
v4[1] = a2;
v4[2] = a3 != 0 ? 0x7F : 0;
return midi_send_buf(a1, v4, 3LL);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, char a2, int a3)
{
char v4[11]; // [rsp+Dh] [rbp-Bh] BYREF

v4[0] = -65;
v4[1] = a2;
v4[2] = a3 != 0 ? 0x7F : 0;
return midi_send_buf(a1, v4, 3LL);
}

original_code:
void
nanokontrol2_set_led(struct nanokontrol2_context *ctx, int which, int value) {
 unsigned char msg[] = { 0xbf, which, value == 0 ? 0x00 : 0x7f };
 midi_send_buf(&ctx->midictx, msg, 3);
}

variable_types: which int

variable_types: value int

variable_types:
msg
unsigned char []


decompiled_code:
void *__fastcall init_nanokontrol2(__int64 a1)
{
void *v1; // rax
void *v2; // rbx

v1 = malloc(0x28uLL);
if ( !v1 )
_assert_fail("ctx != NULL", "nanokontroldriver.c", 0x80u, "init_nanokontrol2");
v2 = v1;
if ( init_midi(v1, a1, nanokontrol2_read_data) )
{
nanokontrol2_switch_mode((__int64)v2, 1);
}
else
{
free(v2);
v2 = 0LL;
}
return v2;
}

canonical_decompiled_code:
void *__fastcall func3(__int64 a1)
{
void *v1; // rax
void *v2; // rbx

v1 = malloc(0x28uLL);
if ( !v1 )
_assert_fail("ctx != NULL", "nanokontroldriver.c", 0x80u, "init_nanokontrol2");
v2 = v1;
if ( init_midi(v1, a1, nanokontrol2_read_data) )
{
func0((__int64)v2, 1);
}
else
{
free(v2);
v2 = 0LL;
}
return v2;
}

original_code:
struct nanokontrol2_context *
init_nanokontrol2(char *path) {
 struct nanokontrol2_context *ctx;
 ctx = malloc(sizeof(struct nanokontrol2_context));
 ((ctx != ((void *)0)) ? (void) (0) : __assert_fail ("ctx != NULL", "nanokontroldriver.c", 128, __extension__ __PRETTY_FUNCTION__));
 if(init_midi(&ctx->midictx, path, nanokontrol2_read_data) == ((void *)0)) {
  free(ctx);
  return ((void *)0);
 }
 nanokontrol2_switch_mode(ctx, 1);
 return ctx;
}

canonical_original_code:
struct nanokontrol2_context *init_nanokontrol2(char *path) {
 struct nanokontrol2_context *ctx;
 ctx = malloc(sizeof(struct nanokontrol2_context));
 ((ctx != ((void *)0)) ? (void) (0) : __assert_fail ("ctx != NULL", "nanokontroldriver.c", 128, __extension__ __PRETTY_FUNCTION__));
 if(init_midi(&ctx->midictx, path, nanokontrol2_read_data) == ((void *)0)) {
  free(ctx);
  return ((void *)0);
 }
 nanokontrol2_switch_mode(ctx, 1);
 return ctx;
}

return_type: struct nanokontrol2_context { struct midi_context { int fd; unsigned long thr; int running; int (*eater)(void *ctx, unsigned char *buffer, int len); } midictx; int active_mode; int requested_mode; } *


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

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

original_code:
void
teardown_nanokontrol2(struct nanokontrol2_context *ctx) {
 teardown_midi(&ctx->midictx);
 free(ctx);
}


decompiled_code:
__int64 __fastcall nanokontrol2_ask_status(__int64 a1)
{
__int64 v2; // [rsp+5h] [rbp-13h] BYREF
__int16 v3; // [rsp+Dh] [rbp-Bh]
char v4; // [rsp+Fh] [rbp-9h]

v2 = 0x1F001301004042F0LL;
v3 = 16;
v4 = -9;
return midi_send_buf(a1, &v2, 11LL);
}

original_code:
void
nanokontrol2_ask_status(struct nanokontrol2_context *ctx) {
 unsigned char buf[] = { 0xf0, 0x42, 0x40, 0x00, 0x01, 0x13, 0x00, 0x1f, 0x10, 0x00, 0xf7 };
 midi_send_buf(&ctx->midictx, buf, 11);
}

variable_types:
buf
unsigned char []


decompiled_code:
ssize_t __fastcall midi_reader(__int64 a1)
{
int v1; // ebx
int i; // er13
ssize_t result; // rax
char src[552]; // [rsp+0h] [rbp-228h] BYREF

*(_DWORD *)(a1 + 16) = 1;
v1 = 0;
while ( 1 )
{
result = read(*(_DWORD *)a1, &src[v1], 512LL - v1);
if ( (_DWORD)result == -1 )
break;
v1 += result;
for ( i = 0; ; i = 1 )
{
result = (*(__int64 (__fastcall **)(__int64, char *, _QWORD))(a1 + 24))(a1, src, (unsigned int)v1);
if ( (int)result < 0 || v1 < (int)result )
_assert_fail("eat >= 0 && eat <= bufpos", "mididriver.c", 0x32u, "midi_reader");
if ( !(_DWORD)result )
break;
v1 -= result;
memmove(src, &src[(int)result], v1);
}
if ( i )
result = midi_input_completed();
if ( !*(_DWORD *)(a1 + 16) )
return result;
}
if ( *(_DWORD *)(a1 + 16) )
_assert_fail("ret != -1", "mididriver.c", 0x2Bu, "midi_reader");
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func0(__int64 a1)
{
int v1; // ebx
int i; // er13
ssize_t result; // rax
char src[552]; // [rsp+0h] [rbp-228h] BYREF

*(_DWORD *)(a1 + 16) = 1;
v1 = 0;
while ( 1 )
{
result = read(*(_DWORD *)a1, &src[v1], 512LL - v1);
if ( (_DWORD)result == -1 )
break;
v1 += result;
for ( i = 0; ; i = 1 )
{
result = (*(__int64 (__fastcall **)(__int64, char *, _QWORD))(a1 + 24))(a1, src, (unsigned int)v1);
if ( (int)result < 0 || v1 < (int)result )
_assert_fail("eat >= 0 && eat <= bufpos", "mididriver.c", 0x32u, "midi_reader");
if ( !(_DWORD)result )
break;
v1 -= result;
memmove(src, &src[(int)result], v1);
}
if ( i )
result = midi_input_completed();
if ( !*(_DWORD *)(a1 + 16) )
return result;
}
if ( *(_DWORD *)(a1 + 16) )
_assert_fail("ret != -1", "mididriver.c", 0x2Bu, "midi_reader");
return result;
}

original_code:
static void
midi_reader(struct midi_context *ctx) {
 unsigned char buffer[512];
 int bufpos = 0;
 ctx->running = 1;
 while(ctx->running) {
  int ret = read(ctx->fd, buffer+bufpos, sizeof(buffer) - bufpos);
  if(ret == -1 && !ctx->running) {
   break;
  }
  ((ret != -1) ? (void) (0) : __assert_fail ("ret != -1", "mididriver.c", 43, __extension__ __PRETTY_FUNCTION__));
  bufpos += ret;
  int eat;
  int did_something = 0;
  do {
   eat = ctx->eater(ctx, buffer, bufpos);
   ((eat >= 0 && eat <= bufpos) ? (void) (0) : __assert_fail ("eat >= 0 && eat <= bufpos", "mididriver.c", 50, __extension__ __PRETTY_FUNCTION__));
   if(eat == 0) {
    break;
   }
   did_something = 1;
   bufpos -= eat;
   memmove(buffer, buffer + eat, bufpos);
  } while(eat > 0);
  if(did_something) {
   midi_input_completed();
  }
 }
}

variable_types: struct midi_context { int fd; unsigned long thr; int running; int (*eater)(void *ctx, unsigned char *buffer, int len); } *

variable_types:
buffer
unsigned char [512]

variable_types: bufpos int

variable_types: ret int

variable_types: eat int


decompiled_code:
void *__fastcall midi_read_thread(void *a1)
{
midi_reader((__int64)a1);
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func1(void *a1)
{
func0((__int64)a1);
return 0LL;
}

original_code:
static void *
midi_read_thread(void *ctx) {
 midi_reader(ctx);
 return ((void *)0);
}

canonical_original_code:
static void *midi_read_thread(void *ctx) {
 midi_reader(ctx);
 return ((void *)0);
}

variable_types:
ctx
void *

return_type: void *


decompiled_code:
int __fastcall midi_print_buf(const char *a1, __int64 a2, int a3)
{
int result; // eax
__int64 v5; // r13
__int64 v6; // rbx
int v7; // ebp
__int64 v8; // rdx

result = printf("%s", a1);
if ( a3 > 0 )
{
v5 = (unsigned int)a3;
v6 = 0LL;
v7 = a3 - 1;
do
{
v8 = 10LL;
if ( v7 != (_DWORD)v6 )
v8 = 32LL;
result = printf("%02x%c", *(unsigned __int8 *)(a2 + v6++), v8);
}
while ( v6 != v5 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(const char *a1, __int64 a2, int a3)
{
int result; // eax
__int64 v5; // r13
__int64 v6; // rbx
int v7; // ebp
__int64 v8; // rdx

result = printf("%s", a1);
if ( a3 > 0 )
{
v5 = (unsigned int)a3;
v6 = 0LL;
v7 = a3 - 1;
do
{
v8 = 10LL;
if ( v7 != (_DWORD)v6 )
v8 = 32LL;
result = printf("%02x%c", *(unsigned __int8 *)(a2 + v6++), v8);
}
while ( v6 != v5 );
}
return result;
}

original_code:
void
midi_print_buf(const char *prefix, const unsigned char *buf, int len) {
 int i;
 printf("%s", prefix);
 for(i = 0; len > i; i++) {
  printf("%02x%c", buf[i], i == len-1 ? '\n' : ' ');
 }
}

canonical_original_code:
void midi_print_buf(char *prefix, unsigned char *buf, int len) {
 int i;
 printf("%s", prefix);
 for(i = 0; len > i; i++) {
  printf("%02x%c", buf[i], i == len-1 ? '\n' : ' ');
 }
}

variable_types:
prefix
char *

variable_types:
buf
unsigned char *

variable_types: i int


decompiled_code:
__int64 __fastcall midi_send_buf(int *a1, const void *a2, int a3)
{
midi_print_buf("> ", (__int64)a2, a3);
if ( a3 != (unsigned int)write(*a1, a2, a3) )
_assert_fail("ret == len", "mididriver.c", 0x1Bu, "midi_send_buf");
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(int *a1, const void *a2, int a3)
{
func2("> ", (__int64)a2, a3);
if ( a3 != (unsigned int)write(*a1, a2, a3) )
_assert_fail("ret == len", "mididriver.c", 0x1Bu, "midi_send_buf");
return 0LL;
}

original_code:
int
midi_send_buf(struct midi_context *ctx, const unsigned char *buf, const int len) {
 int ret;
 midi_print_buf("> ", buf, len);
 ret = write(ctx->fd, buf, len);
 ((ret == len) ? (void) (0) : __assert_fail ("ret == len", "mididriver.c", 27, __extension__ __PRETTY_FUNCTION__));
 return 0;
}

canonical_original_code:
int midi_send_buf(struct midi_context *ctx, unsigned char *buf, int len) {
 int ret;
 midi_print_buf("> ", buf, len);
 ret = write(ctx->fd, buf, len);
 ((ret == len) ? (void) (0) : __assert_fail ("ret == len", "mididriver.c", 27, __extension__ __PRETTY_FUNCTION__));
 return 0;
}

variable_types: len int


decompiled_code:
int __fastcall teardown_midi(__int64 a1)
{
*(_DWORD *)(a1 + 16) = 0;
close(*(_DWORD *)a1);
return pthread_join(*(_QWORD *)(a1 + 8), 0LL);
}

canonical_decompiled_code:
int __fastcall func5(__int64 a1)
{
*(_DWORD *)(a1 + 16) = 0;
close(*(_DWORD *)a1);
return pthread_join(*(_QWORD *)(a1 + 8), 0LL);
}

original_code:
void
teardown_midi(struct midi_context *ctx) {
 ctx->running = 0;
 close(ctx->fd);
 pthread_join(ctx->thr, ((void *)0));
}


decompiled_code:
__int64 __fastcall convert_color(unsigned __int8 a1, _BYTE *a2)
{
__int64 v2; // rax
__int64 result; // rax

v2 = 28LL;
while ( (unsigned __int8)mapping[4 * v2] > a1 )
{
if ( --v2 == -1 )
_assert_fail("!\"reached\"", "colors.c", 0x38u, "convert_color");
}
*a2 = byte_A1[4 * (int)v2];
a2[1] = byte_A2[4 * (int)v2];
result = (unsigned __int8)byte_A3[4 * (int)v2];
a2[2] = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int8 a1, _BYTE *a2)
{
__int64 v2; // rax
__int64 result; // rax

v2 = 28LL;
while ( (unsigned __int8)mapping[4 * v2] > a1 )
{
if ( --v2 == -1 )
_assert_fail("!\"reached\"", "colors.c", 0x38u, "convert_color");
}
*a2 = byte_A1[4 * (int)v2];
a2[1] = byte_A2[4 * (int)v2];
result = (unsigned __int8)byte_A3[4 * (int)v2];
a2[2] = result;
return result;
}

original_code:
void
convert_color(unsigned char input, unsigned char *output) {
 int i;
 for(i = sizeof(mapping) / sizeof(struct mapping_entry) - 1; i >= 0; i--) {
  if(input >= mapping[i].from) {
   output[0] = mapping[i].red;
   output[1] = mapping[i].green;
   output[2] = mapping[i].blue;
   return;
  }
 }
 ((!"reached") ? (void) (0) : __assert_fail ("!\"reached\"", "colors.c", 56, __extension__ __PRETTY_FUNCTION__));
}

variable_types:
input
unsigned char

variable_types:
output
unsigned char *

user_defined_types: struct mapping_entry { unsigned char from; unsigned char red; unsigned char green; unsigned char blue; }


repo: vilmart/kfs

decompiled_code:
void __fastcall signal_handler(unsigned int a1)
{
fprintf(stderr, "signal32: signal received: %d\n", a1);
if ( a1 == 2 )
{
fwrite("Finishing...\n", 1uLL, 0xDuLL, stderr);
wait = 0;
}
}

canonical_decompiled_code:
void __fastcall func7(unsigned int a1)
{
fprintf(stderr, "signal32: signal received: %d\n", a1);
if ( a1 == 2 )
{
fwrite("Finishing...\n", 1uLL, 0xDuLL, stderr);
wait = 0;
}
}

original_code:
void signal_handler (int signum)
{
  fprintf (stderr, "signal32: signal received: %d\n", signum);
  if (signum == 2)
    {
      fprintf (stderr, "Finishing...\n");
      wait = 0;
    }
}

canonical_original_code:
void signal_handler(int signum)
{
  fprintf (stderr, "signal32: signal received: %d\n", signum);
  if (signum == 2)
    {
      fprintf (stderr, "Finishing...\n");
      wait = 0;
    }
}

variable_types: signum int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int i; // ebx
int result; // eax

for ( i = 1; i != 33; ++i )
{
if ( signal(i, (__sighandler_t)signal_handler) == (__sighandler_t)-1LL )
fprintf(stderr, "signal32: signal %d could not be registered.\n", i);
}
result = wait;
while ( wait )
;
return result;
}

original_code:
int main ()
{
  int i;
  for (i = 1; i <= 32; i++)
    {
      if (signal (i, signal_handler) == ((__sighandler_t) -1))
        fprintf (stderr, "signal32: signal %d could not be registered.\n", i);
    }
  while (wait);
  return 0;
}

canonical_original_code:
int main()
{
  int i;
  for (i = 1; i <= 32; i++)
    {
      if (signal (i, signal_handler) == ((__sighandler_t) -1))
        fprintf (stderr, "signal32: signal %d could not be registered.\n", i);
    }
  while (wait);
  return 0;
}


trim

decompiled_code:
const char *__fastcall trim(const char *a1)
{
const char *i; // rbx

if ( a1 && *a1 )
{
for ( i = &a1[strlen(a1) - 1]; a1 <= i; --i )
{
if ( !isspace(*i) )
break;
}
*((_BYTE *)i + 1) = 0;
}
return a1;
}

canonical_decompiled_code:
const char *__fastcall func7(const char *a1)
{
const char *i; // rbx

if ( a1 && *a1 )
{
for ( i = &a1[strlen(a1) - 1]; a1 <= i; --i )
{
if ( !isspace(*i) )
break;
}
*((_BYTE *)i + 1) = 0;
}
return a1;
}

original_code:
char *trim(char *s) {
    char *ptr;
    if (!s)
        return ((void *)0);
    if (!*s)
        return s;
    for (ptr = s + strlen(s) - 1; (ptr >= s) && isspace(*ptr); --ptr);
    ptr[1] = '\0';
    return s;
}

variable_types:
s
char *

variable_types:
ptr
char *

return_type: char *


original_code:
uint32 GetInt32( byte *pBytes )
{
    return (uint32)(*(pBytes + 3) << 24 | *(pBytes + 2) << 16 | *(pBytes + 1) << 8 | *pBytes);
}

canonical_original_code:
unsigned int GetInt32(unsigned char *pBytes)
{
    return (uint32)(*(pBytes + 3) << 24 | *(pBytes + 2) << 16 | *(pBytes + 1) << 8 | *pBytes);
}

variable_types:
pBytes
unsigned char *

return_type: unsigned int


writeFile

decompiled_code:
void __fastcall __noreturn writeFile(__int64 a1)
{
printf("THREAD[%ld]: Hello world from thread: %ld\n", a1, a1);
sleep(5u);
printf("THREAD[%ld]: I'm done\n", a1);
pthread_exit(0LL);
}

canonical_decompiled_code:
void __fastcall __noreturn func9(__int64 a1)
{
printf("THREAD[%ld]: Hello world from thread: %ld\n", a1, a1);
sleep(5u);
printf("THREAD[%ld]: I'm done\n", a1);
pthread_exit(0LL);
}

original_code:
void *writeFile(void *threadid){
    long tid;
    tid = (long)threadid;
    printf("THREAD[%ld]: Hello world from thread: %ld\n", tid, tid);
    sleep(5);
    printf("THREAD[%ld]: I'm done\n", tid);
    pthread_exit(((void *)0));
}

variable_types:
threadid
void *

variable_types: tid long


substring

decompiled_code:
char *__fastcall substring(unsigned int a1, unsigned int a2, const char *a3)
{
char *v4; // r12

v4 = (char *)malloc((int)(a2 - a1 + 1));
printf("%d, %d, %s\n", a1, a2, a3);
sprintf(v4, "%.*s\n", a2 - a1, &a3[a1]);
return v4;
}

canonical_decompiled_code:
char *__fastcall func10(unsigned int a1, unsigned int a2, const char *a3)
{
char *v4; // r12

v4 = (char *)malloc((int)(a2 - a1 + 1));
printf("%d, %d, %s\n", a1, a2, a3);
sprintf(v4, "%.*s\n", a2 - a1, &a3[a1]);
return v4;
}

original_code:
char* substring(int start, int stop, const char *text)
{
    char *substring = (char *)(malloc(stop - start+1));
    printf("%d, %d, %s\n",start, stop, text);
   sprintf(substring, "%.*s\n", stop - start, &text[start]);
   return substring;
}

canonical_original_code:
char *substring(int start, int stop, char *text)
{
    char *substring = (char *)(malloc(stop - start+1));
    printf("%d, %d, %s\n",start, stop, text);
   sprintf(substring, "%.*s\n", stop - start, &text[start]);
   return substring;
}

variable_types: start int

variable_types: stop int

variable_types:
text
char *

variable_types:
substring
char *


createFolder

decompiled_code:
__int64 createFolder()
{
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func11()
{
return 0xFFFFFFFFLL;
}

original_code:
int createFolder(char* folder_name)
{
    return -1;
}

canonical_original_code:
int createFolder(char *folder_name)
{
    return -1;
}


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned __int8 *v3; // r13
char *v4; // r12
unsigned int v5; // er14
unsigned __int8 v6; // al
char *v7; // rbp
char *v8; // r15
unsigned int v9; // eax
char *v10; // r15
const char *v11; // rax
size_t v12; // rax
char *v13; // rbp
unsigned __int8 v14; // bl
unsigned int v15; // ebp
FILE *s; // [rsp+8h] [rbp-70h]
int v17; // [rsp+10h] [rbp-68h]
unsigned int v18; // [rsp+14h] [rbp-64h]
const char *v19; // [rsp+18h] [rbp-60h]
struct timeval tv; // [rsp+20h] [rbp-58h] BYREF
struct timeval v21; // [rsp+30h] [rbp-48h] BYREF

setbuf(_bss_start, 0LL);
v3 = (unsigned __int8 *)tramp_initialize();
v19 = (const char *)tramp_initialize();
v4 = (char *)malloc(0x50000uLL);
tramp_get();
tramp_subscribe();
printf("%c[%d;%dm", 27LL, 1LL, 34LL);
printf("Waiting for messages from %s...", v19);
printf("%c[%dm\n", 27LL, 0LL);
if ( (unsigned int)createFolder("./downloads") )
puts("Failed to create folder downloads");
v5 = 0;
v6 = 0;
v18 = 0;
v17 = -1;
while ( 1 )
{
do
v14 = *v3;
while ( *v3 <= v6 && (v14 != 1 || v6 != 0xFF) );
gettimeofday(&tv, 0LL);
memcpy(v4, v3 + 1, 0x4FFFFuLL);
printf("Seq: %d\n", v14);
if ( !memcmp(v4, "file:", 5uLL) )
break;
if ( v17 >= 0 )
{
++v5;
v15 = GetInt32((unsigned __int8 *)v4);
printf("size of packet %u\n", v15);
v18 += fwrite(v4 + 4, 1uLL, v15, s);
printf("received so far: %d bytes %d packets \n", v18, v5);
fflush(s);
if ( v17 == v18 )
{
fclose(s);
printf("Number of packets received: %d", v5);
v18 = 0;
v17 = -1;
}
gettimeofday(&v21, 0LL);
printf("processing delay: %f\n", ((double)SLODWORD(v21.tv_usec) - (double)SLODWORD(tv.tv_usec)) * 0.5);
LABEL_6:
v6 = v14;
}
else
{
printf("%c[%d;%dm", 27LL, 1LL, 32LL);
printf("[%u] ", v14);
printf("%c[%d;%dm", 27LL, 1LL, 35LL);
printf("<%s> ", v19);
printf("%c[%d;%dm", 27LL, 1LL, 37LL);
printf("%s", (const char *)v3 + 1);
printf("%c[%dm\n", 27LL, 0LL);
v6 = v14;
}
}
++v5;
puts("got file header");
v7 = strchr(v4, 124);
puts(v7);
v8 = substring(5u, (int)v7 - (int)v4, v4);
puts(v8);
v17 = atoi(v8);
free(v8);
v9 = strlen(++v7);
v10 = substring(0, v9, v7);
v11 = trim(v10);
printf("%s,%d\n", v11, (unsigned int)v17);
v12 = strlen(v10);
v13 = (char *)malloc(v12 + 13);
strcpy(v13, "./downloads/");
strcat(v13, v10);
s = fopen(v13, "wb+");
free(v10);
free(v13);
goto LABEL_6;
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned __int8 *v3; // r13
char *v4; // r12
unsigned int v5; // er14
unsigned __int8 v6; // al
char *v7; // rbp
char *v8; // r15
unsigned int v9; // eax
char *v10; // r15
const char *v11; // rax
size_t v12; // rax
char *v13; // rbp
unsigned __int8 v14; // bl
unsigned int v15; // ebp
FILE *s; // [rsp+8h] [rbp-70h]
int v17; // [rsp+10h] [rbp-68h]
unsigned int v18; // [rsp+14h] [rbp-64h]
const char *v19; // [rsp+18h] [rbp-60h]
struct timeval tv; // [rsp+20h] [rbp-58h] BYREF
struct timeval v21; // [rsp+30h] [rbp-48h] BYREF

setbuf(_bss_start, 0LL);
v3 = (unsigned __int8 *)tramp_initialize();
v19 = (const char *)tramp_initialize();
v4 = (char *)malloc(0x50000uLL);
tramp_get();
tramp_subscribe();
printf("%c[%d;%dm", 27LL, 1LL, 34LL);
printf("Waiting for messages from %s...", v19);
printf("%c[%dm\n", 27LL, 0LL);
if ( (unsigned int)func11("./downloads") )
puts("Failed to create folder downloads");
v5 = 0;
v6 = 0;
v18 = 0;
v17 = -1;
while ( 1 )
{
do
v14 = *v3;
while ( *v3 <= v6 && (v14 != 1 || v6 != 0xFF) );
gettimeofday(&tv, 0LL);
memcpy(v4, v3 + 1, 0x4FFFFuLL);
printf("Seq: %d\n", v14);
if ( !memcmp(v4, "file:", 5uLL) )
break;
if ( v17 >= 0 )
{
++v5;
v15 = func8((unsigned __int8 *)v4);
printf("size of packet %u\n", v15);
v18 += fwrite(v4 + 4, 1uLL, v15, s);
printf("received so far: %d bytes %d packets \n", v18, v5);
fflush(s);
if ( v17 == v18 )
{
fclose(s);
printf("Number of packets received: %d", v5);
v18 = 0;
v17 = -1;
}
gettimeofday(&v21, 0LL);
printf("processing delay: %f\n", ((double)SLODWORD(v21.tv_usec) - (double)SLODWORD(tv.tv_usec)) * 0.5);
LABEL_6:
v6 = v14;
}
else
{
printf("%c[%d;%dm", 27LL, 1LL, 32LL);
printf("[%u] ", v14);
printf("%c[%d;%dm", 27LL, 1LL, 35LL);
printf("<%s> ", v19);
printf("%c[%d;%dm", 27LL, 1LL, 37LL);
printf("%s", (const char *)v3 + 1);
printf("%c[%dm\n", 27LL, 0LL);
v6 = v14;
}
}
++v5;
puts("got file header");
v7 = strchr(v4, 124);
puts(v7);
v8 = func10(5u, (int)v7 - (int)v4, v4);
puts(v8);
v17 = atoi(v8);
free(v8);
v9 = strlen(++v7);
v10 = func10(0, v9, v7);
v11 = func7(v10);
printf("%s,%d\n", v11, (unsigned int)v17);
v12 = strlen(v10);
v13 = (char *)malloc(v12 + 13);
strcpy(v13, "./downloads/");
strcat(v13, v10);
s = fopen(v13, "wb+");
free(v10);
free(v13);
goto LABEL_6;
}

original_code:
int main(int argc, char *argv[]) {
 daemonize();
 setbuf(stdout, ((void *)0));
 int ret = -1;
 pthread_t dbus_thread;
 ret = pthread_create(&dbus_thread, ((void *)0), dbus_listen, ((void *)0));
 if(ret != 0) {
  fprintf(stderr, "Could not create RPC thread!\n");
  exit(1);
 }
 bootstrap();
 pthread_t server_thread;
 ret = pthread_create(&server_thread, ((void *)0), server_listen, ((void *)0));
 if(ret != 0) {
  fprintf(stderr, "Could not create TRAMP daemon server thread!\n");
  exit(1);
 }
 fprintf(stderr, "READY!\n");
 pthread_join(dbus_thread, ((void *)0));
 pthread_join(server_thread, ((void *)0));
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
 daemonize();
 setbuf(stdout, ((void *)0));
 int ret = -1;
 unsigned long dbus_thread;
 ret = pthread_create(&dbus_thread, ((void *)0), dbus_listen, ((void *)0));
 if(ret != 0) {
  fprintf(stderr, "Could not create RPC thread!\n");
  exit(1);
 }
 bootstrap();
 unsigned long server_thread;
 ret = pthread_create(&server_thread, ((void *)0), server_listen, ((void *)0));
 if(ret != 0) {
  fprintf(stderr, "Could not create TRAMP daemon server thread!\n");
  exit(1);
 }
 fprintf(stderr, "READY!\n");
 pthread_join(dbus_thread, ((void *)0));
 pthread_join(server_thread, ((void *)0));
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: ret int


intToBytes

decompiled_code:
__int64 __fastcall intToBytes(unsigned int a1, unsigned int *a2)
{
*a2 = a1;
return HIBYTE(a1);
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int a1, unsigned int *a2)
{
*a2 = a1;
return HIBYTE(a1);
}

original_code:
void intToBytes( uint32 val, byte *pBytes )
{
    pBytes[0] = (byte)val;
    pBytes[1] = (byte)(val >> 8);
    pBytes[2] = (byte)(val >> 16);
    pBytes[3] = (byte)(val >> 24);
}

canonical_original_code:
void intToBytes(unsigned int val, unsigned char *pBytes)
{
    pBytes[0] = (unsigned char)val;
    pBytes[1] = (byte)(val >> 8);
    pBytes[2] = (byte)(val >> 16);
    pBytes[3] = (byte)(val >> 24);
}

variable_types:
val
unsigned int


trim

canonical_decompiled_code:
const char *__fastcall func9(const char *a1)
{
const char *i; // rbx

if ( a1 && *a1 )
{
for ( i = &a1[strlen(a1) - 1]; a1 <= i; --i )
{
if ( !isspace(*i) )
break;
}
*((_BYTE *)i + 1) = 0;
}
return a1;
}

canonical_original_code:
char *trim(char *s) {
    char *ptr;
    if (!s)
        return ((void *)0);
    if (!*s)
        return s;
    for (ptr = s + strlen(s) - 1; (ptr >= s) && isspace(*ptr); --ptr);
    ptr[1] = '\0';
    return s;
}


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
void *v3; // r14
const char *v4; // rbx
size_t v5; // rax
int v6; // er15
unsigned int v7; // er13
unsigned int v8; // ebx
char *v9; // rax
char *v10; // rax
unsigned int v11; // ebx
unsigned int v12; // eax
const char *v13; // rax
FILE *stream; // [rsp+8h] [rbp-90h]
unsigned __int8 *v15; // [rsp+10h] [rbp-88h]
unsigned int v16; // [rsp+18h] [rbp-80h]
int v17; // [rsp+1Ch] [rbp-7Ch]
char *s; // [rsp+28h] [rbp-70h]
char *dest; // [rsp+30h] [rbp-68h]
char *v20; // [rsp+38h] [rbp-60h]
struct timeval tv; // [rsp+40h] [rbp-58h] BYREF
struct timeval v22; // [rsp+50h] [rbp-48h] BYREF

setbuf(stdout, 0LL);
v15 = (unsigned __int8 *)tramp_initialize();
dest = (char *)tramp_initialize();
puts("finished tramp init");
v3 = malloc(0x50000uLL);
s = (char *)malloc(0x50uLL);
tramp_publish();
tramp_publish();
*(_QWORD *)dest = 0LL;
*((_QWORD *)dest + 1) = 0LL;
*((_DWORD *)dest + 4) = 0;
v4 = argv[1];
if ( v4 )
{
v5 = strlen(v4);
strncpy(dest, v4, v5);
}
else
{
strcpy(dest, "Anonymous");
}
v6 = *v15;
v7 = 0;
v20 = (char *)(v15 + 1);
while ( 1 )
{
printf("<%s> ", dest);
fgets((char *)v3, 8000, stdin);
if ( memcmp(v3, "send", 4uLL) )
break;
puts("inside send");
strcpy(s, (const char *)v3 + 5);
v13 = trim(s);
stream = fopen(v13, "r");
if ( stream )
{
fseek(stream, 0LL, 2);
v8 = ftell(stream);
v17 = v8;
fseek(stream, 0LL, 0);
puts("file open complete");
v9 = strrchr(s, 47);
if ( v9 )
sprintf(v20, "file:%d|%s", v8, v9 + 1);
else
sprintf(v20, "file:%d|%s", v8, s);
*v15 = ++v6;
++v7;
v10 = strrchr(s, 47);
if ( v10 )
printf("seq: %d file_size: %d file_name: %s\n", (unsigned __int8)v6, v8, v10 + 1);
else
printf("seq: %d file_size: %d file_name: %s\n", (unsigned __int8)v6, v8, s);
sleep(2u);
gettimeofday(&tv, 0LL);
v16 = 0;
while ( 1 )
{
printf("size of chat_msg: %d\n", 8LL);
v11 = fread(v3, 1uLL, 0x4FFFBuLL, stream);
memcpy(v15 + 5, v3, v11);
intToBytes(v11, (unsigned int *)v20);
v12 = GetInt32((unsigned __int8 *)v20);
printf("num bytes from the chat_msg: %u\n", v12);
v16 += v11;
*v15 = ++v6;
++v7;
printf("seq: %d Bytes read : %u\n", (unsigned __int8)v6, v11);
if ( v17 <= (int)v16 )
break;
usleep(0x7A120u);
if ( !v11 )
{
printf("0 bytes read");
goto LABEL_16;
}
}
puts("all bytes sent");
printf("num packets sent: %d, bytes: %d ", v7, v16);
LABEL_16:
gettimeofday(&v22, 0LL);
printf("processing delay: %f\n", ((double)SLODWORD(v22.tv_usec) - (double)SLODWORD(tv.tv_usec)) * 0.5);
LABEL_18:
printf("%d size: ", 80LL);
}
else
{
fprintf(stderr, "can't open file %s", s);
}
}
strcpy(v20, (const char *)v3);
*v15 = ++v6;
++v7;
goto LABEL_18;
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
void *v3; // r14
const char *v4; // rbx
size_t v5; // rax
int v6; // er15
unsigned int v7; // er13
unsigned int v8; // ebx
char *v9; // rax
char *v10; // rax
unsigned int v11; // ebx
unsigned int v12; // eax
const char *v13; // rax
FILE *stream; // [rsp+8h] [rbp-90h]
unsigned __int8 *v15; // [rsp+10h] [rbp-88h]
unsigned int v16; // [rsp+18h] [rbp-80h]
int v17; // [rsp+1Ch] [rbp-7Ch]
char *s; // [rsp+28h] [rbp-70h]
char *dest; // [rsp+30h] [rbp-68h]
char *v20; // [rsp+38h] [rbp-60h]
struct timeval tv; // [rsp+40h] [rbp-58h] BYREF
struct timeval v22; // [rsp+50h] [rbp-48h] BYREF

setbuf(stdout, 0LL);
v15 = (unsigned __int8 *)tramp_initialize();
dest = (char *)tramp_initialize();
puts("finished tramp init");
v3 = malloc(0x50000uLL);
s = (char *)malloc(0x50uLL);
tramp_publish();
tramp_publish();
*(_QWORD *)dest = 0LL;
*((_QWORD *)dest + 1) = 0LL;
*((_DWORD *)dest + 4) = 0;
v4 = argv[1];
if ( v4 )
{
v5 = strlen(v4);
strncpy(dest, v4, v5);
}
else
{
strcpy(dest, "Anonymous");
}
v6 = *v15;
v7 = 0;
v20 = (char *)(v15 + 1);
while ( 1 )
{
printf("<%s> ", dest);
fgets((char *)v3, 8000, stdin);
if ( memcmp(v3, "send", 4uLL) )
break;
puts("inside send");
strcpy(s, (const char *)v3 + 5);
v13 = func9(s);
stream = fopen(v13, "r");
if ( stream )
{
fseek(stream, 0LL, 2);
v8 = ftell(stream);
v17 = v8;
fseek(stream, 0LL, 0);
puts("file open complete");
v9 = strrchr(s, 47);
if ( v9 )
sprintf(v20, "file:%d|%s", v8, v9 + 1);
else
sprintf(v20, "file:%d|%s", v8, s);
*v15 = ++v6;
++v7;
v10 = strrchr(s, 47);
if ( v10 )
printf("seq: %d file_size: %d file_name: %s\n", (unsigned __int8)v6, v8, v10 + 1);
else
printf("seq: %d file_size: %d file_name: %s\n", (unsigned __int8)v6, v8, s);
sleep(2u);
gettimeofday(&tv, 0LL);
v16 = 0;
while ( 1 )
{
printf("size of chat_msg: %d\n", 8LL);
v11 = fread(v3, 1uLL, 0x4FFFBuLL, stream);
memcpy(v15 + 5, v3, v11);
func7(v11, (unsigned int *)v20);
v12 = func8((unsigned __int8 *)v20);
printf("num bytes from the chat_msg: %u\n", v12);
v16 += v11;
*v15 = ++v6;
++v7;
printf("seq: %d Bytes read : %u\n", (unsigned __int8)v6, v11);
if ( v17 <= (int)v16 )
break;
usleep(0x7A120u);
if ( !v11 )
{
printf("0 bytes read");
goto LABEL_16;
}
}
puts("all bytes sent");
printf("num packets sent: %d, bytes: %d ", v7, v16);
LABEL_16:
gettimeofday(&v22, 0LL);
printf("processing delay: %f\n", ((double)SLODWORD(v22.tv_usec) - (double)SLODWORD(tv.tv_usec)) * 0.5);
LABEL_18:
printf("%d size: ", 80LL);
}
else
{
fprintf(stderr, "can't open file %s", s);
}
}
strcpy(v20, (const char *)v3);
*v15 = ++v6;
++v7;
goto LABEL_18;
}


decompiled_code:
void *__fastcall tramp_initialize(const char *a1, size_t a2)
{
unsigned int v2; // er12
int v3; // eax
__int64 v4; // rbx
void *v5; // rax
void *v6; // r12

v2 = strtol(a1, 0LL, 36);
printf("Initializing label '%s' of size '%lu'. Key is '%x'.\n", a1, a2, v2);
v3 = shmget(v2, a2, 1920);
if ( v3 < 0 )
{
v4 = (unsigned int)shmget(v2, a2, 256);
printf("The segment is already present on this device with id '%d'.\n", v4);
if ( (int)v4 < 0 )
{
fwrite("Could not create data segment!\n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
}
else
{
LODWORD(v4) = v3;
printf("The segment was not present on this device. Created it with id '%d'.\n", (unsigned int)v3);
}
v5 = shmat(v4, 0LL, 0);
v6 = v5;
if ( v5 == (void *)-1LL )
{
fwrite("Could not attach data segment!\n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
printf(
"The segment with label '%s' located at '%p' with id '%d' is attached with flag '%d'.\n",
a1,
v5,
(unsigned int)v4,
0LL);
return v6;
}

canonical_decompiled_code:
void *__fastcall func6(const char *a1, size_t a2)
{
unsigned int v2; // er12
int v3; // eax
__int64 v4; // rbx
void *v5; // rax
void *v6; // r12

v2 = strtol(a1, 0LL, 36);
printf("Initializing label '%s' of size '%lu'. Key is '%x'.\n", a1, a2, v2);
v3 = shmget(v2, a2, 1920);
if ( v3 < 0 )
{
v4 = (unsigned int)shmget(v2, a2, 256);
printf("The segment is already present on this device with id '%d'.\n", v4);
if ( (int)v4 < 0 )
{
fwrite("Could not create data segment!\n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
}
else
{
LODWORD(v4) = v3;
printf("The segment was not present on this device. Created it with id '%d'.\n", (unsigned int)v3);
}
v5 = shmat(v4, 0LL, 0);
v6 = v5;
if ( v5 == (void *)-1LL )
{
fwrite("Could not attach data segment!\n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
printf(
"The segment with label '%s' located at '%p' with id '%d' is attached with flag '%d'.\n",
a1,
v5,
(unsigned int)v4,
0LL);
return v6;
}

original_code:
void* tramp_initialize(char *label, size_t size) {
 char *shm = ((void *)0);
 int shmid = -1;
 int shmflg = 0;
 key_t key = strtol(label, ((void *)0), 36);
  printf("Initializing label '%s' of size '%lu'. Key is '%x'.\n", label, size, key);
 shmid = shmget(key, size, 01000 | 02000 | 0400 | 0200);
 if(shmid < 0) {
  shmid = shmget(key, size, 0 | 0400);
   printf("The segment is already present on this device with id '%d'.\n", shmid);
 } else {
   printf("The segment was not present on this device. Created it with id '%d'.\n", shmid);
 }
 if(shmid < 0) {
  fprintf(stderr, "Could not create data segment!\n");
  exit(1);
 }
 shm = (char *) shmat(shmid, ((void *)0), shmflg);
 if(shm == (char *) -1) {
  fprintf(stderr, "Could not attach data segment!\n");
  exit(1);
 }
  printf("The segment with label '%s' located at '%p' with id '%d' is attached with flag '%d'.\n", label, shm, shmid, shmflg);
 return shm;
}

canonical_original_code:
void *tramp_initialize(char *label, unsigned long size) {
 char *shm = ((void *)0);
 int shmid = -1;
 int shmflg = 0;
 int key = strtol(label, ((void *)0), 36);
  printf("Initializing label '%s' of size '%lu'. Key is '%x'.\n", label, size, key);
 shmid = shmget(key, size, 01000 | 02000 | 0400 | 0200);
 if(shmid < 0) {
  shmid = shmget(key, size, 0 | 0400);
   printf("The segment is already present on this device with id '%d'.\n", shmid);
 } else {
   printf("The segment was not present on this device. Created it with id '%d'.\n", shmid);
 }
 if(shmid < 0) {
  fprintf(stderr, "Could not create data segment!\n");
  exit(1);
 }
 shm = (char *) shmat(shmid, ((void *)0), shmflg);
 if(shm == (char *) -1) {
  fprintf(stderr, "Could not attach data segment!\n");
  exit(1);
 }
  printf("The segment with label '%s' located at '%p' with id '%d' is attached with flag '%d'.\n", label, shm, shmid, shmflg);
 return shm;
}

variable_types:
label
char *

variable_types:
size
unsigned long

variable_types:
shm
char *

variable_types: shmid int

variable_types: shmflg int

variable_types: key int


printMatrix

decompiled_code:
int printMatrix()
{
double *v0; // r12
double *v1; // rbp
double *v2; // rbx
int result; // eax

v0 = (double *)&Y;
v1 = (double *)&unk_40FC00;
do
{
v2 = v1 - 2000;
do
printf("%7.3f ", *v2++);
while ( v2 != v1 );
result = printf(" | %7.3f \n", *v0++);
v1 += 2000;
}
while ( v1 != (double *)((char *)&unk_1E84800 + (_QWORD)&unk_40FC00) );
return result;
}

canonical_decompiled_code:
int func7()
{
double *v0; // r12
double *v1; // rbp
double *v2; // rbx
int result; // eax

v0 = (double *)&Y;
v1 = (double *)&unk_40FC00;
do
{
v2 = v1 - 2000;
do
printf("%7.3f ", *v2++);
while ( v2 != v1 );
result = printf(" | %7.3f \n", *v0++);
v1 += 2000;
}
while ( v1 != (double *)((char *)&unk_1E84800 + (_QWORD)&unk_40FC00) );
return result;
}

variable_types: i int

variable_types: j int


printSol

decompiled_code:
int printSol()
{
__int64 i; // rbx
int result; // eax

puts("--- Solution of Y=AX: ");
for ( i = 0LL; i != 2000; ++i )
result = printf(" X%d = %7.3f \n", (unsigned int)i, X[i]);
return result;
}

canonical_decompiled_code:
int func8()
{
__int64 i; // rbx
int result; // eax

puts("--- Solution of Y=AX: ");
for ( i = 0LL; i != 2000; ++i )
result = printf(" X%d = %7.3f \n", (unsigned int)i, X[i]);
return result;
}

original_code:
void printSol()
{
 int i, j;
 printf("--- Solution of Y=AX: \n");
 for (i=0; i<2000; i++) {
  printf(" X%d = %7.3f \n", i, X[i]);
 }
}

canonical_original_code:
void printSol()
{
 int i; int j;
 printf("--- Solution of Y=AX: \n");
 for (i=0; i<2000; i++) {
  printf(" X%d = %7.3f \n", i, X[i]);
 }
}


decompiled_code:
int __fastcall ArrNode_Print(__int64 a1, __int64 a2)
{
__int64 i; // rbx
int result; // eax

if ( a2 > 0 )
{
for ( i = 0LL; i != a2; ++i )
result = printf("ArrNode_Print: element %d points to %d\n", (unsigned int)i, *(_QWORD *)(a1 + 8 * i));
}
return result;
}

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

if ( a2 > 0 )
{
for ( i = 0LL; i != a2; ++i )
result = printf("ArrNode_Print: element %d points to %d\n", (unsigned int)i, *(_QWORD *)(a1 + 8 * i));
}
return result;
}

original_code:
void ArrNode_Print(pArrNode an, long num)
{
 int i;
 for (i=0; i<num; i++)
  printf("ArrNode_Print: element %d points to %d\n", i, an->nodes[i]);
}

canonical_original_code:
void ArrNode_Print(struct ArrNode *an, long num)
{
 int i;
 for (i=0; i<num; i++)
  printf("ArrNode_Print: element %d points to %d\n", i, an->nodes[i]);
}

variable_types: struct ArrNode { struct tagIntListNode { int data; struct tagIntListNode *next; struct tagIntListNode *prev; struct omp_lock_t { unsigned char _x[4]; } lock; int deleted; } *nodes[500]; int curPtr; int numElmt; } *

variable_types: num long

user_defined_types: struct ArrNode { struct tagIntListNode *nodes[500]; int curPtr; int numElmt; }

user_defined_types: struct tagIntListNode { int data; struct tagIntListNode *next; struct tagIntListNode *prev; struct omp_lock_t lock; int deleted; }


decompiled_code:
__int64 __fastcall sys_futex(__int64 a1, unsigned int a2, unsigned int a3, __int64 a4)
{
return syscall(202LL, a1, a2, a3, a4, 0LL, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, unsigned int a2, unsigned int a3, __int64 a4)
{
return syscall(202LL, a1, a2, a3, a4, 0LL, 0LL);
}

original_code:
int sys_futex(int *uaddr, int op, int val, const struct timespec *timeout)
{
  return syscall(202, uaddr, op, val, timeout, ((void *)0), 0);
}

canonical_original_code:
int sys_futex(int *uaddr, int op, int val, struct timespec *timeout)
{
  return syscall(202, uaddr, op, val, timeout, ((void *)0), 0);
}

variable_types:
uaddr
int *

variable_types: op int

variable_types: val int

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


decompiled_code:
__int64 __fastcall futex_await(__int64 a1, unsigned int a2)
{
return sys_futex(a1, 2u, a2, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, unsigned int a2)
{
return func0(a1, 2u, a2, 0LL);
}

original_code:
int futex_await(struct futex *futx, int signal)
{
 return sys_futex(&futx->count, 2, signal, ((void *)0));
}

variable_types:
futx
struct futex { int count; } *

variable_types: signal int


decompiled_code:
void __fastcall futex_init(_DWORD *a1, int a2)
{
*a1 = a2;
}

canonical_decompiled_code:
void __fastcall func4(_DWORD *a1, int a2)
{
*a1 = a2;
}

original_code:
void futex_init(struct futex *futx, int val)
{
 futx->count = val;
 __futex_commit();
}


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

result = *a1;
*(_QWORD *)(a2 + 16) = *a1;
*(_QWORD *)(result + 24) = a2;
*(_QWORD *)(a2 + 24) = 0LL;
*a1 = a2;
return result;
}

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

result = *a1;
*(_QWORD *)(a2 + 16) = *a1;
*(_QWORD *)(result + 24) = a2;
*(_QWORD *)(a2 + 24) = 0LL;
*a1 = a2;
return result;
}

original_code:
void mythread_q_append(mythread_queue_t *queue,mythread_helper_t *newElement)
{
 newElement->prev = queue->head;
 queue->head->next = newElement;
 newElement->next = ((void *)0);
 queue->head = newElement;
}

canonical_original_code:
void mythread_q_append(struct mythread_queue *queue, struct mythread_helper *newElement)
{
 newElement->prev = queue->head;
 queue->head->next = newElement;
 newElement->next = ((void *)0);
 queue->head = newElement;
}

variable_types: struct mythread_queue { struct mythread_helper { int pid; char *threadStack; struct mythread_helper *prev; enum MTHREADSTATE { NEW=0, READY=1, RUNNING=2, WAITING=3, TERMINATED=4 } currState; struct futex { int count; } thread_futex; int joiner_thread; int hasChildren; } *head; struct mythread_helper *currThread; } *

variable_types:
newElement
struct mythread_helper *

user_defined_types: struct mythread_queue { struct mythread_helper *head; struct mythread_helper *currThread; }

user_defined_types: struct mythread_helper { int pid; char *threadStack; struct mythread_helper *prev; enum MTHREADSTATE currState; struct futex thread_futex; int joiner_thread; int hasChildren; }


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

result = *(_QWORD *)(*(_QWORD *)(a1 + 8) + 24LL);
*(_QWORD *)(a1 + 8) = result;
return result;
}

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

result = *(_QWORD *)(*(_QWORD *)(a1 + 8) + 24LL);
*(_QWORD *)(a1 + 8) = result;
return result;
}

original_code:
void mythread_q_remove_end(mythread_queue_t *queue)
{
 queue->tail = queue->tail->next;
}

canonical_original_code:
void mythread_q_remove_end(struct mythread_queue *queue)
{
 queue->tail = queue->tail->next;
}

user_defined_types: enum MTHREADSTATE { NEW=0, READY=1, RUNNING=2, WAITING=3, TERMINATED=4 }


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

for ( result = *(_QWORD *)(a1 + 8); result; result = *(_QWORD *)(result + 24) )
{
if ( *(_DWORD *)result == a2 )
break;
}
return result;
}

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

for ( result = *(_QWORD *)(a1 + 8); result; result = *(_QWORD *)(result + 24) )
{
if ( *(_DWORD *)result == a2 )
break;
}
return result;
}

original_code:
mythread_helper_t *mythread_q_find(mythread_queue_t *queue,mythread_t tid)
{
 mythread_helper_t *currNode;
 currNode = queue->tail;
 while (currNode != ((void *)0))
 {
  if (currNode->pid == tid)
  {
   break;
  }
  currNode = currNode->next;
 }
 return currNode;
}

canonical_original_code:
struct mythread_helper *mythread_q_find(struct mythread_queue *queue, int tid)
{
 struct mythread_helper *currNode;
 currNode = queue->tail;
 while (currNode != ((void *)0))
 {
  if (currNode->pid == tid)
  {
   break;
  }
  currNode = currNode->next;
 }
 return currNode;
}

variable_types: struct mythread_queue { struct mythread_helper *head; struct mythread_helper *currThread; } *

variable_types: tid int

variable_types: struct mythread_helper { int pid; char *threadStack; struct mythread_helper *prev; enum MTHREADSTATE { NEW=0, READY=1, RUNNING=2, WAITING=3, TERMINATED=4 } currState; struct futex { int count; } thread_futex; int joiner_thread; int hasChildren; } *


original_code:
int mythread_q_remove_specific(mythread_queue_t *queue,mythread_t tid)
{
 mythread_helper_t *nodeToRemove = mythread_q_find(queue,tid);
 if (nodeToRemove != ((void *)0))
 {
  if (nodeToRemove != queue->head)
  {
   nodeToRemove->next->prev = nodeToRemove->prev;
  }
  else
  {
   queue->head = nodeToRemove->prev;
  }
  if (nodeToRemove != queue->tail)
  {
   nodeToRemove->prev->next = nodeToRemove->next;
  }
  else
  {
   queue->tail = nodeToRemove->next;
  }
  nodeToRemove->next = ((void *)0);
  nodeToRemove->prev = ((void *)0);
 }
 else
 {
  return -1;
 }
 return 0;
}

canonical_original_code:
int mythread_q_remove_specific(struct mythread_queue *queue, int tid)
{
 struct mythread_helper *nodeToRemove = mythread_q_find(queue,tid);
 if (nodeToRemove != ((void *)0))
 {
  if (nodeToRemove != queue->head)
  {
   nodeToRemove->next->prev = nodeToRemove->prev;
  }
  else
  {
   queue->head = nodeToRemove->prev;
  }
  if (nodeToRemove != queue->tail)
  {
   nodeToRemove->prev->next = nodeToRemove->next;
  }
  else
  {
   queue->tail = nodeToRemove->next;
  }
  nodeToRemove->next = ((void *)0);
  nodeToRemove->prev = ((void *)0);
 }
 else
 {
  return -1;
 }
 return 0;
}


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

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

original_code:
unsigned int mythread_q_count(mythread_queue_t *queue)
{
 unsigned int count = 0;
 mythread_helper_t *currNode;
 currNode = queue->tail;
 if (currNode == ((void *)0))
 {
  return 0;
 }
 while (currNode->next != ((void *)0))
 {
  currNode = currNode->next;
  count++;
 }
 return count;
}

canonical_original_code:
unsigned int mythread_q_count(struct mythread_queue *queue)
{
 unsigned int count = 0;
 struct mythread_helper *currNode;
 currNode = queue->tail;
 if (currNode == ((void *)0))
 {
  return 0;
 }
 while (currNode->next != ((void *)0))
 {
  currNode = currNode->next;
  count++;
 }
 return count;
}

variable_types:
count
unsigned int


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

v1 = *(_QWORD *)(a1 + 8);
if ( !v1 )
return 0;
v2 = *(_QWORD *)(v1 + 24);
if ( !v2 )
return 0;
v3 = 0;
do
{
v3 += *(_DWORD *)(v2 + 32) != 4;
v2 = *(_QWORD *)(v2 + 24);
}
while ( v2 );
return v3;
}

original_code:
unsigned int mythread_q_get_alive_count(mythread_queue_t *queue)
{
 unsigned int count = 0;
 mythread_helper_t *currNode;
 currNode = queue->tail;
 if (currNode == ((void *)0))
 {
  return 0;
 }
 while (currNode->next != ((void *)0))
 {
  currNode = currNode->next;
  if (currNode->currState != TERMINATED)
  {
   count++;
  }
 }
 return count;
}

canonical_original_code:
unsigned int mythread_q_get_alive_count(struct mythread_queue *queue)
{
 unsigned int count = 0;
 struct mythread_helper *currNode;
 currNode = queue->tail;
 if (currNode == ((void *)0))
 {
  return 0;
 }
 while (currNode->next != ((void *)0))
 {
  currNode = currNode->next;
  if (currNode->currState != TERMINATED)
  {
   count++;
  }
 }
 return count;
}


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

v1 = *(_QWORD *)(a1 + 8);
if ( !v1 )
return 0;
v2 = *(_QWORD *)(v1 + 24);
if ( !v2 )
return 0;
v3 = 0;
do
{
v3 += *(_DWORD *)(v2 + 32) == 1;
v2 = *(_QWORD *)(v2 + 24);
}
while ( v2 );
return v3;
}

original_code:
unsigned int mythread_q_get_ready_count(mythread_queue_t *queue)
{
 unsigned int count = 0;
 mythread_helper_t *currNode;
 currNode = queue->tail;
 if (currNode == ((void *)0))
 {
  return 0;
 }
 while (currNode->next != ((void *)0))
 {
  currNode = currNode->next;
  if (currNode->currState == READY)
  {
   count++;
  }
 }
 return count;
}

canonical_original_code:
unsigned int mythread_q_get_ready_count(struct mythread_queue *queue)
{
 unsigned int count = 0;
 struct mythread_helper *currNode;
 currNode = queue->tail;
 if (currNode == ((void *)0))
 {
  return 0;
 }
 while (currNode->next != ((void *)0))
 {
  currNode = currNode->next;
  if (currNode->currState == READY)
  {
   count++;
  }
 }
 return count;
}


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

for ( result = *(_QWORD *)(a1 + 8); result; result = *(_QWORD *)(result + 24) )
{
if ( *(_DWORD *)(result + 32) == 1 )
break;
}
return result;
}

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

for ( result = *(_QWORD *)(a1 + 8); result; result = *(_QWORD *)(result + 24) )
{
if ( *(_DWORD *)(result + 32) == 1 )
break;
}
return result;
}

original_code:
mythread_helper_t *mythread_q_get_highest_ready_thread(mythread_queue_t *queue)
{
 mythread_helper_t *currNode;
 currNode = queue->tail;
 while (currNode != ((void *)0))
 {
  if (currNode->currState == READY)
  {
   return currNode;
  }
  currNode = currNode->next;
 }
 return currNode;
}

canonical_original_code:
struct mythread_helper *mythread_q_get_highest_ready_thread(struct mythread_queue *queue)
{
 struct mythread_helper *currNode;
 currNode = queue->tail;
 while (currNode != ((void *)0))
 {
  if (currNode->currState == READY)
  {
   return currNode;
  }
  currNode = currNode->next;
 }
 return currNode;
}

return_type: struct mythread_helper { int pid; char *threadStack; struct mythread_helper *prev; enum MTHREADSTATE { NEW=0, READY=1, RUNNING=2, WAITING=3, TERMINATED=4 } currState; struct futex { int count; } thread_futex; int joiner_thread; int hasChildren; } *


decompiled_code:
__int64 __fastcall mythread_join(unsigned int a1)
{
unsigned int v1; // ebp
__int64 v2; // rax
__int64 v4; // rdx
__int64 v5[19]; // [rsp+0h] [rbp-98h] BYREF

v5[0] = 0LL;
v5[1] = 0LL;
v5[2] = 0LL;
v5[3] = 0LL;
v5[4] = 0LL;
v5[5] = 0LL;
v5[6] = 0LL;
v5[7] = 0LL;
v5[8] = 0LL;
v5[9] = 0LL;
v5[10] = 0LL;
v5[11] = 0LL;
v5[12] = 0LL;
v5[13] = 0LL;
v5[14] = 0LL;
v5[15] = 0LL;
v1 = 6;
if ( (int)a1 > 0 )
{
v2 = mythread_q_find(mythread_queue, a1);
if ( v2 )
{
if ( *(_DWORD *)(v2 + 32) == 4 || (v1 = *(_DWORD *)(v2 + 40)) != 0 )
{
sprintf((char *)v5, "Thread %d has joiner_thread %d", a1, *(unsigned int *)(v2 + 40));
writeLog("mythread_join", 16LL, v5);
v1 = 0;
}
else
{
v4 = mythread_queue;
*(_DWORD *)(v2 + 40) = **(_DWORD **)(mythread_queue + 16);
*(_DWORD *)(*(_QWORD *)(v4 + 16) + 32LL) = 3;
mythread_swapcontext(*(_QWORD *)(v4 + 16));
}
}
else
{
sprintf((char *)v5, "Couldnt find thread %d in the queue", a1);
writeLog("mythread_join", 8LL, v5);
v1 = 6;
}
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1)
{
unsigned int v1; // ebp
__int64 v2; // rax
__int64 v4; // rdx
__int64 v5[19]; // [rsp+0h] [rbp-98h] BYREF

v5[0] = 0LL;
v5[1] = 0LL;
v5[2] = 0LL;
v5[3] = 0LL;
v5[4] = 0LL;
v5[5] = 0LL;
v5[6] = 0LL;
v5[7] = 0LL;
v5[8] = 0LL;
v5[9] = 0LL;
v5[10] = 0LL;
v5[11] = 0LL;
v5[12] = 0LL;
v5[13] = 0LL;
v5[14] = 0LL;
v5[15] = 0LL;
v1 = 6;
if ( (int)a1 > 0 )
{
v2 = mythread_q_find(mythread_queue, a1);
if ( v2 )
{
if ( *(_DWORD *)(v2 + 32) == 4 || (v1 = *(_DWORD *)(v2 + 40)) != 0 )
{
sprintf((char *)v5, "Thread %d has joiner_thread %d", a1, *(unsigned int *)(v2 + 40));
writeLog("mythread_join", 16LL, v5);
v1 = 0;
}
else
{
v4 = mythread_queue;
*(_DWORD *)(v2 + 40) = **(_DWORD **)(mythread_queue + 16);
*(_DWORD *)(*(_QWORD *)(v4 + 16) + 32LL) = 3;
mythread_swapcontext(*(_QWORD *)(v4 + 16));
}
}
else
{
sprintf((char *)v5, "Couldnt find thread %d in the queue", a1);
writeLog("mythread_join", 8LL, v5);
v1 = 6;
}
}
return v1;
}

original_code:
int mythread_join(mythread_t target_thread, void **status)
{
 char logStr[128] = {'\0'};
 mythread_helper_t *joinee_thread;
 if (target_thread > 0)
 {
  joinee_thread = mythread_q_find(mythread_queue,target_thread);
  if (joinee_thread != ((void *)0) )
  {
   if (joinee_thread->currState == TERMINATED || joinee_thread->joiner_thread != 0)
   {
    sprintf(logStr,"Thread %d has joiner_thread %d",target_thread, joinee_thread->joiner_thread);
    writeLog(__func__,VDEBUG,logStr);
    return MNOERR;
   }
   joinee_thread->joiner_thread = mythread_queue->currThread->pid;
   mythread_queue->currThread->currState = WAITING;
   mythread_swapcontext(mythread_queue->currThread);
   return MNOERR;
  }
  else
  {
   sprintf(logStr,"Couldnt find thread %d in the queue",target_thread);
   writeLog(__func__,VSEVERE,logStr);
   return MERR_INVALIDTID;
  }
 }
 else
 {
  return MERR_INVALIDTID;
 }
}

canonical_original_code:
int mythread_join(int target_thread, void **status)
{
 char logStr[128] = {'\0'};
 struct mythread_helper *joinee_thread;
 if (target_thread > 0)
 {
  joinee_thread = mythread_q_find(mythread_queue,target_thread);
  if (joinee_thread != ((void *)0) )
  {
   if (joinee_thread->currState == TERMINATED || joinee_thread->joiner_thread != 0)
   {
    sprintf(logStr,"Thread %d has joiner_thread %d",target_thread, joinee_thread->joiner_thread);
    writeLog(__func__,VDEBUG,logStr);
    return MNOERR;
   }
   joinee_thread->joiner_thread = mythread_queue->currThread->pid;
   mythread_queue->currThread->currState = WAITING;
   mythread_swapcontext(mythread_queue->currThread);
   return MNOERR;
  }
  else
  {
   sprintf(logStr,"Couldnt find thread %d in the queue",target_thread);
   writeLog(__func__,VSEVERE,logStr);
   return MERR_INVALIDTID;
  }
 }
 else
 {
  return MERR_INVALIDTID;
 }
}

variable_types:
status
void **

variable_types:
logStr
char [128]


decompiled_code:
// write access to const memory has been detected, the output may be wrong!
void mythread_key_helper_init()
{
if ( !mythread_key_helper )
{
*(&mythread_key_helper + 1) = 0LL;
*(_QWORD *)((char *)&loc_B + 5) = 0LL;
*(_QWORD *)((char *)&loc_16 + 2) = 0LL;
*(&mythread_key_helper + 2) = 0LL;
}
}

canonical_decompiled_code:
void func0()
{
if ( !mythread_key_helper )
{
*(&mythread_key_helper + 1) = 0LL;
*(_QWORD *)((char *)&loc_B + 5) = 0LL;
*(_QWORD *)((char *)&loc_16 + 2) = 0LL;
*(&mythread_key_helper + 2) = 0LL;
}
}

original_code:
void mythread_key_helper_init()
{
 if(mythread_key_helper.headkey == ((void *)0))
 {
  mythread_key_helper.headkey = ((void *)0);
  mythread_key_helper.tailkey = ((void *)0);
  mythread_key_helper.headkey->next = ((void *)0);
  mythread_key_helper.tailkey->prev = ((void *)0);
  mythread_key_helper.currKey = mythread_key_helper.headkey;
 }
}


decompiled_code:
__int64 __fastcall mythread_key_create(__int64 a1)
{
__int64 v1; // rax
__int64 v2; // rdx
__int64 v3; // rbx
_QWORD *v4; // rbx
__int64 v5; // rbx
__int64 v6; // rbp
__int64 i; // rdx
__int64 result; // rax

puts("In set specific ");
mythread_key_helper_init();
puts("In set specific1 ");
*(_QWORD *)(*(&mythread_key_helper + 2) + 16) = a1;
v1 = *(_QWORD *)(*(&mythread_key_helper + 2) + 16);
*(&mythread_key_helper + 2) = v1;
v2 = *(&mythread_key_helper + 1);
*(_QWORD *)(v2 + 24) = v1;
*(_QWORD *)(v1 + 16) = v2;
v3 = *(_QWORD *)(a1 + 16);
*(&mythread_key_helper + 1) = v3;
*(_QWORD *)(v3 + 24) = a1;
*(_QWORD *)(v3 + 16) = malloc(8uLL);
puts("In set specific3 ");
*(_DWORD *)a1 = ++numKeys;
puts("In set specific4 ");
*(_QWORD *)(a1 + 8) = malloc(8uLL);
v4 = malloc(8uLL);
*(_QWORD *)(a1 + 32) = v4;
*v4 = malloc(8uLL);
puts("In set specific5 ");
*(_QWORD *)(**(_QWORD **)(a1 + 32) + 24LL) = *(_QWORD *)(mythread_queue + 8);
*(_QWORD *)(*(_QWORD *)(a1 + 32) + 8LL) = **(_QWORD **)(a1 + 32);
*(_QWORD *)(*(_QWORD *)(*(_QWORD *)(a1 + 32) + 8LL) + 24LL) = *(_QWORD *)(**(_QWORD **)(a1 + 32) + 24LL);
v5 = *(_QWORD *)(*(_QWORD *)(a1 + 32) + 8LL);
v6 = *(_QWORD *)(v5 + 24);
puts("In set specific 6");
for ( i = mythread_queue; *(_QWORD *)(i + 8) != v6; v6 = *(_QWORD *)(v6 + 24) )
{
*(_QWORD *)(*(_QWORD *)(v5 + 8) + 16LL) = v5;
*(_QWORD *)(v5 + 24) = v6;
v5 = *(_QWORD *)(v5 + 8);
}
puts("In set specific 7");
*(_QWORD *)(v5 + 8) = 0LL;
result = *(_QWORD *)(a1 + 32);
*(_QWORD *)(result + 8) = v5;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
__int64 v1; // rax
__int64 v2; // rdx
__int64 v3; // rbx
_QWORD *v4; // rbx
__int64 v5; // rbx
__int64 v6; // rbp
__int64 i; // rdx
__int64 result; // rax

puts("In set specific ");
func0();
puts("In set specific1 ");
*(_QWORD *)(*(&mythread_key_helper + 2) + 16) = a1;
v1 = *(_QWORD *)(*(&mythread_key_helper + 2) + 16);
*(&mythread_key_helper + 2) = v1;
v2 = *(&mythread_key_helper + 1);
*(_QWORD *)(v2 + 24) = v1;
*(_QWORD *)(v1 + 16) = v2;
v3 = *(_QWORD *)(a1 + 16);
*(&mythread_key_helper + 1) = v3;
*(_QWORD *)(v3 + 24) = a1;
*(_QWORD *)(v3 + 16) = malloc(8uLL);
puts("In set specific3 ");
*(_DWORD *)a1 = ++numKeys;
puts("In set specific4 ");
*(_QWORD *)(a1 + 8) = malloc(8uLL);
v4 = malloc(8uLL);
*(_QWORD *)(a1 + 32) = v4;
*v4 = malloc(8uLL);
puts("In set specific5 ");
*(_QWORD *)(**(_QWORD **)(a1 + 32) + 24LL) = *(_QWORD *)(mythread_queue + 8);
*(_QWORD *)(*(_QWORD *)(a1 + 32) + 8LL) = **(_QWORD **)(a1 + 32);
*(_QWORD *)(*(_QWORD *)(*(_QWORD *)(a1 + 32) + 8LL) + 24LL) = *(_QWORD *)(**(_QWORD **)(a1 + 32) + 24LL);
v5 = *(_QWORD *)(*(_QWORD *)(a1 + 32) + 8LL);
v6 = *(_QWORD *)(v5 + 24);
puts("In set specific 6");
for ( i = mythread_queue; *(_QWORD *)(i + 8) != v6; v6 = *(_QWORD *)(v6 + 24) )
{
*(_QWORD *)(*(_QWORD *)(v5 + 8) + 16LL) = v5;
*(_QWORD *)(v5 + 24) = v6;
v5 = *(_QWORD *)(v5 + 8);
}
puts("In set specific 7");
*(_QWORD *)(v5 + 8) = 0LL;
result = *(_QWORD *)(a1 + 32);
*(_QWORD *)(result + 8) = v5;
return result;
}

original_code:
int mythread_key_create(mythread_key_t *key, void (*destructor)(void*))
{
 printf("In set specific \n");
 mythread_helper_t *currT;
 mythread_keyval_t *currV;
 mythread_key_helper_init();
 printf("In set specific1 \n");
 mythread_key_helper.currKey->next = key;
 mythread_key_helper.currKey = mythread_key_helper.currKey->next;
 mythread_key_helper.tailkey->prev =mythread_key_helper.currKey;
 mythread_key_helper.currKey->next = mythread_key_helper.tailkey;
 mythread_key_helper.tailkey = key->next;
 mythread_key_helper.tailkey->prev = key;
 mythread_key_helper.tailkey->next = (mythread_key_t *)malloc(sizeof(mythread_key_t *));
 printf("In set specific3 \n");
 key->keyID = ++numKeys;
 printf("In set specific4 \n");
 key->keyval = (mythread_keyval_t *)malloc(sizeof(mythread_keyval_t *));
 key->mythread_keyval_helper = (mythread_keyval_helper_t *)malloc(sizeof(mythread_keyval_helper_t *));
 key->mythread_keyval_helper->headkeyval = (mythread_keyval_t *)malloc(sizeof(mythread_keyval_t *));
 printf("In set specific5 \n");
 key->mythread_keyval_helper->headkeyval->thread = mythread_queue->tail;
 key->mythread_keyval_helper->tailkeyval = key->mythread_keyval_helper->headkeyval;
 key->mythread_keyval_helper->tailkeyval->thread = key->mythread_keyval_helper->headkeyval->thread;
 currT = (mythread_helper_t *)malloc(sizeof(mythread_helper_t *));
 currT = key->mythread_keyval_helper->tailkeyval->thread;
 currV = key->mythread_keyval_helper->tailkeyval;
 printf("In set specific 6\n");
 while(currT != mythread_queue->tail)
 {
  currV->next->prev = currV;
  currV->thread = currT;
  currV = currV->next;
  currT = currT->next;
 }
 printf("In set specific 7\n");
 currV->next = ((void *)0);
 key->mythread_keyval_helper->tailkeyval = currV;
}

canonical_original_code:
int mythread_key_create(struct mythread_key_t *key, void (*destructor)(void *))
{
 printf("In set specific \n");
 struct mythread_helper *currT;
 struct mythread_keyval *currV;
 mythread_key_helper_init();
 printf("In set specific1 \n");
 mythread_key_helper.currKey->next = key;
 mythread_key_helper.currKey = mythread_key_helper.currKey->next;
 mythread_key_helper.tailkey->prev =mythread_key_helper.currKey;
 mythread_key_helper.currKey->next = mythread_key_helper.tailkey;
 mythread_key_helper.tailkey = key->next;
 mythread_key_helper.tailkey->prev = key;
 mythread_key_helper.tailkey->next = (struct mythread_key_t *)malloc(sizeof(struct mythread_key_t *));
 printf("In set specific3 \n");
 key->keyID = ++numKeys;
 printf("In set specific4 \n");
 key->keyval = (struct mythread_keyval *)malloc(sizeof(struct mythread_keyval *));
 key->mythread_keyval_helper = (struct mythread_keyval_helper *)malloc(sizeof(struct mythread_keyval_helper *));
 key->mythread_keyval_helper->headkeyval = (struct mythread_keyval *)malloc(sizeof(struct mythread_keyval *));
 printf("In set specific5 \n");
 key->mythread_keyval_helper->headkeyval->thread = mythread_queue->tail;
 key->mythread_keyval_helper->tailkeyval = key->mythread_keyval_helper->headkeyval;
 key->mythread_keyval_helper->tailkeyval->thread = key->mythread_keyval_helper->headkeyval->thread;
 currT = (struct mythread_helper *)malloc(sizeof(struct mythread_helper *));
 currT = key->mythread_keyval_helper->tailkeyval->thread;
 currV = key->mythread_keyval_helper->tailkeyval;
 printf("In set specific 6\n");
 while(currT != mythread_queue->tail)
 {
  currV->next->prev = currV;
  currV->thread = currT;
  currV = currV->next;
  currT = currT->next;
 }
 printf("In set specific 7\n");
 currV->next = ((void *)0);
 key->mythread_keyval_helper->tailkeyval = currV;
}

variable_types: struct mythread_key_t { int keyID; struct mythread_keyval { void *value; struct mythread_keyval *next; struct mythread_keyval *prev; struct mythread_helper { int pid; char *threadStack; struct mythread_helper *prev; enum MTHREADSTATE { NEW=0, READY=1, RUNNING=2, WAITING=3, TERMINATED=4 } currState; struct futex { int count; } thread_futex; int joiner_thread; int hasChildren; } *thread; } *keyval; struct mythread_key_t *next; struct mythread_key_t *prev; struct mythread_keyval_helper { struct mythread_keyval *headkeyval; } *mythread_keyval_helper; } *

variable_types:
destructor
void (*)(void *)

user_defined_types: struct mythread_key_t { int keyID; struct mythread_keyval *keyval; struct mythread_key_t *next; struct mythread_key_t *prev; struct mythread_keyval_helper *mythread_keyval_helper; }

user_defined_types: struct mythread_keyval { void *value; struct mythread_keyval *next; struct mythread_keyval *prev; struct mythread_helper *thread; }