decompiled_code:
__int64 __fastcall euclid_gcb_GF2sup8_ext(unsigned int a1, unsigned int a2, unsigned int *a3)
{
unsigned int v3; // er12
unsigned int v4; // ebp
unsigned int v5; // er13
unsigned int v6; // er14
__int64 v7; // rbx
int v8; // er15
int v9; // edx
unsigned int v10; // eax
unsigned int v12; // [rsp+Ch] [rbp-4Ch]
int v13; // [rsp+10h] [rbp-48h]
unsigned int v14; // [rsp+14h] [rbp-44h]

v3 = a1;
v4 = a2;
if ( a2 <= 1 )
{
v10 = a1;
v5 = 1;
}
else
{
v5 = 1;
v6 = 0;
v12 = 0;
v13 = 1;
do
{
v7 = (unsigned int)GF2sup8_divid(v3, v4, 0LL);
v8 = v6;
v6 = GF2sup8_mul16(v7, v6) ^ v13;
v14 = v5;
v5 = GF2sup8_mul16((unsigned int)v7, v5) ^ v12;
v9 = GF2sup8_mul16((unsigned int)v7, v4);
v10 = v4;
v4 = v3 ^ v9;
v13 = v8;
v12 = v14;
v3 = v10;
}
while ( v4 > 1 );
}
if ( !v4 )
{
v4 = v10;
v5 = -1;
}
*a3 = v5;
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1, unsigned int a2, unsigned int *a3)
{
unsigned int v3; // er12
unsigned int v4; // ebp
unsigned int v5; // er13
unsigned int v6; // er14
__int64 v7; // rbx
int v8; // er15
int v9; // edx
unsigned int v10; // eax
unsigned int v12; // [rsp+Ch] [rbp-4Ch]
int v13; // [rsp+10h] [rbp-48h]
unsigned int v14; // [rsp+14h] [rbp-44h]

v3 = a1;
v4 = a2;
if ( a2 <= 1 )
{
v10 = a1;
v5 = 1;
}
else
{
v5 = 1;
v6 = 0;
v12 = 0;
v13 = 1;
do
{
v7 = (unsigned int)GF2sup8_divid(v3, v4, 0LL);
v8 = v6;
v6 = GF2sup8_mul16(v7, v6) ^ v13;
v14 = v5;
v5 = GF2sup8_mul16((unsigned int)v7, v5) ^ v12;
v9 = GF2sup8_mul16((unsigned int)v7, v4);
v10 = v4;
v4 = v3 ^ v9;
v13 = v8;
v12 = v14;
v3 = v10;
}
while ( v4 > 1 );
}
if ( !v4 )
{
v4 = v10;
v5 = -1;
}
*a3 = v5;
return v4;
}

original_code:
int euclid_gcb_GF2sup8_ext(int mx,int bx,int * inverse)
{
    int a1x=1,a2x=0,a3x=mx;
    int b1x=0,b2x=1,b3x=bx;
 int t1x=0,t2x=0,t3x=0;
    int qx;
    while(b3x!=0 && b3x!=1)
    {
  qx=GF2sup8_divid(a3x,b3x,((void *)0));
  t1x=((a1x)^(GF2sup8_mul16(qx,b1x)));
  t2x=((a2x)^(GF2sup8_mul16(qx,b2x)));
  t3x=((a3x)^(GF2sup8_mul16(qx,b3x)));
  a1x=b1x,a2x=b2x,a3x=b3x;
  b1x=t1x,b2x=t2x,b3x=t3x;
    }
 if(b3x){
  *inverse=b2x;
  return b3x;
 }else{
  *inverse=-1;
  return a3x;
 }
}

canonical_original_code:
int euclid_gcb_GF2sup8_ext(int mx, int bx, int *inverse)
{
    int a1x = 1; int a2x = 0; int a3x = mx;
    int b1x = 0; int b2x = 1; int b3x = bx;
 int t1x = 0; int t2x = 0; int t3x = 0;
    int qx;
    while(b3x!=0 && b3x!=1)
    {
  qx=GF2sup8_divid(a3x,b3x,((void *)0));
  t1x=((a1x)^(GF2sup8_mul16(qx,b1x)));
  t2x=((a2x)^(GF2sup8_mul16(qx,b2x)));
  t3x=((a3x)^(GF2sup8_mul16(qx,b3x)));
  a1x=b1x,a2x=b2x,a3x=b3x;
  b1x=t1x,b2x=t2x,b3x=t3x;
    }
 if(b3x){
  *inverse=b2x;
  return b3x;
 }else{
  *inverse=-1;
  return a3x;
 }
}

variable_types: mx int

variable_types: bx int

variable_types:
inverse
int *

variable_types: qx int


