main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // er12
int v5; // edx
int v6; // eax
int v7; // ebx
int v8; // edi
socklen_t v9; // edx
int v10; // eax
int v11; // er13
__int64 v12; // rcx
__sigset_t *v13; // rdi
__int64 v14; // rax
__int64 v15; // r12
__pid_t v16; // eax
const char *v17; // rdx
int v18; // eax
epoll_data_t *v19; // r14
epoll_data_t *v20; // rbp
unsigned int v21; // er15
int v22; // eax
int v23; // eax
int v24; // eax
__int64 v25; // r15
int v26; // eax
unsigned int v27; // [rsp+Ch] [rbp-338Ch]
socklen_t addr_len; // [rsp+10h] [rbp-3388h] BYREF
struct epoll_event event; // [rsp+14h] [rbp-3384h] BYREF
struct sockaddr v30; // [rsp+20h] [rbp-3378h] BYREF
struct sockaddr addr; // [rsp+30h] [rbp-3368h] BYREF
char nptr[8]; // [rsp+40h] [rbp-3358h] BYREF
char v33[72]; // [rsp+48h] [rbp-3350h] BYREF
struct sigaction v34; // [rsp+90h] [rbp-3308h] BYREF
int v35[136]; // [rsp+130h] [rbp-3268h] BYREF
struct epoll_event events; // [rsp+350h] [rbp-3048h] BYREF
_DWORD v37[3086]; // [rsp+360h] [rbp-3038h] BYREF

addr_len = 0;
if ( argc <= 1 )
{
write_log(3LL, "connect_server:argc < 2 , please input more information worldid.lineid.xxxx , like:1.1.xxxx", envp);
}
else if ( (int)strsplit(argv[1], 46LL, nptr, 10LL, 8LL) <= 1 )
{
write_log(3LL, "connect_server:illegal argument 1:%s , exit!", argv[1]);
}
else
{
v3 = atoi(nptr);
world_id = v3;
line_id = atoi(v33);
if ( v3 - 1 <= 0xE && (unsigned int)(line_id - 1) <= 0xE )
{
printf("world_id:%d , line_id:%d\n", v3, (unsigned int)line_id);
v5 = (world_id << 28) | (line_id << 24) & 0xF000000;
logic_server_id = v5 | 2;
connect_server_id = v5 | 1;
v6 = socket(2, 1, 0);
v7 = v6;
if ( v6 < 0 )
{
write_log(3LL, "connect_server:create connect server socket failed!");
}
else
{
v8 = v6;
v30 = 0LL;
addr_len = 16;
v30.sa_family = 2;
*(_WORD *)v30.sa_data = _bswap_16_constprop_0((unsigned int)v6, &v30, 16LL);
if ( bind(v8, &v30, v9) < 0 )
{
write_log(3LL, "connect_server:bind connect socket failed!");
}
else
{
v10 = epoll_create(1025);
v11 = v10;
if ( v10 < 0 )
{
write_log(3LL, "connect_server:epoll create failed!");
}
else
{
event.events = 1;
event.data.fd = v7;
if ( epoll_ctl(v10, 1, v7, &event) < 0 )
{
write_log(3LL, "connect_server: append serve fd to epoll failed!");
}
else
{
listen(v7, 1024);
memset(&client, 0, 0xB008uLL);
v12 = 36LL;
v13 = &v34.sa_mask;
v34.sa_handler = (__sighandler_t)handle_signal;
while ( v12 )
{
LODWORD(v13->__val[0]) = 0;
v13 = (__sigset_t *)((char *)v13 + 4);
--v12;
}
sigaction(2, &v34, 0LL);
poll = create_mem_poll();
if ( poll )
{
write_log(1LL, "connect_server:create mem poll success!");
if ( (int)open_bus((unsigned int)connect_server_id, (unsigned int)logic_server_id) < 0 )
{
write_log(3LL, "connect_server:open bus connect <-> logic failed!");
}
else
{
v14 = attach_shm_res(1LL, (unsigned int)world_id, (unsigned int)line_id);
pruntime_env = v14;
if ( v14 )
{
write_log(1LL, "connect_server:attach runtime env %x success!", *(unsigned int *)(v14 + 4));
v15 = pruntime_env + 72LL * (int)index_last_1bit(1LL);
*(_DWORD *)(v15 + 8) = connect_server_id;
v16 = getpid();
v17 = argv[1];
*(_DWORD *)(v15 + 12) = v16;
write_log(1LL, "start connect_server %s success!", v17);
v27 = 0;
while ( 1 )
{
++v27;
v18 = epoll_wait(v11, &events, 1025, 200);
if ( v18 < 0 )
break;
if ( v18 )
{
v19 = &events.data;
v20 = (epoll_data_t *)&v37[3 * (v18 - 1)];
do
{
v21 = v19->fd;
if ( v19->fd == v7 )
{
addr = 0LL;
v25 = (unsigned int)accept(v7, &addr, &addr_len);
write_log(1LL, "connect_server:accept a new client: %d\n", v25);
set_nonblock((unsigned int)v25);
set_sock_buff_size((unsigned int)v25, 5240LL, 2620LL);
event.data.fd = v25;
v26 = client;
*((_DWORD *)&client + (int)client + 1) = v25;
event.events = -2147475455;
LODWORD(client) = v26 + 1;
if ( epoll_ctl(v11, 1, v25, &event) < 0 )
write_log(3LL, "connect_server:epoll_ctl %d failed!", (unsigned int)v25);
}
else
{
v22 = HIDWORD(v19[-1].u64);
if ( (v22 & 0x2000) != 0 )
{
write_log(1LL, "connect_server:client %d closed!", v21);
close_client_isra_0(v19->fd);
}
else if ( (v22 & 1) != 0 )
{
memset(v35, 0, 0x210uLL);
LOWORD(v35[0]) = 1;
v35[1] = v21;
v35[132] = 0;
v23 = read_socket(v21, &v35[2]);
if ( v23 == 524 )
{
puts(">>>read package!");
}
else if ( v23 <= 0 )
{
write_log(1LL, "connect server: nothing to read!", (unsigned int)v23);
}
else
{
write_log(
3LL,
"connect server: read pakcage bytes len %d < cspackage size %d!",
(unsigned int)v23,
524LL);
}
if ( ((unsigned int)v35[2] >> (LOBYTE(v35[4]) + 1))
+ BYTE1(v35[4]) % (LOWORD(v35[3]) + 255) == HIWORD(v35[3]) )
{
v24 = send_bus_pkg((unsigned int)logic_server_id, (unsigned int)connect_server_id, v35);
if ( v24 )
{
if ( v24 == -2 )
{
write_log(3LL, "connect server: send bus to logic full!");
}
else if ( v24 == -1 )
{
write_log(3LL, "connect server: send to logic failed!");
}
}
send_to_client_isra_0(v21);
}
else
{
write_log(
1LL,
"connect_server:recv client %d, no validate seq package: seq:%d vs vali_seq:%d",
v21,
(unsigned int)v35[2],
HIWORD(v35[3]));
}
}
}
v19 = (epoll_data_t *)((char *)v19 + 12);
}
while ( v20 != v19 );
}
try_handle_buses_isra_0();
if ( !(v27 % 5) )
send_to_all_clients_isra_0();
}
write_log(3LL, "connect_server:epoll wait error!");
}
else
{
write_log(3LL, "connect_server:attach runtime env failed!");
}
}
}
else
{
write_log(3LL, "connect_server:create mem poll failed!");
}
}
}
}
}
}
else
{
write_log(3LL, "connect_server:illegal argument1:%s , exit!", argv[1]);
}
}
return -1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // er12
int v5; // edx
int v6; // eax
int v7; // ebx
int v8; // edi
socklen_t v9; // edx
int v10; // eax
int v11; // er13
__int64 v12; // rcx
__sigset_t *v13; // rdi
__int64 v14; // rax
__int64 v15; // r12
__pid_t v16; // eax
const char *v17; // rdx
int v18; // eax
epoll_data_t *v19; // r14
epoll_data_t *v20; // rbp
unsigned int v21; // er15
int v22; // eax
int v23; // eax
int v24; // eax
__int64 v25; // r15
int v26; // eax
unsigned int v27; // [rsp+Ch] [rbp-338Ch]
socklen_t addr_len; // [rsp+10h] [rbp-3388h] BYREF
struct epoll_event event; // [rsp+14h] [rbp-3384h] BYREF
struct sockaddr v30; // [rsp+20h] [rbp-3378h] BYREF
struct sockaddr addr; // [rsp+30h] [rbp-3368h] BYREF
char nptr[8]; // [rsp+40h] [rbp-3358h] BYREF
char v33[72]; // [rsp+48h] [rbp-3350h] BYREF
struct sigaction v34; // [rsp+90h] [rbp-3308h] BYREF
int v35[136]; // [rsp+130h] [rbp-3268h] BYREF
struct epoll_event events; // [rsp+350h] [rbp-3048h] BYREF
_DWORD v37[3086]; // [rsp+360h] [rbp-3038h] BYREF

addr_len = 0;
if ( argc <= 1 )
{
write_log(3LL, "connect_server:argc < 2 , please input more information worldid.lineid.xxxx , like:1.1.xxxx", envp);
}
else if ( (int)strsplit(argv[1], 46LL, nptr, 10LL, 8LL) <= 1 )
{
write_log(3LL, "connect_server:illegal argument 1:%s , exit!", argv[1]);
}
else
{
v3 = atoi(nptr);
world_id = v3;
line_id = atoi(v33);
if ( v3 - 1 <= 0xE && (unsigned int)(line_id - 1) <= 0xE )
{
printf("world_id:%d , line_id:%d\n", v3, (unsigned int)line_id);
v5 = (world_id << 28) | (line_id << 24) & 0xF000000;
logic_server_id = v5 | 2;
connect_server_id = v5 | 1;
v6 = socket(2, 1, 0);
v7 = v6;
if ( v6 < 0 )
{
write_log(3LL, "connect_server:create connect server socket failed!");
}
else
{
v8 = v6;
v30 = 0LL;
addr_len = 16;
v30.sa_family = 2;
*(_WORD *)v30.sa_data = func3((unsigned int)v6, &v30, 16LL);
if ( bind(v8, &v30, v9) < 0 )
{
write_log(3LL, "connect_server:bind connect socket failed!");
}
else
{
v10 = epoll_create(1025);
v11 = v10;
if ( v10 < 0 )
{
write_log(3LL, "connect_server:epoll create failed!");
}
else
{
event.events = 1;
event.data.fd = v7;
if ( epoll_ctl(v10, 1, v7, &event) < 0 )
{
write_log(3LL, "connect_server: append serve fd to epoll failed!");
}
else
{
listen(v7, 1024);
memset(&client, 0, 0xB008uLL);
v12 = 36LL;
v13 = &v34.sa_mask;
v34.sa_handler = (__sighandler_t)handle_signal;
while ( v12 )
{
LODWORD(v13->__val[0]) = 0;
v13 = (__sigset_t *)((char *)v13 + 4);
--v12;
}
sigaction(2, &v34, 0LL);
poll = create_mem_poll();
if ( poll )
{
write_log(1LL, "connect_server:create mem poll success!");
if ( (int)open_bus((unsigned int)connect_server_id, (unsigned int)logic_server_id) < 0 )
{
write_log(3LL, "connect_server:open bus connect <-> logic failed!");
}
else
{
v14 = attach_shm_res(1LL, (unsigned int)world_id, (unsigned int)line_id);
pruntime_env = v14;
if ( v14 )
{
write_log(1LL, "connect_server:attach runtime env %x success!", *(unsigned int *)(v14 + 4));
v15 = pruntime_env + 72LL * (int)index_last_1bit(1LL);
*(_DWORD *)(v15 + 8) = connect_server_id;
v16 = getpid();
v17 = argv[1];
*(_DWORD *)(v15 + 12) = v16;
write_log(1LL, "start connect_server %s success!", v17);
v27 = 0;
while ( 1 )
{
++v27;
v18 = epoll_wait(v11, &events, 1025, 200);
if ( v18 < 0 )
break;
if ( v18 )
{
v19 = &events.data;
v20 = (epoll_data_t *)&v37[3 * (v18 - 1)];
do
{
v21 = v19->fd;
if ( v19->fd == v7 )
{
addr = 0LL;
v25 = (unsigned int)accept(v7, &addr, &addr_len);
write_log(1LL, "connect_server:accept a new client: %d\n", v25);
set_nonblock((unsigned int)v25);
set_sock_buff_size((unsigned int)v25, 5240LL, 2620LL);
event.data.fd = v25;
v26 = client;
*((_DWORD *)&client + (int)client + 1) = v25;
event.events = -2147475455;
LODWORD(client) = v26 + 1;
if ( epoll_ctl(v11, 1, v25, &event) < 0 )
write_log(3LL, "connect_server:epoll_ctl %d failed!", (unsigned int)v25);
}
else
{
v22 = HIDWORD(v19[-1].u64);
if ( (v22 & 0x2000) != 0 )
{
write_log(1LL, "connect_server:client %d closed!", v21);
func5(v19->fd);
}
else if ( (v22 & 1) != 0 )
{
memset(v35, 0, 0x210uLL);
LOWORD(v35[0]) = 1;
v35[1] = v21;
v35[132] = 0;
v23 = func1(v21, &v35[2]);
if ( v23 == 524 )
{
puts(">>>read package!");
}
else if ( v23 <= 0 )
{
write_log(1LL, "connect server: nothing to read!", (unsigned int)v23);
}
else
{
write_log(
3LL,
"connect server: read pakcage bytes len %d < cspackage size %d!",
(unsigned int)v23,
524LL);
}
if ( ((unsigned int)v35[2] >> (LOBYTE(v35[4]) + 1))
+ BYTE1(v35[4]) % (LOWORD(v35[3]) + 255) == HIWORD(v35[3]) )
{
v24 = send_bus_pkg((unsigned int)logic_server_id, (unsigned int)connect_server_id, v35);
if ( v24 )
{
if ( v24 == -2 )
{
write_log(3LL, "connect server: send bus to logic full!");
}
else if ( v24 == -1 )
{
write_log(3LL, "connect server: send to logic failed!");
}
}
func6(v21);
}
else
{
write_log(
1LL,
"connect_server:recv client %d, no validate seq package: seq:%d vs vali_seq:%d",
v21,
(unsigned int)v35[2],
HIWORD(v35[3]));
}
}
}
v19 = (epoll_data_t *)((char *)v19 + 12);
}
while ( v20 != v19 );
}
func4();
if ( !(v27 % 5) )
func7();
}
write_log(3LL, "connect_server:epoll wait error!");
}
else
{
write_log(3LL, "connect_server:attach runtime env failed!");
}
}
}
else
{
write_log(3LL, "connect_server:create mem poll failed!");
}
}
}
}
}
}
else
{
write_log(3LL, "connect_server:illegal argument1:%s , exit!", argv[1]);
}
}
return -1;
}

