original_code:
void
floatformat_from_double (const struct floatformat *fmt,
                         const double *from, void *to)
{
  double dfrom;
  int exponent;
  double mant;
  unsigned int mant_bits, mant_off;
  int mant_bits_left;
  unsigned char *uto = (unsigned char *) to;
  dfrom = *from;
  memset (uto, 0, fmt->totalsize / 8);
  if (dfrom < 0)
    {
      put_field (uto, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1, 1);
      dfrom = -dfrom;
    }
  if (dfrom == 0)
    {
      return;
    }
  if (dfrom != dfrom)
    {
      put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
   fmt->exp_len, fmt->exp_nan);
      put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start,
   32, 1);
      return;
    }
  if (dfrom + dfrom == dfrom)
    {
      put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
   fmt->exp_len, fmt->exp_nan);
      return;
    }
  mant = frexp (dfrom, &exponent);
  if (exponent + fmt->exp_bias - 1 > 0)
    put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
        fmt->exp_len, exponent + fmt->exp_bias - 1);
  else
    {
      put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
   fmt->exp_len, 0);
      mant = ldexp (mant, exponent + fmt->exp_bias - 1);
    }
  mant_bits_left = fmt->man_len;
  mant_off = fmt->man_start;
  while (mant_bits_left > 0)
    {
      unsigned long mant_long;
      mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
      mant *= 4294967296.0;
      mant_long = (unsigned long)mant;
      mant -= mant_long;
      if ((unsigned int) mant_bits_left == fmt->man_len
   && fmt->intbit == floatformat_intbit_no
   && exponent + fmt->exp_bias - 1 > 0)
 {
   mant_long &= 0x7fffffff;
   mant_bits -= 1;
 }
      else if (mant_bits < 32)
 {
   mant_long >>= 32 - mant_bits;
 }
      put_field (uto, fmt->byteorder, fmt->totalsize,
   mant_off, mant_bits, mant_long);
      mant_off += mant_bits;
      mant_bits_left -= mant_bits;
    }
}

canonical_original_code:
void floatformat_from_double(struct floatformat *fmt, double *from, void *to)
{
  double dfrom;
  int exponent;
  double mant;
  unsigned int mant_bits; unsigned int mant_off;
  int mant_bits_left;
  unsigned char *uto = (unsigned char *) to;
  dfrom = *from;
  memset (uto, 0, fmt->totalsize / 8);
  if (dfrom < 0)
    {
      put_field (uto, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1, 1);
      dfrom = -dfrom;
    }
  if (dfrom == 0)
    {
      return;
    }
  if (dfrom != dfrom)
    {
      put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
   fmt->exp_len, fmt->exp_nan);
      put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start,
   32, 1);
      return;
    }
  if (dfrom + dfrom == dfrom)
    {
      put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
   fmt->exp_len, fmt->exp_nan);
      return;
    }
  mant = frexp (dfrom, &exponent);
  if (exponent + fmt->exp_bias - 1 > 0)
    put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
        fmt->exp_len, exponent + fmt->exp_bias - 1);
  else
    {
      put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
   fmt->exp_len, 0);
      mant = ldexp (mant, exponent + fmt->exp_bias - 1);
    }
  mant_bits_left = fmt->man_len;
  mant_off = fmt->man_start;
  while (mant_bits_left > 0)
    {
      unsigned long mant_long;
      mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
      mant *= 4294967296.0;
      mant_long = (unsigned long)mant;
      mant -= mant_long;
      if ((unsigned int) mant_bits_left == fmt->man_len
   && fmt->intbit == floatformat_intbit_no
   && exponent + fmt->exp_bias - 1 > 0)
 {
   mant_long &= 0x7fffffff;
   mant_bits -= 1;
 }
      else if (mant_bits < 32)
 {
   mant_long >>= 32 - mant_bits;
 }
      put_field (uto, fmt->byteorder, fmt->totalsize,
   mant_off, mant_bits, mant_long);
      mant_off += mant_bits;
      mant_bits_left -= mant_bits;
    }
}

variable_types:
from
double *

variable_types:
to
void *

variable_types: dfrom double

variable_types: exponent int

variable_types: mant double

variable_types:
uto
unsigned char *


decompiled_code:
__int64 __fastcall floatformat_is_valid(__int64 a1)
{
return (*(__int64 (**)(void))(a1 + 48))();
}

original_code:
int
floatformat_is_valid (const struct floatformat *fmt, const void *from)
{
  return fmt->is_valid (fmt, from);
}

canonical_original_code:
int floatformat_is_valid(struct floatformat *fmt, void *from)
{
  return fmt->is_valid (fmt, from);
}

variable_types: struct floatformat { enum floatformat_byteorders { floatformat_little=0, floatformat_big=1, floatformat_littlebyte_bigword=2, floatformat_vax=3 } byteorder; unsigned int totalsize; unsigned int sign_start; unsigned int exp_start; unsigned int exp_len; int exp_bias; unsigned int exp_nan; unsigned int man_start; unsigned int man_len; enum floatformat_intbit { floatformat_intbit_yes=0, floatformat_intbit_no=1 } intbit; char *name; int (*is_valid)(struct floatformat *fmt, void *from); struct floatformat *split_half; } *

variable_types:
from
void *

return_type: int

user_defined_types: struct floatformat { enum floatformat_byteorders byteorder; unsigned int totalsize; unsigned int sign_start; unsigned int exp_start; unsigned int exp_len; int exp_bias; unsigned int exp_nan; unsigned int man_start; unsigned int man_len; enum floatformat_intbit intbit; char *name; int (*is_valid)(struct floatformat *fmt, void *from); struct floatformat *split_half; }


ifdown

decompiled_code:
__int64 ifdown()
{
int v0; // eax
int v1; // er12
unsigned int v2; // er14
char *v3; // r15
char *i; // rbp
__int16 v5; // ax
const char *v6; // rdi
int v8; // [rsp+0h] [rbp-A48h] BYREF
char *v9; // [rsp+8h] [rbp-A40h]
char s[2616]; // [rsp+10h] [rbp-A38h] BYREF

v0 = socket(2, 2, 0);
if ( v0 < 0 )
{
v2 = -1;
fwrite("ifdown: ", 1uLL, 8uLL, stderr);
perror("socket");
}
else
{
v1 = v0;
v9 = s;
v8 = 2560;
if ( ioctl(v0, 0x8912uLL, &v8) < 0 )
{
v2 = -1;
fwrite("ifdown: ", 1uLL, 8uLL, stderr);
perror("SIOCGIFCONF");
close(v1);
}
else
{
v2 = 1;
v3 = &s[v8];
while ( 1 )
{
for ( i = s; v3 > i; i += 40 )
{
while ( 1 )
{
if ( (memcmp(i, "shaper", 6uLL) == 0) == v2 && (*i != 108 || i[1] != 111) && !strchr(i, 58) )
{
if ( ioctl(v1, 0x8913uLL, i) < 0 )
break;
v5 = *((_WORD *)i + 8);
if ( (v5 & 1) != 0 )
{
*((_WORD *)i + 8) = v5 & 0xFFFE;
if ( ioctl(v1, 0x8914uLL, i) < 0 )
break;
}
}
i += 40;
if ( v3 <= i )
goto LABEL_15;
}
fwrite("ifdown: shutdown ", 1uLL, 0x11uLL, stderr);
v6 = i;
perror(v6);
}
LABEL_15:
if ( !v2 )
break;
v2 = 0;
}
close(v1);
}
}
return v2;
}

canonical_decompiled_code:
__int64 func0()
{
int v0; // eax
int v1; // er12
unsigned int v2; // er14
char *v3; // r15
char *i; // rbp
__int16 v5; // ax
const char *v6; // rdi
int v8; // [rsp+0h] [rbp-A48h] BYREF
char *v9; // [rsp+8h] [rbp-A40h]
char s[2616]; // [rsp+10h] [rbp-A38h] BYREF

v0 = socket(2, 2, 0);
if ( v0 < 0 )
{
v2 = -1;
fwrite("ifdown: ", 1uLL, 8uLL, stderr);
perror("socket");
}
else
{
v1 = v0;
v9 = s;
v8 = 2560;
if ( ioctl(v0, 0x8912uLL, &v8) < 0 )
{
v2 = -1;
fwrite("ifdown: ", 1uLL, 8uLL, stderr);
perror("SIOCGIFCONF");
close(v1);
}
else
{
v2 = 1;
v3 = &s[v8];
while ( 1 )
{
for ( i = s; v3 > i; i += 40 )
{
while ( 1 )
{
if ( (memcmp(i, "shaper", 6uLL) == 0) == v2 && (*i != 108 || i[1] != 111) && !strchr(i, 58) )
{
if ( ioctl(v1, 0x8913uLL, i) < 0 )
break;
v5 = *((_WORD *)i + 8);
if ( (v5 & 1) != 0 )
{
*((_WORD *)i + 8) = v5 & 0xFFFE;
if ( ioctl(v1, 0x8914uLL, i) < 0 )
break;
}
}
i += 40;
if ( v3 <= i )
goto LABEL_15;
}
fwrite("ifdown: shutdown ", 1uLL, 0x11uLL, stderr);
v6 = i;
perror(v6);
}
LABEL_15:
if ( !v2 )
break;
v2 = 0;
}
close(v1);
}
}
return v2;
}

original_code:
int ifdown(void)
{
 char ifr_buf[sizeof(struct ifreq) * 64];
 char *ifr_end;
 struct ifconf ifc;
 int fd;
 int shaper;
 if ((fd = socket(2, SOCK_DGRAM, 0)) < 0) {
  fprintf(stderr, "ifdown: ");
  perror("socket");
  return -1;
 }
 ifc.ifc_len = sizeof(ifr_buf);
 ifc.ifc_ifcu.ifcu_buf = ifr_buf;
 if (ioctl(fd, 0x8912, &ifc) < 0) {
  fprintf(stderr, "ifdown: ");
  perror("SIOCGIFCONF");
  close(fd);
  return -1;
 }
 ifr_end = ifr_buf + ifc.ifc_len;
 for (shaper = 1; shaper >= 0; shaper--) {
  char *ifr_next = ifr_buf;
  while (ifr_next < ifr_end) {
   struct ifreq *ifr;
   int flags;
   ifr = (struct ifreq *)ifr_next;
   ifr_next += sizeof(struct ifreq);
   if ((strncmp(ifr->ifr_ifrn.ifrn_name, "shaper", 6) == 0)
       != shaper) continue;
   if (strncmp(ifr->ifr_ifrn.ifrn_name, "lo", 2) == 0)
    continue;
   if (strchr(ifr->ifr_ifrn.ifrn_name, ':') != ((void *)0))
    continue;
   if (ioctl(fd, 0x8913, ifr) < 0) {
    fprintf(stderr, "ifdown: shutdown ");
    perror(ifr->ifr_ifrn.ifrn_name);
    continue;
   }
   flags = ifr->ifr_ifru.ifru_flags;
   if (flags & IFF_UP) {
    flags &= ~(IFF_UP);
    ifr->ifr_ifru.ifru_flags = flags;
    if (ioctl(fd, 0x8914, ifr) < 0) {
     fprintf(stderr, "ifdown: shutdown ");
     perror(ifr->ifr_ifrn.ifrn_name);
    }
   }
  }
 }
 close(fd);
 return 0;
}

canonical_original_code:
int ifdown()
{
 char ifr_buf[-1];
 char *ifr_end;
 struct ifconf ifc;
 int fd;
 int shaper;
 if ((fd = socket(2, SOCK_DGRAM, 0)) < 0) {
  fprintf(stderr, "ifdown: ");
  perror("socket");
  return -1;
 }
 ifc.ifc_len = sizeof(ifr_buf);
 ifc.ifc_ifcu.ifcu_buf = ifr_buf;
 if (ioctl(fd, 0x8912, &ifc) < 0) {
  fprintf(stderr, "ifdown: ");
  perror("SIOCGIFCONF");
  close(fd);
  return -1;
 }
 ifr_end = ifr_buf + ifc.ifc_len;
 for (shaper = 1; shaper >= 0; shaper--) {
  char *ifr_next = ifr_buf;
  while (ifr_next < ifr_end) {
   struct ifreq *ifr;
   int flags;
   ifr = (struct ifreq *)ifr_next;
   ifr_next += sizeof(struct ifreq);
   if ((strncmp(ifr->ifr_ifrn.ifrn_name, "shaper", 6) == 0)
       != shaper) continue;
   if (strncmp(ifr->ifr_ifrn.ifrn_name, "lo", 2) == 0)
    continue;
   if (strchr(ifr->ifr_ifrn.ifrn_name, ':') != ((void *)0))
    continue;
   if (ioctl(fd, 0x8913, ifr) < 0) {
    fprintf(stderr, "ifdown: shutdown ");
    perror(ifr->ifr_ifrn.ifrn_name);
    continue;
   }
   flags = ifr->ifr_ifru.ifru_flags;
   if (flags & IFF_UP) {
    flags &= ~(IFF_UP);
    ifr->ifr_ifru.ifru_flags = flags;
    if (ioctl(fd, 0x8914, ifr) < 0) {
     fprintf(stderr, "ifdown: shutdown ");
     perror(ifr->ifr_ifrn.ifrn_name);
    }
   }
  }
 }
 close(fd);
 return 0;
}

variable_types: struct ifconf { int ifc_len; union { char *ifcu_buf; struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr { unsigned short sa_family; char sa_data[14]; } ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap { unsigned long mem_start; unsigned long mem_end; unsigned short base_addr; unsigned char irq; unsigned char dma; unsigned char port; } ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; } *ifcu_req; } ifc_ifcu; }

variable_types: fd int

variable_types: shaper int

variable_types: flags int

user_defined_types: struct ifconf { int ifc_len; union { char *ifcu_buf; struct ifreq *ifcu_req; } ifc_ifcu; }

user_defined_types: struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; }


original_code:
static void fb_update(struct FB *fb)
{
    fb->vi.yoffset = 1;
    ioctl(fb->fd, 0x4601, &fb->vi);
    fb->vi.yoffset = 0;
    ioctl(fb->fd, 0x4601, &fb->vi);
}

