daemonize

decompiled_code:
__int64 daemonize()
{
__pid_t v0; // eax
int *v1; // rbx
char *v2; // rax
__int64 result; // rax
int *v4; // rbx
char *v5; // rax
struct sigaction v6; // [rsp+0h] [rbp-A8h] BYREF

validate_user();
if ( lockfile && *lockfile && open(lockfile, 194, 416LL) < 0 )
{
lockfile = 0LL;
v1 = _errno_location();
v2 = strerror(*v1);
fprintf(stderr, "unable to create lock file: %d (%s)\n", (unsigned int)*v1, v2);
result = 0LL;
}
else
{
v0 = fork();
if ( v0 < 0 )
{
result = 0LL;
}
else
{
if ( v0 > 0 )
exit(0);
umask(0);
if ( setsid() < 0 )
{
thinkd_log(3LL, "could not set sid");
result = 0LL;
}
else
{
std2null();
v6.sa_handler = (__sighandler_t)clean_and_exit;
sigemptyset(&v6.sa_mask);
v6.sa_flags = 0;
sigaction(2, &v6, 0LL);
sigaction(15, &v6, 0LL);
sigaction(3, &v6, 0LL);
v6.sa_handler = (__sighandler_t)load_config;
sigaction(10, &v6, 0LL);
if ( chdir("/") < 0 )
{
v4 = _errno_location();
v5 = strerror(*v4);
thinkd_log(3LL, "cannot into chdir. code %d (%s)", (unsigned int)*v4, v5);
result = 0LL;
}
else
{
result = create_pidfile();
}
}
}
}
return result;
}

canonical_decompiled_code:
__int64 func6()
{
__pid_t v0; // eax
int *v1; // rbx
char *v2; // rax
__int64 result; // rax
int *v4; // rbx
char *v5; // rax
struct sigaction v6; // [rsp+0h] [rbp-A8h] BYREF

func5();
if ( lockfile && *lockfile && open(lockfile, 194, 416LL) < 0 )
{
lockfile = 0LL;
v1 = _errno_location();
v2 = strerror(*v1);
fprintf(stderr, "unable to create lock file: %d (%s)\n", (unsigned int)*v1, v2);
result = 0LL;
}
else
{
v0 = fork();
if ( v0 < 0 )
{
result = 0LL;
}
else
{
if ( v0 > 0 )
exit(0);
umask(0);
if ( setsid() < 0 )
{
thinkd_log(3LL, "could not set sid");
result = 0LL;
}
else
{
func2();
v6.sa_handler = (__sighandler_t)clean_and_exit;
sigemptyset(&v6.sa_mask);
v6.sa_flags = 0;
sigaction(2, &v6, 0LL);
sigaction(15, &v6, 0LL);
sigaction(3, &v6, 0LL);
v6.sa_handler = (__sighandler_t)load_config;
sigaction(10, &v6, 0LL);
if ( chdir("/") < 0 )
{
v4 = _errno_location();
v5 = strerror(*v4);
thinkd_log(3LL, "cannot into chdir. code %d (%s)", (unsigned int)*v4, v5);
result = 0LL;
}
else
{
result = func3();
}
}
}
}
return result;
}

original_code:
static _Bool daemonize()
{
 struct sigaction s_action;
 pid_t pid, sid;
 int lock_fd;
 validate_user();
 if (lockfile && lockfile[0]) {
  lock_fd = open(lockfile, 02|0100|0200, (mode_t) 0640);
  if (lock_fd < 0) {
   lockfile = ((void *)0);
   fprintf(stderr, "unable to create lock file" ": %d (%s)" "\n", (*__errno_location ()), strerror((*__errno_location ())));
   return 0;
  }
 }
 pid = fork();
 if (pid < 0) {
  return 0;
 }
 if (pid > 0) {
  exit(0);
 }
 umask(0);
 sid = setsid();
 if (sid < 0) {
  thinkd_log(3, "could not set sid");
  return 0;
 }
 std2null();
 s_action.__sigaction_handler.sa_handler = clean_and_exit;
 sigemptyset(&s_action.sa_mask);
 s_action.sa_flags = 0;
 sigaction(2, &s_action, ((void *)0));
 sigaction(15, &s_action, ((void *)0));
 sigaction(3, &s_action, ((void *)0));
 s_action.__sigaction_handler.sa_handler = load_config;
 sigaction(10, &s_action, ((void *)0));
 if (chdir("/") < 0) {
  thinkd_log(3, "cannot into chdir. code %d (%s)", (*__errno_location ()), strerror((*__errno_location ())));
  return 0;
 }
 return create_pidfile();
}

canonical_original_code:
static _Bool daemonize()
{
 struct sigaction s_action;
 int pid; int sid;
 int lock_fd;
 validate_user();
 if (lockfile && lockfile[0]) {
  lock_fd = open(lockfile, 02|0100|0200, (unsigned int) 0640);
  if (lock_fd < 0) {
   lockfile = ((void *)0);
   fprintf(stderr, "unable to create lock file" ": %d (%s)" "\n", (*__errno_location ()), strerror((*__errno_location ())));
   return 0;
  }
 }
 pid = fork();
 if (pid < 0) {
  return 0;
 }
 if (pid > 0) {
  exit(0);
 }
 umask(0);
 sid = setsid();
 if (sid < 0) {
  thinkd_log(3, "could not set sid");
  return 0;
 }
 std2null();
 s_action.__sigaction_handler.sa_handler = clean_and_exit;
 sigemptyset(&s_action.sa_mask);
 s_action.sa_flags = 0;
 sigaction(2, &s_action, ((void *)0));
 sigaction(15, &s_action, ((void *)0));
 sigaction(3, &s_action, ((void *)0));
 s_action.__sigaction_handler.sa_handler = load_config;
 sigaction(10, &s_action, ((void *)0));
 if (chdir("/") < 0) {
  thinkd_log(3, "cannot into chdir. code %d (%s)", (*__errno_location ()), strerror((*__errno_location ())));
  return 0;
 }
 return create_pidfile();
}

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

variable_types: pid int

variable_types: sid int

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

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }


repo: JeffreyVdb/Thinkd

decompiled_code:
void __noreturn clean_and_exit()
{
cleanup_before_exit();
exit(0);
}

canonical_decompiled_code:
void __noreturn func7()
{
func4();
exit(0);
}

original_code:
static void clean_and_exit()
{
 cleanup_before_exit();
 exit(0);
}

return_type: void


decompiled_code:
int __fastcall handle_cmd_args(int *a1, char *const **a2)
{
int result; // eax
const char *v3; // rdx
const char *v4[4]; // [rsp+0h] [rbp-C8h] BYREF
struct option longopts; // [rsp+20h] [rbp-A8h] BYREF
const char *v6; // [rsp+40h] [rbp-88h]
int v7; // [rsp+48h] [rbp-80h]
__int64 v8; // [rsp+50h] [rbp-78h]
int v9; // [rsp+58h] [rbp-70h]
const char *v10; // [rsp+60h] [rbp-68h]
int v11; // [rsp+68h] [rbp-60h]
__int64 v12; // [rsp+70h] [rbp-58h]
int v13; // [rsp+78h] [rbp-50h]
__int64 v14; // [rsp+80h] [rbp-48h]
int v15; // [rsp+88h] [rbp-40h]
__int64 v16; // [rsp+90h] [rbp-38h]
int v17; // [rsp+98h] [rbp-30h]
int longind[7]; // [rsp+ACh] [rbp-1Ch] BYREF

longopts.name = "help";
longopts.has_arg = 0;
longopts.flag = 0LL;
longopts.val = 104;
v6 = "version";
v7 = 0;
v8 = 0LL;
v9 = 118;
v10 = "no-probe";
v11 = 0;
v12 = 0LL;
v13 = 110;
v14 = 0LL;
v15 = 0;
v16 = 0LL;
v17 = 0;
v4[0] = "print help message";
v4[1] = "print version of this program";
v4[2] = "do not try detecting the power mode";
while ( 1 )
{
result = getopt_long(*a1, *a2, "hvn", &longopts, longind);
if ( result == -1 )
return result;
if ( result == 110 )
{
probing = 0;
}
else
{
if ( result > 110 )
{
if ( result == 118 )
{
printf("%s %s\n", "thinkd", "2.1");
clean_and_exit("%s %s\n", "thinkd", v3);
}
LABEL_11:
print_usage(&longopts.name, v4);
cleanup_before_exit();
exit(1);
}
if ( result )
{
if ( result == 104 )
{
print_usage(&longopts.name, v4);
clean_and_exit((const char *)&longopts);
}
goto LABEL_11;
}
}
}
}

canonical_decompiled_code:
int __fastcall func8(int *a1, char *const **a2)
{
int result; // eax
const char *v3; // rdx
const char *v4[4]; // [rsp+0h] [rbp-C8h] BYREF
struct option longopts; // [rsp+20h] [rbp-A8h] BYREF
const char *v6; // [rsp+40h] [rbp-88h]
int v7; // [rsp+48h] [rbp-80h]
__int64 v8; // [rsp+50h] [rbp-78h]
int v9; // [rsp+58h] [rbp-70h]
const char *v10; // [rsp+60h] [rbp-68h]
int v11; // [rsp+68h] [rbp-60h]
__int64 v12; // [rsp+70h] [rbp-58h]
int v13; // [rsp+78h] [rbp-50h]
__int64 v14; // [rsp+80h] [rbp-48h]
int v15; // [rsp+88h] [rbp-40h]
__int64 v16; // [rsp+90h] [rbp-38h]
int v17; // [rsp+98h] [rbp-30h]
int longind[7]; // [rsp+ACh] [rbp-1Ch] BYREF

longopts.name = "help";
longopts.has_arg = 0;
longopts.flag = 0LL;
longopts.val = 104;
v6 = "version";
v7 = 0;
v8 = 0LL;
v9 = 118;
v10 = "no-probe";
v11 = 0;
v12 = 0LL;
v13 = 110;
v14 = 0LL;
v15 = 0;
v16 = 0LL;
v17 = 0;
v4[0] = "print help message";
v4[1] = "print version of this program";
v4[2] = "do not try detecting the power mode";
while ( 1 )
{
result = getopt_long(*a1, *a2, "hvn", &longopts, longind);
if ( result == -1 )
return result;
if ( result == 110 )
{
probing = 0;
}
else
{
if ( result > 110 )
{
if ( result == 118 )
{
printf("%s %s\n", "thinkd", "2.1");
func7("%s %s\n", "thinkd", v3);
}
LABEL_11:
func0(&longopts.name, v4);
func4();
exit(1);
}
if ( result )
{
if ( result == 104 )
{
func0(&longopts.name, v4);
func7((const char *)&longopts);
}
goto LABEL_11;
}
}
}
}

original_code:
static void handle_cmd_args(int *argc, char ***argv)
{
 int c, option_index;
 const struct option opts[] = {
  {"help", 0, 0, 'h'},
  {"version", 0, 0, 'v'},
  {"no-probe", 0, 0, 'n'},
  {((void *)0), 0, 0, 0}
 };
 const char * opts_help[] = {
  "print help message",
  "print version of this program",
  "do not try detecting the power mode"
 };
 while ((c = getopt_long(*argc, *argv, "hvn", opts, &option_index)) != -1) {
  switch (c) {
  case 0:
   break;
  case 'n':
   probing = 0;
   break;
  case 'v':
   printf("%s %s\n", "thinkd", "2.1");
   clean_and_exit();
  case 'h':
   print_usage(opts, opts_help);
   clean_and_exit();
  case '?':
  default:
   print_usage(opts, opts_help);
   cleanup_before_exit();
   exit(1);
   break;
  }
 }
}

canonical_original_code:
static void handle_cmd_args(int *argc, char ***argv)
{
 int c; int option_index;
 struct option opts[] = {
  {"help", 0, 0, 'h'},
  {"version", 0, 0, 'v'},
  {"no-probe", 0, 0, 'n'},
  {((void *)0), 0, 0, 0}
 };
 char *opts_help[] = {
  "print help message",
  "print version of this program",
  "do not try detecting the power mode"
 };
 while ((c = getopt_long(*argc, *argv, "hvn", opts, &option_index)) != -1) {
  switch (c) {
  case 0:
   break;
  case 'n':
   probing = 0;
   break;
  case 'v':
   printf("%s %s\n", "thinkd", "2.1");
   clean_and_exit();
  case 'h':
   print_usage(opts, opts_help);
   clean_and_exit();
  case '?':
  default:
   print_usage(opts, opts_help);
   cleanup_before_exit();
   exit(1);
   break;
  }
 }
}

variable_types:
argc
int *

variable_types:
argv
char ***

variable_types: c int

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


decompiled_code:
int __fastcall load_psupply_mode(void *a1)
{
int result; // eax

if ( a1 == &mode_powersave )
{
thinkd_log(6LL, "Battery found. Enabling powersave mode");
sleep_time = 15;
}
else
{
if ( a1 != &mode_performance )
{
result = thinkd_log(6LL, "Mode unrecognized, setting sleep time to default");
sleep_time = 15;
return result;
}
thinkd_log(6LL, "AC adapater is connected. Enabling performance mode");
sleep_time = 5;
}
pthread_mutex_lock(&conf_mutex);
load_power_mode(a1);
current_mode = (__int64)a1;
return pthread_mutex_unlock(&conf_mutex);
}

canonical_decompiled_code:
int __fastcall func9(void *a1)
{
int result; // eax

if ( a1 == &mode_powersave )
{
thinkd_log(6LL, "Battery found. Enabling powersave mode");
sleep_time = 15;
}
else
{
if ( a1 != &mode_performance )
{
result = thinkd_log(6LL, "Mode unrecognized, setting sleep time to default");
sleep_time = 15;
return result;
}
thinkd_log(6LL, "AC adapater is connected. Enabling performance mode");
sleep_time = 5;
}
pthread_mutex_lock(&conf_mutex);
load_power_mode(a1);
current_mode = (__int64)a1;
return pthread_mutex_unlock(&conf_mutex);
}

original_code:
static void load_psupply_mode(power_prefs_t *prefs)
{
 if (prefs == &mode_powersave) {
  thinkd_log(6, "Battery found. Enabling powersave mode");
  sleep_time = 15;
 }
 else if (prefs == &mode_performance) {
  thinkd_log(6, "AC adapater is connected. Enabling performance mode");
  sleep_time = 5;
 }
 else {
  thinkd_log(6, "Mode unrecognized, setting sleep time to default");
  sleep_time = 15;
  return;
 }
 pthread_mutex_lock(&conf_mutex);
 load_power_mode(prefs);
 current_mode = prefs;
 pthread_mutex_unlock(&conf_mutex);
}

canonical_original_code:
static void load_psupply_mode(struct __power_prefs *prefs)
{
 if (prefs == &mode_powersave) {
  thinkd_log(6, "Battery found. Enabling powersave mode");
  sleep_time = 15;
 }
 else if (prefs == &mode_performance) {
  thinkd_log(6, "AC adapater is connected. Enabling performance mode");
  sleep_time = 5;
 }
 else {
  thinkd_log(6, "Mode unrecognized, setting sleep time to default");
  sleep_time = 15;
  return;
 }
 pthread_mutex_lock(&conf_mutex);
 load_power_mode(prefs);
 current_mode = prefs;
 pthread_mutex_unlock(&conf_mutex);
}

