decompiled_code:
__int64 __fastcall inform_disconn(__int64 *a1, __int16 a2)
{
__int64 result; // rax

send_net7(3, a1, (__int64)&manage_fls, a2);
result = recv_netpt(1);
if ( (_DWORD)result == 1 )
{
ntohpt8((__int64)&readbuf);
result = netpt8_check(&readbuf);
if ( !(_DWORD)result )
flsc_error = 15;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func16(__int64 *a1, __int16 a2)
{
__int64 result; // rax

func13(3, a1, (__int64)&manage_fls, a2);
result = func9(1);
if ( (_DWORD)result == 1 )
{
func15((__int64)&readbuf);
result = func4(&readbuf);
if ( !(_DWORD)result )
flsc_error = 15;
}
return result;
}

original_code:
static void inform_disconn(flsunit * unit, uint16_t sid)
{
 send_net7(3, &(unit->server), &(manage_fls), sid);
 int stat = recv_netpt(1);
 if (stat == 1)
 {
  netpt8 *recv = (netpt8 *)readbuf;
  ntohpt8(recv);
  if (!netpt8_check(recv))
  {
   flsc_error = 15;
   return;
  }
  if (recv->head.subcmd == 8 && recv->body.op == 3)
   ;
 }
 return;
}

canonical_original_code:
static void inform_disconn(struct flsunit *unit, unsigned short sid)
{
 send_net7(3, &(unit->server), &(manage_fls), sid);
 int stat = recv_netpt(1);
 if (stat == 1)
 {
  struct netpt8 *recv = (netpt8 *)readbuf;
  ntohpt8(recv);
  if (!netpt8_check(recv))
  {
   flsc_error = 15;
   return;
  }
  if (recv->head.subcmd == 8 && recv->body.op == 3)
   ;
 }
 return;
}

variable_types: struct flsunit { struct addr { unsigned char id; unsigned char stat; unsigned short port; unsigned int ip; } server; int delaycount; int delaytime; long delaystart; } *

variable_types: stat int

variable_types: struct netpt8 { struct phead { char psf; char zero1; unsigned short len; unsigned short subcmd; unsigned short version; unsigned int uid; unsigned short fls_id; unsigned short fls_num; unsigned int client_ip; unsigned short client_port; char zero2[2]; char reserve[40]; } head; struct pbody8 { unsigned char stat; unsigned char op; char zero[2]; } body; char zero[3]; char pef; } *

user_defined_types: struct netpt8 { struct phead head; struct pbody8 body; char zero[3]; char pef; }


repo: haipome/fls

original_code:
static void htonpt1(netpt1 *ptr)
{
 htonpch(&(ptr->head));
 ptr->body.uid = htonl(ptr->body.uid);
 ptr->body.sid = htons(ptr->body.sid);
 ptr->body.num = htons(ptr->body.num);
}

canonical_original_code:
static void htonpt1(struct netpt1 *ptr)
{
 htonpch(&(ptr->head));
 ptr->body.uid = htonl(ptr->body.uid);
 ptr->body.sid = htons(ptr->body.sid);
 ptr->body.num = htons(ptr->body.num);
}

variable_types: struct netpt1 { struct phead { char psf; char zero1; unsigned short len; unsigned short subcmd; unsigned short version; unsigned int uid; unsigned short fls_id; unsigned short fls_num; unsigned int client_ip; unsigned short client_port; char zero2[2]; char reserve[40]; } head; struct pbody1 { unsigned int uid; unsigned short sid; unsigned short num; } body; char zero[3]; char pef; } *

return_type: void

user_defined_types: struct netpt1 { struct phead head; struct pbody1 body; char zero[3]; char pef; }


decompiled_code:
__int16 __fastcall htonpt3(__int64 a1)
{
__int16 result; // ax

htonpch(a1);
*(_DWORD *)(a1 + 64) = _bswap_32(*(_DWORD *)(a1 + 64));
result = _bswap_16(*(_WORD *)(a1 + 68));
*(_WORD *)(a1 + 68) = result;
return result;
}

canonical_decompiled_code:
__int16 __fastcall func21(__int64 a1)
{
__int16 result; // ax

func11(a1);
*(_DWORD *)(a1 + 64) = func8(*(_DWORD *)(a1 + 64));
result = func10(*(_WORD *)(a1 + 68));
*(_WORD *)(a1 + 68) = result;
return result;
}

original_code:
static void htonpt3(netpt3 *ptr)
{
 htonpch(&(ptr->head));
 ptr->body.uid = htonl(ptr->body.uid);
 ptr->body.sid = htons(ptr->body.sid);
}

canonical_original_code:
static void htonpt3(struct netpt3 *ptr)
{
 htonpch(&(ptr->head));
 ptr->body.uid = htonl(ptr->body.uid);
 ptr->body.sid = htons(ptr->body.sid);
}

variable_types: struct netpt3 { struct phead { char psf; char zero1; unsigned short len; unsigned short subcmd; unsigned short version; unsigned int uid; unsigned short fls_id; unsigned short fls_num; unsigned int client_ip; unsigned short client_port; char zero2[2]; char reserve[40]; } head; struct pbody3 { unsigned int uid; unsigned short sid; char zero[2]; } body; char zero[3]; char pef; } *

user_defined_types: struct netpt3 { struct phead head; struct pbody3 body; char zero[3]; char pef; }


decompiled_code:
__int64 __fastcall flsc_init(__int64 a1, __int64 a2, __int64 a3)
{
__int64 result; // rax
int v4; // er12

result = (unsigned int)client_stat;
if ( client_stat != 1 )
{
dword_2728 = 1;
rflsl = 0LL;
qword_F48 = 0LL;
qword_F50 = 0LL;
HIDWORD(rflsl) = HIDWORD(a3);
WORD1(rflsl) = WORD1(a3);
manage_fls = 0;
dword_F14 = HIDWORD(a3);
word_F12 = WORD1(a3);
v4 = socket(2, 2, 0);
sd = v4;
if ( v4 < 0 )
{
flsc_error = 12;
result = 0LL;
}
else
{
cliaddr.sa_family = 2;
*(_DWORD *)&cliaddr.sa_data[2] = _bswap_32(0);
*(_WORD *)cliaddr.sa_data = WORD1(a1);
if ( bind(v4, &cliaddr, 0x10u) < 0 )
{
flsc_error = 13;
result = 0LL;
}
else
{
*(_QWORD *)&cliaddr.sa_family = a1;
*(_QWORD *)&cliaddr.sa_data[6] = a2;
client_stat = 1;
result = 1LL;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func24(__int64 a1, __int64 a2, __int64 a3)
{
__int64 result; // rax
int v4; // er12

result = (unsigned int)client_stat;
if ( client_stat != 1 )
{
dword_2728 = 1;
rflsl = 0LL;
qword_F48 = 0LL;
qword_F50 = 0LL;
HIDWORD(rflsl) = HIDWORD(a3);
WORD1(rflsl) = WORD1(a3);
manage_fls = 0;
dword_F14 = HIDWORD(a3);
word_F12 = WORD1(a3);
v4 = socket(2, 2, 0);
sd = v4;
if ( v4 < 0 )
{
flsc_error = 12;
result = 0LL;
}
else
{
cliaddr.sa_family = 2;
*(_DWORD *)&cliaddr.sa_data[2] = func8(0);
*(_WORD *)cliaddr.sa_data = WORD1(a1);
if ( bind(v4, &cliaddr, 0x10u) < 0 )
{
flsc_error = 13;
result = 0LL;
}
else
{
*(_QWORD *)&cliaddr.sa_family = a1;
*(_QWORD *)&cliaddr.sa_data[6] = a2;
client_stat = 1;
result = 1LL;
}
}
}
return result;
}

original_code:
int flsc_init(struct sockaddr_in local, struct sockaddr_in server)
{
 if (client_stat == 1)
  return 1;
 rflsl.len = 1;
 bzero(rflsl.list, sizeof(flsunit));
 rflsl.list[0].server.id = 0;
 rflsl.list[0].server.ip = server.sin_addr.s_addr;
 rflsl.list[0].server.port = server.sin_port;
 manage_fls.id = 0;
 manage_fls.ip = server.sin_addr.s_addr;
 manage_fls.port = server.sin_port;
 if ((sd = socket(2, SOCK_DGRAM, 0)) < 0)
 {
  flsc_error = 12;
  return 0;
 }
 cliaddr.sin_family = 2;
 cliaddr.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
 cliaddr.sin_port = local.sin_port;
 if (bind(sd, (struct sockaddr *) &cliaddr, sizeof(cliaddr)) < 0)
 {
  flsc_error = 13;
  return 0;
 }
 cliaddr = local;
 client_stat = 1;
 return 1;
}

canonical_original_code:
int flsc_init(struct sockaddr_in local, struct sockaddr_in server)
{
 if (client_stat == 1)
  return 1;
 rflsl.len = 1;
 bzero(rflsl.list, sizeof(flsunit));
 rflsl.list[0].server.id = 0;
 rflsl.list[0].server.ip = server.sin_addr.s_addr;
 rflsl.list[0].server.port = server.sin_port;
 manage_fls.id = 0;
 manage_fls.ip = server.sin_addr.s_addr;
 manage_fls.port = server.sin_port;
 if ((sd = socket(2, SOCK_DGRAM, 0)) < 0)
 {
  flsc_error = 12;
  return 0;
 }
 cliaddr.sin_family = 2;
 cliaddr.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
 cliaddr.sin_port = local.sin_port;
 if (bind(sd, (struct sockaddr *) &cliaddr, sizeof(cliaddr)) < 0)
 {
  flsc_error = 13;
  return 0;
 }
 cliaddr = local;
 client_stat = 1;
 return 1;
}

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:
server
struct sockaddr_in

return_type: int

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

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


decompiled_code:
__int64 __fastcall flsc_freq_query(__int16 a1, unsigned int a2, __m128i *a3)
{
unsigned int v3; // ebp
__int64 *v5; // rdx
unsigned int v7; // ebx

v3 = client_stat;
if ( client_stat )
{
v5 = &rflsl[3 * (a2 % dword_2728)];
v3 = *((_DWORD *)v5 + 3);
if ( v3 )
{
v3 = 0;
}
else
{
send_net3(a1, a2, (__int64)v5);
v7 = recv_netpt(a2);
if ( v7 == 1 )
{
ntohpt4((__int64)&readbuf);
if ( (unsigned int)netpt4_check(&readbuf) )
{
if ( byte_B40 )
{
*a3 = _mm_loadu_si128((const __m128i *)&xmmword_B48);
a3[1].m128i_i64[0] = qword_B58;
v3 = v7;
}
}
else
{
flsc_error = 15;
v3 = 0;
}
}
}
}
else
{
flsc_error = 11;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func26(__int16 a1, unsigned int a2, __m128i *a3)
{
unsigned int v3; // ebp
__int64 *v5; // rdx
unsigned int v7; // ebx

v3 = client_stat;
if ( client_stat )
{
v5 = &rflsl[3 * (a2 % dword_2728)];
v3 = *((_DWORD *)v5 + 3);
if ( v3 )
{
v3 = 0;
}
else
{
func22(a1, a2, (__int64)v5);
v7 = func9(a2);
if ( v7 == 1 )
{
func23((__int64)&readbuf);
if ( (unsigned int)func2(&readbuf) )
{
if ( byte_B40 )
{
*a3 = _mm_loadu_si128((const __m128i *)&xmmword_B48);
a3[1].m128i_i64[0] = qword_B58;
v3 = v7;
}
}
else
{
flsc_error = 15;
v3 = 0;
}
}
}
}
else
{
flsc_error = 11;
}
return v3;
}

original_code:
int flsc_freq_query(uint16_t sid, uint32_t uid, struct flsc_freqdata * freqd)
{
 if (client_stat == 0)
 {
  flsc_error = 11;
  return 0;
 }
 flsunit *unit = rflsl.list + uid % rflsl.len;
 if (unit->delaytime)
 {
  return 0;
 }
 send_net3(sid, uid, &(unit->server));
 if (recv_netpt(uid) == 1)
 {
  netpt4 *recv = (netpt4 *)readbuf;
  ntohpt4(recv);
  if (!netpt4_check(recv))
  {
   flsc_error = 15;
   return 0;
  }
  if (((netpt4 *)readbuf)->body.stat)
  {
   *freqd = ((netpt4 *)readbuf)->body.freqd;
   return 1;
  }
  else
  {
   return 0;
  }
 }
 else
 {
  return 0;
 }
 return 0;
}

canonical_original_code:
int flsc_freq_query(unsigned short sid, unsigned int uid, struct flsc_freqdata *freqd)
{
 if (client_stat == 0)
 {
  flsc_error = 11;
  return 0;
 }
 struct flsunit *unit = rflsl.list + uid % rflsl.len;
 if (unit->delaytime)
 {
  return 0;
 }
 send_net3(sid, uid, &(unit->server));
 if (recv_netpt(uid) == 1)
 {
  struct netpt4 *recv = (netpt4 *)readbuf;
  ntohpt4(recv);
  if (!netpt4_check(recv))
  {
   flsc_error = 15;
   return 0;
  }
  if (((struct netpt4 *)readbuf)->body.stat)
  {
   *freqd = ((struct netpt4 *)readbuf)->body.freqd;
   return 1;
  }
  else
  {
   return 0;
  }
 }
 else
 {
  return 0;
 }
 return 0;
}

variable_types:
sid
unsigned short

variable_types:
uid
unsigned int

variable_types: struct flsc_freqdata { unsigned int timel1; unsigned int limdl1; unsigned int counterl1; unsigned int timel2; unsigned int limdl2; unsigned int counterl2; } *

variable_types: struct netpt4 { struct phead { char psf; char zero1; unsigned short len; unsigned short subcmd; unsigned short version; unsigned int uid; unsigned short fls_id; unsigned short fls_num; unsigned int client_ip; unsigned short client_port; char zero2[2]; char reserve[40]; } head; struct pbody4 { unsigned char stat; char zero; unsigned short sid; unsigned int uid; struct flsc_freqdata { unsigned int timel1; unsigned int limdl1; unsigned int counterl1; unsigned int timel2; unsigned int limdl2; unsigned int counterl2; } freqd; } body; char zero[3]; char pef; } *

user_defined_types: struct flsunit { struct addr server; int delaycount; int delaytime; long delaystart; }

user_defined_types: struct netpt4 { struct phead head; struct pbody4 body; char zero[3]; char pef; }

user_defined_types: struct addr { unsigned char id; unsigned char stat; unsigned short port; unsigned int ip; }

user_defined_types: struct pbody4 { unsigned char stat; char zero; unsigned short sid; unsigned int uid; struct flsc_freqdata freqd; }


decompiled_code:
__int64 __fastcall recv_netpt(int a1)
{
int v2; // er12
int v3; // edi
int v4; // eax
unsigned int v5; // er13
struct timeval v7; // [rsp+0h] [rbp-B8h] BYREF
fd_set readfds; // [rsp+10h] [rbp-A8h] BYREF

v2 = 3;
while ( 1 )
{
memset(&readfds, 0, sizeof(readfds));
v3 = sd;
readfds.fds_bits[sd / 64] |= 1LL << (sd % 64);
v7.tv_sec = 0LL;
v7.tv_usec = 10000LL;
v4 = select(v3 + 1, &readfds, 0LL, 0LL, &v7);
v5 = v4;
if ( v4 == -1 )
{
flsc_error = 18;
return v5;
}
if ( !v4 )
{
flsc_error = 14;
return v5;
}
if ( (readfds.fds_bits[sd / 64] & (1LL << (sd % 64))) == 0 )
return v5;
recvfrom(sd, &readbuf, 0x3E8uLL, 0, &servaddr, &len);
if ( (unsigned int)_bswap_32(dword_B08) == a1 )
break;
if ( !--v2 )
return v5;
}
return 1;
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1)
{
int v2; // er12
int v3; // edi
int v4; // eax
unsigned int v5; // er13
struct timeval v7; // [rsp+0h] [rbp-B8h] BYREF
fd_set readfds; // [rsp+10h] [rbp-A8h] BYREF

v2 = 3;
while ( 1 )
{
memset(&readfds, 0, sizeof(readfds));
v3 = sd;
readfds.fds_bits[sd / 64] |= 1LL << (sd % 64);
v7.tv_sec = 0LL;
v7.tv_usec = 10000LL;
v4 = select(v3 + 1, &readfds, 0LL, 0LL, &v7);
v5 = v4;
if ( v4 == -1 )
{
flsc_error = 18;
return v5;
}
if ( !v4 )
{
flsc_error = 14;
return v5;
}
if ( (readfds.fds_bits[sd / 64] & (1LL << (sd % 64))) == 0 )
return v5;
recvfrom(sd, &readbuf, 0x3E8uLL, 0, &servaddr, &len);
if ( (unsigned int)func8(dword_B08) == a1 )
break;
if ( !--v2 )
return v5;
}
return 1;
}

original_code:
static int recv_netpt(uint32_t uid)
{
 fd_set read_set;
 struct timeval timeout;
 int stat;
 int i;
 for (i = 3; i > 0; --i)
 {
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&read_set)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&read_set)->__fds_bits)[((sd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sd) % (8 * (int) sizeof (__fd_mask)))))));
  timeout.tv_sec = 0;
  timeout.tv_usec = 10000;
  stat = select(sd+1, &read_set, ((void *)0), ((void *)0), &timeout);
  if (stat == -1)
  {
   flsc_error = 18;
   return stat;
  }
  else if (stat == 0)
  {
   flsc_error = 14;
   return stat;
  }
  else if (((((&read_set)->__fds_bits)[((sd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((sd) % (8 * (int) sizeof (__fd_mask)))))) != 0))
  {
   recvfrom(sd, (void *)readbuf, 1000, 0, (struct sockaddr *)&servaddr, &len);
   uint32_t id = ntohl(*(uint32_t *)(((char *)readbuf + 8)));
   if (id != uid)
   {
    continue;
   }
   else
   {
    return 1;
   }
  }
  else
  {
   break;
  }
 }
 return stat;
}

canonical_original_code:
static int recv_netpt(unsigned int uid)
{
 struct fd_set read_set;
 struct timeval timeout;
 int stat;
 int i;
 for (i = 3; i > 0; --i)
 {
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&read_set)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&read_set)->__fds_bits)[((sd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sd) % (8 * (int) sizeof (__fd_mask)))))));
  timeout.tv_sec = 0;
  timeout.tv_usec = 10000;
  stat = select(sd+1, &read_set, ((void *)0), ((void *)0), &timeout);
  if (stat == -1)
  {
   flsc_error = 18;
   return stat;
  }
  else if (stat == 0)
  {
   flsc_error = 14;
   return stat;
  }
  else if (((((&read_set)->__fds_bits)[((sd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((sd) % (8 * (int) sizeof (__fd_mask)))))) != 0))
  {
   recvfrom(sd, (void *)readbuf, 1000, 0, (struct sockaddr *)&servaddr, &len);
   unsigned int id = ntohl(*(uint32_t *)(((char *)readbuf + 8)));
   if (id != uid)
   {
    continue;
   }
   else
   {
    return 1;
   }
  }
  else
  {
   break;
  }
 }
 return stat;
}

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