variable_types: struct FB { unsigned char *bits; int fd; struct fb_var_screeninfo { unsigned int xres; unsigned int yres; unsigned int xres_virtual; unsigned int yres_virtual; unsigned int xoffset; unsigned int yoffset; unsigned int bits_per_pixel; unsigned int grayscale; struct fb_bitfield { unsigned int offset; unsigned int length; unsigned int msb_right; } red; struct fb_bitfield green; struct fb_bitfield blue; struct fb_bitfield transp; unsigned int nonstd; unsigned int activate; unsigned int height; unsigned int width; unsigned int accel_flags; unsigned int pixclock; unsigned int left_margin; unsigned int right_margin; unsigned int upper_margin; unsigned int lower_margin; unsigned int hsync_len; unsigned int vsync_len; unsigned int sync; unsigned int vmode; unsigned int rotate; unsigned int colorspace; unsigned int reserved[4]; } vi; int chars; } *

return_type: void

user_defined_types: struct FB { unsigned char *bits; int fd; struct fb_var_screeninfo vi; int chars; }

user_defined_types: struct fb_var_screeninfo { unsigned int xres; unsigned int yres; unsigned int xres_virtual; unsigned int yres_virtual; unsigned int xoffset; unsigned int yoffset; unsigned int bits_per_pixel; unsigned int grayscale; struct fb_bitfield red; struct fb_bitfield green; struct fb_bitfield blue; struct fb_bitfield transp; unsigned int nonstd; unsigned int activate; unsigned int height; unsigned int width; unsigned int accel_flags; unsigned int pixclock; unsigned int left_margin; unsigned int right_margin; unsigned int upper_margin; unsigned int lower_margin; unsigned int hsync_len; unsigned int vsync_len; unsigned int sync; unsigned int vmode; unsigned int rotate; unsigned int colorspace; unsigned int reserved[4]; }


original_code:
static void print_buf(struct FB *fb, const char *buf, int size)
{
    int pos;
    int x, y, width, height, d, char_width, bytes_per_pixel;
    unsigned char *bits, c;
    width = ((fb)->vi.xres);
    height = ((fb)->vi.yres);
    bytes_per_pixel = fb->vi.bits_per_pixel / 8;
    bits = fb->bits;
    char_width = width / 8;
    for(pos = 0; pos < size; ++pos) {
        c = buf[pos];
        if(fb->chars == char_width ) {
            memmove(bits, bits + 16 * width * bytes_per_pixel,
                    (height - 16) * width * bytes_per_pixel);
            memset(bits + (height - 16) * width * bytes_per_pixel,
                    0, 16 * width * bytes_per_pixel);
            fb->chars = 0;
        }
        if( c == '\n' ) {
            fb->chars = char_width;
            continue;
        }
        for(y = 0; y < 16; ++y) {
            for(x = 0; x < 8; ++x) {
                for(d = 0; d < bytes_per_pixel; ++d) {
                    if(fontdata_8x16[16 * c +y] & 0x80 >> x) {
                        bits[((height-16+y) * width + 8 * fb->chars + x) * bytes_per_pixel + d] = 0xff;
                    }else{
                        bits[((height-16+y) * width + 8 * fb->chars + x) * bytes_per_pixel + d] = 0;
                    }
                }
            }
        }
        ++fb->chars;
    }
}

canonical_original_code:
static void print_buf(struct FB *fb, char *buf, int size)
{
    int pos;
    int x; int y; int width; int height; int d; int char_width; int bytes_per_pixel;
    unsigned char *bits; unsigned char c;
    width = ((fb)->vi.xres);
    height = ((fb)->vi.yres);
    bytes_per_pixel = fb->vi.bits_per_pixel / 8;
    bits = fb->bits;
    char_width = width / 8;
    for(pos = 0; pos < size; ++pos) {
        c = buf[pos];
        if(fb->chars == char_width ) {
            memmove(bits, bits + 16 * width * bytes_per_pixel,
                    (height - 16) * width * bytes_per_pixel);
            memset(bits + (height - 16) * width * bytes_per_pixel,
                    0, 16 * width * bytes_per_pixel);
            fb->chars = 0;
        }
        if( c == '\n' ) {
            fb->chars = char_width;
            continue;
        }
        for(y = 0; y < 16; ++y) {
            for(x = 0; x < 8; ++x) {
                for(d = 0; d < bytes_per_pixel; ++d) {
                    if(fontdata_8x16[16 * c +y] & 0x80 >> x) {
                        bits[((height-16+y) * width + 8 * fb->chars + x) * bytes_per_pixel + d] = 0xff;
                    }else{
                        bits[((height-16+y) * width + 8 * fb->chars + x) * bytes_per_pixel + d] = 0;
                    }
                }
            }
        }
        ++fb->chars;
    }
}

variable_types:
buf
char *

variable_types: size int

variable_types: pos int

variable_types: x int

variable_types: y int

variable_types: width int

variable_types: height int

variable_types: d int

variable_types:
bits
unsigned char *

variable_types:
c
unsigned char

user_defined_types: struct fb_bitfield { unsigned int offset; unsigned int length; unsigned int msb_right; }


decompiled_code:
int __fastcall fb_close(__int64 a1)
{
munmap(*(void **)a1, (unsigned int)(*(_DWORD *)(a1 + 36) * *(_DWORD *)(a1 + 16) * *(_DWORD *)(a1 + 12)) >> 3);
return close(*(_DWORD *)(a1 + 8));
}

original_code:
static void fb_close(struct FB *fb)
{
    munmap(fb->bits, ((fb)->vi.xres * (fb)->vi.yres * (fb)->vi.bits_per_pixel / 8));
    close(fb->fd);
}


decompiled_code:
void print_response_data()
{
__int64 v0; // rcx
unsigned int v1; // eax
bool v2; // zf
int v3; // [rsp+0h] [rbp-4000Ch] BYREF
char ptr[262152]; // [rsp+4h] [rbp-40008h] BYREF

v0 = (unsigned int)dword_E54;
if ( dword_E54 )
{
do
{
if ( (unsigned int)v0 > 0x40000 )
v0 = 0x40000LL;
v1 = libusb_bulk_transfer(usb, 129LL, ptr, v0, &v3, 0LL);
if ( v1 )
{
printf("read error: status=%d\n", v1);
exit(1);
}
fwrite(ptr, v3, 1uLL, stdout);
v2 = dword_E54 == v3;
v0 = (unsigned int)(dword_E54 - v3);
dword_E54 -= v3;
}
while ( !v2 );
}
}

canonical_decompiled_code:
void func3()
{
__int64 v0; // rcx
unsigned int v1; // eax
bool v2; // zf
int v3; // [rsp+0h] [rbp-4000Ch] BYREF
char ptr[262152]; // [rsp+4h] [rbp-40008h] BYREF

v0 = (unsigned int)dword_E54;
if ( dword_E54 )
{
do
{
if ( (unsigned int)v0 > 0x40000 )
v0 = 0x40000LL;
v1 = libusb_bulk_transfer(usb, 129LL, ptr, v0, &v3, 0LL);
if ( v1 )
{
printf("read error: status=%d\n", v1);
exit(1);
}
fwrite(ptr, v3, 1uLL, stdout);
v2 = dword_E54 == v3;
v0 = (unsigned int)(dword_E54 - v3);
dword_E54 -= v3;
}
while ( !v2 );
}
}

original_code:
void print_response_data(void)
{
    int status, transferred, toRead;
    char buf[0x40000];
    while( cur_resp_header.datasize > 0 ) {
        toRead = sizeof(buf) < cur_resp_header.datasize ?
            sizeof(buf) : cur_resp_header.datasize;
        if( (status = libusb_bulk_transfer(usb, 1 | LIBUSB_ENDPOINT_IN,
                (unsigned char*)buf, toRead, &transferred, 0)) != 0)
        {
            printf("read error: status=%d\n", status);
            exit(1);
        }
        fwrite(buf, transferred, 1, stdout);
        cur_resp_header.datasize -= transferred;
    }
}

canonical_original_code:
void print_response_data()
{
    int status; int transferred; int toRead;
    char buf[262144];
    while( cur_resp_header.datasize > 0 ) {
        toRead = sizeof(buf) < cur_resp_header.datasize ?
            sizeof(buf) : cur_resp_header.datasize;
        if( (status = libusb_bulk_transfer(usb, 1 | LIBUSB_ENDPOINT_IN,
                (unsigned char *)buf, toRead, &transferred, 0)) != 0)
        {
            printf("read error: status=%d\n", status);
            exit(1);
        }
        fwrite(buf, transferred, 1, stdout);
        cur_resp_header.datasize -= transferred;
    }
}

variable_types: status int

variable_types: transferred int

variable_types: toRead int


decompiled_code:
__int64 __fastcall send_command(__int64 a1, unsigned int a2, __int64 a3, __int64 a4, __int64 a5, unsigned int a6)
{
unsigned __int16 v8; // r10
__int64 v9; // rdx
unsigned int v10; // ecx
int v11; // eax
int v12; // er9
__int64 v13; // rdx
__int64 v14; // rcx
__int64 v15; // r8
unsigned int v16; // eax
unsigned __int16 v17; // ax
__int64 v18; // rsi
__int16 v19; // dx
unsigned int v21; // [rsp+Ch] [rbp-4Ch] BYREF
__int16 v22; // [rsp+10h] [rbp-48h]
__int16 v23; // [rsp+12h] [rbp-46h]
int v24; // [rsp+14h] [rbp-44h]
__int64 v25; // [rsp+18h] [rbp-40h]
int v26; // [rsp+20h] [rbp-38h]
int v27; // [rsp+24h] [rbp-34h]

if ( dword_E54 )
{
puts("internal error: unread data of previous command");
exit(1);
}
v22 = _uint16_identity(0x1234u);
v23 = _uint16_identity(v8);
v24 = _uint32_identity(a2);
v25 = _uint64_identity(v9);
v11 = _uint32_identity(v10);
v27 = v12;
v26 = v11;
if ( (unsigned int)libusb_bulk_transfer(usb, 2LL, v13, v14, v15, 0LL)
|| a6 && (unsigned int)libusb_bulk_transfer(usb, 2LL, a5, a6, &v21, 0LL) )
{
puts("write error");
exit(1);
}
do
{
v16 = libusb_bulk_transfer(usb, 129LL, &cur_resp_header, 8LL, &v21, 0LL);
if ( v16 )
{
printf("read error: status=%d\n", v16);
exit(1);
}
if ( v21 != 8 )
{
printf("read error: bad header size=%d\n", v21);
exit(1);
}
v17 = _uint16_identity((unsigned __int16)cur_resp_header);
v18 = v17;
cur_resp_header = v17;
word_E52 = _uint16_identity((unsigned __int16)word_E52);
dword_E54 = _uint32_identity(dword_E54);
if ( (_WORD)v18 != 4660 )
{
printf("read error: bad magic=%x\n", v18);
v19 = word_E52;
}
if ( v19 != 2 )
break;
print_response_data();
v19 = word_E52;
}
while ( word_E52 == 2 );
if ( !v19 )
{
puts("command execution failed on device");
exit(1);
}
return (unsigned int)dword_E54;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, unsigned int a2, __int64 a3, __int64 a4, __int64 a5, unsigned int a6)
{
unsigned __int16 v8; // r10
__int64 v9; // rdx
unsigned int v10; // ecx
int v11; // eax
int v12; // er9
__int64 v13; // rdx
__int64 v14; // rcx
__int64 v15; // r8
unsigned int v16; // eax
unsigned __int16 v17; // ax
__int64 v18; // rsi
__int16 v19; // dx
unsigned int v21; // [rsp+Ch] [rbp-4Ch] BYREF
__int16 v22; // [rsp+10h] [rbp-48h]
__int16 v23; // [rsp+12h] [rbp-46h]
int v24; // [rsp+14h] [rbp-44h]
__int64 v25; // [rsp+18h] [rbp-40h]
int v26; // [rsp+20h] [rbp-38h]
int v27; // [rsp+24h] [rbp-34h]

if ( dword_E54 )
{
puts("internal error: unread data of previous command");
exit(1);
}
v22 = func0(0x1234u);
v23 = func0(v8);
v24 = func1(a2);
v25 = func2(v9);
v11 = func1(v10);
v27 = v12;
v26 = v11;
if ( (unsigned int)libusb_bulk_transfer(usb, 2LL, v13, v14, v15, 0LL)
|| a6 && (unsigned int)libusb_bulk_transfer(usb, 2LL, a5, a6, &v21, 0LL) )
{
puts("write error");
exit(1);
}
do
{
v16 = libusb_bulk_transfer(usb, 129LL, &cur_resp_header, 8LL, &v21, 0LL);
if ( v16 )
{
printf("read error: status=%d\n", v16);
exit(1);
}
if ( v21 != 8 )
{
printf("read error: bad header size=%d\n", v21);
exit(1);
}
v17 = func0((unsigned __int16)cur_resp_header);
v18 = v17;
cur_resp_header = v17;
word_E52 = func0((unsigned __int16)word_E52);
dword_E54 = func1(dword_E54);
if ( (_WORD)v18 != 4660 )
{
printf("read error: bad magic=%x\n", v18);
v19 = word_E52;
}
if ( v19 != 2 )
break;
func3();
v19 = word_E52;
}
while ( word_E52 == 2 );
if ( !v19 )
{
puts("command execution failed on device");
exit(1);
}
return (unsigned int)dword_E54;
}

