draw

original_code:
void draw(struct square *m, struct tbl *tbl, struct maze *maze)
{
 initscr();
 int i = 0, j = 0;
 int row = 0, col = 0;
 while(row < 4) {
  while(col < 4) {
   mvprintw(i,j++,"%s","+");
   if (m[row * 4 + col].w[0]) {
    mvprintw(i,j,"%s","--");
    j += 2;
   } else {
    mvprintw(i,j,"%s","  ");
    j += 2;
   }
   ++col;
  }
  mvprintw(i,j++,"%s","+");
  col = 0;
  ++i;
  j = 0;
  while(col < 4) {
   if (m[row * 4 + col].w[3])
    mvprintw(i,j++,"%s","|");
   else
    mvprintw(i,j++,"%s"," ");
   mvprintw(i,j,"%s","  ");
   j += 2;
   ++col;
  }
  if (col == 4)
   mvprintw(i,j++,"%s","|");
  col = 0;
  ++i;
  j = 0;
  ++row;
 }
 while (col < 4) {
  mvprintw(i,j++,"%s","+");
  mvprintw(i,j,"%s","--");
  j += 2;
  ++col;
 }
 mvprintw(i,j,"%s","+");
 struct pos ij;
 struct node *nb = malloc(sizeof(struct node));
 nb->pre = maze->end_id;
 do {
  nb->id = nb->pre;
  nb = hash(nb, tbl);
  ij = id_to_draw_pos(nb->id);
  if (nb->id > 4*(4 -1))
   mvprintw(ij.i,ij.j,"%s","* ");
  else
   mvprintw(ij.i,ij.j,"%s","*");
 } while (nb->id != maze->start_id);
 wrefresh(stdscr);
 char c = wgetch(stdscr);
 if (c == 'q')
  endwin();
 free(nb);
 return;
}

canonical_original_code:
void draw(struct square *m, struct tbl *tbl, struct maze *maze)
{
 initscr();
 int i = 0; int j = 0;
 int row = 0; int col = 0;
 while(row < 4) {
  while(col < 4) {
   mvprintw(i,j++,"%s","+");
   if (m[row * 4 + col].w[0]) {
    mvprintw(i,j,"%s","--");
    j += 2;
   } else {
    mvprintw(i,j,"%s","  ");
    j += 2;
   }
   ++col;
  }
  mvprintw(i,j++,"%s","+");
  col = 0;
  ++i;
  j = 0;
  while(col < 4) {
   if (m[row * 4 + col].w[3])
    mvprintw(i,j++,"%s","|");
   else
    mvprintw(i,j++,"%s"," ");
   mvprintw(i,j,"%s","  ");
   j += 2;
   ++col;
  }
  if (col == 4)
   mvprintw(i,j++,"%s","|");
  col = 0;
  ++i;
  j = 0;
  ++row;
 }
 while (col < 4) {
  mvprintw(i,j++,"%s","+");
  mvprintw(i,j,"%s","--");
  j += 2;
  ++col;
 }
 mvprintw(i,j,"%s","+");
 struct pos ij;
 struct node *nb = malloc(sizeof(struct node));
 nb->pre = maze->end_id;
 do {
  nb->id = nb->pre;
  nb = hash(nb, tbl);
  ij = id_to_draw_pos(nb->id);
  if (nb->id > 4*(4 -1))
   mvprintw(ij.i,ij.j,"%s","* ");
  else
   mvprintw(ij.i,ij.j,"%s","*");
 } while (nb->id != maze->start_id);
 wrefresh(stdscr);
 char c = wgetch(stdscr);
 if (c == 'q')
  endwin();
 free(nb);
 return;
}

variable_types:
m
struct square { _Bool w[4]; } *

variable_types: struct maze { unsigned short start_id; unsigned short end_id; struct square *m; } *

variable_types: i int

variable_types: j int

variable_types: row int

variable_types: col int

variable_types: struct node { unsigned short time; unsigned short id; unsigned short pre; struct node *nxt_in_tbl; } *

variable_types: c char

user_defined_types: struct pos { unsigned char i; unsigned char j; }