variable_types: i int

variable_types:
id
unsigned int


decompiled_code:
__int64 __fastcall is_prime_num(int a1)
{
double v1; // xmm0_8
double v2; // xmm0_8
int v3; // ecx
__int64 result; // rax

v1 = (double)a1;
if ( (double)a1 < 0.0 )
v2 = sqrt(v1);
else
v2 = sqrt(v1);
if ( (int)v2 <= 1 )
return 1LL;
if ( (a1 & 1) == 0 )
return 0LL;
v3 = 2;
while ( (int)v2 >= ++v3 )
{
result = (unsigned int)(a1 % v3);
if ( !(a1 % v3) )
return result;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
double v1; // xmm0_8
double v2; // xmm0_8
int v3; // ecx
__int64 result; // rax

v1 = (double)a1;
if ( (double)a1 < 0.0 )
v2 = sqrt(v1);
else
v2 = sqrt(v1);
if ( (int)v2 <= 1 )
return 1LL;
if ( (a1 & 1) == 0 )
return 0LL;
v3 = 2;
while ( (int)v2 >= ++v3 )
{
result = (unsigned int)(a1 % v3);
if ( !(a1 % v3) )
return result;
}
return 1LL;
}

original_code:
static int is_prime_num(int x)
{
 int i, s;
 s = (int)sqrt(x * 1.0);
 for (i = 2; i <= s; ++i)
 {
  if (!(x % i))
   return 0;
 }
 return 1;
}

canonical_original_code:
static int is_prime_num(int x)
{
 int i; int s;
 s = (int)sqrt(x * 1.0);
 for (i = 2; i <= s; ++i)
 {
  if (!(x % i))
   return 0;
 }
 return 1;
}

variable_types: x int

variable_types: s int


decompiled_code:
void __fastcall prnum_init(int a1, int a2)
{
__int64 v3; // rbp

v3 = 0LL;
if ( a2 > 0 )
{
do
{
while ( !(unsigned int)is_prime_num(a1) )
--a1;
prime_num[v3++] = a1--;
}
while ( v3 != a2 );
}
}

canonical_decompiled_code:
void __fastcall func1(int a1, int a2)
{
__int64 v3; // rbp

v3 = 0LL;
if ( a2 > 0 )
{
do
{
while ( !(unsigned int)func0(a1) )
--a1;
prime_num[v3++] = a1--;
}
while ( v3 != a2 );
}
}

original_code:
static void prnum_init(int max, int dimen)
{
 int i, n = max;
 for (i = 0; i < dimen; ++i)
 {
  do
  {
   if (is_prime_num(n))
   {
    prime_num[i] = n;
    --n;
    break;
   }
   --n;
  }
  while (1);
 }
}

canonical_original_code:
static void prnum_init(int max, int dimen)
{
 int i; int n = max;
 for (i = 0; i < dimen; ++i)
 {
  do
  {
   if (is_prime_num(n))
   {
    prime_num[i] = n;
    --n;
    break;
   }
   --n;
  }
  while (1);
 }
}

variable_types: max int

variable_types: dimen int

variable_types: n int


decompiled_code:
void *shamem_creat()
{
int v0; // ebx
void *v1; // rbp

v0 = shmget(0, size, 384);
if ( v0 < 0 )
err_exit("In shamem_creat: shmget error");
v1 = shmat(v0, 0LL, 0);
if ( v1 == (void *)-1LL )
err_exit("In shamem_creat: shmat error");
setconf1("shamemsize", size);
setconf2("shamemid", _id_offset, v0);
return v1;
}

canonical_decompiled_code:
void *func2()
{
int v0; // ebx
void *v1; // rbp

v0 = shmget(0, size, 384);
if ( v0 < 0 )
err_exit("In shamem_creat: shmget error");
v1 = shmat(v0, 0LL, 0);
if ( v1 == (void *)-1LL )
err_exit("In shamem_creat: shmat error");
setconf1("shamemsize", size);
setconf2("shamemid", _id_offset, v0);
return v1;
}

original_code:
static void *shamem_creat()
{
 void *shmptr;
 int shmid;
 if ((shmid = shmget(((__key_t) 0), shamem.size, 0600)) < 0)
  err_exit("In shamem_creat: shmget error");
 if ((shmptr = shmat(shmid, ((void *)0), 0)) == (void *)-1)
  err_exit("In shamem_creat: shmat error");
 setconf1("shamemsize", shamem.size);
 setconf2("shamemid", __id_offset, shmid);
 return shmptr;
}

canonical_original_code:
static void *shamem_creat()
{
 void *shmptr;
 int shmid;
 if ((shmid = shmget(((int) 0), shamem.size, 0600)) < 0)
  err_exit("In shamem_creat: shmget error");
 if ((shmptr = shmat(shmid, ((void *)0), 0)) == (void *)-1)
  err_exit("In shamem_creat: shmat error");
 setconf1("shamemsize", shamem.size);
 setconf2("shamemid", __id_offset, shmid);
 return shmptr;
}

variable_types:
shmptr
void *

variable_types: shmid int

return_type: void *


decompiled_code:
void *shamem_get()
{
int v0; // eax
int v1; // ebp
void *v2; // rbx
struct shmid_ds v4; // [rsp+0h] [rbp-88h] BYREF

v0 = getconf2("shamemid", _id_offset);
if ( !v0 )
return shamem_creat();
v1 = v0;
v2 = shmat(v0, 0LL, 0);
if ( v2 == (void *)-1LL )
return shamem_creat();
shmctl(v1, 2, &v4);
if ( v4.shm_segsz != size )
{
shmctl(v1, 0, 0LL);
v2 = shamem_creat();
}
return v2;
}

canonical_decompiled_code:
void *func3()
{
int v0; // eax
int v1; // ebp
void *v2; // rbx
struct shmid_ds v4; // [rsp+0h] [rbp-88h] BYREF

v0 = getconf2("shamemid", _id_offset);
if ( !v0 )
return func2();
v1 = v0;
v2 = shmat(v0, 0LL, 0);
if ( v2 == (void *)-1LL )
return func2();
shmctl(v1, 2, &v4);
if ( v4.shm_segsz != size )
{
shmctl(v1, 0, 0LL);
v2 = func2();
}
return v2;
}

original_code:
static void *shamem_get()
{
 void *shmptr = ((void *)0);
 int shmid;
 if ((shmid = (int)getconf2("shamemid", __id_offset)) == 0)
 {
  return shamem_creat();
 }
 else if ((shmptr = shmat(shmid, 0, 0)) == (void *)-1)
 {
  return shamem_creat();
 }
 else
 {
  struct shmid_ds buf;
  shmctl(shmid, 2, &buf);
  if (buf.shm_segsz != shamem.size)
  {
   shmctl(shmid, 0, ((void *)0));
   return shamem_creat();
  }
 }
 return shmptr;
}

variable_types: struct shmid_ds { struct ipc_perm { int __key; unsigned int uid; unsigned int gid; unsigned int cuid; unsigned int cgid; unsigned short mode; unsigned short __pad1; unsigned short __seq; unsigned short __pad2; unsigned long __glibc_reserved1; unsigned long __glibc_reserved2; } shm_perm; unsigned long shm_segsz; long shm_atime; long shm_dtime; long shm_ctime; int shm_cpid; int shm_lpid; unsigned long shm_nattch; unsigned long __glibc_reserved4; unsigned long __glibc_reserved5; }

user_defined_types: struct shmid_ds { struct ipc_perm shm_perm; unsigned long shm_segsz; long shm_atime; long shm_dtime; long shm_ctime; int shm_cpid; int shm_lpid; unsigned long shm_nattch; unsigned long __glibc_reserved4; unsigned long __glibc_reserved5; }


decompiled_code:
void shamem_init()
{
int v0; // ebx
size_t v1; // rdi
__int64 v2; // rax
int v3; // esi
int v4; // edx
__int64 v5; // rdx
void *v6; // rbx
__int64 v7; // rsi
__int64 v8; // rax

if ( shamem_status != 1 )
{
dword_9A4 = getconf1("hashlinemax");
if ( !dword_9A4 )
err_exit("IN shamem_init: the value of hashlinemaxlen is illegal");
if ( dword_9A4 <= 9999 )
err_exit("In shamem_init: the value of hashlinemaxlen should bigger than %d", 10000LL);
dword_9A0 = getconf1("hashdimen");
if ( !dword_9A0 )
err_exit("IN shamem_init: the value of hashdimen is illegal");
if ( dword_9A0 > 100 )
err_exit("In shamem_init: the value of hashdimen should less than %d", 100LL);
v0 = dword_9A0;
v1 = (unsigned int)dword_9A4;
prnum_init(dword_9A4, dword_9A0);
size = 0LL;
dword_9B0 = 0;
if ( v0 > 0 )
{
v2 = 0LL;
v3 = 0;
v1 = 0LL;
do
{
v4 = prime_num[v2];
v1 += 36LL * v4;
v3 += v4;
++v2;
}
while ( v2 != v0 );
size = v1;
dword_9B0 = v3;
}
v6 = shamem_get();
if ( !v6 )
{
v1 = (size_t)"In shamem_init: share memory get error";
err_exit("In shamem_init: share memory get error");
}
v7 = (unsigned int)dword_9A0;
if ( dword_9A0 > 0 )
{
v7 = (unsigned int)dword_9A0;
v8 = 0LL;
LODWORD(v5) = 0;
do
{
shamem[v8] = (__int64)v6 + 36 * (int)v5;
v5 = (unsigned int)(prime_num[v8++] + v5);
}
while ( (unsigned int)v7 != v8 );
}
limit_init(v1, v7, v5);
unit_livet = timel2 + 1;
shamem_status = 1;
}
}

canonical_decompiled_code:
void func4()
{
int v0; // ebx
size_t v1; // rdi
__int64 v2; // rax
int v3; // esi
int v4; // edx
__int64 v5; // rdx
void *v6; // rbx
__int64 v7; // rsi
__int64 v8; // rax

if ( shamem_status != 1 )
{
dword_9A4 = getconf1("hashlinemax");
if ( !dword_9A4 )
err_exit("IN shamem_init: the value of hashlinemaxlen is illegal");
if ( dword_9A4 <= 9999 )
err_exit("In shamem_init: the value of hashlinemaxlen should bigger than %d", 10000LL);
dword_9A0 = getconf1("hashdimen");
if ( !dword_9A0 )
err_exit("IN shamem_init: the value of hashdimen is illegal");
if ( dword_9A0 > 100 )
err_exit("In shamem_init: the value of hashdimen should less than %d", 100LL);
v0 = dword_9A0;
v1 = (unsigned int)dword_9A4;
func1(dword_9A4, dword_9A0);
size = 0LL;
dword_9B0 = 0;
if ( v0 > 0 )
{
v2 = 0LL;
v3 = 0;
v1 = 0LL;
do
{
v4 = prime_num[v2];
v1 += 36LL * v4;
v3 += v4;
++v2;
}
while ( v2 != v0 );
size = v1;
dword_9B0 = v3;
}
v6 = func3();
if ( !v6 )
{
v1 = (size_t)"In shamem_init: share memory get error";
err_exit("In shamem_init: share memory get error");
}
v7 = (unsigned int)dword_9A0;
if ( dword_9A0 > 0 )
{
v7 = (unsigned int)dword_9A0;
v8 = 0LL;
LODWORD(v5) = 0;
do
{
shamem[v8] = (__int64)v6 + 36 * (int)v5;
v5 = (unsigned int)(prime_num[v8++] + v5);
}
while ( (unsigned int)v7 != v8 );
}
limit_init(v1, v7, v5);
unit_livet = timel2 + 1;
shamem_status = 1;
}
}

original_code:
void shamem_init(void)
{
 if (shamem_status == 1)
  return;
 if ((shamem.len = (int)getconf1("hashlinemax")) == 0)
  err_exit("IN shamem_init: the value of hashlinemaxlen is illegal");
 if (shamem.len < 10000)
  err_exit("In shamem_init: the value of hashlinemaxlen should bigger than %d", 10000);
 if ((shamem.dimen = (int)getconf1("hashdimen")) == 0)
  err_exit("IN shamem_init: the value of hashdimen is illegal");
 if (shamem.dimen > 100)
  err_exit("In shamem_init: the value of hashdimen should less than %d", 100);
 prnum_init(shamem.len, shamem.dimen);
 shamem.num = shamem.size = 0;
 int i;
 for (i = 0; i < shamem.dimen; ++i)
 {
  shamem.size += prime_num[i] * sizeof(recunit);
  shamem.num += prime_num[i];
 }
 void *shmptr;
 if ((shmptr = shamem_get()) == ((void *)0))
  err_exit("In shamem_init: share memory get error");
 int sum_pass = 0;
 for (i = 0; i < shamem.dimen; ++i)
 {
  shamem.p[i] = shmptr + sum_pass * sizeof(recunit);
  sum_pass += prime_num[i];
 }
 limit_init();
 unit_livet = timel2 + 1;
 shamem_status = 1;
 return;
}

canonical_original_code:
void shamem_init()
{
 if (shamem_status == 1)
  return;
 if ((shamem.len = (int)getconf1("hashlinemax")) == 0)
  err_exit("IN shamem_init: the value of hashlinemaxlen is illegal");
 if (shamem.len < 10000)
  err_exit("In shamem_init: the value of hashlinemaxlen should bigger than %d", 10000);
 if ((shamem.dimen = (int)getconf1("hashdimen")) == 0)
  err_exit("IN shamem_init: the value of hashdimen is illegal");
 if (shamem.dimen > 100)
  err_exit("In shamem_init: the value of hashdimen should less than %d", 100);
 prnum_init(shamem.len, shamem.dimen);
 shamem.num = shamem.size = 0;
 int i;
 for (i = 0; i < shamem.dimen; ++i)
 {
  shamem.size += prime_num[i] * sizeof(recunit);
  shamem.num += prime_num[i];
 }
 void *shmptr;
 if ((shmptr = shamem_get()) == ((void *)0))
  err_exit("In shamem_init: share memory get error");
 int sum_pass = 0;
 for (i = 0; i < shamem.dimen; ++i)
 {
  shamem.p[i] = shmptr + sum_pass * sizeof(recunit);
  sum_pass += prime_num[i];
 }
 limit_init();
 unit_livet = timel2 + 1;
 shamem_status = 1;
 return;
}


decompiled_code:
__int64 __fastcall hash_search(unsigned __int16 a1, int a2, __int64 a3)
{
unsigned int v5; // edi
__int64 v6; // rcx
__int64 result; // rax
__int64 v8; // rcx

if ( !shamem_status )
shamem_init();
v5 = a2 ^ a1;
if ( dword_9A0 <= 0 )
return 0LL;
v6 = 0LL;
while ( 1 )
{
result = shamem[v6] + 36LL * (v5 % prime_num[v6]);
if ( !*(_BYTE *)result || *(_WORD *)(result + 2) == a1 && *(_DWORD *)(result + 4) == a2 )
break;
if ( ++v6 == dword_9A0 )
{
v8 = 0LL;
while ( 1 )
{
result = shamem[v8] + 36LL * (v5 % prime_num[v8]);
if ( a3 - *(_QWORD *)(result + 16) > unit_livet )
break;
if ( ++v8 == dword_9A0 )
return 0LL;
}
return result;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned __int16 a1, int a2, __int64 a3)
{
unsigned int v5; // edi
__int64 v6; // rcx
__int64 result; // rax
__int64 v8; // rcx

if ( !shamem_status )
func4();
v5 = a2 ^ a1;
if ( dword_9A0 <= 0 )
return 0LL;
v6 = 0LL;
while ( 1 )
{
result = shamem[v6] + 36LL * (v5 % prime_num[v6]);
if ( !*(_BYTE *)result || *(_WORD *)(result + 2) == a1 && *(_DWORD *)(result + 4) == a2 )
break;
if ( ++v6 == dword_9A0 )
{
v8 = 0LL;
while ( 1 )
{
result = shamem[v8] + 36LL * (v5 % prime_num[v8]);
if ( a3 - *(_QWORD *)(result + 16) > unit_livet )
break;
if ( ++v8 == dword_9A0 )
return 0LL;
}
return result;
}
}
return result;
}

original_code:
recunit *hash_search(uint16_t sid, uint32_t uid, time_t now)
{
 if (shamem_status == 0)
 {
  shamem_init();
 }
 int n = 0;
 recunit * p;
 uint32_t key = sid ^ uid;
 while (n < shamem.dimen)
 {
  p = (shamem.p[n]) + (key % prime_num[n]);
  if (p->sign == 0X00)
  {
   return p;
  }
  else if ((p->sid == sid) && (p->uid == uid))
  {
   return p;
  }
  else
  {
   ++n;
  }
 }
 n = 0;
 while (n < shamem.dimen)
 {
  p = (shamem.p[n]) + (key % prime_num[n]);
  if ((now - p->timel2) > unit_livet)
  {
   return p;
  }
  else
  {
   ++n;
  }
 }
 return ((void *)0);
}

canonical_original_code:
struct recunit *hash_search(unsigned short sid, unsigned int uid, long now)
{
 if (shamem_status == 0)
 {
  shamem_init();
 }
 int n = 0;
 struct recunit *p;
 unsigned int key = sid ^ uid;
 while (n < shamem.dimen)
 {
  p = (shamem.p[n]) + (key % prime_num[n]);
  if (p->sign == 0X00)
  {
   return p;
  }
  else if ((p->sid == sid) && (p->uid == uid))
  {
   return p;
  }
  else
  {
   ++n;
  }
 }
 n = 0;
 while (n < shamem.dimen)
 {
  p = (shamem.p[n]) + (key % prime_num[n]);
  if ((now - p->timel2) > unit_livet)
  {
   return p;
  }
  else
  {
   ++n;
  }
 }
 return ((void *)0);
}

variable_types: now long

variable_types: struct recunit { unsigned char sign; unsigned char version; unsigned short sid; unsigned int uid; long timel1; long timel2; unsigned int counterl1; unsigned int counterl2; unsigned int allowedl2; } *

variable_types:
key
unsigned int


decompiled_code:
__int64 memuse_rate()
{
__int64 v0; // rbx
time_t v1; // rax
time_t v2; // r8
__int64 v3; // rax
int v4; // esi
int v5; // edx

v0 = shamem[0];
v1 = time(0LL);
if ( dword_9B0 <= 0 )
{
v4 = 0;
}
else
{
v2 = v1;
v3 = v0;
v4 = 0;
v5 = 0;
do
{
if ( *(_BYTE *)v3 )
v4 += v2 - *(_QWORD *)(v3 + 16) < unit_livet;
v5 += 100;
v3 += 3600LL;
}
while ( v5 < dword_9B0 );
}
return run_msg("share memory use rate = %.4f", (float)((float)((float)v4 * 100.0) / (float)dword_9B0));
}

canonical_decompiled_code:
__int64 func6()
{
__int64 v0; // rbx
time_t v1; // rax
time_t v2; // r8
__int64 v3; // rax
int v4; // esi
int v5; // edx

v0 = shamem[0];
v1 = time(0LL);
if ( dword_9B0 <= 0 )
{
v4 = 0;
}
else
{
v2 = v1;
v3 = v0;
v4 = 0;
v5 = 0;
do
{
if ( *(_BYTE *)v3 )
v4 += v2 - *(_QWORD *)(v3 + 16) < unit_livet;
v5 += 100;
v3 += 3600LL;
}
while ( v5 < dword_9B0 );
}
return run_msg("share memory use rate = %.4f", (float)((float)((float)v4 * 100.0) / (float)dword_9B0));
}

original_code:
void memuse_rate(void)
{
 int samsur = 100;
 recunit *p = shamem.p[0];
 int i, counter = 0;
 time_t now = time(((void *)0));
 for (i = 0; i < shamem.num; i = i + samsur)
  if ((p + i)->sign && (now - (p + i)->timel2) < unit_livet)
   ++counter;
 float userate = ((float)counter * samsur)/ shamem.num;
 run_msg("share memory use rate = %.4f", userate);
 return;
}

canonical_original_code:
void memuse_rate()
{
 int samsur = 100;
 struct recunit *p = shamem.p[0];
 int i; int counter = 0;
 long now = time(((void *)0));
 for (i = 0; i < shamem.num; i = i + samsur)
  if ((p + i)->sign && (now - (p + i)->timel2) < unit_livet)
   ++counter;
 float userate = ((float)counter * samsur)/ shamem.num;
 run_msg("share memory use rate = %.4f", userate);
 return;
}

variable_types: samsur int

variable_types: counter int

variable_types: userate float

user_defined_types: struct recunit { unsigned char sign; unsigned char version; unsigned short sid; unsigned int uid; long timel1; long timel2; unsigned int counterl1; unsigned int counterl2; unsigned int allowedl2; }


getname

decompiled_code:
char *getname()
{
unsigned int v0; // ebx
char *v1; // rax

v0 = _id_offset;
v1 = getenv("HOME");
sprintf(name, "%s%s%s%d%s", v1, "/wb/fls/data/", "flsmemory", v0, ".backup");
return name;
}

canonical_decompiled_code:
char *func0()
{
unsigned int v0; // ebx
char *v1; // rax

v0 = _id_offset;
v1 = getenv("HOME");
sprintf(name, "%s%s%s%d%s", v1, "/wb/fls/data/", "flsmemory", v0, ".backup");
return name;
}

original_code:
char * getname(void)
{
 sprintf(name, "%s%s%s%d%s", getenv("HOME"), location, head, __id_offset, tail);
 return name;
}

canonical_original_code:
char *getname()
{
 sprintf(name, "%s%s%s%d%s", getenv("HOME"), location, head, __id_offset, tail);
 return name;
}

return_type: char *


decompiled_code:
void __noreturn err_exit(const char *a1, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-4D0h] BYREF
char s[1024]; // [rsp+20h] [rbp-4B8h] BYREF

va_start(arg, a1);
vsnprintf(s, 0x400uLL, a1, arg);
fputs(s, stderr);
putc(10, stderr);
fflush(0LL);
addto_log(1LL, s);
exit(1);
}

canonical_decompiled_code:
void __noreturn func0(const char *a1, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-4D0h] BYREF
char s[1024]; // [rsp+20h] [rbp-4B8h] BYREF

va_start(arg, a1);
vsnprintf(s, 0x400uLL, a1, arg);
fputs(s, stderr);
putc(10, stderr);
fflush(0LL);
addto_log(1LL, s);
exit(1);
}

original_code:
void err_exit(const char *fmt, ...)
{
 char buf[1024];
 va_list ap;
 __builtin_va_start(ap,fmt);
 vsnprintf(buf, 1024, fmt, ap);
 __builtin_va_end(ap);
 fputs(buf, stderr);
 putc('\n', stderr);
 fflush(((void *)0));
 addto_log(1, buf);
 exit(1);
}

canonical_original_code:
void err_exit(char *fmt, ...)
{
 char buf[1024];
 __builtin_va_list ap;
 __builtin_va_start(ap,fmt);
 vsnprintf(buf, 1024, fmt, ap);
 __builtin_va_end(ap);
 fputs(buf, stderr);
 putc('\n', stderr);
 fflush(((void *)0));
 addto_log(1, buf);
 exit(1);
}

variable_types:
fmt
char *

variable_types:
buf
char [1024]


decompiled_code:
__int64 run_msg(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-4D0h] BYREF
char s[1024]; // [rsp+20h] [rbp-4B8h] BYREF

va_start(arg, format);
vsnprintf(s, 0x400uLL, format, arg);
if ( _msg_stdout_switch )
{
fputs(s, stdout);
putc(10, stdout);
fflush(0LL);
}
return addto_log(1LL, s);
}