original_code:
int main(int argc , char **argv){
 int iserv_fd;
 int iaccept_fd;
 int iepoll_fd;
 int iactive_fds;
 int handle_fd;
 char arg_segs[10][8];
 int icount;
 int index = 0;
 struct sockaddr_in stserv_addr;
 struct sockaddr_in stcli_addr;
 int iaddr_len = 0;
 struct epoll_event stepoll_event;
 struct epoll_event astepoll_events[1024 + 1];
 sspackage_t sspackage_recv;
 struct sigaction stsig_act;
 int iRet = -1;
 int i;
 u32 ticks = 0;
 if(argc < 2){
  write_log(3 , "connect_server:argc < 2 , please input more information worldid.lineid.xxxx , like:1.1.xxxx");
  return -1;
 }
 icount = strsplit(argv[1] , '.' , arg_segs , 10 , 8);
 if(icount < 2){
  write_log(3 , "connect_server:illegal argument 1:%s , exit!" , argv[1]);
  return -1;
 }
 world_id = atoi(arg_segs[0]);
 line_id = atoi(arg_segs[1]);
 if(world_id<=0 || world_id>15 || line_id<=0 || line_id>15){
  write_log(3 , "connect_server:illegal argument1:%s , exit!" , argv[1]);
  return -1;
 }
 printf("world_id:%d , line_id:%d\n" , world_id , line_id);
 connect_server_id = (world_id << 28 & 0xF0000000) | (line_id << 24 & 0x0F000000) | 0x00000000 | 0x00000001;
 logic_server_id = (world_id << 28 & 0xF0000000) | (line_id << 24 & 0x0F000000) | 0x00000000 | 0x00000002;
 log_server_id = (world_id << 28 & 0xF0000000) | (line_id << 24 & 0x0F000000) | 0x00000000 | 0x00000004;
 iserv_fd = socket(2 , SOCK_STREAM , 0 );
 if(iserv_fd < 0){
  write_log(3 , "connect_server:create connect server socket failed!");
  return -1;
 }
 iaddr_len = sizeof(struct sockaddr_in);
 memset(&stserv_addr , 0 , sizeof(stserv_addr));
 stserv_addr.sin_family = 2;
 stserv_addr.sin_addr.s_addr = ((in_addr_t) 0x00000000);
 stserv_addr.sin_port = htons(8005);
 iRet = bind(iserv_fd , (struct sockaddr *)&stserv_addr , iaddr_len);
 if(iRet < 0){
  write_log(3 , "connect_server:bind connect socket failed!");
  return -1;
 }
 iepoll_fd = epoll_create(1024 + 1);
 if(iepoll_fd < 0){
  write_log(3 , "connect_server:epoll create failed!");
  return -1;
 }
 stepoll_event.events = EPOLLIN;
 stepoll_event.data.fd = iserv_fd;
 iRet = epoll_ctl(iepoll_fd , 1 , iserv_fd , &stepoll_event);
 if(iRet < 0){
  write_log(3 , "connect_server: append serve fd to epoll failed!");
  return -1;
 }
 listen(iserv_fd , 1024);
 memset(&client , 0 , sizeof(CLIENT));
 memset(&stsig_act , 0 , sizeof(stsig_act));
 stsig_act.__sigaction_handler.sa_handler = handle_signal;
 sigaction(2 , &stsig_act , ((void *)0));
 poll = create_mem_poll();
 if(!poll){
  write_log(3 , "connect_server:create mem poll failed!");
  return -1;
 }else{
  write_log(1 , "connect_server:create mem poll success!");
 }
 iRet = open_bus(connect_server_id , logic_server_id);
 if(iRet < 0){
  write_log(3 , "connect_server:open bus connect <-> logic failed!");
  return -1;
 }
 pruntime_env = (runtime_env_t *)attach_shm_res(0x00000001 , world_id , line_id);
 if(!pruntime_env){
  write_log(3 , "connect_server:attach runtime env failed!");
  return -1;
 }else{
  write_log(1 , "connect_server:attach runtime env %x success!" , pruntime_env->basic.global_id);
 }
 index = index_last_1bit(0x00000001);
 pruntime_env->basic.proc_info[index].global_id = connect_server_id;
 pruntime_env->basic.proc_info[index].pid = getpid();
 write_log(1 , "start connect_server %s success!" , argv[1]);
 while(1){
  ticks++;
  iactive_fds = epoll_wait(iepoll_fd , astepoll_events , 1024 +1 , 200);
  if(iactive_fds < 0){
   write_log(3 , "connect_server:epoll wait error!");
   return -1;
  }
  for(i=0; i<iactive_fds; i++){
   if(astepoll_events[i].data.fd == iserv_fd){
    memset(&stcli_addr , 0 , sizeof(stcli_addr));
    iaccept_fd = accept(iserv_fd , (struct sockaddr *)&stcli_addr , &iaddr_len);
    write_log(1 , "connect_server:accept a new client: %d\n" , iaccept_fd);
    set_nonblock(iaccept_fd);
    set_sock_buff_size(iaccept_fd , 10 * sizeof(cspackage_t) , 5 * sizeof(cspackage_t));
    client.active_fds[client.max_active_fd] = iaccept_fd;
    client.max_active_fd++;
    stepoll_event.data.fd = iaccept_fd;
    stepoll_event.events = EPOLLIN | EPOLLET | EPOLLRDHUP;
    iRet = epoll_ctl(iepoll_fd , 1 , iaccept_fd , &stepoll_event);
    if(iRet < 0){
     write_log(3 , "connect_server:epoll_ctl %d failed!" , iaccept_fd);
    }
    continue;
   }
   if(astepoll_events[i].events & EPOLLRDHUP){
    write_log(1 , "connect_server:client %d closed!" , astepoll_events[i].data.fd);
    close_client(astepoll_events[i].data.fd);
    continue;
   }
   if(astepoll_events[i].events & EPOLLIN){
    handle_fd = astepoll_events[i].data.fd;
    memset(&sspackage_recv , 0 , sizeof(sspackage_t));
    sspackage_recv.sshead.package_type = 1;
    sspackage_recv.sshead.client_fd = handle_fd;
    iRet = read_socket(handle_fd , &sspackage_recv.data.cs_data);
    if(iRet == sizeof(cspackage_t)){
     printf(">>>read package!\n");
    }
    else
    if(iRet > 0){
     write_log(3 , "connect server: read pakcage bytes len %d < cspackage size %d!" , iRet , sizeof(cspackage_t));
    }
    else{
     write_log(1 , "connect server: nothing to read!");
    }
    if(((sspackage_recv.data.cs_data.cshead.seq >> sspackage_recv.data.cs_data.cshead.major_version+1) + sspackage_recv.data.cs_data.cshead.minor_version % (sspackage_recv.data.cs_data.cshead.proto_type + 255) ) != sspackage_recv.data.cs_data.cshead.validate_seq){
     write_log(1 , "connect_server:recv client %d, no validate seq package: seq:%d vs vali_seq:%d" , handle_fd ,
       sspackage_recv.data.cs_data.cshead.seq , sspackage_recv.data.cs_data.cshead.validate_seq);
     continue;
    }
    iRet = send_bus_pkg(logic_server_id , connect_server_id , &sspackage_recv);
    if(iRet == 0){
    }
    else
    if(iRet == -2){
     write_log(3 , "connect server: send bus to logic full!");
    }
    else
    if(iRet == -1){
     write_log(3 , "connect server: send to logic failed!");
    }
    send_to_client(handle_fd);
   }
  }
  try_handle_buses();
  if(ticks % 5 == 0){
   send_to_all_clients();
  }
 }
 return 0;
}

canonical_original_code:
int main(int argc, char **argv){
 int iserv_fd;
 int iaccept_fd;
 int iepoll_fd;
 int iactive_fds;
 int handle_fd;
 char arg_segs[10][8];
 int icount;
 int index = 0;
 struct sockaddr_in stserv_addr;
 struct sockaddr_in stcli_addr;
 int iaddr_len = 0;
 struct epoll_event stepoll_event;
 struct epoll_event astepoll_events[-1];
 struct _sspackage sspackage_recv;
 struct sigaction stsig_act;
 int iRet = -1;
 int i;
 unsigned int ticks = 0;
 if(argc < 2){
  write_log(3 , "connect_server:argc < 2 , please input more information worldid.lineid.xxxx , like:1.1.xxxx");
  return -1;
 }
 icount = strsplit(argv[1] , '.' , arg_segs , 10 , 8);
 if(icount < 2){
  write_log(3 , "connect_server:illegal argument 1:%s , exit!" , argv[1]);
  return -1;
 }
 world_id = atoi(arg_segs[0]);
 line_id = atoi(arg_segs[1]);
 if(world_id<=0 || world_id>15 || line_id<=0 || line_id>15){
  write_log(3 , "connect_server:illegal argument1:%s , exit!" , argv[1]);
  return -1;
 }
 printf("world_id:%d , line_id:%d\n" , world_id , line_id);
 connect_server_id = (world_id << 28 & 0xF0000000) | (line_id << 24 & 0x0F000000) | 0x00000000 | 0x00000001;
 logic_server_id = (world_id << 28 & 0xF0000000) | (line_id << 24 & 0x0F000000) | 0x00000000 | 0x00000002;
 log_server_id = (world_id << 28 & 0xF0000000) | (line_id << 24 & 0x0F000000) | 0x00000000 | 0x00000004;
 iserv_fd = socket(2 , SOCK_STREAM , 0 );
 if(iserv_fd < 0){
  write_log(3 , "connect_server:create connect server socket failed!");
  return -1;
 }
 iaddr_len = sizeof(struct sockaddr_in);
 memset(&stserv_addr , 0 , sizeof(stserv_addr));
 stserv_addr.sin_family = 2;
 stserv_addr.sin_addr.s_addr = ((unsigned int) 0x00000000);
 stserv_addr.sin_port = htons(8005);
 iRet = bind(iserv_fd , (struct sockaddr *)&stserv_addr , iaddr_len);
 if(iRet < 0){
  write_log(3 , "connect_server:bind connect socket failed!");
  return -1;
 }
 iepoll_fd = epoll_create(1024 + 1);
 if(iepoll_fd < 0){
  write_log(3 , "connect_server:epoll create failed!");
  return -1;
 }
 stepoll_event.events = EPOLLIN;
 stepoll_event.data.fd = iserv_fd;
 iRet = epoll_ctl(iepoll_fd , 1 , iserv_fd , &stepoll_event);
 if(iRet < 0){
  write_log(3 , "connect_server: append serve fd to epoll failed!");
  return -1;
 }
 listen(iserv_fd , 1024);
 memset(&client , 0 , sizeof(CLIENT));
 memset(&stsig_act , 0 , sizeof(stsig_act));
 stsig_act.__sigaction_handler.sa_handler = handle_signal;
 sigaction(2 , &stsig_act , ((void *)0));
 poll = create_mem_poll();
 if(!poll){
  write_log(3 , "connect_server:create mem poll failed!");
  return -1;
 }else{
  write_log(1 , "connect_server:create mem poll success!");
 }
 iRet = open_bus(connect_server_id , logic_server_id);
 if(iRet < 0){
  write_log(3 , "connect_server:open bus connect <-> logic failed!");
  return -1;
 }
 pruntime_env = (struct _runtime_env *)attach_shm_res(0x00000001 , world_id , line_id);
 if(!pruntime_env){
  write_log(3 , "connect_server:attach runtime env failed!");
  return -1;
 }else{
  write_log(1 , "connect_server:attach runtime env %x success!" , pruntime_env->basic.global_id);
 }
 index = index_last_1bit(0x00000001);
 pruntime_env->basic.proc_info[index].global_id = connect_server_id;
 pruntime_env->basic.proc_info[index].pid = getpid();
 write_log(1 , "start connect_server %s success!" , argv[1]);
 while(1){
  ticks++;
  iactive_fds = epoll_wait(iepoll_fd , astepoll_events , 1024 +1 , 200);
  if(iactive_fds < 0){
   write_log(3 , "connect_server:epoll wait error!");
   return -1;
  }
  for(i=0; i<iactive_fds; i++){
   if(astepoll_events[i].data.fd == iserv_fd){
    memset(&stcli_addr , 0 , sizeof(stcli_addr));
    iaccept_fd = accept(iserv_fd , (struct sockaddr *)&stcli_addr , &iaddr_len);
    write_log(1 , "connect_server:accept a new client: %d\n" , iaccept_fd);
    set_nonblock(iaccept_fd);
    set_sock_buff_size(iaccept_fd , 10 * sizeof(cspackage_t) , 5 * sizeof(cspackage_t));
    client.active_fds[client.max_active_fd] = iaccept_fd;
    client.max_active_fd++;
    stepoll_event.data.fd = iaccept_fd;
    stepoll_event.events = EPOLLIN | EPOLLET | EPOLLRDHUP;
    iRet = epoll_ctl(iepoll_fd , 1 , iaccept_fd , &stepoll_event);
    if(iRet < 0){
     write_log(3 , "connect_server:epoll_ctl %d failed!" , iaccept_fd);
    }
    continue;
   }
   if(astepoll_events[i].events & EPOLLRDHUP){
    write_log(1 , "connect_server:client %d closed!" , astepoll_events[i].data.fd);
    close_client(astepoll_events[i].data.fd);
    continue;
   }
   if(astepoll_events[i].events & EPOLLIN){
    handle_fd = astepoll_events[i].data.fd;
    memset(&sspackage_recv , 0 , sizeof(sspackage_t));
    sspackage_recv.sshead.package_type = 1;
    sspackage_recv.sshead.client_fd = handle_fd;
    iRet = read_socket(handle_fd , &sspackage_recv.data.cs_data);
    if(iRet == sizeof(cspackage_t)){
     printf(">>>read package!\n");
    }
    else
    if(iRet > 0){
     write_log(3 , "connect server: read pakcage bytes len %d < cspackage size %d!" , iRet , sizeof(cspackage_t));
    }
    else{
     write_log(1 , "connect server: nothing to read!");
    }
    if(((sspackage_recv.data.cs_data.cshead.seq >> sspackage_recv.data.cs_data.cshead.major_version+1) + sspackage_recv.data.cs_data.cshead.minor_version % (sspackage_recv.data.cs_data.cshead.proto_type + 255) ) != sspackage_recv.data.cs_data.cshead.validate_seq){
     write_log(1 , "connect_server:recv client %d, no validate seq package: seq:%d vs vali_seq:%d" , handle_fd ,
       sspackage_recv.data.cs_data.cshead.seq , sspackage_recv.data.cs_data.cshead.validate_seq);
     continue;
    }
    iRet = send_bus_pkg(logic_server_id , connect_server_id , &sspackage_recv);
    if(iRet == 0){
    }
    else
    if(iRet == -2){
     write_log(3 , "connect server: send bus to logic full!");
    }
    else
    if(iRet == -1){
     write_log(3 , "connect server: send to logic failed!");
    }
    send_to_client(handle_fd);
   }
  }
  try_handle_buses();
  if(ticks % 5 == 0){
   send_to_all_clients();
  }
 }
 return 0;
}

