decompiled_code:
__int64 __fastcall autodim_steps_get(__int64 a1, __int64 a2)
{
const char *v2; // rax
char *v3; // rax
char *v4; // rbp
unsigned int i; // er12
unsigned int v6; // er13
char *v8; // rax
char *v9; // rbx
__int64 v10; // rdx
char *ptr; // [rsp+0h] [rbp-48h]

v2 = (const char *)config_get(a2, "BACKLIGHT", "autodim_steps", "15/75 25/50 50/25 75/10");
v3 = strdup(v2);
ptr = v3;
if ( !v3 )
return (unsigned int)-12;
v4 = (char *)string_strip(v3);
for ( i = 0; ; ++i )
{
v8 = strchr(v4, 32);
v9 = v8;
if ( v8 )
{
*v8 = 0;
v9 = v8 + 1;
}
if ( strempty(v4) )
break;
v6 = autodim_realloc_steps(a1, i + 1);
if ( v6 )
goto LABEL_4;
v10 = *(_QWORD *)(a1 + 96) + 8LL * i;
if ( sscanf(v4, "%u/%u", v10, v10 + 4) != 2 )
{
logerr("Failed to parse autodim_steps config value\n");
v6 = -22;
goto LABEL_4;
}
if ( i && *(_DWORD *)(*(_QWORD *)(a1 + 96) + 8LL * (i - 1)) >= *(_DWORD *)(*(_QWORD *)(a1 + 96) + 8LL * i) )
{
logerr("Error: autodim_steps, previous step idle-seconds value bigger than adjacent value\n");
v6 = -22;
goto LABEL_4;
}
if ( !v9 )
{
++i;
break;
}
v4 = (char *)string_strip(v9);
}
*(_DWORD *)(a1 + 108) = i;
if ( *((int *)&cmdargs + 1) > 1 )
autodim_dump_steps(a1, "parsed-config");
if ( !(unsigned int)config_get_bool(a2, "BACKLIGHT", "autodim_smooth", 0LL) )
goto LABEL_23;
v6 = autodim_smoothen_steps(a1);
if ( !v6 )
{
if ( *((int *)&cmdargs + 1) > 1 )
autodim_dump_steps(a1, "smoothened");
LABEL_23:
v6 = autodim_optimize_steps(a1);
if ( !v6 && *((int *)&cmdargs + 1) > 1 )
autodim_dump_steps(a1, "optimized");
}
LABEL_4:
free(ptr);
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, __int64 a2)
{
const char *v2; // rax
char *v3; // rax
char *v4; // rbp
unsigned int i; // er12
unsigned int v6; // er13
char *v8; // rax
char *v9; // rbx
__int64 v10; // rdx
char *ptr; // [rsp+0h] [rbp-48h]

v2 = (const char *)config_get(a2, "BACKLIGHT", "autodim_steps", "15/75 25/50 50/25 75/10");
v3 = strdup(v2);
ptr = v3;
if ( !v3 )
return (unsigned int)-12;
v4 = (char *)string_strip(v3);
for ( i = 0; ; ++i )
{
v8 = strchr(v4, 32);
v9 = v8;
if ( v8 )
{
*v8 = 0;
v9 = v8 + 1;
}
if ( func0(v4) )
break;
v6 = func5(a1, i + 1);
if ( v6 )
goto LABEL_4;
v10 = *(_QWORD *)(a1 + 96) + 8LL * i;
if ( sscanf(v4, "%u/%u", v10, v10 + 4) != 2 )
{
logerr("Failed to parse autodim_steps config value\n");
v6 = -22;
goto LABEL_4;
}
if ( i && *(_DWORD *)(*(_QWORD *)(a1 + 96) + 8LL * (i - 1)) >= *(_DWORD *)(*(_QWORD *)(a1 + 96) + 8LL * i) )
{
logerr("Error: autodim_steps, previous step idle-seconds value bigger than adjacent value\n");
v6 = -22;
goto LABEL_4;
}
if ( !v9 )
{
++i;
break;
}
v4 = (char *)string_strip(v9);
}
*(_DWORD *)(a1 + 108) = i;
if ( *((int *)&cmdargs + 1) > 1 )
func8(a1, "parsed-config");
if ( !(unsigned int)config_get_bool(a2, "BACKLIGHT", "autodim_smooth", 0LL) )
goto LABEL_23;
v6 = func6(a1);
if ( !v6 )
{
if ( *((int *)&cmdargs + 1) > 1 )
func8(a1, "smoothened");
LABEL_23:
v6 = func7(a1);
if ( !v6 && *((int *)&cmdargs + 1) > 1 )
func8(a1, "optimized");
}
LABEL_4:
free(ptr);
return v6;
}

original_code:
static int autodim_steps_get(struct autodim *ad, struct config_file *config)
{
 char *string, *s, *next;
 unsigned int stepnr = 0;
 int ret = 0;
 string = strdup(config_get(config, "BACKLIGHT", "autodim_steps",
       "15/75 25/50 50/25 75/10"));
 if (!string)
  return -12;
 s = string_strip(string);
 while (1) {
  next = strchr(s, ' ');
  if (next) {
   next[0] = '\0';
   next++;
  }
  if (strempty(s))
   break;
  ret = autodim_realloc_steps(ad, stepnr + 1);
  if (ret)
   goto out;
  if (sscanf(s, "%u/%u", &ad->steps[stepnr].second,
      &ad->steps[stepnr].percent) != 2) {
   logerr("Failed to parse autodim_steps config value\n");
   ret = -22;
   goto out;
  }
  if (stepnr > 0) {
   if (ad->steps[stepnr - 1].second >= ad->steps[stepnr].second) {
    logerr("Error: autodim_steps, previous step idle-seconds "
           "value bigger than adjacent value\n");
    ret = -22;
    goto out;
   }
  }
  stepnr++;
  if (!next)
   break;
  s = string_strip(next);
 }
 ad->nr_steps = stepnr;
 if ((cmdargs.loglevel >= LOGLEVEL_DEBUG))
  autodim_dump_steps(ad, "parsed-config");
 if (config_get_bool(config, "BACKLIGHT", "autodim_smooth", 0)) {
  ret = autodim_smoothen_steps(ad);
  if (ret)
   goto out;
  if ((cmdargs.loglevel >= LOGLEVEL_DEBUG))
   autodim_dump_steps(ad, "smoothened");
 }
 ret = autodim_optimize_steps(ad);
 if (ret)
  goto out;
 if ((cmdargs.loglevel >= LOGLEVEL_DEBUG))
  autodim_dump_steps(ad, "optimized");
out:
 free(string);
 return ret;
}

canonical_original_code:
static int autodim_steps_get(struct autodim *ad, struct config_file *config)
{
 char *string; char *s; char *next;
 unsigned int stepnr = 0;
 int ret = 0;
 string = strdup(config_get(config, "BACKLIGHT", "autodim_steps",
       "15/75 25/50 50/25 75/10"));
 if (!string)
  return -12;
 s = string_strip(string);
 while (1) {
  next = strchr(s, ' ');
  if (next) {
   next[0] = '\0';
   next++;
  }
  if (strempty(s))
   break;
  ret = autodim_realloc_steps(ad, stepnr + 1);
  if (ret)
   goto out;
  if (sscanf(s, "%u/%u", &ad->steps[stepnr].second,
      &ad->steps[stepnr].percent) != 2) {
   logerr("Failed to parse autodim_steps config value\n");
   ret = -22;
   goto out;
  }
  if (stepnr > 0) {
   if (ad->steps[stepnr - 1].second >= ad->steps[stepnr].second) {
    logerr("Error: autodim_steps, previous step idle-seconds "
           "value bigger than adjacent value\n");
    ret = -22;
    goto out;
   }
  }
  stepnr++;
  if (!next)
   break;
  s = string_strip(next);
 }
 ad->nr_steps = stepnr;
 if ((cmdargs.loglevel >= LOGLEVEL_DEBUG))
  autodim_dump_steps(ad, "parsed-config");
 if (config_get_bool(config, "BACKLIGHT", "autodim_smooth", 0)) {
  ret = autodim_smoothen_steps(ad);
  if (ret)
   goto out;
  if ((cmdargs.loglevel >= LOGLEVEL_DEBUG))
   autodim_dump_steps(ad, "smoothened");
 }
 ret = autodim_optimize_steps(ad);
 if (ret)
  goto out;
 if ((cmdargs.loglevel >= LOGLEVEL_DEBUG))
  autodim_dump_steps(ad, "optimized");
out:
 free(string);
 return ret;
}

variable_types: struct config_file { struct config_section { struct config_file *file; char *name; struct config_item { struct config_section *section; char *name; char *value; struct config_item *next; } *item_hashtab[-1]; struct config_section *next; } *section_hashtab[-1]; } *

variable_types:
string
char *

variable_types:
next
char *

variable_types:
stepnr
unsigned int

variable_types: ret int


repo: mbuesch/pwrtray

decompiled_code:
__int64 __fastcall autodim_timer_start(__int64 a1)
{
__int64 result; // rax
unsigned int v2; // ebp

result = *(unsigned int *)(a1 + 84);
if ( (_DWORD)result )
{
if ( (unsigned int)result >= *(_DWORD *)(a1 + 108) )
return result;
v2 = *(_DWORD *)(*(_QWORD *)(a1 + 96) + 8LL * (unsigned int)result)
- *(_DWORD *)(*(_QWORD *)(a1 + 96) + 8LL * (unsigned int)(result - 1));
}
else
{
v2 = **(_DWORD **)(a1 + 96);
}
if ( *((int *)&cmdargs + 1) > 2 )
logdebug("autodim: Next event in %u sec\n", v2);
sleeptimer_set_timeout_relative(a1 + 24, 1000 * v2);
return sleeptimer_enqueue(a1 + 24);
}

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

result = *(unsigned int *)(a1 + 84);
if ( (_DWORD)result )
{
if ( (unsigned int)result >= *(_DWORD *)(a1 + 108) )
return result;
v2 = *(_DWORD *)(*(_QWORD *)(a1 + 96) + 8LL * (unsigned int)result)
- *(_DWORD *)(*(_QWORD *)(a1 + 96) + 8LL * (unsigned int)(result - 1));
}
else
{
v2 = **(_DWORD **)(a1 + 96);
}
if ( *((int *)&cmdargs + 1) > 2 )
logdebug("autodim: Next event in %u sec\n", v2);
sleeptimer_set_timeout_relative(a1 + 24, 1000 * v2);
return sleeptimer_enqueue(a1 + 24);
}

original_code:
static void autodim_timer_start(struct autodim *ad)
{
 unsigned int sec;
 if (ad->state == 0)
  sec = ad->steps[0].second;
 else if (ad->state < ad->nr_steps)
  sec = ad->steps[ad->state].second - ad->steps[ad->state - 1].second;
 else
  return;
 do { if ((cmdargs.loglevel >= LOGLEVEL_VERBOSE)) _logdebug("autodim: Next event in %u sec\n", sec); } while (0);
 sleeptimer_set_timeout_relative(&ad->timer, sec * 1000);
 sleeptimer_enqueue(&ad->timer);
}

variable_types: struct autodim { struct backlight { char *name; int (*min_brightness)(struct backlight *b); int (*max_brightness)(struct backlight *b); int (*brightness_step)(struct backlight *b); int (*current_brightness)(struct backlight *b); int (*set_brightness)(struct backlight *b, int value); int (*screen_lock)(struct backlight *b, int lock); int (*screen_is_locked)(struct backlight *b); void (*destroy)(struct backlight *b); int (*update)(struct backlight *b); unsigned int poll_interval; int autodim_enabled; int autodim_enabled_on_ac; int framebuffer_fd; int fb_blanked; struct sleeptimer { char *name; struct timespec { long tv_sec; long tv_nsec; } timeout; void (*callback)(struct sleeptimer *timer); unsigned long id; struct list_head { struct list_head *next; struct list_head *prev; } list; } timer; } *bl; int *fds; unsigned int nr_fds; struct sleeptimer timer; int suspended; unsigned int state; unsigned int bl_percent; unsigned int max_percent; struct autodim_step { unsigned int second; unsigned int percent; } *steps; unsigned int nr_allocated_steps; unsigned int nr_steps; } *

variable_types:
sec
unsigned int

return_type: void

user_defined_types: struct autodim { struct backlight *bl; int *fds; unsigned int nr_fds; struct sleeptimer timer; int suspended; unsigned int state; unsigned int bl_percent; unsigned int max_percent; struct autodim_step *steps; unsigned int nr_allocated_steps; unsigned int nr_steps; }

user_defined_types: struct backlight { char *name; int (*min_brightness)(struct backlight *b); int (*max_brightness)(struct backlight *b); int (*brightness_step)(struct backlight *b); int (*current_brightness)(struct backlight *b); int (*set_brightness)(struct backlight *b, int value); int (*screen_lock)(struct backlight *b, int lock); int (*screen_is_locked)(struct backlight *b); void (*destroy)(struct backlight *b); int (*update)(struct backlight *b); unsigned int poll_interval; int autodim_enabled; int autodim_enabled_on_ac; int framebuffer_fd; int fb_blanked; struct sleeptimer timer; }

user_defined_types: struct sleeptimer { char *name; struct timespec timeout; void (*callback)(struct sleeptimer *timer); unsigned long id; struct list_head list; }


decompiled_code:
__int64 __fastcall autodim_timer_callback(__int64 a1)
{
__int64 v1; // rbx

v1 = a1 - 24;
if ( *((int *)&cmdargs + 1) > 2 )
logdebug("autodim: timer triggered\n");
autodim_handle_state(v1);
return autodim_timer_start(v1);
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1)
{
__int64 v1; // rbx

v1 = a1 - 24;
if ( *((int *)&cmdargs + 1) > 2 )
logdebug("autodim: timer triggered\n");
func3(v1);
return func10(v1);
}

original_code:
static void autodim_timer_callback(struct sleeptimer *timer)
{
 struct autodim *ad = ({ __typeof__(((struct autodim *)0)->timer) *__mptr = (timer); (struct autodim *)((char *)__mptr - ((size_t)&((struct autodim *)0)->timer)); });
 do { if ((cmdargs.loglevel >= LOGLEVEL_VERBOSE)) _logdebug("autodim: timer triggered\n"); } while (0);
 autodim_handle_state(ad);
 autodim_timer_start(ad);
}

variable_types: struct sleeptimer { char *name; struct timespec { long tv_sec; long tv_nsec; } timeout; void (*callback)(struct sleeptimer *timer); unsigned long id; struct list_head { struct list_head *next; struct list_head *prev; } list; } *

user_defined_types: struct autodim_step { unsigned int second; unsigned int percent; }


decompiled_code:
__int64 __fastcall autodim_timer_stop(__int64 a1)
{
return sleeptimer_dequeue(a1 + 24);
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1)
{
return sleeptimer_dequeue(a1 + 24);
}