variable_types: struct __power_prefs { int brightness; _Bool bluetooth; _Bool nmi_watchdog; _Bool wireless; _Bool wwan; _Bool audio_powersave; _Bool mute_state; _Bool thinklight_state; } *


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

pthread_mutex_lock(&conf_mutex);
read_ini();
result = pthread_mutex_unlock(&conf_mutex);
if ( current_mode )
result = load_psupply_mode((void *)current_mode);
return result;
}

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

pthread_mutex_lock(&conf_mutex);
read_ini();
result = pthread_mutex_unlock(&conf_mutex);
if ( current_mode )
result = func9((void *)current_mode);
return result;
}

original_code:
static void load_config()
{
 pthread_mutex_lock(&conf_mutex);
 read_ini();
 pthread_mutex_unlock(&conf_mutex);
 if (current_mode)
  load_psupply_mode(current_mode);
}


decompiled_code:
int detect_psupply_mode()
{
int result; // eax
__int64 v1; // [rsp+0h] [rbp-288h] BYREF
char v2[128]; // [rsp+100h] [rbp-188h] BYREF
char s[264]; // [rsp+180h] [rbp-108h] BYREF

if ( (int)scan_power_supply(&v1) < 0 )
{
thinkd_log(3LL, "failed to detect acpi power supply information");
result = load_psupply_mode(&mode_powersave);
}
else
{
snprintf(s, 0x100uLL, "%s/%s", v2, "online");
result = sysfs_read_int(s);
if ( result )
{
if ( (_UNKNOWN *)current_mode != &mode_performance )
result = load_psupply_mode(&mode_performance);
}
else if ( (_UNKNOWN *)current_mode != &mode_powersave )
{
result = load_psupply_mode(&mode_powersave);
}
}
return result;
}

canonical_decompiled_code:
int func11()
{
int result; // eax
__int64 v1; // [rsp+0h] [rbp-288h] BYREF
char v2[128]; // [rsp+100h] [rbp-188h] BYREF
char s[264]; // [rsp+180h] [rbp-108h] BYREF

if ( (int)scan_power_supply(&v1) < 0 )
{
thinkd_log(3LL, "failed to detect acpi power supply information");
result = func9(&mode_powersave);
}
else
{
snprintf(s, 0x100uLL, "%s/%s", v2, "online");
result = sysfs_read_int(s);
if ( result )
{
if ( (_UNKNOWN *)current_mode != &mode_performance )
result = func9(&mode_performance);
}
else if ( (_UNKNOWN *)current_mode != &mode_powersave )
{
result = func9(&mode_powersave);
}
}
return result;
}

original_code:
static void detect_psupply_mode()
{
 char buffer[256];
 int state;
 int num_batts;
 acpi_psupply_t power_supply;
 if ((num_batts = scan_power_supply(&power_supply)) < 0) {
  thinkd_log(3, "failed to detect acpi power supply information");
  load_psupply_mode(&mode_powersave);
  return;
 }
 snprintf(buffer, 256, "%s/%s", power_supply.acdir, "online");
 state = sysfs_read_int(buffer);
 if (state) {
  if (current_mode == &mode_performance) return;
  load_psupply_mode(&mode_performance);
  return;
 }
 if (current_mode == &mode_powersave)
  return;
 load_psupply_mode(&mode_powersave);
}

canonical_original_code:
static void detect_psupply_mode()
{
 char buffer[256];
 int state;
 int num_batts;
 struct __acpi_psupply power_supply;
 if ((num_batts = scan_power_supply(&power_supply)) < 0) {
  thinkd_log(3, "failed to detect acpi power supply information");
  load_psupply_mode(&mode_powersave);
  return;
 }
 snprintf(buffer, 256, "%s/%s", power_supply.acdir, "online");
 state = sysfs_read_int(buffer);
 if (state) {
  if (current_mode == &mode_performance) return;
  load_psupply_mode(&mode_performance);
  return;
 }
 if (current_mode == &mode_powersave)
  return;
 load_psupply_mode(&mode_powersave);
}

variable_types:
buffer
char [256]

variable_types: state int

variable_types: struct __acpi_psupply { char batteries[2][128]; char acdir[128]; }


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
int *v5; // rbx
char *v6; // rax
const char **v7; // [rsp+0h] [rbp-18h] BYREF
int v8[3]; // [rsp+Ch] [rbp-Ch] BYREF

v8[0] = argc;
v7 = argv;
handle_cmd_args(v8, (char *const **)&v7);
if ( !(unsigned int)open_log() && (unsigned __int8)daemonize() )
{
v3 = pthread_mutex_init(&conf_mutex, 0LL);
if ( v3 )
{
v5 = _errno_location();
v6 = strerror(*v5);
thinkd_log(3LL, "FATAL: %d (%s)", (unsigned int)*v5, v6);
clean_and_exit((_BYTE *)&loc_2 + 1);
}
load_config();
detect_psupply_mode();
if ( probing )
{
while ( 1 )
{
sleep(sleep_time);
detect_psupply_mode();
}
}
}
else
{
cleanup_before_exit();
v3 = 1;
}
return v3;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
int *v5; // rbx
char *v6; // rax
const char **v7; // [rsp+0h] [rbp-18h] BYREF
int v8[3]; // [rsp+Ch] [rbp-Ch] BYREF

v8[0] = argc;
v7 = argv;
func8(v8, (char *const **)&v7);
if ( !(unsigned int)func1() && (unsigned __int8)func6() )
{
v3 = pthread_mutex_init(&conf_mutex, 0LL);
if ( v3 )
{
v5 = _errno_location();
v6 = strerror(*v5);
thinkd_log(3LL, "FATAL: %d (%s)", (unsigned int)*v5, v6);
func7((_BYTE *)&loc_2 + 1);
}
func10();
func11();
if ( probing )
{
while ( 1 )
{
sleep(sleep_time);
func11();
}
}
}
else
{
func4();
v3 = 1;
}
return v3;
}

original_code:
int main(int argc, char *argv[])
{
 handle_cmd_args(&argc, &argv);
 if (open_log() || ! daemonize()) {
  cleanup_before_exit();
  return 1;
 }
 if (pthread_mutex_init(&conf_mutex, ((void *)0))) {
  thinkd_log(3, "FATAL" ": %d (%s)", (*__errno_location ()), strerror((*__errno_location ())));
  clean_and_exit();
 }
 load_config();
 detect_psupply_mode();
 if (! probing) {
  ipc_listen();
  return 0;
 }
 while (1) {
  sleep(sleep_time);
  detect_psupply_mode();
 }
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

return_type: int


decompiled_code:
__int64 __fastcall initialize_defaults(__int64 a1)
{
unsigned __int64 v1; // rbx
__int64 result; // rax

v1 = (unsigned int)((_DWORD)&mode_powersave + 36);
do
{
thinkd_log(7LL, "currently initializing %p to defaults", (const void *)v1);
*(_QWORD *)v1 = *(_QWORD *)a1;
result = *(unsigned int *)(a1 + 8);
*(_DWORD *)(v1 + 8) = result;
v1 -= 12LL;
}
while ( (_UNKNOWN *)v1 != (_UNKNOWN *)((char *)&mode_powersave - 12) );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
unsigned __int64 v1; // rbx
__int64 result; // rax

v1 = (unsigned int)((_DWORD)&mode_powersave + 36);
do
{
thinkd_log(7LL, "currently initializing %p to defaults", (const void *)v1);
*(_QWORD *)v1 = *(_QWORD *)a1;
result = *(unsigned int *)(a1 + 8);
*(_DWORD *)(v1 + 8) = result;
v1 -= 12LL;
}
while ( (_UNKNOWN *)v1 != (_UNKNOWN *)((char *)&mode_powersave - 12) );
return result;
}

original_code:
static void initialize_defaults(power_prefs_t *defaults)
{
 power_prefs_t *prefs[] = {
  &mode_powersave, &mode_performance,
  &mode_critical, &mode_heavy_powersave
 };
 for (power_prefs_t *p = *prefs + (sizeof(prefs) / sizeof(prefs[0])) - 1;
      p >= *prefs; --p) {
  thinkd_log(7, "currently initializing %p to defaults", (void *) p);
  memcpy(p, defaults, sizeof(struct __power_prefs));
 }
}

canonical_original_code:
static void initialize_defaults(struct __power_prefs *defaults)
{
 struct __power_prefs *prefs[] = {
  &mode_powersave, &mode_performance,
  &mode_critical, &mode_heavy_powersave
 };
 for (struct __power_prefs *p = *prefs + (sizeof(prefs) / sizeof(prefs[0])) - 1;
      p >= *prefs; --p) {
  thinkd_log(7, "currently initializing %p to defaults", (void *) p);
  memcpy(p, defaults, sizeof(struct __power_prefs));
 }
}

variable_types: struct __power_prefs { int brightness; _Bool bluetooth; _Bool nmi_watchdog; _Bool wireless; _Bool wwan; _Bool audio_powersave; _Bool mute_state; _Bool thinklight_state; } *[]


decompiled_code:
size_t __fastcall str_read_int(_DWORD *a1, const char *a2)
{
size_t result; // rax
const char *v3; // rcx
int v4; // edx

result = strlen(a2);
*a1 = 0;
v3 = &a2[result - 1];
if ( a2 <= v3 )
{
v4 = 1;
while ( (unsigned __int8)(*v3 - 48) <= 9u )
{
result = (unsigned int)(v4 * (*v3 - 48));
*a1 += result;
v4 *= 10;
if ( a2 > --v3 )
return result;
}
result = thinkd_log(3LL, "str_read_int(): expected integer number, got %s", a2);
*a1 = 0;
}
return result;
}

canonical_decompiled_code:
size_t __fastcall func1(_DWORD *a1, const char *a2)
{
size_t result; // rax
const char *v3; // rcx
int v4; // edx

result = strlen(a2);
*a1 = 0;
v3 = &a2[result - 1];
if ( a2 <= v3 )
{
v4 = 1;
while ( (unsigned __int8)(*v3 - 48) <= 9u )
{
result = (unsigned int)(v4 * (*v3 - 48));
*a1 += result;
v4 *= 10;
if ( a2 > --v3 )
return result;
}
result = thinkd_log(3LL, "str_read_int(): expected integer number, got %s", a2);
*a1 = 0;
}
return result;
}

original_code:
void str_read_int(void *store, const char *value)
{
 int *dest = (int*) store;
 int power = 1;
 const char *a_end = value + strlen(value);
 const char *nan_errmsg = "str_read_int(): expected integer number, got %s";
 *dest = 0;
 while (--a_end >= value) {
  if (*a_end > '9' || *a_end < '0') {
   thinkd_log(3, nan_errmsg, value);
   *dest = 0;
   return;
  }
  *dest += (*a_end - '0') * power;
  power *= 10;
 }
}

canonical_original_code:
void str_read_int(void *store, char *value)
{
 int *dest = (int*) store;
 int power = 1;
 char *a_end = value + strlen(value);
 char *nan_errmsg = "str_read_int(): expected integer number, got %s";
 *dest = 0;
 while (--a_end >= value) {
  if (*a_end > '9' || *a_end < '0') {
   thinkd_log(3, nan_errmsg, value);
   *dest = 0;
   return;
  }
  *dest += (*a_end - '0') * power;
  power *= 10;
 }
}

variable_types:
store
void *

variable_types:
value
char *

variable_types:
dest
int *

variable_types: power int


decompiled_code:
__int64 alloc_ini_table()
{
void *v0; // rax
char ***v1; // rdx
char **v2; // rax

v0 = (void *)ec_malloc(64LL);
search_tab = v0;
if ( !v0 )
return 0LL;
v1 = (char ***)v0;
v2 = &ini_table_defs;
do
{
*v1 = v2;
v2 += 3;
++v1;
}
while ( v2 != (char **)(unsigned int)((_DWORD)&ini_table_defs + 192) );
return 8LL;
}

canonical_decompiled_code:
__int64 func3()
{
void *v0; // rax
char ***v1; // rdx
char **v2; // rax

v0 = (void *)ec_malloc(64LL);
search_tab = v0;
if ( !v0 )
return 0LL;
v1 = (char ***)v0;
v2 = &ini_table_defs;
do
{
*v1 = v2;
v2 += 3;
++v1;
}
while ( v2 != (char **)(unsigned int)((_DWORD)&ini_table_defs + 192) );
return 8LL;
}

original_code:
size_t alloc_ini_table()
{
 int idx = 0;
 size_t tab_size, elems, alloc_size;
 tab_size = sizeof(ini_table_defs);
 elems = tab_size / sizeof(struct __ini_table);
 alloc_size = elems * sizeof(struct __ini_table*);
 search_tab = (ini_table_t**) ec_malloc(alloc_size);
 if (! search_tab)
  return (size_t) 0;
 while (idx < elems) {
  search_tab[idx] = &ini_table_defs[idx];
  ++idx;
 }
 return elems;
}

canonical_original_code:
unsigned long alloc_ini_table()
{
 int idx = 0;
 unsigned long tab_size; unsigned long elems; unsigned long alloc_size;
 tab_size = sizeof(ini_table_defs);
 elems = tab_size / sizeof(struct __ini_table);
 alloc_size = elems * sizeof(struct __ini_table *);
 search_tab = (struct __ini_table **) ec_malloc(alloc_size);
 if (! search_tab)
  return (unsigned long) 0;
 while (idx < elems) {
  search_tab[idx] = &ini_table_defs[idx];
  ++idx;
 }
 return elems;
}

variable_types: idx int

variable_types:
elems
unsigned long

return_type: unsigned long

user_defined_types: struct __ini_table { char *key; unsigned long store_offset; void (*handler)(void *, char *); }


decompiled_code:
void free_ini_table()
{
free(search_tab);
search_tab = 0LL;
}

canonical_decompiled_code:
void func4()
{
free(search_tab);
search_tab = 0LL;
}

original_code:
void free_ini_table()
{
 free(search_tab);
 search_tab = ((void *)0);
}


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

result = a1;
if ( !a1 )
{
thinkd_log(3LL, "failed to allocate memory, exiting.");
exit(1);
}
return result;
}

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

result = a1;
if ( !a1 )
{
thinkd_log(3LL, "failed to allocate memory, exiting.");
exit(1);
}
return result;
}

original_code:
static inline void *ec_allocated(void *ptr)
{
 if (ptr)
  return ptr;
 thinkd_log(3, "failed to allocate memory, exiting.");
 exit(1);
}

variable_types:
ptr
void *

return_type: void *


decompiled_code:
const void *__fastcall ec_malloc(size_t a1)
{
void *v1; // rax
const void *v2; // rbx

v1 = malloc(a1);
v2 = (const void *)ec_allocated((__int64)v1);
thinkd_log(7LL, "malloc: address=%p; size=%zu", v2, a1);
return v2;
}

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

v1 = malloc(a1);
v2 = (const void *)func0((__int64)v1);
thinkd_log(7LL, "malloc: address=%p; size=%zu", v2, a1);
return v2;
}

original_code:
void *ec_malloc(size_t nbytes)
{
 void *ptr;
 ptr = ec_allocated(malloc(nbytes));
 thinkd_log(7, "malloc: address=%p; size=%zu", ptr, nbytes);
 return ptr;
}