decompiled_code:
__int64 __fastcall set_top_wall(_QWORD *a1, char a2, unsigned __int8 a3, unsigned __int8 a4)
{
int v4; // edx
__int64 result; // rax

v4 = 4 * a3;
if ( a2 )
{
*(_BYTE *)(*a1 + 4LL * (v4 + a4)) = 1;
result = v4 + a4 - 4;
*(_BYTE *)(*a1 + 4 * result + 2) = 1;
}
else
{
*(_BYTE *)(*a1 + 4LL * (v4 + a4)) = 0;
result = v4 + a4 - 4;
*(_BYTE *)(*a1 + 4 * result + 2) = 0;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(_QWORD *a1, char a2, unsigned __int8 a3, unsigned __int8 a4)
{
int v4; // edx
__int64 result; // rax

v4 = 4 * a3;
if ( a2 )
{
*(_BYTE *)(*a1 + 4LL * (v4 + a4)) = 1;
result = v4 + a4 - 4;
*(_BYTE *)(*a1 + 4 * result + 2) = 1;
}
else
{
*(_BYTE *)(*a1 + 4LL * (v4 + a4)) = 0;
result = v4 + a4 - 4;
*(_BYTE *)(*a1 + 4 * result + 2) = 0;
}
return result;
}

original_code:
void set_top_wall(struct submaze *s, uint8_t build, uint8_t row, uint8_t col)
{
 if (build) {
  s->m[row * 4 + col].w[0] = 1;
  s->m[(row-1) * 4 + col].w[2] = 1;
 } else {
  s->m[row * 4 + col].w[0] = 0;
  s->m[(row - 1) * 4 + col].w[2] = 0;
 }
}

canonical_original_code:
void set_top_wall(struct submaze *s, unsigned char build, unsigned char row, unsigned char col)
{
 if (build) {
  s->m[row * 4 + col].w[0] = 1;
  s->m[(row-1) * 4 + col].w[2] = 1;
 } else {
  s->m[row * 4 + col].w[0] = 0;
  s->m[(row - 1) * 4 + col].w[2] = 0;
 }
}

variable_types: struct submaze { struct square { _Bool w[4]; } *m; unsigned char i; unsigned char j; unsigned char r; unsigned char c; } *

variable_types:
build
unsigned char

variable_types:
row
unsigned char

variable_types:
col
unsigned char

return_type: void

user_defined_types: struct submaze { struct square *m; unsigned char i; unsigned char j; unsigned char r; unsigned char c; }


decompiled_code:
__int64 __fastcall set_left_wall(_QWORD *a1, char a2, unsigned __int8 a3, unsigned __int8 a4)
{
int v4; // edx
__int64 result; // rax

v4 = 4 * a3;
if ( a2 )
{
*(_BYTE *)(*a1 + 4LL * (v4 + a4) + 3) = 1;
result = v4 + a4 - 1;
*(_BYTE *)(*a1 + 4 * result + 1) = 1;
}
else
{
*(_BYTE *)(*a1 + 4LL * (v4 + a4) + 3) = 0;
result = v4 + a4 - 1;
*(_BYTE *)(*a1 + 4 * result + 1) = 0;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(_QWORD *a1, char a2, unsigned __int8 a3, unsigned __int8 a4)
{
int v4; // edx
__int64 result; // rax

v4 = 4 * a3;
if ( a2 )
{
*(_BYTE *)(*a1 + 4LL * (v4 + a4) + 3) = 1;
result = v4 + a4 - 1;
*(_BYTE *)(*a1 + 4 * result + 1) = 1;
}
else
{
*(_BYTE *)(*a1 + 4LL * (v4 + a4) + 3) = 0;
result = v4 + a4 - 1;
*(_BYTE *)(*a1 + 4 * result + 1) = 0;
}
return result;
}

original_code:
void set_left_wall(struct submaze *s, uint8_t build, uint8_t row, uint8_t col)
{
 if (build) {
  s->m[row * 4 + col].w[3] = 1;
  s->m[row * 4 + (col - 1)].w[1] = 1;
 } else {
  s->m[row * 4 + col].w[3] = 0;
  s->m[row * 4 + (col - 1)].w[1] = 0;
 }
}

canonical_original_code:
void set_left_wall(struct submaze *s, unsigned char build, unsigned char row, unsigned char col)
{
 if (build) {
  s->m[row * 4 + col].w[3] = 1;
  s->m[row * 4 + (col - 1)].w[1] = 1;
 } else {
  s->m[row * 4 + col].w[3] = 0;
  s->m[row * 4 + (col - 1)].w[1] = 0;
 }
}


original_code:
void new_submaze(struct submaze *t, struct submaze *s, uint8_t vcut, uint8_t hcut)
{
 for (int i = 0; i < 4; ++i)
  t[i] = *s;
 t[0].c = vcut - s->j - 1;
 t[0].r = hcut - s->i - 1;
 t[1].j = vcut;
 t[1].c = s->j + s->c - vcut;
 t[1].r = hcut - s->i - 1;
 t[2].i = hcut;
 t[2].c = vcut - s->j - 1;
 t[2].r = s->i + s->r - hcut;
 t[3].i = hcut;
 t[3].j = vcut;
 t[3].r = s->i + s->r - hcut;
 t[3].c = s->j + s->c - vcut;
}

canonical_original_code:
void new_submaze(struct submaze *t, struct submaze *s, unsigned char vcut, unsigned char hcut)
{
 for (int i = 0; i < 4; ++i)
  t[i] = *s;
 t[0].c = vcut - s->j - 1;
 t[0].r = hcut - s->i - 1;
 t[1].j = vcut;
 t[1].c = s->j + s->c - vcut;
 t[1].r = hcut - s->i - 1;
 t[2].i = hcut;
 t[2].c = vcut - s->j - 1;
 t[2].r = s->i + s->r - hcut;
 t[3].i = hcut;
 t[3].j = vcut;
 t[3].r = s->i + s->r - hcut;
 t[3].c = s->j + s->c - vcut;
}

variable_types:
s
struct submaze *

variable_types:
vcut
unsigned char

variable_types:
hcut
unsigned char


original_code:
void gen_maze(struct submaze *s)
{
 if ( s->r == 0 || s->c == 0)
  return;
 for (int k = 0; k < 4; ++k) {
  s->m[k].w[0] = 1;
  s->m[(4 - 1) * 4 + k].w[2] = 1;
  s->m[k * 4].w[3] = 1;
  s->m[k * 4 + 4 - 1].w[1] = 1;
 }
 uint8_t hcut = 1 + (rand() % s->r) + s->i;
 uint8_t vcut = 1 + (rand() % s->c) + s->j;
 for (int k = s->j; k <= s->j + s->c; ++k)
  set_top_wall(s,1,hcut,k);
 for (int k = s->i; k <= s->i + s->r; ++k)
  set_left_wall(s,1,k,vcut);
 int no_hole = rand() % 4;
 int hole;
 if (no_hole != 0) {
  hole = s->i + (rand() % (hcut - s->i));
  set_left_wall(s,0,hole,vcut);
 }
 if (no_hole != 1) {
  hole = hcut + (rand() % (s->i + s->r - hcut + 1));
  set_left_wall(s,0,hole,vcut);
 }
 if (no_hole != 2) {
  hole = vcut + (rand() % (s->j + s->c - vcut + 1));
  set_top_wall(s,0,hcut,hole);
 }
 if (no_hole != 3) {
  hole = s->j + (rand() % (vcut - s->j));
  set_top_wall(s,0,hcut,hole);
 }
 struct submaze t[4];
 new_submaze(t,s,vcut,hcut);
 for (int i = 0; i < 4; ++i)
  gen_maze(&t[i]);
}

canonical_original_code:
void gen_maze(struct submaze *s)
{
 if ( s->r == 0 || s->c == 0)
  return;
 for (int k = 0; k < 4; ++k) {
  s->m[k].w[0] = 1;
  s->m[(4 - 1) * 4 + k].w[2] = 1;
  s->m[k * 4].w[3] = 1;
  s->m[k * 4 + 4 - 1].w[1] = 1;
 }
 unsigned char hcut = 1 + (rand() % s->r) + s->i;
 unsigned char vcut = 1 + (rand() % s->c) + s->j;
 for (int k = s->j; k <= s->j + s->c; ++k)
  set_top_wall(s,1,hcut,k);
 for (int k = s->i; k <= s->i + s->r; ++k)
  set_left_wall(s,1,k,vcut);
 int no_hole = rand() % 4;
 int hole;
 if (no_hole != 0) {
  hole = s->i + (rand() % (hcut - s->i));
  set_left_wall(s,0,hole,vcut);
 }
 if (no_hole != 1) {
  hole = hcut + (rand() % (s->i + s->r - hcut + 1));
  set_left_wall(s,0,hole,vcut);
 }
 if (no_hole != 2) {
  hole = vcut + (rand() % (s->j + s->c - vcut + 1));
  set_top_wall(s,0,hcut,hole);
 }
 if (no_hole != 3) {
  hole = s->j + (rand() % (vcut - s->j));
  set_top_wall(s,0,hcut,hole);
 }
 struct submaze t[4];
 new_submaze(t,s,vcut,hcut);
 for (int i = 0; i < 4; ++i)
  gen_maze(&t[i]);
}

variable_types: k int

variable_types: hole int

variable_types: struct submaze { struct square { _Bool w[4]; } *m; unsigned char i; unsigned char j; unsigned char r; unsigned char c; } [4]


itinerary

decompiled_code:
__int64 __fastcall itinerary(__int64 a1, __int64 a2)
{
__int64 result; // rax

*(_QWORD *)(a2 + 8) = a1;
*(_WORD *)a2 = (unsigned __int8)node_at(1LL, a1) == 0 ? 13 : 1;
result = (unsigned __int8)node_at(12LL, a1) == 0 ? 24 : 12;
*(_WORD *)(a2 + 2) = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2)
{
__int64 result; // rax

*(_QWORD *)(a2 + 8) = a1;
*(_WORD *)a2 = (unsigned __int8)node_at(1LL, a1) == 0 ? 13 : 1;
result = (unsigned __int8)node_at(12LL, a1) == 0 ? 24 : 12;
*(_WORD *)(a2 + 2) = result;
return result;
}

original_code:
void itinerary(struct square *m,struct maze *maze) {
 maze->m = m;
 if (node_at(1,m))
  maze->start_id = 1;
 else
  maze->start_id = 4*(4 -1) + 1;
 if (node_at(4*(4 -1),m))
  maze->end_id = 4*(4 -1);
 else
  maze->end_id = 2*4*(4 -1);
 return;
}

canonical_original_code:
void itinerary(struct square *m, struct maze *maze) {
 maze->m = m;
 if (node_at(1,m))
  maze->start_id = 1;
 else
  maze->start_id = 4*(4 -1) + 1;
 if (node_at(4*(4 -1),m))
  maze->end_id = 4*(4 -1);
 else
  maze->end_id = 2*4*(4 -1);
 return;
}

variable_types:
m
struct square { _Bool w[4]; } *

variable_types: struct maze { unsigned short start_id; unsigned short end_id; struct square *m; } *


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rdx
int result; // eax
int v5; // eax
char *v6; // rdi
__int64 i; // rcx
_BYTE v8[64]; // [rsp+0h] [rbp-A8h] BYREF
_WORD v9[8]; // [rsp+40h] [rbp-68h] BYREF
__m128i v10; // [rsp+50h] [rbp-58h] BYREF
char v11[72]; // [rsp+60h] [rbp-48h] BYREF

if ( argc == 2 )
{
v5 = atoi(argv[1]);
srand(v5);
v6 = v11;
for ( i = 16LL; i; --i )
{
*(_DWORD *)v6 = 0;
v6 += 4;
}
v10.m128i_i64[0] = (__int64)v11;
v10.m128i_i32[2] = 50528256;
gen_maze(&v10);
itinerary(v10.m128i_i64[0], (__int64)v9);
fastest_path(v9, v8);
draw(v10.m128i_i64[0], (__int64)v8, v9);
result = 0;
}
else
{
v3 = "maze-test";
if ( argc )
v3 = *argv;
fprintf(stderr, "usage: %s <arg>\n", v3);
result = 1;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rdx
int result; // eax
int v5; // eax
char *v6; // rdi
__int64 i; // rcx
_BYTE v8[64]; // [rsp+0h] [rbp-A8h] BYREF
_WORD v9[8]; // [rsp+40h] [rbp-68h] BYREF
__m128i v10; // [rsp+50h] [rbp-58h] BYREF
char v11[72]; // [rsp+60h] [rbp-48h] BYREF

if ( argc == 2 )
{
v5 = atoi(argv[1]);
srand(v5);
v6 = v11;
for ( i = 16LL; i; --i )
{
*(_DWORD *)v6 = 0;
v6 += 4;
}
v10.m128i_i64[0] = (__int64)v11;
v10.m128i_i32[2] = 50528256;
func5(&v10);
func6(v10.m128i_i64[0], (__int64)v9);
fastest_path(v9, v8);
func1(v10.m128i_i64[0], (__int64)v8, v9);
result = 0;
}
else
{
v3 = "maze-test";
if ( argc )
v3 = *argv;
fprintf(stderr, "usage: %s <arg>\n", v3);
result = 1;
}
return result;
}

original_code:
int main (int argc, char *argv[] ) {
 if (argc != 2) {
  fprintf(stderr, "usage: %s <arg>\n", argc?argv[0]:"maze-test");
  return 1;
 }
 srand(atoi(argv[1]));
 struct square mz[4 * 4] = {};
 struct submaze s_, *s = &s_;
 s->m = mz;
 s->i = 0;
 s->j = 0;
 s->r = 4 -1;
 s->c = 4 -1;
 gen_maze(s);
 struct maze maze;
 itinerary(s->m,&maze);
 struct tbl tbl;
 fastest_path(&maze,&tbl);
 draw(s->m,&tbl,&maze);
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
 if (argc != 2) {
  fprintf(stderr, "usage: %s <arg>\n", argc?argv[0]:"maze-test");
  return 1;
 }
 srand(atoi(argv[1]));
 struct square mz[-1] = {};
 struct submaze s_; struct submaze *s = &s_;
 s->m = mz;
 s->i = 0;
 s->j = 0;
 s->r = 4 -1;
 s->c = 4 -1;
 gen_maze(s);
 struct maze maze;
 itinerary(s->m,&maze);
 struct tbl tbl;
 fastest_path(&maze,&tbl);
 draw(s->m,&tbl,&maze);
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
mz
struct square { _Bool w[4]; } [-1]

variable_types: struct submaze { struct square { _Bool w[4]; } *m; unsigned char i; unsigned char j; unsigned char r; unsigned char c; }

variable_types: struct maze { unsigned short start_id; unsigned short end_id; struct square { _Bool w[4]; } *m; }

variable_types: struct tbl { struct node { unsigned short time; unsigned short id; unsigned short pre; struct node *nxt_in_tbl; } *t[-1]; unsigned short n_tbl; }

return_type: int

user_defined_types: struct maze { unsigned short start_id; unsigned short end_id; struct square *m; }

user_defined_types: struct tbl { struct node *t[-1]; unsigned short n_tbl; }


repo: threedaymonk/ansitee

decompiled_code:
void __fastcall write_without_ansi(int fd, char *a2, __int64 a3)
{
char *v3; // rbx
char *v4; // r12
int v5; // ebp
char v6; // al
char v7; // [rsp+Eh] [rbp-2Ah] BYREF
char buf[41]; // [rsp+Fh] [rbp-29h] BYREF

v7 = 27;
if ( a3 )
{
v3 = a2;
v4 = &a2[a3];
v5 = 0;
do
{
v6 = *v3;
buf[0] = *v3;
if ( v5 == 1 )
{
v5 = 2;
if ( v6 != 91 )
{
write(fd, &v7, 1uLL);
v5 = 0;
}
}
else if ( v5 == 2 )
{
if ( (unsigned __int8)(v6 - 64) <= 0x3Eu )
v5 = 0;
}
else if ( v6 == 27 )
{
v5 = 1;
}
else
{
write(fd, buf, 1uLL);
}
++v3;
}
while ( v3 != v4 );
}
}

original_code:
static ssize_t
write_without_ansi(int fd, const void *buf, size_t count) {
    int state = 0, i;
    char ch, esc = 0x1B;
    for (i = 0; i < count; i++) {
        ch = ((char *)buf)[i];
        switch (state) {
            case 0:
                if (ch == 0x1B) {
                    state = 1;
                } else {
                    write(fd, &ch, 1);
                }
                break;
            case 1:
                if (ch == '[') {
                    state = 2;
                } else {
                    state = 0;
                    write(fd, &esc, 1);
                }
                break;
            case 2:
                if (((ch) >= 0x40 && (ch) <= 0x7E)) {
                    state = 0;
                }
        }
    }
}

canonical_original_code:
static long write_without_ansi(int fd, void *buf, unsigned long count) {
    int state = 0; int i;
    char ch; char esc = 0x1B;
    for (i = 0; i < count; i++) {
        ch = ((char *)buf)[i];
        switch (state) {
            case 0:
                if (ch == 0x1B) {
                    state = 1;
                } else {
                    write(fd, &ch, 1);
                }
                break;
            case 1:
                if (ch == '[') {
                    state = 2;
                } else {
                    state = 0;
                    write(fd, &esc, 1);
                }
                break;
            case 2:
                if (((ch) >= 0x40 && (ch) <= 0x7E)) {
                    state = 0;
                }
        }
    }
}

variable_types: fd int

variable_types:
buf
void *

variable_types:
count
unsigned long

variable_types: state int

variable_types: i int

variable_types: ch char

variable_types: esc char

return_type: long


usage

decompiled_code:
void __noreturn usage()
{
fwrite("usage: tee [-ai] [file ...]\n", 1uLL, 0x1CuLL, stderr);
exit(1);
}

canonical_decompiled_code:
void __noreturn func8()
{
fwrite("usage: tee [-ai] [file ...]\n", 1uLL, 0x1CuLL, stderr);
exit(1);
}

original_code:
static void
usage(void) {
    (void)fprintf(stderr, "usage: tee [-ai] [file ...]\n");
    exit(1);
}

canonical_original_code:
static void usage() {
    (void)fprintf(stderr, "usage: tee [-ai] [file ...]\n");
    exit(1);
}


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // er12
int v4; // er14
int v5; // eax
const char **v6; // rbx
void *v7; // r13
int v8; // ebp
int i; // eax
int v10; // eax
int v11; // er14
__int64 v12; // r12
int v13; // eax
char *v14; // rbp
int v15; // ebx
int status; // [rsp+Ch] [rbp-3Ch]

v3 = 0;
v4 = 0;
while ( 1 )
{
v5 = getopt(argc, (char *const *)argv, "aish");
if ( v5 == -1 )
break;
if ( (_BYTE)v5 == 105 )
{
signal(2, (__sighandler_t)1);
}
else if ( (_BYTE)v5 == 115 )
{
v3 = 1;
}
else
{
if ( (_BYTE)v5 != 97 )
usage();
v4 = 1;
}
}
v6 = &argv[optind];
v7 = malloc(0x2000uLL);
if ( !v7 )
err(1, "malloc");
add(1, 0, (__int64)"stdout");
if ( *v6 )
{
v8 = v4 == 0 ? 577 : 1089;
status = 0;
for ( i = open(*v6, v8, 438LL); ; i = open(*v6, v8, 438LL) )
{
if ( i < 0 )
{
warn("%s", *v6);
status = 1;
}
else
{
add(i, v3, (__int64)*v6);
}
if ( !*++v6 )
break;
}
}
else
{
status = 0;
}
while ( 1 )
{
do
{
v10 = read(0, v7, 0x2000uLL);
v11 = v10;
if ( v10 <= 0 )
{
if ( v10 >= 0 )
exit(status);
err(1, "read");
}
v12 = head;
}
while ( !head );
do
{
v14 = (char *)v7;
v15 = v11;
while ( 1 )
{
if ( *(_DWORD *)(v12 + 12) )
write_without_ansi(*(_DWORD *)(v12 + 8), v14, v15);
else
v13 = write(*(_DWORD *)(v12 + 8), v14, v15);
if ( v13 == -1 )
break;
v14 += v13;
v15 -= v13;
if ( !v15 )
goto LABEL_29;
}
warn("%s", *(const char **)(v12 + 16));
status = 1;
LABEL_29:
v12 = *(_QWORD *)v12;
}
while ( v12 );
}
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // er12
int v4; // er14
int v5; // eax
const char **v6; // rbx
void *v7; // r13
int v8; // ebp
int i; // eax
int v10; // eax
int v11; // er14
__int64 v12; // r12
int v13; // eax
char *v14; // rbp
int v15; // ebx
int status; // [rsp+Ch] [rbp-3Ch]

v3 = 0;
v4 = 0;
while ( 1 )
{
v5 = getopt(argc, (char *const *)argv, "aish");
if ( v5 == -1 )
break;
if ( (_BYTE)v5 == 105 )
{
signal(2, (__sighandler_t)1);
}
else if ( (_BYTE)v5 == 115 )
{
v3 = 1;
}
else
{
if ( (_BYTE)v5 != 97 )
func8();
v4 = 1;
}
}
v6 = &argv[optind];
v7 = malloc(0x2000uLL);
if ( !v7 )
err(1, "malloc");
add(1, 0, (__int64)"stdout");
if ( *v6 )
{
v8 = v4 == 0 ? 577 : 1089;
status = 0;
for ( i = open(*v6, v8, 438LL); ; i = open(*v6, v8, 438LL) )
{
if ( i < 0 )
{
warn("%s", *v6);
status = 1;
}
else
{
add(i, v3, (__int64)*v6);
}
if ( !*++v6 )
break;
}
}
else
{
status = 0;
}
while ( 1 )
{
do
{
v10 = read(0, v7, 0x2000uLL);
v11 = v10;
if ( v10 <= 0 )
{
if ( v10 >= 0 )
exit(status);
err(1, "read");
}
v12 = head;
}
while ( !head );
do
{
v14 = (char *)v7;
v15 = v11;
while ( 1 )
{
if ( *(_DWORD *)(v12 + 12) )
func7(*(_DWORD *)(v12 + 8), v14, v15);
else
v13 = write(*(_DWORD *)(v12 + 8), v14, v15);
if ( v13 == -1 )
break;
v14 += v13;
v15 -= v13;
if ( !v15 )
goto LABEL_29;
}
warn("%s", *(const char **)(v12 + 16));
status = 1;
LABEL_29:
v12 = *(_QWORD *)v12;
}
while ( v12 );
}
}

original_code:
int
main(int argc, char *argv[]) {
    LIST *p;
    int n, fd, rval, wval;
    char *bp;
    int append, strip_ansi, ch, exitval;
    char *buf;
    append = 0;
    strip_ansi = 0;
    while ((ch = getopt(argc, argv, "aish")) != -1) {
        switch((char)ch) {
            case 'a':
                append = 1;
                break;
            case 'i':
                (void)signal(2, ((__sighandler_t) 1));
                break;
            case 's':
                strip_ansi = 1;
                break;
            case '?':
            case 'h':
            default:
                usage();
        }
    }
    argv += optind;
    argc -= optind;
    if ((buf = malloc((8 * 1024))) == ((void *)0)) {
        err(1, "malloc");
    }
    add(1, 0, "stdout");
    for (exitval = 0; *argv; ++argv) {
        if ((fd = open(*argv,
                       01 | 0100 | (append ? 02000 : 01000),
                       (0400|0200|(0400 >> 3)|(0200 >> 3)|((0400 >> 3) >> 3)|((0200 >> 3) >> 3)))) < 0) {
            warn("%s", *argv);
            exitval = 1;
        } else {
            add(fd, strip_ansi, *argv);
        }
    }
    while ((rval = read(0, buf, (8 * 1024))) > 0) {
        for (p = head; p; p = p->next) {
            n = rval;
            bp = buf;
            do {
                if (p->strip_ansi == 0) {
                    wval = write(p->fd, bp, n);
                } else {
                    wval = write_without_ansi(p->fd, bp, n);
                }
                if (wval == -1) {
                    warn("%s", p->name);
                    exitval = 1;
                    break;
                }
                bp += wval;
            } while (n -= wval);
        }
    }
    if (rval < 0) {
        err(1, "read");
    }
    exit(exitval);
}

canonical_original_code:
int main(int argc, char *argv[]) {
    struct _list *p;
    int n; int fd; int rval; int wval;
    char *bp;
    int append; int strip_ansi; int ch; int exitval;
    char *buf;
    append = 0;
    strip_ansi = 0;
    while ((ch = getopt(argc, argv, "aish")) != -1) {
        switch((char)ch) {
            case 'a':
                append = 1;
                break;
            case 'i':
                (void)signal(2, ((void (*)(int)) 1));
                break;
            case 's':
                strip_ansi = 1;
                break;
            case '?':
            case 'h':
            default:
                usage();
        }
    }
    argv += optind;
    argc -= optind;
    if ((buf = malloc((8 * 1024))) == ((void *)0)) {
        err(1, "malloc");
    }
    add(1, 0, "stdout");
    for (exitval = 0; *argv; ++argv) {
        if ((fd = open(*argv,
                       01 | 0100 | (append ? 02000 : 01000),
                       (0400|0200|(0400 >> 3)|(0200 >> 3)|((0400 >> 3) >> 3)|((0200 >> 3) >> 3)))) < 0) {
            warn("%s", *argv);
            exitval = 1;
        } else {
            add(fd, strip_ansi, *argv);
        }
    }
    while ((rval = read(0, buf, (8 * 1024))) > 0) {
        for (p = head; p; p = p->next) {
            n = rval;
            bp = buf;
            do {
                if (p->strip_ansi == 0) {
                    wval = write(p->fd, bp, n);
                } else {
                    wval = write_without_ansi(p->fd, bp, n);
                }
                if (wval == -1) {
                    warn("%s", p->name);
                    exitval = 1;
                    break;
                }
                bp += wval;
            } while (n -= wval);
        }
    }
    if (rval < 0) {
        err(1, "read");
    }
    exit(exitval);
}

variable_types: struct _list { struct _list *next; int fd; int strip_ansi; char *name; } *

variable_types: n int

variable_types: rval int

variable_types: wval int

variable_types:
bp
char *

variable_types: append int

variable_types: ch int

variable_types: exitval int

variable_types:
buf
char *


repo: thrimbor/mandelbrot

decompiled_code:
__m128 __fastcall complex_mul(__m128 a1, __m128 a2)
{
return _mm_addsub_ps(
_mm_mul_ps(a2, _mm_moveldup_ps(a1)),
_mm_mul_ps(_mm_shuffle_ps(a2, a2, 177), _mm_movehdup_ps(a1)));
}

canonical_decompiled_code:
__m128 __fastcall func2(__m128 a1, __m128 a2)
{
return _mm_addsub_ps(
_mm_mul_ps(a2, _mm_moveldup_ps(a1)),
_mm_mul_ps(_mm_shuffle_ps(a2, a2, 177), _mm_movehdup_ps(a1)));
}

canonical_original_code:
static inline float complex_mul(float a, float b)
{
 return _mm_addsub_ps( _mm_mul_ps(_mm_moveldup_ps(a), b),
        _mm_mul_ps(_mm_movehdup_ps(a), ((float) __builtin_ia32_shufps ((__v4sf)(__m128)(b), (__v4sf)(__m128)(b), (int)((((2) << 6) | ((3) << 4) | ((0) << 2) | (1)))))));
}

variable_types: a float

variable_types: b float

return_type: float


testEscapeSeriesForPoint

original_code:
__attribute__ ((hot)) static inline void
testEscapeSeriesForPoint(__m128 c, int maxIterations, int* it1, int* it2)
{
 float r = 2*2;
 float f = 0;
 float g = 0;
    __m128 z = _mm_setzero_ps();
 int iteration1 = 0;
 int iteration2 = 0;
 int it1_done = 0;
 int it2_done = 0;
 float f_sqrt=0, g_sqrt=0;
 while (iteration1 < maxIterations && iteration2 < maxIterations)
 {
  complex_abs_sqr(z, &f, &g);
  if ((f > r) && (g > r)) {
   break;
  } else if (f>r && !it1_done) {
   it1_done = -1;
   f_sqrt = sqrt(f);
   z = ((__m128) __builtin_ia32_shufps ((__v4sf)(__m128)(z), (__v4sf)(__m128)(z), (int)((((3) << 6) | ((2) << 4) | ((3) << 2) | (2)))));
  } else if (g>r && !it2_done) {
   it2_done = -1;
   g_sqrt = sqrt(g);
   z = ((__m128) __builtin_ia32_shufps ((__v4sf)(__m128)(z), (__v4sf)(__m128)(z), (int)((((1) << 6) | ((0) << 4) | ((1) << 2) | (0)))));
  }
  if (it1_done && it2_done) break;
  z = _mm_add_ps(complex_mul(z,z), c);
  if (!it1_done)
   iteration1++;
  if (!it2_done)
   iteration2++;
 }
 if (iteration1 < maxIterations)
 {
  if (!it1_done) f_sqrt = sqrt(f);
  iteration1 += 1.0f - (log(log(f_sqrt) / logof2) / logof2);
 }
 if (iteration2 < maxIterations)
 {
  if (!it2_done) g_sqrt = sqrt(g);
  iteration2 += 1.0f - (log(log(g_sqrt) / logof2) / logof2);
 }
 *it1 = iteration1;
 *it2 = iteration2;
}

canonical_original_code:
static inline void testEscapeSeriesForPoint(float c, int maxIterations, int *it1, int *it2)
{
 float r = 2*2;
 float f = 0;
 float g = 0;
    float z = _mm_setzero_ps();
 int iteration1 = 0;
 int iteration2 = 0;
 int it1_done = 0;
 int it2_done = 0;
 float f_sqrt = 0; float g_sqrt = 0;
 while (iteration1 < maxIterations && iteration2 < maxIterations)
 {
  complex_abs_sqr(z, &f, &g);
  if ((f > r) && (g > r)) {
   break;
  } else if (f>r && !it1_done) {
   it1_done = -1;
   f_sqrt = sqrt(f);
   z = ((float) __builtin_ia32_shufps ((__v4sf)(__m128)(z), (__v4sf)(__m128)(z), (int)((((3) << 6) | ((2) << 4) | ((3) << 2) | (2)))));
  } else if (g>r && !it2_done) {
   it2_done = -1;
   g_sqrt = sqrt(g);
   z = ((float) __builtin_ia32_shufps ((__v4sf)(__m128)(z), (__v4sf)(__m128)(z), (int)((((1) << 6) | ((0) << 4) | ((1) << 2) | (0)))));
  }
  if (it1_done && it2_done) break;
  z = _mm_add_ps(complex_mul(z,z), c);
  if (!it1_done)
   iteration1++;
  if (!it2_done)
   iteration2++;
 }
 if (iteration1 < maxIterations)
 {
  if (!it1_done) f_sqrt = sqrt(f);
  iteration1 += 1.0f - (log(log(f_sqrt) / logof2) / logof2);
 }
 if (iteration2 < maxIterations)
 {
  if (!it2_done) g_sqrt = sqrt(g);
  iteration2 += 1.0f - (log(log(g_sqrt) / logof2) / logof2);
 }
 *it1 = iteration1;
 *it2 = iteration2;
}

variable_types: c float

variable_types: maxIterations int

variable_types: r float

variable_types: f float

variable_types: g float

variable_types: z float


help

decompiled_code:
int help()
{
puts("USAGE: mandelbrot_cli [OPTIONS]");
puts("With [OPTIONS]:");
puts("\t -i --maxiterations INT \t maximum number of series iterations per pixel");
puts("\t -o --outfile FILE \t filename (without appendix) for output picture in PPM format");
return putchar(10);
}

canonical_decompiled_code:
int func10()
{
puts("USAGE: mandelbrot_cli [OPTIONS]");
puts("With [OPTIONS]:");
puts("\t -i --maxiterations INT \t maximum number of series iterations per pixel");
puts("\t -o --outfile FILE \t filename (without appendix) for output picture in PPM format");
return putchar(10);
}

original_code:
void
help(void)
{
    printf("USAGE: mandelbrot_cli [OPTIONS]\n");
    printf("With [OPTIONS]:\n");
    printf("\t -i --maxiterations INT \t maximum number of series iterations per pixel\n");
    printf("\t -o --outfile FILE \t filename (without appendix) for output picture in PPM format\n");
    printf("\n");
}

canonical_original_code:
void help()
{
    printf("USAGE: mandelbrot_cli [OPTIONS]\n");
    printf("With [OPTIONS]:\n");
    printf("\t -i --maxiterations INT \t maximum number of series iterations per pixel\n");
    printf("\t -o --outfile FILE \t filename (without appendix) for output picture in PPM format\n");
    printf("\n");
}


main

repo: tigerjibo/yuv

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
int v4; // er12
const char *v5; // r15
int v6; // er13
double v8; // xmm0_8
double v9; // xmm0_8
double v10; // xmm0_8
double v11; // xmm0_8
double v12; // xmm0_8
int v13; // [rsp+8h] [rbp-60h]
void *v14; // [rsp+18h] [rbp-50h] BYREF
void *ptr; // [rsp+20h] [rbp-48h] BYREF
int v16; // [rsp+28h] [rbp-40h] BYREF
int v17[15]; // [rsp+2Ch] [rbp-3Ch] BYREF

v3 = atoi(argv[1]);
v4 = atoi(argv[2]);
v13 = atoi(argv[3]);
v5 = argv[4];
ptr = 0LL;
v14 = 0LL;
if ( (unsigned int)get_yuv_format(v17, argv[5], v3, v4, &ptr) )
return -1;
v6 = get_yuv_format(&v16, argv[6], v3, v4, &v14);
if ( v6 )
return -1;
if ( argc > 7 && atoi(argv[7]) )
{
v8 = test_3d(v13, v17[0], ptr, v16, v14, v3, v4);
printf("i420_3d_to_yuyv422_sbs> elapsed=%fms, avg=%fms\n", v8, v8 / (double)v13);
}
else if ( v17[0] == 1 )
{
v9 = test(
v13,
1,
ptr,
v16,
v14,
v3,
v4,
(__int64)v5,
(__int64)"i420_4b.yuv",
(void (__fastcall *)(void *, const void *, _QWORD, _QWORD))&uyvy422_to_i420_4byte);
printf("uyvy_to_i420_4b> elapsed=%fms, avg=%fms\n", v9, v9 / (double)v13);
v10 = test(
v13,
v17[0],
ptr,
v16,
v14,
v3,
v4,
(__int64)v5,
(__int64)"i420_wh.yuv",
(void (__fastcall *)(void *, const void *, _QWORD, _QWORD))&uyvy422_to_i420_wh);
printf("uyvy_to_i420_wh> elapsed=%fms, avg=%fms\n", v10, v10 / (double)v13);
}
else if ( !v17[0] && v16 == 1 )
{
v11 = test(
v13,
0,
ptr,
1,
v14,
v3,
v4,
(__int64)v5,
(__int64)"uyvy422.yuv",
(void (__fastcall *)(void *, const void *, _QWORD, _QWORD))&i420_to_uyvy422);
printf("i420_to_uyvy> elapsed=%fms, avg=%fms\n", v11, v11 / (double)v13);
v12 = test(
v13,
v17[0],
ptr,
v16,
v14,
v3,
v4,
(__int64)v5,
(__int64)"uyvy422_bottom.yuv",
(void (__fastcall *)(void *, const void *, _QWORD, _QWORD))&i420_to_uyvy422_bottom);
printf("i420_to_uyvy_bottom> elapsed=%fms, avg=%fms\n", v12, v12 / (double)v13);
}
else
{
printf("fopen(%s) failed\n", v5);
}
if ( ptr )
free(ptr);
if ( v14 )
free(v14);
return v6;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
int v4; // er12
const char *v5; // r15
int v6; // er13
double v8; // xmm0_8
double v9; // xmm0_8
double v10; // xmm0_8
double v11; // xmm0_8
double v12; // xmm0_8
int v13; // [rsp+8h] [rbp-60h]
void *v14; // [rsp+18h] [rbp-50h] BYREF
void *ptr; // [rsp+20h] [rbp-48h] BYREF
int v16; // [rsp+28h] [rbp-40h] BYREF
int v17[15]; // [rsp+2Ch] [rbp-3Ch] BYREF

v3 = atoi(argv[1]);
v4 = atoi(argv[2]);
v13 = atoi(argv[3]);
v5 = argv[4];
ptr = 0LL;
v14 = 0LL;
if ( (unsigned int)func1(v17, argv[5], v3, v4, &ptr) )
return -1;
v6 = func1(&v16, argv[6], v3, v4, &v14);
if ( v6 )
return -1;
if ( argc > 7 && atoi(argv[7]) )
{
v8 = func5(v13, v17[0], ptr, v16, v14, v3, v4);
printf("i420_3d_to_yuyv422_sbs> elapsed=%fms, avg=%fms\n", v8, v8 / (double)v13);
}
else if ( v17[0] == 1 )
{
v9 = func6(
v13,
1,
ptr,
v16,
v14,
v3,
v4,
(__int64)v5,
(__int64)"i420_4b.yuv",
(void (__fastcall *)(void *, const void *, _QWORD, _QWORD))&uyvy422_to_i420_4byte);
printf("uyvy_to_i420_4b> elapsed=%fms, avg=%fms\n", v9, v9 / (double)v13);
v10 = func6(
v13,
v17[0],
ptr,
v16,
v14,
v3,
v4,
(__int64)v5,
(__int64)"i420_wh.yuv",
(void (__fastcall *)(void *, const void *, _QWORD, _QWORD))&uyvy422_to_i420_wh);
printf("uyvy_to_i420_wh> elapsed=%fms, avg=%fms\n", v10, v10 / (double)v13);
}
else if ( !v17[0] && v16 == 1 )
{
v11 = func6(
v13,
0,
ptr,
1,
v14,
v3,
v4,
(__int64)v5,
(__int64)"uyvy422.yuv",
(void (__fastcall *)(void *, const void *, _QWORD, _QWORD))&i420_to_uyvy422);
printf("i420_to_uyvy> elapsed=%fms, avg=%fms\n", v11, v11 / (double)v13);
v12 = func6(
v13,
v17[0],
ptr,
v16,
v14,
v3,
v4,
(__int64)v5,
(__int64)"uyvy422_bottom.yuv",
(void (__fastcall *)(void *, const void *, _QWORD, _QWORD))&i420_to_uyvy422_bottom);
printf("i420_to_uyvy_bottom> elapsed=%fms, avg=%fms\n", v12, v12 / (double)v13);
}
else
{
printf("fopen(%s) failed\n", v5);
}
if ( ptr )
free(ptr);
if ( v14 )
free(v14);
return v6;
}

original_code:
int main(int argc, char **argv)
{
    unsigned int w = (unsigned int) atoi(argv[1]);
    unsigned int h = (unsigned int) atoi(argv[2]);
    int count = atoi(argv[3]);
    char *filepath = argv[4];
    enum yuv_format sf, df;
    char *src = ((void *)0);
    char *dst = ((void *)0);
    double elapsed;
    if (_get_yuv_format(&sf, argv[5], w, h, &src))
        return -1;
    if (_get_yuv_format(&df, argv[6], w, h, &dst))
        return -1;
    if (argc >= 8) {
        if (atoi(argv[7])) {
            elapsed = _test_3d(count, sf, src, df, dst, w, h, filepath, "yuyv422_sbs.yuv");
            printf("i420_3d_to_yuyv422_sbs> elapsed=%fms, avg=%fms\n", elapsed, elapsed / count);
            goto exit;
        }
    }
    if (sf == yuv_uyvy) {
        elapsed = _test(count, sf, src, df, dst, w, h, filepath, "i420_4b.yuv", uyvy422_to_i420_4byte);
        printf("uyvy_to_i420_4b> elapsed=%fms, avg=%fms\n", elapsed, elapsed / count);
        elapsed = _test(count, sf, src, df, dst, w, h, filepath, "i420_wh.yuv", uyvy422_to_i420_wh);
        printf("uyvy_to_i420_wh> elapsed=%fms, avg=%fms\n", elapsed, elapsed / count);
    } else if (sf == yuv_i420 && df == yuv_uyvy) {
        elapsed = _test(count, sf, src, df, dst, w, h, filepath, "uyvy422.yuv", i420_to_uyvy422);
        printf("i420_to_uyvy> elapsed=%fms, avg=%fms\n", elapsed, elapsed / count);
        elapsed = _test(count, sf, src, df, dst, w, h, filepath, "uyvy422_bottom.yuv", i420_to_uyvy422_bottom);
        printf("i420_to_uyvy_bottom> elapsed=%fms, avg=%fms\n", elapsed, elapsed / count);
    } else {
        printf("fopen(%s) failed\n", filepath);
        goto exit;
    }
exit:
    if (src)
        free(src);
    if (dst)
        free(dst);
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    unsigned int w = (unsigned int) atoi(argv[1]);
    unsigned int h = (unsigned int) atoi(argv[2]);
    int count = atoi(argv[3]);
    char *filepath = argv[4];
    enum yuv_format sf; enum yuv_format df;
    char *src = ((void *)0);
    char *dst = ((void *)0);
    double elapsed;
    if (_get_yuv_format(&sf, argv[5], w, h, &src))
        return -1;
    if (_get_yuv_format(&df, argv[6], w, h, &dst))
        return -1;
    if (argc >= 8) {
        if (atoi(argv[7])) {
            elapsed = _test_3d(count, sf, src, df, dst, w, h, filepath, "yuyv422_sbs.yuv");
            printf("i420_3d_to_yuyv422_sbs> elapsed=%fms, avg=%fms\n", elapsed, elapsed / count);
            goto exit;
        }
    }
    if (sf == yuv_uyvy) {
        elapsed = _test(count, sf, src, df, dst, w, h, filepath, "i420_4b.yuv", uyvy422_to_i420_4byte);
        printf("uyvy_to_i420_4b> elapsed=%fms, avg=%fms\n", elapsed, elapsed / count);
        elapsed = _test(count, sf, src, df, dst, w, h, filepath, "i420_wh.yuv", uyvy422_to_i420_wh);
        printf("uyvy_to_i420_wh> elapsed=%fms, avg=%fms\n", elapsed, elapsed / count);
    } else if (sf == yuv_i420 && df == yuv_uyvy) {
        elapsed = _test(count, sf, src, df, dst, w, h, filepath, "uyvy422.yuv", i420_to_uyvy422);
        printf("i420_to_uyvy> elapsed=%fms, avg=%fms\n", elapsed, elapsed / count);
        elapsed = _test(count, sf, src, df, dst, w, h, filepath, "uyvy422_bottom.yuv", i420_to_uyvy422_bottom);
        printf("i420_to_uyvy_bottom> elapsed=%fms, avg=%fms\n", elapsed, elapsed / count);
    } else {
        printf("fopen(%s) failed\n", filepath);
        goto exit;
    }
exit:
    if (src)
        free(src);
    if (dst)
        free(dst);
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
w
unsigned int

variable_types:
h
unsigned int

variable_types: count int

variable_types:
filepath
char *

variable_types: enum yuv_format { yuv_i420=0, yuv_uyvy=1, yuv_yuyv=2 }

variable_types:
src
char *

variable_types:
dst
char *

variable_types: elapsed double


repo: tigerking/zmodem-android

decompiled_code:
void tcp_alarm_handler()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
static void
tcp_alarm_handler(int dummy __attribute__((__unused__)))
{
}

canonical_original_code:
static void tcp_alarm_handler(int dummy)
{
}

variable_types: dummy int


decompiled_code:
__int64 __fastcall tcp_server(char *s)
{
int v1; // ebx
unsigned __int16 v2; // bp
char *v3; // rax
int *v5; // rax
int *v6; // rax
int *v7; // rax
int *v8; // rax
int *v9; // rax
__int64 v10; // [rsp+0h] [rbp-48h] BYREF
int optval; // [rsp+Ch] [rbp-3Ch] BYREF
struct sockaddr v12; // [rsp+10h] [rbp-38h] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-28h] BYREF

optval = 1;
v1 = socket(2, 1, 6);
if ( v1 < 0 )
{
v5 = _errno_location();
error(1, *v5, "socket");
}
if ( setsockopt(v1, 1, 2, &optval, 4u) < 0 )
{
v6 = _errno_location();
error(1, *v6, "setsockopt (reuse address)");
}
*(_QWORD *)&addr.sa_family = 2LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_QWORD *)&addr.sa_data[2] = (unsigned int)_bswap_32(0);
if ( bind(v1, &addr, 0x10u) < 0 )
{
v7 = _errno_location();
error(1, *v7, "bind");
}
v10 = 16LL;
if ( getsockname(v1, &v12, (socklen_t *)&v10) )
{
v8 = _errno_location();
error(1, *v8, "getsockname");
}
v2 = _bswap_16(*(__int16 *)v12.sa_data);
v3 = inet_ntoa(*(struct in_addr *)&v12.sa_data[2]);
sprintf(s, "[%s] <%d>\n", v3, v2);
if ( listen(v1, 1) < 0 )
{
v9 = _errno_location();
error(1, *v9, "listen");
}
getsockname(v1, &v12, (socklen_t *)&v10);
return (unsigned int)v1;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *s)
{
int v1; // ebx
unsigned __int16 v2; // bp
char *v3; // rax
int *v5; // rax
int *v6; // rax
int *v7; // rax
int *v8; // rax
int *v9; // rax
__int64 v10; // [rsp+0h] [rbp-48h] BYREF
int optval; // [rsp+Ch] [rbp-3Ch] BYREF
struct sockaddr v12; // [rsp+10h] [rbp-38h] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-28h] BYREF

optval = 1;
v1 = socket(2, 1, 6);
if ( v1 < 0 )
{
v5 = _errno_location();
error(1, *v5, "socket");
}
if ( setsockopt(v1, 1, 2, &optval, 4u) < 0 )
{
v6 = _errno_location();
error(1, *v6, "setsockopt (reuse address)");
}
*(_QWORD *)&addr.sa_family = 2LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_QWORD *)&addr.sa_data[2] = (unsigned int)func1(0);
if ( bind(v1, &addr, 0x10u) < 0 )
{
v7 = _errno_location();
error(1, *v7, "bind");
}
v10 = 16LL;
if ( getsockname(v1, &v12, (socklen_t *)&v10) )
{
v8 = _errno_location();
error(1, *v8, "getsockname");
}
v2 = func2(*(__int16 *)v12.sa_data);
v3 = inet_ntoa(*(struct in_addr *)&v12.sa_data[2]);
sprintf(s, "[%s] <%d>\n", v3, v2);
if ( listen(v1, 1) < 0 )
{
v9 = _errno_location();
error(1, *v9, "listen");
}
getsockname(v1, &v12, (socklen_t *)&v10);
return (unsigned int)v1;
}

original_code:
int
tcp_server (char *buf)
{
 int sock;
 struct sockaddr_in s;
 struct sockaddr_in t;
 int on=1;
 size_t len;
 if ((sock = socket (2, SOCK_STREAM, IPPROTO_TCP)) < 0) {
  error(1,(*__errno_location ()),"socket");
 }
 if (setsockopt(sock, 1, 2, (char *)&on, sizeof (on)) < 0) {
  error(1,(*__errno_location ()),"setsockopt (reuse address)");
 }
 memset (&s, 0, sizeof (s));
 s.sin_family = 2;
 s.sin_port=0;
 s.sin_addr.s_addr=htonl(((in_addr_t) 0x00000000));
 if (bind(sock, (struct sockaddr *)&s, sizeof (s)) < 0) {
  error(1,(*__errno_location ()),"bind");
 }
 len=sizeof(t);
 if (getsockname (sock, (struct sockaddr *) &t, &len)) {
  error(1,(*__errno_location ()),"getsockname");
 }
 sprintf(buf,"[%s] <%d>\n",inet_ntoa(t.sin_addr),ntohs(t.sin_port));
 if (listen(sock, 1) < 0) {
  error(1,(*__errno_location ()),"listen");
 }
 getsockname (sock, (struct sockaddr *) &t, &len);
 return (sock);
}

canonical_original_code:
int tcp_server(char *buf)
{
 int sock;
 struct sockaddr_in s;
 struct sockaddr_in t;
 int on = 1;
 unsigned long len;
 if ((sock = socket (2, SOCK_STREAM, IPPROTO_TCP)) < 0) {
  error(1,(*__errno_location ()),"socket");
 }
 if (setsockopt(sock, 1, 2, (char *)&on, sizeof (on)) < 0) {
  error(1,(*__errno_location ()),"setsockopt (reuse address)");
 }
 memset (&s, 0, sizeof (s));
 s.sin_family = 2;
 s.sin_port=0;
 s.sin_addr.s_addr=htonl(((unsigned int) 0x00000000));
 if (bind(sock, (struct sockaddr *)&s, sizeof (s)) < 0) {
  error(1,(*__errno_location ()),"bind");
 }
 len=sizeof(t);
 if (getsockname (sock, (struct sockaddr *) &t, &len)) {
  error(1,(*__errno_location ()),"getsockname");
 }
 sprintf(buf,"[%s] <%d>\n",inet_ntoa(t.sin_addr),ntohs(t.sin_port));
 if (listen(sock, 1) < 0) {
  error(1,(*__errno_location ()),"listen");
 }
 getsockname (sock, (struct sockaddr *) &t, &len);
 return (sock);
}

variable_types:
buf
char *

variable_types: sock int

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

variable_types: on int

variable_types:
len
unsigned long

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

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


decompiled_code:
__int64 __fastcall tcp_accept(int fd)
{
int v1; // ebp
int v2; // ebx
int v3; // esi
socklen_t addr_len[2]; // [rsp+8h] [rbp-30h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

*(_QWORD *)addr_len = 16LL;
*(_QWORD *)&addr.sa_family = 0LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
v1 = 6;
while ( 1 )
{
signal(14, (__sighandler_t)tcp_alarm_handler);
alarm(0x1Eu);
v2 = accept(fd, &addr, addr_len);
if ( v2 >= 0 )
break;
v3 = *_errno_location();
if ( v3 == 4 )
{
if ( --v1 )
continue;
}
error(1, v3, "accept");
break;
}
alarm(0);
return (unsigned int)v2;
}

canonical_decompiled_code:
__int64 __fastcall func4(int fd)
{
int v1; // ebp
int v2; // ebx
int v3; // esi
socklen_t addr_len[2]; // [rsp+8h] [rbp-30h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

*(_QWORD *)addr_len = 16LL;
*(_QWORD *)&addr.sa_family = 0LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
v1 = 6;
while ( 1 )
{
signal(14, (__sighandler_t)tcp_alarm_handler);
alarm(0x1Eu);
v2 = accept(fd, &addr, addr_len);
if ( v2 >= 0 )
break;
v3 = *_errno_location();
if ( v3 == 4 )
{
if ( --v1 )
continue;
}
error(1, v3, "accept");
break;
}
alarm(0);
return (unsigned int)v2;
}

original_code:
int
tcp_accept (int d)
{
 int so;
 struct sockaddr_in s;
 size_t namelen;
 int num=0;
 namelen = sizeof(s);
 memset((char*)&s,0, namelen);
retry:
 signal(14, tcp_alarm_handler);
 alarm(30);
 if ((so = accept(d, (struct sockaddr*)&s, &namelen)) < 0) {
  if ((*__errno_location ()) == 4) {
   if (++num<=5)
    goto retry;
  }
  error(1,(*__errno_location ()),"accept");
 }
 alarm(0);
 return so;
}

canonical_original_code:
int tcp_accept(int d)
{
 int so;
 struct sockaddr_in s;
 unsigned long namelen;
 int num = 0;
 namelen = sizeof(s);
 memset((char *)&s,0, namelen);
retry:
 signal(14, tcp_alarm_handler);
 alarm(30);
 if ((so = accept(d, (struct sockaddr *)&s, &namelen)) < 0) {
  if ((*__errno_location ()) == 4) {
   if (++num<=5)
    goto retry;
  }
  error(1,(*__errno_location ()),"accept");
 }
 alarm(0);
 return so;
}

variable_types: d int

variable_types: so int

variable_types:
namelen
unsigned long

variable_types: num int


decompiled_code:
__int64 __fastcall tcp_connect(__int64 a1)
{
const char *v1; // rbp
char *v2; // r12
_BYTE *v3; // rbx
const unsigned __int16 *v4; // rcx
struct hostent *v5; // rbp
const char *v6; // rbx
__int16 v7; // ax
int v8; // ebx
int *v10; // rax
int *v11; // rax
struct sockaddr dest[2]; // [rsp+0h] [rbp-28h] BYREF

*(_QWORD *)&dest[0].sa_family = 2LL;
*(_QWORD *)&dest[0].sa_data[6] = 0LL;
if ( *(_BYTE *)a1 != 91 )
error(1, 0, "tcp_connect: illegal format1\n");
v1 = (const char *)(a1 + 1);
v2 = strchr((const char *)(a1 + 1), 93);
if ( !v2 )
error(1, 0, "tcp_connect: illegal format2\n");
v3 = v2 + 1;
*v2 = 0;
*(_DWORD *)&dest[0].sa_data[2] = inet_addr(v1);
if ( *(_DWORD *)&dest[0].sa_data[2] == -1 )
{
v5 = gethostbyname(v1);
if ( !v5 )
error(1, 0, "tcp_connect: illegal format3\n");
memcpy(&dest[0].sa_data[2], *(const void **)v5->h_addr_list, v5->h_length);
}
v4 = *_ctype_b_loc();
while ( (v4[(unsigned __int8)*v3] & 0x2000) != 0 )
++v3;
if ( *v3 != 60 )
error(1, 0, "tcp_connect: illegal format4\n");
v6 = v3 + 1;
if ( !strchr(v6, 62) )
error(1, 0, "tcp_connect: illegal format5\n");
v7 = strtol(v6, 0LL, 10);
*(_WORD *)dest[0].sa_data = _bswap_16(v7);
v8 = socket(2, 1, 6);
if ( v8 < 0 )
{
v10 = _errno_location();
error(1, *v10, "socket");
}
signal(14, (__sighandler_t)tcp_alarm_handler);
alarm(0x1Eu);
if ( connect(v8, dest, 0x10u) < 0 )
{
v11 = _errno_location();
error(1, *v11, "connect");
}
alarm(0);
return (unsigned int)v8;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
const char *v1; // rbp
char *v2; // r12
_BYTE *v3; // rbx
const unsigned __int16 *v4; // rcx
struct hostent *v5; // rbp
const char *v6; // rbx
__int16 v7; // ax
int v8; // ebx
int *v10; // rax
int *v11; // rax
struct sockaddr dest[2]; // [rsp+0h] [rbp-28h] BYREF

*(_QWORD *)&dest[0].sa_family = 2LL;
*(_QWORD *)&dest[0].sa_data[6] = 0LL;
if ( *(_BYTE *)a1 != 91 )
error(1, 0, "tcp_connect: illegal format1\n");
v1 = (const char *)(a1 + 1);
v2 = strchr((const char *)(a1 + 1), 93);
if ( !v2 )
error(1, 0, "tcp_connect: illegal format2\n");
v3 = v2 + 1;
*v2 = 0;
*(_DWORD *)&dest[0].sa_data[2] = inet_addr(v1);
if ( *(_DWORD *)&dest[0].sa_data[2] == -1 )
{
v5 = gethostbyname(v1);
if ( !v5 )
error(1, 0, "tcp_connect: illegal format3\n");
memcpy(&dest[0].sa_data[2], *(const void **)v5->h_addr_list, v5->h_length);
}
v4 = *_ctype_b_loc();
while ( (v4[(unsigned __int8)*v3] & 0x2000) != 0 )
++v3;
if ( *v3 != 60 )
error(1, 0, "tcp_connect: illegal format4\n");
v6 = v3 + 1;
if ( !strchr(v6, 62) )
error(1, 0, "tcp_connect: illegal format5\n");
v7 = strtol(v6, 0LL, 10);
*(_WORD *)dest[0].sa_data = func2(v7);
v8 = socket(2, 1, 6);
if ( v8 < 0 )
{
v10 = _errno_location();
error(1, *v10, "socket");
}
signal(14, (__sighandler_t)tcp_alarm_handler);
alarm(0x1Eu);
if ( connect(v8, dest, 0x10u) < 0 )
{
v11 = _errno_location();
error(1, *v11, "connect");
}
alarm(0);
return (unsigned int)v8;
}

original_code:
int
tcp_connect (char *buf)
{
 int sock;
 struct sockaddr_in s_in;
 char *p;
 char *q;
 memset(&s_in,0,sizeof(s_in));
 s_in.sin_family = 2;
 if (*buf!='[') {
  error(1,0,"tcp_connect: illegal format1\n");
 }
 p=strchr(buf+1,']');
 if (!p) {
  error(1,0,"tcp_connect: illegal format2\n");
 }
 *p++=0;
 s_in.sin_addr.s_addr=inet_addr(buf+1);
 if (s_in.sin_addr.s_addr== (unsigned long) ((in_addr_t) 0xffffffff)) {
  struct hostent *h=gethostbyname(buf+1);
  if (!h)
   error(1,0,"tcp_connect: illegal format3\n");
  memcpy(& s_in.sin_addr.s_addr,h->h_addr_list[0],h->h_length);
 }
 while (((*__ctype_b_loc ())[(int) (((unsigned char)(*p)))] & (unsigned short int) _ISspace))
  p++;
 if (*p!='<') {
  error(1,0,"tcp_connect: illegal format4\n");
 }
 q=strchr(p+1,'>');
 if (!q)
  error(1,0,"tcp_connect: illegal format5\n");
 s_in.sin_port = htons(strtol(p+1,((void *)0),10));
 if ((sock = socket (2, SOCK_STREAM, IPPROTO_TCP)) < 0) {
  error(1,(*__errno_location ()),"socket");
 }
 signal(14, tcp_alarm_handler);
 alarm(30);
 if (connect (sock, (struct sockaddr *) &s_in, sizeof (s_in)) < 0) {
  error(1,(*__errno_location ()),"connect");
 }
 alarm(0);
 return (sock);
}

canonical_original_code:
int tcp_connect(char *buf)
{
 int sock;
 struct sockaddr_in s_in;
 char *p;
 char *q;
 memset(&s_in,0,sizeof(s_in));
 s_in.sin_family = 2;
 if (*buf!='[') {
  error(1,0,"tcp_connect: illegal format1\n");
 }
 p=strchr(buf+1,']');
 if (!p) {
  error(1,0,"tcp_connect: illegal format2\n");
 }
 *p++=0;
 s_in.sin_addr.s_addr=inet_addr(buf+1);
 if (s_in.sin_addr.s_addr== (unsigned long) ((unsigned int) 0xffffffff)) {
  struct hostent *h = gethostbyname(buf+1);
  if (!h)
   error(1,0,"tcp_connect: illegal format3\n");
  memcpy(& s_in.sin_addr.s_addr,h->h_addr_list[0],h->h_length);
 }
 while (((*__ctype_b_loc ())[(int) (((unsigned char)(*p)))] & (unsigned short) _ISspace))
  p++;
 if (*p!='<') {
  error(1,0,"tcp_connect: illegal format4\n");
 }
 q=strchr(p+1,'>');
 if (!q)
  error(1,0,"tcp_connect: illegal format5\n");
 s_in.sin_port = htons(strtol(p+1,((void *)0),10));
 if ((sock = socket (2, SOCK_STREAM, IPPROTO_TCP)) < 0) {
  error(1,(*__errno_location ()),"socket");
 }
 signal(14, tcp_alarm_handler);
 alarm(30);
 if (connect (sock, (struct sockaddr *) &s_in, sizeof (s_in)) < 0) {
  error(1,(*__errno_location ()),"connect");
 }
 alarm(0);
 return (sock);
}

variable_types:
p
char *

variable_types:
q
char *

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


getspeed

decompiled_code:
__int64 __fastcall getspeed(int a1)
{
_DWORD *v1; // rax
unsigned int v2; // edx

v1 = &unk_4A4;
v2 = 110;
while ( *v1 != a1 )
{
v1 += 2;
v2 = *(v1 - 1);
if ( !v2 )
return 38400;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
_DWORD *v1; // rax
unsigned int v2; // edx

v1 = &unk_4A4;
v2 = 110;
while ( *v1 != a1 )
{
v1 += 2;
v2 = *(v1 - 1);
if ( !v2 )
return 38400;
}
return v2;
}

original_code:
static unsigned
getspeed(speed_t code)
{
 int n;
 for (n=0; speeds[n].baudr; ++n)
  if (speeds[n].speedcode == code)
   return speeds[n].baudr;
 return 38400;
}

canonical_original_code:
static unsigned int getspeed(unsigned int code)
{
 int n;
 for (n=0; speeds[n].baudr; ++n)
  if (speeds[n].speedcode == code)
   return speeds[n].baudr;
 return 38400;
}

variable_types:
code
unsigned int

variable_types: n int

return_type: unsigned int


decompiled_code:
_BOOL8 from_cu()
{
_BOOL8 result; // rax
struct stat v1; // [rsp+0h] [rbp-128h] BYREF
struct stat buf; // [rsp+90h] [rbp-98h] BYREF

v1.st_dev = 0LL;
buf.st_dev = 0LL;
v1.st_rdev = 0LL;
buf.st_rdev = 0LL;
fstat(1, &buf);
fstat(2, &v1);
result = 1LL;
if ( buf.st_rdev == v1.st_rdev )
result = buf.st_dev != v1.st_dev;
Fromcu = result;
return result;
}

canonical_decompiled_code:
_BOOL8 func1()
{
_BOOL8 result; // rax
struct stat v1; // [rsp+0h] [rbp-128h] BYREF
struct stat buf; // [rsp+90h] [rbp-98h] BYREF

v1.st_dev = 0LL;
buf.st_dev = 0LL;
v1.st_rdev = 0LL;
buf.st_rdev = 0LL;
fstat(1, &buf);
fstat(2, &v1);
result = 1LL;
if ( buf.st_rdev == v1.st_rdev )
result = buf.st_dev != v1.st_dev;
Fromcu = result;
return result;
}

original_code:
int
from_cu(void)
{
 struct stat a, b;
 int help=0;
 a.st_rdev=b.st_rdev=a.st_dev=b.st_dev=help;
 fstat(1, &a); fstat(2, &b);
 Fromcu = (a.st_rdev != b.st_rdev) || (a.st_dev != b.st_dev);
 return Fromcu;
}

canonical_original_code:
int from_cu()
{
 struct stat a; struct stat b;
 int help = 0;
 a.st_rdev=b.st_rdev=a.st_dev=b.st_dev=help;
 fstat(1, &a); fstat(2, &b);
 Fromcu = (a.st_rdev != b.st_rdev) || (a.st_dev != b.st_dev);
 return Fromcu;
}

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

variable_types: help int

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


rdchk

decompiled_code:
__int64 __fastcall rdchk(int a1)
{
ioctl(a1, 0x541BuLL, &lf_1);
return (unsigned int)lf_1;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
ioctl(a1, 0x541BuLL, &lf_1);
return (unsigned int)lf_1;
}

original_code:
int
rdchk(int fd)
{
 static long lf;
 ioctl(fd, 0x541B, &lf);
 return ((int) lf);
}

variable_types: fd int

variable_types: lf long


decompiled_code:
__int64 __fastcall io_mode(int fd, __int64 a2)
{
struct termios *v3; // rdi
struct termios *v4; // rsi
__int64 i; // rcx
__int64 result; // rax
tcflag_t v7; // eax
int v8; // eax
tcflag_t v9; // eax
bool v10; // zf
cc_t v11; // al

vfile("mode:%d", a2);
if ( (_DWORD)a2 != 2 )
{
if ( (int)a2 > 2 )
{
result = 0xFFFFFFFFLL;
if ( (_DWORD)a2 != 3 )
return result;
if ( did0_0 )
{
*(__m128i *)&tty.c_iflag = _mm_load_si128((const __m128i *)&oldtty);
*(__m128i *)&tty.c_line = _mm_load_si128((const __m128i *)&oldtty.c_line);
*(__m128i *)&tty.c_cc[15] = _mm_load_si128((const __m128i *)&oldtty.c_cc[15]);
*(_QWORD *)&tty.c_cc[31] = *(_QWORD *)&oldtty.c_cc[31];
tty.c_ospeed = oldtty.c_ospeed;
goto LABEL_30;
}
}
else
{
if ( !(_DWORD)a2 )
{
if ( !did0_0 )
return 0xFFFFFFFFLL;
tcflush(fd, 2);
tcsetattr(fd, 1, &oldtty);
tcflow(fd, 1);
return 0LL;
}
if ( (_DWORD)a2 != 1 )
return 0xFFFFFFFFLL;
if ( did0_0 )
{
v3 = &tty;
v4 = &oldtty;
for ( i = 15LL; i; --i )
{
v3->c_iflag = v4->c_iflag;
v4 = (struct termios *)((char *)v4 + 4);
v3 = (struct termios *)((char *)v3 + 4);
}
goto LABEL_14;
}
}
did0_0 = 1;
tcgetattr(fd, &oldtty);
*(__m128i *)&tty.c_iflag = _mm_load_si128((const __m128i *)&oldtty);
*(__m128i *)&tty.c_line = _mm_load_si128((const __m128i *)&oldtty.c_line);
*(__m128i *)&tty.c_cc[15] = _mm_load_si128((const __m128i *)&oldtty.c_cc[15]);
*(_QWORD *)&tty.c_cc[31] = *(_QWORD *)&oldtty.c_cc[31];
tty.c_ospeed = oldtty.c_ospeed;
if ( (_DWORD)a2 != 3 )
{
LABEL_14:
tty.c_iflag = 1;
LABEL_15:
tty.c_lflag &= 0xFFFFFFF4;
tty.c_oflag = 0;
v7 = tty.c_cflag & 0xFFFFFECF;
tty.c_cflag = v7 | 0x30;
if ( Twostop )
tty.c_cflag = v7 | 0x70;
tty.c_cc[6] = 1;
tty.c_cc[5] = 1;
tcsetattr(fd, 1, &tty);
v8 = cfgetospeed(&tty);
Baudrate = getspeed(v8);
return 0LL;
}
LABEL_30:
tty.c_iflag = 4097;
goto LABEL_15;
}
if ( !did0_0 )
{
did0_0 = 1;
tcgetattr(fd, &oldtty);
}
*(__m128i *)&tty.c_iflag = _mm_load_si128((const __m128i *)&oldtty);
*(__m128i *)&tty.c_line = _mm_load_si128((const __m128i *)&oldtty.c_line);
*(__m128i *)&tty.c_cc[15] = _mm_load_si128((const __m128i *)&oldtty.c_cc[15]);
*(_QWORD *)&tty.c_cc[31] = *(_QWORD *)&oldtty.c_cc[31];
tty.c_ospeed = oldtty.c_ospeed;
tty.c_iflag = 1026;
tty.c_oflag = 0;
v9 = tty.c_cflag;
BYTE1(v9) &= 0xFEu;
tty.c_cflag = v9 | 0x30;
if ( Twostop )
tty.c_cflag = v9 | 0x70;
v10 = protocol == 2;
tty.c_lflag = protocol != 2;
v11 = -1;
if ( !v10 )
v11 = 24;
tty.c_cc[0] = v11;
tty.c_cc[1] = 0;
tty.c_cc[6] = 1;
tty.c_cc[5] = 1;
tcsetattr(fd, 1, &tty);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(int fd, __int64 a2)
{
struct termios *v3; // rdi
struct termios *v4; // rsi
__int64 i; // rcx
__int64 result; // rax
tcflag_t v7; // eax
int v8; // eax
tcflag_t v9; // eax
bool v10; // zf
cc_t v11; // al

vfile("mode:%d", a2);
if ( (_DWORD)a2 != 2 )
{
if ( (int)a2 > 2 )
{
result = 0xFFFFFFFFLL;
if ( (_DWORD)a2 != 3 )
return result;
if ( did0_0 )
{
*(__m128i *)&tty.c_iflag = _mm_load_si128((const __m128i *)&oldtty);
*(__m128i *)&tty.c_line = _mm_load_si128((const __m128i *)&oldtty.c_line);
*(__m128i *)&tty.c_cc[15] = _mm_load_si128((const __m128i *)&oldtty.c_cc[15]);
*(_QWORD *)&tty.c_cc[31] = *(_QWORD *)&oldtty.c_cc[31];
tty.c_ospeed = oldtty.c_ospeed;
goto LABEL_30;
}
}
else
{
if ( !(_DWORD)a2 )
{
if ( !did0_0 )
return 0xFFFFFFFFLL;
tcflush(fd, 2);
tcsetattr(fd, 1, &oldtty);
tcflow(fd, 1);
return 0LL;
}
if ( (_DWORD)a2 != 1 )
return 0xFFFFFFFFLL;
if ( did0_0 )
{
v3 = &tty;
v4 = &oldtty;
for ( i = 15LL; i; --i )
{
v3->c_iflag = v4->c_iflag;
v4 = (struct termios *)((char *)v4 + 4);
v3 = (struct termios *)((char *)v3 + 4);
}
goto LABEL_14;
}
}
did0_0 = 1;
tcgetattr(fd, &oldtty);
*(__m128i *)&tty.c_iflag = _mm_load_si128((const __m128i *)&oldtty);
*(__m128i *)&tty.c_line = _mm_load_si128((const __m128i *)&oldtty.c_line);
*(__m128i *)&tty.c_cc[15] = _mm_load_si128((const __m128i *)&oldtty.c_cc[15]);
*(_QWORD *)&tty.c_cc[31] = *(_QWORD *)&oldtty.c_cc[31];
tty.c_ospeed = oldtty.c_ospeed;
if ( (_DWORD)a2 != 3 )
{
LABEL_14:
tty.c_iflag = 1;
LABEL_15:
tty.c_lflag &= 0xFFFFFFF4;
tty.c_oflag = 0;
v7 = tty.c_cflag & 0xFFFFFECF;
tty.c_cflag = v7 | 0x30;
if ( Twostop )
tty.c_cflag = v7 | 0x70;
tty.c_cc[6] = 1;
tty.c_cc[5] = 1;
tcsetattr(fd, 1, &tty);
v8 = cfgetospeed(&tty);
Baudrate = func0(v8);
return 0LL;
}
LABEL_30:
tty.c_iflag = 4097;
goto LABEL_15;
}
if ( !did0_0 )
{
did0_0 = 1;
tcgetattr(fd, &oldtty);
}
*(__m128i *)&tty.c_iflag = _mm_load_si128((const __m128i *)&oldtty);
*(__m128i *)&tty.c_line = _mm_load_si128((const __m128i *)&oldtty.c_line);
*(__m128i *)&tty.c_cc[15] = _mm_load_si128((const __m128i *)&oldtty.c_cc[15]);
*(_QWORD *)&tty.c_cc[31] = *(_QWORD *)&oldtty.c_cc[31];
tty.c_ospeed = oldtty.c_ospeed;
tty.c_iflag = 1026;
tty.c_oflag = 0;
v9 = tty.c_cflag;
BYTE1(v9) &= 0xFEu;
tty.c_cflag = v9 | 0x30;
if ( Twostop )
tty.c_cflag = v9 | 0x70;
v10 = protocol == 2;
tty.c_lflag = protocol != 2;
v11 = -1;
if ( !v10 )
v11 = 24;
tty.c_cc[0] = v11;
tty.c_cc[1] = 0;
tty.c_cc[6] = 1;
tty.c_cc[5] = 1;
tcsetattr(fd, 1, &tty);
return 0LL;
}

original_code:
int
io_mode(int fd, int n)
{
 static int did0 = 0;
 vfile("mode:%d", n);
 switch(n) {
 case 2:
  if(!did0) {
   did0 = 1;
   tcgetattr(fd,&oldtty);
  }
  tty = oldtty;
  tty.c_iflag = 0000002|0002000;
  tty.c_oflag = 0;
  tty.c_cflag &= ~0000400;
  tty.c_cflag |= 0000060;
  if (Twostop)
   tty.c_cflag |= 0000100;
  tty.c_lflag = protocol==ZM_ZMODEM ? 0 : 0000001;
  tty.c_cc[0] = protocol==ZM_ZMODEM ? -1 : 030;
  if (((int) '\0')!=(-1)) {
   tty.c_cc[1] = '\0';
  } else {
   tty.c_cc[1] = -1;
  }
  tty.c_cc[6] = 1;
  tty.c_cc[5] = 1;
  tcsetattr(fd,1,&tty);
  return 0;
 case 1:
 case 3:
  if(!did0) {
   did0 = 1;
   tcgetattr(fd,&oldtty);
  }
  tty = oldtty;
  tty.c_iflag = 0000001;
  if (n==3)
   tty.c_iflag |= 0010000;
  tty.c_lflag &= ~(0000010 | 0000002 | 0000001);
  tty.c_oflag = 0;
  tty.c_cflag &= ~(0000400);
  tty.c_cflag &= ~(0000060);
  tty.c_cflag |= 0000060;
  if (Twostop)
   tty.c_cflag |= 0000100;
  tty.c_cc[6] = 1;
  tty.c_cc[5] = 1;
  tcsetattr(fd,1,&tty);
  Baudrate = getspeed(cfgetospeed(&tty));
  return 0;
 case 0:
  if(!did0)
   return (-1);
  tcflush (fd,2);
  tcsetattr (fd,1,&oldtty);
  tcflow (fd,1);
  return 0;
  return 0;
 default:
  return (-1);
 }
}


sendbrk

decompiled_code:
int __fastcall sendbrk(int a1)
{
return tcsendbreak(a1, 0);
}

canonical_decompiled_code:
int __fastcall func4(int a1)
{
return tcsendbreak(a1, 0);
}

original_code:
void
sendbrk(int fd)
{
 tcsendbreak(fd,0);
}


purgeline

decompiled_code:
int __fastcall purgeline(int fd)
{
readline_purge();
return ioctl(fd, 0x540BuLL, 0LL);
}

canonical_decompiled_code:
int __fastcall func5(int fd)
{
readline_purge();
return ioctl(fd, 0x540BuLL, 0LL);
}

original_code:
void
purgeline(int fd)
{
 readline_purge();
 ioctl(fd, 0x540B, 0);
}


zperr

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

if ( Verbose > 0 )
{
fprintf(stderr, "Retry %d: ", errors);
va_start(arg, format);
vfprintf(stderr, format, arg);
result = putc(10, stderr);
}
return result;
}

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

if ( Verbose > 0 )
{
fprintf(stderr, "Retry %d: ", errors);
va_start(arg, format);
vfprintf(stderr, format, arg);
result = putc(10, stderr);
}
return result;
}

original_code:
void
zperr(const char *fmt, ...)
{
    va_list ap;
 if (Verbose<=0)
  return;
 fprintf(stderr,"Retry %d: ",errors);
    __builtin_va_start(ap,fmt);
    vfprintf(stderr,fmt, ap);
    __builtin_va_end(ap);
    putc('\n',stderr);
}

canonical_original_code:
void zperr(char *fmt, ...)
{
    __builtin_va_list ap;
 if (Verbose<=0)
  return;
 fprintf(stderr,"Retry %d: ",errors);
    __builtin_va_start(ap,fmt);
    vfprintf(stderr,fmt, ap);
    __builtin_va_end(ap);
    putc('\n',stderr);
}

variable_types:
fmt
char *


zpfatal

decompiled_code:
void zpfatal(char *format, ...)
{
int v1; // ebp
char *v2; // rax
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

if ( Verbose > 0 )
{
v1 = *_errno_location();
fprintf(stderr, "%s: ", program_name);
va_start(arg, format);
vfprintf(stderr, format, arg);
v2 = strerror(v1);
fprintf(stderr, ": %s\n", v2);
}
}

canonical_decompiled_code:
void func1(char *format, ...)
{
int v1; // ebp
char *v2; // rax
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

if ( Verbose > 0 )
{
v1 = *_errno_location();
fprintf(stderr, "%s: ", program_name);
va_start(arg, format);
vfprintf(stderr, format, arg);
v2 = strerror(v1);
fprintf(stderr, ": %s\n", v2);
}
}

original_code:
void
zpfatal(const char *fmt, ...)
{
    va_list ap;
    int err=(*__errno_location ());
 if (Verbose<=0)
  return;
 fprintf(stderr,"%s: ",program_name);
    __builtin_va_start(ap,fmt);
    vfprintf(stderr,fmt, ap);
    __builtin_va_end(ap);
 fprintf(stderr,": %s\n",strerror(err));
}

canonical_original_code:
void zpfatal(char *fmt, ...)
{
    __builtin_va_list ap;
    int err = (*__errno_location ());
 if (Verbose<=0)
  return;
 fprintf(stderr,"%s: ",program_name);
    __builtin_va_start(ap,fmt);
    vfprintf(stderr,fmt, ap);
    __builtin_va_end(ap);
 fprintf(stderr,": %s\n",strerror(err));
}

variable_types: err int


vfile

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

if ( Verbose > 2 )
{
va_start(arg, format);
vfprintf(stderr, format, arg);
result = putc(10, stderr);
}
return result;
}

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

if ( Verbose > 2 )
{
va_start(arg, format);
vfprintf(stderr, format, arg);
result = putc(10, stderr);
}
return result;
}