original_code:
static void autodim_timer_stop(struct autodim *ad)
{
 sleeptimer_dequeue(&ad->timer);
}

user_defined_types: struct timespec { long tv_sec; long tv_nsec; }

user_defined_types: struct list_head { struct list_head *next; struct list_head *prev; }


decompiled_code:
void *autodim_alloc()
{
return zalloc(0x70uLL);
}

canonical_decompiled_code:
void *func13()
{
return func1(0x70uLL);
}

original_code:
struct autodim * autodim_alloc(void)
{
 struct autodim *ad;
 ad = zalloc(sizeof(*ad));
 if (!ad)
  return ((void *)0);
 return ad;
}

canonical_original_code:
struct autodim *autodim_alloc()
{
 struct autodim *ad;
 ad = zalloc(sizeof(*ad));
 if (!ad)
  return ((void *)0);
 return ad;
}

return_type: struct autodim { struct backlight { char *name; int (*min_brightness)(struct backlight *b); int (*max_brightness)(struct backlight *b); int (*brightness_step)(struct backlight *b); int (*current_brightness)(struct backlight *b); int (*set_brightness)(struct backlight *b, int value); int (*screen_lock)(struct backlight *b, int lock); int (*screen_is_locked)(struct backlight *b); void (*destroy)(struct backlight *b); int (*update)(struct backlight *b); unsigned int poll_interval; int autodim_enabled; int autodim_enabled_on_ac; int framebuffer_fd; int fb_blanked; struct sleeptimer { char *name; struct timespec { long tv_sec; long tv_nsec; } timeout; void (*callback)(struct sleeptimer *timer); unsigned long id; struct list_head { struct list_head *next; struct list_head *prev; } list; } timer; } *bl; int *fds; unsigned int nr_fds; struct sleeptimer timer; int suspended; unsigned int state; unsigned int bl_percent; unsigned int max_percent; struct autodim_step { unsigned int second; unsigned int percent; } *steps; unsigned int nr_allocated_steps; unsigned int nr_steps; } *


decompiled_code:
void __fastcall devicelock_dummy_destroy(void *a1)
{
free(a1);
}

canonical_decompiled_code:
void __fastcall func0(void *a1)
{
free(a1);
}

original_code:
static void devicelock_dummy_destroy(struct devicelock *s)
{
 struct devicelock_dummy *ld = ({ __typeof__(((struct devicelock_dummy *)0)->devicelock) *__mptr = (s); (struct devicelock_dummy *)((char *)__mptr - ((size_t)&((struct devicelock_dummy *)0)->devicelock)); });
 free(ld);
}

variable_types: struct devicelock { char *name; void (*destroy)(struct devicelock *s); void (*event)(struct devicelock *s); } *

variable_types: struct devicelock_dummy { struct devicelock { char *name; void (*destroy)(struct devicelock *s); void (*event)(struct devicelock *s); } devicelock; } *

user_defined_types: struct devicelock_dummy { struct devicelock devicelock; }


decompiled_code:
_QWORD *devicelock_dummy_probe()
{
_QWORD *v0; // rax
_QWORD *v1; // rbx

v0 = zalloc(0x18uLL);
v1 = v0;
if ( v0 )
{
devicelock_init(v0, "dummy");
v1[1] = devicelock_dummy_destroy;
}
return v1;
}

canonical_decompiled_code:
_QWORD *func2()
{
_QWORD *v0; // rax
_QWORD *v1; // rbx

v0 = func1(0x18uLL);
v1 = v0;
if ( v0 )
{
devicelock_init(v0, "dummy");
v1[1] = devicelock_dummy_destroy;
}
return v1;
}

original_code:
static struct devicelock * devicelock_dummy_probe(void)
{
 struct devicelock_dummy *ld;
 ld = zalloc(sizeof(*ld));
 if (!ld)
  return ((void *)0);
 devicelock_init(&ld->devicelock, "dummy");
 ld->devicelock.destroy = devicelock_dummy_destroy;
 return &ld->devicelock;
}

canonical_original_code:
static struct devicelock *devicelock_dummy_probe()
{
 struct devicelock_dummy *ld;
 ld = zalloc(sizeof(*ld));
 if (!ld)
  return ((void *)0);
 devicelock_init(&ld->devicelock, "dummy");
 ld->devicelock.destroy = devicelock_dummy_destroy;
 return &ld->devicelock;
}

return_type: struct devicelock { char *name; void (*destroy)(struct devicelock *s); void (*event)(struct devicelock *s); } *


usage

decompiled_code:
int usage()
{
return puts("Usage: pwrtray-xevrep PID SIGNAL GRACEPERIOD");
}

canonical_decompiled_code:
int func7()
{
return puts("Usage: pwrtray-xevrep PID SIGNAL GRACEPERIOD");
}

original_code:
static void usage(void)
{
 printf("Usage: pwrtray-xevrep PID SIGNAL GRACEPERIOD\n");
}

canonical_original_code:
static void usage()
{
 printf("Usage: pwrtray-xevrep PID SIGNAL GRACEPERIOD\n");
}


