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

result = 1LL;
if ( *(_DWORD *)(a2 + 8) != a1 )
result = *(_DWORD *)(a2 + 12) == a1;
return result;
}

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

result = 1LL;
if ( *(_DWORD *)(a2 + 8) != a1 )
result = *(_DWORD *)(a2 + 12) == a1;
return result;
}

original_code:
static int write_filter(int sock, void *data)
{
 struct amt_server *server = data;
 if(server->sock_tcp == sock)
  return 1;
 if(server->sock_udp == sock)
  return 1;
 return 0;
}

variable_types: sock int

variable_types:
data
void *

variable_types: struct amt_server { struct amt_event_base { struct list_head { struct list_head *next; } head; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; unsigned long loop_tid; int event_num; int exit_flag; } *event_base; int sock_tcp; unsigned short tcp_port; struct amt_server_callback { void (*log_cb)(int tag, char *log); void (*update_test)(char *test); void (*sensor_data)(unsigned int num, struct amt_sensor_data { short sensor_type; float data[3]; } *sensor); void (*mouse_data)(int x, int y, int button, int press); void (*touch_data)(unsigned int num, int *x, int *y, int *press); void (*key_data)(int code, int press); } cb; struct amt_log_handle { int log_flag; void (*log_cb)(int tag, char *log); } log_handle; struct protocol_handle { void *data; struct amt_log_handle *log; int (*cmd_response)(void *arg, unsigned short cmd, int retval); int (*update_udp_port)(void *arg, unsigned short port); int (*update_test)(void *arg, char *test); int (*cmd_ack)(void *arg, struct protocol_event { short type; short magic; union { struct control_data { short cmd; short direct; union { char b8[32]; short b16[16]; int b32[8]; } argv; int ret; } control; struct sensor_data { short sensor_type; float data[3]; } sensor[13]; struct mouse_data { short button; short press; int x; int y; } mouse; struct touch_data { unsigned int num; int x[8]; int y[8]; int press[8]; } touch; struct key_data { int keycode; int keypress; } key; char test[32]; } packet; } *event); int (*sensor_control)(void *arg, int sensor, int on); int (*sensor_delay)(void *arg, int sensor, int delay); int (*sensor_data)(void *arg, unsigned int num, struct amt_sensor_data *data); int (*mouse_data)(void *arg, int x, int y, int button, int press); int (*touch_data)(void *arg, unsigned int num, int *x, int *y, int *press); int (*key_data)(void *arg, int code, int press); } protocol; union pthread_mutex_t cmd_mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } cmd_cond; unsigned short last_cmd; int last_ret; int cond_count; } *

user_defined_types: struct amt_server { struct amt_event_base *event_base; int sock_tcp; unsigned short tcp_port; struct amt_server_callback cb; struct amt_log_handle log_handle; struct protocol_handle protocol; union pthread_mutex_t cmd_mutex; union pthread_cond_t cmd_cond; unsigned short last_cmd; int last_ret; int cond_count; }

user_defined_types: struct amt_server_callback { void (*log_cb)(int tag, char *log); void (*update_test)(char *test); void (*sensor_data)(unsigned int num, struct amt_sensor_data *sensor); void (*mouse_data)(int x, int y, int button, int press); void (*touch_data)(unsigned int num, int *x, int *y, int *press); void (*key_data)(int code, int press); }

user_defined_types: struct protocol_handle { void *data; struct amt_log_handle *log; int (*cmd_response)(void *arg, unsigned short cmd, int retval); int (*update_udp_port)(void *arg, unsigned short port); int (*update_test)(void *arg, char *test); int (*cmd_ack)(void *arg, struct protocol_event *event); int (*sensor_control)(void *arg, int sensor, int on); int (*sensor_delay)(void *arg, int sensor, int delay); int (*sensor_data)(void *arg, unsigned int num, struct amt_sensor_data *data); int (*mouse_data)(void *arg, int x, int y, int button, int press); int (*touch_data)(void *arg, unsigned int num, int *x, int *y, int *press); int (*key_data)(void *arg, int code, int press); }

user_defined_types: union pthread_cond_t { struct __pthread_cond_s __data; char __size[48]; long long __align; }

user_defined_types: struct protocol_event { short type; short magic; union { struct control_data control; struct sensor_data sensor[13]; struct mouse_data mouse; struct touch_data touch; struct key_data key; char test[32]; } packet; }


decompiled_code:
__int64 __fastcall event_listen_cb(__int64 a1)
{
__int64 v1; // r12
unsigned int v2; // eax
__int64 v3; // rbp
const char *v4; // rax
_BYTE v6[192]; // [rsp+0h] [rbp-E8h] BYREF
char v7[40]; // [rsp+C0h] [rbp-28h] BYREF

v1 = *(_QWORD *)(a1 + 16);
v2 = amt_sock_accept(*(unsigned int *)(a1 + 24), v7);
v3 = amt_event_set(*(_QWORD *)(a1 + 16), v2, 1LL);
amt_event_add(**(_QWORD **)(a1 + 16), v3, event_read_cb, v3);
v4 = (const char *)amt_get_ip(v7);
_amt_log(32LL, v1 + 72, "%s accept ip: %s\n", "event_listen_cb", v4);
cmd_set_udp_port(v1 + 88, v6, *(unsigned __int16 *)(v1 + 18));
return amt_event_buffer_write(v3, v6, 186LL, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
__int64 v1; // r12
unsigned int v2; // eax
__int64 v3; // rbp
const char *v4; // rax
_BYTE v6[192]; // [rsp+0h] [rbp-E8h] BYREF
char v7[40]; // [rsp+C0h] [rbp-28h] BYREF

v1 = *(_QWORD *)(a1 + 16);
v2 = amt_sock_accept(*(unsigned int *)(a1 + 24), v7);
v3 = amt_event_set(*(_QWORD *)(a1 + 16), v2, 1LL);
amt_event_add(**(_QWORD **)(a1 + 16), v3, event_read_cb, v3);
v4 = (const char *)amt_get_ip(v7);
_amt_log(32LL, v1 + 72, "%s accept ip: %s\n", "event_listen_cb", v4);
cmd_set_udp_port(v1 + 88, v6, *(unsigned __int16 *)(v1 + 18));
return amt_event_buffer_write(v3, v6, 186LL, 0LL);
}

original_code:
static void event_listen_cb(void *arg)
{
 struct sockaddr addr;
 int new_sock;
 struct protocol_event packet;
 struct amt_event *new_event;
 struct amt_event *event = arg;
 struct amt_server *server = (struct amt_server *)event->base;
 new_sock = amt_sock_accept(event->sock, &addr);
 new_event = amt_event_set(event->base, new_sock, 1);
 amt_event_add(*event->base, new_event, event_read_cb, new_event);
 __amt_log((1 << 5), &server->log_handle, "%s accept ip: %s\n", __func__, amt_get_ip(&addr));
 cmd_set_udp_port(&server->protocol, &packet, server->udp_port);
 amt_event_buffer_write(new_event, &packet, sizeof(struct protocol_event), ((void *)0));
}

variable_types:
arg
void *

variable_types: struct sockaddr { unsigned short sa_family; char sa_data[14]; }

variable_types: struct protocol_event { short type; short magic; union { struct control_data { short cmd; short direct; union { char b8[32]; short b16[16]; int b32[8]; } argv; int ret; } control; struct sensor_data { short sensor_type; float data[3]; } sensor[13]; struct mouse_data { short button; short press; int x; int y; } mouse; struct touch_data { unsigned int num; int x[8]; int y[8]; int press[8]; } touch; struct key_data { int keycode; int keypress; } key; char test[32]; } packet; }

variable_types: struct amt_event { struct list_head { struct list_head *next; } list; struct amt_event_base { struct list_head head; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; unsigned long loop_tid; int event_num; int exit_flag; } **base; int sock; int tcp_udp_type; void (*read_cb)(void *arg); void *data; struct list_head write_list; union pthread_mutex_t write_mutex; int status; int err_code; } *

return_type: void

user_defined_types: struct amt_event { struct list_head list; struct amt_event_base **base; int sock; int tcp_udp_type; void (*read_cb)(void *arg); void *data; struct list_head write_list; union pthread_mutex_t write_mutex; int status; int err_code; }

user_defined_types: struct amt_log_handle { int log_flag; void (*log_cb)(int tag, char *log); }

user_defined_types: struct amt_event_base { struct list_head head; union pthread_mutex_t mutex; unsigned long loop_tid; int event_num; int exit_flag; }

user_defined_types: struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }

user_defined_types: struct amt_sensor_data { short sensor_type; float data[3]; }


decompiled_code:
__int64 __fastcall cmd_response(__int64 a1, unsigned __int16 a2, unsigned int a3)
{
_amt_log(2LL, a1 + 72, "%s: cmd = %d, ret = %d\n", "cmd_response", a2, a3);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 184));
if ( *(_WORD *)(a1 + 272) == a2 )
{
if ( a3 != -1 )
*(_DWORD *)(a1 + 276) = a3;
++*(_DWORD *)(a1 + 280);
pthread_cond_signal((pthread_cond_t *)(a1 + 224));
}
pthread_mutex_unlock((pthread_mutex_t *)(a1 + 184));
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, unsigned __int16 a2, unsigned int a3)
{
_amt_log(2LL, a1 + 72, "%s: cmd = %d, ret = %d\n", "cmd_response", a2, a3);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 184));
if ( *(_WORD *)(a1 + 272) == a2 )
{
if ( a3 != -1 )
*(_DWORD *)(a1 + 276) = a3;
++*(_DWORD *)(a1 + 280);
pthread_cond_signal((pthread_cond_t *)(a1 + 224));
}
pthread_mutex_unlock((pthread_mutex_t *)(a1 + 184));
return 0LL;
}

original_code:
static int cmd_response(void *arg, unsigned short cmd, int retval)
{
 struct amt_server *server = arg;
 __amt_log((1 << 1), &server->log_handle, "%s: cmd = %d, ret = %d\n", __func__, cmd, retval);
 pthread_mutex_lock(&server->cmd_mutex);
 if(cmd == server->last_cmd)
 {
  if(retval != -1)
   server->last_ret = retval;
  server->cond_count++;
  pthread_cond_signal(&server->cmd_cond);
 }
 pthread_mutex_unlock(&server->cmd_mutex);
 return 0;
}

variable_types:
cmd
unsigned short

variable_types: retval int

return_type: int

user_defined_types: struct list_head { struct list_head *next; }

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }

user_defined_types: struct control_data { short cmd; short direct; union { char b8[32]; short b16[16]; int b32[8]; } argv; int ret; }

user_defined_types: struct mouse_data { short button; short press; int x; int y; }

user_defined_types: struct touch_data { unsigned int num; int x[8]; int y[8]; int press[8]; }

user_defined_types: struct key_data { int keycode; int keypress; }

user_defined_types: struct sensor_data { short sensor_type; float data[3]; }


decompiled_code:
__int64 __fastcall send_command_common(__int64 a1, __int64 a2)
{
__int64 v2; // rbx
pthread_mutex_t *v3; // r12
int v4; // ebp
unsigned int v5; // ebx
struct timespec v7; // [rsp+0h] [rbp-38h] BYREF
struct timeval tv; // [rsp+10h] [rbp-28h] BYREF

if ( *(_DWORD *)a1 != 1 )
return (unsigned int)-1;
v2 = *(_QWORD *)(a1 + 8);
v3 = (pthread_mutex_t *)(v2 + 184);
pthread_mutex_lock((pthread_mutex_t *)(v2 + 184));
*(_DWORD *)(v2 + 276) = -1;
*(_DWORD *)(v2 + 280) = 0;
*(_WORD *)(v2 + 272) = *(_WORD *)(a2 + 4);
v4 = amt_event_buffer_write_all(*(_QWORD *)v2, a2, 186LL, 0LL, write_filter, v2);
gettimeofday(&tv, 0LL);
v7.tv_sec = tv.tv_sec + 3;
v7.tv_nsec = 1000 * tv.tv_usec;
while ( *(_DWORD *)(v2 + 280) < v4
&& pthread_cond_timedwait((pthread_cond_t *)(v2 + 224), (pthread_mutex_t *)(v2 + 184), &v7) != 110 )
;
v5 = *(_DWORD *)(v2 + 276);
pthread_mutex_unlock(v3);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, __int64 a2)
{
__int64 v2; // rbx
pthread_mutex_t *v3; // r12
int v4; // ebp
unsigned int v5; // ebx
struct timespec v7; // [rsp+0h] [rbp-38h] BYREF
struct timeval tv; // [rsp+10h] [rbp-28h] BYREF

if ( *(_DWORD *)a1 != 1 )
return (unsigned int)-1;
v2 = *(_QWORD *)(a1 + 8);
v3 = (pthread_mutex_t *)(v2 + 184);
pthread_mutex_lock((pthread_mutex_t *)(v2 + 184));
*(_DWORD *)(v2 + 276) = -1;
*(_DWORD *)(v2 + 280) = 0;
*(_WORD *)(v2 + 272) = *(_WORD *)(a2 + 4);
v4 = amt_event_buffer_write_all(*(_QWORD *)v2, a2, 186LL, 0LL, write_filter, v2);
gettimeofday(&tv, 0LL);
v7.tv_sec = tv.tv_sec + 3;
v7.tv_nsec = 1000 * tv.tv_usec;
while ( *(_DWORD *)(v2 + 280) < v4
&& pthread_cond_timedwait((pthread_cond_t *)(v2 + 224), (pthread_mutex_t *)(v2 + 184), &v7) != 110 )
;
v5 = *(_DWORD *)(v2 + 276);
pthread_mutex_unlock(v3);
return v5;
}

original_code:
static int send_command_common(struct amt_handle *handle, struct protocol_event *event)
{
 int count, ret;
 struct amt_server *server;
 struct timeval now;
 struct timespec outtime;
 if(handle->type != 1)
  return -1;
 server = handle->point;
 pthread_mutex_lock(&server->cmd_mutex);
 server->last_ret = -1;
 server->cond_count = 0;
 server->last_cmd = event->packet.control.cmd;
 count = amt_event_buffer_write_all(server->event_base, event, sizeof(struct protocol_event), ((void *)0), write_filter, server);
 gettimeofday(&now, ((void *)0));
 outtime.tv_sec = now.tv_sec + 3;
 outtime.tv_nsec = now.tv_usec * 1000;
 while(count > server->cond_count)
 {
  ret = pthread_cond_timedwait(&server->cmd_cond, &server->cmd_mutex, &outtime);
  if(ret == 110)
   break;
 }
 ret = server->last_ret;
 pthread_mutex_unlock(&server->cmd_mutex);
 return ret;
}