original_code:
void
vfile(const char *format, ...)
{
    va_list ap;
 if (Verbose < 3)
  return;
    __builtin_va_start(ap,format);
    vfprintf(stderr,format, ap);
    __builtin_va_end(ap);
    putc('\n',stderr);
}

canonical_original_code:
void vfile(char *format, ...)
{
    __builtin_va_list ap;
 if (Verbose < 3)
  return;
    __builtin_va_start(ap,format);
    vfprintf(stderr,format, ap);
    __builtin_va_end(ap);
    putc('\n',stderr);
}

variable_types:
format
char *


lsyslog

decompiled_code:
void lsyslog(int pri, char *f, ...)
{
__uid_t v3; // er12
struct passwd *v4; // rax
const char *v5; // rdi
gcc_va_list va; // [rsp+0h] [rbp-E8h] BYREF
char *ptr; // [rsp+18h] [rbp-D0h] BYREF

ptr = 0LL;
if ( enable_syslog )
{
if ( !init_done_2 )
{
init_done_2 = 1;
v3 = getuid();
v4 = getpwuid(v3);
if ( v4 )
{
v5 = v4->pw_name;
if ( v4->pw_name )
{
if ( *v5 )
username_1 = (__int64)strdup(v5);
}
}
if ( !username_1 )
{
username_1 = (__int64)uid_string_0;
sprintf(uid_string_0, "#%lu", v3);
}
}
va_start(va, f);
vasprintf(&ptr, f, va);
syslog(pri, "[%s] %s", (const char *)username_1, ptr);
free(ptr);
}
}