decompiled_code:
_BOOL8 __fastcall timeval_after(_QWORD *a1, _QWORD *a2)
{
_BOOL8 result; // rax

result = 1LL;
if ( *a1 <= *a2 )
{
result = 0LL;
if ( *a1 == *a2 )
result = a1[1] > a2[1];
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func10(_QWORD *a1, _QWORD *a2)
{
_BOOL8 result; // rax

result = 1LL;
if ( *a1 <= *a2 )
{
result = 0LL;
if ( *a1 == *a2 )
result = a1[1] > a2[1];
}
return result;
}

original_code:
int timeval_after(const struct timeval *a, const struct timeval *b)
{
 if (a->tv_sec > b->tv_sec)
  return 1;
 if ((a->tv_sec == b->tv_sec) && (a->tv_usec > b->tv_usec))
  return 1;
 return 0;
}

canonical_original_code:
int timeval_after(struct timeval *a, struct timeval *b)
{
 if (a->tv_sec > b->tv_sec)
  return 1;
 if ((a->tv_sec == b->tv_sec) && (a->tv_usec > b->tv_usec))
  return 1;
 return 0;
}

variable_types: struct timeval { long tv_sec; long tv_usec; } *

variable_types:
b
struct timeval *

return_type: int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
Display *v4; // rbx
int v5; // ebp
int v6; // ebp
unsigned int v7; // eax
XEvent v8; // [rsp+0h] [rbp-188h] BYREF
sigset_t set; // [rsp+C0h] [rbp-C8h] BYREF
__pid_t v10; // [rsp+14Ch] [rbp-3Ch] BYREF
int v11; // [rsp+150h] [rbp-38h] BYREF
int v12; // [rsp+154h] [rbp-34h] BYREF
int v13; // [rsp+158h] [rbp-30h] BYREF
int v14[3]; // [rsp+15Ch] [rbp-2Ch] BYREF
int *v15; // [rsp+168h] [rbp-20h]

v14[0] = 0;
if ( argc == 4 )
{
if ( sscanf(argv[1], "%d", &v10) == 1
&& sscanf(argv[2], "%d", &report_signal) == 1
&& sscanf(argv[3], "%u", &grace_period_ms) == 1 )
{
report_pid = v10;
v4 = XOpenDisplay(0LL);
if ( v4 || (v4 = XOpenDisplay(":0")) != 0LL )
{
if ( XQueryExtension(v4, "XInputExtension", &v11, &v13, &v12) )
{
sigemptyset(&set);
v5 = sigprocmask(2, &set, 0LL);
v6 = install_sighandler(2, (void (*)(int))signal_handler) | v5;
if ( install_sighandler(15, (void (*)(int))signal_handler) | v6 )
{
fwrite("pwrtray-xevrep: Failed to setup signal handlers\n", 1uLL, 0x30uLL, stderr);
}
else
{
LOBYTE(v14[0]) |= 0x4Eu;
*(_WORD *)((char *)v14 + 1) |= 0x198u;
v14[1] = 0;
v14[2] = 4;
v15 = v14;
v7 = XISelectEvents();
if ( !v7 )
{
puts("pwrtray-xevrep: Monitoring X11 input events...");
while ( 1 )
{
XNextEvent(v4, &v8);
if ( XGetEventData(v4, (XGenericEventCookie *)&v8) && v8.type == 35 && v8.xgeneric.extension == v11 )
report_event();
XFreeEventData(v4, (XGenericEventCookie *)&v8);
}
}
fprintf(stderr, "pwrtray-xevrep: Failed to set event mask (%d)\n", v7);
}
}
else
{
fwrite("pwrtray-xevrep: X Input extension not available.\n", 1uLL, 0x31uLL, stderr);
}
}
else
{
fwrite("pwrtray-xevrep: Failed to open DISPLAY\n", 1uLL, 0x27uLL, stderr);
}
}
else
{
usage();
}
}
else
{
usage();
}
return 1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
Display *v4; // rbx
int v5; // ebp
int v6; // ebp
unsigned int v7; // eax
XEvent v8; // [rsp+0h] [rbp-188h] BYREF
sigset_t set; // [rsp+C0h] [rbp-C8h] BYREF
__pid_t v10; // [rsp+14Ch] [rbp-3Ch] BYREF
int v11; // [rsp+150h] [rbp-38h] BYREF
int v12; // [rsp+154h] [rbp-34h] BYREF
int v13; // [rsp+158h] [rbp-30h] BYREF
int v14[3]; // [rsp+15Ch] [rbp-2Ch] BYREF
int *v15; // [rsp+168h] [rbp-20h]

v14[0] = 0;
if ( argc == 4 )
{
if ( sscanf(argv[1], "%d", &v10) == 1
&& sscanf(argv[2], "%d", &report_signal) == 1
&& sscanf(argv[3], "%u", &grace_period_ms) == 1 )
{
report_pid = v10;
v4 = XOpenDisplay(0LL);
if ( v4 || (v4 = XOpenDisplay(":0")) != 0LL )
{
if ( XQueryExtension(v4, "XInputExtension", &v11, &v13, &v12) )
{
sigemptyset(&set);
v5 = sigprocmask(2, &set, 0LL);
v6 = func9(2, (void (*)(int))signal_handler) | v5;
if ( func9(15, (void (*)(int))signal_handler) | v6 )
{
fwrite("pwrtray-xevrep: Failed to setup signal handlers\n", 1uLL, 0x30uLL, stderr);
}
else
{
LOBYTE(v14[0]) |= 0x4Eu;
*(_WORD *)((char *)v14 + 1) |= 0x198u;
v14[1] = 0;
v14[2] = 4;
v15 = v14;
v7 = XISelectEvents();
if ( !v7 )
{
puts("pwrtray-xevrep: Monitoring X11 input events...");
while ( 1 )
{
XNextEvent(v4, &v8);
if ( XGetEventData(v4, (XGenericEventCookie *)&v8) && v8.type == 35 && v8.xgeneric.extension == v11 )
func12();
XFreeEventData(v4, (XGenericEventCookie *)&v8);
}
}
fprintf(stderr, "pwrtray-xevrep: Failed to set event mask (%d)\n", v7);
}
}
else
{
fwrite("pwrtray-xevrep: X Input extension not available.\n", 1uLL, 0x31uLL, stderr);
}
}
else
{
fwrite("pwrtray-xevrep: Failed to open DISPLAY\n", 1uLL, 0x27uLL, stderr);
}
}
else
{
func7();
}
}
else
{
func7();
}
return 1;
}

original_code:
int main(int argc, char **argv)
{
 Window window;
 int res;
 sigset_t sigset;
 display = XOpenDisplay(((void *)0));
 if (!display) {
  display = XOpenDisplay(":0");
  if (!display) {
   fprintf(stderr, "Failed to open DISPLAY\n");
   return 1;
  }
 }
 window = ((&((_XPrivDisplay)(display))->screens[(((_XPrivDisplay)(display))->default_screen)])->root);
 sigemptyset(&sigset);
 res = sigprocmask(2, &sigset, ((void *)0));
 res |= install_sighandler(2, signal_handler);
 res |= install_sighandler(15, signal_handler);
 if (res) {
  fprintf(stderr, "Failed to setup signal handlers\n");
  return 1;
 }
 res = XGrabPointer(display, window,
      0, 0, 1, 1,
      0L, 0L, 0L);
 if (res != 0) {
  fprintf(stderr, "Failed to grab pointer (res=%d)\n", res);
  return 1;
 }
 res = XGrabKeyboard(display, window,
       0, 1, 1,
       0L);
 if (res != 0) {
  fprintf(stderr, "Failed to grab keyboard (res=%d)\n", res);
  XUngrabPointer(display, 0L);
  return 1;
 }
 XFlush(display);
 printf("pwrtray-xlock: locked\n");
 while (1)
  sleep(0xFFFF);
}

canonical_original_code:
int main(int argc, char **argv)
{
 unsigned long window;
 int res;
 struct __sigset_t sigset;
 display = XOpenDisplay(((void *)0));
 if (!display) {
  display = XOpenDisplay(":0");
  if (!display) {
   fprintf(stderr, "Failed to open DISPLAY\n");
   return 1;
  }
 }
 window = ((&((_XPrivDisplay)(display))->screens[(((_XPrivDisplay)(display))->default_screen)])->root);
 sigemptyset(&sigset);
 res = sigprocmask(2, &sigset, ((void *)0));
 res |= install_sighandler(2, signal_handler);
 res |= install_sighandler(15, signal_handler);
 if (res) {
  fprintf(stderr, "Failed to setup signal handlers\n");
  return 1;
 }
 res = XGrabPointer(display, window,
      0, 0, 1, 1,
      0L, 0L, 0L);
 if (res != 0) {
  fprintf(stderr, "Failed to grab pointer (res=%d)\n", res);
  return 1;
 }
 res = XGrabKeyboard(display, window,
       0, 1, 1,
       0L);
 if (res != 0) {
  fprintf(stderr, "Failed to grab keyboard (res=%d)\n", res);
  XUngrabPointer(display, 0L);
  return 1;
 }
 XFlush(display);
 printf("pwrtray-xlock: locked\n");
 while (1)
  sleep(0xFFFF);
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
window
unsigned long

variable_types: res int

variable_types: struct __sigset_t { unsigned long __val[-1]; }


decompiled_code:
__int64 __fastcall parse_commandline(int argc, char **argv)
{
int v2; // eax
__int64 result; // rax
int longind[7]; // [rsp+Ch] [rbp-1Ch] BYREF

while ( 2 )
{
v2 = getopt_long(argc, argv, "hBP:l:L:f", &long_options_0, longind);
if ( v2 == -1 )
return 0LL;
switch ( v2 )
{
case 'B':
cmdargs = 1;
continue;
case 'L':
*((_QWORD *)&cmdargs + 1) = optarg;
continue;
case 'P':
*((_QWORD *)&cmdargs + 2) = optarg;
continue;
case 'f':
*(&cmdargs + 6) = 1;
continue;
case 'h':
usage(stdout, (unsigned int)argc, (const char **)argv);
result = 1LL;
break;
case 'l':
if ( sscanf(optarg, "%d", (unsigned int)((_DWORD)&cmdargs + 4)) == 1 )
continue;
fwrite("Failed to parse --loglevel argument.\n", 1uLL, 0x25uLL, stderr);
result = 0xFFFFFFFFLL;
break;
default:
result = 0xFFFFFFFFLL;
break;
}
break;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int argc, char **argv)
{
int v2; // eax
__int64 result; // rax
int longind[7]; // [rsp+Ch] [rbp-1Ch] BYREF

while ( 2 )
{
v2 = getopt_long(argc, argv, "hBP:l:L:f", &long_options_0, longind);
if ( v2 == -1 )
return 0LL;
switch ( v2 )
{
case 'B':
cmdargs = 1;
continue;
case 'L':
*((_QWORD *)&cmdargs + 1) = optarg;
continue;
case 'P':
*((_QWORD *)&cmdargs + 2) = optarg;
continue;
case 'f':
*(&cmdargs + 6) = 1;
continue;
case 'h':
func0(stdout, (unsigned int)argc, (const char **)argv);
result = 1LL;
break;
case 'l':
if ( sscanf(optarg, "%d", (unsigned int)((_DWORD)&cmdargs + 4)) == 1 )
continue;
fwrite("Failed to parse --loglevel argument.\n", 1uLL, 0x25uLL, stderr);
result = 0xFFFFFFFFLL;
break;
default:
result = 0xFFFFFFFFLL;
break;
}
break;
}
return result;
}

original_code:
int parse_commandline(int argc, char **argv)
{
 static struct option long_options[] = {
  { "help", 0, 0, 'h' },
  { "background", 0, 0, 'B' },
  { "pidfile", 1, 0, 'P' },
  { "loglevel", 1, 0, 'l' },
  { "logfile", 1, 0, 'L' },
  { "force", 0, 0, 'f' },
 };
 int c, idx;
 while (1) {
  c = getopt_long(argc, argv, "hBP:l:L:f",
    long_options, &idx);
  if (c == -1)
   break;
  switch (c) {
  case 'h':
   usage(stdout, argc, argv);
   return 1;
  case 'B':
   cmdargs.background = 1;
   break;
  case 'P':
   cmdargs.pidfile = optarg;
   break;
  case 'l':
   if (sscanf(optarg, "%d", &cmdargs.loglevel) != 1) {
    fprintf(stderr, "Failed to parse --loglevel argument.\n");
    return -1;
   }
   break;
  case 'L':
   cmdargs.logfile = optarg;
   break;
  case 'f':
   cmdargs.force = 1;
   break;
  default:
   return -1;
  }
 }
 return 0;
}

canonical_original_code:
int parse_commandline(int argc, char **argv)
{
 static struct option long_options[] = {
  { "help", 0, 0, 'h' },
  { "background", 0, 0, 'B' },
  { "pidfile", 1, 0, 'P' },
  { "loglevel", 1, 0, 'l' },
  { "logfile", 1, 0, 'L' },
  { "force", 0, 0, 'f' },
 };
 int c; int idx;
 while (1) {
  c = getopt_long(argc, argv, "hBP:l:L:f",
    long_options, &idx);
  if (c == -1)
   break;
  switch (c) {
  case 'h':
   usage(stdout, argc, argv);
   return 1;
  case 'B':
   cmdargs.background = 1;
   break;
  case 'P':
   cmdargs.pidfile = optarg;
   break;
  case 'l':
   if (sscanf(optarg, "%d", &cmdargs.loglevel) != 1) {
    fprintf(stderr, "Failed to parse --loglevel argument.\n");
    return -1;
   }
   break;
  case 'L':
   cmdargs.logfile = optarg;
   break;
  case 'f':
   cmdargs.force = 1;
   break;
  default:
   return -1;
  }
 }
 return 0;
}

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

variable_types: c int

variable_types: idx int


decompiled_code:
_BOOL8 __fastcall list_empty(_QWORD *a1)
{
return *a1 == (_QWORD)a1;
}

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

original_code:
static inline int list_empty(const struct list_head *head)
{
 return head->next == head;
}

canonical_original_code:
static inline int list_empty(struct list_head *head)
{
 return head->next == head;
}

variable_types: struct list_head { struct list_head *next; struct list_head *prev; } *


strempty

decompiled_code:
_BOOL8 __fastcall strempty(_BYTE *a1)
{
_BOOL8 result; // rax

result = 1LL;
if ( a1 )
result = *a1 == 0;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(_BYTE *a1)
{
_BOOL8 result; // rax

result = 1LL;
if ( a1 )
result = *a1 == 0;
return result;
}

original_code:
static inline int strempty(const char *s)
{
 return (s == ((void *)0)) || (s[0] == '\0');
}

canonical_original_code:
static inline int strempty(char *s)
{
 return (s == ((void *)0)) || (s[0] == '\0');
}

variable_types:
s
char *


canonical_decompiled_code:
_BOOL8 __fastcall func5(_QWORD *a1)
{
return *a1 == (_QWORD)a1;
}


original_code:
int
pilot_connector_wait(struct pilot_connector *thiz)
{
 int ret = 0;
 int maxfd = 0;
 fd_set rfds;
 if (thiz->fd >= 0)
 {
  ((void) (((&rfds)->__fds_bits)[((thiz->fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((thiz->fd) % (8 * (int) sizeof (__fd_mask)))))));
  maxfd = thiz->fd + 1;
  do { thiz->prepare_wait.slots.it = thiz->prepare_wait.slots.next; while (thiz->prepare_wait.slots.it) { typeof(thiz->prepare_wait.slots) *slot_it = thiz->prepare_wait.slots.it; thiz->prepare_wait.slots.it = thiz->prepare_wait.slots.it->next; if (slot_it->action(slot_it->dest, thiz) < 0) break; } } while(0);
  if (thiz->distribut == 0)
  {
   ret = select(maxfd, &rfds, ((void *)0), ((void *)0), ((void *)0));
   if (ret > 0 && ((((&rfds)->__fds_bits)[((thiz->fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((thiz->fd) % (8 * (int) sizeof (__fd_mask)))))) != 0))
   {
    fprintf(stderr, "%s ""on %d""\n", __FUNCTION__,thiz->fd);
    thiz->distribut = 1;
   }
  }
  if (thiz->distribut) {
   do { thiz->dispatch_events.slots.it = thiz->dispatch_events.slots.next; while (thiz->dispatch_events.slots.it) { typeof(thiz->dispatch_events.slots) *slot_it = thiz->dispatch_events.slots.it; thiz->dispatch_events.slots.it = thiz->dispatch_events.slots.it->next; if (slot_it->action(slot_it->dest, thiz) < 0) break; } } while(0);
  }
  ret = thiz->distribut;
  thiz->distribut = 0;
 }
 else
  ret = -1;
 return ret;
}

canonical_original_code:
int pilot_connector_wait(struct pilot_connector *thiz)
{
 int ret = 0;
 int maxfd = 0;
 struct fd_set rfds;
 if (thiz->fd >= 0)
 {
  ((void) (((&rfds)->__fds_bits)[((thiz->fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((thiz->fd) % (8 * (int) sizeof (__fd_mask)))))));
  maxfd = thiz->fd + 1;
  do { thiz->prepare_wait.slots.it = thiz->prepare_wait.slots.next; while (thiz->prepare_wait.slots.it) { typeof(thiz->prepare_wait.slots) *slot_it = thiz->prepare_wait.slots.it; thiz->prepare_wait.slots.it = thiz->prepare_wait.slots.it->next; if (slot_it->action(slot_it->dest, thiz) < 0) break; } } while(0);
  if (thiz->distribut == 0)
  {
   ret = select(maxfd, &rfds, ((void *)0), ((void *)0), ((void *)0));
   if (ret > 0 && ((((&rfds)->__fds_bits)[((thiz->fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((thiz->fd) % (8 * (int) sizeof (__fd_mask)))))) != 0))
   {
    fprintf(stderr, "%s ""on %d""\n", __FUNCTION__,thiz->fd);
    thiz->distribut = 1;
   }
  }
  if (thiz->distribut) {
   do { thiz->dispatch_events.slots.it = thiz->dispatch_events.slots.next; while (thiz->dispatch_events.slots.it) { typeof(thiz->dispatch_events.slots) *slot_it = thiz->dispatch_events.slots.it; thiz->dispatch_events.slots.it = thiz->dispatch_events.slots.it->next; if (slot_it->action(slot_it->dest, thiz) < 0) break; } } while(0);
  }
  ret = thiz->distribut;
  thiz->distribut = 0;
 }
 else
  ret = -1;
 return ret;
}

variable_types: struct pilot_connector { struct pilot_application { struct _pilot_list_pilot_connector { struct pilot_connector *item; struct _pilot_list_pilot_connector *next; struct _pilot_list_pilot_connector *it; } connectors; struct _pilot_list_pilot_option { struct pilot_option { char *name; char *value; } *item; struct _pilot_list_pilot_option *next; struct _pilot_list_pilot_option *it; } options; struct fd_set { long __fds_bits[-1]; } rfds; int maxfd; int signal_pipe[2]; char running; char dispatch; } *application; int fd; char distribut; struct { struct pilot_connector_prepare_wait_slot { void *dest; struct pilot_connector_prepare_wait_slot *next; struct pilot_connector_prepare_wait_slot *it; int (*action)(void *, struct pilot_connector *); } slots; } prepare_wait; struct { struct pilot_connector_dispatch_events_slot { void *dest; struct pilot_connector_dispatch_events_slot *next; struct pilot_connector_dispatch_events_slot *it; int (*action)(void *, struct pilot_connector *); } slots; } dispatch_events; struct { struct pilot_connector_disconnect_slot { void *dest; struct pilot_connector_disconnect_slot *next; struct pilot_connector_disconnect_slot *it; int (*action)(void *, struct pilot_connector *); } slots; } disconnect; } *

variable_types: ret int

variable_types: maxfd int

variable_types:
rfds
struct fd_set { long __fds_bits[-1]; }

user_defined_types: struct pilot_connector { struct pilot_application *application; int fd; char distribut; struct { struct pilot_connector_prepare_wait_slot slots; } prepare_wait; struct { struct pilot_connector_dispatch_events_slot slots; } dispatch_events; struct { struct pilot_connector_disconnect_slot slots; } disconnect; }

user_defined_types: struct pilot_application { struct _pilot_list_pilot_connector connectors; struct _pilot_list_pilot_option options; struct fd_set rfds; int maxfd; int signal_pipe[2]; char running; char dispatch; }

user_defined_types: struct _pilot_list_pilot_option { struct pilot_option *item; struct _pilot_list_pilot_option *next; struct _pilot_list_pilot_option *it; }


decompiled_code:
void *__fastcall pilot_application_create(int a1, __int64 a2)
{
void *v2; // rbp
const char **v3; // rbx

v2 = malloc(0xC0uLL);
memset(v2, 0, 0xC0uLL);
if ( a1 > 1 )
{
v3 = (const char **)(a2 + 8);
do
{
if ( (unsigned int)pilot_application_storeopt((__int64)v2, *v3) )
break;
++v3;
}
while ( v3 != (const char **)(a2 + 8LL * (unsigned int)(a1 - 2) + 16) );
}
return v2;
}

canonical_decompiled_code:
void *__fastcall func5(int a1, __int64 a2)
{
void *v2; // rbp
const char **v3; // rbx

v2 = malloc(0xC0uLL);
memset(v2, 0, 0xC0uLL);
if ( a1 > 1 )
{
v3 = (const char **)(a2 + 8);
do
{
if ( (unsigned int)func3((__int64)v2, *v3) )
break;
++v3;
}
while ( v3 != (const char **)(a2 + 8LL * (unsigned int)(a1 - 2) + 16) );
}
return v2;
}

original_code:
struct pilot_application *
pilot_application_create(int argc, const char **argv)
{
 struct pilot_application *application;
 application = malloc(sizeof(*application));
 memset(application, 0, sizeof(*application));
 int i;
 for (i = 1; i < argc; i++)
 {
  if (_pilot_application_storeopt(application, argv[i]))
   break;
 }
 return application;
}

canonical_original_code:
struct pilot_application *pilot_application_create(int argc, char **argv)
{
 struct pilot_application *application;
 application = malloc(sizeof(*application));
 memset(application, 0, sizeof(*application));
 int i;
 for (i = 1; i < argc; i++)
 {
  if (_pilot_application_storeopt(application, argv[i]))
   break;
 }
 return application;
}

variable_types: struct pilot_application { struct _pilot_list_pilot_connector { struct pilot_connector { struct pilot_application *application; int fd; char distribut; struct { struct pilot_connector_prepare_wait_slot { void *dest; struct pilot_connector_prepare_wait_slot *next; struct pilot_connector_prepare_wait_slot *it; int (*action)(void *, struct pilot_connector *); } slots; } prepare_wait; struct { struct pilot_connector_dispatch_events_slot { void *dest; struct pilot_connector_dispatch_events_slot *next; struct pilot_connector_dispatch_events_slot *it; int (*action)(void *, struct pilot_connector *); } slots; } dispatch_events; struct { struct pilot_connector_disconnect_slot { void *dest; struct pilot_connector_disconnect_slot *next; struct pilot_connector_disconnect_slot *it; int (*action)(void *, struct pilot_connector *); } slots; } disconnect; } *item; struct _pilot_list_pilot_connector *next; struct _pilot_list_pilot_connector *it; } connectors; struct _pilot_list_pilot_option { struct pilot_option { char *name; char *value; } *item; struct _pilot_list_pilot_option *next; struct _pilot_list_pilot_option *it; } options; struct fd_set { long __fds_bits[-1]; } rfds; int maxfd; int signal_pipe[2]; char running; char dispatch; } *

variable_types: i int

user_defined_types: struct _pilot_list_pilot_connector { struct pilot_connector *item; struct _pilot_list_pilot_connector *next; struct _pilot_list_pilot_connector *it; }


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

v2 = (_QWORD *)*((_QWORD *)ptr + 1);
for ( *((_QWORD *)ptr + 2) = v2; v2; v2 = (_QWORD *)*((_QWORD *)ptr + 2) )
{
*((_QWORD *)ptr + 2) = v2[1];
free(v2);
}
free(ptr);
}

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

v2 = (_QWORD *)*((_QWORD *)ptr + 1);
for ( *((_QWORD *)ptr + 2) = v2; v2; v2 = (_QWORD *)*((_QWORD *)ptr + 2) )
{
*((_QWORD *)ptr + 2) = v2[1];
free(v2);
}
free(ptr);
}

original_code:
void
pilot_application_destroy(struct pilot_application *application)
{
 do { application->connectors.it = application->connectors.next; while (application->connectors.it) { typeof (application->connectors) *tmp = application->connectors.it; application->connectors.it = application->connectors.it->next; free(tmp); } } while(0);
 free(application);
}

user_defined_types: struct pilot_option { char *name; char *value; }

user_defined_types: struct pilot_connector_prepare_wait_slot { void *dest; struct pilot_connector_prepare_wait_slot *next; struct pilot_connector_prepare_wait_slot *it; int (*action)(void *, struct pilot_connector *); }

user_defined_types: struct pilot_connector_dispatch_events_slot { void *dest; struct pilot_connector_dispatch_events_slot *next; struct pilot_connector_dispatch_events_slot *it; int (*action)(void *, struct pilot_connector *); }

user_defined_types: struct pilot_connector_disconnect_slot { void *dest; struct pilot_connector_disconnect_slot *next; struct pilot_connector_disconnect_slot *it; int (*action)(void *, struct pilot_connector *); }


decompiled_code:
__int64 __fastcall pilot_application_addconnector(__int64 a1, __int64 a2)
{
__int64 v2; // rbx
_QWORD *v3; // rax

do
{
v2 = a1;
a1 = *(_QWORD *)(a1 + 8);
}
while ( a1 );
v3 = malloc(0x18uLL);
*(_QWORD *)(v2 + 8) = v3;
v3[1] = 0LL;
v3[2] = 0LL;
*v3 = a2;
return 0LL;
}

original_code:
int
pilot_application_addconnector(struct pilot_application *application,
      struct pilot_connector *connector)
{
 do { typeof (application->connectors) *it = &application->connectors; while (it->next) it = it->next; it->next = malloc(sizeof(typeof (application->connectors))); it = it->next; memset(it, 0, sizeof(typeof (application->connectors))); it->item = connector; } while(0);
 return 0;
}

variable_types:
connector
struct pilot_connector *


decompiled_code:
__int64 __fastcall pilot_application_removeconnector(_QWORD *a1, __int64 a2)
{
_QWORD *v2; // rbx

while ( 1 )
{
v2 = a1;
a1 = (_QWORD *)a1[1];
if ( !a1 )
break;
if ( *a1 == a2 )
{
v2[2] = a1;
v2[1] = a1[1];
free(a1);
v2[2] = 0LL;
return 0LL;
}
}
return 0LL;
}

original_code:
int
pilot_application_removeconnector(struct pilot_application *application,
      struct pilot_connector *connector)
{
 do { typeof (application->connectors) *it = &application->connectors; while (it->next && it->next->item != connector) it = it->next; if (it->next) { it->it = it->next; it->next = it->next->next; free(it->it); it->it = ((void *)0); } } while(0);
 return 0;
}


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

v2 = *(_QWORD **)ptr;
if ( v2 )
pilot_application_removeconnector(v2, (__int64)ptr);
free(ptr);
}

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

v2 = *(_QWORD **)ptr;
if ( v2 )
func8(v2, (__int64)ptr);
free(ptr);
}

original_code:
void
pilot_connector_destroy(struct pilot_connector *thiz)
{
 if (thiz->application != ((void *)0))
  pilot_application_removeconnector(thiz->application, thiz);
 free(thiz);
}


decompiled_code:
__int64 __fastcall pilot_application_dispatchevents(__int64 a1)
{
__int64 *v1; // rax

*(_QWORD *)(a1 + 16) = *(_QWORD *)(a1 + 8);
do
{
v1 = *(__int64 **)(a1 + 16);
if ( !v1 )
break;
*(_QWORD *)(a1 + 16) = v1[1];
}
while ( (int)pilot_application_dispatch_item_events(a1, *v1) >= 0 );
return 0LL;
}

original_code:
int
pilot_application_dispatchevents(struct pilot_application *application)
{
 do { application->connectors.it = application->connectors.next; while (application->connectors.it) { typeof (application->connectors) *it = application->connectors.it; application->connectors.it = application->connectors.it->next; if (_pilot_application_dispatch_item_events(application, it->item) < 0) break; } } while(0);
 return 0;
}


decompiled_code:
__int64 __fastcall pilot_application_check(__int64 a1)
{
__int64 *v1; // rax
__int64 v2; // rbp
__int64 *v3; // rax

*(_DWORD *)(a1 + 176) = 0;
memset((void *)(a1 + 48), 0, 0x80uLL);
*(_QWORD *)(a1 + 16) = *(_QWORD *)(a1 + 8);
do
{
v1 = *(__int64 **)(a1 + 16);
if ( !v1 )
break;
*(_QWORD *)(a1 + 16) = v1[1];
}
while ( (int)pilot_application_fill_fdset(a1, *v1) >= 0 );
if ( *(_DWORD *)(a1 + 176) )
{
LODWORD(v2) = (*(_BYTE *)(a1 + 188) & 2) != 0;
fprintf(stderr, "%s wait %d\n", "pilot_application_check", (*(_BYTE *)(a1 + 188) & 2) != 0);
if ( (_DWORD)v2 )
{
*(_BYTE *)(a1 + 188) &= 0xFDu;
fprintf(stderr, "%s continue %d\n", "pilot_application_check", 1LL);
}
else
{
v2 = (unsigned int)select(*(_DWORD *)(a1 + 176), (fd_set *)(a1 + 48), 0LL, 0LL, 0LL);
*(_BYTE *)(a1 + 188) &= 0xFDu;
fprintf(stderr, "%s continue %d\n", "pilot_application_check", v2);
if ( (int)v2 <= 0 )
return (unsigned int)v2;
}
*(_QWORD *)(a1 + 16) = *(_QWORD *)(a1 + 8);
do
{
v3 = *(__int64 **)(a1 + 16);
if ( !v3 )
break;
*(_QWORD *)(a1 + 16) = v3[1];
}
while ( (int)pilot_application_check_fdset(a1, *v3) >= 0 );
}
else
{
*(_BYTE *)(a1 + 188) &= 0xFEu;
LODWORD(v2) = -1;
}
return (unsigned int)v2;
}

original_code:
int
pilot_application_check(struct pilot_application *application)
{
 int ret = 0;
 application->maxfd = 0;
 do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&application->rfds)->__fds_bits)[0]) : "memory"); } while (0);
 do { application->connectors.it = application->connectors.next; while (application->connectors.it) { typeof (application->connectors) *it = application->connectors.it; application->connectors.it = application->connectors.it->next; if (_pilot_application_fill_fdset(application, it->item) < 0) break; } } while(0);
 if (application->maxfd == 0)
 {
  application->running = 0;
  return -1;
 }
 ret = (application->dispatch)?1:0;
 fprintf(stderr, "%s ""wait %d""\n", __FUNCTION__, ret);
 if (!ret)
  ret = select(application->maxfd, &application->rfds, ((void *)0), ((void *)0), ((void *)0));
 application->dispatch = 0;
 fprintf(stderr, "%s ""continue %d""\n", __FUNCTION__, ret);
 if (ret > 0)
  do { application->connectors.it = application->connectors.next; while (application->connectors.it) { typeof (application->connectors) *it = application->connectors.it; application->connectors.it = application->connectors.it->next; if (_pilot_application_check_fdset(application, it->item) < 0) break; } } while(0);
 return ret;
}

