decompiled_code:
__int64 __fastcall send_frame_msg_apply_prob_and_rate(__int64 a1, __int64 a2, __int64 a3, __int64 a4, unsigned int a5)
{
__int64 result; // rax
unsigned int v8; // eax
unsigned int v9; // edx
unsigned int v10; // ecx
double v11; // [rsp+8h] [rbp-30h]

v11 = find_prob_by_addrs_and_rate(prob_matrix, a1, a2, a5);
if ( v11 <= generate_random_double() )
{
puts("sent");
v8 = get_signal_by_rate(a5);
send_cloned_frame_msg(a2, a3, v9, v10, v8);
++sent;
result = 1LL;
}
else
{
puts("dropped");
++dropped;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, __int64 a3, __int64 a4, unsigned int a5)
{
__int64 result; // rax
unsigned int v8; // eax
unsigned int v9; // edx
unsigned int v10; // ecx
double v11; // [rsp+8h] [rbp-30h]

v11 = find_prob_by_addrs_and_rate(prob_matrix, a1, a2, a5);
if ( v11 <= func1() )
{
puts("sent");
v8 = func4(a5);
func3(a2, a3, v9, v10, v8);
++sent;
result = 1LL;
}
else
{
puts("dropped");
++dropped;
result = 0LL;
}
return result;
}

original_code:
int send_frame_msg_apply_prob_and_rate(struct mac_address *src,
           struct mac_address *dst,
           char *data, int data_len, int rate_idx)
{
 double prob_per_link = find_prob_by_addrs_and_rate(prob_matrix,
          src,dst, rate_idx);
 double random_double = generate_random_double();
 if (random_double < prob_per_link) {
  printf("dropped\n");
  dropped++;
  return 0;
 } else {
  printf("sent\n");
  int signal = get_signal_by_rate(rate_idx);
  send_cloned_frame_msg(dst,data,data_len,rate_idx,signal);
  sent++;
  return 1;
 }
}

variable_types:
dst
struct mac_address *


original_code:
int jam_mac(struct jammer_cfg *jcfg, struct mac_address *src)
{
 int jam = 0, i;
 for (i = 0; i < jcfg->nmacs; i++) {
  if (!memcmp(&jcfg->macs[i], src, sizeof(struct mac_address))) {
   jam = 1;
   break;
  }
 }
 return jam;
}

canonical_original_code:
int jam_mac(struct jammer_cfg *jcfg, struct mac_address *src)
{
 int jam = 0; int i;
 for (i = 0; i < jcfg->nmacs; i++) {
  if (!memcmp(&jcfg->macs[i], src, sizeof(struct mac_address))) {
   jam = 1;
   break;
  }
 }
 return jam;
}

variable_types: struct jammer_cfg { int jam_all; struct mac_address { unsigned char addr[6]; } *macs; int nmacs; } *

variable_types:
src
struct mac_address *

variable_types: jam int

variable_types: i int

return_type: int

user_defined_types: struct jammer_cfg { int jam_all; struct mac_address *macs; int nmacs; }


decompiled_code:
int __fastcall send_frames_to_radios_with_retries(__int64 a1, __int64 a2, unsigned int a3, unsigned int a4, __int64 a5, __int64 a6)
{
char *v8; // rbx
int v9; // edx
char v10; // al
int v11; // er14
int v12; // eax
__int64 v13; // r15
int v14; // eax
int v15; // eax
unsigned int v16; // eax
__int64 v17; // rcx
int v18; // [rsp+8h] [rbp-70h]
int v19; // [rsp+Ch] [rbp-6Ch]
__int64 v20; // [rsp+10h] [rbp-68h]
int v22; // [rsp+20h] [rbp-58h]
char v25[66]; // [rsp+36h] [rbp-42h] BYREF

if ( jam_cfg )
return puts("medium busy!");
v22 = jam_mac((__int64)&jam_cfg, a1);
if ( v22 )
return puts("medium busy!");
v8 = v25;
set_all_rates_invalid(v25);
v20 = 0LL;
while ( 1 )
{
v9 = v20;
v10 = *(_BYTE *)(a5 + 2 * v20);
if ( v10 == -1 )
break;
if ( v22 == 1 )
goto LABEL_29;
*v8 = v10;
v19 = 1;
if ( *(_BYTE *)(a5 + 2 * v20 + 1) )
{
while ( 1 )
{
while ( 1 )
{
v18 = v22;
v11 = v22;
if ( size > 0 )
break;
v15 = *(unsigned __int8 *)(a5 + 2 * v20 + 1);
v8[1] = v19++;
if ( v15 < v19 )
goto LABEL_22;
}
do
{
while ( 1 )
{
v13 = get_mac_address((unsigned int)v11);
if ( *(_DWORD *)a1 != *(_DWORD *)v13 || *(_WORD *)(a1 + 4) != *(_WORD *)(v13 + 4) )
break;
if ( size <= ++v11 )
goto LABEL_19;
}
if ( (unsigned int)send_frame_msg_apply_prob_and_rate(a1, v13, a2, a3, *v8)
&& *(_DWORD *)v13 == *(_DWORD *)(a2 + 4) )
{
v12 = 1;
if ( *(_WORD *)(v13 + 4) != *(_WORD *)(a2 + 8) )
v12 = v18;
v18 = v12;
}
++v11;
}
while ( size > v11 );
LABEL_19:
v14 = *(unsigned __int8 *)(a5 + 2 * v20 + 1);
v8[1] = v19++;
if ( v14 < v19 )
break;
if ( v18 == 1 )
{
v22 = 1;
goto LABEL_22;
}
}
v22 = v18;
}
LABEL_22:
++v20;
v8 += 2;
if ( v20 == 5 )
{
v9 = 5;
break;
}
}
if ( !v22 )
return send_tx_info_frame_nl(a1, a4, 0, (__int64)v25, a6);
LABEL_29:
++acked;
v16 = get_signal_by_rate(v25[2 * v9 - 2]);
return send_tx_info_frame_nl(a1, a4 | 4, v16, v17, a6);
}

original_code:
void send_frames_to_radios_with_retries(struct mac_address *src, char*data,
     int data_len, unsigned int flags,
     struct hwsim_tx_rate *tx_rates,
     unsigned long cookie)
{
 struct mac_address *dst;
 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)data;
 struct hwsim_tx_rate tx_attempts[5];
 int round = 0, tx_ok = 0, counter, i;
 if (jam_cfg.jam_all || jam_mac(&jam_cfg, src)) {
  printf("medium busy!\n");
  return;
 }
 set_all_rates_invalid(tx_attempts);
 while (round < 5 &&
        tx_rates[round].idx != -1 && tx_ok!=1) {
  counter = 1;
  tx_attempts[round].idx = tx_rates[round].idx;
  while(counter <= tx_rates[round].count && tx_ok !=1 ) {
   for (i=0;i<size;i++) {
    dst = get_mac_address(i);
    if(memcmp(src,dst,sizeof(struct mac_address))
       == 0 ){
     continue;
    }
    if(send_frame_msg_apply_prob_and_rate(
     src, dst, data, data_len,
     tx_attempts[round].idx) &&
     memcmp(dst, hdr->addr1,
     sizeof(struct mac_address))==0) {
      tx_ok = 1;
    }
   }
   tx_attempts[round].count = counter;
   counter++;
  }
  round++;
 }
 if (tx_ok) {
  acked++;
  int signal = get_signal_by_rate(tx_attempts[round-1].idx);
  flags |= (1 << 2);
  send_tx_info_frame_nl(src, flags, signal, tx_attempts, cookie);
 } else {
  send_tx_info_frame_nl(src, flags, 0, tx_attempts, cookie);
 }
}

canonical_original_code:
void send_frames_to_radios_with_retries(struct mac_address *src, char *data, int data_len, unsigned int flags, struct hwsim_tx_rate *tx_rates, unsigned long cookie)
{
 struct mac_address *dst;
 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)data;
 struct hwsim_tx_rate tx_attempts[5];
 int round = 0; int tx_ok = 0; int counter; int i;
 if (jam_cfg.jam_all || jam_mac(&jam_cfg, src)) {
  printf("medium busy!\n");
  return;
 }
 set_all_rates_invalid(tx_attempts);
 while (round < 5 &&
        tx_rates[round].idx != -1 && tx_ok!=1) {
  counter = 1;
  tx_attempts[round].idx = tx_rates[round].idx;
  while(counter <= tx_rates[round].count && tx_ok !=1 ) {
   for (i=0;i<size;i++) {
    dst = get_mac_address(i);
    if(memcmp(src,dst,sizeof(struct mac_address))
       == 0 ){
     continue;
    }
    if(send_frame_msg_apply_prob_and_rate(
     src, dst, data, data_len,
     tx_attempts[round].idx) &&
     memcmp(dst, hdr->addr1,
     sizeof(struct mac_address))==0) {
      tx_ok = 1;
    }
   }
   tx_attempts[round].count = counter;
   counter++;
  }
  round++;
 }
 if (tx_ok) {
  acked++;
  int signal = get_signal_by_rate(tx_attempts[round-1].idx);
  flags |= (1 << 2);
  send_tx_info_frame_nl(src, flags, signal, tx_attempts, cookie);
 } else {
  send_tx_info_frame_nl(src, flags, 0, tx_attempts, cookie);
 }
}

variable_types: struct mac_address { unsigned char addr[6]; } *

variable_types:
data
char *

variable_types:
flags
unsigned int

variable_types: struct hwsim_tx_rate { char idx; unsigned char count; } *

variable_types:
cookie
unsigned long

variable_types: struct ieee80211_hdr { unsigned char frame_control[2]; unsigned char duration_id[2]; unsigned char addr1[6]; unsigned char addr2[6]; unsigned char addr3[6]; unsigned char seq_ctrl[2]; unsigned char addr4[6]; } *

variable_types: struct hwsim_tx_rate { char idx; unsigned char count; } [5]

variable_types: round int

variable_types: counter int

variable_types: signal int

return_type: void