decompiled_code:
__int64 __fastcall istate_mix_columns(unsigned __int8 *a1)
{
unsigned __int8 *v1; // r12
__int64 i; // rbp
int v3; // ebx
int v4; // ebx
int v5; // ebx
__int64 result; // rax
char v7[3]; // [rsp+Ch] [rbp-2Ch]
unsigned __int8 v8; // [rsp+Fh] [rbp-29h]

v1 = a1;
do
{
for ( i = 0LL; i != 4; ++i )
{
v3 = GF2sup8_mul8((unsigned __int8)IMixC[4 * i], *v1);
v4 = GF2sup8_mul8((unsigned __int8)byte_571[4 * i], v1[4]) ^ v3;
v5 = GF2sup8_mul8((unsigned __int8)byte_572[4 * i], v1[8]) ^ v4;
v7[i] = GF2sup8_mul8((unsigned __int8)byte_573[4 * i], v1[12]) ^ v5;
}
*v1 = v7[0];
v1[4] = v7[1];
v1[8] = v7[2];
result = v8;
v1[12] = v8;
++v1;
}
while ( v1 != a1 + 4 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int8 *a1)
{
unsigned __int8 *v1; // r12
__int64 i; // rbp
int v3; // ebx
int v4; // ebx
int v5; // ebx
__int64 result; // rax
char v7[3]; // [rsp+Ch] [rbp-2Ch]
unsigned __int8 v8; // [rsp+Fh] [rbp-29h]

v1 = a1;
do
{
for ( i = 0LL; i != 4; ++i )
{
v3 = GF2sup8_mul8((unsigned __int8)IMixC[4 * i], *v1);
v4 = GF2sup8_mul8((unsigned __int8)byte_571[4 * i], v1[4]) ^ v3;
v5 = GF2sup8_mul8((unsigned __int8)byte_572[4 * i], v1[8]) ^ v4;
v7[i] = GF2sup8_mul8((unsigned __int8)byte_573[4 * i], v1[12]) ^ v5;
}
*v1 = v7[0];
v1[4] = v7[1];
v1[8] = v7[2];
result = v8;
v1[12] = v8;
++v1;
}
while ( v1 != a1 + 4 );
return result;
}

original_code:
static void istate_mix_columns(byte_t * state)
{
 int i,j,ret;
 byte_t tr[4];
 for(i=0;i<4;i++){
  for(j=0;j<4;j++){
   tr[j]=GF2sup8_mul8(IMixC[j][0],state[0*4+i])^
    GF2sup8_mul8(IMixC[j][1],state[1*4+i])^
    GF2sup8_mul8(IMixC[j][2],state[2*4+i])^
    GF2sup8_mul8(IMixC[j][3],state[3*4+i]);
  }
  state[0*4+i]=tr[0];
  state[1*4+i]=tr[1];
  state[2*4+i]=tr[2];
  state[3*4+i]=tr[3];
 }
}

canonical_original_code:
static void istate_mix_columns(unsigned char *state)
{
 int i; int j; int ret;
 unsigned char tr[4];
 for(i=0;i<4;i++){
  for(j=0;j<4;j++){
   tr[j]=GF2sup8_mul8(IMixC[j][0],state[0*4+i])^
    GF2sup8_mul8(IMixC[j][1],state[1*4+i])^
    GF2sup8_mul8(IMixC[j][2],state[2*4+i])^
    GF2sup8_mul8(IMixC[j][3],state[3*4+i]);
  }
  state[0*4+i]=tr[0];
  state[1*4+i]=tr[1];
  state[2*4+i]=tr[2];
  state[3*4+i]=tr[3];
 }
}

variable_types:
state
unsigned char *

variable_types: i int

variable_types: j int

variable_types: ret int

variable_types:
tr
unsigned char [4]

return_type: void


decompiled_code:
__int64 __fastcall state_shift_row_right(int *a1, unsigned int a2)
{
unsigned int v2; // ebx
unsigned int v4; // er12

if ( a2 > 3 )
return (unsigned int)-1;
v2 = a2;
if ( !a1 )
return (unsigned int)-1;
if ( a2 )
{
v4 = *a1;
if ( (unsigned int)is_big_endian() )
*a1 = (v4 >> (8 * a2)) | (v4 << (8 * (4 - a2)));
else
*a1 = (v4 >> (8 * (4 - a2))) | (v4 << (8 * a2));
v2 = 0;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(int *a1, unsigned int a2)
{
unsigned int v2; // ebx
unsigned int v4; // er12

if ( a2 > 3 )
return (unsigned int)-1;
v2 = a2;
if ( !a1 )
return (unsigned int)-1;
if ( a2 )
{
v4 = *a1;
if ( (unsigned int)is_big_endian() )
*a1 = (v4 >> (8 * a2)) | (v4 << (8 * (4 - a2)));
else
*a1 = (v4 >> (8 * (4 - a2))) | (v4 << (8 * a2));
v2 = 0;
}
return v2;
}

original_code:
static int state_shift_row_right(byte_t * state,int n)
{
 if(n>3 || n<0 || !state)
  return -1;
 if(n==0) return 0;
 word_t * swt = (word_t*)state;
 word_t sw=*swt;
 if(is_big_endian()){
  *swt=((sw) >> (n*8))|((sw) << ((32) - (n*8)));
 }else{
  *swt=((sw) << (n*8))|((sw) >> ((32) - (n*8)));
 }
 return 0;
}

canonical_original_code:
static int state_shift_row_right(unsigned char *state, int n)
{
 if(n>3 || n<0 || !state)
  return -1;
 if(n==0) return 0;
 unsigned int *swt = (word_t*)state;
 unsigned int sw = *swt;
 if(is_big_endian()){
  *swt=((sw) >> (n*8))|((sw) << ((32) - (n*8)));
 }else{
  *swt=((sw) << (n*8))|((sw) >> ((32) - (n*8)));
 }
 return 0;
}

variable_types: n int

variable_types:
swt
unsigned int *

variable_types:
sw
unsigned int

return_type: int


decompiled_code:
__int64 __fastcall istate_rshift_bvary(__int64 a1)
{
__int64 v1; // rbp
unsigned int v2; // er12
__int64 v3; // rbx
__int64 result; // rax

v1 = a1 + 4;
v2 = 0;
while ( 1 )
{
v3 = v1 - 4;
result = state_shift_row_right((int *)(v1 - 4), v2);
if ( (int)result < 0 )
break;
do
subbyte(v3++, &isbox);
while ( v3 != v1 );
++v2;
v1 += 4LL;
if ( v2 == 4 )
return 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
__int64 v1; // rbp
unsigned int v2; // er12
__int64 v3; // rbx
__int64 result; // rax

v1 = a1 + 4;
v2 = 0;
while ( 1 )
{
v3 = v1 - 4;
result = func1((int *)(v1 - 4), v2);
if ( (int)result < 0 )
break;
do
subbyte(v3++, &isbox);
while ( v3 != v1 );
++v2;
v1 += 4LL;
if ( v2 == 4 )
return 0LL;
}
return result;
}

original_code:
static int istate_rshift_bvary(byte_t * state)
{
 int i,j,ret;
 for(i=0;i<4;i++)
 {
  ret = state_shift_row_right(&state[i*4],i);
  if(ret < 0)
   return ret;
  for(j=0;j<4;j++)
   subbyte(&state[i*4+j],isbox);
 }
 return 0;
}

canonical_original_code:
static int istate_rshift_bvary(unsigned char *state)
{
 int i; int j; int ret;
 for(i=0;i<4;i++)
 {
  ret = state_shift_row_right(&state[i*4],i);
  if(ret < 0)
   return ret;
  for(j=0;j<4;j++)
   subbyte(&state[i*4+j],isbox);
 }
 return 0;
}


decompiled_code:
__int64 __fastcall Aes128_Dec(__int64 a1, int a2, __int64 a3, int a4, unsigned __int8 *a5)
{
__int64 v7; // rbp
int v8; // er12
__int64 result; // rax

if ( !a1 )
return 0xFFFFFFFFLL;
if ( a2 <= 15 )
return 0xFFFFFFFFLL;
if ( a4 != 16 || a3 == 0 || !a5 )
return 0xFFFFFFFFLL;
state_put(a1, a5);
key_expansion(a3, &W);
state_add_rou_key(a5, (unsigned int)((_DWORD)&W + 160));
v7 = (unsigned int)((_DWORD)&W + 144);
v8 = 9;
do
{
if ( (int)istate_rshift_bvary((__int64)a5) < 0 )
return 0xFFFFFFFFLL;
if ( (int)state_add_rou_key(a5, v7) < 0 )
return 0xFFFFFFFFLL;
istate_mix_columns(a5);
v7 -= 16LL;
--v8;
}
while ( v8 );
if ( (int)istate_rshift_bvary((__int64)a5) < 0 )
result = 0xFFFFFFFFLL;
else
result = (unsigned int)((int)state_add_rou_key(a5, &W) >> 31);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int a2, __int64 a3, int a4, unsigned __int8 *a5)
{
__int64 v7; // rbp
int v8; // er12
__int64 result; // rax

if ( !a1 )
return 0xFFFFFFFFLL;
if ( a2 <= 15 )
return 0xFFFFFFFFLL;
if ( a4 != 16 || a3 == 0 || !a5 )
return 0xFFFFFFFFLL;
state_put(a1, a5);
key_expansion(a3, &W);
state_add_rou_key(a5, (unsigned int)((_DWORD)&W + 160));
v7 = (unsigned int)((_DWORD)&W + 144);
v8 = 9;
do
{
if ( (int)func2((__int64)a5) < 0 )
return 0xFFFFFFFFLL;
if ( (int)state_add_rou_key(a5, v7) < 0 )
return 0xFFFFFFFFLL;
func0(a5);
v7 -= 16LL;
--v8;
}
while ( v8 );
if ( (int)func2((__int64)a5) < 0 )
result = 0xFFFFFFFFLL;
else
result = (unsigned int)((int)state_add_rou_key(a5, &W) >> 31);
return result;
}

original_code:
int Aes128_Dec(byte_t * cipherText,int cilen,byte_t * key,int keylen,byte_t * plainText)
{
 if(!cipherText || cilen<=0 || !key || keylen!=16 || !plainText)
  return -1;
 int i=0,ret=0;
 if(cilen<16)
  return -1;
 state_put(cipherText,plainText);
 key_expansion(key,W);
 i=10;
 state_add_rou_key(plainText,W+i*4);
 i--;
 while(i>0){
  if(istate_rshift_bvary(plainText)<0)
   return -1;
  if(state_add_rou_key(plainText,W+i*4)<0)
   return -1;
  istate_mix_columns(plainText);
  i--;
 }
 if(istate_rshift_bvary(plainText)<0)
  return -1;
 if(state_add_rou_key(plainText,W+i*4)<0)
  return -1;
 return 0;
}

canonical_original_code:
int Aes128_Dec(unsigned char *cipherText, int cilen, unsigned char *key, int keylen, unsigned char *plainText)
{
 if(!cipherText || cilen<=0 || !key || keylen!=16 || !plainText)
  return -1;
 int i = 0; int ret = 0;
 if(cilen<16)
  return -1;
 state_put(cipherText,plainText);
 key_expansion(key,W);
 i=10;
 state_add_rou_key(plainText,W+i*4);
 i--;
 while(i>0){
  if(istate_rshift_bvary(plainText)<0)
   return -1;
  if(state_add_rou_key(plainText,W+i*4)<0)
   return -1;
  istate_mix_columns(plainText);
  i--;
 }
 if(istate_rshift_bvary(plainText)<0)
  return -1;
 if(state_add_rou_key(plainText,W+i*4)<0)
  return -1;
 return 0;
}

variable_types:
cipherText
unsigned char *

variable_types: cilen int

variable_types:
key
unsigned char *

variable_types: keylen int

variable_types:
plainText
unsigned char *


decompiled_code:
__int64 __fastcall Aes128cbc_Pkcs7_Dec(__int64 a1, int a2, __int64 a3, int a4, unsigned __int8 *a5, __int64 a6, __int64 a7)
{
__int64 v10; // r14
unsigned int v11; // ebp
unsigned __int8 *v12; // rsi
__int64 i; // rdi
unsigned __int8 *v14; // rax
_BYTE *v15; // rdx

if ( !a1 || a2 <= 0 )
return (unsigned int)-1;
if ( a4 != 16 || a3 == 0 || !a5 )
return (unsigned int)-1;
state_put(a1, a5);
key_expansion(a3, &W);
state_add_rou_key(a5, (unsigned int)((_DWORD)&W + 160));
v10 = (unsigned int)((_DWORD)&W + 144);
v11 = 9;
do
{
if ( (int)istate_rshift_bvary((__int64)a5) < 0 )
return (unsigned int)-1;
if ( (int)state_add_rou_key(a5, v10) < 0 )
return (unsigned int)-1;
istate_mix_columns(a5);
v10 -= 16LL;
--v11;
}
while ( v11 );
if ( (int)istate_rshift_bvary((__int64)a5) < 0 )
return (unsigned int)-1;
if ( (int)state_add_rou_key(a5, &W) < 0 )
return (unsigned int)-1;
v12 = a5 + 4;
for ( i = 0LL; i != 4; ++i )
{
v14 = v12 - 4;
v15 = (_BYTE *)(a7 + i);
do
{
*v14++ ^= *v15;
v15 += 4;
}
while ( v14 != v12 );
v12 += 4;
}
reverse4x(a5, v12, v15);
PKCS7_UnPad(a5, 16LL, 16LL, a6);
return v11;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, int a2, __int64 a3, int a4, unsigned __int8 *a5, __int64 a6, __int64 a7)
{
__int64 v10; // r14
unsigned int v11; // ebp
unsigned __int8 *v12; // rsi
__int64 i; // rdi
unsigned __int8 *v14; // rax
_BYTE *v15; // rdx

if ( !a1 || a2 <= 0 )
return (unsigned int)-1;
if ( a4 != 16 || a3 == 0 || !a5 )
return (unsigned int)-1;
state_put(a1, a5);
key_expansion(a3, &W);
state_add_rou_key(a5, (unsigned int)((_DWORD)&W + 160));
v10 = (unsigned int)((_DWORD)&W + 144);
v11 = 9;
do
{
if ( (int)func2((__int64)a5) < 0 )
return (unsigned int)-1;
if ( (int)state_add_rou_key(a5, v10) < 0 )
return (unsigned int)-1;
func0(a5);
v10 -= 16LL;
--v11;
}
while ( v11 );
if ( (int)func2((__int64)a5) < 0 )
return (unsigned int)-1;
if ( (int)state_add_rou_key(a5, &W) < 0 )
return (unsigned int)-1;
v12 = a5 + 4;
for ( i = 0LL; i != 4; ++i )
{
v14 = v12 - 4;
v15 = (_BYTE *)(a7 + i);
do
{
*v14++ ^= *v15;
v15 += 4;
}
while ( v14 != v12 );
v12 += 4;
}
reverse4x(a5, v12, v15);
PKCS7_UnPad(a5, 16LL, 16LL, a6);
return v11;
}

original_code:
int Aes128cbc_Pkcs7_Dec(byte_t * cipherText,int cilen,byte_t * key,int keylen,byte_t * plainText,int * ptlen,const byte_t * iv)
{
 if(!cipherText || cilen<=0 || !key || keylen!=16 || !plainText)
  return -1;
 int i=0,ret=0;
 byte_t inPadBuf[16];
 state_put(cipherText,plainText);
 key_expansion(key,W);
 i=10;
 state_add_rou_key(plainText,W+i*4);
 i--;
 while(i>0){
  if(istate_rshift_bvary(plainText)<0)
   return -1;
  if(state_add_rou_key(plainText,W+i*4)<0)
   return -1;
  istate_mix_columns(plainText);
  i--;
 }
 if(istate_rshift_bvary(plainText)<0)
  return -1;
 if(state_add_rou_key(plainText,W+i*4)<0)
  return -1;
 int j;
 for(i=0;i<4;i++)
  for(j=0;j<4;j++)
   plainText[i*4+j]=plainText[i*4+j]^iv[j*4+i];
 reverse4x(plainText);
 PKCS7_UnPad(plainText,16,16,ptlen);
 return 0;
}

canonical_original_code:
int Aes128cbc_Pkcs7_Dec(unsigned char *cipherText, int cilen, unsigned char *key, int keylen, unsigned char *plainText, int *ptlen, unsigned char *iv)
{
 if(!cipherText || cilen<=0 || !key || keylen!=16 || !plainText)
  return -1;
 int i = 0; int ret = 0;
 unsigned char inPadBuf[16];
 state_put(cipherText,plainText);
 key_expansion(key,W);
 i=10;
 state_add_rou_key(plainText,W+i*4);
 i--;
 while(i>0){
  if(istate_rshift_bvary(plainText)<0)
   return -1;
  if(state_add_rou_key(plainText,W+i*4)<0)
   return -1;
  istate_mix_columns(plainText);
  i--;
 }
 if(istate_rshift_bvary(plainText)<0)
  return -1;
 if(state_add_rou_key(plainText,W+i*4)<0)
  return -1;
 int j;
 for(i=0;i<4;i++)
  for(j=0;j<4;j++)
   plainText[i*4+j]=plainText[i*4+j]^iv[j*4+i];
 reverse4x(plainText);
 PKCS7_UnPad(plainText,16,16,ptlen);
 return 0;
}

variable_types:
ptlen
int *

variable_types:
iv
unsigned char *

variable_types:
inPadBuf
unsigned char [16]


decompiled_code:
__int64 __fastcall bit_add_8(char a1)
{
__int64 result; // rax
int v2; // ecx
int v3; // edx

result = a1 & 1;
v2 = 7;
v3 = 1;
do
{
v3 *= 2;
if ( (v3 & a1) != 0 )
result = (unsigned int)result ^ 1;
--v2;
}
while ( v2 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(char a1)
{
__int64 result; // rax
int v2; // ecx
int v3; // edx

result = a1 & 1;
v2 = 7;
v3 = 1;
do
{
v3 *= 2;
if ( (v3 & a1) != 0 )
result = (unsigned int)result ^ 1;
--v2;
}
while ( v2 );
return result;
}

original_code:
static int bit_add_8(int8_t bx)
{
 int i=1;
 int ret=bx&i;
 while(i!=0x80) {
  i<<=1;
  if(bx&i)
   ret=((ret)^(1));
  else
   ret=((ret)^(0));
 }
 return ret;
}

canonical_original_code:
static int bit_add_8(char bx)
{
 int i = 1;
 int ret = bx&i;
 while(i!=0x80) {
  i<<=1;
  if(bx&i)
   ret=((ret)^(1));
  else
   ret=((ret)^(0));
 }
 return ret;
}

variable_types: bx char


decompiled_code:
__int64 __fastcall sbox_init(__int64 a1)
{
__int64 v1; // r8
char v2; // si
__int64 result; // rax

v1 = a1 + 256;
v2 = 0;
do
{
for ( result = 0LL; result != 16; ++result )
*(_BYTE *)(a1 + result) = result | v2;
v2 += 16;
a1 += 16LL;
}
while ( a1 != v1 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
__int64 v1; // r8
char v2; // si
__int64 result; // rax

v1 = a1 + 256;
v2 = 0;
do
{
for ( result = 0LL; result != 16; ++result )
*(_BYTE *)(a1 + result) = result | v2;
v2 += 16;
a1 += 16LL;
}
while ( a1 != v1 );
return result;
}

original_code:
static void sbox_init(byte_t * s)
{
 int8_t i,j;
 for(i=0;i<16;i++)
  for(j=0;j<16;j++)
   s[i*16+j]=i<<4|j;
}

canonical_original_code:
static void sbox_init(unsigned char *s)
{
 char i; char j;
 for(i=0;i<16;i++)
  for(j=0;j<16;j++)
   s[i*16+j]=i<<4|j;
}

variable_types:
s
unsigned char *

variable_types: i char

variable_types: j char


original_code:
static void sbox_bit_column_vector(byte_t * s)
{
 int i,j,k;
 int vary,sxy;
 int bx=0,bt=0;
 for(i=0;i<16;i++)
  for(j=0;j<16;j++){
   bx=0;
   sxy = s[i*16+j];
   for(k=0;k<8;k++) {
    bt=bit_add_8(sxy&vary_matrix[k]);
    if(bt)
     ((&bx)[(k)/8] |= 1<<((k)%8));
   }
   vary=((bx)^(0x63));
   s[i*16+j]=vary;
  }
}

canonical_original_code:
static void sbox_bit_column_vector(unsigned char *s)
{
 int i; int j; int k;
 int vary; int sxy;
 int bx = 0; int bt = 0;
 for(i=0;i<16;i++)
  for(j=0;j<16;j++){
   bx=0;
   sxy = s[i*16+j];
   for(k=0;k<8;k++) {
    bt=bit_add_8(sxy&vary_matrix[k]);
    if(bt)
     ((&bx)[(k)/8] |= 1<<((k)%8));
   }
   vary=((bx)^(0x63));
   s[i*16+j]=vary;
  }
}

variable_types: i int

variable_types: j int

variable_types: k int

variable_types: vary int

variable_types: sxy int

variable_types: bx int

variable_types: bt int


original_code:
static void isbox_bit_column_vector(byte_t * s)
{
 int i,j,k;
 int vary,sxy;
 int bx=0,bt=0;
 for(i=0;i<16;i++)
  for(j=0;j<16;j++){
   bx=0;
   sxy = s[i*16+j];
   for(k=0;k<8;k++) {
    bt=bit_add_8(sxy&ivary_matrix[k]);
    if(bt)
     ((&bx)[(k)/8] |= 1<<((k)%8));
   }
   vary=((bx)^(0x05));
   s[i*16+j]=vary;
  }
}

canonical_original_code:
static void isbox_bit_column_vector(unsigned char *s)
{
 int i; int j; int k;
 int vary; int sxy;
 int bx = 0; int bt = 0;
 for(i=0;i<16;i++)
  for(j=0;j<16;j++){
   bx=0;
   sxy = s[i*16+j];
   for(k=0;k<8;k++) {
    bt=bit_add_8(sxy&ivary_matrix[k]);
    if(bt)
     ((&bx)[(k)/8] |= 1<<((k)%8));
   }
   vary=((bx)^(0x05));
   s[i*16+j]=vary;
  }
}


decompiled_code:
__int64 __fastcall gen_sbox(__int64 a1)
{
sbox_init(a1);
sbox_inverse_gf28(a1);
return sbox_bit_column_vector(a1);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
func1(a1);
func4(a1);
return func2(a1);
}

original_code:
void gen_sbox(byte_t * s)
{
 sbox_init(s);
 sbox_inverse_gf28(s);
 sbox_bit_column_vector(s);
}

canonical_original_code:
void gen_sbox(unsigned char *s)
{
 sbox_init(s);
 sbox_inverse_gf28(s);
 sbox_bit_column_vector(s);
}


decompiled_code:
__int64 __fastcall gen_isbox(__int64 a1)
{
sbox_init(a1);
isbox_bit_column_vector(a1);
return sbox_inverse_gf28(a1);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
func1(a1);
func3(a1);
return func4(a1);
}

original_code:
void gen_isbox(byte_t * is)
{
 sbox_init(is);
 isbox_bit_column_vector(is);
 sbox_inverse_gf28(is);
}

canonical_original_code:
void gen_isbox(unsigned char *is)
{
 sbox_init(is);
 isbox_bit_column_vector(is);
 sbox_inverse_gf28(is);
}

variable_types:
is
unsigned char *


variable_types:
state
unsigned char *

variable_types:
key
unsigned int *


variable_types:
key
unsigned char *

variable_types:
w
unsigned int *

variable_types:
temp
unsigned int


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

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

original_code:
int is_big_endian()
{
    union A k;
    k.n[0]=0;
    k.n[1]=1;
    return (k.m==1)?1:0;
}

variable_types:
k
union A { short m; char n[2]; }


original_code:
void reverse4x(byte_t * box)
{
    int i,j;
    for(i=0;i<3;i++)
        for(j=i+1;j<4;j++)
            swap(&box[4*i+j],&box[4*j+i]);
}

canonical_original_code:
void reverse4x(unsigned char *box)
{
    int i; int j;
    for(i=0;i<3;i++)
        for(j=i+1;j<4;j++)
            swap(&box[4*i+j],&box[4*j+i]);
}

variable_types:
box
unsigned char *


decompiled_code:
__int64 __fastcall PKCS7_Pad(__int64 a1, int a2, int a3, __int64 a4)
{
__int64 result; // rax
__int64 i; // rdx
_BYTE *v8; // rdx
__int64 v9; // rdi

result = (unsigned int)(a3 - a2 % a3);
if ( a2 > 0 )
{
for ( i = 0LL; i != a2; ++i )
*(_BYTE *)(a4 + i) = *(_BYTE *)(a1 + i);
}
if ( a2 < a3 )
{
v8 = (_BYTE *)(a4 + a2);
v9 = (__int64)&v8[a3 - 1 - a2 + 1];
do
*v8++ = result;
while ( v8 != (_BYTE *)v9 );
}
return result;
}

original_code:
void PKCS7_Pad(byte_t * buf,int buflen,int blocksize,byte_t * paddingBuf)
{
    int i;
    int p=blocksize-buflen%blocksize;
    for(i=0;i<buflen;i++)
        paddingBuf[i]=buf[i];
    for(i=buflen;i<blocksize;i++)
        paddingBuf[i]=p;
}

canonical_original_code:
void PKCS7_Pad(unsigned char *buf, int buflen, int blocksize, unsigned char *paddingBuf)
{
    int i;
    int p = blocksize-buflen%blocksize;
    for(i=0;i<buflen;i++)
        paddingBuf[i]=buf[i];
    for(i=buflen;i<blocksize;i++)
        paddingBuf[i]=p;
}

variable_types:
buf
unsigned char *

variable_types: buflen int

variable_types: blocksize int

variable_types:
paddingBuf
unsigned char *

variable_types: p int


decompiled_code:
__int64 __fastcall PKCS7_UnPad(__int64 a1, __int64 a2, int a3, _DWORD *a4)
{
unsigned __int8 v4; // dl
int v5; // eax
_BYTE *v6; // rdi
bool v7; // zf
__int64 result; // rax

v4 = *(_BYTE *)(a1 + a3 - 1);
v5 = 0;
if ( (unsigned __int8)(v4 - 1) <= 0xEu )
{
v6 = (_BYTE *)(16 - v4 + a1);
v5 = 0;
while ( v4 == *v6 )
{
++v5;
++v6;
if ( v5 == v4 )
{
v5 = v4;
break;
}
}
}
v7 = v5 == v4;
result = 16LL;
if ( v7 )
result = 16 - (unsigned int)v4;
*a4 = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2, int a3, _DWORD *a4)
{
unsigned __int8 v4; // dl
int v5; // eax
_BYTE *v6; // rdi
bool v7; // zf
__int64 result; // rax

v4 = *(_BYTE *)(a1 + a3 - 1);
v5 = 0;
if ( (unsigned __int8)(v4 - 1) <= 0xEu )
{
v6 = (_BYTE *)(16 - v4 + a1);
v5 = 0;
while ( v4 == *v6 )
{
++v5;
++v6;
if ( v5 == v4 )
{
v5 = v4;
break;
}
}
}
v7 = v5 == v4;
result = 16LL;
if ( v7 )
result = 16 - (unsigned int)v4;
*a4 = result;
return result;
}

original_code:
void PKCS7_UnPad(byte_t * buf,int buflen,int blocksize,int * orilen)
{
    int i,j=0;
    int pn=buf[blocksize-1];
    if(pn<16 && pn!=0){
        for(i=16-pn;i<16;i++){
            if(buf[i]!=pn)
                break;
            j++;
        }
    }
    *orilen = (j==pn)?16-pn:16;
}

canonical_original_code:
void PKCS7_UnPad(unsigned char *buf, int buflen, int blocksize, int *orilen)
{
    int i; int j = 0;
    int pn = buf[blocksize-1];
    if(pn<16 && pn!=0){
        for(i=16-pn;i<16;i++){
            if(buf[i]!=pn)
                break;
            j++;
        }
    }
    *orilen = (j==pn)?16-pn:16;
}

variable_types:
orilen
int *

variable_types: i int

variable_types: j int

variable_types: pn int


decompiled_code:
void __fastcall tcp_send(__int64 a1)
{
int v1; // ebp
int v2; // ebx
int v3; // eax
int *v4; // rax
int v5; // eax
const char *v6; // rdx

if ( *(_DWORD *)(a1 + 8) - *(_DWORD *)(a1 + 16) > 0 )
{
v1 = *(_DWORD *)(a1 + 8) - *(_DWORD *)(a1 + 16);
v2 = 0;
while ( 1 )
{
v3 = send(sock, (const void *)(*(_QWORD *)(a1 + 16) + v2), v1 - v2, 0);
if ( v3 <= 0 )
break;
v2 += v3;
if ( v1 <= v2 )
return;
}
v4 = _errno_location();
v5 = (unsigned int)strerror(*v4);
error((int)"send: %s\n", v5, v6);
}
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1)
{
int v1; // ebp
int v2; // ebx
int v3; // eax
int *v4; // rax
int v5; // eax
const char *v6; // rdx

if ( *(_DWORD *)(a1 + 8) - *(_DWORD *)(a1 + 16) > 0 )
{
v1 = *(_DWORD *)(a1 + 8) - *(_DWORD *)(a1 + 16);
v2 = 0;
while ( 1 )
{
v3 = send(sock, (const void *)(*(_QWORD *)(a1 + 16) + v2), v1 - v2, 0);
if ( v3 <= 0 )
break;
v2 += v3;
if ( v1 <= v2 )
return;
}
v4 = _errno_location();
v5 = (unsigned int)strerror(*v4);
error((int)"send: %s\n", v5, v6);
}
}

original_code:
void
tcp_send(STREAM s)
{
 int length = s->end - s->data;
 int sent, total = 0;
 while (total < length)
 {
  sent = send(sock, s->data + total, length - total, 0);
  if (sent <= 0)
  {
   error("send: %s\n", strerror((*__errno_location ())));
   return;
  }
  total += sent;
 }
}

canonical_original_code:
void tcp_send(struct stream *s)
{
 int length = s->end - s->data;
 int sent; int total = 0;
 while (total < length)
 {
  sent = send(sock, s->data + total, length - total, 0);
  if (sent <= 0)
  {
   error("send: %s\n", strerror((*__errno_location ())));
   return;
  }
  total += sent;
 }
}

variable_types: struct stream { unsigned char *p; unsigned char *end; unsigned char *data; unsigned int size; unsigned char *iso_hdr; unsigned char *mcs_hdr; unsigned char *sec_hdr; unsigned char *rdp_hdr; } *

variable_types: length int

variable_types: sent int

variable_types: total int


decompiled_code:
__int64 __fastcall tcp_connect(char *cp)
{
struct hostent *v1; // rax
int v2; // ebx
__int64 result; // rax
const char *v4; // rdx
int *v5; // rax
int v6; // eax
const char *v7; // rdx
int *v8; // rax
int v9; // eax
const char *v10; // rdx
int optval; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

optval = 1;
v1 = gethostbyname(cp);
if ( v1 )
{
*(_DWORD *)&addr.sa_data[2] = **(_DWORD **)v1->h_addr_list;
}
else
{
*(_DWORD *)&addr.sa_data[2] = inet_addr(cp);
if ( *(_DWORD *)&addr.sa_data[2] == -1 )
{
error((int)"%s: unable to resolve host\n", (int)cp, v4);
return 0LL;
}
}
v2 = socket(2, 1, 0);
sock = v2;
if ( v2 < 0 )
{
v5 = _errno_location();
v6 = (unsigned int)strerror(*v5);
error((int)"socket: %s\n", v6, v7);
result = 0LL;
}
else
{
addr.sa_family = 2;
*(_WORD *)addr.sa_data = _bswap_16(tcp_port_rdp);
if ( connect(v2, &addr, 0x10u) < 0 )
{
v8 = _errno_location();
v9 = (unsigned int)strerror(*v8);
error((int)"connect: %s\n", v9, v10);
close(sock);
result = 0LL;
}
else
{
setsockopt(sock, 6, 1, &optval, 4u);
dword_358 = 4096;
qword_350 = xmalloc(4096LL);
dword_318 = 4096;
qword_310 = xmalloc(4096LL);
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *cp)
{
struct hostent *v1; // rax
int v2; // ebx
__int64 result; // rax
const char *v4; // rdx
int *v5; // rax
int v6; // eax
const char *v7; // rdx
int *v8; // rax
int v9; // eax
const char *v10; // rdx
int optval; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

optval = 1;
v1 = gethostbyname(cp);
if ( v1 )
{
*(_DWORD *)&addr.sa_data[2] = **(_DWORD **)v1->h_addr_list;
}
else
{
*(_DWORD *)&addr.sa_data[2] = inet_addr(cp);
if ( *(_DWORD *)&addr.sa_data[2] == -1 )
{
error((int)"%s: unable to resolve host\n", (int)cp, v4);
return 0LL;
}
}
v2 = socket(2, 1, 0);
sock = v2;
if ( v2 < 0 )
{
v5 = _errno_location();
v6 = (unsigned int)strerror(*v5);
error((int)"socket: %s\n", v6, v7);
result = 0LL;
}
else
{
addr.sa_family = 2;
*(_WORD *)addr.sa_data = func0(tcp_port_rdp);
if ( connect(v2, &addr, 0x10u) < 0 )
{
v8 = _errno_location();
v9 = (unsigned int)strerror(*v8);
error((int)"connect: %s\n", v9, v10);
close(sock);
result = 0LL;
}
else
{
setsockopt(sock, 6, 1, &optval, 4u);
dword_358 = 4096;
qword_350 = xmalloc(4096LL);
dword_318 = 4096;
qword_310 = xmalloc(4096LL);
result = 1LL;
}
}
return result;
}

original_code:
BOOL
tcp_connect(char *server)
{
 struct hostent *nslookup;
 struct sockaddr_in servaddr;
 int true = 1;
 if ((nslookup = gethostbyname(server)) != ((void *)0))
 {
  memcpy(&servaddr.sin_addr, nslookup->h_addr_list[0], sizeof(servaddr.sin_addr));
 }
 else if ((servaddr.sin_addr.s_addr = inet_addr(server)) == ((in_addr_t) 0xffffffff))
 {
  error("%s: unable to resolve host\n", server);
  return (0);
 }
 if ((sock = socket(2, SOCK_STREAM, 0)) < 0)
 {
  error("socket: %s\n", strerror((*__errno_location ())));
  return (0);
 }
 servaddr.sin_family = 2;
 servaddr.sin_port = htons(tcp_port_rdp);
 if (connect(sock, (struct sockaddr *) &servaddr, sizeof(struct sockaddr)) < 0)
 {
  error("connect: %s\n", strerror((*__errno_location ())));
  close(sock);
  return (0);
 }
 setsockopt(sock, IPPROTO_TCP, 1, (void *) &true, sizeof(true));
 in.size = 4096;
 in.data = xmalloc(in.size);
 out.size = 4096;
 out.data = xmalloc(out.size);
 return (1);
}

canonical_original_code:
int tcp_connect(char *server)
{
 struct hostent *nslookup;
 struct sockaddr_in servaddr;
 int true = 1;
 if ((nslookup = gethostbyname(server)) != ((void *)0))
 {
  memcpy(&servaddr.sin_addr, nslookup->h_addr_list[0], sizeof(servaddr.sin_addr));
 }
 else if ((servaddr.sin_addr.s_addr = inet_addr(server)) == ((unsigned int) 0xffffffff))
 {
  error("%s: unable to resolve host\n", server);
  return (0);
 }
 if ((sock = socket(2, SOCK_STREAM, 0)) < 0)
 {
  error("socket: %s\n", strerror((*__errno_location ())));
  return (0);
 }
 servaddr.sin_family = 2;
 servaddr.sin_port = htons(tcp_port_rdp);
 if (connect(sock, (struct sockaddr *) &servaddr, sizeof(struct sockaddr)) < 0)
 {
  error("connect: %s\n", strerror((*__errno_location ())));
  close(sock);
  return (0);
 }
 setsockopt(sock, IPPROTO_TCP, 1, (void *) &true, sizeof(true));
 in.size = 4096;
 in.data = xmalloc(in.size);
 out.size = 4096;
 out.data = xmalloc(out.size);
 return (1);
}

variable_types:
server
char *

variable_types: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } *

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: true 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:
int tcp_disconnect()
{
return close(sock);
}

canonical_decompiled_code:
int func3()
{
return close(sock);
}

original_code:
void
tcp_disconnect(void)
{
 close(sock);
}

canonical_original_code:
void tcp_disconnect()
{
 close(sock);
}


usage

decompiled_code:
size_t __fastcall usage(const char *a1)
{
fwrite("rdesktop: A Remote Desktop Protocol client.\n", 1uLL, 0x2CuLL, stderr);
fwrite("Version 1.2.0. Copyright (C) 1999-2003 Matt Chapman.\n", 1uLL, 0x35uLL, stderr);
fwrite("See http://www.rdesktop.org/ for more information.\n\n", 1uLL, 0x34uLL, stderr);
fprintf(stderr, "Usage: %s [options] server[:port]\n", a1);
fwrite(" -u: user name\n", 1uLL, 0x11uLL, stderr);
fwrite(" -d: domain\n", 1uLL, 0xEuLL, stderr);
fwrite(" -s: shell\n", 1uLL, 0xDuLL, stderr);
fwrite(" -c: working directory\n", 1uLL, 0x19uLL, stderr);
fwrite(" -p: password (- to prompt)\n", 1uLL, 0x1EuLL, stderr);
fwrite(" -n: client hostname\n", 1uLL, 0x17uLL, stderr);
fwrite(" -k: keyboard layout on terminal server (us,sv,gr,etc.)\n", 1uLL, 0x3AuLL, stderr);
fwrite(" -g: desktop geometry (WxH)\n", 1uLL, 0x1EuLL, stderr);
fwrite(" -f: full-screen mode\n", 1uLL, 0x18uLL, stderr);
fwrite(" -b: force bitmap updates\n", 1uLL, 0x1CuLL, stderr);
fwrite(" -e: disable encryption (French TS)\n", 1uLL, 0x26uLL, stderr);
fwrite(" -m: do not send motion events\n", 1uLL, 0x21uLL, stderr);
fwrite(" -C: use private colour map\n", 1uLL, 0x1EuLL, stderr);
fwrite(" -K: keep window manager key bindings\n", 1uLL, 0x28uLL, stderr);
fwrite(" -T: window title\n", 1uLL, 0x14uLL, stderr);
return fwrite(" -D: hide window manager decorations\n", 1uLL, 0x27uLL, stderr);
}

canonical_decompiled_code:
size_t __fastcall func0(const char *a1)
{
fwrite("rdesktop: A Remote Desktop Protocol client.\n", 1uLL, 0x2CuLL, stderr);
fwrite("Version 1.2.0. Copyright (C) 1999-2003 Matt Chapman.\n", 1uLL, 0x35uLL, stderr);
fwrite("See http://www.rdesktop.org/ for more information.\n\n", 1uLL, 0x34uLL, stderr);
fprintf(stderr, "Usage: %s [options] server[:port]\n", a1);
fwrite(" -u: user name\n", 1uLL, 0x11uLL, stderr);
fwrite(" -d: domain\n", 1uLL, 0xEuLL, stderr);
fwrite(" -s: shell\n", 1uLL, 0xDuLL, stderr);
fwrite(" -c: working directory\n", 1uLL, 0x19uLL, stderr);
fwrite(" -p: password (- to prompt)\n", 1uLL, 0x1EuLL, stderr);
fwrite(" -n: client hostname\n", 1uLL, 0x17uLL, stderr);
fwrite(" -k: keyboard layout on terminal server (us,sv,gr,etc.)\n", 1uLL, 0x3AuLL, stderr);
fwrite(" -g: desktop geometry (WxH)\n", 1uLL, 0x1EuLL, stderr);
fwrite(" -f: full-screen mode\n", 1uLL, 0x18uLL, stderr);
fwrite(" -b: force bitmap updates\n", 1uLL, 0x1CuLL, stderr);
fwrite(" -e: disable encryption (French TS)\n", 1uLL, 0x26uLL, stderr);
fwrite(" -m: do not send motion events\n", 1uLL, 0x21uLL, stderr);
fwrite(" -C: use private colour map\n", 1uLL, 0x1EuLL, stderr);
fwrite(" -K: keep window manager key bindings\n", 1uLL, 0x28uLL, stderr);
fwrite(" -T: window title\n", 1uLL, 0x14uLL, stderr);
return fwrite(" -D: hide window manager decorations\n", 1uLL, 0x27uLL, stderr);
}

original_code:
static void
usage(char *program)
{
 fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n");
 fprintf(stderr, "Version " "1.2.0" ". Copyright (C) 1999-2003 Matt Chapman.\n");
 fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");
 fprintf(stderr, "Usage: %s [options] server[:port]\n", program);
 fprintf(stderr, "   -u: user name\n");
 fprintf(stderr, "   -d: domain\n");
 fprintf(stderr, "   -s: shell\n");
 fprintf(stderr, "   -c: working directory\n");
 fprintf(stderr, "   -p: password (- to prompt)\n");
 fprintf(stderr, "   -n: client hostname\n");
 fprintf(stderr, "   -k: keyboard layout on terminal server (us,sv,gr,etc.)\n");
 fprintf(stderr, "   -g: desktop geometry (WxH)\n");
 fprintf(stderr, "   -f: full-screen mode\n");
 fprintf(stderr, "   -b: force bitmap updates\n");
 fprintf(stderr, "   -e: disable encryption (French TS)\n");
 fprintf(stderr, "   -m: do not send motion events\n");
 fprintf(stderr, "   -C: use private colour map\n");
 fprintf(stderr, "   -K: keep window manager key bindings\n");
 fprintf(stderr, "   -T: window title\n");
 fprintf(stderr, "   -D: hide window manager decorations\n");
}

variable_types:
program
char *


decompiled_code:
int __fastcall generate_random(void *buf)
{
int v1; // ebp
int v2; // er12
int result; // eax
__pid_t v4; // ebp
__uid_t v5; // ebp
_BYTE v6[96]; // [rsp+0h] [rbp-128h] BYREF
struct tms buffer; // [rsp+60h] [rbp-C8h] BYREF
struct stat v8; // [rsp+80h] [rbp-A8h] BYREF

v1 = open("/dev/urandom", 0);
if ( v1 == -1 && (v1 = open("/dev/random", 0), v1 == -1)
|| (v2 = read(v1, buf, 0x20uLL), result = close(v1), v2 != 32) )
{
v4 = getpid();
*(_DWORD *)buf = v4 | (getppid() << 16);
v5 = getuid();
*((_DWORD *)buf + 1) = v5 | (getgid() << 16);
*((_DWORD *)buf + 2) = times(&buffer);
gettimeofday((struct timeval *)((char *)buf + 12), 0LL);
stat("/tmp", &v8);
*((_DWORD *)buf + 5) = v8.st_atim.tv_sec;
*((_DWORD *)buf + 6) = v8.st_mtim.tv_sec;
*((_DWORD *)buf + 7) = v8.st_ctim.tv_sec;
MD5_Init(v6);
MD5_Update(v6, buf, 16LL);
MD5_Final(buf, v6);
MD5_Update(v6, (char *)buf + 16, 16LL);
result = MD5_Final((char *)buf + 16, v6);
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(void *buf)
{
int v1; // ebp
int v2; // er12
int result; // eax
__pid_t v4; // ebp
__uid_t v5; // ebp
_BYTE v6[96]; // [rsp+0h] [rbp-128h] BYREF
struct tms buffer; // [rsp+60h] [rbp-C8h] BYREF
struct stat v8; // [rsp+80h] [rbp-A8h] BYREF

v1 = open("/dev/urandom", 0);
if ( v1 == -1 && (v1 = open("/dev/random", 0), v1 == -1)
|| (v2 = read(v1, buf, 0x20uLL), result = close(v1), v2 != 32) )
{
v4 = getpid();
*(_DWORD *)buf = v4 | (getppid() << 16);
v5 = getuid();
*((_DWORD *)buf + 1) = v5 | (getgid() << 16);
*((_DWORD *)buf + 2) = times(&buffer);
gettimeofday((struct timeval *)((char *)buf + 12), 0LL);
stat("/tmp", &v8);
*((_DWORD *)buf + 5) = v8.st_atim.tv_sec;
*((_DWORD *)buf + 6) = v8.st_mtim.tv_sec;
*((_DWORD *)buf + 7) = v8.st_ctim.tv_sec;
MD5_Init(v6);
MD5_Update(v6, buf, 16LL);
MD5_Final(buf, v6);
MD5_Update(v6, (char *)buf + 16, 16LL);
result = MD5_Final((char *)buf + 16, v6);
}
return result;
}

original_code:
void
generate_random(uint8 * random)
{
 struct stat st;
 struct tms tmsbuf;
 MD5_CTX md5;
 uint32 *r;
 int fd, n;
 if (((fd = open("/dev/urandom", 00)) != -1)
     || ((fd = open("/dev/random", 00)) != -1))
 {
  n = read(fd, random, 32);
  close(fd);
  if (n == 32)
   return;
 }
 r = (uint32 *) random;
 r[0] = (getpid()) | (getppid() << 16);
 r[1] = (getuid()) | (getgid() << 16);
 r[2] = times(&tmsbuf);
 gettimeofday((struct timeval *) &r[3], ((void *)0));
 stat("/tmp", &st);
 r[5] = st.st_atim.tv_sec;
 r[6] = st.st_mtim.tv_sec;
 r[7] = st.st_ctim.tv_sec;
 MD5_Init(&md5);
 MD5_Update(&md5, random, 16);
 MD5_Final(random, &md5);
 MD5_Update(&md5, random + 16, 16);
 MD5_Final(random + 16, &md5);
}

canonical_original_code:
void generate_random(unsigned char *random)
{
 struct stat st;
 struct tms tmsbuf;
 struct MD5state_st md5;
 unsigned int *r;
 int fd; int n;
 if (((fd = open("/dev/urandom", 00)) != -1)
     || ((fd = open("/dev/random", 00)) != -1))
 {
  n = read(fd, random, 32);
  close(fd);
  if (n == 32)
   return;
 }
 r = (unsigned int *) random;
 r[0] = (getpid()) | (getppid() << 16);
 r[1] = (getuid()) | (getgid() << 16);
 r[2] = times(&tmsbuf);
 gettimeofday((struct timeval *) &r[3], ((void *)0));
 stat("/tmp", &st);
 r[5] = st.st_atim.tv_sec;
 r[6] = st.st_mtim.tv_sec;
 r[7] = st.st_ctim.tv_sec;
 MD5_Init(&md5);
 MD5_Update(&md5, random, 16);
 MD5_Final(random, &md5);
 MD5_Update(&md5, random + 16, 16);
 MD5_Final(random + 16, &md5);
}

variable_types:
random
unsigned char *

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

variable_types: struct tms { long tms_utime; long tms_stime; long tms_cutime; long tms_cstime; }

variable_types: struct MD5state_st { unsigned int A; unsigned int Nl; unsigned int data[-1]; unsigned int num; }

variable_types:
r
unsigned int *

variable_types: fd int

variable_types: n int

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

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


xfree

decompiled_code:
void __fastcall xfree(void *a1)
{
free(a1);
}

canonical_decompiled_code:
void __fastcall func3(void *a1)
{
free(a1);
}

original_code:
void
xfree(void *mem)
{
 free(mem);
}

canonical_original_code:
void xfree(void *mem)
{
 free(mem);
}

variable_types:
mem
void *


error

decompiled_code:
int error(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
fwrite("ERROR: ", 1uLL, 7uLL, stderr);
return vfprintf(stderr, format, arg);
}

canonical_decompiled_code:
int func4(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
fwrite("ERROR: ", 1uLL, 7uLL, stderr);
return vfprintf(stderr, format, arg);
}

original_code:
void
error(char *format, ...)
{
 va_list ap;
 fprintf(stderr, "ERROR: ");
 __builtin_va_start(ap,format);
 vfprintf(stderr, format, ap);
 __builtin_va_end(ap);
}

canonical_original_code:
void error(char *format, ...)
{
 __builtin_va_list ap;
 fprintf(stderr, "ERROR: ");
 __builtin_va_start(ap,format);
 vfprintf(stderr, format, ap);
 __builtin_va_end(ap);
}

variable_types:
format
char *


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12
unsigned int v4; // er14
int v5; // er13
int v6; // eax
char *v7; // r15
int result; // eax
char *v9; // rax
int v10; // edx
int v11; // edx
__uid_t v12; // eax
struct passwd *v13; // rax
char *v14; // rax
char *endptr; // [rsp+8h] [rbp-180h] BYREF
char v16[32]; // [rsp+10h] [rbp-178h] BYREF
char v17[128]; // [rsp+30h] [rbp-158h] BYREF
char v18[8]; // [rsp+B0h] [rbp-D8h] BYREF
__int64 v19; // [rsp+B8h] [rbp-D0h]
char dest[16]; // [rsp+C0h] [rbp-C8h] BYREF
char name[64]; // [rsp+D0h] [rbp-B8h] BYREF
char s[120]; // [rsp+110h] [rbp-78h] BYREF

v16[0] = 0;
v17[0] = 0;
v18[0] = 0;
dest[0] = 0;
strcpy(keymapname, "en-us");
v3 = 0;
v4 = 51;
v5 = 0;
while ( 1 )
{
v6 = getopt(argc, (char *const *)argv, "u:d:s:c:p:n:k:g:fbemCKT:Dh?");
if ( v6 == -1 )
break;
switch ( v6 )
{
case 'C':
owncolmap = 1;
continue;
case 'D':
hide_decorations = 1;
continue;
case 'K':
grab_keyboard = 0;
continue;
case 'T':
strncpy(title, optarg, 0x1FuLL);
title[31] = 0;
continue;
case 'b':
orders = 0;
continue;
case 'c':
strncpy(v16, optarg, 0x1FuLL);
v16[31] = 0;
continue;
case 'd':
strncpy(dest, optarg, 0xFuLL);
dest[15] = 0;
continue;
case 'e':
encryption = 0;
continue;
case 'f':
fullscreen = 1;
continue;
case 'g':
if ( !strcmp(optarg, "workarea") )
{
height = 0;
width = 0;
}
else
{
width = strtol(optarg, &endptr, 10);
if ( *endptr == 120 )
height = strtol(endptr + 1, 0LL, 10);
if ( !width || !height )
{
error("invalid geometry\n");
return 1;
}
}
break;
case 'k':
strncpy(keymapname, optarg, 0xFuLL);
keymapname[15] = 0;
continue;
case 'm':
sendmotion = 0;
continue;
case 'n':
strncpy(&hostname, optarg, 0xFuLL);
*(&hostname + 15) = 0;
continue;
case 'p':
v7 = optarg;
if ( *optarg != 45 || optarg[1] )
{
strncpy(v18, optarg, 0xFuLL);
HIBYTE(v19) = 0;
endptr = v7;
if ( *v7 )
{
do
{
endptr = v7 + 1;
*v7 = 88;
v7 = endptr;
}
while ( *endptr );
v4 = 59;
}
else
{
v4 = 59;
}
}
else
{
v5 = 1;
}
continue;
case 's':
strncpy(v17, optarg, 0x7FuLL);
v17[127] = 0;
continue;
case 'u':
strncpy(username, optarg, 0xFuLL);
username[15] = 0;
v3 = 1;
continue;
default:
usage(*argv);
return 1;
}
}
if ( argc - optind <= 0 )
{
usage(*argv);
return 1;
}
strncpy(s, argv[optind], 0x3FuLL);
s[63] = 0;
v9 = strchr(s, 58);
endptr = v9;
if ( v9 )
{
tcp_port_rdp = strtol(v9 + 1, 0LL, 10);
*endptr = 0;
}
if ( !v3 )
{
v12 = getuid();
v13 = getpwuid(v12);
if ( !v13 || !v13->pw_name )
{
error("could not determine username, use -u\n");
return 1;
}
strncpy(username, v13->pw_name, 0xFuLL);
username[15] = 0;
}
if ( !hostname )
{
if ( gethostname(name, 0x40uLL) == -1 )
{
error("could not determine local hostname, use -n\n");
return 1;
}
v14 = strchr(name, 46);
endptr = v14;
if ( v14 )
*v14 = 0;
strncpy(&hostname, name, 0xFuLL);
*(&hostname + 15) = 0;
}
if ( v5 && (unsigned int)read_password(v18, 16) )
v4 = 59;
if ( !title[0] )
{
strcpy(title, "rdesktop - ");
strncat(title, s, 0x14uLL);
}
v10 = ui_init();
result = 1;
if ( v10 )
{
v11 = rdp_connect(s, v4, dest, v18, v17, v16);
result = 1;
if ( v11 )
{
*(_QWORD *)v18 = 0LL;
v19 = 0LL;
if ( (unsigned int)ui_create_window() )
{
rdp_main_loop();
ui_destroy_window();
}
rdp_disconnect();
ui_deinit();
result = 0;
}
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12
unsigned int v4; // er14
int v5; // er13
int v6; // eax
char *v7; // r15
int result; // eax
char *v9; // rax
int v10; // edx
int v11; // edx
__uid_t v12; // eax
struct passwd *v13; // rax
char *v14; // rax
char *endptr; // [rsp+8h] [rbp-180h] BYREF
char v16[32]; // [rsp+10h] [rbp-178h] BYREF
char v17[128]; // [rsp+30h] [rbp-158h] BYREF
char v18[8]; // [rsp+B0h] [rbp-D8h] BYREF
__int64 v19; // [rsp+B8h] [rbp-D0h]
char dest[16]; // [rsp+C0h] [rbp-C8h] BYREF
char name[64]; // [rsp+D0h] [rbp-B8h] BYREF
char s[120]; // [rsp+110h] [rbp-78h] BYREF

v16[0] = 0;
v17[0] = 0;
v18[0] = 0;
dest[0] = 0;
strcpy(keymapname, "en-us");
v3 = 0;
v4 = 51;
v5 = 0;
while ( 1 )
{
v6 = getopt(argc, (char *const *)argv, "u:d:s:c:p:n:k:g:fbemCKT:Dh?");
if ( v6 == -1 )
break;
switch ( v6 )
{
case 'C':
owncolmap = 1;
continue;
case 'D':
hide_decorations = 1;
continue;
case 'K':
grab_keyboard = 0;
continue;
case 'T':
strncpy(title, optarg, 0x1FuLL);
title[31] = 0;
continue;
case 'b':
orders = 0;
continue;
case 'c':
strncpy(v16, optarg, 0x1FuLL);
v16[31] = 0;
continue;
case 'd':
strncpy(dest, optarg, 0xFuLL);
dest[15] = 0;
continue;
case 'e':
encryption = 0;
continue;
case 'f':
fullscreen = 1;
continue;
case 'g':
if ( !strcmp(optarg, "workarea") )
{
height = 0;
width = 0;
}
else
{
width = strtol(optarg, &endptr, 10);
if ( *endptr == 120 )
height = strtol(endptr + 1, 0LL, 10);
if ( !width || !height )
{
func4("invalid geometry\n");
return 1;
}
}
break;
case 'k':
strncpy(keymapname, optarg, 0xFuLL);
keymapname[15] = 0;
continue;
case 'm':
sendmotion = 0;
continue;
case 'n':
strncpy(&hostname, optarg, 0xFuLL);
*(&hostname + 15) = 0;
continue;
case 'p':
v7 = optarg;
if ( *optarg != 45 || optarg[1] )
{
strncpy(v18, optarg, 0xFuLL);
HIBYTE(v19) = 0;
endptr = v7;
if ( *v7 )
{
do
{
endptr = v7 + 1;
*v7 = 88;
v7 = endptr;
}
while ( *endptr );
v4 = 59;
}
else
{
v4 = 59;
}
}
else
{
v5 = 1;
}
continue;
case 's':
strncpy(v17, optarg, 0x7FuLL);
v17[127] = 0;
continue;
case 'u':
strncpy(username, optarg, 0xFuLL);
username[15] = 0;
v3 = 1;
continue;
default:
func0(*argv);
return 1;
}
}
if ( argc - optind <= 0 )
{
func0(*argv);
return 1;
}
strncpy(s, argv[optind], 0x3FuLL);
s[63] = 0;
v9 = strchr(s, 58);
endptr = v9;
if ( v9 )
{
tcp_port_rdp = strtol(v9 + 1, 0LL, 10);
*endptr = 0;
}
if ( !v3 )
{
v12 = getuid();
v13 = getpwuid(v12);
if ( !v13 || !v13->pw_name )
{
func4("could not determine username, use -u\n");
return 1;
}
strncpy(username, v13->pw_name, 0xFuLL);
username[15] = 0;
}
if ( !hostname )
{
if ( gethostname(name, 0x40uLL) == -1 )
{
func4("could not determine local hostname, use -n\n");
return 1;
}
v14 = strchr(name, 46);
endptr = v14;
if ( v14 )
*v14 = 0;
strncpy(&hostname, name, 0xFuLL);
*(&hostname + 15) = 0;
}
if ( v5 && (unsigned int)func1(v18, 16) )
v4 = 59;
if ( !title[0] )
{
strcpy(title, "rdesktop - ");
strncat(title, s, 0x14uLL);
}
v10 = ui_init();
result = 1;
if ( v10 )
{
v11 = rdp_connect(s, v4, dest, v18, v17, v16);
result = 1;
if ( v11 )
{
*(_QWORD *)v18 = 0LL;
v19 = 0LL;
if ( (unsigned int)ui_create_window() )
{
rdp_main_loop();
ui_destroy_window();
}
rdp_disconnect();
ui_deinit();
result = 0;
}
}
return result;
}

original_code:
int
main(int argc, char *argv[])
{
 char server[64];
 char fullhostname[64];
 char domain[16];
 char password[16];
 char shell[128];
 char directory[32];
 BOOL prompt_password;
 struct passwd *pw;
 uint32 flags;
 char *p;
 int c;
 int username_option = 0;
 flags = 0x33;
 prompt_password = (0);
 domain[0] = password[0] = shell[0] = directory[0] = 0;
 strcpy(keymapname, "en-us");
 while ((c = getopt(argc, argv, "u:d:s:c:p:n:k:g:fbemCKT:Dh?")) != -1)
 {
  switch (c)
  {
   case 'u':
    { strncpy(username,optarg,sizeof(username)-1); username[sizeof(username)-1] = 0; };
    username_option = 1;
    break;
   case 'd':
    { strncpy(domain,optarg,sizeof(domain)-1); domain[sizeof(domain)-1] = 0; };
    break;
   case 's':
    { strncpy(shell,optarg,sizeof(shell)-1); shell[sizeof(shell)-1] = 0; };
    break;
   case 'c':
    { strncpy(directory,optarg,sizeof(directory)-1); directory[sizeof(directory)-1] = 0; };
    break;
   case 'p':
    if ((optarg[0] == '-') && (optarg[1] == 0))
    {
     prompt_password = (1);
     break;
    }
    { strncpy(password,optarg,sizeof(password)-1); password[sizeof(password)-1] = 0; };
    flags |= 0x8;
    p = optarg;
    while (*p)
     *(p++) = 'X';
    break;
   case 'n':
    { strncpy(hostname,optarg,sizeof(hostname)-1); hostname[sizeof(hostname)-1] = 0; };
    break;
   case 'k':
    { strncpy(keymapname,optarg,sizeof(keymapname)-1); keymapname[sizeof(keymapname)-1] = 0; };
    break;
   case 'g':
    if (!strcmp(optarg, "workarea"))
    {
     width = height = 0;
     break;
    }
    width = strtol(optarg, &p, 10);
    if (*p == 'x')
     height = strtol(p + 1, ((void *)0), 10);
    if ((width == 0) || (height == 0))
    {
     error("invalid geometry\n");
     return 1;
    }
    break;
   case 'f':
    fullscreen = (1);
    break;
   case 'b':
    orders = (0);
    break;
   case 'e':
    encryption = (0);
    break;
   case 'm':
    sendmotion = (0);
    break;
   case 'C':
    owncolmap = (1);
    break;
   case 'K':
    grab_keyboard = (0);
    break;
   case 'T':
    { strncpy(title,optarg,sizeof(title)-1); title[sizeof(title)-1] = 0; };
    break;
   case 'D':
    hide_decorations = (1);
    break;
   case 'h':
   case '?':
   default:
    usage(argv[0]);
    return 1;
  }
 }
 if (argc - optind < 1)
 {
  usage(argv[0]);
  return 1;
 }
 { strncpy(server,argv[optind],sizeof(server)-1); server[sizeof(server)-1] = 0; };
 p = strchr(server, ':');
 if (p != ((void *)0))
 {
  tcp_port_rdp = strtol(p + 1, ((void *)0), 10);
  *p = 0;
 }
 if (!username_option)
 {
  pw = getpwuid(getuid());
  if ((pw == ((void *)0)) || (pw->pw_name == ((void *)0)))
  {
   error("could not determine username, use -u\n");
   return 1;
  }
  { strncpy(username,pw->pw_name,sizeof(username)-1); username[sizeof(username)-1] = 0; };
 }
 if (hostname[0] == 0)
 {
  if (gethostname(fullhostname, sizeof(fullhostname)) == -1)
  {
   error("could not determine local hostname, use -n\n");
   return 1;
  }
  p = strchr(fullhostname, '.');
  if (p != ((void *)0))
   *p = 0;
  { strncpy(hostname,fullhostname,sizeof(hostname)-1); hostname[sizeof(hostname)-1] = 0; };
 }
 if (prompt_password && read_password(password, sizeof(password)))
  flags |= 0x8;
 if (title[0] == 0)
 {
  strcpy(title, "rdesktop - ");
  strncat(title, server, sizeof(title) - sizeof("rdesktop - "));
 }
 if (!ui_init())
  return 1;
 if (!rdp_connect(server, flags, domain, password, shell, directory))
  return 1;
 ;
 memset(password, 0, sizeof(password));
 if (ui_create_window())
 {
  rdp_main_loop();
  ui_destroy_window();
 }
 ;
 rdp_disconnect();
 ui_deinit();
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 char server[64];
 char fullhostname[64];
 char domain[16];
 char password[16];
 char shell[128];
 char directory[32];
 int prompt_password;
 struct passwd *pw;
 unsigned int flags;
 char *p;
 int c;
 int username_option = 0;
 flags = 0x33;
 prompt_password = (0);
 domain[0] = password[0] = shell[0] = directory[0] = 0;
 strcpy(keymapname, "en-us");
 while ((c = getopt(argc, argv, "u:d:s:c:p:n:k:g:fbemCKT:Dh?")) != -1)
 {
  switch (c)
  {
   case 'u':
    { strncpy(username,optarg,sizeof(username)-1); username[sizeof(username)-1] = 0; };
    username_option = 1;
    break;
   case 'd':
    { strncpy(domain,optarg,sizeof(domain)-1); domain[sizeof(domain)-1] = 0; };
    break;
   case 's':
    { strncpy(shell,optarg,sizeof(shell)-1); shell[sizeof(shell)-1] = 0; };
    break;
   case 'c':
    { strncpy(directory,optarg,sizeof(directory)-1); directory[sizeof(directory)-1] = 0; };
    break;
   case 'p':
    if ((optarg[0] == '-') && (optarg[1] == 0))
    {
     prompt_password = (1);
     break;
    }
    { strncpy(password,optarg,sizeof(password)-1); password[sizeof(password)-1] = 0; };
    flags |= 0x8;
    p = optarg;
    while (*p)
     *(p++) = 'X';
    break;
   case 'n':
    { strncpy(hostname,optarg,sizeof(hostname)-1); hostname[sizeof(hostname)-1] = 0; };
    break;
   case 'k':
    { strncpy(keymapname,optarg,sizeof(keymapname)-1); keymapname[sizeof(keymapname)-1] = 0; };
    break;
   case 'g':
    if (!strcmp(optarg, "workarea"))
    {
     width = height = 0;
     break;
    }
    width = strtol(optarg, &p, 10);
    if (*p == 'x')
     height = strtol(p + 1, ((void *)0), 10);
    if ((width == 0) || (height == 0))
    {
     error("invalid geometry\n");
     return 1;
    }
    break;
   case 'f':
    fullscreen = (1);
    break;
   case 'b':
    orders = (0);
    break;
   case 'e':
    encryption = (0);
    break;
   case 'm':
    sendmotion = (0);
    break;
   case 'C':
    owncolmap = (1);
    break;
   case 'K':
    grab_keyboard = (0);
    break;
   case 'T':
    { strncpy(title,optarg,sizeof(title)-1); title[sizeof(title)-1] = 0; };
    break;
   case 'D':
    hide_decorations = (1);
    break;
   case 'h':
   case '?':
   default:
    usage(argv[0]);
    return 1;
  }
 }
 if (argc - optind < 1)
 {
  usage(argv[0]);
  return 1;
 }
 { strncpy(server,argv[optind],sizeof(server)-1); server[sizeof(server)-1] = 0; };
 p = strchr(server, ':');
 if (p != ((void *)0))
 {
  tcp_port_rdp = strtol(p + 1, ((void *)0), 10);
  *p = 0;
 }
 if (!username_option)
 {
  pw = getpwuid(getuid());
  if ((pw == ((void *)0)) || (pw->pw_name == ((void *)0)))
  {
   error("could not determine username, use -u\n");
   return 1;
  }
  { strncpy(username,pw->pw_name,sizeof(username)-1); username[sizeof(username)-1] = 0; };
 }
 if (hostname[0] == 0)
 {
  if (gethostname(fullhostname, sizeof(fullhostname)) == -1)
  {
   error("could not determine local hostname, use -n\n");
   return 1;
  }
  p = strchr(fullhostname, '.');
  if (p != ((void *)0))
   *p = 0;
  { strncpy(hostname,fullhostname,sizeof(hostname)-1); hostname[sizeof(hostname)-1] = 0; };
 }
 if (prompt_password && read_password(password, sizeof(password)))
  flags |= 0x8;
 if (title[0] == 0)
 {
  strcpy(title, "rdesktop - ");
  strncat(title, server, sizeof(title) - sizeof("rdesktop - "));
 }
 if (!ui_init())
  return 1;
 if (!rdp_connect(server, flags, domain, password, shell, directory))
  return 1;
 ;
 memset(password, 0, sizeof(password));
 if (ui_create_window())
 {
  rdp_main_loop();
  ui_destroy_window();
 }
 ;
 rdp_disconnect();
 ui_deinit();
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
server
char [64]

variable_types:
fullhostname
char [64]

variable_types:
domain
char [16]

variable_types:
password
char [16]

variable_types:
shell
char [128]

variable_types:
directory
char [32]

variable_types: struct passwd { char *pw_name; char *pw_passwd; unsigned int pw_uid; unsigned int pw_gid; char *pw_gecos; char *pw_dir; char *pw_shell; } *

variable_types:
flags
unsigned int

variable_types:
p
char *

variable_types: c int


xmalloc

decompiled_code:
void *__fastcall xmalloc(unsigned int a1)
{
void *result; // rax

result = malloc((int)a1);
if ( !result )
{
error("xmalloc %d\n", a1);
exit(1);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func5(unsigned int a1)
{
void *result; // rax

result = malloc((int)a1);
if ( !result )
{
func4("xmalloc %d\n", a1);
exit(1);
}
return result;
}

original_code:
void *
xmalloc(int size)
{
 void *mem = malloc(size);
 if (mem == ((void *)0))
 {
  error("xmalloc %d\n", size);
  exit(1);
 }
 return mem;
}

canonical_original_code:
void *xmalloc(int size)
{
 void *mem = malloc(size);
 if (mem == ((void *)0))
 {
  error("xmalloc %d\n", size);
  exit(1);
 }
 return mem;
}

variable_types: size int

return_type: void *


xrealloc

decompiled_code:
void *__fastcall xrealloc(void *a1, unsigned int a2)
{
void *result; // rax

result = realloc(a1, (int)a2);
if ( !result )
{
error("xrealloc %d\n", a2);
exit(1);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func6(void *a1, unsigned int a2)
{
void *result; // rax

result = realloc(a1, (int)a2);
if ( !result )
{
func4("xrealloc %d\n", a2);
exit(1);
}
return result;
}

original_code:
void *
xrealloc(void *oldmem, int size)
{
 void *mem = realloc(oldmem, size);
 if (mem == ((void *)0))
 {
  error("xrealloc %d\n", size);
  exit(1);
 }
 return mem;
}

canonical_original_code:
void *xrealloc(void *oldmem, int size)
{
 void *mem = realloc(oldmem, size);
 if (mem == ((void *)0))
 {
  error("xrealloc %d\n", size);
  exit(1);
 }
 return mem;
}

variable_types:
oldmem
void *


warning

decompiled_code:
int warning(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
fwrite("WARNING: ", 1uLL, 9uLL, stderr);
return vfprintf(stderr, format, arg);
}

canonical_decompiled_code:
int func7(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
fwrite("WARNING: ", 1uLL, 9uLL, stderr);
return vfprintf(stderr, format, arg);
}

original_code:
void
warning(char *format, ...)
{
 va_list ap;
 fprintf(stderr, "WARNING: ");
 __builtin_va_start(ap,format);
 vfprintf(stderr, format, ap);
 __builtin_va_end(ap);
}

canonical_original_code:
void warning(char *format, ...)
{
 __builtin_va_list ap;
 fprintf(stderr, "WARNING: ");
 __builtin_va_start(ap,format);
 vfprintf(stderr, format, ap);
 __builtin_va_end(ap);
}


unimpl

decompiled_code:
int unimpl(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
fwrite("NOT IMPLEMENTED: ", 1uLL, 0x11uLL, stderr);
return vfprintf(stderr, format, arg);
}

canonical_decompiled_code:
int func8(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
fwrite("NOT IMPLEMENTED: ", 1uLL, 0x11uLL, stderr);
return vfprintf(stderr, format, arg);
}

original_code:
void
unimpl(char *format, ...)
{
 va_list ap;
 fprintf(stderr, "NOT IMPLEMENTED: ");
 __builtin_va_start(ap,format);
 vfprintf(stderr, format, ap);
 __builtin_va_end(ap);
}

canonical_original_code:
void unimpl(char *format, ...)
{
 __builtin_va_list ap;
 fprintf(stderr, "NOT IMPLEMENTED: ");
 __builtin_va_start(ap,format);
 vfprintf(stderr, format, ap);
 __builtin_va_end(ap);
}


hexdump

decompiled_code:
int __fastcall hexdump(__int64 a1, unsigned int a2)
{
unsigned int v3; // er14
int v4; // ebx
__int64 v5; // rbx
unsigned __int8 v6; // di
int result; // eax
unsigned int v8; // ebp
__int64 v9; // rbx

if ( a2 )
{
v3 = 0;
do
{
printf("%04x ", v3);
v8 = a2 - v3;
if ( a2 - v3 > 0x10 )
v8 = 16;
if ( a2 == v3 )
{
v4 = 0;
}
else
{
v9 = 0LL;
do
printf("%02x ", *(unsigned __int8 *)(a1 + v9++));
while ( v8 > (unsigned int)v9 );
v4 = 1;
if ( a2 != v3 )
v4 = v8;
if ( v4 > 15 )
{
LABEL_6:
v5 = 0LL;
do
{
v6 = *(_BYTE *)(a1 + v5);
if ( (unsigned __int8)(v6 - 32) >= 0x5Fu )
v6 = 46;
putchar(v6);
++v5;
}
while ( v8 > (unsigned int)v5 );
goto LABEL_10;
}
}
do
{
printf(" ");
++v4;
}
while ( v4 <= 15 );
if ( a2 != v3 )
goto LABEL_6;
LABEL_10:
result = putchar(10);
v3 += v8;
a1 += v8;
}
while ( a2 > v3 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func9(__int64 a1, unsigned int a2)
{
unsigned int v3; // er14
int v4; // ebx
__int64 v5; // rbx
unsigned __int8 v6; // di
int result; // eax
unsigned int v8; // ebp
__int64 v9; // rbx

if ( a2 )
{
v3 = 0;
do
{
printf("%04x ", v3);
v8 = a2 - v3;
if ( a2 - v3 > 0x10 )
v8 = 16;
if ( a2 == v3 )
{
v4 = 0;
}
else
{
v9 = 0LL;
do
printf("%02x ", *(unsigned __int8 *)(a1 + v9++));
while ( v8 > (unsigned int)v9 );
v4 = 1;
if ( a2 != v3 )
v4 = v8;
if ( v4 > 15 )
{
LABEL_6:
v5 = 0LL;
do
{
v6 = *(_BYTE *)(a1 + v5);
if ( (unsigned __int8)(v6 - 32) >= 0x5Fu )
v6 = 46;
putchar(v6);
++v5;
}
while ( v8 > (unsigned int)v5 );
goto LABEL_10;
}
}
do
{
printf(" ");
++v4;
}
while ( v4 <= 15 );
if ( a2 != v3 )
goto LABEL_6;
LABEL_10:
result = putchar(10);
v3 += v8;
a1 += v8;
}
while ( a2 > v3 );
}
return result;
}

original_code:
void
hexdump(unsigned char *p, unsigned int len)
{
 unsigned char *line = p;
 unsigned int thisline, offset = 0;
 int i;
 while (offset < len)
 {
  printf("%04x ", offset);
  thisline = len - offset;
  if (thisline > 16)
   thisline = 16;
  for (i = 0; i < thisline; i++)
   printf("%02x ", line[i]);
  for (; i < 16; i++)
   printf("   ");
  for (i = 0; i < thisline; i++)
   printf("%c", (line[i] >= 0x20 && line[i] < 0x7f) ? line[i] : '.');
  printf("\n");
  offset += thisline;
  line += thisline;
 }
}

canonical_original_code:
void hexdump(unsigned char *p, unsigned int len)
{
 unsigned char *line = p;
 unsigned int thisline; unsigned int offset = 0;
 int i;
 while (offset < len)
 {
  printf("%04x ", offset);
  thisline = len - offset;
  if (thisline > 16)
   thisline = 16;
  for (i = 0; i < thisline; i++)
   printf("%02x ", line[i]);
  for (; i < 16; i++)
   printf("   ");
  for (i = 0; i < thisline; i++)
   printf("%c", (line[i] >= 0x20 && line[i] < 0x7f) ? line[i] : '.');
  printf("\n");
  offset += thisline;
  line += thisline;
 }
}

variable_types:
p
unsigned char *

variable_types:
len
unsigned int

variable_types:
line
unsigned char *

variable_types:
thisline
unsigned int

variable_types:
offset
unsigned int

variable_types: i int


variable_types: struct stream { unsigned char *p; unsigned char *end; unsigned char *data; unsigned int size; unsigned char *iso_hdr; unsigned char *mcs_hdr; unsigned char *sec_hdr; unsigned char *rdp_hdr; } *


decompiled_code:
__int64 mcs_send_aurq()
{
_QWORD *v0; // rdi
_BYTE *v1; // rax

v0 = (_QWORD *)iso_init(1LL);
v1 = (_BYTE *)(*v0)++;
*v1 = 40;
v0[1] = *v0;
return iso_send();
}

original_code:
static void
mcs_send_aurq(void)
{
 STREAM s;
 s = iso_init(1);
 *((s)->p++) = (MCS_AURQ << 2);;
 (s)->end = (s)->p;;
 iso_send(s);
}

canonical_original_code:
static void mcs_send_aurq()
{
 struct stream *s;
 s = iso_init(1);
 *((s)->p++) = (MCS_AURQ << 2);;
 (s)->end = (s)->p;;
 iso_send(s);
}

user_defined_types: struct stream { unsigned char *p; unsigned char *end; unsigned char *data; unsigned int size; unsigned char *iso_hdr; unsigned char *mcs_hdr; unsigned char *sec_hdr; unsigned char *rdp_hdr; }


decompiled_code:
__int64 __fastcall mcs_send_cjrq(__int16 a1)
{
_QWORD *v2; // rdi
_BYTE *v3; // rax
char v4; // dl
_BYTE *v5; // rax
char v6; // dl
_BYTE *v7; // rax
_BYTE *v8; // rax
_BYTE *v9; // rax

v2 = (_QWORD *)iso_init(5LL);
v3 = (_BYTE *)(*v2)++;
*v3 = 56;
v4 = HIBYTE(mcs_userid);
v5 = (_BYTE *)(*v2)++;
*v5 = v4;
v6 = mcs_userid;
v7 = (_BYTE *)(*v2)++;
*v7 = v6;
v8 = (_BYTE *)(*v2)++;
*v8 = HIBYTE(a1);
v9 = (_BYTE *)(*v2)++;
*v9 = a1;
v2[1] = *v2;
return iso_send();
}

canonical_original_code:
static void mcs_send_cjrq(unsigned short chanid)
{
 struct stream *s;
 s = iso_init(5);
 *((s)->p++) = (MCS_CJRQ << 2);;
 { *((s)->p++) = ((mcs_userid) >> 8) & 0xff; *((s)->p++) = (mcs_userid) & 0xff; };
 { *((s)->p++) = ((chanid) >> 8) & 0xff; *((s)->p++) = (chanid) & 0xff; };
 (s)->end = (s)->p;;
 iso_send(s);
}

variable_types:
chanid
unsigned short


decompiled_code:
_BOOL8 mcs_recv_cjcf()
{
unsigned __int8 **v0; // rax
unsigned __int8 *v1; // rdx
unsigned __int8 v2; // si
unsigned __int8 v3; // cl
unsigned __int8 *v4; // rcx
unsigned __int8 *v5; // rdx
_BOOL8 result; // rax

v0 = (unsigned __int8 **)iso_recv();
if ( !v0 )
return 0LL;
v1 = (*v0)++;
v2 = *v1;
if ( *v1 >> 2 == 15 )
{
*v0 = v1 + 2;
v3 = v1[1];
if ( v3 )
{
error((int)"CJrq: %d\n", v3, (const char *)v1);
result = 0LL;
}
else
{
v4 = v1 + 8;
v5 = v1 + 6;
if ( (v2 & 2) != 0 )
v5 = v4;
*v0 = v5;
result = v5 == v0[1];
}
}
else
{
error((int)"expected CJcf, got %d\n", v2, (const char *)v1);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
_BOOL8 func6()
{
unsigned __int8 **v0; // rax
unsigned __int8 *v1; // rdx
unsigned __int8 v2; // si
unsigned __int8 v3; // cl
unsigned __int8 *v4; // rcx
unsigned __int8 *v5; // rdx
_BOOL8 result; // rax

v0 = (unsigned __int8 **)iso_recv();
if ( !v0 )
return 0LL;
v1 = (*v0)++;
v2 = *v1;
if ( *v1 >> 2 == 15 )
{
*v0 = v1 + 2;
v3 = v1[1];
if ( v3 )
{
error((int)"CJrq: %d\n", v3, (const char *)v1);
result = 0LL;
}
else
{
v4 = v1 + 8;
v5 = v1 + 6;
if ( (v2 & 2) != 0 )
v5 = v4;
*v0 = v5;
result = v5 == v0[1];
}
}
else
{
error((int)"expected CJcf, got %d\n", v2, (const char *)v1);
result = 0LL;
}
return result;
}

original_code:
static BOOL
mcs_recv_cjcf(void)
{
 uint8 opcode, result;
 STREAM s;
 s = iso_recv();
 if (s == ((void *)0))
  return (0);
 opcode = *((s)->p++);;
 if ((opcode >> 2) != MCS_CJCF)
 {
  error("expected CJcf, got %d\n", opcode);
  return (0);
 }
 result = *((s)->p++);;
 if (result != 0)
 {
  error("CJrq: %d\n", result);
  return (0);
 }
 (s)->p += 4;;
 if (opcode & 2)
  (s)->p += 2;;
 return ((s)->p == (s)->end);
}

canonical_original_code:
static int mcs_recv_cjcf()
{
 unsigned char opcode; unsigned char result;
 struct stream *s;
 s = iso_recv();
 if (s == ((void *)0))
  return (0);
 opcode = *((s)->p++);;
 if ((opcode >> 2) != MCS_CJCF)
 {
  error("expected CJcf, got %d\n", opcode);
  return (0);
 }
 result = *((s)->p++);;
 if (result != 0)
 {
  error("CJrq: %d\n", result);
  return (0);
 }
 (s)->p += 4;;
 if (opcode & 2)
  (s)->p += 2;;
 return ((s)->p == (s)->end);
}

variable_types:
opcode
unsigned char

variable_types:
result
unsigned char


decompiled_code:
__int64 __fastcall mcs_send_connect_initial(__int64 a1)
{
__int64 v1; // rbp
void **v2; // rbx
_BYTE *v3; // rax
char *v4; // rbp

v1 = *(_QWORD *)(a1 + 8) - *(_QWORD *)(a1 + 16);
v2 = (void **)iso_init((unsigned int)(v1 + 118));
ber_out_header(v2, 32613, v1 + 113);
ber_out_header(v2, 4, 0);
ber_out_header(v2, 4, 0);
ber_out_header(v2, 1, 1);
v3 = *v2;
*v2 = (char *)*v2 + 1;
*v3 = -1;
mcs_out_domain_params(v2, 2, 2, 0, 0xFFFF);
mcs_out_domain_params(v2, 1, 1, 1, 1056);
mcs_out_domain_params(v2, 0xFFFF, 64535, 0xFFFF, 0xFFFF);
ber_out_header(v2, 4, v1);
memcpy(*v2, *(const void **)(a1 + 16), (int)v1);
v4 = (char *)*v2 + (int)v1;
*v2 = v4;
v2[1] = v4;
return iso_send(v2);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
__int64 v1; // rbp
void **v2; // rbx
_BYTE *v3; // rax
char *v4; // rbp

v1 = *(_QWORD *)(a1 + 8) - *(_QWORD *)(a1 + 16);
v2 = (void **)iso_init((unsigned int)(v1 + 118));
ber_out_header(v2, 32613, v1 + 113);
ber_out_header(v2, 4, 0);
ber_out_header(v2, 4, 0);
ber_out_header(v2, 1, 1);
v3 = *v2;
*v2 = (char *)*v2 + 1;
*v3 = -1;
mcs_out_domain_params(v2, 2, 2, 0, 0xFFFF);
mcs_out_domain_params(v2, 1, 1, 1, 1056);
mcs_out_domain_params(v2, 0xFFFF, 64535, 0xFFFF, 0xFFFF);
ber_out_header(v2, 4, v1);
memcpy(*v2, *(const void **)(a1 + 16), (int)v1);
v4 = (char *)*v2 + (int)v1;
*v2 = v4;
v2[1] = v4;
return iso_send(v2);
}

original_code:
static void
mcs_send_connect_initial(STREAM mcs_data)
{
 int datalen = mcs_data->end - mcs_data->data;
 int length = 7 + 3 * 34 + 4 + datalen;
 STREAM s;
 s = iso_init(length + 5);
 ber_out_header(s, 0x7f65, length);
 ber_out_header(s, 4, 0);
 ber_out_header(s, 4, 0);
 ber_out_header(s, 1, 1);
 *((s)->p++) = 0xff;;
 mcs_out_domain_params(s, 2, 2, 0, 0xffff);
 mcs_out_domain_params(s, 1, 1, 1, 0x420);
 mcs_out_domain_params(s, 0xffff, 0xfc17, 0xffff, 0xffff);
 ber_out_header(s, 4, datalen);
 { memcpy((s)->p,mcs_data->data,datalen); (s)->p += datalen; };
 (s)->end = (s)->p;;
 iso_send(s);
}

canonical_original_code:
static void mcs_send_connect_initial(struct stream *mcs_data)
{
 int datalen = mcs_data->end - mcs_data->data;
 int length = 7 + 3 * 34 + 4 + datalen;
 struct stream *s;
 s = iso_init(length + 5);
 ber_out_header(s, 0x7f65, length);
 ber_out_header(s, 4, 0);
 ber_out_header(s, 4, 0);
 ber_out_header(s, 1, 1);
 *((s)->p++) = 0xff;;
 mcs_out_domain_params(s, 2, 2, 0, 0xffff);
 mcs_out_domain_params(s, 1, 1, 1, 0x420);
 mcs_out_domain_params(s, 0xffff, 0xfc17, 0xffff, 0xffff);
 ber_out_header(s, 4, datalen);
 { memcpy((s)->p,mcs_data->data,datalen); (s)->p += datalen; };
 (s)->end = (s)->p;;
 iso_send(s);
}

variable_types: datalen int

variable_types: length int


decompiled_code:
_BOOL8 __fastcall mcs_recv_connect_response(__int64 a1)
{
unsigned __int64 *v1; // rbx
_BOOL8 result; // rax
_BYTE *v3; // rax
const char *v4; // rdx
const char *v5; // rdx
__int64 v6; // rax
int errnum[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = (unsigned __int64 *)iso_recv();
result = 0LL;
if ( v1 )
{
ber_parse_header(v1, 32614, errnum);
ber_parse_header(v1, 10, errnum);
v3 = (_BYTE *)*v1;
v4 = (const char *)(*v1 + 1);
*v1 = (unsigned __int64)v4;
if ( *v3 )
{
error((int)"MCS connect: %d\n", (unsigned __int8)*v3, v4);
result = 0LL;
}
else
{
ber_parse_header(v1, 2, errnum);
*v1 += errnum[0];
mcs_parse_domain_params(v1);
ber_parse_header(v1, 4, errnum);
if ( *(_DWORD *)(a1 + 24) < errnum[0] )
{
error((int)"MCS data length %d\n", errnum[0], v5);
errnum[0] = *(_DWORD *)(a1 + 24);
}
memcpy(*(void **)(a1 + 16), (const void *)*v1, errnum[0]);
*v1 += errnum[0];
v6 = *(_QWORD *)(a1 + 16);
*(_QWORD *)a1 = v6;
*(_QWORD *)(a1 + 8) = errnum[0] + v6;
result = *v1 == v1[1];
}
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func8(__int64 a1)
{
unsigned __int64 *v1; // rbx
_BOOL8 result; // rax
_BYTE *v3; // rax
const char *v4; // rdx
const char *v5; // rdx
__int64 v6; // rax
int errnum[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = (unsigned __int64 *)iso_recv();
result = 0LL;
if ( v1 )
{
func3(v1, 32614, errnum);
func3(v1, 10, errnum);
v3 = (_BYTE *)*v1;
v4 = (const char *)(*v1 + 1);
*v1 = (unsigned __int64)v4;
if ( *v3 )
{
error((int)"MCS connect: %d\n", (unsigned __int8)*v3, v4);
result = 0LL;
}
else
{
func3(v1, 2, errnum);
*v1 += errnum[0];
func4(v1);
func3(v1, 4, errnum);
if ( *(_DWORD *)(a1 + 24) < errnum[0] )
{
error((int)"MCS data length %d\n", errnum[0], v5);
errnum[0] = *(_DWORD *)(a1 + 24);
}
memcpy(*(void **)(a1 + 16), (const void *)*v1, errnum[0]);
*v1 += errnum[0];
v6 = *(_QWORD *)(a1 + 16);
*(_QWORD *)a1 = v6;
*(_QWORD *)(a1 + 8) = errnum[0] + v6;
result = *v1 == v1[1];
}
}
return result;
}

original_code:
static BOOL
mcs_recv_connect_response(STREAM mcs_data)
{
 uint8 result;
 int length;
 STREAM s;
 s = iso_recv();
 if (s == ((void *)0))
  return (0);
 ber_parse_header(s, 0x7f66, &length);
 ber_parse_header(s, 10, &length);
 result = *((s)->p++);;
 if (result != 0)
 {
  error("MCS connect: %d\n", result);
  return (0);
 }
 ber_parse_header(s, 2, &length);
 (s)->p += length;;
 mcs_parse_domain_params(s);
 ber_parse_header(s, 4, &length);
 if (length > mcs_data->size)
 {
  error("MCS data length %d\n", length);
  length = mcs_data->size;
 }
 { memcpy(mcs_data->data,(s)->p,length); (s)->p += length; };
 mcs_data->p = mcs_data->data;
 mcs_data->end = mcs_data->data + length;
 return ((s)->p == (s)->end);
}

canonical_original_code:
static int mcs_recv_connect_response(struct stream *mcs_data)
{
 unsigned char result;
 int length;
 struct stream *s;
 s = iso_recv();
 if (s == ((void *)0))
  return (0);
 ber_parse_header(s, 0x7f66, &length);
 ber_parse_header(s, 10, &length);
 result = *((s)->p++);;
 if (result != 0)
 {
  error("MCS connect: %d\n", result);
  return (0);
 }
 ber_parse_header(s, 2, &length);
 (s)->p += length;;
 mcs_parse_domain_params(s);
 ber_parse_header(s, 4, &length);
 if (length > mcs_data->size)
 {
  error("MCS data length %d\n", length);
  length = mcs_data->size;
 }
 { memcpy(mcs_data->data,(s)->p,length); (s)->p += length; };
 mcs_data->p = mcs_data->data;
 mcs_data->end = mcs_data->data + length;
 return ((s)->p == (s)->end);
}


variable_types:
length
unsigned short


decompiled_code:
_QWORD *mcs_recv()
{
_QWORD *result; // rax
const char *v1; // rdx
char v2; // cl

result = (_QWORD *)iso_recv();
if ( result )
{
v1 = (const char *)(*result)++;
v2 = (unsigned __int8)*v1 >> 2;
if ( v2 == 26 )
{
*result = v1 + 7;
if ( v1[6] < 0 )
*result = v1 + 8;
}
else
{
result = 0LL;
if ( v2 != 8 )
{
error((int)"expected data, got %d\n", *(unsigned __int8 *)v1, v1);
result = 0LL;
}
}
}
return result;
}

canonical_decompiled_code:
_QWORD *func10()
{
_QWORD *result; // rax
const char *v1; // rdx
char v2; // cl

result = (_QWORD *)iso_recv();
if ( result )
{
v1 = (const char *)(*result)++;
v2 = (unsigned __int8)*v1 >> 2;
if ( v2 == 26 )
{
*result = v1 + 7;
if ( v1[6] < 0 )
*result = v1 + 8;
}
else
{
result = 0LL;
if ( v2 != 8 )
{
error((int)"expected data, got %d\n", *(unsigned __int8 *)v1, v1);
result = 0LL;
}
}
}
return result;
}

original_code:
STREAM
mcs_recv(void)
{
 uint8 opcode, appid, length;
 STREAM s;
 s = iso_recv();
 if (s == ((void *)0))
  return ((void *)0);
 opcode = *((s)->p++);;
 appid = opcode >> 2;
 if (appid != MCS_SDIN)
 {
  if (appid != MCS_DPUM)
  {
   error("expected data, got %d\n", opcode);
  }
  return ((void *)0);
 }
 (s)->p += 5;;
 length = *((s)->p++);;
 if (length & 0x80)
  (s)->p += 1;;
 return s;
}

canonical_original_code:
struct stream *mcs_recv()
{
 unsigned char opcode; unsigned char appid; unsigned char length;
 struct stream *s;
 s = iso_recv();
 if (s == ((void *)0))
  return ((void *)0);
 opcode = *((s)->p++);;
 appid = opcode >> 2;
 if (appid != MCS_SDIN)
 {
  if (appid != MCS_DPUM)
  {
   error("expected data, got %d\n", opcode);
  }
  return ((void *)0);
 }
 (s)->p += 5;;
 length = *((s)->p++);;
 if (length & 0x80)
  (s)->p += 1;;
 return s;
}

variable_types:
appid
unsigned char

variable_types:
length
unsigned char

return_type: struct stream { unsigned char *p; unsigned char *end; unsigned char *data; unsigned int size; unsigned char *iso_hdr; unsigned char *mcs_hdr; unsigned char *sec_hdr; unsigned char *rdp_hdr; } *


decompiled_code:
__int64 __fastcall mcs_connect(__int64 a1, __int64 a2)
{
__int64 result; // rax
_BOOL4 v3; // edx

result = iso_connect();
if ( (_DWORD)result )
{
mcs_send_connect_initial(a2);
if ( !mcs_recv_connect_response(a2)
|| (mcs_send_edrq(), mcs_send_aurq(), !mcs_recv_aucf(&mcs_userid))
|| (mcs_send_cjrq(mcs_userid + 1001), !mcs_recv_cjcf())
|| (mcs_send_cjrq(1003), v3 = mcs_recv_cjcf(), result = 1LL, !v3) )
{
iso_disconnect();
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, __int64 a2)
{
__int64 result; // rax
_BOOL4 v3; // edx

result = iso_connect();
if ( (_DWORD)result )
{
func7(a2);
if ( !func8(a2)
|| (func0(), func1(), !func5(&mcs_userid))
|| (func2(mcs_userid + 1001), !func6())
|| (func2(1003), v3 = func6(), result = 1LL, !v3) )
{
iso_disconnect();
result = 0LL;
}
}
return result;
}

original_code:
BOOL
mcs_connect(char *server, STREAM mcs_data)
{
 if (!iso_connect(server))
  return (0);
 mcs_send_connect_initial(mcs_data);
 if (!mcs_recv_connect_response(mcs_data))
  goto error;
 mcs_send_edrq();
 mcs_send_aurq();
 if (!mcs_recv_aucf(&mcs_userid))
  goto error;
 mcs_send_cjrq(mcs_userid + 1001);
 if (!mcs_recv_cjcf())
  goto error;
 mcs_send_cjrq(1003);
 if (!mcs_recv_cjcf())
  goto error;
 return (1);
      error:
 iso_disconnect();
 return (0);
}

canonical_original_code:
int mcs_connect(char *server, struct stream *mcs_data)
{
 if (!iso_connect(server))
  return (0);
 mcs_send_connect_initial(mcs_data);
 if (!mcs_recv_connect_response(mcs_data))
  goto error;
 mcs_send_edrq();
 mcs_send_aurq();
 if (!mcs_recv_aucf(&mcs_userid))
  goto error;
 mcs_send_cjrq(mcs_userid + 1001);
 if (!mcs_recv_cjcf())
  goto error;
 mcs_send_cjrq(1003);
 if (!mcs_recv_cjcf())
  goto error;
 return (1);
      error:
 iso_disconnect();
 return (0);
}

variable_types:
server
char *


decompiled_code:
__int64 mcs_disconnect()
{
return iso_disconnect();
}

canonical_decompiled_code:
__int64 func12()
{
return iso_disconnect();
}

original_code:
void
mcs_disconnect(void)
{
 iso_disconnect();
}

canonical_original_code:
void mcs_disconnect()
{
 iso_disconnect();
}


decompiled_code:
__int64 __fastcall licence_generate_keys(__int64 a1, __int64 a2, __int64 a3)
{
_BYTE v4[48]; // [rsp+0h] [rbp-78h] BYREF
__m128i v5; // [rsp+30h] [rbp-48h] BYREF
char v6[56]; // [rsp+40h] [rbp-38h] BYREF

sec_hash_48(v4, a3, a1, a2, 65LL);
sec_hash_48(&v5, v4, a2, a1, 65LL);
licence_sign_key = (__int128)_mm_loadu_si128(&v5);
return sec_hash_16(&licence_key, v6, a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2, __int64 a3)
{
_BYTE v4[48]; // [rsp+0h] [rbp-78h] BYREF
__m128i v5; // [rsp+30h] [rbp-48h] BYREF
char v6[56]; // [rsp+40h] [rbp-38h] BYREF

sec_hash_48(v4, a3, a1, a2, 65LL);
sec_hash_48(&v5, v4, a2, a1, 65LL);
licence_sign_key = (__int128)_mm_loadu_si128(&v5);
return sec_hash_16(&licence_key, v6, a1, a2);
}

original_code:
static void
licence_generate_keys(uint8 * client_key, uint8 * server_key, uint8 * client_rsa)
{
 uint8 session_key[48];
 uint8 temp_hash[48];
 sec_hash_48(temp_hash, client_rsa, client_key, server_key, 65);
 sec_hash_48(session_key, temp_hash, server_key, client_key, 65);
 memcpy(licence_sign_key, session_key, 16);
 sec_hash_16(licence_key, &session_key[16], client_key, server_key);
}

canonical_original_code:
static void licence_generate_keys(unsigned char *client_key, unsigned char *server_key, unsigned char *client_rsa)
{
 unsigned char session_key[48];
 unsigned char temp_hash[48];
 sec_hash_48(temp_hash, client_rsa, client_key, server_key, 65);
 sec_hash_48(session_key, temp_hash, server_key, client_key, 65);
 memcpy(licence_sign_key, session_key, 16);
 sec_hash_16(licence_key, &session_key[16], client_key, server_key);
}


decompiled_code:
__int64 __fastcall licence_process_demand(__int64 *a1)
{
__int64 v1; // rsi
__m128i v3; // [rsp+0h] [rbp-48h] BYREF
__int64 v4; // [rsp+10h] [rbp-38h]
__int64 v5; // [rsp+18h] [rbp-30h]
__int64 v6; // [rsp+20h] [rbp-28h]
__int64 v7; // [rsp+28h] [rbp-20h]
__int64 v8; // [rsp+30h] [rbp-18h]
__int64 v9; // [rsp+38h] [rbp-10h]

v1 = *a1;
*a1 += 32LL;
v3 = 0uLL;
v4 = 0LL;
v5 = 0LL;
v6 = 0LL;
v7 = 0LL;
v8 = 0LL;
v9 = 0LL;
licence_generate_keys((__int64)&v3, v1, (__int64)&v3);
return licence_send_request(&v3, &v3, (const char *)&username, &hostname);
}

original_code:
static void
licence_process_demand(STREAM s)
{
 uint8 null_data[64];
 uint8 *server_random;
 { server_random = (s)->p; (s)->p += 32; };
 memset(null_data, 0, sizeof(null_data));
 licence_generate_keys(null_data, server_random, null_data);
 licence_send_request(null_data, null_data, username, hostname);
}

canonical_original_code:
static void licence_process_demand(struct stream *s)
{
 unsigned char null_data[64];
 unsigned char *server_random;
 { server_random = (s)->p; (s)->p += 32; };
 memset(null_data, 0, sizeof(null_data));
 licence_generate_keys(null_data, server_random, null_data);
 licence_send_request(null_data, null_data, username, hostname);
}


variable_types:
token
unsigned char *

variable_types:
signature
unsigned char *


decompiled_code:
unsigned __int64 __fastcall licence_process_issue(_QWORD *a1)
{
__int64 v1; // rdx
int v2; // ecx
__int64 v3; // rbp
unsigned __int64 result; // rax
unsigned __int8 *v5; // rax
__int16 v6; // dx
_BYTE v7[1064]; // [rsp+0h] [rbp-428h] BYREF

v1 = *a1;
*a1 += 3LL;
v2 = *(unsigned __int8 *)(v1 + 2);
*a1 = v1 + 4;
v3 = v2 + (*(unsigned __int8 *)(v1 + 3) << 8);
result = v3 + v1 + 4;
if ( a1[1] >= result )
{
RC4_set_key(v7, 16LL, &licence_key);
RC4(v7, v3, *a1, *a1);
v5 = (unsigned __int8 *)(*a1)++;
v6 = *v5;
*a1 = v5 + 2;
result = v5[1] << 8;
if ( !((_WORD)result + v6) )
licence_issued = 1;
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func4(_QWORD *a1)
{
__int64 v1; // rdx
int v2; // ecx
__int64 v3; // rbp
unsigned __int64 result; // rax
unsigned __int8 *v5; // rax
__int16 v6; // dx
_BYTE v7[1064]; // [rsp+0h] [rbp-428h] BYREF

v1 = *a1;
*a1 += 3LL;
v2 = *(unsigned __int8 *)(v1 + 2);
*a1 = v1 + 4;
v3 = v2 + (*(unsigned __int8 *)(v1 + 3) << 8);
result = v3 + v1 + 4;
if ( a1[1] >= result )
{
RC4_set_key(v7, 16LL, &licence_key);
RC4(v7, v3, *a1, *a1);
v5 = (unsigned __int8 *)(*a1)++;
v6 = *v5;
*a1 = v5 + 2;
result = v5[1] << 8;
if ( !((_WORD)result + v6) )
licence_issued = 1;
}
return result;
}

original_code:
static void
licence_process_issue(STREAM s)
{
 RC4_KEY crypt_key;
 uint32 length;
 uint16 check;
 (s)->p += 2;;
 { length = *((s)->p++); length += *((s)->p++) << 8; };
 if (!((s)->p + length <= (s)->end))
  return;
 RC4_set_key(&crypt_key, 16, licence_key);
 RC4(&crypt_key, length, s->p, s->p);
 { check = *((s)->p++); check += *((s)->p++) << 8; };
 if (check != 0)
  return;
 licence_issued = (1);
}

canonical_original_code:
static void licence_process_issue(struct stream *s)
{
 struct rc4_key_st crypt_key;
 unsigned int length;
 unsigned short check;
 (s)->p += 2;;
 { length = *((s)->p++); length += *((s)->p++) << 8; };
 if (!((s)->p + length <= (s)->end))
  return;
 RC4_set_key(&crypt_key, 16, licence_key);
 RC4(&crypt_key, length, s->p, s->p);
 { check = *((s)->p++); check += *((s)->p++) << 8; };
 if (check != 0)
  return;
 licence_issued = (1);
}

variable_types: struct rc4_key_st { unsigned int x; unsigned int data[256]; }

variable_types:
length
unsigned int

variable_types:
check
unsigned short


decompiled_code:
char *__fastcall licence_generate_hwid(__int64 a1)
{
buf_out_uint32(a1, 2LL);
return strncpy((char *)(a1 + 4), &hostname, 0x10uLL);
}

canonical_decompiled_code:
char *__fastcall func6(__int64 a1)
{
buf_out_uint32(a1, 2LL);
return strncpy((char *)(a1 + 4), &hostname, 0x10uLL);
}

original_code:
static void
licence_generate_hwid(uint8 * hwid)
{
 buf_out_uint32(hwid, 2);
 strncpy((char *) (hwid + 4), hostname, 20 - 4);
}

canonical_original_code:
static void licence_generate_hwid(unsigned char *hwid)
{
 buf_out_uint32(hwid, 2);
 strncpy((char *) (hwid + 4), hostname, 20 - 4);
}

variable_types:
hwid
unsigned char *


original_code:
static void
licence_process_authreq(STREAM s)
{
 uint8 *in_token, *in_sig;
 uint8 out_token[10], decrypt_token[10];
 uint8 hwid[20], crypt_hwid[20];
 uint8 sealed_buffer[10 + 20];
 uint8 out_sig[16];
 RC4_KEY crypt_key;
 licence_parse_authreq(s, &in_token, &in_sig);
 memcpy(out_token, in_token, 10);
 RC4_set_key(&crypt_key, 16, licence_key);
 RC4(&crypt_key, 10, in_token, decrypt_token);
 licence_generate_hwid(hwid);
 memcpy(sealed_buffer, decrypt_token, 10);
 memcpy(sealed_buffer + 10, hwid, 20);
 sec_sign(out_sig, 16, licence_sign_key, 16, sealed_buffer, sizeof(sealed_buffer));
 RC4_set_key(&crypt_key, 16, licence_key);
 RC4(&crypt_key, 20, hwid, crypt_hwid);
 licence_send_authresp(out_token, crypt_hwid, out_sig);
}

canonical_original_code:
static void licence_process_authreq(struct stream *s)
{
 unsigned char *in_token; unsigned char *in_sig;
 unsigned char out_token[10]; unsigned char decrypt_token[10];
 unsigned char hwid[20]; unsigned char crypt_hwid[20];
 unsigned char sealed_buffer[-1];
 unsigned char out_sig[16];
 struct rc4_key_st crypt_key;
 licence_parse_authreq(s, &in_token, &in_sig);
 memcpy(out_token, in_token, 10);
 RC4_set_key(&crypt_key, 16, licence_key);
 RC4(&crypt_key, 10, in_token, decrypt_token);
 licence_generate_hwid(hwid);
 memcpy(sealed_buffer, decrypt_token, 10);
 memcpy(sealed_buffer + 10, hwid, 20);
 sec_sign(out_sig, 16, licence_sign_key, 16, sealed_buffer, sizeof(sealed_buffer));
 RC4_set_key(&crypt_key, 16, licence_key);
 RC4(&crypt_key, 20, hwid, crypt_hwid);
 licence_send_authresp(out_token, crypt_hwid, out_sig);
}

variable_types:
hwid
unsigned char [20]

user_defined_types: struct rc4_key_st { unsigned int x; unsigned int data[256]; }


decompiled_code:
unsigned __int64 __fastcall licence_process(__int64 *a1)
{
unsigned __int8 *v1; // rdx
unsigned __int64 result; // rax
unsigned __int16 v3; // si

v1 = (unsigned __int8 *)(*a1)++;
result = *v1;
*a1 = (__int64)(v1 + 2);
v3 = result + (v1[1] << 8);
*a1 = (__int64)(v1 + 4);
if ( v3 == 515 )
return licence_process_issue(a1);
if ( v3 <= 0x203u )
{
if ( v3 == 513 )
return licence_process_demand(a1);
if ( v3 == 514 )
return licence_process_authreq(a1);
return unimpl("licence tag 0x%x\n", v3);
}
if ( v3 != 516 && v3 != 767 )
return unimpl("licence tag 0x%x\n", v3);
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func8(__int64 *a1)
{
unsigned __int8 *v1; // rdx
unsigned __int64 result; // rax
unsigned __int16 v3; // si

v1 = (unsigned __int8 *)(*a1)++;
result = *v1;
*a1 = (__int64)(v1 + 2);
v3 = result + (v1[1] << 8);
*a1 = (__int64)(v1 + 4);
if ( v3 == 515 )
return func4(a1);
if ( v3 <= 0x203u )
{
if ( v3 == 513 )
return func2(a1);
if ( v3 == 514 )
return func7(a1);
return unimpl("licence tag 0x%x\n", v3);
}
if ( v3 != 516 && v3 != 767 )
return unimpl("licence tag 0x%x\n", v3);
return result;
}

original_code:
void
licence_process(STREAM s)
{
 uint16 tag;
 { tag = *((s)->p++); tag += *((s)->p++) << 8; };
 (s)->p += 2;;
 switch (tag)
 {
  case 0x0201:
   licence_process_demand(s);
   break;
  case 0x0202:
   licence_process_authreq(s);
   break;
  case 0x0203:
   licence_process_issue(s);
   break;
  case 0x0204:
   break;
  case 0x02ff:
   break;
  default:
   unimpl("licence tag 0x%x\n", tag);
 }
}

canonical_original_code:
void licence_process(struct stream *s)
{
 unsigned short tag;
 { tag = *((s)->p++); tag += *((s)->p++) << 8; };
 (s)->p += 2;;
 switch (tag)
 {
  case 0x0201:
   licence_process_demand(s);
   break;
  case 0x0202:
   licence_process_authreq(s);
   break;
  case 0x0203:
   licence_process_issue(s);
   break;
  case 0x0204:
   break;
  case 0x02ff:
   break;
  default:
   unimpl("licence tag 0x%x\n", tag);
 }
}

variable_types: struct stream { unsigned char *p; unsigned char *end; unsigned char *data; unsigned int size; unsigned char *iso_hdr; unsigned char *mcs_hdr; unsigned char *sec_hdr; unsigned char *rdp_hdr; } *

variable_types:
tag
unsigned short


decompiled_code:
__int64 __fastcall update_modifier_state(unsigned __int8 a1, int a2)
{
__int64 result; // rax
_BOOL4 v3; // edx
unsigned int v4; // eax
int v5; // edx
int v6; // eax

if ( a1 == 69 )
{
if ( a2 )
{
v5 = (unsigned __int16)remote_modifier_state;
BYTE1(v5) = HIBYTE(remote_modifier_state) & 0xFE;
v6 = ~(unsigned __int16)remote_modifier_state;
LOWORD(v6) = v6 & 0x100;
result = v5 | (unsigned int)v6;
remote_modifier_state = result;
}
}
else if ( a1 <= 0x45u )
{
if ( a1 == 54 )
{
v3 = a2 != 0;
result = v3 | remote_modifier_state & 0xFFFEu;
remote_modifier_state = v3 | remote_modifier_state & 0xFFFE;
}
else if ( a1 <= 0x36u )
{
if ( a1 == 29 )
{
result = (32 * (a2 != 0)) | remote_modifier_state & 0xFFDFu;
remote_modifier_state = (32 * (a2 != 0)) | remote_modifier_state & 0xFFDF;
}
else if ( a1 == 42 )
{
result = (2 * (a2 != 0)) | remote_modifier_state & 0xFFFDu;
remote_modifier_state = (2 * (a2 != 0)) | remote_modifier_state & 0xFFFD;
}
}
else if ( a1 == 56 )
{
result = (8 * (a2 != 0)) | remote_modifier_state & 0xFFF7u;
remote_modifier_state = (8 * (a2 != 0)) | remote_modifier_state & 0xFFF7;
}
}
else if ( a1 == 0xDB )
{
v4 = (unsigned __int16)remote_modifier_state;
LOBYTE(v4) = remote_modifier_state & 0x7F;
result = ((a2 != 0) << 7) | v4;
remote_modifier_state = result;
}
else if ( a1 <= 0xDBu )
{
if ( a1 == 0x9D )
{
result = (16 * (a2 != 0)) | remote_modifier_state & 0xFFEFu;
remote_modifier_state = (16 * (a2 != 0)) | remote_modifier_state & 0xFFEF;
}
else if ( a1 == 0xB8 )
{
result = (4 * (a2 != 0)) | remote_modifier_state & 0xFFFBu;
remote_modifier_state = (4 * (a2 != 0)) | remote_modifier_state & 0xFFFB;
}
}
else if ( a1 == 0xDC )
{
result = ((a2 != 0) << 6) | remote_modifier_state & 0xFFBFu;
remote_modifier_state = ((a2 != 0) << 6) | remote_modifier_state & 0xFFBF;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int8 a1, int a2)
{
__int64 result; // rax
_BOOL4 v3; // edx
unsigned int v4; // eax
int v5; // edx
int v6; // eax

if ( a1 == 69 )
{
if ( a2 )
{
v5 = (unsigned __int16)remote_modifier_state;
BYTE1(v5) = HIBYTE(remote_modifier_state) & 0xFE;
v6 = ~(unsigned __int16)remote_modifier_state;
LOWORD(v6) = v6 & 0x100;
result = v5 | (unsigned int)v6;
remote_modifier_state = result;
}
}
else if ( a1 <= 0x45u )
{
if ( a1 == 54 )
{
v3 = a2 != 0;
result = v3 | remote_modifier_state & 0xFFFEu;
remote_modifier_state = v3 | remote_modifier_state & 0xFFFE;
}
else if ( a1 <= 0x36u )
{
if ( a1 == 29 )
{
result = (32 * (a2 != 0)) | remote_modifier_state & 0xFFDFu;
remote_modifier_state = (32 * (a2 != 0)) | remote_modifier_state & 0xFFDF;
}
else if ( a1 == 42 )
{
result = (2 * (a2 != 0)) | remote_modifier_state & 0xFFFDu;
remote_modifier_state = (2 * (a2 != 0)) | remote_modifier_state & 0xFFFD;
}
}
else if ( a1 == 56 )
{
result = (8 * (a2 != 0)) | remote_modifier_state & 0xFFF7u;
remote_modifier_state = (8 * (a2 != 0)) | remote_modifier_state & 0xFFF7;
}
}
else if ( a1 == 0xDB )
{
v4 = (unsigned __int16)remote_modifier_state;
LOBYTE(v4) = remote_modifier_state & 0x7F;
result = ((a2 != 0) << 7) | v4;
remote_modifier_state = result;
}
else if ( a1 <= 0xDBu )
{
if ( a1 == 0x9D )
{
result = (16 * (a2 != 0)) | remote_modifier_state & 0xFFEFu;
remote_modifier_state = (16 * (a2 != 0)) | remote_modifier_state & 0xFFEF;
}
else if ( a1 == 0xB8 )
{
result = (4 * (a2 != 0)) | remote_modifier_state & 0xFFFBu;
remote_modifier_state = (4 * (a2 != 0)) | remote_modifier_state & 0xFFFB;
}
}
else if ( a1 == 0xDC )
{
result = ((a2 != 0) << 6) | remote_modifier_state & 0xFFBFu;
remote_modifier_state = ((a2 != 0) << 6) | remote_modifier_state & 0xFFBF;
}
return result;
}

original_code:
static void
update_modifier_state(uint8 scancode, BOOL pressed)
{
 switch (scancode)
 {
  case 0x2a:
   (remote_modifier_state = ((remote_modifier_state & ~(1<<1)) | (pressed ? (1<<1) : 0)));
   break;
  case 0x36:
   (remote_modifier_state = ((remote_modifier_state & ~(1<<0)) | (pressed ? (1<<0) : 0)));
   break;
  case 0x1d:
   (remote_modifier_state = ((remote_modifier_state & ~(1<<5)) | (pressed ? (1<<5) : 0)));
   break;
  case (0x80 | 0x1d):
   (remote_modifier_state = ((remote_modifier_state & ~(1<<4)) | (pressed ? (1<<4) : 0)));
   break;
  case 0x38:
   (remote_modifier_state = ((remote_modifier_state & ~(1<<3)) | (pressed ? (1<<3) : 0)));
   break;
  case (0x80 | 0x38):
   (remote_modifier_state = ((remote_modifier_state & ~(1<<2)) | (pressed ? (1<<2) : 0)));
   break;
  case (0x80 | 0x5b):
   (remote_modifier_state = ((remote_modifier_state & ~(1<<7)) | (pressed ? (1<<7) : 0)));
   break;
  case (0x80 | 0x5c):
   (remote_modifier_state = ((remote_modifier_state & ~(1<<6)) | (pressed ? (1<<6) : 0)));
   break;
  case 0x45:
   if (pressed)
   {
    BOOL newNumLockState;
    newNumLockState =
     (((remote_modifier_state & (1<<8))>0) == 0);
    (remote_modifier_state = ((remote_modifier_state & ~(1<<8)) | (newNumLockState ? (1<<8) : 0)));
   }
   break;
 }
}

canonical_original_code:
static void update_modifier_state(unsigned char scancode, int pressed)
{
 switch (scancode)
 {
  case 0x2a:
   (remote_modifier_state = ((remote_modifier_state & ~(1<<1)) | (pressed ? (1<<1) : 0)));
   break;
  case 0x36:
   (remote_modifier_state = ((remote_modifier_state & ~(1<<0)) | (pressed ? (1<<0) : 0)));
   break;
  case 0x1d:
   (remote_modifier_state = ((remote_modifier_state & ~(1<<5)) | (pressed ? (1<<5) : 0)));
   break;
  case (0x80 | 0x1d):
   (remote_modifier_state = ((remote_modifier_state & ~(1<<4)) | (pressed ? (1<<4) : 0)));
   break;
  case 0x38:
   (remote_modifier_state = ((remote_modifier_state & ~(1<<3)) | (pressed ? (1<<3) : 0)));
   break;
  case (0x80 | 0x38):
   (remote_modifier_state = ((remote_modifier_state & ~(1<<2)) | (pressed ? (1<<2) : 0)));
   break;
  case (0x80 | 0x5b):
   (remote_modifier_state = ((remote_modifier_state & ~(1<<7)) | (pressed ? (1<<7) : 0)));
   break;
  case (0x80 | 0x5c):
   (remote_modifier_state = ((remote_modifier_state & ~(1<<6)) | (pressed ? (1<<6) : 0)));
   break;
  case 0x45:
   if (pressed)
   {
    int newNumLockState;
    newNumLockState =
     (((remote_modifier_state & (1<<8))>0) == 0);
    (remote_modifier_state = ((remote_modifier_state & ~(1<<8)) | (newNumLockState ? (1<<8) : 0)));
   }
   break;
 }
}

variable_types:
scancode
unsigned char

variable_types: pressed int

variable_types: newNumLockState int


decompiled_code:
KeySym __fastcall add_to_keymap(const char *a1, char a2, __int16 a3, const char *a4)
{
KeySym result; // rax

result = XStringToKeysym(a1);
if ( !result )
return warning("Bad keysym %s in keymap %s\n", a1, a4);
result = (unsigned __int16)result;
keymap[4 * (unsigned __int16)result] = a2;
word_C62[2 * (unsigned __int16)result] = a3;
return result;
}

canonical_decompiled_code:
KeySym __fastcall func1(const char *a1, char a2, __int16 a3, const char *a4)
{
KeySym result; // rax

result = XStringToKeysym(a1);
if ( !result )
return warning("Bad keysym %s in keymap %s\n", a1, a4);
result = (unsigned __int16)result;
keymap[4 * (unsigned __int16)result] = a2;
word_C62[2 * (unsigned __int16)result] = a3;
return result;
}

original_code:
static void
add_to_keymap(char *keyname, uint8 scancode, uint16 modifiers, char *mapname)
{
 KeySym keysym;
 keysym = XStringToKeysym(keyname);
 if (keysym == 0L)
 {
  warning("Bad keysym %s in keymap %s\n", keyname, mapname);
  return;
 }
 ;
 keymap[keysym & 0xffff].scancode = scancode;
 keymap[keysym & 0xffff].modifiers = modifiers;
 return;
}

canonical_original_code:
static void add_to_keymap(char *keyname, unsigned char scancode, unsigned short modifiers, char *mapname)
{
 unsigned long keysym;
 keysym = XStringToKeysym(keyname);
 if (keysym == 0L)
 {
  warning("Bad keysym %s in keymap %s\n", keyname, mapname);
  return;
 }
 ;
 keymap[keysym & 0xffff].scancode = scancode;
 keymap[keysym & 0xffff].modifiers = modifiers;
 return;
}

variable_types:
keyname
char *

variable_types:
modifiers
unsigned short

variable_types:
mapname
char *

variable_types:
keysym
unsigned long


decompiled_code:
int xkeymap_init()
{
char v0; // di
char *v1; // rbx
int v3[3]; // [rsp+Ch] [rbp-Ch] BYREF

v0 = keymapname;
if ( keymapname )
{
v1 = (char *)&keymapname;
do
{
*v1++ = tolower(v0);
v0 = *v1;
}
while ( *v1 );
}
if ( strcmp((const char *)&keymapname, "none") && (unsigned int)xkeymap_read((const char *)&keymapname) )
keymap_loaded = 1;
return XDisplayKeycodes(display, &min_keycode, v3);
}

canonical_decompiled_code:
int func3()
{
char v0; // di
char *v1; // rbx
int v3[3]; // [rsp+Ch] [rbp-Ch] BYREF

v0 = keymapname;
if ( keymapname )
{
v1 = (char *)&keymapname;
do
{
*v1++ = tolower(v0);
v0 = *v1;
}
while ( *v1 );
}
if ( strcmp((const char *)&keymapname, "none") && (unsigned int)func2((const char *)&keymapname) )
keymap_loaded = 1;
return XDisplayKeycodes(display, &min_keycode, v3);
}

original_code:
void
xkeymap_init(void)
{
 unsigned int max_keycode;
 char *mapname_ptr;
 mapname_ptr = keymapname;
 while (*mapname_ptr)
 {
  *mapname_ptr = tolower((int)*mapname_ptr);
  mapname_ptr++;
 }
 if (strcmp(keymapname, "none"))
 {
  if (xkeymap_read(keymapname))
   keymap_loaded = 1;
 }
 XDisplayKeycodes(display, &min_keycode, (int *) &max_keycode);
}

canonical_original_code:
void xkeymap_init()
{
 unsigned int max_keycode;
 char *mapname_ptr;
 mapname_ptr = keymapname;
 while (*mapname_ptr)
 {
  *mapname_ptr = tolower((int)*mapname_ptr);
  mapname_ptr++;
 }
 if (strcmp(keymapname, "none"))
 {
  if (xkeymap_read(keymapname))
   keymap_loaded = 1;
 }
 XDisplayKeycodes(display, &min_keycode, (int *) &max_keycode);
}


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

switch ( a1 )
{
case 1:
result = 4096LL;
break;
case 2:
result = 0x4000LL;
break;
case 3:
result = 0x2000LL;
break;
case 4:
result = 640LL;
break;
case 5:
result = 896LL;
break;
default:
result = 0LL;
break;
}
return result;
}

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

switch ( a1 )
{
case 1:
result = 4096LL;
break;
case 2:
result = 0x4000LL;
break;
case 3:
result = 0x2000LL;
break;
case 4:
result = 640LL;
break;
case 5:
result = 896LL;
break;
default:
result = 0LL;
break;
}
return result;
}

original_code:
uint16
xkeymap_translate_button(unsigned int button)
{
 switch (button)
 {
  case 1:
   return 0x1000;
  case 2:
   return 0x4000;
  case 3:
   return 0x2000;
  case 4:
   return 0x0280;
  case 5:
   return 0x0380;
 }
 return 0;
}

canonical_original_code:
unsigned short xkeymap_translate_button(unsigned int button)
{
 switch (button)
 {
  case 1:
   return 0x1000;
  case 2:
   return 0x4000;
  case 3:
   return 0x2000;
  case 4:
   return 0x0280;
  case 5:
   return 0x0380;
 }
 return 0;
}

variable_types:
button
unsigned int

return_type: unsigned short


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

result = "NoSymbol";
if ( a1 )
{
result = XKeysymToString(a1);
if ( !result )
result = "(no name)";
}
return result;
}

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

result = "NoSymbol";
if ( a1 )
{
result = XKeysymToString(a1);
if ( !result )
result = "(no name)";
}
return result;
}

original_code:
char *
get_ksname(uint32 keysym)
{
 char *ksname = ((void *)0);
 if (keysym == 0L)
  ksname = "NoSymbol";
 else if (!(ksname = XKeysymToString(keysym)))
  ksname = "(no name)";
 return ksname;
}

canonical_original_code:
char *get_ksname(unsigned int keysym)
{
 char *ksname = ((void *)0);
 if (keysym == 0L)
  ksname = "NoSymbol";
 else if (!(ksname = XKeysymToString(keysym)))
  ksname = "(no name)";
 return ksname;
}

variable_types:
keysym
unsigned int

variable_types:
ksname
char *

return_type: char *


decompiled_code:
__int64 __fastcall xkeymap_translate_key(unsigned int a1, unsigned int a2, char a3)
{
int v4; // ebx
unsigned __int8 v5; // bp
char *v7; // rax

v4 = (unsigned __int16)word_C62[2 * (unsigned __int16)a1];
if ( (v4 & 0x800) != 0 )
return (unsigned int)(v4 << 16);
if ( (v4 & 0x400) != 0 && (a3 & 1) != 0 )
v4 = 2;
v5 = keymap[4 * (unsigned __int16)a1];
if ( v5 )
return v5 | (unsigned int)(v4 << 16);
if ( keymap_loaded )
{
v7 = get_ksname(a1);
warning("No translation for (keysym 0x%lx, %s)\n", a1, v7);
}
if ( a2 <= 0x60 && min_keycode <= a2 )
{
v5 = a2 - min_keycode;
if ( (a3 & 1) != 0 )
v4 = 2;
}
return (v4 << 16) | (unsigned int)v5;
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned int a1, unsigned int a2, char a3)
{
int v4; // ebx
unsigned __int8 v5; // bp
char *v7; // rax

v4 = (unsigned __int16)word_C62[2 * (unsigned __int16)a1];
if ( (v4 & 0x800) != 0 )
return (unsigned int)(v4 << 16);
if ( (v4 & 0x400) != 0 && (a3 & 1) != 0 )
v4 = 2;
v5 = keymap[4 * (unsigned __int16)a1];
if ( v5 )
return v5 | (unsigned int)(v4 << 16);
if ( keymap_loaded )
{
v7 = func5(a1);
warning("No translation for (keysym 0x%lx, %s)\n", a1, v7);
}
if ( a2 <= 0x60 && min_keycode <= a2 )
{
v5 = a2 - min_keycode;
if ( (a3 & 1) != 0 )
v4 = 2;
}
return (v4 << 16) | (unsigned int)v5;
}

original_code:
key_translation
xkeymap_translate_key(uint32 keysym, unsigned int keycode, unsigned int state)
{
 key_translation tr = { 0, 0 };
 tr = keymap[keysym & 0xffff];
 if (tr.modifiers & (1<<11))
 {
  ;
  tr.scancode = 0;
  return tr;
 }
 if (tr.modifiers & (1<<10))
 {
  if (state & (1<<0))
  {
   tr.modifiers = (1<<1);
  }
 }
 if (tr.scancode != 0)
 {
  ;
  return tr;
 }
 if (keymap_loaded)
  warning("No translation for (keysym 0x%lx, %s)\n", keysym, get_ksname(keysym));
 if ((keycode >= min_keycode) && (keycode <= 0x60))
 {
  tr.scancode = keycode - min_keycode;
  if (state & (1<<0))
  {
   tr.modifiers = (1<<1);
  }
  ;
 }
 else
 {
  ;
 }
 return tr;
}

canonical_original_code:
struct _key_translation xkeymap_translate_key(unsigned int keysym, unsigned int keycode, unsigned int state)
{
 struct _key_translation tr = { 0, 0 };
 tr = keymap[keysym & 0xffff];
 if (tr.modifiers & (1<<11))
 {
  ;
  tr.scancode = 0;
  return tr;
 }
 if (tr.modifiers & (1<<10))
 {
  if (state & (1<<0))
  {
   tr.modifiers = (1<<1);
  }
 }
 if (tr.scancode != 0)
 {
  ;
  return tr;
 }
 if (keymap_loaded)
  warning("No translation for (keysym 0x%lx, %s)\n", keysym, get_ksname(keysym));
 if ((keycode >= min_keycode) && (keycode <= 0x60))
 {
  tr.scancode = keycode - min_keycode;
  if (state & (1<<0))
  {
   tr.modifiers = (1<<1);
  }
  ;
 }
 else
 {
  ;
 }
 return tr;
}

variable_types:
keycode
unsigned int

variable_types:
state
unsigned int

variable_types: struct _key_translation { unsigned char scancode; unsigned short modifiers; }