canonical_original_code:
int pilot_application_check(struct pilot_application *application)
{
 int ret = 0;
 application->maxfd = 0;
 do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&application->rfds)->__fds_bits)[0]) : "memory"); } while (0);
 do { application->connectors.it = application->connectors.next; while (application->connectors.it) { typeof (application->connectors) *it = application->connectors.it; application->connectors.it = application->connectors.it->next; if (_pilot_application_fill_fdset(application, it->item) < 0) break; } } while(0);
 if (application->maxfd == 0)
 {
  application->running = 0;
  return -1;
 }
 ret = (application->dispatch)?1:0;
 fprintf(stderr, "%s ""wait %d""\n", __FUNCTION__, ret);
 if (!ret)
  ret = select(application->maxfd, &application->rfds, ((void *)0), ((void *)0), ((void *)0));
 application->dispatch = 0;
 fprintf(stderr, "%s ""continue %d""\n", __FUNCTION__, ret);
 if (ret > 0)
  do { application->connectors.it = application->connectors.next; while (application->connectors.it) { typeof (application->connectors) *it = application->connectors.it; application->connectors.it = application->connectors.it->next; if (_pilot_application_check_fdset(application, it->item) < 0) break; } } while(0);
 return ret;
}


decompiled_code:
__int64 __fastcall pilot_application_run(__int64 a1)
{
int v1; // ebx

*(_BYTE *)(a1 + 188) |= 1u;
do
{
v1 = pilot_application_check(a1);
if ( v1 >= 0 )
v1 = pilot_application_dispatchevents(a1);
if ( v1 != -1 )
continue;
if ( *_errno_location() != 4 )
break;
v1 = 0;
}
while ( (*(_BYTE *)(a1 + 188) & 1) != 0 );
return (unsigned int)v1;
}

original_code:
int
pilot_application_run(struct pilot_application *application)
{
 int ret = 0;
 application->running = 1;
 while (application->running && ret != -1) {
  ret = pilot_application_check(application);
  if (ret >=0)
   ret = pilot_application_dispatchevents(application);
  if (ret == -1 && (*__errno_location ()) == 4) {
   ret = 0;
  }
 }
 return ret;
}


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

result = a2;
*(_BYTE *)(a1 + 188) &= 0xFEu;
return result;
}

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

result = a2;
*(_BYTE *)(a1 + 188) &= 0xFEu;
return result;
}

original_code:
int
pilot_application_exit(struct pilot_application *application, int ret)
{
 application->running = 0;
 return ret;
}


original_code:
const char *
pilot_application_getopt_string(struct pilot_application *application, char *name)
{
 struct pilot_option *option;
 const char *value = ((void *)0);
 int len = strlen(name);
 option = (application->options.next)?application->options.next->item:((void *)0); application->options.it = application->options.next;
 while (option && strncmp(name, option->name, len))
 {
  option = (application->options.it->next)?application->options.it->next->item:((void *)0);application->options.it = application->options.it->next;;
 }
 if (option)
  value = option->value;
 return value;
}

canonical_original_code:
char *pilot_application_getopt_string(struct pilot_application *application, char *name)
{
 struct pilot_option *option;
 char *value = ((void *)0);
 int len = strlen(name);
 option = (application->options.next)?application->options.next->item:((void *)0); application->options.it = application->options.next;
 while (option && strncmp(name, option->name, len))
 {
  option = (application->options.it->next)?application->options.it->next->item:((void *)0);application->options.it = application->options.it->next;;
 }
 if (option)
  value = option->value;
 return value;
}

variable_types:
name
char *

variable_types: struct pilot_option { char *name; char *value; } *

variable_types:
value
char *

variable_types: len int

return_type: char *


decompiled_code:
int __fastcall pilot_application_getopt_int(__int64 a1, const char *a2)
{
int v2; // eax
const char ***v3; // rdx
const char **v4; // rbx
size_t v5; // r12
int result; // eax
const char ***v7; // rax
const char *v8; // rdi

v2 = strlen(a2);
v3 = *(const char ****)(a1 + 32);
if ( !v3 )
{
*(_QWORD *)(a1 + 40) = 0LL;
return 0;
}
v4 = *v3;
*(_QWORD *)(a1 + 40) = v3;
if ( !v4 )
return 0;
v5 = v2;
while ( 1 )
{
result = strncmp(a2, *v4, v5);
if ( !result )
break;
v7 = *(const char ****)(*(_QWORD *)(a1 + 40) + 8LL);
if ( !v7 )
{
*(_QWORD *)(a1 + 40) = 0LL;
return 0;
}
v4 = *v7;
*(_QWORD *)(a1 + 40) = v7;
if ( !v4 )
return 0;
}
v8 = v4[1];
if ( v8 )
result = atoi(v8);
return result;
}

canonical_decompiled_code:
int __fastcall func15(__int64 a1, const char *a2)
{
int v2; // eax
const char ***v3; // rdx
const char **v4; // rbx
size_t v5; // r12
int result; // eax
const char ***v7; // rax
const char *v8; // rdi

v2 = strlen(a2);
v3 = *(const char ****)(a1 + 32);
if ( !v3 )
{
*(_QWORD *)(a1 + 40) = 0LL;
return 0;
}
v4 = *v3;
*(_QWORD *)(a1 + 40) = v3;
if ( !v4 )
return 0;
v5 = v2;
while ( 1 )
{
result = strncmp(a2, *v4, v5);
if ( !result )
break;
v7 = *(const char ****)(*(_QWORD *)(a1 + 40) + 8LL);
if ( !v7 )
{
*(_QWORD *)(a1 + 40) = 0LL;
return 0;
}
v4 = *v7;
*(_QWORD *)(a1 + 40) = v7;
if ( !v4 )
return 0;
}
v8 = v4[1];
if ( v8 )
result = atoi(v8);
return result;
}

original_code:
int
pilot_application_getopt_int(struct pilot_application *application, char *name)
{
 struct pilot_option *option;
 int value = 0;
 int len = strlen(name);
 option = (application->options.next)?application->options.next->item:((void *)0); application->options.it = application->options.next;
 while (option && strncmp(name, option->name, len))
 {
  option = (application->options.it->next)?application->options.it->next->item:((void *)0);application->options.it = application->options.it->next;;
 }
 if (option && option->value)
  value = atoi(option->value);
 return value;
}

variable_types: value int