canonical_decompiled_code:
__int64 func1(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-4D0h] BYREF
char s[1024]; // [rsp+20h] [rbp-4B8h] BYREF

va_start(arg, format);
vsnprintf(s, 0x400uLL, format, arg);
if ( _msg_stdout_switch )
{
fputs(s, stdout);
putc(10, stdout);
fflush(0LL);
}
return addto_log(1LL, s);
}

original_code:
void run_msg(const char *fmt, ...)
{
 char buf[1024];
 va_list ap;
 __builtin_va_start(ap,fmt);
 vsnprintf(buf, 1024, fmt, ap);
 __builtin_va_end(ap);
 extern int __msg_stdout_switch;
 if (__msg_stdout_switch)
 {
  fputs(buf, stdout);
  putc('\n', stdout);
  fflush(((void *)0));
 }
 addto_log(1, buf);
}

canonical_original_code:
void run_msg(char *fmt, ...)
{
 char buf[1024];
 __builtin_va_list ap;
 __builtin_va_start(ap,fmt);
 vsnprintf(buf, 1024, fmt, ap);
 __builtin_va_end(ap);
 extern int __msg_stdout_switch;
 if (__msg_stdout_switch)
 {
  fputs(buf, stdout);
  putc('\n', stdout);
  fflush(((void *)0));
 }
 addto_log(1, buf);
}


limducom

decompiled_code:
__int64 __fastcall limducom(const void *a1, const void *a2)
{
return *(unsigned __int16 *)a1 - (unsigned int)*(unsigned __int16 *)a2;
}

canonical_decompiled_code:
__int64 __fastcall func0(const void *a1, const void *a2)
{
return *(unsigned __int16 *)a1 - (unsigned int)*(unsigned __int16 *)a2;
}

original_code:
static int limducom(const void * p1, const void * p2)
{
 return (((struct limit_unit *)p1)->sid - ((struct limit_unit *)p2)->sid);
}

canonical_original_code:
static int limducom(void *p1, void *p2)
{
 return (((struct limit_unit *)p1)->sid - ((struct limit_unit *)p2)->sid);
}

user_defined_types: struct limit_unit { unsigned short sid; unsigned int limit[2]; }