canonical_decompiled_code:
void func0(int pri, char *f, ...)
{
__uid_t v3; // er12
struct passwd *v4; // rax
const char *v5; // rdi
gcc_va_list va; // [rsp+0h] [rbp-E8h] BYREF
char *ptr; // [rsp+18h] [rbp-D0h] BYREF

ptr = 0LL;
if ( enable_syslog )
{
if ( !init_done_2 )
{
init_done_2 = 1;
v3 = getuid();
v4 = getpwuid(v3);
if ( v4 )
{
v5 = v4->pw_name;
if ( v4->pw_name )
{
if ( *v5 )
username_1 = (__int64)strdup(v5);
}
}
if ( !username_1 )
{
username_1 = (__int64)uid_string_0;
sprintf(uid_string_0, "#%lu", v3);
}
}
va_start(va, f);
vasprintf(&ptr, f, va);
syslog(pri, "[%s] %s", (const char *)username_1, ptr);
free(ptr);
}
}

original_code:
void
lsyslog(int prio, const char *format, ...)
{
 static char *username=((void *)0);
 static char uid_string[20]="";
 char *s=((void *)0);
 static int init_done=0;
    va_list ap;
 if (!enable_syslog)
  return;
 if (!init_done) {
  uid_t uid;
  struct passwd *pwd;
  init_done=1;
  uid=getuid();
  pwd=getpwuid(uid);
  if (pwd && pwd->pw_name && *pwd->pw_name) {
   username=strdup(pwd->pw_name);
  }
  if (!username) {
   username=uid_string;
   sprintf(uid_string,"#%lu",(unsigned long) uid);
  }
 }
    __builtin_va_start(ap,format);
    vasprintf(&s,format, ap);
    __builtin_va_end(ap);
    syslog(prio,"[%s] %s",username,s);
 free(s);
}