decompiled_code:
__int64 send_register_msg()
{
unsigned int v0; // eax
__int64 v1; // rdx
__int64 result; // rax

msg = (void *)nlmsg_alloc();
if ( msg )
{
v0 = genl_family_get_id(family);
genlmsg_put(msg, 0LL, 0LL, v0, 0LL, 1LL, 1LL, 1LL);
nl_send_auto_complete(sock, msg, v1);
nlmsg_free(msg);
result = 0LL;
}
else
{
puts("Error allocating new message MSG!");
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 func9()
{
unsigned int v0; // eax
__int64 v1; // rdx
__int64 result; // rax

msg = (void *)nlmsg_alloc();
if ( msg )
{
v0 = genl_family_get_id(family);
genlmsg_put(msg, 0LL, 0LL, v0, 0LL, 1LL, 1LL, 1LL);
nl_send_auto_complete(sock, msg, v1);
nlmsg_free(msg);
result = 0LL;
}
else
{
puts("Error allocating new message MSG!");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int send_register_msg()
{
 msg = nlmsg_alloc();
 if (!msg) {
  printf("Error allocating new message MSG!\n");
  return -1;
 }
 genlmsg_put(msg, 0, 0, genl_family_get_id(family),
      0, 0x01, 1, 1);
 nl_send_auto_complete(sock,msg);
 nlmsg_free(msg);
 return 0;
}


decompiled_code:
__int64 init_netlink()
{
void *v0; // rax

v0 = (void *)nl_cb_alloc(3LL);
cb = v0;
if ( !v0 )
{
puts("Error allocating netlink callbacks");
exit(1);
}
sock = (void *)nl_socket_alloc_cb(v0);
if ( !sock )
{
puts("Error allocationg netlink socket");
exit(1);
}
genl_connect(sock);
genl_ctrl_alloc_cache(sock, &cache);
family = (void *)genl_ctrl_search_by_name(cache, "MAC80211_HWSIM");
if ( !family )
{
puts("Family MAC80211_HWSIM not registered");
exit(1);
}
return nl_cb_set(cb, 5LL, 3LL, process_messages_cb, 0LL);
}

canonical_decompiled_code:
__int64 func10()
{
void *v0; // rax

v0 = (void *)nl_cb_alloc(3LL);
cb = v0;
if ( !v0 )
{
puts("Error allocating netlink callbacks");
exit(1);
}
sock = (void *)nl_socket_alloc_cb(v0);
if ( !sock )
{
puts("Error allocationg netlink socket");
exit(1);
}
genl_connect(sock);
genl_ctrl_alloc_cache(sock, &cache);
family = (void *)genl_ctrl_search_by_name(cache, "MAC80211_HWSIM");
if ( !family )
{
puts("Family MAC80211_HWSIM not registered");
exit(1);
}
return nl_cb_set(cb, 5LL, 3LL, process_messages_cb, 0LL);
}

original_code:
void init_netlink()
{
 cb = nl_cb_alloc(NL_CB_CUSTOM);
 if (!cb) {
  printf("Error allocating netlink callbacks\n");
  exit(1);
 }
 sock = nl_socket_alloc_cb(cb);
 if (!sock) {
  printf("Error allocationg netlink socket\n");
  exit(1);
 }
 genl_connect(sock);
 genl_ctrl_alloc_cache(sock, &cache);
 family = genl_ctrl_search_by_name(cache, "MAC80211_HWSIM");
 if (!family) {
  printf("Family MAC80211_HWSIM not registered\n");
  exit(1);
 }
 nl_cb_set(cb, NL_CB_MSG_IN, NL_CB_CUSTOM, process_messages_cb, ((void *)0));
}


decompiled_code:
void __fastcall __noreturn print_help(int status)
{
printf("wmediumd v%s - a wireless medium simulator\n", "0.1");
puts("wmediumd [-h] [-V] [-c FILE] [-o FILE]\n");
puts(" -h print this help and exit");
puts(" -V print version and exit\n");
puts(" -c FILE set intput config file");
puts(" -o FILE set output config file\n");
exit(status);
}

canonical_decompiled_code:
void __fastcall __noreturn func11(int status)
{
printf("wmediumd v%s - a wireless medium simulator\n", "0.1");
puts("wmediumd [-h] [-V] [-c FILE] [-o FILE]\n");
puts(" -h print this help and exit");
puts(" -V print version and exit\n");
puts(" -c FILE set intput config file");
puts(" -o FILE set output config file\n");
exit(status);
}

original_code:
void print_help(int exval)
{
 printf("wmediumd v%s - a wireless medium simulator\n", "0.1");
 printf("wmediumd [-h] [-V] [-c FILE] [-o FILE]\n\n");
 printf("  -h              print this help and exit\n");
 printf("  -V              print version and exit\n\n");
 printf("  -c FILE         set intput config file\n");
 printf("  -o FILE         set output config file\n\n");
 exit(exval);
}

variable_types: exval int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v5[7]; // [rsp+Ch] [rbp-1Ch] BYREF

setvbuf(stdout, 0LL, 1, 0x2000uLL);
if ( argc == 1 )
{
fwrite("This program needs arguments....\n\n", 1uLL, 0x22uLL, stderr);
print_help(1);
}
while ( 1 )
{
v3 = getopt(argc, (char *const *)argv, "hVc:o:");
if ( v3 == -1 )
break;
if ( v3 == 99 )
{
printf("Input configuration file: %s\n", optarg);
load_config(optarg);
}
else if ( v3 > 99 )
{
if ( v3 == 104 )
print_help(0);
if ( v3 == 111 )
{
printf("Output configuration file: %s\n", optarg);
puts("How many interfaces are active?");
_isoc99_scanf("%d", v5);
if ( v5[0] <= 1 )
{
puts("active interfaces must be at least 2");
exit(1);
}
write_config(optarg, 0.0);
}
}
else
{
if ( v3 == 86 )
{
printf("wmediumd v%s - a wireless medium simulator for mac80211_hwsim\n", "0.1");
exit(0);
}
if ( v3 <= 86 )
{
if ( v3 == 58 )
{
printf("wmediumd: Error - Option `%c' needs a value\n\n", (unsigned int)optopt);
print_help(1);
}
if ( v3 == 63 )
{
printf("wmediumd: Error - No such option: `%c'\n\n", (unsigned int)optopt);
print_help(1);
}
}
}
}
if ( optind < argc )
print_help(1);
print_prob_matrix(prob_matrix);
running = 1;
signal(10, (__sighandler_t)kill_handler);
init_netlink();
if ( !(unsigned int)send_register_msg() )
puts("REGISTER SENT!");
while ( running )
nl_recvmsgs_default(sock);
free(sock);
free(msg);
free(cb);
free(cache);
free(family);
free(prob_matrix);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v5[7]; // [rsp+Ch] [rbp-1Ch] BYREF

setvbuf(stdout, 0LL, 1, 0x2000uLL);
if ( argc == 1 )
{
fwrite("This program needs arguments....\n\n", 1uLL, 0x22uLL, stderr);
func11(1);
}
while ( 1 )
{
v3 = getopt(argc, (char *const *)argv, "hVc:o:");
if ( v3 == -1 )
break;
if ( v3 == 99 )
{
printf("Input configuration file: %s\n", optarg);
load_config(optarg);
}
else if ( v3 > 99 )
{
if ( v3 == 104 )
func11(0);
if ( v3 == 111 )
{
printf("Output configuration file: %s\n", optarg);
puts("How many interfaces are active?");
_isoc99_scanf("%d", v5);
if ( v5[0] <= 1 )
{
puts("active interfaces must be at least 2");
exit(1);
}
write_config(optarg, 0.0);
}
}
else
{
if ( v3 == 86 )
{
printf("wmediumd v%s - a wireless medium simulator for mac80211_hwsim\n", "0.1");
exit(0);
}
if ( v3 <= 86 )
{
if ( v3 == 58 )
{
printf("wmediumd: Error - Option `%c' needs a value\n\n", (unsigned int)optopt);
func11(1);
}
if ( v3 == 63 )
{
printf("wmediumd: Error - No such option: `%c'\n\n", (unsigned int)optopt);
func11(1);
}
}
}
}
if ( optind < argc )
func11(1);
print_prob_matrix(prob_matrix);
running = 1;
signal(10, (__sighandler_t)kill_handler);
func10();
if ( !(unsigned int)func9() )
puts("REGISTER SENT!");
while ( running )
nl_recvmsgs_default(sock);
free(sock);
free(msg);
free(cb);
free(cache);
free(family);
free(prob_matrix);
return 0;
}

original_code:
int main(int argc, char* argv[]) {
 int opt, ifaces;
 setvbuf (stdout, ((void *)0), 1, 8192);
 if(argc == 1) {
  fprintf(stderr, "This program needs arguments....\n\n");
  print_help(1);
 }
 while((opt = getopt(argc, argv, "hVc:o:")) != -1) {
  switch(opt) {
  case 'h':
   print_help(0);
       break;
  case 'V':
   printf("wmediumd v%s - a wireless medium simulator "
          "for mac80211_hwsim\n", "0.1");
   exit(0);
   break;
  case 'c':
   printf("Input configuration file: %s\n", optarg);
   load_config(optarg);
   break;
  case 'o':
   printf("Output configuration file: %s\n", optarg);
   printf("How many interfaces are active?\n");
   scanf("%d",&ifaces);
   if (ifaces < 2) {
    printf("active interfaces must be at least 2\n");
    exit(1);
   }
    write_config(optarg, ifaces, 0.0);
   break;
  case ':':
   printf("wmediumd: Error - Option `%c' "
          "needs a value\n\n", optopt);
   print_help(1);
   break;
  case '?':
   printf("wmediumd: Error - No such option:"
          " `%c'\n\n", optopt);
   print_help(1);
   break;
  }
 }
 if (optind < argc)
  print_help(1);
 print_prob_matrix(prob_matrix);
 running = 1;
 signal(10, kill_handler);
 init_netlink();
 if (send_register_msg()==0)
  printf("REGISTER SENT!\n");
 while(running) {
  nl_recvmsgs_default(sock);
 }
 free(sock);
 free(msg);
 free(cb);
 free(cache);
 free(family);
 free(prob_matrix);
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
 int opt; int ifaces;
 setvbuf (stdout, ((void *)0), 1, 8192);
 if(argc == 1) {
  fprintf(stderr, "This program needs arguments....\n\n");
  print_help(1);
 }
 while((opt = getopt(argc, argv, "hVc:o:")) != -1) {
  switch(opt) {
  case 'h':
   print_help(0);
       break;
  case 'V':
   printf("wmediumd v%s - a wireless medium simulator "
          "for mac80211_hwsim\n", "0.1");
   exit(0);
   break;
  case 'c':
   printf("Input configuration file: %s\n", optarg);
   load_config(optarg);
   break;
  case 'o':
   printf("Output configuration file: %s\n", optarg);
   printf("How many interfaces are active?\n");
   scanf("%d",&ifaces);
   if (ifaces < 2) {
    printf("active interfaces must be at least 2\n");
    exit(1);
   }
    write_config(optarg, ifaces, 0.0);
   break;
  case ':':
   printf("wmediumd: Error - Option `%c' "
          "needs a value\n\n", optopt);
   print_help(1);
   break;
  case '?':
   printf("wmediumd: Error - No such option:"
          " `%c'\n\n", optopt);
   print_help(1);
   break;
  }
 }
 if (optind < argc)
  print_help(1);
 print_prob_matrix(prob_matrix);
 running = 1;
 signal(10, kill_handler);
 init_netlink();
 if (send_register_msg()==0)
  printf("REGISTER SENT!\n");
 while(running) {
  nl_recvmsgs_default(sock);
 }
 free(sock);
 free(msg);
 free(cb);
 free(cache);
 free(family);
 free(prob_matrix);
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: opt int

variable_types: ifaces int


decompiled_code:
unsigned __int64 __fastcall string_to_mac_address(__int64 a1)
{
unsigned int v2; // [rsp+Ah] [rbp-2Eh]
unsigned __int16 v3; // [rsp+Eh] [rbp-2Ah]
int v4; // [rsp+10h] [rbp-28h] BYREF
int v5; // [rsp+14h] [rbp-24h] BYREF
int v6; // [rsp+18h] [rbp-20h] BYREF
int v7; // [rsp+1Ch] [rbp-1Ch] BYREF
int v8; // [rsp+20h] [rbp-18h] BYREF
int v9; // [rsp+24h] [rbp-14h] BYREF

_isoc99_sscanf(a1, "%x:%x:%x:%x:%x:%x", &v4, &v5, &v6, &v7, &v8, &v9);
LOBYTE(v2) = v4;
BYTE1(v2) = v5;
BYTE2(v2) = v6;
HIBYTE(v2) = v7;
LOBYTE(v3) = v8;
HIBYTE(v3) = v9;
return ((unsigned __int64)v3 << 32) | v2;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func0(__int64 a1)
{
unsigned int v2; // [rsp+Ah] [rbp-2Eh]
unsigned __int16 v3; // [rsp+Eh] [rbp-2Ah]
int v4; // [rsp+10h] [rbp-28h] BYREF
int v5; // [rsp+14h] [rbp-24h] BYREF
int v6; // [rsp+18h] [rbp-20h] BYREF
int v7; // [rsp+1Ch] [rbp-1Ch] BYREF
int v8; // [rsp+20h] [rbp-18h] BYREF
int v9; // [rsp+24h] [rbp-14h] BYREF

_isoc99_sscanf(a1, "%x:%x:%x:%x:%x:%x", &v4, &v5, &v6, &v7, &v8, &v9);
LOBYTE(v2) = v4;
BYTE1(v2) = v5;
BYTE2(v2) = v6;
HIBYTE(v2) = v7;
LOBYTE(v3) = v8;
HIBYTE(v3) = v9;
return ((unsigned __int64)v3 << 32) | v2;
}

original_code:
struct mac_address string_to_mac_address(const char* str)
{
 struct mac_address mac;
 int a[6];
 sscanf(str, "%x:%x:%x:%x:%x:%x",
        &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]);
 mac.addr[0] = a[0];
 mac.addr[1] = a[1];
 mac.addr[2] = a[2];
 mac.addr[3] = a[3];
 mac.addr[4] = a[4];
 mac.addr[5] = a[5];
 return mac;
}

canonical_original_code:
struct mac_address string_to_mac_address(char *str)
{
 struct mac_address mac;
 int a[6];
 sscanf(str, "%x:%x:%x:%x:%x:%x",
        &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]);
 mac.addr[0] = a[0];
 mac.addr[1] = a[1];
 mac.addr[2] = a[2];
 mac.addr[3] = a[3];
 mac.addr[4] = a[4];
 mac.addr[5] = a[5];
 return mac;
}

variable_types:
str
char *

variable_types: struct mac_address { unsigned char addr[6]; }

variable_types:
a
int [6]


decompiled_code:
char *__fastcall str_replace(char *src, char *needle, char *s)
{
char *v3; // r14
char *v4; // r13
__int64 v5; // rbp
size_t v7; // r15
size_t v8; // r12
char *v9; // rax
char *v10; // rax
char *i; // rbp
char *v12; // rsi
char *v13; // rdi
size_t v14; // r14
char *v15; // rbp
char *v16; // rax
size_t v18; // [rsp+0h] [rbp-58h]
char *v20; // [rsp+18h] [rbp-40h]

v3 = s;
v4 = src;
v5 = 0LL;
v7 = strlen(src);
v8 = strlen(needle);
v18 = strlen(v3);
while ( 1 )
{
v9 = strstr(src, needle);
if ( !v9 )
break;
++v5;
src = &v9[v8];
}
v10 = (char *)malloc(v7 + (v18 - v8) * v5 + 1);
v20 = v10;
if ( v10 )
{
for ( i = v10; ; i = &v15[v18] )
{
v16 = strstr(v4, needle);
if ( !v16 )
break;
v12 = v4;
v13 = i;
v14 = v16 - v4;
v4 = &v16[v8];
v15 = &i[v14];
memcpy(v13, v12, v14);
memcpy(v15, s, v18 + 1);
}
strcpy(i, v4);
}
return v20;
}

canonical_decompiled_code:
char *__fastcall func0(char *src, char *needle, char *s)
{
char *v3; // r14
char *v4; // r13
__int64 v5; // rbp
size_t v7; // r15
size_t v8; // r12
char *v9; // rax
char *v10; // rax
char *i; // rbp
char *v12; // rsi
char *v13; // rdi
size_t v14; // r14
char *v15; // rbp
char *v16; // rax
size_t v18; // [rsp+0h] [rbp-58h]
char *v20; // [rsp+18h] [rbp-40h]

v3 = s;
v4 = src;
v5 = 0LL;
v7 = strlen(src);
v8 = strlen(needle);
v18 = strlen(v3);
while ( 1 )
{
v9 = strstr(src, needle);
if ( !v9 )
break;
++v5;
src = &v9[v8];
}
v10 = (char *)malloc(v7 + (v18 - v8) * v5 + 1);
v20 = v10;
if ( v10 )
{
for ( i = v10; ; i = &v15[v18] )
{
v16 = strstr(v4, needle);
if ( !v16 )
break;
v12 = v4;
v13 = i;
v14 = v16 - v4;
v4 = &v16[v8];
v15 = &i[v14];
memcpy(v13, v12, v14);
memcpy(v15, s, v18 + 1);
}
strcpy(i, v4);
}
return v20;
}

original_code:
char *str_replace(const char *str, const char *old, const char *new)
{
 char *ret, *r;
 const char *p, *q;
 size_t len_str = strlen(str);
 size_t len_old = strlen(old);
 size_t len_new = strlen(new);
 size_t count;
 for(count = 0, p = str; (p = strstr(p, old)); p += len_old)
  count++;
 ret = malloc(count * (len_new - len_old) + len_str + 1);
 if(!ret)
  return ((void *)0);
 for(r = ret, p = str; (q = strstr(p, old)); p = q + len_old) {
  count = q - p;
  memcpy(r, p, count);
  r += count;
  strcpy(r, new);
  r += len_new;
 }
 strcpy(r, p);
 return ret;
}

canonical_original_code:
char *str_replace(char *str, char *old, char *new)
{
 char *ret; char *r;
 char *p; char *q;
 unsigned long len_str = strlen(str);
 unsigned long len_old = strlen(old);
 unsigned long len_new = strlen(new);
 unsigned long count;
 for(count = 0, p = str; (p = strstr(p, old)); p += len_old)
  count++;
 ret = malloc(count * (len_new - len_old) + len_str + 1);
 if(!ret)
  return ((void *)0);
 for(r = ret, p = str; (q = strstr(p, old)); p = q + len_old) {
  count = q - p;
  memcpy(r, p, count);
  r += count;
  strcpy(r, new);
  r += len_new;
 }
 strcpy(r, p);
 return ret;
}

variable_types:
old
char *

variable_types:
new
char *

variable_types:
ret
char *

variable_types:
r
char *

variable_types:
p
char *

variable_types:
q
char *

variable_types:
count
unsigned long

return_type: char *


usage

repo: jlusticky/Bachelor-thesis

decompiled_code:
void __noreturn usage()
{
fprintf(
stderr,
"%s\n%s\n%s\n%s\n%s\n%s\n",
"Usage: sntp [-p] [-i n] [-b | -c | -s] address",
"\t-p\tuse privileged NTP port",
"\t-i n\tcontinue sending NTP messsage every i seconds",
"\t-b\tbroadcast NTP message to address",
"\t-c\tclient NTP message to address (default)",
"\t-s\tserver NTP message to address");
exit(1);
}

canonical_decompiled_code:
void __noreturn func9()
{
fprintf(
stderr,
"%s\n%s\n%s\n%s\n%s\n%s\n",
"Usage: sntp [-p] [-i n] [-b | -c | -s] address",
"\t-p\tuse privileged NTP port",
"\t-i n\tcontinue sending NTP messsage every i seconds",
"\t-b\tbroadcast NTP message to address",
"\t-c\tclient NTP message to address (default)",
"\t-s\tserver NTP message to address");
exit(1);
}

original_code:
void
usage(void)
{
 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n",
  "Usage: sntp [-p] [-i n] [-b | -c | -s] address",
  "\t-p\tuse privileged NTP port",
  "\t-i n\tcontinue sending NTP messsage every i seconds",
  "\t-b\tbroadcast NTP message to address",
  "\t-c\tclient NTP message to address (default)",
  "\t-s\tserver NTP message to address");
 exit(1);
}

canonical_original_code:
void usage()
{
 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n",
  "Usage: sntp [-p] [-i n] [-b | -c | -s] address",
  "\t-p\tuse privileged NTP port",
  "\t-i n\tcontinue sending NTP messsage every i seconds",
  "\t-b\tbroadcast NTP message to address",
  "\t-c\tclient NTP message to address (default)",
  "\t-s\tserver NTP message to address");
 exit(1);
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // er12
const char *v5; // rbx
int v6; // er13
int v7; // ebp
__int16 v8; // ax
__int16 v9; // r14
socklen_t v10; // er9
const struct sockaddr *v11; // r8
unsigned int v12; // edi
__time_t v13; // rsi
__int64 v14; // rdx
__int64 v15; // rcx
__int64 v16; // rdx
const char *v17; // rcx
__int64 v18; // rdx
__int64 v19; // rcx
unsigned int v20; // eax
__int64 v21; // rdx
unsigned int v22; // eax
int v23; // ecx
double v24; // xmm0_8
unsigned __int64 v25; // rcx
__int64 v27; // rcx
__m128i v28; // xmm2
_DWORD *v29; // rdi
struct sockaddr buf; // [rsp+0h] [rbp-A8h] BYREF
struct timespec tp; // [rsp+10h] [rbp-98h] BYREF
_BYTE addr[24]; // [rsp+20h] [rbp-88h] BYREF
int v33; // [rsp+38h] [rbp-70h]
char v34[40]; // [rsp+40h] [rbp-68h] BYREF
unsigned int v35; // [rsp+68h] [rbp-40h]
unsigned int v36; // [rsp+6Ch] [rbp-3Ch]

while ( 2 )
{
v3 = getopt(argc, (char *const *)argv, "bci:ps");
if ( v3 != -1 )
{
switch ( v3 )
{
case 'b':
bflag = 1;
continue;
case 'c':
cflag = 1;
continue;
case 'i':
ival = strtol(optarg, 0LL, 10);
continue;
case 'p':
pflag = 1;
continue;
case 's':
sflag = 1;
continue;
default:
goto LABEL_39;
}
}
break;
}
v4 = argc - _bss_start;
if ( argc - _bss_start != 1 )
LABEL_39:
usage();
v5 = argv[_bss_start];
if ( bflag )
ntpmsg = -27;
else
ntpmsg = -29 - ((sflag == 0) - 1);
v6 = strchr(v5, 58) == 0LL ? 2 : 10;
v7 = socket(v6, 2, 0);
if ( v7 == -1 )
{
perror("socket");
}
else
{
v8 = _bswap_16_constprop_0();
v9 = v8;
if ( !pflag )
goto LABEL_21;
if ( v6 == 10 )
{
v27 = 7LL;
v28 = _mm_loadu_si128((const __m128i *)&in6addr_any);
v29 = addr;
while ( v27 )
{
*v29++ = 0;
--v27;
}
*(_WORD *)addr = 10;
*(_WORD *)&addr[2] = v8;
*(__m128i *)&addr[8] = v28;
if ( bind(v7, (const struct sockaddr *)addr, 0x1Cu) != -1 )
goto LABEL_21;
}
else
{
buf.sa_family = 2;
*(_WORD *)buf.sa_data = v8;
*(_DWORD *)&buf.sa_data[2] = 0;
if ( bind(v7, &buf, 0x10u) != -1 )
{
while ( 1 )
{
LABEL_21:
clock_gettime(0, &tp);
v13 = tp.tv_sec;
dword_404108 = _bswap_32((unsigned int)(LODWORD(tp.tv_sec) - 2085978496), tp.tv_sec, v14, v5, 0.0);
dword_40410C = _bswap_32(
(unsigned int)(int)((double)SLODWORD(tp.tv_nsec) * 4294967296.0 / 1000000000.0),
v13,
tp.tv_nsec,
v15,
(double)SLODWORD(tp.tv_nsec) * 4294967296.0 / 1000000000.0);
printf("Sending time %ld sec %ld nsec to %s\n", v13, v16, v17);
if ( v6 == 10 )
{
*(_OWORD *)addr = 0LL;
*(_WORD *)addr = 10;
*(_QWORD *)&addr[16] = 0LL;
v33 = 0;
*(_WORD *)&addr[2] = v9;
inet_pton(10, v5, &addr[8]);
v10 = 28;
v11 = (const struct sockaddr *)addr;
}
else
{
buf = 0LL;
buf.sa_family = 2;
*(_WORD *)buf.sa_data = v9;
inet_pton(2, v5, &buf.sa_data[2]);
v10 = 16;
v11 = &buf;
}
if ( sendto(v7, &ntpmsg, 0x30uLL, 0, v11, v10) == -1 )
{
perror("sendto");
close(v7);
return v4;
}
if ( (ntpmsg & 7) == 3 )
{
if ( recv(v7, v34, 0x30uLL, 0) == -1 )
{
perror("recv");
close(v7);
return v4;
}
v20 = _bswap_32(v36, v34, v18, v19, 0.0);
v22 = _bswap_32(v35, v34, v21, v20, 0.0);
v24 = (double)v23 * 1000000000.0 * 2.328306436538696e-10;
if ( v24 >= 9.223372036854776e18 )
v25 = (unsigned int)(int)(v24 - 9.223372036854776e18) ^ 0x8000000000000000LL;
else
v25 = (unsigned int)(int)v24;
printf("NTP time got from %s: %lu sec %lu nsec\n", v5, v22 - 2208988800LL, v25);
v12 = ival;
if ( !ival )
return 0;
}
else
{
v12 = ival;
if ( !ival )
return 0;
}
sleep(v12);
}
}
}
perror("bind");
close(v7);
}
return v4;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // er12
const char *v5; // rbx
int v6; // er13
int v7; // ebp
__int16 v8; // ax
__int16 v9; // r14
socklen_t v10; // er9
const struct sockaddr *v11; // r8
unsigned int v12; // edi
__time_t v13; // rsi
__int64 v14; // rdx
__int64 v15; // rcx
__int64 v16; // rdx
const char *v17; // rcx
__int64 v18; // rdx
__int64 v19; // rcx
unsigned int v20; // eax
__int64 v21; // rdx
unsigned int v22; // eax
int v23; // ecx
double v24; // xmm0_8
unsigned __int64 v25; // rcx
__int64 v27; // rcx
__m128i v28; // xmm2
_DWORD *v29; // rdi
struct sockaddr buf; // [rsp+0h] [rbp-A8h] BYREF
struct timespec tp; // [rsp+10h] [rbp-98h] BYREF
_BYTE addr[24]; // [rsp+20h] [rbp-88h] BYREF
int v33; // [rsp+38h] [rbp-70h]
char v34[40]; // [rsp+40h] [rbp-68h] BYREF
unsigned int v35; // [rsp+68h] [rbp-40h]
unsigned int v36; // [rsp+6Ch] [rbp-3Ch]

while ( 2 )
{
v3 = getopt(argc, (char *const *)argv, "bci:ps");
if ( v3 != -1 )
{
switch ( v3 )
{
case 'b':
bflag = 1;
continue;
case 'c':
cflag = 1;
continue;
case 'i':
ival = strtol(optarg, 0LL, 10);
continue;
case 'p':
pflag = 1;
continue;
case 's':
sflag = 1;
continue;
default:
goto LABEL_39;
}
}
break;
}
v4 = argc - _bss_start;
if ( argc - _bss_start != 1 )
LABEL_39:
func9();
v5 = argv[_bss_start];
if ( bflag )
ntpmsg = -27;
else
ntpmsg = -29 - ((sflag == 0) - 1);
v6 = strchr(v5, 58) == 0LL ? 2 : 10;
v7 = socket(v6, 2, 0);
if ( v7 == -1 )
{
perror("socket");
}
else
{
v8 = func8();
v9 = v8;
if ( !pflag )
goto LABEL_21;
if ( v6 == 10 )
{
v27 = 7LL;
v28 = _mm_loadu_si128((const __m128i *)&in6addr_any);
v29 = addr;
while ( v27 )
{
*v29++ = 0;
--v27;
}
*(_WORD *)addr = 10;
*(_WORD *)&addr[2] = v8;
*(__m128i *)&addr[8] = v28;
if ( bind(v7, (const struct sockaddr *)addr, 0x1Cu) != -1 )
goto LABEL_21;
}
else
{
buf.sa_family = 2;
*(_WORD *)buf.sa_data = v8;
*(_DWORD *)&buf.sa_data[2] = 0;
if ( bind(v7, &buf, 0x10u) != -1 )
{
while ( 1 )
{
LABEL_21:
clock_gettime(0, &tp);
v13 = tp.tv_sec;
dword_404108 = func7((unsigned int)(LODWORD(tp.tv_sec) - 2085978496), tp.tv_sec, v14, v5, 0.0);
dword_40410C = func7(
(unsigned int)(int)((double)SLODWORD(tp.tv_nsec) * 4294967296.0 / 1000000000.0),
v13,
tp.tv_nsec,
v15,
(double)SLODWORD(tp.tv_nsec) * 4294967296.0 / 1000000000.0);
printf("Sending time %ld sec %ld nsec to %s\n", v13, v16, v17);
if ( v6 == 10 )
{
*(_OWORD *)addr = 0LL;
*(_WORD *)addr = 10;
*(_QWORD *)&addr[16] = 0LL;
v33 = 0;
*(_WORD *)&addr[2] = v9;
inet_pton(10, v5, &addr[8]);
v10 = 28;
v11 = (const struct sockaddr *)addr;
}
else
{
buf = 0LL;
buf.sa_family = 2;
*(_WORD *)buf.sa_data = v9;
inet_pton(2, v5, &buf.sa_data[2]);
v10 = 16;
v11 = &buf;
}
if ( sendto(v7, &ntpmsg, 0x30uLL, 0, v11, v10) == -1 )
{
perror("sendto");
close(v7);
return v4;
}
if ( (ntpmsg & 7) == 3 )
{
if ( recv(v7, v34, 0x30uLL, 0) == -1 )
{
perror("recv");
close(v7);
return v4;
}
v20 = func7(v36, v34, v18, v19, 0.0);
v22 = func7(v35, v34, v21, v20, 0.0);
v24 = (double)v23 * 1000000000.0 * 2.328306436538696e-10;
if ( v24 >= 9.223372036854776e18 )
v25 = (unsigned int)(int)(v24 - 9.223372036854776e18) ^ 0x8000000000000000LL;
else
v25 = (unsigned int)(int)v24;
printf("NTP time got from %s: %lu sec %lu nsec\n", v5, v22 - 2208988800LL, v25);
v12 = ival;
if ( !ival )
return 0;
}
else
{
v12 = ival;
if ( !ival )
return 0;
}
sleep(v12);
}
}
}
perror("bind");
close(v7);
}
return v4;
}

original_code:
int
main(int argc, char *argv[])
{
 int ch;
 while ((ch = getopt(argc, argv, "bci:ps")) != -1)
 {
  switch (ch)
  {
   case 'b':
    bflag = 1;
    break;
   case 'c':
    cflag = 1;
    break;
   case 'i':
    ival = (int) strtol(optarg, ((void *)0), 10);
    break;
   case 'p':
    pflag = 1;
    break;
   case 's':
    sflag = 1;
    break;
   default:
    usage();
  }
 }
 argc -= optind;
 argv += optind;
 if (argc != 1)
 {
  usage();
 }
 const char * host = argv[0];
 if (bflag)
 {
  ntpmsg.status = 5 | (4 << 3) | (3 << 6);
 }
 else if (sflag)
 {
  ntpmsg.status = 4 | (4 << 3) | (3 << 6);
 }
 else
 {
  ntpmsg.status = 3 | (4 << 3) | (3 << 6);
 }
 int family;
 if (strchr(host, ':') != ((void *)0))
 {
  family = 10;
 }
 else
 {
  family = 2;
 }
 int sockfd = socket(family, SOCK_DGRAM, 0);
 if (sockfd == -1)
 {
  perror("socket");
  return 1;
 }
 struct sockaddr_in6 sin6;
 struct sockaddr_in sin4;
 if (pflag)
 {
  if (family == 10)
  {
   memset(&sin6, 0, sizeof(sin6));
   sin6.sin6_family = 10;
   sin6.sin6_port = htons(123);
   sin6.sin6_addr = in6addr_any;
   if (bind(sockfd, (const struct sockaddr *) &sin6, sizeof(struct sockaddr_in6)) == -1)
   {
    perror("bind");
    close(sockfd);
    return 1;
   }
  }
  else
  {
   sin4.sin_family = 2;
   sin4.sin_port = htons(123);
   sin4.sin_addr.s_addr = ((in_addr_t) 0x00000000);
   if (bind(sockfd, (const struct sockaddr *) &sin4, sizeof(struct sockaddr_in)) == -1)
   {
    perror("bind");
    close(sockfd);
    return 1;
   }
  }
 }
 for (;;)
 {
  struct timespec ts;
  clock_gettime(0, &ts);
  ntpmsg.xmttime.int_partl = htonl(ts.tv_sec + 2208988800UL);
  ntpmsg.xmttime.fractionl = htonl((double) ts.tv_nsec * 0x100000000UL / 1000000000);
  printf("Sending time %ld sec %ld nsec to %s\n", ts.tv_sec, ts.tv_nsec, host);
  if (family == 10)
  {
   memset(&sin6, 0, sizeof(sin6));
   sin6.sin6_family = 10;
   sin6.sin6_port = htons(123);
   inet_pton(10, host, &sin6.sin6_addr);
   if (sendto(sockfd, &ntpmsg, sizeof(ntpmsg), 0, (const struct sockaddr *) &sin6, sizeof(struct sockaddr_in6)) == -1)
   {
    perror("sendto");
    close(sockfd);
    return 1;
   }
  }
  else
  {
   memset(&sin4, 0, sizeof(sin4));
   sin4.sin_family = 2;
   sin4.sin_port = htons(123);
   inet_pton(2, host, &sin4.sin_addr);
   if (sendto(sockfd, &ntpmsg, sizeof(ntpmsg), 0, (const struct sockaddr *) &sin4, sizeof(struct sockaddr_in)) == -1)
   {
    perror("sendto");
    close(sockfd);
    return 1;
   }
  }
  if ((ntpmsg.status & (7 << 0)) == 3)
  {
   struct ntp_msg recvmsg;
   if (recv(sockfd, &recvmsg, sizeof(recvmsg), 0) == -1)
   {
    perror("recv");
    close(sockfd);
    return 1;
   }
   printf("NTP time got from %s: %lu sec %lu nsec\n", host, ntohl(recvmsg.xmttime.int_partl) - 2208988800UL,
    (unsigned long) (((double) ntohl(recvmsg.xmttime.fractionl) * 1000000000) / 0x100000000UL));
  }
  if (ival == 0)
  {
   return 0;
  }
  else
  {
   sleep(ival);
  }
 }
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 int ch;
 while ((ch = getopt(argc, argv, "bci:ps")) != -1)
 {
  switch (ch)
  {
   case 'b':
    bflag = 1;
    break;
   case 'c':
    cflag = 1;
    break;
   case 'i':
    ival = (int) strtol(optarg, ((void *)0), 10);
    break;
   case 'p':
    pflag = 1;
    break;
   case 's':
    sflag = 1;
    break;
   default:
    usage();
  }
 }
 argc -= optind;
 argv += optind;
 if (argc != 1)
 {
  usage();
 }
 char *host = argv[0];
 if (bflag)
 {
  ntpmsg.status = 5 | (4 << 3) | (3 << 6);
 }
 else if (sflag)
 {
  ntpmsg.status = 4 | (4 << 3) | (3 << 6);
 }
 else
 {
  ntpmsg.status = 3 | (4 << 3) | (3 << 6);
 }
 int family;
 if (strchr(host, ':') != ((void *)0))
 {
  family = 10;
 }
 else
 {
  family = 2;
 }
 int sockfd = socket(family, SOCK_DGRAM, 0);
 if (sockfd == -1)
 {
  perror("socket");
  return 1;
 }
 struct sockaddr_in6 sin6;
 struct sockaddr_in sin4;
 if (pflag)
 {
  if (family == 10)
  {
   memset(&sin6, 0, sizeof(sin6));
   sin6.sin6_family = 10;
   sin6.sin6_port = htons(123);
   sin6.sin6_addr = in6addr_any;
   if (bind(sockfd, (struct sockaddr *) &sin6, sizeof(struct sockaddr_in6)) == -1)
   {
    perror("bind");
    close(sockfd);
    return 1;
   }
  }
  else
  {
   sin4.sin_family = 2;
   sin4.sin_port = htons(123);
   sin4.sin_addr.s_addr = ((unsigned int) 0x00000000);
   if (bind(sockfd, (struct sockaddr *) &sin4, sizeof(struct sockaddr_in)) == -1)
   {
    perror("bind");
    close(sockfd);
    return 1;
   }
  }
 }
 for (;;)
 {
  struct timespec ts;
  clock_gettime(0, &ts);
  ntpmsg.xmttime.int_partl = htonl(ts.tv_sec + 2208988800UL);
  ntpmsg.xmttime.fractionl = htonl((double) ts.tv_nsec * 0x100000000UL / 1000000000);
  printf("Sending time %ld sec %ld nsec to %s\n", ts.tv_sec, ts.tv_nsec, host);
  if (family == 10)
  {
   memset(&sin6, 0, sizeof(sin6));
   sin6.sin6_family = 10;
   sin6.sin6_port = htons(123);
   inet_pton(10, host, &sin6.sin6_addr);
   if (sendto(sockfd, &ntpmsg, sizeof(ntpmsg), 0, (struct sockaddr *) &sin6, sizeof(struct sockaddr_in6)) == -1)
   {
    perror("sendto");
    close(sockfd);
    return 1;
   }
  }
  else
  {
   memset(&sin4, 0, sizeof(sin4));
   sin4.sin_family = 2;
   sin4.sin_port = htons(123);
   inet_pton(2, host, &sin4.sin_addr);
   if (sendto(sockfd, &ntpmsg, sizeof(ntpmsg), 0, (struct sockaddr *) &sin4, sizeof(struct sockaddr_in)) == -1)
   {
    perror("sendto");
    close(sockfd);
    return 1;
   }
  }
  if ((ntpmsg.status & (7 << 0)) == 3)
  {
   struct ntp_msg recvmsg;
   if (recv(sockfd, &recvmsg, sizeof(recvmsg), 0) == -1)
   {
    perror("recv");
    close(sockfd);
    return 1;
   }
   printf("NTP time got from %s: %lu sec %lu nsec\n", host, ntohl(recvmsg.xmttime.int_partl) - 2208988800UL,
    (unsigned long) (((double) ntohl(recvmsg.xmttime.fractionl) * 1000000000) / 0x100000000UL));
  }
  if (ival == 0)
  {
   return 0;
  }
  else
  {
   sleep(ival);
  }
 }
 return 0;
}

variable_types: ch int

variable_types:
host
char *

variable_types: family int

variable_types: sockfd int

variable_types: struct sockaddr_in6 { unsigned short sin6_family; unsigned short sin6_port; unsigned int sin6_flowinfo; struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; } sin6_addr; unsigned int sin6_scope_id; }

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 timespec { long tv_sec; long tv_nsec; }