canonical_original_code:
void *ec_malloc(unsigned long nbytes)
{
 void *ptr;
 ptr = ec_allocated(malloc(nbytes));
 thinkd_log(7, "malloc: address=%p; size=%zu", ptr, nbytes);
 return ptr;
}

variable_types:
nbytes
unsigned long


decompiled_code:
const void *__fastcall ec_calloc(size_t a1, size_t a2)
{
void *v2; // rax
const void *v3; // rbx

v2 = calloc(a1, a2);
v3 = (const void *)ec_allocated((__int64)v2);
thinkd_log(7LL, "calloc: address=%p; nmem=%zu; size=%zu", v3, a1, a2);
return v3;
}

canonical_decompiled_code:
const void *__fastcall func2(size_t a1, size_t a2)
{
void *v2; // rax
const void *v3; // rbx

v2 = calloc(a1, a2);
v3 = (const void *)func0((__int64)v2);
thinkd_log(7LL, "calloc: address=%p; nmem=%zu; size=%zu", v3, a1, a2);
return v3;
}

original_code:
void *ec_calloc(size_t nelems, size_t nbytes)
{
 void *ptr;
 ptr = ec_allocated(calloc(nelems, nbytes));
 thinkd_log(7, "calloc: address=%p; nmem=%zu; size=%zu", ptr, nelems, nbytes);
 return ptr;
}

canonical_original_code:
void *ec_calloc(unsigned long nelems, unsigned long nbytes)
{
 void *ptr;
 ptr = ec_allocated(calloc(nelems, nbytes));
 thinkd_log(7, "calloc: address=%p; nmem=%zu; size=%zu", ptr, nelems, nbytes);
 return ptr;
}

variable_types:
nelems
unsigned long


decompiled_code:
__int64 __fastcall set_audio_state(unsigned int a1, __int64 a2)
{
unsigned __int8 v2; // bp
const char *v3; // rdx
char v5[512]; // [rsp+0h] [rbp-618h] BYREF
char s[512]; // [rsp+200h] [rbp-418h] BYREF
char filename[536]; // [rsp+400h] [rbp-218h] BYREF

if ( a1 )
{
if ( a1 == 1 )
thinkd_log(6LL, "AC97 powersaving not supported");
else
thinkd_log(3LL, "not found: %d", a1);
}
else
{
v2 = *(_BYTE *)(a2 + 8);
snprintf(v5, 0x200uLL, "%s/parameters/%s", "/sys/module/snd_hda_intel/", "power_save_controller");
snprintf(s, 0x200uLL, "%s/parameters/%s", "/sys/module/snd_hda_intel/", "power_save");
pprintf(v5, "%c", v2 == 0 ? 78 : 89);
pprintf(s, "%d", v2);
}
snprintf(filename, 0x200uLL, "%s/%s", "/proc/acpi/ibm", "volume");
v3 = "mute";
if ( !*(_BYTE *)(a2 + 9) )
v3 = "unmute";
return pprintf(filename, "%s", v3);
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1, __int64 a2)
{
unsigned __int8 v2; // bp
const char *v3; // rdx
char v5[512]; // [rsp+0h] [rbp-618h] BYREF
char s[512]; // [rsp+200h] [rbp-418h] BYREF
char filename[536]; // [rsp+400h] [rbp-218h] BYREF

if ( a1 )
{
if ( a1 == 1 )
thinkd_log(6LL, "AC97 powersaving not supported");
else
thinkd_log(3LL, "not found: %d", a1);
}
else
{
v2 = *(_BYTE *)(a2 + 8);
snprintf(v5, 0x200uLL, "%s/parameters/%s", "/sys/module/snd_hda_intel/", "power_save_controller");
snprintf(s, 0x200uLL, "%s/parameters/%s", "/sys/module/snd_hda_intel/", "power_save");
func0(v5, "%c", v2 == 0 ? 78 : 89);
func0(s, "%d", v2);
}
snprintf(filename, 0x200uLL, "%s/%s", "/proc/acpi/ibm", "volume");
v3 = "mute";
if ( !*(_BYTE *)(a2 + 9) )
v3 = "unmute";
return func0(filename, "%s", v3);
}

original_code:
static void set_audio_state(audio_type_t type, const power_prefs_t *prefs)
{
 const char *MUTE_ON = "mute";
 const char *MUTE_OFF = "unmute";
 const char *BASE_ACPI_PATH = "/proc/acpi/ibm";
 sysfs_path_t mute_path;
 int state = prefs->audio_powersave;
 switch (type) {
 case HDA_INTEL: {
  const char *PATH_FORMAT = "%s/parameters/%s";
  const char *BASE_PATH = "/sys/module/snd_hda_intel/";
  sysfs_path_t ps_path_c, ps_path;
  snprintf(ps_path_c, 512, PATH_FORMAT, BASE_PATH, "power_save_controller");
  snprintf(ps_path, 512, PATH_FORMAT, BASE_PATH, "power_save");
  pprintf(ps_path_c, "%c", state ? 'Y' : 'N');
  pprintf(ps_path, "%d", state ? 1 : 0);
  break;
 }
 case AC97:
  thinkd_log(6, "AC97 powersaving not supported");
  break;
 default:
  thinkd_log(3, "not found: %d", (int) type);
  break;
 }
 snprintf(mute_path, 512, "%s/%s", BASE_ACPI_PATH, "volume");
 pprintf(mute_path, "%s", prefs->mute_state ? MUTE_ON : MUTE_OFF);
}

canonical_original_code:
static void set_audio_state(enum __audio_type type, struct __power_prefs *prefs)
{
 char *MUTE_ON = "mute";
 char *MUTE_OFF = "unmute";
 char *BASE_ACPI_PATH = "/proc/acpi/ibm";
 char mute_path[512];
 int state = prefs->audio_powersave;
 switch (type) {
 case HDA_INTEL: {
  char *PATH_FORMAT = "%s/parameters/%s";
  char *BASE_PATH = "/sys/module/snd_hda_intel/";
  char ps_path_c[512]; char ps_path[512];
  snprintf(ps_path_c, 512, PATH_FORMAT, BASE_PATH, "power_save_controller");
  snprintf(ps_path, 512, PATH_FORMAT, BASE_PATH, "power_save");
  pprintf(ps_path_c, "%c", state ? 'Y' : 'N');
  pprintf(ps_path, "%d", state ? 1 : 0);
  break;
 }
 case AC97:
  thinkd_log(6, "AC97 powersaving not supported");
  break;
 default:
  thinkd_log(3, "not found: %d", (int) type);
  break;
 }
 snprintf(mute_path, 512, "%s/%s", BASE_ACPI_PATH, "volume");
 pprintf(mute_path, "%s", prefs->mute_state ? MUTE_ON : MUTE_OFF);
}

variable_types: enum __audio_type { HDA_INTEL=0, AC97=1 }

user_defined_types: struct __power_prefs { int brightness; _Bool bluetooth; _Bool nmi_watchdog; _Bool wireless; _Bool wwan; _Bool audio_powersave; _Bool mute_state; _Bool thinklight_state; }


decompiled_code:
void __fastcall set_rfkill_devices(unsigned __int8 *a1)
{
const char **v1; // rbx
char *i; // rcx
int *v3; // rbx
char *v4; // rax
char v5[64]; // [rsp+0h] [rbp-6A8h] BYREF
char filename[512]; // [rsp+40h] [rbp-668h] BYREF
char v7[512]; // [rsp+240h] [rbp-468h] BYREF
glob_t pglob; // [rsp+440h] [rbp-268h] BYREF
char s[536]; // [rsp+490h] [rbp-218h] BYREF

snprintf(s, 0x200uLL, "%s/rfkill?", "/sys/class/rfkill");
if ( glob(s, 0, 0LL, &pglob) )
{
v3 = _errno_location();
v4 = strerror(*v3);
thinkd_log(3LL, "glob: %d (%s)", (unsigned int)*v3, v4);
}
else
{
v1 = (const char **)pglob.gl_pathv;
for ( i = *pglob.gl_pathv; *v1; i = (char *)*v1 )
{
snprintf(v7, 0x200uLL, "%s/%s", i, "name");
snprintf(filename, 0x200uLL, "%s/%s", *v1, "state");
sysfs_read_str(v5, 512, v7);
if ( !strcmp(v5, "tpacpi_bluetooth_sw") )
{
pprintf(filename, "%d", a1[4]);
}
else if ( !strcmp(v5, "tpacpi_wwan_sw") )
{
pprintf(filename, "%d", a1[7]);
}
else
{
pprintf(filename, "%d", a1[6]);
}
++v1;
}
globfree(&pglob);
}
}

canonical_decompiled_code:
void __fastcall func5(unsigned __int8 *a1)
{
const char **v1; // rbx
char *i; // rcx
int *v3; // rbx
char *v4; // rax
char v5[64]; // [rsp+0h] [rbp-6A8h] BYREF
char filename[512]; // [rsp+40h] [rbp-668h] BYREF
char v7[512]; // [rsp+240h] [rbp-468h] BYREF
glob_t pglob; // [rsp+440h] [rbp-268h] BYREF
char s[536]; // [rsp+490h] [rbp-218h] BYREF

snprintf(s, 0x200uLL, "%s/rfkill?", "/sys/class/rfkill");
if ( glob(s, 0, 0LL, &pglob) )
{
v3 = _errno_location();
v4 = strerror(*v3);
thinkd_log(3LL, "glob: %d (%s)", (unsigned int)*v3, v4);
}
else
{
v1 = (const char **)pglob.gl_pathv;
for ( i = *pglob.gl_pathv; *v1; i = (char *)*v1 )
{
snprintf(v7, 0x200uLL, "%s/%s", i, "name");
snprintf(filename, 0x200uLL, "%s/%s", *v1, "state");
func3(v5, 512, v7);
if ( !strcmp(v5, "tpacpi_bluetooth_sw") )
{
func0(filename, "%d", a1[4]);
}
else if ( !strcmp(v5, "tpacpi_wwan_sw") )
{
func0(filename, "%d", a1[7]);
}
else
{
func0(filename, "%d", a1[6]);
}
++v1;
}
globfree(&pglob);
}
}

original_code:
static void set_rfkill_devices(const power_prefs_t *prefs)
{
 const char *RFKILL_DIR = "/sys/class/rfkill";
 sysfs_path_t rfkill_paths;
 glob_t glob_buf;
 snprintf(rfkill_paths, 512, "%s/rfkill?", RFKILL_DIR);
 if (! glob(rfkill_paths, 0, ((void *)0), &glob_buf) == 0) {
  thinkd_log(3, "glob" ": %d (%s)", (*__errno_location ()), strerror((*__errno_location ())));
  return;
 }
 for (char **p = glob_buf.gl_pathv; *p; ++p) {
  sysfs_path_t rf_state_path, rf_name_path;
  sysfs_value_t name;
  snprintf(rf_name_path, 512, "%s/%s", *p, "name");
  snprintf(rf_state_path, 512, "%s/%s", *p, "state");
  sysfs_read_str(name, 512, rf_name_path);
  if (strcmp(name, "tpacpi_bluetooth_sw") == 0)
   pprintf(rf_state_path, "%d", (int) prefs->bluetooth);
  else if (strcmp(name, "tpacpi_wwan_sw") == 0)
   pprintf(rf_state_path, "%d", (int) prefs->wwan);
  else {
   pprintf(rf_state_path, "%d", (int) prefs->wireless);
  }
 }
 globfree(&glob_buf);
}

canonical_original_code:
static void set_rfkill_devices(struct __power_prefs *prefs)
{
 char *RFKILL_DIR = "/sys/class/rfkill";
 char rfkill_paths[512];
 struct glob_t glob_buf;
 snprintf(rfkill_paths, 512, "%s/rfkill?", RFKILL_DIR);
 if (! glob(rfkill_paths, 0, ((void *)0), &glob_buf) == 0) {
  thinkd_log(3, "glob" ": %d (%s)", (*__errno_location ()), strerror((*__errno_location ())));
  return;
 }
 for (char **p = glob_buf.gl_pathv; *p; ++p) {
  char rf_state_path[512]; char rf_name_path[512];
  char name[64];
  snprintf(rf_name_path, 512, "%s/%s", *p, "name");
  snprintf(rf_state_path, 512, "%s/%s", *p, "state");
  sysfs_read_str(name, 512, rf_name_path);
  if (strcmp(name, "tpacpi_bluetooth_sw") == 0)
   pprintf(rf_state_path, "%d", (int) prefs->bluetooth);
  else if (strcmp(name, "tpacpi_wwan_sw") == 0)
   pprintf(rf_state_path, "%d", (int) prefs->wwan);
  else {
   pprintf(rf_state_path, "%d", (int) prefs->wireless);
  }
 }
 globfree(&glob_buf);
}

variable_types: struct __power_prefs { int brightness; _Bool bluetooth; _Bool nmi_watchdog; _Bool wireless; _Bool wwan; _Bool audio_powersave; _Bool mute_state; _Bool thinklight_state; } *

variable_types: struct glob_t { unsigned long gl_pathc; char **gl_pathv; unsigned long gl_offs; int gl_flags; void (*gl_closedir)(void *); void *(*gl_readdir)(void *); void *(*gl_opendir)(char *); int (*gl_lstat)(char *, void *); int (*gl_stat)(char *, void *); }

variable_types:
p
char **

variable_types:
name
char [64]


decompiled_code:
__int64 __fastcall set_nmi_watchdog(unsigned __int8 a1)
{
return pprintf("/proc/sys/kernel/nmi_watchdog", "%u", a1);
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned __int8 a1)
{
return func0("/proc/sys/kernel/nmi_watchdog", "%u", a1);
}

original_code:
void set_nmi_watchdog(_Bool state)
{
 const char *nmi_path = "/proc/sys/kernel/nmi_watchdog";
 pprintf(nmi_path, "%u", (unsigned int) state);
}

canonical_original_code:
void set_nmi_watchdog(_Bool state)
{
 char *nmi_path = "/proc/sys/kernel/nmi_watchdog";
 pprintf(nmi_path, "%u", (unsigned int) state);
}


decompiled_code:
__int64 __fastcall load_power_mode(unsigned __int8 *a1)
{
int v1; // ecx
int v2; // eax
__int64 v3; // rdx
const char *v4; // rdx
char v6[264]; // [rsp+0h] [rbp-108h] BYREF

strcpy(v6, "/sys/class/backlight/acpi_video0/max_brightness");
v1 = sysfs_read_int(v6);
v2 = *(_DWORD *)a1 / (100 / v1);
v3 = 0LL;
if ( v2 >= 0 )
{
if ( v1 <= v2 )
v2 = v1;
v3 = (unsigned int)v2;
}
strcpy(v6, "/sys/class/backlight/acpi_video0/brightness");
pprintf(v6, "%d", v3);
set_nmi_watchdog(a1[5]);
set_audio_powersaving((__int64)a1);
set_rfkill_devices(a1);
snprintf(v6, 0x100uLL, "%s/%s", "/proc/acpi/ibm", "light");
v4 = "on";
if ( !a1[10] )
v4 = "off";
return pprintf(v6, "%s", v4);
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned __int8 *a1)
{
int v1; // ecx
int v2; // eax
__int64 v3; // rdx
const char *v4; // rdx
char v6[264]; // [rsp+0h] [rbp-108h] BYREF

strcpy(v6, "/sys/class/backlight/acpi_video0/max_brightness");
v1 = func2(v6);
v2 = *(_DWORD *)a1 / (100 / v1);
v3 = 0LL;
if ( v2 >= 0 )
{
if ( v1 <= v2 )
v2 = v1;
v3 = (unsigned int)v2;
}
strcpy(v6, "/sys/class/backlight/acpi_video0/brightness");
func0(v6, "%d", v3);
func6(a1[5]);
func7((__int64)a1);
func5(a1);
snprintf(v6, 0x100uLL, "%s/%s", "/proc/acpi/ibm", "light");
v4 = "on";
if ( !a1[10] )
v4 = "off";
return func0(v6, "%s", v4);
}