variable_types: argc int

variable_types:
argv
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 epoll_event { unsigned int events; union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; } data; } [-1]

variable_types: struct _sspackage { struct _sshead { unsigned short package_type; int client_fd; } sshead; union _ssdata { struct cspackage_t { struct _cshead { unsigned int seq; unsigned short proto_type; unsigned short validate_seq; unsigned char major_version; unsigned char minor_version; } cshead; union _csdata { struct _req_validate_info { unsigned char is_validate; unsigned short proto_type; char player_name[64]; char player_passwd[32]; } req_validate_player; struct _reply_validate_info { unsigned char is_validate; } reply_validate_player; char acdata[512]; } data; } cs_data; } data; }

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

variable_types: iRet int

variable_types:
ticks
unsigned int

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 epoll_event { unsigned int events; union epoll_data data; }

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

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

user_defined_types: struct _runtime_env { struct _rdwr_lock rdwr_lock; struct _rt_env_basic basic; }

user_defined_types: struct _rdwr_lock { unsigned char read_lock; unsigned char write_lock; unsigned char rd_lock_count; }

user_defined_types: struct _rt_env_basic { unsigned int global_id; struct _proc_info proc_info[23]; }

user_defined_types: struct _proc_info { unsigned int global_id; int pid; char ctrl_msg[64]; }

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


repo: nmsoccer/XXGame

decompiled_code:
__int64 show_time()
{
puts("Yes,Show Time Now!");
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
puts("Yes,Show Time Now!");
return 0LL;
}

original_code:
int show_time(void)
{
 printf("Yes,Show Time Now!\n");
 return 0;
}

canonical_original_code:
int show_time()
{
 printf("Yes,Show Time Now!\n");
 return 0;
}

return_type: int


original_code:
int module_start(int type , runtime_env_t *pruntime_env , void *ptr)
{
 void *handle = ((void *)0);
 char module_path[256] = "\0";
 module_commond_t *pcommond = (module_commond_t *)ptr;
 printf("module0 starts...\n");
 switch(type)
 {
 case 1:
 break;
 }
 switch(pcommond->type)
 {
 case 1:
  strcpy(module_path , pcommond->data.pmodule_dir_path);
  strcat(module_path , "cs_module0.so");
  printf("~~! reload %s\n" , module_path);
  handle = dlopen(module_path, 0x00001);
  if(!handle)
  {
   printf("load module %s failed!\n" , module_path);
   return -1;
  }
  test = dlsym(handle , "show_time");
  test();
  break;
 default:
  break;
 }
 return -1;
}

canonical_original_code:
int module_start(int type, struct _runtime_env *pruntime_env, void *ptr)
{
 void *handle = ((void *)0);
 char module_path[256] = "\0";
 struct _module_commond *pcommond = (module_commond_t *)ptr;
 printf("module0 starts...\n");
 switch(type)
 {
 case 1:
 break;
 }
 switch(pcommond->type)
 {
 case 1:
  strcpy(module_path , pcommond->data.pmodule_dir_path);
  strcat(module_path , "cs_module0.so");
  printf("~~! reload %s\n" , module_path);
  handle = dlopen(module_path, 0x00001);
  if(!handle)
  {
   printf("load module %s failed!\n" , module_path);
   return -1;
  }
  test = dlsym(handle , "show_time");
  test();
  break;
 default:
  break;
 }
 return -1;
}

variable_types: type int

variable_types: struct _runtime_env { struct _rdwr_lock { unsigned char read_lock; unsigned char write_lock; unsigned char rd_lock_count; } rdwr_lock; struct _rt_env_basic { unsigned int global_id; struct _proc_info { unsigned int global_id; int pid; char ctrl_msg[64]; } proc_info[23]; } basic; } *

variable_types:
ptr
void *

variable_types:
handle
void *

variable_types: struct _module_commond { unsigned char type; union _module_commond_data { char *pmodule_dir_path; void *pdata; } data; } *

user_defined_types: struct _module_commond { unsigned char type; union _module_commond_data data; }


repo: nodeleap/tinyhash

decompiled_code:
__int64 __fastcall tiny_hash_string_hasher(unsigned __int8 *a1)
{
unsigned __int8 v1; // al
unsigned int i; // er8

v1 = *a1;
for ( i = -2128831035; *a1; v1 = *a1 )
{
++a1;
i = 16777619 * (i ^ v1);
}
return i;
}

original_code:
static HASH tiny_hash_string_hasher(const void *key) {
    unsigned char *s = (unsigned char *) key;
    HASH h = 2166136261;
    while (*s) {
        h ^= (u_int32_t) *s++;
        h *= (HASH) 0x01000193;
    }
    return h;
}

canonical_original_code:
static unsigned int tiny_hash_string_hasher(void *key) {
    unsigned char *s = (unsigned char *) key;
    unsigned int h = 2166136261;
    while (*s) {
        h ^= (u_int32_t) *s++;
        h *= (unsigned int) 0x01000193;
    }
    return h;
}

variable_types:
key
void *

variable_types:
s
unsigned char *

variable_types:
h
unsigned int

return_type: unsigned int


decompiled_code:
void __fastcall tiny_hash_reclaim(__int64 a1, _OWORD *a2)
{
*a2 = 0LL;
a2[1] = 0LL;
if ( *(_QWORD *)(a1 + 8) <= (unsigned __int64)a2 )
*(_QWORD *)(a1 + 8) = a2 + 2;
}

original_code:
static void
tiny_hash_reclaim(TinyHash *t, Slot *n)
{
    memset(n, 0, sizeof(*n));
    if (n >= t->free_slot) {
        t->free_slot = n + 1;
    }
}

canonical_original_code:
static void tiny_hash_reclaim(struct TinyHash *t, struct Slot *n)
{
    memset(n, 0, sizeof(*n));
    if (n >= t->free_slot) {
        t->free_slot = n + 1;
    }
}

variable_types: struct TinyHash { struct Slot { void *key; void *value; struct Slot *next; unsigned int hash; unsigned char taken; } *slot; struct Slot *free_slot; unsigned int size; unsigned int count; float max_full; unsigned int max_count; unsigned int (*hasher)(void *); int (*tester)(void *, void *); } *

variable_types:
n
struct Slot *

return_type: void

user_defined_types: struct TinyHash { struct Slot *slot; struct Slot *free_slot; unsigned int size; unsigned int count; float max_full; unsigned int max_count; unsigned int (*hasher)(void *); int (*tester)(void *, void *); }


decompiled_code:
_BOOL8 __fastcall tiny_hash_string_tester(const char *a1, const char *a2)
{
return strcmp(a1, a2) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(const char *a1, const char *a2)
{
return strcmp(a1, a2) == 0;
}

original_code:
static int tiny_hash_string_tester(const void *s1, const void *s2) {
    return !strcmp((const char *) s1, (const char *) s2);
}

canonical_original_code:
static int tiny_hash_string_tester(void *s1, void *s2) {
    return !strcmp((char *) s1, (char *) s2);
}


decompiled_code:
_BOOL8 __fastcall tiny_hash_string_tester_caseless(const char *a1, const char *a2)
{
return strcasecmp(a1, a2) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(const char *a1, const char *a2)
{
return strcasecmp(a1, a2) == 0;
}

original_code:
static int tiny_hash_string_tester_caseless(const void *s1, const void *s2) {
    return !strcasecmp((const char *) s1, (const char *) s2);
}

canonical_original_code:
static int tiny_hash_string_tester_caseless(void *s1, void *s2) {
    return !strcasecmp((char *) s1, (char *) s2);
}


decompiled_code:
__int64 __fastcall tiny_hash_string_hasher_caseless(unsigned __int8 *a1)
{
unsigned __int8 v1; // bl
unsigned __int8 *v2; // rbp
const __int32_t *v3; // rcx
__int64 result; // rax
int v5; // edx

v1 = *a1;
if ( !*a1 )
return 2166136261LL;
v2 = a1;
v3 = *_ctype_tolower_loc();
LODWORD(result) = -2128831035;
do
{
++v2;
v5 = SLOBYTE(v3[v1]);
v1 = *v2;
result = 16777619 * (v5 ^ (unsigned int)result);
}
while ( *v2 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned __int8 *a1)
{
unsigned __int8 v1; // bl
unsigned __int8 *v2; // rbp
const __int32_t *v3; // rcx
__int64 result; // rax
int v5; // edx

v1 = *a1;
if ( !*a1 )
return 2166136261LL;
v2 = a1;
v3 = *_ctype_tolower_loc();
LODWORD(result) = -2128831035;
do
{
++v2;
v5 = SLOBYTE(v3[v1]);
v1 = *v2;
result = 16777619 * (v5 ^ (unsigned int)result);
}
while ( *v2 );
return result;
}

original_code:
static HASH tiny_hash_string_hasher_caseless(const void *key) {
    unsigned char *s = (unsigned char *) key;
    HASH h = 2166136261;
    while (*s) {
        char c;
        c = tolower(*s++);
        h ^= (u_int32_t) c;
        h *= (HASH) 0x01000193;
    }
    return h;
}

canonical_original_code:
static unsigned int tiny_hash_string_hasher_caseless(void *key) {
    unsigned char *s = (unsigned char *) key;
    unsigned int h = 2166136261;
    while (*s) {
        char c;
        c = tolower(*s++);
        h ^= (unsigned int) c;
        h *= (unsigned int) 0x01000193;
    }
    return h;
}

variable_types: c char


decompiled_code:
__int64 __fastcall tiny_hash_locate(__int64 a1, unsigned int a2, __int64 a3)
{
__int64 v4; // r12

v4 = *(_QWORD *)a1 + 32LL * (a2 % *(_DWORD *)(a1 + 16));
do
{
if ( (unsigned int)tiny_hash_test_isra_0(a1, *(_QWORD *)v4, *(_DWORD *)(v4 + 24), a2, a3) )
break;
v4 = *(_QWORD *)(v4 + 16);
}
while ( v4 );
return v4;
}

original_code:
static Slot *
tiny_hash_locate(TinyHash *t, HASH h, const void *k)
{
    Slot *m = (&(t)->slot[(h) % (t)->size]);
    do {
        if (tiny_hash_test(t, m, h, k)) {
            return m;
        }
        else {
            m = m->next;
        }
    } while (m);
    return ((void *)0);
}

canonical_original_code:
static struct Slot *tiny_hash_locate(struct TinyHash *t, unsigned int h, void *k)
{
    struct Slot *m = (&(t)->slot[(h) % (t)->size]);
    do {
        if (tiny_hash_test(t, m, h, k)) {
            return m;
        }
        else {
            m = m->next;
        }
    } while (m);
    return ((void *)0);
}

variable_types: struct TinyHash { struct Slot *slot; struct Slot *free_slot; unsigned int size; unsigned int count; float max_full; unsigned int max_count; unsigned int (*hasher)(void *); int (*tester)(void *, void *); } *

variable_types:
k
void *

variable_types: struct Slot { void *key; void *value; struct Slot *next; unsigned int hash; unsigned char taken; } *


decompiled_code:
__int64 __fastcall tiny_hash_count(__int64 a1)
{
return *(unsigned int *)(a1 + 20);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
return *(unsigned int *)(a1 + 20);
}

original_code:
uint32_t
tiny_hash_count(TinyHash *t)
{
    return t->count;
}

canonical_original_code:
unsigned int tiny_hash_count(struct TinyHash *t)
{
    return t->count;
}

user_defined_types: struct Slot { void *key; void *value; struct Slot *next; unsigned int hash; unsigned char taken; }


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

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

original_code:
void
tiny_hash_destroy(TinyHash *t)
{
    free(t->slot);
    free(t);
}

canonical_original_code:
void tiny_hash_destroy(struct TinyHash *t)
{
    free(t->slot);
    free(t);
}


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

result = *(unsigned int *)(a1 + 20);
if ( (_DWORD)result )
{
memset(*(void **)a1, 0, 32LL * *(unsigned int *)(a1 + 16));
v2 = *(unsigned int *)(a1 + 16);
*(_DWORD *)(a1 + 20) = 0;
result = *(_QWORD *)a1 + 32 * v2;
*(_QWORD *)(a1 + 8) = result;
}
return result;
}

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

result = *(unsigned int *)(a1 + 20);
if ( (_DWORD)result )
{
memset(*(void **)a1, 0, 32LL * *(unsigned int *)(a1 + 16));
v2 = *(unsigned int *)(a1 + 16);
*(_DWORD *)(a1 + 20) = 0;
result = *(_QWORD *)a1 + 32 * v2;
*(_QWORD *)(a1 + 8) = result;
}
return result;
}

original_code:
void
tiny_hash_clear(TinyHash *t)
{
    if (t->count > 0) {
        memset(t->slot, 0, t->size * sizeof(Slot));
        t->free_slot = t->slot + t->size;
        t->count = 0;
    }
}

canonical_original_code:
void tiny_hash_clear(struct TinyHash *t)
{
    if (t->count > 0) {
        memset(t->slot, 0, t->size * sizeof(Slot));
        t->free_slot = t->slot + t->size;
        t->count = 0;
    }
}


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

v2 = (*(__int64 (__fastcall **)(__int64))(a1 + 32))(a2);
return tiny_hash_locate(a1, v2, a2) != 0;
}

original_code:
int
tiny_hash_exists(TinyHash *t, const void *key)
{
    HASH h = t->hasher(key);
    Slot *n = tiny_hash_locate(t, h, key);
    return n ? 1 : 0;
}