variable_types: struct ntp_msg { unsigned char status; unsigned char stratum; unsigned char ppoll; char precision; struct s_fixedpt { unsigned short int_parts; unsigned short fractions; } rootdelay; struct s_fixedpt dispersion; unsigned int refid; struct l_fixedpt { unsigned int int_partl; unsigned int fractionl; } reftime; struct l_fixedpt orgtime; struct l_fixedpt rectime; struct l_fixedpt xmttime; }

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

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

user_defined_types: struct ntp_msg { unsigned char status; unsigned char stratum; unsigned char ppoll; char precision; struct s_fixedpt rootdelay; struct s_fixedpt dispersion; unsigned int refid; struct l_fixedpt reftime; struct l_fixedpt orgtime; struct l_fixedpt rectime; struct l_fixedpt xmttime; }

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


repo: jmahler/linux-utils

decompiled_code:
unsigned int sig_alrm()
{
unsigned int result; // eax

result = psend();
if ( (int)ival > 0 )
result = alarm(ival);
return result;
}

canonical_decompiled_code:
unsigned int func7()
{
unsigned int result; // eax

result = func10();
if ( (int)ival > 0 )
result = alarm(ival);
return result;
}

original_code:
void sig_alrm()
{
 psend();
 if (ival > 0)
  alarm(ival);
 return;
}