original_code:
void load_power_mode(const power_prefs_t *prefs)
{
 const char *BASE_ACPI_PROC = "/proc/acpi/ibm";
 const int VAL_SIZ = 256;
 char buffer[VAL_SIZ];
 int max_brightness, brightness_adjust;
 snprintf(buffer, VAL_SIZ, "/sys/class/backlight/acpi_video0" "/max_brightness");
 max_brightness = sysfs_read_int(buffer);
 brightness_adjust = prefs->brightness / (100 / max_brightness);
 if (brightness_adjust < 0)
  brightness_adjust = 0;
 else if (brightness_adjust > max_brightness)
  brightness_adjust = max_brightness;
 snprintf(buffer, VAL_SIZ, "/sys/class/backlight/acpi_video0" "/brightness");
 pprintf(buffer, "%d", brightness_adjust);
 set_nmi_watchdog(prefs->nmi_watchdog);
 set_audio_powersaving(prefs);
 set_rfkill_devices(prefs);
 snprintf(buffer, VAL_SIZ, "%s/%s", BASE_ACPI_PROC, "light");
 pprintf(buffer, "%s", prefs->thinklight_state ? "on" : "off");
}

canonical_original_code:
void load_power_mode(struct __power_prefs *prefs)
{
 char *BASE_ACPI_PROC = "/proc/acpi/ibm";
 int VAL_SIZ = 256;
 char buffer[-1];
 int max_brightness; int brightness_adjust;
 snprintf(buffer, VAL_SIZ, "/sys/class/backlight/acpi_video0" "/max_brightness");
 max_brightness = sysfs_read_int(buffer);
 brightness_adjust = prefs->brightness / (100 / max_brightness);
 if (brightness_adjust < 0)
  brightness_adjust = 0;
 else if (brightness_adjust > max_brightness)
  brightness_adjust = max_brightness;
 snprintf(buffer, VAL_SIZ, "/sys/class/backlight/acpi_video0" "/brightness");
 pprintf(buffer, "%d", brightness_adjust);
 set_nmi_watchdog(prefs->nmi_watchdog);
 set_audio_powersaving(prefs);
 set_rfkill_devices(prefs);
 snprintf(buffer, VAL_SIZ, "%s/%s", BASE_ACPI_PROC, "light");
 pprintf(buffer, "%s", prefs->thinklight_state ? "on" : "off");
}

variable_types:
buffer
char [-1]


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

if ( err_logfile )
{
result = (info_logfile == 0LL) + (debug_logfile == 0LL);
if ( !result )
{
_lock_log_files(12);
fclose(err_logfile);
fclose(info_logfile);
result = fclose(debug_logfile);
}
}
return result;
}

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

if ( err_logfile )
{
result = (info_logfile == 0LL) + (debug_logfile == 0LL);
if ( !result )
{
func1(12);
fclose(err_logfile);
fclose(info_logfile);
result = fclose(debug_logfile);
}
}
return result;
}

original_code:
void thinkd_close_log()
{
 int res = 0;
 res += (int) (!debug_logfile);
 res += (int) (!err_logfile || !info_logfile);
 if (res)
  return;
 __lock_log_files(8 | 4);
 fclose(err_logfile);
 fclose(info_logfile);
 fclose(debug_logfile);
}

variable_types: res int


decompiled_code:
void thinkd_log(int a1, const char *a2, ...)
{
struct tm *v2; // rcx
size_t v3; // r14
size_t v4; // r14
char *v5; // r15
int *v6; // rax
char *v7; // rax
char s[128]; // [rsp+0h] [rbp-180h] BYREF
time_t timer; // [rsp+80h] [rbp-100h] BYREF
gcc_va_list arg; // [rsp+88h] [rbp-F8h] BYREF

va_start(arg, a2);
timer = time(0LL);
v2 = localtime(&timer);
if ( !v2 || !strftime(s, 0x80uLL, "%r: ", v2) )
{
v6 = _errno_location();
v7 = strerror(*v6);
fprintf(err_logfile, "%s\n", v7);
return;
}
v3 = strlen(a2);
v4 = strlen(s) + v3;
v5 = (char *)calloc(v4 + 2, 1uLL);
strcpy(v5, s);
strcat(v5, a2);
v5[v4] = 10;
if ( a1 > 6 )
{
if ( a1 == 7 )
{
vfprintf(debug_logfile, v5, arg);
goto LABEL_11;
}
LABEL_12:
vfprintf(info_logfile, v5, arg);
goto LABEL_11;
}
if ( a1 > 4 || a1 != 3 )
goto LABEL_12;
vfprintf(err_logfile, v5, arg);
LABEL_11:
free(v5);
}

canonical_decompiled_code:
void func3(int a1, const char *a2, ...)
{
struct tm *v2; // rcx
size_t v3; // r14
size_t v4; // r14
char *v5; // r15
int *v6; // rax
char *v7; // rax
char s[128]; // [rsp+0h] [rbp-180h] BYREF
time_t timer; // [rsp+80h] [rbp-100h] BYREF
gcc_va_list arg; // [rsp+88h] [rbp-F8h] BYREF

va_start(arg, a2);
timer = time(0LL);
v2 = localtime(&timer);
if ( !v2 || !strftime(s, 0x80uLL, "%r: ", v2) )
{
v6 = _errno_location();
v7 = strerror(*v6);
fprintf(err_logfile, "%s\n", v7);
return;
}
v3 = strlen(a2);
v4 = strlen(s) + v3;
v5 = (char *)calloc(v4 + 2, 1uLL);
strcpy(v5, s);
strcat(v5, a2);
v5[v4] = 10;
if ( a1 > 6 )
{
if ( a1 == 7 )
{
vfprintf(debug_logfile, v5, arg);
goto LABEL_11;
}
LABEL_12:
vfprintf(info_logfile, v5, arg);
goto LABEL_11;
}
if ( a1 > 4 || a1 != 3 )
goto LABEL_12;
vfprintf(err_logfile, v5, arg);
LABEL_11:
free(v5);
}

original_code:
void thinkd_log(int priority, const char *format, ...)
{
 va_list args;
 __builtin_va_start(args,format);
 const int DATE_FORMAT_SIZE = 128;
 const char *DATE_FORMAT = "%r: ";
 time_t t;
 struct tm *ltime;
 size_t format_len;
 char * newl_format;
 char dformat_buffer[DATE_FORMAT_SIZE];
 t = time(((void *)0));
 ltime = localtime(&t);
 if (! ltime) {
  fprintf(err_logfile, "%s\n", strerror((*__errno_location ())));
  return;
 }
 if (! strftime(dformat_buffer, DATE_FORMAT_SIZE, DATE_FORMAT, ltime)) {
  fprintf(err_logfile, "%s\n", strerror((*__errno_location ())));
  return;
 }
 format_len = strlen(format) + strlen(dformat_buffer);
 newl_format = (char *) calloc(format_len + 2, sizeof(char));
 strcpy(newl_format, dformat_buffer);
 strcat(newl_format, format);
 newl_format[format_len] = '\n';
 switch (priority) {
 case 7:
  vfprintf(debug_logfile, newl_format, args);
  break;
 case 6:
 case 5:
  vfprintf(info_logfile, newl_format, args);
  break;
 case 3:
  vfprintf(err_logfile, newl_format, args);
  break;
 default:
  vfprintf(info_logfile, newl_format, args);
  break;
 }
 free(newl_format);
 __builtin_va_end(args);
}

canonical_original_code:
void thinkd_log(int priority, char *format, ...)
{
 __builtin_va_list args;
 __builtin_va_start(args,format);
 int DATE_FORMAT_SIZE = 128;
 char *DATE_FORMAT = "%r: ";
 long t;
 struct tm *ltime;
 unsigned long format_len;
 char *newl_format;
 char dformat_buffer[-1];
 t = time(((void *)0));
 ltime = localtime(&t);
 if (! ltime) {
  fprintf(err_logfile, "%s\n", strerror((*__errno_location ())));
  return;
 }
 if (! strftime(dformat_buffer, DATE_FORMAT_SIZE, DATE_FORMAT, ltime)) {
  fprintf(err_logfile, "%s\n", strerror((*__errno_location ())));
  return;
 }
 format_len = strlen(format) + strlen(dformat_buffer);
 newl_format = (char *) calloc(format_len + 2, sizeof(char));
 strcpy(newl_format, dformat_buffer);
 strcat(newl_format, format);
 newl_format[format_len] = '\n';
 switch (priority) {
 case 7:
  vfprintf(debug_logfile, newl_format, args);
  break;
 case 6:
 case 5:
  vfprintf(info_logfile, newl_format, args);
  break;
 case 3:
  vfprintf(err_logfile, newl_format, args);
  break;
 default:
  vfprintf(info_logfile, newl_format, args);
  break;
 }
 free(newl_format);
 __builtin_va_end(args);
}

variable_types: priority int

variable_types:
format
char *

variable_types: t long

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


repo: LIS/lis-next

decompiled_code:
__int64 __fastcall hv_start_fcopy(__int64 a1)
{
const char *v1; // r12
const char *v2; // rbp
char *v4; // rax
char *v5; // rbx
int *v6; // rax
char *v7; // rax

filesize = 0LL;
v1 = (const char *)(a1 + 556);
snprintf(target_fname, 0x104uLL, "%s/%s", (const char *)(a1 + 556), (const char *)(a1 + 36));
syslog(6, "Target file name: %s", target_fname);
v2 = (const char *)(a1 + 556);
while ( 1 )
{
v4 = strchr(v2, 47);
v5 = v4;
if ( !v4 )
break;
if ( v2 == v4 )
{
++v2;
}
else
{
*v4 = 0;
if ( access(v1, 0) )
{
if ( (*(_BYTE *)(a1 + 1076) & 2) == 0 )
{
syslog(3, "Invalid path: %s", v1);
return 2147500037LL;
}
if ( mkdir(v1, 0x1EDu) )
{
syslog(3, "Failed to create %s", v1);
return 2147500037LL;
}
}
v2 = v5 + 1;
*v5 = 47;
}
}
if ( !access(target_fname, 0) )
{
syslog(6, "File: %s exists", target_fname);
if ( (*(_BYTE *)(a1 + 1076) & 1) == 0 )
return 2147942480LL;
}
target_fd = open(target_fname, 524866, 484LL);
if ( target_fd != -1 )
return 0LL;
v6 = __errno_location();
v7 = strerror(*v6);
syslog(6, "Open Failed: %s", v7);
return 2147500037LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
const char *v1; // r12
const char *v2; // rbp
char *v4; // rax
char *v5; // rbx
int *v6; // rax
char *v7; // rax

filesize = 0LL;
v1 = (const char *)(a1 + 556);
snprintf(target_fname, 0x104uLL, "%s/%s", (const char *)(a1 + 556), (const char *)(a1 + 36));
syslog(6, "Target file name: %s", target_fname);
v2 = (const char *)(a1 + 556);
while ( 1 )
{
v4 = strchr(v2, 47);
v5 = v4;
if ( !v4 )
break;
if ( v2 == v4 )
{
++v2;
}
else
{
*v4 = 0;
if ( access(v1, 0) )
{
if ( (*(_BYTE *)(a1 + 1076) & 2) == 0 )
{
syslog(3, "Invalid path: %s", v1);
return 2147500037LL;
}
if ( mkdir(v1, 0x1EDu) )
{
syslog(3, "Failed to create %s", v1);
return 2147500037LL;
}
}
v2 = v5 + 1;
*v5 = 47;
}
}
if ( !access(target_fname, 0) )
{
syslog(6, "File: %s exists", target_fname);
if ( (*(_BYTE *)(a1 + 1076) & 1) == 0 )
return 2147942480LL;
}
target_fd = open(target_fname, 524866, 484LL);
if ( target_fd != -1 )
return 0LL;
v6 = __errno_location();
v7 = strerror(*v6);
syslog(6, "Open Failed: %s", v7);
return 2147500037LL;
}

original_code:
static int hv_start_fcopy(struct hv_start_fcopy *smsg)
{
 int error = 0x80004005;
 char *q, *p;
 filesize = 0;
 p = (char *)smsg->path_name;
 snprintf(target_fname, sizeof(target_fname), "%s/%s",
   (char *)smsg->path_name, (char *)smsg->file_name);
 syslog(6, "Target file name: %s", target_fname);
 while ((q = strchr(p, '/')) != ((void *)0)) {
  if (q == p) {
   p++;
   continue;
  }
  *q = '\0';
  if (access((char *)smsg->path_name, 0)) {
   if (smsg->copy_flags & 0x2) {
    if (mkdir((char *)smsg->path_name, 0755)) {
     syslog(3, "Failed to create %s",
      (char *)smsg->path_name);
     goto done;
    }
   } else {
    syslog(3, "Invalid path: %s",
     (char *)smsg->path_name);
    goto done;
   }
  }
  p = q + 1;
  *q = '/';
 }
 if (!access(target_fname, 0)) {
  syslog(6, "File: %s exists", target_fname);
  if (!(smsg->copy_flags & 0x1)) {
   error = 0x80070050;
   goto done;
  }
 }
 target_fd = open(target_fname,
    02 | 0100 | 01000 | 02000000, 0744);
 if (target_fd == -1) {
  syslog(6, "Open Failed: %s", strerror((*__errno_location ())));
  goto done;
 }
 error = 0;
done:
 return error;
}

canonical_original_code:
static int hv_start_fcopy(struct hv_start_fcopy *smsg)
{
 int error = 0x80004005;
 char *q; char *p;
 filesize = 0;
 p = (char *)smsg->path_name;
 snprintf(target_fname, sizeof(target_fname), "%s/%s",
   (char *)smsg->path_name, (char *)smsg->file_name);
 syslog(6, "Target file name: %s", target_fname);
 while ((q = strchr(p, '/')) != ((void *)0)) {
  if (q == p) {
   p++;
   continue;
  }
  *q = '\0';
  if (access((char *)smsg->path_name, 0)) {
   if (smsg->copy_flags & 0x2) {
    if (mkdir((char *)smsg->path_name, 0755)) {
     syslog(3, "Failed to create %s",
      (char *)smsg->path_name);
     goto done;
    }
   } else {
    syslog(3, "Invalid path: %s",
     (char *)smsg->path_name);
    goto done;
   }
  }
  p = q + 1;
  *q = '/';
 }
 if (!access(target_fname, 0)) {
  syslog(6, "File: %s exists", target_fname);
  if (!(smsg->copy_flags & 0x1)) {
   error = 0x80070050;
   goto done;
  }
 }
 target_fd = open(target_fname,
    02 | 0100 | 01000 | 02000000, 0744);
 if (target_fd == -1) {
  syslog(6, "Open Failed: %s", strerror((*__errno_location ())));
  goto done;
 }
 error = 0;
done:
 return error;
}