decompiled_code:
__int64 __fastcall set_cloexec_or_close(int fd)
{
unsigned int v1; // ebx
int v2; // eax

v1 = fd;
if ( fd != -1 )
{
v2 = fcntl(fd, 1);
if ( v2 == -1 || fcntl(fd, 2, v2 | 1) == -1 )
{
close(fd);
v1 = -1;
}
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func0(int fd)
{
unsigned int v1; // ebx
int v2; // eax

v1 = fd;
if ( fd != -1 )
{
v2 = fcntl(fd, 1);
if ( v2 == -1 || fcntl(fd, 2, v2 | 1) == -1 )
{
close(fd);
v1 = -1;
}
}
return v1;
}

original_code:
static int
set_cloexec_or_close(int fd)
{
 long flags;
 if (fd == -1)
  return -1;
 flags = fcntl(fd, 1);
 if (flags == -1)
  goto err;
 if (fcntl(fd, 2, flags | 1) == -1)
  goto err;
 return fd;
err:
 close(fd);
 return -1;
}

variable_types: fd int

variable_types: flags long


decompiled_code:
__int64 __fastcall create_tmpfile_cloexec(char *name)
{
int v1; // eax
unsigned int v2; // ebx

v1 = mkstemp(name);
v2 = v1;
if ( v1 >= 0 )
{
v2 = set_cloexec_or_close(v1);
unlink(name);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *name)
{
int v1; // eax
unsigned int v2; // ebx

v1 = mkstemp(name);
v2 = v1;
if ( v1 >= 0 )
{
v2 = func0(v1);
unlink(name);
}
return v2;
}

original_code:
static int
create_tmpfile_cloexec(char *tmpname)
{
 int fd;
 fd = mkstemp(tmpname);
 if (fd >= 0) {
  fd = set_cloexec_or_close(fd);
  unlink(tmpname);
 }
 return fd;
}

variable_types:
tmpname
char *


decompiled_code:
__int64 __fastcall os_socketpair_cloexec(int domain, int type, int protocol, int fds[2])
{
int v6; // ebx
int v8; // eax
int v9; // edi

v6 = socketpair(domain, type | 0x80000, protocol, fds);
if ( v6 )
{
if ( *_errno_location() == 22 )
{
v6 = socketpair(domain, type, protocol, fds);
if ( v6 >= 0 )
{
*fds = set_cloexec_or_close(*fds);
v8 = set_cloexec_or_close(fds[1]);
fds[1] = v8;
v9 = *fds;
if ( v8 == -1 || v9 == -1 )
{
close(v9);
close(fds[1]);
v6 = -1;
}
else
{
v6 = 0;
}
}
}
}
return (unsigned int)v6;
}

canonical_decompiled_code:
__int64 __fastcall func2(int domain, int type, int protocol, int fds[2])
{
int v6; // ebx
int v8; // eax
int v9; // edi

v6 = socketpair(domain, type | 0x80000, protocol, fds);
if ( v6 )
{
if ( *_errno_location() == 22 )
{
v6 = socketpair(domain, type, protocol, fds);
if ( v6 >= 0 )
{
*fds = func0(*fds);
v8 = func0(fds[1]);
fds[1] = v8;
v9 = *fds;
if ( v8 == -1 || v9 == -1 )
{
close(v9);
close(fds[1]);
v6 = -1;
}
else
{
v6 = 0;
}
}
}
}
return (unsigned int)v6;
}

original_code:
int
os_socketpair_cloexec(int domain, int type, int protocol, int *sv)
{
 int ret;
 ret = socketpair(domain, type | SOCK_CLOEXEC, protocol, sv);
 if (ret == 0 || (*__errno_location ()) != 22)
  return ret;
 ret = socketpair(domain, type, protocol, sv);
 if (ret < 0)
  return ret;
 sv[0] = set_cloexec_or_close(sv[0]);
 sv[1] = set_cloexec_or_close(sv[1]);
 if (sv[0] != -1 && sv[1] != -1)
  return 0;
 close(sv[0]);
 close(sv[1]);
 return -1;
}

variable_types: domain int

variable_types: type int

variable_types: protocol int

variable_types:
sv
int *


decompiled_code:
int os_epoll_create_cloexec()
{
int result; // eax
int v1; // eax

result = epoll_create1(0x80000);
if ( result < 0 )
{
if ( *_errno_location() == 22 )
{
v1 = epoll_create(1);
result = set_cloexec_or_close(v1);
}
else
{
result = -1;
}
}
return result;
}

canonical_decompiled_code:
int func3()
{
int result; // eax
int v1; // eax

result = epoll_create1(0x80000);
if ( result < 0 )
{
if ( *_errno_location() == 22 )
{
v1 = epoll_create(1);
result = func0(v1);
}
else
{
result = -1;
}
}
return result;
}

original_code:
int
os_epoll_create_cloexec(void)
{
 int fd;
 fd = epoll_create1(EPOLL_CLOEXEC);
 if (fd >= 0)
  return fd;
 if ((*__errno_location ()) != 22)
  return -1;
 fd = epoll_create(1);
 return set_cloexec_or_close(fd);
}

canonical_original_code:
int os_epoll_create_cloexec()
{
 int fd;
 fd = epoll_create1(EPOLL_CLOEXEC);
 if (fd >= 0)
  return fd;
 if ((*__errno_location ()) != 22)
  return -1;
 fd = epoll_create(1);
 return set_cloexec_or_close(fd);
}


decompiled_code:
__int64 __fastcall os_create_anonymous_file(__off_t length)
{
char *v1; // rax
const char *v2; // rbp
size_t v3; // rax
char *v4; // rax
char *v5; // rbx
size_t v6; // rax
int v7; // ebp

v1 = getenv("XDG_RUNTIME_DIR");
if ( v1 )
{
v2 = v1;
v3 = strlen(v1);
v4 = (char *)malloc(v3 + 22);
v5 = v4;
if ( v4 )
{
strcpy(v4, v2);
v6 = strlen(v5);
*(__m128i *)&v5[v6] = _mm_load_si128((const __m128i *)&template_0);
strcpy(&v5[v6 + 16], "XXXXX");
v7 = create_tmpfile_cloexec(v5);
free(v5);
if ( v7 < 0 )
{
v7 = -1;
}
else if ( ftruncate(v7, length) < 0 )
{
close(v7);
v7 = -1;
}
}
else
{
v7 = -1;
}
}
else
{
*_errno_location() = 2;
v7 = -1;
}
return (unsigned int)v7;
}

canonical_decompiled_code:
__int64 __fastcall func4(__off_t length)
{
char *v1; // rax
const char *v2; // rbp
size_t v3; // rax
char *v4; // rax
char *v5; // rbx
size_t v6; // rax
int v7; // ebp

v1 = getenv("XDG_RUNTIME_DIR");
if ( v1 )
{
v2 = v1;
v3 = strlen(v1);
v4 = (char *)malloc(v3 + 22);
v5 = v4;
if ( v4 )
{
strcpy(v4, v2);
v6 = strlen(v5);
*(__m128i *)&v5[v6] = _mm_load_si128((const __m128i *)&template_0);
strcpy(&v5[v6 + 16], "XXXXX");
v7 = func1(v5);
free(v5);
if ( v7 < 0 )
{
v7 = -1;
}
else if ( ftruncate(v7, length) < 0 )
{
close(v7);
v7 = -1;
}
}
else
{
v7 = -1;
}
}
else
{
*_errno_location() = 2;
v7 = -1;
}
return (unsigned int)v7;
}

original_code:
int
os_create_anonymous_file(off_t size)
{
 static const char template[] = "/weston-shared-XXXXXX";
 const char *path;
 char *name;
 int fd;
 path = getenv("XDG_RUNTIME_DIR");
 if (!path) {
  (*__errno_location ()) = 2;
  return -1;
 }
 name = malloc(strlen(path) + sizeof(template));
 if (!name)
  return -1;
 strcpy(name, path);
 strcat(name, template);
 fd = create_tmpfile_cloexec(name);
 free(name);
 if (fd < 0)
  return -1;
 if (ftruncate(fd, size) < 0) {
  close(fd);
  return -1;
 }
 return fd;
}

canonical_original_code:
int os_create_anonymous_file(long size)
{
 static char template[] = "/weston-shared-XXXXXX";
 char *path;
 char *name;
 int fd;
 path = getenv("XDG_RUNTIME_DIR");
 if (!path) {
  (*__errno_location ()) = 2;
  return -1;
 }
 name = malloc(strlen(path) + sizeof(template));
 if (!name)
  return -1;
 strcpy(name, path);
 strcat(name, template);
 fd = create_tmpfile_cloexec(name);
 free(name);
 if (fd < 0)
  return -1;
 if (ftruncate(fd, size) < 0) {
  close(fd);
  return -1;
 }
 return fd;
}

variable_types: size long

variable_types:
template
char []

variable_types:
path
char *


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

result = pilot_widget_create(a1, *(_QWORD *)(a1 + 12), *(_QWORD *)(a1 + 20));
*(_QWORD *)(result + 48) = pilot_canvas_redraw;
*(_QWORD *)(result + 32) = pilot_canvas_destroy;
*(_BYTE *)(result + 216) |= 1u;
return result;
}

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

result = pilot_widget_create(a1, *(_QWORD *)(a1 + 12), *(_QWORD *)(a1 + 20));
*(_QWORD *)(result + 48) = pilot_canvas_redraw;
*(_QWORD *)(result + 32) = pilot_canvas_destroy;
*(_BYTE *)(result + 216) |= 1u;
return result;
}

original_code:
struct pilot_widget *
pilot_canvas_create(struct pilot_widget *parent)
{
 struct pilot_widget *thiz;
 int i;
 thiz = pilot_widget_create(parent, parent->drawingrect);
 thiz->action.redraw = _pilot_canvas_redraw;
 thiz->action.destroy = _pilot_canvas_destroy;
 thiz->focussable = 1;
 return thiz;
}

canonical_original_code:
struct pilot_widget *pilot_canvas_create(struct pilot_widget *parent)
{
 struct pilot_widget *thiz;
 int i;
 thiz = pilot_widget_create(parent, parent->drawingrect);
 thiz->action.redraw = _pilot_canvas_redraw;
 thiz->action.destroy = _pilot_canvas_destroy;
 thiz->focussable = 1;
 return thiz;
}

variable_types:
parent
struct pilot_widget *

variable_types: struct pilot_widget { struct pilot_widget *parent; enum pilot_widget_type_t { EWidgetWindow=0, EWidgetCommon=1 } type; struct pilot_rect { int x; int y; unsigned int w; unsigned int h; } drawingrect; struct { void (*destroy)(struct pilot_widget *thiz); int (*show)(struct pilot_widget *thiz); int (*redraw)(struct pilot_widget *thiz, struct pilot_blit { void *data; struct pilot_rect rect; enum pilot_pixel_format_t { PILOT_DISPLAY_ARGB8888=0, PILOT_DISPLAY_XRGB8888=1 } format; struct { void (*destroy)(struct pilot_blit *thiz); int (*fill)(struct pilot_blit *thiz, unsigned int color); int (*copy)(struct pilot_blit *thiz, struct pilot_blit *src); } action; } *blit); int (*resize)(struct pilot_widget *thiz, unsigned int width, unsigned int height); } action; struct _pilot_list_pilot_widget { struct pilot_widget *item; struct _pilot_list_pilot_widget *next; struct _pilot_list_pilot_widget *it; } childs; struct { struct pilot_widget_keyChanged_slot { void *dest; struct pilot_widget_keyChanged_slot *next; struct pilot_widget_keyChanged_slot *it; int (*action)(void *, unsigned int key, char state); } slots; } keyChanged; struct { struct pilot_widget_mouseMoved_slot { void *dest; struct pilot_widget_mouseMoved_slot *next; struct pilot_widget_mouseMoved_slot *it; int (*action)(void *, unsigned int key, char state); } slots; } mouseMoved; struct { struct pilot_widget_mousebuttonChanged_slot { void *dest; struct pilot_widget_mousebuttonChanged_slot *next; struct pilot_widget_mousebuttonChanged_slot *it; int (*action)(void *, unsigned int key, char state); } slots; } mousebuttonChanged; struct { struct pilot_widget_mousewheelMoved_slot { void *dest; struct pilot_widget_mousewheelMoved_slot *next; struct pilot_widget_mousewheelMoved_slot *it; int (*action)(void *, unsigned int key, char state); } slots; } mousewheelMoved; char focussable; char force_redraw; void *widget; } *

variable_types: i int

user_defined_types: struct pilot_widget { struct pilot_widget *parent; enum pilot_widget_type_t type; struct pilot_rect drawingrect; struct { void (*destroy)(struct pilot_widget *thiz); int (*show)(struct pilot_widget *thiz); int (*redraw)(struct pilot_widget *thiz, struct pilot_blit *blit); int (*resize)(struct pilot_widget *thiz, unsigned int width, unsigned int height); } action; struct _pilot_list_pilot_widget childs; struct { struct pilot_widget_keyChanged_slot slots; } keyChanged; struct { struct pilot_widget_mouseMoved_slot slots; } mouseMoved; struct { struct pilot_widget_mousebuttonChanged_slot slots; } mousebuttonChanged; struct { struct pilot_widget_mousewheelMoved_slot slots; } mousewheelMoved; char focussable; char force_redraw; void *widget; }

user_defined_types: struct pilot_blit { void *data; struct pilot_rect rect; enum pilot_pixel_format_t format; struct { void (*destroy)(struct pilot_blit *thiz); int (*fill)(struct pilot_blit *thiz, unsigned int color); int (*copy)(struct pilot_blit *thiz, struct pilot_blit *src); } action; }


decompiled_code:
__int64 __fastcall pilot_canvas_draw_data(__int64 a1)
{
return *(_QWORD *)(*(_QWORD *)(a1 + 224) + 8LL);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
return *(_QWORD *)(*(_QWORD *)(a1 + 224) + 8LL);
}

original_code:
void *
pilot_canvas_draw_data(struct pilot_widget *thiz)
{
 struct pilot_canvas_data *canvas = (struct pilot_canvas_data *)thiz->widget;
 return canvas->data;
}

canonical_original_code:
void *pilot_canvas_draw_data(struct pilot_widget *thiz)
{
 struct pilot_canvas_data *canvas = (struct pilot_canvas_data *)thiz->widget;
 return canvas->data;
}

variable_types: struct pilot_canvas_data { int (*handler)(void *draw_data, struct pilot_blit { void *data; struct pilot_rect { int x; int y; unsigned int w; unsigned int h; } rect; enum pilot_pixel_format_t { PILOT_DISPLAY_ARGB8888=0, PILOT_DISPLAY_XRGB8888=1 } format; struct { void (*destroy)(struct pilot_blit *thiz); int (*fill)(struct pilot_blit *thiz, unsigned int color); int (*copy)(struct pilot_blit *thiz, struct pilot_blit *src); } action; } *blit); void *data; } *

return_type: void *

user_defined_types: struct pilot_canvas_data { int (*handler)(void *draw_data, struct pilot_blit *blit); void *data; }

user_defined_types: enum pilot_widget_type_t { EWidgetWindow=0, EWidgetCommon=1 }

user_defined_types: struct _pilot_list_pilot_widget { struct pilot_widget *item; struct _pilot_list_pilot_widget *next; struct _pilot_list_pilot_widget *it; }

user_defined_types: struct pilot_widget_keyChanged_slot { void *dest; struct pilot_widget_keyChanged_slot *next; struct pilot_widget_keyChanged_slot *it; int (*action)(void *, unsigned int key, char state); }

user_defined_types: struct pilot_widget_mouseMoved_slot { void *dest; struct pilot_widget_mouseMoved_slot *next; struct pilot_widget_mouseMoved_slot *it; int (*action)(void *, unsigned int key, char state); }

user_defined_types: struct pilot_widget_mousebuttonChanged_slot { void *dest; struct pilot_widget_mousebuttonChanged_slot *next; struct pilot_widget_mousebuttonChanged_slot *it; int (*action)(void *, unsigned int key, char state); }

user_defined_types: struct pilot_widget_mousewheelMoved_slot { void *dest; struct pilot_widget_mousewheelMoved_slot *next; struct pilot_widget_mousewheelMoved_slot *it; int (*action)(void *, unsigned int key, char state); }


decompiled_code:
void __fastcall pilot_canvas_data_destroy(void *a1)
{
free(a1);
}

canonical_decompiled_code:
void __fastcall func3(void *a1)
{
free(a1);
}

original_code:
void
pilot_canvas_data_destroy(struct pilot_canvas_data *thiz)
{
 free(thiz);
}

user_defined_types: struct pilot_rect { int x; int y; unsigned int w; unsigned int h; }

user_defined_types: enum pilot_pixel_format_t { PILOT_DISPLAY_ARGB8888=0, PILOT_DISPLAY_XRGB8888=1 }


decompiled_code:
__int64 __fastcall pilot_canvas_set_draw_handler(__int64 a1, __int64 a2, __int64 a3)
{
void *v5; // rdi

v5 = *(void **)(a1 + 224);
if ( v5 )
pilot_canvas_data_destroy(v5);
if ( !a2 )
return 0xFFFFFFFFLL;
*(_QWORD *)(a1 + 224) = pilot_canvas_data_create(a2, a3);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2, __int64 a3)
{
void *v5; // rdi

v5 = *(void **)(a1 + 224);
if ( v5 )
func3(v5);
if ( !a2 )
return 0xFFFFFFFFLL;
*(_QWORD *)(a1 + 224) = pilot_canvas_data_create(a2, a3);
return 0LL;
}

original_code:
int
pilot_canvas_set_draw_handler(struct pilot_widget *thiz, f_draw_handler handler, void *data)
{
 int ret = -1;
 if (thiz->widget)
 {
  pilot_canvas_data_destroy((struct pilot_canvas_data *)thiz->widget);
 }
 if (handler)
 {
  thiz->widget = (void*)pilot_canvas_data_create(handler,data);
  ret = 0;
 }
 return ret;
}

canonical_original_code:
int pilot_canvas_set_draw_handler(struct pilot_widget *thiz, int (*handler)(void *draw_data, struct pilot_blit *blit), void *data)
{
 int ret = -1;
 if (thiz->widget)
 {
  pilot_canvas_data_destroy((struct pilot_canvas_data *)thiz->widget);
 }
 if (handler)
 {
  thiz->widget = (void *)pilot_canvas_data_create(handler,data);
  ret = 0;
 }
 return ret;
}

variable_types: int (*)(void *draw_data, struct pilot_blit *blit)

variable_types:
data
void *

variable_types: ret int


mipow

repo: mchalek/euler

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

if ( a2 <= 0 )
return 1LL;
v2 = 0;
LODWORD(result) = 1;
do
{
result = (unsigned int)(a1 * (int)result % 250);
++v2;
}
while ( a2 != v2 );
return result;
}

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