canonical_original_code:
int tiny_hash_exists(struct TinyHash *t, void *key)
{
    unsigned int h = t->hasher(key);
    struct Slot *n = tiny_hash_locate(t, h, key);
    return n ? 1 : 0;
}


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

v2 = (*(__int64 (__fastcall **)(__int64))(a1 + 32))(a2);
result = tiny_hash_locate(a1, v2, a2);
if ( result )
result = *(_QWORD *)(result + 8);
return result;
}

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

v2 = (*(__int64 (__fastcall **)(__int64))(a1 + 32))(a2);
result = func6(a1, v2, a2);
if ( result )
result = *(_QWORD *)(result + 8);
return result;
}

original_code:
void *tiny_hash_get(TinyHash *t, const void *key) {
    HASH h = t->hasher(key);
    Slot *n = tiny_hash_locate(t, h, key);
    return n ? (void *) n->value : ((void *)0);
}

canonical_original_code:
void *tiny_hash_get(struct TinyHash *t, void *key) {
    unsigned int h = t->hasher(key);
    struct Slot *n = tiny_hash_locate(t, h, key);
    return n ? (void *) n->value : ((void *)0);
}

return_type: void *


original_code:
int
tiny_hash_remove(TinyHash *t, const void *k)
{
    HASH h = t->hasher(k);
    Slot *m = (&(t)->slot[(h) % (t)->size]);
    Slot *n = m;
    do {
        if (tiny_hash_test(t, n, h, k)) {
            break;
        }
        else {
            m = n;
            n = n->next;
        }
    } while (n);
    if (!n) {
        return -1;
    }
    if (m == n) {
        if (m->next) {
            Slot *p = m->next;
            *m = *m->next;
            tiny_hash_reclaim(t, p);
        }
        else {
            tiny_hash_reclaim(t, n);
        }
    }
    else {
        m->next = n->next;
        tiny_hash_reclaim(t, n);
    }
    ((void) sizeof ((t->count > 0) ? 1 : 0), __extension__ ({ if (t->count > 0) ; else __assert_fail ("t->count > 0", "tinyhash.c", 253, __extension__ __PRETTY_FUNCTION__); }));
    t->count--;
    return 0;
}

canonical_original_code:
int tiny_hash_remove(struct TinyHash *t, void *k)
{
    unsigned int h = t->hasher(k);
    struct Slot *m = (&(t)->slot[(h) % (t)->size]);
    struct Slot *n = m;
    do {
        if (tiny_hash_test(t, n, h, k)) {
            break;
        }
        else {
            m = n;
            n = n->next;
        }
    } while (n);
    if (!n) {
        return -1;
    }
    if (m == n) {
        if (m->next) {
            struct Slot *p = m->next;
            *m = *m->next;
            tiny_hash_reclaim(t, p);
        }
        else {
            tiny_hash_reclaim(t, n);
        }
    }
    else {
        m->next = n->next;
        tiny_hash_reclaim(t, n);
    }
    ((void) sizeof ((t->count > 0) ? 1 : 0), __extension__ ({ if (t->count > 0) ; else __assert_fail ("t->count > 0", "tinyhash.c", 253, __extension__ __PRETTY_FUNCTION__); }));
    t->count--;
    return 0;
}


original_code:
int
tiny_hash_resize(TinyHash *t, uint32_t size)
{
    uint32_t old_size;
    Slot *old_slot;
    uint32_t i;
    if (size > 1073741824) {
        return -1;
    }
    if (size == 0) {
        size = 64;
    }
    old_size = t->size;
    old_slot = t->slot;
    t->slot = (Slot *) malloc(size * sizeof(Slot));
    if (t->slot == ((void *)0)) {
        t->slot = old_slot;
        return -1;
    }
    memset(t->slot, 0, size * sizeof(Slot));
    t->size = size;
    t->free_slot = t->slot + size;
    t->count = 0;
    t->max_count = size * t->max_full;
    for (i = 0; i < old_size; i++) {
        Slot *slot = old_slot + i;
        if (slot->taken) {
            tiny_hash_insert(t, slot->hash, slot->key, slot->value);
        }
    }
    free(old_slot);
    return 0;
}

canonical_original_code:
int tiny_hash_resize(struct TinyHash *t, unsigned int size)
{
    unsigned int old_size;
    struct Slot *old_slot;
    unsigned int i;
    if (size > 1073741824) {
        return -1;
    }
    if (size == 0) {
        size = 64;
    }
    old_size = t->size;
    old_slot = t->slot;
    t->slot = (struct Slot *) malloc(size * sizeof(Slot));
    if (t->slot == ((void *)0)) {
        t->slot = old_slot;
        return -1;
    }
    memset(t->slot, 0, size * sizeof(Slot));
    t->size = size;
    t->free_slot = t->slot + size;
    t->count = 0;
    t->max_count = size * t->max_full;
    for (i = 0; i < old_size; i++) {
        struct Slot *slot = old_slot + i;
        if (slot->taken) {
            tiny_hash_insert(t, slot->hash, slot->key, slot->value);
        }
    }
    free(old_slot);
    return 0;
}

variable_types:
size
unsigned int

variable_types:
i
unsigned int


original_code:
static int
tiny_hash_insert(TinyHash *t, HASH h, const void *key, const void *value)
{
    Slot *m;
    if (t->count >= t->max_count) {
        if (tiny_hash_resize(t, 2 * t->size) != 0) {
            return -1;
        }
    }
    m = (&(t)->slot[(h) % (t)->size]);
    if (m->taken) {
        Slot *n = ((void *)0);
        Slot *mm = (&(t)->slot[(m->hash) % (t)->size]);
        for (n = ((void *)0); t->free_slot > t->slot;) {
            n = --t->free_slot;
            if (!n->taken) {
                break;
            }
        }
        ((void) sizeof ((n && !n->taken) ? 1 : 0), __extension__ ({ if (n && !n->taken) ; else __assert_fail ("n && !n->taken", "tinyhash.c", 178, __extension__ __PRETTY_FUNCTION__); }));
        if (mm == m) {
            n->next = m->next;
            m->next = n;
            n->taken = 1;
            m = n;
        } else {
            while (mm->next != m) {
                mm = mm->next;
            }
            mm->next = n;
            *n = *m;
            m->next = ((void *)0);
        }
    }
    else {
        m->next = ((void *)0);
        m->taken = 1;
    }
    m->key = key;
    m->value = value;
    m->hash = h;
    t->count++;
    return 0;
}

canonical_original_code:
static int tiny_hash_insert(struct TinyHash *t, unsigned int h, void *key, void *value)
{
    struct Slot *m;
    if (t->count >= t->max_count) {
        if (tiny_hash_resize(t, 2 * t->size) != 0) {
            return -1;
        }
    }
    m = (&(t)->slot[(h) % (t)->size]);
    if (m->taken) {
        struct Slot *n = ((void *)0);
        struct Slot *mm = (&(t)->slot[(m->hash) % (t)->size]);
        for (n = ((void *)0); t->free_slot > t->slot;) {
            n = --t->free_slot;
            if (!n->taken) {
                break;
            }
        }
        ((void) sizeof ((n && !n->taken) ? 1 : 0), __extension__ ({ if (n && !n->taken) ; else __assert_fail ("n && !n->taken", "tinyhash.c", 178, __extension__ __PRETTY_FUNCTION__); }));
        if (mm == m) {
            n->next = m->next;
            m->next = n;
            n->taken = 1;
            m = n;
        } else {
            while (mm->next != m) {
                mm = mm->next;
            }
            mm->next = n;
            *n = *m;
            m->next = ((void *)0);
        }
    }
    else {
        m->next = ((void *)0);
        m->taken = 1;
    }
    m->key = key;
    m->value = value;
    m->hash = h;
    t->count++;
    return 0;
}

variable_types:
value
void *


decompiled_code:
__int64 __fastcall tiny_hash_put(__int64 a1, __int64 a2, __int64 a3)
{
unsigned int v4; // er13
__int64 v5; // rax

v4 = (*(__int64 (__fastcall **)(__int64))(a1 + 32))(a2);
v5 = tiny_hash_locate(a1, v4, a2);
if ( !v5 )
return tiny_hash_insert(a1, v4, a2, a3);
*(_QWORD *)(v5 + 8) = a3;
return 0LL;
}

original_code:
int
tiny_hash_put(TinyHash *t, const void *key, const void *value) {
    HASH h = t->hasher(key);
    Slot *n = tiny_hash_locate(t, h, key);
    if (n) {
        n->value = value;
        return 0;
    }
    return tiny_hash_insert(t, h, key, value);
}

canonical_original_code:
int tiny_hash_put(struct TinyHash *t, void *key, void *value) {
    unsigned int h = t->hasher(key);
    struct Slot *n = tiny_hash_locate(t, h, key);
    if (n) {
        n->value = value;
        return 0;
    }
    return tiny_hash_insert(t, h, key, value);
}


