decompiled_code:
__int64 __fastcall database_open(__int64 a1)
{
return (unsigned int)((int)sqlite3_open(a1, &database_handle) >> 31);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return (unsigned int)((int)sqlite3_open(a1, &database_handle) >> 31);
}

original_code:
int database_open(char *filename)
{
 if(sqlite3_open(filename,&database_handle)<0)
  return -1;
 return 0;
}


repo: ElFeesho/OldCProjects

decompiled_code:
void __fastcall signal_catcher(unsigned int a1)
{
log_writef(0LL, "Caught signal: %d, closing\n", a1);
log_file_close();
}

canonical_decompiled_code:
void __fastcall func0(unsigned int a1)
{
log_writef(0LL, "Caught signal: %d, closing\n", a1);
log_file_close();
}

original_code:
void signal_catcher(int signal)
{
 log_writef(LOG_INFO,"Caught signal: %d, closing\n",signal);
 log_file_close();
}

variable_types: signal int

return_type: void


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
log_file_open(argc, argv, envp);
signal(2, (__sighandler_t)signal_catcher);
signal(3, (__sighandler_t)signal_catcher);
signal(11, (__sighandler_t)signal_catcher);
signal(15, (__sighandler_t)signal_catcher);
network_init(10101LL);
return 0;
}

original_code:
int main(int argc, char ** argv)
{
 log_file_open();
 signal(2,signal_catcher);
 signal(3,signal_catcher);
 signal(11,signal_catcher);
 signal(15,signal_catcher);
 network_init(10101);
 return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
 log_file_open();
 signal(2,signal_catcher);
 signal(3,signal_catcher);
 signal(11,signal_catcher);
 signal(15,signal_catcher);
 network_init(10101);
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

return_type: int


decompiled_code:
void panels_update()
{
;
}

canonical_decompiled_code:
void func13()
{
;
}

original_code:
void panels_update()
{
}

canonical_original_code:
void panels_update()
{
}


decompiled_code:
__int64 __fastcall net_connect(void *buf, void *a2)
{
size_t v2; // rax
size_t v3; // rax
struct sockaddr v5; // [rsp+0h] [rbp-1028h] BYREF
__int128 bufa; // [rsp+10h] [rbp-1018h] BYREF
char v7[4080]; // [rsp+20h] [rbp-1008h] BYREF

sock = socket(2, 1, 0);
v5.sa_family = 2;
*(_WORD *)v5.sa_data = _bswap_16_constprop_0("192.168.1.254");
*(_DWORD *)&v5.sa_data[2] = inet_addr("192.168.1.254");
if ( connect(sock, &v5, 0x10u) )
return 0xFFFFFFFFLL;
v2 = strlen((const char *)buf);
send(sock, buf, v2, 0);
send(sock, "\r\n", 2uLL, 0);
v3 = strlen((const char *)a2);
send(sock, a2, v3, 0);
send(sock, "\r\n", 2uLL, 0);
sleep(1u);
bufa = 0LL;
memset(v7, 0, sizeof(v7));
recv(sock, &bufa, 0x1000uLL, 64);
return (unsigned int)sock;
}

canonical_decompiled_code:
__int64 __fastcall func1(void *buf, void *a2)
{
size_t v2; // rax
size_t v3; // rax
struct sockaddr v5; // [rsp+0h] [rbp-1028h] BYREF
__int128 bufa; // [rsp+10h] [rbp-1018h] BYREF
char v7[4080]; // [rsp+20h] [rbp-1008h] BYREF

sock = socket(2, 1, 0);
v5.sa_family = 2;
*(_WORD *)v5.sa_data = func0("192.168.1.254");
*(_DWORD *)&v5.sa_data[2] = inet_addr("192.168.1.254");
if ( connect(sock, &v5, 0x10u) )
return 0xFFFFFFFFLL;
v2 = strlen((const char *)buf);
send(sock, buf, v2, 0);
send(sock, "\r\n", 2uLL, 0);
v3 = strlen((const char *)a2);
send(sock, a2, v3, 0);
send(sock, "\r\n", 2uLL, 0);
sleep(1u);
bufa = 0LL;
memset(v7, 0, sizeof(v7));
recv(sock, &bufa, 0x1000uLL, 64);
return (unsigned int)sock;
}

original_code:
int net_connect(const char *username, const char *password)
{
 sock = socket(2, SOCK_STREAM, 0);
 struct sockaddr_in sock_dst;
 sock_dst.sin_family = 2;
 sock_dst.sin_port = htons(23);
 sock_dst.sin_addr.s_addr = inet_addr("192.168.1.254");
 if(connect(sock, (struct sockaddr*)&sock_dst, sizeof(sock_dst))!=0)
  return -1;
 send(sock, username, strlen(username),0);
 send(sock, "\r\n", 2, 0);
 send(sock, password, strlen(password),0);
 send(sock, "\r\n", 2, 0);
 sleep(1);
 char buffer[4096] = { 0 };
 recv(sock, buffer, 4096, MSG_DONTWAIT);
 return sock;
}

canonical_original_code:
int net_connect(char *username, char *password)
{
 sock = socket(2, SOCK_STREAM, 0);
 struct sockaddr_in sock_dst;
 sock_dst.sin_family = 2;
 sock_dst.sin_port = htons(23);
 sock_dst.sin_addr.s_addr = inet_addr("192.168.1.254");
 if(connect(sock, (struct sockaddr *)&sock_dst, sizeof(sock_dst))!=0)
  return -1;
 send(sock, username, strlen(username),0);
 send(sock, "\r\n", 2, 0);
 send(sock, password, strlen(password),0);
 send(sock, "\r\n", 2, 0);
 sleep(1);
 char buffer[4096] = { 0 };
 recv(sock, buffer, 4096, MSG_DONTWAIT);
 return sock;
}

variable_types:
username
char *

variable_types:
password
char *

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

variable_types:
buffer
char [4096]

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

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


decompiled_code:
char *net_update()
{
int v0; // ebp
char *v1; // rbx
char *v2; // rsi
__int64 v3; // rax
char s[16]; // [rsp+0h] [rbp-1018h] BYREF
char v6[4080]; // [rsp+10h] [rbp-1008h] BYREF

v0 = 0;
v1 = s;
send(sock, ":xdsl info\r\n", 0xCuLL, 0);
*(_OWORD *)s = 0LL;
memset(v6, 0, sizeof(v6));
do
{
v2 = v1++;
++v0;
recv(sock, v2, 1uLL, 0);
}
while ( *(v1 - 1) != 62 );
v3 = 0LL;
while ( s[v3] != 123 )
{
if ( ++v3 == v0 )
return strdup(&s[12]);
}
s[(int)v3 - 2] = 0;
return strdup(&s[12]);
}

canonical_decompiled_code:
char *func2()
{
int v0; // ebp
char *v1; // rbx
char *v2; // rsi
__int64 v3; // rax
char s[16]; // [rsp+0h] [rbp-1018h] BYREF
char v6[4080]; // [rsp+10h] [rbp-1008h] BYREF

v0 = 0;
v1 = s;
send(sock, ":xdsl info\r\n", 0xCuLL, 0);
*(_OWORD *)s = 0LL;
memset(v6, 0, sizeof(v6));
do
{
v2 = v1++;
++v0;
recv(sock, v2, 1uLL, 0);
}
while ( *(v1 - 1) != 62 );
v3 = 0LL;
while ( s[v3] != 123 )
{
if ( ++v3 == v0 )
return strdup(&s[12]);
}
s[(int)v3 - 2] = 0;
return strdup(&s[12]);
}

original_code:
const char *net_update()
{
 send(sock, ":xdsl info\r\n", 12, 0);
 char buffer[4096] = { 0 };
 int i = 0;
 do
 {
  recv(sock,buffer+i,1,0);
 }
 while(buffer[i++]!='>');
 int k;
 for(k = 0;k<i;k++)
 {
  if(buffer[k]=='{')
  {
   buffer[k-2] = '\0';
   break;
  }
 }
 return strdup(buffer+12);
}

variable_types: i int

variable_types: k int

return_type: char *


decompiled_code:
int net_shutdown()
{
return shutdown(sock, 0);
}

canonical_decompiled_code:
int func3()
{
return shutdown(sock, 0);
}

original_code:
void net_shutdown()
{
 shutdown(sock, 0);
}


decompiled_code:
void handle_sig()
{
puts("THE GOGGLES DO NOTHING!");
}

canonical_decompiled_code:
void func0()
{
puts("THE GOGGLES DO NOTHING!");
}

original_code:
void handle_sig(int signum)
{
 printf("THE GOGGLES DO NOTHING!\n");
}

variable_types: signum int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
signal(13, (__sighandler_t)handle_sig);
if ( argc <= 1 )
{
network_init("localhost");
}
else
{
network_init(argv[1]);
if ( argc != 2 )
{
camera_open(argv[2], 320LL, 240LL);
goto LABEL_4;
}
}
camera_open("/dev/video0", 320LL, 240LL);
LABEL_4:
while ( (unsigned __int8)update_camera() )
;
return 0;
}

original_code:
int main(int argc, char **argv)
{
 signal(13,handle_sig);
 if(argc>1)
  network_init(argv[1]);
 else
  network_init("localhost");
 if(argc>2)
  camera_open(argv[2],320, 240);
 else
  camera_open("/dev/video0",320, 240);
 while(update_camera());
 return 0;
}


buttonDown

decompiled_code:
__int64 __fastcall buttonDown(char a1)
{
return (buttonsdown & (unsigned int)(1 << a1)) >> a1;
}

canonical_decompiled_code:
__int64 __fastcall func8(char a1)
{
return (buttonsdown & (unsigned int)(1 << a1)) >> a1;
}

original_code:
int buttonDown(int buttonnum)
{
 return (buttonsdown & (1<<buttonnum))>>buttonnum;
}

variable_types: buttonnum int


decompiled_code:
time_t __fastcall take_photo(__int64 a1)
{
int v1; // ebx
char *v2; // rax
__int64 v3; // rdx
time_t result; // rax

v1 = ltime_1;
if ( !ltime_1 )
{
ltime_1 = time(0LL);
LABEL_3:
v2 = (char *)malloc(0x80uLL);
v3 = (unsigned int)img_num_0++;
sprintf(v2, "caught%03d.bmp", v3);
return bitmap_write(a1, 320LL, 240LL);
}
result = time(0LL);
if ( v1 + 2 < (int)result )
goto LABEL_3;
return result;
}

canonical_decompiled_code:
time_t __fastcall func0(__int64 a1)
{
int v1; // ebx
char *v2; // rax
__int64 v3; // rdx
time_t result; // rax

v1 = ltime_1;
if ( !ltime_1 )
{
ltime_1 = time(0LL);
LABEL_3:
v2 = (char *)malloc(0x80uLL);
v3 = (unsigned int)img_num_0++;
sprintf(v2, "caught%03d.bmp", v3);
return bitmap_write(a1, 320LL, 240LL);
}
result = time(0LL);
if ( v1 + 2 < (int)result )
goto LABEL_3;
return result;
}

original_code:
void take_photo(void *data)
{
 static int ltime = 0;
 static int img_num = 0;
 if(ltime == 0)
 {
  ltime = time(((void *)0));
 }
 else if(time(((void *)0))<ltime+3)
 {
  return;
 }
 char *filename = malloc(128);
 sprintf(filename,"caught%03d.bmp",img_num++);
 bitmap_write(data, 320, 240);
}

variable_types:
data
void *

variable_types: ltime int

variable_types:
filename
char *


decompiled_code:
__int64 update_camera()
{
__int64 v0; // rbp
int v2; // [rsp+8h] [rbp-20h] BYREF
char v3[4]; // [rsp+Ch] [rbp-1Ch] BYREF
char v4[4]; // [rsp+10h] [rbp-18h] BYREF
char v5[4]; // [rsp+14h] [rbp-14h] BYREF
void *ptr; // [rsp+18h] [rbp-10h] BYREF

v0 = camera_read_image();
manip_check_diffs(v0, &v2, v3, v4, v5);
if ( v2 != -2 )
take_photo(v0);
if ( v0 )
{
ptr = 0LL;
network_encode_data(v0, &ptr);
free(ptr);
}
return 1LL;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 v0; // rbp
int v2; // [rsp+8h] [rbp-20h] BYREF
char v3[4]; // [rsp+Ch] [rbp-1Ch] BYREF
char v4[4]; // [rsp+10h] [rbp-18h] BYREF
char v5[4]; // [rsp+14h] [rbp-14h] BYREF
void *ptr; // [rsp+18h] [rbp-10h] BYREF

v0 = camera_read_image();
manip_check_diffs(v0, &v2, v3, v4, v5);
if ( v2 != -2 )
func0(v0);
if ( v0 )
{
ptr = 0LL;
network_encode_data(v0, &ptr);
free(ptr);
}
return 1LL;
}

original_code:
char update_camera()
{
 char *data = camera_read_image();
   int x, y,x2,y2;
   manip_check_diffs(data, &x, &y, &x2, &y2);
 if(x!=-2)
 {
  take_photo(data);
 }
 if(data==((void *)0))
  return 1;
 unsigned char *send_data = ((void *)0);
 int bytes = network_encode_data(data, &send_data);
 free(send_data);
 return 1;
}

canonical_original_code:
char update_camera()
{
 char *data = camera_read_image();
   int x; int y; int x2; int y2;
   manip_check_diffs(data, &x, &y, &x2, &y2);
 if(x!=-2)
 {
  take_photo(data);
 }
 if(data==((void *)0))
  return 1;
 unsigned char *send_data = ((void *)0);
 int bytes = network_encode_data(data, &send_data);
 free(send_data);
 return 1;
}

variable_types:
data
char *

variable_types: x int

variable_types: y int

variable_types: bytes int

return_type: char


original_code:
char *generate_question()
{
 char *answer = ((void *)0);
 int matrix_a[2], matrix_b[2];
 matrix_a[0] = (rand()%4)+1;
 matrix_a[1] = (rand()%4)+1;
 matrix_b[0] = (rand()%4)+1;
 matrix_b[1] = (rand()%4)+1;
 int matrix_a_els[matrix_a[0]][matrix_a[1]];
 int matrix_b_els[matrix_b[0]][matrix_b[1]];
 int i = 0;
 int j = 0;
 int k = 0;
 for(i = 0;i<matrix_a[0];i++)
 {
  for(j = 0;j<matrix_a[1];j++)
  {
   matrix_a_els[i][j] = rand()%10;
  }
 }
 for(i = 0;i<matrix_b[0];i++)
 {
  for(j = 0;j<matrix_b[1];j++)
  {
   matrix_b_els[i][j] = rand()%10;
  }
 }
 int max = (matrix_a[0]<=matrix_b[0])?(matrix_b[0]):(matrix_a[0]);
 int chars_moved = 2;
 int cchars_moved = 2;
 for(i = 0;i<max;i++)
 {
  if(i<matrix_a[0])
  {
   printf("[ ");
   for(j=0;j<matrix_a[1];j++)
   {
    printf("%d ",matrix_a_els[i][j]);
    cchars_moved += 2;
   }
   printf("] ");
   cchars_moved += 2;
   if(cchars_moved>chars_moved)
   {
    chars_moved = cchars_moved;
   }
  }
  else
  {
   for(k=0;k<chars_moved;k++,printf(" "));
  }
  if(i<matrix_b[0])
  {
   printf("[ ");
   for(j=0;j<matrix_b[1];j++)
   {
    printf("%d ",matrix_b_els[i][j]);
   }
   printf("]");
  }
  printf("\n");
  cchars_moved = 0;
 }
 while(fgetc(stdin)<0);
 printf("\nAnswer:");
 if(matrix_a[0]!=matrix_b[1])
 {
  printf("\nImpossible to multiply matrices, matrix A doesn't have the same amount of columns as matrix B has rows\n");
 }
 else
 {
  printf("Answer matrix size %dx%d\n\n",matrix_a[1],matrix_b[0]);
  int answer_matrix[matrix_a[0]][matrix_b[1]];
  for(i = 0;i<matrix_a[0];i++)
  {
   for(j=0;j<matrix_b[0];j++)
   {
    int ans = 0;
    for(k = 0; k<matrix_a[1];k++)
    {
     ans += matrix_a_els[i][k]*matrix_b_els[k][j];
    }
    answer_matrix[i][j] = ans;
   }
  }
  answer = malloc(1024);
  memset(answer, '\0', 1024);
  for(i=0;i<matrix_a[0];i++)
  {
   printf("\n[ ");
   for(j=0;j<matrix_b[1];j++)
   {
    printf("%d ",answer_matrix[i][j]);
   }
   printf("]");
  }
  printf("\n");
 }
 return ((void *)0);
}