original_code:
int send_command(enum BootdiskCommand cmd, unsigned param1,
        unsigned long long start, unsigned count,
        const void *data, unsigned datasize)
{
    struct bootdisk_cmd_header cmd_header;
    int status, transferred;
    if( cur_resp_header.datasize > 0 ) {
        printf("internal error: unread data of previous command\n");
        exit(1);
    }
    cmd_header.magic = __uint16_identity (0x1234);
    cmd_header.cmd = __uint16_identity (cmd);
    cmd_header.param1 = __uint32_identity (param1);
    cmd_header.start = __uint64_identity (start);
    cmd_header.count = __uint32_identity (count);
    cmd_header.datasize = datasize;
    if( libusb_bulk_transfer(usb, 2 | LIBUSB_ENDPOINT_OUT,
                (unsigned char*)&cmd_header, sizeof(cmd_header),
                &transferred, 0) != 0 )
    {
        printf("write error\n");
        exit(1);
    }
    if( datasize > 0 && libusb_bulk_transfer(usb, 2 | LIBUSB_ENDPOINT_OUT,
                (unsigned char*)data, datasize, &transferred, 0) != 0 )
    {
        printf("write error\n");
        exit(1);
    }
    do {
        if( (status = libusb_bulk_transfer(usb, 1 | LIBUSB_ENDPOINT_IN,
                (void*)&cur_resp_header, sizeof(cur_resp_header),
                &transferred, 0)) != 0)
        {
            printf("read error: status=%d\n", status);
            exit(1);
        }
        if( transferred != sizeof(cur_resp_header) ) {
            printf("read error: bad header size=%d\n", transferred);
            exit(1);
        }
        cur_resp_header.magic = __uint16_identity (cur_resp_header.magic);
        cur_resp_header.status = __uint16_identity (cur_resp_header.status);
        cur_resp_header.datasize = __uint32_identity (cur_resp_header.datasize);
        if( cur_resp_header.magic != 0x1234 ) {
            printf("read error: bad magic=%x\n", cur_resp_header.magic);
        }
        if( cur_resp_header.status == BRST_MSG )
            print_response_data();
    }while( cur_resp_header.status == BRST_MSG );
    if( cur_resp_header.status == BRST_FAIL ) {
        printf("command execution failed on device\n");
        exit(1);
    }
    return cur_resp_header.datasize;
}

canonical_original_code:
int send_command(enum BootdiskCommand cmd, unsigned int param1, unsigned long long start, unsigned int count, void *data, unsigned int datasize)
{
    struct bootdisk_cmd_header cmd_header;
    int status; int transferred;
    if( cur_resp_header.datasize > 0 ) {
        printf("internal error: unread data of previous command\n");
        exit(1);
    }
    cmd_header.magic = __uint16_identity (0x1234);
    cmd_header.cmd = __uint16_identity (cmd);
    cmd_header.param1 = __uint32_identity (param1);
    cmd_header.start = __uint64_identity (start);
    cmd_header.count = __uint32_identity (count);
    cmd_header.datasize = datasize;
    if( libusb_bulk_transfer(usb, 2 | LIBUSB_ENDPOINT_OUT,
                (unsigned char *)&cmd_header, sizeof(cmd_header),
                &transferred, 0) != 0 )
    {
        printf("write error\n");
        exit(1);
    }
    if( datasize > 0 && libusb_bulk_transfer(usb, 2 | LIBUSB_ENDPOINT_OUT,
                (unsigned char *)data, datasize, &transferred, 0) != 0 )
    {
        printf("write error\n");
        exit(1);
    }
    do {
        if( (status = libusb_bulk_transfer(usb, 1 | LIBUSB_ENDPOINT_IN,
                (void *)&cur_resp_header, sizeof(cur_resp_header),
                &transferred, 0)) != 0)
        {
            printf("read error: status=%d\n", status);
            exit(1);
        }
        if( transferred != sizeof(cur_resp_header) ) {
            printf("read error: bad header size=%d\n", transferred);
            exit(1);
        }
        cur_resp_header.magic = __uint16_identity (cur_resp_header.magic);
        cur_resp_header.status = __uint16_identity (cur_resp_header.status);
        cur_resp_header.datasize = __uint32_identity (cur_resp_header.datasize);
        if( cur_resp_header.magic != 0x1234 ) {
            printf("read error: bad magic=%x\n", cur_resp_header.magic);
        }
        if( cur_resp_header.status == BRST_MSG )
            print_response_data();
    }while( cur_resp_header.status == BRST_MSG );
    if( cur_resp_header.status == BRST_FAIL ) {
        printf("command execution failed on device\n");
        exit(1);
    }
    return cur_resp_header.datasize;
}

variable_types: enum BootdiskCommand { BCMD_NONE=0, BCMD_FLASHMEM_PARAMS=expr, BCMD_BOARD_EXIT=expr, BCMD_PING=expr, BCMD_MEMJUMPTO=expr, BCMD_GETLOG=expr, BCMD_SLEEPTEST=expr, BCMD_DISKREAD=expr, BCMD_DISKWRITE=expr, BCMD_MEMREAD=expr, BCMD_MEMWRITE=expr, BCMD_MEMEXEC=expr, BCMD_MOUNT=expr, BCMD_GETMOUNT_STATUS=expr }

variable_types:
start
unsigned long long

variable_types:
count
unsigned int

variable_types:
data
void *

variable_types:
datasize
unsigned int

variable_types: struct bootdisk_cmd_header { unsigned short magic; unsigned short cmd; unsigned int param1; unsigned long start; unsigned int count; unsigned int datasize; }


decompiled_code:
void __fastcall get_response_data(__int64 a1, unsigned int a2)
{
__int64 v3; // rcx
unsigned int v4; // eax
bool v5; // zf
int v6[3]; // [rsp+Ch] [rbp-Ch] BYREF

if ( dword_E54 != a2 )
{
puts("error: data size to receive doesn't match expected");
exit(1);
}
v3 = a2;
if ( a2 )
{
do
{
v4 = libusb_bulk_transfer(usb, 129LL, a1, v3, v6, 0LL);
if ( v4 )
{
printf("read error: status=%d\n", v4);
exit(1);
}
a1 += v6[0];
v5 = dword_E54 == v6[0];
v3 = (unsigned int)(dword_E54 - v6[0]);
dword_E54 -= v6[0];
}
while ( !v5 );
}
}

canonical_decompiled_code:
void __fastcall func5(__int64 a1, unsigned int a2)
{
__int64 v3; // rcx
unsigned int v4; // eax
bool v5; // zf
int v6[3]; // [rsp+Ch] [rbp-Ch] BYREF

if ( dword_E54 != a2 )
{
puts("error: data size to receive doesn't match expected");
exit(1);
}
v3 = a2;
if ( a2 )
{
do
{
v4 = libusb_bulk_transfer(usb, 129LL, a1, v3, v6, 0LL);
if ( v4 )
{
printf("read error: status=%d\n", v4);
exit(1);
}
a1 += v6[0];
v5 = dword_E54 == v6[0];
v3 = (unsigned int)(dword_E54 - v6[0]);
dword_E54 -= v6[0];
}
while ( !v5 );
}
}

original_code:
void get_response_data(void *buf, unsigned bufsize)
{
    int status, transferred;
    if( bufsize != cur_resp_header.datasize ) {
        printf("error: data size to receive doesn't match expected\n");
        exit(1);
    }
    while( cur_resp_header.datasize > 0 ) {
        if( (status = libusb_bulk_transfer(usb, 1 | LIBUSB_ENDPOINT_IN,
                buf, cur_resp_header.datasize, &transferred, 0)) != 0)
        {
            printf("read error: status=%d\n", status);
            exit(1);
        }
        buf += transferred;
        cur_resp_header.datasize -= transferred;
    }
}

canonical_original_code:
void get_response_data(void *buf, unsigned int bufsize)
{
    int status; int transferred;
    if( bufsize != cur_resp_header.datasize ) {
        printf("error: data size to receive doesn't match expected\n");
        exit(1);
    }
    while( cur_resp_header.datasize > 0 ) {
        if( (status = libusb_bulk_transfer(usb, 1 | LIBUSB_ENDPOINT_IN,
                buf, cur_resp_header.datasize, &transferred, 0)) != 0)
        {
            printf("read error: status=%d\n", status);
            exit(1);
        }
        buf += transferred;
        cur_resp_header.datasize -= transferred;
    }
}

variable_types:
buf
void *

variable_types:
bufsize
unsigned int


decompiled_code:
void __fastcall read_disk(unsigned int a1, __int64 a2, unsigned int a3, __int64 a4)
{
send_command(82LL, a1, a2, a3, 0LL, 0);
get_response_data(a4, a3 << 9);
}

canonical_decompiled_code:
void __fastcall func6(unsigned int a1, __int64 a2, unsigned int a3, __int64 a4)
{
func4(82LL, a1, a2, a3, 0LL, 0);
func5(a4, a3 << 9);
}

original_code:
void read_disk(enum FlashMemoryArea fmarea,
        unsigned long long firstSector,
        unsigned long long count, void *rdbuf)
{
    send_command(BCMD_DISKREAD, fmarea, firstSector, count, ((void *)0), 0);
    get_response_data(rdbuf, count * 512);
}

variable_types: enum FlashMemoryArea { FMAREA_BOOT0=0, FMAREA_BOOT1=1, FMAREA_PHYDISK=2, FMAREA_LOGDISK=3, FMAREA_COUNT=4 }

variable_types:
firstSector
unsigned long long

variable_types:
count
unsigned long long

variable_types:
rdbuf
void *


getPartitionData

decompiled_code:
int *getPartitionData()
{
int *result; // rax
unsigned __int16 v1; // ax
char *v2; // rbp
char *v3; // rbx
int v4; // er12
unsigned int v5; // eax
__int64 v6; // rdx
unsigned int v7; // edi
unsigned int v8; // eax
__int64 v9; // rdx
char *v10; // rdi
const char *v11; // rsi
int v12; // eax
unsigned int v13[2]; // [rsp-4h] [rbp-418h] BYREF
__int64 v14; // [rsp+4h] [rbp-410h]
unsigned __int16 v15; // [rsp+Eh] [rbp-406h]
char src; // [rsp+20h] [rbp-3F4h] BYREF

if ( isValid_1 == -1 )
{
isValid_1 = 0;
read_disk(3u, 0LL, 2u, (__int64)v13);
if ( v14 == 0x3131337774666F73LL )
{
partitionData_0 = _uint32_identity(v13[1]);
v1 = _uint16_identity(v15);
dword_AA4 = v1;
if ( v1 > 0xFu )
{
printf("error: wrong partition count=%d\n", v1);
result = 0LL;
}
else
{
v2 = dest;
v3 = &src;
v4 = 0;
if ( v1 )
{
do
{
++v4;
_uint32_identity(*((_DWORD *)v3 - 4));
v5 = _uint32_identity(*((_DWORD *)v3 - 3));
v7 = *((_DWORD *)v3 - 2);
*((_QWORD *)v2 - 2) = v5 + (v6 << 32);
_uint32_identity(v7);
v8 = _uint32_identity(*((_DWORD *)v3 - 1));
v2[12] = 0;
*((_QWORD *)v2 - 1) = v8 + (v9 << 32);
strncpy(v2, v3, 0xCuLL);
v10 = v2 + 13;
v11 = v3 + 12;
v2[25] = 0;
v3 += 64;
v2 += 56;
strncpy(v10, v11, 0xCuLL);
v12 = _uint32_identity(*((_DWORD *)v3 - 10));
LODWORD(v10) = *((_DWORD *)v3 - 9);
*((_DWORD *)v2 - 7) = v12;
*((_DWORD *)v2 - 6) = _uint32_identity((unsigned int)v10);
}
while ( dword_AA4 > v4 );
}
isValid_1 = 1;
result = &partitionData_0;
}
}
else
{
puts("disk seems to not have allwinner partition table");
result = 0LL;
}
}
else
{
result = &partitionData_0;
if ( !isValid_1 )
result = 0LL;
}
return result;
}

canonical_decompiled_code:
int *func7()
{
int *result; // rax
unsigned __int16 v1; // ax
char *v2; // rbp
char *v3; // rbx
int v4; // er12
unsigned int v5; // eax
__int64 v6; // rdx
unsigned int v7; // edi
unsigned int v8; // eax
__int64 v9; // rdx
char *v10; // rdi
const char *v11; // rsi
int v12; // eax
unsigned int v13[2]; // [rsp-4h] [rbp-418h] BYREF
__int64 v14; // [rsp+4h] [rbp-410h]
unsigned __int16 v15; // [rsp+Eh] [rbp-406h]
char src; // [rsp+20h] [rbp-3F4h] BYREF

if ( isValid_1 == -1 )
{
isValid_1 = 0;
func6(3u, 0LL, 2u, (__int64)v13);
if ( v14 == 0x3131337774666F73LL )
{
partitionData_0 = func1(v13[1]);
v1 = func0(v15);
dword_AA4 = v1;
if ( v1 > 0xFu )
{
printf("error: wrong partition count=%d\n", v1);
result = 0LL;
}
else
{
v2 = dest;
v3 = &src;
v4 = 0;
if ( v1 )
{
do
{
++v4;
func1(*((_DWORD *)v3 - 4));
v5 = func1(*((_DWORD *)v3 - 3));
v7 = *((_DWORD *)v3 - 2);
*((_QWORD *)v2 - 2) = v5 + (v6 << 32);
func1(v7);
v8 = func1(*((_DWORD *)v3 - 1));
v2[12] = 0;
*((_QWORD *)v2 - 1) = v8 + (v9 << 32);
strncpy(v2, v3, 0xCuLL);
v10 = v2 + 13;
v11 = v3 + 12;
v2[25] = 0;
v3 += 64;
v2 += 56;
strncpy(v10, v11, 0xCuLL);
v12 = func1(*((_DWORD *)v3 - 10));
LODWORD(v10) = *((_DWORD *)v3 - 9);
*((_DWORD *)v2 - 7) = v12;
*((_DWORD *)v2 - 6) = func1((unsigned int)v10);
}
while ( dword_AA4 > v4 );
}
isValid_1 = 1;
result = &partitionData_0;
}
}
else
{
puts("disk seems to not have allwinner partition table");
result = 0LL;
}
}
else
{
result = &partitionData_0;
if ( !isValid_1 )
result = 0LL;
}
return result;
}