variable_types: struct hv_start_fcopy { struct hv_fcopy_hdr { unsigned int operation; struct uuid_le { unsigned char b[16]; } service_id0; struct uuid_le service_id1; } hdr; unsigned short file_name[260]; unsigned short path_name[260]; unsigned int copy_flags; unsigned long long file_size; } *

variable_types: error int

variable_types:
q
char *

variable_types:
p
char *

user_defined_types: struct hv_start_fcopy { struct hv_fcopy_hdr hdr; unsigned short file_name[260]; unsigned short path_name[260]; unsigned int copy_flags; unsigned long long file_size; }

user_defined_types: struct hv_fcopy_hdr { unsigned int operation; struct uuid_le service_id0; struct uuid_le service_id1; }


decompiled_code:
__int64 hv_copy_finished()
{
close(target_fd);
return 0LL;
}

canonical_decompiled_code:
__int64 func9()
{
close(target_fd);
return 0LL;
}

original_code:
static int hv_copy_finished(void)
{
 close(target_fd);
 return 0;
}

canonical_original_code:
static int hv_copy_finished()
{
 close(target_fd);
 return 0;
}


decompiled_code:
__int64 hv_copy_cancel()
{
close(target_fd);
unlink(target_fname);
return 0LL;
}

canonical_decompiled_code:
__int64 func10()
{
close(target_fd);
unlink(target_fname);
return 0LL;
}

original_code:
static int hv_copy_cancel(void)
{
 close(target_fd);
 unlink(target_fname);
 return 0;
}

canonical_original_code:
static int hv_copy_cancel()
{
 close(target_fd);
 unlink(target_fname);
 return 0;
}


decompiled_code:
int __fastcall print_usage(const char **a1)
{
return fprintf(
_bss_start,
"Usage: %s [options]\n"
"Options are:\n"
" -n, --no-daemon stay in foreground, don't daemonize\n"
" -h, --help print this help\n",
*a1);
}

canonical_decompiled_code:
int __fastcall func11(const char **a1)
{
return fprintf(
_bss_start,
"Usage: %s [options]\n"
"Options are:\n"
" -n, --no-daemon stay in foreground, don't daemonize\n"
" -h, --help print this help\n",
*a1);
}

original_code:
void print_usage(char *argv[])
{
 fprintf(stderr, "Usage: %s [options]\n"
  "Options are:\n"
  "  -n, --no-daemon        stay in foreground, don't daemonize\n"
  "  -h, --help             print this help\n", argv[0]);
}

variable_types:
argv
char *[]


decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
const char **v3; // rbx
unsigned int v4; // er13
int v5; // eax
unsigned int v6; // er14
_DWORD *v7; // rax
struct epoll_event *v8; // r15
int v9; // eax
int v10; // ebx
epoll_data_t *v11; // rbp
__int64 v12; // r12
uint64_t v13; // rbx
int v14; // eax
_DWORD *v15; // rax

v3 = (const char **)utils_getopt(argc, argv, envp);
printf("Init server on port %s ...", *v3);
fflush(stdout);
v4 = net_listen(*v3);
utils_free_options(v3);
net_set_nonblock(v4);
v5 = epoll_create1(0);
if ( v5 < 0 )
{
perror("epoll");
exit(1);
}
v6 = v5;
v7 = malloc(0x20uLL);
v7[6] = v4;
net_epoll_interface_add(v6, v7);
puts("Ok");
v8 = (struct epoll_event *)calloc(0x80uLL, 0xCuLL);
do
{
LABEL_3:
v9 = epoll_wait(v6, v8, 128, -1);
v10 = v9;
if ( v9 < 0 )
{
perror("epoll");
exit(1);
}
net_check_sockets(v8, v9);
}
while ( v10 <= 0 );
v11 = &v8->data;
v12 = (__int64)&v8[(unsigned int)(v10 - 1) + 1].data;
while ( 1 )
{
v13 = v11->u64;
if ( v11->u64 )
{
if ( *(_DWORD *)(v13 + 24) == v4 )
{
net_accept_connections(v6, v4);
}
else
{
if ( *(_QWORD *)(v13 + 16) )
{
if ( *(_QWORD *)v13 )
net_close_proxy(v11->u64);
if ( (int)net_exchange(*(unsigned int *)(v13 + 24), *(unsigned int *)(v13 + 8), *(_QWORD *)(v13 + 16)) > 0 )
goto LABEL_11;
}
else
{
v14 = http_proxy_make_request(v6, v11->u64);
if ( v14 == -1 )
{
perror("http_proxy_make_request");
}
else if ( v14 != 1 )
{
v15 = calloc(1uLL, 0x20uLL);
v15[6] = *(_DWORD *)(v13 + 8);
*((_QWORD *)v15 + 2) = *(_QWORD *)v13;
v15[2] = *(_DWORD *)(v13 + 24);
*(_QWORD *)v15 = *(_QWORD *)(v13 + 16);
net_epoll_interface_add(v6, v13);
goto LABEL_11;
}
}
net_close_proxy(v13);
}
}
LABEL_11:
v11 = (epoll_data_t *)((char *)v11 + 12);
if ( v11 == (epoll_data_t *)v12 )
goto LABEL_3;
}
}

original_code:
int
main(int argc, char **argv)
{
 int i;
 int n;
 int ret;
 int sock;
 int epollfd;
 struct net_proxy *proxy;
 struct net_proxy *newproxy;
 struct epoll_event *events;
 struct utils_options *opts;
 opts = utils_getopt(argc, argv);
 printf("Init server on port %s ...", opts->port);
 fflush(stdout);
 sock = net_listen(opts->port);
 utils_free_options(opts);
 net_set_nonblock(sock);
 epollfd = epoll_create1(0);
 if(epollfd<0) {
  perror("epoll");
  exit(1);
 }
 proxy = malloc(sizeof(*proxy));
 proxy->fd = sock;
 net_epoll_interface_add(epollfd, proxy);
 puts("Ok");
 events = calloc(128, sizeof(*events));
 for( ; ; ) {
  ret = epoll_wait(epollfd, events, 128, -1);
  if(ret<0) {
   perror("epoll");
   exit(1);
  }
  net_check_sockets(events, ret);
  for(i=0, n=ret; i < n; i++) {
   if(!events[i].data.ptr)
    continue;
   proxy = events[i].data.ptr;
   if(proxy->fd==sock) {
    net_accept_connections(epollfd, sock);
    continue;
   }
   if(proxy->remaining) {
    if(proxy->peer.remaining)
     net_close_proxy(proxy);
    ret = net_exchange(proxy->fd, proxy->peer.fd, proxy->remaining);
    if(ret<=0)
     net_close_proxy(proxy);
    continue;
   }
   ret = http_proxy_make_request(epollfd, proxy);
   switch(ret) {
   case -1:
    perror("http_proxy_make_request");
   case 1:
    net_close_proxy(proxy);
    continue;
   }
   newproxy = calloc(1, sizeof(*proxy));
   newproxy->fd = proxy->peer.fd;
   newproxy->remaining = proxy->peer.remaining;
   newproxy->peer.fd = proxy->fd;
   newproxy->peer.remaining = proxy->remaining;
   net_epoll_interface_add(epollfd, proxy);
  }
 }
   return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: i int

variable_types: n int

variable_types: ret int

variable_types: sock int

variable_types: epollfd int

variable_types: struct net_proxy { struct { unsigned long remaining; int fd; } peer; unsigned long remaining; int fd; } *

variable_types: struct epoll_event { unsigned int events; union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; } data; } *

variable_types:
opts
struct utils_options { char *port; } *

user_defined_types: struct epoll_event { unsigned int events; union epoll_data data; }


decompiled_code:
void __fastcall __noreturn net_sys_err(const char *a1)
{
perror(a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(const char *a1)
{
perror(a1);
exit(1);
}

original_code:
static void
net_sys_err(const char *s)
{
 perror(s);
 exit(1);
}

canonical_original_code:
static void net_sys_err(char *s)
{
 perror(s);
 exit(1);
}

variable_types:
s
char *


decompiled_code:
bool net_check_errno_EAGAIN()
{
return *_errno_location() == 11;
}

canonical_decompiled_code:
bool func1()
{
return *_errno_location() == 11;
}

original_code:
static _Bool
net_check_errno_EAGAIN(void)
{
 return (((*__errno_location ())==11) || ((*__errno_location ())==11));
}

canonical_original_code:
static _Bool net_check_errno_EAGAIN()
{
 return (((*__errno_location ())==11) || ((*__errno_location ())==11));
}


decompiled_code:
__int64 __fastcall net_splice(int fdin, int fdout, size_t len)
{
unsigned int v4; // ebp
int v5; // eax
int v6; // edx
__int64 result; // rax
bool v8; // zf

v4 = 0;
while ( 1 )
{
v5 = splice(fdin, 0LL, fdout, 0LL, len, 3u);
v6 = v5;
if ( v5 <= 0 )
break;
v4 += v5;
len -= v5;
if ( !len )
return v4;
}
result = v4;
if ( v6 < 0 )
{
v8 = !net_check_errno_EAGAIN();
result = 0xFFFFFFFFLL;
if ( !v8 )
result = v4;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int fdin, int fdout, size_t len)
{
unsigned int v4; // ebp
int v5; // eax
int v6; // edx
__int64 result; // rax
bool v8; // zf

v4 = 0;
while ( 1 )
{
v5 = splice(fdin, 0LL, fdout, 0LL, len, 3u);
v6 = v5;
if ( v5 <= 0 )
break;
v4 += v5;
len -= v5;
if ( !len )
return v4;
}
result = v4;
if ( v6 < 0 )
{
v8 = !func1();
result = 0xFFFFFFFFLL;
if ( !v8 )
result = v4;
}
return result;
}

original_code:
static int
net_splice(int in, int out, size_t data_len)
{
 int len;
 size_t nbytes =0;
 do {
  len = splice(in, ((void *)0), out, ((void *)0), data_len,
  1 | 2);
  if(len <= 0) {
   if(len<0 && net_check_errno_EAGAIN())
    return nbytes;
   else if(!len)
    return nbytes;
   else
    return -1;
  }
  data_len -= len;
  nbytes += len;
 } while(data_len);
   return nbytes;
}

canonical_original_code:
static int net_splice(int in, int out, unsigned long data_len)
{
 int len;
 unsigned long nbytes = 0;
 do {
  len = splice(in, ((void *)0), out, ((void *)0), data_len,
  1 | 2);
  if(len <= 0) {
   if(len<0 && net_check_errno_EAGAIN())
    return nbytes;
   else if(!len)
    return nbytes;
   else
    return -1;
  }
  data_len -= len;
  nbytes += len;
 } while(data_len);
   return nbytes;
}

variable_types: in int

variable_types: out int

variable_types: len int

variable_types:
nbytes
unsigned long


decompiled_code:
int __fastcall net_set_nonblock(int fd)
{
int v1; // eax
int result; // eax

v1 = fcntl(fd, 3, 0LL);
if ( v1 < 0 )
net_sys_err("fcntl");
BYTE1(v1) |= 8u;
result = fcntl(fd, 4, (unsigned int)v1);
if ( result < 0 )
net_sys_err("fcntl");
return result;
}

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

v1 = fcntl(fd, 3, 0LL);
if ( v1 < 0 )
func0("fcntl");
BYTE1(v1) |= 8u;
result = fcntl(fd, 4, (unsigned int)v1);
if ( result < 0 )
func0("fcntl");
return result;
}

original_code:
void
net_set_nonblock(int sock)
{
 int ret;
 ret = fcntl(sock, 3, 0);
 if(ret<0)
  net_sys_err("fcntl");
 ret = fcntl(sock, 4, 04000|ret);
 if(ret<0)
  net_sys_err("fcntl");
    return;
}


decompiled_code:
int __fastcall net_epoll_interface_add(int a1, epoll_data_t a2)
{
struct epoll_event event; // [rsp+4h] [rbp-14h] BYREF

event.events = -2147483647;
event.data = a2;
return epoll_ctl(a1, 1, *(_DWORD *)(a2.u64 + 24), &event);
}

canonical_decompiled_code:
int __fastcall func4(int a1, epoll_data_t a2)
{
struct epoll_event event; // [rsp+4h] [rbp-14h] BYREF

event.events = -2147483647;
event.data = a2;
return epoll_ctl(a1, 1, *(_DWORD *)(a2.u64 + 24), &event);
}

original_code:
int
net_epoll_interface_add(int epollfd, struct net_proxy *proxy)
{
 struct epoll_event ev;
 ev.events = EPOLLET | EPOLLIN;
 ev.data.ptr = proxy;
 return epoll_ctl(epollfd, 1, proxy->fd, &ev);
}

variable_types: struct epoll_event { unsigned int events; union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; } data; }

user_defined_types: struct net_proxy { struct { unsigned long remaining; int fd; } peer; unsigned long remaining; int fd; }


decompiled_code:
void __fastcall net_accept_connections(int a1, int a2)
{
int v2; // eax
int v3; // ebx
int *v4; // rbp

do
{
while ( 1 )
{
v2 = accept(a2, 0LL, 0LL);
v3 = v2;
if ( v2 >= 0 )
break;
if ( net_check_errno_EAGAIN() )
return;
}
net_set_nonblock(v2);
v4 = (int *)calloc(1uLL, 0x20uLL);
v4[6] = v3;
}
while ( !net_epoll_interface_add(a1, (epoll_data_t)v4) );
if ( *_errno_location() != 28 )
net_sys_err("epoll");
close(v4[6]);
free(v4);
perror("epoll");
}

canonical_decompiled_code:
void __fastcall func5(int a1, int a2)
{
int v2; // eax
int v3; // ebx
int *v4; // rbp

do
{
while ( 1 )
{
v2 = accept(a2, 0LL, 0LL);
v3 = v2;
if ( v2 >= 0 )
break;
if ( func1() )
return;
}
func3(v2);
v4 = (int *)calloc(1uLL, 0x20uLL);
v4[6] = v3;
}
while ( !func4(a1, (epoll_data_t)v4) );
if ( *_errno_location() != 28 )
func0("epoll");
close(v4[6]);
free(v4);
perror("epoll");
}

original_code:
void
net_accept_connections(int epollfd, int sock_accept)
{
 int sock;
 int ret;
 struct net_proxy *proxy;
 while(1) {
  sock = accept(sock_accept, ((void *)0), ((void *)0));
  if(sock<0 && net_check_errno_EAGAIN())
   break;
  if(sock<0)
   continue;
  net_set_nonblock(sock);
  proxy = calloc(1, sizeof(*proxy));
  proxy->fd = sock;
  ret = net_epoll_interface_add(epollfd, proxy);
  if(!ret)
   continue;
  if((*__errno_location ())==28) {
   close(proxy->fd);
   free(proxy);
   perror("epoll");
   return;
  }
  net_sys_err("epoll");
 }
 return;
}


original_code:
void
net_check_sockets(struct epoll_event *evs, size_t n_events)
{
 int i;
 struct net_proxy *proxy;
 for(i=0; i < n_events ; i++ ) {
  if((evs[i].events & EPOLLERR) || (evs[i].events & EPOLLHUP) ||
                                   (!(evs[i].events & EPOLLIN)) ) {
   proxy = evs[i].data.ptr;
   if(!proxy)
    continue;
   close(proxy->fd);
   close(proxy->peer.fd);
   free(proxy);
   evs[i].data.ptr = ((void *)0);
  }
 }
}

canonical_original_code:
void net_check_sockets(struct epoll_event *evs, unsigned long n_events)
{
 int i;
 struct net_proxy *proxy;
 for(i=0; i < n_events ; i++ ) {
  if((evs[i].events & EPOLLERR) || (evs[i].events & EPOLLHUP) ||
                                   (!(evs[i].events & EPOLLIN)) ) {
   proxy = evs[i].data.ptr;
   if(!proxy)
    continue;
   close(proxy->fd);
   close(proxy->peer.fd);
   free(proxy);
   evs[i].data.ptr = ((void *)0);
  }
 }
}

user_defined_types: union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; }