canonical_original_code:
static int send_command_common(struct amt_handle *handle, struct protocol_event *event)
{
 int count; int ret;
 struct amt_server *server;
 struct timeval now;
 struct timespec outtime;
 if(handle->type != 1)
  return -1;
 server = handle->point;
 pthread_mutex_lock(&server->cmd_mutex);
 server->last_ret = -1;
 server->cond_count = 0;
 server->last_cmd = event->packet.control.cmd;
 count = amt_event_buffer_write_all(server->event_base, event, sizeof(struct protocol_event), ((void *)0), write_filter, server);
 gettimeofday(&now, ((void *)0));
 outtime.tv_sec = now.tv_sec + 3;
 outtime.tv_nsec = now.tv_usec * 1000;
 while(count > server->cond_count)
 {
  ret = pthread_cond_timedwait(&server->cmd_cond, &server->cmd_mutex, &outtime);
  if(ret == 110)
   break;
 }
 ret = server->last_ret;
 pthread_mutex_unlock(&server->cmd_mutex);
 return ret;
}

variable_types: struct amt_handle { int type; void *point; } *

variable_types: struct protocol_event { short type; short magic; union { struct control_data { short cmd; short direct; union { char b8[32]; short b16[16]; int b32[8]; } argv; int ret; } control; struct sensor_data { short sensor_type; float data[3]; } sensor[13]; struct mouse_data { short button; short press; int x; int y; } mouse; struct touch_data { unsigned int num; int x[8]; int y[8]; int press[8]; } touch; struct key_data { int keycode; int keypress; } key; char test[32]; } packet; } *

variable_types: count int

variable_types: ret int

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

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

user_defined_types: struct amt_server { struct amt_event_base *event_base; int sock_tcp; unsigned short tcp_port; struct amt_server_callback cb; struct amt_log_handle log_handle; struct protocol_handle protocol; union pthread_mutex_t cmd_mutex; union pthread_cond_t cmd_cond; unsigned short last_cmd; int last_ret; int cond_count; }

user_defined_types: struct amt_server_callback { void (*log_cb)(int tag, char *log); void (*update_test)(char *test); void (*sensor_data)(unsigned int num, struct amt_sensor_data *sensor); void (*mouse_data)(int x, int y, int button, int press); void (*touch_data)(unsigned int num, int *x, int *y, int *press); void (*key_data)(int code, int press); }

user_defined_types: struct protocol_handle { void *data; struct amt_log_handle *log; int (*cmd_response)(void *arg, unsigned short cmd, int retval); int (*update_udp_port)(void *arg, unsigned short port); int (*update_test)(void *arg, char *test); int (*cmd_ack)(void *arg, struct protocol_event *event); int (*sensor_control)(void *arg, int sensor, int on); int (*sensor_delay)(void *arg, int sensor, int delay); int (*sensor_data)(void *arg, unsigned int num, struct amt_sensor_data *data); int (*mouse_data)(void *arg, int x, int y, int button, int press); int (*touch_data)(void *arg, unsigned int num, int *x, int *y, int *press); int (*key_data)(void *arg, int code, int press); }


decompiled_code:
__int64 __fastcall deinit_server_sock(void *ptr)
{
__int64 result; // rax

if ( *(_DWORD *)ptr == 1 )
{
amt_event_base_deinit(**((_QWORD **)ptr + 1));
free(*((void **)ptr + 1));
free(ptr);
result = communicator_deinit();
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(void *ptr)
{
__int64 result; // rax

if ( *(_DWORD *)ptr == 1 )
{
amt_event_base_deinit(**((_QWORD **)ptr + 1));
free(*((void **)ptr + 1));
free(ptr);
result = communicator_deinit();
}
return result;
}

original_code:
void deinit_server_sock(struct amt_handle *handle)
{
 struct amt_server *server;
 if(handle->type != 1)
  return;
 server = handle->point;
 amt_event_base_deinit(server->event_base);
 free(handle->point);
 free(handle);
 communicator_deinit();
}

user_defined_types: struct amt_handle { int type; void *point; }

user_defined_types: union pthread_cond_t { struct __pthread_cond_s __data; char __size[48]; long long __align; }

user_defined_types: struct protocol_event { short type; short magic; union { struct control_data control; struct sensor_data sensor[13]; struct mouse_data mouse; struct touch_data touch; struct key_data key; char test[32]; } packet; }


decompiled_code:
__int64 __fastcall control_server_log(__int64 a1, __int64 a2)
{
__int64 v2; // rdi
__int64 result; // rax

if ( *(_DWORD *)a1 == 1 )
{
v2 = *(_QWORD *)(a1 + 8);
if ( *(_QWORD *)(v2 + 24) )
result = amt_log_control(v2 + 72, a2);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, __int64 a2)
{
__int64 v2; // rdi
__int64 result; // rax

if ( *(_DWORD *)a1 == 1 )
{
v2 = *(_QWORD *)(a1 + 8);
if ( *(_QWORD *)(v2 + 24) )
result = amt_log_control(v2 + 72, a2);
}
return result;
}

original_code:
void control_server_log(struct amt_handle *handle, int tag_on)
{
 struct amt_server *server;
 if(handle->type != 1)
  return;
 server = handle->point;
 if(server->cb.log_cb)
  amt_log_control(&server->log_handle, tag_on);
}

user_defined_types: struct amt_log_handle { int log_flag; void (*log_cb)(int tag, char *log); }

user_defined_types: struct amt_event_base { struct list_head head; union pthread_mutex_t mutex; unsigned long loop_tid; int event_num; int exit_flag; }

user_defined_types: struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; }


decompiled_code:
__int64 __fastcall sensor_server_control(__int64 a1, unsigned int a2, unsigned int a3)
{
_BYTE v4[200]; // [rsp-C8h] [rbp-C8h] BYREF

if ( *(_DWORD *)a1 != 1 )
return 0xFFFFFFFFLL;
cmd_set_sensor_control(*(_QWORD *)(a1 + 8) + 88LL, v4, a2, a3);
return send_command_common(a1, (__int64)v4);
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, unsigned int a2, unsigned int a3)
{
_BYTE v4[200]; // [rsp-C8h] [rbp-C8h] BYREF

if ( *(_DWORD *)a1 != 1 )
return 0xFFFFFFFFLL;
cmd_set_sensor_control(*(_QWORD *)(a1 + 8) + 88LL, v4, a2, a3);
return func9(a1, (__int64)v4);
}

original_code:
int sensor_server_control(struct amt_handle *handle, int sensor, int on)
{
 struct protocol_event packet;
 struct amt_server *server;
 if(handle->type != 1)
  return -1;
 server = handle->point;
 cmd_set_sensor_control(&server->protocol, &packet, sensor, on);
 return send_command_common(handle, &packet);
}

variable_types: sensor int

variable_types: on int

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }

user_defined_types: struct amt_sensor_data { short sensor_type; float data[3]; }


decompiled_code:
__int64 __fastcall sensor_server_delay(__int64 a1, unsigned int a2, unsigned int a3)
{
_BYTE v4[200]; // [rsp-C8h] [rbp-C8h] BYREF

if ( *(_DWORD *)a1 != 1 )
return 0xFFFFFFFFLL;
cmd_set_sensor_delay(*(_QWORD *)(a1 + 8) + 88LL, v4, a2, a3);
return send_command_common(a1, (__int64)v4);
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1, unsigned int a2, unsigned int a3)
{
_BYTE v4[200]; // [rsp-C8h] [rbp-C8h] BYREF

if ( *(_DWORD *)a1 != 1 )
return 0xFFFFFFFFLL;
cmd_set_sensor_delay(*(_QWORD *)(a1 + 8) + 88LL, v4, a2, a3);
return func9(a1, (__int64)v4);
}

original_code:
int sensor_server_delay(struct amt_handle *handle, int sensor, int delay)
{
 struct protocol_event packet;
 struct amt_server *server;
 if(handle->type != 1)
  return -1;
 server = handle->point;
 cmd_set_sensor_delay(&server->protocol, &packet, sensor, delay);
 return send_command_common(handle, &packet);
}

variable_types: delay int

user_defined_types: struct control_data { short cmd; short direct; union { char b8[32]; short b16[16]; int b32[8]; } argv; int ret; }

user_defined_types: struct list_head { struct list_head *next; }

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }


decompiled_code:
__int64 __fastcall update_test(__int64 a1, __int64 a2)
{
void (__fastcall *v2)(__int64); // rax

v2 = *(void (__fastcall **)(__int64))(a1 + 32);
if ( v2 )
v2(a2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
void (__fastcall *v2)(__int64); // rax

v2 = *(void (__fastcall **)(__int64))(a1 + 32);
if ( v2 )
v2(a2);
return 0LL;
}

original_code:
static int update_test(void *arg, char *test)
{
 struct amt_server *server = arg;
 if(server->cb.update_test)
  server->cb.update_test(test);
 return 0;
}

variable_types:
test
char *

user_defined_types: struct mouse_data { short button; short press; int x; int y; }

user_defined_types: struct touch_data { unsigned int num; int x[8]; int y[8]; int press[8]; }

user_defined_types: struct key_data { int keycode; int keypress; }


decompiled_code:
__int64 __fastcall event_read_cb(__int64 a1)
{
__int64 v1; // rbp
__int64 result; // rax
_BYTE v3[192]; // [rsp+0h] [rbp-E8h] BYREF
char v4[40]; // [rsp+C0h] [rbp-28h] BYREF

v1 = *(_QWORD *)(a1 + 16);
result = amt_event_buffer_read(a1, v3, 186LL, v4);
if ( (int)result <= 0 )
{
_amt_log(32LL, v1 + 72, "socket error\n");
result = amt_event_del_safe(a1);
}
else if ( (_DWORD)result == 186 )
{
result = recv_packet(v1 + 88, v3);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
__int64 v1; // rbp
__int64 result; // rax
_BYTE v3[192]; // [rsp+0h] [rbp-E8h] BYREF
char v4[40]; // [rsp+C0h] [rbp-28h] BYREF

v1 = *(_QWORD *)(a1 + 16);
result = amt_event_buffer_read(a1, v3, 186LL, v4);
if ( (int)result <= 0 )
{
_amt_log(32LL, v1 + 72, "socket error\n");
result = amt_event_del_safe(a1);
}
else if ( (_DWORD)result == 186 )
{
result = recv_packet(v1 + 88, v3);
}
return result;
}

original_code:
static void event_read_cb(void *arg)
{
 int size;
 struct sockaddr addr;
 struct protocol_event packet;
 struct amt_event *event = arg;
 struct amt_server *server = (struct amt_server *)event->base;
 size = amt_event_buffer_read(event, &packet, sizeof(struct protocol_event), &addr);
 if(size <= 0)
 {
  __amt_log((1 << 5), &server->log_handle, "socket error\n");
  amt_event_del_safe(event);
 }
 else if(size == sizeof(struct protocol_event))
  recv_packet(&server->protocol, &packet);
}

variable_types: size int

variable_types: struct amt_server { struct amt_event_base { struct list_head { struct list_head *next; } head; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; unsigned long loop_tid; int event_num; int exit_flag; } *event_base; int sock_tcp; unsigned short tcp_port; struct amt_server_callback { void (*log_cb)(int tag, char *log); void (*update_test)(char *test); void (*sensor_data)(unsigned int num, struct amt_sensor_data { short sensor_type; float data[3]; } *sensor); void (*mouse_data)(int x, int y, int button, int press); void (*touch_data)(unsigned int num, int *x, int *y, int *press); void (*key_data)(int code, int press); } cb; struct amt_log_handle { int log_flag; void (*log_cb)(int tag, char *log); } log_handle; struct protocol_handle { void *data; struct amt_log_handle *log; int (*cmd_response)(void *arg, unsigned short cmd, int retval); int (*update_udp_port)(void *arg, unsigned short port); int (*update_test)(void *arg, char *test); int (*cmd_ack)(void *arg, struct protocol_event { short type; short magic; union { struct control_data { short cmd; short direct; union { char b8[32]; short b16[16]; int b32[8]; } argv; int ret; } control; struct sensor_data { short sensor_type; float data[3]; } sensor[13]; struct mouse_data { short button; short press; int x; int y; } mouse; struct touch_data { unsigned int num; int x[8]; int y[8]; int press[8]; } touch; struct key_data { int keycode; int keypress; } key; char test[32]; } packet; } *event); int (*sensor_control)(void *arg, int sensor, int on); int (*sensor_delay)(void *arg, int sensor, int delay); int (*sensor_data)(void *arg, unsigned int num, struct amt_sensor_data *data); int (*mouse_data)(void *arg, int x, int y, int button, int press); int (*touch_data)(void *arg, unsigned int num, int *x, int *y, int *press); int (*key_data)(void *arg, int code, int press); } protocol; union pthread_mutex_t cmd_mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } cmd_cond; unsigned short last_cmd; int last_ret; int cond_count; } *

user_defined_types: struct sockaddr { unsigned short sa_family; char sa_data[14]; }

user_defined_types: struct amt_event { struct list_head list; struct amt_event_base **base; int sock; int tcp_udp_type; void (*read_cb)(void *arg); void *data; struct list_head write_list; union pthread_mutex_t write_mutex; int status; int err_code; }

user_defined_types: struct amt_server { struct amt_event_base *event_base; int sock_tcp; unsigned short tcp_port; struct amt_server_callback cb; struct amt_log_handle log_handle; struct protocol_handle protocol; union pthread_mutex_t cmd_mutex; union pthread_cond_t cmd_cond; unsigned short last_cmd; int last_ret; int cond_count; }

user_defined_types: struct amt_server_callback { void (*log_cb)(int tag, char *log); void (*update_test)(char *test); void (*sensor_data)(unsigned int num, struct amt_sensor_data *sensor); void (*mouse_data)(int x, int y, int button, int press); void (*touch_data)(unsigned int num, int *x, int *y, int *press); void (*key_data)(int code, int press); }


decompiled_code:
void __fastcall amt_log_register(__int64 a1, __int64 a2)
{
*(_DWORD *)a1 = 0;
*(_QWORD *)(a1 + 8) = a2;
}

original_code:
void amt_log_register(struct amt_log_handle *handle, amt_log_callback cb)
{
 handle->log_flag = 0;
 handle->log_cb = cb;
}

canonical_original_code:
void amt_log_register(struct amt_log_handle *handle, void (*cb)(int tag, char *log))
{
 handle->log_flag = 0;
 handle->log_cb = cb;
}

variable_types: struct amt_log_handle { int log_flag; void (*log_cb)(int tag, char *log); } *

variable_types:
cb
void (*)(int tag, char *log)


decompiled_code:
void __fastcall amt_log_control(int *a1, char a2)
{
*a1 = a2 & 0x3F;
}

canonical_decompiled_code:
void __fastcall func2(int *a1, char a2)
{
*a1 = a2 & 0x3F;
}

original_code:
void amt_log_control(struct amt_log_handle *handle, int tag_on)
{
 handle->log_flag = tag_on & ((1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4) |(1 << 5));
}


repo: araisrobo/minsoc

decompiled_code:
int __fastcall print_or32_dis_usage(const char **a1)
{
puts(" OpenRISC instruction decoder");
putchar(10);
puts(" Decodes an instruction passed on the command line, otherwise reads from stdin.");
printf(
" The option %s prints out the original instruction along with the decoded version\n"
" similar to the objdump disassembler output, except without address information\n",
"-print-orig-insn");
putchar(10);
puts(" Usage:");
putchar(10);
puts(" Decode a single instruction from the command line:");
printf(" \t%s 0x1500000\n", *a1);
putchar(10);
puts(" Decode a list of instructions from stdin:");
printf(" \t%s < list_of_insns\n", *a1);
putchar(10);
puts(" Decode a list of instructions from stdin, and print out the original instruction as well:");
printf(" \t%s -print-orig-insn < list_of_insns\n", *a1);
putchar(10);
puts(" Note: The values don't require the leading \"0x\"");
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func0(const char **a1)
{
puts(" OpenRISC instruction decoder");
putchar(10);
puts(" Decodes an instruction passed on the command line, otherwise reads from stdin.");
printf(
" The option %s prints out the original instruction along with the decoded version\n"
" similar to the objdump disassembler output, except without address information\n",
"-print-orig-insn");
putchar(10);
puts(" Usage:");
putchar(10);
puts(" Decode a single instruction from the command line:");
printf(" \t%s 0x1500000\n", *a1);
putchar(10);
puts(" Decode a list of instructions from stdin:");
printf(" \t%s < list_of_insns\n", *a1);
putchar(10);
puts(" Decode a list of instructions from stdin, and print out the original instruction as well:");
printf(" \t%s -print-orig-insn < list_of_insns\n", *a1);
putchar(10);
puts(" Note: The values don't require the leading \"0x\"");
return putchar(10);
}

original_code:
static void print_or32_dis_usage(char *argv[])
{
  printf(" OpenRISC instruction decoder\n");
  printf("\n");
  printf(" Decodes an instruction passed on the command line, otherwise reads from stdin.\n");
  printf(" The option %s prints out the original instruction along with the decoded version\n similar to the objdump disassembler output, except without address information\n", "-print-orig-insn");
  printf("\n");
  printf(" Usage:\n");
  printf("\n");
  printf(" Decode a single instruction from the command line:\n");
  printf(" \t%s 0x1500000\n", argv[0]);
  printf("\n");
  printf(" Decode a list of instructions from stdin:\n");
  printf(" \t%s < list_of_insns\n", argv[0]);
  printf("\n");
  printf(" Decode a list of instructions from stdin, and print out the original instruction as well:\n");
  printf(" \t%s -print-orig-insn < list_of_insns\n", argv[0]);
  printf("\n");
  printf(" Note: The values don't require the leading \"0x\"\n");
  printf("\n");
  return;
}

variable_types:
argv
char *[]


decompiled_code:
_BOOL8 __fastcall or32_opcode_match(unsigned __int64 a1, char *a2)
{
unsigned int v2; // ebp
int v3; // edx
_BOOL4 v4; // ecx

v2 = or32_extract(49, a2, a1);
v3 = or32_extract(48, a2, a1);
v4 = 0;
if ( (v2 & (unsigned int)a1) == (unsigned __int64)v2 )
v4 = (v3 & (unsigned int)a1) == 0LL;
return v4;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(unsigned __int64 a1, char *a2)
{
unsigned int v2; // ebp
int v3; // edx
_BOOL4 v4; // ecx

v2 = func1(49, a2, a1);
v3 = func1(48, a2, a1);
v4 = 0;
if ( (v2 & (unsigned int)a1) == (unsigned __int64)v2 )
v4 = (v3 & (unsigned int)a1) == 0LL;
return v4;
}

original_code:
static int
or32_opcode_match (unsigned long insn, char *encoding)
{
  unsigned long ones, zeros;
  ones = or32_extract ('1', encoding, insn);
  zeros = or32_extract ('0', encoding, insn);
  insn &= 0xffffffff;
  ones &= 0xffffffff;
  zeros &= 0xffffffff;
  if ((insn & ones) != ones)
    {
      return 0;
    }
  if ((~insn & zeros) != zeros)
    {
      return 0;
    }
  return 1;
}

canonical_original_code:
static int or32_opcode_match(unsigned long insn, char *encoding)
{
  unsigned long ones; unsigned long zeros;
  ones = or32_extract ('1', encoding, insn);
  zeros = or32_extract ('0', encoding, insn);
  insn &= 0xffffffff;
  ones &= 0xffffffff;
  zeros &= 0xffffffff;
  if ((insn & ones) != ones)
    {
      return 0;
    }
  if ((~insn & zeros) != zeros)
    {
      return 0;
    }
  return 1;
}

variable_types:
insn
unsigned long

variable_types:
encoding
char *

variable_types:
ones
unsigned long

variable_types:
zeros
unsigned long


original_code:
static unsigned long
or32_extract (char param_ch, char *enc_initial, unsigned long insn)
{
  char *enc;
  unsigned long ret = 0;
  int opc_pos = 0;
  int param_pos = 0;
  for (enc = enc_initial; *enc != '\0'; enc++)
    if (*enc == param_ch)
      {
 if (enc - 2 >= enc_initial && (*(enc - 2) == '0') && (*(enc - 1) == 'x'))
   continue;
 else
   param_pos++;
      }
  opc_pos = 32;
  for (enc = enc_initial; *enc != '\0'; )
    if ((*enc == '0') && (*(enc + 1) == 'x'))
      {
 opc_pos -= 4;
 if ((param_ch == '0') || (param_ch == '1'))
   {
     unsigned long tmp = strtoul (enc, ((void *)0), 16);
     if (param_ch == '0')
       tmp = 15 - tmp;
     ret |= tmp << opc_pos;
   }
 enc += 3;
      }
    else if ((*enc == '0') || (*enc == '1'))
      {
 opc_pos--;
 if (param_ch == *enc)
   ret |= 1 << opc_pos;
 enc++;
      }
    else if (*enc == param_ch)
      {
 opc_pos--;
 param_pos--;
 ret += ((insn >> opc_pos) & 0x1) << param_pos;
 if (!param_pos
     && letter_signed (param_ch)
     && ret >> (letter_range (param_ch) - 1))
   {
     ret |= 0xffffffff << letter_range(param_ch);
   }
 enc++;
      }
  else if (((*__ctype_b_loc ())[(int) ((*enc))] & (unsigned short int) _ISalpha))
      {
 opc_pos--;
 enc++;
      }
    else if (*enc == '-')
      {
 opc_pos--;
 enc++;
      }
    else
      enc++;
  return ret;
}

canonical_original_code:
static unsigned long or32_extract(char param_ch, char *enc_initial, unsigned long insn)
{
  char *enc;
  unsigned long ret = 0;
  int opc_pos = 0;
  int param_pos = 0;
  for (enc = enc_initial; *enc != '\0'; enc++)
    if (*enc == param_ch)
      {
 if (enc - 2 >= enc_initial && (*(enc - 2) == '0') && (*(enc - 1) == 'x'))
   continue;
 else
   param_pos++;
      }
  opc_pos = 32;
  for (enc = enc_initial; *enc != '\0'; )
    if ((*enc == '0') && (*(enc + 1) == 'x'))
      {
 opc_pos -= 4;
 if ((param_ch == '0') || (param_ch == '1'))
   {
     unsigned long tmp = strtoul (enc, ((void *)0), 16);
     if (param_ch == '0')
       tmp = 15 - tmp;
     ret |= tmp << opc_pos;
   }
 enc += 3;
      }
    else if ((*enc == '0') || (*enc == '1'))
      {
 opc_pos--;
 if (param_ch == *enc)
   ret |= 1 << opc_pos;
 enc++;
      }
    else if (*enc == param_ch)
      {
 opc_pos--;
 param_pos--;
 ret += ((insn >> opc_pos) & 0x1) << param_pos;
 if (!param_pos
     && letter_signed (param_ch)
     && ret >> (letter_range (param_ch) - 1))
   {
     ret |= 0xffffffff << letter_range(param_ch);
   }
 enc++;
      }
  else if (((*__ctype_b_loc ())[(int) ((*enc))] & (unsigned short) _ISalpha))
      {
 opc_pos--;
 enc++;
      }
    else if (*enc == '-')
      {
 opc_pos--;
 enc++;
      }
    else
      enc++;
  return ret;
}

variable_types:
enc
char *

variable_types:
ret
unsigned long

variable_types:
tmp
unsigned long

return_type: unsigned long


decompiled_code:
int __fastcall or32_print_register(char a1, char *a2, unsigned __int64 a3)
{
int v3; // eax

v3 = or32_extract(a1, a2, a3);
if ( a1 == 65 || a1 == 66 || a1 == 68 || v3 <= 15 )
return printf("r%d", (unsigned int)v3);
if ( v3 > 31 )
return printf("X%d", (unsigned int)v3);
return printf("r%d", (unsigned int)(v3 - 16));
}

canonical_decompiled_code:
int __fastcall func3(char a1, char *a2, unsigned __int64 a3)
{
int v3; // eax

v3 = func1(a1, a2, a3);
if ( a1 == 65 || a1 == 66 || a1 == 68 || v3 <= 15 )
return printf("r%d", (unsigned int)v3);
if ( v3 > 31 )
return printf("X%d", (unsigned int)v3);
return printf("r%d", (unsigned int)(v3 - 16));
}

original_code:
static void
or32_print_register (char param_ch,
       char *encoding,
       unsigned long insn)
{
  int regnum = or32_extract (param_ch, encoding, insn);
  if (param_ch == 'A')
    printf("r%d", regnum);
  else if (param_ch == 'B')
    printf("r%d", regnum);
  else if (param_ch == 'D')
    printf("r%d", regnum);
  else if (regnum < 16)
    printf("r%d", regnum);
  else if (regnum < 32)
    printf("r%d", regnum-16);
  else
    printf("X%d", regnum);
}

canonical_original_code:
static void or32_print_register(char param_ch, char *encoding, unsigned long insn)
{
  int regnum = or32_extract (param_ch, encoding, insn);
  if (param_ch == 'A')
    printf("r%d", regnum);
  else if (param_ch == 'B')
    printf("r%d", regnum);
  else if (param_ch == 'D')
    printf("r%d", regnum);
  else if (regnum < 16)
    printf("r%d", regnum);
  else if (regnum < 32)
    printf("r%d", regnum-16);
  else
    printf("X%d", regnum);
}

variable_types: regnum int


decompiled_code:
int __fastcall or32_print_immediate(char a1, char *a2, unsigned __int64 a3)
{
unsigned int v3; // ebp

v3 = or32_extract(a1, a2, a3);
letter_signed((unsigned int)a1);
return printf("0x%x", v3);
}

canonical_decompiled_code:
int __fastcall func4(char a1, char *a2, unsigned __int64 a3)
{
unsigned int v3; // ebp

v3 = func1(a1, a2, a3);
letter_signed((unsigned int)a1);
return printf("0x%x", v3);
}

original_code:
static void
or32_print_immediate (char param_ch,
        char *encoding,
        unsigned long insn)
{
  int imm = or32_extract(param_ch, encoding, insn);
  if (letter_signed(param_ch))
    printf("0x%x", imm);
  else
    printf("0x%x", imm);
}

canonical_original_code:
static void or32_print_immediate(char param_ch, char *encoding, unsigned long insn)
{
  int imm = or32_extract(param_ch, encoding, insn);
  if (letter_signed(param_ch))
    printf("0x%x", imm);
  else
    printf("0x%x", imm);
}

variable_types: imm int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v5; // r9
__int64 v6; // rax
unsigned __int64 v7; // rax
unsigned __int64 v8; // r12
char *v9; // rax
char v10; // al
char v11; // cl
char *v12; // rax
unsigned __int64 v13; // rbx
char v14; // bp
char nptr[8]; // [rsp+6h] [rbp-22h] BYREF
char v17; // [rsp+Eh] [rbp-1Ah] BYREF
char v18; // [rsp+10h] [rbp-18h] BYREF

if ( argc <= 1 )
{
LABEL_20:
v14 = 0;
if ( argc == 2 )
{
v13 = strtoul(argv[1], 0LL, 16);
goto LABEL_22;
}
goto LABEL_13;
}
v5 = (unsigned int)argc;
v6 = 1LL;
while ( 1 )
{
if ( *argv[v6] != 45 )
goto LABEL_3;
if ( !strcmp(argv[v6], "-print-orig-insn") )
break;
if ( !memcmp(argv[v6], "-h", 2uLL) )
{
print_or32_dis_usage(argv);
return 0;
}
LABEL_3:
if ( v5 == ++v6 )
goto LABEL_20;
}
v14 = 1;
if ( argc != 3 )
{
while ( 1 )
{
LABEL_13:
while ( 1 )
{
v10 = getchar();
v11 = v10;
if ( v10 != 10 )
break;
v7 = strtoul(nptr, 0LL, 16);
v8 = v7;
if ( v14 )
printf(" %.8lx\t", v7);
print_insn(v8);
putchar(10);
v9 = nptr;
do
*v9++ = 0;
while ( &v18 != v9 );
}
if ( v10 == -1 )
break;
v12 = nptr;
do
{
*v12 = v12[1];
++v12;
}
while ( &v17 != v12 );
nptr[7] = v11;
}
return 0;
}
if ( (_DWORD)v6 == 1 )
v13 = strtoul(argv[2], 0LL, 16);
else
v13 = strtoul(argv[1], 0LL, 16);
printf(" %.8lx\t", v13);
LABEL_22:
print_insn(v13);
putchar(10);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v5; // r9
__int64 v6; // rax
unsigned __int64 v7; // rax
unsigned __int64 v8; // r12
char *v9; // rax
char v10; // al
char v11; // cl
char *v12; // rax
unsigned __int64 v13; // rbx
char v14; // bp
char nptr[8]; // [rsp+6h] [rbp-22h] BYREF
char v17; // [rsp+Eh] [rbp-1Ah] BYREF
char v18; // [rsp+10h] [rbp-18h] BYREF

if ( argc <= 1 )
{
LABEL_20:
v14 = 0;
if ( argc == 2 )
{
v13 = strtoul(argv[1], 0LL, 16);
goto LABEL_22;
}
goto LABEL_13;
}
v5 = (unsigned int)argc;
v6 = 1LL;
while ( 1 )
{
if ( *argv[v6] != 45 )
goto LABEL_3;
if ( !strcmp(argv[v6], "-print-orig-insn") )
break;
if ( !memcmp(argv[v6], "-h", 2uLL) )
{
func0(argv);
return 0;
}
LABEL_3:
if ( v5 == ++v6 )
goto LABEL_20;
}
v14 = 1;
if ( argc != 3 )
{
while ( 1 )
{
LABEL_13:
while ( 1 )
{
v10 = getchar();
v11 = v10;
if ( v10 != 10 )
break;
v7 = strtoul(nptr, 0LL, 16);
v8 = v7;
if ( v14 )
printf(" %.8lx\t", v7);
func5(v8);
putchar(10);
v9 = nptr;
do
*v9++ = 0;
while ( &v18 != v9 );
}
if ( v10 == -1 )
break;
v12 = nptr;
do
{
*v12 = v12[1];
++v12;
}
while ( &v17 != v12 );
nptr[7] = v11;
}
return 0;
}
if ( (_DWORD)v6 == 1 )
v13 = strtoul(argv[2], 0LL, 16);
else
v13 = strtoul(argv[1], 0LL, 16);
printf(" %.8lx\t", v13);
LABEL_22:
func5(v13);
putchar(10);
return 0;
}

original_code:
int main (int argc, char *argv[])
{
  unsigned long insn;
  int print_orig_inst_option = 0;
  int i;
  for(i=1;i<argc;i++)
    {
      if (strncmp(argv[i],"-", 1) == 0 )
 {
   if (strcmp(argv[i],"-print-orig-insn") == 0 )
     {
       print_orig_inst_option = 1;
       break;
     }
   if (strncmp(argv[i],"-h",2) == 0 )
     {
       print_or32_dis_usage(argv);
       return 0;
     }
 }
    }
  int use_stdin = 0;
  if ((argc == 3 && print_orig_inst_option) || (argc == 2 && !print_orig_inst_option))
    use_stdin = 0;
  else
    use_stdin = 1;
  if (!use_stdin)
    {
      int index = 1;
      if (argc > 2 )
 {
   if (i == 1) index++;
 }
      insn = strtoul(argv[index], ((void *)0), 16);
      if (print_orig_inst_option)
 printf(" %.8lx\t", insn);
      print_insn (insn);
      printf("\n");
    }
  else
    {
      char inp[10];
      while(1)
 {
   char c;
   c = getchar();
   if (c == '\n')
     {
       insn = strtoul(inp, ((void *)0), 16);
       if (print_orig_inst_option)
  printf(" %.8lx\t", insn);
       print_insn (insn);
       printf("\n");
       for(i=0;i<10;i++)inp[i]=0;
     }
   else if (c == (-1))
     {
       break;
     }
   else
     {
       for(i=0;i<9-1;i++)inp[i]=inp[i+1];
       inp[7]=c;
     }
 }
    }
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
  unsigned long insn;
  int print_orig_inst_option = 0;
  int i;
  for(i=1;i<argc;i++)
    {
      if (strncmp(argv[i],"-", 1) == 0 )
 {
   if (strcmp(argv[i],"-print-orig-insn") == 0 )
     {
       print_orig_inst_option = 1;
       break;
     }
   if (strncmp(argv[i],"-h",2) == 0 )
     {
       print_or32_dis_usage(argv);
       return 0;
     }
 }
    }
  int use_stdin = 0;
  if ((argc == 3 && print_orig_inst_option) || (argc == 2 && !print_orig_inst_option))
    use_stdin = 0;
  else
    use_stdin = 1;
  if (!use_stdin)
    {
      int index = 1;
      if (argc > 2 )
 {
   if (i == 1) index++;
 }
      insn = strtoul(argv[index], ((void *)0), 16);
      if (print_orig_inst_option)
 printf(" %.8lx\t", insn);
      print_insn (insn);
      printf("\n");
    }
  else
    {
      char inp[10];
      while(1)
 {
   char c;
   c = getchar();
   if (c == '\n')
     {
       insn = strtoul(inp, ((void *)0), 16);
       if (print_orig_inst_option)
  printf(" %.8lx\t", insn);
       print_insn (insn);
       printf("\n");
       for(i=0;i<10;i++)inp[i]=0;
     }
   else if (c == (-1))
     {
       break;
     }
   else
     {
       for(i=0;i<9-1;i++)inp[i]=inp[i+1];
       inp[7]=c;
     }
 }
    }
  return 0;
}

variable_types: argc int

variable_types: i int

variable_types: index int

variable_types:
inp
char [10]

variable_types: c char


repo: arcfide/chez-sockets

decompiled_code:
int __fastcall get_ffi_value(char *s1)
{
bool v1; // zf
int result; // eax

v1 = strcmp(s1, "$ipproto-ip") == 0;
result = !v1;
if ( !v1 )
{
result = 255;
if ( strcmp(s1, "$ipproto-raw") )
{
result = 6;
if ( strcmp(s1, "$ipproto-tcp") )
{
result = 17;
if ( strcmp(s1, "$ipproto-udp") )
{
result = 1;
if ( strcmp(s1, "$sol-socket") )
{
result = 2;
if ( strcmp(s1, "%ai/canonname") )
{
result = 4;
if ( strcmp(s1, "%ai/numerichost") )
{
result = 1;
if ( strcmp(s1, "%ai/passive") )
{
result = 4;
if ( strcmp(s1, "%msg/dont-route") )
{
result = 1;
if ( strcmp(s1, "%msg/out-of-band") )
{
result = 2;
if ( strcmp(s1, "%msg/peek") )
{
if ( strcmp(s1, "%socket-domain/internet") )
{
result = 10;
if ( strcmp(s1, "%socket-domain/internet-v6") )
{
result = 2;
if ( strcmp(s1, "%socket-type/datagram") )
{
result = 4;
if ( strcmp(s1, "%socket-type/random") )
{
result = 3;
if ( strcmp(s1, "%socket-type/raw") )
{
result = 5;
if ( strcmp(s1, "%socket-type/sequence-packet") )
{
result = 1;
if ( strcmp(s1, "%socket-type/stream") )
{
result = 128;
if ( strcmp(s1, "%somaxconn") )
{
result = 2;
if ( strcmp(s1, "af-inet") )
{
result = 16;
if ( strcmp(s1, "size-of/addr-in") )
{
result = 48;
if ( strcmp(s1, "size-of/addrinfo") )
{
result = 4;
if ( strcmp(s1, "size-of/integer") )
{
if ( strcmp(s1, "size-of/ip") )
{
result = 8;
if ( strcmp(s1, "size-of/pointer") )
{
if ( !strcmp(s1, "size-of/port") )
{
result = 2;
}
else if ( !strcmp(s1, "size-of/protoent") )
{
result = 24;
}
else if ( !strcmp(s1, "size-of/size-t") )
{
result = 8;
}
else if ( !strcmp(s1, "size-of/sockaddr-in") )
{
result = 16;
}
else if ( !strcmp(s1, "size-of/socklen-t") )
{
result = 4;
}
else if ( !strcmp(s1, "size-of/sa-family") )
{
result = 2;
}
else
{
result = strcmp(s1, "%shutdown/read");
if ( result )
{
if ( !strcmp(s1, "%shutdown/read&write") )
{
result = 2;
}
else if ( !strcmp(s1, "%shutdown/write") )
{
result = 1;
}
else if ( !strcmp(s1, "invalid-socket") )
{
result = -1;
}
else if ( !strcmp(s1, "$file-set-flag") )
{
result = 4;
}
else if ( !strcmp(s1, "$option-non-blocking") )
{
result = 2048;
}
else if ( !strcmp(s1, "$error-again") )
{
result = 11;
}
else if ( !strcmp(s1, "$error-in-progress") )
{
result = 115;
}
else if ( !strcmp(s1, "$error-would-block") )
{
result = 11;
}
else if ( !strcmp(s1, "$socket-error") )
{
result = -1;
}
else if ( !strcmp(s1, "%socket-domain/local") )
{
result = 1;
}
else if ( !strcmp(s1, "%socket-domain/unix") )
{
result = 1;
}
else if ( !strcmp(s1, "af-unix") )
{
result = 1;
}
else if ( !strcmp(s1, "size-of/sockaddr-un") )
{
result = 110;
}
else if ( !strcmp(s1, "unix-max-path") )
{
result = 108;
}
else
{
result = strcmp(s1, "size-of/addr-un") == 0 ? 0x6E : 0;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func6(char *s1)
{
bool v1; // zf
int result; // eax

v1 = strcmp(s1, "$ipproto-ip") == 0;
result = !v1;
if ( !v1 )
{
result = 255;
if ( strcmp(s1, "$ipproto-raw") )
{
result = 6;
if ( strcmp(s1, "$ipproto-tcp") )
{
result = 17;
if ( strcmp(s1, "$ipproto-udp") )
{
result = 1;
if ( strcmp(s1, "$sol-socket") )
{
result = 2;
if ( strcmp(s1, "%ai/canonname") )
{
result = 4;
if ( strcmp(s1, "%ai/numerichost") )
{
result = 1;
if ( strcmp(s1, "%ai/passive") )
{
result = 4;
if ( strcmp(s1, "%msg/dont-route") )
{
result = 1;
if ( strcmp(s1, "%msg/out-of-band") )
{
result = 2;
if ( strcmp(s1, "%msg/peek") )
{
if ( strcmp(s1, "%socket-domain/internet") )
{
result = 10;
if ( strcmp(s1, "%socket-domain/internet-v6") )
{
result = 2;
if ( strcmp(s1, "%socket-type/datagram") )
{
result = 4;
if ( strcmp(s1, "%socket-type/random") )
{
result = 3;
if ( strcmp(s1, "%socket-type/raw") )
{
result = 5;
if ( strcmp(s1, "%socket-type/sequence-packet") )
{
result = 1;
if ( strcmp(s1, "%socket-type/stream") )
{
result = 128;
if ( strcmp(s1, "%somaxconn") )
{
result = 2;
if ( strcmp(s1, "af-inet") )
{
result = 16;
if ( strcmp(s1, "size-of/addr-in") )
{
result = 48;
if ( strcmp(s1, "size-of/addrinfo") )
{
result = 4;
if ( strcmp(s1, "size-of/integer") )
{
if ( strcmp(s1, "size-of/ip") )
{
result = 8;
if ( strcmp(s1, "size-of/pointer") )
{
if ( !strcmp(s1, "size-of/port") )
{
result = 2;
}
else if ( !strcmp(s1, "size-of/protoent") )
{
result = 24;
}
else if ( !strcmp(s1, "size-of/size-t") )
{
result = 8;
}
else if ( !strcmp(s1, "size-of/sockaddr-in") )
{
result = 16;
}
else if ( !strcmp(s1, "size-of/socklen-t") )
{
result = 4;
}
else if ( !strcmp(s1, "size-of/sa-family") )
{
result = 2;
}
else
{
result = strcmp(s1, "%shutdown/read");
if ( result )
{
if ( !strcmp(s1, "%shutdown/read&write") )
{
result = 2;
}
else if ( !strcmp(s1, "%shutdown/write") )
{
result = 1;
}
else if ( !strcmp(s1, "invalid-socket") )
{
result = -1;
}
else if ( !strcmp(s1, "$file-set-flag") )
{
result = 4;
}
else if ( !strcmp(s1, "$option-non-blocking") )
{
result = 2048;
}
else if ( !strcmp(s1, "$error-again") )
{
result = 11;
}
else if ( !strcmp(s1, "$error-in-progress") )
{
result = 115;
}
else if ( !strcmp(s1, "$error-would-block") )
{
result = 11;
}
else if ( !strcmp(s1, "$socket-error") )
{
result = -1;
}
else if ( !strcmp(s1, "%socket-domain/local") )
{
result = 1;
}
else if ( !strcmp(s1, "%socket-domain/unix") )
{
result = 1;
}
else if ( !strcmp(s1, "af-unix") )
{
result = 1;
}
else if ( !strcmp(s1, "size-of/sockaddr-un") )
{
result = 110;
}
else if ( !strcmp(s1, "unix-max-path") )
{
result = 108;
}
else
{
result = strcmp(s1, "size-of/addr-un") == 0 ? 0x6E : 0;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
return result;
}

original_code:
int get_ffi_value(const char *val) {
  struct sockaddr_in sai;
  struct sockaddr_un sau;
  if (!strcmp(val, "$ipproto-ip")) return IPPROTO_IP;
  if (!strcmp(val, "$ipproto-raw")) return IPPROTO_RAW;
  if (!strcmp(val, "$ipproto-tcp")) return IPPROTO_TCP;
  if (!strcmp(val, "$ipproto-udp")) return IPPROTO_UDP;
  if (!strcmp(val, "$sol-socket")) return 1;
  if (!strcmp(val, "%ai/canonname")) return 0x0002;
  if (!strcmp(val, "%ai/numerichost")) return 0x0004;
  if (!strcmp(val, "%ai/passive")) return 0x0001;
  if (!strcmp(val, "%msg/dont-route")) return MSG_DONTROUTE;
  if (!strcmp(val, "%msg/out-of-band")) return MSG_OOB;
  if (!strcmp(val, "%msg/peek")) return MSG_PEEK;
  if (!strcmp(val, "%socket-domain/internet")) return 2;
  if (!strcmp(val, "%socket-domain/internet-v6")) return 10;
  if (!strcmp(val, "%socket-type/datagram")) return SOCK_DGRAM;
  if (!strcmp(val, "%socket-type/random")) return SOCK_RDM;
  if (!strcmp(val, "%socket-type/raw")) return SOCK_RAW;
  if (!strcmp(val, "%socket-type/sequence-packet")) return SOCK_SEQPACKET;
  if (!strcmp(val, "%socket-type/stream")) return SOCK_STREAM;
  if (!strcmp(val, "%somaxconn")) return 128;
  if (!strcmp(val, "af-inet")) return 2;
  if (!strcmp(val, "size-of/addr-in")) return sizeof(struct sockaddr_in);
  if (!strcmp(val, "size-of/addrinfo")) return sizeof(struct addrinfo);
  if (!strcmp(val, "size-of/integer")) return sizeof(int);
  if (!strcmp(val, "size-of/ip")) return sizeof(struct in_addr);
  if (!strcmp(val, "size-of/pointer")) return sizeof(void *);
  if (!strcmp(val, "size-of/port")) return sizeof(sai.sin_port);
  if (!strcmp(val, "size-of/protoent")) return sizeof(struct protoent);
  if (!strcmp(val, "size-of/size-t")) return sizeof(size_t);
  if (!strcmp(val, "size-of/sockaddr-in")) return sizeof(struct sockaddr_in);
  if (!strcmp(val, "size-of/socklen-t")) return sizeof(socklen_t);
  if (!strcmp(val, "size-of/sa-family")) return sizeof(sa_family_t);
  if (!strcmp(val, "%shutdown/read")) return SHUT_RD;
  if (!strcmp(val, "%shutdown/read&write")) return SHUT_RDWR;
  if (!strcmp(val, "%shutdown/write")) return SHUT_WR;
  if (!strcmp(val, "invalid-socket")) return -1;
  if (!strcmp(val, "$file-set-flag")) return 4;
  if (!strcmp(val, "$option-non-blocking")) return 04000;
  if (!strcmp(val, "$error-again")) return 11;
  if (!strcmp(val, "$error-in-progress")) return 115;
  if (!strcmp(val, "$error-would-block")) return 11;
  if (!strcmp(val, "$socket-error")) return -1;
  if (!strcmp(val, "%socket-domain/local")) return 1;
  if (!strcmp(val, "%socket-domain/unix")) return 1;
  if (!strcmp(val, "af-unix")) return 1;
  if (!strcmp(val, "size-of/sockaddr-un")) return sizeof(struct sockaddr_un);
  if (!strcmp(val, "unix-max-path")) return sizeof(sau.sun_path);
  if (!strcmp(val, "size-of/addr-un")) return sizeof(struct sockaddr_un);
  return 0;
}

canonical_original_code:
int get_ffi_value(char *val) {
  struct sockaddr_in sai;
  struct sockaddr_un sau;
  if (!strcmp(val, "$ipproto-ip")) return IPPROTO_IP;
  if (!strcmp(val, "$ipproto-raw")) return IPPROTO_RAW;
  if (!strcmp(val, "$ipproto-tcp")) return IPPROTO_TCP;
  if (!strcmp(val, "$ipproto-udp")) return IPPROTO_UDP;
  if (!strcmp(val, "$sol-socket")) return 1;
  if (!strcmp(val, "%ai/canonname")) return 0x0002;
  if (!strcmp(val, "%ai/numerichost")) return 0x0004;
  if (!strcmp(val, "%ai/passive")) return 0x0001;
  if (!strcmp(val, "%msg/dont-route")) return MSG_DONTROUTE;
  if (!strcmp(val, "%msg/out-of-band")) return MSG_OOB;
  if (!strcmp(val, "%msg/peek")) return MSG_PEEK;
  if (!strcmp(val, "%socket-domain/internet")) return 2;
  if (!strcmp(val, "%socket-domain/internet-v6")) return 10;
  if (!strcmp(val, "%socket-type/datagram")) return SOCK_DGRAM;
  if (!strcmp(val, "%socket-type/random")) return SOCK_RDM;
  if (!strcmp(val, "%socket-type/raw")) return SOCK_RAW;
  if (!strcmp(val, "%socket-type/sequence-packet")) return SOCK_SEQPACKET;
  if (!strcmp(val, "%socket-type/stream")) return SOCK_STREAM;
  if (!strcmp(val, "%somaxconn")) return 128;
  if (!strcmp(val, "af-inet")) return 2;
  if (!strcmp(val, "size-of/addr-in")) return sizeof(struct sockaddr_in);
  if (!strcmp(val, "size-of/addrinfo")) return sizeof(struct addrinfo);
  if (!strcmp(val, "size-of/integer")) return sizeof(int);
  if (!strcmp(val, "size-of/ip")) return sizeof(struct in_addr);
  if (!strcmp(val, "size-of/pointer")) return sizeof(void *);
  if (!strcmp(val, "size-of/port")) return sizeof(sai.sin_port);
  if (!strcmp(val, "size-of/protoent")) return sizeof(struct protoent);
  if (!strcmp(val, "size-of/size-t")) return sizeof(unsigned long);
  if (!strcmp(val, "size-of/sockaddr-in")) return sizeof(struct sockaddr_in);
  if (!strcmp(val, "size-of/socklen-t")) return sizeof(socklen_t);
  if (!strcmp(val, "size-of/sa-family")) return sizeof(sa_family_t);
  if (!strcmp(val, "%shutdown/read")) return SHUT_RD;
  if (!strcmp(val, "%shutdown/read&write")) return SHUT_RDWR;
  if (!strcmp(val, "%shutdown/write")) return SHUT_WR;
  if (!strcmp(val, "invalid-socket")) return -1;
  if (!strcmp(val, "$file-set-flag")) return 4;
  if (!strcmp(val, "$option-non-blocking")) return 04000;
  if (!strcmp(val, "$error-again")) return 11;
  if (!strcmp(val, "$error-in-progress")) return 115;
  if (!strcmp(val, "$error-would-block")) return 11;
  if (!strcmp(val, "$socket-error")) return -1;
  if (!strcmp(val, "%socket-domain/local")) return 1;
  if (!strcmp(val, "%socket-domain/unix")) return 1;
  if (!strcmp(val, "af-unix")) return 1;
  if (!strcmp(val, "size-of/sockaddr-un")) return sizeof(struct sockaddr_un);
  if (!strcmp(val, "unix-max-path")) return sizeof(sau.sun_path);
  if (!strcmp(val, "size-of/addr-un")) return sizeof(struct sockaddr_un);
  return 0;
}

variable_types:
val
char *

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; }

variable_types: struct sockaddr_un { unsigned short sun_family; char sun_path[108]; }

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }

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; }

user_defined_types: struct protoent { char *p_name; char **p_aliases; int p_proto; }


repo: arctanb/srtm

decompiled_code:
__int64 __fastcall next_degree_down(int a1)
{
return (unsigned int)((a1 + 648000) / 3600 - 180);
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1)
{
return (unsigned int)((a1 + 648000) / 3600 - 180);
}

original_code:
static inline int32_t next_degree_down(int32_t arcsecond) {
  return (arcsecond + 180*60*60) / (60*60) - 180;
}

canonical_original_code:
static inline int next_degree_down(int arcsecond) {
  return (arcsecond + 180*60*60) / (60*60) - 180;
}

variable_types: arcsecond int


decompiled_code:
int __fastcall get_file_params(int a1, int a2, char *a3, _DWORD *a4, _DWORD *a5)
{
int v8; // ebx
int v9; // eax

v8 = next_degree_down(a1);
v9 = next_degree_down(a2);
*a4 = -3600 * v8 + a1;
*a5 = -3600 * v9 + a2;
return sprintf(a3, "data/%c%02d%c%03d.hgt", ((v9 >> 31) & 5u) + 78, abs32(v9), ((v8 >> 31) & 0x12u) + 69, abs32(v8));
}

original_code:
static inline void get_file_params(int32_t east, int32_t north, char *fname,
    uint32_t *rel_east, uint32_t *rel_north) {
  int32_t east_deg = next_degree_down(east);
  int32_t north_deg = next_degree_down(north);
  *rel_east = east - east_deg * 60 * 60;
  *rel_north = north - north_deg * 60 * 60;
  sprintf(fname, "data/%c%02d%c%03d.hgt",
      north_deg >= 0 ? 'N' : 'S',
      (((north_deg) < 0) ? -(north_deg) : (north_deg)),
      east_deg >= 0 ? 'E' : 'W',
      (((east_deg) < 0) ? -(east_deg) : (east_deg)));
}

canonical_original_code:
static inline void get_file_params(int east, int north, char *fname, unsigned int *rel_east, unsigned int *rel_north) {
  int east_deg = next_degree_down(east);
  int north_deg = next_degree_down(north);
  *rel_east = east - east_deg * 60 * 60;
  *rel_north = north - north_deg * 60 * 60;
  sprintf(fname, "data/%c%02d%c%03d.hgt",
      north_deg >= 0 ? 'N' : 'S',
      (((north_deg) < 0) ? -(north_deg) : (north_deg)),
      east_deg >= 0 ? 'E' : 'W',
      (((east_deg) < 0) ? -(east_deg) : (east_deg)));
}

variable_types: east int

variable_types: north int

variable_types:
fname
char *


original_code:
void straight_line(int32_t s_east, int32_t s_north, int32_t e_east,
    int32_t e_north, double *s_dist) {
  double delta_east = e_east - s_east;
  double delta_north = e_north - s_north;
  double delta = sqrt(delta_east * delta_east + delta_north + delta_north);
  double a = pow(sin(delta_north * (3.1415926535*(1.0/180/60/60))), 2) +
      pow(sin(delta_east * (3.1415926535*(1.0/180/60/60))), 2) *
      cos(s_north * (3.1415926535*(1.0/180/60/60))) * cos(e_north * (3.1415926535*(1.0/180/60/60)));
  double c = 2 * atan2(sqrt(a), sqrt(1-a));
  double dist = 6371000 * c;
  uint32_t num_steps = delta / 2;
  for (uint32_t i = 0; i < num_steps; ++i) {
    int32_t east = s_east + (delta_east * i / num_steps);
    int32_t north = s_north + (delta_north * i / num_steps);
    uint16_t result = read_lat_long(east, north);
    double cur_dist = *s_dist + dist * i / num_steps;
    printf("%f, %d\n", cur_dist, result);
  }
  *s_dist += dist;
}

canonical_original_code:
void straight_line(int s_east, int s_north, int e_east, int e_north, double *s_dist) {
  double delta_east = e_east - s_east;
  double delta_north = e_north - s_north;
  double delta = sqrt(delta_east * delta_east + delta_north + delta_north);
  double a = pow(sin(delta_north * (3.1415926535*(1.0/180/60/60))), 2) +
      pow(sin(delta_east * (3.1415926535*(1.0/180/60/60))), 2) *
      cos(s_north * (3.1415926535*(1.0/180/60/60))) * cos(e_north * (3.1415926535*(1.0/180/60/60)));
  double c = 2 * atan2(sqrt(a), sqrt(1-a));
  double dist = 6371000 * c;
  unsigned int num_steps = delta / 2;
  for (unsigned int i = 0; i < num_steps; ++i) {
    int east = s_east + (delta_east * i / num_steps);
    int north = s_north + (delta_north * i / num_steps);
    unsigned short result = read_lat_long(east, north);
    double cur_dist = *s_dist + dist * i / num_steps;
    printf("%f, %d\n", cur_dist, result);
  }
  *s_dist += dist;
}

variable_types: delta double

variable_types: a double

variable_types: c double

variable_types: dist double

variable_types:
i
unsigned int

variable_types:
result
unsigned short


compareRule

repo: ardagnir/beerfridge

decompiled_code:
int __fastcall compareRule(const void *a1, const void *a2)
{
return strcmp(*(const char **)a1, *(const char **)a2);
}

canonical_decompiled_code:
int __fastcall func0(const void *a1, const void *a2)
{
return strcmp(*(const char **)a1, *(const char **)a2);
}

original_code:
static int compareRule(const void *r1, const void* r2)
{
    return strcmp(((processRule*)r1)->processName, ((processRule*)r2)->processName);
}

canonical_original_code:
static int compareRule(void *r1, void *r2)
{
    return strcmp(((struct processRule *)r1)->processName, ((struct processRule *)r2)->processName);
}

user_defined_types: struct processRule { char *processName; long cpuShares[-1]; long cpuCap[-1]; int muted[-1]; }


slowLookup

decompiled_code:
const char **__fastcall slowLookup(char *s2, __int64 a2, __int64 a3)
{
__int64 v4; // rbx
const char **v5; // rbp

if ( a3 <= 0 )
return 0LL;
v4 = 0LL;
while ( 1 )
{
v5 = *(const char ***)(a2 + 8 * v4);
if ( !strcmp(*v5, s2) )
break;
if ( ++v4 == a3 )
return 0LL;
}
return v5;
}

canonical_decompiled_code:
const char **__fastcall func1(char *s2, __int64 a2, __int64 a3)
{
__int64 v4; // rbx
const char **v5; // rbp

if ( a3 <= 0 )
return 0LL;
v4 = 0LL;
while ( 1 )
{
v5 = *(const char ***)(a2 + 8 * v4);
if ( !strcmp(*v5, s2) )
break;
if ( ++v4 == a3 )
return 0LL;
}
return v5;
}

original_code:
const processRule* slowLookup(char* processName, processRule** ruleList, long numberRules)
{
    for(int i=0; i<numberRules; i++)
    {
        if(strcmp(ruleList[i]->processName, processName) == 0)
            return ruleList[i];
    }
    return 0;
}

canonical_original_code:
struct processRule *slowLookup(char *processName, struct processRule **ruleList, long numberRules)
{
    for(int i = 0; i<numberRules; i++)
    {
        if(strcmp(ruleList[i]->processName, processName) == 0)
            return ruleList[i];
    }
    return 0;
}

variable_types:
processName
char *

variable_types:
ruleList
struct processRule **

variable_types: numberRules long

variable_types: i int

return_type: struct processRule { char *processName; long cpuShares[-1]; long cpuCap[-1]; int muted[-1]; } *


processVariableLine

decompiled_code:
char __fastcall processVariableLine(char *a1)
{
char *v1; // rax
bool v2; // zf
char *v3; // rax
char *v4; // rax
char *v5; // rax

v1 = strtok(a1, ": ");
if ( !strcmp(v1, "multiplier") )
{
v4 = strtok(0LL, ": ");
LODWORD(v1) = atoi(v4);
m_multiplier = (int)v1;
}
else if ( !strcmp(v1, "denominator") )
{
v5 = strtok(0LL, ": ");
LODWORD(v1) = atoi(v5);
m_denominator = (int)v1;
}
else
{
v2 = strcmp(v1, "guibias") == 0;
LOBYTE(v1) = !v2;
if ( v2 )
{
v3 = strtok(0LL, ": ");
LODWORD(v1) = atoi(v3);
m_guibias = (int)v1;
}
}
return (char)v1;
}

canonical_decompiled_code:
char __fastcall func2(char *a1)
{
char *v1; // rax
bool v2; // zf
char *v3; // rax
char *v4; // rax
char *v5; // rax

v1 = strtok(a1, ": ");
if ( !strcmp(v1, "multiplier") )
{
v4 = strtok(0LL, ": ");
LODWORD(v1) = atoi(v4);
m_multiplier = (int)v1;
}
else if ( !strcmp(v1, "denominator") )
{
v5 = strtok(0LL, ": ");
LODWORD(v1) = atoi(v5);
m_denominator = (int)v1;
}
else
{
v2 = strcmp(v1, "guibias") == 0;
LOBYTE(v1) = !v2;
if ( v2 )
{
v3 = strtok(0LL, ": ");
LODWORD(v1) = atoi(v3);
m_guibias = (int)v1;
}
}
return (char)v1;
}

original_code:
void processVariableLine(char* lineData)
{
    char* varName=strtok(lineData, ": ");
    if( strcmp(varName, "multiplier")==0 )
    {
      m_multiplier=atoi(strtok(0, ": "));
    }
    else if( strcmp(varName, "denominator")==0 )
    {
      m_denominator=atoi(strtok(0, ": "));
    }
    else if( strcmp(varName, "guibias")==0 )
    {
      m_guibias=atoi(strtok(0, ": "));
    }
}

canonical_original_code:
void processVariableLine(char *lineData)
{
    char *varName = strtok(lineData, ": ");
    if( strcmp(varName, "multiplier")==0 )
    {
      m_multiplier=atoi(strtok(0, ": "));
    }
    else if( strcmp(varName, "denominator")==0 )
    {
      m_denominator=atoi(strtok(0, ": "));
    }
    else if( strcmp(varName, "guibias")==0 )
    {
      m_guibias=atoi(strtok(0, ": "));
    }
}

variable_types:
lineData
char *

variable_types:
varName
char *


processLineData

decompiled_code:
unsigned __int8 __fastcall processLineData(unsigned __int8 *a1, __int64 a2, __int64 *a3, _DWORD *a4)
{
const unsigned __int16 *v8; // rcx
unsigned __int8 result; // al
bool v10; // zf
void **v11; // rbp
__m128i *v12; // rbp
__m128i v13; // [rsp+0h] [rbp-98h] BYREF
__m128i v14; // [rsp+10h] [rbp-88h] BYREF
__m128i v15; // [rsp+20h] [rbp-78h] BYREF
__m128i v16; // [rsp+30h] [rbp-68h] BYREF
__m128i v17; // [rsp+40h] [rbp-58h] BYREF
__m128i v18; // [rsp+50h] [rbp-48h] BYREF
__m128i v19[3]; // [rsp+60h] [rbp-38h] BYREF

v8 = *_ctype_b_loc();
while ( 1 )
{
result = *a1;
if ( (v8[(char)*a1] & 0x2000) == 0 )
break;
++a1;
}
if ( result && result != 35 )
{
if ( *a4 )
{
v10 = memcmp(a1, "END_VARIABLES", 0xDuLL) == 0;
result = !v10;
if ( v10 )
*a4 = 0;
else
result = processVariableLine((char *)a1);
}
else
{
v10 = memcmp(a1, "START_VARIABLES", 0xFuLL) == 0;
result = !v10;
if ( v10 )
{
*a4 = 1;
}
else
{
v11 = (void **)(a2 + 8 * *a3);
*v11 = malloc(0x70uLL);
v12 = *(__m128i **)(a2 + 8 * *a3);
result = (unsigned __int8)parseRuleLine(&v13, (char *)a1, a2, *a3);
*v12 = _mm_loadu_si128(&v13);
v12[1] = _mm_loadu_si128(&v14);
v12[2] = _mm_loadu_si128(&v15);
v12[3] = _mm_loadu_si128(&v16);
v12[4] = _mm_loadu_si128(&v17);
v12[5] = _mm_loadu_si128(&v18);
v12[6] = _mm_loadu_si128(v19);
++*a3;
}
}
}
return result;
}

original_code:
void processLineData(char* lineData, processRule** ruleList, long* numberRules, bool* variables)
{
    while(((*__ctype_b_loc ())[(int) ((lineData[0]))] & (unsigned short int) _ISspace))
    {
        lineData++;
    }
    if(lineData[0]=='\0' || lineData[0]=='#')
    {
        return;
    }
    if(*variables)
    {
        if(strncmp(lineData, "END_VARIABLES", strlen("END_VARIABLES")) ==0 )
        {
           *variables = 0;
        }
        else
        {
           processVariableLine(lineData);
        }
    }
    else
    {
        if(strncmp(lineData, "START_VARIABLES", strlen("START_VARIABLES")) ==0 )
        {
           *variables=1;
        }
        else
        {
           ruleList[*numberRules]=malloc(sizeof(processRule));
           *ruleList[*numberRules]=parseRuleLine(lineData, ruleList, *numberRules);
           (*numberRules)++;
        }
    }
}

canonical_original_code:
void processLineData(char *lineData, struct processRule **ruleList, long *numberRules, int *variables)
{
    while(((*__ctype_b_loc ())[(int) ((lineData[0]))] & (unsigned short) _ISspace))
    {
        lineData++;
    }
    if(lineData[0]=='\0' || lineData[0]=='#')
    {
        return;
    }
    if(*variables)
    {
        if(strncmp(lineData, "END_VARIABLES", strlen("END_VARIABLES")) ==0 )
        {
           *variables = 0;
        }
        else
        {
           processVariableLine(lineData);
        }
    }
    else
    {
        if(strncmp(lineData, "START_VARIABLES", strlen("START_VARIABLES")) ==0 )
        {
           *variables=1;
        }
        else
        {
           ruleList[*numberRules]=malloc(sizeof(processRule));
           *ruleList[*numberRules]=parseRuleLine(lineData, ruleList, *numberRules);
           (*numberRules)++;
        }
    }
}

variable_types: struct processRule { char *processName; long cpuShares[-1]; long cpuCap[-1]; int muted[-1]; } **

variable_types:
numberRules
long *

variable_types:
variables
int *


lookupRule

decompiled_code:
const char **__fastcall lookupRule(char *s1)
{
__int64 v1; // r12
__int64 v2; // r15
__int64 v3; // r13
__int64 v4; // rbp
const char **v5; // rbx
int v6; // eax

v1 = m_numberRules - 1;
if ( m_numberRules - 1 < 0 )
{
LABEL_8:
v5 = 0LL;
if ( strcmp(s1, "!DEFAULT") )
v5 = (const char **)m_defaultRule;
}
else
{
v2 = m_ruleList;
v3 = 0LL;
while ( 1 )
{
v4 = (v3 + v1) / 2;
v5 = (const char **)(v2 + 112 * v4);
v6 = strcmp(s1, *v5);
if ( !v6 )
break;
if ( v6 <= 0 )
v1 = v4 - 1;
else
v3 = v4 + 1;
if ( v3 > v1 )
goto LABEL_8;
}
}
return v5;
}

canonical_decompiled_code:
const char **__fastcall func4(char *s1)
{
__int64 v1; // r12
__int64 v2; // r15
__int64 v3; // r13
__int64 v4; // rbp
const char **v5; // rbx
int v6; // eax

v1 = m_numberRules - 1;
if ( m_numberRules - 1 < 0 )
{
LABEL_8:
v5 = 0LL;
if ( strcmp(s1, "!DEFAULT") )
v5 = (const char **)m_defaultRule;
}
else
{
v2 = m_ruleList;
v3 = 0LL;
while ( 1 )
{
v4 = (v3 + v1) / 2;
v5 = (const char **)(v2 + 112 * v4);
v6 = strcmp(s1, *v5);
if ( !v6 )
break;
if ( v6 <= 0 )
v1 = v4 - 1;
else
v3 = v4 + 1;
if ( v3 > v1 )
goto LABEL_8;
}
}
return v5;
}

original_code:
const processRule* lookupRule(const char* processName)
{
    long minIndex=0;
    long maxIndex=m_numberRules-1;
    long middleIndex;
    long comparison;
    while( minIndex<=maxIndex )
    {
        middleIndex = ( minIndex + maxIndex )/2;
        comparison = strcmp( processName, m_ruleList[middleIndex].processName );
        if( comparison==0 )
        {
            return &m_ruleList[middleIndex];
        }
        else if( comparison > 0 )
        {
            minIndex=middleIndex+1;
        }
        else
        {
            maxIndex=middleIndex-1;
        }
    }
    if(strcmp(processName,"!DEFAULT")!=0)
    {
      return m_defaultRule;
    }
    else
    {
      return 0;
    }
}

canonical_original_code:
struct processRule *lookupRule(char *processName)
{
    long minIndex = 0;
    long maxIndex = m_numberRules-1;
    long middleIndex;
    long comparison;
    while( minIndex<=maxIndex )
    {
        middleIndex = ( minIndex + maxIndex )/2;
        comparison = strcmp( processName, m_ruleList[middleIndex].processName );
        if( comparison==0 )
        {
            return &m_ruleList[middleIndex];
        }
        else if( comparison > 0 )
        {
            minIndex=middleIndex+1;
        }
        else
        {
            maxIndex=middleIndex-1;
        }
    }
    if(strcmp(processName,"!DEFAULT")!=0)
    {
      return m_defaultRule;
    }
    else
    {
      return 0;
    }
}

variable_types: minIndex long

variable_types: maxIndex long

variable_types: middleIndex long

variable_types: comparison long


finalizeRules

original_code:
void finalizeRules(processRule** ruleList, long numberRules)
{
   m_ruleList=malloc(sizeof(processRule)*numberRules);
   for(long i=0; i<numberRules; i++)
   {
       m_ruleList[i]=*ruleList[i];
       free(ruleList[i]);
   }
   qsort(m_ruleList, numberRules, sizeof(processRule), compareRule);
   m_defaultRule=lookupRule("!DEFAULT");
}

canonical_original_code:
void finalizeRules(struct processRule **ruleList, long numberRules)
{
   m_ruleList=malloc(sizeof(processRule)*numberRules);
   for(long i = 0; i<numberRules; i++)
   {
       m_ruleList[i]=*ruleList[i];
       free(ruleList[i]);
   }
   qsort(m_ruleList, numberRules, sizeof(processRule), compareRule);
   m_defaultRule=lookupRule("!DEFAULT");
}

variable_types: i long


repo: arenn/gzrt

decompiled_code:
void __fastcall __noreturn show_usage_and_exit(int status)
{
fprintf(stderr, "%s\n", "Usage: gzrecover [-hpsVv] [-o <outfile>] [infile]");
exit(status);
}

canonical_decompiled_code:
void __fastcall __noreturn func7(int status)
{
fprintf(stderr, "%s\n", "Usage: gzrecover [-hpsVv] [-o <outfile>] [infile]");
exit(status);
}

original_code:
void
show_usage_and_exit(int exit_status)
{
  fprintf(stderr, "%s\n", usage);
  exit(exit_status);
}


decompiled_code:
ssize_t __fastcall read_internal(int fd, void *buf, size_t nbytes)
{
ssize_t v4; // rbx
int v5; // eax

do
{
v4 = read(fd, buf, nbytes);
if ( v4 != -1 )
break;
v5 = *__errno_location();
}
while ( v5 == 4 || v5 == 11 );
return v4;
}

canonical_decompiled_code:
ssize_t __fastcall func8(int fd, void *buf, size_t nbytes)
{
ssize_t v4; // rbx
int v5; // eax

do
{
v4 = read(fd, buf, nbytes);
if ( v4 != -1 )
break;
v5 = *__errno_location();
}
while ( v5 == 4 || v5 == 11 );
return v4;
}

original_code:
ssize_t
read_internal(int fd, void *buf, size_t count)
{
  ssize_t rc = 0;
  for (;;)
    {
      rc = read(fd, buf, count);
      if ((rc == -1) && (((*__errno_location ()) == 4) || ((*__errno_location ()) == 11)))
        continue;
      return(rc);
    }
}

canonical_original_code:
long read_internal(int fd, void *buf, unsigned long count)
{
  long rc = 0;
  for (;;)
    {
      rc = read(fd, buf, count);
      if ((rc == -1) && (((*__errno_location ()) == 4) || ((*__errno_location ()) == 11)))
        continue;
      return(rc);
    }
}

variable_types: fd int

variable_types:
buf
void *

variable_types:
count
unsigned long

variable_types: rc long

return_type: long


decompiled_code:
__int64 __fastcall open_outfile(char *s)
{
char *v1; // rbx
const char *v2; // rbx
size_t v3; // rax
char *v4; // rax
char *v5; // rbp
size_t v6; // rax
char *v7; // rax
char *v8; // rax
__int64 v9; // rcx
__int64 v10; // rcx
unsigned int v11; // ebx

if ( stdout_specified )
return 1;
v1 = s;
if ( !outfile_specified )
{
v6 = strlen(s);
v5 = (char *)malloc(v6 + 25);
if ( v5 )
{
v7 = strstr(s, ".gz");
if ( v7 )
*v7 = 0;
v8 = strrchr(s, 47);
if ( v8 )
v1 = v8 + 1;
if ( split_mode )
{
v10 = (unsigned int)suffix_0++;
sprintf(v5, "%s.recovered.%d", v1, v10);
}
else
{
sprintf(v5, "%s.recovered", v1);
}
goto LABEL_16;
}
LABEL_13:
perror("malloc");
exit(1);
}
v2 = user_outname;
v3 = strlen(user_outname);
v4 = (char *)malloc(v3 + 9);
v5 = v4;
if ( !v4 )
goto LABEL_13;
if ( split_mode )
{
v9 = (unsigned int)suffix_0++;
sprintf(v4, "%s.%d", v2, v9);
}
else
{
strcpy(v4, v2);
}
LABEL_16:
v11 = open64(v5, 66, 384LL);
if ( v11 == -1 )
{
perror("open");
exit(1);
}
if ( verbose_mode )
fprintf(stderr, "Opened output file for writing: %s\n", v5);
free(v5);
return v11;
}

canonical_decompiled_code:
__int64 __fastcall func9(char *s)
{
char *v1; // rbx
const char *v2; // rbx
size_t v3; // rax
char *v4; // rax
char *v5; // rbp
size_t v6; // rax
char *v7; // rax
char *v8; // rax
__int64 v9; // rcx
__int64 v10; // rcx
unsigned int v11; // ebx

if ( stdout_specified )
return 1;
v1 = s;
if ( !outfile_specified )
{
v6 = strlen(s);
v5 = (char *)malloc(v6 + 25);
if ( v5 )
{
v7 = strstr(s, ".gz");
if ( v7 )
*v7 = 0;
v8 = strrchr(s, 47);
if ( v8 )
v1 = v8 + 1;
if ( split_mode )
{
v10 = (unsigned int)suffix_0++;
sprintf(v5, "%s.recovered.%d", v1, v10);
}
else
{
sprintf(v5, "%s.recovered", v1);
}
goto LABEL_16;
}
LABEL_13:
perror("malloc");
exit(1);
}
v2 = user_outname;
v3 = strlen(user_outname);
v4 = (char *)malloc(v3 + 9);
v5 = v4;
if ( !v4 )
goto LABEL_13;
if ( split_mode )
{
v9 = (unsigned int)suffix_0++;
sprintf(v4, "%s.%d", v2, v9);
}
else
{
strcpy(v4, v2);
}
LABEL_16:
v11 = open64(v5, 66, 384LL);
if ( v11 == -1 )
{
perror("open");
exit(1);
}
if ( verbose_mode )
fprintf(stderr, "Opened output file for writing: %s\n", v5);
free(v5);
return v11;
}

original_code:
int
open_outfile(char *infile)
{
  int ofd;
  char *outfile, *ptr;
  static int suffix = 1;
  if (stdout_specified)
    return 1;
  if (outfile_specified)
    outfile = (char *)malloc(strlen(user_outname) + 9);
  else
    outfile = (char *)malloc(strlen(infile) + 25);
  if( outfile == 0 ){ perror("malloc"); exit(1); }
  if (!outfile_specified)
   {
     ptr = strstr(infile, ".gz");
     if (ptr)
       *ptr = '\0';
     ptr = strrchr(infile, '/');
     if (ptr)
       infile = ptr+1;
   }
  if (outfile_specified && split_mode)
    sprintf(outfile, "%s.%d", user_outname, suffix++);
  else if (outfile_specified)
    strcpy(outfile, user_outname);
  else if (split_mode)
    sprintf(outfile, "%s.recovered.%d", infile, suffix++);
  else
    sprintf(outfile, "%s.recovered", infile);
  ofd = open(outfile, 02 | 0100, 0200 | 0400);
  if( ofd == -1 ){ perror("open"); exit(1); }
  if (verbose_mode)
    fprintf(stderr, "Opened output file for writing: %s\n", outfile);
  free(outfile);
  return(ofd);
}

canonical_original_code:
int open_outfile(char *infile)
{
  int ofd;
  char *outfile; char *ptr;
  static int suffix = 1;
  if (stdout_specified)
    return 1;
  if (outfile_specified)
    outfile = (char *)malloc(strlen(user_outname) + 9);
  else
    outfile = (char *)malloc(strlen(infile) + 25);
  if( outfile == 0 ){ perror("malloc"); exit(1); }
  if (!outfile_specified)
   {
     ptr = strstr(infile, ".gz");
     if (ptr)
       *ptr = '\0';
     ptr = strrchr(infile, '/');
     if (ptr)
       infile = ptr+1;
   }
  if (outfile_specified && split_mode)
    sprintf(outfile, "%s.%d", user_outname, suffix++);
  else if (outfile_specified)
    strcpy(outfile, user_outname);
  else if (split_mode)
    sprintf(outfile, "%s.recovered.%d", infile, suffix++);
  else
    sprintf(outfile, "%s.recovered", infile);
  ofd = open(outfile, 02 | 0100, 0200 | 0400);
  if( ofd == -1 ){ perror("open"); exit(1); }
  if (verbose_mode)
    fprintf(stderr, "Opened output file for writing: %s\n", outfile);
  free(outfile);
  return(ofd);
}

variable_types:
infile
char *

variable_types: ofd int

variable_types:
outfile
char *

variable_types:
ptr
char *

variable_types: suffix int


main

repo: arfoll/occcl

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
int v4; // ebx
int v5; // ebp
const char *v7; // rax
_DWORD v8[5]; // [rsp+0h] [rbp-48h] BYREF
unsigned int v9; // [rsp+14h] [rbp-34h]
int v10; // [rsp+18h] [rbp-30h]
unsigned int v11; // [rsp+1Ch] [rbp-2Ch]
int v12; // [rsp+20h] [rbp-28h]

v8[4] = 0;
v9 = 0;
v10 = 0;
v11 = 25;
v12 = 0;
argp_parse(&argp, argc, (char **)argv, 0, 0LL, v8);
v3 = initialisecl(v9);
v4 = v3;
if ( v3 )
{
v7 = (const char *)errorMessageCL(v3);
fprintf(stdout, "initialisecl() returned %s\n", v7);
v4 = 1;
}
else
{
v5 = v12;
if ( v12 )
{
printPlatformInfo();
printDevInfo();
}
else
{
fwrite("========= CL DEVICE =========\n", 1uLL, 0x1EuLL, stdout);
printDeviceName();
printDevExt();
sinTest();
occoidsTest();
mandelbrotVisTest();
if ( !v10 )
{
if ( (unsigned int)getCorrectDevice("cl_khr_byte_addressable_store") )
{
fwrite(
"No devices supporting cl_khr_byte_addressable_store found - bypassing rot13, mandelbrot and modulot tests.\n",
1uLL,
0x6BuLL,
stdout);
return 2;
}
rot13Test();
mandelbrotTest(v9, v11);
if ( (unsigned int)getCorrectDevice("cl_khr_fp64") )
{
fwrite("No devices supporting cl_khr_fp64 found - bypassing modulo tests.\n", 1uLL, 0x42uLL, stdout);
return 3;
}
moduloTest();
}
destroycl();
v4 = v5;
}
}
return v4;
}

original_code:
int main (int argc, char *argv[])
{
  cl_int error;
  struct arguments arguments;
  arguments.silent = 0;
  arguments.verbose = 0;
  arguments.nocharext = 0;
  arguments.mandeliterations = 25;
  arguments.info = 0;
  argp_parse (&argp, argc, argv, 0, 0, &arguments);
  error = initialisecl(arguments.verbose);
  if(error != 0) {
    fprintf (stdout, "initialisecl() returned %s\n", errorMessageCL(error));
    return 1;
  }
  if (arguments.info) {
    printPlatformInfo();
    printDevInfo();
    return 0;
  }
  fprintf (stdout, "========= CL DEVICE =========\n");
  printDeviceName();
  printDevExt();
  sinTest();
  occoidsTest();
  mandelbrotVisTest();
  if (!arguments.nocharext) {
    if (getCorrectDevice("cl_khr_byte_addressable_store")) {
      fprintf (stdout, "No devices supporting cl_khr_byte_addressable_store found - bypassing rot13, mandelbrot and modulot tests.\n");
      return 2;
    }
    rot13Test();
    mandelbrotTest(arguments.verbose, arguments.mandeliterations);
    if (getCorrectDevice("cl_khr_fp64")) {
      fprintf (stdout, "No devices supporting cl_khr_fp64 found - bypassing modulo tests.\n");
      return 3;
    }
    moduloTest();
  }
  destroycl();
  return error;
}

canonical_original_code:
int main(int argc, char *argv[])
{
  int error;
  struct arguments arguments;
  arguments.silent = 0;
  arguments.verbose = 0;
  arguments.nocharext = 0;
  arguments.mandeliterations = 25;
  arguments.info = 0;
  argp_parse (&argp, argc, argv, 0, 0, &arguments);
  error = initialisecl(arguments.verbose);
  if(error != 0) {
    fprintf (stdout, "initialisecl() returned %s\n", errorMessageCL(error));
    return 1;
  }
  if (arguments.info) {
    printPlatformInfo();
    printDevInfo();
    return 0;
  }
  fprintf (stdout, "========= CL DEVICE =========\n");
  printDeviceName();
  printDevExt();
  sinTest();
  occoidsTest();
  mandelbrotVisTest();
  if (!arguments.nocharext) {
    if (getCorrectDevice("cl_khr_byte_addressable_store")) {
      fprintf (stdout, "No devices supporting cl_khr_byte_addressable_store found - bypassing rot13, mandelbrot and modulot tests.\n");
      return 2;
    }
    rot13Test();
    mandelbrotTest(arguments.verbose, arguments.mandeliterations);
    if (getCorrectDevice("cl_khr_fp64")) {
      fprintf (stdout, "No devices supporting cl_khr_fp64 found - bypassing modulo tests.\n");
      return 3;
    }
    moduloTest();
  }
  destroycl();
  return error;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: error int

variable_types: struct arguments { char *args[2]; int silent; }


textcolour

decompiled_code:
int __fastcall textcolour(int a1)
{
char s[21]; // [rsp+3h] [rbp-15h] BYREF

sprintf(s, "%c[%dm", 27LL, (unsigned int)colours[a1]);
return fputs(s, stdout);
}

canonical_decompiled_code:
int __fastcall func0(int a1)
{
char s[21]; // [rsp+3h] [rbp-15h] BYREF

sprintf(s, "%c[%dm", 27LL, (unsigned int)colours[a1]);
return fputs(s, stdout);
}

original_code:
void textcolour(int attr)
{
  char command[13];
  sprintf(command, "%c[%dm", 0x1B, colours[attr]);
  fprintf(stdout, "%s", command);
}

variable_types: attr int

variable_types:
command
char [13]


decompiled_code:
int __fastcall printf_job(float *a1, int a2)
{
float *v2; // rbx

if ( a2 > 0 )
{
v2 = a1;
do
fprintf(stderr, "%f ", *v2++);
while ( v2 != &a1[a2 - 1 + 1] );
}
return fputc(10, stderr);
}

canonical_decompiled_code:
int __fastcall func0(float *a1, int a2)
{
float *v2; // rbx

if ( a2 > 0 )
{
v2 = a1;
do
fprintf(stderr, "%f ", *v2++);
while ( v2 != &a1[a2 - 1 + 1] );
}
return fputc(10, stderr);
}

original_code:
void printf_job (cl_float *job, cl_int length)
{
  int i = 0;
  for (i = 0; i < length; i++) {
    fprintf(stderr, "%f ", job[i]);
  }
  fprintf(stderr, "\n");
}

canonical_original_code:
void printf_job(float *job, int length)
{
  int i = 0;
  for (i = 0; i < length; i++) {
    fprintf(stderr, "%f ", job[i]);
  }
  fprintf(stderr, "\n");
}

variable_types:
job
float *

variable_types: length int

variable_types: i int


mandelbrotTest

decompiled_code:
__int64 mandelbrotTest()
{
__int64 v0; // rbx
const char *v1; // rax
void *v2; // rbp
__int64 v4[7]; // [rsp+0h] [rbp-38h] BYREF

fwrite("========= MANDELBROT =========\n", 1uLL, 0x1FuLL, stdout);
v0 = (unsigned int)init_mandelbrot();
v1 = (const char *)errorMessageCL(v0);
fprintf(stdout, "init errors = %s\n", v1);
v4[0] = LC2;
v4[1] = LC3;
v4[2] = LC4;
v4[3] = LC5;
v2 = malloc(0x2710uLL);
mandelbrot(v2, v4);
free(v2);
return (unsigned int)v0;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 v0; // rbx
const char *v1; // rax
void *v2; // rbp
__int64 v4[7]; // [rsp+0h] [rbp-38h] BYREF

fwrite("========= MANDELBROT =========\n", 1uLL, 0x1FuLL, stdout);
v0 = (unsigned int)init_mandelbrot();
v1 = (const char *)errorMessageCL(v0);
fprintf(stdout, "init errors = %s\n", v1);
v4[0] = LC2;
v4[1] = LC3;
v4[2] = LC4;
v4[3] = LC5;
v2 = malloc(0x2710uLL);
mandelbrot(v2, v4);
free(v2);
return (unsigned int)v0;
}

original_code:
int mandelbrotTest(int verbose, int iterations) {
  cl_int error = 0;
  fprintf (stdout, "========= MANDELBROT =========\n");
  error = init_mandelbrot();
  fprintf (stdout, "init errors = %s\n", errorMessageCL(error));
  cl_fract job[4] = {-25.000000, 534.086426, -0.271229, 1.159260};
  cl_char *dataptr = (cl_char*)malloc(sizeof(cl_char)*100*2*50);
  cl_char (*chdata)[200] = (cl_char*) dataptr;
  mandelbrot(chdata, &job[0]);
  free (dataptr);
  return error;
}

canonical_original_code:
int mandelbrotTest(int verbose, int iterations) {
  int error = 0;
  fprintf (stdout, "========= MANDELBROT =========\n");
  error = init_mandelbrot();
  fprintf (stdout, "init errors = %s\n", errorMessageCL(error));
  double job[4] = {-25.000000, 534.086426, -0.271229, 1.159260};
  char *dataptr = (cl_char*)malloc(sizeof(cl_char)*100*2*50);
  cl_char (*chdata)[200] = (char *) dataptr;
  mandelbrot(chdata, &job[0]);
  free (dataptr);
  return error;
}

variable_types: verbose int

variable_types: iterations int

variable_types:
job
double [4]

variable_types:
dataptr
char *


mandelbrotVisTest

decompiled_code:
__int64 mandelbrotVisTest()
{
__int64 v0; // rbx
const char *v1; // rax
void *v2; // rbp
__int64 v4[7]; // [rsp+0h] [rbp-38h] BYREF

fwrite("========= MANDELBROT VIS =========\n", 1uLL, 0x23uLL, stdout);
v0 = (unsigned int)init_mandelbrotvis();
v1 = (const char *)errorMessageCL(v0);
fprintf(stdout, "init errors = %s\n", v1);
v4[0] = LC7;
v4[1] = LC3;
v4[2] = LC4;
v4[3] = LC5;
v2 = malloc(0x4B000uLL);
mandelbrotvis(v2, v4);
free(v2);
return (unsigned int)v0;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 v0; // rbx
const char *v1; // rax
void *v2; // rbp
__int64 v4[7]; // [rsp+0h] [rbp-38h] BYREF

fwrite("========= MANDELBROT VIS =========\n", 1uLL, 0x23uLL, stdout);
v0 = (unsigned int)init_mandelbrotvis();
v1 = (const char *)errorMessageCL(v0);
fprintf(stdout, "init errors = %s\n", v1);
v4[0] = LC7;
v4[1] = LC3;
v4[2] = LC4;
v4[3] = LC5;
v2 = malloc(0x4B000uLL);
mandelbrotvis(v2, v4);
free(v2);
return (unsigned int)v0;
}

original_code:
int mandelbrotVisTest() {
  cl_int error = 0;
  fprintf (stdout, "========= MANDELBROT VIS =========\n");
  error = init_mandelbrotvis();
  fprintf (stdout, "init errors = %s\n", errorMessageCL(error));
  cl_fract job[4] = {0.5, 534.086426, -0.271229, 1.159260};
  cl_int *dataptr = (cl_int*)malloc(sizeof(cl_int)*240*320);
  mandelbrotvis(dataptr, &job[0]);
  free (dataptr);
  return error;
}

canonical_original_code:
int mandelbrotVisTest() {
  int error = 0;
  fprintf (stdout, "========= MANDELBROT VIS =========\n");
  error = init_mandelbrotvis();
  fprintf (stdout, "init errors = %s\n", errorMessageCL(error));
  double job[4] = {0.5, 534.086426, -0.271229, 1.159260};
  int *dataptr = (cl_int*)malloc(sizeof(cl_int)*240*320);
  mandelbrotvis(dataptr, &job[0]);
  free (dataptr);
  return error;
}

variable_types:
dataptr
int *


occoidsTest

decompiled_code:
__int64 occoidsTest()
{
fwrite("========= OCCOIDS TEST =========\n", 1uLL, 0x21uLL, stdout);
return 0LL;
}

canonical_decompiled_code:
__int64 func2()
{
fwrite("========= OCCOIDS TEST =========\n", 1uLL, 0x21uLL, stdout);
return 0LL;
}

original_code:
int occoidsTest() {
  cl_int error = 0;
  fprintf (stdout, "========= OCCOIDS TEST =========\n");
  return error;
}

canonical_original_code:
int occoidsTest() {
  int error = 0;
  fprintf (stdout, "========= OCCOIDS TEST =========\n");
  return error;
}


moduloTest

decompiled_code:
__int64 moduloTest()
{
int v0; // ebx
int v1; // ebp
float v2; // xmm0_4
float v4; // [rsp+4h] [rbp-24h] BYREF
float v5; // [rsp+8h] [rbp-20h] BYREF
int v6[7]; // [rsp+Ch] [rbp-1Ch] BYREF

fwrite("========= MODULO PRECISION TEST =========\n", 1uLL, 0x2AuLL, stdout);
v0 = init_modulo();
v6[0] = -30;
v5 = 0.123145;
v4 = 0.0;
v1 = modulo(v6, &v4, &v5);
fprintf(stdout, "CL exceution = %f\n", v4);
v2 = (double)(v6[0] - 50) / (v5 + v5);
fprintf(stdout, "C execution = %f\n", (float)(v2 * (float)(v2 * v2)));
return (unsigned int)(v0 + v1);
}

canonical_decompiled_code:
__int64 func3()
{
int v0; // ebx
int v1; // ebp
float v2; // xmm0_4
float v4; // [rsp+4h] [rbp-24h] BYREF
float v5; // [rsp+8h] [rbp-20h] BYREF
int v6[7]; // [rsp+Ch] [rbp-1Ch] BYREF

fwrite("========= MODULO PRECISION TEST =========\n", 1uLL, 0x2AuLL, stdout);
v0 = init_modulo();
v6[0] = -30;
v5 = 0.123145;
v4 = 0.0;
v1 = modulo(v6, &v4, &v5);
fprintf(stdout, "CL exceution = %f\n", v4);
v2 = (double)(v6[0] - 50) / (v5 + v5);
fprintf(stdout, "C execution = %f\n", (float)(v2 * (float)(v2 * v2)));
return (unsigned int)(v0 + v1);
}

original_code:
int moduloTest() {
  cl_int error = 0;
  fprintf (stdout, "========= MODULO PRECISION TEST =========\n");
  error = init_modulo();
  cl_int num = -30;
  cl_float amount = 0.123145;
  cl_float mod = 0;
  error += modulo(&num, &mod, &amount);
  fprintf (stdout, "CL exceution = %f\n", mod);
  cl_float res = (num - 50) / (amount * 2.0);
  res = (res * res) * res;
  fprintf (stdout, "C  execution = %f\n", res);
  return error;
}

canonical_original_code:
int moduloTest() {
  int error = 0;
  fprintf (stdout, "========= MODULO PRECISION TEST =========\n");
  error = init_modulo();
  int num = -30;
  float amount = 0.123145;
  float mod = 0;
  error += modulo(&num, &mod, &amount);
  fprintf (stdout, "CL exceution = %f\n", mod);
  float res = (num - 50) / (amount * 2.0);
  res = (res * res) * res;
  fprintf (stdout, "C  execution = %f\n", res);
  return error;
}

variable_types: num int

variable_types: amount float

variable_types: mod float

variable_types: res float


sinTest

decompiled_code:
__int64 sinTest()
{
float *v0; // rbx
__int64 i; // rax
unsigned int v2; // ebp
const char *v3; // rax
unsigned int v4; // ebp
const char *v5; // rax

fwrite("========= SIN =========\n", 1uLL, 0x18uLL, stdout);
v0 = (float *)malloc(0xA000uLL);
for ( i = 0LL; i != 10240; ++i )
v0[i] = (float)(int)i;
v2 = init_sin();
v3 = (const char *)errorMessageCL(v2);
fprintf(stdout, "init errors = %s\n", v3);
v4 = clsin(v0) + v2;
v5 = (const char *)errorMessageCL(v4);
fprintf(stdout, "clsin errors = %s\n", v5);
free(v0);
return v4;
}

canonical_decompiled_code:
__int64 func4()
{
float *v0; // rbx
__int64 i; // rax
unsigned int v2; // ebp
const char *v3; // rax
unsigned int v4; // ebp
const char *v5; // rax

fwrite("========= SIN =========\n", 1uLL, 0x18uLL, stdout);
v0 = (float *)malloc(0xA000uLL);
for ( i = 0LL; i != 10240; ++i )
v0[i] = (float)(int)i;
v2 = init_sin();
v3 = (const char *)errorMessageCL(v2);
fprintf(stdout, "init errors = %s\n", v3);
v4 = clsin(v0) + v2;
v5 = (const char *)errorMessageCL(v4);
fprintf(stdout, "clsin errors = %s\n", v5);
free(v0);
return v4;
}

original_code:
int sinTest() {
  cl_int error = 0;
  int i;
  fprintf (stdout, "========= SIN =========\n");
  cl_float *data;
  data = (cl_float*)malloc(sizeof(cl_float)*10240);
  for (i=0; i<10240; i++) {
    data[i] = i;
  }
  error = init_sin();
  fprintf (stdout, "init errors = %s\n", errorMessageCL(error));
  error += clsin(data);
  fprintf (stdout, "clsin errors = %s\n", errorMessageCL(error));
  free (data);
  return error;
}

canonical_original_code:
int sinTest() {
  int error = 0;
  int i;
  fprintf (stdout, "========= SIN =========\n");
  float *data;
  data = (float *)malloc(sizeof(cl_float)*10240);
  for (i=0; i<10240; i++) {
    data[i] = i;
  }
  error = init_sin();
  fprintf (stdout, "init errors = %s\n", errorMessageCL(error));
  error += clsin(data);
  fprintf (stdout, "clsin errors = %s\n", errorMessageCL(error));
  free (data);
  return error;
}

variable_types:
data
float *


decompiled_code:
__int64 rot13Test()
{
unsigned int v0; // ebx
const char *v1; // rax
unsigned int v2; // ebx
char v4[14]; // [rsp+4h] [rbp-24h] BYREF
char v5[22]; // [rsp+12h] [rbp-16h] BYREF

fwrite("========= ROT13 =========\n", 1uLL, 0x1AuLL, stdout);
v0 = init_rot13();
v1 = (const char *)errorMessageCL(v0);
fprintf(stdout, "init errors = %s\n", v1);
strcpy(v5, "Hello, World!");
v2 = rot13(v5, v4) + v0;
fprintf(stdout, "rot13 errors = %d, ciphertext = %s\n", v2, v4);
return v2;
}

canonical_decompiled_code:
__int64 func5()
{
unsigned int v0; // ebx
const char *v1; // rax
unsigned int v2; // ebx
char v4[14]; // [rsp+4h] [rbp-24h] BYREF
char v5[22]; // [rsp+12h] [rbp-16h] BYREF

fwrite("========= ROT13 =========\n", 1uLL, 0x1AuLL, stdout);
v0 = init_rot13();
v1 = (const char *)errorMessageCL(v0);
fprintf(stdout, "init errors = %s\n", v1);
strcpy(v5, "Hello, World!");
v2 = rot13(v5, v4) + v0;
fprintf(stdout, "rot13 errors = %d, ciphertext = %s\n", v2, v4);
return v2;
}

original_code:
int rot13Test() {
  cl_int error = 0;
  fprintf (stdout, "========= ROT13 =========\n");
  error = init_rot13();
  fprintf (stdout, "init errors = %s\n", errorMessageCL(error));
  char plaintext[]="Hello, World!";
  char ciphertext[sizeof plaintext];
  error += rot13(plaintext, ciphertext);
  fprintf (stdout, "rot13 errors = %d, ciphertext = %s\n", error, ciphertext);
  return error;
}

canonical_original_code:
int rot13Test() {
  int error = 0;
  fprintf (stdout, "========= ROT13 =========\n");
  error = init_rot13();
  fprintf (stdout, "init errors = %s\n", errorMessageCL(error));
  char plaintext[] = "Hello, World!";
  char ciphertext[-1];
  error += rot13(plaintext, ciphertext);
  fprintf (stdout, "rot13 errors = %d, ciphertext = %s\n", error, ciphertext);
  return error;
}

variable_types: error int

variable_types:
plaintext
char []

variable_types:
ciphertext
char [-1]


decompiled_code:
__int64 init_sin()
{
__int64 result; // rax
const char *v1; // [rsp+0h] [rbp-20h] BYREF
__int64 v2; // [rsp+8h] [rbp-18h] BYREF
unsigned int v3[3]; // [rsp+14h] [rbp-Ch] BYREF

result = 1LL;
if ( !sin_init )
{
context = get_cl_context();
get_cl_device();
v2 = 289LL;
v1 = " __kernel void sin_cl (global float *data) { "
" int id = get_global_id(0); data[id] = sin(data[id]); "
" } ";
v3[0] = buildcl(&v1, &v2, &prog, &unk_1BC, 1LL);
k_sin = clCreateKernel(prog, "sin_cl", v3);
cq = get_command_queue();
result = v3[0];
if ( !v3[0] )
sin_init = 1;
}
return result;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 result; // rax
const char *v1; // [rsp+0h] [rbp-20h] BYREF
__int64 v2; // [rsp+8h] [rbp-18h] BYREF
unsigned int v3[3]; // [rsp+14h] [rbp-Ch] BYREF

result = 1LL;
if ( !sin_init )
{
context = get_cl_context();
get_cl_device();
v2 = 289LL;
v1 = " __kernel void sin_cl (global float *data) { "
" int id = get_global_id(0); data[id] = sin(data[id]); "
" } ";
v3[0] = buildcl(&v1, &v2, &prog, &unk_1BC, 1LL);
k_sin = clCreateKernel(prog, "sin_cl", v3);
cq = get_command_queue();
result = v3[0];
if ( !v3[0] )
sin_init = 1;
}
return result;
}

original_code:
int init_sin ()
{
  cl_int error;
  if (sin_init)
    return 1;
  context = get_cl_context();
  device = get_cl_device();
  const char *src=sin_cl;
  size_t srcsize=strlen(sin_cl);
  const char *srcptr[]={src};
  error = buildcl (srcptr, &srcsize, &prog, "", 1);
  k_sin = clCreateKernel(prog, "sin_cl", &error);
  cq = get_command_queue();
  if (!error)
    sin_init = 1;
  return error;
}

canonical_original_code:
int init_sin()
{
  int error;
  if (sin_init)
    return 1;
  context = get_cl_context();
  device = get_cl_device();
  char *src = sin_cl;
  unsigned long srcsize = strlen(sin_cl);
  char *srcptr[] = {src};
  error = buildcl (srcptr, &srcsize, &prog, "", 1);
  k_sin = clCreateKernel(prog, "sin_cl", &error);
  cq = get_command_queue();
  if (!error)
    sin_init = 1;
  return error;
}

variable_types:
src
char *

variable_types:
srcsize
unsigned long

variable_types:
srcptr
char *[]


original_code:
void mandelbrot_c (cl_char (*data)[200], cl_fract *job)
{
  int j, i;
  for (j = 0; j < 50; j++) {
    job[0] = (cl_fract) j - (50/2);
    cl_fract y = job[0]/job[1] - job[2];
    for (i = 0; i < 100; i++) {
      cl_fract real = ((i - 50) / (job[1] * 2.0)) - job[3];
      cl_fract imag = y;
      cl_fract iter_real = 0.0;
      cl_fract iter_imag = 0.0;
      int count = 0;
      while ((((iter_real*iter_real)+(iter_imag*iter_imag)) < 32.0) && (count < visheight)) {
        cl_fract iter_real2 = iter_real;
        cl_fract iter_imag2 = iter_imag;
        cl_fract iter_r;
        cl_fract iter_i;
        iter_r = (iter_real*iter_real2) - (iter_imag*iter_imag2);
        iter_i = (iter_imag*iter_real2) + (iter_real*iter_imag2);
        iter_real = real + iter_r;
        iter_imag = imag + iter_i;
        count++;
      }
      int val = count % 16;
      data[j][i*2] = (char) (val % 6);
      data[j][(i*2)+1] = table_int[val];
    }
  }
}

canonical_original_code:
void mandelbrot_c(char (*data)[200], double *job)
{
  int j; int i;
  for (j = 0; j < 50; j++) {
    job[0] = (double) j - (50/2);
    double y = job[0]/job[1] - job[2];
    for (i = 0; i < 100; i++) {
      double real = ((i - 50) / (job[1] * 2.0)) - job[3];
      double imag = y;
      double iter_real = 0.0;
      double iter_imag = 0.0;
      int count = 0;
      while ((((iter_real*iter_real)+(iter_imag*iter_imag)) < 32.0) && (count < visheight)) {
        double iter_real2 = iter_real;
        double iter_imag2 = iter_imag;
        double iter_r;
        double iter_i;
        iter_r = (iter_real*iter_real2) - (iter_imag*iter_imag2);
        iter_i = (iter_imag*iter_real2) + (iter_real*iter_imag2);
        iter_real = real + iter_r;
        iter_imag = imag + iter_i;
        count++;
      }
      int val = count % 16;
      data[j][i*2] = (char) (val % 6);
      data[j][(i*2)+1] = table_int[val];
    }
  }
}

variable_types:
data
char (*)[200]

variable_types:
job
double *

variable_types: j int

variable_types: i int

variable_types: y double

variable_types: real double

variable_types: imag double

variable_types: count int

variable_types: val int


initialiseJobs

original_code:
void initialiseJobs()
{
  int i, index;
  cl_fract zoom = 16.0;
  cl_fract xdrift = 0.0;
  cl_fract ydrift = 0.0;
  cl_fract diffx;
  cl_fract diffy;
  cl_fract xtarget = 1.16000014859;
  cl_fract ytarget = -0.27140215302;
  for (i = 0; i < iterations; i++) {
    index = i*3;
    jobs[index] = zoom;
    jobs[index+1] = ydrift;
    jobs[index+2] = xdrift;
    zoom = zoom + (zoom / 32.0);
    diffx = xtarget - xdrift;
    diffy = ytarget - ydrift;
    xdrift = xdrift + (diffx / 16.0);
    ydrift = ydrift + (diffy / 16.0);
  }
}

canonical_original_code:
void initialiseJobs()
{
  int i; int index;
  double zoom = 16.0;
  double xdrift = 0.0;
  double ydrift = 0.0;
  double diffx;
  double diffy;
  double xtarget = 1.16000014859;
  double ytarget = -0.27140215302;
  for (i = 0; i < iterations; i++) {
    index = i*3;
    jobs[index] = zoom;
    jobs[index+1] = ydrift;
    jobs[index+2] = xdrift;
    zoom = zoom + (zoom / 32.0);
    diffx = xtarget - xdrift;
    diffy = ytarget - ydrift;
    xdrift = xdrift + (diffx / 16.0);
    ydrift = ydrift + (diffy / 16.0);
  }
}

variable_types: index int

variable_types: zoom double

variable_types: xdrift double

variable_types: ydrift double

variable_types: diffx double

variable_types: diffy double

variable_types: xtarget double

variable_types: ytarget double