decompiled_code:
unsigned __int64 __fastcall tiny_hash_first(__int64 a1)
{
unsigned __int64 result; // rax
unsigned __int64 v2; // rdx

result = *(_QWORD *)a1;
v2 = *(_QWORD *)a1 + 32LL * *(unsigned int *)(a1 + 16);
if ( *(_QWORD *)a1 >= v2 )
return 0LL;
while ( !*(_BYTE *)(result + 28) )
{
result += 32LL;
if ( result >= v2 )
return 0LL;
}
if ( result >= v2 )
return 0LL;
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func18(__int64 a1)
{
unsigned __int64 result; // rax
unsigned __int64 v2; // rdx

result = *(_QWORD *)a1;
v2 = *(_QWORD *)a1 + 32LL * *(unsigned int *)(a1 + 16);
if ( *(_QWORD *)a1 >= v2 )
return 0LL;
while ( !*(_BYTE *)(result + 28) )
{
result += 32LL;
if ( result >= v2 )
return 0LL;
}
if ( result >= v2 )
return 0LL;
return result;
}

original_code:
const TinyHashIterator *tiny_hash_first(TinyHash *t) {
    Slot *slot = t->slot;
    while (slot < t->slot + t->size && !slot->taken) {
        slot++;
    }
    if (slot < t->slot + t->size && slot->taken) {
        return (const TinyHashIterator *) slot;
    }
    return ((void *)0);
}

canonical_original_code:
struct TinyHashIterator *tiny_hash_first(struct TinyHash *t) {
    struct Slot *slot = t->slot;
    while (slot < t->slot + t->size && !slot->taken) {
        slot++;
    }
    if (slot < t->slot + t->size && slot->taken) {
        return (struct TinyHashIterator *) slot;
    }
    return ((void *)0);
}

return_type: struct TinyHashIterator { void *key; void *value; } *


decompiled_code:
unsigned __int64 __fastcall tiny_hash_next(__int64 a1, __int64 a2)
{
unsigned __int64 result; // rax
unsigned __int64 v3; // rdx

result = a2 + 32;
v3 = *(_QWORD *)a1 + 32LL * *(unsigned int *)(a1 + 16);
if ( a2 + 32 >= v3 )
return 0LL;
while ( !*(_BYTE *)(result + 28) )
{
result += 32LL;
if ( result >= v3 )
return 0LL;
}
if ( v3 <= result )
return 0LL;
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func19(__int64 a1, __int64 a2)
{
unsigned __int64 result; // rax
unsigned __int64 v3; // rdx

result = a2 + 32;
v3 = *(_QWORD *)a1 + 32LL * *(unsigned int *)(a1 + 16);
if ( a2 + 32 >= v3 )
return 0LL;
while ( !*(_BYTE *)(result + 28) )
{
result += 32LL;
if ( result >= v3 )
return 0LL;
}
if ( v3 <= result )
return 0LL;
return result;
}

original_code:
const TinyHashIterator *tiny_hash_next(TinyHash *t, const TinyHashIterator *p) {
    Slot *slot = (Slot *) p;
    ++slot;
    while (slot < t->slot + t->size && !slot->taken) {
        slot++;
    }
    if (slot < t->slot + t->size && slot->taken) {
        return (const TinyHashIterator *) slot;
    }
    return ((void *)0);
}

canonical_original_code:
struct TinyHashIterator *tiny_hash_next(struct TinyHash *t, struct TinyHashIterator *p) {
    struct Slot *slot = (Slot *) p;
    ++slot;
    while (slot < t->slot + t->size && !slot->taken) {
        slot++;
    }
    if (slot < t->slot + t->size && slot->taken) {
        return (struct TinyHashIterator *) slot;
    }
    return ((void *)0);
}

variable_types:
p
struct TinyHashIterator *

user_defined_types: struct TinyHashIterator { void *key; void *value; }


original_code:
void tiny_hash_dump(TinyHash *t, char *buf, int length) {
    uint32_t i, so;
    *buf = '\0';
    for (i = 0, so = 0; i < t->size; i++) {
        Slot *slot = &t->slot[i];
        if (slot->taken) {
            so += snprintf(buf + so, length - so, "(%d,%d,%d)", i,
                    slot->hash, slot->next ? (int)(slot->next - t->slot) : -1);
        }
    }
}

canonical_original_code:
void tiny_hash_dump(struct TinyHash *t, char *buf, int length) {
    unsigned int i; unsigned int so;
    *buf = '\0';
    for (i = 0, so = 0; i < t->size; i++) {
        struct Slot *slot = &t->slot[i];
        if (slot->taken) {
            so += snprintf(buf + so, length - so, "(%d,%d,%d)", i,
                    slot->hash, slot->next ? (int)(slot->next - t->slot) : -1);
        }
    }
}

variable_types:
buf
char *

variable_types: length int

variable_types:
so
unsigned int


decompiled_code:
__int64 __fastcall test_hasher(char *a1)
{
_BYTE *v1; // rbx
__int64 v2; // rbp
const unsigned __int16 *v3; // rdx
int v4; // eax
unsigned int i; // er8

v1 = a1;
v2 = *a1;
v3 = *_ctype_b_loc();
if ( (_BYTE)v2 )
{
while ( (v3[v2] & 0x800) == 0 )
{
v2 = (char)*++v1;
if ( !(_BYTE)v2 )
goto LABEL_10;
}
}
else
{
LABEL_10:
if ( (*v3 & 0x800) == 0 )
return 0xFFFFFFFFLL;
}
v4 = (char)*v1;
for ( i = 0; *v1; v4 = (char)*v1 )
{
if ( (v3[(char)v4] & 0x800) == 0 )
break;
++v1;
i = v4 + 10 * i - 48;
}
return i;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *a1)
{
_BYTE *v1; // rbx
__int64 v2; // rbp
const unsigned __int16 *v3; // rdx
int v4; // eax
unsigned int i; // er8

v1 = a1;
v2 = *a1;
v3 = *_ctype_b_loc();
if ( (_BYTE)v2 )
{
while ( (v3[v2] & 0x800) == 0 )
{
v2 = (char)*++v1;
if ( !(_BYTE)v2 )
goto LABEL_10;
}
}
else
{
LABEL_10:
if ( (*v3 & 0x800) == 0 )
return 0xFFFFFFFFLL;
}
v4 = (char)*v1;
for ( i = 0; *v1; v4 = (char)*v1 )
{
if ( (v3[(char)v4] & 0x800) == 0 )
break;
++v1;
i = v4 + 10 * i - 48;
}
return i;
}

original_code:
static uint32_t test_hasher (const void *k) {
    const char *s = (const char *) k;
    uint32_t n;
    while (*s && !((*__ctype_b_loc ())[(int) ((*s))] & (unsigned short int) _ISdigit)) {
        s++;
    }
    if (!((*__ctype_b_loc ())[(int) ((*s))] & (unsigned short int) _ISdigit)) return (uint32_t) -1;
    for (n = 0; *s && ((*__ctype_b_loc ())[(int) ((*s))] & (unsigned short int) _ISdigit); s++) {
        n = n * 10 + (*s - '0');
    }
    return n;
}

canonical_original_code:
static unsigned int test_hasher(void *k) {
    char *s = (const char *) k;
    unsigned int n;
    while (*s && !((*__ctype_b_loc ())[(int) ((*s))] & (unsigned short) _ISdigit)) {
        s++;
    }
    if (!((*__ctype_b_loc ())[(int) ((*s))] & (unsigned short) _ISdigit)) return (unsigned int) -1;
    for (n = 0; *s && ((*__ctype_b_loc ())[(int) ((*s))] & (unsigned short) _ISdigit); s++) {
        n = n * 10 + (*s - '0');
    }
    return n;
}

variable_types:
s
char *

variable_types:
n
unsigned int


repo: noelaye/siggen

decompiled_code:
__int64 find_main()
{
unsigned int v0; // er8
const char **i; // rdx

if ( n_procs <= 0 )
return 0LL;
v0 = 1;
for ( i = (const char **)(procs + 152LL); strcmp(*i, "main"); i += 16 )
{
if ( (int)++v0 > n_procs )
return 0LL;
}
return v0;
}

canonical_decompiled_code:
__int64 func1()
{
unsigned int v0; // er8
const char **i; // rdx

if ( n_procs <= 0 )
return 0LL;
v0 = 1;
for ( i = (const char **)(procs + 152LL); strcmp(*i, "main"); i += 16 )
{
if ( (int)++v0 > n_procs )
return 0LL;
}
return v0;
}

original_code:
int find_main()
{
 int p;
 for (p = 1; p <= n_procs; p++){
  if (strcmp(procs[p].proc_name,"main") == 0)
   return p;
 }
 return 0;
}

variable_types: p int


decompiled_code:
__int64 current_chain()
{
return (unsigned int)max_chain;
}

canonical_decompiled_code:
__int64 func0()
{
return (unsigned int)max_chain;
}

original_code: int current_chain() {return max_chain;}

canonical_original_code: int current_chain() {return max_chain;}


repo: noonat/hashtable

decompiled_code:
void *__fastcall table_init(__int64 a1, int a2, void *a3, void *a4)
{
void *result; // rax

if ( !a1 )
_assert_fail("table != NULL", "table.c", 0x10Eu, "table_init");
if ( !a2 )
_assert_fail("type != H_NULL", "table.c", 0x10Fu, "table_init");
*(_QWORD *)a1 = &dummy_node;
if ( !a3 )
a3 = &hash_code;
result = &hash_equal;
*(_DWORD *)(a1 + 24) = a2;
if ( !a4 )
a4 = &hash_equal;
*(_DWORD *)(a1 + 8) = 0;
*(_QWORD *)(a1 + 16) = &dummy_node;
*(_QWORD *)(a1 + 32) = a3;
*(_QWORD *)(a1 + 40) = a4;
return result;
}

canonical_decompiled_code:
void *__fastcall func8(__int64 a1, int a2, void *a3, void *a4)
{
void *result; // rax

if ( !a1 )
_assert_fail("table != NULL", "table.c", 0x10Eu, "table_init");
if ( !a2 )
_assert_fail("type != H_NULL", "table.c", 0x10Fu, "table_init");
*(_QWORD *)a1 = &dummy_node;
if ( !a3 )
a3 = &hash_code;
result = &hash_equal;
*(_DWORD *)(a1 + 24) = a2;
if ( !a4 )
a4 = &hash_equal;
*(_DWORD *)(a1 + 8) = 0;
*(_QWORD *)(a1 + 16) = &dummy_node;
*(_QWORD *)(a1 + 32) = a3;
*(_QWORD *)(a1 + 40) = a4;
return result;
}

original_code:
void table_init(table_t * const table, const htype_t type, hash_func_t fn,
                hash_equal_func_t eq_fn) {
  ((table != ((void *)0)) ? (void) (0) : __assert_fail ("table != NULL", "table.c", 270, __extension__ __PRETTY_FUNCTION__));
  ((type != H_NULL) ? (void) (0) : __assert_fail ("type != H_NULL", "table.c", 271, __extension__ __PRETTY_FUNCTION__));
  table->nodes = ((table_node_t *)&_dummy_node);
  table->nodes_size_log = 0;
  table->inactive_node = ((table_node_t *)&_dummy_node);
  table->hash_type = type;
  table->hash_func = fn ? fn : hash_code;
  table->hash_equal_func = eq_fn ? eq_fn : hash_equal;
}

canonical_original_code:
void table_init(struct _table *table, enum _htype type, unsigned int (*fn)(enum _htype, unsigned long), int (*eq_fn)(enum _htype, unsigned long, unsigned long)) {
  ((table != ((void *)0)) ? (void) (0) : __assert_fail ("table != NULL", "table.c", 270, __extension__ __PRETTY_FUNCTION__));
  ((type != H_NULL) ? (void) (0) : __assert_fail ("type != H_NULL", "table.c", 271, __extension__ __PRETTY_FUNCTION__));
  table->nodes = ((struct _table_node *)&_dummy_node);
  table->nodes_size_log = 0;
  table->inactive_node = ((struct _table_node *)&_dummy_node);
  table->hash_type = type;
  table->hash_func = fn ? fn : hash_code;
  table->hash_equal_func = eq_fn ? eq_fn : hash_equal;
}

variable_types: struct _table { struct _table_node { unsigned long key; unsigned long value; struct _table_node *next; char active; } *nodes; int nodes_size_log; struct _table_node *inactive_node; enum _htype { H_NULL=0, H_INT8=1, H_INT16=2, H_INT32=3, H_INT64=4, H_UINT8=5, H_UINT16=6, H_UINT32=7, H_UINT64=8, H_STRING=9, H_VOID=10 } hash_type; unsigned int (*hash_func)(enum _htype { H_NULL=0, H_INT8=1, H_INT16=2, H_INT32=3, H_INT64=4, H_UINT8=5, H_UINT16=6, H_UINT32=7, H_UINT64=8, H_STRING=9, H_VOID=10 }, unsigned long); int (*hash_equal_func)(enum _htype { H_NULL=0, H_INT8=1, H_INT16=2, H_INT32=3, H_INT64=4, H_UINT8=5, H_UINT16=6, H_UINT32=7, H_UINT64=8, H_STRING=9, H_VOID=10 }, unsigned long, unsigned long); enum _table_error { TABLE_ERROR_NONE=0, TABLE_ERROR_MEMORY=1, TABLE_ERROR_OVERFLOW=2, MAX_TABLE_ERROR=3 } error; } *

variable_types: unsigned int (*)(enum _htype { H_NULL=0, H_INT8=1, H_INT16=2, H_INT32=3, H_INT64=4, H_UINT8=5, H_UINT16=6, H_UINT32=7, H_UINT64=8, H_STRING=9, H_VOID=10 }, unsigned long)

variable_types: int (*)(enum _htype { H_NULL=0, H_INT8=1, H_INT16=2, H_INT32=3, H_INT64=4, H_UINT8=5, H_UINT16=6, H_UINT32=7, H_UINT64=8, H_STRING=9, H_VOID=10 }, unsigned long, unsigned long)

user_defined_types: struct _table { struct _table_node *nodes; int nodes_size_log; struct _table_node *inactive_node; enum _htype hash_type; unsigned int (*hash_func)(enum _htype, unsigned long); int (*hash_equal_func)(enum _htype, unsigned long, unsigned long); enum _table_error error; }


decompiled_code:
void __fastcall table_destroy(void **a1)
{
if ( a1 )
{
table_nodes_free(*a1);
*a1 = 0LL;
}
}

canonical_decompiled_code:
void __fastcall func9(void **a1)
{
if ( a1 )
{
func3(*a1);
*a1 = 0LL;
}
}

original_code:
void table_destroy(table_t * const table) {
  if (table != ((void *)0)) {
    _table_nodes_free(table->nodes);
    table->nodes = ((void *)0);
  }
}

canonical_original_code:
void table_destroy(struct _table *table) {
  if (table != ((void *)0)) {
    _table_nodes_free(table->nodes);
    table->nodes = ((void *)0);
  }
}

user_defined_types: enum _htype { H_NULL=0, H_INT8=1, H_INT16=2, H_INT32=3, H_INT64=4, H_UINT8=5, H_UINT16=6, H_UINT32=7, H_UINT64=8, H_STRING=9, H_VOID=10 }

user_defined_types: enum _table_error { TABLE_ERROR_NONE=0, TABLE_ERROR_MEMORY=1, TABLE_ERROR_OVERFLOW=2, MAX_TABLE_ERROR=3 }

user_defined_types: struct _table_node { unsigned long key; unsigned long value; struct _table_node *next; char active; }


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

if ( !a1 )
_assert_fail("table != NULL", "table.c", 0x120u, "table_get");
v2 = table_node_find_key(a1, a2, 0LL);
v3 = 0LL;
if ( v2 )
v3 = v2[1];
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, __int64 a2)
{
_QWORD *v2; // rax
__int64 v3; // r8

if ( !a1 )
_assert_fail("table != NULL", "table.c", 0x120u, "table_get");
v2 = table_node_find_key(a1, a2, 0LL);
v3 = 0LL;
if ( v2 )
v3 = v2[1];
return v3;
}

original_code:
hvalue_t table_get(const table_t * const table, const hvalue_t key) {
  ((table != ((void *)0)) ? (void) (0) : __assert_fail ("table != NULL", "table.c", 288, __extension__ __PRETTY_FUNCTION__));
  const table_node_t * const node = _table_node_find_key(table, key, ((void *)0));
  return node != ((void *)0) ? node->value : 0;
}

canonical_original_code:
unsigned long table_get(struct _table *table, unsigned long key) {
  ((table != ((void *)0)) ? (void) (0) : __assert_fail ("table != NULL", "table.c", 288, __extension__ __PRETTY_FUNCTION__));
  struct _table_node *node = _table_node_find_key(table, key, ((void *)0));
  return node != ((void *)0) ? node->value : 0;
}

variable_types:
key
unsigned long

variable_types: struct _table_node { unsigned long key; unsigned long value; struct _table_node *next; char active; } *

return_type: unsigned long


decompiled_code:
_BOOL8 __fastcall table_contains(__int64 a1, __int64 a2)
{
_QWORD *v2; // rax
_BOOL4 v3; // er8

if ( !a1 )
_assert_fail("table != NULL", "table.c", 0x134u, "table_contains");
v2 = table_node_find_key(a1, a2, 0LL);
v3 = 0;
if ( v2 )
v3 = *((_BYTE *)v2 + 24) != 0;
return v3;
}

canonical_decompiled_code:
_BOOL8 __fastcall func12(__int64 a1, __int64 a2)
{
_QWORD *v2; // rax
_BOOL4 v3; // er8

if ( !a1 )
_assert_fail("table != NULL", "table.c", 0x134u, "table_contains");
v2 = table_node_find_key(a1, a2, 0LL);
v3 = 0;
if ( v2 )
v3 = *((_BYTE *)v2 + 24) != 0;
return v3;
}

original_code:
int32_t table_contains(const table_t * const table, const hvalue_t key) {
  ((table != ((void *)0)) ? (void) (0) : __assert_fail ("table != NULL", "table.c", 308, __extension__ __PRETTY_FUNCTION__));
  const table_node_t * const node = _table_node_find_key(table, key, ((void *)0));
  return node != ((void *)0) && node->active;
}

canonical_original_code:
int table_contains(struct _table *table, unsigned long key) {
  ((table != ((void *)0)) ? (void) (0) : __assert_fail ("table != NULL", "table.c", 308, __extension__ __PRETTY_FUNCTION__));
  struct _table_node *node = _table_node_find_key(table, key, ((void *)0));
  return node != ((void *)0) && node->active;
}


decompiled_code:
const char *__fastcall table_error_string(unsigned int a1)
{
const char *result; // rax

result = "unknown error";
if ( a1 <= 2 )
result = CSWTCH_26[a1];
return result;
}

canonical_decompiled_code:
const char *__fastcall func14(unsigned int a1)
{
const char *result; // rax

result = "unknown error";
if ( a1 <= 2 )
result = CSWTCH_26[a1];
return result;
}

original_code:
const char *table_error_string(const table_error_t error) {
  switch (error) {
    case TABLE_ERROR_NONE:
      return "none";
    case TABLE_ERROR_MEMORY:
      return "out of memory";
    case TABLE_ERROR_OVERFLOW:
      return "table size too large";
    default:
      return "unknown error";
  }
}

canonical_original_code:
char *table_error_string(enum _table_error error) {
  switch (error) {
    case TABLE_ERROR_NONE:
      return "none";
    case TABLE_ERROR_MEMORY:
      return "out of memory";
    case TABLE_ERROR_OVERFLOW:
      return "table size too large";
    default:
      return "unknown error";
  }
}

variable_types: enum _table_error { TABLE_ERROR_NONE=0, TABLE_ERROR_MEMORY=1, TABLE_ERROR_OVERFLOW=2, MAX_TABLE_ERROR=3 }

return_type: char *


decompiled_code:
__int64 __fastcall hash_code(unsigned int a1, char *a2)
{
if ( a1 == 9 )
return hash_func_string_constprop_0(a2);
if ( a1 > 9 || ((a1 - 4) & 0xFFFFFFFB) != 0 )
return (unsigned int)a2;
return hash_func_int64_constprop_0((__int64)a2);
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1, char *a2)
{
if ( a1 == 9 )
return func1(a2);
if ( a1 > 9 || ((a1 - 4) & 0xFFFFFFFB) != 0 )
return (unsigned int)a2;
return func0((__int64)a2);
}

original_code:
hcode_t hash_code(const htype_t type, const hvalue_t value) {
  switch (type) {
    case H_INT64:
    case H_UINT64:
      return hash_func_int64(type, value);
    case H_STRING:
      return hash_func_string(type, value);
    case H_VOID:
      return (hcode_t)(size_t)value;
    default:
      return (hcode_t)value;
  }
}

canonical_original_code:
unsigned int hash_code(enum _htype type, unsigned long value) {
  switch (type) {
    case H_INT64:
    case H_UINT64:
      return hash_func_int64(type, value);
    case H_STRING:
      return hash_func_string(type, value);
    case H_VOID:
      return (unsigned int)(unsigned long)value;
    default:
      return (unsigned int)value;
  }
}

variable_types: enum _htype { H_NULL=0, H_INT8=1, H_INT16=2, H_INT32=3, H_INT64=4, H_UINT8=5, H_UINT16=6, H_UINT32=7, H_UINT64=8, H_STRING=9, H_VOID=10 }

variable_types:
value
unsigned long


decompiled_code:
_BOOL8 __fastcall hash_equal(int a1, const char *a2, const char *a3)
{
_BOOL8 result; // rax

result = a2 == a3;
if ( a1 == 9 )
{
if ( a3 != 0LL && a2 != 0LL || (result = 0LL, a2 == a3) )
result = strcmp(a2, a3) == 0;
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(int a1, const char *a2, const char *a3)
{
_BOOL8 result; // rax

result = a2 == a3;
if ( a1 == 9 )
{
if ( a3 != 0LL && a2 != 0LL || (result = 0LL, a2 == a3) )
result = strcmp(a2, a3) == 0;
}
return result;
}

original_code:
int32_t hash_equal(const htype_t type, const hvalue_t a, const hvalue_t b) {
  switch (type) {
    case H_STRING: {
      const char *sa = (const char *)(size_t)a;
      const char *sb = (const char *)(size_t)b;
      if ((sa == ((void *)0) || sb == ((void *)0)) && sa != sb) {
        return 0;
      } else{
        return strcmp(sa, sb) == 0;
      }
    }
    default:
      return a == b;
  }
}

canonical_original_code:
int hash_equal(enum _htype type, unsigned long a, unsigned long b) {
  switch (type) {
    case H_STRING: {
      char *sa = (const char *)(size_t)a;
      char *sb = (const char *)(size_t)b;
      if ((sa == ((void *)0) || sb == ((void *)0)) && sa != sb) {
        return 0;
      } else{
        return strcmp(sa, sb) == 0;
      }
    }
    default:
      return a == b;
  }
}

variable_types:
a
unsigned long

variable_types:
b
unsigned long

variable_types:
sa
char *

variable_types:
sb
char *


repo: noqsi/NIPP

decompiled_code:
__int64 __fastcall nipp_check_message(__int64 a1)
{
__int64 v1; // rax
unsigned int v2; // er8
__int64 v3; // rdx

v1 = a1 + 1;
v2 = -1;
v3 = a1 + (unsigned __int16)__ROL2__(*(_WORD *)(a1 + 4), 8) + 7;
while ( 1 )
{
LOBYTE(v2) = *(_BYTE *)a1 ^ v2;
a1 = v1;
if ( v1 == v3 )
break;
++v1;
}
return v2;
}

original_code:
uint8_t nipp_check_message( nipp_message_t *m )
{
 unsigned len = ((unsigned)(((*m)[4]<<8)|(*m)[5])+1) + 6;
 unsigned i;
 uint8_t parity = 0xff;
 for( i = 0; i < len; i += 1 )
  parity ^= (*m)[i];
 return parity;
}

canonical_original_code:
unsigned char nipp_check_message(unsigned char (*m)[-1])
{
 unsigned int len = ((unsigned)(((*m)[4]<<8)|(*m)[5])+1) + 6;
 unsigned int i;
 unsigned char parity = 0xff;
 for( i = 0; i < len; i += 1 )
  parity ^= (*m)[i];
 return parity;
}

variable_types:
m
unsigned char (*)[-1]

variable_types:
len
unsigned int

variable_types:
i
unsigned int

variable_types:
parity
unsigned char

return_type: unsigned char


decompiled_code:
__int64 __fastcall nipp_add_checksum(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rcx

*(_BYTE *)(a1 + 7) = 0;
result = nipp_check_message(a1);
*(_BYTE *)(v2 + 7) = result;
return result;
}

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

*(_BYTE *)(a1 + 7) = 0;
result = func0(a1);
*(_BYTE *)(v2 + 7) = result;
return result;
}

original_code:
void
nipp_add_checksum( nipp_message_t *m)
{
 (*m)[7] = 0;
 (*m)[7] = nipp_check_message( m );
}

canonical_original_code:
void nipp_add_checksum(unsigned char (*m)[-1])
{
 (*m)[7] = 0;
 (*m)[7] = nipp_check_message( m );
}


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

if ( a2 > 0x3FF )
{
nipp_errno = 2;
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
*(_WORD *)(a1 + 4) = __ROL2__(a2 - 1, 8);
}
return result;
}

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

if ( a2 > 0x3FF )
{
nipp_errno = 2;
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
*(_WORD *)(a1 + 4) = __ROL2__(a2 - 1, 8);
}
return result;
}

