decompiled_code:
void __fastcall led_channel_hammerhead_set_value(unsigned int *a1, unsigned int a2)
{
unsigned int v2; // eax

if ( a1[1] != -1 )
{
v2 = led_util_scale_value(a2, *a1);
dprintf(a1[1], "%d", v2);
}
}

canonical_decompiled_code:
void __fastcall func3(unsigned int *a1, unsigned int a2)
{
unsigned int v2; // eax

if ( a1[1] != -1 )
{
v2 = led_util_scale_value(a2, *a1);
dprintf(a1[1], "%d", v2);
}
}

original_code:
static void
led_channel_hammerhead_set_value(const led_channel_hammerhead_t *self,
                                 int value)
{
  if( self->fd_brightness != -1 )
  {
    dprintf(self->fd_brightness, "%d", led_util_scale_value(value, self->cached_max_brightness));
  }
}

canonical_original_code:
static void led_channel_hammerhead_set_value(struct led_channel_hammerhead_t *self, int value)
{
  if( self->fd_brightness != -1 )
  {
    dprintf(self->fd_brightness, "%d", led_util_scale_value(value, self->cached_max_brightness));
  }
}

variable_types: value int


repo: nemomobile/mce-plugin-libhybris

decompiled_code:
void __fastcall led_control_hammerhead_value_cb(unsigned int *a1, unsigned int a2, unsigned int a3, unsigned int a4)
{
led_channel_hammerhead_set_value(a1, a2);
led_channel_hammerhead_set_value(a1 + 4, a3);
led_channel_hammerhead_set_value(a1 + 8, a4);
}

canonical_decompiled_code:
void __fastcall func4(unsigned int *a1, unsigned int a2, unsigned int a3, unsigned int a4)
{
func3(a1, a2);
func3(a1 + 4, a3);
func3(a1 + 8, a4);
}

original_code:
static void
led_control_hammerhead_value_cb(void *data, int r, int g, int b)
{
  const led_channel_hammerhead_t *channel = data;
  led_channel_hammerhead_set_value(channel + 0, r);
  led_channel_hammerhead_set_value(channel + 1, g);
  led_channel_hammerhead_set_value(channel + 2, b);
}

canonical_original_code:
static void led_control_hammerhead_value_cb(void *data, int r, int g, int b)
{
  struct led_channel_hammerhead_t *channel = data;
  led_channel_hammerhead_set_value(channel + 0, r);
  led_channel_hammerhead_set_value(channel + 1, g);
  led_channel_hammerhead_set_value(channel + 2, b);
}

variable_types:
data
void *

variable_types: r int

variable_types: g int

variable_types: b int

variable_types: struct led_channel_hammerhead_t { int cached_max_brightness; int fd_brightness; int fd_on_off_ms; int fd_rgb_start; } *

return_type: void


decompiled_code:
int __fastcall led_control_hammerhead_enable_cb(__int64 a1, unsigned __int8 a2)
{
led_channel_hammerhead_set_enabled(a1, a2);
led_channel_hammerhead_set_enabled(a1 + 16, a2);
return led_channel_hammerhead_set_enabled(a1 + 32, a2);
}

canonical_decompiled_code:
int __fastcall func6(__int64 a1, unsigned __int8 a2)
{
func5(a1, a2);
func5(a1 + 16, a2);
return func5(a1 + 32, a2);
}

original_code:
static void
led_control_hammerhead_enable_cb(void *data, _Bool enable)
{
  const led_channel_hammerhead_t *channel = data;
  led_channel_hammerhead_set_enabled(channel + 0, enable);
  led_channel_hammerhead_set_enabled(channel + 1, enable);
  led_channel_hammerhead_set_enabled(channel + 2, enable);
}

canonical_original_code:
static void led_control_hammerhead_enable_cb(void *data, _Bool enable)
{
  struct led_channel_hammerhead_t *channel = data;
  led_channel_hammerhead_set_enabled(channel + 0, enable);
  led_channel_hammerhead_set_enabled(channel + 1, enable);
  led_channel_hammerhead_set_enabled(channel + 2, enable);
}

user_defined_types: struct led_channel_hammerhead_t { int cached_max_brightness; int fd_brightness; int fd_on_off_ms; int fd_rgb_start; }


decompiled_code:
void __fastcall led_channel_hammerhead_set_blink(__int64 a1, unsigned int a2, unsigned int a3)
{
int v3; // eax
char v4[40]; // [rsp-28h] [rbp-28h] BYREF

if ( *(_DWORD *)(a1 + 8) != -1 )
{
v3 = snprintf(v4, 0x20uLL, "%d %d", a2, a3);
if ( (unsigned int)(v3 - 1) <= 0x1F )
write(*(_DWORD *)(a1 + 8), v4, v3);
}
}

canonical_decompiled_code:
void __fastcall func7(__int64 a1, unsigned int a2, unsigned int a3)
{
int v3; // eax
char v4[40]; // [rsp-28h] [rbp-28h] BYREF

if ( *(_DWORD *)(a1 + 8) != -1 )
{
v3 = snprintf(v4, 0x20uLL, "%d %d", a2, a3);
if ( (unsigned int)(v3 - 1) <= 0x1F )
write(*(_DWORD *)(a1 + 8), v4, v3);
}
}

original_code:
static void
led_channel_hammerhead_set_blink(const led_channel_hammerhead_t *self,
                                 int on_ms, int off_ms)
{
  if( self->fd_on_off_ms != -1 )
  {
    char tmp[32];
    int len = snprintf(tmp, sizeof tmp, "%d %d", on_ms, off_ms);
    if( len > 0 && len <= (int)sizeof tmp )
    {
      if( write(self->fd_on_off_ms, tmp, len) < 0 ) {
      }
    }
  }
}

canonical_original_code:
static void led_channel_hammerhead_set_blink(struct led_channel_hammerhead_t *self, int on_ms, int off_ms)
{
  if( self->fd_on_off_ms != -1 )
  {
    char tmp[32];
    int len = snprintf(tmp, sizeof tmp, "%d %d", on_ms, off_ms);
    if( len > 0 && len <= (int)sizeof tmp )
    {
      if( write(self->fd_on_off_ms, tmp, len) < 0 ) {
      }
    }
  }
}

variable_types:
tmp
char [32]

variable_types: len int


decompiled_code:
void __fastcall led_control_hammerhead_blink_cb(__int64 a1, unsigned int a2, unsigned int a3)
{
led_channel_hammerhead_set_blink(a1, a2, a3);
led_channel_hammerhead_set_blink(a1 + 16, a2, a3);
led_channel_hammerhead_set_blink(a1 + 32, a2, a3);
}

canonical_decompiled_code:
void __fastcall func8(__int64 a1, unsigned int a2, unsigned int a3)
{
func7(a1, a2, a3);
func7(a1 + 16, a2, a3);
func7(a1 + 32, a2, a3);
}

original_code:
static void
led_control_hammerhead_blink_cb(void *data, int on_ms, int off_ms)
{
  const led_channel_hammerhead_t *channel = data;
  led_channel_hammerhead_set_blink(channel + 0, on_ms, off_ms);
  led_channel_hammerhead_set_blink(channel + 1, on_ms, off_ms);
  led_channel_hammerhead_set_blink(channel + 2, on_ms, off_ms);
}

canonical_original_code:
static void led_control_hammerhead_blink_cb(void *data, int on_ms, int off_ms)
{
  struct led_channel_hammerhead_t *channel = data;
  led_channel_hammerhead_set_blink(channel + 0, on_ms, off_ms);
  led_channel_hammerhead_set_blink(channel + 1, on_ms, off_ms);
  led_channel_hammerhead_set_blink(channel + 2, on_ms, off_ms);
}


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

led_channel_hammerhead_close((__int64)a1);
v2 = led_util_read_number(*a2);
*a1 = v2;
if ( v2 <= 0
|| !(unsigned __int8)led_util_open_file(a1 + 1, a2[1])
|| !(unsigned __int8)led_util_open_file(a1 + 2, a2[2])
|| (result = led_util_open_file(a1 + 3, a2[3]), !(_BYTE)result) )
{
led_channel_hammerhead_close((__int64)a1);
result = 0LL;
}
return result;
}

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

func1((__int64)a1);
v2 = led_util_read_number(*a2);
*a1 = v2;
if ( v2 <= 0
|| !(unsigned __int8)led_util_open_file(a1 + 1, a2[1])
|| !(unsigned __int8)led_util_open_file(a1 + 2, a2[2])
|| (result = led_util_open_file(a1 + 3, a2[3]), !(_BYTE)result) )
{
func1((__int64)a1);
result = 0LL;
}
return result;
}

original_code:
static _Bool
led_channel_hammerhead_probe(led_channel_hammerhead_t *self,
                             const led_paths_hammerhead_t *path)
{
  _Bool res = 0;
  led_channel_hammerhead_close(self);
  if( (self->cached_max_brightness = led_util_read_number(path->max_brightness)) <= 0 )
  {
    goto cleanup;
  }
  if( !led_util_open_file(&self->fd_brightness, path->brightness) ||
      !led_util_open_file(&self->fd_on_off_ms, path->on_off_ms) ||
      !led_util_open_file(&self->fd_rgb_start, path->rgb_start) )
  {
    goto cleanup;
  }
  res = 1;
cleanup:
  if( !res ) led_channel_hammerhead_close(self);
  return res;
}

canonical_original_code:
static _Bool led_channel_hammerhead_probe(struct led_channel_hammerhead_t *self, struct led_paths_hammerhead_t *path)
{
  _Bool res = 0;
  led_channel_hammerhead_close(self);
  if( (self->cached_max_brightness = led_util_read_number(path->max_brightness)) <= 0 )
  {
    goto cleanup;
  }
  if( !led_util_open_file(&self->fd_brightness, path->brightness) ||
      !led_util_open_file(&self->fd_on_off_ms, path->on_off_ms) ||
      !led_util_open_file(&self->fd_rgb_start, path->rgb_start) )
  {
    goto cleanup;
  }
  res = 1;
cleanup:
  if( !res ) led_channel_hammerhead_close(self);
  return res;
}

variable_types: struct led_paths_hammerhead_t { char *max_brightness; char *brightness; char *on_off_ms; char *rgb_start; } *


decompiled_code:
__int64 __fastcall led_control_hammerhead_probe(__int64 a1)
{
unsigned int v1; // ebp

led_channel_hammerhead_init(channel_3);
led_channel_hammerhead_init(dword_3F0);
led_channel_hammerhead_init(dword_400);
*(_QWORD *)a1 = "hammerhead";
*(_QWORD *)(a1 + 8) = channel_3;
*(_QWORD *)(a1 + 24) = led_control_hammerhead_enable_cb;
*(_QWORD *)(a1 + 32) = led_control_hammerhead_blink_cb;
*(_QWORD *)(a1 + 40) = led_control_hammerhead_value_cb;
*(_QWORD *)(a1 + 48) = led_control_hammerhead_close_cb;
*(_BYTE *)(a1 + 16) = 0;
if ( !*(_BYTE *)(a1 + 17) || (v1 = led_control_hammerhead_dynamic_probe(channel_3), !(_BYTE)v1) )
{
v1 = led_control_hammerhead_static_probe(channel_3);
if ( !(_BYTE)v1 )
led_control_close(a1);
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1)
{
unsigned int v1; // ebp

func0(channel_3);
func0(dword_3F0);
func0(dword_400);
*(_QWORD *)a1 = "hammerhead";
*(_QWORD *)(a1 + 8) = channel_3;
*(_QWORD *)(a1 + 24) = led_control_hammerhead_enable_cb;
*(_QWORD *)(a1 + 32) = led_control_hammerhead_blink_cb;
*(_QWORD *)(a1 + 40) = led_control_hammerhead_value_cb;
*(_QWORD *)(a1 + 48) = led_control_hammerhead_close_cb;
*(_BYTE *)(a1 + 16) = 0;
if ( !*(_BYTE *)(a1 + 17) || (v1 = func10(channel_3), !(_BYTE)v1) )
{
v1 = func11(channel_3);
if ( !(_BYTE)v1 )
led_control_close(a1);
}
return v1;
}

original_code:
_Bool
led_control_hammerhead_probe(led_control_t *self)
{
  static led_channel_hammerhead_t channel[3];
  _Bool ack = 0;
  led_channel_hammerhead_init(channel+0);
  led_channel_hammerhead_init(channel+1);
  led_channel_hammerhead_init(channel+2);
  self->name = "hammerhead";
  self->data = channel;
  self->enable = led_control_hammerhead_enable_cb;
  self->blink = led_control_hammerhead_blink_cb;
  self->value = led_control_hammerhead_value_cb;
  self->close = led_control_hammerhead_close_cb;
  self->can_breathe = 0;
  if( self->use_config )
    ack = led_control_hammerhead_dynamic_probe(channel);
  if( !ack )
    ack = led_control_hammerhead_static_probe(channel);
  if( !ack )
    led_control_close(self);
  return ack;
}

canonical_original_code:
_Bool led_control_hammerhead_probe(struct led_control_t *self)
{
  static struct led_channel_hammerhead_t channel[3];
  _Bool ack = 0;
  led_channel_hammerhead_init(channel+0);
  led_channel_hammerhead_init(channel+1);
  led_channel_hammerhead_init(channel+2);
  self->name = "hammerhead";
  self->data = channel;
  self->enable = led_control_hammerhead_enable_cb;
  self->blink = led_control_hammerhead_blink_cb;
  self->value = led_control_hammerhead_value_cb;
  self->close = led_control_hammerhead_close_cb;
  self->can_breathe = 0;
  if( self->use_config )
    ack = led_control_hammerhead_dynamic_probe(channel);
  if( !ack )
    ack = led_control_hammerhead_static_probe(channel);
  if( !ack )
    led_control_close(self);
  return ack;
}

variable_types: struct led_control_t { char *name; void *data; _Bool can_breathe; _Bool use_config; enum led_ramp_t { LED_RAMP_DISABLED=0, LED_RAMP_HALF_SINE=1, LED_RAMP_HARD_STEP=2 } breath_type; void (*enable)(void *data, _Bool enable); void (*blink)(void *data, int on_ms, int off_ms); void (*value)(void *data, int r, int g, int b); void (*close)(void *data); } *

variable_types: struct led_channel_hammerhead_t { int cached_max_brightness; int fd_brightness; int fd_on_off_ms; int fd_rgb_start; } [3]

user_defined_types: struct led_control_t { char *name; void *data; _Bool can_breathe; _Bool use_config; enum led_ramp_t breath_type; void (*enable)(void *data, _Bool enable); void (*blink)(void *data, int on_ms, int off_ms); void (*value)(void *data, int r, int g, int b); void (*close)(void *data); }


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

result = a1;
if ( a3 > (int)a1 )
{
result = a2;
if ( a3 <= (int)a2 )
result = (unsigned int)a3;
}
return result;
}

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

result = a1;
if ( a3 > (int)a1 )
{
result = a2;
if ( a3 <= (int)a2 )
result = (unsigned int)a3;
}
return result;
}

original_code:
static inline int
clamp_to_range(int lo, int hi, int val)
{
  return val <= lo ? lo : val <= hi ? val : hi;
}

variable_types: lo int

variable_types: hi int

variable_types: val int

return_type: int


decompiled_code:
__int64 mce_hybris_indicator_init()
{
const char *v0; // r8

if ( done_7 || (done_7 = 1, !(unsigned __int8)sysfs_led_init()) )
{
v0 = "true";
if ( !ack_6 )
v0 = "false";
}
else
{
mce_hybris_indicator_uses_sysfs = 1;
ack_6 = 1;
v0 = "true";
}
mce_hybris_log(7LL, "plugin-api.c", "mce_hybris_indicator_init", "res = %s", v0);
return (unsigned __int8)ack_6;
}