decompiled_code:
__int64 __fastcall addto_limd(__int16 a1, int a2, int a3)
{
__int64 result; // rax
__int16 v4; // [rsp+4h] [rbp-14h] BYREF
int v5; // [rsp+8h] [rbp-10h]
int v6; // [rsp+Ch] [rbp-Ch]

v4 = a1;
v5 = a2;
v6 = a3;
result = add_item((__int64)&v4);
if ( (_DWORD)result )
{
qsort(&limd, dword_30A0, 0xCuLL, (__compar_fn_t)limducom);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int16 a1, int a2, int a3)
{
__int64 result; // rax
__int16 v4; // [rsp+4h] [rbp-14h] BYREF
int v5; // [rsp+8h] [rbp-10h]
int v6; // [rsp+Ch] [rbp-Ch]

v4 = a1;
v5 = a2;
v6 = a3;
result = func1((__int64)&v4);
if ( (_DWORD)result )
{
qsort(&limd, dword_30A0, 0xCuLL, (__compar_fn_t)limducom);
result = 1LL;
}
return result;
}

original_code:
int addto_limd(uint16_t sid, uint32_t lim1, uint32_t lim2)
{
 struct limit_unit temp;
 temp.sid = sid;
 temp.limit[0] = lim1;
 temp.limit[1] = lim2;
 if (!add_item(&temp))
  return 0;
 qsort(limd.list, limd.len, sizeof(struct limit_unit), limducom);
 return 1;
}

canonical_original_code:
int addto_limd(unsigned short sid, unsigned int lim1, unsigned int lim2)
{
 struct limit_unit temp;
 temp.sid = sid;
 temp.limit[0] = lim1;
 temp.limit[1] = lim2;
 if (!add_item(&temp))
  return 0;
 qsort(limd.list, limd.len, sizeof(struct limit_unit), limducom);
 return 1;
}

variable_types:
sid
unsigned short

variable_types: struct limit_unit { unsigned short sid; unsigned int limit[2]; }


decompiled_code:
char *__fastcall search_limd(unsigned __int16 a1)
{
int v1; // esi
int v2; // edx
int v4; // eax
unsigned __int16 v5; // cx

v1 = dword_30A0 - 1;
if ( dword_30A0 - 1 < 0 )
return 0LL;
v2 = 0;
do
{
v4 = v2 + (v1 - v2) / 2;
v5 = *((_WORD *)&limd + 6 * v4);
if ( v5 == a1 )
return (char *)&limd + 12 * v4;
if ( v5 <= a1 )
v2 = v4 + 1;
else
v1 = v4 - 1;
}
while ( v2 <= v1 );
return 0LL;
}

canonical_decompiled_code:
char *__fastcall func3(unsigned __int16 a1)
{
int v1; // esi
int v2; // edx
int v4; // eax
unsigned __int16 v5; // cx

v1 = dword_30A0 - 1;
if ( dword_30A0 - 1 < 0 )
return 0LL;
v2 = 0;
do
{
v4 = v2 + (v1 - v2) / 2;
v5 = *((_WORD *)&limd + 6 * v4);
if ( v5 == a1 )
return (char *)&limd + 12 * v4;
if ( v5 <= a1 )
v2 = v4 + 1;
else
v1 = v4 - 1;
}
while ( v2 <= v1 );
return 0LL;
}

original_code:
struct limit_unit * search_limd(uint16_t sid)
{
 int low = 0;
 int high = limd.len - 1;
 int mid;
 while (low <= high)
 {
  mid = low + ((high - low) / 2);
  if (limd.list[mid].sid == sid)
  {
   return limd.list + mid;
  }
  else if (limd.list[mid].sid > sid)
  {
   high = mid - 1;
  }
  else
  {
   low = mid + 1;
  }
 }
 return ((void *)0);
}

canonical_original_code:
struct limit_unit *search_limd(unsigned short sid)
{
 int low = 0;
 int high = limd.len - 1;
 int mid;
 while (low <= high)
 {
  mid = low + ((high - low) / 2);
  if (limd.list[mid].sid == sid)
  {
   return limd.list + mid;
  }
  else if (limd.list[mid].sid > sid)
  {
   high = mid - 1;
  }
  else
  {
   low = mid + 1;
  }
 }
 return ((void *)0);
}

variable_types: low int

variable_types: high int

variable_types: mid int

return_type: struct limit_unit { unsigned short sid; unsigned int limit[2]; } *


decompiled_code:
__int64 __fastcall get_limd(unsigned __int16 a1, _DWORD *a2, _DWORD *a3)
{
char *v4; // rax

if ( !a2 )
return 0LL;
if ( !a3 )
return 0LL;
if ( !dword_30A0 )
limit_init();
v4 = search_limd(a1);
if ( !v4 )
return 0LL;
*a2 = *((_DWORD *)v4 + 1);
*a3 = *((_DWORD *)v4 + 2);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned __int16 a1, _DWORD *a2, _DWORD *a3)
{
char *v4; // rax

if ( !a2 )
return 0LL;
if ( !a3 )
return 0LL;
if ( !dword_30A0 )
limit_init();
v4 = func3(a1);
if ( !v4 )
return 0LL;
*a2 = *((_DWORD *)v4 + 1);
*a3 = *((_DWORD *)v4 + 2);
return 1LL;
}

original_code:
int get_limd(const uint16_t sid, uint32_t *lim1, uint32_t *lim2)
{
 if (((void *)0) == lim1 || ((void *)0) == lim2)
  return 0;
 if (limd.len == 0)
 {
  limit_init();
 }
 struct limit_unit * temp;
 if ((temp = search_limd(sid)) == ((void *)0))
  return 0;
 *lim1 = temp->limit[0];
 *lim2 = temp->limit[1];
 return 1;
}

canonical_original_code:
int get_limd(unsigned short sid, unsigned int *lim1, unsigned int *lim2)
{
 if (((void *)0) == lim1 || ((void *)0) == lim2)
  return 0;
 if (limd.len == 0)
 {
  limit_init();
 }
 struct limit_unit *temp;
 if ((temp = search_limd(sid)) == ((void *)0))
  return 0;
 *lim1 = temp->limit[0];
 *lim2 = temp->limit[1];
 return 1;
}

variable_types: struct limit_unit { unsigned short sid; unsigned int limit[2]; } *


original_code:
static int add_item(struct limit_unit * ptr)
{
 if (((void *)0) == ptr)
  return 0;
 if (limd.len >= 1000)
  return 0;
 int i;
 for (i = 0; i < limd.len; ++i)
  if (ptr->sid == limd.list[i].sid)
   return 1;
 i = limd.len;
 memset(limd.list + i, '\0', sizeof(struct limit_unit));
 limd.list[i].sid = ptr->sid;
 limd.list[i].limit[0] = ptr->limit[0];
 limd.list[i].limit[1] = ptr->limit[1];
 ++limd.len;
 return 1;
}

canonical_original_code:
static int add_item(struct limit_unit *ptr)
{
 if (((void *)0) == ptr)
  return 0;
 if (limd.len >= 1000)
  return 0;
 int i;
 for (i = 0; i < limd.len; ++i)
  if (ptr->sid == limd.list[i].sid)
   return 1;
 i = limd.len;
 memset(limd.list + i, '\0', sizeof(struct limit_unit));
 limd.list[i].sid = ptr->sid;
 limd.list[i].limit[0] = ptr->limit[0];
 limd.list[i].limit[1] = ptr->limit[1];
 ++limd.len;
 return 1;
}

variable_types: i int


original_code:
static void unit_init(recunit *unit, const netpt1 *p, time_t now)
{
 if (((void *)0) == unit || ((void *)0) == p)
  return;
 unit->sign = 1;
 unit->version = 1;
 unit->sid = p->body.sid;
 unit->uid = p->body.uid;
 unit->timel1 = now;
 unit->counterl1 = 0;
 unit->timel2 = now;
 unit->counterl2 = 0;
 unit->allowedl2 = 0;
 return;
}

canonical_original_code:
static void unit_init(struct recunit *unit, struct netpt1 *p, long now)
{
 if (((void *)0) == unit || ((void *)0) == p)
  return;
 unit->sign = 1;
 unit->version = 1;
 unit->sid = p->body.sid;
 unit->uid = p->body.uid;
 unit->timel1 = now;
 unit->counterl1 = 0;
 unit->timel2 = now;
 unit->counterl2 = 0;
 unit->allowedl2 = 0;
 return;
}

variable_types: struct netpt1 { struct phead { char psf; char zero1; unsigned short len; unsigned short subcmd; unsigned short version; unsigned int uid; unsigned short fls_id; unsigned short fls_num; unsigned int client_ip; unsigned short client_port; char zero2[2]; char reserve[40]; } head; struct pbody1 { unsigned int uid; unsigned short sid; unsigned short num; } body; char zero[3]; char pef; } *


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

if ( a1 && a2 && *(_BYTE *)a1 && *(_DWORD *)(a1 + 28) > (unsigned int)limit2 )
{
v2 = localtime((const time_t *)(a1 + 8));
strftime(buf, 0x14uLL, "%H:%M:%S", v2);
sprintf(
msg,
"%10d\t%4d\t%s\t%4d\t%4d\t%4d",
*(unsigned int *)(a2 + 64),
*(unsigned __int16 *)(a2 + 68),
buf,
*(unsigned int *)(a1 + 28),
timel2,
(unsigned int)limit2);
addto_log(3LL, msg);
if ( *(_DWORD *)(a1 + 28) / (unsigned int)limit2 >= warning_rate )
addto_log(4LL, msg);
}
}

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

if ( a1 && a2 && *(_BYTE *)a1 && *(_DWORD *)(a1 + 28) > (unsigned int)limit2 )
{
v2 = localtime((const time_t *)(a1 + 8));
strftime(buf, 0x14uLL, "%H:%M:%S", v2);
sprintf(
msg,
"%10d\t%4d\t%s\t%4d\t%4d\t%4d",
*(unsigned int *)(a2 + 64),
*(unsigned __int16 *)(a2 + 68),
buf,
*(unsigned int *)(a1 + 28),
timel2,
(unsigned int)limit2);
addto_log(3LL, msg);
if ( *(_DWORD *)(a1 + 28) / (unsigned int)limit2 >= warning_rate )
addto_log(4LL, msg);
}
}

original_code:
static void output_log2(recunit *unit, const netpt1 *p)
{
 if (((void *)0) == unit || ((void *)0) == p)
  return;
 if (unit->sign == 0)
  return;
 if (unit->counterl2 > limit2)
 {
  struct tm *hour = localtime(&(unit->timel1));
  strftime(buf, 20, "%H:%M:%S", hour);
  sprintf(msg, "%10d\t%4d\t%s\t%4d\t%4d\t%4d",p->body.uid, p->body.sid, buf, unit->counterl2, timel2, limit2);
  addto_log(3, msg);
  if ((unit->counterl2 / limit2) >= warning_rate)
   addto_log(4, msg);
 }
}

canonical_original_code:
static void output_log2(struct recunit *unit, struct netpt1 *p)
{
 if (((void *)0) == unit || ((void *)0) == p)
  return;
 if (unit->sign == 0)
  return;
 if (unit->counterl2 > limit2)
 {
  struct tm *hour = localtime(&(unit->timel1));
  strftime(buf, 20, "%H:%M:%S", hour);
  sprintf(msg, "%10d\t%4d\t%s\t%4d\t%4d\t%4d",p->body.uid, p->body.sid, buf, unit->counterl2, timel2, limit2);
  addto_log(3, msg);
  if ((unit->counterl2 / limit2) >= warning_rate)
   addto_log(4, msg);
 }
}

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

user_defined_types: struct phead { char psf; char zero1; unsigned short len; unsigned short subcmd; unsigned short version; unsigned int uid; unsigned short fls_id; unsigned short fls_num; unsigned int client_ip; unsigned short client_port; char zero2[2]; char reserve[40]; }

user_defined_types: struct pbody1 { unsigned int uid; unsigned short sid; unsigned short num; }


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

if ( a1 && a2 && *(_BYTE *)a1 && *(_DWORD *)(a1 + 24) > (unsigned int)limit1 )
{
v2 = localtime((const time_t *)(a1 + 8));
strftime(buf, 0x14uLL, "%H:%M:%S", v2);
sprintf(
msg,
"%10d %4d %s %4d %4d %4d",
*(unsigned int *)(a2 + 64),
*(unsigned __int16 *)(a2 + 68),
buf,
*(unsigned int *)(a1 + 24),
timel1,
(unsigned int)limit1);
addto_log(3LL, msg);
if ( *(_DWORD *)(a1 + 24) / (unsigned int)limit1 >= warning_rate )
addto_log(4LL, msg);
}
}

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

if ( a1 && a2 && *(_BYTE *)a1 && *(_DWORD *)(a1 + 24) > (unsigned int)limit1 )
{
v2 = localtime((const time_t *)(a1 + 8));
strftime(buf, 0x14uLL, "%H:%M:%S", v2);
sprintf(
msg,
"%10d %4d %s %4d %4d %4d",
*(unsigned int *)(a2 + 64),
*(unsigned __int16 *)(a2 + 68),
buf,
*(unsigned int *)(a1 + 24),
timel1,
(unsigned int)limit1);
addto_log(3LL, msg);
if ( *(_DWORD *)(a1 + 24) / (unsigned int)limit1 >= warning_rate )
addto_log(4LL, msg);
}
}

original_code:
static void output_log1(recunit *unit, const netpt1 *p)
{
 if (((void *)0) == unit || ((void *)0) == p)
  return;
 if (unit->sign == 0)
  return;
 if (unit->counterl1 > limit1)
 {
  struct tm *hour = localtime(&(unit->timel1));
  strftime(buf, 20, "%H:%M:%S", hour);
  sprintf(msg, "%10d  %4d  %s  %4d  %4d  %4d",p->body.uid, p->body.sid, buf, unit->counterl1, timel1, limit1);
  addto_log(3, msg);
  if ((unit->counterl1 / limit1) >= warning_rate)
   addto_log(4, msg);
 }
}

canonical_original_code:
static void output_log1(struct recunit *unit, struct netpt1 *p)
{
 if (((void *)0) == unit || ((void *)0) == p)
  return;
 if (unit->sign == 0)
  return;
 if (unit->counterl1 > limit1)
 {
  struct tm *hour = localtime(&(unit->timel1));
  strftime(buf, 20, "%H:%M:%S", hour);
  sprintf(msg, "%10d  %4d  %s  %4d  %4d  %4d",p->body.uid, p->body.sid, buf, unit->counterl1, timel1, limit1);
  addto_log(3, msg);
  if ((unit->counterl1 / limit1) >= warning_rate)
   addto_log(4, msg);
 }
}

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


original_code:
static void update_counter(recunit *unit, const netpt1 *p, time_t now)
{
 if (((void *)0) == unit || ((void *)0) == p)
  return;
 int not_use = !(unit->sign);
 int not_same = (unit->sid != p->body.sid) || (unit->uid != p->body.uid);
 int is_old = (now - unit->timel2) >= timel2;
 if (not_use || not_same || is_old)
 {
  output_log1(unit, p);
  output_log2(unit, p);
  unit_init(unit, p, now);
 }
 if ((now - unit->timel1) >= timel1)
 {
  output_log1(unit, p);
  unit->timel1 = now;
  unit->counterl1 = 0;
 }
 unit->counterl1 += p->body.num;
 unit->counterl2 += p->body.num;
 return;
}

canonical_original_code:
static void update_counter(struct recunit *unit, struct netpt1 *p, long now)
{
 if (((void *)0) == unit || ((void *)0) == p)
  return;
 int not_use = !(unit->sign);
 int not_same = (unit->sid != p->body.sid) || (unit->uid != p->body.uid);
 int is_old = (now - unit->timel2) >= timel2;
 if (not_use || not_same || is_old)
 {
  output_log1(unit, p);
  output_log2(unit, p);
  unit_init(unit, p, now);
 }
 if ((now - unit->timel1) >= timel1)
 {
  output_log1(unit, p);
  unit->timel1 = now;
  unit->counterl1 = 0;
 }
 unit->counterl1 += p->body.num;
 unit->counterl2 += p->body.num;
 return;
}

variable_types: now long