decompiled_code:
__int64 __fastcall net_listen(char *service)
{
int v1; // eax
struct addrinfo *v2; // rbx
const char *v3; // rax
int v4; // eax
unsigned int v5; // ebp
int v6; // eax
struct addrinfo v8; // [rsp+0h] [rbp-58h] BYREF
struct addrinfo *pai; // [rsp+38h] [rbp-20h] BYREF

pai = 0LL;
*(_QWORD *)&v8.ai_flags = 1LL;
*(_QWORD *)&v8.ai_socktype = 1LL;
*(_QWORD *)&v8.ai_addrlen = 0LL;
v8.ai_addr = 0LL;
v8.ai_canonname = 0LL;
v8.ai_next = 0LL;
v1 = getaddrinfo(0LL, service, &v8, &pai);
if ( v1 )
{
v3 = gai_strerror(v1);
fprintf(stderr, "getaddrinfo: %s\n", v3);
exit(1);
}
v2 = pai;
if ( !pai )
{
LABEL_3:
fwrite("Could not bind\n", 1uLL, 0xFuLL, stderr);
exit(1);
}
while ( 1 )
{
v4 = socket(v2->ai_family, v2->ai_socktype, v2->ai_protocol);
v5 = v4;
if ( v4 >= 0 )
break;
LABEL_6:
v2 = v2->ai_next;
if ( !v2 )
goto LABEL_3;
}
v6 = bind(v4, v2->ai_addr, v2->ai_addrlen);
if ( v6 )
{
if ( v6 < 0 && *_errno_location() == 1 )
net_sys_err("bind");
close(v5);
goto LABEL_6;
}
freeaddrinfo(pai);
if ( listen(v5, 128) < 0 )
net_sys_err("listen");
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func7(char *service)
{
int v1; // eax
struct addrinfo *v2; // rbx
const char *v3; // rax
int v4; // eax
unsigned int v5; // ebp
int v6; // eax
struct addrinfo v8; // [rsp+0h] [rbp-58h] BYREF
struct addrinfo *pai; // [rsp+38h] [rbp-20h] BYREF

pai = 0LL;
*(_QWORD *)&v8.ai_flags = 1LL;
*(_QWORD *)&v8.ai_socktype = 1LL;
*(_QWORD *)&v8.ai_addrlen = 0LL;
v8.ai_addr = 0LL;
v8.ai_canonname = 0LL;
v8.ai_next = 0LL;
v1 = getaddrinfo(0LL, service, &v8, &pai);
if ( v1 )
{
v3 = gai_strerror(v1);
fprintf(stderr, "getaddrinfo: %s\n", v3);
exit(1);
}
v2 = pai;
if ( !pai )
{
LABEL_3:
fwrite("Could not bind\n", 1uLL, 0xFuLL, stderr);
exit(1);
}
while ( 1 )
{
v4 = socket(v2->ai_family, v2->ai_socktype, v2->ai_protocol);
v5 = v4;
if ( v4 >= 0 )
break;
LABEL_6:
v2 = v2->ai_next;
if ( !v2 )
goto LABEL_3;
}
v6 = bind(v4, v2->ai_addr, v2->ai_addrlen);
if ( v6 )
{
if ( v6 < 0 && *_errno_location() == 1 )
func0("bind");
close(v5);
goto LABEL_6;
}
freeaddrinfo(pai);
if ( listen(v5, 128) < 0 )
func0("listen");
return v5;
}

original_code:
int
net_listen(char *service)
{
 int sock;
 int ret;
 struct addrinfo *rp;
 struct addrinfo *result =((void *)0);
 struct addrinfo hints;
 memset(&hints, 0, sizeof(hints));
 hints.ai_family = 0;
 hints.ai_socktype = SOCK_STREAM;
 hints.ai_flags = 0x0001;
 hints.ai_protocol = 0;
 ret = getaddrinfo(((void *)0), service, &hints, &result);
 if(ret) {
  fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
  exit(1);
 }
 for(rp=result; rp; rp = rp->ai_next) {
  sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  if(sock<0)
   continue;
  ret = bind(sock, rp->ai_addr, rp->ai_addrlen);
  if(!ret)
   break;
  if(ret<0 && (*__errno_location ())==1)
   net_sys_err("bind");
  close(sock);
 }
 if(!rp) {
  fprintf(stderr, "Could not bind\n");
  exit(1);
 }
 freeaddrinfo(result);
 ret = listen(sock, 128);
 if(ret<0)
  net_sys_err("listen");
   return sock;
}

canonical_original_code:
int net_listen(char *service)
{
 int sock;
 int ret;
 struct addrinfo *rp;
 struct addrinfo *result = ((void *)0);
 struct addrinfo hints;
 memset(&hints, 0, sizeof(hints));
 hints.ai_family = 0;
 hints.ai_socktype = SOCK_STREAM;
 hints.ai_flags = 0x0001;
 hints.ai_protocol = 0;
 ret = getaddrinfo(((void *)0), service, &hints, &result);
 if(ret) {
  fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
  exit(1);
 }
 for(rp=result; rp; rp = rp->ai_next) {
  sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  if(sock<0)
   continue;
  ret = bind(sock, rp->ai_addr, rp->ai_addrlen);
  if(!ret)
   break;
  if(ret<0 && (*__errno_location ())==1)
   net_sys_err("bind");
  close(sock);
 }
 if(!rp) {
  fprintf(stderr, "Could not bind\n");
  exit(1);
 }
 freeaddrinfo(result);
 ret = listen(sock, 128);
 if(ret<0)
  net_sys_err("listen");
   return sock;
}

variable_types:
service
char *

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; } *

user_defined_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }


decompiled_code:
__int64 __fastcall net_connect(const char *a1, const char *a2)
{
unsigned int v2; // ebp
struct addrinfo *v3; // rbx
int v4; // eax
struct addrinfo *pai; // [rsp+8h] [rbp-50h] BYREF
struct addrinfo req; // [rsp+10h] [rbp-48h] BYREF

*(_QWORD *)&req.ai_flags = 0LL;
*(_QWORD *)&req.ai_socktype = 1LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
if ( getaddrinfo(a1, a2, &req, &pai) < 0 )
return (unsigned int)-1;
v3 = pai;
if ( pai )
{
while ( 1 )
{
v4 = socket(v3->ai_family, v3->ai_socktype, v3->ai_protocol);
v2 = v4;
if ( v4 < 0 )
break;
if ( connect(v4, v3->ai_addr, v3->ai_addrlen) )
{
v3 = v3->ai_next;
if ( v3 )
continue;
}
goto LABEL_6;
}
v2 = -1;
}
else
{
LABEL_6:
freeaddrinfo(pai);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func8(const char *a1, const char *a2)
{
unsigned int v2; // ebp
struct addrinfo *v3; // rbx
int v4; // eax
struct addrinfo *pai; // [rsp+8h] [rbp-50h] BYREF
struct addrinfo req; // [rsp+10h] [rbp-48h] BYREF

*(_QWORD *)&req.ai_flags = 0LL;
*(_QWORD *)&req.ai_socktype = 1LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
if ( getaddrinfo(a1, a2, &req, &pai) < 0 )
return (unsigned int)-1;
v3 = pai;
if ( pai )
{
while ( 1 )
{
v4 = socket(v3->ai_family, v3->ai_socktype, v3->ai_protocol);
v2 = v4;
if ( v4 < 0 )
break;
if ( connect(v4, v3->ai_addr, v3->ai_addrlen) )
{
v3 = v3->ai_next;
if ( v3 )
continue;
}
goto LABEL_6;
}
v2 = -1;
}
else
{
LABEL_6:
freeaddrinfo(pai);
}
return v2;
}

original_code:
int
net_connect(const char *host, const char *service)
{
 int sock;
 int ret;
 struct addrinfo hints;
 struct addrinfo *result;
 struct addrinfo *rp;
 memset(&hints, 0, sizeof(hints));
 hints.ai_family = 0;
 hints.ai_socktype = SOCK_STREAM;
 hints.ai_flags = 0;
 hints.ai_protocol = 0;
 ret = getaddrinfo(host, service, &hints, &result);
 if(ret<0)
  return -1;
 for(rp = result; rp; rp = rp->ai_next) {
  sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  if(sock<0)
   return -1;
  ret = connect(sock, rp->ai_addr, rp->ai_addrlen);
  if(!ret)
   break;
 }
 freeaddrinfo(result);
   return sock;
}

canonical_original_code:
int net_connect(char *host, char *service)
{
 int sock;
 int ret;
 struct addrinfo hints;
 struct addrinfo *result;
 struct addrinfo *rp;
 memset(&hints, 0, sizeof(hints));
 hints.ai_family = 0;
 hints.ai_socktype = SOCK_STREAM;
 hints.ai_flags = 0;
 hints.ai_protocol = 0;
 ret = getaddrinfo(host, service, &hints, &result);
 if(ret<0)
  return -1;
 for(rp = result; rp; rp = rp->ai_next) {
  sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  if(sock<0)
   return -1;
  ret = connect(sock, rp->ai_addr, rp->ai_addrlen);
  if(!ret)
   break;
 }
 freeaddrinfo(result);
   return sock;
}

variable_types:
host
char *

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }


decompiled_code:
size_t __fastcall net_send(int fd, __int64 a2, size_t a3)
{
size_t v3; // r14
size_t v4; // rbx
ssize_t v5; // rax
__int64 v6; // rbp

v3 = a3;
v4 = a3;
v5 = 0LL;
v6 = 0LL;
while ( 1 )
{
v6 += v5;
v5 = write(fd, (const void *)(a2 + v6), v4);
if ( v5 < 0 )
break;
v4 -= v5;
if ( !v4 )
return v3;
}
return -1LL;
}

canonical_decompiled_code:
size_t __fastcall func9(int fd, __int64 a2, size_t a3)
{
size_t v3; // r14
size_t v4; // rbx
ssize_t v5; // rax
__int64 v6; // rbp

v3 = a3;
v4 = a3;
v5 = 0LL;
v6 = 0LL;
while ( 1 )
{
v6 += v5;
v5 = write(fd, (const void *)(a2 + v6), v4);
if ( v5 < 0 )
break;
v4 -= v5;
if ( !v4 )
return v3;
}
return -1LL;
}

original_code:
ssize_t
net_send(int sock, const char *buf, size_t nbytes)
{
 size_t idx =0;
 ssize_t n =0;
 const size_t ret = nbytes;
 do {
  idx += n;
  n = write(sock, &buf[idx], nbytes);
  if(n < 0)
   return -1;
  nbytes -= n;
 } while(nbytes);
   return ret;
}

canonical_original_code:
long net_send(int sock, char *buf, unsigned long nbytes)
{
 unsigned long idx = 0;
 long n = 0;
 unsigned long ret = nbytes;
 do {
  idx += n;
  n = write(sock, &buf[idx], nbytes);
  if(n < 0)
   return -1;
  nbytes -= n;
 } while(nbytes);
   return ret;
}

variable_types:
buf
char *

variable_types:
idx
unsigned long

variable_types: n long

variable_types:
ret
unsigned long

return_type: long


decompiled_code:
ssize_t __fastcall net_recv(int fd, __int64 a2, size_t a3, int a4)
{
__int64 v6; // rbp
ssize_t v7; // r12
ssize_t result; // rax
bool v9; // zf

v6 = 0LL;
v7 = 0LL;
while ( 1 )
{
result = recv(fd, (void *)(a2 + v6), a3, a4);
if ( !result )
break;
if ( result < 0 )
{
v9 = !net_check_errno_EAGAIN();
result = -1LL;
if ( !v9 )
result = v7;
return result;
}
v7 += result;
v6 += result;
a3 -= result;
if ( !a3 )
return v7;
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func10(int fd, __int64 a2, size_t a3, int a4)
{
__int64 v6; // rbp
ssize_t v7; // r12
ssize_t result; // rax
bool v9; // zf

v6 = 0LL;
v7 = 0LL;
while ( 1 )
{
result = recv(fd, (void *)(a2 + v6), a3, a4);
if ( !result )
break;
if ( result < 0 )
{
v9 = !func1();
result = -1LL;
if ( !v9 )
result = v7;
return result;
}
v7 += result;
v6 += result;
a3 -= result;
if ( !a3 )
return v7;
}
return result;
}

original_code:
ssize_t
net_recv(int sock, char *buf, size_t nbytes, int flags)
{
 size_t n = 0;
 size_t idx = 0;
 ssize_t ret;
 do {
  ret = recv(sock, &buf[idx], nbytes, flags);
  if(!ret)
   return 0;
  if(ret<0 && net_check_errno_EAGAIN())
   return n;
  if(ret<0)
   return -1;
  n += ret;
  idx += ret;
  nbytes -= ret;
 } while(nbytes);
   return n;
}

canonical_original_code:
long net_recv(int sock, char *buf, unsigned long nbytes, int flags)
{
 unsigned long n = 0;
 unsigned long idx = 0;
 long ret;
 do {
  ret = recv(sock, &buf[idx], nbytes, flags);
  if(!ret)
   return 0;
  if(ret<0 && net_check_errno_EAGAIN())
   return n;
  if(ret<0)
   return -1;
  n += ret;
  idx += ret;
  nbytes -= ret;
 } while(nbytes);
   return n;
}

variable_types: sock int

variable_types: flags int

variable_types:
n
unsigned long

variable_types: ret long


decompiled_code:
__int64 __fastcall net_exchange(int fdin, int fdout, size_t len)
{
int v4; // eax
int v5; // ebx
int pipedes[2]; // [rsp+8h] [rbp-20h] BYREF

if ( pipe(pipedes) < 0 )
net_sys_err("pipe()");
v4 = net_splice(fdin, pipedes[1], len);
v5 = v4;
if ( v4 > 0 )
{
v5 = net_splice(pipedes[0], fdout, v4);
if ( v5 > 0 )
{
close(pipedes[0]);
close(pipedes[1]);
}
}
return (unsigned int)v5;
}

canonical_decompiled_code:
__int64 __fastcall func11(int fdin, int fdout, size_t len)
{
int v4; // eax
int v5; // ebx
int pipedes[2]; // [rsp+8h] [rbp-20h] BYREF

if ( pipe(pipedes) < 0 )
func0("pipe()");
v4 = func2(fdin, pipedes[1], len);
v5 = v4;
if ( v4 > 0 )
{
v5 = func2(pipedes[0], fdout, v4);
if ( v5 > 0 )
{
close(pipedes[0]);
close(pipedes[1]);
}
}
return (unsigned int)v5;
}

original_code:
int
net_exchange(int sock_in, int sock_out, size_t nbytes)
{
 int ret;
 int pipefd[2];
 ret = pipe(pipefd);
 if(ret<0)
  net_sys_err("pipe()");
 ret = net_splice(sock_in, pipefd[1], nbytes);
 if(ret<=0)
  return ret;
 ret = net_splice(pipefd[0], sock_out, ret);
 if(ret<=0)
  return ret;
 close(pipefd[0]);
 close(pipefd[1]);
   return ret;
}

canonical_original_code:
int net_exchange(int sock_in, int sock_out, unsigned long nbytes)
{
 int ret;
 int pipefd[2];
 ret = pipe(pipefd);
 if(ret<0)
  net_sys_err("pipe()");
 ret = net_splice(sock_in, pipefd[1], nbytes);
 if(ret<=0)
  return ret;
 ret = net_splice(pipefd[0], sock_out, ret);
 if(ret<=0)
  return ret;
 close(pipefd[0]);
 close(pipefd[1]);
   return ret;
}

variable_types: ret int

variable_types:
pipefd
int [2]


decompiled_code:
void __fastcall net_close_proxy(void *ptr)
{
int v2; // edi
int v3; // edi

if ( ptr )
{
v2 = *((_DWORD *)ptr + 6);
if ( v2 > 2 )
close(v2);
v3 = *((_DWORD *)ptr + 2);
if ( v3 > 2 )
close(v3);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func12(void *ptr)
{
int v2; // edi
int v3; // edi

if ( ptr )
{
v2 = *((_DWORD *)ptr + 6);
if ( v2 > 2 )
close(v2);
v3 = *((_DWORD *)ptr + 2);
if ( v3 > 2 )
close(v3);
free(ptr);
}
}

original_code:
void
net_close_proxy(struct net_proxy *proxy)
{
 if(!proxy)
  return;
 if(proxy->fd > 2)
  close(proxy->fd);
 if(proxy->peer.fd > 2 )
  close(proxy->peer.fd);
 free(proxy);
    return;
}

variable_types: struct net_proxy { struct { unsigned long remaining; int fd; } peer; unsigned long remaining; int fd; } *


decompiled_code:
void __fastcall http_free_url(void *ptr)
{
void *v2; // rdi
void *v3; // rdi
void *v4; // rdi
void *v5; // rdi

if ( ptr )
{
v2 = *(void **)ptr;
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 1);
if ( v3 )
free(v3);
v4 = (void *)*((_QWORD *)ptr + 2);
if ( v4 )
free(v4);
v5 = (void *)*((_QWORD *)ptr + 3);
if ( v5 )
free(v5);
free(ptr);
}
}

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

if ( ptr )
{
v2 = *(void **)ptr;
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 1);
if ( v3 )
free(v3);
v4 = (void *)*((_QWORD *)ptr + 2);
if ( v4 )
free(v4);
v5 = (void *)*((_QWORD *)ptr + 3);
if ( v5 )
free(v5);
free(ptr);
}
}

original_code:
static void
http_free_url(struct http_url *url)
{
 if(!url)
  return;
 if(url->scheme)
  free(url->scheme);
 if(url->host)
  free(url->host);
 if(url->path)
  free(url->path);
 if(url->service)
  free(url->service);
 free(url);
 return;
}

variable_types: struct http_url { char *scheme; char *host; char *path; char *service; char issecure; } *


decompiled_code:
void __fastcall http_free_request(__int64 a1)
{
void *v2; // rdi
void *v3; // rdi

if ( a1 )
{
v2 = *(void **)(a1 + 8);
if ( v2 )
free(v2);
v3 = *(void **)(a1 + 16);
if ( v3 )
free(v3);
http_free_url(*(void **)a1);
}
}

original_code:
static void
http_free_request(struct http_request *req)
{
 if(!req)
  return;
 if(req->method)
  free(req->method);
 if(req->protocol)
  free(req->protocol);
 http_free_url(req->url);
 return;
}

variable_types: struct http_request { struct http_url { char *scheme; char *host; char *path; char *service; char issecure; } *url; char *method; char *protocol; unsigned long content_length; } *

user_defined_types: struct http_request { struct http_url *url; char *method; char *protocol; unsigned long content_length; }


decompiled_code:
__int64 __fastcall http_proxy_make_request(__int64 a1, __int64 a2)
{
const char *v2; // rax
char *v3; // r12
_QWORD *v4; // rax
__int64 v5; // r13
int v6; // eax
int v7; // ebx
char *v8; // rax
char *v9; // r14
size_t v10; // rax
__int64 v11; // rdx
int v13; // eax

v2 = (const char *)http_get_header(*(unsigned int *)(a2 + 24));
if ( v2 )
{
v3 = (char *)v2;
v4 = http_parse_request(v2);
v5 = (__int64)v4;
if ( v4 )
{
v6 = net_connect(*(_QWORD *)(*v4 + 8LL), *(_QWORD *)(*v4 + 24LL));
v7 = v6;
if ( v6 >= 0 )
{
net_set_nonblock((unsigned int)v6);
*(_DWORD *)(a2 + 8) = v7;
v7 = dprintf(
v7,
"%s %s %s%s",
*(const char **)(v5 + 8),
*(const char **)(*(_QWORD *)v5 + 16LL),
*(const char **)(v5 + 16),
"\r\n");
if ( v7 >= 0 )
{
v8 = strstr(v3, "\r\n");
v9 = v8;
if ( v8 )
{
v10 = strlen(v8);
v7 = net_send(*(unsigned int *)(a2 + 8), v9, v10);
if ( v7 >= 0 )
{
v11 = *(_QWORD *)(v5 + 24);
*(_QWORD *)(a2 + 16) = v11;
if ( v11 )
{
v13 = net_exchange(*(unsigned int *)(a2 + 24), *(unsigned int *)(a2 + 8));
v7 = v13;
if ( v13 <= 0 )
net_close_proxy(a2);
else
*(_QWORD *)(a2 + 16) -= v13;
}
}
}
}
}
free(v3);
http_free_request(v5);
}
else
{
free(v3);
v7 = 1;
}
}
else
{
v7 = -1;
}
return (unsigned int)v7;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2)
{
const char *v2; // rax
char *v3; // r12
_QWORD *v4; // rax
__int64 v5; // r13
int v6; // eax
int v7; // ebx
char *v8; // rax
char *v9; // r14
size_t v10; // rax
__int64 v11; // rdx
int v13; // eax

v2 = (const char *)func2(*(unsigned int *)(a2 + 24));
if ( v2 )
{
v3 = (char *)v2;
v4 = http_parse_request(v2);
v5 = (__int64)v4;
if ( v4 )
{
v6 = net_connect(*(_QWORD *)(*v4 + 8LL), *(_QWORD *)(*v4 + 24LL));
v7 = v6;
if ( v6 >= 0 )
{
net_set_nonblock((unsigned int)v6);
*(_DWORD *)(a2 + 8) = v7;
v7 = dprintf(
v7,
"%s %s %s%s",
*(const char **)(v5 + 8),
*(const char **)(*(_QWORD *)v5 + 16LL),
*(const char **)(v5 + 16),
"\r\n");
if ( v7 >= 0 )
{
v8 = strstr(v3, "\r\n");
v9 = v8;
if ( v8 )
{
v10 = strlen(v8);
v7 = net_send(*(unsigned int *)(a2 + 8), v9, v10);
if ( v7 >= 0 )
{
v11 = *(_QWORD *)(v5 + 24);
*(_QWORD *)(a2 + 16) = v11;
if ( v11 )
{
v13 = net_exchange(*(unsigned int *)(a2 + 24), *(unsigned int *)(a2 + 8));
v7 = v13;
if ( v13 <= 0 )
net_close_proxy(a2);
else
*(_QWORD *)(a2 + 16) -= v13;
}
}
}
}
}
free(v3);
func1(v5);
}
else
{
free(v3);
v7 = 1;
}
}
else
{
v7 = -1;
}
return (unsigned int)v7;
}

original_code:
int
http_proxy_make_request(int epollfd, struct net_proxy *proxy)
{
 int ret;
 char *header;
 char *ptr;
 struct http_request *req;
 header = http_get_header(proxy->fd);
 if(!header)
  return -1;
 req = http_parse_request(header);
 if(!req) {
  free(header);
  return 1;
 }
 ret = net_connect(req->url->host, req->url->service);
 if(ret<0)
  goto end_func;
 net_set_nonblock(ret);
 proxy->peer.fd = ret;
 ret=dprintf(proxy->peer.fd, "%s %s %s%s", req->method, req->url->path,
                            req->protocol, "\r\n");
 if(ret<0)
  goto end_func;
 ptr = strstr(header, "\r\n");
 if(!ptr)
  goto end_func;
 ret = net_send(proxy->peer.fd, ptr, strlen(ptr));
 if(ret<0)
  goto end_func;
 proxy->remaining = req->content_length;
 if(proxy->remaining) {
  ret =net_exchange(proxy->fd, proxy->peer.fd, proxy->remaining);
  if(ret<=0) {
   net_close_proxy(proxy);
   goto end_func;
  }
  proxy->remaining -= ret;
 }
end_func:
 free(header);
 http_free_request(req);
 return ret;
}

variable_types: epollfd int

variable_types:
header
char *

variable_types:
ptr
char *

user_defined_types: struct http_url { char *scheme; char *host; char *path; char *service; char issecure; }


decompiled_code:
__int64 __fastcall utils_parse_number(char *nptr)
{
int *v1; // rbx
__int64 result; // rax

v1 = _errno_location();
*v1 = 0;
result = strtol(nptr, 0LL, 10);
if ( *v1 == 34 && (unsigned __int64)(result + 0x7FFFFFFFFFFFFFFFLL) > 0xFFFFFFFFFFFFFFFDLL )
return -1LL;
if ( !result )
{
if ( *v1 )
result = -1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *nptr)
{
int *v1; // rbx
__int64 result; // rax

v1 = _errno_location();
*v1 = 0;
result = strtol(nptr, 0LL, 10);
if ( *v1 == 34 && (unsigned __int64)(result + 0x7FFFFFFFFFFFFFFFLL) > 0xFFFFFFFFFFFFFFFDLL )
return -1LL;
if ( !result )
{
if ( *v1 )
result = -1LL;
}
return result;
}

original_code:
long int
utils_parse_number(const char *s)
{
 long int n;
 (*__errno_location ()) = 0;
 n = strtol(s, (char**)0, 0x0A);
 if(((*__errno_location ()) == 34 && (n==0x7fffffffffffffffL || n==(-0x7fffffffffffffffL - 1L)))
                               || ((*__errno_location ()) != 0 && n== 0))
  return -1;
 return n;
}

canonical_original_code:
long utils_parse_number(char *s)
{
 long n;
 (*__errno_location ()) = 0;
 n = strtol(s, (char **)0, 0x0A);
 if(((*__errno_location ()) == 34 && (n==0x7fffffffffffffffL || n==(-0x7fffffffffffffffL - 1L)))
                               || ((*__errno_location ()) != 0 && n== 0))
  return -1;
 return n;
}

variable_types:
s
char *


decompiled_code:
void **__fastcall utils_getopt(int argc, char **argv)
{
void **v2; // rbx
char v3; // r13
char v4; // al
char *v6; // r15

opterr = 0;
v2 = (void **)malloc(8uLL);
*v2 = strdup("8080");
v3 = 0;
while ( 1 )
{
while ( 1 )
{
v4 = getopt(argc, argv, ":hdp:b:");
if ( v4 != 100 )
break;
v3 = 1;
}
if ( v4 <= 100 )
break;
if ( v4 != 112 )
goto LABEL_15;
v6 = optarg;
if ( !optarg )
{
fwrite("Error: Optior -p has a null argument.\n", 1uLL, 0x26uLL, stderr);
exit(1);
}
free(*v2);
*v2 = strdup(v6);
}
if ( v4 != -1 )
{
if ( v4 == 58 )
{
fprintf(stderr, "Option %c requires an operand\n", (unsigned int)optopt);
exit(1);
}
LABEL_15:
fprintf(stderr, "Usage: %s [-d] [-p port/service]\n", *argv);
exit(1);
}
if ( v3 )
{
printf("Going to background...");
daemon(1, 0);
puts("\tOk");
}
return v2;
}

canonical_decompiled_code:
void **__fastcall func1(int argc, char **argv)
{
void **v2; // rbx
char v3; // r13
char v4; // al
char *v6; // r15

opterr = 0;
v2 = (void **)malloc(8uLL);
*v2 = strdup("8080");
v3 = 0;
while ( 1 )
{
while ( 1 )
{
v4 = getopt(argc, argv, ":hdp:b:");
if ( v4 != 100 )
break;
v3 = 1;
}
if ( v4 <= 100 )
break;
if ( v4 != 112 )
goto LABEL_15;
v6 = optarg;
if ( !optarg )
{
fwrite("Error: Optior -p has a null argument.\n", 1uLL, 0x26uLL, stderr);
exit(1);
}
free(*v2);
*v2 = strdup(v6);
}
if ( v4 != -1 )
{
if ( v4 == 58 )
{
fprintf(stderr, "Option %c requires an operand\n", (unsigned int)optopt);
exit(1);
}
LABEL_15:
fprintf(stderr, "Usage: %s [-d] [-p port/service]\n", *argv);
exit(1);
}
if ( v3 )
{
printf("Going to background...");
daemon(1, 0);
puts("\tOk");
}
return v2;
}

original_code:
struct utils_options*
utils_getopt(int argc, char* const argv[])
{
 char c;
 uint8_t background = 0x00;
 extern int opterr;
 extern int optopt;
 extern char *optarg;
 struct utils_options *opts;
 opterr = 0;
 opts = malloc(sizeof(*opts));
 opts->port = strdup("8080");
again:
 c = getopt(argc, argv, ":hdp:b:");
 switch(c) {
 case 'p':
  if(!optarg) {
   fprintf(stderr,
        "Error: Optior -p has a null argument.\n");
   exit(1);
  }
  free(opts->port);
  opts->port = strdup(optarg);
  goto again;
 case 'd':
  background = 0x01;
  goto again;
 case ':':
  fprintf(stderr, "Option %c requires an operand\n", optopt);
  exit(1);
 case -1:
  break;
 default:
  fprintf(stderr, "Usage: %s [-d] [-p port/service]\n", argv[0]);
  exit(1);
 }
 if(background) {
  printf("Going to background...");
  daemon(0x01, 0x00);
  printf("\tOk\n");
 }
 return opts;
}

canonical_original_code:
struct utils_options *utils_getopt(int argc, char *argv[])
{
 char c;
 unsigned char background = 0x00;
 extern int opterr;
 extern int optopt;
 extern char *optarg;
 struct utils_options *opts;
 opterr = 0;
 opts = malloc(sizeof(*opts));
 opts->port = strdup("8080");
again:
 c = getopt(argc, argv, ":hdp:b:");
 switch(c) {
 case 'p':
  if(!optarg) {
   fprintf(stderr,
        "Error: Optior -p has a null argument.\n");
   exit(1);
  }
  free(opts->port);
  opts->port = strdup(optarg);
  goto again;
 case 'd':
  background = 0x01;
  goto again;
 case ':':
  fprintf(stderr, "Option %c requires an operand\n", optopt);
  exit(1);
 case -1:
  break;
 default:
  fprintf(stderr, "Usage: %s [-d] [-p port/service]\n", argv[0]);
  exit(1);
 }
 if(background) {
  printf("Going to background...");
  daemon(0x01, 0x00);
  printf("\tOk\n");
 }
 return opts;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: c char

variable_types:
background
unsigned char

variable_types: opterr int

variable_types: optopt int

variable_types:
optarg
char *

variable_types:
opts
struct utils_options { char *port; } *

return_type: struct utils_options { char *port; } *


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

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

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

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

original_code:
void
utils_free_options(struct utils_options *opts)
{
 if(!opts)
  return;
 if(opts->port)
  free(opts->port);
 free(opts);
}


decompiled_code:
__int64 __fastcall minheap_heapify(__int64 a1, int a2, int a3)
{
int v3; // er9
__int64 result; // rax
__int64 *v5; // rcx
__int64 v6; // rsi
__int64 *v7; // rax

v3 = 2 * a2 + 2;
result = (unsigned int)(2 * a2 + 1);
if ( (int)result < a3 && **(_DWORD **)(a1 + 8LL * (int)result) < **(_DWORD **)(a1 + 8LL * a2) )
{
if ( v3 >= a3 )
{
v3 = 2 * a2 + 1;
}
else if ( **(_DWORD **)(a1 + 8LL * v3) >= **(_DWORD **)(a1 + 8LL * (int)result) )
{
v3 = 2 * a2 + 1;
}
goto LABEL_10;
}
if ( v3 < a3 )
{
result = **(unsigned int **)(a1 + 8LL * a2);
if ( **(_DWORD **)(a1 + 8LL * v3) < (int)result )
{
LABEL_10:
if ( v3 != a2 )
{
v5 = (__int64 *)(a1 + 8LL * a2);
v6 = *v5;
v7 = (__int64 *)(a1 + 8LL * v3);
*v5 = *v7;
*v7 = v6;
result = minheap_heapify(a1, (unsigned int)v3);
}
return result;
}
}
return result;
}

original_code:
void minheap_heapify(PCB *A[], int i, int n){
 int right = 2 * (i+1);
 int left = right - 1;
 int min = i;
 PCB *tmp = 0;
 if (left < n && A[left]->priority < A[i]->priority) {
  min = left;
 }
 if (right < n && A[right]->priority < A[min]->priority) {
  min = right;
 }
 if (min != i) {
  tmp = A[i];
  A[i] = A[min];
  A[min] = tmp;
  minheap_heapify(A, min, n);
 }
}

canonical_original_code:
void minheap_heapify(struct PCB *A[], int i, int n){
 int right = 2 * (i+1);
 int left = right - 1;
 int min = i;
 struct PCB *tmp = 0;
 if (left < n && A[left]->priority < A[i]->priority) {
  min = left;
 }
 if (right < n && A[right]->priority < A[min]->priority) {
  min = right;
 }
 if (min != i) {
  tmp = A[i];
  A[i] = A[min];
  A[min] = tmp;
  minheap_heapify(A, min, n);
 }
}

variable_types: struct PCB { int priority; int pid; int time_left; int time_arrived; int time_waiting; int turnaround; } *[]

variable_types: i int

variable_types: n int

variable_types: right int

variable_types: left int

variable_types: min int


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

if ( a2 > 0 )
{
v2 = a2;
do
result = minheap_heapify(a1, --v2 / 2, a2);
while ( v2 );
}
return result;
}

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

if ( a2 > 0 )
{
v2 = a2;
do
result = func0(a1, --v2 / 2, a2);
while ( v2 );
}
return result;
}

original_code:
void minheap_build(PCB *A[], int n){
 for(int i = n; i > 0; i--){
  int parent = (i-1) / 2;
  minheap_heapify(A, parent, n);
 }
}

canonical_original_code:
void minheap_build(struct PCB *A[], int n){
 for(int i = n; i > 0; i--){
  int parent = (i-1) / 2;
  minheap_heapify(A, parent, n);
 }
}

variable_types: parent int

user_defined_types: struct PCB { int priority; int pid; int time_left; int time_arrived; int time_waiting; int turnaround; }


original_code:
PCB *minheap_pop_min(PCB *A[], int *n) {
 PCB *min = A[0];
 A[0] = A[*n - 1];
 A[*n - 1] = ((void *)0);
 A = realloc(A, sizeof(PCB *) * (*n - 1));
 *n -= 1;
 minheap_heapify(A, 0, *n);
 return min;
}

canonical_original_code:
struct PCB *minheap_pop_min(struct PCB *A[], int *n) {
 struct PCB *min = A[0];
 A[0] = A[*n - 1];
 A[*n - 1] = ((void *)0);
 A = realloc(A, sizeof(struct PCB *) * (*n - 1));
 *n -= 1;
 minheap_heapify(A, 0, *n);
 return min;
}

variable_types:
A
struct PCB *[]

variable_types:
n
int *

variable_types: struct PCB { int priority; int pid; int time_left; int time_arrived; int time_waiting; int turnaround; } *


decompiled_code:
void minheap_insert()
{
;
}

canonical_decompiled_code:
void func3()
{
;
}

original_code:
void minheap_insert(PCB *A[], PCB *item, int *n) {
}

canonical_original_code:
void minheap_insert(struct PCB *A[], struct PCB *item, int *n) {
}

variable_types:
item
struct PCB *


decompiled_code:
int __fastcall epoll_add(int a1, epoll_data_t a2)
{
int result; // eax
int *v3; // rax
struct epoll_event event; // [rsp+4h] [rbp-14h] BYREF

event.events = 1;
event.data = a2;
result = epoll_ctl(a1, 1, *(_DWORD *)(a2.u64 + 424), &event);
if ( result )
{
v3 = _errno_location();
syslog(3, "failed to add peer's connection to epoll fd, errno %d", (unsigned int)*v3);
exit(1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(int a1, epoll_data_t a2)
{
int result; // eax
int *v3; // rax
struct epoll_event event; // [rsp+4h] [rbp-14h] BYREF

event.events = 1;
event.data = a2;
result = epoll_ctl(a1, 1, *(_DWORD *)(a2.u64 + 424), &event);
if ( result )
{
v3 = _errno_location();
syslog(3, "failed to add peer's connection to epoll fd, errno %d", (unsigned int)*v3);
exit(1);
}
return result;
}

original_code:
static void
epoll_add(int epfd, struct peer *peer)
{
 struct epoll_event ev;
 memset(&ev, 0, sizeof(ev));
 ev.events = EPOLLIN;
 ev.data.ptr = peer;
 int error = epoll_ctl(epfd, 1, peer->proto.conn, &ev);
 if (error) {
  syslog(3, "failed to add peer's connection to epoll fd, errno %d", (*__errno_location ()));
  exit(1);
 }
}

variable_types: epfd int

variable_types: struct peer { enum peer_state { PEER_DISCONNECTED=0, PEER_CONNECTED=1 } state; int addr_family; struct sockaddr_in6 { unsigned short sin6_family; unsigned short sin6_port; unsigned int sin6_flowinfo; struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; } sin6_addr; unsigned int sin6_scope_id; } addr; struct version_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; int version; unsigned long services; unsigned long timestamp; struct netaddr { unsigned int timestamp; unsigned long services; struct in6_addr ip; unsigned short port; } recv_addr; struct netaddr trans_addr; unsigned long nonce; unsigned char user_agent_len; char user_agent[-1]; int start_height; unsigned char relay; } version; struct peer *next; struct protocol { int conn; enum protocol_state { WAIT_HEADER=0, WAIT_MESSAGE=1, HAVE_MESSAGE=2 } state; struct buffer { union { unsigned long len; unsigned long idx; }; unsigned long size; unsigned char bytes[]; } *buf; struct msg_hdr { unsigned char start[4]; char command[12]; unsigned int payload_size; unsigned char checksum[4]; } hdr; union message { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; struct version_msg version; struct ping_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; unsigned long nonce; } ping; struct addr_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; unsigned long count; struct netaddr addr_list[]; } addr; struct getheaders_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; unsigned int version; unsigned long hash_count; unsigned char stop_hash[32]; unsigned char hash[32]; } getheaders; struct inv_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; unsigned long count; struct inv_vec { unsigned int type; unsigned char hash[32]; } inv_vec[]; } inv; struct headers_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; unsigned long count; struct block_hdr { unsigned int version; unsigned char prev_hash[32]; unsigned char merkle_root[32]; unsigned int timestamp; unsigned int bits; unsigned int nonce; unsigned long tx_count; } headers[]; } headers; struct block_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; struct block_hdr hdr; struct tx_msg { unsigned int version; unsigned char id[32]; unsigned long in_count; struct tx_input { struct outpoint { unsigned char hash[32]; unsigned int index; } prev_output; unsigned int seq; unsigned long script_len; unsigned char script[]; } **in; unsigned long out_count; struct tx_output { unsigned long value; unsigned long pk_script_len; unsigned char pk_script[]; } **out; unsigned int locktime; } tx[]; } block; } *msg; } proto; } *

