softPwmThread

decompiled_code:
void __noreturn softPwmThread()
{
__int64 v0; // r12
int v1; // ebp
int v2; // ebx

v0 = newPin;
newPin = -1;
oliHiPri(50LL);
while ( 1 )
{
v1 = marks[v0];
v2 = range[v0] - v1;
if ( v1 )
digitalWrite((unsigned int)v0, 1LL);
delayMicroseconds((unsigned int)(100 * v1));
if ( v2 )
digitalWrite((unsigned int)v0, 0LL);
delayMicroseconds((unsigned int)(100 * v2));
}
}

canonical_decompiled_code:
void __noreturn func0()
{
__int64 v0; // r12
int v1; // ebp
int v2; // ebx

v0 = newPin;
newPin = -1;
oliHiPri(50LL);
while ( 1 )
{
v1 = marks[v0];
v2 = range[v0] - v1;
if ( v1 )
digitalWrite((unsigned int)v0, 1LL);
delayMicroseconds((unsigned int)(100 * v1));
if ( v2 )
digitalWrite((unsigned int)v0, 0LL);
delayMicroseconds((unsigned int)(100 * v2));
}
}

original_code:
static void *softPwmThread (void *dummy)
{
  int pin, mark, space;
  pin = newPin;
  newPin = -1;
  oliHiPri(50);
  for (;;)
  {
    mark = marks[pin];
    space = range[pin] - mark;
    if (mark != 0)
      digitalWrite(pin, 1);
    delayMicroseconds(mark * 100);
    if (space != 0)
      digitalWrite(pin, 0);
    delayMicroseconds(space * 100);
  }
  return ((void *)0);
}

canonical_original_code:
static void *softPwmThread(void *dummy)
{
  int pin; int mark; int space;
  pin = newPin;
  newPin = -1;
  oliHiPri(50);
  for (;;)
  {
    mark = marks[pin];
    space = range[pin] - mark;
    if (mark != 0)
      digitalWrite(pin, 1);
    delayMicroseconds(mark * 100);
    if (space != 0)
      digitalWrite(pin, 0);
    delayMicroseconds(space * 100);
  }
  return ((void *)0);
}

variable_types:
dummy
void *

variable_types: pin int

variable_types: mark int

variable_types: space int

return_type: void *


softPwmCreate

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

pinMode(a1, 1LL);
digitalWrite((unsigned int)a1, 0LL);
marks[(int)a1] = a2;
range[(int)a1] = a3;
newPin = a1;
v4 = oliThreadCreate(softPwmThread);
while ( newPin != -1 )
delay(1LL);
return v4;
}

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

pinMode(a1, 1LL);
digitalWrite((unsigned int)a1, 0LL);
marks[(int)a1] = a2;
range[(int)a1] = a3;
newPin = a1;
v4 = oliThreadCreate(softPwmThread);
while ( newPin != -1 )
delay(1LL);
return v4;
}

original_code:
int softPwmCreate(int pin, int initialValue, int pwmRange)
{
  int res;
  pinMode (pin, 1);
  digitalWrite(pin, 0);
  marks[pin] = initialValue;
  range[pin] = pwmRange;
  newPin = pin;
  res = oliThreadCreate(softPwmThread);
  while (newPin != -1)
    delay(1);
  return res;
}

variable_types: initialValue int

variable_types: pwmRange int

variable_types: res int

return_type: int


decompiled_code:
__int64 wiringOliI2CRead()
{
return i2c_smbus_read_byte();
}

canonical_decompiled_code:
__int64 func0()
{
return i2c_smbus_read_byte();
}

original_code:
int wiringOliI2CRead(int fd)
{
  return i2c_smbus_read_byte(fd);
}

variable_types: fd int


decompiled_code:
__int64 wiringOliI2CReadReg8()
{
return i2c_smbus_read_byte_data();
}

canonical_decompiled_code:
__int64 func1()
{
return i2c_smbus_read_byte_data();
}

original_code:
int wiringOliI2CReadReg8(int fd, int reg)
{
  return i2c_smbus_read_byte_data(fd, reg);
}

variable_types: reg int


decompiled_code:
__int64 wiringOliI2CReadReg16()
{
return i2c_smbus_read_word_data();
}

canonical_decompiled_code:
__int64 func2()
{
return i2c_smbus_read_word_data();
}

original_code:
int wiringOliI2CReadReg16(int fd, int reg)
{
  return i2c_smbus_read_word_data(fd, reg);
}


decompiled_code:
__int64 wiringOliI2CWrite()
{
return i2c_smbus_write_byte();
}

canonical_decompiled_code:
__int64 func3()
{
return i2c_smbus_write_byte();
}

original_code:
int wiringOliI2CWrite(int fd, int data)
{
  return i2c_smbus_write_byte(fd, data);
}

variable_types: data int


decompiled_code:
__int64 wiringOliI2CWriteReg8()
{
return i2c_smbus_write_byte_data();
}

canonical_decompiled_code:
__int64 func4()
{
return i2c_smbus_write_byte_data();
}

original_code:
int wiringOliI2CWriteReg8(int fd, int reg, int data)
{
  return i2c_smbus_write_byte_data(fd, reg, data);
}


decompiled_code:
__int64 wiringOliI2CWriteReg16()
{
return i2c_smbus_write_word_data();
}

canonical_decompiled_code:
__int64 func5()
{
return i2c_smbus_write_word_data();
}

original_code:
int wiringOliI2CWriteReg16(int fd, int reg, int data)
{
  return i2c_smbus_write_word_data(fd, reg, data);
}


decompiled_code:
__int64 __fastcall wiringOliI2CSetup(int a1, unsigned int a2)
{
int v2; // er12

if ( a1 == 2 )
{
v2 = open("/dev/i2c-2", 2);
}
else if ( a1 > 2 )
{
if ( a1 != 3 )
return 0xFFFFFFFFLL;
v2 = open("/dev/i2c-3", 2);
}
else if ( a1 )
{
if ( a1 != 1 )
return 0xFFFFFFFFLL;
v2 = open("/dev/i2c-1", 2);
}
else
{
v2 = open("/dev/i2c-0", 2);
}
if ( v2 >= 0 && ioctl(v2, 0x706uLL, a2) >= 0 )
return (unsigned int)v2;
return 0xFFFFFFFFLL;
}

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

if ( a1 == 2 )
{
v2 = open("/dev/i2c-2", 2);
}
else if ( a1 > 2 )
{
if ( a1 != 3 )
return 0xFFFFFFFFLL;
v2 = open("/dev/i2c-3", 2);
}
else if ( a1 )
{
if ( a1 != 1 )
return 0xFFFFFFFFLL;
v2 = open("/dev/i2c-1", 2);
}
else
{
v2 = open("/dev/i2c-0", 2);
}
if ( v2 >= 0 && ioctl(v2, 0x706uLL, a2) >= 0 )
return (unsigned int)v2;
return 0xFFFFFFFFLL;
}

original_code:
int wiringOliI2CSetup(int device, int address)
{
  int fd=-1;
  switch(device)
  {
    case 0:
      fd = open("/dev/i2c-0", 02);
      break;
    case 1:
      fd = open("/dev/i2c-1", 02);
      break;
    case 2:
      fd = open("/dev/i2c-2", 02);
      break;
    case 3:
      fd = open("/dev/i2c-3", 02);
      break;
    default:
      break;
  }
  if (fd < 0)
    return -1;
  if (ioctl(fd, 0x0706, address) < 0)
    return -1;
  return fd;
}

canonical_original_code:
int wiringOliI2CSetup(int device, int address)
{
  int fd = -1;
  switch(device)
  {
    case 0:
      fd = open("/dev/i2c-0", 02);
      break;
    case 1:
      fd = open("/dev/i2c-1", 02);
      break;
    case 2:
      fd = open("/dev/i2c-2", 02);
      break;
    case 3:
      fd = open("/dev/i2c-3", 02);
      break;
    default:
      break;
  }
  if (fd < 0)
    return -1;
  if (ioctl(fd, 0x0706, address) < 0)
    return -1;
  return fd;
}

variable_types: device int

variable_types: address int


decompiled_code:
// attributes: thunk
int I2C_Close(int fd)
{
return close(fd);
}

canonical_decompiled_code:
int func7(int fd)
{
return close(fd);
}

original_code:
void I2C_Close(int fd)
{
  if(close(fd) < 0)
  {
    return;
  }
}

return_type: void


decompiled_code:
ssize_t __fastcall I2C_Send(int a1, const void *a2, unsigned __int8 a3)
{
return write(a1, a2, a3);
}

canonical_decompiled_code:
ssize_t __fastcall func8(int a1, const void *a2, unsigned __int8 a3)
{
return write(a1, a2, a3);
}

original_code:
void I2C_Send(int fd, unsigned char *buffer, unsigned char num_bytes)
{
  int count = 0;
  count = write(fd, buffer, num_bytes);
  if(count != num_bytes)
  {
    return;
  }
}

variable_types:
buffer
unsigned char *

variable_types: count int


decompiled_code:
ssize_t __fastcall I2C_Read(int a1, void *a2, unsigned __int8 a3)
{
return read(a1, a2, a3);
}

canonical_decompiled_code:
ssize_t __fastcall func9(int a1, void *a2, unsigned __int8 a3)
{
return read(a1, a2, a3);
}

original_code:
void I2C_Read(int fd, unsigned char *buffer, unsigned char num_bytes)
{
  int count = 0;
  count = read(fd, buffer, num_bytes);
  if(count != num_bytes)
  {
    return;
  }
}


shiftIn