original_code:
int nipp_truncate( nipp_message_t *m, unsigned length )
{
 if( length >= 1024 ) {
  nipp_errno = 2;
  return -1;
 }
 length -= 1;
 (*m)[4] = length>>8;
 (*m)[5] = length;
 return 0;
}

canonical_original_code:
int nipp_truncate(unsigned char (*m)[-1], unsigned int length)
{
 if( length >= 1024 ) {
  nipp_errno = 2;
  return -1;
 }
 length -= 1;
 (*m)[4] = length>>8;
 (*m)[5] = length;
 return 0;
}

variable_types:
length
unsigned int


decompiled_code:
__int64 __fastcall nipp_send(__int64 a1)
{
unsigned __int16 v1; // bx

v1 = __ROL2__(*(_WORD *)(a1 + 4), 8);
if ( (unsigned int)v1 + 1 > 0x400 )
{
nipp_errno = 2;
nipp_abort_tx();
}
else if ( (int)nipp_send_sync() >= 0 )
{
return nipp_send_buffer(a1, (unsigned int)v1 + 7);
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
unsigned __int16 v1; // bx

v1 = __ROL2__(*(_WORD *)(a1 + 4), 8);
if ( (unsigned int)v1 + 1 > 0x400 )
{
nipp_errno = 2;
nipp_abort_tx();
}
else if ( (int)nipp_send_sync() >= 0 )
{
return nipp_send_buffer(a1, (unsigned int)v1 + 7);
}
return 0xFFFFFFFFLL;
}

original_code:
int nipp_send( nipp_message_t *m )
{
 unsigned length = ((unsigned)(((*m)[4]<<8)|(*m)[5])+1);
 if( length > 1024 ) {
  nipp_errno = 2;
  nipp_abort_tx( m );
  return -1;
 }
 if( nipp_send_sync() < 0 ) return -1;
 return nipp_send_buffer( m, length + 6);
}

canonical_original_code:
int nipp_send(unsigned char (*m)[-1])
{
 unsigned int length = ((unsigned)(((*m)[4]<<8)|(*m)[5])+1);
 if( length > 1024 ) {
  nipp_errno = 2;
  nipp_abort_tx( m );
  return -1;
 }
 if( nipp_send_sync() < 0 ) return -1;
 return nipp_send_buffer( m, length + 6);
}


decompiled_code:
void *__fastcall nipp_get_message(int a1)
{
__int64 v1; // rax
int v2; // eax
void *result; // rax
int v4; // ebx
__int64 v5; // rax
unsigned int v6; // ebx
int v7; // eax
int v8[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v8[0] = a1;
if ( !found_sync_2 )
{
if ( (int)nipp_find_sync(v8) < 0 )
return 0LL;
found_sync_2 = 1;
}
v1 = (unsigned int)bytes_1;
if ( (unsigned int)bytes_1 <= 5 )
{
do
{
v2 = nipp_get_bytes((char *)&b_0 + v1, (unsigned int)(6 - v1), v8);
if ( !v2 )
return 0LL;
v1 = (unsigned int)(bytes_1 + v2);
bytes_1 = v1;
}
while ( (unsigned int)v1 <= 5 );
}
v4 = (unsigned __int16)__ROL2__(word_4C4, 8);
if ( (unsigned int)(unsigned __int16)v4 + 1 > 0x400 )
{
nipp_errno = 2;
nipp_abort_rx();
bytes_1 = 0;
return 0LL;
}
v5 = (unsigned int)bytes_1;
v6 = v4 + 7;
if ( v6 > bytes_1 )
{
do
{
v7 = nipp_get_bytes((char *)&b_0 + v5, v6 - (unsigned int)v5, v8);
if ( !v7 )
return 0LL;
v5 = (unsigned int)(bytes_1 + v7);
bytes_1 = v5;
}
while ( (unsigned int)v5 < v6 );
}
found_sync_2 = 0;
result = &b_0;
bytes_1 = 0;
return result;
}

canonical_decompiled_code:
void *__fastcall func6(int a1)
{
__int64 v1; // rax
int v2; // eax
void *result; // rax
int v4; // ebx
__int64 v5; // rax
unsigned int v6; // ebx
int v7; // eax
int v8[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v8[0] = a1;
if ( !found_sync_2 )
{
if ( (int)nipp_find_sync(v8) < 0 )
return 0LL;
found_sync_2 = 1;
}
v1 = (unsigned int)bytes_1;
if ( (unsigned int)bytes_1 <= 5 )
{
do
{
v2 = nipp_get_bytes((char *)&b_0 + v1, (unsigned int)(6 - v1), v8);
if ( !v2 )
return 0LL;
v1 = (unsigned int)(bytes_1 + v2);
bytes_1 = v1;
}
while ( (unsigned int)v1 <= 5 );
}
v4 = (unsigned __int16)__ROL2__(word_4C4, 8);
if ( (unsigned int)(unsigned __int16)v4 + 1 > 0x400 )
{
nipp_errno = 2;
nipp_abort_rx();
bytes_1 = 0;
return 0LL;
}
v5 = (unsigned int)bytes_1;
v6 = v4 + 7;
if ( v6 > bytes_1 )
{
do
{
v7 = nipp_get_bytes((char *)&b_0 + v5, v6 - (unsigned int)v5, v8);
if ( !v7 )
return 0LL;
v5 = (unsigned int)(bytes_1 + v7);
bytes_1 = v5;
}
while ( (unsigned int)v5 < v6 );
}
found_sync_2 = 0;
result = &b_0;
bytes_1 = 0;
return result;
}

original_code:
nipp_message_t *nipp_get_message( unsigned timeout )
{
 static nipp_message_t b;
 static unsigned bytes = 0;
 static int found_sync = 0;
 unsigned c, t;
 int n;
 int hdrlen;
 if( !found_sync ) {
  n = nipp_find_sync( &timeout );
  if( n < 0 ) return 0;
  found_sync = 1;
 }
 hdrlen = 6;
 while( bytes < hdrlen )
 {
  c = nipp_get_bytes( b + bytes, hdrlen - bytes,
       &timeout );
  if( !c ) return 0;
  bytes += c;
 }
 t = ((unsigned)(((*&b)[4]<<8)|(*&b)[5])+1);
 if( t > 1024 ) {
  nipp_errno = 2;
  nipp_abort_rx();
  bytes = 0;
  return 0;
 }
 t += 6;
 while( bytes < t ) {
  c = nipp_get_bytes( b + bytes, t - bytes, &timeout );
  if( !c ) return 0;
  bytes += c;
 }
 bytes = found_sync = 0;
 return &b;
}

canonical_original_code:
unsigned char (*nipp_get_message(unsigned int timeout))[-1]
{
 static unsigned char b[-1];
 static unsigned int bytes = 0;
 static int found_sync = 0;
 unsigned int c; unsigned int t;
 int n;
 int hdrlen;
 if( !found_sync ) {
  n = nipp_find_sync( &timeout );
  if( n < 0 ) return 0;
  found_sync = 1;
 }
 hdrlen = 6;
 while( bytes < hdrlen )
 {
  c = nipp_get_bytes( b + bytes, hdrlen - bytes,
       &timeout );
  if( !c ) return 0;
  bytes += c;
 }
 t = ((unsigned int)(((*&b)[4]<<8)|(*&b)[5])+1);
 if( t > 1024 ) {
  nipp_errno = 2;
  nipp_abort_rx();
  bytes = 0;
  return 0;
 }
 t += 6;
 while( bytes < t ) {
  c = nipp_get_bytes( b + bytes, t - bytes, &timeout );
  if( !c ) return 0;
  bytes += c;
 }
 bytes = found_sync = 0;
 return &b;
}

variable_types:
timeout
unsigned int

variable_types:
b
unsigned char [-1]

variable_types:
bytes
unsigned int

variable_types:
c
unsigned int

variable_types:
t
unsigned int

variable_types: n int

variable_types: hdrlen int

return_type: unsigned char (*)[-1]


decompiled_code:
__int64 nipp_default_handler()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func7()
{
return 0LL;
}

original_code:
int nipp_default_handler( nipp_message_t *msg )
{
 return 0;
}

canonical_original_code:
int nipp_default_handler(unsigned char (*msg)[-1])
{
 return 0;
}

variable_types:
msg
unsigned char (*)[-1]


original_code:
unsigned int
nipp_unpack(uint8_t *m, int offset, int lenval)
{
 int out=0;
 int outlen = 0;
 int byte, toget, offbit, offbyte;
 while (outlen < lenval)
 {
        offbit = (offset + outlen) % 8;
        offbyte = (offset + outlen) / 8;
  byte = m[offbyte];
  if (lenval + offbit - outlen < 8)
   toget = lenval - outlen;
  else
   toget = 8 - offbit;
  offbit = 8 - (offbit + toget);
  out = (out << toget) | ((byte >> offbit) & ((1<<toget)-1));
  outlen += toget;
 }
 return out;
}

canonical_original_code:
unsigned int nipp_unpack(unsigned char *m, int offset, int lenval)
{
 int out = 0;
 int outlen = 0;
 int byte; int toget; int offbit; int offbyte;
 while (outlen < lenval)
 {
        offbit = (offset + outlen) % 8;
        offbyte = (offset + outlen) / 8;
  byte = m[offbyte];
  if (lenval + offbit - outlen < 8)
   toget = lenval - outlen;
  else
   toget = 8 - offbit;
  offbit = 8 - (offbit + toget);
  out = (out << toget) | ((byte >> offbit) & ((1<<toget)-1));
  outlen += toget;
 }
 return out;
}

variable_types:
m
unsigned char *

variable_types: offset int

variable_types: lenval int

variable_types: out int

variable_types: outlen int

variable_types: byte int

variable_types: toget int

variable_types: offbit int

variable_types: offbyte int


original_code:
void
nipp_pack(uint8_t *m, int offset, int lenval, unsigned int val)
{
 int endoffset, mask, toset, val2set;
 int offbit, offbyte;
    endoffset = offset + lenval;
    while (offset < endoffset)
 {
        offbit = offset % 8;
        offbyte = offset / 8;
        mask=0;
        toset = 8-offbit;
        if (offset + toset > endoffset)
            toset = endoffset - offset;
        val2set = val >> (endoffset - offset - toset);
        offbit = 8 - (offbit + toset);
        if (offbit > 0)
            mask = (1<<offbit) - 1;
        if (toset + offbit != 8)
            mask = mask | (~((1<< (toset + offbit)) - 1));
        m[offbyte] = (m[offbyte] & mask) | ((val2set << offbit) & (~mask & 0xFF));
        offset = offset + toset;
 }
}

canonical_original_code:
void nipp_pack(unsigned char *m, int offset, int lenval, unsigned int val)
{
 int endoffset; int mask; int toset; int val2set;
 int offbit; int offbyte;
    endoffset = offset + lenval;
    while (offset < endoffset)
 {
        offbit = offset % 8;
        offbyte = offset / 8;
        mask=0;
        toset = 8-offbit;
        if (offset + toset > endoffset)
            toset = endoffset - offset;
        val2set = val >> (endoffset - offset - toset);
        offbit = 8 - (offbit + toset);
        if (offbit > 0)
            mask = (1<<offbit) - 1;
        if (toset + offbit != 8)
            mask = mask | (~((1<< (toset + offbit)) - 1));
        m[offbyte] = (m[offbyte] & mask) | ((val2set << offbit) & (~mask & 0xFF));
        offset = offset + toset;
 }
}

variable_types:
val
unsigned int

variable_types: endoffset int

variable_types: mask int

variable_types: toset int


repo: norayr/oric-tools

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

result = (unsigned int)plotter;
if ( (int)plotter <= 479 )
{
result = (unsigned int)(plotter + 1);
LODWORD(plotter) = plotter + 1;
}
return result;
}

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

result = (unsigned int)plotter;
if ( (int)plotter <= 479 )
{
result = (unsigned int)(plotter + 1);
LODWORD(plotter) = plotter + 1;
}
return result;
}

original_code:
void inc_x()
{
 if (plotter.pos.x < 480)
  plotter.pos.x++;
}


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

result = (unsigned int)plotter;
if ( (int)plotter > 0 )
{
result = (unsigned int)(plotter - 1);
LODWORD(plotter) = plotter - 1;
}
return result;
}

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