canonical_decompiled_code:
__int64 func1()
{
const char *v0; // r8

if ( done_7 || (done_7 = 1, !(unsigned __int8)sysfs_led_init()) )
{
v0 = "true";
if ( !ack_6 )
v0 = "false";
}
else
{
mce_hybris_indicator_uses_sysfs = 1;
ack_6 = 1;
v0 = "true";
}
mce_hybris_log(7LL, "plugin-api.c", "mce_hybris_indicator_init", "res = %s", v0);
return (unsigned __int8)ack_6;
}

original_code:
_Bool
mce_hybris_indicator_init(void)
{
  static _Bool done = 0;
  static _Bool ack = 0;
  if( done ) {
    goto cleanup;
  }
  done = 1;
  if( sysfs_led_init() ) {
    mce_hybris_indicator_uses_sysfs = 1;
  }
  else {
    goto cleanup;
  }
  ack = 1;
cleanup:
  mce_hybris_log(LL_DEBUG, "plugin-api.c", __FUNCTION__ ,"res = %s", ack ? "true" : "false");
  return ack;
}

canonical_original_code:
_Bool mce_hybris_indicator_init()
{
  static _Bool done = 0;
  static _Bool ack = 0;
  if( done ) {
    goto cleanup;
  }
  done = 1;
  if( sysfs_led_init() ) {
    mce_hybris_indicator_uses_sysfs = 1;
  }
  else {
    goto cleanup;
  }
  ack = 1;
cleanup:
  mce_hybris_log(LL_DEBUG, "plugin-api.c", __FUNCTION__ ,"res = %s", ack ? "true" : "false");
  return ack;
}


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

if ( mce_hybris_indicator_uses_sysfs )
result = sysfs_led_quit();
return result;
}

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

if ( mce_hybris_indicator_uses_sysfs )
result = sysfs_led_quit();
return result;
}

original_code:
void
mce_hybris_indicator_quit(void)
{
  if( mce_hybris_indicator_uses_sysfs ) {
    sysfs_led_quit();
  }
}

canonical_original_code:
void mce_hybris_indicator_quit()
{
  if( mce_hybris_indicator_uses_sysfs ) {
    sysfs_led_quit();
  }
}


decompiled_code:
__int64 __fastcall mce_hybris_indicator_set_pattern(signed int a1, signed int a2, signed int a3, signed int a4, signed int a5)
{
__int64 v7; // rbx
int v8; // eax
__int64 v9; // rbp
unsigned int v10; // er12
unsigned int v11; // er13
__int64 v12; // r14
unsigned int v13; // er15
const char *v14; // rax

v7 = (unsigned int)clamp_to_range(0, 0xEA60u, a4);
v8 = clamp_to_range(0, 0xEA60u, a5);
if ( (int)v7 <= 49 || (v9 = (unsigned int)v8, v8 <= 49) )
{
v9 = 0LL;
v7 = 0LL;
}
v10 = clamp_to_range(0, 0xFFu, a1);
v11 = clamp_to_range(0, 0xFFu, a2);
v12 = (unsigned int)clamp_to_range(0, 0xFFu, a3);
v13 = (unsigned __int8)mce_hybris_indicator_uses_sysfs;
v14 = "failure";
if ( mce_hybris_indicator_uses_sysfs )
{
v13 = sysfs_led_set_pattern(v10, v11, (unsigned int)v12, (unsigned int)v7, (unsigned int)v9);
v14 = "success";
if ( !(_BYTE)v13 )
v14 = "failure";
}
mce_hybris_log(
7LL,
"plugin-api.c",
"mce_hybris_indicator_set_pattern",
"pattern(%d,%d,%d,%d,%d) -> %s",
v10,
v11,
v12,
v7,
v9,
v14);
return v13;
}

canonical_decompiled_code:
__int64 __fastcall func3(signed int a1, signed int a2, signed int a3, signed int a4, signed int a5)
{
__int64 v7; // rbx
int v8; // eax
__int64 v9; // rbp
unsigned int v10; // er12
unsigned int v11; // er13
__int64 v12; // r14
unsigned int v13; // er15
const char *v14; // rax

v7 = (unsigned int)func0(0, 0xEA60u, a4);
v8 = func0(0, 0xEA60u, a5);
if ( (int)v7 <= 49 || (v9 = (unsigned int)v8, v8 <= 49) )
{
v9 = 0LL;
v7 = 0LL;
}
v10 = func0(0, 0xFFu, a1);
v11 = func0(0, 0xFFu, a2);
v12 = (unsigned int)func0(0, 0xFFu, a3);
v13 = (unsigned __int8)mce_hybris_indicator_uses_sysfs;
v14 = "failure";
if ( mce_hybris_indicator_uses_sysfs )
{
v13 = sysfs_led_set_pattern(v10, v11, (unsigned int)v12, (unsigned int)v7, (unsigned int)v9);
v14 = "success";
if ( !(_BYTE)v13 )
v14 = "failure";
}
mce_hybris_log(
7LL,
"plugin-api.c",
"mce_hybris_indicator_set_pattern",
"pattern(%d,%d,%d,%d,%d) -> %s",
v10,
v11,
v12,
v7,
v9,
v14);
return v13;
}

original_code:
_Bool
mce_hybris_indicator_set_pattern(int r, int g, int b, int ms_on, int ms_off)
{
  _Bool ack = 0;
  ms_on = clamp_to_range(0, 60000, ms_on);
  ms_off = clamp_to_range(0, 60000, ms_off);
  if( ms_on < 50 || ms_off < 50 ) {
    ms_on = ms_off = 0;
  }
  r = clamp_to_range(0, 255, r);
  g = clamp_to_range(0, 255, g);
  b = clamp_to_range(0, 255, b);
  if( mce_hybris_indicator_uses_sysfs ) {
    ack = sysfs_led_set_pattern(r, g, b, ms_on, ms_off);
  }
  mce_hybris_log(LL_DEBUG, "plugin-api.c", __FUNCTION__ ,"pattern(%d,%d,%d,%d,%d) -> %s", r,g,b, ms_on, ms_off , ack ? "success" : "failure");
  return ack;
}


decompiled_code:
__int64 mce_hybris_indicator_can_breathe()
{
unsigned int v0; // ebx
unsigned int v1; // eax
const char *v2; // r8
unsigned int v3; // eax

v0 = (unsigned __int8)mce_hybris_indicator_uses_sysfs;
if ( mce_hybris_indicator_uses_sysfs )
{
v3 = sysfs_led_can_breathe();
v0 = v3;
if ( !logged_3 )
{
logged_3 = 1;
v2 = "true";
if ( !(_BYTE)v3 )
v2 = "false";
goto LABEL_7;
}
}
else
{
v1 = (unsigned __int8)logged_3;
if ( !logged_3 )
{
logged_3 = 1;
v0 = v1;
v2 = "false";
LABEL_7:
mce_hybris_log(7LL, "plugin-api.c", "mce_hybris_indicator_can_breathe", "res = %s", v2);
return v0;
}
}
return v0;
}

canonical_decompiled_code:
__int64 func4()
{
unsigned int v0; // ebx
unsigned int v1; // eax
const char *v2; // r8
unsigned int v3; // eax

v0 = (unsigned __int8)mce_hybris_indicator_uses_sysfs;
if ( mce_hybris_indicator_uses_sysfs )
{
v3 = sysfs_led_can_breathe();
v0 = v3;
if ( !logged_3 )
{
logged_3 = 1;
v2 = "true";
if ( !(_BYTE)v3 )
v2 = "false";
goto LABEL_7;
}
}
else
{
v1 = (unsigned __int8)logged_3;
if ( !logged_3 )
{
logged_3 = 1;
v0 = v1;
v2 = "false";
LABEL_7:
mce_hybris_log(7LL, "plugin-api.c", "mce_hybris_indicator_can_breathe", "res = %s", v2);
return v0;
}
}
return v0;
}

original_code:
_Bool
mce_hybris_indicator_can_breathe(void)
{
  _Bool ack = 0;
  if( mce_hybris_indicator_uses_sysfs )
  {
    ack = sysfs_led_can_breathe();
  }
  static _Bool logged = 0;
  if( !logged )
  {
    logged = 1;
    mce_hybris_log(LL_DEBUG, "plugin-api.c", __FUNCTION__ ,"res = %s", ack ? "true" : "false");
  }
  return ack;
}

canonical_original_code:
_Bool mce_hybris_indicator_can_breathe()
{
  _Bool ack = 0;
  if( mce_hybris_indicator_uses_sysfs )
  {
    ack = sysfs_led_can_breathe();
  }
  static _Bool logged = 0;
  if( !logged )
  {
    logged = 1;
    mce_hybris_log(LL_DEBUG, "plugin-api.c", __FUNCTION__ ,"res = %s", ack ? "true" : "false");
  }
  return ack;
}