decompiled_code:
__int64 __fastcall shiftIn(unsigned __int8 a1, unsigned __int8 a2, char a3)
{
unsigned int v3; // er12
int v4; // ebx
unsigned int v5; // eax
__int64 result; // rax
int v7; // ebx

v3 = 0;
v4 = 0;
if ( a3 == 1 )
{
v7 = 7;
do
{
digitalWrite(a2, 1LL);
v3 |= (unsigned int)digitalRead(a1) << v7;
digitalWrite(a2, 0LL);
}
while ( v7-- != 0 );
result = v3;
}
else
{
do
{
digitalWrite(a2, 1LL);
v5 = (unsigned int)digitalRead(a1) << v4++;
v3 |= v5;
digitalWrite(a2, 0LL);
}
while ( v4 != 8 );
result = v3;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int8 a1, unsigned __int8 a2, char a3)
{
unsigned int v3; // er12
int v4; // ebx
unsigned int v5; // eax
__int64 result; // rax
int v7; // ebx

v3 = 0;
v4 = 0;
if ( a3 == 1 )
{
v7 = 7;
do
{
digitalWrite(a2, 1LL);
v3 |= (unsigned int)digitalRead(a1) << v7;
digitalWrite(a2, 0LL);
}
while ( v7-- != 0 );
result = v3;
}
else
{
do
{
digitalWrite(a2, 1LL);
v5 = (unsigned int)digitalRead(a1) << v4++;
v3 |= v5;
digitalWrite(a2, 0LL);
}
while ( v4 != 8 );
result = v3;
}
return result;
}

original_code:
uint8_t shiftIn(uint8_t dPin, uint8_t cPin, uint8_t order)
{
  uint8_t value = 0;
  int8_t i;
  if (order == 1)
    for (i = 7; i >= 0; --i)
    {
      digitalWrite(cPin, 1);
      value |= digitalRead(dPin) << i;
      digitalWrite(cPin, 0);
    }
  else
    for (i = 0; i < 8; ++i)
    {
      digitalWrite(cPin, 1);
      value |= digitalRead(dPin) << i;
      digitalWrite(cPin, 0);
    }
  return value;
}

canonical_original_code:
unsigned char shiftIn(unsigned char dPin, unsigned char cPin, unsigned char order)
{
  unsigned char value = 0;
  char i;
  if (order == 1)
    for (i = 7; i >= 0; --i)
    {
      digitalWrite(cPin, 1);
      value |= digitalRead(dPin) << i;
      digitalWrite(cPin, 0);
    }
  else
    for (i = 0; i < 8; ++i)
    {
      digitalWrite(cPin, 1);
      value |= digitalRead(dPin) << i;
      digitalWrite(cPin, 0);
    }
  return value;
}

variable_types:
dPin
unsigned char

variable_types:
cPin
unsigned char

variable_types:
order
unsigned char

variable_types:
value
unsigned char

variable_types: i char

return_type: unsigned char


shiftOut

decompiled_code:
__int64 __fastcall shiftOut(unsigned __int8 a1, unsigned __int8 a2, char a3, unsigned __int8 a4)
{
int v4; // er12
int v5; // ebx
char v6; // cl
__int64 result; // rax
int v8; // ebx

v4 = a4;
v5 = 0;
if ( a3 == 1 )
{
v8 = 7;
do
{
digitalWrite(a1, v4 & (unsigned int)(1 << v8));
digitalWrite(a2, 1LL);
result = digitalWrite(a2, 0LL);
}
while ( v8-- != 0 );
}
else
{
do
{
v6 = v5++;
digitalWrite(a1, v4 & (unsigned int)(1 << v6));
digitalWrite(a2, 1LL);
result = digitalWrite(a2, 0LL);
}
while ( v5 != 8 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int8 a1, unsigned __int8 a2, char a3, unsigned __int8 a4)
{
int v4; // er12
int v5; // ebx
char v6; // cl
__int64 result; // rax
int v8; // ebx

v4 = a4;
v5 = 0;
if ( a3 == 1 )
{
v8 = 7;
do
{
digitalWrite(a1, v4 & (unsigned int)(1 << v8));
digitalWrite(a2, 1LL);
result = digitalWrite(a2, 0LL);
}
while ( v8-- != 0 );
}
else
{
do
{
v6 = v5++;
digitalWrite(a1, v4 & (unsigned int)(1 << v6));
digitalWrite(a2, 1LL);
result = digitalWrite(a2, 0LL);
}
while ( v5 != 8 );
}
return result;
}

original_code:
void shiftOut(uint8_t dPin, uint8_t cPin, uint8_t order, uint8_t val)
{
  int8_t i;
  if (order == 1)
    for (i = 7; i >= 0; --i)
    {
      digitalWrite(dPin, val & (1 << i));
      digitalWrite(cPin, 1);
      digitalWrite(cPin, 0);
    }
  else
    for (i = 0; i < 8; ++i)
    {
      digitalWrite(dPin, val & (1 << i));
      digitalWrite(cPin, 1);
      digitalWrite(cPin, 0);
    }
}

canonical_original_code:
void shiftOut(unsigned char dPin, unsigned char cPin, unsigned char order, unsigned char val)
{
  char i;
  if (order == 1)
    for (i = 7; i >= 0; --i)
    {
      digitalWrite(dPin, val & (1 << i));
      digitalWrite(cPin, 1);
      digitalWrite(cPin, 0);
    }
  else
    for (i = 0; i < 8; ++i)
    {
      digitalWrite(dPin, val & (1 << i));
      digitalWrite(cPin, 1);
      digitalWrite(cPin, 0);
    }
}

variable_types:
val
unsigned char


oliThreadCreate

decompiled_code:
int __fastcall oliThreadCreate(void *(*start_routine)(void *))
{
pthread_t newthread[2]; // [rsp+8h] [rbp-10h] BYREF

return pthread_create(newthread, 0LL, start_routine, 0LL);
}

canonical_decompiled_code:
int __fastcall func0(void *(*start_routine)(void *))
{
pthread_t newthread[2]; // [rsp+8h] [rbp-10h] BYREF

return pthread_create(newthread, 0LL, start_routine, 0LL);
}

original_code:
int oliThreadCreate(void *(*fn)(void *))
{
  pthread_t myThread;
  return pthread_create(&myThread, ((void *)0), fn, ((void *)0));
}

canonical_original_code:
int oliThreadCreate(void *(*fn)(void *))
{
  unsigned long myThread;
  return pthread_create(&myThread, ((void *)0), fn, ((void *)0));
}

variable_types:
fn
void *(*)(void *)

variable_types:
myThread
unsigned long


oliLock

decompiled_code:
int __fastcall oliLock(int a1)
{
return pthread_mutex_lock(&oliMutexes[a1]);
}

canonical_decompiled_code:
int __fastcall func1(int a1)
{
return pthread_mutex_lock(&oliMutexes[a1]);
}

original_code:
void oliLock(int key)
{
  pthread_mutex_lock(&oliMutexes[key]);
}

variable_types: key int


oliUnlock

decompiled_code:
int __fastcall oliUnlock(int a1)
{
return pthread_mutex_unlock(&oliMutexes[a1]);
}

canonical_decompiled_code:
int __fastcall func2(int a1)
{
return pthread_mutex_unlock(&oliMutexes[a1]);
}

original_code:
void oliUnlock(int key)
{
  pthread_mutex_unlock(&oliMutexes[key]);
}


checkGraph

original_code:
void checkGraph(gnode_t* graph){
 fprintf(stdout, "\n**********checkGraph**********\n");
 gnode_t *ptr=graph;
 while(ptr !=((void *)0)){
  fprintf(stdout, "%s --> ",ptr->name);
  gnode_t *ptr2=ptr->neighbors;
  while (ptr2 != ((void *)0)){
   fprintf(stdout, "- %s -",ptr2->name);
   ptr2=ptr2->next;
  }
  fprintf(stdout, " \n");
  ptr = ptr->next;
 }
 fprintf(stdout, "*********************************\n\n");
}

canonical_original_code:
void checkGraph(struct gnode *graph){
 fprintf(stdout, "\n**********checkGraph**********\n");
 struct gnode *ptr = graph;
 while(ptr !=((void *)0)){
  fprintf(stdout, "%s --> ",ptr->name);
  struct gnode *ptr2 = ptr->neighbors;
  while (ptr2 != ((void *)0)){
   fprintf(stdout, "- %s -",ptr2->name);
   ptr2=ptr2->next;
  }
  fprintf(stdout, " \n");
  ptr = ptr->next;
 }
 fprintf(stdout, "*********************************\n\n");
}

variable_types: struct gnode { int seqNum; char name[1024]; struct gnode *neighbors; struct gnode *next; } *


checkQ

decompiled_code:
size_t __fastcall checkQ(unsigned int *a1)
{
const void **i; // rbx

fprintf(stdout, "\n**********checkQ : size = %d head:%p**********\n", *a1, *((const void **)a1 + 1));
for ( i = (const void **)*((_QWORD *)a1 + 1); i; i = (const void **)i[3] )
fprintf(
stdout,
"%s -- %d -- %p -- %p\n",
(const char *)*i,
*((unsigned int *)*i + 256),
*i,
*((const void **)*i + 129));
return fwrite("*********************************\n\n", 1uLL, 0x23uLL, stdout);
}

canonical_decompiled_code:
size_t __fastcall func1(unsigned int *a1)
{
const void **i; // rbx

fprintf(stdout, "\n**********checkQ : size = %d head:%p**********\n", *a1, *((const void **)a1 + 1));
for ( i = (const void **)*((_QWORD *)a1 + 1); i; i = (const void **)i[3] )
fprintf(
stdout,
"%s -- %d -- %p -- %p\n",
(const char *)*i,
*((unsigned int *)*i + 256),
*i,
*((const void **)*i + 129));
return fwrite("*********************************\n\n", 1uLL, 0x23uLL, stdout);
}

original_code:
void checkQ(queue_t *Q){
 fprintf(stdout, "\n**********checkQ : size = %d head:%p**********\n",Q->size,Q->head);
 node_t* ptr = Q->head;
 while(ptr!=((void *)0)){
  d_node_t * data = (d_node_t *)ptr->data;
  fprintf(stdout, "%s  --  %d -- %p -- %p\n",data->name,data->weight,data,data->next);
  ptr=ptr->next;
 }
 fprintf(stdout, "*********************************\n\n");
}

canonical_original_code:
void checkQ(struct queue *Q){
 fprintf(stdout, "\n**********checkQ : size = %d head:%p**********\n",Q->size,Q->head);
 struct node *ptr = Q->head;
 while(ptr!=((void *)0)){
  struct d_node_s *data = (d_node_t *)ptr->data;
  fprintf(stdout, "%s  --  %d -- %p -- %p\n",data->name,data->weight,data,data->next);
  ptr=ptr->next;
 }
 fprintf(stdout, "*********************************\n\n");
}

variable_types: struct queue { int size; struct node { void *data; int weight; struct node *prev; struct node *next; } *head; struct node *tail; } *

variable_types: struct d_node_s { char name[1024]; int weight; struct d_node_s *next; } *

user_defined_types: struct queue { int size; struct node *head; struct node *tail; }


checkL

original_code:
void checkL(d_node_t *L){
 fprintf(stdout, "\n**********checkL**********\n");
 d_node_t* ptr = L;
 while(ptr!=((void *)0)){
  fprintf(stdout, "%s  --  %d -- %p -- %p\n",ptr->name,ptr->weight,ptr,ptr->next);
  ptr=ptr->next;
 }
 fprintf(stdout, "*********************************\n\n");
}

canonical_original_code:
void checkL(struct d_node_s *L){
 fprintf(stdout, "\n**********checkL**********\n");
 struct d_node_s *ptr = L;
 while(ptr!=((void *)0)){
  fprintf(stdout, "%s  --  %d -- %p -- %p\n",ptr->name,ptr->weight,ptr,ptr->next);
  ptr=ptr->next;
 }
 fprintf(stdout, "*********************************\n\n");
}

user_defined_types: struct d_node_s { char name[1024]; int weight; struct d_node_s *next; }


containNode

decompiled_code:
__int64 __fastcall containNode(char *s2, char *s1)
{
char *v2; // rbx
unsigned int v3; // er12

v2 = s1;
if ( verbal <= 2 )
{
if ( s1 )
goto LABEL_3;
return 0LL;
}
fprintf(stdout, "-----in containNode : %s-----\n", s2);
if ( !s1 )
return 0LL;
do
{
LABEL_3:
if ( !strcmp(v2, s2) )
{
v3 = 1;
if ( verbal <= 2 )
return v3;
fwrite("-----in containNode yes-----\n", 1uLL, 0x1DuLL, stdout);
return 1LL;
}
v2 = (char *)*((_QWORD *)v2 + 129);
}
while ( v2 );
v3 = 0;
if ( verbal <= 2 )
return v3;
fwrite("-----in containNode no-----\n", 1uLL, 0x1CuLL, stdout);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *s2, char *s1)
{
char *v2; // rbx
unsigned int v3; // er12

v2 = s1;
if ( verbal <= 2 )
{
if ( s1 )
goto LABEL_3;
return 0LL;
}
fprintf(stdout, "-----in containNode : %s-----\n", s2);
if ( !s1 )
return 0LL;
do
{
LABEL_3:
if ( !strcmp(v2, s2) )
{
v3 = 1;
if ( verbal <= 2 )
return v3;
fwrite("-----in containNode yes-----\n", 1uLL, 0x1DuLL, stdout);
return 1LL;
}
v2 = (char *)*((_QWORD *)v2 + 129);
}
while ( v2 );
v3 = 0;
if ( verbal <= 2 )
return v3;
fwrite("-----in containNode no-----\n", 1uLL, 0x1CuLL, stdout);
return 0LL;
}

original_code:
int containNode(char* name, d_node_t *list){
 if(verbal>2)
  fprintf(stdout, "-----in containNode : %s-----\n",name);
 if(list==((void *)0)) return 0;
 d_node_t *ptr=list;
 while(ptr!=((void *)0)){
  if(!strcmp(ptr->name,name)){
   if(verbal>2)
    fprintf(stdout, "-----in containNode yes-----\n");
   return 1;
  }
  ptr=ptr->next;
 }
 if(verbal>2)
  fprintf(stdout, "-----in containNode no-----\n");
 return 0;
}

canonical_original_code:
int containNode(char *name, struct d_node_s *list){
 if(verbal>2)
  fprintf(stdout, "-----in containNode : %s-----\n",name);
 if(list==((void *)0)) return 0;
 struct d_node_s *ptr = list;
 while(ptr!=((void *)0)){
  if(!strcmp(ptr->name,name)){
   if(verbal>2)
    fprintf(stdout, "-----in containNode yes-----\n");
   return 1;
  }
  ptr=ptr->next;
 }
 if(verbal>2)
  fprintf(stdout, "-----in containNode no-----\n");
 return 0;
}

variable_types:
name
char *


findGraphNode

decompiled_code:
__int64 __fastcall findGraphNode(char *s1, __int64 a2)
{
if ( verbal > 2 )
fwrite("-----in findGraphNode-----\n", 1uLL, 0x1BuLL, stdout);
while ( 1 )
{
if ( !a2 )
return 0LL;
if ( !strcmp(s1, (const char *)(a2 + 4)) )
break;
a2 = *(_QWORD *)(a2 + 1040);
}
return a2;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *s1, __int64 a2)
{
if ( verbal > 2 )
fwrite("-----in findGraphNode-----\n", 1uLL, 0x1BuLL, stdout);
while ( 1 )
{
if ( !a2 )
return 0LL;
if ( !strcmp(s1, (const char *)(a2 + 4)) )
break;
a2 = *(_QWORD *)(a2 + 1040);
}
return a2;
}

original_code:
gnode_t *findGraphNode(char *name,gnode_t* graph){
 if(verbal>2)
  fprintf(stdout, "-----in findGraphNode-----\n");
 gnode_t *ptr=graph;
 while(ptr!=((void *)0)){
  if(!strcmp(name,ptr->name)){
   return ptr;
  }
  ptr=ptr->next;
 }
 return ((void *)0);
}

canonical_original_code:
struct gnode *findGraphNode(char *name, struct gnode *graph){
 if(verbal>2)
  fprintf(stdout, "-----in findGraphNode-----\n");
 struct gnode *ptr = graph;
 while(ptr!=((void *)0)){
  if(!strcmp(name,ptr->name)){
   return ptr;
  }
  ptr=ptr->next;
 }
 return ((void *)0);
}

variable_types:
graph
struct gnode *

return_type: struct gnode { int seqNum; char name[1024]; struct gnode *neighbors; struct gnode *next; } *


isServer

decompiled_code:
__int64 __fastcall isServer(char *s1, __int64 a2)
{
__int64 v2; // rbx

if ( verbal > 2 )
fprintf(stdout, "-----in isServer: %s-----\n", s1);
v2 = *(_QWORD *)(a2 + 8);
if ( !v2 )
return 0LL;
while ( strcmp(s1, (const char *)v2) )
{
v2 = *(_QWORD *)(v2 + 24);
if ( !v2 )
return 0LL;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func5(char *s1, __int64 a2)
{
__int64 v2; // rbx

if ( verbal > 2 )
fprintf(stdout, "-----in isServer: %s-----\n", s1);
v2 = *(_QWORD *)(a2 + 8);
if ( !v2 )
return 0LL;
while ( strcmp(s1, (const char *)v2) )
{
v2 = *(_QWORD *)(v2 + 24);
if ( !v2 )
return 0LL;
}
return v2;
}

original_code:
ip_t *isServer(char *name,robin_list_t* list){
 if(verbal>2)
  fprintf(stdout, "-----in isServer: %s-----\n",name);
 robin_node_t *ptr;
 for(ptr=list->head;ptr!=((void *)0);ptr=ptr->next){
  if(!strcmp(name,ptr->ip.ip_str))
   return &ptr->ip;
 }
 return ((void *)0);
}

canonical_original_code:
struct ip_t *isServer(char *name, struct robin_list_t *list){
 if(verbal>2)
  fprintf(stdout, "-----in isServer: %s-----\n",name);
 struct robin_node_s *ptr;
 for(ptr=list->head;ptr!=((void *)0);ptr=ptr->next){
  if(!strcmp(name,ptr->ip.ip_str))
   return &ptr->ip;
 }
 return ((void *)0);
}

variable_types: struct robin_list_t { int size; struct robin_node_s { struct ip_t ip; struct robin_node_s *next; } *head; struct robin_node_s *tail; struct robin_node_s *ptr; } *

variable_types: struct robin_node_s { struct ip_t { char ip_str[20]; unsigned char ip[4]; } ip; struct robin_node_s *next; } *

return_type: struct ip_t { char ip_str[20]; unsigned char ip[4]; } *

user_defined_types: struct robin_list_t { int size; struct robin_node_s *head; struct robin_node_s *tail; struct robin_node_s *ptr; }


addToPQ

decompiled_code:
void __fastcall addToPQ(__int64 a1, int a2, __int64 a3)
{
__int64 v4; // rbx
unsigned int v5; // er12
char *v6; // rax
char *v7; // rbp

v4 = a1;
if ( verbal > 2 )
fwrite("-----in addToPQ-----\n", 1uLL, 0x15uLL, stdout);
v5 = a2 + 1;
if ( a1 )
{
do
{
v6 = (char *)malloc(0x410uLL);
v7 = v6;
if ( verbal > 2 )
fprintf(stdout, "----->new node: %p-----\n", v6);
strcpy(v7, (const char *)(v4 + 4));
*((_DWORD *)v7 + 256) = v5;
*((_QWORD *)v7 + 129) = 0LL;
enqueuePQ(a3, v7, v5);
v4 = *(_QWORD *)(v4 + 1040);
}
while ( v4 );
}
}

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

v4 = a1;
if ( verbal > 2 )
fwrite("-----in addToPQ-----\n", 1uLL, 0x15uLL, stdout);
v5 = a2 + 1;
if ( a1 )
{
do
{
v6 = (char *)malloc(0x410uLL);
v7 = v6;
if ( verbal > 2 )
fprintf(stdout, "----->new node: %p-----\n", v6);
strcpy(v7, (const char *)(v4 + 4));
*((_DWORD *)v7 + 256) = v5;
*((_QWORD *)v7 + 129) = 0LL;
enqueuePQ(a3, v7, v5);
v4 = *(_QWORD *)(v4 + 1040);
}
while ( v4 );
}
}

original_code:
void addToPQ(gnode_t *neighbors,int weight,queue_t *PQ){
 if(verbal>2)
  fprintf(stdout, "-----in addToPQ-----\n");
 gnode_t *ptr=neighbors;
 while(ptr!=((void *)0)){
  d_node_t* new = (d_node_t *)malloc(sizeof(d_node_t));
  if(verbal>2)
   fprintf(stdout, "----->new node: %p-----\n",new);
  strcpy(new->name,ptr->name);
  new->weight = weight+1;
  new->next = ((void *)0);
  enqueuePQ(PQ,new,new->weight);
  ptr = ptr->next;
 }
}

canonical_original_code:
void addToPQ(struct gnode *neighbors, int weight, struct queue *PQ){
 if(verbal>2)
  fprintf(stdout, "-----in addToPQ-----\n");
 struct gnode *ptr = neighbors;
 while(ptr!=((void *)0)){
  struct d_node_s *new = (d_node_t *)malloc(sizeof(d_node_t));
  if(verbal>2)
   fprintf(stdout, "----->new node: %p-----\n",new);
  strcpy(new->name,ptr->name);
  new->weight = weight+1;
  new->next = ((void *)0);
  enqueuePQ(PQ,new,new->weight);
  ptr = ptr->next;
 }
}

variable_types: weight int

user_defined_types: struct gnode { int seqNum; char name[1024]; struct gnode *neighbors; struct gnode *next; }

user_defined_types: struct node { void *data; int weight; struct node *prev; struct node *next; }


freeALL

decompiled_code:
void __fastcall freeALL(void *ptr, void *a2)
{
void *v2; // rbx
void *v3; // rax
void *v4; // rdi

v2 = a2;
if ( verbal > 2 )
{
fwrite("-----in freeALL-----\n", 1uLL, 0x15uLL, stdout);
if ( verbal > 2 )
checkQ((unsigned int *)ptr);
}
while ( *(_DWORD *)ptr )
{
v3 = (void *)dequeue(ptr);
free(v3);
}
free(ptr);
if ( verbal <= 2 || (fwrite("-----in freeALL -2 -----\n", 1uLL, 0x19uLL, stdout), verbal <= 2) )
{
if ( !a2 )
return;
goto LABEL_7;
}
checkL((__int64)a2);
if ( a2 )
{
do
{
LABEL_7:
v4 = v2;
v2 = (void *)*((_QWORD *)v2 + 129);
free(v4);
}
while ( v2 );
}
if ( verbal > 2 )
fwrite("-----freeALL done-----\n", 1uLL, 0x17uLL, stdout);
}

canonical_decompiled_code:
void __fastcall func7(void *ptr, void *a2)
{
void *v2; // rbx
void *v3; // rax
void *v4; // rdi

v2 = a2;
if ( verbal > 2 )
{
fwrite("-----in freeALL-----\n", 1uLL, 0x15uLL, stdout);
if ( verbal > 2 )
func1((unsigned int *)ptr);
}
while ( *(_DWORD *)ptr )
{
v3 = (void *)dequeue(ptr);
free(v3);
}
free(ptr);
if ( verbal <= 2 || (fwrite("-----in freeALL -2 -----\n", 1uLL, 0x19uLL, stdout), verbal <= 2) )
{
if ( !a2 )
return;
goto LABEL_7;
}
func2((__int64)a2);
if ( a2 )
{
do
{
LABEL_7:
v4 = v2;
v2 = (void *)*((_QWORD *)v2 + 129);
free(v4);
}
while ( v2 );
}
if ( verbal > 2 )
fwrite("-----freeALL done-----\n", 1uLL, 0x17uLL, stdout);
}

original_code:
void freeALL(queue_t *PQ,d_node_t *list){
 if(verbal>2)
  fprintf(stdout, "-----in freeALL-----\n");
 if(verbal>2) checkQ(PQ);
 while(PQ->size != 0){
  d_node_t *n = dequeue(PQ);
  free(n);
 }
 free(PQ);
 if(verbal>2)
     fprintf(stdout, "-----in freeALL -2 -----\n");
 if(verbal>2) checkL(list);
 d_node_t *ptr = list;
 while(ptr!=((void *)0)){
  d_node_t *tmp = ptr;
  ptr=ptr->next;
  free(tmp);
 }
 if(verbal>2)
  fprintf(stdout, "-----freeALL done-----\n");
}

canonical_original_code:
void freeALL(struct queue *PQ, struct d_node_s *list){
 if(verbal>2)
  fprintf(stdout, "-----in freeALL-----\n");
 if(verbal>2) checkQ(PQ);
 while(PQ->size != 0){
  struct d_node_s *n = dequeue(PQ);
  free(n);
 }
 free(PQ);
 if(verbal>2)
     fprintf(stdout, "-----in freeALL -2 -----\n");
 if(verbal>2) checkL(list);
 struct d_node_s *ptr = list;
 while(ptr!=((void *)0)){
  struct d_node_s *tmp = ptr;
  ptr=ptr->next;
  free(tmp);
 }
 if(verbal>2)
  fprintf(stdout, "-----freeALL done-----\n");
}


decompiled_code:
void *__fastcall getIP_LSAs(__int64 a1, const char *a2, _DWORD *a3)
{
int v3; // eax
void *v5; // rbp
unsigned int *v6; // r12
char *v7; // r14
__int64 v8; // rax
__int64 v9; // rax

v3 = verbal;
if ( verbal > 2 )
{
checkGraph(a1);
v3 = verbal;
}
if ( v3 > 1 )
fwrite("-----in getIP_LSAs-----\n", 1uLL, 0x18uLL, stdout);
v5 = 0LL;
if ( !a1 || !*a3 )
return v5;
v6 = (unsigned int *)new_queue();
v7 = 0LL;
v5 = malloc(0x410uLL);
strcpy((char *)v5, a2);
*((_DWORD *)v5 + 256) = 0;
*((_QWORD *)v5 + 129) = 0LL;
while ( 1 )
{
while ( verbal <= 2 )
{
if ( !(unsigned int)containNode((char *)v5, v7) )
goto LABEL_9;
LABEL_15:
free(v5);
v5 = (void *)dequeue(v6);
if ( !v5 )
goto LABEL_16;
}
checkQ(v6);
if ( (unsigned int)containNode((char *)v5, v7) )
goto LABEL_15;
LABEL_9:
*((_QWORD *)v5 + 129) = v7;
v8 = isServer((char *)v5, (__int64)a3);
if ( v8 )
break;
v9 = findGraphNode((char *)v5, a1);
if ( v9 )
addToPQ(*(_QWORD *)(v9 + 1032), *((_DWORD *)v5 + 256), (__int64)v6);
v7 = (char *)v5;
v5 = (void *)dequeue(v6);
if ( !v5 )
goto LABEL_16;
}
v7 = (char *)v5;
v5 = (void *)v8;
LABEL_16:
freeALL(v6, v7);
if ( verbal <= 2 )
return v5;
fprintf(stdout, "-----getIP_LSAs done %p-----\n", v5);
return v5;
}

canonical_decompiled_code:
void *__fastcall func8(__int64 a1, const char *a2, _DWORD *a3)
{
int v3; // eax
void *v5; // rbp
unsigned int *v6; // r12
char *v7; // r14
__int64 v8; // rax
__int64 v9; // rax

v3 = verbal;
if ( verbal > 2 )
{
func0(a1);
v3 = verbal;
}
if ( v3 > 1 )
fwrite("-----in getIP_LSAs-----\n", 1uLL, 0x18uLL, stdout);
v5 = 0LL;
if ( !a1 || !*a3 )
return v5;
v6 = (unsigned int *)new_queue();
v7 = 0LL;
v5 = malloc(0x410uLL);
strcpy((char *)v5, a2);
*((_DWORD *)v5 + 256) = 0;
*((_QWORD *)v5 + 129) = 0LL;
while ( 1 )
{
while ( verbal <= 2 )
{
if ( !(unsigned int)func3((char *)v5, v7) )
goto LABEL_9;
LABEL_15:
free(v5);
v5 = (void *)dequeue(v6);
if ( !v5 )
goto LABEL_16;
}
func1(v6);
if ( (unsigned int)func3((char *)v5, v7) )
goto LABEL_15;
LABEL_9:
*((_QWORD *)v5 + 129) = v7;
v8 = func5((char *)v5, (__int64)a3);
if ( v8 )
break;
v9 = func4((char *)v5, a1);
if ( v9 )
func6(*(_QWORD *)(v9 + 1032), *((_DWORD *)v5 + 256), (__int64)v6);
v7 = (char *)v5;
v5 = (void *)dequeue(v6);
if ( !v5 )
goto LABEL_16;
}
v7 = (char *)v5;
v5 = (void *)v8;
LABEL_16:
func7(v6, v7);
if ( verbal <= 2 )
return v5;
fprintf(stdout, "-----getIP_LSAs done %p-----\n", v5);
return v5;
}

original_code:
ip_t *getIP_LSAs(gnode_t* graph, char* src, robin_list_t* servers){
 if(verbal>2) checkGraph(graph);
 if(verbal>1)
  fprintf(stdout, "-----in getIP_LSAs-----\n");
 if(graph==((void *)0)||(servers->size == 0)) return ((void *)0);
 ip_t *ret=((void *)0);
 d_node_t *visit=((void *)0);
 queue_t *PQ = new_queue();
 d_node_t* curr = (d_node_t *)malloc(sizeof(d_node_t));
 strcpy(curr->name,src);
 curr->weight = 0;
 curr->next = ((void *)0);
 do{
  if(verbal>2)
   checkQ(PQ);
  if(containNode(curr->name,visit)) {
   free(curr);
   curr = ((void *)0);
   curr=(d_node_t *)dequeue(PQ);
   continue;
  }
  curr->next = visit;
  visit = curr;
  if((ret = isServer(curr->name,servers))!=((void *)0)) {
   break;
  }
  gnode_t *tmp = findGraphNode(curr->name,graph);
  if(tmp == ((void *)0)) {
   curr=(d_node_t *)dequeue(PQ);
   continue;
  }
  addToPQ(tmp->neighbors,curr->weight,PQ);
  curr=(d_node_t *)dequeue(PQ);
 }while(curr!=((void *)0));
 freeALL(PQ,visit);
 if(verbal>2)
  fprintf(stdout, "-----getIP_LSAs done %p-----\n",ret);
 return ret;
}

canonical_original_code:
struct ip_t *getIP_LSAs(struct gnode *graph, char *src, struct robin_list_t *servers){
 if(verbal>2) checkGraph(graph);
 if(verbal>1)
  fprintf(stdout, "-----in getIP_LSAs-----\n");
 if(graph==((void *)0)||(servers->size == 0)) return ((void *)0);
 struct ip_t *ret = ((void *)0);
 struct d_node_s *visit = ((void *)0);
 struct queue *PQ = new_queue();
 struct d_node_s *curr = (d_node_t *)malloc(sizeof(d_node_t));
 strcpy(curr->name,src);
 curr->weight = 0;
 curr->next = ((void *)0);
 do{
  if(verbal>2)
   checkQ(PQ);
  if(containNode(curr->name,visit)) {
   free(curr);
   curr = ((void *)0);
   curr=(struct d_node_s *)dequeue(PQ);
   continue;
  }
  curr->next = visit;
  visit = curr;
  if((ret = isServer(curr->name,servers))!=((void *)0)) {
   break;
  }
  struct gnode *tmp = findGraphNode(curr->name,graph);
  if(tmp == ((void *)0)) {
   curr=(struct d_node_s *)dequeue(PQ);
   continue;
  }
  addToPQ(tmp->neighbors,curr->weight,PQ);
  curr=(struct d_node_s *)dequeue(PQ);
 }while(curr!=((void *)0));
 freeALL(PQ,visit);
 if(verbal>2)
  fprintf(stdout, "-----getIP_LSAs done %p-----\n",ret);
 return ret;
}

variable_types:
src
char *

variable_types: struct ip_t { char ip_str[20]; unsigned char ip[4]; } *

user_defined_types: struct robin_node_s { struct ip_t ip; struct robin_node_s *next; }


buildRequestHeader

decompiled_code:
__int64 __fastcall buildRequestHeader(__int64 a1, char *a2)
{
__int64 v2; // rax
char src[16]; // [rsp+0h] [rbp-2018h] BYREF
char v5[8176]; // [rsp+10h] [rbp-2008h] BYREF

*(_OWORD *)src = 0LL;
memset(v5, 0, sizeof(v5));
while ( 1 )
{
v2 = readLine(*(_QWORD *)(a1 + 3192), src, 0x2000LL);
if ( !v2 )
{
LABEL_13:
strcat(a2, "\r\n");
return 0LL;
}
if ( v2 < 0 )
return 0xFFFFFFFFLL;
if ( src[0] == 13 && src[1] == 10 && !src[2] )
goto LABEL_13;
if ( !strncasecmp(src, "Connection: close", 0x11uLL) )
{
*(_DWORD *)(a1 + 3140) = 1;
strcat(a2, src);
}
else
{
if ( !strncasecmp(src, "Host: ", 6uLL) )
strcpy((char *)(a1 + 36), &src[6]);
strcat(a2, src);
}
}
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, char *a2)
{
__int64 v2; // rax
char src[16]; // [rsp+0h] [rbp-2018h] BYREF
char v5[8176]; // [rsp+10h] [rbp-2008h] BYREF

*(_OWORD *)src = 0LL;
memset(v5, 0, sizeof(v5));
while ( 1 )
{
v2 = readLine(*(_QWORD *)(a1 + 3192), src, 0x2000LL);
if ( !v2 )
{
LABEL_13:
strcat(a2, "\r\n");
return 0LL;
}
if ( v2 < 0 )
return 0xFFFFFFFFLL;
if ( src[0] == 13 && src[1] == 10 && !src[2] )
goto LABEL_13;
if ( !strncasecmp(src, "Connection: close", 0x11uLL) )
{
*(_DWORD *)(a1 + 3140) = 1;
strcat(a2, src);
}
else
{
if ( !strncasecmp(src, "Host: ", 6uLL) )
strcpy((char *)(a1 + 36), &src[6]);
strcat(a2, src);
}
}
}

original_code:
int buildRequestHeader(socket_t *pair, char* header){
 char tmpbuf[8192]={0};
 ssize_t n;
 while((n=readLine(pair->buf_client,tmpbuf,8192))!=0){
  if(n<0) return -1;
  if(!strcmp(tmpbuf,"\r\n"))
   break;
  else if(!strncasecmp(tmpbuf,"Connection: close",strlen("Connection: close"))){
   pair->close=1;
   strcat(header,tmpbuf);
  }else if(!strncasecmp(tmpbuf,"Host: ",strlen("Host: "))){
   strcpy(pair->host,tmpbuf+strlen("Host: "));
   strcat(header,tmpbuf);
  }else
   strcat(header,tmpbuf);
 }
 strcat(header,"\r\n");
 return 0;
}

canonical_original_code:
int buildRequestHeader(struct socket_t *pair, char *header){
 char tmpbuf[8192] = {0};
 long n;
 while((n=readLine(pair->buf_client,tmpbuf,8192))!=0){
  if(n<0) return -1;
  if(!strcmp(tmpbuf,"\r\n"))
   break;
  else if(!strncasecmp(tmpbuf,"Connection: close",strlen("Connection: close"))){
   pair->close=1;
   strcat(header,tmpbuf);
  }else if(!strncasecmp(tmpbuf,"Host: ",strlen("Host: "))){
   strcpy(pair->host,tmpbuf+strlen("Host: "));
   strcat(header,tmpbuf);
  }else
   strcat(header,tmpbuf);
 }
 strcat(header,"\r\n");
 return 0;
}

variable_types: struct socket_t { int client_fd; int server_fd; char *remote_addr; char server_addr[20]; char host[1024]; int host_port; int method; char version[1024]; int request_type; char path[1024]; char *urn; int request_rate; int seg_num; int frag_num; int close; int recv; int left; int contentlen; char *content_buf; enum server_t { HEADER=0, CONTENT=1 } server_stat; struct queue { int size; struct node { void *data; struct node *prev; struct node *next; } *head; struct node *tail; } *requestQueue; struct buffer { char *ptr; char *buf; unsigned long count; unsigned long length; unsigned long size; } *buf_server; struct buffer *buf_client; struct buffer *buf_send_client; struct buffer *buf_send_server; } *

variable_types:
header
char *

variable_types:
tmpbuf
char [8192]

variable_types: n long

user_defined_types: struct socket_t { int client_fd; int server_fd; char *remote_addr; char server_addr[20]; char host[1024]; int host_port; int method; char version[1024]; int request_type; char path[1024]; char *urn; int request_rate; int seg_num; int frag_num; int close; int recv; int left; int contentlen; char *content_buf; enum server_t server_stat; struct queue *requestQueue; struct buffer *buf_server; struct buffer *buf_client; struct buffer *buf_send_client; struct buffer *buf_send_server; }


decompiled_code:
__int64 __fastcall open_serverfd(__int64 a1)
{
const char *v2; // r12
const char *v3; // rdi
int v4; // eax
int v5; // er12
char *v6; // rax
__int64 result; // rax
char service[5]; // [rsp+3h] [rbp-65h] BYREF
struct addrinfo *pai; // [rsp+8h] [rbp-60h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-58h] BYREF
struct addrinfo req; // [rsp+20h] [rbp-48h] BYREF

v2 = *(const char **)(proxy_stat + 24LL);
if ( verbal )
fprintf(stdout, "fake:%s\nwww:%s\n", *(const char **)(proxy_stat + 24LL), *(const char **)(proxy_stat + 40LL));
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = inet_addr(v2);
*(_WORD *)addr.sa_data = _bswap_16_constprop_0();
v3 = *(const char **)(proxy_stat + 40LL);
if ( v3 )
{
strcpy(service, "8080");
*(_OWORD *)&req.ai_flags = 0LL;
req.ai_socktype = 1;
req.ai_flags = 1;
*(_OWORD *)&req.ai_addrlen = 0LL;
*(_OWORD *)&req.ai_canonname = 0LL;
if ( getaddrinfo(v3, service, &req, &pai) )
{
fwrite("getaddrinfo error\n", 1uLL, 0x12uLL, stderr);
return 0xFFFFFFFFLL;
}
}
else if ( (unsigned int)resolve("video.cs.cmu.edu", "8080", 0LL, &pai) )
{
fwrite("getaddrinfo error in proxy_process\n", 1uLL, 0x23uLL, stderr);
exit(-1);
}
v4 = socket(pai->ai_family, pai->ai_socktype, pai->ai_protocol);
v5 = v4;
if ( v4 == -1 )
{
fwrite("Socket failed\n", 1uLL, 0xEuLL, stderr);
result = 0xFFFFFFFFLL;
}
else if ( bind(v4, &addr, 0x10u) )
{
fwrite("Failed binding socket\n", 1uLL, 0x16uLL, stderr);
result = 0xFFFFFFFFLL;
}
else if ( connect(v5, pai->ai_addr, pai->ai_addrlen) )
{
fwrite("Connect failed\n", 1uLL, 0xFuLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
v6 = inet_ntoa((struct in_addr)HIDWORD(*(_QWORD *)pai->ai_addr));
strcpy((char *)(a1 + 16), v6);
freeaddrinfo(pai);
*(_DWORD *)(a1 + 4) = v5;
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
const char *v2; // r12
const char *v3; // rdi
int v4; // eax
int v5; // er12
char *v6; // rax
__int64 result; // rax
char service[5]; // [rsp+3h] [rbp-65h] BYREF
struct addrinfo *pai; // [rsp+8h] [rbp-60h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-58h] BYREF
struct addrinfo req; // [rsp+20h] [rbp-48h] BYREF

v2 = *(const char **)(proxy_stat + 24LL);
if ( verbal )
fprintf(stdout, "fake:%s\nwww:%s\n", *(const char **)(proxy_stat + 24LL), *(const char **)(proxy_stat + 40LL));
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = inet_addr(v2);
*(_WORD *)addr.sa_data = func0();
v3 = *(const char **)(proxy_stat + 40LL);
if ( v3 )
{
strcpy(service, "8080");
*(_OWORD *)&req.ai_flags = 0LL;
req.ai_socktype = 1;
req.ai_flags = 1;
*(_OWORD *)&req.ai_addrlen = 0LL;
*(_OWORD *)&req.ai_canonname = 0LL;
if ( getaddrinfo(v3, service, &req, &pai) )
{
fwrite("getaddrinfo error\n", 1uLL, 0x12uLL, stderr);
return 0xFFFFFFFFLL;
}
}
else if ( (unsigned int)resolve("video.cs.cmu.edu", "8080", 0LL, &pai) )
{
fwrite("getaddrinfo error in proxy_process\n", 1uLL, 0x23uLL, stderr);
exit(-1);
}
v4 = socket(pai->ai_family, pai->ai_socktype, pai->ai_protocol);
v5 = v4;
if ( v4 == -1 )
{
fwrite("Socket failed\n", 1uLL, 0xEuLL, stderr);
result = 0xFFFFFFFFLL;
}
else if ( bind(v4, &addr, 0x10u) )
{
fwrite("Failed binding socket\n", 1uLL, 0x16uLL, stderr);
result = 0xFFFFFFFFLL;
}
else if ( connect(v5, pai->ai_addr, pai->ai_addrlen) )
{
fwrite("Connect failed\n", 1uLL, 0xFuLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
v6 = inet_ntoa((struct in_addr)HIDWORD(*(_QWORD *)pai->ai_addr));
strcpy((char *)(a1 + 16), v6);
freeaddrinfo(pai);
*(_DWORD *)(a1 + 4) = v5;
result = 1LL;
}
return result;
}

original_code:
int open_serverfd(socket_t *pair)
{
  char *fake_ip = proxy_stat->fakeIP;
  unsigned short rand_port = 0;
 if(verbal)fprintf(stdout, "fake:%s\nwww:%s\n",proxy_stat->fakeIP, proxy_stat->wwwIP);
 int sock;
  struct addrinfo *servinfo;
  struct sockaddr_in addr;
  addr.sin_family=2;
  addr.sin_addr.s_addr = inet_addr(fake_ip);
  addr.sin_port=htons(rand_port);
 if(proxy_stat->wwwIP == ((void *)0)){
  if(resolve("video.cs.cmu.edu", "8080", ((void *)0), &servinfo) != 0){
   fprintf(stderr, "getaddrinfo error in proxy_process\n");
   exit(-1);
  }
 }
 else{
   char *server_ip = proxy_stat->wwwIP;
   char server_port[] = "8080";
   struct addrinfo hints;
   memset(&hints, 0, sizeof(struct addrinfo));
   hints.ai_family = 0;
   hints.ai_socktype = SOCK_STREAM;
   hints.ai_flags = 0x0001;
   if(getaddrinfo(server_ip, server_port, &hints, &servinfo) != 0){
   fprintf(stderr, "getaddrinfo error\n");
   return -1;
  }
 }
 if((sock = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol)) == -1){
  fprintf(stderr, "Socket failed\n");
  return -1;
 }
 if(bind(sock, (struct sockaddr *)&addr, sizeof(addr))){
  fprintf(stderr, "Failed binding socket\n");
  return -1;
 }
  if(connect(sock, servinfo->ai_addr, servinfo->ai_addrlen)){
  fprintf(stderr, "Connect failed\n");
  return -1;
 }
 struct sockaddr_in tmpaddr = *((struct sockaddr_in*)servinfo->ai_addr);
  char* serverIP = inet_ntoa(tmpaddr.sin_addr);
  strcpy(pair->server_addr,serverIP);
 freeaddrinfo(servinfo);
  pair->server_fd = sock;
 return 1;
}

canonical_original_code:
int open_serverfd(struct socket_t *pair)
{
  char *fake_ip = proxy_stat->fakeIP;
  unsigned short rand_port = 0;
 if(verbal)fprintf(stdout, "fake:%s\nwww:%s\n",proxy_stat->fakeIP, proxy_stat->wwwIP);
 int sock;
  struct addrinfo *servinfo;
  struct sockaddr_in addr;
  addr.sin_family=2;
  addr.sin_addr.s_addr = inet_addr(fake_ip);
  addr.sin_port=htons(rand_port);
 if(proxy_stat->wwwIP == ((void *)0)){
  if(resolve("video.cs.cmu.edu", "8080", ((void *)0), &servinfo) != 0){
   fprintf(stderr, "getaddrinfo error in proxy_process\n");
   exit(-1);
  }
 }
 else{
   char *server_ip = proxy_stat->wwwIP;
   char server_port[] = "8080";
   struct addrinfo hints;
   memset(&hints, 0, sizeof(struct addrinfo));
   hints.ai_family = 0;
   hints.ai_socktype = SOCK_STREAM;
   hints.ai_flags = 0x0001;
   if(getaddrinfo(server_ip, server_port, &hints, &servinfo) != 0){
   fprintf(stderr, "getaddrinfo error\n");
   return -1;
  }
 }
 if((sock = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol)) == -1){
  fprintf(stderr, "Socket failed\n");
  return -1;
 }
 if(bind(sock, (struct sockaddr *)&addr, sizeof(addr))){
  fprintf(stderr, "Failed binding socket\n");
  return -1;
 }
  if(connect(sock, servinfo->ai_addr, servinfo->ai_addrlen)){
  fprintf(stderr, "Connect failed\n");
  return -1;
 }
 struct sockaddr_in tmpaddr = *((struct sockaddr_in*)servinfo->ai_addr);
  char *serverIP = inet_ntoa(tmpaddr.sin_addr);
  strcpy(pair->server_addr,serverIP);
 freeaddrinfo(servinfo);
  pair->server_fd = sock;
 return 1;
}

variable_types: sock int

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

variable_types: 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:
serverIP
char *

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

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

user_defined_types: struct buffer { char *ptr; char *buf; unsigned long count; unsigned long length; unsigned long size; }

user_defined_types: struct node { void *data; struct node *prev; struct node *next; }


addData

decompiled_code:
__int64 __fastcall addData(char **a1, const void *a2, size_t a3)
{
size_t v4; // rax
char *v5; // r13
size_t v6; // r12
size_t v7; // rax
char *v8; // r15
char *v9; // rax
__int64 result; // rax

if ( verbal )
fwrite("--->addData\n", 1uLL, 0xCuLL, stdout);
v4 = (size_t)a1[4];
v5 = *a1;
v6 = (size_t)&a1[3][a3];
if ( v6 <= v4 )
goto LABEL_8;
v7 = v4 + 0x2000;
v8 = a1[1];
if ( v7 >= v6 )
v6 = v7;
v9 = (char *)realloc(a1[1], v6);
if ( v9 )
{
a1[1] = v9;
a1[4] = (char *)v6;
v5 = &v9[(int)v5 - (int)v8];
*a1 = v5;
LABEL_8:
memcpy(v5, a2, a3);
a1[3] += a3;
result = 0LL;
*a1 += a3;
return result;
}
fwrite("Error: in realloc.\n", 1uLL, 0x13uLL, stderr);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func3(char **a1, const void *a2, size_t a3)
{
size_t v4; // rax
char *v5; // r13
size_t v6; // r12
size_t v7; // rax
char *v8; // r15
char *v9; // rax
__int64 result; // rax

if ( verbal )
fwrite("--->addData\n", 1uLL, 0xCuLL, stdout);
v4 = (size_t)a1[4];
v5 = *a1;
v6 = (size_t)&a1[3][a3];
if ( v6 <= v4 )
goto LABEL_8;
v7 = v4 + 0x2000;
v8 = a1[1];
if ( v7 >= v6 )
v6 = v7;
v9 = (char *)realloc(a1[1], v6);
if ( v9 )
{
a1[1] = v9;
a1[4] = (char *)v6;
v5 = &v9[(int)v5 - (int)v8];
*a1 = v5;
LABEL_8:
memcpy(v5, a2, a3);
a1[3] += a3;
result = 0LL;
*a1 += a3;
return result;
}
fwrite("Error: in realloc.\n", 1uLL, 0x13uLL, stderr);
return 0xFFFFFFFFLL;
}

original_code:
int addData(buffer *b,void* data,size_t len){
 if(verbal)
 fprintf(stdout, "--->addData\n");
 int offset=b->ptr-b->buf;
 if(b->length+len>b->size){
  size_t ex=((b->length+len)>(b->size+8192))
  ?(b->length+len):(b->size+8192);
  char *new=(char *)realloc(b->buf,ex);
  if(new==((void *)0)){
   fprintf(stderr, "Error: in realloc.\n");
   return -1;
  }
  b->buf=new;
  b->ptr=new+offset;
  b->size=ex;
 }
 if(memcpy(b->ptr,data,len)==((void *)0)){
  fprintf(stderr, "Error: in memcpy.\n");
  return -1;
 }
 b->length+=len;
 b->ptr+=len;
 return 0;
}

canonical_original_code:
int addData(struct buffer *b, void *data, unsigned long len){
 if(verbal)
 fprintf(stdout, "--->addData\n");
 int offset = b->ptr-b->buf;
 if(b->length+len>b->size){
  unsigned long ex = ((b->length+len)>(b->size+8192))
  ?(b->length+len):(b->size+8192);
  char *new = (char *)realloc(b->buf,ex);
  if(new==((void *)0)){
   fprintf(stderr, "Error: in realloc.\n");
   return -1;
  }
  b->buf=new;
  b->ptr=new+offset;
  b->size=ex;
 }
 if(memcpy(b->ptr,data,len)==((void *)0)){
  fprintf(stderr, "Error: in memcpy.\n");
  return -1;
 }
 b->length+=len;
 b->ptr+=len;
 return 0;
}

variable_types: struct buffer { char *ptr; char *buf; unsigned long count; unsigned long length; unsigned long size; } *

variable_types:
data
void *

variable_types:
len
unsigned long

variable_types: offset int

variable_types:
ex
unsigned long

variable_types:
new
char *


buildManifestContent

decompiled_code:
__int64 __fastcall buildManifestContent(__int64 a1, const char *a2)
{
size_t v3; // rax
_DWORD *v4; // rax
__int64 v5; // rdi
size_t v6; // rax
unsigned int v7; // er12
_DWORD *v8; // rax
__int64 v9; // rdi
char v11[8192]; // [rsp+0h] [rbp-4028h] BYREF
char s[8232]; // [rsp+2000h] [rbp-2028h] BYREF

sprintf(v11, "GET %sbig_buck_bunny.f4m %s", (const char *)(a1 + 2096), (const char *)(a1 + 1068));
if ( !strstr(v11, "\r\n") )
strcat(v11, "\r\n");
strcat(v11, a2);
sprintf(s, "GET %sbig_buck_bunny_nolist.f4m %s", (const char *)(a1 + 2096), (const char *)(a1 + 1068));
if ( !strstr(s, "\r\n") )
strcat(s, "\r\n");
strcat(s, a2);
v3 = strlen(v11);
if ( (unsigned int)addData(*(char ***)(a1 + 3208), v11, v3) )
{
v7 = -1;
fwrite("add data error - add manifest data\n", 1uLL, 0x23uLL, stderr);
}
else
{
v4 = malloc(0x410uLL);
v5 = *(_QWORD *)(a1 + 3176);
*v4 = 1;
enqueue(v5, v4);
v6 = strlen(s);
v7 = addData(*(char ***)(a1 + 3208), s, v6);
if ( v7 )
{
v7 = -1;
fwrite("add data error - add nolist manifest data\n", 1uLL, 0x2AuLL, stderr);
}
else
{
v8 = malloc(0x410uLL);
v9 = *(_QWORD *)(a1 + 3176);
*v8 = 3;
enqueue(v9, v8);
}
}
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, const char *a2)
{
size_t v3; // rax
_DWORD *v4; // rax
__int64 v5; // rdi
size_t v6; // rax
unsigned int v7; // er12
_DWORD *v8; // rax
__int64 v9; // rdi
char v11[8192]; // [rsp+0h] [rbp-4028h] BYREF
char s[8232]; // [rsp+2000h] [rbp-2028h] BYREF

sprintf(v11, "GET %sbig_buck_bunny.f4m %s", (const char *)(a1 + 2096), (const char *)(a1 + 1068));
if ( !strstr(v11, "\r\n") )
strcat(v11, "\r\n");
strcat(v11, a2);
sprintf(s, "GET %sbig_buck_bunny_nolist.f4m %s", (const char *)(a1 + 2096), (const char *)(a1 + 1068));
if ( !strstr(s, "\r\n") )
strcat(s, "\r\n");
strcat(s, a2);
v3 = strlen(v11);
if ( (unsigned int)func3(*(char ***)(a1 + 3208), v11, v3) )
{
v7 = -1;
fwrite("add data error - add manifest data\n", 1uLL, 0x23uLL, stderr);
}
else
{
v4 = malloc(0x410uLL);
v5 = *(_QWORD *)(a1 + 3176);
*v4 = 1;
enqueue(v5, v4);
v6 = strlen(s);
v7 = func3(*(char ***)(a1 + 3208), s, v6);
if ( v7 )
{
v7 = -1;
fwrite("add data error - add nolist manifest data\n", 1uLL, 0x2AuLL, stderr);
}
else
{
v8 = malloc(0x410uLL);
v9 = *(_QWORD *)(a1 + 3176);
*v8 = 3;
enqueue(v9, v8);
}
}
return v7;
}

original_code:
int buildManifestContent(socket_t *pair,char *header){
 char request[8192];
 char request_nolist[8192];
 sprintf(request,"GET %sbig_buck_bunny.f4m %s",pair->path,pair->version);
 if(strstr(request,"\r\n")==((void *)0))
   strcat(request,"\r\n");
 strcat(request,header);
 sprintf(request_nolist,"GET %sbig_buck_bunny_nolist.f4m %s",pair->path,pair->version);
 if(strstr(request_nolist,"\r\n")==((void *)0))
   strcat(request_nolist,"\r\n");
 strcat(request_nolist,header);
 if(addData(pair->buf_send_server,(void *)request,strlen(request))){
  fprintf(stderr, "add data error - add manifest data\n");
   return -1;
  }
  pstate_t *label_list = (pstate_t *)malloc(sizeof(pstate_t));
  label_list->request_type=1;
  enqueue(pair->requestQueue,(void *)label_list);
  if(addData(pair->buf_send_server,(void *)request_nolist,strlen(request_nolist))){
   fprintf(stderr, "add data error - add nolist manifest data\n");
   return -1;
  }
  pstate_t *label_nolist = (pstate_t *)malloc(sizeof(pstate_t));
  label_nolist->request_type=3;
  enqueue(pair->requestQueue,(void *)label_nolist);
  return 0;
}

canonical_original_code:
int buildManifestContent(struct socket_t *pair, char *header){
 char request[8192];
 char request_nolist[8192];
 sprintf(request,"GET %sbig_buck_bunny.f4m %s",pair->path,pair->version);
 if(strstr(request,"\r\n")==((void *)0))
   strcat(request,"\r\n");
 strcat(request,header);
 sprintf(request_nolist,"GET %sbig_buck_bunny_nolist.f4m %s",pair->path,pair->version);
 if(strstr(request_nolist,"\r\n")==((void *)0))
   strcat(request_nolist,"\r\n");
 strcat(request_nolist,header);
 if(addData(pair->buf_send_server,(void *)request,strlen(request))){
  fprintf(stderr, "add data error - add manifest data\n");
   return -1;
  }
  struct pstate_t *label_list = (pstate_t *)malloc(sizeof(pstate_t));
  label_list->request_type=1;
  enqueue(pair->requestQueue,(void *)label_list);
  if(addData(pair->buf_send_server,(void *)request_nolist,strlen(request_nolist))){
   fprintf(stderr, "add data error - add nolist manifest data\n");
   return -1;
  }
  struct pstate_t *label_nolist = (pstate_t *)malloc(sizeof(pstate_t));
  label_nolist->request_type=3;
  enqueue(pair->requestQueue,(void *)label_nolist);
  return 0;
}

variable_types:
request
char [8192]

variable_types: struct pstate_t { int request_type; int request_bitrate; long long send_time; char chunk_name[1024]; } *


buildRequestContent

decompiled_code:
__int64 __fastcall buildRequestContent(__int64 a1)
{
int v1; // eax
const char *v2; // r13
size_t v3; // rax
char *v4; // rax
char *v5; // r13
size_t v6; // rax
char *v7; // rax
unsigned int v8; // er12
void *v9; // r13
int v10; // eax
__int64 result; // rax
__int64 v12; // rax
__int64 v13; // rcx
__int64 v14; // rdx
__int128 v15; // [rsp+0h] [rbp-4018h] BYREF
char v16[8176]; // [rsp+10h] [rbp-4008h] BYREF
char src[16]; // [rsp+2000h] [rbp-2018h] BYREF
char v18[8176]; // [rsp+2010h] [rbp-2008h] BYREF

v15 = 0LL;
memset(v16, 0, sizeof(v16));
*(_OWORD *)src = 0LL;
memset(v18, 0, sizeof(v18));
buildRequestHeader(a1, src);
v1 = *(_DWORD *)(a1 + 2092);
strcpy((char *)&v15, "GET ");
if ( v1 == 1 )
{
fprintf(stdout, "%p\n", *(const void **)(a1 + 3120));
**(_BYTE **)(a1 + 3120) = 0;
fprintf(stdout, "%p---%c\n", *(const void **)(a1 + 3120), (unsigned int)**(char **)(a1 + 3120));
result = (unsigned int)buildManifestContent(a1, src);
}
else
{
v2 = (const char *)(a1 + 2096);
if ( v1 == 2 )
{
v2 = (const char *)(a1 + 2096);
**(_BYTE **)(a1 + 3120) = 0;
sprintf(
(char *)(a1 + 2096),
"%s%dSeg%d-Frag%d ",
(const char *)(a1 + 2096),
*(unsigned int *)(proxy_stat + 72LL),
*(unsigned int *)(a1 + 3132),
*(unsigned int *)(a1 + 3136));
}
v3 = strlen((const char *)&v15);
v4 = stpcpy((char *)&v15 + v3, v2);
*v4 = 32;
v5 = stpcpy(v4 + 1, (const char *)(a1 + 1068));
if ( !strstr((const char *)&v15, "\r\n") )
strcpy(v5, "\r\n");
v6 = strlen((const char *)&v15);
v7 = stpcpy((char *)&v15 + v6, src);
v8 = addData(*(char ***)(a1 + 3208), &v15, v7 - (char *)&v15);
if ( v8 )
{
v8 = -1;
}
else
{
v9 = malloc(0x410uLL);
v10 = *(_DWORD *)(a1 + 2092);
*(_DWORD *)v9 = v10;
if ( v10 == 2 )
{
v12 = getSystemTime(1040LL, &v15);
v13 = *(unsigned int *)(a1 + 3136);
v14 = *(unsigned int *)(a1 + 3132);
*((_QWORD *)v9 + 1) = v12;
sprintf((char *)v9 + 16, "Seg%d-Frag%d", v14, v13);
*((_DWORD *)v9 + 1) = *(_DWORD *)(proxy_stat + 72LL);
}
enqueue(*(_QWORD *)(a1 + 3176), v9);
}
result = v8;
}
return result;
}

original_code:
int buildRequestContent(socket_t *pair){
  char request[8192]={0};
  char header[8192]={0};
  buildRequestHeader(pair, header);
  strcpy(request,"GET");
  strcat(request," ");
  switch(pair->request_type){
   case 1:
    fprintf(stdout, "%p\n",pair->urn);
    *(pair->urn)=0;
    fprintf(stdout, "%p---%c\n",pair->urn,*(pair->urn));
    return buildManifestContent(pair,header);
   case 2:
    *(pair->urn)=0;
    sprintf(pair->path,"%s%dSeg%d-Frag%d ",pair->path,
     proxy_stat->bitrate,pair->seg_num,pair->frag_num);
    break;
   default:
    break;
  }
  strcat(request,pair->path);
  strcat(request," ");
  strcat(request,pair->version);
  if(strstr(request,"\r\n")==((void *)0))
   strcat(request,"\r\n");
  strcat(request,header);
  if(addData(pair->buf_send_server,(void *)request,strlen(request)))
   return -1;
  pstate_t *label = (pstate_t *)malloc(sizeof(pstate_t));
  label->request_type=pair->request_type;
  if(pair->request_type==2){
   label->send_time=getSystemTime();
   sprintf(label->chunk_name,"Seg%d-Frag%d",pair->seg_num,pair->frag_num);
   label->request_bitrate=proxy_stat->bitrate;
  }
  enqueue(pair->requestQueue,(void*)label);
  return 0;
}

canonical_original_code:
int buildRequestContent(struct socket_t *pair){
  char request[8192] = {0};
  char header[8192] = {0};
  buildRequestHeader(pair, header);
  strcpy(request,"GET");
  strcat(request," ");
  switch(pair->request_type){
   case 1:
    fprintf(stdout, "%p\n",pair->urn);
    *(pair->urn)=0;
    fprintf(stdout, "%p---%c\n",pair->urn,*(pair->urn));
    return buildManifestContent(pair,header);
   case 2:
    *(pair->urn)=0;
    sprintf(pair->path,"%s%dSeg%d-Frag%d ",pair->path,
     proxy_stat->bitrate,pair->seg_num,pair->frag_num);
    break;
   default:
    break;
  }
  strcat(request,pair->path);
  strcat(request," ");
  strcat(request,pair->version);
  if(strstr(request,"\r\n")==((void *)0))
   strcat(request,"\r\n");
  strcat(request,header);
  if(addData(pair->buf_send_server,(void *)request,strlen(request)))
   return -1;
  struct pstate_t *label = (pstate_t *)malloc(sizeof(pstate_t));
  label->request_type=pair->request_type;
  if(pair->request_type==2){
   label->send_time=getSystemTime();
   sprintf(label->chunk_name,"Seg%d-Frag%d",pair->seg_num,pair->frag_num);
   label->request_bitrate=proxy_stat->bitrate;
  }
  enqueue(pair->requestQueue,(void *)label);
  return 0;
}

variable_types:
header
char [8192]

user_defined_types: struct pstate_t { int request_type; int request_bitrate; long long send_time; char chunk_name[1024]; }


initBuffer

decompiled_code:
_QWORD *initBuffer()
{
_QWORD *v0; // rax
_QWORD *v1; // r12
void *v2; // rax
void *v4; // rdi

v0 = calloc(1uLL, 0x28uLL);
v1 = v0;
if ( v0 )
{
v0[4] = 0x2000LL;
v0[3] = 0LL;
v0[2] = 0LL;
v2 = calloc(0x2000uLL, 1uLL);
v1[1] = v2;
if ( v2 )
{
*v1 = v2;
}
else
{
v4 = v1;
v1 = 0LL;
free(v4);
fwrite("error in init buf\n", 1uLL, 0x12uLL, stderr);
}
}
return v1;
}

canonical_decompiled_code:
_QWORD *func0()
{
_QWORD *v0; // rax
_QWORD *v1; // r12
void *v2; // rax
void *v4; // rdi

v0 = calloc(1uLL, 0x28uLL);
v1 = v0;
if ( v0 )
{
v0[4] = 0x2000LL;
v0[3] = 0LL;
v0[2] = 0LL;
v2 = calloc(0x2000uLL, 1uLL);
v1[1] = v2;
if ( v2 )
{
*v1 = v2;
}
else
{
v4 = v1;
v1 = 0LL;
free(v4);
fwrite("error in init buf\n", 1uLL, 0x12uLL, stderr);
}
}
return v1;
}

original_code:
buffer *initBuffer(){
 buffer *b;
 if((b=(buffer *)calloc(1,sizeof(buffer)))==((void *)0))
  return ((void *)0);
 b->size=8192;
 b->length=0;
 b->count=0;
 if((b->buf=(char *)calloc(8192,sizeof(char)))==((void *)0)){
  free(b);
  fprintf(stderr, "error in init buf\n");
  return ((void *)0);
 }
 b->ptr=b->buf;
 return b;
}

canonical_original_code:
struct buffer *initBuffer(){
 struct buffer *b;
 if((b=(struct buffer *)calloc(1,sizeof(buffer)))==((void *)0))
  return ((void *)0);
 b->size=8192;
 b->length=0;
 b->count=0;
 if((b->buf=(char *)calloc(8192,sizeof(char)))==((void *)0)){
  free(b);
  fprintf(stderr, "error in init buf\n");
  return ((void *)0);
 }
 b->ptr=b->buf;
 return b;
}

return_type: struct buffer { char *ptr; char *buf; unsigned long count; unsigned long length; unsigned long size; } *


freeBuffer

decompiled_code:
void __fastcall freeBuffer(void *ptr)
{
void *v2; // rdi

v2 = (void *)*((_QWORD *)ptr + 1);
if ( v2 )
{
free(v2);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
void *v2; // rdi

v2 = (void *)*((_QWORD *)ptr + 1);
if ( v2 )
{
free(v2);
free(ptr);
}
}

original_code:
void freeBuffer(buffer *b){
 if(b->buf!=((void *)0)){
  free(b->buf);
  free(b);
  b=((void *)0);
 }
}

canonical_original_code:
void freeBuffer(struct buffer *b){
 if(b->buf!=((void *)0)){
  free(b->buf);
  free(b);
  b=((void *)0);
 }
}


initSocketBuffer

decompiled_code:
__int64 __fastcall initSocketBuffer(_QWORD *a1)
{
_QWORD *v1; // rax
_QWORD *v2; // rax
_QWORD *v3; // rax
_QWORD *v4; // rax
__int64 result; // rax

v1 = initBuffer();
a1[398] = v1;
if ( v1 )
{
v2 = initBuffer();
a1[399] = v2;
if ( v2 )
{
v3 = initBuffer();
a1[400] = v3;
if ( v3 )
{
v4 = initBuffer();
a1[401] = v4;
if ( v4 )
{
result = 0LL;
}
else
{
fwrite("error in init buf_send_server\n", 1uLL, 0x1EuLL, stderr);
result = 0xFFFFFFFFLL;
}
}
else
{
fwrite("error in init buf_send_client\n", 1uLL, 0x1EuLL, stderr);
result = 0xFFFFFFFFLL;
}
}
else
{
fwrite("error in init buf_client\n", 1uLL, 0x19uLL, stderr);
result = 0xFFFFFFFFLL;
}
}
else
{
fwrite("error in init buf_server\n", 1uLL, 0x19uLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(_QWORD *a1)
{
_QWORD *v1; // rax
_QWORD *v2; // rax
_QWORD *v3; // rax
_QWORD *v4; // rax
__int64 result; // rax

v1 = func0();
a1[398] = v1;
if ( v1 )
{
v2 = func0();
a1[399] = v2;
if ( v2 )
{
v3 = func0();
a1[400] = v3;
if ( v3 )
{
v4 = func0();
a1[401] = v4;
if ( v4 )
{
result = 0LL;
}
else
{
fwrite("error in init buf_send_server\n", 1uLL, 0x1EuLL, stderr);
result = 0xFFFFFFFFLL;
}
}
else
{
fwrite("error in init buf_send_client\n", 1uLL, 0x1EuLL, stderr);
result = 0xFFFFFFFFLL;
}
}
else
{
fwrite("error in init buf_client\n", 1uLL, 0x19uLL, stderr);
result = 0xFFFFFFFFLL;
}
}
else
{
fwrite("error in init buf_server\n", 1uLL, 0x19uLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int initSocketBuffer(socket_t *s){
 if((s->buf_server=initBuffer())==((void *)0)){
  fprintf(stderr, "error in init buf_server\n");
  return -1;
 }
 if((s->buf_client=initBuffer())==((void *)0)){
  fprintf(stderr, "error in init buf_client\n");
  return -1;
 }
 if((s->buf_send_client=initBuffer())==((void *)0)){
  fprintf(stderr, "error in init buf_send_client\n");
  return -1;
 }
 if((s->buf_send_server=initBuffer())==((void *)0)){
  fprintf(stderr, "error in init buf_send_server\n");
  return -1;
 }
 return 0;
}

canonical_original_code:
int initSocketBuffer(struct socket_t *s){
 if((s->buf_server=initBuffer())==((void *)0)){
  fprintf(stderr, "error in init buf_server\n");
  return -1;
 }
 if((s->buf_client=initBuffer())==((void *)0)){
  fprintf(stderr, "error in init buf_client\n");
  return -1;
 }
 if((s->buf_send_client=initBuffer())==((void *)0)){
  fprintf(stderr, "error in init buf_send_client\n");
  return -1;
 }
 if((s->buf_send_server=initBuffer())==((void *)0)){
  fprintf(stderr, "error in init buf_send_server\n");
  return -1;
 }
 return 0;
}

variable_types: struct socket_t { int client_fd; int server_fd; char *remote_addr; char server_addr[20]; char host[1024]; int host_port; int method; char version[1024]; int request_type; char path[1024]; char *urn; int request_rate; int seg_num; int frag_num; int close; int recv; int left; int contentlen; char *content_buf; enum server_t { HEADER=0, CONTENT=1 } server_stat; struct queue { int size; struct node { void *data; struct node *prev; struct node *next; } *head; struct node *tail; } *requestQueue; struct buffer { char *ptr; char *buf; unsigned long count; unsigned long length; unsigned long size; } *buf_server; struct buffer *buf_client; struct buffer *buf_send_client; struct buffer *buf_send_server; } *


cleanSocketBuffer

decompiled_code:
void __fastcall cleanSocketBuffer(void **a1)
{
freeBuffer(a1[398]);
freeBuffer(a1[399]);
freeBuffer(a1[401]);
freeBuffer(a1[400]);
}

original_code:
void cleanSocketBuffer(socket_t *s){
 freeBuffer(s->buf_server);
 freeBuffer(s->buf_client);
 freeBuffer(s->buf_send_server);
 freeBuffer(s->buf_send_client);
}

canonical_original_code:
void cleanSocketBuffer(struct socket_t *s){
 freeBuffer(s->buf_server);
 freeBuffer(s->buf_client);
 freeBuffer(s->buf_send_server);
 freeBuffer(s->buf_send_client);
}


resetSocketBuffer

decompiled_code:
__int64 __fastcall resetSocketBuffer(void **a1)
{
cleanSocketBuffer(a1);
return initSocketBuffer(a1);
}

canonical_decompiled_code:
__int64 __fastcall func4(void **a1)
{
func3(a1);
return func2(a1);
}

original_code:
int resetSocketBuffer(socket_t *s){
 cleanSocketBuffer(s);
 return initSocketBuffer(s);
}

canonical_original_code:
int resetSocketBuffer(struct socket_t *s){
 cleanSocketBuffer(s);
 return initSocketBuffer(s);
}


initSocketPair

original_code:
void initSocketPair(socket_t *pair){
  pair->client_fd=-1;
  pair->server_fd=-1;
  pair->remote_addr=((void *)0);
  bzero(pair->host, sizeof(char)*1024);
  pair->host_port=-1;
  pair->method=-1;
  bzero(pair->version, sizeof(char)*1024);
  pair->request_type=-1;
  bzero(pair->path, sizeof(char)*1024);
  pair->urn=((void *)0);
  pair->request_rate=-1;
  pair->seg_num=-1;
  pair->frag_num=-1;
  pair->close=-1;
 pair->recv = 0;
 pair->left = 0;
  pair->contentlen=-1;
 pair->content_buf = ((void *)0);
 pair->server_stat = HEADER;
  pair->requestQueue=new_queue();
  pair->buf_server=((void *)0);
 pair->buf_client=((void *)0);
 pair->buf_send_client=((void *)0);
 pair->buf_send_server=((void *)0);
 }

canonical_original_code:
void initSocketPair(struct socket_t *pair){
  pair->client_fd=-1;
  pair->server_fd=-1;
  pair->remote_addr=((void *)0);
  bzero(pair->host, sizeof(char)*1024);
  pair->host_port=-1;
  pair->method=-1;
  bzero(pair->version, sizeof(char)*1024);
  pair->request_type=-1;
  bzero(pair->path, sizeof(char)*1024);
  pair->urn=((void *)0);
  pair->request_rate=-1;
  pair->seg_num=-1;
  pair->frag_num=-1;
  pair->close=-1;
 pair->recv = 0;
 pair->left = 0;
  pair->contentlen=-1;
 pair->content_buf = ((void *)0);
 pair->server_stat = HEADER;
  pair->requestQueue=new_queue();
  pair->buf_server=((void *)0);
 pair->buf_client=((void *)0);
 pair->buf_send_client=((void *)0);
 pair->buf_send_server=((void *)0);
 }


resetSocketPair

original_code:
void resetSocketPair(socket_t *s){
 if(s->requestQueue!=((void *)0)){
  free_queue(s->requestQueue);
  s->requestQueue=((void *)0);
 }
 cleanSocketBuffer(s);
 if(s->server_fd>0){
  close(s->server_fd);
  ((void) (((&proxy_stat->p->write_set)->__fds_bits)[((s->server_fd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((s->server_fd) % (8 * (int) sizeof (__fd_mask)))))));
  ((void) (((&proxy_stat->p->read_set)->__fds_bits)[((s->server_fd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((s->server_fd) % (8 * (int) sizeof (__fd_mask)))))));
 }
 initSocketPair(s);
}

canonical_original_code:
void resetSocketPair(struct socket_t *s){
 if(s->requestQueue!=((void *)0)){
  free_queue(s->requestQueue);
  s->requestQueue=((void *)0);
 }
 cleanSocketBuffer(s);
 if(s->server_fd>0){
  close(s->server_fd);
  ((void) (((&proxy_stat->p->write_set)->__fds_bits)[((s->server_fd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((s->server_fd) % (8 * (int) sizeof (__fd_mask)))))));
  ((void) (((&proxy_stat->p->read_set)->__fds_bits)[((s->server_fd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((s->server_fd) % (8 * (int) sizeof (__fd_mask)))))));
 }
 initSocketPair(s);
}


clientReadByte

decompiled_code:
__int64 __fastcall clientReadByte(__int64 a1)
{
__int64 v2; // r12
signed __int64 v3; // rax
__int64 v4; // rdi
signed __int64 v5; // rbx
__int64 result; // rax
char buf[26]; // [rsp+Eh] [rbp-1Ah] BYREF

v2 = *(_QWORD *)(a1 + 3192);
v3 = recv(*(_DWORD *)a1, buf, 1uLL, 64);
if ( !v3 )
return 0xFFFFFFFFLL;
if ( v3 < 0 )
return (unsigned int)-(*_errno_location() != 11);
v4 = *(_QWORD *)(v2 + 24);
v5 = v3;
if ( (unsigned __int64)(v4 + v3) > 0x2000 )
{
fwrite("Request is too large\n", 1uLL, 0x15uLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
memcpy((void *)(*(_QWORD *)(v2 + 8) + v4), buf, v3);
*(_QWORD *)(*(_QWORD *)(a1 + 3192) + 24LL) += v5;
result = (unsigned int)v5;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
__int64 v2; // r12
signed __int64 v3; // rax
__int64 v4; // rdi
signed __int64 v5; // rbx
__int64 result; // rax
char buf[26]; // [rsp+Eh] [rbp-1Ah] BYREF

v2 = *(_QWORD *)(a1 + 3192);
v3 = recv(*(_DWORD *)a1, buf, 1uLL, 64);
if ( !v3 )
return 0xFFFFFFFFLL;
if ( v3 < 0 )
return (unsigned int)-(*_errno_location() != 11);
v4 = *(_QWORD *)(v2 + 24);
v5 = v3;
if ( (unsigned __int64)(v4 + v3) > 0x2000 )
{
fwrite("Request is too large\n", 1uLL, 0x15uLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
memcpy((void *)(*(_QWORD *)(v2 + 8) + v4), buf, v3);
*(_QWORD *)(*(_QWORD *)(a1 + 3192) + 24LL) += v5;
result = (unsigned int)v5;
}
return result;
}

original_code:
int clientReadByte(socket_t *pair){
 ssize_t n;
 char tmpbuf[2];
 buffer *clientbuf=pair->buf_client;
 n=recv(pair->client_fd,tmpbuf,1,MSG_DONTWAIT);
 if(n==0){
  return -1;
 }else if(n<0){
  if((*__errno_location ())==11)
   return 0;
  return -1;
 }else{
  if((clientbuf->length+n>8192)){
   fprintf(stderr, "Request is too large\n");
   return -1;
  }else{
   memcpy(clientbuf->buf+clientbuf->length,tmpbuf,n);
   pair->buf_client->length+=n;
  }
 }
 return n;
}

canonical_original_code:
int clientReadByte(struct socket_t *pair){
 long n;
 char tmpbuf[2];
 struct buffer *clientbuf = pair->buf_client;
 n=recv(pair->client_fd,tmpbuf,1,MSG_DONTWAIT);
 if(n==0){
  return -1;
 }else if(n<0){
  if((*__errno_location ())==11)
   return 0;
  return -1;
 }else{
  if((clientbuf->length+n>8192)){
   fprintf(stderr, "Request is too large\n");
   return -1;
  }else{
   memcpy(clientbuf->buf+clientbuf->length,tmpbuf,n);
   pair->buf_client->length+=n;
  }
 }
 return n;
}

variable_types:
tmpbuf
char [2]


serverReadByte

decompiled_code:
ssize_t __fastcall serverReadByte(__int64 a1)
{
ssize_t result; // rax

result = recv(
*(_DWORD *)(a1 + 4),
(void *)(*(_QWORD *)(*(_QWORD *)(a1 + 3184) + 8LL) + *(_QWORD *)(*(_QWORD *)(a1 + 3184) + 24LL)),
1uLL,
64);
if ( result > 0 )
*(_QWORD *)(*(_QWORD *)(a1 + 3184) + 24LL) += result;
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func8(__int64 a1)
{
ssize_t result; // rax

result = recv(
*(_DWORD *)(a1 + 4),
(void *)(*(_QWORD *)(*(_QWORD *)(a1 + 3184) + 8LL) + *(_QWORD *)(*(_QWORD *)(a1 + 3184) + 24LL)),
1uLL,
64);
if ( result > 0 )
*(_QWORD *)(*(_QWORD *)(a1 + 3184) + 24LL) += result;
return result;
}

original_code:
int serverReadByte(socket_t *pair)
{
  int fd = pair->server_fd;
 char *buf = pair->buf_server->buf + pair->buf_server->length;
 ssize_t n;
  if((n=recv(fd,buf,1,MSG_DONTWAIT))<=0){
  return n;
 }
 pair->buf_server->length+=n;
  return n;
}

canonical_original_code:
int serverReadByte(struct socket_t *pair)
{
  int fd = pair->server_fd;
 char *buf = pair->buf_server->buf + pair->buf_server->length;
 long n;
  if((n=recv(fd,buf,1,MSG_DONTWAIT))<=0){
  return n;
 }
 pair->buf_server->length+=n;
  return n;
}

variable_types: fd int

variable_types:
buf
char *


serverReadContent

decompiled_code:
ssize_t __fastcall serverReadContent(__int64 a1)
{
ssize_t result; // rax

result = recv(*(_DWORD *)(a1 + 4), (void *)(*(_QWORD *)(a1 + 3160) + *(int *)(a1 + 3144)), *(int *)(a1 + 3148), 64);
if ( result > 0 )
{
*(_DWORD *)(a1 + 3144) += result;
*(_DWORD *)(a1 + 3148) -= result;
}
return result;
}

original_code:
int serverReadContent(socket_t *pair)
{
  int fd = pair->server_fd;
  char *buf = pair->content_buf + pair->recv;
 int len = pair->left;
 ssize_t n;
 if((n = recv(fd, buf, len, MSG_DONTWAIT)) <= 0){
  return n;
 }
 pair->recv+=n;
  pair->left-=n;
  return n;
}

canonical_original_code:
int serverReadContent(struct socket_t *pair)
{
  int fd = pair->server_fd;
  char *buf = pair->content_buf + pair->recv;
 int len = pair->left;
 long n;
 if((n = recv(fd, buf, len, MSG_DONTWAIT)) <= 0){
  return n;
 }
 pair->recv+=n;
  pair->left-=n;
  return n;
}

variable_types: len int

variable_types: n long


readLine

original_code:
ssize_t readLine(buffer* src,char *usrbuf, ssize_t maxlen){
 ssize_t n;
 char *bufptr=usrbuf;
 for (n=1;n<maxlen;n++){
  if(src->count<src->length){
   *bufptr=*(src->ptr++);
   src->count++;
   if(*bufptr=='\n'){
    bufptr++;
    break;
   }
   bufptr++;
  }else if (n==1){
   return 0;
  }else
   break;
 }
 *bufptr=0;
 return n;
}

canonical_original_code:
long readLine(struct buffer *src, char *usrbuf, long maxlen){
 long n;
 char *bufptr = usrbuf;
 for (n=1;n<maxlen;n++){
  if(src->count<src->length){
   *bufptr=*(src->ptr++);
   src->count++;
   if(*bufptr=='\n'){
    bufptr++;
    break;
   }
   bufptr++;
  }else if (n==1){
   return 0;
  }else
   break;
 }
 *bufptr=0;
 return n;
}

variable_types:
usrbuf
char *

variable_types: maxlen long

variable_types:
bufptr
char *

return_type: long


decompiled_code:
void __fastcall free_send_packet(void *ptr)
{
free(*((void **)ptr + 3));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
free(*((void **)ptr + 3));
free(ptr);
}

original_code:
void free_send_packet(send_packet_t *p){
 free(p->data);
 free(p);
}

canonical_original_code:
void free_send_packet(struct send_packet_t *p){
 free(p->data);
 free(p);
}

variable_types: struct send_packet_t { int size; int sent; struct sockaddr { unsigned short sa_family; char sa_data[14]; } address; struct packet_t { struct header { unsigned int id; unsigned int qr; unsigned int opcode; unsigned int aa; unsigned int tc; unsigned int rd; unsigned int ra; unsigned int z; unsigned int rcode; unsigned int qdcount; unsigned int ancount; unsigned int nscount; unsigned int arcount; } header; char data[1024]; } *data; } *

user_defined_types: struct send_packet_t { int size; int sent; struct sockaddr address; struct packet_t *data; }

user_defined_types: struct packet_t { struct header header; char data[1024]; }


checkBuffer

decompiled_code:
__int64 __fastcall checkBuffer(__int64 a1, const char *a2)
{
__int64 result; // rax
__int64 v3; // [rsp-18h] [rbp-18h]

if ( verbal )
{
fprintf(stdout, "-------->check buf:%s<---------\n%s\n", a2, *(const char **)(a1 + 8));
fprintf(
stdout,
"size:%d length:%d count:%d ptr:%p buf:%p\n\n",
*(_QWORD *)(a1 + 32),
*(_QWORD *)(a1 + 24),
*(unsigned int *)(a1 + 16),
*(const void **)a1,
*(const void **)(a1 + 8));
result = v3;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, const char *a2)
{
__int64 result; // rax
__int64 v3; // [rsp-18h] [rbp-18h]

if ( verbal )
{
fprintf(stdout, "-------->check buf:%s<---------\n%s\n", a2, *(const char **)(a1 + 8));
fprintf(
stdout,
"size:%d length:%d count:%d ptr:%p buf:%p\n\n",
*(_QWORD *)(a1 + 32),
*(_QWORD *)(a1 + 24),
*(unsigned int *)(a1 + 16),
*(const void **)a1,
*(const void **)(a1 + 8));
result = v3;
}
return result;
}

original_code:
void checkBuffer(buffer *b,char *name){
 if (verbal==0) return;
 fprintf(stdout, "-------->check buf:%s<---------\n%s\n",name,b->buf);
 fprintf(stdout, "size:%d length:%d count:%d ptr:%p buf:%p\n\n",
  (int)b->size,(int)b->length,(int)b->count,
  b->ptr,b->buf);
}

canonical_original_code:
void checkBuffer(struct buffer *b, char *name){
 if (verbal==0) return;
 fprintf(stdout, "-------->check buf:%s<---------\n%s\n",name,b->buf);
 fprintf(stdout, "size:%d length:%d count:%d ptr:%p buf:%p\n\n",
  (int)b->size,(int)b->length,(int)b->count,
  b->ptr,b->buf);
}

variable_types: struct buffer { char *ptr; char *buf; unsigned long count; unsigned long length; unsigned long size; } *

variable_types:
name
char *


parseMethod

decompiled_code:
__int64 __fastcall parseMethod(__int64 a1, const char *a2)
{
char *v2; // rax
char *v3; // rax
const char *v4; // rbp
__int64 result; // rax
int v6; // [rsp+4h] [rbp-1424h] BYREF
int v7; // [rsp+8h] [rbp-1420h] BYREF
int v8; // [rsp+Ch] [rbp-141Ch] BYREF
int v9; // [rsp+10h] [rbp-1418h] BYREF
char v10; // [rsp+14h] [rbp-1414h]
char haystack[2048]; // [rsp+410h] [rbp-1018h] BYREF
char s[1024]; // [rsp+C10h] [rbp-818h] BYREF
char src[1048]; // [rsp+1010h] [rbp-418h] BYREF

if ( strchr(a2, 47) )
{
_isoc99_sscanf(a2, "%s %s %s", &v9, haystack, a1 + 1068);
if ( v9 == 5522759 )
{
*(_DWORD *)(a1 + 1064) = 1;
}
else if ( v9 != 1145128264 || v10 )
{
if ( v9 != 1414745936 || v10 )
*(_DWORD *)(a1 + 1064) = -1;
else
*(_DWORD *)(a1 + 1064) = 3;
}
else
{
*(_DWORD *)(a1 + 1064) = 2;
}
if ( strstr(haystack, "://") )
{
if ( !(unsigned int)_isoc99_sscanf(haystack, "%[^:]://%[^/]%s", &haystack[1024], s, src) )
fwrite("sscanf fault - uri - 1\n", 1uLL, 0x17uLL, stderr);
}
else if ( !(unsigned int)_isoc99_sscanf(haystack, "%s", src) )
{
fwrite("sscanf fault - uri - 2\n", 1uLL, 0x17uLL, stderr);
}
if ( strchr(s, 58) )
{
if ( !(unsigned int)_isoc99_sscanf(s, "%[^:]:%d", a1 + 36, a1 + 1060) )
fwrite("sscanf fault - host\n", 1uLL, 0x14uLL, stderr);
}
else
{
strcpy((char *)(a1 + 36), s);
*(_DWORD *)(a1 + 1060) = 80;
}
v2 = strcpy((char *)(a1 + 2096), src);
v3 = strrchr(v2, 47);
*(_QWORD *)(a1 + 3120) = v3;
if ( v3 )
{
v4 = v3 + 1;
*(_QWORD *)(a1 + 3120) = v3 + 1;
}
else
{
fwrite("urn===============null\n", 1uLL, 0x17uLL, stderr);
v4 = *(const char **)(a1 + 3120);
}
if ( strstr(v4, ".f4m") )
{
*(_DWORD *)(a1 + 2092) = 1;
result = 0LL;
}
else if ( strchr(v4, 46) || !(unsigned int)_isoc99_sscanf(v4, "%dSeg%d-Frag%d", &v6, &v7, &v8) )
{
*(_DWORD *)(a1 + 2092) = 3;
result = 0LL;
}
else
{
*(_DWORD *)(a1 + 2092) = 2;
*(_DWORD *)(a1 + 3128) = v6;
*(_DWORD *)(a1 + 3132) = v7;
*(_DWORD *)(a1 + 3136) = v8;
result = 0LL;
}
}
else
{
fwrite("bad request - without '/' \n", 1uLL, 0x1BuLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, const char *a2)
{
char *v2; // rax
char *v3; // rax
const char *v4; // rbp
__int64 result; // rax
int v6; // [rsp+4h] [rbp-1424h] BYREF
int v7; // [rsp+8h] [rbp-1420h] BYREF
int v8; // [rsp+Ch] [rbp-141Ch] BYREF
int v9; // [rsp+10h] [rbp-1418h] BYREF
char v10; // [rsp+14h] [rbp-1414h]
char haystack[2048]; // [rsp+410h] [rbp-1018h] BYREF
char s[1024]; // [rsp+C10h] [rbp-818h] BYREF
char src[1048]; // [rsp+1010h] [rbp-418h] BYREF

if ( strchr(a2, 47) )
{
_isoc99_sscanf(a2, "%s %s %s", &v9, haystack, a1 + 1068);
if ( v9 == 5522759 )
{
*(_DWORD *)(a1 + 1064) = 1;
}
else if ( v9 != 1145128264 || v10 )
{
if ( v9 != 1414745936 || v10 )
*(_DWORD *)(a1 + 1064) = -1;
else
*(_DWORD *)(a1 + 1064) = 3;
}
else
{
*(_DWORD *)(a1 + 1064) = 2;
}
if ( strstr(haystack, "://") )
{
if ( !(unsigned int)_isoc99_sscanf(haystack, "%[^:]://%[^/]%s", &haystack[1024], s, src) )
fwrite("sscanf fault - uri - 1\n", 1uLL, 0x17uLL, stderr);
}
else if ( !(unsigned int)_isoc99_sscanf(haystack, "%s", src) )
{
fwrite("sscanf fault - uri - 2\n", 1uLL, 0x17uLL, stderr);
}
if ( strchr(s, 58) )
{
if ( !(unsigned int)_isoc99_sscanf(s, "%[^:]:%d", a1 + 36, a1 + 1060) )
fwrite("sscanf fault - host\n", 1uLL, 0x14uLL, stderr);
}
else
{
strcpy((char *)(a1 + 36), s);
*(_DWORD *)(a1 + 1060) = 80;
}
v2 = strcpy((char *)(a1 + 2096), src);
v3 = strrchr(v2, 47);
*(_QWORD *)(a1 + 3120) = v3;
if ( v3 )
{
v4 = v3 + 1;
*(_QWORD *)(a1 + 3120) = v3 + 1;
}
else
{
fwrite("urn===============null\n", 1uLL, 0x17uLL, stderr);
v4 = *(const char **)(a1 + 3120);
}
if ( strstr(v4, ".f4m") )
{
*(_DWORD *)(a1 + 2092) = 1;
result = 0LL;
}
else if ( strchr(v4, 46) || !(unsigned int)_isoc99_sscanf(v4, "%dSeg%d-Frag%d", &v6, &v7, &v8) )
{
*(_DWORD *)(a1 + 2092) = 3;
result = 0LL;
}
else
{
*(_DWORD *)(a1 + 2092) = 2;
*(_DWORD *)(a1 + 3128) = v6;
*(_DWORD *)(a1 + 3132) = v7;
*(_DWORD *)(a1 + 3136) = v8;
result = 0LL;
}
}
else
{
fwrite("bad request - without '/' \n", 1uLL, 0x1BuLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int parseMethod(socket_t *pair,char *buf){
 char method[1024],uri[1024];
 char protocol[1024],host[1024],path[1024];
 if(strstr(buf,"/")==((void *)0)){
  fprintf(stderr, "bad request - without '/' \n");
  return -1;
 }
 sscanf(buf,"%s %s %s",method,uri,pair->version);
 if(!strcmp(method,"GET"))
  pair->method = 1;
 else if(!strcmp(method,"HEAD"))
  pair->method = 2;
 else if(!strcmp(method,"POST"))
  pair->method = 3;
 else{
  pair->method = -1;
 }
 if(strstr(uri,"://")!=((void *)0)){
  if(sscanf(uri,"%[^:]://%[^/]%s",protocol,host,path)==0){
   fprintf(stderr, "sscanf fault - uri - 1\n");
  }
 }else{
  if(sscanf(uri,"%s",path)==0){
   fprintf(stderr, "sscanf fault - uri - 2\n");
  }
 }
 if(strstr(host,":")!=((void *)0)){
  if(sscanf(host,"%[^:]:%d",pair->host,&pair->host_port)==0){
   fprintf(stderr, "sscanf fault - host\n");
  }
 }else{
  strcpy(pair->host,host);
  pair->host_port=80;
 }
 strcpy(pair->path,path);
 pair->urn=strrchr(pair->path,'/');
 if(pair->urn==((void *)0)) {
  fprintf(stderr, "urn===============null\n");
 }
 else
  pair->urn++;
 int bitrate,seg,frag;
 if(strstr(pair->urn,".f4m")){
  pair->request_type = 1;
 }else if((strchr(pair->urn,'.')==((void *)0))&&(sscanf(pair->urn,"%dSeg%d-Frag%d",&bitrate,&seg,&frag)!=0)){
  pair->request_rate=bitrate;
  pair->seg_num=seg;
  pair->frag_num=frag;
  pair->request_type = 2;
 }else
  pair->request_type = 3;
 return 0;
}

canonical_original_code:
int parseMethod(struct socket_t *pair, char *buf){
 char method[1024]; char uri[1024];
 char protocol[1024]; char host[1024]; char path[1024];
 if(strstr(buf,"/")==((void *)0)){
  fprintf(stderr, "bad request - without '/' \n");
  return -1;
 }
 sscanf(buf,"%s %s %s",method,uri,pair->version);
 if(!strcmp(method,"GET"))
  pair->method = 1;
 else if(!strcmp(method,"HEAD"))
  pair->method = 2;
 else if(!strcmp(method,"POST"))
  pair->method = 3;
 else{
  pair->method = -1;
 }
 if(strstr(uri,"://")!=((void *)0)){
  if(sscanf(uri,"%[^:]://%[^/]%s",protocol,host,path)==0){
   fprintf(stderr, "sscanf fault - uri - 1\n");
  }
 }else{
  if(sscanf(uri,"%s",path)==0){
   fprintf(stderr, "sscanf fault - uri - 2\n");
  }
 }
 if(strstr(host,":")!=((void *)0)){
  if(sscanf(host,"%[^:]:%d",pair->host,&pair->host_port)==0){
   fprintf(stderr, "sscanf fault - host\n");
  }
 }else{
  strcpy(pair->host,host);
  pair->host_port=80;
 }
 strcpy(pair->path,path);
 pair->urn=strrchr(pair->path,'/');
 if(pair->urn==((void *)0)) {
  fprintf(stderr, "urn===============null\n");
 }
 else
  pair->urn++;
 int bitrate; int seg; int frag;
 if(strstr(pair->urn,".f4m")){
  pair->request_type = 1;
 }else if((strchr(pair->urn,'.')==((void *)0))&&(sscanf(pair->urn,"%dSeg%d-Frag%d",&bitrate,&seg,&frag)!=0)){
  pair->request_rate=bitrate;
  pair->seg_num=seg;
  pair->frag_num=frag;
  pair->request_type = 2;
 }else
  pair->request_type = 3;
 return 0;
}

variable_types:
method
char [1024]

variable_types:
uri
char [1024]

variable_types:
protocol
char [1024]

variable_types:
host
char [1024]

variable_types:
path
char [1024]

variable_types: bitrate int

variable_types: seg int

variable_types: frag int


parseClientRequest

decompiled_code:
__int64 __fastcall parseClientRequest(__int64 a1)
{
__int64 result; // rax
char v2[1032]; // [rsp+0h] [rbp-408h] BYREF

checkBuffer(*(_QWORD *)(a1 + 3192), "buf_client");
if ( readLine(*(_QWORD *)(a1 + 3192), v2, 1024LL) )
{
if ( (int)parseMethod(a1, v2) < 0 )
{
fwrite("error parse header!!!!!!!!!!!\n", 1uLL, 0x1EuLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
else
{
fwrite("bad request - zero content\n", 1uLL, 0x1BuLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
__int64 result; // rax
char v2[1032]; // [rsp+0h] [rbp-408h] BYREF

func0(*(_QWORD *)(a1 + 3192), "buf_client");
if ( readLine(*(_QWORD *)(a1 + 3192), v2, 1024LL) )
{
if ( (int)func1(a1, v2) < 0 )
{
fwrite("error parse header!!!!!!!!!!!\n", 1uLL, 0x1EuLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
else
{
fwrite("bad request - zero content\n", 1uLL, 0x1BuLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int parseClientRequest(socket_t *pair){
 size_t n;
 char buf[1024];
 checkBuffer(pair->buf_client,"buf_client");
 if((n=readLine(pair->buf_client,buf,1024))==0){
  fprintf(stderr, "bad request - zero content\n");
  return -1;
 }
 if(parseMethod(pair,buf)<0){
  fprintf(stderr, "error parse header!!!!!!!!!!!\n");
  return -1;
 }
 return 0;
}

canonical_original_code:
int parseClientRequest(struct socket_t *pair){
 unsigned long n;
 char buf[1024];
 checkBuffer(pair->buf_client,"buf_client");
 if((n=readLine(pair->buf_client,buf,1024))==0){
  fprintf(stderr, "bad request - zero content\n");
  return -1;
 }
 if(parseMethod(pair,buf)<0){
  fprintf(stderr, "error parse header!!!!!!!!!!!\n");
  return -1;
 }
 return 0;
}

variable_types: struct socket_t { int client_fd; int server_fd; char *remote_addr; char server_addr[20]; char host[1024]; int host_port; int method; char version[1024]; int request_type; char path[1024]; char *urn; int request_rate; int seg_num; int frag_num; int close; int recv; int left; int contentlen; char *content_buf; enum server_t { HEADER=0, CONTENT=1 } server_stat; struct queue { int size; struct node { void *data; struct node *prev; struct node *next; } *head; struct node *tail; } *requestQueue; struct buffer { char *ptr; char *buf; unsigned long count; unsigned long length; unsigned long size; } *buf_server; struct buffer *buf_client; struct buffer *buf_send_client; struct buffer *buf_send_server; } *

variable_types:
n
unsigned long

variable_types:
buf
char [1024]


parseManifestFile

original_code:
int parseManifestFile(char *buf)
{
 proxy_stat->bitrates[0] = 10;
 proxy_stat->bitrates[1] = 100;
 proxy_stat->bitrates[2] = 500;
 proxy_stat->bitrates[3] = 1000;
 proxy_stat->bitrate = proxy_stat->bitrates[0];
 proxy_stat->t = (float)proxy_stat->bitrates[0];
 return 0;
}

variable_types:
buf
char *


parseLine

decompiled_code:
__int64 __fastcall parseLine(__int64 a1, const char *a2)
{
size_t v3; // rdi
int v5; // [rsp+Ch] [rbp-1Ch] BYREF

if ( strstr(a2, "Content-Length") )
{
v5 = 0;
_isoc99_sscanf(a2, "Content-Length: %d", &v5);
v3 = v5;
*(_DWORD *)(a1 + 3144) = 0;
*(_DWORD *)(a1 + 3148) = v3;
*(_DWORD *)(a1 + 3152) = v3;
*(_QWORD *)(a1 + 3160) = calloc(v3, 1uLL);
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, const char *a2)
{
size_t v3; // rdi
int v5; // [rsp+Ch] [rbp-1Ch] BYREF

if ( strstr(a2, "Content-Length") )
{
v5 = 0;
_isoc99_sscanf(a2, "Content-Length: %d", &v5);
v3 = v5;
*(_DWORD *)(a1 + 3144) = 0;
*(_DWORD *)(a1 + 3148) = v3;
*(_DWORD *)(a1 + 3152) = v3;
*(_QWORD *)(a1 + 3160) = calloc(v3, 1uLL);
}
return 0LL;
}

original_code:
int parseLine(socket_t *pair, char *buf)
{
 if(strstr(buf, "Content-Length")){
  int len = 0;
  sscanf(buf,"Content-Length: %d",&len);
  pair->recv = 0;
  pair->left = len;
  pair->contentlen = len;
  pair->content_buf = calloc(len,sizeof(char));
 }
 return 0;
}

canonical_original_code:
int parseLine(struct socket_t *pair, char *buf)
{
 if(strstr(buf, "Content-Length")){
  int len = 0;
  sscanf(buf,"Content-Length: %d",&len);
  pair->recv = 0;
  pair->left = len;
  pair->contentlen = len;
  pair->content_buf = calloc(len,sizeof(char));
 }
 return 0;
}

variable_types: len int


parseServerHeader

decompiled_code:
__int64 __fastcall parseServerHeader(__int64 a1)
{
__int64 result; // rax
__int128 v2; // [rsp+0h] [rbp-408h] BYREF
char v3[1008]; // [rsp+10h] [rbp-3F8h] BYREF

v2 = 0LL;
memset(v3, 0, sizeof(v3));
while ( readLine(*(_QWORD *)(a1 + 3184), &v2, 1024LL) )
{
parseLine(a1, (const char *)&v2);
if ( (_BYTE)v2 == 13 )
{
result = (unsigned int)BYTE1(v2) - 10;
if ( BYTE1(v2) == 10 )
return result;
}
}
fwrite("bad request - zero content\n", 1uLL, 0x1BuLL, stderr);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
__int64 result; // rax
__int128 v2; // [rsp+0h] [rbp-408h] BYREF
char v3[1008]; // [rsp+10h] [rbp-3F8h] BYREF

v2 = 0LL;
memset(v3, 0, sizeof(v3));
while ( readLine(*(_QWORD *)(a1 + 3184), &v2, 1024LL) )
{
func4(a1, (const char *)&v2);
if ( (_BYTE)v2 == 13 )
{
result = (unsigned int)BYTE1(v2) - 10;
if ( BYTE1(v2) == 10 )
return result;
}
}
fwrite("bad request - zero content\n", 1uLL, 0x1BuLL, stderr);
return 0xFFFFFFFFLL;
}

original_code:
int parseServerHeader(socket_t *pair)
{
 char buf[1024] = {0};
 do{
  size_t n;
  if((n=readLine(pair->buf_server,buf,1024))==0){
   fprintf(stderr, "bad request - zero content\n");
   return -1;
  }
  parseLine(pair, buf);
 }while(strncmp(buf,"\r\n",2)!=0);
 return 0;
}

canonical_original_code:
int parseServerHeader(struct socket_t *pair)
{
 char buf[1024] = {0};
 do{
  unsigned long n;
  if((n=readLine(pair->buf_server,buf,1024))==0){
   fprintf(stderr, "bad request - zero content\n");
   return -1;
  }
  parseLine(pair, buf);
 }while(strncmp(buf,"\r\n",2)!=0);
 return 0;
}


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

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

original_code:
int init_mydns(const char *dns_ip, unsigned int dns_port)
{
 return 0;
}

canonical_original_code:
int init_mydns(char *dns_ip, unsigned int dns_port)
{
 return 0;
}


resolve

decompiled_code:
__int64 __fastcall resolve(__int64 a1, const char *a2, __int64 a3, struct addrinfo **a4)
{
__int16 v5; // ax
sa_family_t v6; // dx
int v7; // edx
int v8; // eax
int v9; // ebp
ssize_t v10; // rax
int v12; // [rsp+8h] [rbp-CF0h] BYREF
socklen_t addr_len; // [rsp+Ch] [rbp-CECh] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-CE8h] BYREF
struct sockaddr v15; // [rsp+20h] [rbp-CD8h] BYREF
struct sockaddr v16; // [rsp+30h] [rbp-CC8h] BYREF
fd_set readfds; // [rsp+40h] [rbp-CB8h] BYREF
__int128 buf; // [rsp+C0h] [rbp-C38h] BYREF
char v19[1008]; // [rsp+D0h] [rbp-C28h] BYREF
__int128 v20; // [rsp+4C0h] [rbp-838h] BYREF
char v21[1008]; // [rsp+4D0h] [rbp-828h] BYREF
char name[16]; // [rsp+8C0h] [rbp-438h] BYREF
char v23[1008]; // [rsp+8D0h] [rbp-428h] BYREF

memset(&readfds, 0, sizeof(readfds));
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = inet_addr(*(const char **)(proxy_stat + 24LL));
v5 = _bswap_16(0);
v15.sa_family = v6;
*(_WORD *)addr.sa_data = v5;
*(_DWORD *)&v15.sa_data[2] = inet_addr(*(const char **)(proxy_stat + 32LL));
*(_WORD *)v15.sa_data = _bswap_16(*(_WORD *)(proxy_stat + 8LL));
v8 = socket(2, 2, v7);
if ( v8 == -1 )
{
puts("Failed creating dns socket");
exit(-1);
}
v9 = v8;
if ( bind(v8, &addr, 0x10u) )
{
puts("Failed binding dns socket");
exit(-1);
}
buf = 0LL;
memset(v19, 0, sizeof(v19));
v12 = 0;
construct_request_packet(a1, &buf, &v12);
puts("send to dns server");
sendto(v9, &buf, v12, 0, &v15, 0x10u);
readfds.fds_bits[v9 / 64] |= 1LL << (v9 % 64);
if ( select(v9 + 1, &readfds, 0LL, 0LL, 0LL) < 0 )
{
fwrite("select error\n", 1uLL, 0xDuLL, stderr);
exit(-1);
}
printf("-------------dns select done----------------\n ");
memset(v21, 0, sizeof(v21));
memset(v23, 0, sizeof(v23));
v20 = 0LL;
*(_OWORD *)name = 0LL;
addr_len = 16;
v10 = recvfrom(v9, &v20, 0x400uLL, 0, &v16, &addr_len);
printf("recv %d bytes\n", v10);
parse_dns_response(&v20, name, 0.0);
printf("ip = %s\n", name);
if ( getaddrinfo(name, a2, 0LL, a4) )
{
fwrite("dns getaddrinfo error\n", 1uLL, 0x16uLL, stderr);
exit(-1);
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, const char *a2, __int64 a3, struct addrinfo **a4)
{
__int16 v5; // ax
sa_family_t v6; // dx
int v7; // edx
int v8; // eax
int v9; // ebp
ssize_t v10; // rax
int v12; // [rsp+8h] [rbp-CF0h] BYREF
socklen_t addr_len; // [rsp+Ch] [rbp-CECh] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-CE8h] BYREF
struct sockaddr v15; // [rsp+20h] [rbp-CD8h] BYREF
struct sockaddr v16; // [rsp+30h] [rbp-CC8h] BYREF
fd_set readfds; // [rsp+40h] [rbp-CB8h] BYREF
__int128 buf; // [rsp+C0h] [rbp-C38h] BYREF
char v19[1008]; // [rsp+D0h] [rbp-C28h] BYREF
__int128 v20; // [rsp+4C0h] [rbp-838h] BYREF
char v21[1008]; // [rsp+4D0h] [rbp-828h] BYREF
char name[16]; // [rsp+8C0h] [rbp-438h] BYREF
char v23[1008]; // [rsp+8D0h] [rbp-428h] BYREF

memset(&readfds, 0, sizeof(readfds));
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = inet_addr(*(const char **)(proxy_stat + 24LL));
v5 = func0(0);
v15.sa_family = v6;
*(_WORD *)addr.sa_data = v5;
*(_DWORD *)&v15.sa_data[2] = inet_addr(*(const char **)(proxy_stat + 32LL));
*(_WORD *)v15.sa_data = func0(*(_WORD *)(proxy_stat + 8LL));
v8 = socket(2, 2, v7);
if ( v8 == -1 )
{
puts("Failed creating dns socket");
exit(-1);
}
v9 = v8;
if ( bind(v8, &addr, 0x10u) )
{
puts("Failed binding dns socket");
exit(-1);
}
buf = 0LL;
memset(v19, 0, sizeof(v19));
v12 = 0;
construct_request_packet(a1, &buf, &v12);
puts("send to dns server");
sendto(v9, &buf, v12, 0, &v15, 0x10u);
readfds.fds_bits[v9 / 64] |= 1LL << (v9 % 64);
if ( select(v9 + 1, &readfds, 0LL, 0LL, 0LL) < 0 )
{
fwrite("select error\n", 1uLL, 0xDuLL, stderr);
exit(-1);
}
printf("-------------dns select done----------------\n ");
memset(v21, 0, sizeof(v21));
memset(v23, 0, sizeof(v23));
v20 = 0LL;
*(_OWORD *)name = 0LL;
addr_len = 16;
v10 = recvfrom(v9, &v20, 0x400uLL, 0, &v16, &addr_len);
printf("recv %d bytes\n", v10);
parse_dns_response(&v20, name, 0.0);
printf("ip = %s\n", name);
if ( getaddrinfo(name, a2, 0LL, a4) )
{
fwrite("dns getaddrinfo error\n", 1uLL, 0x16uLL, stderr);
exit(-1);
}
return 0LL;
}

original_code:
int resolve(const char *node, const char *service,
      const struct addrinfo *hints, struct addrinfo **res)
{
 int sock;
 struct sockaddr_in addr,dnsaddr;
 int fdmax;
 fd_set read_fd;
 do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&read_fd)->__fds_bits)[0]) : "memory"); } while (0);
 addr.sin_family = 2;
 addr.sin_addr.s_addr = inet_addr(proxy_stat->fakeIP);
 addr.sin_port = htons(0);
 dnsaddr.sin_family = 2;
 dnsaddr.sin_addr.s_addr = inet_addr(proxy_stat->dnsIP);
 dnsaddr.sin_port = htons(proxy_stat->dnsPort);
 if((sock = socket(2, SOCK_DGRAM, 0)) == -1){
  printf("Failed creating dns socket\n");
  exit(-1);
 }
 if(bind(sock, (struct sockaddr *)&addr, sizeof(addr))){
  printf("Failed binding dns socket\n");
  exit(-1);
 }
 char buf[1024] = {0};
 int len = 0;
 construct_request_packet(node, buf, &len);
 printf("send to dns server\n");
 sendto(sock, buf, len, 0, (struct sockaddr *)&dnsaddr, sizeof(dnsaddr));
 fdmax = sock;
 ((void) (((&read_fd)->__fds_bits)[((sock) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sock) % (8 * (int) sizeof (__fd_mask)))))));
 if(select(fdmax+1, &read_fd, ((void *)0), ((void *)0), ((void *)0)) < 0){
  fprintf(stderr, "select error\n");
  exit(-1);
 }
 printf("-------------dns select done----------------\n ");
 struct sockaddr_in from;
 socklen_t fromlen = sizeof(from);
 char buf2[1024] = {0};
 char ip[1024] = {0};
 int num = recvfrom(sock, buf2, 1024, 0, (struct sockaddr *)&from, &fromlen);
 printf("recv %d bytes\n", num);
 parse_dns_response(buf2, ip);
 printf("ip = %s\n", ip);
 if(getaddrinfo(ip,service,((void *)0),res) != 0){
  fprintf(stderr,"dns getaddrinfo error\n");
  exit(-1);
 }
 return 0;
}

canonical_original_code:
int resolve(char *node, char *service, struct addrinfo *hints, struct addrinfo **res)
{
 int sock;
 struct sockaddr_in addr; struct sockaddr_in dnsaddr;
 int fdmax;
 struct fd_set read_fd;
 do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&read_fd)->__fds_bits)[0]) : "memory"); } while (0);
 addr.sin_family = 2;
 addr.sin_addr.s_addr = inet_addr(proxy_stat->fakeIP);
 addr.sin_port = htons(0);
 dnsaddr.sin_family = 2;
 dnsaddr.sin_addr.s_addr = inet_addr(proxy_stat->dnsIP);
 dnsaddr.sin_port = htons(proxy_stat->dnsPort);
 if((sock = socket(2, SOCK_DGRAM, 0)) == -1){
  printf("Failed creating dns socket\n");
  exit(-1);
 }
 if(bind(sock, (struct sockaddr *)&addr, sizeof(addr))){
  printf("Failed binding dns socket\n");
  exit(-1);
 }
 char buf[1024] = {0};
 int len = 0;
 construct_request_packet(node, buf, &len);
 printf("send to dns server\n");
 sendto(sock, buf, len, 0, (struct sockaddr *)&dnsaddr, sizeof(dnsaddr));
 fdmax = sock;
 ((void) (((&read_fd)->__fds_bits)[((sock) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sock) % (8 * (int) sizeof (__fd_mask)))))));
 if(select(fdmax+1, &read_fd, ((void *)0), ((void *)0), ((void *)0)) < 0){
  fprintf(stderr, "select error\n");
  exit(-1);
 }
 printf("-------------dns select done----------------\n ");
 struct sockaddr_in from;
 unsigned int fromlen = sizeof(from);
 char buf2[1024] = {0};
 char ip[1024] = {0};
 int num = recvfrom(sock, buf2, 1024, 0, (struct sockaddr *)&from, &fromlen);
 printf("recv %d bytes\n", num);
 parse_dns_response(buf2, ip);
 printf("ip = %s\n", ip);
 if(getaddrinfo(ip,service,((void *)0),res) != 0){
  fprintf(stderr,"dns getaddrinfo error\n");
  exit(-1);
 }
 return 0;
}

variable_types:
node
char *

variable_types:
service
char *

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

variable_types:
res
struct addrinfo **

variable_types: sock int

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

variable_types: fdmax int

variable_types:
fromlen
unsigned int

variable_types:
ip
char [1024]

variable_types: num int


decompiled_code:
_QWORD *new_queue()
{
_QWORD *result; // rax

result = malloc(0x18uLL);
*(_DWORD *)result = 0;
result[2] = 0LL;
result[1] = 0LL;
return result;
}

canonical_decompiled_code:
_QWORD *func0()
{
_QWORD *result; // rax

result = malloc(0x18uLL);
*(_DWORD *)result = 0;
result[2] = 0LL;
result[1] = 0LL;
return result;
}

original_code:
queue_t *new_queue()
{
    queue_t *q = malloc(sizeof(queue_t));
    q->size = 0;
    q->head = q->tail = ((void *)0);
    return q;
}

canonical_original_code:
struct queue *new_queue()
{
    struct queue *q = malloc(sizeof(queue_t));
    q->size = 0;
    q->head = q->tail = ((void *)0);
    return q;
}

variable_types: struct queue { int size; struct node { void *data; struct node *prev; struct node *next; } *head; struct node *tail; } *


decompiled_code:
void __fastcall free_queue(void *ptr)
{
while ( *(_DWORD *)ptr )
dequeue((__int64)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
while ( *(_DWORD *)ptr )
func1((__int64)ptr);
free(ptr);
}

original_code:
void free_queue(queue_t *queue)
{
  while(queue->size != 0){
    dequeue(queue);
  }
  free(queue);
}

canonical_original_code:
void free_queue(struct queue *queue)
{
  while(queue->size != 0){
    dequeue(queue);
  }
  free(queue);
}


enqueuePQ

original_code:
int enqueuePQ(queue_t *queue, void *data,int weight)
{
  ((void) sizeof ((queue != ((void *)0)) ? 1 : 0), __extension__ ({ if (queue != ((void *)0)) ; else __assert_fail ("queue != NULL", "src/dns_queue.c", 85, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((data != ((void *)0)) ? 1 : 0), __extension__ ({ if (data != ((void *)0)) ; else __assert_fail ("data != NULL", "src/dns_queue.c", 86, __extension__ __PRETTY_FUNCTION__); }));
  node_t *node = malloc(sizeof(node_t));
  node->weight=weight;
  node->data = data;
  node->prev = ((void *)0);
  node->next = ((void *)0);
  if(queue->tail == ((void *)0)){
    queue->head = node;
    queue->tail = node;
  }else{
    node_t *ptr;
    for(ptr = queue->head;ptr != ((void *)0); ptr = ptr->next){
      if(weight <= ptr->weight){
        if(ptr == queue->head){
          node->next = queue->head;
          queue->head = node;
        }else{
          node->prev = ptr->prev;
          ptr->prev->next = node;
          ptr->prev = node;
          node->next = ptr;
        }
        break;
      }
    }
    if(ptr == ((void *)0)){
      queue->tail->next = node;
      node->prev = queue->tail;
      queue->tail = node;
    }
  }
  queue->size++;
  return 0;
}

canonical_original_code:
int enqueuePQ(struct queue *queue, void *data, int weight)
{
  ((void) sizeof ((queue != ((void *)0)) ? 1 : 0), __extension__ ({ if (queue != ((void *)0)) ; else __assert_fail ("queue != NULL", "src/dns_queue.c", 85, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((data != ((void *)0)) ? 1 : 0), __extension__ ({ if (data != ((void *)0)) ; else __assert_fail ("data != NULL", "src/dns_queue.c", 86, __extension__ __PRETTY_FUNCTION__); }));
  struct node *node = malloc(sizeof(node_t));
  node->weight=weight;
  node->data = data;
  node->prev = ((void *)0);
  node->next = ((void *)0);
  if(queue->tail == ((void *)0)){
    queue->head = node;
    queue->tail = node;
  }else{
    struct node *ptr;
    for(ptr = queue->head;ptr != ((void *)0); ptr = ptr->next){
      if(weight <= ptr->weight){
        if(ptr == queue->head){
          node->next = queue->head;
          queue->head = node;
        }else{
          node->prev = ptr->prev;
          ptr->prev->next = node;
          ptr->prev = node;
          node->next = ptr;
        }
        break;
      }
    }
    if(ptr == ((void *)0)){
      queue->tail->next = node;
      node->prev = queue->tail;
      queue->tail = node;
    }
  }
  queue->size++;
  return 0;
}

variable_types: struct queue { int size; struct node { void *data; int weight; struct node *prev; struct node *next; } *head; struct node *tail; } *

variable_types:
data
void *

variable_types: weight int


decompiled_code:
__int64 next_bitrate()
{
signed int v0; // er8
unsigned int *v1; // rax

v0 = *(_DWORD *)(proxy_stat + 72LL);
v1 = (unsigned int *)(proxy_stat + 56LL);
while ( (int)*v1 <= v0 )
{
if ( ++v1 == (unsigned int *)(proxy_stat + 72LL) )
return (unsigned int)v0;
}
return *v1;
}

canonical_decompiled_code:
__int64 func0()
{
signed int v0; // er8
unsigned int *v1; // rax

v0 = *(_DWORD *)(proxy_stat + 72LL);
v1 = (unsigned int *)(proxy_stat + 56LL);
while ( (int)*v1 <= v0 )
{
if ( ++v1 == (unsigned int *)(proxy_stat + 72LL) )
return (unsigned int)v0;
}
return *v1;
}

original_code:
int next_bitrate()
{
  int cur_bitrate = proxy_stat->bitrate;
 int next_bitrate = proxy_stat->bitrate;
 for(int i=0; i<4; i++){
  if(proxy_stat->bitrates[i]>cur_bitrate){
   next_bitrate = proxy_stat->bitrates[i];
   break;
  }
 }
 return next_bitrate;
}

canonical_original_code:
int next_bitrate()
{
  int cur_bitrate = proxy_stat->bitrate;
 int next_bitrate = proxy_stat->bitrate;
 for(int i = 0; i<4; i++){
  if(proxy_stat->bitrates[i]>cur_bitrate){
   next_bitrate = proxy_stat->bitrates[i];
   break;
  }
 }
 return next_bitrate;
}

variable_types: i int