canonical_original_code:
char *generate_question()
{
 char *answer = ((void *)0);
 int matrix_a[2]; int matrix_b[2];
 matrix_a[0] = (rand()%4)+1;
 matrix_a[1] = (rand()%4)+1;
 matrix_b[0] = (rand()%4)+1;
 matrix_b[1] = (rand()%4)+1;
 int matrix_a_els[-1][-1];
 int matrix_b_els[-1][-1];
 int i = 0;
 int j = 0;
 int k = 0;
 for(i = 0;i<matrix_a[0];i++)
 {
  for(j = 0;j<matrix_a[1];j++)
  {
   matrix_a_els[i][j] = rand()%10;
  }
 }
 for(i = 0;i<matrix_b[0];i++)
 {
  for(j = 0;j<matrix_b[1];j++)
  {
   matrix_b_els[i][j] = rand()%10;
  }
 }
 int max = (matrix_a[0]<=matrix_b[0])?(matrix_b[0]):(matrix_a[0]);
 int chars_moved = 2;
 int cchars_moved = 2;
 for(i = 0;i<max;i++)
 {
  if(i<matrix_a[0])
  {
   printf("[ ");
   for(j=0;j<matrix_a[1];j++)
   {
    printf("%d ",matrix_a_els[i][j]);
    cchars_moved += 2;
   }
   printf("] ");
   cchars_moved += 2;
   if(cchars_moved>chars_moved)
   {
    chars_moved = cchars_moved;
   }
  }
  else
  {
   for(k=0;k<chars_moved;k++,printf(" "));
  }
  if(i<matrix_b[0])
  {
   printf("[ ");
   for(j=0;j<matrix_b[1];j++)
   {
    printf("%d ",matrix_b_els[i][j]);
   }
   printf("]");
  }
  printf("\n");
  cchars_moved = 0;
 }
 while(fgetc(stdin)<0);
 printf("\nAnswer:");
 if(matrix_a[0]!=matrix_b[1])
 {
  printf("\nImpossible to multiply matrices, matrix A doesn't have the same amount of columns as matrix B has rows\n");
 }
 else
 {
  printf("Answer matrix size %dx%d\n\n",matrix_a[1],matrix_b[0]);
  int answer_matrix[-1][-1];
  for(i = 0;i<matrix_a[0];i++)
  {
   for(j=0;j<matrix_b[0];j++)
   {
    int ans = 0;
    for(k = 0; k<matrix_a[1];k++)
    {
     ans += matrix_a_els[i][k]*matrix_b_els[k][j];
    }
    answer_matrix[i][j] = ans;
   }
  }
  answer = malloc(1024);
  memset(answer, '\0', 1024);
  for(i=0;i<matrix_a[0];i++)
  {
   printf("\n[ ");
   for(j=0;j<matrix_b[1];j++)
   {
    printf("%d ",answer_matrix[i][j]);
   }
   printf("]");
  }
  printf("\n");
 }
 return ((void *)0);
}

variable_types:
answer
char *

variable_types: j int

variable_types: max int

variable_types: ans int


main

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

puts("Matrix Multiplication Revision - by Feesh!\n");
puts("Let's start- Questions will appear, you will need");
puts("to answer them on a piece of paper, as you cannot");
puts("input your answer directly into the program.\n");
puts("You will receive a question, and then you will");
puts("have to hit the return key to see the answer!\n");
puts("Press CTRL+C to exit the program\n");
v3 = time(0LL);
v4 = (FILE *)v3;
srand(v3);
while ( 1 )
{
generate_question(v4, argv);
do
v4 = _bss_start;
while ( fgetc(_bss_start) < 0 );
}
}

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

puts("Matrix Multiplication Revision - by Feesh!\n");
puts("Let's start- Questions will appear, you will need");
puts("to answer them on a piece of paper, as you cannot");
puts("input your answer directly into the program.\n");
puts("You will receive a question, and then you will");
puts("have to hit the return key to see the answer!\n");
puts("Press CTRL+C to exit the program\n");
v3 = time(0LL);
v4 = (FILE *)v3;
srand(v3);
while ( 1 )
{
func7(v4, argv);
do
v4 = _bss_start;
while ( fgetc(_bss_start) < 0 );
}
}

original_code:
int main(int argc, char **argv)
{
 printf("Matrix Multiplication Revision - by Feesh!\n\n");
 printf("Let's start- Questions will appear, you will need\n");
 printf("to answer them on a piece of paper, as you cannot\n");
 printf("input your answer directly into the program.\n\n");
 printf("You will receive a question, and then you will\n");
 printf("have to hit the return key to see the answer!\n\n");
 printf("Press CTRL+C to exit the program\n\n");
 srand(time(((void *)0)));
 for(;;)
 {
  generate_question();
  while(fgetc(stdin)<0);
 }
 return 0;
}

variable_types: argc int

variable_types:
argv
char **