pinit

decompiled_code:
int __fastcall pinit(const char *a1)
{
int v1; // eax
struct protoent *v2; // rax
__uid_t v3; // eax
const char *v5; // rax
int optval; // [rsp+Ch] [rbp-3Ch] BYREF
struct addrinfo req; // [rsp+10h] [rbp-38h] BYREF

*(_OWORD *)&req.ai_socktype = 0LL;
req.ai_next = 0LL;
*(_QWORD *)&req.ai_flags = 0x200000002LL;
req.ai_socktype = 3;
*(_OWORD *)&req.ai_addr = 0LL;
v1 = getaddrinfo(a1, 0LL, &req, &ai);
if ( v1 )
{
v5 = gai_strerror(v1);
fprintf(stderr, "gettaddrinfo failed: %s\n", v5);
exit(1);
}
inet_ntop(2, &ai->ai_addr->sa_data[2], host, 0x10u);
printf("PING %s (%s) %d data bytes of data.\n", ai->ai_canonname, host, (unsigned int)datalen);
v2 = getprotobyname("icmp");
sockfd = socket(2, 3, v2->p_proto);
if ( sockfd == -1 )
{
if ( *__errno_location() == 1 )
fwrite("socket(): Operation not permitted, are you root?\n", 1uLL, 0x31uLL, stderr);
else
perror("socket()");
exit(1);
}
v3 = getuid();
setuid(v3);
optval = 61440;
return setsockopt(sockfd, 1, 8, &optval, 4u);
}

canonical_decompiled_code:
int __fastcall func8(const char *a1)
{
int v1; // eax
struct protoent *v2; // rax
__uid_t v3; // eax
const char *v5; // rax
int optval; // [rsp+Ch] [rbp-3Ch] BYREF
struct addrinfo req; // [rsp+10h] [rbp-38h] BYREF

*(_OWORD *)&req.ai_socktype = 0LL;
req.ai_next = 0LL;
*(_QWORD *)&req.ai_flags = 0x200000002LL;
req.ai_socktype = 3;
*(_OWORD *)&req.ai_addr = 0LL;
v1 = getaddrinfo(a1, 0LL, &req, &ai);
if ( v1 )
{
v5 = gai_strerror(v1);
fprintf(stderr, "gettaddrinfo failed: %s\n", v5);
exit(1);
}
inet_ntop(2, &ai->ai_addr->sa_data[2], host, 0x10u);
printf("PING %s (%s) %d data bytes of data.\n", ai->ai_canonname, host, (unsigned int)datalen);
v2 = getprotobyname("icmp");
sockfd = socket(2, 3, v2->p_proto);
if ( sockfd == -1 )
{
if ( *__errno_location() == 1 )
fwrite("socket(): Operation not permitted, are you root?\n", 1uLL, 0x31uLL, stderr);
else
perror("socket()");
exit(1);
}
v3 = getuid();
setuid(v3);
optval = 61440;
return setsockopt(sockfd, 1, 8, &optval, 4u);
}

original_code:
void pinit(char* node)
{
 struct addrinfo hints;
 int ret;
 struct sockaddr_in* sin;
 struct protoent* prent;
 int proto;
 int size;
 memset(&hints, 0, sizeof(hints));
 hints.ai_family = 2;
 hints.ai_socktype = SOCK_RAW;
 hints.ai_flags = 0x0002;
 if ( (ret = getaddrinfo(node, ((void *)0), &hints, &ai)) != 0) {
  fprintf(stderr, "gettaddrinfo failed: %s\n", gai_strerror(ret));
  exit(1);
 }
 sin = (struct sockaddr_in*) ai->ai_addr;
 inet_ntop(2, &(sin->sin_addr), host, 16);
 printf("PING %s (%s) %d data bytes of data.\n", ai->ai_canonname,
   host, datalen);
 prent = getprotobyname("icmp");
 proto = prent->p_proto;
 sockfd = socket(2, SOCK_RAW, proto);
 if (-1 == sockfd) {
  if ((*__errno_location ()) == 1) {
   fprintf(stderr, "socket(): Operation not permitted, are you root?\n");
  } else {
   perror("socket()");
  }
  exit(1);
 }
 setuid(getuid());
 size = 60 * 1024;
 setsockopt(sockfd, 1, 8, &size, sizeof(size));
}

canonical_original_code:
void pinit(char *node)
{
 struct addrinfo hints;
 int ret;
 struct sockaddr_in *sin;
 struct protoent *prent;
 int proto;
 int size;
 memset(&hints, 0, sizeof(hints));
 hints.ai_family = 2;
 hints.ai_socktype = SOCK_RAW;
 hints.ai_flags = 0x0002;
 if ( (ret = getaddrinfo(node, ((void *)0), &hints, &ai)) != 0) {
  fprintf(stderr, "gettaddrinfo failed: %s\n", gai_strerror(ret));
  exit(1);
 }
 sin = (struct sockaddr_in *) ai->ai_addr;
 inet_ntop(2, &(sin->sin_addr), host, 16);
 printf("PING %s (%s) %d data bytes of data.\n", ai->ai_canonname,
   host, datalen);
 prent = getprotobyname("icmp");
 proto = prent->p_proto;
 sockfd = socket(2, SOCK_RAW, proto);
 if (-1 == sockfd) {
  if ((*__errno_location ()) == 1) {
   fprintf(stderr, "socket(): Operation not permitted, are you root?\n");
  } else {
   perror("socket()");
  }
  exit(1);
 }
 setuid(getuid());
 size = 60 * 1024;
 setsockopt(sockfd, 1, 8, &size, sizeof(size));
}

variable_types:
node
char *

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

variable_types: ret int

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 protoent { char *p_name; char **p_aliases; int p_proto; } *

variable_types: proto int

variable_types: size int

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


original_code:
unsigned short in_cksum(unsigned short* addr, int len)
{
 int nleft = len;
 int sum = 0;
 unsigned short *w = addr;
 unsigned short answer = 0;
 while (nleft > 1) {
  sum += *w++;
  nleft -= 2;
 }
 if (nleft == 1) {
  *(unsigned char *) (&answer) = *(unsigned char*) w;
  sum += answer;
 }
 sum = (sum >> 16) + (sum & 0xffff);
 sum += (sum >> 16);
 answer = ~sum;
 return (answer);
}

canonical_original_code:
unsigned short in_cksum(unsigned short *addr, int len)
{
 int nleft = len;
 int sum = 0;
 unsigned short *w = addr;
 unsigned short answer = 0;
 while (nleft > 1) {
  sum += *w++;
  nleft -= 2;
 }
 if (nleft == 1) {
  *(unsigned char *) (&answer) = *(unsigned char *) w;
  sum += answer;
 }
 sum = (sum >> 16) + (sum & 0xffff);
 sum += (sum >> 16);
 answer = ~sum;
 return (answer);
}

variable_types:
addr
unsigned short *

variable_types: len int

variable_types: nleft int

variable_types: sum int

variable_types:
w
unsigned short *

variable_types:
answer
unsigned short

return_type: unsigned short


psend

decompiled_code:
ssize_t psend()
{
int v0; // er9
unsigned __int16 v2; // [rsp+0h] [rbp-5E8h] BYREF
__int16 v3; // [rsp+2h] [rbp-5E6h]
__int16 v4; // [rsp+4h] [rbp-5E4h]
__int16 v5; // [rsp+6h] [rbp-5E2h]
struct timeval tv; // [rsp+8h] [rbp-5E0h] BYREF

v2 = 8;
v4 = pid;
v5 = nsent++;
gettimeofday(&tv, 0LL);
v3 = 0;
v3 = in_cksum(&v2, datalen + 8);
return sendto(sockfd, &v2, v0, 0, ai->ai_addr, ai->ai_addrlen);
}

canonical_decompiled_code:
ssize_t func10()
{
int v0; // er9
unsigned __int16 v2; // [rsp+0h] [rbp-5E8h] BYREF
__int16 v3; // [rsp+2h] [rbp-5E6h]
__int16 v4; // [rsp+4h] [rbp-5E4h]
__int16 v5; // [rsp+6h] [rbp-5E2h]
struct timeval tv; // [rsp+8h] [rbp-5E0h] BYREF

v2 = 8;
v4 = pid;
v5 = nsent++;
gettimeofday(&tv, 0LL);
v3 = 0;
v3 = func9(&v2, datalen + 8);
return sendto(sockfd, &v2, v0, 0, ai->ai_addr, ai->ai_addrlen);
}

original_code:
void psend()
{
 int len;
 struct icmp* icmp;
 char sendbuf[1500];
 icmp = (struct icmp *) sendbuf;
 icmp->icmp_type = 8;
 icmp->icmp_code = 0;
 icmp->icmp_hun.ih_idseq.icd_id = pid;
 icmp->icmp_hun.ih_idseq.icd_seq = nsent++;
 gettimeofday((struct timeval *) icmp->icmp_dun.id_data, ((void *)0));
 len = 8 + datalen;
 icmp->icmp_cksum = 0;
 icmp->icmp_cksum = in_cksum((u_short *) icmp, len);
 sendto(sockfd, sendbuf, len, 0, ai->ai_addr, ai->ai_addrlen);
}

canonical_original_code:
void psend()
{
 int len;
 struct icmp *icmp;
 char sendbuf[1500];
 icmp = (struct icmp *) sendbuf;
 icmp->icmp_type = 8;
 icmp->icmp_code = 0;
 icmp->icmp_hun.ih_idseq.icd_id = pid;
 icmp->icmp_hun.ih_idseq.icd_seq = nsent++;
 gettimeofday((struct timeval *) icmp->icmp_dun.id_data, ((void *)0));
 len = 8 + datalen;
 icmp->icmp_cksum = 0;
 icmp->icmp_cksum = in_cksum((unsigned short *) icmp, len);
 sendto(sockfd, sendbuf, len, 0, ai->ai_addr, ai->ai_addrlen);
}

variable_types: struct icmp { unsigned char icmp_type; unsigned char icmp_code; unsigned short icmp_cksum; union { unsigned char ih_pptr; struct in_addr { unsigned int s_addr; } ih_gwaddr; struct ih_idseq { unsigned short icd_id; unsigned short icd_seq; } ih_idseq; unsigned int ih_void; struct ih_pmtu { unsigned short ipm_void; unsigned short ipm_nextmtu; } ih_pmtu; struct ih_rtradv { unsigned char irt_num_addrs; unsigned char irt_wpa; unsigned short irt_lifetime; } ih_rtradv; } icmp_hun; union { struct { unsigned int its_otime; unsigned int its_rtime; unsigned int its_ttime; } id_ts; struct { struct ip { unsigned int ip_hl; unsigned int ip_v; unsigned char ip_tos; unsigned short ip_len; unsigned short ip_id; unsigned short ip_off; unsigned char ip_ttl; unsigned char ip_p; unsigned short ip_sum; struct in_addr ip_src; } idi_ip; } id_ip; struct icmp_ra_addr { unsigned int ira_addr; unsigned int ira_preference; } id_radv; unsigned int id_mask; unsigned char id_data[1]; } icmp_dun; } *

variable_types:
sendbuf
char [1500]

user_defined_types: struct icmp { unsigned char icmp_type; unsigned char icmp_code; unsigned short icmp_cksum; union { unsigned char ih_pptr; struct in_addr ih_gwaddr; struct ih_idseq ih_idseq; unsigned int ih_void; struct ih_pmtu ih_pmtu; struct ih_rtradv ih_rtradv; } icmp_hun; union { struct { unsigned int its_otime; unsigned int its_rtime; unsigned int its_ttime; } id_ts; struct { struct ip idi_ip; } id_ip; struct icmp_ra_addr id_radv; unsigned int id_mask; unsigned char id_data[1]; } icmp_dun; }

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


decompiled_code:
double __fastcall tv_sub(_QWORD *a1, _QWORD *a2)
{
return (double)(int)*a1 * 1000.0
+ (double)(int)a1[1] / 1000.0
- ((double)(int)a2[1] / 1000.0
+ (double)(int)*a2 * 1000.0);
}

original_code:
double tv_sub(struct timeval* tve, struct timeval* tvs) {
 double mse, mss;
 mse = tve->tv_sec * 1000.0 + tve->tv_usec / 1000.0;
 mss = tvs->tv_sec * 1000.0 + tvs->tv_usec / 1000.0;
 return (mse - mss);
}

canonical_original_code:
double tv_sub(struct timeval *tve, struct timeval *tvs) {
 double mse; double mss;
 mse = tve->tv_sec * 1000.0 + tve->tv_usec / 1000.0;
 mss = tvs->tv_sec * 1000.0 + tvs->tv_usec / 1000.0;
 return (mse - mss);
}

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

variable_types:
tvs
struct timeval *

variable_types: mse double

variable_types: mss double

return_type: double


pproc