decompiled_code:
__int64 __fastcall access_query(__int64 a1, _BYTE *a2, __int64 a3)
{
__int64 result; // rax
__int64 v5; // r12
unsigned int v6; // edx

if ( !a1 )
return 0LL;
result = 0LL;
if ( a2 )
{
v5 = hash_search(*(unsigned __int16 *)(a1 + 68), *(unsigned int *)(a1 + 64));
if ( v5 )
{
result = get_limd(*(unsigned __int16 *)(a1 + 68), &limit1, &limit2);
if ( (_DWORD)result )
{
*a2 = 1;
update_counter(v5, a1, a3);
result = 0LL;
if ( *(_DWORD *)(v5 + 24) <= (unsigned int)limit1 )
{
v6 = *(_DWORD *)(v5 + 32);
if ( v6 < limit2 )
{
*(_DWORD *)(v5 + 32) = *(unsigned __int16 *)(a1 + 70) + v6;
result = 1LL;
}
}
}
else
{
*a2 = 0;
}
}
else
{
*a2 = 0;
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, _BYTE *a2, __int64 a3)
{
__int64 result; // rax
__int64 v5; // r12
unsigned int v6; // edx

if ( !a1 )
return 0LL;
result = 0LL;
if ( a2 )
{
v5 = hash_search(*(unsigned __int16 *)(a1 + 68), *(unsigned int *)(a1 + 64));
if ( v5 )
{
result = get_limd(*(unsigned __int16 *)(a1 + 68), &limit1, &limit2);
if ( (_DWORD)result )
{
*a2 = 1;
func3(v5, a1, a3);
result = 0LL;
if ( *(_DWORD *)(v5 + 24) <= (unsigned int)limit1 )
{
v6 = *(_DWORD *)(v5 + 32);
if ( v6 < limit2 )
{
*(_DWORD *)(v5 + 32) = *(unsigned __int16 *)(a1 + 70) + v6;
result = 1LL;
}
}
}
else
{
*a2 = 0;
}
}
else
{
*a2 = 0;
result = 0LL;
}
}
return result;
}

original_code:
uint8_t access_query(const netpt1 *p, uint8_t * stat, time_t now)
{
 if (((void *)0) == p || ((void *)0) == stat)
  return 0;
 recunit *memunit = hash_search(p->body.sid, p->body.uid, now);
 if (memunit == ((void *)0))
 {
  *stat = 0;
  return 0;
 }
 if(!(get_limd(p->body.sid, &limit1, &limit2)))
 {
  *stat = 0;
  return 0;
 }
 *stat = 1;
 update_counter(memunit, p, now);
 if (memunit->counterl1 > limit1)
 {
  return 0;
 }
 else if (memunit->allowedl2 >= limit2)
 {
  return 0;
 }
 else
 {
  memunit->allowedl2 += p->body.num;
  return 1;
 }
 return 1;
}

canonical_original_code:
unsigned char access_query(struct netpt1 *p, unsigned char *stat, long now)
{
 if (((void *)0) == p || ((void *)0) == stat)
  return 0;
 struct recunit *memunit = hash_search(p->body.sid, p->body.uid, now);
 if (memunit == ((void *)0))
 {
  *stat = 0;
  return 0;
 }
 if(!(get_limd(p->body.sid, &limit1, &limit2)))
 {
  *stat = 0;
  return 0;
 }
 *stat = 1;
 update_counter(memunit, p, now);
 if (memunit->counterl1 > limit1)
 {
  return 0;
 }
 else if (memunit->allowedl2 >= limit2)
 {
  return 0;
 }
 else
 {
  memunit->allowedl2 += p->body.num;
  return 1;
 }
 return 1;
}

variable_types:
stat
unsigned char *

variable_types: struct recunit { unsigned char sign; unsigned char version; unsigned short sid; unsigned int uid; long timel1; long timel2; unsigned int counterl1; unsigned int counterl2; unsigned int allowedl2; } *

return_type: unsigned char


decompiled_code:
_BOOL8 __fastcall netpt1_check(__int64 a1)
{
return search_limd(*(unsigned __int16 *)(a1 + 68)) != 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(__int64 a1)
{
return search_limd(*(unsigned __int16 *)(a1 + 68)) != 0;
}

original_code:
static int netpt1_check(const netpt1 *ptr)
{
 if (search_limd(ptr->body.sid) == ((void *)0))
 {
  return 0;
 }
 return 1;
}

canonical_original_code:
static int netpt1_check(struct netpt1 *ptr)
{
 if (search_limd(ptr->body.sid) == ((void *)0))
 {
  return 0;
 }
 return 1;
}


decompiled_code:
_BOOL8 __fastcall netpt3_check(__int64 a1)
{
return search_limd(*(unsigned __int16 *)(a1 + 68)) != 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(__int64 a1)
{
return search_limd(*(unsigned __int16 *)(a1 + 68)) != 0;
}

original_code:
static int netpt3_check(const netpt3 *ptr)
{
 if (search_limd(ptr->body.sid) == ((void *)0))
 {
  return 0;
 }
 return 1;
}

canonical_original_code:
static int netpt3_check(struct netpt3 *ptr)
{
 if (search_limd(ptr->body.sid) == ((void *)0))
 {
  return 0;
 }
 return 1;
}

variable_types: struct netpt3 { struct phead { char psf; char zero1; unsigned short len; unsigned short subcmd; unsigned short version; unsigned int uid; unsigned short fls_id; unsigned short fls_num; unsigned int client_ip; unsigned short client_port; char zero2[2]; char reserve[40]; } head; struct pbody3 { unsigned int uid; unsigned short sid; char zero[2]; } body; char zero[3]; char pef; } *


decompiled_code:
_BOOL8 __fastcall netpt7_check(__int64 a1)
{
__int64 v1; // rdx
_BOOL8 result; // rax

v1 = search_limd(*(unsigned __int16 *)(a1 + 66));
result = 1LL;
if ( !v1 )
result = *(_WORD *)(a1 + 66) == 0;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(__int64 a1)
{
__int64 v1; // rdx
_BOOL8 result; // rax

v1 = search_limd(*(unsigned __int16 *)(a1 + 66));
result = 1LL;
if ( !v1 )
result = *(_WORD *)(a1 + 66) == 0;
return result;
}

original_code:
static int netpt7_check(const netpt7 *ptr)
{
 if (search_limd(ptr->body.sid) == ((void *)0) && ptr->body.sid != 0)
 {
  return 0;
 }
 return 1;
}

canonical_original_code:
static int netpt7_check(struct netpt7 *ptr)
{
 if (search_limd(ptr->body.sid) == ((void *)0) && ptr->body.sid != 0)
 {
  return 0;
 }
 return 1;
}

variable_types: struct netpt7 { struct phead { char psf; char zero1; unsigned short len; unsigned short subcmd; unsigned short version; unsigned int uid; unsigned short fls_id; unsigned short fls_num; unsigned int client_ip; unsigned short client_port; char zero2[2]; char reserve[40]; } head; struct pbody7 { unsigned char stat; char zero; unsigned short sid; struct addr { unsigned char id; unsigned char zero; unsigned short port; unsigned int ip; } fls; } body; char zero[3]; char pef; } *

user_defined_types: struct netpt7 { struct phead head; struct pbody7 body; char zero[3]; char pef; }

user_defined_types: struct pbody7 { unsigned char stat; char zero; unsigned short sid; struct addr fls; }


decompiled_code:
__int16 fls_main()
{
time_t v0; // rax
time_t v1; // rbp
__int64 v2; // rbx
unsigned __int8 v3; // al
__int64 v4; // rax
unsigned __int8 v6; // [rsp+Fh] [rbp-29h] BYREF
time_t timer; // [rsp+10h] [rbp-28h] BYREF
time_t v8; // [rsp+18h] [rbp-20h] BYREF

time(&timer);
v0 = time(0LL);
v1 = v0;
while ( fls_switch )
{
time(&v8);
v0 = read_net();
v2 = v0;
if ( v0 )
{
LOWORD(v0) = _bswap_16(*(_WORD *)(v0 + 4));
switch ( (_WORD)v0 )
{
case 3:
ntohpt3(v2);
LODWORD(v0) = netpt3_check(v2);
if ( (_DWORD)v0 )
{
v4 = hash_search(*(unsigned __int16 *)(v2 + 68), *(unsigned int *)(v2 + 64), v8);
LOWORD(v0) = send_net4(v2, v4 != 0, v4);
}
break;
case 7:
if ( manage_switch )
{
ntohpt7(v2);
LODWORD(v0) = netpt7_check(v2);
if ( (_DWORD)v0 )
{
LOWORD(v0) = *(unsigned __int8 *)(v2 + 64);
switch ( (_BYTE)v0 )
{
case 2:
remove_rflsl(v2 + 68);
send_net8(v2, 4LL, 3LL);
LOWORD(v0) = addto_log(1LL, "fls id: %d closed.", *(unsigned __int8 *)(v2 + 68));
break;
case 3:
remove_rflsl(v2 + 68);
send_net8(v2, 4LL, 3LL);
LOWORD(v0) = addto_log(1LL, "fls id: %d disconnected!", *(unsigned __int8 *)(v2 + 68));
break;
case 1:
if ( !(unsigned int)addto_rflsl(v2 + 68) )
run_msg("fls server is too much!");
send_net8(v2, 4LL, 2LL);
LOWORD(v0) = addto_log(1LL, "fls id: %d started.", *(unsigned __int8 *)(v2 + 68));
break;
}
}
}
break;
case 1:
ntohpt1(v2);
LODWORD(v0) = netpt1_check(v2);
if ( (_DWORD)v0 )
{
if ( v8 != timer )
{
addto_log(
2LL,
"%5d %5d %5d %5d",
(unsigned int)call_0,
(unsigned int)callow_1,
(unsigned int)crefuse_2,
(unsigned int)cfail_3);
cfail_3 = 0;
crefuse_2 = 0;
callow_1 = 0;
call_0 = 0;
timer = v8;
}
++call_0;
if ( manage_switch && *(unsigned __int16 *)(v2 + 14) != rflsl[768] )
{
LOWORD(v0) = send_net6(v2);
++callow_1;
}
else
{
v3 = access_query(v2, &v6, v8);
if ( v6 == 1 )
{
if ( v3 == 1 )
++callow_1;
else
++crefuse_2;
}
else
{
++cfail_3;
}
LOWORD(v0) = send_net2(v2, v6, v3);
}
}
break;
}
if ( _memuse_rate_switch )
{
v0 = 10 * ((v8 - v1) / 10);
if ( v8 - v1 == v0 && v1 != v8 )
{
LODWORD(v0) = fork();
if ( (int)v0 < 0 )
{
LOWORD(v0) = run_msg("In fls_main memuse_rate: fork error");
}
else
{
if ( !(_DWORD)v0 )
{
memuse_rate();
exit(0);
}
v1 = v8;
}
}
}
}
}
return v0;
}

canonical_decompiled_code:
__int16 func4()
{
time_t v0; // rax
time_t v1; // rbp
__int64 v2; // rbx
unsigned __int8 v3; // al
__int64 v4; // rax
unsigned __int8 v6; // [rsp+Fh] [rbp-29h] BYREF
time_t timer; // [rsp+10h] [rbp-28h] BYREF
time_t v8; // [rsp+18h] [rbp-20h] BYREF

time(&timer);
v0 = time(0LL);
v1 = v0;
while ( fls_switch )
{
time(&v8);
v0 = read_net();
v2 = v0;
if ( v0 )
{
LOWORD(v0) = func0(*(_WORD *)(v0 + 4));
switch ( (_WORD)v0 )
{
case 3:
ntohpt3(v2);
LODWORD(v0) = func2(v2);
if ( (_DWORD)v0 )
{
v4 = hash_search(*(unsigned __int16 *)(v2 + 68), *(unsigned int *)(v2 + 64), v8);
LOWORD(v0) = send_net4(v2, v4 != 0, v4);
}
break;
case 7:
if ( manage_switch )
{
ntohpt7(v2);
LODWORD(v0) = func3(v2);
if ( (_DWORD)v0 )
{
LOWORD(v0) = *(unsigned __int8 *)(v2 + 64);
switch ( (_BYTE)v0 )
{
case 2:
remove_rflsl(v2 + 68);
send_net8(v2, 4LL, 3LL);
LOWORD(v0) = addto_log(1LL, "fls id: %d closed.", *(unsigned __int8 *)(v2 + 68));
break;
case 3:
remove_rflsl(v2 + 68);
send_net8(v2, 4LL, 3LL);
LOWORD(v0) = addto_log(1LL, "fls id: %d disconnected!", *(unsigned __int8 *)(v2 + 68));
break;
case 1:
if ( !(unsigned int)addto_rflsl(v2 + 68) )
run_msg("fls server is too much!");
send_net8(v2, 4LL, 2LL);
LOWORD(v0) = addto_log(1LL, "fls id: %d started.", *(unsigned __int8 *)(v2 + 68));
break;
}
}
}
break;
case 1:
ntohpt1(v2);
LODWORD(v0) = func1(v2);
if ( (_DWORD)v0 )
{
if ( v8 != timer )
{
addto_log(
2LL,
"%5d %5d %5d %5d",
(unsigned int)call_0,
(unsigned int)callow_1,
(unsigned int)crefuse_2,
(unsigned int)cfail_3);
cfail_3 = 0;
crefuse_2 = 0;
callow_1 = 0;
call_0 = 0;
timer = v8;
}
++call_0;
if ( manage_switch && *(unsigned __int16 *)(v2 + 14) != rflsl[768] )
{
LOWORD(v0) = send_net6(v2);
++callow_1;
}
else
{
v3 = access_query(v2, &v6, v8);
if ( v6 == 1 )
{
if ( v3 == 1 )
++callow_1;
else
++crefuse_2;
}
else
{
++cfail_3;
}
LOWORD(v0) = send_net2(v2, v6, v3);
}
}
break;
}
if ( _memuse_rate_switch )
{
v0 = 10 * ((v8 - v1) / 10);
if ( v8 - v1 == v0 && v1 != v8 )
{
LODWORD(v0) = fork();
if ( (int)v0 < 0 )
{
LOWORD(v0) = run_msg("In fls_main memuse_rate: fork error");
}
else
{
if ( !(_DWORD)v0 )
{
memuse_rate();
exit(0);
}
v1 = v8;
}
}
}
}
}
return v0;
}

original_code:
void fls_main(void)
{
 void * ptr;
 uint16_t cmd;
 time_t now, last_mrout, last_wlog;
 static int call = 0, callow = 0, crefuse = 0, cfail = 0;
 time(&last_wlog);
 last_mrout = time(((void *)0));
 extern int __memuse_rate_switch;
 while (fls_switch)
 {
  time(&now);
  if ((ptr = read_net()) == ((void *)0))
   continue;
  cmd = ntohs(*(uint16_t *)(((char *)ptr + 4)));
  switch (cmd)
  {
   case 1:
   {
    ntohpt1(ptr);
    if (!netpt1_check(ptr))
     break;
    if (now != last_wlog)
    {
     addto_log(2, "%5d  %5d  %5d  %5d", call, callow, crefuse, cfail);
     call = callow = crefuse = cfail = 0;
     last_wlog = now;
    }
    ++call;
    if (manage_switch && ((netpt1 *)ptr)->head.fls_num != rflsl.len)
    {
     send_net6(ptr);
     ++callow;
     break;
    }
    uint8_t stat, result;
    result = access_query(ptr, &stat, now);
    if (stat == 1)
    {
     if (result == 1)
     {
      ++callow;
     }
     else
     {
      ++crefuse;
     }
    }
    else
    {
     ++cfail;
    }
    send_net2(ptr, stat, result);
    break;
   }
   case 3:
   {
    ntohpt3(ptr);
    if (!netpt3_check(ptr))
     break;
    uint8_t stat;
    recunit *unit;
    if ((unit = hash_search(((netpt3 *)ptr)->body.sid, ((netpt3 *)ptr)->body.uid, now)) == ((void *)0))
    {
     stat = 0;
    }
    else
    {
     stat = 1;
    }
    send_net4(ptr, stat, unit);
    break;
   }
   case 7:
   {
    if (!manage_switch)
     break;
    ntohpt7(ptr);
    if (!netpt7_check(ptr))
     break;
    uint8_t stat = ((netpt7 *)ptr)->body.stat;
    switch (stat)
    {
     case 1:
     {
      if (addto_rflsl(&(((netpt7 *)ptr)->body.fls)) == 0)
      {
       run_msg("fls server is too much!");
      }
      send_net8(ptr, 4, 2);
      addto_log(1, "fls id: %d started.", ((netpt7 *)ptr)->body.fls.id);
      break;
     }
     case 2:
     {
      remove_rflsl(&(((netpt7 *)ptr)->body.fls));
      send_net8(ptr, 4, 3);
      addto_log(1, "fls id: %d closed.", ((netpt7 *)ptr)->body.fls.id);
      break;
     }
     case 3:
     {
      remove_rflsl(&(((netpt7 *)ptr)->body.fls));
      send_net8(ptr, 4, 3);
      addto_log(1, "fls id: %d disconnected!", ((netpt7 *)ptr)->body.fls.id);
      break;
     }
     default:
     {
      break;
     }
    }
   }
   default:
   {
    break;
   }
  }
  if (__memuse_rate_switch && (((now - last_mrout) % 10) == 0) && last_mrout != now)
  {
   pid_t pid;
   if ((pid = fork()) < 0)
   {
    run_msg("In fls_main memuse_rate: fork error");
   }
   else if (pid == 0)
   {
    memuse_rate();
    exit(0);
   }
   else
   {
    last_mrout = now;
   }
  }
 }
}

canonical_original_code:
void fls_main()
{
 void *ptr;
 unsigned short cmd;
 long now; long last_mrout; long last_wlog;
 static int call = 0; static int callow = 0; static int crefuse = 0; static int cfail = 0;
 time(&last_wlog);
 last_mrout = time(((void *)0));
 extern int __memuse_rate_switch;
 while (fls_switch)
 {
  time(&now);
  if ((ptr = read_net()) == ((void *)0))
   continue;
  cmd = ntohs(*(unsigned short *)(((char *)ptr + 4)));
  switch (cmd)
  {
   case 1:
   {
    ntohpt1(ptr);
    if (!netpt1_check(ptr))
     break;
    if (now != last_wlog)
    {
     addto_log(2, "%5d  %5d  %5d  %5d", call, callow, crefuse, cfail);
     call = callow = crefuse = cfail = 0;
     last_wlog = now;
    }
    ++call;
    if (manage_switch && ((struct netpt1 *)ptr)->head.fls_num != rflsl.len)
    {
     send_net6(ptr);
     ++callow;
     break;
    }
    unsigned char stat; unsigned char result;
    result = access_query(ptr, &stat, now);
    if (stat == 1)
    {
     if (result == 1)
     {
      ++callow;
     }
     else
     {
      ++crefuse;
     }
    }
    else
    {
     ++cfail;
    }
    send_net2(ptr, stat, result);
    break;
   }
   case 3:
   {
    ntohpt3(ptr);
    if (!netpt3_check(ptr))
     break;
    unsigned char stat;
    struct recunit *unit;
    if ((unit = hash_search(((struct netpt3 *)ptr)->body.sid, ((struct netpt3 *)ptr)->body.uid, now)) == ((void *)0))
    {
     stat = 0;
    }
    else
    {
     stat = 1;
    }
    send_net4(ptr, stat, unit);
    break;
   }
   case 7:
   {
    if (!manage_switch)
     break;
    ntohpt7(ptr);
    if (!netpt7_check(ptr))
     break;
    unsigned char stat = ((netpt7 *)ptr)->body.stat;
    switch (stat)
    {
     case 1:
     {
      if (addto_rflsl(&(((struct netpt7 *)ptr)->body.fls)) == 0)
      {
       run_msg("fls server is too much!");
      }
      send_net8(ptr, 4, 2);
      addto_log(1, "fls id: %d started.", ((struct netpt7 *)ptr)->body.fls.id);
      break;
     }
     case 2:
     {
      remove_rflsl(&(((struct netpt7 *)ptr)->body.fls));
      send_net8(ptr, 4, 3);
      addto_log(1, "fls id: %d closed.", ((struct netpt7 *)ptr)->body.fls.id);
      break;
     }
     case 3:
     {
      remove_rflsl(&(((struct netpt7 *)ptr)->body.fls));
      send_net8(ptr, 4, 3);
      addto_log(1, "fls id: %d disconnected!", ((struct netpt7 *)ptr)->body.fls.id);
      break;
     }
     default:
     {
      break;
     }
    }
   }
   default:
   {
    break;
   }
  }
  if (__memuse_rate_switch && (((now - last_mrout) % 10) == 0) && last_mrout != now)
  {
   int pid;
   if ((pid = fork()) < 0)
   {
    run_msg("In fls_main memuse_rate: fork error");
   }
   else if (pid == 0)
   {
    memuse_rate();
    exit(0);
   }
   else
   {
    last_mrout = now;
   }
  }
 }
}

variable_types:
ptr
void *

variable_types:
cmd
unsigned short

variable_types: call int

variable_types: callow int

variable_types: crefuse int

variable_types: cfail int

variable_types:
stat
unsigned char

variable_types:
result
unsigned char

variable_types: pid int

user_defined_types: struct pbody3 { unsigned int uid; unsigned short sid; char zero[2]; }

user_defined_types: struct addr { unsigned char id; unsigned char zero; unsigned short port; unsigned int ip; }


prepareMesh

repo: halcy/rasterizer

decompiled_code:
unsigned __int64 __fastcall prepareMesh(__int64 a1)
{
int v1; // ebp
unsigned __int64 result; // rax
__int64 v3; // rdx

*(_DWORD *)(a1 + 12) = 0;
v1 = *(_DWORD *)(a1 + 8);
result = (unsigned __int64)malloc(112LL * v1);
*(_QWORD *)(a1 + 16) = result;
if ( v1 > 0 )
{
v3 = 0LL;
LODWORD(result) = 0;
do
{
*(_DWORD *)(*(_QWORD *)(a1 + 16) + v3 + 108) = result;
result = (unsigned int)(result + 1);
v3 += 112LL;
}
while ( *(_DWORD *)(a1 + 8) > (int)result );
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func1(__int64 a1)
{
int v1; // ebp
unsigned __int64 result; // rax
__int64 v3; // rdx

*(_DWORD *)(a1 + 12) = 0;
v1 = *(_DWORD *)(a1 + 8);
result = (unsigned __int64)malloc(112LL * v1);
*(_QWORD *)(a1 + 16) = result;
if ( v1 > 0 )
{
v3 = 0LL;
LODWORD(result) = 0;
do
{
*(_DWORD *)(*(_QWORD *)(a1 + 16) + v3 + 108) = result;
result = (unsigned int)(result + 1);
v3 += 112LL;
}
while ( *(_DWORD *)(a1 + 8) > (int)result );
}
return result;
}

original_code:
void prepareMesh(model* newModel) {
 newModel->curTriangle = 0;
 newModel->triangles = (triangle*)malloc(sizeof(triangle) * newModel->triangleCount);
 for (int i = 0; i < newModel->triangleCount; i++) {
  newModel->triangles[i].ID = i;
 }
}

canonical_original_code:
void prepareMesh(struct model *newModel) {
 newModel->curTriangle = 0;
 newModel->triangles = (struct triangle *)malloc(sizeof(triangle) * newModel->triangleCount);
 for (int i = 0; i < newModel->triangleCount; i++) {
  newModel->triangles[i].ID = i;
 }
}

variable_types: struct model { float *mesh; int triangleCount; int curTriangle; struct triangle { float vertices[3][3]; float normals[3][3]; float colors[3][3]; int ID; } *triangles; } *

variable_types: i int

user_defined_types: struct model { float *mesh; int triangleCount; int curTriangle; struct triangle *triangles; }


freeModel

decompiled_code:
void __fastcall freeModel(void **a1)
{
free(*a1);
free(a1[2]);
}

canonical_decompiled_code:
void __fastcall func2(void **a1)
{
free(*a1);
free(a1[2]);
}

original_code:
void freeModel(model* m) {
 free(m->mesh);
 free(m->triangles);
}

canonical_original_code:
void freeModel(struct model *m) {
 free(m->mesh);
 free(m->triangles);
}

user_defined_types: struct triangle { float vertices[3][3]; float normals[3][3]; float colors[3][3]; int ID; }


modelTrianglesLeft

decompiled_code:
_BOOL8 __fastcall modelTrianglesLeft(__int64 a1)
{
return *(_DWORD *)(a1 + 12) != *(_DWORD *)(a1 + 8);
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(__int64 a1)
{
return *(_DWORD *)(a1 + 12) != *(_DWORD *)(a1 + 8);
}

original_code:
int modelTrianglesLeft(model* m) {
 return(m->curTriangle != m->triangleCount);
}

canonical_original_code:
int modelTrianglesLeft(struct model *m) {
 return(m->curTriangle != m->triangleCount);
}


modelNextTriangle

decompiled_code:
__int64 __fastcall modelNextTriangle(__int64 a1)
{
int v1; // edx

v1 = *(_DWORD *)(a1 + 12);
*(_DWORD *)(a1 + 12) = v1 + 1;
return *(_QWORD *)(a1 + 16) + 112LL * v1;
}

original_code:
triangle* modelNextTriangle(model* m) {
 return &m->triangles[m->curTriangle++];
}

canonical_original_code:
struct triangle *modelNextTriangle(struct model *m) {
 return &m->triangles[m->curTriangle++];
}

variable_types: struct model { float *mesh; int triangleCount; int curTriangle; struct triangle *triangles; } *

return_type: struct triangle { float vertices[3][3]; float normals[3][3]; float colors[3][3]; int ID; } *


modelTriangleCount

decompiled_code:
__int64 __fastcall modelTriangleCount(__int64 a1)
{
return *(unsigned int *)(a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
return *(unsigned int *)(a1 + 8);
}

original_code:
int modelTriangleCount(model* m) {
 return m->triangleCount;
}

canonical_original_code:
int modelTriangleCount(struct model *m) {
 return m->triangleCount;
}


applyTransforms

original_code:
void applyTransforms(model* m, matrix mvMatrixO, matrix pMatrixO) {
 vec3 triVec;
 vec3 transVec;
 vec3 projVec;
 for(int i = 0; i < m->triangleCount; i++) {
  for(int j = 0; j < 6; j++) {
   triVec = makeVec3(
    m->mesh[m->triangles[i].ID * 18 + j*3],
    m->mesh[m->triangles[i].ID * 18 + j*3 + 1],
    m->mesh[m->triangles[i].ID * 18 + j*3 + 2]
   );
   if (j % 2 == 0) {
    matrixApply(&transVec, mvMatrixO, triVec);
    matrixApplyPerspective(&projVec, pMatrixO, transVec);
    m->triangles[i].vertices[j/2][0] = projVec.x;
    m->triangles[i].vertices[j/2][1] = projVec.y;
    m->triangles[i].vertices[j/2][2] = projVec.z;
   }
   else {
    matrixApplyNormal(&transVec, mvMatrixO, triVec);
    m->triangles[i].normals[j/2][0] = transVec.x;
    m->triangles[i].normals[j/2][1] = transVec.y;
    m->triangles[i].normals[j/2][2] = transVec.z;
   }
  }
 }
 m->curTriangle = 0;
}

canonical_original_code:
void applyTransforms(struct model *m, struct matrix mvMatrixO, struct matrix pMatrixO) {
 struct vec3 triVec;
 struct vec3 transVec;
 struct vec3 projVec;
 for(int i = 0; i < m->triangleCount; i++) {
  for(int j = 0; j < 6; j++) {
   triVec = makeVec3(
    m->mesh[m->triangles[i].ID * 18 + j*3],
    m->mesh[m->triangles[i].ID * 18 + j*3 + 1],
    m->mesh[m->triangles[i].ID * 18 + j*3 + 2]
   );
   if (j % 2 == 0) {
    matrixApply(&transVec, mvMatrixO, triVec);
    matrixApplyPerspective(&projVec, pMatrixO, transVec);
    m->triangles[i].vertices[j/2][0] = projVec.x;
    m->triangles[i].vertices[j/2][1] = projVec.y;
    m->triangles[i].vertices[j/2][2] = projVec.z;
   }
   else {
    matrixApplyNormal(&transVec, mvMatrixO, triVec);
    m->triangles[i].normals[j/2][0] = transVec.x;
    m->triangles[i].normals[j/2][1] = transVec.y;
    m->triangles[i].normals[j/2][2] = transVec.z;
   }
  }
 }
 m->curTriangle = 0;
}

variable_types:
mvMatrixO
struct matrix { float v[16]; }

variable_types:
pMatrixO
struct matrix

variable_types: struct vec3 { float x; float y; float z; }

variable_types: j int


shade

original_code:
void shade(model* m, float lx, float ly, float lz) {
 for(int i = 0; i != m->triangleCount; i++) {
  for(int j = 0; j < 3; j++) {
   for(int c = 0; c < 3; c++) {
    float Lx = lx - m->mesh[m->triangles[i].ID * 18 + j*6];
    float Ly = ly - m->mesh[m->triangles[i].ID * 18 + j*6 + 1];
    float Lz = lz - m->mesh[m->triangles[i].ID * 18 + j*6 + 2];
    float lenL = sqrt(Lx*Lx + Ly*Ly + Lz*Lz);
    Lx /= lenL;
    Ly /= lenL;
    Lz /= lenL;
    float NdotL =
     m->triangles[i].normals[j][0] * Lx +
     m->triangles[i].normals[j][1] * Ly +
     m->triangles[i].normals[j][2] * Lz;
    if (NdotL < 0.0f) {
     NdotL = 0.0f;
    }
    m->triangles[i].colors[j][c] = NdotL;
   }
  }
 }
}

canonical_original_code:
void shade(struct model *m, float lx, float ly, float lz) {
 for(int i = 0; i != m->triangleCount; i++) {
  for(int j = 0; j < 3; j++) {
   for(int c = 0; c < 3; c++) {
    float Lx = lx - m->mesh[m->triangles[i].ID * 18 + j*6];
    float Ly = ly - m->mesh[m->triangles[i].ID * 18 + j*6 + 1];
    float Lz = lz - m->mesh[m->triangles[i].ID * 18 + j*6 + 2];
    float lenL = sqrt(Lx*Lx + Ly*Ly + Lz*Lz);
    Lx /= lenL;
    Ly /= lenL;
    Lz /= lenL;
    float NdotL = m->triangles[i].normals[j][0] * Lx +
     m->triangles[i].normals[j][1] * Ly +
     m->triangles[i].normals[j][2] * Lz;
    if (NdotL < 0.0f) {
     NdotL = 0.0f;
    }
    m->triangles[i].colors[j][c] = NdotL;
   }
  }
 }
}

variable_types: lx float

variable_types: ly float

variable_types: lz float

variable_types: c int

variable_types: Lx float

variable_types: Ly float

variable_types: Lz float

variable_types: lenL float

variable_types: NdotL float


original_code:
void bmp_init( const char* file_name, int x_size, int y_size ) {
 bmp_header file_head;
 file_head.file_type [ 0 ] = 'B';
 file_head.file_type [ 1 ] = 'M';
 file_head.file_size = x_size * y_size * 3 + 54;
 file_head.reserved_1 = 0;
 file_head.reserved_2 = 0;
 file_head.pixel_offset = 54;
 file_head.header_size = 40;
 file_head.x_size = x_size;
 file_head.y_size = y_size;
 file_head.planes = 1;
 file_head.bpp = 24;
 file_head.compression = 0;
 file_head.image_size = 0;
 file_head.x_ppm = 0;
 file_head.y_ppm = 0;
 file_head.used_colors = 0;
 file_head.important_colors = 0;
 bmp_file = fopen( file_name, "w" );
 fwrite( (char*)(&file_head), 1, sizeof(file_head), bmp_file );
 line_max = x_size * 3;
 line_pos = 0;
}

canonical_original_code:
void bmp_init(char *file_name, int x_size, int y_size) {
 struct bmp_header file_head;
 file_head.file_type [ 0 ] = 'B';
 file_head.file_type [ 1 ] = 'M';
 file_head.file_size = x_size * y_size * 3 + 54;
 file_head.reserved_1 = 0;
 file_head.reserved_2 = 0;
 file_head.pixel_offset = 54;
 file_head.header_size = 40;
 file_head.x_size = x_size;
 file_head.y_size = y_size;
 file_head.planes = 1;
 file_head.bpp = 24;
 file_head.compression = 0;
 file_head.image_size = 0;
 file_head.x_ppm = 0;
 file_head.y_ppm = 0;
 file_head.used_colors = 0;
 file_head.important_colors = 0;
 bmp_file = fopen( file_name, "w" );
 fwrite( (char *)(&file_head), 1, sizeof(file_head), bmp_file );
 line_max = x_size * 3;
 line_pos = 0;
}

variable_types: struct bmp_header { unsigned char file_type[2]; unsigned int file_size; unsigned short reserved_1; unsigned short reserved_2; unsigned int pixel_offset; unsigned int header_size; unsigned int x_size; unsigned int y_size; unsigned short planes; unsigned short bpp; unsigned int compression; unsigned int image_size; unsigned int x_ppm; unsigned int y_ppm; unsigned int used_colors; unsigned int important_colors; }


decompiled_code:
__int64 __fastcall bmp_pixel(char a1, char a2, char a3)
{
__int64 result; // rax
char ptr; // [rsp+Dh] [rbp-Bh] BYREF
char v5; // [rsp+Eh] [rbp-Ah] BYREF
char v6[9]; // [rsp+Fh] [rbp-9h] BYREF

v6[0] = a1;
v5 = a2;
ptr = a3;
fwrite(&ptr, 1uLL, 1uLL, bmp_file);
fwrite(&v5, 1uLL, 1uLL, bmp_file);
fwrite(v6, 1uLL, 1uLL, bmp_file);
result = (unsigned int)(line_pos + 3);
line_pos = result;
if ( (_DWORD)result == line_max )
{
if ( (int)result < line_max % 4 + line_max )
{
do
{
fwrite(&ptr, 1uLL, 1uLL, bmp_file);
++line_pos;
result = (unsigned int)(line_max + line_max % 4);
}
while ( line_pos < (int)result );
}
line_pos = 0;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(char a1, char a2, char a3)
{
__int64 result; // rax
char ptr; // [rsp+Dh] [rbp-Bh] BYREF
char v5; // [rsp+Eh] [rbp-Ah] BYREF
char v6[9]; // [rsp+Fh] [rbp-9h] BYREF

v6[0] = a1;
v5 = a2;
ptr = a3;
fwrite(&ptr, 1uLL, 1uLL, bmp_file);
fwrite(&v5, 1uLL, 1uLL, bmp_file);
fwrite(v6, 1uLL, 1uLL, bmp_file);
result = (unsigned int)(line_pos + 3);
line_pos = result;
if ( (_DWORD)result == line_max )
{
if ( (int)result < line_max % 4 + line_max )
{
do
{
fwrite(&ptr, 1uLL, 1uLL, bmp_file);
++line_pos;
result = (unsigned int)(line_max + line_max % 4);
}
while ( line_pos < (int)result );
}
line_pos = 0;
}
return result;
}

original_code:
void bmp_pixel(int r, int g, int b) {
 char tmp_r = (char)r;
 char tmp_g = (char)g;
 char tmp_b = (char)b;
 fwrite( &tmp_b, 1, 1, bmp_file );
 fwrite( &tmp_g, 1, 1, bmp_file );
 fwrite( &tmp_r, 1, 1, bmp_file );
 line_pos+=3;
 if( line_pos == line_max ) {
  while( line_pos < line_max + line_max % 4 ) {
   fwrite( &tmp_b, 1, 1, bmp_file );
   line_pos++;
  }
  line_pos = 0;
 }
}

variable_types: r int

variable_types: g int

variable_types: b int


decompiled_code:
int bmp_close()
{
fflush(bmp_file);
return fclose(bmp_file);
}

canonical_decompiled_code:
int func2()
{
fflush(bmp_file);
return fclose(bmp_file);
}

original_code:
void bmp_close() {
 fflush( bmp_file );
 fclose( bmp_file );
}


setPixel

decompiled_code:
void __fastcall setPixel(int a1, int a2, double a3, double a4, __int64 a5, __int64 a6, __int64 a7, __int64 a8, int a9, int a10, __int64 a11)
{
double *v11; // rdi

v11 = (double *)(a11 + 16LL * (a9 * a2 + a1));
*v11 = a3;
v11[1] = a4;
}

canonical_decompiled_code:
void __fastcall func5(int a1, int a2, double a3, double a4, __int64 a5, __int64 a6, __int64 a7, __int64 a8, int a9, int a10, __int64 a11)
{
double *v11; // rdi

v11 = (double *)(a11 + 16LL * (a9 * a2 + a1));
*v11 = a3;
v11[1] = a4;
}

original_code:
inline void setPixel(buffer b, int x, int y, colour c) {
 b.data[x+b.width*y] = c;
}

canonical_original_code:
inline void setPixel(struct buffer b, int x, int y, struct colour c) {
 b.data[x+b.width*y] = c;
}

variable_types: struct buffer { int width; int firstLine; int size; struct colour { float r; float b; float g; float a; } *data; }

variable_types: x int

variable_types: y int

variable_types:
c
struct colour

user_defined_types: struct buffer { int width; int firstLine; int size; struct colour *data; }


expose

decompiled_code:
__int64 __fastcall expose(float a1, float a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7, __int64 a8, __int64 a9, int a10, __int64 a11)
{
__int64 result; // rax
__int64 v12; // r14
int v13; // ebp
__int64 v14; // rbx

result = a11;
if ( (int)a9 > 0 )
{
result = (unsigned int)a9;
v12 = 0LL;
do
{
v13 = HIDWORD(a9);
if ( a10 > SHIDWORD(a9) )
{
v14 = a11 + 16 * (v12 + HIDWORD(a9) * (int)a9);
do
{
scale(v14, a1);
applyGamma(v14, 1.0 / a2);
result = clip(v14);
++v13;
v14 += 16LL * (int)a9;
}
while ( a10 != v13 );
}
++v12;
}
while ( v12 != (unsigned int)a9 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(float a1, float a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7, __int64 a8, __int64 a9, int a10, __int64 a11)
{
__int64 result; // rax
__int64 v12; // r14
int v13; // ebp
__int64 v14; // rbx

result = a11;
if ( (int)a9 > 0 )
{
result = (unsigned int)a9;
v12 = 0LL;
do
{
v13 = HIDWORD(a9);
if ( a10 > SHIDWORD(a9) )
{
v14 = a11 + 16 * (v12 + HIDWORD(a9) * (int)a9);
do
{
scale(v14, a1);
applyGamma(v14, 1.0 / a2);
result = clip(v14);
++v13;
v14 += 16LL * (int)a9;
}
while ( a10 != v13 );
}
++v12;
}
while ( v12 != (unsigned int)a9 );
}
return result;
}

original_code:
void expose(buffer b, float factor, float gamma) {
 for( int x = 0; x < b.width; x++ ) {
  for( int y = b.firstLine; y < b.size; y++ ) {
   scale( &b.data[x+b.width*y], factor );
   applyGamma( &b.data[x+b.width*y], 1.0f / gamma);
   clip( &b.data[x+b.width*y] );
  }
 }
}

canonical_original_code:
void expose(struct buffer b, float factor, float gamma) {
 for( int x = 0; x < b.width; x++ ) {
  for( int y = b.firstLine; y < b.size; y++ ) {
   scale( &b.data[x+b.width*y], factor );
   applyGamma( &b.data[x+b.width*y], 1.0f / gamma);
   clip( &b.data[x+b.width*y] );
  }
 }
}

variable_types: factor float

variable_types: gamma float

user_defined_types: struct colour { float r; float b; float g; float a; }


clear

original_code:
void clear(buffer b) {
 for( int x = 0; x < b.width; x++ ) {
  for( int y = b.firstLine; y < b.size; y++ ) {
   b.data[x+b.width*y] = (makeColour( 0.0, 0.0, 0.0 ));
  }
 }
}

canonical_original_code:
void clear(struct buffer b) {
 for( int x = 0; x < b.width; x++ ) {
  for( int y = b.firstLine; y < b.size; y++ ) {
   b.data[x+b.width*y] = (makeColour( 0.0, 0.0, 0.0 ));
  }
 }
}


writeToImage

original_code:
void writeToImage(buffer b, const char* filename) {
 bmp_init( filename, b.width, b.size );
 for( int y = b.firstLine; y < b.size; y++ ) {
  for( int x = 0; x < b.width; x++ ) {
   vec3 c = getRGB( b.data[x+b.width*y] );
   bmp_pixel( (int)c.x, (int)c.y, (int)c.z );
  }
 }
 bmp_close();
}

canonical_original_code:
void writeToImage(struct buffer b, char *filename) {
 bmp_init( filename, b.width, b.size );
 for( int y = b.firstLine; y < b.size; y++ ) {
  for( int x = 0; x < b.width; x++ ) {
   struct vec3 c = getRGB( b.data[x+b.width*y] );
   bmp_pixel( (int)c.x, (int)c.y, (int)c.z );
  }
 }
 bmp_close();
}

variable_types:
filename
char *

variable_types: struct vec3 { float x; float y; float z; }


freeBuffer

decompiled_code:
void __fastcall freeBuffer(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, void *ptr)
{
free(ptr);
}

canonical_decompiled_code:
void __fastcall func9(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, void *ptr)
{
free(ptr);
}

original_code:
void freeBuffer(buffer b) {
 free(b.data);
}

canonical_original_code:
void freeBuffer(struct buffer b) {
 free(b.data);
}


rasterize

decompiled_code:
__int64 __fastcall rasterize(__int64 a1, int *a2, __int64 a3)
{
__int64 result; // rax
float *v4; // rax
float *v5; // rbp
float *v6; // rdx
float *v7; // rcx
float *v8; // rax
float *v9; // rsi
int v10; // ecx
__int64 v11; // rax
float v12; // xmm3_4
float v13; // xmm2_4
float v14; // xmm0_4
float v15; // xmm4_4
float v16; // xmm0_4
int i; // ecx
double v18; // xmm0_8
int v19; // esi
double v20; // xmm4_8
int v21; // edx
double v22; // xmm2_8
int v23; // ecx
double v24; // xmm0_8
int v25; // eax
int v26; // er15
int v27; // er12
int v28; // er13
int v29; // eax
int v30; // er14
int v31; // er15
int v32; // ebx
float v33; // xmm1_4
float v34; // xmm0_4
float v35; // xmm2_4
float v36; // xmm1_4
float v37; // xmm0_4
float v38; // xmm2_4
float v39; // xmm1_4
float v40; // xmm3_4
float *v41; // rax
double v42; // xmm4_8
double v43; // xmm3_8
float v44; // [rsp+4h] [rbp-D4h]
float v45; // [rsp+Ch] [rbp-CCh]
float v46; // [rsp+14h] [rbp-C4h]
int v48; // [rsp+28h] [rbp-B0h]
int v49; // [rsp+2Ch] [rbp-ACh]
float v50; // [rsp+30h] [rbp-A8h]
float v51; // [rsp+34h] [rbp-A4h]
float v52; // [rsp+40h] [rbp-98h]
float v53; // [rsp+44h] [rbp-94h]
int v54; // [rsp+4Ch] [rbp-8Ch]
float v55; // [rsp+50h] [rbp-88h] BYREF
float v56; // [rsp+54h] [rbp-84h]
float v57; // [rsp+58h] [rbp-80h]
float v58; // [rsp+5Ch] [rbp-7Ch] BYREF
float v59; // [rsp+60h] [rbp-78h]
float v60; // [rsp+64h] [rbp-74h]
float v61; // [rsp+68h] [rbp-70h]
float v62; // [rsp+6Ch] [rbp-6Ch]
float v63; // [rsp+70h] [rbp-68h]
float v64; // [rsp+74h] [rbp-64h]
float v65; // [rsp+78h] [rbp-60h]
float v66; // [rsp+7Ch] [rbp-5Ch]
float v67; // [rsp+80h] [rbp-58h]
float v68; // [rsp+84h] [rbp-54h]
float v69; // [rsp+88h] [rbp-50h]

v48 = *a2;
v54 = a2[2];
v53 = (float)(*a2 / 2);
while ( 1 )
{
result = modelTrianglesLeft(a1);
if ( !(_DWORD)result )
break;
v4 = (float *)modelNextTriangle(a1);
v5 = v4;
if ( (float)((float)((float)(v4[3] - *v4) * (float)(v4[7] - v4[1]))
+ (float)((float)(v4[6] - *v4) * (float)(v4[1] - v4[4]))) >= 0.0 )
{
v6 = &v55;
v7 = v4 + 1;
v8 = &v55;
do
{
*v8 = (float)(*(v7 - 1) + 1.0) * v53;
v8[3] = (float)(*v7 + 1.0) * (float)(v54 / 2);
++v8;
v7 += 3;
}
while ( v8 != &v58 );
v9 = &v55;
v10 = 0;
do
{
v11 = ++v10 % 3;
v12 = v9[3] - *(&v58 + v11);
v13 = *(&v55 + v11) - *v9;
v14 = (float)(v13 * v13) + (float)(v12 * v12);
v15 = 1.0 / fsqrt(v14);
v16 = (float)((float)((float)(v14 * v15) * v15) + -3.0) * (float)(v15 * -0.5);
v9[6] = v12 * v16;
v9[9] = v16 * v13;
++v9;
}
while ( v10 != 3 );
for ( i = 2; i != 5; ++i )
{
v6[12] = (float)((float)(*(&v55 + i % 3) - *v6) * v6[6]) + (float)((float)(*(&v58 + i % 3) - v6[3]) * v6[9]);
++v6;
}
v46 = v57;
v45 = v56;
v44 = v55;
v18 = fmin(fmin(v57, v56), v55);
v19 = (int)v18;
if ( (double)(int)v18 > v18 )
v19 = (int)v18 - 1;
v50 = v60;
v51 = v59;
v52 = v58;
v20 = fmin(fmin(v60, v59), v58);
v21 = (int)v20;
if ( (double)(int)v20 > v20 )
v21 = (int)v20 - 1;
v22 = fmax(fmax(v56, v57), v55);
v23 = (int)v22;
if ( v22 > (double)(int)v22 )
v23 = (int)v22 + 1;
v24 = fmax(fmax(v59, v60), v58);
v25 = (int)v24 + 1;
if ( v24 <= (double)(int)v24 )
v25 = (int)v24;
v26 = (int)fmax((double)v19, 0.0);
v27 = (int)fmin((double)v23, (double)v48);
v28 = (int)fmax((double)v21, 0.0);
v29 = (int)fmin((double)v25, (double)v54);
v49 = v29;
if ( v26 <= v27 && v28 < v29 )
{
v30 = v26;
v31 = v28 * v48;
do
{
v32 = v30;
if ( v30 < v27 )
{
do
{
v33 = (float)v32;
v34 = (float)((float)((float)v32 - v44) * v61) + (float)((float)((float)v28 - v52) * v64);
if ( v34 >= 0.0 )
{
v35 = (float)((float)(v33 - v45) * v62) + (float)((float)((float)v28 - v51) * v65);
if ( v35 >= 0.0 )
{
v36 = (float)((float)(v33 - v46) * v63) + (float)((float)((float)v28 - v50) * v66);
if ( v36 >= 0.0 )
{
v37 = v34 / v67;
v38 = v35 / v68;
v39 = v36 / v69;
v40 = (float)((float)(v38 / v5[2]) + (float)(v39 / v5[5])) + (float)(v37 / v5[8]);
v41 = (float *)(a3 + 4LL * (v32 + v31));
if ( v40 > *v41 )
{
*v41 = v40;
HIDWORD(v42) = 0;
HIDWORD(v43) = 0;
*(float *)&v42 = (float)((float)(v38 * v5[20]) + (float)(v39 * v5[23])) + (float)(v37 * v5[26]);
*(float *)&v43 = (float)((float)(v38 * v5[19]) + (float)(v39 * v5[22])) + (float)(v37 * v5[25]);
makeColour(
(float)(v37 * v5[24]) + (float)((float)(v39 * v5[21]) + (float)(v38 * v5[18])),
v43,
v42);
setPixel((unsigned int)v32, (unsigned int)v28);
}
}
}
}
++v32;
}
while ( v27 != v32 );
}
++v28;
v31 += v48;
}
while ( v49 != v28 );
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int *a2, __int64 a3)
{
__int64 result; // rax
float *v4; // rax
float *v5; // rbp
float *v6; // rdx
float *v7; // rcx
float *v8; // rax
float *v9; // rsi
int v10; // ecx
__int64 v11; // rax
float v12; // xmm3_4
float v13; // xmm2_4
float v14; // xmm0_4
float v15; // xmm4_4
float v16; // xmm0_4
int i; // ecx
double v18; // xmm0_8
int v19; // esi
double v20; // xmm4_8
int v21; // edx
double v22; // xmm2_8
int v23; // ecx
double v24; // xmm0_8
int v25; // eax
int v26; // er15
int v27; // er12
int v28; // er13
int v29; // eax
int v30; // er14
int v31; // er15
int v32; // ebx
float v33; // xmm1_4
float v34; // xmm0_4
float v35; // xmm2_4
float v36; // xmm1_4
float v37; // xmm0_4
float v38; // xmm2_4
float v39; // xmm1_4
float v40; // xmm3_4
float *v41; // rax
double v42; // xmm4_8
double v43; // xmm3_8
float v44; // [rsp+4h] [rbp-D4h]
float v45; // [rsp+Ch] [rbp-CCh]
float v46; // [rsp+14h] [rbp-C4h]
int v48; // [rsp+28h] [rbp-B0h]
int v49; // [rsp+2Ch] [rbp-ACh]
float v50; // [rsp+30h] [rbp-A8h]
float v51; // [rsp+34h] [rbp-A4h]
float v52; // [rsp+40h] [rbp-98h]
float v53; // [rsp+44h] [rbp-94h]
int v54; // [rsp+4Ch] [rbp-8Ch]
float v55; // [rsp+50h] [rbp-88h] BYREF
float v56; // [rsp+54h] [rbp-84h]
float v57; // [rsp+58h] [rbp-80h]
float v58; // [rsp+5Ch] [rbp-7Ch] BYREF
float v59; // [rsp+60h] [rbp-78h]
float v60; // [rsp+64h] [rbp-74h]
float v61; // [rsp+68h] [rbp-70h]
float v62; // [rsp+6Ch] [rbp-6Ch]
float v63; // [rsp+70h] [rbp-68h]
float v64; // [rsp+74h] [rbp-64h]
float v65; // [rsp+78h] [rbp-60h]
float v66; // [rsp+7Ch] [rbp-5Ch]
float v67; // [rsp+80h] [rbp-58h]
float v68; // [rsp+84h] [rbp-54h]
float v69; // [rsp+88h] [rbp-50h]

v48 = *a2;
v54 = a2[2];
v53 = (float)(*a2 / 2);
while ( 1 )
{
result = modelTrianglesLeft(a1);
if ( !(_DWORD)result )
break;
v4 = (float *)modelNextTriangle(a1);
v5 = v4;
if ( (float)((float)((float)(v4[3] - *v4) * (float)(v4[7] - v4[1]))
+ (float)((float)(v4[6] - *v4) * (float)(v4[1] - v4[4]))) >= 0.0 )
{
v6 = &v55;
v7 = v4 + 1;
v8 = &v55;
do
{
*v8 = (float)(*(v7 - 1) + 1.0) * v53;
v8[3] = (float)(*v7 + 1.0) * (float)(v54 / 2);
++v8;
v7 += 3;
}
while ( v8 != &v58 );
v9 = &v55;
v10 = 0;
do
{
v11 = ++v10 % 3;
v12 = v9[3] - *(&v58 + v11);
v13 = *(&v55 + v11) - *v9;
v14 = (float)(v13 * v13) + (float)(v12 * v12);
v15 = 1.0 / fsqrt(v14);
v16 = (float)((float)((float)(v14 * v15) * v15) + -3.0) * (float)(v15 * -0.5);
v9[6] = v12 * v16;
v9[9] = v16 * v13;
++v9;
}
while ( v10 != 3 );
for ( i = 2; i != 5; ++i )
{
v6[12] = (float)((float)(*(&v55 + i % 3) - *v6) * v6[6]) + (float)((float)(*(&v58 + i % 3) - v6[3]) * v6[9]);
++v6;
}
v46 = v57;
v45 = v56;
v44 = v55;
v18 = fmin(fmin(v57, v56), v55);
v19 = (int)v18;
if ( (double)(int)v18 > v18 )
v19 = (int)v18 - 1;
v50 = v60;
v51 = v59;
v52 = v58;
v20 = fmin(fmin(v60, v59), v58);
v21 = (int)v20;
if ( (double)(int)v20 > v20 )
v21 = (int)v20 - 1;
v22 = fmax(fmax(v56, v57), v55);
v23 = (int)v22;
if ( v22 > (double)(int)v22 )
v23 = (int)v22 + 1;
v24 = fmax(fmax(v59, v60), v58);
v25 = (int)v24 + 1;
if ( v24 <= (double)(int)v24 )
v25 = (int)v24;
v26 = (int)fmax((double)v19, 0.0);
v27 = (int)fmin((double)v23, (double)v48);
v28 = (int)fmax((double)v21, 0.0);
v29 = (int)fmin((double)v25, (double)v54);
v49 = v29;
if ( v26 <= v27 && v28 < v29 )
{
v30 = v26;
v31 = v28 * v48;
do
{
v32 = v30;
if ( v30 < v27 )
{
do
{
v33 = (float)v32;
v34 = (float)((float)((float)v32 - v44) * v61) + (float)((float)((float)v28 - v52) * v64);
if ( v34 >= 0.0 )
{
v35 = (float)((float)(v33 - v45) * v62) + (float)((float)((float)v28 - v51) * v65);
if ( v35 >= 0.0 )
{
v36 = (float)((float)(v33 - v46) * v63) + (float)((float)((float)v28 - v50) * v66);
if ( v36 >= 0.0 )
{
v37 = v34 / v67;
v38 = v35 / v68;
v39 = v36 / v69;
v40 = (float)((float)(v38 / v5[2]) + (float)(v39 / v5[5])) + (float)(v37 / v5[8]);
v41 = (float *)(a3 + 4LL * (v32 + v31));
if ( v40 > *v41 )
{
*v41 = v40;
HIDWORD(v42) = 0;
HIDWORD(v43) = 0;
*(float *)&v42 = (float)((float)(v38 * v5[20]) + (float)(v39 * v5[23])) + (float)(v37 * v5[26]);
*(float *)&v43 = (float)((float)(v38 * v5[19]) + (float)(v39 * v5[22])) + (float)(v37 * v5[25]);
makeColour(
(float)(v37 * v5[24]) + (float)((float)(v39 * v5[21]) + (float)(v38 * v5[18])),
v43,
v42);
setPixel((unsigned int)v32, (unsigned int)v28);
}
}
}
}
++v32;
}
while ( v27 != v32 );
}
++v28;
v31 += v48;
}
while ( v49 != v28 );
}
}
}
return result;
}

original_code:
void rasterize(model* m, buffer* pbuf, float* zbuf) {
 int width = pbuf->width;
 int height = pbuf->size;
 tri t;
 triangle* modelTri;
 float d1;
 float d2;
 float d3;
 float r;
 float g;
 float b;
 int x;
 int y;
 float z;
 int i;
 float l;
 while(modelTrianglesLeft(m)) {
  modelTri = modelNextTriangle(m);
  if(
   (modelTri->vertices[1][0] - modelTri->vertices[0][0]) *
   (modelTri->vertices[2][1] - modelTri->vertices[0][1]) -
   (modelTri->vertices[2][0] - modelTri->vertices[0][0]) *
   (modelTri->vertices[1][1] - modelTri->vertices[0][1])
   < 0
  ) {
   continue;
  }
  for( i = 0; i < 3; i++ ) {
   t.sx[i] = (((modelTri->vertices[i][0])+1)*((float)(width/2)));
   t.sy[i] = (((modelTri->vertices[i][1])+1)*((float)(height/2)));
  }
  for( i = 0; i < 3; i++ ) {
   t.nx[i] = -(t.sy[(i+1)%3] - t.sy[i]);
   t.ny[i] = (t.sx[(i+1)%3] - t.sx[i]);
   l = sqrt( t.nx[i] * t.nx[i] + t.ny[i] * t.ny[i] );
   t.nx[i] /= l;
   t.ny[i] /= l;
  }
  for( i = 0; i < 3; i++ ) {
   t.b[i] = t.nx[i] * ( t.sx[(i+2)%3] - t.sx[i] ) + t.ny[i] * ( t.sy[(i+2)%3] - t.sy[i] );
  }
  t.xmin = floor( fmin( fmin( t.sx[0], t.sx[1] ), t.sx[2] ) );
  t.ymin = floor( fmin( fmin( t.sy[0], t.sy[1] ), t.sy[2] ) );
  t.xmax = ceil( fmax( fmax( t.sx[0], t.sx[1] ), t.sx[2] ) );
  t.ymax = ceil( fmax( fmax( t.sy[0], t.sy[1] ), t.sy[2] ) );
  t.xmin = fmax(0, t.xmin);
  t.xmax = fmin(width, t.xmax);
  t.ymin = fmax(0, t.ymin);
  t.ymax = fmin(height, t.ymax);
  if(t.ymin > t.ymax || t.xmin > t.xmax) {
   continue;
  }
  for( y = t.ymin; y < t.ymax; y++ ) {
   for( x = t.xmin; x < t.xmax; x++ ) {
    d1 = t.nx[0] * ( x - t.sx[0] ) + t.ny[0] * ( y - t.sy[0] );
    if( d1 >= 0 ) {
     d2 = t.nx[1] * ( x - t.sx[1] ) + t.ny[1] * ( y - t.sy[1] );
     if( d2 >= 0 ) {
      d3 = t.nx[2] * ( x - t.sx[2] ) + t.ny[2] * ( y - t.sy[2] );
      if( d3 >= 0 ) {
       d1 /= t.b[0];
       d2 /= t.b[1];
       d3 /= t.b[2];
       z =
        1.0f / modelTri->vertices[0][2] * d2 +
        1.0f / modelTri->vertices[1][2] * d3 +
        1.0f / modelTri->vertices[2][2] * d1;
       if( z > zbuf[y*width+x] ) {
        zbuf[y*width+x] = z;
        r =
         modelTri->colors[0][0] * d2 +
         modelTri->colors[1][0] * d3 +
         modelTri->colors[2][0] * d1;
        g =
         modelTri->colors[0][1] * d2 +
         modelTri->colors[1][1] * d3 +
         modelTri->colors[2][1] * d1;
        b =
         modelTri->colors[0][2] * d2 +
         modelTri->colors[1][2] * d3 +
         modelTri->colors[2][2] * d1;
        setPixel(*pbuf, x, y, makeColour(r,g,b));
       }
      }
     }
    }
   }
  }
 }
}

canonical_original_code:
void rasterize(struct model *m, struct buffer *pbuf, float *zbuf) {
 int width = pbuf->width;
 int height = pbuf->size;
 struct tri t;
 struct triangle *modelTri;
 float d1;
 float d2;
 float d3;
 float r;
 float g;
 float b;
 int x;
 int y;
 float z;
 int i;
 float l;
 while(modelTrianglesLeft(m)) {
  modelTri = modelNextTriangle(m);
  if(
   (modelTri->vertices[1][0] - modelTri->vertices[0][0]) *
   (modelTri->vertices[2][1] - modelTri->vertices[0][1]) -
   (modelTri->vertices[2][0] - modelTri->vertices[0][0]) *
   (modelTri->vertices[1][1] - modelTri->vertices[0][1])
   < 0
  ) {
   continue;
  }
  for( i = 0; i < 3; i++ ) {
   t.sx[i] = (((modelTri->vertices[i][0])+1)*((float)(width/2)));
   t.sy[i] = (((modelTri->vertices[i][1])+1)*((float)(height/2)));
  }
  for( i = 0; i < 3; i++ ) {
   t.nx[i] = -(t.sy[(i+1)%3] - t.sy[i]);
   t.ny[i] = (t.sx[(i+1)%3] - t.sx[i]);
   l = sqrt( t.nx[i] * t.nx[i] + t.ny[i] * t.ny[i] );
   t.nx[i] /= l;
   t.ny[i] /= l;
  }
  for( i = 0; i < 3; i++ ) {
   t.b[i] = t.nx[i] * ( t.sx[(i+2)%3] - t.sx[i] ) + t.ny[i] * ( t.sy[(i+2)%3] - t.sy[i] );
  }
  t.xmin = floor( fmin( fmin( t.sx[0], t.sx[1] ), t.sx[2] ) );
  t.ymin = floor( fmin( fmin( t.sy[0], t.sy[1] ), t.sy[2] ) );
  t.xmax = ceil( fmax( fmax( t.sx[0], t.sx[1] ), t.sx[2] ) );
  t.ymax = ceil( fmax( fmax( t.sy[0], t.sy[1] ), t.sy[2] ) );
  t.xmin = fmax(0, t.xmin);
  t.xmax = fmin(width, t.xmax);
  t.ymin = fmax(0, t.ymin);
  t.ymax = fmin(height, t.ymax);
  if(t.ymin > t.ymax || t.xmin > t.xmax) {
   continue;
  }
  for( y = t.ymin; y < t.ymax; y++ ) {
   for( x = t.xmin; x < t.xmax; x++ ) {
    d1 = t.nx[0] * ( x - t.sx[0] ) + t.ny[0] * ( y - t.sy[0] );
    if( d1 >= 0 ) {
     d2 = t.nx[1] * ( x - t.sx[1] ) + t.ny[1] * ( y - t.sy[1] );
     if( d2 >= 0 ) {
      d3 = t.nx[2] * ( x - t.sx[2] ) + t.ny[2] * ( y - t.sy[2] );
      if( d3 >= 0 ) {
       d1 /= t.b[0];
       d2 /= t.b[1];
       d3 /= t.b[2];
       z =
        1.0f / modelTri->vertices[0][2] * d2 +
        1.0f / modelTri->vertices[1][2] * d3 +
        1.0f / modelTri->vertices[2][2] * d1;
       if( z > zbuf[y*width+x] ) {
        zbuf[y*width+x] = z;
        r =
         modelTri->colors[0][0] * d2 +
         modelTri->colors[1][0] * d3 +
         modelTri->colors[2][0] * d1;
        g =
         modelTri->colors[0][1] * d2 +
         modelTri->colors[1][1] * d3 +
         modelTri->colors[2][1] * d1;
        b =
         modelTri->colors[0][2] * d2 +
         modelTri->colors[1][2] * d3 +
         modelTri->colors[2][2] * d1;
        setPixel(*pbuf, x, y, makeColour(r,g,b));
       }
      }
     }
    }
   }
  }
 }
}

variable_types: struct model { float *mesh; int triangleCount; int curTriangle; struct triangle { float vertices[3][3]; float normals[3][3]; float colors[3][3]; int ID; } *triangles; } *

variable_types: struct buffer { int width; int firstLine; int size; struct colour { float r; float b; float g; float a; } *data; } *

variable_types:
zbuf
float *

variable_types: width int

variable_types: height int

variable_types: struct tri { float sx[3]; float sy[3]; float nx[3]; float ny[3]; float b[3]; int xmin; int xmax; int ymin; int ymax; }

variable_types: r float

variable_types: g float

variable_types: b float

variable_types: z float

variable_types: i int

variable_types: l float


decompiled_code:
double __fastcall makeVec2(__m128i a1, __m128i a2)
{
double result; // xmm0_8

*(_QWORD *)&result = ((unsigned __int64)(unsigned int)_mm_cvtsi128_si32(a2) << 32) | (unsigned int)_mm_cvtsi128_si32(a1);
return result;
}

canonical_decompiled_code:
double __fastcall func0(__m128i a1, __m128i a2)
{
double result; // xmm0_8

*(_QWORD *)&result = ((unsigned __int64)(unsigned int)_mm_cvtsi128_si32(a2) << 32) | (unsigned int)_mm_cvtsi128_si32(a1);
return result;
}

original_code:
inline vec2 makeVec2(float x, float y) {
 vec2 v;
 v.x = x;
 v.y = y;
 return v;
}

canonical_original_code:
inline struct vec2 makeVec2(float x, float y) {
 struct vec2 v;
 v.x = x;
 v.y = y;
 return v;
}

variable_types: x float

variable_types: y float

variable_types:
v
struct vec2 { float x; float y; }

return_type: struct vec2 { float x; float y; }


decompiled_code:
__m128i __fastcall makeVec3(float a1, float a2)
{
__m128i v3; // [rsp+0h] [rbp-14h] BYREF

v3.m128i_i64[0] = __PAIR64__(LODWORD(a2), LODWORD(a1));
return _mm_loadl_epi64(&v3);
}

canonical_decompiled_code:
__m128i __fastcall func1(float a1, float a2)
{
__m128i v3; // [rsp+0h] [rbp-14h] BYREF

v3.m128i_i64[0] = __PAIR64__(LODWORD(a2), LODWORD(a1));
return _mm_loadl_epi64(&v3);
}

original_code:
inline vec3 makeVec3(float x, float y, float z) {
 vec3 v;
 v.x = x;
 v.y = y;
 v.z = z;
 return v;
}

canonical_original_code:
inline struct vec3 makeVec3(float x, float y, float z) {
 struct vec3 v;
 v.x = x;
 v.y = y;
 v.z = z;
 return v;
}

return_type: struct vec3 { float x; float y; float z; }


decompiled_code:
__int64 __fastcall mult2(float *a1, double a2, float a3)
{
__int64 result; // rax

*a1 = *(float *)&a2 * a3;
result = HIDWORD(*(_QWORD *)&a2);
a1[1] = a3 * *((float *)&a2 + 1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(float *a1, double a2, float a3)
{
__int64 result; // rax

*a1 = *(float *)&a2 * a3;
result = HIDWORD(*(_QWORD *)&a2);
a1[1] = a3 * *((float *)&a2 + 1);
return result;
}

original_code:
inline void mult2(vec2 * r, vec2 a, float s) {
 r->x = a.x * s;
 r->y = a.y * s;
}

canonical_original_code:
inline void mult2(struct vec2 *r, struct vec2 a, float s) {
 r->x = a.x * s;
 r->y = a.y * s;
}

variable_types:
r
struct vec2 { float x; float y; } *

variable_types:
a
struct vec2

variable_types: s float


decompiled_code:
void __fastcall mult3(float *a1, double a2, float a3, float a4)
{
*a1 = a4 * *(float *)&a2;
a1[1] = a4 * *((float *)&a2 + 1);
a1[2] = a4 * a3;
}

canonical_decompiled_code:
void __fastcall func3(float *a1, double a2, float a3, float a4)
{
*a1 = a4 * *(float *)&a2;
a1[1] = a4 * *((float *)&a2 + 1);
a1[2] = a4 * a3;
}

original_code:
inline void mult3(vec3 * r, vec3 a, float s) {
 r->x = a.x * s;
 r->y = a.y * s;
 r->z = a.z * s;
}

canonical_original_code:
inline void mult3(struct vec3 *r, struct vec3 a, float s) {
 r->x = a.x * s;
 r->y = a.y * s;
 r->z = a.z * s;
}

variable_types: struct vec3 { float x; float y; float z; } *

variable_types:
a
struct vec3


decompiled_code:
void __fastcall scale2(float *a1, float a2)
{
*a1 = a2 * *a1;
a1[1] = a2 * a1[1];
}

canonical_decompiled_code:
void __fastcall func4(float *a1, float a2)
{
*a1 = a2 * *a1;
a1[1] = a2 * a1[1];
}

original_code:
inline void scale2(vec2 * r, float s) {
 r->x *= s;
 r->y *= s;
}

canonical_original_code:
inline void scale2(struct vec2 *r, float s) {
 r->x *= s;
 r->y *= s;
}


original_code:
inline void scale3(vec3 * r, float s) {
 r->x *= s;
 r->y *= s;
 r->z *= s;
}

canonical_original_code:
inline void scale3(struct vec3 *r, float s) {
 r->x *= s;
 r->y *= s;
 r->z *= s;
}

user_defined_types: struct vec3 { float x; float y; float z; }


decompiled_code:
__int64 __fastcall div2(float *a1, double a2, float a3)
{
__int64 result; // rax

*a1 = *(float *)&a2 / a3;
result = HIDWORD(*(_QWORD *)&a2);
a1[1] = *((float *)&a2 + 1) / a3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(float *a1, double a2, float a3)
{
__int64 result; // rax

*a1 = *(float *)&a2 / a3;
result = HIDWORD(*(_QWORD *)&a2);
a1[1] = *((float *)&a2 + 1) / a3;
return result;
}

original_code:
inline void div2(vec2 * r, vec2 a, float s) {
 r->x = a.x / s;
 r->y = a.y / s;
}

canonical_original_code:
inline void div2(struct vec2 *r, struct vec2 a, float s) {
 r->x = a.x / s;
 r->y = a.y / s;
}


original_code:
inline void div3(vec3 * r, vec3 a, float s) {
 r->x = a.x / s;
 r->y = a.y / s;
 r->z = a.z / s;
}

canonical_original_code:
inline void div3(struct vec3 *r, struct vec3 a, float s) {
 r->x = a.x / s;
 r->y = a.y / s;
 r->z = a.z / s;
}


decompiled_code:
void __fastcall invScale2(float *a1, float a2)
{
*a1 = *a1 / a2;
a1[1] = a1[1] / a2;
}

canonical_decompiled_code:
void __fastcall func8(float *a1, float a2)
{
*a1 = *a1 / a2;
a1[1] = a1[1] / a2;
}

original_code:
inline void invScale2(vec2 * r, float s) {
 r->x /= s;
 r->y /= s;
}

canonical_original_code:
inline void invScale2(struct vec2 *r, float s) {
 r->x /= s;
 r->y /= s;
}


original_code:
inline void invScale3(vec3 * r, float s) {
 r->x /= s;
 r->y /= s;
 r->z /= s;
}

canonical_original_code:
inline void invScale3(struct vec3 *r, float s) {
 r->x /= s;
 r->y /= s;
 r->z /= s;
}