decompiled_code:
__int64 __fastcall mce_hybris_indicator_enable_breathing(unsigned __int8 a1)
{
const char *v1; // r8
__int64 result; // rax

v1 = "true";
if ( !a1 )
v1 = "false";
result = mce_hybris_log(7LL, "plugin-api.c", "mce_hybris_indicator_enable_breathing", "enable = %s", v1);
if ( mce_hybris_indicator_uses_sysfs )
result = sysfs_led_set_breathing(a1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned __int8 a1)
{
const char *v1; // r8
__int64 result; // rax

v1 = "true";
if ( !a1 )
v1 = "false";
result = mce_hybris_log(7LL, "plugin-api.c", "mce_hybris_indicator_enable_breathing", "enable = %s", v1);
if ( mce_hybris_indicator_uses_sysfs )
result = sysfs_led_set_breathing(a1);
return result;
}

original_code:
void
mce_hybris_indicator_enable_breathing(_Bool enable)
{
  mce_hybris_log(LL_DEBUG, "plugin-api.c", __FUNCTION__ ,"enable = %s", enable ? "true" : "false");
  if( mce_hybris_indicator_uses_sysfs ) {
    sysfs_led_set_breathing(enable);
  }
}


decompiled_code:
__int64 __fastcall mce_hybris_indicator_set_brightness(unsigned int a1)
{
unsigned int v2; // eax

mce_hybris_log(7LL, "plugin-api.c", "mce_hybris_indicator_set_brightness", "level = %d", a1);
if ( mce_hybris_indicator_uses_sysfs )
{
v2 = clamp_to_range(1u, 0xFFu, a1);
sysfs_led_set_brightness(v2);
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned int a1)
{
unsigned int v2; // eax

mce_hybris_log(7LL, "plugin-api.c", "mce_hybris_indicator_set_brightness", "level = %d", a1);
if ( mce_hybris_indicator_uses_sysfs )
{
v2 = func0(1u, 0xFFu, a1);
sysfs_led_set_brightness(v2);
}
return 1LL;
}

original_code:
_Bool
mce_hybris_indicator_set_brightness(int level)
{
  mce_hybris_log(LL_DEBUG, "plugin-api.c", __FUNCTION__ ,"level = %d", level);
  if( mce_hybris_indicator_uses_sysfs ) {
    level = clamp_to_range(1, 255, level);
    sysfs_led_set_brightness(level);
  }
  return 1;
}

variable_types: level int


decompiled_code:
void mce_hybris_quit()
{
;
}

canonical_decompiled_code:
void func7()
{
;
}

original_code:
void
mce_hybris_quit(void)
{
}

canonical_original_code:
void mce_hybris_quit()
{
}


decompiled_code:
__int64 __fastcall led_util_read_number(const char *a1)
{
int v1; // eax
int v2; // ebx
int v3; // eax
unsigned int v4; // ebp
char v6[88]; // [rsp+0h] [rbp-58h] BYREF

v1 = open64(a1, 0);
v2 = v1;
if ( v1 == -1 )
return (unsigned int)-1;
v3 = read(v1, v6, 0x3FuLL);
if ( v3 < 0 )
{
v4 = -1;
}
else
{
v6[v3] = 0;
v4 = strtol(v6, 0LL, 0);
}
close(v2);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
int v1; // eax
int v2; // ebx
int v3; // eax
unsigned int v4; // ebp
char v6[88]; // [rsp+0h] [rbp-58h] BYREF

v1 = open64(a1, 0);
v2 = v1;
if ( v1 == -1 )
return (unsigned int)-1;
v3 = read(v1, v6, 0x3FuLL);
if ( v3 < 0 )
{
v4 = -1;
}
else
{
v6[v3] = 0;
v4 = strtol(v6, 0LL, 0);
}
close(v2);
return v4;
}

original_code:
int
led_util_read_number(const char *path)
{
  int res = -1;
  int fd = -1;
  char tmp[64];
  if( (fd = open(path, 00)) == -1 ) {
    goto cleanup;
  }
  int rc = read(fd, tmp, sizeof tmp - 1);
  if( rc < 0 ) {
    goto cleanup;
  }
  tmp[rc] = 0;
  res = strtol(tmp, 0, 0);
cleanup:
  if( fd != -1 ) close(fd);
  return res;
}

canonical_original_code:
int led_util_read_number(char *path)
{
  int res = -1;
  int fd = -1;
  char tmp[64];
  if( (fd = open(path, 00)) == -1 ) {
    goto cleanup;
  }
  int rc = read(fd, tmp, sizeof tmp - 1);
  if( rc < 0 ) {
    goto cleanup;
  }
  tmp[rc] = 0;
  res = strtol(tmp, 0, 0);
cleanup:
  if( fd != -1 ) close(fd);
  return res;
}

variable_types:
path
char *

variable_types: res int

variable_types: fd int

variable_types:
tmp
char [64]

variable_types: rc int


decompiled_code:
int __fastcall led_util_close_file(int *a1)
{
int v2; // edi
int result; // eax

if ( a1 )
{
v2 = *a1;
if ( v2 != -1 )
{
result = close(v2);
*a1 = -1;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(int *a1)
{
int v2; // edi
int result; // eax

if ( a1 )
{
v2 = *a1;
if ( v2 != -1 )
{
result = close(v2);
*a1 = -1;
}
}
return result;
}

original_code:
void
led_util_close_file(int *fd_ptr)
{
  if( fd_ptr && *fd_ptr != -1 )
  {
    close(*fd_ptr), *fd_ptr = -1;
  }
}


decompiled_code:
__int64 __fastcall led_util_open_file(int *a1, const char *a2)
{
unsigned int v2; // ebp
int v4; // eax

led_util_close_file(a1);
LOBYTE(v2) = a1 != 0LL && a2 != 0LL;
if ( (_BYTE)v2 )
{
v4 = open64(a2, 1025);
*a1 = v4;
if ( v4 == -1 )
{
v2 = 0;
if ( *_errno_location() != 2 )
mce_hybris_log(4LL, "sysfs-led-util.c", "led_util_open_file", "%s: %s: %m", a2, "open");
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func2(int *a1, const char *a2)
{
unsigned int v2; // ebp
int v4; // eax

func1(a1);
LOBYTE(v2) = a1 != 0LL && a2 != 0LL;
if ( (_BYTE)v2 )
{
v4 = open64(a2, 1025);
*a1 = v4;
if ( v4 == -1 )
{
v2 = 0;
if ( *_errno_location() != 2 )
mce_hybris_log(4LL, "sysfs-led-util.c", "led_util_open_file", "%s: %s: %m", a2, "open");
}
}
return v2;
}

original_code:
_Bool
led_util_open_file(int *fd_ptr, const char *path)
{
  _Bool res = 0;
  led_util_close_file(fd_ptr);
  if( fd_ptr && path )
  {
    if( (*fd_ptr = open(path, 01|02000)) != -1 )
    {
      res = 1;
    }
    else if( (*__errno_location ()) != 2 )
    {
      mce_hybris_log(LL_WARN, "sysfs-led-util.c", __FUNCTION__ ,"%s: %s: %m", path, "open");
    }
  }
  return res;
}

canonical_original_code:
_Bool led_util_open_file(int *fd_ptr, char *path)
{
  _Bool res = 0;
  led_util_close_file(fd_ptr);
  if( fd_ptr && path )
  {
    if( (*fd_ptr = open(path, 01|02000)) != -1 )
    {
      res = 1;
    }
    else if( (*__errno_location ()) != 2 )
    {
      mce_hybris_log(LL_WARN, "sysfs-led-util.c", __FUNCTION__ ,"%s: %s: %m", path, "open");
    }
  }
  return res;
}


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

v2 = a2 * a1;
result = 0LL;
if ( v2 >= -382 )
{
result = (unsigned int)((v2 + 128) / 255);
if ( (int)result > a2 )
result = (unsigned int)a2;
}
return result;
}

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

v2 = a2 * a1;
result = 0LL;
if ( v2 >= -382 )
{
result = (unsigned int)((v2 + 128) / 255);
if ( (int)result > a2 )
result = (unsigned int)a2;
}
return result;
}

original_code:
int
led_util_scale_value(int in, int max)
{
  int out = (in * max + 128) / 255;
  return (out < 0) ? 0 : (out < max) ? out : max;
}

variable_types: in int

variable_types: max int

variable_types: out int


decompiled_code:
__int64 __fastcall led_util_gcd(int a1, int a2)
{
signed int v2; // eax
int v3; // ecx
unsigned int v4; // esi
int v6; // edx

v2 = abs32(a1);
v3 = abs32(a2);
if ( v2 < v3 )
{
if ( !a1 )
return (unsigned int)v3;
v6 = v2;
v2 = v3;
v3 = v6;
}
else if ( !a2 )
{
v4 = v2;
goto LABEL_4;
}
do
{
v4 = v3;
v3 = v2 % v3;
v2 = v4;
}
while ( v3 );
LABEL_4:
if ( !v4 )
v4 = 1;
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1, int a2)
{
signed int v2; // eax
int v3; // ecx
unsigned int v4; // esi
int v6; // edx

v2 = abs32(a1);
v3 = abs32(a2);
if ( v2 < v3 )
{
if ( !a1 )
return (unsigned int)v3;
v6 = v2;
v2 = v3;
v3 = v6;
}
else if ( !a2 )
{
v4 = v2;
goto LABEL_4;
}
do
{
v4 = v3;
v3 = v2 % v3;
v2 = v4;
}
while ( v3 );
LABEL_4:
if ( !v4 )
v4 = 1;
return v4;
}

original_code:
int
led_util_gcd(int a, int b)
{
  int t;
  if( a < 0 ) a = -a;
  if( b < 0 ) b = -b;
  if( a < b ) t = a, a = b, b = t;
  while( b ) t = a % b, a = b, b = t;
  return a ?: 1;
}

variable_types: a int

variable_types: b int

variable_types: t int


decompiled_code:
__int64 __fastcall led_util_roundup(int a1, int a2)
{
int v2; // edx
int v3; // esi

v2 = a1 % a2;
v3 = a2 - a1 % a2;
if ( !v2 )
v3 = 0;
return (unsigned int)(v3 + a1);
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1, int a2)
{
int v2; // edx
int v3; // esi

v2 = a1 % a2;
v3 = a2 - a1 % a2;
if ( !v2 )
v3 = 0;
return (unsigned int)(v3 + a1);
}

original_code:
int
led_util_roundup(int val, int range)
{
  int extra = val % range;
  if( extra ) extra = range - extra;
  return val + extra;
}

variable_types: range int

variable_types: extra int


decompiled_code:
void __fastcall mce_hybris_set_log_hook(__int64 a1)
{
mce_hybris_log_cb = a1;
}

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

original_code:
void
mce_hybris_set_log_hook(mce_hybris_log_fn cb)
{
  mce_hybris_log_cb = cb;
}

canonical_original_code:
void mce_hybris_set_log_hook(void (*cb)(int lev, char *file, char *func, char *text))
{
  mce_hybris_log_cb = cb;
}

variable_types: void (*)(int lev, char *file, char *func, char *text)


decompiled_code:
void mce_hybris_log(unsigned int a1, const char *a2, const char *a3, const char *a4, ...)
{
gcc_va_list va; // [rsp+0h] [rbp-E8h] BYREF
char *ptr; // [rsp+18h] [rbp-D0h] BYREF

va_start(va, a4);
ptr = 0LL;
if ( vasprintf(&ptr, a4, va) < 0 )
ptr = 0LL;
if ( ptr )
{
if ( mce_hybris_log_cb )
mce_hybris_log_cb(a1, a2, a3);
else
fprintf(stderr, "%s: %s: %s\n", a2, a3, ptr);
free(ptr);
}
}

canonical_decompiled_code:
void func1(unsigned int a1, const char *a2, const char *a3, const char *a4, ...)
{
gcc_va_list va; // [rsp+0h] [rbp-E8h] BYREF
char *ptr; // [rsp+18h] [rbp-D0h] BYREF

va_start(va, a4);
ptr = 0LL;
if ( vasprintf(&ptr, a4, va) < 0 )
ptr = 0LL;
if ( ptr )
{
if ( mce_hybris_log_cb )
mce_hybris_log_cb(a1, a2, a3);
else
fprintf(stderr, "%s: %s: %s\n", a2, a3, ptr);
free(ptr);
}
}

original_code:
void
mce_hybris_log(int lev, const char *file, const char *func,
               const char *fmt, ...)
{
  char *msg = 0;
  va_list va;
  __builtin_va_start(va,fmt);
  if( vasprintf(&msg, fmt, va) < 0 ) msg = 0;
  __builtin_va_end(va);
  if( msg ) {
    if( mce_hybris_log_cb ) {
      mce_hybris_log_cb(lev, file, func, msg);
    }
    else {
      fprintf(stderr, "%s: %s: %s\n", file, func, msg);
    }
    free(msg);
  }
}

canonical_original_code:
void mce_hybris_log(int lev, char *file, char *func, char *fmt, ...)
{
  char *msg = 0;
  __builtin_va_list va;
  __builtin_va_start(va,fmt);
  if( vasprintf(&msg, fmt, va) < 0 ) msg = 0;
  __builtin_va_end(va);
  if( msg ) {
    if( mce_hybris_log_cb ) {
      mce_hybris_log_cb(lev, file, func, msg);
    }
    else {
      fprintf(stderr, "%s: %s: %s\n", file, func, msg);
    }
    free(msg);
  }
}

variable_types: lev int

variable_types:
file
char *

variable_types:
func
char *

variable_types:
fmt
char *

variable_types:
msg
char *


decompiled_code:
void *__fastcall malloc_aligned(size_t len)
{
int v1; // ebx
void *v2; // rbp

v1 = open("/dev/zero", 0);
v2 = mmap(0LL, len, 3, 2, v1, 0LL);
close(v1);
return v2;
}

canonical_decompiled_code:
void *__fastcall func7(size_t len)
{
int v1; // ebx
void *v2; // rbp

v1 = open("/dev/zero", 0);
v2 = mmap(0LL, len, 3, 2, v1, 0LL);
close(v1);
return v2;
}

original_code:
void *malloc_aligned(long size) {
    int f;
    char *buff;
    f=open("/dev/zero",00000000);
    buff=mmap(0,size,0x1|0x2,0x02,f,0);
    close(f);
    return buff;
}

variable_types: size long

variable_types: f int

variable_types:
buff
char *

return_type: void *


decompiled_code:
int __fastcall free_aligned(void *a1, size_t a2)
{
return munmap(a1, a2);
}

canonical_decompiled_code:
int __fastcall func8(void *a1, size_t a2)
{
return munmap(a1, a2);
}

original_code:
int free_aligned(void *addr, long size) {
    munmap(addr,size);
}

variable_types:
addr
void *


decompiled_code:
_BOOL8 __fastcall is_photoframe(int fd)
{
_BYTE *v1; // rbx
int v2; // ebp
char s2[39]; // [rsp+1h] [rbp-27h] BYREF

strcpy(s2, "SITRONIX CORP.");
v1 = malloc_aligned(0x200uLL);
lseek(fd, 0LL, 0);
read(fd, v1, 0x200uLL);
v1[15] = 0;
v2 = strcmp(v1, s2);
free_aligned(v1, 0x200uLL);
return v2 == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func9(int fd)
{
_BYTE *v1; // rbx
int v2; // ebp
char s2[39]; // [rsp+1h] [rbp-27h] BYREF

strcpy(s2, "SITRONIX CORP.");
v1 = func7(0x200uLL);
lseek(fd, 0LL, 0);
read(fd, v1, 0x200uLL);
v1[15] = 0;
v2 = strcmp(v1, s2);
func8(v1, 0x200uLL);
return v2 == 0;
}

original_code:
int is_photoframe(int f) {
    int y,res;
    char id[]="SITRONIX CORP.";
    char *buff;
    buff=malloc_aligned(0x200);
    lseek(f,0x0,0);
    y=read(f,buff,0x200);
    buff[15]=0;
    res=strcmp(buff,id)==0?1:0;
    free_aligned(buff,0x200);
    return res;
}

canonical_original_code:
int is_photoframe(int f) {
    int y; int res;
    char id[] = "SITRONIX CORP.";
    char *buff;
    buff=malloc_aligned(0x200);
    lseek(f,0x0,0);
    y=read(f,buff,0x200);
    buff[15]=0;
    res=strcmp(buff,id)==0?1:0;
    free_aligned(buff,0x200);
    return res;
}

variable_types: y int

variable_types: res int

variable_types:
id
char []


sendcmd

decompiled_code:
ssize_t __fastcall sendcmd(int fd, char a2, int a3, int a4, char a5)
{
_BYTE *v8; // rbx

v8 = malloc_aligned(0x200uLL);
*v8 = a2;
v8[1] = HIBYTE(a3);
v8[2] = BYTE2(a3);
v8[3] = BYTE1(a3);
v8[4] = a3;
v8[5] = HIBYTE(a4);
v8[6] = BYTE2(a4);
v8[7] = BYTE1(a4);
v8[8] = a4;
v8[9] = a5;
lseek(fd, 25088LL, 0);
return write(fd, v8, 0x200uLL);
}

original_code:
int sendcmd(int f,int cmd, unsigned int arg1, unsigned int arg2, unsigned char arg3) {
    unsigned char *buff;
    buff=malloc_aligned(0x200);
    buff[0]=cmd;
    buff[1]=(arg1>>24)&0xff;
    buff[2]=(arg1>>16)&0xff;
    buff[3]=(arg1>>8)&0xff;
    buff[4]=(arg1>>0)&0xff;
    buff[5]=(arg2>>24)&0xff;
    buff[6]=(arg2>>16)&0xff;
    buff[7]=(arg2>>8)&0xff;
    buff[8]=(arg2>>0)&0xff;
    buff[9]=(arg3);
    lseek(f,0x6200,0);
    return write(f,buff,0x200);
}

canonical_original_code:
int sendcmd(int f, int cmd, unsigned int arg1, unsigned int arg2, unsigned char arg3) {
    unsigned char *buff;
    buff=malloc_aligned(0x200);
    buff[0]=cmd;
    buff[1]=(arg1>>24)&0xff;
    buff[2]=(arg1>>16)&0xff;
    buff[3]=(arg1>>8)&0xff;
    buff[4]=(arg1>>0)&0xff;
    buff[5]=(arg2>>24)&0xff;
    buff[6]=(arg2>>16)&0xff;
    buff[7]=(arg2>>8)&0xff;
    buff[8]=(arg2>>0)&0xff;
    buff[9]=(arg3);
    lseek(f,0x6200,0);
    return write(f,buff,0x200);
}

variable_types: cmd int

variable_types:
buff
unsigned char *


tst

original_code:
int tst(int f) {
    unsigned char *buff;
    buff=malloc_aligned(0x200);
    buff[0]=3;
    buff[1]=0;
    buff[2]=0;
    buff[3]=0;
    buff[4]=0x04;
    buff[5]=0;
    buff[6]=0;
    buff[7]=0;
    lseek(f,0x4400,0);
    return write(f,buff,0x200);
}


decompiled_code:
ssize_t __fastcall read_data(int fd, void *buf, int a3)
{
lseek(fd, 45056LL, 0);
return read(fd, buf, a3);
}

canonical_decompiled_code:
ssize_t __fastcall func12(int fd, void *buf, int a3)
{
lseek(fd, 45056LL, 0);
return read(fd, buf, a3);
}

original_code:
int read_data(int f, char* buff, int len) {
    lseek(f,0xb000,0);
    return read(f,buff,len);
}

canonical_original_code:
int read_data(int f, char *buff, int len) {
    lseek(f,0xb000,0);
    return read(f,buff,len);
}

variable_types: len int


decompiled_code:
ssize_t __fastcall write_data(int fd, void *buf, int a3)
{
lseek(fd, 26112LL, 0);
return write(fd, buf, a3);
}

canonical_decompiled_code:
ssize_t __fastcall func13(int fd, void *buf, int a3)
{
lseek(fd, 26112LL, 0);
return write(fd, buf, a3);
}

original_code:
int write_data(int f,char* buff, int len) {
    lseek(f,0x6600,0);
    return write(f,buff,len);
}

canonical_original_code:
int write_data(int f, char *buff, int len) {
    lseek(f,0x6600,0);
    return write(f,buff,len);
}


dumpmem

decompiled_code:
int __fastcall dumpmem(unsigned __int8 *a1, int a2)
{
unsigned __int8 *v2; // r15
unsigned __int8 *v3; // rbp
unsigned int v4; // er14
unsigned __int8 *v5; // rbx
unsigned __int8 *v6; // rbx
int result; // eax
unsigned int v8; // er12

if ( a2 > 0 )
{
v2 = a1;
v3 = a1 + 16;
v4 = 0;
do
{
printf("%04x: ", v4);
v5 = v2;
v8 = v4 - (_DWORD)v2;
do
{
if ( a2 >= (int)(v8 + (_DWORD)v5) )
printf("%02hhx ", *v5);
else
printf(" ");
++v5;
}
while ( v5 != v3 );
printf("- ");
v6 = v2;
do
{
if ( a2 >= (int)(v8 + (_DWORD)v6) )
{
if ( (unsigned __int8)(*v6 - 32) <= 0x5Fu )
putchar(*v6);
else
putchar(46);
}
++v6;
}
while ( v6 != v3 );
result = putchar(10);
v4 += 16;
v3 += 16;
v2 += 16;
}
while ( v4 != ((a2 - 1) & 0xFFFFFFF0) + 16 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func14(unsigned __int8 *a1, int a2)
{
unsigned __int8 *v2; // r15
unsigned __int8 *v3; // rbp
unsigned int v4; // er14
unsigned __int8 *v5; // rbx
unsigned __int8 *v6; // rbx
int result; // eax
unsigned int v8; // er12

if ( a2 > 0 )
{
v2 = a1;
v3 = a1 + 16;
v4 = 0;
do
{
printf("%04x: ", v4);
v5 = v2;
v8 = v4 - (_DWORD)v2;
do
{
if ( a2 >= (int)(v8 + (_DWORD)v5) )
printf("%02hhx ", *v5);
else
printf(" ");
++v5;
}
while ( v5 != v3 );
printf("- ");
v6 = v2;
do
{
if ( a2 >= (int)(v8 + (_DWORD)v6) )
{
if ( (unsigned __int8)(*v6 - 32) <= 0x5Fu )
putchar(*v6);
else
putchar(46);
}
++v6;
}
while ( v6 != v3 );
result = putchar(10);
v4 += 16;
v3 += 16;
v2 += 16;
}
while ( v4 != ((a2 - 1) & 0xFFFFFFF0) + 16 );
}
return result;
}

original_code:
void dumpmem(unsigned char* mem, int len) {
    int x,y;
    for (x=0; x<len; x+=16) {
 printf("%04x: ",x);
 for (y=0; y<16; y++) {
     if ((x+y)>len) {
  printf("   ");
     } else {
  printf("%02hhx ",mem[x+y]);
     }
 }
 printf("- ");
 for (y=0; y<16; y++) {
     if ((x+y)<=len) {
  if (mem[x+y]<32 || mem[x+y]>127) {
      printf(".");
  } else {
      printf("%c",mem[x+y]);
  }
     }
 }
 printf("\n");
    }
}

canonical_original_code:
void dumpmem(unsigned char *mem, int len) {
    int x; int y;
    for (x=0; x<len; x+=16) {
 printf("%04x: ",x);
 for (y=0; y<16; y++) {
     if ((x+y)>len) {
  printf("   ");
     } else {
  printf("%02hhx ",mem[x+y]);
     }
 }
 printf("- ");
 for (y=0; y<16; y++) {
     if ((x+y)<=len) {
  if (mem[x+y]<32 || mem[x+y]>127) {
      printf(".");
  } else {
      printf("%c",mem[x+y]);
  }
     }
 }
 printf("\n");
    }
}

variable_types:
mem
unsigned char *

variable_types: x int


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // er14
const char *v6; // rdx
_BOOL8 v7; // r8
int v8; // ebx
bool v9; // cl
bool v10; // cf
bool v11; // zf
const char *v12; // rdi
__int64 v13; // rcx
const char *v14; // rsi
int v15; // ebp
unsigned __int8 *v16; // r12
int i; // ebx
int j; // ebx
int v19; // ebx
int v20; // er13
int v21; // ebx
int v22; // er13

if ( argc <= 1 )
{
printf("Usage:\n%s [-d|-u|-df|-uf|-m] file [device]\n", *argv);
puts(" -d: dump mem");
puts(" -u: upload mem");
puts(" -df: dump firmware");
puts(" -uf: upload firmware");
puts(" -m: set message (10 chars)");
puts(" file: file to dump to or upload from");
puts(" device: /dev/sdX (default: /dev/sda)");
exit(0);
}
v6 = argv[1];
v7 = strcmp(v6, "-u") != 0;
v8 = 2;
if ( strcmp(v6, "-d") )
v8 = !v7;
if ( !strcmp(v6, "-uf") )
v8 = 3;
v9 = strcmp(v6, "-df") != 0;
v10 = 0;
v11 = !v9;
if ( !v9 )
v8 = 4;
v12 = "-m";
v13 = 3LL;
v14 = v6;
do
{
if ( !v13 )
break;
v10 = *v14 < (unsigned int)*v12;
v11 = *v14++ == *v12++;
--v13;
}
while ( v11 );
LOBYTE(v13) = (!v10 && !v11) - v10;
if ( (_BYTE)v13 )
{
if ( !v8 )
{
printf("Invalid command: %s\n", v6);
exit(1);
}
}
else
{
v8 = 5;
}
if ( argc <= 3 )
v15 = open("/dev/sda", 1069058, v6, v13, v7);
else
v15 = open(argv[3], 1069058, v6, v13, v7);
if ( !is_photoframe(v15) )
{
fwrite("No photoframe found there.\n", 1uLL, 0x1BuLL, stderr);
exit(1);
}
if ( ((v8 - 2) & 0xFFFFFFFD) != 0 )
{
if ( (v8 & 0xFFFFFFFD) == 1 )
v3 = open(argv[2], 0);
}
else
{
v3 = open(argv[2], 577, 420LL);
}
if ( v3 < 0 && v8 != 5 )
{
fprintf(stderr, "Error opening %s.\n", argv[2]);
exit(1);
}
v16 = (unsigned __int8 *)malloc_aligned(0x10000uLL);
sendcmd(v15, 1, 0, 0, 0);
read_data(v15, v16, 512);
if ( *v16 != 8 )
{
printf("Expected response 8 on cmd 1, got 0x%hhx!\n", *v16);
exit(1);
}
if ( v8 == 2 )
{
for ( i = 0; i != 62; ++i )
{
sendcmd(v15, 4, i, 0x8000, 0);
read_data(v15, v16, 0x8000);
write(v3, v16, 0x8000uLL);
fputc(46, stderr);
}
fputc(10, stderr);
puts("Memory dumped.");
}
else if ( ((v8 - 2) & 0xFFFFFFFD) != 0 )
{
switch ( v8 )
{
case 1:
v19 = 0;
while ( 1 )
{
sendcmd(v15, 3, v19, 0x8000, 0);
v20 = read(v3, v16, 0x8000uLL);
write_data(v15, v16, 0x8000);
sendcmd(v15, 2, v19, 0x8000, 0);
read_data(v15, v16, 512);
if ( v20 != 0x8000 )
break;
fputc(46, stderr);
if ( (unsigned int)++v19 > 0x3D )
goto LABEL_47;
}
puts("Premature file end.");
fputc(46, stderr);
LABEL_47:
fputc(10, stderr);
puts("Memory uploaded.");
break;
case 3:
puts("Firmware update! If unsure, press ctrl-C NOW!");
sleep(3u);
puts("Too late. Commencing firmware update...");
v21 = 0;
while ( 1 )
{
sendcmd(v15, 3, v21 | 0x80000000, 0x8000, 0);
v22 = read(v3, v16, 0x8000uLL);
write_data(v15, v16, 0x8000);
sendcmd(v15, 2, v21, 0x8000, 0);
read_data(v15, v16, 512);
sendcmd(v15, 3, v21 | 0x1F40, 0x8000, 0);
write_data(v15, v16, 0x8000);
if ( v22 != 0x8000 )
break;
fputc(46, stderr);
if ( (unsigned int)++v21 > 1 )
goto LABEL_52;
}
puts("Premature file end. Hope everything still works OK.");
fputc(46, stderr);
LABEL_52:
fputc(10, stderr);
puts("Firmware upgraded. Un- and replug USB connection to restart device.");
break;
case 5:
sendcmd(v15, 9, 0, 0, 0);
strcpy((char *)v16, argv[2]);
write_data(v15, v16, 512);
puts("Message written.");
break;
}
}
else
{
for ( j = 62; j != 64; ++j )
{
sendcmd(v15, 4, j, 0x8000, 0);
read_data(v15, v16, 0x8000);
write(v3, v16, 0x8000uLL);
fputc(46, stderr);
}
fputc(10, stderr);
puts("Firmware dumped.");
}
exit(0);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // er14
const char *v6; // rdx
_BOOL8 v7; // r8
int v8; // ebx
bool v9; // cl
bool v10; // cf
bool v11; // zf
const char *v12; // rdi
__int64 v13; // rcx
const char *v14; // rsi
int v15; // ebp
unsigned __int8 *v16; // r12
int i; // ebx
int j; // ebx
int v19; // ebx
int v20; // er13
int v21; // ebx
int v22; // er13

if ( argc <= 1 )
{
printf("Usage:\n%s [-d|-u|-df|-uf|-m] file [device]\n", *argv);
puts(" -d: dump mem");
puts(" -u: upload mem");
puts(" -df: dump firmware");
puts(" -uf: upload firmware");
puts(" -m: set message (10 chars)");
puts(" file: file to dump to or upload from");
puts(" device: /dev/sdX (default: /dev/sda)");
exit(0);
}
v6 = argv[1];
v7 = strcmp(v6, "-u") != 0;
v8 = 2;
if ( strcmp(v6, "-d") )
v8 = !v7;
if ( !strcmp(v6, "-uf") )
v8 = 3;
v9 = strcmp(v6, "-df") != 0;
v10 = 0;
v11 = !v9;
if ( !v9 )
v8 = 4;
v12 = "-m";
v13 = 3LL;
v14 = v6;
do
{
if ( !v13 )
break;
v10 = *v14 < (unsigned int)*v12;
v11 = *v14++ == *v12++;
--v13;
}
while ( v11 );
LOBYTE(v13) = (!v10 && !v11) - v10;
if ( (_BYTE)v13 )
{
if ( !v8 )
{
printf("Invalid command: %s\n", v6);
exit(1);
}
}
else
{
v8 = 5;
}
if ( argc <= 3 )
v15 = open("/dev/sda", 1069058, v6, v13, v7);
else
v15 = open(argv[3], 1069058, v6, v13, v7);
if ( !func9(v15) )
{
fwrite("No photoframe found there.\n", 1uLL, 0x1BuLL, stderr);
exit(1);
}
if ( ((v8 - 2) & 0xFFFFFFFD) != 0 )
{
if ( (v8 & 0xFFFFFFFD) == 1 )
v3 = open(argv[2], 0);
}
else
{
v3 = open(argv[2], 577, 420LL);
}
if ( v3 < 0 && v8 != 5 )
{
fprintf(stderr, "Error opening %s.\n", argv[2]);
exit(1);
}
v16 = (unsigned __int8 *)func7(0x10000uLL);
func10(v15, 1, 0, 0, 0);
func12(v15, v16, 512);
if ( *v16 != 8 )
{
printf("Expected response 8 on cmd 1, got 0x%hhx!\n", *v16);
exit(1);
}
if ( v8 == 2 )
{
for ( i = 0; i != 62; ++i )
{
func10(v15, 4, i, 0x8000, 0);
func12(v15, v16, 0x8000);
write(v3, v16, 0x8000uLL);
fputc(46, stderr);
}
fputc(10, stderr);
puts("Memory dumped.");
}
else if ( ((v8 - 2) & 0xFFFFFFFD) != 0 )
{
switch ( v8 )
{
case 1:
v19 = 0;
while ( 1 )
{
func10(v15, 3, v19, 0x8000, 0);
v20 = read(v3, v16, 0x8000uLL);
func13(v15, v16, 0x8000);
func10(v15, 2, v19, 0x8000, 0);
func12(v15, v16, 512);
if ( v20 != 0x8000 )
break;
fputc(46, stderr);
if ( (unsigned int)++v19 > 0x3D )
goto LABEL_47;
}
puts("Premature file end.");
fputc(46, stderr);
LABEL_47:
fputc(10, stderr);
puts("Memory uploaded.");
break;
case 3:
puts("Firmware update! If unsure, press ctrl-C NOW!");
sleep(3u);
puts("Too late. Commencing firmware update...");
v21 = 0;
while ( 1 )
{
func10(v15, 3, v21 | 0x80000000, 0x8000, 0);
v22 = read(v3, v16, 0x8000uLL);
func13(v15, v16, 0x8000);
func10(v15, 2, v21, 0x8000, 0);
func12(v15, v16, 512);
func10(v15, 3, v21 | 0x1F40, 0x8000, 0);
func13(v15, v16, 0x8000);
if ( v22 != 0x8000 )
break;
fputc(46, stderr);
if ( (unsigned int)++v21 > 1 )
goto LABEL_52;
}
puts("Premature file end. Hope everything still works OK.");
fputc(46, stderr);
LABEL_52:
fputc(10, stderr);
puts("Firmware upgraded. Un- and replug USB connection to restart device.");
break;
case 5:
func10(v15, 9, 0, 0, 0);
strcpy((char *)v16, argv[2]);
func13(v15, v16, 512);
puts("Message written.");
break;
}
}
else
{
for ( j = 62; j != 64; ++j )
{
func10(v15, 4, j, 0x8000, 0);
func12(v15, v16, 0x8000);
write(v3, v16, 0x8000uLL);
fputc(46, stderr);
}
fputc(10, stderr);
puts("Firmware dumped.");
}
exit(0);
}

original_code:
int main(int argc, char** argv) {
    int f,o;
    unsigned int x,y;
    int mode=0;
    unsigned char *buff;
    if (argc<2) {
 printf("Usage:\n%s [-d|-u|-df|-uf|-m] file [device]\n",argv[0]);
 printf(" -d: dump mem\n");
 printf(" -u: upload mem\n");
 printf(" -df: dump firmware\n");
 printf(" -uf: upload firmware\n");
 printf(" -m: set message (10 chars)\n");
 printf(" file: file to dump to or upload from\n");
 printf(" device: /dev/sdX (default: /dev/sda)\n");
 exit(0);
    }
    if (strcmp(argv[1],"-u")==0) mode=1;
    if (strcmp(argv[1],"-d")==0) mode=2;
    if (strcmp(argv[1],"-uf")==0) mode=3;
    if (strcmp(argv[1],"-df")==0) mode=4;
    if (strcmp(argv[1],"-m")==0) mode=5;
    if (mode==0) {
 printf("Invalid command: %s\n",argv[1]);
 exit(1);
    }
    if (argc>=4) {
 f=open(argv[3],00000002|00040000|(04000000|00010000));
    } else {
 f=open("/dev/sda",00000002|00040000|(04000000|00010000));
    }
    if (!is_photoframe(f)) {
 fprintf(stderr,"No photoframe found there.\n");
 exit(1);
    }
    if (mode==2 || mode==4) {
 o=open(argv[2],00000001|00001000|00000100,0644);
    } else if (mode==1 || mode==3) {
 o=open(argv[2],00000000);
    }
    if (o<0 && mode!=5 && mode!=6) {
 fprintf(stderr,"Error opening %s.\n",argv[2]);
 exit(1);
    }
    buff=malloc_aligned(0x10000);
    sendcmd(f,1,0,0,0);
    read_data(f,buff,0x200);
    if (buff[0]!=8) {
 printf("Expected response 8 on cmd 1, got 0x%hhx!\n",buff[0]);
 exit(1);
    }
    if (mode==2) {
 for (x=0; x<((2048-64)/32); x++) {
     sendcmd(f,4,x,0x8000,0);
     read_data(f,buff,0x8000);
     write(o,buff,0x8000);
     fprintf(stderr,".");
 }
 fprintf(stderr,"\n");
 printf("Memory dumped.\n");
    } else if (mode==4) {
 for (x=((2048-64)/32); x<(2048/32); x++) {
     sendcmd(f,4,x,0x8000,0);
     read_data(f,buff,0x8000);
     write(o,buff,0x8000);
     fprintf(stderr,".");
 }
 fprintf(stderr,"\n");
 printf("Firmware dumped.\n");
    } else if (mode==1) {
 for (x=0; x<((2048-64)/32); x++) {
     sendcmd(f,3,x,0x8000,0);
     y=read(o,buff,0x8000);
     write_data(f,buff,0x8000);
     sendcmd(f,2,x,0x8000,0);
     read_data(f,buff,0x200);
     if (y!=0x8000) {
  printf("Premature file end.\n");
  x=9999;
     }
     fprintf(stderr,".");
 }
 fprintf(stderr,"\n");
 printf("Memory uploaded.\n");
    } else if (mode==3) {
 printf("Firmware update! If unsure, press ctrl-C NOW!\n");
 sleep(3);
 printf("Too late. Commencing firmware update...\n");
 for (x=0; x<2; x++) {
     sendcmd(f,3,x|0x80000000,0x8000,0);
     y=read(o,buff,0x8000);
     write_data(f,buff,0x8000);
     sendcmd(f,2|0x80000000,x,0x8000,0);
     read_data(f,buff,0x200);
     sendcmd(f,3,x|0x1f40,0x8000,0);
     write_data(f,buff,0x8000);
     if (y!=0x8000) {
  printf("Premature file end. Hope everything still works OK.\n");
  x=9999;
     }
     fprintf(stderr,".");
 }
 fprintf(stderr,"\n");
 printf("Firmware upgraded. Un- and replug USB connection to restart device.\n");
    } else if (mode==5) {
 sendcmd(f,9,0,0,0);
 strcpy(buff,argv[2]);
 write_data(f,buff,0x200);
 printf("Message written.\n");
    }
    exit(0);
}

canonical_original_code:
int main(int argc, char **argv) {
    int f; int o;
    unsigned int x; unsigned int y;
    int mode = 0;
    unsigned char *buff;
    if (argc<2) {
 printf("Usage:\n%s [-d|-u|-df|-uf|-m] file [device]\n",argv[0]);
 printf(" -d: dump mem\n");
 printf(" -u: upload mem\n");
 printf(" -df: dump firmware\n");
 printf(" -uf: upload firmware\n");
 printf(" -m: set message (10 chars)\n");
 printf(" file: file to dump to or upload from\n");
 printf(" device: /dev/sdX (default: /dev/sda)\n");
 exit(0);
    }
    if (strcmp(argv[1],"-u")==0) mode=1;
    if (strcmp(argv[1],"-d")==0) mode=2;
    if (strcmp(argv[1],"-uf")==0) mode=3;
    if (strcmp(argv[1],"-df")==0) mode=4;
    if (strcmp(argv[1],"-m")==0) mode=5;
    if (mode==0) {
 printf("Invalid command: %s\n",argv[1]);
 exit(1);
    }
    if (argc>=4) {
 f=open(argv[3],00000002|00040000|(04000000|00010000));
    } else {
 f=open("/dev/sda",00000002|00040000|(04000000|00010000));
    }
    if (!is_photoframe(f)) {
 fprintf(stderr,"No photoframe found there.\n");
 exit(1);
    }
    if (mode==2 || mode==4) {
 o=open(argv[2],00000001|00001000|00000100,0644);
    } else if (mode==1 || mode==3) {
 o=open(argv[2],00000000);
    }
    if (o<0 && mode!=5 && mode!=6) {
 fprintf(stderr,"Error opening %s.\n",argv[2]);
 exit(1);
    }
    buff=malloc_aligned(0x10000);
    sendcmd(f,1,0,0,0);
    read_data(f,buff,0x200);
    if (buff[0]!=8) {
 printf("Expected response 8 on cmd 1, got 0x%hhx!\n",buff[0]);
 exit(1);
    }
    if (mode==2) {
 for (x=0; x<((2048-64)/32); x++) {
     sendcmd(f,4,x,0x8000,0);
     read_data(f,buff,0x8000);
     write(o,buff,0x8000);
     fprintf(stderr,".");
 }
 fprintf(stderr,"\n");
 printf("Memory dumped.\n");
    } else if (mode==4) {
 for (x=((2048-64)/32); x<(2048/32); x++) {
     sendcmd(f,4,x,0x8000,0);
     read_data(f,buff,0x8000);
     write(o,buff,0x8000);
     fprintf(stderr,".");
 }
 fprintf(stderr,"\n");
 printf("Firmware dumped.\n");
    } else if (mode==1) {
 for (x=0; x<((2048-64)/32); x++) {
     sendcmd(f,3,x,0x8000,0);
     y=read(o,buff,0x8000);
     write_data(f,buff,0x8000);
     sendcmd(f,2,x,0x8000,0);
     read_data(f,buff,0x200);
     if (y!=0x8000) {
  printf("Premature file end.\n");
  x=9999;
     }
     fprintf(stderr,".");
 }
 fprintf(stderr,"\n");
 printf("Memory uploaded.\n");
    } else if (mode==3) {
 printf("Firmware update! If unsure, press ctrl-C NOW!\n");
 sleep(3);
 printf("Too late. Commencing firmware update...\n");
 for (x=0; x<2; x++) {
     sendcmd(f,3,x|0x80000000,0x8000,0);
     y=read(o,buff,0x8000);
     write_data(f,buff,0x8000);
     sendcmd(f,2|0x80000000,x,0x8000,0);
     read_data(f,buff,0x200);
     sendcmd(f,3,x|0x1f40,0x8000,0);
     write_data(f,buff,0x8000);
     if (y!=0x8000) {
  printf("Premature file end. Hope everything still works OK.\n");
  x=9999;
     }
     fprintf(stderr,".");
 }
 fprintf(stderr,"\n");
 printf("Firmware upgraded. Un- and replug USB connection to restart device.\n");
    } else if (mode==5) {
 sendcmd(f,9,0,0,0);
 strcpy(buff,argv[2]);
 write_data(f,buff,0x200);
 printf("Message written.\n");
    }
    exit(0);
}

variable_types: argc int

variable_types:
argv
char **

variable_types: o int

variable_types:
x
unsigned int

variable_types:
y
unsigned int

variable_types: mode int


testpic

decompiled_code:
int __fastcall testpic(__int64 a1, const char *a2)
{
char i; // bp
void *v4; // rdi
struct termios v6; // [rsp+0h] [rbp-98h] BYREF
struct termios termios_p; // [rsp+40h] [rbp-58h] BYREF

tcgetattr(0, &v6);
tcgetattr(0, &termios_p);
termios_p.c_lflag &= 0xFFFFFFFD;
tcsetattr(0, 0, &termios_p);
setbuf(stdin, 0LL);
for ( i = 105; i != 113; i = getchar() )
{
v4 = *(void **)(a1 + 32);
if ( v4 )
{
free(v4);
*(_QWORD *)(a1 + 32) = 0LL;
}
switch ( i )
{
case 'i':
puts(" With this mode, you can find some values for a new specfile.");
puts(" u and d moves picture up and down");
puts(" l and r moves picture left and right");
puts(" t toggles bpp");
puts(" i gives this info");
puts(" q quits");
break;
case 'u':
--*(_DWORD *)(a1 + 44);
break;
case 'd':
++*(_DWORD *)(a1 + 44);
break;
case 'l':
--*(_DWORD *)(a1 + 40);
break;
case 'r':
++*(_DWORD *)(a1 + 40);
break;
case 't':
if ( *(_DWORD *)(a1 + 12) == 12 )
*(_DWORD *)(a1 + 12) = 16;
else
*(_DWORD *)(a1 + 12) = 12;
break;
}
printf(
" pressed\nCurrent settings: offx=%i offy=%i bpp=%i\n",
*(unsigned int *)(a1 + 40),
*(unsigned int *)(a1 + 44),
*(unsigned int *)(a1 + 12));
sendpic(a1, a2);
}
printf("K, bye.");
return tcsetattr(0, 0, &v6);
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1, const char *a2)
{
char i; // bp
void *v4; // rdi
struct termios v6; // [rsp+0h] [rbp-98h] BYREF
struct termios termios_p; // [rsp+40h] [rbp-58h] BYREF

tcgetattr(0, &v6);
tcgetattr(0, &termios_p);
termios_p.c_lflag &= 0xFFFFFFFD;
tcsetattr(0, 0, &termios_p);
setbuf(stdin, 0LL);
for ( i = 105; i != 113; i = getchar() )
{
v4 = *(void **)(a1 + 32);
if ( v4 )
{
free(v4);
*(_QWORD *)(a1 + 32) = 0LL;
}
switch ( i )
{
case 'i':
puts(" With this mode, you can find some values for a new specfile.");
puts(" u and d moves picture up and down");
puts(" l and r moves picture left and right");
puts(" t toggles bpp");
puts(" i gives this info");
puts(" q quits");
break;
case 'u':
--*(_DWORD *)(a1 + 44);
break;
case 'd':
++*(_DWORD *)(a1 + 44);
break;
case 'l':
--*(_DWORD *)(a1 + 40);
break;
case 'r':
++*(_DWORD *)(a1 + 40);
break;
case 't':
if ( *(_DWORD *)(a1 + 12) == 12 )
*(_DWORD *)(a1 + 12) = 16;
else
*(_DWORD *)(a1 + 12) = 12;
break;
}
printf(
" pressed\nCurrent settings: offx=%i offy=%i bpp=%i\n",
*(unsigned int *)(a1 + 40),
*(unsigned int *)(a1 + 44),
*(unsigned int *)(a1 + 12));
func0(a1, a2);
}
printf("K, bye.");
return tcsetattr(0, 0, &v6);
}

original_code:
void testpic(st2205_handle *h, char* what) {
    char c;
    struct termios term;
    struct termios oldterm;
    tcgetattr(0, &oldterm);
    tcgetattr(0, &term);
    term.c_lflag &= ~0000002;
    tcsetattr(0, 0, &term);
    setbuf(stdin, ((void *)0));
    c='i';
    while (c!='q') {
 if (h->oldpix!=((void *)0)) {
     free(h->oldpix);
     h->oldpix=((void *)0);
 }
 if (c=='i') {
     printf(" With this mode, you can find some values for a new specfile.\n");
     printf(" u and d moves picture up and down\n");
     printf(" l and r moves picture left and right\n");
     printf(" t toggles bpp\n");
     printf(" i gives this info\n");
     printf(" q quits\n");
 } else if (c=='u') {
     h->offy--;
 } else if (c=='d') {
     h->offy++;
 } else if (c=='l') {
     h->offx--;
 } else if (c=='r') {
     h->offx++;
 } else if (c=='t') {
     if (h->bpp==12) h->bpp=16; else h->bpp=12;
 }
 printf(" pressed\nCurrent settings: offx=%i offy=%i bpp=%i\n",h->offx,h->offy,h->bpp);
 sendpic(h,what);
 c=getchar();
    }
    printf("K, bye.");
    tcsetattr(0, 0, &oldterm);
}

canonical_original_code:
void testpic(struct st2205_handle *h, char *what) {
    char c;
    struct termios term;
    struct termios oldterm;
    tcgetattr(0, &oldterm);
    tcgetattr(0, &term);
    term.c_lflag &= ~0000002;
    tcsetattr(0, 0, &term);
    setbuf(stdin, ((void *)0));
    c='i';
    while (c!='q') {
 if (h->oldpix!=((void *)0)) {
     free(h->oldpix);
     h->oldpix=((void *)0);
 }
 if (c=='i') {
     printf(" With this mode, you can find some values for a new specfile.\n");
     printf(" u and d moves picture up and down\n");
     printf(" l and r moves picture left and right\n");
     printf(" t toggles bpp\n");
     printf(" i gives this info\n");
     printf(" q quits\n");
 } else if (c=='u') {
     h->offy--;
 } else if (c=='d') {
     h->offy++;
 } else if (c=='l') {
     h->offx--;
 } else if (c=='r') {
     h->offx++;
 } else if (c=='t') {
     if (h->bpp==12) h->bpp=16; else h->bpp=12;
 }
 printf(" pressed\nCurrent settings: offx=%i offy=%i bpp=%i\n",h->offx,h->offy,h->bpp);
 sendpic(h,what);
 c=getchar();
    }
    printf("K, bye.");
    tcsetattr(0, 0, &oldterm);
}

variable_types: struct st2205_handle { int fd; unsigned int width; unsigned int height; int bpp; int proto; char *buff; unsigned char *oldpix; int offx; int offy; } *

variable_types:
what
char *

variable_types: c char

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int *v3; // rax
__int64 v4; // rbp
const char *v5; // r12
const char *v6; // rbx

if ( argc <= 1 )
{
puts("Usage:");
printf(" %s /dev/sdX [-upload] pic.png\n", *argv);
puts(" sends a picture to the screen");
printf(" %s /dev/sdX -backlight on|off\n", *argv);
puts(" for backlight control");
printf(" %s /dev/sdX -test pic.png\n", *argv);
puts(" Test-mode: interactively find out values for the spec-file");
exit(0);
}
v3 = (unsigned int *)st2205_open(argv[1]);
v4 = (__int64)v3;
if ( !v3 )
{
puts("Open failed!");
exit(1);
}
printf("Found device: %ix%i, %i bpp\n", v3[1], v3[2], v3[3]);
v5 = argv[2];
if ( !strcmp(v5, "-backlight") )
{
v6 = argv[3];
if ( !strcmp(v6, "off") )
{
st2205_backlight(v4, 0LL);
}
else if ( !strcmp(v6, "on") )
{
st2205_backlight(v4, 1LL);
}
}
else if ( !strcmp(v5, "-upload") )
{
sendthis(v4, argv[3]);
}
else if ( !strcmp(v5, "-test") )
{
testpic(v4, argv[3]);
}
else
{
sendthis(v4, v5);
}
st2205_close(v4);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int *v3; // rax
__int64 v4; // rbp
const char *v5; // r12
const char *v6; // rbx

if ( argc <= 1 )
{
puts("Usage:");
printf(" %s /dev/sdX [-upload] pic.png\n", *argv);
puts(" sends a picture to the screen");
printf(" %s /dev/sdX -backlight on|off\n", *argv);
puts(" for backlight control");
printf(" %s /dev/sdX -test pic.png\n", *argv);
puts(" Test-mode: interactively find out values for the spec-file");
exit(0);
}
v3 = (unsigned int *)st2205_open(argv[1]);
v4 = (__int64)v3;
if ( !v3 )
{
puts("Open failed!");
exit(1);
}
printf("Found device: %ix%i, %i bpp\n", v3[1], v3[2], v3[3]);
v5 = argv[2];
if ( !strcmp(v5, "-backlight") )
{
v6 = argv[3];
if ( !strcmp(v6, "off") )
{
st2205_backlight(v4, 0LL);
}
else if ( !strcmp(v6, "on") )
{
st2205_backlight(v4, 1LL);
}
}
else if ( !strcmp(v5, "-upload") )
{
func1(v4, argv[3]);
}
else if ( !strcmp(v5, "-test") )
{
func2(v4, argv[3]);
}
else
{
func1(v4, v5);
}
st2205_close(v4);
return 0;
}

original_code:
int main(int argc, char **argv) {
    st2205_handle *h;
    if (argc<2) {
 printf("Usage:\n");
 printf(" %s /dev/sdX [-upload] pic.png\n",argv[0]);
 printf("  sends a picture to the screen\n");
 printf(" %s /dev/sdX -backlight on|off\n",argv[0]);
 printf("  for backlight control\n");
 printf(" %s /dev/sdX -test pic.png\n",argv[0]);
 printf("  Test-mode: interactively find out values for the spec-file\n");
 exit(0);
    }
    h=st2205_open(argv[1]);
    if (h==((void *)0)) {
 printf("Open failed!\n");
 exit(1);
    }
    printf("Found device: %ix%i, %i bpp\n",h->width,h->height,h->bpp);
    if (strcmp(argv[2],"-backlight")==0) {
 if (strcmp(argv[3],"off")==0) {
     st2205_backlight(h, 0);
 } else if (strcmp(argv[3],"on")==0) {
         st2205_backlight(h, 1);
 }
    } else if (strcmp(argv[2],"-upload")==0) {
 sendthis(h,argv[3]);
    } else if (strcmp(argv[2],"-test")==0) {
 testpic(h,argv[3]);
    } else {
 sendthis(h,argv[2]);
    }
    st2205_close(h);
    return(0);
}

canonical_original_code:
int main(int argc, char **argv) {
    struct st2205_handle *h;
    if (argc<2) {
 printf("Usage:\n");
 printf(" %s /dev/sdX [-upload] pic.png\n",argv[0]);
 printf("  sends a picture to the screen\n");
 printf(" %s /dev/sdX -backlight on|off\n",argv[0]);
 printf("  for backlight control\n");
 printf(" %s /dev/sdX -test pic.png\n",argv[0]);
 printf("  Test-mode: interactively find out values for the spec-file\n");
 exit(0);
    }
    h=st2205_open(argv[1]);
    if (h==((void *)0)) {
 printf("Open failed!\n");
 exit(1);
    }
    printf("Found device: %ix%i, %i bpp\n",h->width,h->height,h->bpp);
    if (strcmp(argv[2],"-backlight")==0) {
 if (strcmp(argv[3],"off")==0) {
     st2205_backlight(h, 0);
 } else if (strcmp(argv[3],"on")==0) {
         st2205_backlight(h, 1);
 }
    } else if (strcmp(argv[2],"-upload")==0) {
 sendthis(h,argv[3]);
    } else if (strcmp(argv[2],"-test")==0) {
 testpic(h,argv[3]);
    } else {
 sendthis(h,argv[2]);
    }
    st2205_close(h);
    return(0);
}

user_defined_types: struct st2205_handle { int fd; unsigned int width; unsigned int height; int bpp; int proto; char *buff; unsigned char *oldpix; int offx; int offy; }


alhood

repo: neopunisher/LDA-C

decompiled_code:
double __fastcall alhood(int a1, int a2, double a3, double a4)
{
double v4; // rbx

v4 = lgamma((double)a2 * a3);
return (v4 - (double)a2 * lgamma(a3)) * (double)a1 + (a3 - 1.0) * a4;
}

canonical_decompiled_code:
double __fastcall func0(int a1, int a2, double a3, double a4)
{
double v4; // rbx

v4 = lgamma((double)a2 * a3);
return (v4 - (double)a2 * lgamma(a3)) * (double)a1 + (a3 - 1.0) * a4;
}

original_code:
double alhood(double a, double ss, int D, int K)
{ return(D * (lgamma(K * a) - K * lgamma(a)) + (a - 1) * ss); }

variable_types: a double

variable_types: ss double

variable_types: D int

variable_types: K int

return_type: double


decompiled_code:
double __fastcall d_alhood(int a1, int a2, double a3, double a4)
{
double v6; // [rsp+18h] [rbp-20h]

v6 = digamma((double)a2 * a3) * (double)a2;
return (v6 - (double)a2 * digamma(a3)) * (double)a1 + a4;
}

canonical_decompiled_code:
double __fastcall func1(int a1, int a2, double a3, double a4)
{
double v6; // [rsp+18h] [rbp-20h]

v6 = digamma((double)a2 * a3) * (double)a2;
return (v6 - (double)a2 * digamma(a3)) * (double)a1 + a4;
}

original_code:
double d_alhood(double a, double ss, int D, int K)
{ return(D * (K * digamma(K * a) - K * digamma(a)) + ss); }


decompiled_code:
double __fastcall d2_alhood(int a1, int a2, double a3)
{
double v5; // [rsp+18h] [rbp-20h]

v5 = (double)(a2 * a2) * trigamma((double)a2 * a3);
return (v5 - (double)a2 * trigamma(a3)) * (double)a1;
}

canonical_decompiled_code:
double __fastcall func2(int a1, int a2, double a3)
{
double v5; // [rsp+18h] [rbp-20h]

v5 = (double)(a2 * a2) * trigamma((double)a2 * a3);
return (v5 - (double)a2 * trigamma(a3)) * (double)a1;
}

original_code:
double d2_alhood(double a, int D, int K)
{ return(D * (K * K * trigamma(K * a) - K * trigamma(a))); }


decompiled_code:
double __fastcall opt_alpha(int a1, int a2, double a3)
{
int v3; // er13
double v4; // r15
double i; // xmm0_8
double v6; // rbx
double x; // [rsp+8h] [rbp-50h]
double v9; // [rsp+10h] [rbp-48h]

v3 = 1;
v4 = *(double *)&LC2;
x = *(double *)&LC3;
for ( i = alhood(a1, a2, 100.0, a3); ; i = alhood(a1, a2, x, a3) )
{
v6 = i;
v9 = d_alhood(a1, a2, x, a3);
v4 = v4 - v9 / (d2_alhood(a1, a2, x) * x + v9);
printf("alpha maximization : %5.5f %5.5f\n", v6, v9);
if ( COERCE_DOUBLE(*(_QWORD *)&v9 & LC7) <= 0.00001 || v3 > 999 )
break;
++v3;
x = exp(v4);
}
return exp(v4);
}

canonical_decompiled_code:
double __fastcall func3(int a1, int a2, double a3)
{
int v3; // er13
double v4; // r15
double i; // xmm0_8
double v6; // rbx
double x; // [rsp+8h] [rbp-50h]
double v9; // [rsp+10h] [rbp-48h]

v3 = 1;
v4 = *(double *)&LC2;
x = *(double *)&LC3;
for ( i = func0(a1, a2, 100.0, a3); ; i = func0(a1, a2, x, a3) )
{
v6 = i;
v9 = func1(a1, a2, x, a3);
v4 = v4 - v9 / (func2(a1, a2, x) * x + v9);
printf("alpha maximization : %5.5f %5.5f\n", v6, v9);
if ( COERCE_DOUBLE(*(_QWORD *)&v9 & LC7) <= 0.00001 || v3 > 999 )
break;
++v3;
x = exp(v4);
}
return exp(v4);
}

original_code:
double opt_alpha(double ss, int D, int K)
{
    double a, log_a, init_a = 100;
    double f, df, d2f;
    int iter = 0;
    log_a = log(init_a);
    do
    {
        iter++;
        a = exp(log_a);
        if (__builtin_isnan (a))
        {
            init_a = init_a * 10;
            printf("warning : alpha is nan; new init = %5.5f\n", init_a);
            a = init_a;
            log_a = log(a);
        }
        f = alhood(a, ss, D, K);
        df = d_alhood(a, ss, D, K);
        d2f = d2_alhood(a, D, K);
        log_a = log_a - df/(d2f * a + df);
        printf("alpha maximization : %5.5f   %5.5f\n", f, df);
    }
    while ((fabs(df) > 1e-5) && (iter < 1000));
    return(exp(log_a));
}

canonical_original_code:
double opt_alpha(double ss, int D, int K)
{
    double a; double log_a; double init_a = 100;
    double f; double df; double d2f;
    int iter = 0;
    log_a = log(init_a);
    do
    {
        iter++;
        a = exp(log_a);
        if (__builtin_isnan (a))
        {
            init_a = init_a * 10;
            printf("warning : alpha is nan; new init = %5.5f\n", init_a);
            a = init_a;
            log_a = log(a);
        }
        f = alhood(a, ss, D, K);
        df = d_alhood(a, ss, D, K);
        d2f = d2_alhood(a, D, K);
        log_a = log_a - df/(d2f * a + df);
        printf("alpha maximization : %5.5f   %5.5f\n", f, df);
    }
    while ((fabs(df) > 1e-5) && (iter < 1000));
    return(exp(log_a));
}

variable_types: f double

variable_types: df double

variable_types: iter int


original_code:
double doc_e_step(document* doc, double* gamma, double** phi,
                  lda_model* model, lda_suffstats* ss)
{
    double likelihood;
    int n, k;
    likelihood = lda_inference(doc, model, gamma, phi);
    double gamma_sum = 0;
    for (k = 0; k < model->num_topics; k++)
    {
        gamma_sum += gamma[k];
        ss->alpha_suffstats += digamma(gamma[k]);
    }
    ss->alpha_suffstats -= model->num_topics * digamma(gamma_sum);
    for (n = 0; n < doc->length; n++)
    {
        for (k = 0; k < model->num_topics; k++)
        {
            ss->class_word[k][doc->words[n]] += doc->counts[n]*phi[n][k];
            ss->class_total[k] += doc->counts[n]*phi[n][k];
        }
    }
    ss->num_docs = ss->num_docs + 1;
    return(likelihood);
}

canonical_original_code:
double doc_e_step(struct document *doc, double *gamma, double **phi, struct lda_model *model, struct lda_suffstats *ss)
{
    double likelihood;
    int n; int k;
    likelihood = lda_inference(doc, model, gamma, phi);
    double gamma_sum = 0;
    for (k = 0; k < model->num_topics; k++)
    {
        gamma_sum += gamma[k];
        ss->alpha_suffstats += digamma(gamma[k]);
    }
    ss->alpha_suffstats -= model->num_topics * digamma(gamma_sum);
    for (n = 0; n < doc->length; n++)
    {
        for (k = 0; k < model->num_topics; k++)
        {
            ss->class_word[k][doc->words[n]] += doc->counts[n]*phi[n][k];
            ss->class_total[k] += doc->counts[n]*phi[n][k];
        }
    }
    ss->num_docs = ss->num_docs + 1;
    return(likelihood);
}

variable_types: struct document { int *words; int *counts; int length; int total; } *

variable_types:
gamma
double *

variable_types:
phi
double **

variable_types: struct lda_model { double alpha; double **log_prob_w; int num_topics; int num_terms; } *

variable_types: struct lda_suffstats { double **class_word; double *class_total; double alpha_suffstats; int num_docs; } *

variable_types: likelihood double

variable_types: n int

variable_types: k int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
bool v4; // cf
bool v5; // zf
const char *v6; // rsi
const char *v7; // rdi
__int64 v8; // rcx
char v9; // al
bool v10; // cf
bool v11; // zf
const char *v12; // rsi
const char *v13; // rdi
__int64 v14; // rcx
__int64 v16; // rbp
__int64 v17; // rax
time_t timer; // [rsp+8h] [rbp-20h] BYREF

time(&timer);
seedMT(timer);
v4 = argc == 0;
v5 = argc == 1;
if ( argc <= 1 )
{
puts("usage : lda est [initial alpha] [k] [settings] [data] [random/seeded/*] [directory]");
puts(" lda inf [settings] [model] [data] [name]");
}
else
{
v6 = argv[1];
v7 = "est";
v8 = 4LL;
do
{
if ( !v8 )
break;
v4 = *v6 < (unsigned int)*v7;
v5 = *v6++ == *v7++;
--v8;
}
while ( v5 );
v9 = (!v4 && !v5) - v4;
v10 = 0;
v11 = v9 == 0;
if ( !v9 )
{
INITIAL_ALPHA = atof(argv[2]);
NTOPICS = atoi(argv[3]);
read_settings(argv[4]);
v16 = read_data(argv[5], v6);
make_directory(argv[7]);
run_em(argv[6], argv[7], v16);
}
v12 = argv[1];
v13 = "inf";
v14 = 4LL;
do
{
if ( !v14 )
break;
v10 = *v12 < (unsigned int)*v13;
v11 = *v12++ == *v13++;
--v14;
}
while ( v11 );
if ( (!v10 && !v11) == v10 )
{
read_settings(argv[2]);
v17 = read_data(argv[4], v12);
infer((__int64)argv[3], argv[5], v17);
}
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
bool v4; // cf
bool v5; // zf
const char *v6; // rsi
const char *v7; // rdi
__int64 v8; // rcx
char v9; // al
bool v10; // cf
bool v11; // zf
const char *v12; // rsi
const char *v13; // rdi
__int64 v14; // rcx
__int64 v16; // rbp
__int64 v17; // rax
time_t timer; // [rsp+8h] [rbp-20h] BYREF

time(&timer);
seedMT(timer);
v4 = argc == 0;
v5 = argc == 1;
if ( argc <= 1 )
{
puts("usage : lda est [initial alpha] [k] [settings] [data] [random/seeded/*] [directory]");
puts(" lda inf [settings] [model] [data] [name]");
}
else
{
v6 = argv[1];
v7 = "est";
v8 = 4LL;
do
{
if ( !v8 )
break;
v4 = *v6 < (unsigned int)*v7;
v5 = *v6++ == *v7++;
--v8;
}
while ( v5 );
v9 = (!v4 && !v5) - v4;
v10 = 0;
v11 = v9 == 0;
if ( !v9 )
{
INITIAL_ALPHA = atof(argv[2]);
NTOPICS = atoi(argv[3]);
func4(argv[4]);
v16 = read_data(argv[5], v6);
make_directory(argv[7]);
func3(argv[6], argv[7], v16);
}
v12 = argv[1];
v13 = "inf";
v14 = 4LL;
do
{
if ( !v14 )
break;
v10 = *v12 < (unsigned int)*v13;
v11 = *v12++ == *v13++;
--v14;
}
while ( v11 );
if ( (!v10 && !v11) == v10 )
{
func4(argv[2]);
v17 = read_data(argv[4], v12);
func5((__int64)argv[3], argv[5], v17);
}
}
return 0;
}

original_code:
int main(int argc, char* argv[])
{
    corpus* corpus;
    long t1;
    (void) time(&t1);
    seedMT(t1);
    if (argc > 1)
    {
        if (strcmp(argv[1], "est")==0)
        {
            INITIAL_ALPHA = atof(argv[2]);
            NTOPICS = atoi(argv[3]);
            read_settings(argv[4]);
            corpus = read_data(argv[5]);
            make_directory(argv[7]);
            run_em(argv[6], argv[7], corpus);
        }
        if (strcmp(argv[1], "inf")==0)
        {
            read_settings(argv[2]);
            corpus = read_data(argv[4]);
            infer(argv[3], argv[5], corpus);
        }
    }
    else
    {
        printf("usage : lda est [initial alpha] [k] [settings] [data] [random/seeded/*] [directory]\n");
        printf("        lda inf [settings] [model] [data] [name]\n");
    }
    return(0);
}

canonical_original_code:
int main(int argc, char *argv[])
{
    struct corpus *corpus;
    long t1;
    (void) time(&t1);
    seedMT(t1);
    if (argc > 1)
    {
        if (strcmp(argv[1], "est")==0)
        {
            INITIAL_ALPHA = atof(argv[2]);
            NTOPICS = atoi(argv[3]);
            read_settings(argv[4]);
            corpus = read_data(argv[5]);
            make_directory(argv[7]);
            run_em(argv[6], argv[7], corpus);
        }
        if (strcmp(argv[1], "inf")==0)
        {
            read_settings(argv[2]);
            corpus = read_data(argv[4]);
            infer(argv[3], argv[5], corpus);
        }
    }
    else
    {
        printf("usage : lda est [initial alpha] [k] [settings] [data] [random/seeded/*] [directory]\n");
        printf("        lda inf [settings] [model] [data] [name]\n");
    }
    return(0);
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct corpus { struct document { int *words; int *counts; int length; int total; } *docs; int num_terms; int num_docs; } *

user_defined_types: struct corpus { struct document *docs; int num_terms; int num_docs; }


original_code:
double
compute_likelihood(document* doc, lda_model* model, double** phi, double* var_gamma)
{
    double likelihood = 0, digsum = 0, var_gamma_sum = 0, dig[model->num_topics];
    int k, n;
    for (k = 0; k < model->num_topics; k++)
    {
 dig[k] = digamma(var_gamma[k]);
 var_gamma_sum += var_gamma[k];
    }
    digsum = digamma(var_gamma_sum);
    likelihood =
 lgamma(model->alpha * model -> num_topics)
 - model -> num_topics * lgamma(model->alpha)
 - (lgamma(var_gamma_sum));
    for (k = 0; k < model->num_topics; k++)
    {
 likelihood +=
     (model->alpha - 1)*(dig[k] - digsum) + lgamma(var_gamma[k])
     - (var_gamma[k] - 1)*(dig[k] - digsum);
 for (n = 0; n < doc->length; n++)
 {
            if (phi[n][k] > 0)
            {
                likelihood += doc->counts[n]*
                    (phi[n][k]*((dig[k] - digsum) - log(phi[n][k])
                                + model->log_prob_w[k][doc->words[n]]));
            }
        }
    }
    return(likelihood);
}

canonical_original_code:
double compute_likelihood(struct document *doc, struct lda_model *model, double **phi, double *var_gamma)
{
    double likelihood = 0; double digsum = 0; double var_gamma_sum = 0; double dig[-1];
    int k; int n;
    for (k = 0; k < model->num_topics; k++)
    {
 dig[k] = digamma(var_gamma[k]);
 var_gamma_sum += var_gamma[k];
    }
    digsum = digamma(var_gamma_sum);
    likelihood =
 lgamma(model->alpha * model -> num_topics)
 - model -> num_topics * lgamma(model->alpha)
 - (lgamma(var_gamma_sum));
    for (k = 0; k < model->num_topics; k++)
    {
 likelihood +=
     (model->alpha - 1)*(dig[k] - digsum) + lgamma(var_gamma[k])
     - (var_gamma[k] - 1)*(dig[k] - digsum);
 for (n = 0; n < doc->length; n++)
 {
            if (phi[n][k] > 0)
            {
                likelihood += doc->counts[n]*
                    (phi[n][k]*((dig[k] - digsum) - log(phi[n][k])
                                + model->log_prob_w[k][doc->words[n]]));
            }
        }
    }
    return(likelihood);
}

variable_types: digsum double

variable_types:
dig
double [-1]

user_defined_types: struct document { int *words; int *counts; int length; int total; }

user_defined_types: struct lda_model { double alpha; double **log_prob_w; int num_topics; int num_terms; }


original_code:
double lda_inference(document* doc, lda_model* model, double* var_gamma, double** phi)
{
    double converged = 1;
    double phisum = 0, likelihood = 0;
    double likelihood_old = 0, oldphi[model->num_topics];
    int k, n, var_iter;
    double digamma_gam[model->num_topics];
    for (k = 0; k < model->num_topics; k++)
    {
        var_gamma[k] = model->alpha + (doc->total/((double) model->num_topics));
        digamma_gam[k] = digamma(var_gamma[k]);
        for (n = 0; n < doc->length; n++)
            phi[n][k] = 1.0/model->num_topics;
    }
    var_iter = 0;
    while ((converged > VAR_CONVERGED) &&
           ((var_iter < VAR_MAX_ITER) || (VAR_MAX_ITER == -1)))
    {
 var_iter++;
 for (n = 0; n < doc->length; n++)
 {
            phisum = 0;
            for (k = 0; k < model->num_topics; k++)
            {
                oldphi[k] = phi[n][k];
                phi[n][k] =
                    digamma_gam[k] +
                    model->log_prob_w[k][doc->words[n]];
                if (k > 0)
                    phisum = log_sum(phisum, phi[n][k]);
                else
                    phisum = phi[n][k];
            }
            for (k = 0; k < model->num_topics; k++)
            {
                phi[n][k] = exp(phi[n][k] - phisum);
                var_gamma[k] =
                    var_gamma[k] + doc->counts[n]*(phi[n][k] - oldphi[k]);
                digamma_gam[k] = digamma(var_gamma[k]);
            }
        }
        likelihood = compute_likelihood(doc, model, phi, var_gamma);
        ((void) sizeof ((!__builtin_isnan (likelihood)) ? 1 : 0), __extension__ ({ if (!__builtin_isnan (likelihood)) ; else __assert_fail ("!isnan(likelihood)", "lda-inference.c", 78, __extension__ __PRETTY_FUNCTION__); }));
        converged = (likelihood_old - likelihood) / likelihood_old;
        likelihood_old = likelihood;
    }
    return(likelihood);
}

canonical_original_code:
double lda_inference(struct document *doc, struct lda_model *model, double *var_gamma, double **phi)
{
    double converged = 1;
    double phisum = 0; double likelihood = 0;
    double likelihood_old = 0; double oldphi[-1];
    int k; int n; int var_iter;
    double digamma_gam[-1];
    for (k = 0; k < model->num_topics; k++)
    {
        var_gamma[k] = model->alpha + (doc->total/((double) model->num_topics));
        digamma_gam[k] = digamma(var_gamma[k]);
        for (n = 0; n < doc->length; n++)
            phi[n][k] = 1.0/model->num_topics;
    }
    var_iter = 0;
    while ((converged > VAR_CONVERGED) &&
           ((var_iter < VAR_MAX_ITER) || (VAR_MAX_ITER == -1)))
    {
 var_iter++;
 for (n = 0; n < doc->length; n++)
 {
            phisum = 0;
            for (k = 0; k < model->num_topics; k++)
            {
                oldphi[k] = phi[n][k];
                phi[n][k] =
                    digamma_gam[k] +
                    model->log_prob_w[k][doc->words[n]];
                if (k > 0)
                    phisum = log_sum(phisum, phi[n][k]);
                else
                    phisum = phi[n][k];
            }
            for (k = 0; k < model->num_topics; k++)
            {
                phi[n][k] = exp(phi[n][k] - phisum);
                var_gamma[k] =
                    var_gamma[k] + doc->counts[n]*(phi[n][k] - oldphi[k]);
                digamma_gam[k] = digamma(var_gamma[k]);
            }
        }
        likelihood = compute_likelihood(doc, model, phi, var_gamma);
        ((void) sizeof ((!__builtin_isnan (likelihood)) ? 1 : 0), __extension__ ({ if (!__builtin_isnan (likelihood)) ; else __assert_fail ("!isnan(likelihood)", "lda-inference.c", 78, __extension__ __PRETTY_FUNCTION__); }));
        converged = (likelihood_old - likelihood) / likelihood_old;
        likelihood_old = likelihood;
    }
    return(likelihood);
}

variable_types: converged double

variable_types: phisum double

variable_types:
oldphi
double [-1]


decompiled_code:
double __fastcall log_sum(double a1, double a2)
{
double v2; // xmm0_8
double result; // xmm0_8
double v4; // xmm0_8

if ( a2 <= a1 )
{
v4 = exp(a2 - a1);
result = log(v4 + 1.0) + a1;
}
else
{
v2 = exp(a1 - a2);
result = log(v2 + 1.0) + a2;
}
return result;
}

canonical_decompiled_code:
double __fastcall func0(double a1, double a2)
{
double v2; // xmm0_8
double result; // xmm0_8
double v4; // xmm0_8

if ( a2 <= a1 )
{
v4 = exp(a2 - a1);
result = log(v4 + 1.0) + a1;
}
else
{
v2 = exp(a1 - a2);
result = log(v2 + 1.0) + a2;
}
return result;
}

original_code:
double log_sum(double log_a, double log_b)
{
  double v;
  if (log_a < log_b)
  {
      v = log_b+log(1 + exp(log_a-log_b));
  }
  else
  {
      v = log_a+log(1 + exp(log_b-log_a));
  }
  return(v);
}

variable_types: v double


decompiled_code:
int __fastcall make_directory(const char *a1)
{
return mkdir(a1, 0x1C0u);
}

canonical_decompiled_code:
int __fastcall func4(const char *a1)
{
return mkdir(a1, 0x1C0u);
}

original_code:
void make_directory(char* name)
{
    mkdir(name, 0400|0200|0100);
}

canonical_original_code:
void make_directory(char *name)
{
    mkdir(name, 0400|0200|0100);
}

variable_types:
name
char *


argmax

decompiled_code:
__int64 __fastcall argmax(double *a1, int a2)
{
double v2; // xmm0_8
__int64 v3; // rax
unsigned int v4; // edx
double v5; // xmm1_8

v2 = *a1;
if ( a2 <= 1 )
return 0;
v3 = 1LL;
v4 = 0;
do
{
v5 = a1[v3];
if ( v5 > v2 )
v4 = v3;
v2 = fmax(v5, v2);
++v3;
}
while ( v3 != a2 );
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func5(double *a1, int a2)
{
double v2; // xmm0_8
__int64 v3; // rax
unsigned int v4; // edx
double v5; // xmm1_8

v2 = *a1;
if ( a2 <= 1 )
return 0;
v3 = 1LL;
v4 = 0;
do
{
v5 = a1[v3];
if ( v5 > v2 )
v4 = v3;
v2 = fmax(v5, v2);
++v3;
}
while ( v3 != a2 );
return v4;
}

original_code:
int argmax(double* x, int n)
{
    int i;
    double max = x[0];
    int argmax = 0;
    for (i = 1; i < n; i++)
    {
        if (x[i] > max)
        {
            max = x[i];
            argmax = i;
        }
    }
    return(argmax);
}

canonical_original_code:
int argmax(double *x, int n)
{
    int i;
    double max = x[0];
    int argmax = 0;
    for (i = 1; i < n; i++)
    {
        if (x[i] > max)
        {
            max = x[i];
            argmax = i;
        }
    }
    return(argmax);
}

variable_types:
x
double *

variable_types: i int

variable_types: max double

variable_types: argmax int


original_code:
void lda_mle(lda_model* model, lda_suffstats* ss, int estimate_alpha)
{
    int k; int w;
    for (k = 0; k < model->num_topics; k++)
    {
        for (w = 0; w < model->num_terms; w++)
        {
            if (ss->class_word[k][w] > 0)
            {
                model->log_prob_w[k][w] =
                    log(ss->class_word[k][w]) -
                    log(ss->class_total[k]);
            }
            else
                model->log_prob_w[k][w] = -100;
        }
    }
    if (estimate_alpha == 1)
    {
        model->alpha = opt_alpha(ss->alpha_suffstats,
                                 ss->num_docs,
                                 model->num_topics);
        printf("new alpha = %5.5f\n", model->alpha);
    }
}

canonical_original_code:
void lda_mle(struct lda_model *model, struct lda_suffstats *ss, int estimate_alpha)
{
    int k; int w;
    for (k = 0; k < model->num_topics; k++)
    {
        for (w = 0; w < model->num_terms; w++)
        {
            if (ss->class_word[k][w] > 0)
            {
                model->log_prob_w[k][w] =
                    log(ss->class_word[k][w]) -
                    log(ss->class_total[k]);
            }
            else
                model->log_prob_w[k][w] = -100;
        }
    }
    if (estimate_alpha == 1)
    {
        model->alpha = opt_alpha(ss->alpha_suffstats,
                                 ss->num_docs,
                                 model->num_topics);
        printf("new alpha = %5.5f\n", model->alpha);
    }
}

variable_types: struct lda_model { double alpha; double **log_prob_w; int num_topics; int num_terms; } *

variable_types: struct lda_suffstats { double **class_word; double *class_total; double alpha_suffstats; int num_docs; } *

variable_types: w int


original_code:
void zero_initialize_ss(lda_suffstats* ss, lda_model* model)
{
    int k, w;
    for (k = 0; k < model->num_topics; k++)
    {
        ss->class_total[k] = 0;
        for (w = 0; w < model->num_terms; w++)
        {
            ss->class_word[k][w] = 0;
        }
    }
    ss->num_docs = 0;
    ss->alpha_suffstats = 0;
}

canonical_original_code:
void zero_initialize_ss(struct lda_suffstats *ss, struct lda_model *model)
{
    int k; int w;
    for (k = 0; k < model->num_topics; k++)
    {
        ss->class_total[k] = 0;
        for (w = 0; w < model->num_terms; w++)
        {
            ss->class_word[k][w] = 0;
        }
    }
    ss->num_docs = 0;
    ss->alpha_suffstats = 0;
}

variable_types: k int

user_defined_types: struct lda_suffstats { double **class_word; double *class_total; double alpha_suffstats; int num_docs; }


decompiled_code:
unsigned __int64 __fastcall random_initialize_ss(_QWORD *a1, __int64 a2)
{
unsigned __int64 result; // rax
int v3; // er14
__int64 v4; // r15
__int64 v5; // r13
__int64 v6; // rbp
double v7; // xmm0_8
__int64 v8; // rbx
__int64 v9; // rax

result = *(unsigned int *)(a2 + 16);
v3 = *(_DWORD *)(a2 + 20);
if ( (int)result > 0 )
{
v4 = 8LL * (unsigned int)result;
result = (unsigned int)(v3 - 1);
v5 = 8 * result + 8;
v6 = 0LL;
do
{
if ( v3 > 0 )
{
v8 = 0LL;
do
{
v9 = randomMT();
if ( v9 < 0 )
v7 = (double)(int)(v9 & 1 | ((unsigned __int64)v9 >> 1))
+ (double)(int)(v9 & 1 | ((unsigned __int64)v9 >> 1));
else
v7 = (double)(int)v9;
*(double *)(*(_QWORD *)(*a1 + v6) + v8) = v7 * 2.328306436538696e-10
+ 1.0 / (double)v3
+ *(double *)(*(_QWORD *)(*a1 + v6) + v8);
result = a1[1] + v6;
*(double *)result = *(double *)result + *(double *)(*(_QWORD *)(*a1 + v6) + v8);
v8 += 8LL;
}
while ( v8 != v5 );
}
v6 += 8LL;
}
while ( v6 != v4 );
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func2(_QWORD *a1, __int64 a2)
{
unsigned __int64 result; // rax
int v3; // er14
__int64 v4; // r15
__int64 v5; // r13
__int64 v6; // rbp
double v7; // xmm0_8
__int64 v8; // rbx
__int64 v9; // rax

result = *(unsigned int *)(a2 + 16);
v3 = *(_DWORD *)(a2 + 20);
if ( (int)result > 0 )
{
v4 = 8LL * (unsigned int)result;
result = (unsigned int)(v3 - 1);
v5 = 8 * result + 8;
v6 = 0LL;
do
{
if ( v3 > 0 )
{
v8 = 0LL;
do
{
v9 = randomMT();
if ( v9 < 0 )
v7 = (double)(int)(v9 & 1 | ((unsigned __int64)v9 >> 1))
+ (double)(int)(v9 & 1 | ((unsigned __int64)v9 >> 1));
else
v7 = (double)(int)v9;
*(double *)(*(_QWORD *)(*a1 + v6) + v8) = v7 * 2.328306436538696e-10
+ 1.0 / (double)v3
+ *(double *)(*(_QWORD *)(*a1 + v6) + v8);
result = a1[1] + v6;
*(double *)result = *(double *)result + *(double *)(*(_QWORD *)(*a1 + v6) + v8);
v8 += 8LL;
}
while ( v8 != v5 );
}
v6 += 8LL;
}
while ( v6 != v4 );
}
return result;
}

original_code:
void random_initialize_ss(lda_suffstats* ss, lda_model* model)
{
    int num_topics = model->num_topics;
    int num_terms = model->num_terms;
    int k, n;
    for (k = 0; k < num_topics; k++)
    {
        for (n = 0; n < num_terms; n++)
        {
            ss->class_word[k][n] += 1.0/num_terms + (double) (((unsigned long) randomMT()) / 4294967296.);
            ss->class_total[k] += ss->class_word[k][n];
        }
    }
}

canonical_original_code:
void random_initialize_ss(struct lda_suffstats *ss, struct lda_model *model)
{
    int num_topics = model->num_topics;
    int num_terms = model->num_terms;
    int k; int n;
    for (k = 0; k < num_topics; k++)
    {
        for (n = 0; n < num_terms; n++)
        {
            ss->class_word[k][n] += 1.0/num_terms + (double) (((unsigned long) randomMT()) / 4294967296.);
            ss->class_total[k] += ss->class_word[k][n];
        }
    }
}

variable_types: n int


original_code:
void corpus_initialize_ss(lda_suffstats* ss, lda_model* model, corpus* c)
{
    int num_topics = model->num_topics;
    int i, k, d, n;
    document* doc;
    for (k = 0; k < num_topics; k++)
    {
        for (i = 0; i < 1; i++)
        {
            d = floor((double) (((unsigned long) randomMT()) / 4294967296.) * c->num_docs);
            printf("initialized with document %d\n", d);
            doc = &(c->docs[d]);
            for (n = 0; n < doc->length; n++)
            {
                ss->class_word[k][doc->words[n]] += doc->counts[n];
            }
        }
        for (n = 0; n < model->num_terms; n++)
        {
            ss->class_word[k][n] += 1.0;
            ss->class_total[k] = ss->class_total[k] + ss->class_word[k][n];
        }
    }
}

canonical_original_code:
void corpus_initialize_ss(struct lda_suffstats *ss, struct lda_model *model, struct corpus *c)
{
    int num_topics = model->num_topics;
    int i; int k; int d; int n;
    struct document *doc;
    for (k = 0; k < num_topics; k++)
    {
        for (i = 0; i < 1; i++)
        {
            d = floor((double) (((unsigned long) randomMT()) / 4294967296.) * c->num_docs);
            printf("initialized with document %d\n", d);
            doc = &(c->docs[d]);
            for (n = 0; n < doc->length; n++)
            {
                ss->class_word[k][doc->words[n]] += doc->counts[n];
            }
        }
        for (n = 0; n < model->num_terms; n++)
        {
            ss->class_word[k][n] += 1.0;
            ss->class_total[k] = ss->class_total[k] + ss->class_word[k][n];
        }
    }
}

variable_types: struct corpus { struct document { int *words; int *counts; int length; int total; } *docs; int num_terms; int num_docs; } *

variable_types: d int


original_code:
void free_lda_model(lda_model* model)
{
    int i;
    for (i = 0; i < model->num_topics; i++)
    {
 free(model->log_prob_w[i]);
    }
    free(model->log_prob_w);
}

canonical_original_code:
void free_lda_model(struct lda_model *model)
{
    int i;
    for (i = 0; i < model->num_topics; i++)
    {
 free(model->log_prob_w[i]);
    }
    free(model->log_prob_w);
}


reloadMT

variable_types:
pM
unsigned long *

variable_types: j int

return_type: unsigned long


randomMT

canonical_original_code:
unsigned long randomMT()
 {
    unsigned long y;
    if(--left < 0)
        return(reloadMT());
    y = *next++;
    y ^= (y >> 11);
    y ^= (y << 7) & 0x9D2C5680U;
    y ^= (y << 15) & 0xEFC60000U;
    y ^= (y >> 18);
    return(y);
 }

variable_types:
y
unsigned long


decompiled_code:
__int64 __fastcall max_corpus_length(int *a1)
{
int v1; // edx
unsigned int *v2; // rax
__int64 v3; // rsi
unsigned int v4; // edx

v1 = a1[3];
if ( v1 <= 0 )
return 0;
v2 = (unsigned int *)(*(_QWORD *)a1 + 16LL);
v3 = *(_QWORD *)a1 + 24LL * (unsigned int)(v1 - 1) + 40;
v4 = 0;
do
{
if ( (int)v4 < (int)*v2 )
v4 = *v2;
v2 += 6;
}
while ( v2 != (unsigned int *)v3 );
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func0(int *a1)
{
int v1; // edx
unsigned int *v2; // rax
__int64 v3; // rsi
unsigned int v4; // edx

v1 = a1[3];
if ( v1 <= 0 )
return 0;
v2 = (unsigned int *)(*(_QWORD *)a1 + 16LL);
v3 = *(_QWORD *)a1 + 24LL * (unsigned int)(v1 - 1) + 40;
v4 = 0;
do
{
if ( (int)v4 < (int)*v2 )
v4 = *v2;
v2 += 6;
}
while ( v2 != (unsigned int *)v3 );
return v4;
}

original_code:
int max_corpus_length(corpus* c)
{
    int n, max = 0;
    for (n = 0; n < c->num_docs; n++)
 if (c->docs[n].length > max) max = c->docs[n].length;
    return(max);
}

canonical_original_code:
int max_corpus_length(struct corpus *c)
{
    int n; int max = 0;
    for (n = 0; n < c->num_docs; n++)
 if (c->docs[n].length > max) max = c->docs[n].length;
    return(max);
}

variable_types: max int


repl

repo: neosam/mylisp

decompiled_code:
void __noreturn repl()
{
__int64 v0; // rbx
__int64 v1; // rax
__int64 v2; // rax
char v3[264]; // [rsp+0h] [rbp-108h] BYREF

while ( 1 )
{
printf("> ");
fflush(stdout);
fgets(v3, 255, stdin);
initParserString(v3);
v0 = globalState;
v1 = read();
v2 = eval(v1, v0);
print(v2);
putchar(10);
}
}

canonical_decompiled_code:
void __noreturn func7()
{
__int64 v0; // rbx
__int64 v1; // rax
__int64 v2; // rax
char v3[264]; // [rsp+0h] [rbp-108h] BYREF

while ( 1 )
{
printf("> ");
fflush(stdout);
fgets(v3, 255, stdin);
func10(v3);
v0 = globalState;
v1 = func18();
v2 = eval(v1, v0);
func24(v2);
putchar(10);
}
}

original_code:
void repl()
{
        char input[256];
        while (1) {
                printf("> ");
                fflush(stdout);
                fgets(input, 255, stdin);
                initParserString(input);
                print(eval(read(), globalState));
                printf("\n");
        }
}

variable_types:
input
char [256]


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
_DWORD *v3; // rbx
__int64 v4; // rax

v3 = malloc(0x10uLL);
*v3 = 4;
v3[1] = 1;
globalState = newState(16LL, argv);
**(_DWORD **)globalState = 1;
v4 = globalState;
*(_QWORD *)(*(_QWORD *)globalState + 8LL) = "+";
*(_QWORD *)(*(_QWORD *)v4 + 16LL) = add;
*(_DWORD *)(*(_QWORD *)v4 + 24LL) = 1;
*(_QWORD *)(*(_QWORD *)v4 + 32LL) = "-";
*(_QWORD *)(*(_QWORD *)v4 + 40LL) = sub;
*(_DWORD *)(*(_QWORD *)v4 + 48LL) = 1;
*(_QWORD *)(*(_QWORD *)v4 + 56LL) = "*";
*(_QWORD *)(*(_QWORD *)v4 + 64LL) = mul;
*(_DWORD *)(*(_QWORD *)v4 + 72LL) = 2;
*(_QWORD *)(*(_QWORD *)v4 + 80LL) = "one";
*(_QWORD *)(*(_QWORD *)v4 + 88LL) = v3;
repl();
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
_DWORD *v3; // rbx
__int64 v4; // rax

v3 = malloc(0x10uLL);
*v3 = 4;
v3[1] = 1;
globalState = func26(16LL, argv);
**(_DWORD **)globalState = 1;
v4 = globalState;
*(_QWORD *)(*(_QWORD *)globalState + 8LL) = "+";
*(_QWORD *)(*(_QWORD *)v4 + 16LL) = add;
*(_DWORD *)(*(_QWORD *)v4 + 24LL) = 1;
*(_QWORD *)(*(_QWORD *)v4 + 32LL) = "-";
*(_QWORD *)(*(_QWORD *)v4 + 40LL) = sub;
*(_DWORD *)(*(_QWORD *)v4 + 48LL) = 1;
*(_QWORD *)(*(_QWORD *)v4 + 56LL) = "*";
*(_QWORD *)(*(_QWORD *)v4 + 64LL) = mul;
*(_DWORD *)(*(_QWORD *)v4 + 72LL) = 2;
*(_QWORD *)(*(_QWORD *)v4 + 80LL) = "one";
*(_QWORD *)(*(_QWORD *)v4 + 88LL) = v3;
func7();
}

original_code:
int main()
{
        union elem *one = (union elem *) malloc(sizeof(union elem));
        one->type = 4;
        one->elemInt.value = 1;
        globalState = newState();
        globalState->entries[0].stateInfo.type = 1;
        globalState->entries[0].stateInfo.id = "+";
        globalState->entries[0].stateBuildin.func = add;
        globalState->entries[1].stateInfo.type = 1;
        globalState->entries[1].stateInfo.id = "-";
        globalState->entries[1].stateBuildin.func = sub;
        globalState->entries[2].stateInfo.type = 1;
        globalState->entries[2].stateInfo.id = "*";
        globalState->entries[2].stateBuildin.func = mul;
        globalState->entries[3].stateInfo.type = 2;
        globalState->entries[3].stateInfo.id = "one";
        globalState->entries[3].stateVar.elem = one;
        repl();
        return 0;
}

variable_types: union elem { int type; struct elemInt { int type; int value; } elemInt; struct elemList { int type; struct list { union elem *elem; struct list *next; } *value; } elemList; struct elemID { int type; char *value; } elemID; } *

user_defined_types: union elem { int type; struct elemInt elemInt; struct elemList elemList; struct elemID elemID; }

user_defined_types: struct elemList { int type; struct list *value; }


nextChar

decompiled_code:
__int64 nextChar()
{
int v1; // eax

if ( inputType )
{
if ( inputType == 1 )
{
v1 = inputStringPos++;
currentChar = *(_BYTE *)(inputString + v1);
}
}
else
{
currentChar = fgetc(inputFile);
}
fflush(stdout);
return (unsigned __int8)currentChar;
}

canonical_decompiled_code:
__int64 func8()
{
int v1; // eax

if ( inputType )
{
if ( inputType == 1 )
{
v1 = inputStringPos++;
currentChar = *(_BYTE *)(inputString + v1);
}
}
else
{
currentChar = fgetc(inputFile);
}
fflush(stdout);
return (unsigned __int8)currentChar;
}

original_code:
char nextChar()
{
        switch (inputType) {
        case 0:
                currentChar = fgetc(inputFile);
                break;
        case 1:
                currentChar = inputString[inputStringPos++];
                break;
        }
        fflush(stdout);
        return currentChar;
}

return_type: char


initParserString

decompiled_code:
__int64 __fastcall initParserString(__int64 a1)
{
inputString = a1;
inputType = 1;
inputStringPos = 0;
return nextChar();
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
inputString = a1;
inputType = 1;
inputStringPos = 0;
return func8();
}

original_code:
void initParserString(char *str)
{
        inputString = str;
        inputType = 1;
        inputStringPos = 0;
        nextChar();
}

variable_types:
str
char *


isWhitespace

decompiled_code:
__int64 __fastcall isWhitespace(char a1)
{
return ((a1 & 0xDF) == 0) | (unsigned __int8)((unsigned __int8)(a1 - 9) <= 1u);
}

canonical_decompiled_code:
__int64 __fastcall func11(char a1)
{
return ((a1 & 0xDF) == 0) | (unsigned __int8)((unsigned __int8)(a1 - 9) <= 1u);
}

original_code:
int isWhitespace(char c)
{
        if (c == '\n' || c == ' ' || c == '\t' || c == '\0')
                return 1;
        return 0;
}

variable_types: c char


jumpWhitespace

decompiled_code:
__int64 jumpWhitespace()
{
char v0; // bl
__int64 result; // rax

while ( 1 )
{
v0 = currentChar;
result = isWhitespace(currentChar);
if ( !v0 || !(_DWORD)result )
break;
nextChar();
}
return result;
}

canonical_decompiled_code:
__int64 func12()
{
char v0; // bl
__int64 result; // rax

while ( 1 )
{
v0 = currentChar;
result = func11(currentChar);
if ( !v0 || !(_DWORD)result )
break;
func8();
}
return result;
}

original_code:
void jumpWhitespace()
{
        while (isWhitespace(currentChar) && currentChar != '\0')
                nextChar();
}


parseNumber

decompiled_code:
__int64 parseNumber()
{
char i; // al

intres = 0;
for ( i = currentChar; (unsigned __int8)(currentChar - 48) <= 9u; i = currentChar )
{
intres = i + 10 * intres - 48;
nextChar();
}
return jumpWhitespace();
}

canonical_decompiled_code:
__int64 func13()
{
char i; // al

intres = 0;
for ( i = currentChar; (unsigned __int8)(currentChar - 48) <= 9u; i = currentChar )
{
intres = i + 10 * intres - 48;
func8();
}
return func12();
}

original_code:
void parseNumber()
{
        intres = 0;
        while (currentChar >= '0' && currentChar <= '9') {
                intres *= 10;
                intres += currentChar - '0';
                nextChar();
        }
        jumpWhitespace();
}


parseID

decompiled_code:
__int64 parseID()
{
__int64 i; // rbp
char v1; // bl
int v2; // eax
__int64 result; // rax

idres = (__int64)malloc(0x100uLL);
for ( i = 0LL; ; ++i )
{
v1 = currentChar;
v2 = isWhitespace(currentChar);
if ( v1 == 41 || v2 )
break;
*(_BYTE *)(idres + i) = v1;
nextChar();
}
result = idres;
*(_BYTE *)(idres + (int)i) = 0;
return result;
}

canonical_decompiled_code:
__int64 func14()
{
__int64 i; // rbp
char v1; // bl
int v2; // eax
__int64 result; // rax

idres = (__int64)malloc(0x100uLL);
for ( i = 0LL; ; ++i )
{
v1 = currentChar;
v2 = func11(currentChar);
if ( v1 == 41 || v2 )
break;
*(_BYTE *)(idres + i) = v1;
func8();
}
result = idres;
*(_BYTE *)(idres + (int)i) = 0;
return result;
}

original_code:
void parseID()
{
        idres = (char *) malloc(sizeof(char) * 256);
        int pos = 0;
        while (!isWhitespace(currentChar) && currentChar != ')') {
                idres[pos] = currentChar;
                nextChar();
                pos++;
        }
        idres[pos] = '\0';
}

variable_types: pos int


parseToken

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

jumpWhitespace();
if ( currentChar == 41 )
{
nextChar();
jumpWhitespace();
currentToken = 3;
result = 3LL;
}
else
{
if ( currentChar > 41 )
{
if ( (unsigned __int8)(currentChar - 48) <= 9u )
{
parseNumber();
jumpWhitespace();
currentToken = 4;
return 4LL;
}
}
else if ( currentChar == 40 )
{
nextChar();
jumpWhitespace();
currentToken = 2;
return 2LL;
}
parseID();
jumpWhitespace();
currentToken = 5;
result = 5LL;
}
return result;
}

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

func12();
if ( currentChar == 41 )
{
func8();
func12();
currentToken = 3;
result = 3LL;
}
else
{
if ( currentChar > 41 )
{
if ( (unsigned __int8)(currentChar - 48) <= 9u )
{
func13();
func12();
currentToken = 4;
return 4LL;
}
}
else if ( currentChar == 40 )
{
func8();
func12();
currentToken = 2;
return 2LL;
}
func14();
func12();
currentToken = 5;
result = 5LL;
}
return result;
}

original_code:
int parseToken()
{
        jumpWhitespace();
        switch (currentChar) {
        case '(':
                nextChar();
                jumpWhitespace();
                currentToken = 2;
                return 2;
        case ')':
                nextChar();
                jumpWhitespace();
                currentToken = 3;
                return 3;
        case '0': case '1': case '2': case '3': case '4':
        case '5': case '6': case '7': case '8': case '9':
                parseNumber();
                jumpWhitespace();
                currentToken = 4;
                return 4;
        default:
                parseID();
                jumpWhitespace();
                currentToken = 5;
                return 5;
        }
}