decompiled_code:
__int64 __fastcall controls_get_max_gain(int a1)
{
__int64 result; // rax
__int128 v2[2]; // [rsp+0h] [rbp-58h] BYREF
__int128 v3; // [rsp+20h] [rbp-38h]
__int128 v4; // [rsp+30h] [rbp-28h]
int v5; // [rsp+40h] [rbp-18h]

v2[0] = 0LL;
v5 = 0;
LODWORD(v2[0]) = 9963795;
v2[1] = 0LL;
v3 = 0LL;
v4 = 0LL;
if ( ioctl(a1, 0xC0445624uLL, v2) )
{
perror("ioctl VIDIOC_QUERYCTRL failed for V4L2_CID_GAIN");
result = HIDWORD(v3);
}
else
{
if ( (BYTE8(v4) & 1) != 0 )
_assert_fail("!(queryctrl.flags & V4L2_CTRL_FLAG_DISABLED)", "controls.c", 0x15u, "controls_get_max_gain");
result = HIDWORD(v3);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
__int64 result; // rax
__int128 v2[2]; // [rsp+0h] [rbp-58h] BYREF
__int128 v3; // [rsp+20h] [rbp-38h]
__int128 v4; // [rsp+30h] [rbp-28h]
int v5; // [rsp+40h] [rbp-18h]

v2[0] = 0LL;
v5 = 0;
LODWORD(v2[0]) = 9963795;
v2[1] = 0LL;
v3 = 0LL;
v4 = 0LL;
if ( ioctl(a1, 0xC0445624uLL, v2) )
{
perror("ioctl VIDIOC_QUERYCTRL failed for V4L2_CID_GAIN");
result = HIDWORD(v3);
}
else
{
if ( (BYTE8(v4) & 1) != 0 )
_assert_fail("!(queryctrl.flags & V4L2_CTRL_FLAG_DISABLED)", "controls.c", 0x15u, "controls_get_max_gain");
result = HIDWORD(v3);
}
return result;
}

original_code:
int controls_get_max_gain(int fd)
{
 printf("Stub get max gain\n");
 return 0;
}

variable_types: fd int


decompiled_code:
__int64 __fastcall controls_get_gain(int a1)
{
int v2; // [rsp+8h] [rbp-10h] BYREF
unsigned int v3; // [rsp+Ch] [rbp-Ch]

v3 = 0;
v2 = 9963795;
if ( ioctl(a1, 0xC008561BuLL, &v2) )
perror("VIDIOC_G_CTRL");
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
int v2; // [rsp+8h] [rbp-10h] BYREF
unsigned int v3; // [rsp+Ch] [rbp-Ch]

v3 = 0;
v2 = 9963795;
if ( ioctl(a1, 0xC008561BuLL, &v2) )
perror("VIDIOC_G_CTRL");
return v3;
}

original_code:
int controls_get_gain(int fd)
{
 printf("Stub get gain\n");
 return 0;
}


decompiled_code:
int __fastcall controls_set_gain(int a1, unsigned int a2)
{
int result; // eax
unsigned int v3; // [rsp+8h] [rbp-10h] BYREF
unsigned int v4; // [rsp+Ch] [rbp-Ch]

v4 = a2;
v3 = 9963795;
result = ioctl(a1, 0xC008561CuLL, &v3);
if ( result == -1 )
{
perror("VIDEOC_S_CTRL");
fprintf(stderr, "control.id=%x\n", v3);
result = fprintf(stderr, "control.value=%d\n", v4);
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(int a1, unsigned int a2)
{
int result; // eax
unsigned int v3; // [rsp+8h] [rbp-10h] BYREF
unsigned int v4; // [rsp+Ch] [rbp-Ch]

v4 = a2;
v3 = 9963795;
result = ioctl(a1, 0xC008561CuLL, &v3);
if ( result == -1 )
{
perror("VIDEOC_S_CTRL");
fprintf(stderr, "control.id=%x\n", v3);
result = fprintf(stderr, "control.value=%d\n", v4);
}
return result;
}

original_code:
void controls_set_gain(int fd, int v)
{
 printf("Stub set gain\n");
}

variable_types: v int


decompiled_code:
__int64 __fastcall controls_get_jpegcompr(int a1)
{
unsigned int v2[38]; // [rsp+10h] [rbp-98h] BYREF

if ( ioctl(a1, 0x808C563DuLL, v2) != -1 )
return v2[0];
perror("VIDIOC_G_JPEGCOMP");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func17(int a1)
{
unsigned int v2[38]; // [rsp+10h] [rbp-98h] BYREF

if ( ioctl(a1, 0x808C563DuLL, v2) != -1 )
return v2[0];
perror("VIDIOC_G_JPEGCOMP");
return 0xFFFFFFFFLL;
}

original_code:
int controls_get_jpegcompr(int fd)
{
  struct v4l2_jpegcompression compr;
  int rv=ioctl(fd, (((2U) << (((0 +8)+8)+14)) | ((('V')) << (0 +8)) | (((61)) << 0) | ((((sizeof(struct v4l2_jpegcompression)))) << ((0 +8)+8))), &compr);
  if (rv==-1)
  {
    perror("VIDIOC_G_JPEGCOMP");
    return -1;
  }
  return compr.quality;
}

canonical_original_code:
int controls_get_jpegcompr(int fd)
{
  struct v4l2_jpegcompression compr;
  int rv = ioctl(fd, (((2U) << (((0 +8)+8)+14)) | ((('V')) << (0 +8)) | (((61)) << 0) | ((((sizeof(struct v4l2_jpegcompression)))) << ((0 +8)+8))), &compr);
  if (rv==-1)
  {
    perror("VIDIOC_G_JPEGCOMP");
    return -1;
  }
  return compr.quality;
}

variable_types: struct v4l2_jpegcompression { int quality; int APPn; int APP_len; char APP_data[60]; int COM_len; char COM_data[60]; unsigned int jpeg_markers; }

variable_types: rv int


decompiled_code:
__int64 __fastcall controls_get_max_gain(int a1)
{
__int64 result; // rax
__int128 v2[2]; // [rsp+0h] [rbp-58h] BYREF
__int128 v3; // [rsp+20h] [rbp-38h]
__int128 v4; // [rsp+30h] [rbp-28h]
int v5; // [rsp+40h] [rbp-18h]

v2[0] = 0LL;
v5 = 0;
LODWORD(v2[0]) = 9963795;
v2[1] = 0LL;
v3 = 0LL;
v4 = 0LL;
if ( ioctl(a1, 0xC0445624uLL, v2) )
{
perror("ioctl VIDIOC_QUERYCTRL failed for V4L2_CID_GAIN");
result = HIDWORD(v3);
}
else
{
if ( (BYTE8(v4) & 1) != 0 )
_assert_fail("!(queryctrl.flags & V4L2_CTRL_FLAG_DISABLED)", "controls.c", 0x5Fu, "controls_get_max_gain");
result = HIDWORD(v3);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
__int64 result; // rax
__int128 v2[2]; // [rsp+0h] [rbp-58h] BYREF
__int128 v3; // [rsp+20h] [rbp-38h]
__int128 v4; // [rsp+30h] [rbp-28h]
int v5; // [rsp+40h] [rbp-18h]

v2[0] = 0LL;
v5 = 0;
LODWORD(v2[0]) = 9963795;
v2[1] = 0LL;
v3 = 0LL;
v4 = 0LL;
if ( ioctl(a1, 0xC0445624uLL, v2) )
{
perror("ioctl VIDIOC_QUERYCTRL failed for V4L2_CID_GAIN");
result = HIDWORD(v3);
}
else
{
if ( (BYTE8(v4) & 1) != 0 )
_assert_fail("!(queryctrl.flags & V4L2_CTRL_FLAG_DISABLED)", "controls.c", 0x5Fu, "controls_get_max_gain");
result = HIDWORD(v3);
}
return result;
}

canonical_original_code:
int controls_get_max_gain(int fd)
{
 printf("Stub get max gain\n");
 return 0;
}


canonical_decompiled_code:
__int64 __fastcall func3(int a1)
{
int v2; // [rsp+8h] [rbp-10h] BYREF
unsigned int v3; // [rsp+Ch] [rbp-Ch]

v3 = 0;
v2 = 9963795;
if ( ioctl(a1, 0xC008561BuLL, &v2) )
perror("VIDIOC_G_CTRL");
return v3;
}

canonical_original_code:
int controls_get_gain(int fd)
{
 printf("Stub get gain\n");
 return 0;
}


canonical_decompiled_code:
int __fastcall func4(int a1, unsigned int a2)
{
int result; // eax
unsigned int v3; // [rsp+8h] [rbp-10h] BYREF
unsigned int v4; // [rsp+Ch] [rbp-Ch]

v4 = a2;
v3 = 9963795;
result = ioctl(a1, 0xC008561CuLL, &v3);
if ( result == -1 )
{
perror("VIDEOC_S_CTRL");
fprintf(stderr, "control.id=%x\n", v3);
result = fprintf(stderr, "control.value=%d\n", v4);
}
return result;
}

canonical_original_code:
void controls_set_gain(int fd, int v)
{
 printf("Stub set gain\n");
}


canonical_decompiled_code:
__int64 __fastcall func9(int a1)
{
unsigned int v2[38]; // [rsp+10h] [rbp-98h] BYREF

if ( ioctl(a1, 0x808C563DuLL, v2) != -1 )
return v2[0];
perror("VIDIOC_G_JPEGCOMP");
return 0xFFFFFFFFLL;
}

user_defined_types: struct v4l2_jpegcompression { int quality; int APPn; int APP_len; char APP_data[60]; int COM_len; char COM_data[60]; unsigned int jpeg_markers; }


original_code:
void colour_convert(double H, double S, double V, double *R, double *G, double *B)
{
 if( V == 0 )
 {
  *R = 0;
  *G = 0;
  *B = 0;
 }
 else if( S == 0 )
 {
  *R = V;
  *G = V;
  *B = V;
 }
 else
 {
  const double hf = H / 60.0;
  const int i = (int) floor( hf );
  const double f = hf - i;
  const double pv = V * ( 1 - S );
  const double qv = V * ( 1 - S * f );
  const double tv = V * ( 1 - S * ( 1 - f ) );
  switch( i )
  {
   case 0:
    *R = V;
    *G = tv;
    *B = pv;
    break;
   case 1:
    *R = qv;
    *G = V;
    *B = pv;
    break;
   case 2:
    *R = pv;
    *G = V;
    *B = tv;
    break;
   case 3:
    *R = pv;
    *G = qv;
    *B = V;
    break;
   case 4:
    *R = tv;
    *G = pv;
    *B = V;
    break;
   case 5:
    *R = V;
    *G = pv;
    *B = qv;
    break;
   case 6:
    *R = V;
    *G = tv;
    *B = pv;
    break;
   case -1:
    *R = V;
    *G = pv;
    *B = qv;
    break;
   default:
    fprintf(stderr,"i Value error in Pixel conversion, Value is %d\n",i);
    break;
  }
 }
 *R *= 255.0F;
 *G *= 255.0F;
 *B *= 255.0F;
}

canonical_original_code:
void colour_convert(double H, double S, double V, double *R, double *G, double *B)
{
 if( V == 0 )
 {
  *R = 0;
  *G = 0;
  *B = 0;
 }
 else if( S == 0 )
 {
  *R = V;
  *G = V;
  *B = V;
 }
 else
 {
  double hf = H / 60.0;
  int i = (int) floor( hf );
  double f = hf - i;
  double pv = V * ( 1 - S );
  double qv = V * ( 1 - S * f );
  double tv = V * ( 1 - S * ( 1 - f ) );
  switch( i )
  {
   case 0:
    *R = V;
    *G = tv;
    *B = pv;
    break;
   case 1:
    *R = qv;
    *G = V;
    *B = pv;
    break;
   case 2:
    *R = pv;
    *G = V;
    *B = tv;
    break;
   case 3:
    *R = pv;
    *G = qv;
    *B = V;
    break;
   case 4:
    *R = tv;
    *G = pv;
    *B = V;
    break;
   case 5:
    *R = V;
    *G = pv;
    *B = qv;
    break;
   case 6:
    *R = V;
    *G = tv;
    *B = pv;
    break;
   case -1:
    *R = V;
    *G = pv;
    *B = qv;
    break;
   default:
    fprintf(stderr,"i Value error in Pixel conversion, Value is %d\n",i);
    break;
  }
 }
 *R *= 255.0F;
 *G *= 255.0F;
 *B *= 255.0F;
}

variable_types: H double

variable_types: S double

variable_types: V double

variable_types:
R
double *

variable_types:
G
double *

variable_types:
B
double *

variable_types: hf double

variable_types: i int

variable_types: f double

variable_types: pv double

variable_types: qv double

variable_types: tv double


decompiled_code:
__int64 window_show()
{
__int64 v0; // rax

v0 = window_create();
return gtk_widget_show_all(v0);
}

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

v0 = func6();
return gtk_widget_show_all(v0);
}

original_code:
void window_show()
{
 gtk_widget_show_all(window_create());
}


decompiled_code:
__int64 __fastcall update_camera(__int64 a1)
{
__int64 v1; // rax
__int64 v2; // rbx
__int64 v3; // rax
__int64 v5[4]; // [rsp+8h] [rbp-20h] BYREF

v1 = camera_read_image();
if ( v1 )
{
v2 = v1;
v5[0] = 0LL;
network_encode_data(v1, v5);
g_free(v5[0]);
v3 = gdk_gc_new(*(_QWORD *)(a1 + 80));
gdk_draw_rgb_image(*(_QWORD *)(a1 + 80), v3, 0LL, 0LL, 320LL, 240LL, 0LL, v2, 960LL);
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
__int64 v1; // rax
__int64 v2; // rbx
__int64 v3; // rax
__int64 v5[4]; // [rsp+8h] [rbp-20h] BYREF

v1 = camera_read_image();
if ( v1 )
{
v2 = v1;
v5[0] = 0LL;
network_encode_data(v1, v5);
g_free(v5[0]);
v3 = gdk_gc_new(*(_QWORD *)(a1 + 80));
gdk_draw_rgb_image(*(_QWORD *)(a1 + 80), v3, 0LL, 0LL, 320LL, 240LL, 0LL, v2, 960LL);
}
return 1LL;
}

variable_types:
data
char *

variable_types: x int

variable_types: y int

variable_types: bytes int


repo: ElFeesho/nonogram

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

v3 = time(0LL);
v4 = v3;
srand(v3);
gameboard_init(v4, argv);
while ( (unsigned int)gameboard_process_input() )
gameboard_render();
gameboard_deinit();
return 0;
}

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

v3 = time(0LL);
v4 = v3;
srand(v3);
func14(v4, argv);
while ( (unsigned int)func17() )
func16();
func15();
return 0;
}

original_code:
int main(int argc, char **argv)
{
 srand(time(((void *)0)));
 gameboard_init();
 while(gameboard_process_input())
 {
  gameboard_render();
 }
 gameboard_deinit();
 return 0;
}

variable_types: argc int

variable_types:
argv
char **


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

image_generate(10LL, 10LL);
result = 0LL;
memset(gameboard, 0, 0x60uLL);
gameboard[24] = 0;
return result;
}

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

func18(10LL, 10LL);
result = 0LL;
memset(gameboard, 0, 0x60uLL);
gameboard[24] = 0;
return result;
}

original_code:
void reset_board(void)
{
 image_generate(10, 10);
 int x, y;
 for(y = 0;y<10;y++)
 {
  for(x = 0;x<10;x++)
  {
   gameboard[y][x] = 0;
  }
 }
}

canonical_original_code:
void reset_board()
{
 image_generate(10, 10);
 int x; int y;
 for(y = 0;y<10;y++)
 {
  for(x = 0;x<10;x++)
  {
   gameboard[y][x] = 0;
  }
 }
}


decompiled_code:
__int64 put_pixel()
{
return SDL_FillRect();
}

canonical_decompiled_code:
__int64 func8()
{
return SDL_FillRect();
}

original_code:
void put_pixel(int x, int y, unsigned int col)
{
 SDL_Rect pos;
 pos.x = x;
 pos.y = y;
 pos.w = 1;
 pos.h = 1;
 SDL_FillRect(screen, &pos, col);
}

canonical_original_code:
void put_pixel(int x, int y, unsigned int col)
{
 struct SDL_Rect pos;
 pos.x = x;
 pos.y = y;
 pos.w = 1;
 pos.h = 1;
 SDL_FillRect(screen, &pos, col);
}

variable_types:
col
unsigned int

variable_types: struct SDL_Rect { short x; unsigned short w; }


decompiled_code:
__int64 draw_highlight()
{
SDL_MapRGB();
return SDL_FillRect();
}

canonical_decompiled_code:
__int64 func9()
{
SDL_MapRGB();
return SDL_FillRect();
}

original_code:
void draw_highlight(void)
{
 SDL_Rect t;
 t.x = stage_x+sel_x*24+1;
 t.y = stage_y+sel_y*24+1;
 t.w = 23;
 t.h = 23;
 SDL_FillRect(screen, &t, SDL_MapRGB(screen->format,0,128,255));
}

canonical_original_code:
void draw_highlight()
{
 struct SDL_Rect t;
 t.x = stage_x+sel_x*24+1;
 t.y = stage_y+sel_y*24+1;
 t.w = 23;
 t.h = 23;
 SDL_FillRect(screen, &t, SDL_MapRGB(screen->format,0,128,255));
}

user_defined_types: struct SDL_Rect { short x; unsigned short w; }


decompiled_code:
__int64 draw_gameboard()
{
_DWORD *v0; // r13
__int16 v1; // r12
_BYTE *v2; // rbp
int v3; // ebx
__int64 result; // rax

v0 = gameboard;
v1 = 63;
do
{
v2 = v0;
v3 = 123;
do
{
while ( *v2 != 1 )
{
v3 += 24;
++v2;
if ( (_WORD)v3 == 363 )
goto LABEL_6;
}
v3 += 24;
++v2;
SDL_MapRGB();
result = SDL_FillRect();
}
while ( (_WORD)v3 != 363 );
LABEL_6:
v0 = (_DWORD *)((char *)v0 + 10);
v1 += 24;
}
while ( v0 != (_DWORD *)&unk_404124 );
return result;
}

original_code:
void draw_gameboard(void)
{
 int x, y;
 for(y = 0;y<10;y++)
 {
  for(x = 0;x<10;x++)
  {
   if(gameboard[y][x] == 1)
   {
    SDL_Rect t;
    t.x = stage_x+x*24+3;
    t.y = stage_y+y*24+3;
    t.w = 19;
    t.h = 19;
    SDL_FillRect(screen, &t, SDL_MapRGB(screen->format,0,0,0));
   }
  }
 }
}

canonical_original_code:
void draw_gameboard()
{
 int x; int y;
 for(y = 0;y<10;y++)
 {
  for(x = 0;x<10;x++)
  {
   if(gameboard[y][x] == 1)
   {
    struct SDL_Rect t;
    t.x = stage_x+x*24+3;
    t.y = stage_y+y*24+3;
    t.w = 19;
    t.h = 19;
    SDL_FillRect(screen, &t, SDL_MapRGB(screen->format,0,0,0));
   }
  }
 }
}


decompiled_code:
char __fastcall do_move(int a1, unsigned int a2)
{
__int64 v2; // rax
_BYTE *v3; // rdx

v2 = image_get_row(a2);
if ( *(_DWORD *)(v2 + 4LL * a1) == 1 )
{
v3 = (char *)gameboard + 10 * (int)a2 + a1;
LOBYTE(v2) = 1 - *v3;
*v3 = v2;
}
return v2;
}

canonical_decompiled_code:
char __fastcall func11(int a1, unsigned int a2)
{
__int64 v2; // rax
_BYTE *v3; // rdx

v2 = func19(a2);
if ( *(_DWORD *)(v2 + 4LL * a1) == 1 )
{
v3 = (char *)gameboard + 10 * (int)a2 + a1;
LOBYTE(v2) = 1 - *v3;
*v3 = v2;
}
return v2;
}

original_code:
void do_move(int x, int y)
{
 int *row = image_get_row(y);
 if(row[x] == 1)
  gameboard[y][x] = 1-gameboard[y][x];
}

variable_types:
row
int *


decompiled_code:
__int64 __fastcall draw_number(int a1, unsigned int a2, int a3)
{
int v3; // er14
__int64 result; // rax
int v5; // ebp
unsigned __int64 v7; // r15
unsigned __int64 v8; // r13
__int64 i; // r12
unsigned int v10; // eax

v3 = a3;
result = (unsigned int)(int)(log10((double)a3) + 1.0);
v5 = a1 + 5 * result;
if ( (int)result > 0 )
{
do
{
v7 = 100LL * (v3 % 10);
v8 = v7 + 100;
do
{
for ( i = 0LL; i != 5; ++i )
{
if ( num_font[i + v7 / 4] == 1 )
{
v10 = SDL_MapRGB();
put_pixel((unsigned int)(v5 + i), a2, v10);
}
}
v7 += 20LL;
++a2;
}
while ( v7 != v8 );
v5 -= 5;
result = (unsigned int)(v3 >> 31);
v3 /= 10;
}
while ( a1 != v5 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(int a1, unsigned int a2, int a3)
{
int v3; // er14
__int64 result; // rax
int v5; // ebp
unsigned __int64 v7; // r15
unsigned __int64 v8; // r13
__int64 i; // r12
unsigned int v10; // eax

v3 = a3;
result = (unsigned int)(int)(log10((double)a3) + 1.0);
v5 = a1 + 5 * result;
if ( (int)result > 0 )
{
do
{
v7 = 100LL * (v3 % 10);
v8 = v7 + 100;
do
{
for ( i = 0LL; i != 5; ++i )
{
if ( num_font[i + v7 / 4] == 1 )
{
v10 = SDL_MapRGB();
func8((unsigned int)(v5 + i), a2, v10);
}
}
v7 += 20LL;
++a2;
}
while ( v7 != v8 );
v5 -= 5;
result = (unsigned int)(v3 >> 31);
v3 /= 10;
}
while ( a1 != v5 );
}
return result;
}

original_code:
void draw_number(int x, int y, int num)
{
 int ix, iy;
 int n = -1;
 int t = log10(num)+1;
 while(t>0)
 {
  n = num%10;
  for(iy = 0;iy<5;iy++)
  {
   for(ix = 0;ix<5;ix++)
   {
    if(num_font[n][iy][ix] == 1)
    {
     put_pixel(x+t*5+ix,y+iy,SDL_MapRGB(screen->format,0,0,0));
    }
   }
  }
  num /= 10;
  t--;
 }
}

canonical_original_code:
void draw_number(int x, int y, int num)
{
 int ix; int iy;
 int n = -1;
 int t = log10(num)+1;
 while(t>0)
 {
  n = num%10;
  for(iy = 0;iy<5;iy++)
  {
   for(ix = 0;ix<5;ix++)
   {
    if(num_font[n][iy][ix] == 1)
    {
     put_pixel(x+t*5+ix,y+iy,SDL_MapRGB(screen->format,0,0,0));
    }
   }
  }
  num /= 10;
  t--;
 }
}

variable_types: num int

variable_types: ix int

variable_types: iy int

variable_types: n int

variable_types: t int


canonical_original_code:
void draw_grid()
{
 int x; int y;
 for(y = 0;y<=10;y++)
 {
  struct SDL_Rect t;
  t.x = stage_x+1;
  t.y = stage_y+y*24;
  t.w = 10*24-1;
  t.h = 1;
  SDL_FillRect(screen, &t, 0);
 }
 for(x = 0;x<=10;x++)
 {
  struct SDL_Rect t;
  t.x = stage_x+x*24;
  t.y = stage_y+1;
  t.w = 1;
  t.h = 10*24-1;
  SDL_FillRect(screen, &t, 0);
 }
 for(y = 0;y<10;y++)
 {
  int *img_row = image_get_row(y);
  int cx = 0;
  int f = 0;
  for(x = 10 -1;x>=0;x--)
  {
   if(img_row[x] == 1)
   {
    cx++;
   }
   else
   {
    if(cx>0)
    {
     draw_number((stage_x-12)-(f*10),stage_y+y*24+10,cx);
     cx = 0;
     f++;
    }
   }
  }
  if(f == 0 || cx!=0)
  {
   draw_number((stage_x-12)-(f*10),stage_y+y*24+10,cx);
  }
 }
 for(x = 0;x<10;x++)
 {
  int cy = 0;
  int f = 0;
  for(y = 10 -1;y>=0;y--)
  {
   int *img_row = image_get_row(y);
   if(img_row[x] == 1)
   {
    cy++;
   }
   else
   {
    if(cy>0)
    {
     draw_number(stage_x+x*24+5, (stage_y-12)-(f*10),cy);
     cy = 0;
     f++;
    }
   }
  }
  if(f == 0 || cy!=0)
  {
   draw_number(stage_x+x*24+5, (stage_y-12)-(f*10),cy);
  }
 }
}

variable_types: cx int

variable_types: f int

variable_types: cy int


decompiled_code:
__int64 gameboard_init()
{
SDL_Init();
screen = SDL_SetVideoMode();
return image_generate(10LL, 10LL);
}

canonical_decompiled_code:
__int64 func14()
{
SDL_Init();
screen = SDL_SetVideoMode();
return func18(10LL, 10LL);
}

original_code:
void gameboard_init(void)
{
 SDL_Init(0x00000020);
 screen = SDL_SetVideoMode(480,360,0,0x00000000);
 image_generate(10, 10);
}

canonical_original_code:
void gameboard_init()
{
 SDL_Init(0x00000020);
 screen = SDL_SetVideoMode(480,360,0,0x00000000);
 image_generate(10, 10);
}


decompiled_code:
__int64 gameboard_deinit()
{
image_destroy();
SDL_FreeSurface();
return SDL_Quit();
}

canonical_decompiled_code:
__int64 func15()
{
func20();
SDL_FreeSurface();
return SDL_Quit();
}

original_code:
void gameboard_deinit(void)
{
 image_destroy();
 SDL_FreeSurface(screen);
 SDL_Quit();
}

canonical_original_code:
void gameboard_deinit()
{
 image_destroy();
 SDL_FreeSurface(screen);
 SDL_Quit();
}


decompiled_code:
__int64 gameboard_render()
{
__int64 v0; // rdi

SDL_MapRGB();
v0 = screen;
SDL_FillRect();
draw_grid(v0, 0LL);
draw_highlight();
draw_gameboard();
return SDL_Flip();
}

canonical_decompiled_code:
__int64 func16()
{
__int64 v0; // rdi

SDL_MapRGB();
v0 = screen;
SDL_FillRect();
func13(v0, 0LL);
func9();
func10();
return SDL_Flip();
}

original_code:
void gameboard_render(void)
{
 SDL_FillRect(screen, ((void *)0), SDL_MapRGB(screen->format,255,255,255));
 draw_grid();
 draw_highlight();
 draw_gameboard();
 SDL_Flip(screen);
}

canonical_original_code:
void gameboard_render()
{
 SDL_FillRect(screen, ((void *)0), SDL_MapRGB(screen->format,255,255,255));
 draw_grid();
 draw_highlight();
 draw_gameboard();
 SDL_Flip(screen);
}


decompiled_code:
unsigned __int64 __fastcall image_generate(int a1, int a2)
{
unsigned __int64 result; // rax
_QWORD *v3; // r14
_QWORD *v4; // rbp
unsigned __int64 v5; // rbx
__int64 v6; // rbx
__int64 v7; // r13

result = (unsigned __int64)malloc(8LL * a2);
imh = a2;
image = (void *)result;
if ( a2 > 0 )
{
v3 = (_QWORD *)result;
v4 = (_QWORD *)(result + 8LL * (unsigned int)a2);
v5 = result;
do
{
v5 += 8LL;
*(_QWORD *)(v5 - 8) = malloc(8LL * a1);
}
while ( (_QWORD *)v5 != v4 );
result = (unsigned int)(a1 - 1);
v6 = 4 * result + 4;
do
{
v7 = 0LL;
if ( a1 > 0 )
{
do
{
result = (unsigned int)(rand() % 2);
*(_DWORD *)(*v3 + v7) = result;
v7 += 4LL;
}
while ( v6 != v7 );
}
++v3;
}
while ( v3 != v4 );
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func18(int a1, int a2)
{
unsigned __int64 result; // rax
_QWORD *v3; // r14
_QWORD *v4; // rbp
unsigned __int64 v5; // rbx
__int64 v6; // rbx
__int64 v7; // r13

result = (unsigned __int64)malloc(8LL * a2);
imh = a2;
image = (void *)result;
if ( a2 > 0 )
{
v3 = (_QWORD *)result;
v4 = (_QWORD *)(result + 8LL * (unsigned int)a2);
v5 = result;
do
{
v5 += 8LL;
*(_QWORD *)(v5 - 8) = malloc(8LL * a1);
}
while ( (_QWORD *)v5 != v4 );
result = (unsigned int)(a1 - 1);
v6 = 4 * result + 4;
do
{
v7 = 0LL;
if ( a1 > 0 )
{
do
{
result = (unsigned int)(rand() % 2);
*(_DWORD *)(*v3 + v7) = result;
v7 += 4LL;
}
while ( v6 != v7 );
}
++v3;
}
while ( v3 != v4 );
}
return result;
}

original_code:
void image_generate(int xs, int ys)
{
 image = malloc(sizeof(int*)*ys);
 imw = xs;
 imh = ys;
 int x;
 int y;
 for(y = 0;y<ys;y++)
 {
  image[y] = malloc(sizeof(int*)*xs);
 }
 for(y = 0;y<ys;y++)
 {
  for(x = 0;x<xs;x++)
  {
   image[y][x] = rand()%2;
  }
 }
}

canonical_original_code:
void image_generate(int xs, int ys)
{
 image = malloc(sizeof(int *)*ys);
 imw = xs;
 imh = ys;
 int x;
 int y;
 for(y = 0;y<ys;y++)
 {
  image[y] = malloc(sizeof(int *)*xs);
 }
 for(y = 0;y<ys;y++)
 {
  for(x = 0;x<xs;x++)
  {
   image[y][x] = rand()%2;
  }
 }
}

variable_types: xs int

variable_types: ys int


decompiled_code:
__int64 __fastcall image_get_row(int a1)
{
return *((_QWORD *)image + a1);
}

canonical_decompiled_code:
__int64 __fastcall func19(int a1)
{
return *((_QWORD *)image + a1);
}

original_code:
int *image_get_row(int r)
{
 return image[r];
}

variable_types: r int

return_type: int *


decompiled_code:
void image_destroy()
{
void *v0; // r12
void **v1; // rbx
void **v2; // rbp
void *v3; // rdi

v0 = image;
if ( imh > 0 )
{
v1 = (void **)image;
v2 = (void **)((char *)image + 8 * (unsigned int)(imh - 1) + 8);
do
{
v3 = *v1++;
free(v3);
}
while ( v1 != v2 );
}
free(v0);
}

canonical_decompiled_code:
void func20()
{
void *v0; // r12
void **v1; // rbx
void **v2; // rbp
void *v3; // rdi

v0 = image;
if ( imh > 0 )
{
v1 = (void **)image;
v2 = (void **)((char *)image + 8 * (unsigned int)(imh - 1) + 8);
do
{
v3 = *v1++;
free(v3);
}
while ( v1 != v2 );
}
free(v0);
}

original_code:
void image_destroy()
{
 int y;
 for(y = 0;y<imh;y++)
 {
  free(image[y]);
 }
 free(image);
}

variable_types: y int


initICreateCom

repo: Elektordi/robotcite

decompiled_code:
__int64 __fastcall initICreateCom(const char *a1)
{
__int64 result; // rax
int *v2; // rax
char *v3; // rax

result = initSerial(a1, 57600LL, 8LL, 1LL, 0LL);
iCreateCom = result;
if ( (_DWORD)result == -1 )
{
v2 = _errno_location();
v3 = strerror(*v2);
printf("*** Unable to open %s at 57600 bauds (8n1) [%s]\n", a1, v3);
abort();
}
return result;
}

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

result = initSerial(a1, 57600LL, 8LL, 1LL, 0LL);
iCreateCom = result;
if ( (_DWORD)result == -1 )
{
v2 = _errno_location();
v3 = strerror(*v2);
printf("*** Unable to open %s at 57600 bauds (8n1) [%s]\n", a1, v3);
abort();
}
return result;
}

original_code:
void initICreateCom(char* device)
{
  iCreateCom = initSerial(device, 57600, 8, 1, 0);
 if(iCreateCom==-1)
 {
  printf("*** Unable to open %s at 57600 bauds (8n1) [%s]\n",device,strerror((*__errno_location ())));
  abort();
 }
}

canonical_original_code:
void initICreateCom(char *device)
{
  iCreateCom = initSerial(device, 57600, 8, 1, 0);
 if(iCreateCom==-1)
 {
  printf("*** Unable to open %s at 57600 bauds (8n1) [%s]\n",device,strerror((*__errno_location ())));
  abort();
 }
}

variable_types:
device
char *


closeICreateCom

decompiled_code:
__int64 closeICreateCom()
{
return closeSerial((unsigned int)iCreateCom);
}

canonical_decompiled_code:
__int64 func1()
{
return closeSerial((unsigned int)iCreateCom);
}

original_code:
void closeICreateCom()
{
 closeSerial(iCreateCom);
}


decompiled_code:
__int64 iC_Start()
{
sendSerial((unsigned int)iCreateCom, 128LL);
return flushRecieveBuffer((unsigned int)iCreateCom);
}

canonical_decompiled_code:
__int64 func2()
{
sendSerial((unsigned int)iCreateCom, 128LL);
return flushRecieveBuffer((unsigned int)iCreateCom);
}

original_code:
void iC_Start(void)
{
  sendSerial(iCreateCom,128);
  flushRecieveBuffer(iCreateCom);
}

canonical_original_code:
void iC_Start()
{
  sendSerial(iCreateCom,128);
  flushRecieveBuffer(iCreateCom);
}


decompiled_code:
__int64 __fastcall iC_Baud(unsigned __int8 a1)
{
sendSerial((unsigned int)iCreateCom, 129LL);
return sendSerial((unsigned int)iCreateCom, a1);
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned __int8 a1)
{
sendSerial((unsigned int)iCreateCom, 129LL);
return sendSerial((unsigned int)iCreateCom, a1);
}

original_code:
void iC_Baud(unsigned char baudCode)
{
  sendSerial(iCreateCom,129);
  sendSerial(iCreateCom,baudCode);
}

variable_types:
baudCode
unsigned char


decompiled_code:
__int64 iC_Safe()
{
return sendSerial((unsigned int)iCreateCom, 131LL);
}

canonical_decompiled_code:
__int64 func4()
{
return sendSerial((unsigned int)iCreateCom, 131LL);
}

original_code:
void iC_Safe(void)
{
  sendSerial(iCreateCom,131);
}

canonical_original_code:
void iC_Safe()
{
  sendSerial(iCreateCom,131);
}


decompiled_code:
__int64 __fastcall iC_Drive(unsigned __int8 a1, unsigned __int8 a2, unsigned __int8 a3, unsigned __int8 a4)
{
sendSerial((unsigned int)iCreateCom, 137LL);
sendSerial((unsigned int)iCreateCom, a1);
sendSerial((unsigned int)iCreateCom, a2);
sendSerial((unsigned int)iCreateCom, a3);
return sendSerial((unsigned int)iCreateCom, a4);
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned __int8 a1, unsigned __int8 a2, unsigned __int8 a3, unsigned __int8 a4)
{
sendSerial((unsigned int)iCreateCom, 137LL);
sendSerial((unsigned int)iCreateCom, a1);
sendSerial((unsigned int)iCreateCom, a2);
sendSerial((unsigned int)iCreateCom, a3);
return sendSerial((unsigned int)iCreateCom, a4);
}

original_code:
void iC_Drive(unsigned char velocity_h, unsigned char velocity_l, unsigned char radius_h, unsigned char radius_l)
{
  sendSerial(iCreateCom,137);
  sendSerial(iCreateCom,velocity_h);
  sendSerial(iCreateCom,velocity_l);
  sendSerial(iCreateCom,radius_h);
  sendSerial(iCreateCom,radius_l);
}


decompiled_code:
__int64 __fastcall iC_DriveDirect(unsigned __int8 a1, unsigned __int8 a2, unsigned __int8 a3, unsigned __int8 a4)
{
sendSerial((unsigned int)iCreateCom, 145LL);
sendSerial((unsigned int)iCreateCom, a1);
sendSerial((unsigned int)iCreateCom, a2);
sendSerial((unsigned int)iCreateCom, a3);
return sendSerial((unsigned int)iCreateCom, a4);
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned __int8 a1, unsigned __int8 a2, unsigned __int8 a3, unsigned __int8 a4)
{
sendSerial((unsigned int)iCreateCom, 145LL);
sendSerial((unsigned int)iCreateCom, a1);
sendSerial((unsigned int)iCreateCom, a2);
sendSerial((unsigned int)iCreateCom, a3);
return sendSerial((unsigned int)iCreateCom, a4);
}

original_code:
void iC_DriveDirect(unsigned char rVelocity_h, unsigned char rVelocity_l, unsigned char lVelocity_h, unsigned char lVelocity_l)
{
  sendSerial(iCreateCom,145);
  sendSerial(iCreateCom,rVelocity_h);
  sendSerial(iCreateCom,rVelocity_l);
  sendSerial(iCreateCom,lVelocity_h);
 sendSerial(iCreateCom,lVelocity_l);
}


decompiled_code:
__int64 __fastcall iC_LEDs(unsigned __int8 a1, unsigned __int8 a2, unsigned __int8 a3)
{
sendSerial((unsigned int)iCreateCom, 139LL);
sendSerial((unsigned int)iCreateCom, a1);
sendSerial((unsigned int)iCreateCom, a2);
return sendSerial((unsigned int)iCreateCom, a3);
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned __int8 a1, unsigned __int8 a2, unsigned __int8 a3)
{
sendSerial((unsigned int)iCreateCom, 139LL);
sendSerial((unsigned int)iCreateCom, a1);
sendSerial((unsigned int)iCreateCom, a2);
return sendSerial((unsigned int)iCreateCom, a3);
}

original_code:
void iC_LEDs(unsigned char LEDBits, unsigned char powerCol, unsigned char powerInt)
{
  sendSerial(iCreateCom,139);
  sendSerial(iCreateCom,LEDBits);
  sendSerial(iCreateCom,powerCol);
  sendSerial(iCreateCom,powerInt);
}

variable_types:
LEDBits
unsigned char

variable_types:
powerCol
unsigned char

variable_types:
powerInt
unsigned char


decompiled_code:
__int64 __fastcall iC_PWMLowSide(unsigned __int8 a1, unsigned __int8 a2, unsigned __int8 a3)
{
sendSerial((unsigned int)iCreateCom, 144LL);
sendSerial((unsigned int)iCreateCom, a1);
sendSerial((unsigned int)iCreateCom, a2);
return sendSerial((unsigned int)iCreateCom, a3);
}

canonical_decompiled_code:
__int64 __fastcall func11(unsigned __int8 a1, unsigned __int8 a2, unsigned __int8 a3)
{
sendSerial((unsigned int)iCreateCom, 144LL);
sendSerial((unsigned int)iCreateCom, a1);
sendSerial((unsigned int)iCreateCom, a2);
return sendSerial((unsigned int)iCreateCom, a3);
}

original_code:
void iC_PWMLowSide(unsigned char LSD2Duty, unsigned char LSD1Duty, unsigned char LSD0Duty)
{
  sendSerial(iCreateCom,144);
  sendSerial(iCreateCom,LSD2Duty);
  sendSerial(iCreateCom,LSD1Duty);
  sendSerial(iCreateCom,LSD0Duty);
}


decompiled_code:
__int64 __fastcall iC_LowSideDrivers(unsigned __int8 a1)
{
sendSerial((unsigned int)iCreateCom, 138LL);
return sendSerial((unsigned int)iCreateCom, a1);
}

canonical_decompiled_code:
__int64 __fastcall func12(unsigned __int8 a1)
{
sendSerial((unsigned int)iCreateCom, 138LL);
return sendSerial((unsigned int)iCreateCom, a1);
}

original_code:
void iC_LowSideDrivers(unsigned char driverBits)
{
  sendSerial(iCreateCom,138);
  sendSerial(iCreateCom,driverBits);
}

variable_types:
driverBits
unsigned char


decompiled_code:
__int64 __fastcall iC_SendIR(unsigned __int8 a1)
{
sendSerial((unsigned int)iCreateCom, 151LL);
return sendSerial((unsigned int)iCreateCom, a1);
}

canonical_decompiled_code:
__int64 __fastcall func13(unsigned __int8 a1)
{
sendSerial((unsigned int)iCreateCom, 151LL);
return sendSerial((unsigned int)iCreateCom, a1);
}

original_code:
void iC_SendIR(unsigned char byteValue)
{
  sendSerial(iCreateCom,151);
  sendSerial(iCreateCom,byteValue);
}

variable_types:
byteValue
unsigned char


decompiled_code:
__int64 __fastcall iC_Sensor(unsigned __int8 a1)
{
sendSerial((unsigned int)iCreateCom, 142LL);
return sendSerial((unsigned int)iCreateCom, a1);
}

canonical_decompiled_code:
__int64 __fastcall func14(unsigned __int8 a1)
{
sendSerial((unsigned int)iCreateCom, 142LL);
return sendSerial((unsigned int)iCreateCom, a1);
}

original_code:
void iC_Sensor(unsigned char sensorId)
{
  sendSerial(iCreateCom,142);
  sendSerial(iCreateCom,sensorId);
}

variable_types:
sensorId
unsigned char


decompiled_code:
__int64 iC_getBumpsAndWDrops()
{
iC_Sensor(7u);
return receiveSerial((unsigned int)iCreateCom);
}

canonical_decompiled_code:
__int64 func15()
{
func14(7u);
return receiveSerial((unsigned int)iCreateCom);
}

original_code:
unsigned char iC_getBumpsAndWDrops()
{
  iC_Sensor(7);
  return receiveSerial(iCreateCom);
}

return_type: unsigned char


decompiled_code:
__int64 iC_getWall()
{
iC_Sensor(8u);
return receiveSerial((unsigned int)iCreateCom);
}

canonical_decompiled_code:
__int64 func16()
{
func14(8u);
return receiveSerial((unsigned int)iCreateCom);
}

original_code:
unsigned char iC_getWall()
{
  iC_Sensor(8);
  return receiveSerial(iCreateCom);
}


decompiled_code:
__int64 iC_getCliffL()
{
iC_Sensor(9u);
return receiveSerial((unsigned int)iCreateCom);
}

canonical_decompiled_code:
__int64 func17()
{
func14(9u);
return receiveSerial((unsigned int)iCreateCom);
}

original_code:
unsigned char iC_getCliffL()
{
  iC_Sensor(9);
  return receiveSerial(iCreateCom);
}


decompiled_code:
__int64 iC_getCliffFL()
{
iC_Sensor(0xAu);
return receiveSerial((unsigned int)iCreateCom);
}

canonical_decompiled_code:
__int64 func18()
{
func14(0xAu);
return receiveSerial((unsigned int)iCreateCom);
}

original_code:
unsigned char iC_getCliffFL()
{
  iC_Sensor(10);
  return receiveSerial(iCreateCom);
}


decompiled_code:
__int64 iC_getCliffFR()
{
iC_Sensor(0xBu);
return receiveSerial((unsigned int)iCreateCom);
}

canonical_decompiled_code:
__int64 func19()
{
func14(0xBu);
return receiveSerial((unsigned int)iCreateCom);
}

original_code:
unsigned char iC_getCliffFR()
{
  iC_Sensor(11);
  return receiveSerial(iCreateCom);
}


decompiled_code:
__int64 iC_getCliffR()
{
iC_Sensor(0xCu);
return receiveSerial((unsigned int)iCreateCom);
}

canonical_decompiled_code:
__int64 func20()
{
func14(0xCu);
return receiveSerial((unsigned int)iCreateCom);
}

original_code:
unsigned char iC_getCliffR()
{
  iC_Sensor(12);
  return receiveSerial(iCreateCom);
}


decompiled_code:
__int64 iC_getVirtualWall()
{
iC_Sensor(0xDu);
return receiveSerial((unsigned int)iCreateCom);
}

canonical_decompiled_code:
__int64 func21()
{
func14(0xDu);
return receiveSerial((unsigned int)iCreateCom);
}

original_code:
unsigned char iC_getVirtualWall()
{
  iC_Sensor(13);
  return receiveSerial(iCreateCom);
}


decompiled_code:
__int64 iC_getOvercurrents()
{
iC_Sensor(0xEu);
return receiveSerial((unsigned int)iCreateCom);
}

canonical_decompiled_code:
__int64 func22()
{
func14(0xEu);
return receiveSerial((unsigned int)iCreateCom);
}

original_code:
unsigned char iC_getOvercurrents()
{
  iC_Sensor(14);
  return receiveSerial(iCreateCom);
}


decompiled_code:
__int64 iC_getButtons()
{
iC_Sensor(0x12u);
return receiveSerial((unsigned int)iCreateCom);
}

canonical_decompiled_code:
__int64 func24()
{
func14(0x12u);
return receiveSerial((unsigned int)iCreateCom);
}

original_code:
unsigned char iC_getButtons()
{
  iC_Sensor(18);
  return receiveSerial(iCreateCom);
}


decompiled_code:
__int64 iC_getDistance()
{
int v0; // ebx

iC_Sensor(0x13u);
v0 = receiveSerial((unsigned int)iCreateCom);
return (v0 << 8) + (unsigned int)(unsigned __int8)receiveSerial((unsigned int)iCreateCom) - 0x7FFF;
}

canonical_decompiled_code:
__int64 func25()
{
int v0; // ebx

func14(0x13u);
v0 = receiveSerial((unsigned int)iCreateCom);
return (v0 << 8) + (unsigned int)(unsigned __int8)receiveSerial((unsigned int)iCreateCom) - 0x7FFF;
}

original_code:
signed short iC_getDistance()
{
  iC_Sensor(19);
  return (receiveSerial(iCreateCom)<<8) + receiveSerial(iCreateCom) - 0x7FFF;
}

return_type: short


decompiled_code:
__int64 iC_getAngle()
{
int v0; // ebx

iC_Sensor(0x14u);
v0 = receiveSerial((unsigned int)iCreateCom);
return (v0 << 8) + (unsigned int)(unsigned __int8)receiveSerial((unsigned int)iCreateCom) - 0x7FFF;
}

canonical_decompiled_code:
__int64 func26()
{
int v0; // ebx

func14(0x14u);
v0 = receiveSerial((unsigned int)iCreateCom);
return (v0 << 8) + (unsigned int)(unsigned __int8)receiveSerial((unsigned int)iCreateCom) - 0x7FFF;
}

original_code:
signed short iC_getAngle()
{
  iC_Sensor(20);
  return (receiveSerial(iCreateCom)<<8) + receiveSerial(iCreateCom) - 0x7FFF;
}


decompiled_code:
__int64 iC_getVoltage()
{
int v0; // ebx

iC_Sensor(0x16u);
v0 = receiveSerial((unsigned int)iCreateCom);
return (v0 << 8) + (unsigned int)(unsigned __int8)receiveSerial((unsigned int)iCreateCom);
}

canonical_decompiled_code:
__int64 func28()
{
int v0; // ebx

func14(0x16u);
v0 = receiveSerial((unsigned int)iCreateCom);
return (v0 << 8) + (unsigned int)(unsigned __int8)receiveSerial((unsigned int)iCreateCom);
}

original_code:
unsigned short iC_getVoltage()
{
  iC_Sensor(22);
  return (receiveSerial(iCreateCom)<<8) + receiveSerial(iCreateCom);
}

return_type: unsigned short


decompiled_code:
__int64 iC_getCurrent()
{
int v0; // ebx

iC_Sensor(0x17u);
v0 = receiveSerial((unsigned int)iCreateCom);
return (v0 << 8) + (unsigned int)(unsigned __int8)receiveSerial((unsigned int)iCreateCom) - 0x7FFF;
}

canonical_decompiled_code:
__int64 func29()
{
int v0; // ebx

func14(0x17u);
v0 = receiveSerial((unsigned int)iCreateCom);
return (v0 << 8) + (unsigned int)(unsigned __int8)receiveSerial((unsigned int)iCreateCom) - 0x7FFF;
}

original_code:
signed short iC_getCurrent()
{
  iC_Sensor(23);
  return (receiveSerial(iCreateCom)<<8) + receiveSerial(iCreateCom) - 0x7FFF;
}


decompiled_code:
__int64 iC_getBattTemp()
{
iC_Sensor(0x18u);
return (unsigned int)receiveSerial((unsigned int)iCreateCom) - 127;
}

canonical_decompiled_code:
__int64 func30()
{
func14(0x18u);
return (unsigned int)receiveSerial((unsigned int)iCreateCom) - 127;
}

original_code:
signed char iC_getBattTemp()
{
  iC_Sensor(24);
  return receiveSerial(iCreateCom) - 0x7F;
}


decompiled_code:
__int64 iC_getBattCap()
{
int v0; // ebx

iC_Sensor(0x1Au);
v0 = receiveSerial((unsigned int)iCreateCom);
return (v0 << 8) + (unsigned int)(unsigned __int8)receiveSerial((unsigned int)iCreateCom);
}

canonical_decompiled_code:
__int64 func32()
{
int v0; // ebx

func14(0x1Au);
v0 = receiveSerial((unsigned int)iCreateCom);
return (v0 << 8) + (unsigned int)(unsigned __int8)receiveSerial((unsigned int)iCreateCom);
}

original_code:
unsigned short iC_getBattCap()
{
  iC_Sensor(26);
 return (receiveSerial(iCreateCom)<<8) + receiveSerial(iCreateCom);
}


decompiled_code:
__int64 iC_getCliffLSig()
{
int v0; // ebx

iC_Sensor(0x1Cu);
v0 = receiveSerial((unsigned int)iCreateCom);
return (v0 << 8) + (unsigned int)(unsigned __int8)receiveSerial((unsigned int)iCreateCom);
}

canonical_decompiled_code:
__int64 func34()
{
int v0; // ebx

func14(0x1Cu);
v0 = receiveSerial((unsigned int)iCreateCom);
return (v0 << 8) + (unsigned int)(unsigned __int8)receiveSerial((unsigned int)iCreateCom);
}

original_code:
unsigned short iC_getCliffLSig()
{
  iC_Sensor(28);
  return (receiveSerial(iCreateCom)<<8) + receiveSerial(iCreateCom);
}


decompiled_code:
__int64 iC_getCliffFLSig()
{
int v0; // ebx

iC_Sensor(0x1Du);
v0 = receiveSerial((unsigned int)iCreateCom);
return (v0 << 8) + (unsigned int)(unsigned __int8)receiveSerial((unsigned int)iCreateCom);
}

canonical_decompiled_code:
__int64 func35()
{
int v0; // ebx

func14(0x1Du);
v0 = receiveSerial((unsigned int)iCreateCom);
return (v0 << 8) + (unsigned int)(unsigned __int8)receiveSerial((unsigned int)iCreateCom);
}

original_code:
unsigned short iC_getCliffFLSig()
{
  iC_Sensor(29);
  return (receiveSerial(iCreateCom)<<8) + receiveSerial(iCreateCom);
}


decompiled_code:
__int64 iC_getCliffFRSig()
{
int v0; // ebx

iC_Sensor(0x1Eu);
v0 = receiveSerial((unsigned int)iCreateCom);
return (v0 << 8) + (unsigned int)(unsigned __int8)receiveSerial((unsigned int)iCreateCom);
}

canonical_decompiled_code:
__int64 func36()
{
int v0; // ebx

func14(0x1Eu);
v0 = receiveSerial((unsigned int)iCreateCom);
return (v0 << 8) + (unsigned int)(unsigned __int8)receiveSerial((unsigned int)iCreateCom);
}

original_code:
unsigned short iC_getCliffFRSig()
{
  iC_Sensor(30);
  return (receiveSerial(iCreateCom)<<8) + receiveSerial(iCreateCom);
}


decompiled_code:
__int64 iC_getCliffRSig()
{
int v0; // ebx

iC_Sensor(0x1Fu);
v0 = receiveSerial((unsigned int)iCreateCom);
return (v0 << 8) + (unsigned int)(unsigned __int8)receiveSerial((unsigned int)iCreateCom);
}

canonical_decompiled_code:
__int64 func37()
{
int v0; // ebx

func14(0x1Fu);
v0 = receiveSerial((unsigned int)iCreateCom);
return (v0 << 8) + (unsigned int)(unsigned __int8)receiveSerial((unsigned int)iCreateCom);
}

original_code:
unsigned short iC_getCliffRSig()
{
  iC_Sensor(31);
  return (receiveSerial(iCreateCom)<<8) + receiveSerial(iCreateCom);
}


decompiled_code:
__int64 iC_getDigitalIn()
{
iC_Sensor(0x20u);
return receiveSerial((unsigned int)iCreateCom);
}

canonical_decompiled_code:
__int64 func38()
{
func14(0x20u);
return receiveSerial((unsigned int)iCreateCom);
}

original_code:
unsigned char iC_getDigitalIn()
{
  iC_Sensor(32);
  return receiveSerial(iCreateCom);
}


decompiled_code:
__int64 iC_getAnalogIn()
{
int v0; // ebx

iC_Sensor(0x21u);
v0 = receiveSerial((unsigned int)iCreateCom);
return (v0 << 8) + (unsigned int)(unsigned __int8)receiveSerial((unsigned int)iCreateCom);
}

canonical_decompiled_code:
__int64 func39()
{
int v0; // ebx

func14(0x21u);
v0 = receiveSerial((unsigned int)iCreateCom);
return (v0 << 8) + (unsigned int)(unsigned __int8)receiveSerial((unsigned int)iCreateCom);
}

original_code:
unsigned short iC_getAnalogIn()
{
  iC_Sensor(33);
  return (receiveSerial(iCreateCom)<<8) + receiveSerial(iCreateCom);
}


decompiled_code:
__int64 iC_getChargeSrcAvailable()
{
iC_Sensor(0x22u);
return receiveSerial((unsigned int)iCreateCom);
}

canonical_decompiled_code:
__int64 func40()
{
func14(0x22u);
return receiveSerial((unsigned int)iCreateCom);
}

original_code:
unsigned char iC_getChargeSrcAvailable()
{
  iC_Sensor(34);
  return receiveSerial(iCreateCom);
}


decompiled_code:
__int64 iC_getOIMode()
{
iC_Sensor(0x23u);
return receiveSerial((unsigned int)iCreateCom);
}

canonical_decompiled_code:
__int64 func41()
{
func14(0x23u);
return receiveSerial((unsigned int)iCreateCom);
}

original_code:
unsigned char iC_getOIMode()
{
  iC_Sensor(35);
  return receiveSerial(iCreateCom);
}


decompiled_code:
__int64 iC_getSongNumber()
{
iC_Sensor(0x24u);
return receiveSerial((unsigned int)iCreateCom);
}

canonical_decompiled_code:
__int64 func42()
{
func14(0x24u);
return receiveSerial((unsigned int)iCreateCom);
}

original_code:
unsigned char iC_getSongNumber()
{
  iC_Sensor(36);
  return receiveSerial(iCreateCom);
}


decompiled_code:
__int64 iC_getSongPlaying()
{
iC_Sensor(0x25u);
return receiveSerial((unsigned int)iCreateCom);
}

canonical_decompiled_code:
__int64 func43()
{
func14(0x25u);
return receiveSerial((unsigned int)iCreateCom);
}

original_code:
unsigned char iC_getSongPlaying()
{
  iC_Sensor(37);
  return receiveSerial(iCreateCom);
}


decompiled_code:
__int64 iC_getNStreamPackets()
{
iC_Sensor(0x26u);
return receiveSerial((unsigned int)iCreateCom);
}

canonical_decompiled_code:
__int64 func44()
{
func14(0x26u);
return receiveSerial((unsigned int)iCreateCom);
}

original_code:
unsigned char iC_getNStreamPackets()
{
  iC_Sensor(38);
  return receiveSerial(iCreateCom);
}


decompiled_code:
__int64 iC_getVelocity()
{
int v0; // ebx

iC_Sensor(0x27u);
v0 = receiveSerial((unsigned int)iCreateCom);
return (v0 << 8) + (unsigned int)(unsigned __int8)receiveSerial((unsigned int)iCreateCom) - 0x7FFF;
}

canonical_decompiled_code:
__int64 func45()
{
int v0; // ebx

func14(0x27u);
v0 = receiveSerial((unsigned int)iCreateCom);
return (v0 << 8) + (unsigned int)(unsigned __int8)receiveSerial((unsigned int)iCreateCom) - 0x7FFF;
}

original_code:
signed short iC_getVelocity()
{
  iC_Sensor(39);
  return (receiveSerial(iCreateCom)<<8) + receiveSerial(iCreateCom) - 0x7FFF;
}


decompiled_code:
__int64 iC_getRadius()
{
int v0; // ebx

iC_Sensor(0x28u);
v0 = receiveSerial((unsigned int)iCreateCom);
return (v0 << 8) + (unsigned int)(unsigned __int8)receiveSerial((unsigned int)iCreateCom) - 0x7FFF;
}

canonical_decompiled_code:
__int64 func46()
{
int v0; // ebx

func14(0x28u);
v0 = receiveSerial((unsigned int)iCreateCom);
return (v0 << 8) + (unsigned int)(unsigned __int8)receiveSerial((unsigned int)iCreateCom) - 0x7FFF;
}

original_code:
signed short iC_getRadius()
{
  iC_Sensor(40);
  return (receiveSerial(iCreateCom)<<8) + receiveSerial(iCreateCom) - 0x7FFF;
}


decompiled_code:
__int64 iC_getRVelocity()
{
int v0; // ebx

iC_Sensor(0x29u);
v0 = receiveSerial((unsigned int)iCreateCom);
return (v0 << 8) + (unsigned int)(unsigned __int8)receiveSerial((unsigned int)iCreateCom) - 0x7FFF;
}

canonical_decompiled_code:
__int64 func47()
{
int v0; // ebx

func14(0x29u);
v0 = receiveSerial((unsigned int)iCreateCom);
return (v0 << 8) + (unsigned int)(unsigned __int8)receiveSerial((unsigned int)iCreateCom) - 0x7FFF;
}

original_code:
signed short iC_getRVelocity()
{
  iC_Sensor(41);
  return (receiveSerial(iCreateCom)<<8) + receiveSerial(iCreateCom) - 0x7FFF;
}


decompiled_code:
__int64 iC_getLVelocity()
{
int v0; // ebx

iC_Sensor(0x2Au);
v0 = receiveSerial((unsigned int)iCreateCom);
return (v0 << 8) + (unsigned int)(unsigned __int8)receiveSerial((unsigned int)iCreateCom) - 0x7FFF;
}

canonical_decompiled_code:
__int64 func48()
{
int v0; // ebx

func14(0x2Au);
v0 = receiveSerial((unsigned int)iCreateCom);
return (v0 << 8) + (unsigned int)(unsigned __int8)receiveSerial((unsigned int)iCreateCom) - 0x7FFF;
}

original_code:
signed short iC_getLVelocity()
{
  iC_Sensor(42);
  return (receiveSerial(iCreateCom)<<8) + receiveSerial(iCreateCom) - 0x7FFF;
}


decompiled_code:
__int64 iC_Full()
{
return sendSerial((unsigned int)iCreateCom, 132LL);
}

canonical_decompiled_code:
__int64 func5()
{
return sendSerial((unsigned int)iCreateCom, 132LL);
}

original_code:
void iC_Full(void)
{
  sendSerial(iCreateCom,132);
}

canonical_original_code:
void iC_Full()
{
  sendSerial(iCreateCom,132);
}


decompiled_code:
__int64 __fastcall iC_Demo(unsigned __int8 a1)
{
sendSerial((unsigned int)iCreateCom, 136LL);
return sendSerial((unsigned int)iCreateCom, a1);
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned __int8 a1)
{
sendSerial((unsigned int)iCreateCom, 136LL);
return sendSerial((unsigned int)iCreateCom, a1);
}

original_code:
void iC_Demo(unsigned char demoID)
{
  sendSerial(iCreateCom,136);
  sendSerial(iCreateCom,demoID);
}

variable_types:
demoID
unsigned char


decompiled_code:
__int64 __fastcall iC_DigitalOut(unsigned __int8 a1)
{
sendSerial((unsigned int)iCreateCom, 147LL);
return sendSerial((unsigned int)iCreateCom, a1);
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned __int8 a1)
{
sendSerial((unsigned int)iCreateCom, 147LL);
return sendSerial((unsigned int)iCreateCom, a1);
}

original_code:
void iC_DigitalOut(unsigned char outBits)
{
  sendSerial(iCreateCom,147);
  sendSerial(iCreateCom,outBits);
}

variable_types:
outBits
unsigned char


decompiled_code:
__int64 iC_getIRByte()
{
iC_Sensor(0x11u);
return receiveSerial((unsigned int)iCreateCom);
}

canonical_decompiled_code:
__int64 func23()
{
func14(0x11u);
return receiveSerial((unsigned int)iCreateCom);
}

original_code:
unsigned char iC_getIRByte()
{
  iC_Sensor(17);
  return receiveSerial(iCreateCom);
}


decompiled_code:
__int64 iC_getChargeState()
{
iC_Sensor(0x15u);
return receiveSerial((unsigned int)iCreateCom);
}

canonical_decompiled_code:
__int64 func27()
{
func14(0x15u);
return receiveSerial((unsigned int)iCreateCom);
}

original_code:
unsigned char iC_getChargeState()
{
  iC_Sensor(21);
  return receiveSerial(iCreateCom);
}


decompiled_code:
__int64 iC_getBattCharge()
{
int v0; // ebx

iC_Sensor(0x19u);
v0 = receiveSerial((unsigned int)iCreateCom);
return (v0 << 8) + (unsigned int)(unsigned __int8)receiveSerial((unsigned int)iCreateCom);
}

canonical_decompiled_code:
__int64 func31()
{
int v0; // ebx

func14(0x19u);
v0 = receiveSerial((unsigned int)iCreateCom);
return (v0 << 8) + (unsigned int)(unsigned __int8)receiveSerial((unsigned int)iCreateCom);
}

original_code:
unsigned short iC_getBattCharge()
{
  iC_Sensor(25);
 return (receiveSerial(iCreateCom)<<8) + receiveSerial(iCreateCom);
}


decompiled_code:
__int64 iC_getWallSig()
{
int v0; // ebx

iC_Sensor(0x1Bu);
v0 = receiveSerial((unsigned int)iCreateCom);
return (v0 << 8) + (unsigned int)(unsigned __int8)receiveSerial((unsigned int)iCreateCom);
}

canonical_decompiled_code:
__int64 func33()
{
int v0; // ebx

func14(0x1Bu);
v0 = receiveSerial((unsigned int)iCreateCom);
return (v0 << 8) + (unsigned int)(unsigned __int8)receiveSerial((unsigned int)iCreateCom);
}

original_code:
unsigned short iC_getWallSig()
{
  iC_Sensor(27);
  return (receiveSerial(iCreateCom)<<8) + receiveSerial(iCreateCom);
}


original_code:
void init_erc_shared()
{
 ERC->alive = 0;
 strcpy(ERC->status,"");
 ERC->id_status = -1;
 ERC->Battery = 0;
 ERC->ManualControl = 0;
 ERC->vl = 0;
 ERC->vr = 0;
 ERC->plante_a_portee = -1;
 int i;
 for(i=0;i<5;i++)
 {
  ERC->level_w[i]=100;
  ERC->level_p[i]=100;
  ERC->level_n[i]=100;
  ERC->level_k[i]=100;
 }
}

variable_types: i int


decompiled_code:
void *shm_master()
{
int v0; // eax
void *result; // rax

v0 = shmget(shm_key, 8uLL, 950);
if ( v0 < 0 )
{
puts("Shared memory create failed!");
abort();
}
result = shmat(v0, 0LL, 0);
if ( result == (void *)-1LL )
{
puts("Shared memory attach failed!");
abort();
}
ERC = result;
return result;
}

canonical_decompiled_code:
void *func2()
{
int v0; // eax
void *result; // rax

v0 = shmget(shm_key, 8uLL, 950);
if ( v0 < 0 )
{
puts("Shared memory create failed!");
abort();
}
result = shmat(v0, 0LL, 0);
if ( result == (void *)-1LL )
{
puts("Shared memory attach failed!");
abort();
}
ERC = result;
return result;
}

original_code:
void shm_master()
{
 int mem_ID;
 void* ptr_mem_partagee;
 if ((mem_ID = shmget(shm_key, sizeof(ERC), 0666 | 01000)) < 0)
 {
  printf("Shared memory create failed!\n");
  abort();
 }
 if ((ptr_mem_partagee = shmat(mem_ID, ((void *)0), 0)) == (void*) -1)
 {
  printf("Shared memory attach failed!\n");
  abort();
 }
 ERC = ptr_mem_partagee;
}

canonical_original_code:
void shm_master()
{
 int mem_ID;
 void *ptr_mem_partagee;
 if ((mem_ID = shmget(shm_key, sizeof(ERC), 0666 | 01000)) < 0)
 {
  printf("Shared memory create failed!\n");
  abort();
 }
 if ((ptr_mem_partagee = shmat(mem_ID, ((void *)0), 0)) == (void *) -1)
 {
  printf("Shared memory attach failed!\n");
  abort();
 }
 ERC = ptr_mem_partagee;
}


decompiled_code:
void *shm_slave()
{
int v0; // eax
void *result; // rax

v0 = shmget(shm_key, 8uLL, 438);
if ( v0 < 0 )
{
puts("Shared memory get failed!");
abort();
}
result = shmat(v0, 0LL, 0);
if ( result == (void *)-1LL )
{
puts("Shared memory attach failed!");
abort();
}
ERC = result;
return result;
}

canonical_decompiled_code:
void *func3()
{
int v0; // eax
void *result; // rax

v0 = shmget(shm_key, 8uLL, 438);
if ( v0 < 0 )
{
puts("Shared memory get failed!");
abort();
}
result = shmat(v0, 0LL, 0);
if ( result == (void *)-1LL )
{
puts("Shared memory attach failed!");
abort();
}
ERC = result;
return result;
}

original_code:
void shm_slave()
{
 int mem_ID;
 void* ptr_mem_partagee;
 if ((mem_ID = shmget(shm_key, sizeof(ERC), 0666)) < 0)
 {
  printf("Shared memory get failed!\n");
  abort();
 }
 if ((ptr_mem_partagee = shmat(mem_ID, ((void *)0), 0)) == (void*) -1)
 {
  printf("Shared memory attach failed!\n");
  abort();
 }
 ERC = ptr_mem_partagee;
}

canonical_original_code:
void shm_slave()
{
 int mem_ID;
 void *ptr_mem_partagee;
 if ((mem_ID = shmget(shm_key, sizeof(ERC), 0666)) < 0)
 {
  printf("Shared memory get failed!\n");
  abort();
 }
 if ((ptr_mem_partagee = shmat(mem_ID, ((void *)0), 0)) == (void *) -1)
 {
  printf("Shared memory attach failed!\n");
  abort();
 }
 ERC = ptr_mem_partagee;
}


decompiled_code:
int shm_leave()
{
return shmdt(ERC);
}

canonical_decompiled_code:
int func4()
{
return shmdt(ERC);
}

original_code:
void shm_leave()
{
 shmdt(ERC);
}

canonical_original_code:
void shm_leave()
{
 shmdt(ERC);
}


decompiled_code:
void *__fastcall set_status(char a1, const char *a2)
{
void *result; // rax

strcpy(robot_status, a2);
result = ERC;
*((_BYTE *)ERC + 129) = a1;
return result;
}

canonical_decompiled_code:
void *__fastcall func7(char a1, const char *a2)
{
void *result; // rax

strcpy(robot_status, a2);
result = ERC;
*((_BYTE *)ERC + 129) = a1;
return result;
}

original_code:
void set_status(int id, const char* status)
{
 strcpy(robot_status,status);
 ERC->id_status = id;
}

canonical_original_code:
void set_status(int id, char *status)
{
 strcpy(robot_status,status);
 ERC->id_status = id;
}

variable_types: id int

variable_types:
status
char *


decompiled_code:
int state_back()
{
int result; // eax

set_status(3, "Backwarding");
if ( ++backstep <= 9 )
{
result = drive_direct(0xFFFFFF9C, 0xFFFFFF9C);
if ( backstep <= 20 )
return result;
LABEL_5:
result = drive_direct(0, 0);
backstep = 0;
state = (__int64)state_search;
return result;
}
result = drive_direct(-100 * lastrot, 100 * lastrot);
if ( backstep > 20 )
goto LABEL_5;
return result;
}

canonical_decompiled_code:
int func9()
{
int result; // eax

func7(3, "Backwarding");
if ( ++backstep <= 9 )
{
result = func8(0xFFFFFF9C, 0xFFFFFF9C);
if ( backstep <= 20 )
return result;
LABEL_5:
result = func8(0, 0);
backstep = 0;
state = (__int64)state_search;
return result;
}
result = func8(-100 * lastrot, 100 * lastrot);
if ( backstep > 20 )
goto LABEL_5;
return result;
}

original_code:
void state_back()
{
 set_status(3,"Backwarding");
 backstep++;
 if(backstep<10)
 {
  drive_direct(-100,-100);
 }
 else
 {
  drive_direct(-100*lastrot,100*lastrot);
 }
 if(backstep>20)
 {
  drive_direct(0,0);
  backstep=0;
  state = state_search;
 }
}


decompiled_code:
int state_undock()
{
int result; // eax

set_status(7, "Disengaging");
if ( ++undock_step <= 19 )
{
result = drive_direct(0xFFFFFF9C, 0xFFFFFF9C);
if ( undock_step <= 30 )
return result;
LABEL_5:
result = drive_direct(0, 0);
undock_step = 0;
state = (__int64)state_search;
return result;
}
result = drive_direct(0x64u, 0xFFFFFF9C);
if ( undock_step > 30 )
goto LABEL_5;
return result;
}

canonical_decompiled_code:
int func10()
{
int result; // eax

func7(7, "Disengaging");
if ( ++undock_step <= 19 )
{
result = func8(0xFFFFFF9C, 0xFFFFFF9C);
if ( undock_step <= 30 )
return result;
LABEL_5:
result = func8(0, 0);
undock_step = 0;
state = (__int64)state_search;
return result;
}
result = func8(0x64u, 0xFFFFFF9C);
if ( undock_step > 30 )
goto LABEL_5;
return result;
}

original_code:
void state_undock()
{
 set_status(7,"Disengaging");
 undock_step++;
 if(undock_step<20)
 {
  drive_direct(-100,-100);
 }
 else
 {
  drive_direct(100,-100);
 }
 if(undock_step>30)
 {
  drive_direct(0,0);
  undock_step=0;
  state = state_search;
 }
}


decompiled_code:
__int16 state_search()
{
__int16 result; // ax

set_status(1, "Searching flowers");
result = search_around(1LL, (__int64)"Searching flowers");
if ( !(_BYTE)result )
{
if ( (__int16)iC_getBattCharge() < low_charge )
state = (__int64)state_search_base;
result = (unsigned __int8)iC_getIRByte();
if ( base_is_flower )
{
if ( result == 255 )
return result;
state = (__int64)state_docking;
flower = 0;
*((_BYTE *)ERC + 178) = 0;
}
if ( result <= 239 )
{
result >>= 4;
state = (__int64)state_docking;
flower = result;
*((_BYTE *)ERC + 178) = result;
}
}
return result;
}

canonical_decompiled_code:
__int16 func16()
{
__int16 result; // ax

func7(1, "Searching flowers");
result = func14(1LL, (__int64)"Searching flowers");
if ( !(_BYTE)result )
{
if ( (__int16)func11() < low_charge )
state = (__int64)state_search_base;
result = (unsigned __int8)func15();
if ( base_is_flower )
{
if ( result == 255 )
return result;
state = (__int64)state_docking;
flower = 0;
*((_BYTE *)ERC + 178) = 0;
}
if ( result <= 239 )
{
result >>= 4;
state = (__int64)state_docking;
flower = result;
*((_BYTE *)ERC + 178) = result;
}
}
return result;
}

original_code:
void state_search()
{
 set_status(1,"Searching flowers");
 if(search_around()) return;
 if(iC_getBattCharge()<low_charge)
 {
  state = state_search_base;
 }
 short ir = iC_getIRByte();
 if(base_is_flower && ir != 255)
 {
  state = state_docking;
  ERC->plante_a_portee = flower = 0;
 }
 if(ir<240)
 {
  state = state_docking;
  ERC->plante_a_portee = flower = ir>>4;
 }
}

variable_types: ir short


decompiled_code:
int state_search_base()
{
int result; // eax

set_status(2, "Searching base");
result = search_around(2LL, (__int64)"Searching base");
if ( !(_BYTE)result )
{
result = iC_getIRByte();
if ( (_BYTE)result == 0xFE )
{
drive_direct(0, 0);
result = iC_Demo(1);
state = (__int64)state_demo_dock;
}
}
return result;
}

canonical_decompiled_code:
int func21()
{
int result; // eax

func7(2, "Searching base");
result = func14(2LL, (__int64)"Searching base");
if ( !(_BYTE)result )
{
result = func15();
if ( (_BYTE)result == 0xFE )
{
func8(0, 0);
result = func20(1);
state = (__int64)state_demo_dock;
}
}
return result;
}

original_code:
void state_search_base()
{
 set_status(2,"Searching base");
 if(search_around()) return;
 if(iC_getIRByte()==254)
 {
  drive_direct(0,0);
  iC_Demo(1);
  state = state_demo_dock;
 }
}


decompiled_code:
int state_demo_dock()
{
int result; // eax
int v1; // edx

set_status(4, "Docking on base");
if ( (char)iC_getChargeState() > 0 )
state = (__int64)state_charge;
result = iC_getIRByte();
v1 = demo_dock_timeout;
if ( (unsigned __int8)result == 255 || (unsigned __int8)result < 0xF1u || (unsigned __int8)result == 242 )
v1 = ++demo_dock_timeout;
if ( v1 > 30 )
{
iC_Demo(-1);
usleep(0x7A120u);
result = iC_Full();
state = (__int64)state_search_base;
demo_dock_timeout = 0;
}
return result;
}

canonical_decompiled_code:
int func22()
{
int result; // eax
int v1; // edx

func7(4, "Docking on base");
if ( (char)func18() > 0 )
state = (__int64)state_charge;
result = func15();
v1 = demo_dock_timeout;
if ( (unsigned __int8)result == 255 || (unsigned __int8)result < 0xF1u || (unsigned __int8)result == 242 )
v1 = ++demo_dock_timeout;
if ( v1 > 30 )
{
func20(-1);
usleep(0x7A120u);
result = func19();
state = (__int64)state_search_base;
demo_dock_timeout = 0;
}
return result;
}

original_code:
void state_demo_dock()
{
 set_status(4,"Docking on base");
 if(iC_getChargeState()>0)
 {
  state = state_charge;
 }
 int ir = iC_getIRByte();
 if(ir<240 || ir==255 || ir==242 || ir==240)
  demo_dock_timeout++;
 if(demo_dock_timeout>30)
 {
  iC_Demo(-1);
  usleep(500000);
  iC_Full();
  state = state_search_base;
  demo_dock_timeout = 0;
 }
}

variable_types: ir int


decompiled_code:
int state_charge()
{
int result; // eax

set_status(5, "Charging battery");
result = (__int16)iC_getBattCharge();
if ( (__int16)result > high_charge )
{
iC_Demo(-1);
usleep(0x7A120u);
result = iC_Full();
state = (__int64)state_undock;
}
return result;
}

canonical_decompiled_code:
int func23()
{
int result; // eax

func7(5, "Charging battery");
result = (__int16)func11();
if ( (__int16)result > high_charge )
{
func20(-1);
usleep(0x7A120u);
result = func19();
state = (__int64)state_undock;
}
return result;
}

original_code:
void state_charge()
{
 set_status(5,"Charging battery");
 if(iC_getBattCharge()>high_charge)
 {
  iC_Demo(-1);
  usleep(500000);
  iC_Full();
  state = state_undock;
 }
}


decompiled_code:
int state_action()
{
_BYTE *v0; // rax
int v1; // edx

LODWORD(v0) = (unsigned __int8)action_state;
v1 = action_lenght;
if ( action_state == 4 )
{
LODWORD(v0) = (_DWORD)ERC;
++*((_WORD *)ERC + flower + 79);
if ( v1 <= 0 )
{
action_state = 5;
set_status(14, "[flower] Distributing K.");
action_lenght = 100 - *((__int16 *)ERC + flower + 84);
LODWORD(v0) = iC_DigitalOut(action_state);
return (int)v0;
}
goto LABEL_6;
}
if ( action_state > 4 )
{
if ( action_state == 5 )
{
v0 = ERC;
++*((_WORD *)ERC + flower + 84);
if ( v1 > 0 )
goto LABEL_6;
}
else
{
if ( action_lenght > 0 )
goto LABEL_6;
LABEL_11:
v0 = ERC;
}
v0[178] = -1;
flower = -1;
action_state = 0;
state = (__int64)state_undock;
LODWORD(v0) = iC_DigitalOut(0);
return (int)v0;
}
if ( action_state == 2 )
{
LODWORD(v0) = (_DWORD)ERC;
++*((_WORD *)ERC + flower + 69);
if ( v1 <= 0 )
{
action_state = 3;
LABEL_14:
set_status(12, "[flower] Distributing P.");
action_lenght = 100 - *((__int16 *)ERC + flower + 74);
LODWORD(v0) = iC_DigitalOut(action_state);
return (int)v0;
}
LABEL_6:
action_lenght = v1 - 1;
return (int)v0;
}
if ( action_state == 3 )
{
LODWORD(v0) = (_DWORD)ERC;
++*((_WORD *)ERC + flower + 74);
if ( v1 <= 0 )
{
action_state = 4;
set_status(13, "[flower] Distributing N.");
action_lenght = 100 - *((__int16 *)ERC + flower + 79);
LODWORD(v0) = iC_DigitalOut(action_state);
return (int)v0;
}
goto LABEL_6;
}
if ( action_lenght > 0 )
goto LABEL_6;
++action_state;
switch ( (char)v0 )
{
case 0:
set_status(10, "[flower] Scanning");
action_lenght = 50;
LODWORD(v0) = iC_DigitalOut(action_state);
break;
case 1:
set_status(11, "[flower] Watering");
action_lenght = 100 - *((__int16 *)ERC + flower + 69);
LODWORD(v0) = iC_DigitalOut(action_state);
break;
case 2:
goto LABEL_14;
default:
goto LABEL_11;
}
return (int)v0;
}

canonical_decompiled_code:
int func25()
{
_BYTE *v0; // rax
int v1; // edx

LODWORD(v0) = (unsigned __int8)action_state;
v1 = action_lenght;
if ( action_state == 4 )
{
LODWORD(v0) = (_DWORD)ERC;
++*((_WORD *)ERC + flower + 79);
if ( v1 <= 0 )
{
action_state = 5;
func7(14, "[flower] Distributing K.");
action_lenght = 100 - *((__int16 *)ERC + flower + 84);
LODWORD(v0) = func24(action_state);
return (int)v0;
}
goto LABEL_6;
}
if ( action_state > 4 )
{
if ( action_state == 5 )
{
v0 = ERC;
++*((_WORD *)ERC + flower + 84);
if ( v1 > 0 )
goto LABEL_6;
}
else
{
if ( action_lenght > 0 )
goto LABEL_6;
LABEL_11:
v0 = ERC;
}
v0[178] = -1;
flower = -1;
action_state = 0;
state = (__int64)state_undock;
LODWORD(v0) = func24(0);
return (int)v0;
}
if ( action_state == 2 )
{
LODWORD(v0) = (_DWORD)ERC;
++*((_WORD *)ERC + flower + 69);
if ( v1 <= 0 )
{
action_state = 3;
LABEL_14:
func7(12, "[flower] Distributing P.");
action_lenght = 100 - *((__int16 *)ERC + flower + 74);
LODWORD(v0) = func24(action_state);
return (int)v0;
}
LABEL_6:
action_lenght = v1 - 1;
return (int)v0;
}
if ( action_state == 3 )
{
LODWORD(v0) = (_DWORD)ERC;
++*((_WORD *)ERC + flower + 74);
if ( v1 <= 0 )
{
action_state = 4;
func7(13, "[flower] Distributing N.");
action_lenght = 100 - *((__int16 *)ERC + flower + 79);
LODWORD(v0) = func24(action_state);
return (int)v0;
}
goto LABEL_6;
}
if ( action_lenght > 0 )
goto LABEL_6;
++action_state;
switch ( (char)v0 )
{
case 0:
func7(10, "[flower] Scanning");
action_lenght = 50;
LODWORD(v0) = func24(action_state);
break;
case 1:
func7(11, "[flower] Watering");
action_lenght = 100 - *((__int16 *)ERC + flower + 69);
LODWORD(v0) = func24(action_state);
break;
case 2:
goto LABEL_14;
default:
goto LABEL_11;
}
return (int)v0;
}

original_code:
void state_action()
{
 switch(action_state)
 {
  case 2:
   ERC->level_w[flower]++;
   break;
  case 3:
   ERC->level_p[flower]++;
   break;
  case 4:
   ERC->level_n[flower]++;
   break;
  case 5:
   ERC->level_k[flower]++;
   break;
 }
 if(action_lenght<=0)
 {
  action_state++;
  switch(action_state)
  {
   case 1:
    set_status(10,"[flower] Scanning");
    action_lenght = 50;
    break;
   case 2:
    set_status(11,"[flower] Watering");
    action_lenght = 100 - ERC->level_w[flower];
    break;
   case 3:
    set_status(12,"[flower] Distributing P.");
    action_lenght = 100 - ERC->level_p[flower];
    break;
   case 4:
    set_status(13,"[flower] Distributing N.");
    action_lenght = 100 - ERC->level_n[flower];
    break;
   case 5:
    set_status(14,"[flower] Distributing K.");
    action_lenght = 100 - ERC->level_k[flower];
    break;
   default:
    ERC->plante_a_portee = flower = -1;
    action_state = 0;
    state = state_undock;
  }
  iC_DigitalOut(action_state);
 }
 else
  action_lenght--;
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
const char *v4; // rbp
__int64 v5; // r14
int result; // eax
WINDOW *v7; // rbp
unsigned int v8; // eax
char *v9; // rsi
int v10; // eax
int v11; // ebx
const char *v12; // rdi
__int16 v13; // ax
__int64 v14; // rdx
bool v15; // zf
_WORD *v16; // rax
__int64 v17; // r12
__int64 v18; // r12
__int64 v19; // r12
__int64 v20; // r12
int v21; // edx
int v22; // edx
int v23; // edx

if ( argc <= 1 )
{
LABEL_12:
v7 = initscr();
cbreak();
noecho();
nodelay(v7, 1);
scrollok(v7, 1);
idlok(v7, 1);
printw("Simulating connection...\n");
iC_Full();
signal(2, (__sighandler_t)quit);
signal(15, (__sighandler_t)quit);
signal(3, (__sighandler_t)quit);
shm_master();
init_erc_shared();
v8 = time(0LL);
srand(v8);
v9 = (char *)(unsigned int)(__int16)iC_getBattCharge("Connected... (Battery=%d)\n", quit);
printw("Connected... (Battery=%d)\n", v9);
state = (__int64 (*)(void))state_search;
*(_BYTE *)ERC = 1;
help();
while ( loop )
{
wrefresh(stdscr);
usleep(0x186A0u);
v10 = wgetch(stdscr);
if ( v10 != -1 )
{
switch ( v10 )
{
case '<':
flower = 0;
state = (__int64 (*)(void))state_action;
*((_BYTE *)ERC + 178) = 0;
break;
case 'b':
state = (__int64 (*)(void))state_back;
break;
case 'c':
flower = 3;
state = (__int64 (*)(void))state_action;
*((_BYTE *)ERC + 178) = 3;
break;
case 'd':
state = (__int64 (*)(void))state_demo_dock;
break;
case 'f':
state = (__int64 (*)(void))state_docking;
break;
case 'h':
help();
break;
case 'm':
batt -= 100;
break;
case 'p':
batt += 100;
break;
case 'q':
loop = 0;
break;
case 's':
state = (__int64 (*)(void))state_search;
break;
case 'v':
flower = 4;
state = (__int64 (*)(void))state_action;
*((_BYTE *)ERC + 178) = 4;
break;
case 'w':
flower = 1;
state = (__int64 (*)(void))state_action;
*((_BYTE *)ERC + 178) = 1;
break;
case 'x':
flower = 2;
state = (__int64 (*)(void))state_action;
*((_BYTE *)ERC + 178) = 2;
break;
default:
printw("Touche %d inconnue !\n", v9);
break;
}
}
v11 = sub_delay;
if ( !sub_delay )
{
sub_delay = 50;
do
{
if ( flower != v11 )
{
v16 = ERC;
v17 = v11 + 64LL;
if ( *((__int16 *)ERC + v11 + 69) > 5 )
{
v23 = rand();
v16 = ERC;
*((_WORD *)ERC + v17 + 5) -= v23 % 6;
}
else
{
*((_WORD *)ERC + v17 + 5) = 0;
}
v18 = v11 + 72LL;
if ( (__int16)v16[v11 + 74] > 2 )
{
v22 = rand();
v16 = ERC;
*((_WORD *)ERC + v18 + 2) -= v22 % 3;
}
else
{
v16[v18 + 2] = 0;
}
v19 = v11 + 72LL;
if ( (__int16)v16[v11 + 79] > 2 )
{
v21 = rand();
v16 = ERC;
*((_WORD *)ERC + v19 + 7) -= v21 % 3;
}
else
{
v16[v19 + 7] = 0;
}
v20 = v11 + 80LL;
if ( (__int16)v16[v11 + 84] > 2 )
*((_WORD *)ERC + v20 + 4) -= rand() % 3;
else
v16[v20 + 4] = 0;
}
++v11;
}
while ( v11 != 5 );
v11 = sub_delay;
}
v9 = robot_status;
v12 = robot_laststatus;
sub_delay = v11 - 1;
if ( strcmp(robot_laststatus, robot_status) )
{
printw("New status: %s (was: %s)\n", robot_status, robot_laststatus);
strcpy(robot_laststatus, robot_status);
v9 = robot_status;
v12 = (char *)ERC + 1;
strcpy((char *)ERC + 1, robot_status);
}
v13 = iC_getBattCharge(v12, robot_status, ERC);
v15 = *(_BYTE *)(v14 + 132) == 0;
*(_WORD *)(v14 + 130) = v13;
if ( v15 )
{
state();
}
else
{
set_status(0, "Manual control");
v9 = (char *)(unsigned int)*((__int16 *)ERC + 68);
drive_direct(*((__int16 *)ERC + 67), (unsigned int)v9);
}
}
*(_BYTE *)ERC = 0;
printw("Terminated...\n");
shm_leave();
endwin();
result = 0;
}
else
{
v3 = 1;
while ( 2 )
{
v4 = argv[v3];
v5 = v3;
if ( *v4 == 45 && strlen(argv[v3]) == 2 )
{
switch ( v4[1] )
{
case 'V':
case 'h':
return usage(*argv);
case 'b':
base_is_flower = 1;
++v3;
printw("Homebase will now be seen as a flower...\n");
goto LABEL_11;
case 'c':
if ( v3 + 1 < argc )
high_charge = atoi(argv[v5 + 1]);
goto LABEL_10;
case 'k':
if ( v3 + 1 < argc )
shm_key = atoi(argv[v5 + 1]);
goto LABEL_10;
case 'l':
if ( v3 + 1 < argc )
low_charge = atoi(argv[v5 + 1]);
LABEL_10:
v3 += 2;
LABEL_11:
if ( v3 >= argc )
goto LABEL_12;
continue;
default:
return printw("Unknow argument -%c\nRun %s -h for help...\n", (unsigned int)v4[1], *argv);
}
}
break;
}
result = printw("Bad argument: %s\nRun %s -h for help...\n", v4, *argv);
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
const char *v4; // rbp
__int64 v5; // r14
int result; // eax
WINDOW *v7; // rbp
unsigned int v8; // eax
char *v9; // rsi
int v10; // eax
int v11; // ebx
const char *v12; // rdi
__int16 v13; // ax
__int64 v14; // rdx
bool v15; // zf
_WORD *v16; // rax
__int64 v17; // r12
__int64 v18; // r12
__int64 v19; // r12
__int64 v20; // r12
int v21; // edx
int v22; // edx
int v23; // edx

if ( argc <= 1 )
{
LABEL_12:
v7 = initscr();
cbreak();
noecho();
nodelay(v7, 1);
scrollok(v7, 1);
idlok(v7, 1);
printw("Simulating connection...\n");
func19();
signal(2, (__sighandler_t)quit);
signal(15, (__sighandler_t)quit);
signal(3, (__sighandler_t)quit);
func2();
func1();
v8 = time(0LL);
srand(v8);
v9 = (char *)(unsigned int)(__int16)func11("Connected... (Battery=%d)\n", quit);
printw("Connected... (Battery=%d)\n", v9);
state = (__int64 (*)(void))state_search;
*(_BYTE *)ERC = 1;
func5();
while ( loop )
{
wrefresh(stdscr);
usleep(0x186A0u);
v10 = wgetch(stdscr);
if ( v10 != -1 )
{
switch ( v10 )
{
case '<':
flower = 0;
state = (__int64 (*)(void))state_action;
*((_BYTE *)ERC + 178) = 0;
break;
case 'b':
state = (__int64 (*)(void))state_back;
break;
case 'c':
flower = 3;
state = (__int64 (*)(void))state_action;
*((_BYTE *)ERC + 178) = 3;
break;
case 'd':
state = (__int64 (*)(void))state_demo_dock;
break;
case 'f':
state = (__int64 (*)(void))state_docking;
break;
case 'h':
func5();
break;
case 'm':
batt -= 100;
break;
case 'p':
batt += 100;
break;
case 'q':
loop = 0;
break;
case 's':
state = (__int64 (*)(void))state_search;
break;
case 'v':
flower = 4;
state = (__int64 (*)(void))state_action;
*((_BYTE *)ERC + 178) = 4;
break;
case 'w':
flower = 1;
state = (__int64 (*)(void))state_action;
*((_BYTE *)ERC + 178) = 1;
break;
case 'x':
flower = 2;
state = (__int64 (*)(void))state_action;
*((_BYTE *)ERC + 178) = 2;
break;
default:
printw("Touche %d inconnue !\n", v9);
break;
}
}
v11 = sub_delay;
if ( !sub_delay )
{
sub_delay = 50;
do
{
if ( flower != v11 )
{
v16 = ERC;
v17 = v11 + 64LL;
if ( *((__int16 *)ERC + v11 + 69) > 5 )
{
v23 = rand();
v16 = ERC;
*((_WORD *)ERC + v17 + 5) -= v23 % 6;
}
else
{
*((_WORD *)ERC + v17 + 5) = 0;
}
v18 = v11 + 72LL;
if ( (__int16)v16[v11 + 74] > 2 )
{
v22 = rand();
v16 = ERC;
*((_WORD *)ERC + v18 + 2) -= v22 % 3;
}
else
{
v16[v18 + 2] = 0;
}
v19 = v11 + 72LL;
if ( (__int16)v16[v11 + 79] > 2 )
{
v21 = rand();
v16 = ERC;
*((_WORD *)ERC + v19 + 7) -= v21 % 3;
}
else
{
v16[v19 + 7] = 0;
}
v20 = v11 + 80LL;
if ( (__int16)v16[v11 + 84] > 2 )
*((_WORD *)ERC + v20 + 4) -= rand() % 3;
else
v16[v20 + 4] = 0;
}
++v11;
}
while ( v11 != 5 );
v11 = sub_delay;
}
v9 = robot_status;
v12 = robot_laststatus;
sub_delay = v11 - 1;
if ( strcmp(robot_laststatus, robot_status) )
{
printw("New status: %s (was: %s)\n", robot_status, robot_laststatus);
strcpy(robot_laststatus, robot_status);
v9 = robot_status;
v12 = (char *)ERC + 1;
strcpy((char *)ERC + 1, robot_status);
}
v13 = func11(v12, robot_status, ERC);
v15 = *(_BYTE *)(v14 + 132) == 0;
*(_WORD *)(v14 + 130) = v13;
if ( v15 )
{
state();
}
else
{
func7(0, "Manual control");
v9 = (char *)(unsigned int)*((__int16 *)ERC + 68);
func8(*((__int16 *)ERC + 67), (unsigned int)v9);
}
}
*(_BYTE *)ERC = 0;
printw("Terminated...\n");
func4();
endwin();
result = 0;
}
else
{
v3 = 1;
while ( 2 )
{
v4 = argv[v3];
v5 = v3;
if ( *v4 == 45 && strlen(argv[v3]) == 2 )
{
switch ( v4[1] )
{
case 'V':
case 'h':
return func6(*argv);
case 'b':
base_is_flower = 1;
++v3;
printw("Homebase will now be seen as a flower...\n");
goto LABEL_11;
case 'c':
if ( v3 + 1 < argc )
high_charge = atoi(argv[v5 + 1]);
goto LABEL_10;
case 'k':
if ( v3 + 1 < argc )
shm_key = atoi(argv[v5 + 1]);
goto LABEL_10;
case 'l':
if ( v3 + 1 < argc )
low_charge = atoi(argv[v5 + 1]);
LABEL_10:
v3 += 2;
LABEL_11:
if ( v3 >= argc )
goto LABEL_12;
continue;
default:
return printw("Unknow argument -%c\nRun %s -h for help...\n", (unsigned int)v4[1], *argv);
}
}
break;
}
result = printw("Bad argument: %s\nRun %s -h for help...\n", v4, *argv);
}
return result;
}

original_code:
int main( int argc, char* args[] )
{
 char* serialport = "/dev/ttyUSB0";
 if(argc>1)
 {
  int arg=1;
  while(arg<argc)
  {
   if(args[arg][0]!='-' || strlen(args[arg])!=2)
   {
    printf("Bad argument: %s\nRun %s -h for help...\n",args[arg],args[0]);
    return;
   }
   switch(args[arg][1])
   {
    case 'h':
    case 'V':
     usage(args[0]);
     return;
    case 's':
     if(arg+1<argc)
      serialport = args[arg+1];
     arg+=2;
     break;
    case 'l':
     if(arg+1<argc)
      low_charge = atoi(args[arg+1]);
     arg+=2;
     break;
    case 'c':
     if(arg+1<argc)
      high_charge = atoi(args[arg+1]);
     arg+=2;
     break;
    case 'k':
     if(arg+1<argc)
      shm_key = atoi(args[arg+1]);
     arg+=2;
     break;
    case 'b':
     base_is_flower = 1;
     printf("Homebase will now be seen as a flower...\n");
     arg++;
     break;
    default:
     printf("Unknow argument -%c\nRun %s -h for help...\n",args[arg][1],args[0]);
     return;
   }
  }
 }
 printf("Connecting to %s...\n",serialport);
 initICreateCom(serialport);
 iC_Start();
 iC_Full();
 signal(2,quit);
  signal(15,quit);
  signal(3,quit);
  shm_master();
  init_erc_shared();
  srand ( time(((void *)0)) );
 printf("Connected... (Battery=%d)\n",iC_getBattCharge());
 state = state_search;
 ERC->alive=1;
 while(loop)
 {
  usleep(100000);
  if(!sub_delay)
  {
   sub_delay = 50;
   int flwid;
   for(flwid=0;flwid<5;flwid++)
   {
    if(flwid==flower) continue;
    if(ERC->level_w[flwid]<6)
     ERC->level_w[flwid]=0;
    else
     ERC->level_w[flwid]-= rand()%6;
    if(ERC->level_p[flwid]<3)
     ERC->level_p[flwid]=0;
    else
     ERC->level_p[flwid]-= rand()%3;
    if(ERC->level_n[flwid]<3)
     ERC->level_n[flwid]=0;
    else
     ERC->level_n[flwid]-= rand()%3;
    if(ERC->level_k[flwid]<3)
     ERC->level_k[flwid]=0;
    else
     ERC->level_k[flwid]-= rand()%3;
   }
  }
  sub_delay--;
  if(strcmp(robot_laststatus,robot_status)!=0)
  {
   printf("New status: %s (was: %s)\n",robot_status,robot_laststatus);
   strcpy(robot_laststatus,robot_status);
   strcpy(ERC->status,robot_status);
  }
  ERC->Battery = iC_getBattCharge();
  if(ERC->ManualControl)
  {
   set_status(0,"Manual control");
   drive_direct(ERC->vl, ERC->vr);
  }
  else
   (*state)();
 }
 ERC->alive=0;
 iC_DriveDirect(0,0,0,0);
 closeICreateCom();
 printf("Terminated...\n");
 shm_leave();
}

canonical_original_code:
int main(int argc, char *args[])
{
 char *serialport = "/dev/ttyUSB0";
 if(argc>1)
 {
  int arg = 1;
  while(arg<argc)
  {
   if(args[arg][0]!='-' || strlen(args[arg])!=2)
   {
    printf("Bad argument: %s\nRun %s -h for help...\n",args[arg],args[0]);
    return;
   }
   switch(args[arg][1])
   {
    case 'h':
    case 'V':
     usage(args[0]);
     return;
    case 's':
     if(arg+1<argc)
      serialport = args[arg+1];
     arg+=2;
     break;
    case 'l':
     if(arg+1<argc)
      low_charge = atoi(args[arg+1]);
     arg+=2;
     break;
    case 'c':
     if(arg+1<argc)
      high_charge = atoi(args[arg+1]);
     arg+=2;
     break;
    case 'k':
     if(arg+1<argc)
      shm_key = atoi(args[arg+1]);
     arg+=2;
     break;
    case 'b':
     base_is_flower = 1;
     printf("Homebase will now be seen as a flower...\n");
     arg++;
     break;
    default:
     printf("Unknow argument -%c\nRun %s -h for help...\n",args[arg][1],args[0]);
     return;
   }
  }
 }
 printf("Connecting to %s...\n",serialport);
 initICreateCom(serialport);
 iC_Start();
 iC_Full();
 signal(2,quit);
  signal(15,quit);
  signal(3,quit);
  shm_master();
  init_erc_shared();
  srand ( time(((void *)0)) );
 printf("Connected... (Battery=%d)\n",iC_getBattCharge());
 state = state_search;
 ERC->alive=1;
 while(loop)
 {
  usleep(100000);
  if(!sub_delay)
  {
   sub_delay = 50;
   int flwid;
   for(flwid=0;flwid<5;flwid++)
   {
    if(flwid==flower) continue;
    if(ERC->level_w[flwid]<6)
     ERC->level_w[flwid]=0;
    else
     ERC->level_w[flwid]-= rand()%6;
    if(ERC->level_p[flwid]<3)
     ERC->level_p[flwid]=0;
    else
     ERC->level_p[flwid]-= rand()%3;
    if(ERC->level_n[flwid]<3)
     ERC->level_n[flwid]=0;
    else
     ERC->level_n[flwid]-= rand()%3;
    if(ERC->level_k[flwid]<3)
     ERC->level_k[flwid]=0;
    else
     ERC->level_k[flwid]-= rand()%3;
   }
  }
  sub_delay--;
  if(strcmp(robot_laststatus,robot_status)!=0)
  {
   printf("New status: %s (was: %s)\n",robot_status,robot_laststatus);
   strcpy(robot_laststatus,robot_status);
   strcpy(ERC->status,robot_status);
  }
  ERC->Battery = iC_getBattCharge();
  if(ERC->ManualControl)
  {
   set_status(0,"Manual control");
   drive_direct(ERC->vl, ERC->vr);
  }
  else
   (*state)();
 }
 ERC->alive=0;
 iC_DriveDirect(0,0,0,0);
 closeICreateCom();
 printf("Terminated...\n");
 shm_leave();
}

variable_types: argc int

variable_types:
args
char *[]

variable_types:
serialport
char *

variable_types: arg int

variable_types: flwid int


closeSerial

decompiled_code:
int __fastcall closeSerial(int a1)
{
int result; // eax

result = close(a1);
if ( result < 0 )
result = puts("close failed... ignored!");
return result;
}

canonical_decompiled_code:
int __fastcall func0(int a1)
{
int result; // eax

result = close(a1);
if ( result < 0 )
result = puts("close failed... ignored!");
return result;
}

original_code:
void closeSerial(int fd)
{
 if(close(fd)<0)
  printf("close failed... ignored!\n");
}

variable_types: fd int


sendSerial

decompiled_code:
int __fastcall sendSerial(int a1, char a2)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a2;
write(a1, buf, 1uLL);
return usleep(0x2710u);
}

canonical_decompiled_code:
int __fastcall func1(int a1, char a2)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a2;
write(a1, buf, 1uLL);
return usleep(0x2710u);
}

original_code:
int sendSerial(int fd, unsigned char data)
{
  write(fd,&data,1);
 usleep(10000);
}

variable_types:
data
unsigned char


receiveSerial

decompiled_code:
__int64 __fastcall receiveSerial(int fd)
{
clock_t v1; // rbp
char buf[25]; // [rsp+Fh] [rbp-19h] BYREF

v1 = clock() + 1000000;
while ( !read(fd, buf, 1uLL) )
{
usleep(1u);
if ( clock() > v1 )
{
puts("receive timed out!");
abort();
}
}
return (unsigned __int8)buf[0];
}

canonical_decompiled_code:
__int64 __fastcall func2(int fd)
{
clock_t v1; // rbp
char buf[25]; // [rsp+Fh] [rbp-19h] BYREF

v1 = clock() + 1000000;
while ( !read(fd, buf, 1uLL) )
{
usleep(1u);
if ( clock() > v1 )
{
puts("receive timed out!");
abort();
}
}
return (unsigned __int8)buf[0];
}

original_code:
unsigned char receiveSerial(int fd)
{
 clock_t timeout;
 timeout = clock()+((__clock_t) 1000000);
  unsigned char data;
  while(!read(fd,&data,1))
  {
   usleep(1);
   if(clock() > timeout)
  {
   printf("receive timed out!\n");
   abort();
  }
  }
  return data;
}

canonical_original_code:
unsigned char receiveSerial(int fd)
{
 long timeout;
 timeout = clock()+((long) 1000000);
  unsigned char data;
  while(!read(fd,&data,1))
  {
   usleep(1);
   if(clock() > timeout)
  {
   printf("receive timed out!\n");
   abort();
  }
  }
  return data;
}

variable_types: timeout long


flushRecieveBuffer

decompiled_code:
ssize_t __fastcall flushRecieveBuffer(int fd)
{
ssize_t result; // rax
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

do
result = read(fd, buf, 1uLL);
while ( result > 0 );
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func3(int fd)
{
ssize_t result; // rax
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

do
result = read(fd, buf, 1uLL);
while ( result > 0 );
return result;
}

original_code:
void flushRecieveBuffer(int fd)
{
 unsigned char data;
 while(read(fd,&data,1)>0);
}


selectBaud

original_code:
long selectBaud(long baudrate)
{
  long BAUD;
  switch (baudrate)
  {
      case 230400:
        BAUD = 0010003;
        break;
      case 115200:
        BAUD = 0010002;
        break;
      case 57600:
        BAUD = 0010001;
        break;
      case 38400:
        BAUD = 0000017;
        break;
      case 19200:
        BAUD = 0000016;
        break;
      case 9600:
      default :
        BAUD = 0000015;
        break;
      case 4800:
        BAUD = 0000014;
        break;
      case 2400:
        BAUD = 0000013;
        break;
      case 1800:
        BAUD = 0000012;
        break;
      case 1200:
        BAUD = 0000011;
        break;
      case 600:
        BAUD = 0000010;
        break;
      case 300:
        BAUD = 0000007;
        break;
      case 200:
        BAUD = 0000006;
        break;
      case 150:
        BAUD = 0000005;
        break;
      case 134:
        BAUD = 0000004;
        break;
      case 110:
        BAUD = 0000003;
        break;
      case 75:
        BAUD = 0000002;
        break;
      case 50:
        BAUD = 0000001;
        break;
      case 0:
        BAUD = 0000000;
        break;
  }
  return BAUD;
}

variable_types: baudrate long

variable_types: BAUD long

return_type: long


selectNDataBits

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

result = 48LL;
if ( (unsigned int)(a1 - 5) <= 2 )
result = 16LL * a1 - 80;
return result;
}

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

result = 48LL;
if ( (unsigned int)(a1 - 5) <= 2 )
result = 16LL * a1 - 80;
return result;
}

original_code:
long selectNDataBits(int nData)
{
  long DATABITS;
  switch (nData)
  {
      case 8:
      default:
        DATABITS = 0000060;
        break;
      case 7:
        DATABITS = 0000040;
        break;
      case 6:
        DATABITS = 0000020;
        break;
      case 5:
        DATABITS = 0000000;
        break;
  }
  return DATABITS;
}

variable_types: nData int

variable_types: DATABITS long


selectNStopBits

decompiled_code:
unsigned __int64 __fastcall selectNStopBits(int a1)
{
return (unsigned __int64)(a1 == 2) << 6;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func6(int a1)
{
return (unsigned __int64)(a1 == 2) << 6;
}

original_code:
long selectNStopBits(int nStop)
{
  long STOPBITS;
  switch (nStop)
  {
      case 1:
      default:
        STOPBITS = 0;
        break;
      case 2:
        STOPBITS = 0000100;
        break;
  }
  return STOPBITS;
}

variable_types: nStop int

variable_types: STOPBITS long


selectParity

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

if ( a1 == 1 )
return 256LL;
result = 256LL;
if ( a1 != 2 )
result = 0LL;
return result;
}

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

if ( a1 == 1 )
return 256LL;
result = 256LL;
if ( a1 != 2 )
result = 0LL;
return result;
}

original_code:
ParityState selectParity(int parity)
{
  ParityState par;
  switch (parity)
  {
      case 0:
      default:
        par.PARITY_ON = 0;
        par.PARITY_ODD = 0;
        break;
      case 1:
        par.PARITY_ON = 0000400;
        par.PARITY_ODD = 0001000;
        break;
      case 2:
        par.PARITY_ON = 0000400;
        par.PARITY_ODD = 0;
        break;
  }
  return par;
}

canonical_original_code:
struct ParityState selectParity(int parity)
{
  struct ParityState par;
  switch (parity)
  {
      case 0:
      default:
        par.PARITY_ON = 0;
        par.PARITY_ODD = 0;
        break;
      case 1:
        par.PARITY_ON = 0000400;
        par.PARITY_ODD = 0001000;
        break;
      case 2:
        par.PARITY_ON = 0000400;
        par.PARITY_ODD = 0;
        break;
  }
  return par;
}

variable_types: parity int

variable_types: struct ParityState { long PARITY_ON; long PARITY_ODD; }