original_code:
static struct MbrPartitionData *getPartitionData(void)
{
    static struct MbrPartitionData partitionData;
    static int isValid = -1;
    if( isValid == -1 ) {
        MBR mbr;
        int i;
        isValid = 0;
        read_disk(FMAREA_LOGDISK, 0, 2, &mbr);
        if( memcmp(mbr.magic, MBR_MAGIC, 8) ) {
            printf("disk seems to not have allwinner partition table\n");
            return ((void *)0);
        }
        partitionData.version = __uint32_identity (mbr.version);
        partitionData.partitionCount = __uint16_identity (mbr.PartCount);
        if( partitionData.partitionCount < 0 ||
                partitionData.partitionCount > MBR_MAX_PART_COUNT )
        {
            printf("error: wrong partition count=%d\n",
                    partitionData.partitionCount);
            return ((void *)0);
        }
        for(i = 0; i < partitionData.partitionCount; ++i) {
            partitionData.partitions[i].firstSector =
                ((unsigned long long)__uint32_identity (mbr.array[i].addrhi) << 32) +
                __uint32_identity (mbr.array[i].addrlo);
            partitionData.partitions[i].sectorCount =
                ((unsigned long long)__uint32_identity (mbr.array[i].lenhi) << 32) +
                __uint32_identity (mbr.array[i].lenlo);
            memset(partitionData.partitions[i].classname, 0,
                    sizeof(partitionData.partitions[i].classname));
            strncpy(partitionData.partitions[i].classname,
                    mbr.array[i].classname,
                    sizeof(mbr.array[i].classname));
            memset(partitionData.partitions[i].name, 0,
                    sizeof(partitionData.partitions[i].name));
            strncpy(partitionData.partitions[i].name, mbr.array[i].name,
                    sizeof(mbr.array[i].name));
            partitionData.partitions[i].user_type =
                __uint32_identity (mbr.array[i].user_type);
            partitionData.partitions[i].ro = __uint32_identity (mbr.array[i].ro);
        }
        isValid = 1;
    }
    return isValid ? &partitionData : ((void *)0);
}

canonical_original_code:
static struct MbrPartitionData *getPartitionData()
{
    static struct MbrPartitionData partitionData;
    static int isValid = -1;
    if( isValid == -1 ) {
        struct tag_MBR mbr;
        int i;
        isValid = 0;
        read_disk(FMAREA_LOGDISK, 0, 2, &mbr);
        if( memcmp(mbr.magic, MBR_MAGIC, 8) ) {
            printf("disk seems to not have allwinner partition table\n");
            return ((void *)0);
        }
        partitionData.version = __uint32_identity (mbr.version);
        partitionData.partitionCount = __uint16_identity (mbr.PartCount);
        if( partitionData.partitionCount < 0 ||
                partitionData.partitionCount > MBR_MAX_PART_COUNT )
        {
            printf("error: wrong partition count=%d\n",
                    partitionData.partitionCount);
            return ((void *)0);
        }
        for(i = 0; i < partitionData.partitionCount; ++i) {
            partitionData.partitions[i].firstSector =
                ((unsigned long long)__uint32_identity (mbr.array[i].addrhi) << 32) +
                __uint32_identity (mbr.array[i].addrlo);
            partitionData.partitions[i].sectorCount =
                ((unsigned long long)__uint32_identity (mbr.array[i].lenhi) << 32) +
                __uint32_identity (mbr.array[i].lenlo);
            memset(partitionData.partitions[i].classname, 0,
                    sizeof(partitionData.partitions[i].classname));
            strncpy(partitionData.partitions[i].classname,
                    mbr.array[i].classname,
                    sizeof(mbr.array[i].classname));
            memset(partitionData.partitions[i].name, 0,
                    sizeof(partitionData.partitions[i].name));
            strncpy(partitionData.partitions[i].name, mbr.array[i].name,
                    sizeof(mbr.array[i].name));
            partitionData.partitions[i].user_type =
                __uint32_identity (mbr.array[i].user_type);
            partitionData.partitions[i].ro = __uint32_identity (mbr.array[i].ro);
        }
        isValid = 1;
    }
    return isValid ? &partitionData : ((void *)0);
}

variable_types: isValid int

variable_types: struct tag_MBR { unsigned int crc32; unsigned int version; char magic[8]; unsigned char copy; unsigned char index; unsigned short PartCount; struct tag_PARTITION { unsigned int addrhi; unsigned int addrlo; unsigned int lenhi; unsigned int lenlo; char classname[12]; char name[12]; unsigned int user_type; unsigned int ro; char res[16]; } array[-1]; char res[-1]; }

variable_types: i int

return_type: struct MbrPartitionData { int version; int partitionCount; struct MbrPartition { unsigned long long firstSector; unsigned long long sectorCount; char classname[13]; char name[13]; unsigned int user_type; unsigned int ro; } partitions[-1]; } *

user_defined_types: struct MbrPartitionData { int version; int partitionCount; struct MbrPartition partitions[-1]; }

user_defined_types: struct tag_MBR { unsigned int crc32; unsigned int version; char magic[8]; unsigned char copy; unsigned char index; unsigned short PartCount; struct tag_PARTITION array[-1]; char res[-1]; }


decompiled_code:
__int64 __fastcall write_disk(unsigned int a1, __int64 a2, unsigned int a3, __int64 a4)
{
return send_command(87LL, a1, a2, a3, a4, a3 << 9);
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned int a1, __int64 a2, unsigned int a3, __int64 a4)
{
return func4(87LL, a1, a2, a3, a4, a3 << 9);
}

original_code:
void write_disk(enum FlashMemoryArea fmarea,
        unsigned long long firstSector,
        unsigned long long count, const void *wrbuf)
{
    send_command(BCMD_DISKWRITE, fmarea, firstSector, count, wrbuf, count*512);
}

canonical_original_code:
void write_disk(enum FlashMemoryArea fmarea, unsigned long long firstSector, unsigned long long count, void *wrbuf)
{
    send_command(BCMD_DISKWRITE, fmarea, firstSector, count, wrbuf, count*512);
}

variable_types:
wrbuf
void *

user_defined_types: enum FlashMemoryArea { FMAREA_BOOT0=0, FMAREA_BOOT1=1, FMAREA_PHYDISK=2, FMAREA_LOGDISK=3, FMAREA_COUNT=4 }


decompiled_code:
void *get_flashmem_props()
{
unsigned int *v1; // rdx
int v2; // eax
__int64 v3; // rdx
unsigned int v4; // edi
int v5; // eax
__int64 v6; // rdx
unsigned int v7; // edi
unsigned int v8; // eax
int v9; // eax

if ( is_initialized_3 )
return &props_2;
send_command(70LL, 0, 0LL, 0LL, 0LL, 0);
get_response_data((__int64)&props_2, 0x48u);
v1 = (unsigned int *)&props_2;
do
{
v2 = _uint32_identity(*v1);
v4 = *(_DWORD *)(v3 - 8);
*(_DWORD *)(v3 - 12) = v2;
v5 = _uint32_identity(v4);
v7 = *(_DWORD *)(v6 - 4);
*(_DWORD *)(v6 - 8) = v5;
v8 = _uint32_identity(v7);
*(v1 - 1) = v8;
}
while ( v1 != (unsigned int *)&dword_E30 );
dword_E30 = _uint32_identity(dword_E30);
dword_E34 = _uint32_identity(dword_E34);
dword_E38 = _uint32_identity(dword_E38);
dword_E3C = _uint32_identity(dword_E3C);
dword_E40 = _uint32_identity(dword_E40);
v9 = _uint32_identity(dword_E44);
is_initialized_3 = 1;
dword_E44 = v9;
return &props_2;
}

canonical_decompiled_code:
void *func9()
{
unsigned int *v1; // rdx
int v2; // eax
__int64 v3; // rdx
unsigned int v4; // edi
int v5; // eax
__int64 v6; // rdx
unsigned int v7; // edi
unsigned int v8; // eax
int v9; // eax

if ( is_initialized_3 )
return &props_2;
func4(70LL, 0, 0LL, 0LL, 0LL, 0);
func5((__int64)&props_2, 0x48u);
v1 = (unsigned int *)&props_2;
do
{
v2 = func1(*v1);
v4 = *(_DWORD *)(v3 - 8);
*(_DWORD *)(v3 - 12) = v2;
v5 = func1(v4);
v7 = *(_DWORD *)(v6 - 4);
*(_DWORD *)(v6 - 8) = v5;
v8 = func1(v7);
*(v1 - 1) = v8;
}
while ( v1 != (unsigned int *)&dword_E30 );
dword_E30 = func1(dword_E30);
dword_E34 = func1(dword_E34);
dword_E38 = func1(dword_E38);
dword_E3C = func1(dword_E3C);
dword_E40 = func1(dword_E40);
v9 = func1(dword_E44);
is_initialized_3 = 1;
dword_E44 = v9;
return &props_2;
}

original_code:
struct flashmem_properties *get_flashmem_props(void)
{
    static struct flashmem_properties props;
    static int is_initialized;
    if( ! is_initialized ) {
        int i;
        send_command(BCMD_FLASHMEM_PARAMS, 0, 0, 0, ((void *)0), 0);
        get_response_data(&props, sizeof(props));
        for(i = 0; i < FMAREA_COUNT; ++i) {
            props.areas[i].sectors_per_page =
                __uint32_identity (props.areas[i].sectors_per_page);
            props.areas[i].first_pageno =
                __uint32_identity (props.areas[i].first_pageno);
            props.areas[i].page_count =
                __uint32_identity (props.areas[i].page_count);
        }
        props.sectors_per_page = __uint32_identity (props.sectors_per_page);
        props.pages_per_block = __uint32_identity (props.pages_per_block);
        props.blocks_per_chip = __uint32_identity (props.blocks_per_chip);
        props.chip_cnt = __uint32_identity (props.chip_cnt);
        props.pagewithbadflag = __uint32_identity (props.pagewithbadflag);
        props.block_cnt_of_zone = __uint32_identity (props.block_cnt_of_zone);
        is_initialized = 1;
    }
    return &props;
}

canonical_original_code:
struct flashmem_properties *get_flashmem_props()
{
    static struct flashmem_properties props;
    static int is_initialized;
    if( ! is_initialized ) {
        int i;
        send_command(BCMD_FLASHMEM_PARAMS, 0, 0, 0, ((void *)0), 0);
        get_response_data(&props, sizeof(props));
        for(i = 0; i < FMAREA_COUNT; ++i) {
            props.areas[i].sectors_per_page =
                __uint32_identity (props.areas[i].sectors_per_page);
            props.areas[i].first_pageno =
                __uint32_identity (props.areas[i].first_pageno);
            props.areas[i].page_count =
                __uint32_identity (props.areas[i].page_count);
        }
        props.sectors_per_page = __uint32_identity (props.sectors_per_page);
        props.pages_per_block = __uint32_identity (props.pages_per_block);
        props.blocks_per_chip = __uint32_identity (props.blocks_per_chip);
        props.chip_cnt = __uint32_identity (props.chip_cnt);
        props.pagewithbadflag = __uint32_identity (props.pagewithbadflag);
        props.block_cnt_of_zone = __uint32_identity (props.block_cnt_of_zone);
        is_initialized = 1;
    }
    return &props;
}

return_type: struct flashmem_properties { struct flashmarea_properties { unsigned int sectors_per_page; unsigned int first_pageno; unsigned int page_count; } areas[-1]; unsigned int sectors_per_page; unsigned int pages_per_block; unsigned int blocks_per_chip; unsigned int chip_cnt; unsigned int pagewithbadflag; unsigned int block_cnt_of_zone; } *

user_defined_types: struct flashmem_properties { struct flashmarea_properties areas[-1]; unsigned int sectors_per_page; unsigned int pages_per_block; unsigned int blocks_per_chip; unsigned int chip_cnt; unsigned int pagewithbadflag; unsigned int block_cnt_of_zone; }


getMbrVersion