variable_types: struct epoll_event { unsigned int events; union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; } data; }

variable_types: error int

user_defined_types: struct peer { enum peer_state state; int addr_family; struct sockaddr_in6 addr; struct version_msg version; struct peer *next; struct protocol proto; }

user_defined_types: struct sockaddr_in6 { unsigned short sin6_family; unsigned short sin6_port; unsigned int sin6_flowinfo; struct in6_addr sin6_addr; unsigned int sin6_scope_id; }

user_defined_types: struct version_msg { enum msg_types type; int version; unsigned long services; unsigned long timestamp; struct netaddr recv_addr; struct netaddr trans_addr; unsigned long nonce; unsigned char user_agent_len; char user_agent[-1]; int start_height; unsigned char relay; }

user_defined_types: struct protocol { int conn; enum protocol_state state; struct buffer *buf; struct msg_hdr hdr; union message *msg; }

user_defined_types: union message { enum msg_types type; struct version_msg version; struct ping_msg ping; struct addr_msg addr; struct getheaders_msg getheaders; struct inv_msg inv; struct headers_msg headers; struct block_msg block; }

user_defined_types: struct ping_msg { enum msg_types type; unsigned long nonce; }

user_defined_types: struct addr_msg { enum msg_types type; unsigned long count; struct netaddr addr_list[]; }

user_defined_types: struct getheaders_msg { enum msg_types type; unsigned int version; unsigned long hash_count; unsigned char stop_hash[32]; unsigned char hash[32]; }

user_defined_types: struct inv_msg { enum msg_types type; unsigned long count; struct inv_vec inv_vec[]; }

user_defined_types: struct headers_msg { enum msg_types type; unsigned long count; struct block_hdr headers[]; }

user_defined_types: struct block_msg { enum msg_types type; struct block_hdr hdr; struct tx_msg tx[]; }

user_defined_types: struct tx_msg { unsigned int version; unsigned char id[32]; unsigned long in_count; struct tx_input **in; unsigned long out_count; struct tx_output **out; unsigned int locktime; }

user_defined_types: struct tx_input { struct outpoint prev_output; unsigned int seq; unsigned long script_len; unsigned char script[]; }


decompiled_code:
int __fastcall epoll_del(int a1, __int64 a2)
{
int result; // eax
int *v3; // rax
struct epoll_event event; // [rsp+4h] [rbp-14h] BYREF

*(_QWORD *)&event.events = 0LL;
HIDWORD(event.data.u64) = 0;
result = epoll_ctl(a1, 2, *(_DWORD *)(a2 + 424), &event);
if ( result )
{
v3 = _errno_location();
syslog(3, "failed to remove peer's connection from epoll fd, errno %d", (unsigned int)*v3);
exit(1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(int a1, __int64 a2)
{
int result; // eax
int *v3; // rax
struct epoll_event event; // [rsp+4h] [rbp-14h] BYREF

*(_QWORD *)&event.events = 0LL;
HIDWORD(event.data.u64) = 0;
result = epoll_ctl(a1, 2, *(_DWORD *)(a2 + 424), &event);
if ( result )
{
v3 = _errno_location();
syslog(3, "failed to remove peer's connection from epoll fd, errno %d", (unsigned int)*v3);
exit(1);
}
return result;
}

original_code:
static void
epoll_del(int epfd, struct peer *peer)
{
 struct epoll_event ev;
 memset(&ev, 0, sizeof(ev));
 int error = epoll_ctl(epfd, 2, peer->proto.conn, &ev);
 if (error) {
  syslog(3, "failed to remove peer's connection from epoll fd, errno %d", (*__errno_location ()));
  exit(1);
 }
}

user_defined_types: struct epoll_event { unsigned int events; union epoll_data data; }

user_defined_types: union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; }

user_defined_types: enum peer_state { PEER_DISCONNECTED=0, PEER_CONNECTED=1 }

user_defined_types: struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; }

user_defined_types: enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 }

user_defined_types: struct netaddr { unsigned int timestamp; unsigned long services; struct in6_addr ip; unsigned short port; }

user_defined_types: enum protocol_state { WAIT_HEADER=0, WAIT_MESSAGE=1, HAVE_MESSAGE=2 }

user_defined_types: struct msg_hdr { unsigned char start[4]; char command[12]; unsigned int payload_size; unsigned char checksum[4]; }

user_defined_types: struct buffer { union { unsigned long len; unsigned long idx; }; unsigned long size; unsigned char bytes[]; }

user_defined_types: struct inv_vec { unsigned int type; unsigned char hash[32]; }

user_defined_types: struct block_hdr { unsigned int version; unsigned char prev_hash[32]; unsigned char merkle_root[32]; unsigned int timestamp; unsigned int bits; unsigned int nonce; unsigned long tx_count; }

user_defined_types: struct tx_output { unsigned long value; unsigned long pk_script_len; unsigned char pk_script[]; }

user_defined_types: struct outpoint { unsigned char hash[32]; unsigned int index; }


original_code:
static int
request_headers(int fd)
{
 struct getheaders_msg msg;
 msg.version = 70014;
 msg.hash_count = 1;
 memset(msg.stop_hash, 0, sizeof(msg.stop_hash));
 memcpy(msg.hash, genesis_block_hash, sizeof(genesis_block_hash));
 return write_getheaders_msg(fd, &msg);
}

variable_types: fd int

variable_types: struct getheaders_msg { enum msg_types { MSG_UNKNOWN=0, MSG_VERSION=1, MSG_VERACK=2, MSG_PING=3, MSG_PONG=4, MSG_ADDR=5, MSG_INV=6, MSG_HEADERS=7, MSG_BLOCK=8 } type; unsigned int version; unsigned long hash_count; unsigned char stop_hash[32]; unsigned char hash[32]; }

user_defined_types: struct getheaders_msg { enum msg_types type; unsigned int version; unsigned long hash_count; unsigned char stop_hash[32]; unsigned char hash[32]; }