canonical_original_code:
void lsyslog(int prio, char *format, ...)
{
 static char *username = ((void *)0);
 static char uid_string[20] = "";
 char *s = ((void *)0);
 static int init_done = 0;
    __builtin_va_list ap;
 if (!enable_syslog)
  return;
 if (!init_done) {
  unsigned int uid;
  struct passwd *pwd;
  init_done=1;
  uid=getuid();
  pwd=getpwuid(uid);
  if (pwd && pwd->pw_name && *pwd->pw_name) {
   username=strdup(pwd->pw_name);
  }
  if (!username) {
   username=uid_string;
   sprintf(uid_string,"#%lu",(unsigned long) uid);
  }
 }
    __builtin_va_start(ap,format);
    vasprintf(&s,format, ap);
    __builtin_va_end(ap);
    syslog(prio,"[%s] %s",username,s);
 free(s);
}

variable_types: prio int

variable_types:
username
char *

variable_types:
s
char *

variable_types:
uid
unsigned int

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


error

decompiled_code:
int error(int status, int errnum, char *format, ...)
{
int result; // eax
char *v5; // rax
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

if ( error_print_progname )
{
error_print_progname();
}
else
{
fflush(stdout);
fprintf(stderr, "%s: ", program_name);
}
va_start(arg, format);
vfprintf(stderr, format, arg);
++error_message_count;
if ( errnum )
{
v5 = strerror(errnum);
fprintf(stderr, ": %s", v5);
}
putc(10, stderr);
result = fflush(stderr);
if ( status )
exit(status);
return result;
}