decompiled_code:
__int64 getMbrVersion()
{
int *v0; // rax
__int64 result; // rax

v0 = getPartitionData();
if ( v0 )
result = (unsigned int)*v0;
else
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 func10()
{
int *v0; // rax
__int64 result; // rax

v0 = func7();
if ( v0 )
result = (unsigned int)*v0;
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int getMbrVersion(void)
{
    struct MbrPartitionData *pdata = getPartitionData();
    return pdata == ((void *)0) ? -1 : pdata->version;
}

canonical_original_code:
int getMbrVersion()
{
    struct MbrPartitionData *pdata = getPartitionData();
    return pdata == ((void *)0) ? -1 : pdata->version;
}

variable_types: struct MbrPartitionData { int version; int partitionCount; struct MbrPartition { unsigned long long firstSector; unsigned long long sectorCount; char classname[13]; char name[13]; unsigned int user_type; unsigned int ro; } partitions[-1]; } *


getPartitionCount

decompiled_code:
__int64 getPartitionCount()
{
int *v0; // rax
__int64 result; // rax

v0 = getPartitionData();
if ( v0 )
result = (unsigned int)v0[1];
else
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 func11()
{
int *v0; // rax
__int64 result; // rax

v0 = func7();
if ( v0 )
result = (unsigned int)v0[1];
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int getPartitionCount(void)
{
    struct MbrPartitionData *pdata = getPartitionData();
    return pdata == ((void *)0) ? -1 : pdata->partitionCount;
}

canonical_original_code:
int getPartitionCount()
{
    struct MbrPartitionData *pdata = getPartitionData();
    return pdata == ((void *)0) ? -1 : pdata->partitionCount;
}

user_defined_types: struct MbrPartition { unsigned long long firstSector; unsigned long long sectorCount; char classname[13]; char name[13]; unsigned int user_type; unsigned int ro; }


getPartitionNoParams

decompiled_code:
int *__fastcall getPartitionNoParams(int a1)
{
int *result; // rax

result = getPartitionData();
if ( result )
{
if ( result[1] <= a1 )
result = 0LL;
else
result += 14 * a1 + 2;
}
return result;
}

canonical_decompiled_code:
int *__fastcall func12(int a1)
{
int *result; // rax

result = func7();
if ( result )
{
if ( result[1] <= a1 )
result = 0LL;
else
result += 14 * a1 + 2;
}
return result;
}

original_code:
struct MbrPartition *getPartitionNoParams(int partNo)
{
    struct MbrPartitionData *pdata = getPartitionData();
    return pdata == ((void *)0) || partNo >= pdata->partitionCount ? ((void *)0) :
        pdata->partitions + partNo;
}

variable_types: partNo int

return_type: struct MbrPartition { unsigned long long firstSector; unsigned long long sectorCount; char classname[13]; char name[13]; unsigned int user_type; unsigned int ro; } *


getParitionParams

decompiled_code:
__int64 __fastcall getParitionParams(char *nptr, _DWORD *a2, unsigned __int64 *a3, _QWORD *a4)
{
unsigned int *v5; // rax
unsigned int *v6; // r12
__int64 v7; // rax
__int64 result; // rax
int v9; // eax
const char *v10; // r14
int v11; // er15
__int64 v12; // rsi
__int64 v13; // rdx
__int64 v14; // rax
unsigned __int64 v15; // rcx
unsigned __int64 v16; // rsi
int v17; // eax
int v19; // [rsp+14h] [rbp-44h]
int *v20; // [rsp+18h] [rbp-40h]

v5 = (unsigned int *)get_flashmem_props();
*a2 = 4;
v6 = v5;
if ( !strcmp(nptr, "boot0") )
{
*a2 = 0;
v7 = 0LL;
LABEL_3:
*a3 = 0LL;
*a4 = v6[3 * v7 + 2] * (unsigned __int64)v6[3 * v7];
return 1LL;
}
if ( !strcmp(nptr, "boot1") )
{
*a2 = 1;
v7 = 1LL;
goto LABEL_3;
}
if ( !strcmp(nptr, "disk-logic") )
{
*a2 = 3;
v7 = 3LL;
goto LABEL_3;
}
v20 = getPartitionData();
if ( !v20 )
return 0LL;
v9 = v20[1];
v19 = v9;
if ( v9 <= 0 )
{
if ( v9 )
{
v11 = 0;
}
else
{
LABEL_17:
if ( ((*_ctype_b_loc())[*nptr] & 0x800) == 0 )
{
printf("partition with name %s not found\n", nptr);
return 0LL;
}
v17 = strtoul(nptr, 0LL, 0);
v11 = v17;
if ( v17 < 0 || v20[1] <= v17 )
{
printf("wrong prtition number %s\n", nptr);
return 0LL;
}
}
}
else
{
v10 = (char *)v20 + 37;
v11 = 0;
while ( strcmp(nptr, v10) )
{
++v11;
v10 += 56;
if ( v19 == v11 )
goto LABEL_17;
}
}
*a2 = 3;
v12 = v6[11];
v13 = 14LL * v11;
*a3 = *(_QWORD *)&v20[v13 + 2];
v14 = *(_QWORD *)&v20[v13 + 4];
v15 = v12 * v6[9];
*a4 = v14;
if ( *a3 >= v15 || (v16 = v14 + *a3, result = 1LL, v16 > v15) )
{
printf("ERROR: corrupted MBR, partition %s is beyond disk size\n", (const char *)&v20[v13 + 9] + 1);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(char *nptr, _DWORD *a2, unsigned __int64 *a3, _QWORD *a4)
{
unsigned int *v5; // rax
unsigned int *v6; // r12
__int64 v7; // rax
__int64 result; // rax
int v9; // eax
const char *v10; // r14
int v11; // er15
__int64 v12; // rsi
__int64 v13; // rdx
__int64 v14; // rax
unsigned __int64 v15; // rcx
unsigned __int64 v16; // rsi
int v17; // eax
int v19; // [rsp+14h] [rbp-44h]
int *v20; // [rsp+18h] [rbp-40h]

v5 = (unsigned int *)func9();
*a2 = 4;
v6 = v5;
if ( !strcmp(nptr, "boot0") )
{
*a2 = 0;
v7 = 0LL;
LABEL_3:
*a3 = 0LL;
*a4 = v6[3 * v7 + 2] * (unsigned __int64)v6[3 * v7];
return 1LL;
}
if ( !strcmp(nptr, "boot1") )
{
*a2 = 1;
v7 = 1LL;
goto LABEL_3;
}
if ( !strcmp(nptr, "disk-logic") )
{
*a2 = 3;
v7 = 3LL;
goto LABEL_3;
}
v20 = func7();
if ( !v20 )
return 0LL;
v9 = v20[1];
v19 = v9;
if ( v9 <= 0 )
{
if ( v9 )
{
v11 = 0;
}
else
{
LABEL_17:
if ( ((*_ctype_b_loc())[*nptr] & 0x800) == 0 )
{
printf("partition with name %s not found\n", nptr);
return 0LL;
}
v17 = strtoul(nptr, 0LL, 0);
v11 = v17;
if ( v17 < 0 || v20[1] <= v17 )
{
printf("wrong prtition number %s\n", nptr);
return 0LL;
}
}
}
else
{
v10 = (char *)v20 + 37;
v11 = 0;
while ( strcmp(nptr, v10) )
{
++v11;
v10 += 56;
if ( v19 == v11 )
goto LABEL_17;
}
}
*a2 = 3;
v12 = v6[11];
v13 = 14LL * v11;
*a3 = *(_QWORD *)&v20[v13 + 2];
v14 = *(_QWORD *)&v20[v13 + 4];
v15 = v12 * v6[9];
*a4 = v14;
if ( *a3 >= v15 || (v16 = v14 + *a3, result = 1LL, v16 > v15) )
{
printf("ERROR: corrupted MBR, partition %s is beyond disk size\n", (const char *)&v20[v13 + 9] + 1);
result = 0LL;
}
return result;
}

original_code:
int getParitionParams(const char *partName,
        enum FlashMemoryArea *pFMArea,
        unsigned long long *pFirstSector,
        unsigned long long *pSectorCount)
{
    struct flashmem_properties *props = get_flashmem_props();
    struct MbrPartitionData *pdata;
    *pFMArea = FMAREA_COUNT;
    if( ! strcmp(partName, "boot0") ) {
        *pFMArea = FMAREA_BOOT0;
    }else if( ! strcmp(partName, "boot1") ) {
        *pFMArea = FMAREA_BOOT1;
    }else if( ! strcmp(partName, "disk-logic") ) {
        *pFMArea = FMAREA_LOGDISK;
    }
    if( *pFMArea != FMAREA_COUNT ) {
        *pFirstSector = 0LL;
        *pSectorCount = 1ULL * props->areas[*pFMArea].sectors_per_page *
            props->areas[*pFMArea].page_count;
    }else{
        int i;
        unsigned long long diskSize;
        if( (pdata = getPartitionData()) == ((void *)0) )
            return 0;
        for( i = 0; i < pdata->partitionCount; ++i ) {
            if( ! strcmp(partName, pdata->partitions[i].name) )
                break;
        }
        if( i == pdata->partitionCount ) {
            if( ((*__ctype_b_loc ())[(int) ((partName[0]))] & (unsigned short int) _ISdigit) ) {
                i = strtoul(partName, ((void *)0), 0);
                if( i < 0 || i >= pdata->partitionCount ) {
                    printf("wrong prtition number %s\n", partName);
                    return 0;
                }
            }else{
                printf("partition with name %s not found\n", partName);
                return 0;
            }
        }
        *pFMArea = FMAREA_LOGDISK;
        *pFirstSector = pdata->partitions[i].firstSector;
        *pSectorCount = pdata->partitions[i].sectorCount;
        diskSize = 1ULL * props->areas[FMAREA_LOGDISK].sectors_per_page *
            props->areas[FMAREA_LOGDISK].page_count;
        if( *pFirstSector >= diskSize ||
                *pFirstSector + *pSectorCount > diskSize)
        {
            printf("ERROR: corrupted MBR, partition %s is beyond disk size\n",
                    pdata->partitions[i].name);
            return 0;
        }
    }
    return 1;
}

canonical_original_code:
int getParitionParams(char *partName, enum FlashMemoryArea *pFMArea, unsigned long long *pFirstSector, unsigned long long *pSectorCount)
{
    struct flashmem_properties *props = get_flashmem_props();
    struct MbrPartitionData *pdata;
    *pFMArea = FMAREA_COUNT;
    if( ! strcmp(partName, "boot0") ) {
        *pFMArea = FMAREA_BOOT0;
    }else if( ! strcmp(partName, "boot1") ) {
        *pFMArea = FMAREA_BOOT1;
    }else if( ! strcmp(partName, "disk-logic") ) {
        *pFMArea = FMAREA_LOGDISK;
    }
    if( *pFMArea != FMAREA_COUNT ) {
        *pFirstSector = 0LL;
        *pSectorCount = 1ULL * props->areas[*pFMArea].sectors_per_page *
            props->areas[*pFMArea].page_count;
    }else{
        int i;
        unsigned long long diskSize;
        if( (pdata = getPartitionData()) == ((void *)0) )
            return 0;
        for( i = 0; i < pdata->partitionCount; ++i ) {
            if( ! strcmp(partName, pdata->partitions[i].name) )
                break;
        }
        if( i == pdata->partitionCount ) {
            if( ((*__ctype_b_loc ())[(int) ((partName[0]))] & (unsigned short) _ISdigit) ) {
                i = strtoul(partName, ((void *)0), 0);
                if( i < 0 || i >= pdata->partitionCount ) {
                    printf("wrong prtition number %s\n", partName);
                    return 0;
                }
            }else{
                printf("partition with name %s not found\n", partName);
                return 0;
            }
        }
        *pFMArea = FMAREA_LOGDISK;
        *pFirstSector = pdata->partitions[i].firstSector;
        *pSectorCount = pdata->partitions[i].sectorCount;
        diskSize = 1ULL * props->areas[FMAREA_LOGDISK].sectors_per_page *
            props->areas[FMAREA_LOGDISK].page_count;
        if( *pFirstSector >= diskSize ||
                *pFirstSector + *pSectorCount > diskSize)
        {
            printf("ERROR: corrupted MBR, partition %s is beyond disk size\n",
                    pdata->partitions[i].name);
            return 0;
        }
    }
    return 1;
}

variable_types:
partName
char *

variable_types: enum FlashMemoryArea { FMAREA_BOOT0=0, FMAREA_BOOT1=1, FMAREA_PHYDISK=2, FMAREA_LOGDISK=3, FMAREA_COUNT=4 } *

variable_types:
pFirstSector
unsigned long long *

variable_types:
pSectorCount
unsigned long long *

variable_types: struct flashmem_properties { struct flashmarea_properties { unsigned int sectors_per_page; unsigned int first_pageno; unsigned int page_count; } areas[-1]; unsigned int sectors_per_page; unsigned int pages_per_block; unsigned int blocks_per_chip; unsigned int chip_cnt; unsigned int pagewithbadflag; unsigned int block_cnt_of_zone; } *

variable_types:
diskSize
unsigned long long


decompiled_code:
__int64 __fastcall get_mountstatus(unsigned int *a1)
{
unsigned int v2; // eax
unsigned int v3; // edi
unsigned int v4; // eax
unsigned int v5; // edi
__int64 result; // rax

send_command(109LL, 0, 0LL, 0LL, 0LL, 0);
get_response_data((__int64)a1, 0xCu);
v2 = _uint32_identity(*a1);
v3 = a1[1];
*a1 = v2;
v4 = _uint32_identity(v3);
v5 = a1[2];
a1[1] = v4;
result = _uint32_identity(v5);
a1[2] = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func14(unsigned int *a1)
{
unsigned int v2; // eax
unsigned int v3; // edi
unsigned int v4; // eax
unsigned int v5; // edi
__int64 result; // rax

func4(109LL, 0, 0LL, 0LL, 0LL, 0);
func5((__int64)a1, 0xCu);
v2 = func1(*a1);
v3 = a1[1];
*a1 = v2;
v4 = func1(v3);
v5 = a1[2];
a1[1] = v4;
result = func1(v5);
a1[2] = result;
return result;
}

original_code:
void get_mountstatus(struct diskmount_status *stat)
{
    send_command(BCMD_GETMOUNT_STATUS, 0, 0, 0, ((void *)0), 0);
    get_response_data(stat, sizeof(*stat));
    stat->mountMode = __uint32_identity (stat->mountMode);
    stat->firstSector = __uint32_identity (stat->firstSector);
    stat->sectorCount = __uint32_identity (stat->sectorCount);
}

variable_types: struct diskmount_status { unsigned int mountMode; unsigned int firstSector; unsigned int sectorCount; } *


decompiled_code:
__int64 usbcomm_init()
{
unsigned int v0; // er12
__int64 v1; // rdi
int v3; // ebx
__int64 v4; // rax

if ( (unsigned int)libusb_init(0LL) )
{
v0 = -1;
puts("libusb_init fail");
}
else
{
v0 = 0;
usb = libusb_open_device_with_vid_pid(0LL, 2996LL, 4095LL);
v1 = usb;
if ( usb )
goto LABEL_3;
printf("open device failed, trying to fel...");
fflush(stdout);
v3 = fel_ainol();
if ( v3 )
{
v0 = -1;
fwrite("\nfailed to open A10 USB FEL device\n", 1uLL, 0x23uLL, stderr);
return v0;
}
if ( !usb )
{
do
{
++v3;
putchar(46);
fflush(stdout);
usleep(0x7A120u);
v4 = libusb_open_device_with_vid_pid(0LL, 2996LL, 4095LL);
usb = v4;
}
while ( v3 != 10 && !v4 );
}
putchar(10);
v1 = usb;
if ( usb )
{
LABEL_3:
libusb_claim_interface(v1, 0LL);
}
else
{
v0 = -1;
puts("open device failed");
}
}
return v0;
}

canonical_decompiled_code:
__int64 func15()
{
unsigned int v0; // er12
__int64 v1; // rdi
int v3; // ebx
__int64 v4; // rax

if ( (unsigned int)libusb_init(0LL) )
{
v0 = -1;
puts("libusb_init fail");
}
else
{
v0 = 0;
usb = libusb_open_device_with_vid_pid(0LL, 2996LL, 4095LL);
v1 = usb;
if ( usb )
goto LABEL_3;
printf("open device failed, trying to fel...");
fflush(stdout);
v3 = fel_ainol();
if ( v3 )
{
v0 = -1;
fwrite("\nfailed to open A10 USB FEL device\n", 1uLL, 0x23uLL, stderr);
return v0;
}
if ( !usb )
{
do
{
++v3;
putchar(46);
fflush(stdout);
usleep(0x7A120u);
v4 = libusb_open_device_with_vid_pid(0LL, 2996LL, 4095LL);
usb = v4;
}
while ( v3 != 10 && !v4 );
}
putchar(10);
v1 = usb;
if ( usb )
{
LABEL_3:
libusb_claim_interface(v1, 0LL);
}
else
{
v0 = -1;
puts("open device failed");
}
}
return v0;
}

original_code:
int usbcomm_init(void)
{
    int i;
    if( libusb_init(((void *)0)) != 0 ) {
        printf("libusb_init fail\n");
        return -1;
    }
    if( (usb = libusb_open_device_with_vid_pid(((void *)0), 0xbb4, 0xfff)) == ((void *)0) )
    {
        printf("open device failed, trying to fel...");
        fflush(stdout);
        if( fel_ainol() != 0 ) {
            fprintf(stderr, "\nfailed to open A10 USB FEL device\n");
            return -1;
        }
        for(i = 0; usb == ((void *)0) && i < 10; ++i) {
            printf(".");
            fflush(stdout);
            usleep(500000);
            usb = libusb_open_device_with_vid_pid(((void *)0), 0xbb4, 0xfff);
        }
        printf("\n");
        if( usb == ((void *)0) ) {
            printf("open device failed\n");
            return -1;
        }
    }
    libusb_claim_interface(usb, 0);
    return 0;
}

canonical_original_code:
int usbcomm_init()
{
    int i;
    if( libusb_init(((void *)0)) != 0 ) {
        printf("libusb_init fail\n");
        return -1;
    }
    if( (usb = libusb_open_device_with_vid_pid(((void *)0), 0xbb4, 0xfff)) == ((void *)0) )
    {
        printf("open device failed, trying to fel...");
        fflush(stdout);
        if( fel_ainol() != 0 ) {
            fprintf(stderr, "\nfailed to open A10 USB FEL device\n");
            return -1;
        }
        for(i = 0; usb == ((void *)0) && i < 10; ++i) {
            printf(".");
            fflush(stdout);
            usleep(500000);
            usb = libusb_open_device_with_vid_pid(((void *)0), 0xbb4, 0xfff);
        }
        printf("\n");
        if( usb == ((void *)0) ) {
            printf("open device failed\n");
            return -1;
        }
    }
    libusb_claim_interface(usb, 0);
    return 0;
}


decompiled_code:
__int64 usbcomm_exit()
{
libusb_release_interface(usb, 0LL);
libusb_close(usb);
return libusb_exit(0LL);
}

canonical_decompiled_code:
__int64 func16()
{
libusb_release_interface(usb, 0LL);
libusb_close(usb);
return libusb_exit(0LL);
}

original_code:
void usbcomm_exit(void)
{
    libusb_release_interface(usb, 0);
    libusb_close(usb);
    libusb_exit(((void *)0));
}

canonical_original_code:
void usbcomm_exit()
{
    libusb_release_interface(usb, 0);
    libusb_close(usb);
    libusb_exit(((void *)0));
}


decompiled_code:
void __fastcall extract_img(const char *a1, const char *a2, int a3, unsigned int a4, unsigned int a5)
{
size_t v5; // rbp
size_t v7; // rdx
int v8; // ebx
int *v9; // rax
char *v10; // rax
int v11; // er13
unsigned int v12; // edx
int *v13; // rax
char *v14; // rax
int *v15; // rax
char *v16; // rax
int *v17; // rax
char *v18; // rax
char v19[1024]; // [rsp+0h] [rbp-8428h] BYREF
char buf[32808]; // [rsp+400h] [rbp-8028h] BYREF

v5 = a5;
if ( a5 )
{
if ( (int)lseek(a3, a4, 0) < 0 )
{
v13 = __errno_location();
v14 = strerror(*v13);
fprintf(stderr, "lseek: %s\n", v14);
}
else
{
v7 = 0x8000LL;
if ( (unsigned int)v5 <= 0x8000 )
v7 = v5;
v8 = read(a3, buf, v7);
if ( v8 > 0 )
{
snprintf(v19, 0x400uLL, "%s_%s", a1, a2);
printf("extracting %s into %s\n", a1, v19);
v11 = open(v19, 65, 436LL);
if ( v11 < 0 )
{
v17 = __errno_location();
v18 = strerror(*v17);
fprintf(stderr, "unable to create %s: %s\n", v19, v18);
}
else
{
while ( (unsigned int)write(v11, buf, (unsigned int)v8) == v8 )
{
LODWORD(v5) = v5 - v8;
if ( !(_DWORD)v5 )
{
v8 = 0;
LABEL_15:
close(v11);
goto LABEL_7;
}
v12 = 0x8000;
if ( (unsigned int)v5 <= 0x8000 )
v12 = v5;
v8 = read(a3, buf, v12);
if ( v8 <= 0 )
goto LABEL_15;
}
v15 = __errno_location();
v16 = strerror(*v15);
fprintf(stderr, "write: %s\n", v16);
close(v11);
}
}
else
{
LABEL_7:
if ( v8 )
{
v9 = __errno_location();
v10 = strerror(*v9);
fprintf(stderr, "read: %s\n", v10);
}
}
}
}
}

canonical_decompiled_code:
void __fastcall func7(const char *a1, const char *a2, int a3, unsigned int a4, unsigned int a5)
{
size_t v5; // rbp
size_t v7; // rdx
int v8; // ebx
int *v9; // rax
char *v10; // rax
int v11; // er13
unsigned int v12; // edx
int *v13; // rax
char *v14; // rax
int *v15; // rax
char *v16; // rax
int *v17; // rax
char *v18; // rax
char v19[1024]; // [rsp+0h] [rbp-8428h] BYREF
char buf[32808]; // [rsp+400h] [rbp-8028h] BYREF

v5 = a5;
if ( a5 )
{
if ( (int)lseek(a3, a4, 0) < 0 )
{
v13 = __errno_location();
v14 = strerror(*v13);
fprintf(stderr, "lseek: %s\n", v14);
}
else
{
v7 = 0x8000LL;
if ( (unsigned int)v5 <= 0x8000 )
v7 = v5;
v8 = read(a3, buf, v7);
if ( v8 > 0 )
{
snprintf(v19, 0x400uLL, "%s_%s", a1, a2);
printf("extracting %s into %s\n", a1, v19);
v11 = open(v19, 65, 436LL);
if ( v11 < 0 )
{
v17 = __errno_location();
v18 = strerror(*v17);
fprintf(stderr, "unable to create %s: %s\n", v19, v18);
}
else
{
while ( (unsigned int)write(v11, buf, (unsigned int)v8) == v8 )
{
LODWORD(v5) = v5 - v8;
if ( !(_DWORD)v5 )
{
v8 = 0;
LABEL_15:
close(v11);
goto LABEL_7;
}
v12 = 0x8000;
if ( (unsigned int)v5 <= 0x8000 )
v12 = v5;
v8 = read(a3, buf, v12);
if ( v8 <= 0 )
goto LABEL_15;
}
v15 = __errno_location();
v16 = strerror(*v15);
fprintf(stderr, "write: %s\n", v16);
close(v11);
}
}
else
{
LABEL_7:
if ( v8 )
{
v9 = __errno_location();
v10 = strerror(*v9);
fprintf(stderr, "read: %s\n", v10);
}
}
}
}
}

original_code:
static void extract_img(const char *image_name, const char *fname,
        int fd, unsigned offset, unsigned size)
{
    char out_fname[1024], buf[32768];
    int fdOut, rd;
    if( size == 0 )
        return;
    if( lseek(fd, offset, 0) < 0 ) {
        fprintf(stderr, "lseek: %s\n", strerror((*__errno_location ())));
        return;
    }
    rd = read(fd, buf, sizeof(buf) > size ? size : sizeof(buf));
    if( rd > 0 ) {
        snprintf(out_fname, sizeof(out_fname), "%s_%s", image_name, fname);
        printf("extracting %s into %s\n", image_name, out_fname);
        if( (fdOut = open(out_fname, 01 | 0100, 0664)) < 0 ) {
            fprintf(stderr, "unable to create %s: %s\n", out_fname,
                    strerror((*__errno_location ())));
            return;
        }
        while( rd > 0 ) {
            if( write(fdOut, buf, rd) != rd ) {
                fprintf(stderr, "write: %s\n", strerror((*__errno_location ())));
                close(fdOut);
                return;
            }
            size -= rd;
            if( size > 0 ) {
                rd = read(fd, buf, sizeof(buf) > size ? size : sizeof(buf));
            }else{
                rd = 0;
            }
        }
        close(fdOut);
    }
    if( rd < 0 ) {
        fprintf(stderr, "read: %s\n", strerror((*__errno_location ())));
    }
}

canonical_original_code:
static void extract_img(char *image_name, char *fname, int fd, unsigned int offset, unsigned int size)
{
    char out_fname[1024]; char buf[32768];
    int fdOut; int rd;
    if( size == 0 )
        return;
    if( lseek(fd, offset, 0) < 0 ) {
        fprintf(stderr, "lseek: %s\n", strerror((*__errno_location ())));
        return;
    }
    rd = read(fd, buf, sizeof(buf) > size ? size : sizeof(buf));
    if( rd > 0 ) {
        snprintf(out_fname, sizeof(out_fname), "%s_%s", image_name, fname);
        printf("extracting %s into %s\n", image_name, out_fname);
        if( (fdOut = open(out_fname, 01 | 0100, 0664)) < 0 ) {
            fprintf(stderr, "unable to create %s: %s\n", out_fname,
                    strerror((*__errno_location ())));
            return;
        }
        while( rd > 0 ) {
            if( write(fdOut, buf, rd) != rd ) {
                fprintf(stderr, "write: %s\n", strerror((*__errno_location ())));
                close(fdOut);
                return;
            }
            size -= rd;
            if( size > 0 ) {
                rd = read(fd, buf, sizeof(buf) > size ? size : sizeof(buf));
            }else{
                rd = 0;
            }
        }
        close(fdOut);
    }
    if( rd < 0 ) {
        fprintf(stderr, "read: %s\n", strerror((*__errno_location ())));
    }
}

variable_types:
fname
char *

variable_types: fd int

variable_types:
offset
unsigned int

variable_types:
size
unsigned int

variable_types:
buf
char [32768]

variable_types: fdOut int

variable_types: rd int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r13
int v4; // er12
int v5; // er14
int v6; // eax
int *v8; // rax
char *v9; // rax
int *v10; // rax
char *v11; // rax
__int64 v12[79]; // [rsp+0h] [rbp-278h] BYREF

if ( argc == 1 )
{
v4 = 0;
printf("usage: %s [-e] <image file>\n", *argv);
puts(" -e - extract kernel and initrd");
return v4;
}
v3 = argv[1];
v4 = *(unsigned __int8 *)v3 - 45;
if ( *v3 == 45 )
{
v4 = *((unsigned __int8 *)v3 + 1) - 101;
if ( v3[1] == 101 )
{
v4 = *((unsigned __int8 *)v3 + 2);
if ( !v3[2] )
{
if ( argc <= 2 )
{
v4 = 1;
puts("file name?");
return v4;
}
v3 = argv[2];
}
}
}
v5 = open(v3, 0, envp);
if ( v5 < 0 )
{
v8 = __errno_location();
v4 = 1;
v9 = strerror(*v8);
fprintf(stderr, "failed to open %s: %s\n", v3, v9);
}
else
{
memset(v12, 0, 0x260uLL);
v6 = read(v5, v12, 0x260uLL);
if ( v6 < 0 )
{
v10 = __errno_location();
v4 = 1;
v11 = strerror(*v10);
fprintf(stderr, "fatal: read failed: %s\n", v11);
}
else
{
if ( v6 <= 607 )
printf("warn: header is truncated, file size=%d, header size=%d\n", (unsigned int)v6, 608LL);
if ( v12[0] != 0x2144494F52444E41LL )
puts("warn: file doest not seem to be a fastboot image");
printf(" kernel size (bytes): %u\n", LODWORD(v12[1]));
printf(" kernel addr: 0x%x\n", HIDWORD(v12[1]));
printf(" ramdisk size (bytes): %u\n", LODWORD(v12[2]));
printf(" ramdisk addr: 0x%x\n", HIDWORD(v12[2]));
printf(" second size: %u\n", LODWORD(v12[3]));
printf(" second addr: 0x%x\n", HIDWORD(v12[3]));
printf(" tags addr: 0x%x\n", LODWORD(v12[4]));
printf(" page size: %u\n", HIDWORD(v12[4]));
printf(" unused1: %u\n", LODWORD(v12[5]));
printf(" unused2: %u\n", HIDWORD(v12[5]));
printf(" product name: %.*s\n", 16, (const char *)&v12[6]);
printf(" cmdline: %.*s\n", 512, (const char *)&v12[8]);
putchar(10);
if ( v4 )
{
v4 = 0;
}
else if ( HIDWORD(v12[4]) )
{
extract_img("bImage", v3, (unsigned int)v5, HIDWORD(v12[4]), LODWORD(v12[1]));
extract_img(
"initrd",
v3,
(unsigned int)v5,
(unsigned int)(LODWORD(v12[1]) + 2 * HIDWORD(v12[4]) - LODWORD(v12[1]) % HIDWORD(v12[4])),
LODWORD(v12[2]));
puts("done.\n");
}
}
}
return v4;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r13
int v4; // er12
int v5; // er14
int v6; // eax
int *v8; // rax
char *v9; // rax
int *v10; // rax
char *v11; // rax
__int64 v12[79]; // [rsp+0h] [rbp-278h] BYREF

if ( argc == 1 )
{
v4 = 0;
printf("usage: %s [-e] <image file>\n", *argv);
puts(" -e - extract kernel and initrd");
return v4;
}
v3 = argv[1];
v4 = *(unsigned __int8 *)v3 - 45;
if ( *v3 == 45 )
{
v4 = *((unsigned __int8 *)v3 + 1) - 101;
if ( v3[1] == 101 )
{
v4 = *((unsigned __int8 *)v3 + 2);
if ( !v3[2] )
{
if ( argc <= 2 )
{
v4 = 1;
puts("file name?");
return v4;
}
v3 = argv[2];
}
}
}
v5 = open(v3, 0, envp);
if ( v5 < 0 )
{
v8 = __errno_location();
v4 = 1;
v9 = strerror(*v8);
fprintf(stderr, "failed to open %s: %s\n", v3, v9);
}
else
{
memset(v12, 0, 0x260uLL);
v6 = read(v5, v12, 0x260uLL);
if ( v6 < 0 )
{
v10 = __errno_location();
v4 = 1;
v11 = strerror(*v10);
fprintf(stderr, "fatal: read failed: %s\n", v11);
}
else
{
if ( v6 <= 607 )
printf("warn: header is truncated, file size=%d, header size=%d\n", (unsigned int)v6, 608LL);
if ( v12[0] != 0x2144494F52444E41LL )
puts("warn: file doest not seem to be a fastboot image");
printf(" kernel size (bytes): %u\n", LODWORD(v12[1]));
printf(" kernel addr: 0x%x\n", HIDWORD(v12[1]));
printf(" ramdisk size (bytes): %u\n", LODWORD(v12[2]));
printf(" ramdisk addr: 0x%x\n", HIDWORD(v12[2]));
printf(" second size: %u\n", LODWORD(v12[3]));
printf(" second addr: 0x%x\n", HIDWORD(v12[3]));
printf(" tags addr: 0x%x\n", LODWORD(v12[4]));
printf(" page size: %u\n", HIDWORD(v12[4]));
printf(" unused1: %u\n", LODWORD(v12[5]));
printf(" unused2: %u\n", HIDWORD(v12[5]));
printf(" product name: %.*s\n", 16, (const char *)&v12[6]);
printf(" cmdline: %.*s\n", 512, (const char *)&v12[8]);
putchar(10);
if ( v4 )
{
v4 = 0;
}
else if ( HIDWORD(v12[4]) )
{
func7("bImage", v3, (unsigned int)v5, HIDWORD(v12[4]), LODWORD(v12[1]));
func7(
"initrd",
v3,
(unsigned int)v5,
(unsigned int)(LODWORD(v12[1]) + 2 * HIDWORD(v12[4]) - LODWORD(v12[1]) % HIDWORD(v12[4])),
LODWORD(v12[2]));
puts("done.\n");
}
}
}
return v4;
}

original_code:
int main(int argc, char *argv[])
{
    int fd, rd;
    struct fastboot_boot_img_hdr hdr;
    int extract;
    const char *fname;
    if( argc == 1 ) {
        printf("usage: %s [-e] <image file>\n", argv[0]);
        printf("    -e      - extract kernel and initrd\n");
        return 0;
    }
    fname = argv[1];
    if( extract = !strcmp(argv[1], "-e") ) {
        if( argc < 3 ) {
            printf("file name?\n");
            return 1;
        }
        fname = argv[2];
    }
    if( (fd = open(fname, 00)) < 0 ) {
        fprintf(stderr, "failed to open %s: %s\n", fname, strerror((*__errno_location ())));
        return 1;
    }
    memset(&hdr, 0, sizeof(hdr));
    if( (rd = read(fd, &hdr, sizeof(hdr))) < 0 ) {
        fprintf(stderr, "fatal: read failed: %s\n", strerror((*__errno_location ())));
        return 1;
    }
    if( rd < sizeof(hdr) ) {
        printf("warn: header is truncated, file size=%d, header size=%d\n",
                rd, sizeof(hdr));
    }
    if( memcmp(hdr.magic, "ANDROID!", 8) ) {
        printf("warn: file doest not seem to be a fastboot image\n");
    }
 printf("    kernel size (bytes):  %u\n", hdr.kernel_size);
 printf("    kernel addr:          0x%x\n", hdr.kernel_addr);
 printf("    ramdisk size (bytes): %u\n", hdr.ramdisk_size);
 printf("    ramdisk addr:         0x%x\n", hdr.ramdisk_addr);
 printf("    second size:          %u\n", hdr.second_size);
 printf("    second addr:          0x%x\n", hdr.second_addr);
 printf("    tags addr:            0x%x\n", hdr.tags_addr);
 printf("    page size:            %u\n", hdr.page_size);
 printf("    unused1:              %u\n", hdr.unused[0]);
 printf("    unused2:              %u\n", hdr.unused[1]);
 printf("    product name:         %.*s\n", 16, hdr.name);
 printf("    cmdline:              %.*s\n", 512,
            hdr.cmdline);
    printf("\n");
    if( extract && hdr.page_size > 0 ) {
        extract_img("bImage", fname, fd, hdr.page_size, hdr.kernel_size);
        extract_img("initrd", fname, fd, 2 * hdr.page_size + hdr.kernel_size -
                hdr.kernel_size % hdr.page_size, hdr.ramdisk_size);
        printf("done.\n\n");
    }
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    int fd; int rd;
    struct fastboot_boot_img_hdr hdr;
    int extract;
    char *fname;
    if( argc == 1 ) {
        printf("usage: %s [-e] <image file>\n", argv[0]);
        printf("    -e      - extract kernel and initrd\n");
        return 0;
    }
    fname = argv[1];
    if( extract = !strcmp(argv[1], "-e") ) {
        if( argc < 3 ) {
            printf("file name?\n");
            return 1;
        }
        fname = argv[2];
    }
    if( (fd = open(fname, 00)) < 0 ) {
        fprintf(stderr, "failed to open %s: %s\n", fname, strerror((*__errno_location ())));
        return 1;
    }
    memset(&hdr, 0, sizeof(hdr));
    if( (rd = read(fd, &hdr, sizeof(hdr))) < 0 ) {
        fprintf(stderr, "fatal: read failed: %s\n", strerror((*__errno_location ())));
        return 1;
    }
    if( rd < sizeof(hdr) ) {
        printf("warn: header is truncated, file size=%d, header size=%d\n",
                rd, sizeof(hdr));
    }
    if( memcmp(hdr.magic, "ANDROID!", 8) ) {
        printf("warn: file doest not seem to be a fastboot image\n");
    }
 printf("    kernel size (bytes):  %u\n", hdr.kernel_size);
 printf("    kernel addr:          0x%x\n", hdr.kernel_addr);
 printf("    ramdisk size (bytes): %u\n", hdr.ramdisk_size);
 printf("    ramdisk addr:         0x%x\n", hdr.ramdisk_addr);
 printf("    second size:          %u\n", hdr.second_size);
 printf("    second addr:          0x%x\n", hdr.second_addr);
 printf("    tags addr:            0x%x\n", hdr.tags_addr);
 printf("    page size:            %u\n", hdr.page_size);
 printf("    unused1:              %u\n", hdr.unused[0]);
 printf("    unused2:              %u\n", hdr.unused[1]);
 printf("    product name:         %.*s\n", 16, hdr.name);
 printf("    cmdline:              %.*s\n", 512,
            hdr.cmdline);
    printf("\n");
    if( extract && hdr.page_size > 0 ) {
        extract_img("bImage", fname, fd, hdr.page_size, hdr.kernel_size);
        extract_img("initrd", fname, fd, 2 * hdr.page_size + hdr.kernel_size -
                hdr.kernel_size % hdr.page_size, hdr.ramdisk_size);
        printf("done.\n\n");
    }
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct fastboot_boot_img_hdr { unsigned char magic[8]; unsigned int kernel_size; unsigned int kernel_addr; unsigned int ramdisk_size; unsigned int ramdisk_addr; unsigned int second_size; unsigned int second_addr; unsigned int tags_addr; unsigned int page_size; unsigned int unused[2]; unsigned char name[16]; unsigned char cmdline[512]; unsigned int id[8]; }

variable_types: extract int


decompiled_code:
int __fastcall disp_getbrightness(int a1)
{
__int64 v2[5]; // [rsp+0h] [rbp-28h] BYREF

v2[0] = 0LL;
return ioctl(a1, 0x143uLL, v2);
}

canonical_decompiled_code:
int __fastcall func7(int a1)
{
__int64 v2[5]; // [rsp+0h] [rbp-28h] BYREF

v2[0] = 0LL;
return ioctl(a1, 0x143uLL, v2);
}

original_code:
static int disp_getbrightness(int disp_fd)
{
    unsigned long arg[4];
    arg[0] = 0;
    return ioctl(disp_fd, DISP_CMD_LCD_GET_BRIGHTNESS, (unsigned long)arg);
}

variable_types:
arg
unsigned long [4]


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rax
int v4; // eax
unsigned int v5; // ebp
unsigned int v6; // er13
int v7; // eax
char v8; // dl
int v9; // esi
int v10; // er12
int v11; // eax
unsigned int v13; // eax

if ( argc <= 1 )
{
v11 = open("/dev/disp", 2, envp);
v5 = v11;
if ( v11 >= 0 )
{
disp_announceversion_isra_0((unsigned int)v11);
v13 = disp_getbrightness(v5);
v6 = v13;
if ( argc == 1 )
{
v10 = 0;
printf("brightness: %d\n", v13);
return v10;
}
goto LABEL_5;
}
LABEL_17:
v10 = 1;
puts("/dev/disp open fail");
return v10;
}
v3 = argv[1];
if ( *(_WORD *)v3 == 26669 )
{
v10 = *((unsigned __int8 *)v3 + 2);
if ( !v3[2] )
{
puts("usage: sunxi-disp-brightness [[+|-]<number>]");
return v10;
}
}
v4 = open("/dev/disp", 2, envp);
v5 = v4;
if ( v4 < 0 )
goto LABEL_17;
disp_announceversion_isra_0((unsigned int)v4);
v6 = disp_getbrightness(v5);
LABEL_5:
v7 = strtoul(&argv[1][((*argv[1] - 43) & 0xFD) == 0], 0LL, 10);
v8 = *argv[1];
if ( v8 == 43 )
{
v7 += v6;
}
else if ( v8 == 45 )
{
v7 = v6 - v7;
}
if ( v7 > 255 )
v7 = 255;
v9 = 0;
if ( v7 >= 0 )
v9 = v7;
v10 = 0;
if ( v9 != v6 )
disp_setbrightness_isra_0(v5);
return v10;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rax
int v4; // eax
unsigned int v5; // ebp
unsigned int v6; // er13
int v7; // eax
char v8; // dl
int v9; // esi
int v10; // er12
int v11; // eax
unsigned int v13; // eax

if ( argc <= 1 )
{
v11 = open("/dev/disp", 2, envp);
v5 = v11;
if ( v11 >= 0 )
{
func9((unsigned int)v11);
v13 = func7(v5);
v6 = v13;
if ( argc == 1 )
{
v10 = 0;
printf("brightness: %d\n", v13);
return v10;
}
goto LABEL_5;
}
LABEL_17:
v10 = 1;
puts("/dev/disp open fail");
return v10;
}
v3 = argv[1];
if ( *(_WORD *)v3 == 26669 )
{
v10 = *((unsigned __int8 *)v3 + 2);
if ( !v3[2] )
{
puts("usage: sunxi-disp-brightness [[+|-]<number>]");
return v10;
}
}
v4 = open("/dev/disp", 2, envp);
v5 = v4;
if ( v4 < 0 )
goto LABEL_17;
func9((unsigned int)v4);
v6 = func7(v5);
LABEL_5:
v7 = strtoul(&argv[1][((*argv[1] - 43) & 0xFD) == 0], 0LL, 10);
v8 = *argv[1];
if ( v8 == 43 )
{
v7 += v6;
}
else if ( v8 == 45 )
{
v7 = v6 - v7;
}
if ( v7 > 255 )
v7 = 255;
v9 = 0;
if ( v7 >= 0 )
v9 = v7;
v10 = 0;
if ( v9 != v6 )
func8(v5);
return v10;
}

original_code:
int main(int argc, char *argv[])
{
    int disp_fd, cur_brightness, new_brightness;
    char *arg;
    if( argc > 1 && !strcmp(argv[1], "-h") ) {
        printf("usage: sunxi-disp-brightness [[+|-]<number>]\n");
        return 0;
    }
    if( (disp_fd = open("/dev/disp", 02)) < 0 ) {
        printf("/dev/disp open fail\n");
        return 1;
    }
    disp_announceversion(disp_fd);
    cur_brightness = disp_getbrightness(disp_fd);
    if(argc == 1 ) {
        printf("brightness: %d\n", cur_brightness);
    }else{
        arg = argv[1];
        if( *arg == '+' || *arg == '-' )
            ++arg;
        new_brightness = strtoul(arg, ((void *)0), 10);
        if( *argv[1] == '+' )
            new_brightness += cur_brightness;
        else if( *argv[1] == '-' )
            new_brightness = cur_brightness - new_brightness;
        if( new_brightness < 0 )
            new_brightness = 0;
        if( new_brightness > 255 )
            new_brightness = 255;
        if( new_brightness != cur_brightness )
            disp_setbrightness(disp_fd, new_brightness);
    }
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    int disp_fd; int cur_brightness; int new_brightness;
    char *arg;
    if( argc > 1 && !strcmp(argv[1], "-h") ) {
        printf("usage: sunxi-disp-brightness [[+|-]<number>]\n");
        return 0;
    }
    if( (disp_fd = open("/dev/disp", 02)) < 0 ) {
        printf("/dev/disp open fail\n");
        return 1;
    }
    disp_announceversion(disp_fd);
    cur_brightness = disp_getbrightness(disp_fd);
    if(argc == 1 ) {
        printf("brightness: %d\n", cur_brightness);
    }else{
        arg = argv[1];
        if( *arg == '+' || *arg == '-' )
            ++arg;
        new_brightness = strtoul(arg, ((void *)0), 10);
        if( *argv[1] == '+' )
            new_brightness += cur_brightness;
        else if( *argv[1] == '-' )
            new_brightness = cur_brightness - new_brightness;
        if( new_brightness < 0 )
            new_brightness = 0;
        if( new_brightness > 255 )
            new_brightness = 255;
        if( new_brightness != cur_brightness )
            disp_setbrightness(disp_fd, new_brightness);
    }
    return 0;
}

variable_types:
arg
char *


ThreadFun

decompiled_code:
void *__fastcall ThreadFun(void *a1)
{
int v2; // [rsp+Ch] [rbp-3FCh] BYREF
char v3[1016]; // [rsp+10h] [rbp-3F8h] BYREF

while ( !(unsigned int)libusb_bulk_transfer(usb, 129LL, v3, 1000LL, &v2, 0LL) )
printf("response: %.*s\n", v2, v3);
if ( !do_finish )
puts("read error");
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func0(void *a1)
{
int v2; // [rsp+Ch] [rbp-3FCh] BYREF
char v3[1016]; // [rsp+10h] [rbp-3F8h] BYREF

while ( !(unsigned int)libusb_bulk_transfer(usb, 129LL, v3, 1000LL, &v2, 0LL) )
printf("response: %.*s\n", v2, v3);
if ( !do_finish )
puts("read error");
return 0LL;
}

original_code:
static void *ThreadFun(void *par)
{
    char buf[1000];
    int transferred;
    while( libusb_bulk_transfer(usb, 1 | LIBUSB_ENDPOINT_IN, buf,
                sizeof(buf), &transferred, 0) == 0)
    {
        printf("response: %.*s\n", transferred, buf);
    }
    if( ! do_finish )
        printf("read error\n");
    return ((void *)0);
}

variable_types:
par
void *

variable_types:
buf
char [1000]

variable_types: transferred int

return_type: void *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12
size_t v4; // rax
char v6[4]; // [rsp+4h] [rbp-404h] BYREF
pthread_t newthread; // [rsp+8h] [rbp-400h] BYREF
char s[1016]; // [rsp+10h] [rbp-3F8h] BYREF

if ( (unsigned int)libusb_init(0LL, argv, envp) )
{
puts("libusb_init fail");
return 1;
}
v3 = 0;
usb = libusb_open_device_with_vid_pid(0LL, 2996LL, 4095LL);
if ( !usb )
{
puts("open device failed");
return 1;
}
libusb_claim_interface(usb, 0LL);
pthread_create(&newthread, 0LL, ThreadFun, 0LL);
while ( (unsigned int)gets(s) )
{
v4 = strlen(s);
if ( (unsigned int)libusb_bulk_transfer(usb, 2LL, s, v4, v6, 0LL) )
{
puts("write error");
break;
}
}
do_finish = 1;
libusb_release_interface(usb, 0LL);
libusb_close(usb);
libusb_exit(0LL);
return v3;
}

original_code:
int main(int argc, char *argv[])
{
    int transferred;
    char buf[1000];
    pthread_t thr;
    if( libusb_init(((void *)0)) != 0 ) {
        printf("libusb_init fail\n");
        return 1;
    }
    if( (usb = libusb_open_device_with_vid_pid(((void *)0), 0xbb4, 0xfff)) == ((void *)0) ) {
        printf("open device failed\n");
        return 1;
    }
    libusb_claim_interface(usb, 0);
    pthread_create(&thr, ((void *)0), ThreadFun, ((void *)0));
    while( gets(buf) != ((void *)0) ) {
        if( libusb_bulk_transfer(usb, 2 | LIBUSB_ENDPOINT_OUT,
                    buf, strlen(buf), &transferred, 0) != 0 )
        {
            printf("write error\n");
            break;
        }
    }
    do_finish = 1;
    libusb_release_interface(usb, 0);
    libusb_close(usb);
    libusb_exit(((void *)0));
}

canonical_original_code:
int main(int argc, char *argv[])
{
    int transferred;
    char buf[1000];
    unsigned long thr;
    if( libusb_init(((void *)0)) != 0 ) {
        printf("libusb_init fail\n");
        return 1;
    }
    if( (usb = libusb_open_device_with_vid_pid(((void *)0), 0xbb4, 0xfff)) == ((void *)0) ) {
        printf("open device failed\n");
        return 1;
    }
    libusb_claim_interface(usb, 0);
    pthread_create(&thr, ((void *)0), ThreadFun, ((void *)0));
    while( gets(buf) != ((void *)0) ) {
        if( libusb_bulk_transfer(usb, 2 | LIBUSB_ENDPOINT_OUT,
                    buf, strlen(buf), &transferred, 0) != 0 )
        {
            printf("write error\n");
            break;
        }
    }
    do_finish = 1;
    libusb_release_interface(usb, 0);
    libusb_close(usb);
    libusb_exit(((void *)0));
}

variable_types:
thr
unsigned long


decompiled_code:
__int64 __fastcall round_up(unsigned int a1, unsigned int a2)
{
unsigned int v2; // er8
int v3; // edx
int v4; // esi

v2 = a1;
v3 = a1 % a2;
v4 = a1 + a2 - a1 % a2;
if ( v3 )
v2 = v4;
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1, unsigned int a2)
{
unsigned int v2; // er8
int v3; // edx
int v4; // esi

v2 = a1;
v3 = a1 % a2;
v4 = a1 + a2 - a1 % a2;
if ( v3 )
v2 = v4;
return v2;
}

original_code:
static unsigned round_up(unsigned val, unsigned round)
{
    if( val % round )
        val += round - val % round;
    return val;
}

canonical_original_code:
static unsigned int round_up(unsigned int val, unsigned int round)
{
    if( val % round )
        val += round - val % round;
    return val;
}

variable_types:
val
unsigned int

variable_types:
round
unsigned int

return_type: unsigned int


setBootImgChecksum

decompiled_code:
__int64 __fastcall setBootImgChecksum(__int64 a1, int a2)
{
unsigned int v2; // eax
unsigned int *v3; // rcx
unsigned int *v4; // rdx
int v5; // eax
int v6; // er8
unsigned int v7; // er8
__int64 v8; // rcx
__int64 result; // rax
__int64 v10; // rcx
__int64 v11; // rcx

v2 = _uint32_identity(0x5F0A6C39u);
v3[3] = v2;
if ( a2 <= 0LL )
{
result = _uint32_identity(0);
*(_DWORD *)(v11 + 12) = result;
}
else
{
v4 = v3;
do
{
v5 = _uint32_identity(*v4);
v7 = v5 + v6;
}
while ( (__int64)v4 - v8 < a2 );
result = _uint32_identity(v7);
*(_DWORD *)(v10 + 12) = result;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2)
{
unsigned int v2; // eax
unsigned int *v3; // rcx
unsigned int *v4; // rdx
int v5; // eax
int v6; // er8
unsigned int v7; // er8
__int64 v8; // rcx
__int64 result; // rax
__int64 v10; // rcx
__int64 v11; // rcx

v2 = func0(0x5F0A6C39u);
v3[3] = v2;
if ( a2 <= 0LL )
{
result = func0(0);
*(_DWORD *)(v11 + 12) = result;
}
else
{
v4 = v3;
do
{
v5 = func0(*v4);
v7 = v5 + v6;
}
while ( (__int64)v4 - v8 < a2 );
result = func0(v7);
*(_DWORD *)(v10 + 12) = result;
}
return result;
}

original_code:
static void setBootImgChecksum(char *image, int imageSize)
{
    uint32_t val = __uint32_identity (0x5F0A6C39);
    unsigned checksum = 0;
    char *addr = image;
    memcpy(image+12, &val, 4);
    while(addr - image < imageSize) {
        memcpy(&val, addr, 4);
        checksum += __uint32_identity (val);
        addr += 4;
    }
    val = __uint32_identity (checksum);
    memcpy(image+12, &val, 4);
}

canonical_original_code:
static void setBootImgChecksum(char *image, int imageSize)
{
    unsigned int val = __uint32_identity (0x5F0A6C39);
    unsigned int checksum = 0;
    char *addr = image;
    memcpy(image+12, &val, 4);
    while(addr - image < imageSize) {
        memcpy(&val, addr, 4);
        checksum += __uint32_identity (val);
        addr += 4;
    }
    val = __uint32_identity (checksum);
    memcpy(image+12, &val, 4);
}

variable_types:
image
char *

variable_types: imageSize int

variable_types:
checksum
unsigned int

variable_types:
addr
char *


decompiled_code:
void __fastcall egon0_embed(void *src, size_t n)
{
int *v2; // rax
int v3; // ebx
_DWORD *v4; // r15
size_t v5; // r14
unsigned int v6; // eax
size_t v7; // r13
unsigned int v8; // ebx
unsigned int v9; // er12
unsigned int v10; // ebp
int v11; // er14
char *v12; // rdi
unsigned int size; // [rsp+8h] [rbp-40h]

v2 = (int *)get_flashmem_props();
v3 = *v2;
size = (*v2 * v2[2]) << 9;
if ( size < (unsigned int)n )
{
puts("error: boot0 image file too big");
}
else
{
v4 = malloc(size);
memcpy(v4, src, (unsigned int)n);
v5 = (unsigned int)round_up(n, 0x400u);
memset((char *)v4 + (unsigned int)n, -1, v5);
v4[4] = _uint32_identity(v5);
setBootImgChecksum((__int64)v4, v5);
v6 = round_up(v5, v3 << 15);
v7 = v6;
v8 = v6;
memset((char *)v4 + v5, -1, v6);
v9 = size / (unsigned int)v7;
if ( size / (unsigned int)v7 > 1 )
{
v10 = v7;
v11 = 1;
do
{
++v11;
v12 = (char *)v4 + v10;
v10 += v8;
memcpy(v12, v4, v7);
}
while ( v9 != v11 );
}
memset((char *)v4 + v9 * v8, -1, size % (unsigned int)v7);
write_disk(0LL, 0LL, size >> 9, v4);
free(v4);
}
}

canonical_decompiled_code:
void __fastcall func3(void *src, size_t n)
{
int *v2; // rax
int v3; // ebx
_DWORD *v4; // r15
size_t v5; // r14
unsigned int v6; // eax
size_t v7; // r13
unsigned int v8; // ebx
unsigned int v9; // er12
unsigned int v10; // ebp
int v11; // er14
char *v12; // rdi
unsigned int size; // [rsp+8h] [rbp-40h]

v2 = (int *)get_flashmem_props();
v3 = *v2;
size = (*v2 * v2[2]) << 9;
if ( size < (unsigned int)n )
{
puts("error: boot0 image file too big");
}
else
{
v4 = malloc(size);
memcpy(v4, src, (unsigned int)n);
v5 = (unsigned int)func1(n, 0x400u);
memset((char *)v4 + (unsigned int)n, -1, v5);
v4[4] = func0(v5);
func2((__int64)v4, v5);
v6 = func1(v5, v3 << 15);
v7 = v6;
v8 = v6;
memset((char *)v4 + v5, -1, v6);
v9 = size / (unsigned int)v7;
if ( size / (unsigned int)v7 > 1 )
{
v10 = v7;
v11 = 1;
do
{
++v11;
v12 = (char *)v4 + v10;
v10 += v8;
memcpy(v12, v4, v7);
}
while ( v9 != v11 );
}
memset((char *)v4 + v9 * v8, -1, size % (unsigned int)v7);
write_disk(0LL, 0LL, size >> 9, v4);
free(v4);
}
}

original_code:
static void egon0_embed(const char *image, unsigned imageSize)
{
    struct flashmem_properties *props = get_flashmem_props();
    unsigned pageSize = props->areas[FMAREA_BOOT0].sectors_per_page * 512;
    unsigned areaSize = props->areas[FMAREA_BOOT0].page_count * pageSize;
    unsigned i, repCount;
    char *buf;
    uint32_t imgSizeLe;
    if( imageSize > areaSize ) {
        printf("error: boot0 image file too big\n");
        return;
    }
    buf = malloc(areaSize);
    memcpy(buf, image, imageSize);
    memset(buf + imageSize, '\xff', round_up(imageSize, 1024));
    imageSize = round_up(imageSize, 1024);
    imgSizeLe = __uint32_identity (imageSize);
    memcpy(buf + 16, &imgSizeLe, 4);
    setBootImgChecksum(buf, imageSize);
    memset(buf + imageSize, '\xff', round_up(imageSize, 64 * pageSize));
    imageSize = round_up(imageSize, 64 * pageSize);
    repCount = areaSize / imageSize;
    for(i = 1; i < repCount; ++i) {
        memcpy(buf + i * imageSize, buf, imageSize);
    }
    memset(buf + repCount * imageSize, '\xff', areaSize - repCount * imageSize);
    write_disk(FMAREA_BOOT0, 0, areaSize / 512, buf);
    free(buf);
}

canonical_original_code:
static void egon0_embed(char *image, unsigned int imageSize)
{
    struct flashmem_properties *props = get_flashmem_props();
    unsigned int pageSize = props->areas[FMAREA_BOOT0].sectors_per_page * 512;
    unsigned int areaSize = props->areas[FMAREA_BOOT0].page_count * pageSize;
    unsigned int i; unsigned int repCount;
    char *buf;
    unsigned int imgSizeLe;
    if( imageSize > areaSize ) {
        printf("error: boot0 image file too big\n");
        return;
    }
    buf = malloc(areaSize);
    memcpy(buf, image, imageSize);
    memset(buf + imageSize, '\xff', round_up(imageSize, 1024));
    imageSize = round_up(imageSize, 1024);
    imgSizeLe = __uint32_identity (imageSize);
    memcpy(buf + 16, &imgSizeLe, 4);
    setBootImgChecksum(buf, imageSize);
    memset(buf + imageSize, '\xff', round_up(imageSize, 64 * pageSize));
    imageSize = round_up(imageSize, 64 * pageSize);
    repCount = areaSize / imageSize;
    for(i = 1; i < repCount; ++i) {
        memcpy(buf + i * imageSize, buf, imageSize);
    }
    memset(buf + repCount * imageSize, '\xff', areaSize - repCount * imageSize);
    write_disk(FMAREA_BOOT0, 0, areaSize / 512, buf);
    free(buf);
}

variable_types:
imageSize
unsigned int

variable_types: struct flashmem_properties { struct flashmarea_properties { unsigned int sectors_per_page; unsigned int first_pageno; unsigned int page_count; } areas[-1]; unsigned int sectors_per_page; unsigned int pages_per_block; unsigned int blocks_per_chip; unsigned int chip_cnt; unsigned int pagewithbadflag; unsigned int block_cnt_of_zone; } *

variable_types:
pageSize
unsigned int

variable_types:
areaSize
unsigned int

variable_types:
i
unsigned int

variable_types:
repCount
unsigned int

variable_types:
buf
char *

variable_types:
imgSizeLe
unsigned int


strstrip

decompiled_code:
const char *__fastcall strstrip(const char *a1)
{
size_t v1; // rax
const char *i; // rax

v1 = strlen(a1);
if ( v1 )
{
for ( i = &a1[v1 - 1]; a1 != i; --i )
{
if ( (unsigned __int8)(*i - 9) > 4u && *i != 32 )
break;
}
*((_BYTE *)i + 1) = 0;
}
return a1;
}

canonical_decompiled_code:
const char *__fastcall func1(const char *a1)
{
size_t v1; // rax
const char *i; // rax

v1 = strlen(a1);
if ( v1 )
{
for ( i = &a1[v1 - 1]; a1 != i; --i )
{
if ( (unsigned __int8)(*i - 9) > 4u && *i != 32 )
break;
}
*((_BYTE *)i + 1) = 0;
}
return a1;
}

canonical_original_code:
static char *strstrip(char *str)
{
 unsigned long len = strlen(str);
 if (len) {
  char *end = str + len - 1;
  while ((end != str) && (((*end)==' ')||((*end)=='\n')||((*end)=='\t')||((*end)=='\v')||((*end)=='\r')||((*end)=='\f')))
   end--;
  *(end + 1) = '\0';
 }
 return str;
}

variable_types:
str
char *

variable_types:
len
unsigned long

variable_types:
end
char *

return_type: char *