if ( a2 <= 0 )
return 1LL;
v2 = 0;
LODWORD(result) = 1;
do
{
result = (unsigned int)(a1 * (int)result % 250);
++v2;
}
while ( a2 != v2 );
return result;
}

original_code:
int mipow(int a, int b)
{
    int ret = 1;
    int i;
    for(i = 0; i < b; i++) {
        ret *= a;
        ret %= 250;
    }
    return ret;
}

variable_types: a int

variable_types: b int


nn

decompiled_code:
__int64 __fastcall nn(int a1)
{
int v2; // edi
int v3; // eax
__int64 result; // rax

v2 = a1 % 250;
v3 = path_to_cycle[v2];
if ( v3 >= a1 )
result = mipow(v2, a1);
else
result = mipow(cycle_start[v2], (a1 - v3 - 1) % cycle_len[v2] + 1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1)
{
int v2; // edi
int v3; // eax
__int64 result; // rax

v2 = a1 % 250;
v3 = path_to_cycle[v2];
if ( v3 >= a1 )
result = func8(v2, a1);
else
result = func8(cycle_start[v2], (a1 - v3 - 1) % cycle_len[v2] + 1);
return result;
}

original_code:
int nn(int n)
{
    int base = n % 250;
    if(n <= path_to_cycle[base]) {
        return mipow(base, n);
    }
    int cycle_remainder = (n - path_to_cycle[base] - 1) % cycle_len[base];
    return mipow(cycle_start[base], 1 + cycle_remainder);
}

variable_types: n int

variable_types: base int


init

original_code:
void init()
{
    int i;
    cycle_len[0] = 1;
    path_to_cycle[0] = 0;
    cycle_start[0] = 0;
    cycle_len[1] = 1;
    path_to_cycle[1] = 0;
    cycle_start[1] = 1;
    _Bool hit[250];
    int logs[250];
    for(i = 2; i < 250; i++) {
        memset(hit, 0, sizeof(hit));
        memset(logs, 0, sizeof(logs));
        int j = i;
        int k = 1;
        do {
            logs[j] = k++;
            hit[j] = 1;
            j *= i;
            j %= 250;
        } while(!hit[j]);
        cycle_len[i] = k - logs[j];
        path_to_cycle[i] = logs[j] - 1;
        cycle_start[i] = j;
    }
}

variable_types:
hit
_Bool [250]

variable_types:
logs
int [250]

variable_types: j int

variable_types: k int


main

original_code:
int main()
{
    init();
    int i;
    long ways[250];
    memset(ways, 0, sizeof(ways));
    ways[0] = 1;
    ways[1] = 1;
    for(i = 2; i <= 250250; i++) {
        long prev[250];
        memcpy(prev, ways, sizeof(prev));
        int j;
        int item = nn(i);
        for(j = 0; j < 250; j++) {
            int k = (j + item) % 250;
            ways[k] += prev[j];
            ways[k] %= 10000000000000000l;
        }
    }
    printf("result: %ld\n", ways[0] - 1);
    return 0;
}

variable_types:
ways
long [250]

variable_types:
prev
long [250]

variable_types: item int


nCr

decompiled_code:
__int64 __fastcall nCr(int a1, int a2)
{
__int64 v2; // rdx
__int64 result; // rax
__int64 v4; // rsi
__int64 v5; // rcx

LODWORD(v2) = a1 - a2 + 1;
if ( a1 < (int)v2 )
{
result = 1LL;
}
else
{
v2 = (int)v2;
result = 1LL;
do
result *= v2++;
while ( v2 != (unsigned int)(a2 - 1) + (__int64)(a1 - a2) + 2 );
}
if ( a2 > 0 )
{
v4 = (unsigned int)(a2 + 1);
v5 = 1LL;
do
result /= v5++;
while ( v5 != v4 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, int a2)
{
__int64 v2; // rdx
__int64 result; // rax
__int64 v4; // rsi
__int64 v5; // rcx

LODWORD(v2) = a1 - a2 + 1;
if ( a1 < (int)v2 )
{
result = 1LL;
}
else
{
v2 = (int)v2;
result = 1LL;
do
result *= v2++;
while ( v2 != (unsigned int)(a2 - 1) + (__int64)(a1 - a2) + 2 );
}
if ( a2 > 0 )
{
v4 = (unsigned int)(a2 + 1);
v5 = 1LL;
do
result /= v5++;
while ( v5 != v4 );
}
return result;
}

original_code:
long nCr(int n, int r)
{
    long ret = 1;
    int i;
    for(i = n-r+1; i <= n; i++) {
        ret *= i;
    }
    for(i = 1; i <= r; i++)
        ret /= i;
    return ret;
}

variable_types: r int

variable_types: ret long

return_type: long


build

original_code:
long build(int ncol, int depth, int rem, long weight)
{
    if(depth == ncol)
        return weight;
    int min = (((1) > (rem - 10*(ncol-depth-1))) ? (1) : (rem - 10*(ncol-depth-1)));
    int max = (((rem) < (10)) ? (rem) : (10));
    int count;
    long ret = 0;
    for(count = min; count <= max; count++) {
        ret += build(ncol, depth+1, rem - count, weight * nCr(10, count));
    }
    return ret;
}

variable_types: ncol int

variable_types: depth int

variable_types: rem int

variable_types: weight long

variable_types: min int

variable_types: max int

variable_types: count int


main

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

v3 = 2LL;
v4 = 0LL;
v5 = 0LL;
do
{
v6 = build(v3, 0, 20, 1LL);
v7 = nCr(7, v3);
v4 += v7 * v6;
v5 += v6 * v3 * v7;
printf("ways for n == %d: %ld\n", (unsigned int)v3++, v6);
}
while ( v3 != 8 );
printf("total sum: %ld\n", v5);
printf("expected: %ld / %ld == %.9f\n", v5, v4, (double)(int)v5 / (double)(int)v4);
return 0;
}

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

v3 = 2LL;
v4 = 0LL;
v5 = 0LL;
do
{
v6 = func8(v3, 0, 20, 1LL);
v7 = func7(7, v3);
v4 += v7 * v6;
v5 += v6 * v3 * v7;
printf("ways for n == %d: %ld\n", (unsigned int)v3++, v6);
}
while ( v3 != 8 );
printf("total sum: %ld\n", v5);
printf("expected: %ld / %ld == %.9f\n", v5, v4, (double)(int)v5 / (double)(int)v4);
return 0;
}

original_code:
int main()
{
    long S = 0;
    long S0 = 0;
    int n;
    for(n = 2; n <= 7; n++) {
        long Sn = build(n, 0, 20, 1);
        long ncr = nCr(7, n);
        S0 += Sn * ncr;
        S += n*Sn * ncr;
        printf("ways for n == %d: %ld\n", n, Sn);
    }
    printf("total sum: %ld\n", S);
    printf("expected: %ld / %ld == %.9f\n", S, S0, ((double) S) / S0);
    return 0;
}

variable_types: S long

variable_types: n int

variable_types: Sn long

variable_types: ncr long


original_code:
int next_int(void) {
    if(++k < 56) {
        long k3 = (k*k*k) % 1000000;
        int result = (100003 - 200003*k + 300007*k3) % 1000000;
        S[read++] = result;
        read %= 55;
        return result;
    } else {
        int read_minus_24 = (read + 55 - 24) % 55;
        int read_minus_55 = read;
        int result = (S[read_minus_24] + S[read_minus_55]) % 1000000;
        S[read++] = result;
        read %= 55;
        return result;
    }
}

canonical_original_code:
int next_int() {
    if(++k < 56) {
        long k3 = (k*k*k) % 1000000;
        int result = (100003 - 200003*k + 300007*k3) % 1000000;
        S[read++] = result;
        read %= 55;
        return result;
    } else {
        int read_minus_24 = (read + 55 - 24) % 55;
        int read_minus_55 = read;
        int result = (S[read_minus_24] + S[read_minus_55]) % 1000000;
        S[read++] = result;
        read %= 55;
        return result;
    }
}

variable_types: result int


decompiled_code:
unsigned __int64 next_call()
{
__int64 v0; // rbx

v0 = (unsigned int)next_int();
return (int)next_int() | (unsigned __int64)(v0 << 32);
}

canonical_decompiled_code:
unsigned __int64 func14()
{
__int64 v0; // rbx

v0 = (unsigned int)func13();
return (int)func13() | (unsigned __int64)(v0 << 32);
}

original_code:
uint64_t next_call() {
    int c0 = next_int();
    int c1 = next_int();
    uint64_t result = c1;
    result |= ((uint64_t) c0) << 32;
    return result;
}

canonical_original_code:
unsigned long next_call() {
    int c0 = next_int();
    int c1 = next_int();
    unsigned long result = c1;
    result |= ((unsigned long) c0) << 32;
    return result;
}

variable_types:
result
unsigned long

return_type: unsigned long


original_code:
_Bool in_pm_net(int caller, uint64_t *is_friend) {
    int word = caller / 64;
    uint64_t bit = 1ul << (caller % 64);
    return is_friend[word] & bit;
}

canonical_original_code:
_Bool in_pm_net(int caller, unsigned long *is_friend) {
    int word = caller / 64;
    unsigned long bit = 1ul << (caller % 64);
    return is_friend[word] & bit;
}

variable_types: caller int

variable_types: word int

variable_types:
bit
unsigned long


original_code:
_Bool set_if_not(int caller, uint64_t *is_friend) {
    int word = caller / 64;
    uint64_t bit = 1ul << (caller % 64);
    _Bool state = is_friend[word] & bit;
    is_friend[word] |= bit;
    return state;
}

canonical_original_code:
_Bool set_if_not(int caller, unsigned long *is_friend) {
    int word = caller / 64;
    unsigned long bit = 1ul << (caller % 64);
    _Bool state = is_friend[word] & bit;
    is_friend[word] |= bit;
    return state;
}


decompiled_code:
__int64 __fastcall count_connected(unsigned __int64 *a1)
{
unsigned __int64 *v1; // rbx
unsigned int v2; // ebp

v1 = a1;
v2 = 0;
do
v2 += popcnt(*v1++);
while ( v1 != a1 + 15625 );
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func19(unsigned __int64 *a1)
{
unsigned __int64 *v1; // rbx
unsigned int v2; // ebp

v1 = a1;
v2 = 0;
do
v2 += func7(*v1++);
while ( v1 != a1 + 15625 );
return v2;
}

original_code:
int count_connected(uint64_t *words) {
    int ret = 0;
    int i;
    int num_words = (1000000 +63) / 64;
    for(i = 0; i < num_words; i++) {
        ret += _popcnt(words[i]);
    }
    return ret;
}

canonical_original_code:
int count_connected(unsigned long *words) {
    int ret = 0;
    int i;
    int num_words = (1000000 +63) / 64;
    for(i = 0; i < num_words; i++) {
        ret += _popcnt(words[i]);
    }
    return ret;
}

variable_types:
words
unsigned long *

variable_types: ret int

variable_types: i int


propagate

decompiled_code:
__int64 __fastcall propagate(int a1, __int64 a2, __int64 a3)
{
_DWORD *v4; // rax
int i; // er12
int src[3]; // [rsp+Ch] [rbp-4Ch] BYREF
int v8; // [rsp+18h] [rbp-40h] BYREF
int v9; // [rsp+1Ch] [rbp-3Ch] BYREF
size_t v10[7]; // [rsp+20h] [rbp-38h] BYREF

src[0] = a1;
set_if_not(a1, a2);
queue_init(4, v10);
enqueue(src, v10);
while ( (unsigned __int8)dequeue(&v9, v10) )
{
v4 = (_DWORD *)(a3 + ((__int64)v9 << 10));
for ( i = 0; *v4 > i; v4 = (_DWORD *)(a3 + ((__int64)v9 << 10)) )
{
v8 = v4[i + 1];
if ( !set_if_not(v8, a2) )
enqueue(&v8, v10);
++i;
}
}
return queue_cleanup(v10);
}

original_code:
void propagate(int caller, uint64_t *is_friend, user_t *calls) {
    set_if_not(caller, is_friend);
    queue_t q;
    queue_init(sizeof(int), &q);
    enqueue(&caller, &q);
    int user;
    while(dequeue(&user, &q)) {
        int i;
        for(i = 0; i < calls[user].num_calls; i++) {
            int friend = calls[user].called[i];
            if(!set_if_not(friend, is_friend)) {
                enqueue(&friend, &q);
            }
        }
    }
    queue_cleanup(&q);
}

canonical_original_code:
void propagate(int caller, unsigned long *is_friend, struct user_t *calls) {
    set_if_not(caller, is_friend);
    struct queue_t q;
    queue_init(sizeof(int), &q);
    enqueue(&caller, &q);
    int user;
    while(dequeue(&user, &q)) {
        int i;
        for(i = 0; i < calls[user].num_calls; i++) {
            int friend = calls[user].called[i];
            if(!set_if_not(friend, is_friend)) {
                enqueue(&friend, &q);
            }
        }
    }
    queue_cleanup(&q);
}

variable_types: struct user_t { int num_calls; int called[255]; } *

variable_types: struct queue_t { long item_size; long num_items; struct _payload_t { void *item; struct _payload_t *next; struct _payload_t *prev; } *head; }

variable_types: user int

variable_types: friend int

user_defined_types: struct queue_t { long item_size; long num_items; struct _payload_t *head; }


dequeue

decompiled_code:
__int64 __fastcall dequeue(void *a1, size_t *a2)
{
const void **v2; // rax
size_t v3; // rbp
size_t v4; // rax

v2 = (const void **)a2[2];
if ( !v2 )
return 0LL;
if ( a1 )
memcpy(a1, *v2, *a2);
v3 = a2[2];
v4 = *(_QWORD *)(v3 + 8);
a2[2] = v4;
if ( v4 )
*(_QWORD *)(v4 + 16) = 0LL;
else
a2[3] = 0LL;
free(*(void **)v3);
free((void *)v3);
--a2[1];
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func10(void *a1, size_t *a2)
{
const void **v2; // rax
size_t v3; // rbp
size_t v4; // rax

v2 = (const void **)a2[2];
if ( !v2 )
return 0LL;
if ( a1 )
memcpy(a1, *v2, *a2);
v3 = a2[2];
v4 = *(_QWORD *)(v3 + 8);
a2[2] = v4;
if ( v4 )
*(_QWORD *)(v4 + 16) = 0LL;
else
a2[3] = 0LL;
free(*(void **)v3);
free((void *)v3);
--a2[1];
return 1LL;
}

original_code:
static _Bool dequeue(void *item, queue_t *q) {
    if(q->head == ((void *)0)) {
        return 0;
    }
    if(item != ((void *)0))
        memcpy(item, q->head->item, q->item_size);
    payload_t *node = q->head;
    q->head = q->head->next;
    if(((void *)0) != q->head) {
        q->head->prev = ((void *)0);
    } else {
        q->tail = ((void *)0);
    }
    free(node->item);
    free(node);
    q->num_items--;
    return 1;
}

canonical_original_code:
static _Bool dequeue(void *item, struct queue_t *q) {
    if(q->head == ((void *)0)) {
        return 0;
    }
    if(item != ((void *)0))
        memcpy(item, q->head->item, q->item_size);
    struct _payload_t *node = q->head;
    q->head = q->head->next;
    if(((void *)0) != q->head) {
        q->head->prev = ((void *)0);
    } else {
        q->tail = ((void *)0);
    }
    free(node->item);
    free(node);
    q->num_items--;
    return 1;
}

variable_types:
item
void *

variable_types: struct queue_t { long item_size; long num_items; struct _payload_t { void *item; struct _payload_t *next; struct _payload_t *prev; } *head; } *


insert

original_code:
int insert(int caller0, int caller1, user_t *calls) {
    int num_new_users = !calls[caller0].num_calls + !calls[caller1].num_calls;
    append_user(caller0, calls + caller1);
    append_user(caller1, calls + caller0);
    return num_new_users;
}

canonical_original_code:
int insert(int caller0, int caller1, struct user_t *calls) {
    int num_new_users = !calls[caller0].num_calls + !calls[caller1].num_calls;
    append_user(caller0, calls + caller1);
    append_user(caller1, calls + caller0);
    return num_new_users;
}

user_defined_types: struct user_t { int num_calls; int called[255]; }


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned __int64 *v3; // r15
int v4; // er13
unsigned int v5; // ebp
unsigned __int64 v6; // rax
int v7; // ebx
unsigned __int64 v8; // r12
char v9; // r14
char v11; // [rsp+7h] [rbp-41h]
void *v12; // [rsp+8h] [rbp-40h]

v3 = (unsigned __int64 *)calloc(0x3D09uLL, 8uLL);
set_if_not(0x7FFFF, (__int64)v3);
v12 = calloc(0xF4240uLL, 0x400uLL);
v4 = count_connected(v3);
v5 = 0;
do
{
v6 = next_call();
v7 = v6;
v8 = HIDWORD(v6);
if ( HIDWORD(v6) != (_DWORD)v6 )
{
++v5;
insert(SHIDWORD(v6), v6, (__int64)v12);
v9 = in_pm_net(v8, (__int64)v3);
v11 = in_pm_net(v7, (__int64)v3);
if ( v5 == 100000 * ((int)v5 / 100000) )
printf("%d / %d connected after %d calls\n", (unsigned int)v4, 1000000LL, v5);
if ( v9 != v11 )
{
if ( v9 )
propagate(v7, (__int64)v3, (__int64)v12);
else
propagate(v8, (__int64)v3, (__int64)v12);
v4 = count_connected(v3);
}
}
}
while ( v4 <= 989999 );
printf("99%% coverage after %d calls!\n", v5);
printf("%d connected out of %d users\n", (unsigned int)v4, 1000000LL);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned __int64 *v3; // r15
int v4; // er13
unsigned int v5; // ebp
unsigned __int64 v6; // rax
int v7; // ebx
unsigned __int64 v8; // r12
char v9; // r14
char v11; // [rsp+7h] [rbp-41h]
void *v12; // [rsp+8h] [rbp-40h]

v3 = (unsigned __int64 *)calloc(0x3D09uLL, 8uLL);
func18(0x7FFFF, (__int64)v3);
v12 = calloc(0xF4240uLL, 0x400uLL);
v4 = func19(v3);
v5 = 0;
do
{
v6 = func14();
v7 = v6;
v8 = HIDWORD(v6);
if ( HIDWORD(v6) != (_DWORD)v6 )
{
++v5;
func16(SHIDWORD(v6), v6, (__int64)v12);
v9 = func17(v8, (__int64)v3);
v11 = func17(v7, (__int64)v3);
if ( v5 == 100000 * ((int)v5 / 100000) )
printf("%d / %d connected after %d calls\n", (unsigned int)v4, 1000000LL, v5);
if ( v9 != v11 )
{
if ( v9 )
func20(v7, (__int64)v3, (__int64)v12);
else
func20(v8, (__int64)v3, (__int64)v12);
v4 = func19(v3);
}
}
}
while ( v4 <= 989999 );
printf("99%% coverage after %d calls!\n", v5);
printf("%d connected out of %d users\n", (unsigned int)v4, 1000000LL);
return 0;
}

original_code:
int main(void) {
    uint64_t *is_friend = calloc((1000000 +63) / 64, sizeof(uint64_t));
    set_if_not(524287, is_friend);
    user_t *calls = calloc(1000000, sizeof(user_t));
    int network_size = 1000000;
    int connected = count_connected(is_friend);
    int num_calls = 0;
    do {
        uint64_t this_call = next_call();
        int caller0 = this_call >> 32;
        int caller1 = this_call & 0xffffffff;
        if(caller0 == caller1)
            continue;
        num_calls++;
        insert(caller0, caller1, calls);
        _Bool c0_in_net = in_pm_net(caller0, is_friend);
        _Bool c1_in_net = in_pm_net(caller1, is_friend);
        if(!(num_calls % 100000))
            printf("%d / %d connected after %d calls\n", connected, network_size, num_calls);
        if(!(c0_in_net ^ c1_in_net))
            continue;
        if(c0_in_net) {
            propagate(caller1, is_friend, calls);
        } else {
            propagate(caller0, is_friend, calls);
        }
        connected = count_connected(is_friend);
    } while(connected < (network_size * 99) / 100);
    printf("99%% coverage after %d calls!\n", num_calls);
    printf("%d connected out of %d users\n", connected, network_size);
    return 0;
}

canonical_original_code:
int main() {
    unsigned long *is_friend = calloc((1000000 +63) / 64, sizeof(uint64_t));
    set_if_not(524287, is_friend);
    struct user_t *calls = calloc(1000000, sizeof(user_t));
    int network_size = 1000000;
    int connected = count_connected(is_friend);
    int num_calls = 0;
    do {
        unsigned long this_call = next_call();
        int caller0 = this_call >> 32;
        int caller1 = this_call & 0xffffffff;
        if(caller0 == caller1)
            continue;
        num_calls++;
        insert(caller0, caller1, calls);
        _Bool c0_in_net = in_pm_net(caller0, is_friend);
        _Bool c1_in_net = in_pm_net(caller1, is_friend);
        if(!(num_calls % 100000))
            printf("%d / %d connected after %d calls\n", connected, network_size, num_calls);
        if(!(c0_in_net ^ c1_in_net))
            continue;
        if(c0_in_net) {
            propagate(caller1, is_friend, calls);
        } else {
            propagate(caller0, is_friend, calls);
        }
        connected = count_connected(is_friend);
    } while(connected < (network_size * 99) / 100);
    printf("99%% coverage after %d calls!\n", num_calls);
    printf("%d connected out of %d users\n", connected, network_size);
    return 0;
}

variable_types: connected int


original_code:
int max_count(dsum_t *ds, int n, long ndiv, long div[])
{
    int ret = 0;
    int i;
    for(i = 0; i < ndiv; i++) {
        long d = div[i];
        if(d == 1)
            continue;
        if(d == n)
            ret++;
        else
            ret += ds[n / d].nsum;
    }
    return ret;
}

canonical_original_code:
int max_count(struct _ds *ds, int n, long ndiv, long div[])
{
    int ret = 0;
    int i;
    for(i = 0; i < ndiv; i++) {
        long d = div[i];
        if(d == 1)
            continue;
        if(d == n)
            ret++;
        else
            ret += ds[n / d].nsum;
    }
    return ret;
}

variable_types: struct _ds { int nsum; struct _sc { int sum; int count; } *sums; } *

variable_types: n int

variable_types: ndiv long

variable_types:
div
long []

variable_types: d long

user_defined_types: struct _ds { int nsum; struct _sc *sums; }


dedupe

original_code:
void dedupe(int *n, sumcount_t *items)
{
    qsort(items, *n, sizeof(sumcount_t), cmp);
    int i, j;
    j = 1;
    for(i = 1; i < *n; i++) {
        if(cmp(items + i, items + j - 1)) {
            items[j++] = items[i];
        }
    }
    *n = j;
}

canonical_original_code:
void dedupe(int *n, struct _sc *items)
{
    qsort(items, *n, sizeof(sumcount_t), cmp);
    int i; int j;
    j = 1;
    for(i = 1; i < *n; i++) {
        if(cmp(items + i, items + j - 1)) {
            items[j++] = items[i];
        }
    }
    *n = j;
}

variable_types:
n
int *

variable_types:
items
struct _sc { int sum; int count; } *

variable_types: j int


cmp

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

result = (unsigned int)(*(_DWORD *)a1 - *(_DWORD *)a2);
if ( *(_DWORD *)a1 == *(_DWORD *)a2 )
result = (unsigned int)(*((_DWORD *)a1 + 1) - *((_DWORD *)a2 + 1));
return result;
}

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

result = (unsigned int)(*(_DWORD *)a1 - *(_DWORD *)a2);
if ( *(_DWORD *)a1 == *(_DWORD *)a2 )
result = (unsigned int)(*((_DWORD *)a1 + 1) - *((_DWORD *)a2 + 1));
return result;
}