canonical_decompiled_code:
int func0(int status, int errnum, char *format, ...)
{
int result; // eax
char *v5; // rax
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

if ( error_print_progname )
{
error_print_progname();
}
else
{
fflush(stdout);
fprintf(stderr, "%s: ", program_name);
}
va_start(arg, format);
vfprintf(stderr, format, arg);
++error_message_count;
if ( errnum )
{
v5 = strerror(errnum);
fprintf(stderr, ": %s", v5);
}
putc(10, stderr);
result = fflush(stderr);
if ( status )
exit(status);
return result;
}

original_code:
void
error (int status, int errnum, const char *message, ...)
{
  va_list args;
  if (error_print_progname)
    (*error_print_progname) ();
  else
    {
      fflush (stdout);
      fprintf (stderr, "%s: ", program_name);
    }
  __builtin_va_start(args,message);
  vfprintf (stderr, message, args);
  __builtin_va_end(args);
  ++error_message_count;
  if (errnum)
    fprintf (stderr, ": %s", strerror (errnum));
  putc ('\n', stderr);
  fflush (stderr);
  if (status)
    exit (status);
}

canonical_original_code:
void error(int status, int errnum, char *message, ...)
{
  __builtin_va_list args;
  if (error_print_progname)
    (*error_print_progname) ();
  else
    {
      fflush (stdout);
      fprintf (stderr, "%s: ", program_name);
    }
  __builtin_va_start(args,message);
  vfprintf (stderr, message, args);
  __builtin_va_end(args);
  ++error_message_count;
  if (errnum)
    fprintf (stderr, ": %s", strerror (errnum));
  putc ('\n', stderr);
  fflush (stderr);
  if (status)
    exit (status);
}

variable_types: status int

variable_types: errnum int

variable_types:
message
char *


decompiled_code:
void error_at_line(int status, int errnum, char *s2, unsigned int a4, const char *a5, ...)
{
char *v8; // rax
gcc_va_list arg; // [rsp+8h] [rbp-F0h] BYREF

if ( error_one_per_line )
{
if ( old_line_number_1 == a4 && (old_file_name_0 == s2 || !strcmp(old_file_name_0, s2)) )
return;
old_file_name_0 = s2;
old_line_number_1 = a4;
}
if ( error_print_progname )
{
error_print_progname();
}
else
{
fflush(stdout);
fprintf(stderr, "%s:", program_name);
}
if ( s2 )
fprintf(stderr, "%s:%d: ", s2, a4);
va_start(arg, a5);
vfprintf(stderr, a5, arg);
++error_message_count;
if ( errnum )
{
v8 = strerror(errnum);
fprintf(stderr, ": %s", v8);
}
putc(10, stderr);
fflush(stderr);
if ( status )
exit(status);
}

canonical_decompiled_code:
void func1(int status, int errnum, char *s2, unsigned int a4, const char *a5, ...)
{
char *v8; // rax
gcc_va_list arg; // [rsp+8h] [rbp-F0h] BYREF

if ( error_one_per_line )
{
if ( old_line_number_1 == a4 && (old_file_name_0 == s2 || !strcmp(old_file_name_0, s2)) )
return;
old_file_name_0 = s2;
old_line_number_1 = a4;
}
if ( error_print_progname )
{
error_print_progname();
}
else
{
fflush(stdout);
fprintf(stderr, "%s:", program_name);
}
if ( s2 )
fprintf(stderr, "%s:%d: ", s2, a4);
va_start(arg, a5);
vfprintf(stderr, a5, arg);
++error_message_count;
if ( errnum )
{
v8 = strerror(errnum);
fprintf(stderr, ": %s", v8);
}
putc(10, stderr);
fflush(stderr);
if ( status )
exit(status);
}

original_code:
void
error_at_line (int status, int errnum, const char *file_name,
        unsigned int line_number, const char *message, ...)
{
  va_list args;
  if (error_one_per_line)
    {
      static const char *old_file_name;
      static unsigned int old_line_number;
      if (old_line_number == line_number &&
   (file_name == old_file_name || !strcmp (old_file_name, file_name)))
 return;
      old_file_name = file_name;
      old_line_number = line_number;
    }
  if (error_print_progname)
    (*error_print_progname) ();
  else
    {
      fflush (stdout);
      fprintf (stderr, "%s:", program_name);
    }
  if (file_name != ((void *)0))
    fprintf (stderr, "%s:%d: ", file_name, line_number);
  __builtin_va_start(args,message);
  vfprintf (stderr, message, args);
  __builtin_va_end(args);
  ++error_message_count;
  if (errnum)
    fprintf (stderr, ": %s", strerror (errnum));
  putc ('\n', stderr);
  fflush (stderr);
  if (status)
    exit (status);
}

canonical_original_code:
void error_at_line(int status, int errnum, char *file_name, unsigned int line_number, char *message, ...)
{
  __builtin_va_list args;
  if (error_one_per_line)
    {
      static char *old_file_name;
      static unsigned int old_line_number;
      if (old_line_number == line_number &&
   (file_name == old_file_name || !strcmp (old_file_name, file_name)))
 return;
      old_file_name = file_name;
      old_line_number = line_number;
    }
  if (error_print_progname)
    (*error_print_progname) ();
  else
    {
      fflush (stdout);
      fprintf (stderr, "%s:", program_name);
    }
  if (file_name != ((void *)0))
    fprintf (stderr, "%s:%d: ", file_name, line_number);
  __builtin_va_start(args,message);
  vfprintf (stderr, message, args);
  __builtin_va_end(args);
  ++error_message_count;
  if (errnum)
    fprintf (stderr, ": %s", strerror (errnum));
  putc ('\n', stderr);
  fflush (stderr);
  if (status)
    exit (status);
}


canit