decompiled_code:
int __fastcall pproc(_BYTE *a1, int a2, _QWORD *a3)
{
int result; // eax
int v6; // er10
_BYTE *v7; // rdx
double v8; // xmm0_8
__int64 v9; // rcx
unsigned int v10; // er10
__int64 v11; // rdx

result = 4 * (*a1 & 0xF);
v6 = a2 - result;
v7 = &a1[result];
if ( a2 - result <= 7 )
{
fprintf(stderr, "icmplen (%d) < 8\n", (unsigned int)v6);
goto LABEL_8;
}
if ( !*v7 )
{
result = *((unsigned __int16 *)v7 + 2);
if ( result == pid )
{
if ( v6 > 15 )
{
v8 = tv_sub(a3, (_QWORD *)v7 + 1);
return printf(
"%d bytes from %s (%s): icmp_seq=%u, ttl=%d, rtt=%.3f ms\n",
v10,
ai->ai_canonname,
host,
*(unsigned __int16 *)(v11 + 6),
*(unsigned __int8 *)(v9 + 8),
v8);
}
fprintf(stderr, "icmplen (%d) < 16\n", (unsigned int)v6);
LABEL_8:
exit(1);
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func12(_BYTE *a1, int a2, _QWORD *a3)
{
int result; // eax
int v6; // er10
_BYTE *v7; // rdx
double v8; // xmm0_8
__int64 v9; // rcx
unsigned int v10; // er10
__int64 v11; // rdx

result = 4 * (*a1 & 0xF);
v6 = a2 - result;
v7 = &a1[result];
if ( a2 - result <= 7 )
{
fprintf(stderr, "icmplen (%d) < 8\n", (unsigned int)v6);
goto LABEL_8;
}
if ( !*v7 )
{
result = *((unsigned __int16 *)v7 + 2);
if ( result == pid )
{
if ( v6 > 15 )
{
v8 = func11(a3, (_QWORD *)v7 + 1);
return printf(
"%d bytes from %s (%s): icmp_seq=%u, ttl=%d, rtt=%.3f ms\n",
v10,
ai->ai_canonname,
host,
*(unsigned __int16 *)(v11 + 6),
*(unsigned __int8 *)(v9 + 8),
v8);
}
fprintf(stderr, "icmplen (%d) < 16\n", (unsigned int)v6);
LABEL_8:
exit(1);
}
}
return result;
}

original_code:
void pproc(char* buf, ssize_t len, struct timeval* tvrecv)
{
 int hlen1;
 int icmplen;
 double rtt;
 struct ip* ip;
 struct icmp* icmp;
 struct timeval* tvsend;
 ip = (struct ip *) buf;
 hlen1 = ip->ip_hl << 2;
 icmp = (struct icmp *) (buf + hlen1);
 if ( (icmplen = len - hlen1) < 8) {
  fprintf(stderr, "icmplen (%d) < 8\n", icmplen);
  exit(1);
 }
 if (0 == icmp->icmp_type) {
  if (icmp->icmp_hun.ih_idseq.icd_id != pid)
   return;
  if (icmplen < 16) {
   fprintf(stderr, "icmplen (%d) < 16\n", icmplen);
   exit(1);
  }
  tvsend = (struct timeval *) icmp->icmp_dun.id_data;
  rtt = tv_sub(tvrecv, tvsend);
  printf("%d bytes from %s (%s): icmp_seq=%u, ttl=%d, rtt=%.3f ms\n",
    icmplen, ai->ai_canonname, host,
    icmp->icmp_hun.ih_idseq.icd_seq, ip->ip_ttl, rtt);
 }
}

canonical_original_code:
void pproc(char *buf, long len, struct timeval *tvrecv)
{
 int hlen1;
 int icmplen;
 double rtt;
 struct ip *ip;
 struct icmp *icmp;
 struct timeval *tvsend;
 ip = (struct ip *) buf;
 hlen1 = ip->ip_hl << 2;
 icmp = (struct icmp *) (buf + hlen1);
 if ( (icmplen = len - hlen1) < 8) {
  fprintf(stderr, "icmplen (%d) < 8\n", icmplen);
  exit(1);
 }
 if (0 == icmp->icmp_type) {
  if (icmp->icmp_hun.ih_idseq.icd_id != pid)
   return;
  if (icmplen < 16) {
   fprintf(stderr, "icmplen (%d) < 16\n", icmplen);
   exit(1);
  }
  tvsend = (struct timeval *) icmp->icmp_dun.id_data;
  rtt = tv_sub(tvrecv, tvsend);
  printf("%d bytes from %s (%s): icmp_seq=%u, ttl=%d, rtt=%.3f ms\n",
    icmplen, ai->ai_canonname, host,
    icmp->icmp_hun.ih_idseq.icd_seq, ip->ip_ttl, rtt);
 }
}

variable_types:
buf
char *

variable_types: len long

variable_types: icmplen int

variable_types: rtt double

variable_types: struct ip { unsigned int ip_hl; unsigned int ip_v; unsigned char ip_tos; unsigned short ip_len; unsigned short ip_id; unsigned short ip_off; unsigned char ip_ttl; unsigned char ip_p; unsigned short ip_sum; struct in_addr { unsigned int s_addr; } ip_src; } *

user_defined_types: struct ip { unsigned int ip_hl; unsigned int ip_v; unsigned char ip_tos; unsigned short ip_len; unsigned short ip_id; unsigned short ip_off; unsigned char ip_ttl; unsigned char ip_p; unsigned short ip_sum; struct in_addr ip_src; }

user_defined_types: struct ih_idseq { unsigned short icd_id; unsigned short icd_seq; }

user_defined_types: struct ih_pmtu { unsigned short ipm_void; unsigned short ipm_nextmtu; }

user_defined_types: struct ih_rtradv { unsigned char irt_num_addrs; unsigned char irt_wpa; unsigned short irt_lifetime; }

user_defined_types: struct icmp_ra_addr { unsigned int ira_addr; unsigned int ira_preference; }


precv

decompiled_code:
int precv()
{
ssize_t v0; // rax
int *v1; // rax
int v2; // ebp
socklen_t addr_len; // [rsp+Ch] [rbp-60Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-608h] BYREF
struct timeval tv; // [rsp+20h] [rbp-5F8h] BYREF
char buf[1512]; // [rsp+30h] [rbp-5E8h] BYREF

addr_len = ai->ai_addrlen;
v0 = recvfrom(sockfd, buf, 0x5DCuLL, 0, &addr, &addr_len);
if ( v0 >= 0 )
{
v2 = v0;
gettimeofday(&tv, 0LL);
LODWORD(v1) = pproc(buf, v2, &tv);
}
else
{
v1 = __errno_location();
if ( *v1 != 4 )
{
perror("recvfrom");
exit(1);
}
}
return (int)v1;
}

canonical_decompiled_code:
int func13()
{
ssize_t v0; // rax
int *v1; // rax
int v2; // ebp
socklen_t addr_len; // [rsp+Ch] [rbp-60Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-608h] BYREF
struct timeval tv; // [rsp+20h] [rbp-5F8h] BYREF
char buf[1512]; // [rsp+30h] [rbp-5E8h] BYREF

addr_len = ai->ai_addrlen;
v0 = recvfrom(sockfd, buf, 0x5DCuLL, 0, &addr, &addr_len);
if ( v0 >= 0 )
{
v2 = v0;
gettimeofday(&tv, 0LL);
LODWORD(v1) = func12(buf, v2, &tv);
}
else
{
v1 = __errno_location();
if ( *v1 != 4 )
{
perror("recvfrom");
exit(1);
}
}
return (int)v1;
}

original_code:
void precv()
{
 socklen_t len;
 ssize_t n;
 struct sockaddr sarecv;
 struct timeval tval;
 char recvbuf[1500];
 do {
  len = ai->ai_addrlen;
  n = recvfrom(sockfd, recvbuf, sizeof(recvbuf), 0, &sarecv, &len);
  if (n < 0) {
   if ((*__errno_location ()) == 4) {
    continue;
   } else {
    perror("recvfrom");
    exit(1);
   }
  }
  gettimeofday(&tval, ((void *)0));
  pproc(recvbuf, n, &tval);
 } while (0);
}

canonical_original_code:
void precv()
{
 unsigned int len;
 long n;
 struct sockaddr sarecv;
 struct timeval tval;
 char recvbuf[1500];
 do {
  len = ai->ai_addrlen;
  n = recvfrom(sockfd, recvbuf, sizeof(recvbuf), 0, &sarecv, &len);
  if (n < 0) {
   if ((*__errno_location ()) == 4) {
    continue;
   } else {
    perror("recvfrom");
    exit(1);
   }
  }
  gettimeofday(&tval, ((void *)0));
  pproc(recvbuf, n, &tval);
 } while (0);
}

variable_types:
len
unsigned int

variable_types: n long

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

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

variable_types:
recvbuf
char [1500]


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // eax
const char *v4; // rbp
__sigset_t *v5; // rdi
__int64 v6; // rcx
__int64 v7; // rdi
struct sigaction v8; // [rsp+0h] [rbp-B8h] BYREF

opterr = 0;
while ( 1 )
{
v3 = getopt(argc, (char *const *)argv, "i");
if ( v3 == -1 )
break;
if ( v3 == 63 )
{
fprintf(stderr, "unrecognized option: %s\n", argv[optind - 1]);
goto LABEL_7;
}
if ( v3 == 105 )
ival = atoi(argv[optind]);
}
if ( argc > 1 )
{
v4 = argv[argc - 1];
pid = getpid();
pinit(v4);
v5 = &v8.sa_mask;
v6 = 36LL;
v8.sa_handler = (__sighandler_t)sig_alrm;
while ( v6 )
{
LODWORD(v5->__val[0]) = 0;
v5 = (__sigset_t *)((char *)v5 + 4);
--v6;
}
sigaction(14, &v8, 0LL);
v7 = (unsigned int)pid;
kill(pid, 14);
while ( 1 )
{
if ( !ival )
{
v7 = (unsigned int)pid;
kill(pid, 14);
}
precv(v7);
}
}
fprintf(stderr, "usage: %s [opts] <host>\n", *argv);
LABEL_7:
exit(1);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // eax
const char *v4; // rbp
__sigset_t *v5; // rdi
__int64 v6; // rcx
__int64 v7; // rdi
struct sigaction v8; // [rsp+0h] [rbp-B8h] BYREF

opterr = 0;
while ( 1 )
{
v3 = getopt(argc, (char *const *)argv, "i");
if ( v3 == -1 )
break;
if ( v3 == 63 )
{
fprintf(stderr, "unrecognized option: %s\n", argv[optind - 1]);
goto LABEL_7;
}
if ( v3 == 105 )
ival = atoi(argv[optind]);
}
if ( argc > 1 )
{
v4 = argv[argc - 1];
pid = getpid();
func8(v4);
v5 = &v8.sa_mask;
v6 = 36LL;
v8.sa_handler = (__sighandler_t)sig_alrm;
while ( v6 )
{
LODWORD(v5->__val[0]) = 0;
v5 = (__sigset_t *)((char *)v5 + 4);
--v6;
}
sigaction(14, &v8, 0LL);
v7 = (unsigned int)pid;
kill(pid, 14);
while ( 1 )
{
if ( !ival )
{
v7 = (unsigned int)pid;
kill(pid, 14);
}
func13(v7);
}
}
fprintf(stderr, "usage: %s [opts] <host>\n", *argv);
LABEL_7:
exit(1);
}

original_code:
int main(int argc, char* argv[])
{
 int c;
 char* node;
 struct sigaction sa;
 opterr = 0;
 while ( (c = getopt(argc, argv, "i")) != -1) {
  switch (c) {
  case 'i':
   ival = atoi(argv[optind]);
   break;
  case '?':
   fprintf(stderr, "unrecognized option: %s\n", argv[optind-1]);
   exit(1);
  }
 }
 if (!(argc >= 2)) {
  fprintf(stderr, "usage: %s [opts] <host>\n", argv[0]);
  exit(1);
 }
 node = argv[argc - 1];
 pid = getpid();
 pinit(node);
 memset(&sa, 0, sizeof(sa));
 sa.__sigaction_handler.sa_handler = sig_alrm;
 sigaction(14, &sa, 0);
 kill(pid, 14);
 while (1) {
  if (0 == ival)
   kill(pid, 14);
  precv();
 }
 exit(0);
}

canonical_original_code:
int main(int argc, char *argv[])
{
 int c;
 char *node;
 struct sigaction sa;
 opterr = 0;
 while ( (c = getopt(argc, argv, "i")) != -1) {
  switch (c) {
  case 'i':
   ival = atoi(argv[optind]);
   break;
  case '?':
   fprintf(stderr, "unrecognized option: %s\n", argv[optind-1]);
   exit(1);
  }
 }
 if (!(argc >= 2)) {
  fprintf(stderr, "usage: %s [opts] <host>\n", argv[0]);
  exit(1);
 }
 node = argv[argc - 1];
 pid = getpid();
 pinit(node);
 memset(&sa, 0, sizeof(sa));
 sa.__sigaction_handler.sa_handler = sig_alrm;
 sigaction(14, &sa, 0);
 kill(pid, 14);
 while (1) {
  if (0 == ival)
   kill(pid, 14);
  precv();
 }
 exit(0);
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: c int

variable_types:
node
char *

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

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

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


streql

decompiled_code:
_BOOL8 __fastcall streql(__int64 a1, __int64 a2)
{
__int64 v2; // rax
char v3; // dl
char v4; // cl

v2 = 0LL;
do
{
v3 = *(_BYTE *)(a1 + v2);
v4 = *(_BYTE *)(a2 + v2);
if ( !v3 )
return v4 == 0;
if ( !v4 )
break;
++v2;
}
while ( v3 == v4 );
return 0LL;
}

canonical_decompiled_code:
_BOOL8 __fastcall func7(__int64 a1, __int64 a2)
{
__int64 v2; // rax
char v3; // dl
char v4; // cl

v2 = 0LL;
do
{
v3 = *(_BYTE *)(a1 + v2);
v4 = *(_BYTE *)(a2 + v2);
if ( !v3 )
return v4 == 0;
if ( !v4 )
break;
++v2;
}
while ( v3 == v4 );
return 0LL;
}

original_code:
int streql(char *s1, char *s2)
{
 do {
  if (*s1 == '\0')
   if (*s2 == '\0')
    return 1;
   else
    return 0;
  else if (*s2 == '\0')
   return 0;
 } while (*(s1++) == *(s2++));
 return 0;
}


repo: jmakovicka/nfblock

decompiled_code:
__int64 __fastcall nfblock_dbus_init(void (*a1)(__int64, const char *, ...))
{
int v1; // er12
__int64 result; // rax
const char *vars8; // [rsp+8h] [rbp+8h]

dbus_error_init();
dbconn = dbus_bus_get();
if ( (unsigned int)dbus_error_is_set() )
{
a1(3LL, "Error connecting to dbus-daemon: %s", vars8);
result = 0xFFFFFFFFLL;
}
else
{
a1(6LL, "Connected to system bus.");
dbus_error_init();
v1 = dbus_bus_request_name();
if ( (unsigned int)dbus_error_is_set() )
{
a1(3LL, "Error requesting name: %s.", vars8);
result = 0xFFFFFFFFLL;
}
else if ( v1 == 3 )
{
a1(4LL, "nfblockd is already running. Exiting.");
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(void (*a1)(__int64, const char *, ...))
{
int v1; // er12
__int64 result; // rax
const char *vars8; // [rsp+8h] [rbp+8h]

dbus_error_init();
dbconn = dbus_bus_get();
if ( (unsigned int)dbus_error_is_set() )
{
a1(3LL, "Error connecting to dbus-daemon: %s", vars8);
result = 0xFFFFFFFFLL;
}
else
{
a1(6LL, "Connected to system bus.");
dbus_error_init();
v1 = dbus_bus_request_name();
if ( (unsigned int)dbus_error_is_set() )
{
a1(3LL, "Error requesting name: %s.", vars8);
result = 0xFFFFFFFFLL;
}
else if ( v1 == 3 )
{
a1(4LL, "nfblockd is already running. Exiting.");
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
return result;
}

original_code:
int
nfblock_dbus_init(log_func_t do_log)
{
    DBusError dberr;
    int req;
    dbus_error_init(&dberr);
    dbconn = dbus_bus_get(DBUS_BUS_SYSTEM, &dberr);
    if (dbus_error_is_set(&dberr)) {
        do_log(3, "Error connecting to dbus-daemon: %s", dberr.message);
        return -1;
    }
    do_log(6, "Connected to system bus.");
    dbus_error_init(&dberr);
    req = dbus_bus_request_name(dbconn, "org.netfilter.nfblock",
        0x4, &dberr);
    if (dbus_error_is_set(&dberr)) {
        do_log(3, "Error requesting name: %s.", dberr.message);
        return -1;
    }
    if (req == 3) {
        do_log(4, "nfblockd is already running. Exiting.");
        return -1;
    }
    return 0;
}

canonical_original_code:
int nfblock_dbus_init(void (*do_log)(int priority, char *format, ...))
{
    struct DBusError dberr;
    int req;
    dbus_error_init(&dberr);
    dbconn = dbus_bus_get(DBUS_BUS_SYSTEM, &dberr);
    if (dbus_error_is_set(&dberr)) {
        do_log(3, "Error connecting to dbus-daemon: %s", dberr.message);
        return -1;
    }
    do_log(6, "Connected to system bus.");
    dbus_error_init(&dberr);
    req = dbus_bus_request_name(dbconn, "org.netfilter.nfblock",
        0x4, &dberr);
    if (dbus_error_is_set(&dberr)) {
        do_log(3, "Error requesting name: %s.", dberr.message);
        return -1;
    }
    if (req == 3) {
        do_log(4, "nfblockd is already running. Exiting.");
        return -1;
    }
    return 0;
}

variable_types: void (*)(int priority, char *format, ...)

variable_types: struct DBusError { char *name; char *message; unsigned int dummy1; unsigned int dummy2; unsigned int dummy3; unsigned int dummy4; unsigned int dummy5; void *padding1; }

variable_types: req int


decompiled_code:
__int64 __fastcall strip_crlf(_BYTE *a1)
{
__int64 result; // rax

while ( 1 )
{
result = (unsigned __int8)*a1;
if ( !(_BYTE)result )
break;
if ( (_BYTE)result == 13 || (_BYTE)result == 10 )
{
*a1 = 0;
return result;
}
++a1;
}
return result;
}

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

while ( 1 )
{
result = (unsigned __int8)*a1;
if ( !(_BYTE)result )
break;
if ( (_BYTE)result == 13 || (_BYTE)result == 10 )
{
*a1 = 0;
return result;
}
++a1;
}
return result;
}

original_code:
static void
strip_crlf(char* str)
{
    while (*str) {
        if (*str == '\r' || *str == '\n') {
            *str = 0;
            break;
        }
        str++;
    }
}

canonical_original_code:
static void strip_crlf(char *str)
{
    while (*str) {
        if (*str == '\r' || *str == '\n') {
            *str = 0;
            break;
        }
        str++;
    }
}

variable_types:
str
char *


decompiled_code:
__int64 __fastcall load_list(__int64 a1, const char *a2, const char *a3)
{
unsigned int v4; // er13
unsigned int v5; // er13
unsigned int v6; // er13
unsigned int v7; // er12
__int64 result; // rax

v4 = *(_DWORD *)(a1 + 16);
if ( (unsigned int)loadlist_p2b(a1, a2) )
{
blocklist_clear(a1, v4);
v5 = *(_DWORD *)(a1 + 16);
if ( !a3 )
a3 = "ISO8859-1";
if ( (unsigned int)loadlist_dat(a1, a2, a3) )
{
blocklist_clear(a1, v5);
v6 = *(_DWORD *)(a1 + 16);
v7 = loadlist_p2p(a1, a2, a3);
if ( v7 )
{
v7 = -1;
blocklist_clear(a1, v6);
}
else
{
do_log(7LL, "PeerGuardian Ascii: %d entries loaded", *(_DWORD *)(a1 + 16) - v6);
}
result = v7;
}
else
{
do_log(7LL, "IPFilter: %d entries loaded", *(_DWORD *)(a1 + 16) - v5);
result = 0LL;
}
}
else
{
do_log(7LL, "PeerGuardian Binary: %d entries loaded", *(_DWORD *)(a1 + 16) - v4);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, const char *a2, const char *a3)
{
unsigned int v4; // er13
unsigned int v5; // er13
unsigned int v6; // er13
unsigned int v7; // er12
__int64 result; // rax

v4 = *(_DWORD *)(a1 + 16);
if ( (unsigned int)func5(a1, a2) )
{
blocklist_clear(a1, v4);
v5 = *(_DWORD *)(a1 + 16);
if ( !a3 )
a3 = "ISO8859-1";
if ( (unsigned int)func2(a1, a2, a3) )
{
blocklist_clear(a1, v5);
v6 = *(_DWORD *)(a1 + 16);
v7 = func3(a1, a2, a3);
if ( v7 )
{
v7 = -1;
blocklist_clear(a1, v6);
}
else
{
do_log(7LL, "PeerGuardian Ascii: %d entries loaded", *(_DWORD *)(a1 + 16) - v6);
}
result = v7;
}
else
{
do_log(7LL, "IPFilter: %d entries loaded", *(_DWORD *)(a1 + 16) - v5);
result = 0LL;
}
}
else
{
do_log(7LL, "PeerGuardian Binary: %d entries loaded", *(_DWORD *)(a1 + 16) - v4);
result = 0LL;
}
return result;
}

original_code:
int
load_list(blocklist_t* blocklist, const char* filename, const char* charset)
{
    int prevcount;
    prevcount = blocklist->count;
    if (loadlist_p2b(blocklist, filename) == 0) {
        do_log(7, "PeerGuardian Binary: %d entries loaded", blocklist->count - prevcount);
        return 0;
    }
    blocklist_clear(blocklist, prevcount);
    prevcount = blocklist->count;
    if (loadlist_dat(blocklist, filename, charset ? charset : "ISO8859-1") == 0) {
        do_log(7, "IPFilter: %d entries loaded", blocklist->count - prevcount);
        return 0;
    }
    blocklist_clear(blocklist, prevcount);
    prevcount = blocklist->count;
    if (loadlist_p2p(blocklist, filename, charset ? charset : "ISO8859-1") == 0) {
        do_log(7, "PeerGuardian Ascii: %d entries loaded", blocklist->count - prevcount);
        return 0;
    }
    blocklist_clear(blocklist, prevcount);
    return -1;
}

canonical_original_code:
int load_list(struct blocklist_t *blocklist, char *filename, char *charset)
{
    int prevcount;
    prevcount = blocklist->count;
    if (loadlist_p2b(blocklist, filename) == 0) {
        do_log(7, "PeerGuardian Binary: %d entries loaded", blocklist->count - prevcount);
        return 0;
    }
    blocklist_clear(blocklist, prevcount);
    prevcount = blocklist->count;
    if (loadlist_dat(blocklist, filename, charset ? charset : "ISO8859-1") == 0) {
        do_log(7, "IPFilter: %d entries loaded", blocklist->count - prevcount);
        return 0;
    }
    blocklist_clear(blocklist, prevcount);
    prevcount = blocklist->count;
    if (loadlist_p2p(blocklist, filename, charset ? charset : "ISO8859-1") == 0) {
        do_log(7, "PeerGuardian Ascii: %d entries loaded", blocklist->count - prevcount);
        return 0;
    }
    blocklist_clear(blocklist, prevcount);
    return -1;
}

variable_types: struct blocklist_t { struct block_entry_t { unsigned int ip_min; } *entries; struct block_entry2_t { char *name; int hits; int merged_idx; long lasttime; } *entries2; unsigned int count; struct block_sub_entry_t { char *name; unsigned int ip_min; } *subentries; unsigned int subcount; } *

variable_types:
filename
char *

variable_types:
charset
char *

variable_types: prevcount int

user_defined_types: struct blocklist_t { struct block_entry_t *entries; struct block_entry2_t *entries2; unsigned int count; struct block_sub_entry_t *subentries; unsigned int subcount; }


logger

repo: jmaurice/zorak-services

decompiled_code:
int logger(int pri, char *format, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-40D0h] BYREF
char s[16384]; // [rsp+20h] [rbp-40B8h] BYREF

va_start(arg, format);
vsnprintf(s, 0x4000uLL, format, arg);
syslog(pri, "%s", s);
result = *((_DWORD *)&me + 4);
if ( result )
result = printf("%lu.%06lu *** LOG: %s\n", me.tv_sec, me.tv_usec, s);
return result;
}

canonical_decompiled_code:
int func0(int pri, char *format, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-40D0h] BYREF
char s[16384]; // [rsp+20h] [rbp-40B8h] BYREF

va_start(arg, format);
vsnprintf(s, 0x4000uLL, format, arg);
syslog(pri, "%s", s);
result = *((_DWORD *)&me + 4);
if ( result )
result = printf("%lu.%06lu *** LOG: %s\n", me.tv_sec, me.tv_usec, s);
return result;
}

original_code:
void
logger(int priority, char *input, ...)
{
 char logbuf[16384];
 va_list ap;
 __builtin_va_start(ap,input);
 vsnprintf(logbuf, 16384, input, ap);
 syslog(priority, "%s", logbuf);
 if (me.debug)
  printf("%lu.%06lu *** LOG: %s\n", me.now.tv_sec, me.now.tv_usec, logbuf);
 __builtin_va_end(ap);
}

canonical_original_code:
void logger(int priority, char *input, ...)
{
 char logbuf[16384];
 __builtin_va_list ap;
 __builtin_va_start(ap,input);
 vsnprintf(logbuf, 16384, input, ap);
 syslog(priority, "%s", logbuf);
 if (me.debug)
  printf("%lu.%06lu *** LOG: %s\n", me.now.tv_sec, me.now.tv_usec, logbuf);
 __builtin_va_end(ap);
}

variable_types: priority int

variable_types:
input
char *

variable_types:
logbuf
char [16384]


debug

decompiled_code:
void debug(int a1, const char *a2, ...)
{
char *v2; // rax
const char *v3; // r8
gcc_va_list arg; // [rsp+8h] [rbp-40D0h] BYREF
char s[16384]; // [rsp+20h] [rbp-40B8h] BYREF

if ( *((_DWORD *)&me + 4) >= a1 )
{
va_start(arg, a2);
vsnprintf(s, 0x4000uLL, a2, arg);
v2 = strchr(s, 10);
v3 = (const char *)&unk_479A;
if ( !v2 )
v3 = "\n";
printf("%lu.%06lu %s%s", me.tv_sec, me.tv_usec, s, v3);
fflush(stdout);
}
}

canonical_decompiled_code:
void func1(int a1, const char *a2, ...)
{
char *v2; // rax
const char *v3; // r8
gcc_va_list arg; // [rsp+8h] [rbp-40D0h] BYREF
char s[16384]; // [rsp+20h] [rbp-40B8h] BYREF

if ( *((_DWORD *)&me + 4) >= a1 )
{
va_start(arg, a2);
vsnprintf(s, 0x4000uLL, a2, arg);
v2 = strchr(s, 10);
v3 = (const char *)&unk_479A;
if ( !v2 )
v3 = "\n";
printf("%lu.%06lu %s%s", me.tv_sec, me.tv_usec, s, v3);
fflush(stdout);
}
}

original_code:
void
debug(int debug, char *input, ...)
{
 char logbuf[16384];
 va_list ap;
 if (me.debug < debug)
  return;
 __builtin_va_start(ap,input);
 vsnprintf(logbuf, 16384, input, ap);
 if (0)
  syslog(7, "%s", logbuf);
 printf("%lu.%06lu %s%s", me.now.tv_sec, me.now.tv_usec, logbuf, (strchr(logbuf, '\n') ? "" : "\n"));
 fflush(stdout);
 __builtin_va_end(ap);
}

canonical_original_code:
void debug(int debug, char *input, ...)
{
 char logbuf[16384];
 __builtin_va_list ap;
 if (me.debug < debug)
  return;
 __builtin_va_start(ap,input);
 vsnprintf(logbuf, 16384, input, ap);
 if (0)
  syslog(7, "%s", logbuf);
 printf("%lu.%06lu %s%s", me.now.tv_sec, me.now.tv_usec, logbuf, (strchr(logbuf, '\n') ? "" : "\n"));
 fflush(stdout);
 __builtin_va_end(ap);
}

variable_types: debug int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
unsigned int v4; // eax
unsigned int v6; // eax

memset(&me, 0, 0x40A0uLL);
*((_QWORD *)&me + 3) = "services.conf";
gettimeofday(&me, 0LL);
signal(13, handler);
while ( 1 )
{
v3 = getopt(argc, (char *const *)argv, "df:");
if ( v3 == -1 )
break;
if ( v3 == 100 )
{
++*((_DWORD *)&me + 4);
}
else if ( v3 == 102 )
{
*((_QWORD *)&me + 3) = lstrdup(optarg);
}
}
if ( (unsigned int)parse_conf() )
err(1, "Unable to open %s", *((const char **)&me + 3));
if ( *((_DWORD *)&me + 4) )
{
v6 = getpid();
logger(6, "running in the foreground (pid %d)", v6);
logger(5, "running in debug mode (level %d)", *((unsigned int *)&me + 4));
goto LABEL_11;
}
if ( !fork() )
{
v4 = getpid();
logger(5, "running in the background (pid %d)", v4);
LABEL_11:
io_loop();
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
unsigned int v4; // eax
unsigned int v6; // eax

memset(&me, 0, 0x40A0uLL);
*((_QWORD *)&me + 3) = "services.conf";
gettimeofday(&me, 0LL);
signal(13, handler);
while ( 1 )
{
v3 = getopt(argc, (char *const *)argv, "df:");
if ( v3 == -1 )
break;
if ( v3 == 100 )
{
++*((_DWORD *)&me + 4);
}
else if ( v3 == 102 )
{
*((_QWORD *)&me + 3) = lstrdup(optarg);
}
}
if ( (unsigned int)func2() )
err(1, "Unable to open %s", *((const char **)&me + 3));
if ( *((_DWORD *)&me + 4) )
{
v6 = getpid();
func0(6, "running in the foreground (pid %d)", v6);
func0(5, "running in debug mode (level %d)", *((unsigned int *)&me + 4));
goto LABEL_11;
}
if ( !fork() )
{
v4 = getpid();
func0(5, "running in the background (pid %d)", v4);
LABEL_11:
io_loop();
}
return 0;
}

original_code:
int
main(int argc, char **argv)
{
 int i;
 memset(&me, 0, sizeof(me));
 me.conffile = "services.conf";
 gettimeofday(&me.now, ((void *)0));
 signal(13, ((__sighandler_t) 1));
 while ((i = getopt(argc, argv, "df:")) != -1) {
  switch (i) {
   case 'd':
    me.debug++;
    break;
   case 'f':
    me.conffile = lstrdup(optarg);
    break;
  }
 }
 if (parse_conf() != 0)
  err(1, "Unable to open %s", me.conffile);
 if (me.debug) {
  logger(6, "running in the foreground (pid %d)", getpid());
  logger(5, "running in debug mode (level %d)", me.debug);
 } else {
  if (fork() != 0)
   return 0;
  logger(5, "running in the background (pid %d)", getpid());
 }
 io_loop();
 return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
 int i;
 memset(&me, 0, sizeof(me));
 me.conffile = "services.conf";
 gettimeofday(&me.now, ((void *)0));
 signal(13, ((void (*)(int)) 1));
 while ((i = getopt(argc, argv, "df:")) != -1) {
  switch (i) {
   case 'd':
    me.debug++;
    break;
   case 'f':
    me.conffile = lstrdup(optarg);
    break;
  }
 }
 if (parse_conf() != 0)
  err(1, "Unable to open %s", me.conffile);
 if (me.debug) {
  logger(6, "running in the foreground (pid %d)", getpid());
  logger(5, "running in debug mode (level %d)", me.debug);
 } else {
  if (fork() != 0)
   return 0;
  logger(5, "running in the background (pid %d)", getpid());
 }
 io_loop();
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: i int


decompiled_code:
__int64 __fastcall sc_help(__int64 a1, __int64 a2)
{
sreply(a1, *(_QWORD *)(a2 + 24), "Available commands:");
sreply((unsigned int)a1, *(_QWORD *)(a2 + 24), " ");
if ( (_DWORD)a1 == 2 )
{
if ( (*(_BYTE *)(a2 + 64) & 0x40) != 0 )
{
sreply(2LL, *(_QWORD *)(a2 + 24), byte_848);
sreply(2LL, *(_QWORD *)(a2 + 24), " ");
}
sreply(2LL, *(_QWORD *)(a2 + 24), byte_888);
sreply(2LL, *(_QWORD *)(a2 + 24), byte_8B8);
sreply(2LL, *(_QWORD *)(a2 + 24), byte_8E8);
sreply(2LL, *(_QWORD *)(a2 + 24), byte_918);
sreply(2LL, *(_QWORD *)(a2 + 24), byte_948);
sreply(2LL, *(_QWORD *)(a2 + 24), byte_980);
}
else
{
sreply((unsigned int)a1, *(_QWORD *)(a2 + 24), "(no commands available at this time)");
}
sreply((unsigned int)a1, *(_QWORD *)(a2 + 24), " ");
sreply(
(unsigned int)a1,
*(_QWORD *)(a2 + 24),
"All commands sent to %s are logged. Have a nice day!",
*((const char **)&sclients + 4 * (int)a1));
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
sreply(a1, *(_QWORD *)(a2 + 24), "Available commands:");
sreply((unsigned int)a1, *(_QWORD *)(a2 + 24), " ");
if ( (_DWORD)a1 == 2 )
{
if ( (*(_BYTE *)(a2 + 64) & 0x40) != 0 )
{
sreply(2LL, *(_QWORD *)(a2 + 24), byte_848);
sreply(2LL, *(_QWORD *)(a2 + 24), " ");
}
sreply(2LL, *(_QWORD *)(a2 + 24), byte_888);
sreply(2LL, *(_QWORD *)(a2 + 24), byte_8B8);
sreply(2LL, *(_QWORD *)(a2 + 24), byte_8E8);
sreply(2LL, *(_QWORD *)(a2 + 24), byte_918);
sreply(2LL, *(_QWORD *)(a2 + 24), byte_948);
sreply(2LL, *(_QWORD *)(a2 + 24), byte_980);
}
else
{
sreply((unsigned int)a1, *(_QWORD *)(a2 + 24), "(no commands available at this time)");
}
sreply((unsigned int)a1, *(_QWORD *)(a2 + 24), " ");
sreply(
(unsigned int)a1,
*(_QWORD *)(a2 + 24),
"All commands sent to %s are logged. Have a nice day!",
*((const char **)&sclients + 4 * (int)a1));
return 1LL;
}

original_code:
int
sc_help(int sc, client *cptr, char *args)
{
 sreply(sc, cptr->nick, "Available commands:");
 sreply(sc, cptr->nick, " ");
 switch (sc) {
  case 2:
  if ((cptr->flags & 0x00000040)) {
   sreply(sc, cptr->nick, "\2JUPE\2       Remove a compromised server from the network");
   sreply(sc, cptr->nick, " ");
  }
   sreply(sc, cptr->nick, "\2STATUS\2     Status of the network and services");
   sreply(sc, cptr->nick, "\2OPME\2       Gain operator status in a channel");
   sreply(sc, cptr->nick, "\2GLINE\2      Add a tkline to all client servers");
   sreply(sc, cptr->nick, "\2UNGLINE\2    Remove a previously added GLINE");
   sreply(sc, cptr->nick, "\2LONERS\2     List clients on the network in 0 channels");
   sreply(sc, cptr->nick, "\2CLONES\2     List clients connecting from the same host");
   break;
  default:
   sreply(sc, cptr->nick, "(no commands available at this time)");
   break;
 }
 sreply(sc, cptr->nick, " ");
 sreply(sc, cptr->nick, "All commands sent to %s are logged. Have a nice day!", sclients[sc].nick);
 return 1;
}

canonical_original_code:
int sc_help(int sc, struct _client *cptr, char *args)
{
 sreply(sc, cptr->nick, "Available commands:");
 sreply(sc, cptr->nick, " ");
 switch (sc) {
  case 2:
  if ((cptr->flags & 0x00000040)) {
   sreply(sc, cptr->nick, "\2JUPE\2       Remove a compromised server from the network");
   sreply(sc, cptr->nick, " ");
  }
   sreply(sc, cptr->nick, "\2STATUS\2     Status of the network and services");
   sreply(sc, cptr->nick, "\2OPME\2       Gain operator status in a channel");
   sreply(sc, cptr->nick, "\2GLINE\2      Add a tkline to all client servers");
   sreply(sc, cptr->nick, "\2UNGLINE\2    Remove a previously added GLINE");
   sreply(sc, cptr->nick, "\2LONERS\2     List clients on the network in 0 channels");
   sreply(sc, cptr->nick, "\2CLONES\2     List clients connecting from the same host");
   break;
  default:
   sreply(sc, cptr->nick, "(no commands available at this time)");
   break;
 }
 sreply(sc, cptr->nick, " ");
 sreply(sc, cptr->nick, "All commands sent to %s are logged. Have a nice day!", sclients[sc].nick);
 return 1;
}

variable_types: sc int

variable_types: struct _client { struct _client *next; struct _client *prev; struct _client *server; char *nick; char *user; char *host; char *name; long ts; int flags; int channelcount; } *

variable_types:
args
char *


decompiled_code:
__int64 __fastcall os_raw(__int64 a1, __int64 a2, const char *a3)
{
if ( !a3 )
return 1LL;
if ( strlen(a3) > 1 )
sendhub("%s\r\n", a3);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, const char *a3)
{
if ( !a3 )
return 1LL;
if ( strlen(a3) > 1 )
sendhub("%s\r\n", a3);
return 1LL;
}

original_code:
int
os_raw(int sc, client *cptr, char *args)
{
 if (args && strlen(args) > 1)
  sendhub("%s\r\n", args);
 return 1;
}

canonical_original_code:
int os_raw(int sc, struct _client *cptr, char *args)
{
 if (args && strlen(args) > 1)
  sendhub("%s\r\n", args);
 return 1;
}

user_defined_types: struct _client { struct _client *next; struct _client *prev; struct _client *server; char *nick; char *user; char *host; char *name; long ts; int flags; int channelcount; }


decompiled_code:
__int64 __fastcall os_opme(unsigned int a1, __int64 a2, _BYTE *a3)
{
__int64 v5; // rax
__int64 v6; // r13
__int64 v7; // rax

if ( !a3 || !*a3 )
return sreply(a1, *(_QWORD *)(a2 + 24), "Usage: /msg %s OPME channel", sclients[4 * (int)a1]);
v5 = channel_list_search(a3);
v6 = v5;
if ( !v5 )
return sreply(a1, *(_QWORD *)(a2 + 24), "Channel %s does not exist.", a3);
v7 = channel_member_search(v5, a2);
if ( !v7 )
return sreply(a1, *(_QWORD *)(a2 + 24), "You are not in %s", a3);
*(_DWORD *)(v7 + 24) |= 1u;
sendhub(":%s MODE %s +o %s\r\n", *((const char **)&me + 4), *(const char **)(v6 + 16), *(const char **)(a2 + 24));
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1, __int64 a2, _BYTE *a3)
{
__int64 v5; // rax
__int64 v6; // r13
__int64 v7; // rax

if ( !a3 || !*a3 )
return sreply(a1, *(_QWORD *)(a2 + 24), "Usage: /msg %s OPME channel", sclients[4 * (int)a1]);
v5 = channel_list_search(a3);
v6 = v5;
if ( !v5 )
return sreply(a1, *(_QWORD *)(a2 + 24), "Channel %s does not exist.", a3);
v7 = channel_member_search(v5, a2);
if ( !v7 )
return sreply(a1, *(_QWORD *)(a2 + 24), "You are not in %s", a3);
*(_DWORD *)(v7 + 24) |= 1u;
sendhub(":%s MODE %s +o %s\r\n", *((const char **)&me + 4), *(const char **)(v6 + 16), *(const char **)(a2 + 24));
return 1LL;
}

original_code:
int
os_opme(int sc, client *cptr, char *args)
{
 channel *chan;
 cmember *member;
 if (!args || strlen(args) < 1)
  return sreply(sc, cptr->nick, "Usage: /msg %s OPME channel", sclients[sc].nick);
 if (!(chan = channel_list_search(args)))
  return sreply(sc, cptr->nick, "Channel %s does not exist.", args);
 if (!(member = channel_member_search(chan, cptr)))
  return sreply(sc, cptr->nick, "You are not in %s", args);
 member->flags = ((member->flags) |= (0x00000001));
 sendhub(":%s MODE %s +o %s\r\n", me.servername, chan->name, cptr->nick);
 return 1;
}

canonical_original_code:
int os_opme(int sc, struct _client *cptr, char *args)
{
 struct _channel *chan;
 struct _cmember *member;
 if (!args || strlen(args) < 1)
  return sreply(sc, cptr->nick, "Usage: /msg %s OPME channel", sclients[sc].nick);
 if (!(chan = channel_list_search(args)))
  return sreply(sc, cptr->nick, "Channel %s does not exist.", args);
 if (!(member = channel_member_search(chan, cptr)))
  return sreply(sc, cptr->nick, "You are not in %s", args);
 member->flags = ((member->flags) |= (0x00000001));
 sendhub(":%s MODE %s +o %s\r\n", me.servername, chan->name, cptr->nick);
 return 1;
}

variable_types: struct _channel { struct _channel *next; struct _channel *prev; char *name; long ts; struct _cmember { struct _cmember *next; struct _cmember *prev; struct _client { struct _client *next; struct _client *prev; struct _client *server; char *nick; char *user; char *host; char *name; long ts; int flags; int channelcount; } *user; int flags; } *members; int flags; char *topic; char *key; int memberlimit; int membercount; } *

user_defined_types: struct _channel { struct _channel *next; struct _channel *prev; char *name; long ts; struct _cmember *members; int flags; char *topic; char *key; int memberlimit; int membercount; }

user_defined_types: struct _cmember { struct _cmember *next; struct _cmember *prev; struct _client *user; int flags; }


decompiled_code:
__int64 __fastcall os_gline(unsigned int a1, __int64 a2, char *a3)
{
char *v5; // r13
char *v6; // r14
char *v7; // r8

if ( !a3 )
return sreply(
a1,
*(_QWORD *)(a2 + 24),
"Usage: /msg %s GLINE *@*.host.mask reason",
(const char *)sclients[4 * (int)a1]);
if ( strlen(a3) <= 2 )
return sreply(
a1,
*(_QWORD *)(a2 + 24),
"Usage: /msg %s GLINE *@*.host.mask reason",
(const char *)sclients[4 * (int)a1]);
v5 = strtok(a3, "@");
if ( !v5 )
return sreply(
a1,
*(_QWORD *)(a2 + 24),
"Usage: /msg %s GLINE *@*.host.mask reason",
(const char *)sclients[4 * (int)a1]);
v6 = strtok(0LL, " ");
if ( !v6 )
return sreply(
a1,
*(_QWORD *)(a2 + 24),
"Usage: /msg %s GLINE *@*.host.mask reason",
(const char *)sclients[4 * (int)a1]);
v7 = strtok(0LL, &byte_79B);
if ( !v7 )
return sreply(
a1,
*(_QWORD *)(a2 + 24),
"Usage: /msg %s GLINE *@*.host.mask reason",
(const char *)sclients[4 * (int)a1]);
sendhub(":%s ENCAP * KLINE 864000 %s %s :%s\r\n", (const char *)sclients[4], v5, v6, v7);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1, __int64 a2, char *a3)
{
char *v5; // r13
char *v6; // r14
char *v7; // r8

if ( !a3 )
return sreply(
a1,
*(_QWORD *)(a2 + 24),
"Usage: /msg %s GLINE *@*.host.mask reason",
(const char *)sclients[4 * (int)a1]);
if ( strlen(a3) <= 2 )
return sreply(
a1,
*(_QWORD *)(a2 + 24),
"Usage: /msg %s GLINE *@*.host.mask reason",
(const char *)sclients[4 * (int)a1]);
v5 = strtok(a3, "@");
if ( !v5 )
return sreply(
a1,
*(_QWORD *)(a2 + 24),
"Usage: /msg %s GLINE *@*.host.mask reason",
(const char *)sclients[4 * (int)a1]);
v6 = strtok(0LL, " ");
if ( !v6 )
return sreply(
a1,
*(_QWORD *)(a2 + 24),
"Usage: /msg %s GLINE *@*.host.mask reason",
(const char *)sclients[4 * (int)a1]);
v7 = strtok(0LL, &byte_79B);
if ( !v7 )
return sreply(
a1,
*(_QWORD *)(a2 + 24),
"Usage: /msg %s GLINE *@*.host.mask reason",
(const char *)sclients[4 * (int)a1]);
sendhub(":%s ENCAP * KLINE 864000 %s %s :%s\r\n", (const char *)sclients[4], v5, v6, v7);
return 1LL;
}

original_code:
int
os_gline(int sc, client *cptr, char *args)
{
 char *um, *hm, *reason;
 if (!args || strlen(args) < 3 || !(um = strtok(args, "@")) || !(hm = strtok(((void *)0), " ")) || !(reason = strtok(((void *)0), "")))
  return sreply(sc, cptr->nick, "Usage: /msg %s GLINE *@*.host.mask reason", sclients[sc].nick);
 sendhub(":%s ENCAP * KLINE 864000 %s %s :%s\r\n", sclients[1].nick, um, hm, reason);
 return 1;
}

canonical_original_code:
int os_gline(int sc, struct _client *cptr, char *args)
{
 char *um; char *hm; char *reason;
 if (!args || strlen(args) < 3 || !(um = strtok(args, "@")) || !(hm = strtok(((void *)0), " ")) || !(reason = strtok(((void *)0), "")))
  return sreply(sc, cptr->nick, "Usage: /msg %s GLINE *@*.host.mask reason", sclients[sc].nick);
 sendhub(":%s ENCAP * KLINE 864000 %s %s :%s\r\n", sclients[1].nick, um, hm, reason);
 return 1;
}

variable_types:
um
char *

variable_types:
hm
char *

variable_types:
reason
char *


decompiled_code:
__int64 __fastcall os_ungline(unsigned int a1, __int64 a2, char *a3)
{
char *v5; // r13
char *v6; // rcx

if ( !a3 )
return sreply(a1, *(_QWORD *)(a2 + 24), "Usage: /msg %s UNGLINE *@*.host.mask", (const char *)sclients[4 * (int)a1]);
if ( strlen(a3) <= 2 )
return sreply(a1, *(_QWORD *)(a2 + 24), "Usage: /msg %s UNGLINE *@*.host.mask", (const char *)sclients[4 * (int)a1]);
v5 = strtok(a3, "@");
if ( !v5 )
return sreply(a1, *(_QWORD *)(a2 + 24), "Usage: /msg %s UNGLINE *@*.host.mask", (const char *)sclients[4 * (int)a1]);
v6 = strtok(0LL, " ");
if ( !v6 )
return sreply(a1, *(_QWORD *)(a2 + 24), "Usage: /msg %s UNGLINE *@*.host.mask", (const char *)sclients[4 * (int)a1]);
sendhub(":%s ENCAP * UNKLINE %s %s\r\n", (const char *)sclients[4], v5, v6);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1, __int64 a2, char *a3)
{
char *v5; // r13
char *v6; // rcx

if ( !a3 )
return sreply(a1, *(_QWORD *)(a2 + 24), "Usage: /msg %s UNGLINE *@*.host.mask", (const char *)sclients[4 * (int)a1]);
if ( strlen(a3) <= 2 )
return sreply(a1, *(_QWORD *)(a2 + 24), "Usage: /msg %s UNGLINE *@*.host.mask", (const char *)sclients[4 * (int)a1]);
v5 = strtok(a3, "@");
if ( !v5 )
return sreply(a1, *(_QWORD *)(a2 + 24), "Usage: /msg %s UNGLINE *@*.host.mask", (const char *)sclients[4 * (int)a1]);
v6 = strtok(0LL, " ");
if ( !v6 )
return sreply(a1, *(_QWORD *)(a2 + 24), "Usage: /msg %s UNGLINE *@*.host.mask", (const char *)sclients[4 * (int)a1]);
sendhub(":%s ENCAP * UNKLINE %s %s\r\n", (const char *)sclients[4], v5, v6);
return 1LL;
}

original_code:
int
os_ungline(int sc, client *cptr, char *args)
{
 char *um, *hm;
 if (!args || strlen(args) < 3 || !(um = strtok(args, "@")) || !(hm = strtok(((void *)0), " ")))
  return sreply(sc, cptr->nick, "Usage: /msg %s UNGLINE *@*.host.mask", sclients[sc].nick);
 sendhub(":%s ENCAP * UNKLINE %s %s\r\n", sclients[1].nick, um, hm);
 return 1;
}

canonical_original_code:
int os_ungline(int sc, struct _client *cptr, char *args)
{
 char *um; char *hm;
 if (!args || strlen(args) < 3 || !(um = strtok(args, "@")) || !(hm = strtok(((void *)0), " ")))
  return sreply(sc, cptr->nick, "Usage: /msg %s UNGLINE *@*.host.mask", sclients[sc].nick);
 sendhub(":%s ENCAP * UNKLINE %s %s\r\n", sclients[1].nick, um, hm);
 return 1;
}


decompiled_code:
__int64 __fastcall os_status(__int64 a1, __int64 a2)
{
sreply(a1, *(_QWORD *)(a2 + 24), "Network Status:");
sreply((unsigned int)a1, *(_QWORD *)(a2 + 24), " ");
sreply((unsigned int)a1, *(_QWORD *)(a2 + 24), "Clients: %lu", me[2065]);
sreply((unsigned int)a1, *(_QWORD *)(a2 + 24), "Servers: %lu", me[2064]);
sreply((unsigned int)a1, *(_QWORD *)(a2 + 24), "Channels: %lu", me[2066]);
sreply((unsigned int)a1, *(_QWORD *)(a2 + 24), "Snuggles: %lu", me[2067]);
sreply((unsigned int)a1, *(_QWORD *)(a2 + 24), "Memory Usage: %lu", me[2063]);
sreply((unsigned int)a1, *(_QWORD *)(a2 + 24), " ");
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2)
{
sreply(a1, *(_QWORD *)(a2 + 24), "Network Status:");
sreply((unsigned int)a1, *(_QWORD *)(a2 + 24), " ");
sreply((unsigned int)a1, *(_QWORD *)(a2 + 24), "Clients: %lu", me[2065]);
sreply((unsigned int)a1, *(_QWORD *)(a2 + 24), "Servers: %lu", me[2064]);
sreply((unsigned int)a1, *(_QWORD *)(a2 + 24), "Channels: %lu", me[2066]);
sreply((unsigned int)a1, *(_QWORD *)(a2 + 24), "Snuggles: %lu", me[2067]);
sreply((unsigned int)a1, *(_QWORD *)(a2 + 24), "Memory Usage: %lu", me[2063]);
sreply((unsigned int)a1, *(_QWORD *)(a2 + 24), " ");
return 1LL;
}

original_code:
int
os_status(int sc, client *cptr, char *args)
{
 sreply(sc, cptr->nick, "Network Status:");
 sreply(sc, cptr->nick, " ");
 sreply(sc, cptr->nick, "Clients: %lu", me.clients);
 sreply(sc, cptr->nick, "Servers: %lu", me.servers);
 sreply(sc, cptr->nick, "Channels: %lu", me.channels);
 sreply(sc, cptr->nick, "Snuggles: %lu", me.snuggles);
 sreply(sc, cptr->nick, "Memory Usage: %lu", me.memusage);
 sreply(sc, cptr->nick, " ");
 return 1;
}

canonical_original_code:
int os_status(int sc, struct _client *cptr, char *args)
{
 sreply(sc, cptr->nick, "Network Status:");
 sreply(sc, cptr->nick, " ");
 sreply(sc, cptr->nick, "Clients: %lu", me.clients);
 sreply(sc, cptr->nick, "Servers: %lu", me.servers);
 sreply(sc, cptr->nick, "Channels: %lu", me.channels);
 sreply(sc, cptr->nick, "Snuggles: %lu", me.snuggles);
 sreply(sc, cptr->nick, "Memory Usage: %lu", me.memusage);
 sreply(sc, cptr->nick, " ");
 return 1;
}


decompiled_code:
__int64 __fastcall os_loners(__int64 a1, __int64 a2)
{
unsigned int v2; // er12
__int64 **v3; // rbx

v2 = 0;
v3 = (__int64 **)client_list;
sreply(a1, *(_QWORD *)(a2 + 24), "The following users are not in any channels:");
for ( ; v3; v3 = (__int64 **)*v3 )
{
while ( ((unsigned __int64)v3[8] & 0xFFFFFFFF00000010LL) != 16 )
{
v3 = (__int64 **)*v3;
if ( !v3 )
goto LABEL_6;
}
sreply(
(unsigned int)a1,
*(_QWORD *)(a2 + 24),
"%d) [%s] %s!%s@%s$%s",
++v2,
(const char *)v3[2][3],
(const char *)v3[3],
(const char *)v3[4],
(const char *)v3[5],
(const char *)v3[6]);
}
LABEL_6:
sreply((unsigned int)a1, *(_QWORD *)(a2 + 24), "Total of %d loners on the network.", v2);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2)
{
unsigned int v2; // er12
__int64 **v3; // rbx

v2 = 0;
v3 = (__int64 **)client_list;
sreply(a1, *(_QWORD *)(a2 + 24), "The following users are not in any channels:");
for ( ; v3; v3 = (__int64 **)*v3 )
{
while ( ((unsigned __int64)v3[8] & 0xFFFFFFFF00000010LL) != 16 )
{
v3 = (__int64 **)*v3;
if ( !v3 )
goto LABEL_6;
}
sreply(
(unsigned int)a1,
*(_QWORD *)(a2 + 24),
"%d) [%s] %s!%s@%s$%s",
++v2,
(const char *)v3[2][3],
(const char *)v3[3],
(const char *)v3[4],
(const char *)v3[5],
(const char *)v3[6]);
}
LABEL_6:
sreply((unsigned int)a1, *(_QWORD *)(a2 + 24), "Total of %d loners on the network.", v2);
return 1LL;
}

original_code:
int
os_loners(int sc, client *cptr, char *args)
{
 client *c = client_list;
 int i = 0;
 sreply(sc, cptr->nick, "The following users are not in any channels:");
 for (; c; c = c->next)
  if ((c->flags & 0x00000010) && c->channelcount == 0)
   sreply(sc, cptr->nick, "%d) [%s] %s!%s@%s$%s", ++i, c->server->nick, c->nick, c->user, c->host, c->name);
 sreply(sc, cptr->nick, "Total of %d loners on the network.", i);
 return 1;
}

canonical_original_code:
int os_loners(int sc, struct _client *cptr, char *args)
{
 struct _client *c = client_list;
 int i = 0;
 sreply(sc, cptr->nick, "The following users are not in any channels:");
 for (; c; c = c->next)
  if ((c->flags & 0x00000010) && c->channelcount == 0)
   sreply(sc, cptr->nick, "%d) [%s] %s!%s@%s$%s", ++i, c->server->nick, c->nick, c->user, c->host, c->name);
 sreply(sc, cptr->nick, "Total of %d loners on the network.", i);
 return 1;
}


decompiled_code:
__int64 __fastcall os_who(unsigned int a1, __int64 a2, char *a3)
{
__int64 v3; // rbx
char *v4; // r12
char *v5; // r13
char *v6; // r14
int v7; // eax
__int64 v8; // rbx
__int64 v9; // r15
char *v10; // rax
__int64 v11; // rcx
char *v13; // [rsp+8h] [rbp-690h]
unsigned int v14; // [rsp+14h] [rbp-684h]
char *v15; // [rsp+18h] [rbp-680h]
char v16[1656]; // [rsp+20h] [rbp-678h] BYREF

v3 = a2;
if ( !a3 )
return sreply(
a1,
*(_QWORD *)(a2 + 24),
"Usage: /msg %s WHO mask!mask@mask$mask",
(const char *)sclients[4 * (int)a1]);
v4 = strtok(a3, "!");
if ( !v4 )
return sreply(
a1,
*(_QWORD *)(a2 + 24),
"Usage: /msg %s WHO mask!mask@mask$mask",
(const char *)sclients[4 * (int)a1]);
v5 = strtok(0LL, "@");
if ( !v5 )
return sreply(
a1,
*(_QWORD *)(a2 + 24),
"Usage: /msg %s WHO mask!mask@mask$mask",
(const char *)sclients[4 * (int)a1]);
v6 = strtok(0LL, "$");
if ( !v6 )
return sreply(
a1,
*(_QWORD *)(a2 + 24),
"Usage: /msg %s WHO mask!mask@mask$mask",
(const char *)sclients[4 * (int)a1]);
v13 = strtok(0LL, &byte_79B);
if ( !v13 )
return sreply(
a1,
*(_QWORD *)(a2 + 24),
"Usage: /msg %s WHO mask!mask@mask$mask",
(const char *)sclients[4 * (int)a1]);
v7 = user_list_search(v16, 200LL, v4, v5, v6, v13);
v14 = v7;
if ( !v7 )
return sreply(a1, *(_QWORD *)(a2 + 24), "No users found: %s!%s@%s$%s", v4, v5, v6, v13);
if ( v7 > 0 )
{
v15 = v4;
v8 = 1LL;
v9 = (unsigned int)(v7 + 1);
do
{
v10 = (&v15)[v8];
v11 = (unsigned int)v8++;
sreply(
a1,
*(_QWORD *)(a2 + 24),
"%d) [%s] %s!%s@%s$%s (%d channels)",
v11,
*(const char **)(*((_QWORD *)v10 + 2) + 24LL),
*((const char **)v10 + 3),
*((const char **)v10 + 4),
*((const char **)v10 + 5),
*((const char **)v10 + 6),
*((unsigned int *)v10 + 17));
}
while ( v9 != v8 );
v3 = a2;
v4 = v15;
}
return sreply(
a1,
*(_QWORD *)(v3 + 24),
"A total of %d users matched your search (%s!%s@%s$%s).",
v14,
v4,
v5,
v6,
v13);
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int a1, __int64 a2, char *a3)
{
__int64 v3; // rbx
char *v4; // r12
char *v5; // r13
char *v6; // r14
int v7; // eax
__int64 v8; // rbx
__int64 v9; // r15
char *v10; // rax
__int64 v11; // rcx
char *v13; // [rsp+8h] [rbp-690h]
unsigned int v14; // [rsp+14h] [rbp-684h]
char *v15; // [rsp+18h] [rbp-680h]
char v16[1656]; // [rsp+20h] [rbp-678h] BYREF

v3 = a2;
if ( !a3 )
return sreply(
a1,
*(_QWORD *)(a2 + 24),
"Usage: /msg %s WHO mask!mask@mask$mask",
(const char *)sclients[4 * (int)a1]);
v4 = strtok(a3, "!");
if ( !v4 )
return sreply(
a1,
*(_QWORD *)(a2 + 24),
"Usage: /msg %s WHO mask!mask@mask$mask",
(const char *)sclients[4 * (int)a1]);
v5 = strtok(0LL, "@");
if ( !v5 )
return sreply(
a1,
*(_QWORD *)(a2 + 24),
"Usage: /msg %s WHO mask!mask@mask$mask",
(const char *)sclients[4 * (int)a1]);
v6 = strtok(0LL, "$");
if ( !v6 )
return sreply(
a1,
*(_QWORD *)(a2 + 24),
"Usage: /msg %s WHO mask!mask@mask$mask",
(const char *)sclients[4 * (int)a1]);
v13 = strtok(0LL, &byte_79B);
if ( !v13 )
return sreply(
a1,
*(_QWORD *)(a2 + 24),
"Usage: /msg %s WHO mask!mask@mask$mask",
(const char *)sclients[4 * (int)a1]);
v7 = user_list_search(v16, 200LL, v4, v5, v6, v13);
v14 = v7;
if ( !v7 )
return sreply(a1, *(_QWORD *)(a2 + 24), "No users found: %s!%s@%s$%s", v4, v5, v6, v13);
if ( v7 > 0 )
{
v15 = v4;
v8 = 1LL;
v9 = (unsigned int)(v7 + 1);
do
{
v10 = (&v15)[v8];
v11 = (unsigned int)v8++;
sreply(
a1,
*(_QWORD *)(a2 + 24),
"%d) [%s] %s!%s@%s$%s (%d channels)",
v11,
*(const char **)(*((_QWORD *)v10 + 2) + 24LL),
*((const char **)v10 + 3),
*((const char **)v10 + 4),
*((const char **)v10 + 5),
*((const char **)v10 + 6),
*((unsigned int *)v10 + 17));
}
while ( v9 != v8 );
v3 = a2;
v4 = v15;
}
return sreply(
a1,
*(_QWORD *)(v3 + 24),
"A total of %d users matched your search (%s!%s@%s$%s).",
v14,
v4,
v5,
v6,
v13);
}

original_code:
int
os_who(int sc, client *cptr, char *args)
{
 client *results[200];
 char *nm, *um, *hm, *gm;
 int i, r;
 if (!args || !(nm = strtok(args, "!")) || !(um = strtok(((void *)0), "@")) || !(hm = strtok(((void *)0), "$")) || !(gm = strtok(((void *)0), "")))
  return sreply(sc, cptr->nick, "Usage: /msg %s WHO mask!mask@mask$mask", sclients[sc].nick);
 if ((r = user_list_search(results, 200, nm, um, hm, gm)) == 0)
  return sreply(sc, cptr->nick, "No users found: %s!%s@%s$%s", nm, um, hm, gm);
 for (i = 0; i < r; i++) {
  sreply(sc, cptr->nick, "%d) [%s] %s!%s@%s$%s (%d channels)", i + 1, results[i]->server->nick, results[i]->nick, results[i]->user, results[i]->host, results[i]->name, results[i]->channelcount);
 }
 return sreply(sc, cptr->nick, "A total of %d users matched your search (%s!%s@%s$%s).", r, nm, um, hm, gm);
}

canonical_original_code:
int os_who(int sc, struct _client *cptr, char *args)
{
 struct _client *results[200];
 char *nm; char *um; char *hm; char *gm;
 int i; int r;
 if (!args || !(nm = strtok(args, "!")) || !(um = strtok(((void *)0), "@")) || !(hm = strtok(((void *)0), "$")) || !(gm = strtok(((void *)0), "")))
  return sreply(sc, cptr->nick, "Usage: /msg %s WHO mask!mask@mask$mask", sclients[sc].nick);
 if ((r = user_list_search(results, 200, nm, um, hm, gm)) == 0)
  return sreply(sc, cptr->nick, "No users found: %s!%s@%s$%s", nm, um, hm, gm);
 for (i = 0; i < r; i++) {
  sreply(sc, cptr->nick, "%d) [%s] %s!%s@%s$%s (%d channels)", i + 1, results[i]->server->nick, results[i]->nick, results[i]->user, results[i]->host, results[i]->name, results[i]->channelcount);
 }
 return sreply(sc, cptr->nick, "A total of %d users matched your search (%s!%s@%s$%s).", r, nm, um, hm, gm);
}

variable_types: struct _client { struct _client *next; struct _client *prev; struct _client *server; char *nick; char *user; char *host; char *name; long ts; int flags; int channelcount; } *[200]

variable_types:
nm
char *

variable_types:
gm
char *

variable_types: r int