original_code:
int cmp(const void *va, const void *vb)
{
    const sumcount_t *a = va;
    const sumcount_t *b = vb;
    if(a->sum == b->sum) {
        return a->count - b->count;
    } else {
        return a->sum - b->sum;
    }
}

canonical_original_code:
int cmp(void *va, void *vb)
{
    struct _sc *a = va;
    struct _sc *b = vb;
    if(a->sum == b->sum) {
        return a->count - b->count;
    } else {
        return a->sum - b->sum;
    }
}

variable_types:
va
void *

variable_types:
vb
void *

variable_types:
a
struct _sc { int sum; int count; } *

variable_types:
b
struct _sc { int sum; int count; } *


insert

original_code:
void insert(dsum_t *ds, int n, long ndiv, long div[]) {
    sumcount_t *tmp = malloc(max_count(ds, n, ndiv, div) * sizeof(sumcount_t));
    int c = 0;
    int i;
    for(i = 0; i < ndiv; i++) {
        long d = div[i];
        if(d == 1)
            continue;
        if(d == n) {
            tmp[c].sum = n;
            tmp[c++].count = 1;
        } else {
            int rem = n / d;
            int j;
            for(j = 0; j < ds[rem].nsum; j++) {
                tmp[c].sum = d + ds[rem].sums[j].sum;
                tmp[c++].count = 1 + ds[rem].sums[j].count;
            }
        }
    }
    dedupe(&c, tmp);
    ds[n].nsum = c;
    ds[n].sums = realloc(tmp, c*sizeof(sumcount_t));
}

canonical_original_code:
void insert(struct _ds *ds, int n, long ndiv, long div[]) {
    struct _sc *tmp = malloc(max_count(ds, n, ndiv, div) * sizeof(sumcount_t));
    int c = 0;
    int i;
    for(i = 0; i < ndiv; i++) {
        long d = div[i];
        if(d == 1)
            continue;
        if(d == n) {
            tmp[c].sum = n;
            tmp[c++].count = 1;
        } else {
            int rem = n / d;
            int j;
            for(j = 0; j < ds[rem].nsum; j++) {
                tmp[c].sum = d + ds[rem].sums[j].sum;
                tmp[c++].count = 1 + ds[rem].sums[j].count;
            }
        }
    }
    dedupe(&c, tmp);
    ds[n].nsum = c;
    ds[n].sums = realloc(tmp, c*sizeof(sumcount_t));
}

variable_types:
tmp
struct _sc { int sum; int count; } *

variable_types: c int

variable_types: i int

variable_types: rem int


main