result = (unsigned int)plotter;
if ( (int)plotter > 0 )
{
result = (unsigned int)(plotter - 1);
LODWORD(plotter) = plotter - 1;
}
return result;
}

original_code:
void dec_x()
{
 if (plotter.pos.x > 0)
  plotter.pos.x--;
}


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

result = roll_sheet((unsigned int)(HIDWORD(plotter) + 1));
if ( (_DWORD)result )
++HIDWORD(plotter);
return result;
}

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

result = roll_sheet((unsigned int)(HIDWORD(plotter) + 1));
if ( (_DWORD)result )
++HIDWORD(plotter);
return result;
}

original_code:
void inc_y()
{
 if (roll_sheet(plotter.pos.y+1))
  plotter.pos.y++;
}


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

result = roll_sheet((unsigned int)(HIDWORD(plotter) - 1));
if ( (_DWORD)result )
--HIDWORD(plotter);
return result;
}

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

result = roll_sheet((unsigned int)(HIDWORD(plotter) - 1));
if ( (_DWORD)result )
--HIDWORD(plotter);
return result;
}

original_code:
void dec_y()
{
 if (roll_sheet(plotter.pos.y-1))
  plotter.pos.y--;
}


decompiled_code:
void __fastcall change_color(unsigned int a1)
{
if ( a1 <= 3 )
dword_78C = a1;
}

canonical_decompiled_code:
void __fastcall func4(unsigned int a1)
{
if ( a1 <= 3 )
dword_78C = a1;
}

original_code:
void change_color(int pen)
{
 if (pen>=0 && pen<=3)
  plotter.pen=pen;
}

variable_types: pen int


decompiled_code:
void __fastcall set_dir(unsigned int a1)
{
if ( a1 <= 3 )
dword_790 = a1;
}

canonical_decompiled_code:
void __fastcall func5(unsigned int a1)
{
if ( a1 <= 3 )
dword_790 = a1;
}

original_code:
void set_dir(int dir)
{
 if (dir>=0 && dir<=3)
  plotter.direction=dir;
}

variable_types: dir int


home

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

result = qword_79C;
plotter = qword_79C;
return result;
}

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

result = qword_79C;
plotter = qword_79C;
return result;
}

original_code: void home() { plotter.pos=plotter.origin; }


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

result = plotter;
qword_79C = plotter;
return result;
}

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

result = plotter;
qword_79C = plotter;
return result;
}

original_code: void set_origin() { plotter.origin=plotter.pos; }


decompiled_code:
__int64 set_graphic_mode()
{
dword_798 = 1;
return set_origin();
}

canonical_decompiled_code:
__int64 func8()
{
dword_798 = 1;
return func7();
}

original_code: void set_graphic_mode() { plotter.graphics=true; set_origin(); }


decompiled_code:
void set_text_mode()
{
dword_798 = 0;
}

canonical_decompiled_code:
void func9()
{
dword_798 = 0;
}

original_code: void set_text_mode() { plotter.graphics=false; }


decompiled_code:
void __fastcall set_line_type(unsigned int a1)
{
if ( a1 <= 0xF )
dword_7A4 = a1;
}

canonical_decompiled_code:
void __fastcall func10(unsigned int a1)
{
if ( a1 <= 0xF )
dword_7A4 = a1;
}

original_code:
void set_line_type(int line_type)
{
 if (line_type>=0 && line_type<=15)
  plotter.line_type=line_type;
}


decompiled_code:
void __fastcall set_char_size(unsigned int a1)
{
if ( a1 <= 0x3F )
dword_794 = a1;
}

canonical_decompiled_code:
void __fastcall func11(unsigned int a1)
{
if ( a1 <= 0x3F )
dword_794 = a1;
}

original_code:
void set_char_size(int char_size)
{
 if (char_size>=0 && char_size<=63)
  plotter.char_size=char_size;
}


plot

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

result = roll_sheet(a1 >> 32);
if ( (_DWORD)result )
result = mark_point((unsigned int)a1, (unsigned int)(a2 + 1));
return result;
}

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

result = roll_sheet(a1 >> 32);
if ( (_DWORD)result )
result = mark_point((unsigned int)a1, (unsigned int)(a2 + 1));
return result;
}

original_code:
void plot(position pos, int colour)
{
 if (roll_sheet(pos.y))
  mark_point(pos.x,colour+1);
}

canonical_original_code:
void plot(struct position pos, int colour)
{
 if (roll_sheet(pos.y))
  mark_point(pos.x,colour+1);
}

variable_types:
pos
struct position { int x; }

variable_types: colour int


decompiled_code:
__int64 pen_down()
{
dword_788 = 1;
return plot(plotter, dword_78C);
}

canonical_decompiled_code:
__int64 func13()
{
dword_788 = 1;
return func12(plotter, dword_78C);
}

original_code:
void pen_down()
{
 plotter.pen_down=true;
 plot(plotter.pos,plotter.pen);
}


decompiled_code:
void pen_down_no_mark()
{
dword_788 = 1;
}

canonical_decompiled_code:
void func14()
{
dword_788 = 1;
}

original_code:
void pen_down_no_mark()
{
 plotter.pen_down=true;
}


decompiled_code:
void pen_up()
{
dword_788 = 0;
}

canonical_decompiled_code:
void func15()
{
dword_788 = 0;
}

original_code: void pen_up() { plotter.pen_down=false; }


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

result = (unsigned int)((dword_78C + 1) % 4);
dword_78C = (dword_78C + 1) % 4;
return result;
}

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

result = (unsigned int)((dword_78C + 1) % 4);
dword_78C = (dword_78C + 1) % 4;
return result;
}

original_code: void rotate_color() { plotter.pen=(plotter.pen+1)%4; }


decompiled_code:
__int64 get_dir()
{
return (unsigned int)dword_790;
}

canonical_decompiled_code:
__int64 func17()
{
return (unsigned int)dword_790;
}

original_code: int get_dir() { return plotter.direction; }


decompiled_code:
__int64 get_char_size()
{
return (unsigned int)dword_794;
}

canonical_decompiled_code:
__int64 func18()
{
return (unsigned int)dword_794;
}

original_code: int get_char_size() { return plotter.char_size; }


decompiled_code:
__int64 get_pen_pos()
{
return plotter;
}

canonical_decompiled_code:
__int64 func19()
{
return plotter;
}

canonical_original_code: struct position get_pen_pos() { return plotter.pos; }

return_type: struct position { int x; }


decompiled_code:
__int64 get_origin()
{
return qword_79C;
}

canonical_decompiled_code:
__int64 func20()
{
return qword_79C;
}

canonical_original_code: struct position get_origin() { return plotter.origin; }


decompiled_code:
void __fastcall set_pen_pos(__int64 a1)
{
plotter = a1;
}

canonical_decompiled_code:
void __fastcall func21(__int64 a1)
{
plotter = a1;
}

original_code: void set_pen_pos(position newpos) { plotter.pos=newpos; }

canonical_original_code: void set_pen_pos(struct position newpos) { plotter.pos=newpos; }

variable_types:
newpos
struct position { int x; }


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

switch ( a1 )
{
case 0:
goto LABEL_10;
case 1:
dec_y();
LABEL_10:
if ( dword_788 )
plot(plotter, dword_78C);
return inc_x();
case 2:
result = dec_y();
if ( dword_788 )
return plot(plotter, dword_78C);
return result;
case 3:
dec_y();
goto LABEL_7;
case 4:
LABEL_7:
result = dec_x();
if ( dword_788 )
return plot(plotter, dword_78C);
return result;
case 5:
dec_x();
goto LABEL_3;
case 6:
LABEL_3:
if ( dword_788 )
plot(plotter, dword_78C);
result = inc_y();
break;
case 7:
if ( dword_788 )
plot(plotter, dword_78C);
inc_y();
result = inc_x();
break;
default:
return result;
}
return result;
}

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

switch ( a1 )
{
case 0:
goto LABEL_10;
case 1:
func3();
LABEL_10:
if ( dword_788 )
func12(plotter, dword_78C);
return func0();
case 2:
result = func3();
if ( dword_788 )
return func12(plotter, dword_78C);
return result;
case 3:
func3();
goto LABEL_7;
case 4:
LABEL_7:
result = func1();
if ( dword_788 )
return func12(plotter, dword_78C);
return result;
case 5:
func1();
goto LABEL_3;
case 6:
LABEL_3:
if ( dword_788 )
func12(plotter, dword_78C);
result = func2();
break;
case 7:
if ( dword_788 )
func12(plotter, dword_78C);
func2();
result = func0();
break;
default:
return result;
}
return result;
}

original_code:
void pen_step(int absolute_dir)
{
 switch (absolute_dir) {
 case 1:
  dec_y();
 case 0:
  if (plotter.pen_down)
   plot(plotter.pos,plotter.pen);
  inc_x();
  break;
 case 2:
  dec_y();
  if (plotter.pen_down)
   plot(plotter.pos,plotter.pen);
  break;
 case 3:
  dec_y();
 case 4:
  dec_x();
  if (plotter.pen_down)
   plot(plotter.pos,plotter.pen);
  break;
 case 5:
  dec_x();
 case 6:
  if (plotter.pen_down)
   plot(plotter.pos,plotter.pen);
  inc_y();
  break;
 case 7:
  if (plotter.pen_down)
   plot(plotter.pos,plotter.pen);
  inc_y();
  inc_x();
  break;
 }
}