decompiled_code:
ssize_t __fastcall canit(int fd)
{
size_t v1; // rax
ssize_t result; // rax
size_t v3; // rax

purgeline();
v1 = strlen(&canistr_0);
result = write(fd, &canistr_0, v1);
if ( !fd )
{
v3 = strlen(&canistr_0);
result = write(1, &canistr_0, v3);
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func0(int fd)
{
size_t v1; // rax
ssize_t result; // rax
size_t v3; // rax

purgeline();
v1 = strlen(&canistr_0);
result = write(fd, &canistr_0, v1);
if ( !fd )
{
v3 = strlen(&canistr_0);
result = write(1, &canistr_0, v3);
}
return result;
}

original_code:
void
canit (int fd)
{
 static char canistr[] =
 {
  24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 0
 };
 purgeline(fd);
 write(fd,canistr,strlen(canistr));
 if (fd==0)
  write(1,canistr,strlen(canistr));
}

canonical_original_code:
void canit(int fd)
{
 static char canistr[] = {
  24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 0
 };
 purgeline(fd);
 write(fd,canistr,strlen(canistr));
 if (fd==0)
  write(1,canistr,strlen(canistr));
}

variable_types:
canistr
char []


decompiled_code:
void zreadline_alarm_handler()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
static void
zreadline_alarm_handler(int dummy __attribute__((__unused__)))
{
}

canonical_original_code:
static void zreadline_alarm_handler(int dummy)
{
}

variable_types: dummy int


decompiled_code:
__int64 __fastcall readline_internal(unsigned int a1)
{
unsigned int v1; // ebx
int v2; // ecx
__int64 v3; // rcx
int v4; // edx
char v5; // al
int v6; // esi
unsigned __int8 *v7; // rax
__int64 result; // rax
int *v9; // rbx
char *v10; // rax
__int64 i; // rbx
int v12; // ebx
__int64 v13; // rbp

if ( no_timeout )
{
if ( Verbose > 5 )
fprintf(stderr, "Calling read: Readnum=%d ", readline_readnum);
}
else
{
if ( a1 > 0x13 || (v1 = 3, a1 == 1) )
{
v1 = 1;
if ( a1 > 9 )
v1 = a1 / 0xA;
}
if ( Verbose > 5 )
fprintf(stderr, "Calling read: alarm=%d Readnum=%d ", v1, readline_readnum);
signal(14, (__sighandler_t)zreadline_alarm_handler);
alarm(v1);
}
readline_ptr = (__int64)readline_buffer;
readline_left = read(readline_fd, readline_buffer, readline_readnum);
if ( !no_timeout )
alarm(0);
v2 = readline_left;
if ( readline_left <= 0 )
{
if ( Verbose <= 5 )
return 4294967294LL;
}
else if ( bytes_per_error )
{
v3 = ct_1 + readline_left;
ct_1 = v3;
v4 = mod_0;
v5 = 0;
LABEL_12:
v6 = v4;
while ( bytes_per_error < v3 )
{
*(_BYTE *)(readline_ptr + v3 % bytes_per_error) ^= v6;
v3 -= bytes_per_error;
++v6;
v4 = 1;
v5 = 1;
if ( v6 == 256 )
goto LABEL_12;
}
if ( v5 )
{
mod_0 = v6;
ct_1 = v3;
}
if ( Verbose <= 5 )
{
LABEL_23:
v2 = readline_left;
if ( readline_left <= 0 )
return 4294967294LL;
goto LABEL_24;
}
}
else if ( Verbose <= 5 )
{
LABEL_24:
readline_left = v2 - 1;
v7 = (unsigned __int8 *)readline_ptr++;
return *v7;
}
fprintf(stderr, "Read returned %d bytes\n", (unsigned int)readline_left);
if ( readline_left == -1 )
{
v9 = _errno_location();
v10 = strerror(*v9);
fprintf(stderr, "errno=%d:%s\n", (unsigned int)*v9, v10);
}
if ( Verbose <= 9 )
goto LABEL_23;
v12 = readline_left;
result = 4294967294LL;
if ( readline_left > 0 )
{
fwrite(" ", 1uLL, 4uLL, stderr);
if ( v12 > 48 )
v12 = 48;
v13 = (unsigned int)(v12 - 1);
for ( i = 0LL; ; ++i )
{
fprintf(stderr, "%02x ", *(unsigned __int8 *)(readline_ptr + i));
if ( i == v13 )
break;
if ( (_DWORD)i + 1 == 24 * (((int)i + 1) / 24) && (_DWORD)i != -1 )
fwrite("\n ", 1uLL, 5uLL, stderr);
}
fputc(10, stderr);
goto LABEL_23;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1)
{
unsigned int v1; // ebx
int v2; // ecx
__int64 v3; // rcx
int v4; // edx
char v5; // al
int v6; // esi
unsigned __int8 *v7; // rax
__int64 result; // rax
int *v9; // rbx
char *v10; // rax
__int64 i; // rbx
int v12; // ebx
__int64 v13; // rbp

if ( no_timeout )
{
if ( Verbose > 5 )
fprintf(stderr, "Calling read: Readnum=%d ", readline_readnum);
}
else
{
if ( a1 > 0x13 || (v1 = 3, a1 == 1) )
{
v1 = 1;
if ( a1 > 9 )
v1 = a1 / 0xA;
}
if ( Verbose > 5 )
fprintf(stderr, "Calling read: alarm=%d Readnum=%d ", v1, readline_readnum);
signal(14, (__sighandler_t)zreadline_alarm_handler);
alarm(v1);
}
readline_ptr = (__int64)readline_buffer;
readline_left = read(readline_fd, readline_buffer, readline_readnum);
if ( !no_timeout )
alarm(0);
v2 = readline_left;
if ( readline_left <= 0 )
{
if ( Verbose <= 5 )
return 4294967294LL;
}
else if ( bytes_per_error )
{
v3 = ct_1 + readline_left;
ct_1 = v3;
v4 = mod_0;
v5 = 0;
LABEL_12:
v6 = v4;
while ( bytes_per_error < v3 )
{
*(_BYTE *)(readline_ptr + v3 % bytes_per_error) ^= v6;
v3 -= bytes_per_error;
++v6;
v4 = 1;
v5 = 1;
if ( v6 == 256 )
goto LABEL_12;
}
if ( v5 )
{
mod_0 = v6;
ct_1 = v3;
}
if ( Verbose <= 5 )
{
LABEL_23:
v2 = readline_left;
if ( readline_left <= 0 )
return 4294967294LL;
goto LABEL_24;
}
}
else if ( Verbose <= 5 )
{
LABEL_24:
readline_left = v2 - 1;
v7 = (unsigned __int8 *)readline_ptr++;
return *v7;
}
fprintf(stderr, "Read returned %d bytes\n", (unsigned int)readline_left);
if ( readline_left == -1 )
{
v9 = _errno_location();
v10 = strerror(*v9);
fprintf(stderr, "errno=%d:%s\n", (unsigned int)*v9, v10);
}
if ( Verbose <= 9 )
goto LABEL_23;
v12 = readline_left;
result = 4294967294LL;
if ( readline_left > 0 )
{
fwrite(" ", 1uLL, 4uLL, stderr);
if ( v12 > 48 )
v12 = 48;
v13 = (unsigned int)(v12 - 1);
for ( i = 0LL; ; ++i )
{
fprintf(stderr, "%02x ", *(unsigned __int8 *)(readline_ptr + i));
if ( i == v13 )
break;
if ( (_DWORD)i + 1 == 24 * (((int)i + 1) / 24) && (_DWORD)i != -1 )
fwrite("\n ", 1uLL, 5uLL, stderr);
}
fputc(10, stderr);
goto LABEL_23;
}
return result;
}

original_code:
int
readline_internal(unsigned int timeout)
{
 if (!no_timeout)
 {
  unsigned int n;
  n = timeout/10;
  if (n < 2 && timeout!=1)
   n = 3;
  else if (n==0)
   n=1;
  if (Verbose > 5)
   fprintf(stderr,"Calling read: alarm=%d  Readnum=%d ", n, readline_readnum);
  signal(14, zreadline_alarm_handler);
  alarm(n);
 }
 else if (Verbose > 5)
  fprintf(stderr,"Calling read: Readnum=%d ", readline_readnum);
 readline_ptr=readline_buffer;
 readline_left=read(readline_fd, readline_ptr, readline_readnum);
 if (!no_timeout)
  alarm(0);
 if (readline_left>0 && bytes_per_error) {
  static long ct=0;
  static int mod=1;
  ct+=readline_left;
  while (ct>bytes_per_error) {
   readline_ptr[ct % bytes_per_error]^=mod;
   ct-=bytes_per_error;
   mod++;
   if (mod==256)
    mod=1;
  }
 }
 if (Verbose > 5) {
  fprintf(stderr,"Read returned %d bytes\n", readline_left);
  if (readline_left==-1)
   fprintf(stderr,"errno=%d:%s\n", (*__errno_location ()),strerror((*__errno_location ())));
  if (Verbose > 9 && readline_left>0) {
   int i,j;
   j=readline_left > 48 ? 48 : readline_left;
   fputs("    ",stderr);
   for (i=0;i<j;i++) {
    if (i%24==0 && i)
     fputs("\n    ",stderr);
    fprintf(stderr,"%02x ", readline_ptr[i] & 0377);
   }
   fprintf(stderr,"\n");
  }
 }
 if (readline_left < 1)
  return (-2);
 --readline_left;
 return (*readline_ptr++ & 0377);
}

canonical_original_code:
int readline_internal(unsigned int timeout)
{
 if (!no_timeout)
 {
  unsigned int n;
  n = timeout/10;
  if (n < 2 && timeout!=1)
   n = 3;
  else if (n==0)
   n=1;
  if (Verbose > 5)
   fprintf(stderr,"Calling read: alarm=%d  Readnum=%d ", n, readline_readnum);
  signal(14, zreadline_alarm_handler);
  alarm(n);
 }
 else if (Verbose > 5)
  fprintf(stderr,"Calling read: Readnum=%d ", readline_readnum);
 readline_ptr=readline_buffer;
 readline_left=read(readline_fd, readline_ptr, readline_readnum);
 if (!no_timeout)
  alarm(0);
 if (readline_left>0 && bytes_per_error) {
  static long ct = 0;
  static int mod = 1;
  ct+=readline_left;
  while (ct>bytes_per_error) {
   readline_ptr[ct % bytes_per_error]^=mod;
   ct-=bytes_per_error;
   mod++;
   if (mod==256)
    mod=1;
  }
 }
 if (Verbose > 5) {
  fprintf(stderr,"Read returned %d bytes\n", readline_left);
  if (readline_left==-1)
   fprintf(stderr,"errno=%d:%s\n", (*__errno_location ()),strerror((*__errno_location ())));
  if (Verbose > 9 && readline_left>0) {
   int i; int j;
   j=readline_left > 48 ? 48 : readline_left;
   fputs("    ",stderr);
   for (i=0;i<j;i++) {
    if (i%24==0 && i)
     fputs("\n    ",stderr);
    fprintf(stderr,"%02x ", readline_ptr[i] & 0377);
   }
   fprintf(stderr,"\n");
  }
 }
 if (readline_left < 1)
  return (-2);
 --readline_left;
 return (*readline_ptr++ & 0377);
}

variable_types:
timeout
unsigned int

variable_types:
n
unsigned int

variable_types: ct long

variable_types: mod int

variable_types: i int

variable_types: j int


decompiled_code:
void __fastcall readline_setup(int a1, size_t a2, size_t a3)
{
readline_fd = a1;
readline_readnum = a2;
if ( a2 >= a3 )
a3 = a2;
readline_buffer = malloc(a3);
if ( !readline_buffer )
error(1, 0, "out of memory");
}

canonical_decompiled_code:
void __fastcall func2(int a1, size_t a2, size_t a3)
{
readline_fd = a1;
readline_readnum = a2;
if ( a2 >= a3 )
a3 = a2;
readline_buffer = malloc(a3);
if ( !readline_buffer )
error(1, 0, "out of memory");
}

original_code:
void
readline_setup(int fd, size_t readnum, size_t bufsize)
{
 readline_fd=fd;
 readline_readnum=readnum;
 readline_buffer=malloc(bufsize > readnum ? bufsize : readnum);
 if (!readline_buffer)
  error(1,0,"out of memory");
}

canonical_original_code:
void readline_setup(int fd, unsigned long readnum, unsigned long bufsize)
{
 readline_fd=fd;
 readline_readnum=readnum;
 readline_buffer=malloc(bufsize > readnum ? bufsize : readnum);
 if (!readline_buffer)
  error(1,0,"out of memory");
}

variable_types: fd int

variable_types:
readnum
unsigned long

variable_types:
bufsize
unsigned long


decompiled_code:
void readline_purge()
{
readline_left = 0;
}

canonical_decompiled_code:
void func3()
{
readline_left = 0;
}

original_code:
void
readline_purge(void)
{
 readline_left=0;
 return;
}

canonical_original_code:
void readline_purge()
{
 readline_left=0;
 return;
}


protname

decompiled_code:
const char *protname()
{
const char *result; // rax

result = "XMODEM";
if ( protocol )
{
result = "ZMODEM";
if ( protocol == 1 )
result = "YMODEM";
}
return result;
}

canonical_decompiled_code:
const char *func0()
{
const char *result; // rax

result = "XMODEM";
if ( protocol )
{
result = "ZMODEM";
if ( protocol == 1 )
result = "YMODEM";
}
return result;
}

original_code:
const char *
protname(void)
{
 const char *prot_name;
 switch(protocol) {
 case ZM_XMODEM:
  prot_name="XMODEM";
  break;
 case ZM_YMODEM:
  prot_name="YMODEM";
  break;
 default:
  prot_name="ZMODEM";
  break;
 }
 return prot_name;
}

canonical_original_code:
char *protname()
{
 char *prot_name;
 switch(protocol) {
 case ZM_XMODEM:
  prot_name="XMODEM";
  break;
 case ZM_YMODEM:
  prot_name="YMODEM";
  break;
 default:
  prot_name="ZMODEM";
  break;
 }
 return prot_name;
}

return_type: char *


timing

decompiled_code:
double __fastcall timing(int a1, _QWORD *a2)
{
double result; // xmm0_8
struct timezone tz; // [rsp+8h] [rbp-30h] BYREF
struct timeval tv; // [rsp+10h] [rbp-28h] BYREF

tz.tz_dsttime = 0;
gettimeofday(&tv, &tz);
result = (double)SLODWORD(tv.tv_usec) / 1000000.0 + (double)SLODWORD(tv.tv_sec);
if ( a2 )
*a2 = (unsigned int)(int)result;
if ( !a1 )
return result - *(double *)&starttime_2;
starttime_2 = *(_QWORD *)&result;
return result;
}

canonical_decompiled_code:
double __fastcall func0(int a1, _QWORD *a2)
{
double result; // xmm0_8
struct timezone tz; // [rsp+8h] [rbp-30h] BYREF
struct timeval tv; // [rsp+10h] [rbp-28h] BYREF

tz.tz_dsttime = 0;
gettimeofday(&tv, &tz);
result = (double)SLODWORD(tv.tv_usec) / 1000000.0 + (double)SLODWORD(tv.tv_sec);
if ( a2 )
*a2 = (unsigned int)(int)result;
if ( !a1 )
return result - *(double *)&starttime_2;
starttime_2 = *(_QWORD *)&result;
return result;
}

original_code:
double
timing (int reset, time_t *nowp)
{
  static double elaptime, starttime, stoptime;
  double yet;
  struct timeval tv;
  struct timezone tz;
  tz.tz_dsttime = 0;
  gettimeofday (&tv, &tz);
  yet=tv.tv_sec + tv.tv_usec/1000000.0;
  if (nowp)
    *nowp=(time_t) yet;
  if (reset) {
    starttime = yet;
    return starttime;
  }
  else {
    stoptime = yet;
    elaptime = stoptime - starttime;
    return elaptime;
  }
}

canonical_original_code:
double timing(int reset, long *nowp)
{
  static double elaptime; static double starttime; static double stoptime;
  double yet;
  struct timeval tv;
  struct timezone tz;
  tz.tz_dsttime = 0;
  gettimeofday (&tv, &tz);
  yet=tv.tv_sec + tv.tv_usec/1000000.0;
  if (nowp)
    *nowp=(long) yet;
  if (reset) {
    starttime = yet;
    return starttime;
  }
  else {
    stoptime = yet;
    elaptime = stoptime - starttime;
    return elaptime;
  }
}

variable_types: reset int

variable_types:
nowp
long *

variable_types: elaptime double

variable_types: starttime double

variable_types: stoptime double

variable_types: yet double

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

variable_types: struct timezone { int tz_minuteswest; int tz_dsttime; }

return_type: double


deviceConnect

decompiled_code:
__int64 deviceConnect()
{
unsigned int v0; // ebx

if ( (unsigned int)idevice_new() )
return (unsigned int)-1;
v0 = 0;
if ( (unsigned int)idevice_get_udid() )
printf("%s Unable to communicate with device.%s\n", "\x1B[31m", "\x1B[0m");
printf(" UDID: %s!\n", (const char *)udid);
return v0;
}

canonical_decompiled_code:
__int64 func8()
{
unsigned int v0; // ebx

if ( (unsigned int)idevice_new() )
return (unsigned int)-1;
v0 = 0;
if ( (unsigned int)idevice_get_udid() )
printf("%s Unable to communicate with device.%s\n", "\x1B[31m", "\x1B[0m");
printf(" UDID: %s!\n", (const char *)udid);
return v0;
}

original_code:
int deviceConnect() {
 idevice_error_t ideverr = 0;
 ideverr = idevice_new(&gDevice, ((void *)0));
 if (ideverr != IDEVICE_E_SUCCESS) {
  return -1;
 }
 ideverr = idevice_get_udid(gDevice, &udid);
 if (ideverr != IDEVICE_E_SUCCESS) {
  printf("%s Unable to communicate with device.%s\n", "\x1B[31m", "\x1B[0m");
 }
 printf(" UDID: %s!\n", udid);
 return 0;
}

canonical_original_code:
int deviceConnect() {
 enum idevice_error_t ideverr = 0;
 ideverr = idevice_new(&gDevice, ((void *)0));
 if (ideverr != IDEVICE_E_SUCCESS) {
  return -1;
 }
 ideverr = idevice_get_udid(gDevice, &udid);
 if (ideverr != IDEVICE_E_SUCCESS) {
  printf("%s Unable to communicate with device.%s\n", "\x1B[31m", "\x1B[0m");
 }
 printf(" UDID: %s!\n", udid);
 return 0;
}

variable_types: enum idevice_error_t { IDEVICE_E_SUCCESS=0, IDEVICE_E_INVALID_ARG=-1, IDEVICE_E_UNKNOWN_ERROR=-2, IDEVICE_E_NO_DEVICE=-3, IDEVICE_E_NOT_ENOUGH_DATA=-4, IDEVICE_E_BAD_HEADER=-5, IDEVICE_E_SSL_ERROR=-6 }


startLockdownd

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

puts(" lockdownd...");
result = lockdownd_client_new_with_handshake();
lderr = result;
if ( (_DWORD)result )
{
printf("%s [*] Unable to connect to lockdownd. Please reboot your device and try again.%s\n", "\x1B[31m", "\x1B[0m");
result = 0xFFFFFFFFLL;
}
return result;
}

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

puts(" lockdownd...");
result = lockdownd_client_new_with_handshake();
lderr = result;
if ( (_DWORD)result )
{
printf("%s [*] Unable to connect to lockdownd. Please reboot your device and try again.%s\n", "\x1B[31m", "\x1B[0m");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int startLockdownd() {
 printf(" lockdownd...\n");
 lderr = lockdownd_client_new_with_handshake(gDevice, &gLockdown, "exVasi0n");
 if (lderr != LOCKDOWN_E_SUCCESS) {
  printf("%s [*] Unable to connect to lockdownd. Please reboot your device and try again.%s\n", "\x1B[31m", "\x1B[0m");
  return -1;
 }
 return 0;
}


startAFC

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

puts(" afc service");
result = lockdownd_start_service();
lderr = result;
if ( (_DWORD)result )
{
printf("%s afc service error%s\n", "\x1B[31m", "\x1B[0m");
result = 0xFFFFFFFFLL;
}
return result;
}

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

puts(" afc service");
result = lockdownd_start_service();
lderr = result;
if ( (_DWORD)result )
{
printf("%s afc service error%s\n", "\x1B[31m", "\x1B[0m");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int startAFC() {
 printf(" afc service\n");
 lderr = lockdownd_start_service(gLockdown, "com.apple.afc", &port);
 if (lderr != LOCKDOWN_E_SUCCESS) {
  printf("%s afc service error%s\n", "\x1B[31m", "\x1B[0m");
  return -1;
 }
 return 0;
}


connectAFC

decompiled_code:
__int64 connectAFC()
{
unsigned int v0; // ebx

puts(" afc client");
afcerr = afc_client_new();
if ( afcerr )
{
printf("%s afc client error%s\n", "\x1B[31m", "\x1B[0m");
lockdownd_client_free();
idevice_free();
v0 = -1;
}
else
{
v0 = 0;
lockdownd_client_free();
gLockdown = 0LL;
puts(" copying files");
}
return v0;
}

canonical_decompiled_code:
__int64 func11()
{
unsigned int v0; // ebx

puts(" afc client");
afcerr = afc_client_new();
if ( afcerr )
{
printf("%s afc client error%s\n", "\x1B[31m", "\x1B[0m");
lockdownd_client_free();
idevice_free();
v0 = -1;
}
else
{
v0 = 0;
lockdownd_client_free();
gLockdown = 0LL;
puts(" copying files");
}
return v0;
}

original_code:
int connectAFC() {
 printf(" afc client\n");
 afcerr = afc_client_new(gDevice, port, &gAfc);
 if (afcerr != AFC_E_SUCCESS) {
  printf("%s afc client error%s\n", "\x1B[31m", "\x1B[0m");
  lockdownd_client_free(gLockdown);
  idevice_free(gDevice);
  return -1;
 }
 lockdownd_client_free(gLockdown);
 gLockdown = ((void *)0);
 printf(" copying files\n");
 return 0;
}


main

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

puts(" exVasi0n, tihmstar will pwn you :O");
v3 = (__int64)" waiting for device";
puts(" waiting for device");
while ( (unsigned int)deviceConnect(v3, argv) )
{
v3 = 1LL;
sleep(1u);
}
puts(" device found!");
if ( (unsigned int)startLockdownd() )
return -1;
if ( (unsigned int)startAFC() )
return -1;
if ( (unsigned int)connectAFC() )
return -1;
afcerr = afc_make_directory();
if ( afcerr )
{
printf("%s Error creating dir %s\n\n", "\x1B[31m", "\x1B[0m");
afc_client_free();
idevice_free();
v5 = -1;
}
else
{
afcerr = afc_send_file(gAfc, "mylittlesecret.tar");
if ( afcerr )
{
printf("%s Error putting file.%s\n\n", "\x1B[31m", "\x1B[0m");
v5 = -1;
}
else
{
puts(" lockdownd...");
lderr = lockdownd_client_new_with_handshake();
if ( lderr )
{
printf(
"%s [*] Unable to connect to lockdownd. Please reboot your device and try again.%s\n",
"\x1B[31m",
"\x1B[0m");
v5 = -1;
}
else
{
puts(" gonna reboot");
lderr = lockdownd_start_service();
if ( lderr )
{
printf("%s diag service error%s\n", "\x1B[31m", "\x1B[0m");
v5 = -1;
}
else
{
v4 = diagnostics_relay_client_new();
v5 = v4;
diagerr = v4;
if ( v4 )
{
printf("%s diag client error %s %d\n", "\x1B[31m", "\x1B[0m", v4);
lockdownd_client_free();
idevice_free();
v5 = -1;
}
else
{
v6 = diagnostics_relay_restart();
diagerr = v6;
if ( ((v6 + 2) & 0xFFFFFFFD) != 0 )
{
printf("%s reboot error, reboot manually %d %s\n", "\x1B[0m", v6, "\x1B[0m");
lockdownd_client_free();
idevice_free();
v5 = -1;
}
else
{
puts(" done :P ");
}
}
}
}
}
}
return v5;
}

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

puts(" exVasi0n, tihmstar will pwn you :O");
v3 = (__int64)" waiting for device";
puts(" waiting for device");
while ( (unsigned int)func8(v3, argv) )
{
v3 = 1LL;
sleep(1u);
}
puts(" device found!");
if ( (unsigned int)func9() )
return -1;
if ( (unsigned int)func10() )
return -1;
if ( (unsigned int)func11() )
return -1;
afcerr = afc_make_directory();
if ( afcerr )
{
printf("%s Error creating dir %s\n\n", "\x1B[31m", "\x1B[0m");
afc_client_free();
idevice_free();
v5 = -1;
}
else
{
afcerr = func7(gAfc, "mylittlesecret.tar");
if ( afcerr )
{
printf("%s Error putting file.%s\n\n", "\x1B[31m", "\x1B[0m");
v5 = -1;
}
else
{
puts(" lockdownd...");
lderr = lockdownd_client_new_with_handshake();
if ( lderr )
{
printf(
"%s [*] Unable to connect to lockdownd. Please reboot your device and try again.%s\n",
"\x1B[31m",
"\x1B[0m");
v5 = -1;
}
else
{
puts(" gonna reboot");
lderr = lockdownd_start_service();
if ( lderr )
{
printf("%s diag service error%s\n", "\x1B[31m", "\x1B[0m");
v5 = -1;
}
else
{
v4 = diagnostics_relay_client_new();
v5 = v4;
diagerr = v4;
if ( v4 )
{
printf("%s diag client error %s %d\n", "\x1B[31m", "\x1B[0m", v4);
lockdownd_client_free();
idevice_free();
v5 = -1;
}
else
{
v6 = diagnostics_relay_restart();
diagerr = v6;
if ( ((v6 + 2) & 0xFFFFFFFD) != 0 )
{
printf("%s reboot error, reboot manually %d %s\n", "\x1B[0m", v6, "\x1B[0m");
lockdownd_client_free();
idevice_free();
v5 = -1;
}
else
{
puts(" done :P ");
}
}
}
}
}
}
return v5;
}

original_code:
int main(int argc, char *argv[]) {
 printf(" exVasi0n, tihmstar will pwn you :O\n");
 printf(" waiting for device\n");
 while (deviceConnect() != 0) {
  sleep(1);
 }
 printf(" device found!\n");
 if (startLockdownd() != 0) {
  return -1;
 }
 if (startAFC() != 0) {
  return -1;
 }
 if (connectAFC() != 0) {
  return -1;
 }
 afcerr = afc_make_directory(gAfc, "/evasi0n-install");
 if (afcerr != AFC_E_SUCCESS) {
  printf("%s Error creating dir %s\n\n", "\x1B[31m", "\x1B[0m");
  afc_client_free(gAfc);
  idevice_free(gDevice);
  return -1;
 }
 afcerr = afc_send_file(gAfc, "mylittlesecret.tar", "evasi0n-install/Cydia.tar");
 if (afcerr != AFC_E_SUCCESS) {
  printf("%s Error putting file.%s\n\n", "\x1B[31m", "\x1B[0m");
  return -1;
 }
 printf(" lockdownd...\n");
 lderr = lockdownd_client_new_with_handshake(gDevice, &gLockdown, "exVasi0n");
 if (lderr != LOCKDOWN_E_SUCCESS) {
  printf("%s [*] Unable to connect to lockdownd. Please reboot your device and try again.%s\n", "\x1B[31m", "\x1B[0m");
  return -1;
 }
 printf(" gonna reboot\n");
 lderr = lockdownd_start_service(gLockdown, "com.apple.mobile.diagnostics_relay", &port);
 if (lderr != LOCKDOWN_E_SUCCESS) {
  printf("%s diag service error%s\n", "\x1B[31m", "\x1B[0m");
  return -1;
 }
 diagerr = diagnostics_relay_client_new(gDevice, port, &gDiag);
 if (diagerr != DIAGNOSTICS_RELAY_E_SUCCESS) {
  printf("%s diag client error %s %d\n", "\x1B[31m", "\x1B[0m",diagerr);
  lockdownd_client_free(gLockdown);
  idevice_free(gDevice);
  return -1;
 }
 diagerr = diagnostics_relay_restart(gDiag, (1 << 2));
 if (diagerr != DIAGNOSTICS_RELAY_E_SUCCESS && diagerr != -2) {
        printf("%s reboot error, reboot manually %d %s\n", "\x1B[0m",diagerr, "\x1B[0m");
  lockdownd_client_free(gLockdown);
  idevice_free(gDevice);
  return -1;
 }
 printf(" done :P \n");
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


repo: tillmannw/prowler

decompiled_code:
time_t __fastcall session_init(__int64 a1, _QWORD *a2, __int64 a3)
{
__int64 v4; // rbx
_QWORD *v5; // rcx
__int64 v6; // rdi
__int64 v7; // rax
__int64 v8; // r12
time_t result; // rax
__int64 v10; // rbx

v4 = 104 * a1;
v5 = (_QWORD *)(session + 104 * a1);
*v5 = 0LL;
v5[12] = 0LL;
memset(
(void *)((unsigned __int64)(v5 + 1) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)v5 - (((_DWORD)v5 + 8) & 0xFFFFFFF8) + 104) >> 3));
v6 = a2[1];
v7 = session;
*(_QWORD *)(session + v4) = *a2;
*(_QWORD *)(v7 + v4 + 8) = v6;
*(_WORD *)(session + v4) = 2;
v8 = session + v4;
result = time(0LL);
*(_QWORD *)(v8 + 88) = result;
v10 = session + v4;
*(_QWORD *)(v10 + 80) = result;
if ( result == -1 )
{
perror("time()");
exit(1);
}
*(_DWORD *)(v10 + 64) = 1;
*(_QWORD *)(v10 + 96) = a3;
return result;
}