original_code:
int main()
{
    dsum_t *ds = calloc(1 + 14000, sizeof(dsum_t));
    ds[1].nsum = 0;
    ds[1].sums = malloc(sizeof(sumcount_t));
    ds[1].sums[0].sum = 0;
    ds[1].sums[0].count = 0;
    int *mps = calloc(1 + 12000, sizeof(int));
    int i;
    int filled = 0;
    long *div;
    div = malloc((1 + 14000)*sizeof(long));
    div[0] = 1;
    for(i = 2; i <= 14000 && filled < 12000 - 2 + 1; i++) {
        int j;
        long nd = 1;
        int max_j = (int) ceil(sqrt(i));
        for(j = 2; j <= max_j; j++) {
            if(!(i % j)) {
                div[nd++] = j;
                div[nd++] = i / j;
            }
        }
        div[nd++] = i;
        insert(ds, i, nd, div);
        for(j = 0; j < ds[i].nsum; j++) {
            int k = i - ds[i].sums[j].sum + ds[i].sums[j].count;
            if(k < 2 || k > 12000)
                continue;
            if(!mps[k]) {
                mps[k] = i;
                filled++;
            }
        }
    }
    for(i = 2; i <= 12000; i++)
        printf("k == %d: %d\n", i, mps[i]);
    return 0;
}

canonical_original_code:
int main()
{
    struct _ds *ds = calloc(1 + 14000, sizeof(dsum_t));
    ds[1].nsum = 0;
    ds[1].sums = malloc(sizeof(sumcount_t));
    ds[1].sums[0].sum = 0;
    ds[1].sums[0].count = 0;
    int *mps = calloc(1 + 12000, sizeof(int));
    int i;
    int filled = 0;
    long *div;
    div = malloc((1 + 14000)*sizeof(long));
    div[0] = 1;
    for(i = 2; i <= 14000 && filled < 12000 - 2 + 1; i++) {
        int j;
        long nd = 1;
        int max_j = (int) ceil(sqrt(i));
        for(j = 2; j <= max_j; j++) {
            if(!(i % j)) {
                div[nd++] = j;
                div[nd++] = i / j;
            }
        }
        div[nd++] = i;
        insert(ds, i, nd, div);
        for(j = 0; j < ds[i].nsum; j++) {
            int k = i - ds[i].sums[j].sum + ds[i].sums[j].count;
            if(k < 2 || k > 12000)
                continue;
            if(!mps[k]) {
                mps[k] = i;
                filled++;
            }
        }
    }
    for(i = 2; i <= 12000; i++)
        printf("k == %d: %d\n", i, mps[i]);
    return 0;
}

variable_types:
mps
int *

variable_types: filled int

variable_types:
div
long *

variable_types: nd long

variable_types: k int


pairCmp

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

result = 0xFFFFFFFFLL;
if ( *(double *)a2 <= *(double *)a1 )
result = 1LL;
return result;
}

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

result = 0xFFFFFFFFLL;
if ( *(double *)a2 <= *(double *)a1 )
result = 1LL;
return result;
}

original_code:
int pairCmp(const void *A, const void *B)
{
    return (((struct arrpair *) A) -> sum < ((struct arrpair *) B) -> sum) ? -1 : 1;
}

canonical_original_code:
int pairCmp(void *A, void *B)
{
    return (((struct arrpair *) A) -> sum < ((struct arrpair *) B) -> sum) ? -1 : 1;
}

variable_types:
A
void *

variable_types:
B
void *


sumList

decompiled_code:
__int64 __fastcall sumList(int a1, int a2, int a3, int a4, int a5, int *a6, double a7, __int64 a8)
{
int v8; // ebx
int v9; // eax
__int64 result; // rax
int v12; // er15

v8 = (int)a6;
if ( a4 <= a5 )
{
v12 = a4 + 1;
sumList(a1, a2 | (1LL << ((unsigned __int8)a4 - (unsigned __int8)a3)), a3, a4 + 1, a5, (_DWORD)a6, a8);
result = sumList(a1, a2, a3, v12, a5, v8, a8);
}
else
{
*(_DWORD *)(a8 + 16LL * *a6 + 8) = a2;
v9 = (*a6)++;
result = 16LL * v9;
*(double *)(a8 + result) = a7;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1, int a2, int a3, int a4, int a5, int *a6, double a7, __int64 a8)
{
int v8; // ebx
int v9; // eax
__int64 result; // rax
int v12; // er15

v8 = (int)a6;
if ( a4 <= a5 )
{
v12 = a4 + 1;
func8(a1, a2 | (1LL << ((unsigned __int8)a4 - (unsigned __int8)a3)), a3, a4 + 1, a5, (_DWORD)a6, a8);
result = func8(a1, a2, a3, v12, a5, v8, a8);
}
else
{
*(_DWORD *)(a8 + 16LL * *a6 + 8) = a2;
v9 = (*a6)++;
result = 16LL * v9;
*(double *)(a8 + result) = a7;
}
return result;
}

original_code:
void sumList(const double logp[], int p, double sum, int zero, int n, int nstop, int *k, struct arrpair list[])
{
    if(n > nstop) {
        list[*k].p = p;
        list[(*k)++].sum = sum;
        return;
    }
    sumList(logp, p | (1l << (n-zero)), sum + logp[n], zero, n + 1, nstop, k, list);
    sumList(logp, p, sum, zero, n + 1, nstop, k, list);
    return;
}

canonical_original_code:
void sumList(double logp[], int p, double sum, int zero, int n, int nstop, int *k, struct arrpair list[])
{
    if(n > nstop) {
        list[*k].p = p;
        list[(*k)++].sum = sum;
        return;
    }
    sumList(logp, p | (1l << (n-zero)), sum + logp[n], zero, n + 1, nstop, k, list);
    sumList(logp, p, sum, zero, n + 1, nstop, k, list);
    return;
}

variable_types:
logp
double []

variable_types: p int

variable_types: sum double

variable_types: zero int

variable_types: n int

variable_types: nstop int

variable_types:
k
int *

variable_types: struct arrpair { double sum; int p; } []


findMax

decompiled_code:
__int64 __fastcall findMax(int a1, __int64 a2, __m128i *a3, double a4)
{
__int64 result; // rax
const __m128i *v7; // rdx
int v8; // ecx
double v9; // xmm0_8

v7 = (const __m128i *)(a2 + 16LL * a1 - 16);
if ( a4 > *(double *)v7->m128i_i64 )
{
*a3 = _mm_loadu_si128(v7);
}
else
{
if ( a1 )
{
LODWORD(result) = a1 / 2;
if ( a1 != 1 )
{
v8 = 0;
do
{
v9 = *(double *)(a2 + 16LL * (int)result);
if ( v9 > a4 )
{
if ( a4 > v9 )
break;
a1 = result;
}
else if ( a4 > v9 )
{
v8 = result;
}
if ( v8 == a1 )
break;
LODWORD(result) = (v8 + a1) / 2;
}
while ( v8 + 1 != a1 );
}
}
result = 16LL * (int)result;
*a3 = _mm_loadu_si128((const __m128i *)(a2 + result));
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1, __int64 a2, __m128i *a3, double a4)
{
__int64 result; // rax
const __m128i *v7; // rdx
int v8; // ecx
double v9; // xmm0_8

v7 = (const __m128i *)(a2 + 16LL * a1 - 16);
if ( a4 > *(double *)v7->m128i_i64 )
{
*a3 = _mm_loadu_si128(v7);
}
else
{
if ( a1 )
{
LODWORD(result) = a1 / 2;
if ( a1 != 1 )
{
v8 = 0;
do
{
v9 = *(double *)(a2 + 16LL * (int)result);
if ( v9 > a4 )
{
if ( a4 > v9 )
break;
a1 = result;
}
else if ( a4 > v9 )
{
v8 = result;
}
if ( v8 == a1 )
break;
LODWORD(result) = (v8 + a1) / 2;
}
while ( v8 + 1 != a1 );
}
}
result = 16LL * (int)result;
*a3 = _mm_loadu_si128((const __m128i *)(a2 + result));
}
return result;
}

original_code:
void findMax(int k, struct arrpair *list, double limit, struct arrpair *res)
{
    int i0 = 0, i1 = k, ix;
    if(limit > list[k-1].sum) {
        *res = list[k-1];
        return;
    }
    while(i0 != i1) {
        ix = (i0 + i1) / 2;
        if(i0 + 1 == i1)
            break;
        if(list[ix].sum > limit)
            i1 = ix;
        if(list[ix].sum < limit)
            i0 = ix;
    }
    *res = list[ix];
    return;
}

canonical_original_code:
void findMax(int k, struct arrpair *list, double limit, struct arrpair *res)
{
    int i0 = 0; int i1 = k; int ix;
    if(limit > list[k-1].sum) {
        *res = list[k-1];
        return;
    }
    while(i0 != i1) {
        ix = (i0 + i1) / 2;
        if(i0 + 1 == i1)
            break;
        if(list[ix].sum > limit)
            i1 = ix;
        if(list[ix].sum < limit)
            i0 = ix;
    }
    *res = list[ix];
    return;
}

variable_types:
list
struct arrpair { double sum; int p; } *

variable_types: limit double

variable_types:
res
struct arrpair *

variable_types: ix int


main

original_code:
int main(void)
{
    double logp[42];
    struct arrpair *listlow, *listhi, maxPair;
    double maxsum = 0, sopt;
    int klo = 0, khi = 0;
    int i, phi, plo;
    unsigned long int ans = 1;
    for(i = 0; i < 42; i++) {
        logp[i] = log10(p[i]);
        maxsum += logp[i];
    }
    maxsum /= 2;
    listlow = (struct arrpair *) malloc(sizeof(struct arrpair) * (1l << 21));
    listhi = (struct arrpair *) malloc(sizeof(struct arrpair) * (1l << 21));
    printf("Generating tables...");
    sumList(logp, 0, 0.0, 0, 0, 42/2-1, &khi, listhi);
    sumList(logp, 0, 0.0, 42/2, 42/2, 42 -1, &klo, listlow);
    printf("Done.\nSorting...");
    fflush(stdout);
    qsort(listhi, khi, sizeof(struct arrpair), pairCmp);
    qsort(listlow, klo, sizeof(struct arrpair), pairCmp);
    printf("Done.\nSearching...");
    fflush(stdout);
    sopt = 0.0;
    for(i = 0; i < khi; i++) {
        if(maxsum > listhi[i].sum)
            findMax(klo, listlow, maxsum - listhi[i].sum, &maxPair);
        else
            maxPair.sum = 0;
        if(maxPair.sum + listhi[i].sum > sopt &&
                maxPair.sum + listhi[i].sum < maxsum) {
            sopt = maxPair.sum + listhi[i].sum;
            plo = maxPair.p;
            phi = listhi[i].p;
        }
    }
    printf("Done.\nresult:\n\t");
    for(i = 0; i < 21; i++)
        if(phi & (1l << i)) {
            printf("%d * ", p[i]);
            ans *= p[i];
            ans %= 10000000000000000;
        }
    for(i = 0; i < 21; i++)
        if(plo & (1l << i)) {
            printf("%d * ", p[i+21]);
            ans *= p[i+21];
            ans %= 10000000000000000;
        }
    printf("== %llu\n", ans);
    return 0;
}

canonical_original_code:
int main()
{
    double logp[42];
    struct arrpair *listlow; struct arrpair *listhi; struct arrpair maxPair;
    double maxsum = 0; double sopt;
    int klo = 0; int khi = 0;
    int i; int phi; int plo;
    unsigned long ans = 1;
    for(i = 0; i < 42; i++) {
        logp[i] = log10(p[i]);
        maxsum += logp[i];
    }
    maxsum /= 2;
    listlow = (struct arrpair *) malloc(sizeof(struct arrpair) * (1l << 21));
    listhi = (struct arrpair *) malloc(sizeof(struct arrpair) * (1l << 21));
    printf("Generating tables...");
    sumList(logp, 0, 0.0, 0, 0, 42/2-1, &khi, listhi);
    sumList(logp, 0, 0.0, 42/2, 42/2, 42 -1, &klo, listlow);
    printf("Done.\nSorting...");
    fflush(stdout);
    qsort(listhi, khi, sizeof(struct arrpair), pairCmp);
    qsort(listlow, klo, sizeof(struct arrpair), pairCmp);
    printf("Done.\nSearching...");
    fflush(stdout);
    sopt = 0.0;
    for(i = 0; i < khi; i++) {
        if(maxsum > listhi[i].sum)
            findMax(klo, listlow, maxsum - listhi[i].sum, &maxPair);
        else
            maxPair.sum = 0;
        if(maxPair.sum + listhi[i].sum > sopt &&
                maxPair.sum + listhi[i].sum < maxsum) {
            sopt = maxPair.sum + listhi[i].sum;
            plo = maxPair.p;
            phi = listhi[i].p;
        }
    }
    printf("Done.\nresult:\n\t");
    for(i = 0; i < 21; i++)
        if(phi & (1l << i)) {
            printf("%d * ", p[i]);
            ans *= p[i];
            ans %= 10000000000000000;
        }
    for(i = 0; i < 21; i++)
        if(plo & (1l << i)) {
            printf("%d * ", p[i+21]);
            ans *= p[i+21];
            ans %= 10000000000000000;
        }
    printf("== %llu\n", ans);
    return 0;
}

variable_types:
logp
double [42]

variable_types:
listlow
struct arrpair { double sum; int p; } *

variable_types:
listhi
struct arrpair { double sum; int p; } *

variable_types:
maxPair
struct arrpair { double sum; int p; }

variable_types: maxsum double

variable_types: sopt double

variable_types: klo int

variable_types: khi int

variable_types: i int

variable_types: phi int

variable_types: plo int

variable_types:
ans
unsigned long


decompiled_code:
__int64 __fastcall count_pieces(unsigned __int64 a1)
{
unsigned __int64 v1; // rbx
unsigned int v2; // ebp
unsigned __int64 v3; // rbx

if ( !a1 )
return 0;
v1 = a1;
v2 = 0;
do
{
++v2;
v3 = v1 >> trailz(v1);
v1 = v3 >> trailz(~v3);
}
while ( v1 );
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned __int64 a1)
{
unsigned __int64 v1; // rbx
unsigned int v2; // ebp
unsigned __int64 v3; // rbx

if ( !a1 )
return 0;
v1 = a1;
v2 = 0;
do
{
++v2;
v3 = v1 >> func7(v1);
v1 = v3 >> func7(~v3);
}
while ( v1 );
return v2;
}

original_code:
static inline int count_pieces(uint64_t config) {
    int result = 0;
    while(config) {
        result++;
        config >>= _trailz(config);
        config >>= _trailz(~config);
    }
    return result;
}

canonical_original_code:
static inline int count_pieces(unsigned long config) {
    int result = 0;
    while(config) {
        result++;
        config >>= _trailz(config);
        config >>= _trailz(~config);
    }
    return result;
}

variable_types:
config
unsigned long

variable_types: result int