decompiled_code:
__int64 __fastcall move_pen(int a1, int a2)
{
int v2; // eax
char v3; // bp
__int64 result; // rax
int v5; // ebp
int v6; // ebx

v2 = get_dir();
v3 = a1 + 2 * (4 - v2);
result = (unsigned int)((a1 + 2 * (4 - v2)) >> 31) >> 29;
v5 = (((_BYTE)result + v3) & 7) - result;
if ( a2 > 0 )
{
v6 = 0;
while ( 2 )
{
switch ( v5 )
{
case 0:
goto LABEL_5;
case 1:
dec_y();
goto LABEL_5;
case 2:
dec_y();
goto LABEL_6;
case 3:
dec_y();
goto LABEL_13;
case 4:
LABEL_13:
dec_x();
goto LABEL_6;
case 5:
dec_x();
goto LABEL_10;
case 6:
LABEL_10:
inc_y();
result = (unsigned int)dword_788;
if ( dword_788 )
goto LABEL_11;
goto LABEL_7;
case 7:
inc_y();
LABEL_5:
inc_x();
goto LABEL_6;
default:
LABEL_6:
result = (unsigned int)dword_788;
if ( dword_788 )
LABEL_11:
result = plot(plotter, dword_78C);
LABEL_7:
if ( a2 == ++v6 )
return result;
continue;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func24(int a1, int a2)
{
int v2; // eax
char v3; // bp
__int64 result; // rax
int v5; // ebp
int v6; // ebx

v2 = func17();
v3 = a1 + 2 * (4 - v2);
result = (unsigned int)((a1 + 2 * (4 - v2)) >> 31) >> 29;
v5 = (((_BYTE)result + v3) & 7) - result;
if ( a2 > 0 )
{
v6 = 0;
while ( 2 )
{
switch ( v5 )
{
case 0:
goto LABEL_5;
case 1:
func3();
goto LABEL_5;
case 2:
func3();
goto LABEL_6;
case 3:
func3();
goto LABEL_13;
case 4:
LABEL_13:
func1();
goto LABEL_6;
case 5:
func1();
goto LABEL_10;
case 6:
LABEL_10:
func2();
result = (unsigned int)dword_788;
if ( dword_788 )
goto LABEL_11;
goto LABEL_7;
case 7:
func2();
LABEL_5:
func0();
goto LABEL_6;
default:
LABEL_6:
result = (unsigned int)dword_788;
if ( dword_788 )
LABEL_11:
result = func12(plotter, dword_78C);
LABEL_7:
if ( a2 == ++v6 )
return result;
continue;
}
}
}
return result;
}

original_code:
void move_pen(int relative_dir,int length)
{
 int i;
 int absolute_dir = (8-2*get_dir()+relative_dir)%8;
 for (i=0;i<length;i++) {
  switch (absolute_dir) {
  case 1: dec_y();
  case 0: inc_x(); break;
  case 2: dec_y(); break;
  case 3:dec_y();
  case 4: dec_x(); break;
  case 5:dec_x();
  case 6: inc_y(); break;
  case 7:inc_y(); inc_x(); break;
  }
  if (plotter.pen_down)
   plot(plotter.pos,plotter.pen);
 }
}

canonical_original_code:
void move_pen(int relative_dir, int length)
{
 int i;
 int absolute_dir = (8-2*get_dir()+relative_dir)%8;
 for (i=0;i<length;i++) {
  switch (absolute_dir) {
  case 1: dec_y();
  case 0: inc_x(); break;
  case 2: dec_y(); break;
  case 3:dec_y();
  case 4: dec_x(); break;
  case 5:dec_x();
  case 6: inc_y(); break;
  case 7:inc_y(); inc_x(); break;
  }
  if (plotter.pen_down)
   plot(plotter.pos,plotter.pen);
 }
}

variable_types: length int

variable_types: i int


decompiled_code:
void __fastcall draw_to(int a1, int a2)
{
int v2; // ebx
int v3; // esi
int v4; // er13
double v5; // xmm0_8
int v6; // er14
double v7; // xmm1_8
double v8; // xmm0_8
int v9; // er12
int v10; // er12
double v11; // xmm0_8
double v12; // [rsp+10h] [rbp-38h]
double v13; // [rsp+10h] [rbp-38h]

v2 = a1 - plotter;
v3 = a2 - HIDWORD(plotter);
if ( v3 | (a1 - (_DWORD)plotter) )
{
v4 = abs32(v2);
v5 = (double)v4;
v6 = abs32(v3);
v7 = (double)v6;
if ( v4 < v6 )
{
v10 = 0;
v13 = v5 / v7;
v11 = 0.0;
do
{
dot_count();
v11 = v11 + v13;
if ( v11 > 0.5 )
{
if ( v2 <= 0 )
{
if ( v3 <= 0 )
pen_step(3);
else
pen_step(5);
v11 = v11 - 1.0;
}
else
{
if ( v3 <= 0 )
pen_step(1);
else
pen_step(7);
v11 = v11 - 1.0;
}
}
else if ( v3 <= 0 )
{
pen_step(2);
}
else
{
pen_step(6);
}
++v10;
}
while ( v6 != v10 );
}
else
{
v12 = v7 / v5;
if ( v2 )
{
v8 = 0.0;
v9 = 0;
while ( 1 )
{
dot_count();
v8 = v8 + v12;
if ( v8 > 0.5 )
break;
if ( v2 <= 0 )
{
pen_step(4);
LABEL_9:
if ( v4 == ++v9 )
goto LABEL_13;
}
else
{
++v9;
pen_step(0);
if ( v4 == v9 )
goto LABEL_13;
}
}
if ( v3 <= 0 )
{
if ( v2 <= 0 )
pen_step(3);
else
pen_step(1);
v8 = v8 - 1.0;
}
else
{
if ( v2 <= 0 )
pen_step(5);
else
pen_step(7);
v8 = v8 - 1.0;
}
goto LABEL_9;
}
}
LABEL_13:
pen_up();
}
}

original_code:
void draw_to(int xdest, int ydest)
{
 int i;
 int dx=xdest-plotter.pos.x;
 int dy=ydest-plotter.pos.y;
 if (dx==0 && dy==0) return;
 if ( abs(dx) < abs(dy) ) {
  double frac=0.0;
  double step=((double)abs(dx))/((double)abs(dy));
  for (i=0;i<abs(dy);i++) {
   dot_count();
   frac+=step;
   if (frac>0.5) {
    frac-=1.0;
    if (dx>0) {
     if (dy>0) pen_step(7);
     else pen_step(1);
    } else {
     if (dy>0) pen_step(5);
     else pen_step(3);
    }
   } else {
    if (dy>0) pen_step(6);
    else pen_step(2);
   }
  }
 } else {
  double frac=0.0;
  double step=((double)abs(dy))/((double)abs(dx));
  for (i=0;i<abs(dx);i++) {
   dot_count();
   frac+=step;
   if (frac>0.5) {
    frac-=1.0;
    if (dy>0) {
     if (dx>0) pen_step(7);
     else pen_step(5);
    } else {
     if (dx>0) pen_step(1);
     else pen_step(3);
    }
   } else {
    if (dx>0) pen_step(0);
    else pen_step(4);
   }
  }
 }
 pen_up();
}

canonical_original_code:
void draw_to(int xdest, int ydest)
{
 int i;
 int dx = xdest-plotter.pos.x;
 int dy = ydest-plotter.pos.y;
 if (dx==0 && dy==0) return;
 if ( abs(dx) < abs(dy) ) {
  double frac = 0.0;
  double step = ((double)abs(dx))/((double)abs(dy));
  for (i=0;i<abs(dy);i++) {
   dot_count();
   frac+=step;
   if (frac>0.5) {
    frac-=1.0;
    if (dx>0) {
     if (dy>0) pen_step(7);
     else pen_step(1);
    } else {
     if (dy>0) pen_step(5);
     else pen_step(3);
    }
   } else {
    if (dy>0) pen_step(6);
    else pen_step(2);
   }
  }
 } else {
  double frac = 0.0;
  double step = ((double)abs(dy))/((double)abs(dx));
  for (i=0;i<abs(dx);i++) {
   dot_count();
   frac+=step;
   if (frac>0.5) {
    frac-=1.0;
    if (dy>0) {
     if (dx>0) pen_step(7);
     else pen_step(5);
    } else {
     if (dx>0) pen_step(1);
     else pen_step(3);
    }
   } else {
    if (dx>0) pen_step(0);
    else pen_step(4);
   }
  }
 }
 pen_up();
}

variable_types: xdest int

variable_types: ydest int

variable_types: dx int

variable_types: dy int

variable_types: frac double

variable_types: step double


decompiled_code:
__int64 init_plotter()
{
int v0; // ebx
__int64 result; // rax

dword_794 = 1;
HIDWORD(plotter) = 999;
HIDWORD(qword_79C) = 999;
v0 = 24;
do
{
result = pen_step(6);
--v0;
}
while ( v0 );
return result;
}

canonical_decompiled_code:
__int64 func27()
{
int v0; // ebx
__int64 result; // rax

dword_794 = 1;
HIDWORD(plotter) = 999;
HIDWORD(qword_79C) = 999;
v0 = 24;
do
{
result = func23(6);
--v0;
}
while ( v0 );
return result;
}

original_code:
void init_plotter()
{
 int i;
 plotter.char_size=1;
 plotter.origin.y = plotter.pos.y = 999;
 for (i=0;i<24;i++)
  pen_step(6);
}


repo: norisys/libqr

original_code:
static void render_line_1(unsigned char * out,
                          const unsigned char * in,
                          size_t mod_bits,
                          size_t dim,
                          unsigned long mark,
                          unsigned long space)
{
        unsigned char in_mask;
        size_t n, b;
        in_mask = 1;
        n = dim;
        while (n-- > 0) {
                unsigned long v = (*in & in_mask) ? mark : space;
                if ((in_mask <<= 1) == 0) {
                        in_mask = 1;
                        ++in;
                }
                b = mod_bits / 8;
                while (b-- > 0) {
                        *out++ = (unsigned char) v;
                        v >>= 8;
                }
        }
}

canonical_original_code:
static void render_line_1(unsigned char *out, unsigned char *in, unsigned long mod_bits, unsigned long dim, unsigned long mark, unsigned long space)
{
        unsigned char in_mask;
        unsigned long n; unsigned long b;
        in_mask = 1;
        n = dim;
        while (n-- > 0) {
                unsigned long v = (*in & in_mask) ? mark : space;
                if ((in_mask <<= 1) == 0) {
                        in_mask = 1;
                        ++in;
                }
                b = mod_bits / 8;
                while (b-- > 0) {
                        *out++ = (unsigned char) v;
                        v >>= 8;
                }
        }
}

variable_types:
out
unsigned char *

variable_types:
in
unsigned char *

variable_types:
dim
unsigned long

variable_types:
mark
unsigned long

variable_types:
space
unsigned long

variable_types:
n
unsigned long

variable_types:
b
unsigned long

variable_types:
v
unsigned long


decompiled_code:
void __fastcall qr_bitmap_destroy(void *ptr)
{
if ( ptr )
{
free(*(void **)ptr);
free(*((void **)ptr + 1));
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
if ( ptr )
{
free(*(void **)ptr);
free(*((void **)ptr + 1));
free(ptr);
}
}

original_code:
void qr_bitmap_destroy(struct qr_bitmap * bmp)
{
        if (bmp) {
                free(bmp->bits);
                free(bmp->mask);
                free(bmp);
        }
}

canonical_original_code:
void qr_bitmap_destroy(struct qr_bitmap *bmp)
{
        if (bmp) {
                free(bmp->bits);
                free(bmp->mask);
                free(bmp);
        }
}

variable_types: struct qr_bitmap { unsigned char *bits; unsigned char *mask; unsigned long stride; unsigned long width; } *


decompiled_code:
__int64 __fastcall qr_bitmap_add_mask(_QWORD *a1)
{
size_t v1; // r12
void *v2; // rax

v1 = a1[3] * a1[2];
v2 = malloc(v1);
a1[1] = v2;
if ( !v2 )
return 0xFFFFFFFFLL;
memset(v2, 255, v1);
return 0LL;
}

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

v1 = a1[3] * a1[2];
v2 = malloc(v1);
a1[1] = v2;
if ( !v2 )
return 0xFFFFFFFFLL;
memset(v2, 255, v1);
return 0LL;
}

original_code:
int qr_bitmap_add_mask(struct qr_bitmap * bmp)
{
        size_t size = bmp->stride * bmp->width;
        bmp->mask = malloc(size);
        if (!bmp->mask)
                return -1;
        memset(bmp->mask, 0xFF, size);
        return 0;
}

canonical_original_code:
int qr_bitmap_add_mask(struct qr_bitmap *bmp)
{
        unsigned long size = bmp->stride * bmp->width;
        bmp->mask = malloc(size);
        if (!bmp->mask)
                return -1;
        memset(bmp->mask, 0xFF, size);
        return 0;
}

variable_types:
size
unsigned long

user_defined_types: struct qr_bitmap { unsigned char *bits; unsigned char *mask; unsigned long stride; unsigned long width; }


decompiled_code:
void **__fastcall qr_bitmap_clone(__int64 a1)
{
void **v2; // rax
void **v3; // r12
void *v4; // rdx
size_t v5; // r13
void *v6; // rdi

v2 = (void **)qr_bitmap_create(*(_QWORD *)(a1 + 24), *(_QWORD *)(a1 + 32), *(_QWORD *)(a1 + 8) != 0LL);
v3 = v2;
if ( v2 )
{
v4 = v2[2];
if ( v4 != *(void **)(a1 + 16) )
_assert_fail("bmp->stride == src->stride", "bitmap.c", 0x47u, "qr_bitmap_clone");
v5 = (_QWORD)v2[3] * (_QWORD)v4;
memcpy(*v2, *(const void **)a1, v5);
v6 = v3[1];
if ( v6 )
memcpy(v6, *(const void **)(a1 + 8), v5);
}
return v3;
}

canonical_decompiled_code:
void **__fastcall func4(__int64 a1)
{
void **v2; // rax
void **v3; // r12
void *v4; // rdx
size_t v5; // r13
void *v6; // rdi

v2 = (void **)qr_bitmap_create(*(_QWORD *)(a1 + 24), *(_QWORD *)(a1 + 32), *(_QWORD *)(a1 + 8) != 0LL);
v3 = v2;
if ( v2 )
{
v4 = v2[2];
if ( v4 != *(void **)(a1 + 16) )
_assert_fail("bmp->stride == src->stride", "bitmap.c", 0x47u, "qr_bitmap_clone");
v5 = (_QWORD)v2[3] * (_QWORD)v4;
memcpy(*v2, *(const void **)a1, v5);
v6 = v3[1];
if ( v6 )
memcpy(v6, *(const void **)(a1 + 8), v5);
}
return v3;
}

original_code:
struct qr_bitmap * qr_bitmap_clone(const struct qr_bitmap * src)
{
        struct qr_bitmap * bmp;
        size_t size;
        bmp = qr_bitmap_create(src->width, src->height, !!src->mask);
        if (!bmp)
                return 0;
        ((bmp->stride == src->stride) ? (void) (0) : __assert_fail ("bmp->stride == src->stride", "bitmap.c", 71, __extension__ __PRETTY_FUNCTION__));
        size = bmp->width * bmp->stride;
        memcpy(bmp->bits, src->bits, size);
        if (bmp->mask)
                memcpy(bmp->mask, src->mask, size);
        return bmp;
}

canonical_original_code:
struct qr_bitmap *qr_bitmap_clone(struct qr_bitmap *src)
{
        struct qr_bitmap *bmp;
        unsigned long size;
        bmp = qr_bitmap_create(src->width, src->height, !!src->mask);
        if (!bmp)
                return 0;
        ((bmp->stride == src->stride) ? (void) (0) : __assert_fail ("bmp->stride == src->stride", "bitmap.c", 71, __extension__ __PRETTY_FUNCTION__));
        size = bmp->width * bmp->stride;
        memcpy(bmp->bits, src->bits, size);
        if (bmp->mask)
                memcpy(bmp->mask, src->mask, size);
        return bmp;
}

variable_types:
src
struct qr_bitmap *

return_type: struct qr_bitmap { unsigned char *bits; unsigned char *mask; unsigned long stride; unsigned long width; } *