canonical_decompiled_code:
time_t __fastcall func1(__int64 a1, _QWORD *a2, __int64 a3)
{
__int64 v4; // rbx
_QWORD *v5; // rcx
__int64 v6; // rdi
__int64 v7; // rax
__int64 v8; // r12
time_t result; // rax
__int64 v10; // rbx

v4 = 104 * a1;
v5 = (_QWORD *)(session + 104 * a1);
*v5 = 0LL;
v5[12] = 0LL;
memset(
(void *)((unsigned __int64)(v5 + 1) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)v5 - (((_DWORD)v5 + 8) & 0xFFFFFFF8) + 104) >> 3));
v6 = a2[1];
v7 = session;
*(_QWORD *)(session + v4) = *a2;
*(_QWORD *)(v7 + v4 + 8) = v6;
*(_WORD *)(session + v4) = 2;
v8 = session + v4;
result = time(0LL);
*(_QWORD *)(v8 + 88) = result;
v10 = session + v4;
*(_QWORD *)(v10 + 80) = result;
if ( result == -1 )
{
perror("time()");
exit(1);
}
*(_DWORD *)(v10 + 64) = 1;
*(_QWORD *)(v10 + 96) = a3;
return result;
}

original_code:
void session_init(size_t i, struct sockaddr_in *saddr, size_t peerlist_index) {
 memset(&session[i], 0, sizeof(session_t));
 memcpy(&session[i].peer, saddr, sizeof(struct sockaddr_in));
 session[i].peer.sin_family = 2;
 if ((session[i].session_start = session[i].last_action = time(((void *)0))) == -1) {
  perror("time()");
  exit(1);
 }
 session[i].used = 1;
 session[i].peerlist_index = peerlist_index;
 return;
}

canonical_original_code:
void session_init(unsigned long i, struct sockaddr_in *saddr, unsigned long peerlist_index) {
 memset(&session[i], 0, sizeof(session_t));
 memcpy(&session[i].peer, saddr, sizeof(struct sockaddr_in));
 session[i].peer.sin_family = 2;
 if ((session[i].session_start = session[i].last_action = time(((void *)0))) == -1) {
  perror("time()");
  exit(1);
 }
 session[i].used = 1;
 session[i].peerlist_index = peerlist_index;
 return;
}

variable_types:
i
unsigned long

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]; } *


decompiled_code:
__int64 __fastcall session_connect(__int64 a1)
{
int *v1; // r12
int v2; // eax
unsigned int v3; // eax
__int64 v4; // r12
unsigned int v5; // ebp
__int64 v6; // rax
int v8; // eax
__int64 v9; // rbp

v1 = (int *)(fds + 8 * a1);
v2 = socket(2, 1, 0);
*v1 = v2;
if ( v2 == -1 )
{
perror("socket()");
exit(1);
}
v3 = fcntl(*(_DWORD *)(fds + 8 * a1), 3, 0LL);
if ( v3 == -1 || (BYTE1(v3) |= 8u, fcntl(*(_DWORD *)(fds + 8 * a1), 4, v3) == -1) )
{
perror("fcntl()");
exit(1);
}
*(_WORD *)(fds + 8 * a1 + 4) = 5;
v4 = 104 * a1;
v5 = connect(*(_DWORD *)(fds + 8 * a1), (const struct sockaddr *)(session + 104 * a1), 0x10u);
if ( v5 == -1 )
{
v8 = *_errno_location();
if ( v8 == 101 )
return v5;
if ( v8 != 115 )
{
perror("connect()");
exit(1);
}
*(_DWORD *)(session + v4 + 68) = 1;
}
v6 = *(_QWORD *)(session + v4 + 96);
if ( v6 != -1 )
{
v9 = peerlist + 48 * v6;
*(_QWORD *)(v9 + 24) = time(0LL);
*(_DWORD *)(peerlist + 48LL * *(_QWORD *)(session + v4 + 96) + 12) = 1;
}
return *(unsigned int *)(fds + 8 * a1);
}

original_code:
int session_connect(size_t i) {
 int flags;
 if ((fds[i].fd = socket(2, SOCK_STREAM, 0)) == -1) {
  perror("socket()");
  exit(1);
 }
 if (((flags = fcntl(fds[i].fd, 3, 0)) == -1) ||
     (fcntl(fds[i].fd, 4, flags | 04000) == -1)) {
  perror("fcntl()");
  exit(1);
 }
 fds[i].events = 0x004 | 0x001;
 if (connect(fds[i].fd, (struct sockaddr *) &session[i].peer, sizeof(struct sockaddr_in)) == -1) {
  switch ((*__errno_location ())) {
  case 115:
   break;
  case 101:
   return -1;
   break;
  default:
   perror("connect()");
   exit(1);
  }
  session[i].connecting = 1;
 }
 if (session[i].peerlist_index != -1) {
  peerlist[session[i].peerlist_index].last_connect = time(((void *)0));
  peerlist[session[i].peerlist_index].active = 1;
 }
 return fds[i].fd;
}

canonical_original_code:
int session_connect(unsigned long i) {
 int flags;
 if ((fds[i].fd = socket(2, SOCK_STREAM, 0)) == -1) {
  perror("socket()");
  exit(1);
 }
 if (((flags = fcntl(fds[i].fd, 3, 0)) == -1) ||
     (fcntl(fds[i].fd, 4, flags | 04000) == -1)) {
  perror("fcntl()");
  exit(1);
 }
 fds[i].events = 0x004 | 0x001;
 if (connect(fds[i].fd, (struct sockaddr *) &session[i].peer, sizeof(struct sockaddr_in)) == -1) {
  switch ((*__errno_location ())) {
  case 115:
   break;
  case 101:
   return -1;
   break;
  default:
   perror("connect()");
   exit(1);
  }
  session[i].connecting = 1;
 }
 if (session[i].peerlist_index != -1) {
  peerlist[session[i].peerlist_index].last_connect = time(((void *)0));
  peerlist[session[i].peerlist_index].active = 1;
 }
 return fds[i].fd;
}

variable_types: flags int