decompiled_code:
__int64 __fastcall rtu_fd_init(unsigned __int16 a1, __int64 a2)
{
unsigned int v2; // er12
__int64 v4; // [rsp+0h] [rbp-118h] BYREF

trace_printf("rtu_fd.c", 132LL, 16LL, "clean filtering database.");
clean_fd();
trace_printf("rtu_fd.c", 134LL, 16LL, "clean vlan database.");
clean_vd();
trace_printf("rtu_fd.c", 136LL, 16LL, "clean aging map.");
rtu_read_aging_bitmap(&v4);
trace_printf("rtu_fd.c", 138LL, 16LL, "set aging time [%d].", a2);
aging_time = a2;
v2 = pthread_mutex_init(&fd_mutex, 0LL);
if ( !v2 )
{
trace_printf("rtu_fd.c", 145LL, 16LL, "set hash poly.");
rtu_fd_set_hash_poly(a1);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func18(unsigned __int16 a1, __int64 a2)
{
unsigned int v2; // er12
__int64 v4; // [rsp+0h] [rbp-118h] BYREF

trace_printf("rtu_fd.c", 132LL, 16LL, "clean filtering database.");
func3();
trace_printf("rtu_fd.c", 134LL, 16LL, "clean vlan database.");
func4();
trace_printf("rtu_fd.c", 136LL, 16LL, "clean aging map.");
rtu_read_aging_bitmap(&v4);
trace_printf("rtu_fd.c", 138LL, 16LL, "set aging time [%d].", a2);
aging_time = a2;
v2 = pthread_mutex_init(&fd_mutex, 0LL);
if ( !v2 )
{
trace_printf("rtu_fd.c", 145LL, 16LL, "set hash poly.");
func17(a1);
}
return v2;
}

original_code:
int rtu_fd_init(uint16_t poly, unsigned long aging)
{
    uint32_t bitmap[2048 / 32];
    int err;
    trace_printf("rtu_fd.c", 132, 0x10, "clean filtering database.");;
    clean_fd();
    trace_printf("rtu_fd.c", 134, 0x10, "clean vlan database.");;
    clean_vd();
    trace_printf("rtu_fd.c", 136, 0x10, "clean aging map.");;
    rtu_read_aging_bitmap(bitmap);
    trace_printf("rtu_fd.c", 138, 0x10, "set aging time [%d].", aging);;
    aging_time = aging;
    err = pthread_mutex_init(&fd_mutex, ((void *)0));
    if (err)
        return err;
    trace_printf("rtu_fd.c", 145, 0x10, "set hash poly.");;
    rtu_fd_set_hash_poly(poly);
    return 0;
}

canonical_original_code:
int rtu_fd_init(unsigned short poly, unsigned long aging)
{
    unsigned int bitmap[-1];
    int err;
    trace_printf("rtu_fd.c", 132, 0x10, "clean filtering database.");;
    clean_fd();
    trace_printf("rtu_fd.c", 134, 0x10, "clean vlan database.");;
    clean_vd();
    trace_printf("rtu_fd.c", 136, 0x10, "clean aging map.");;
    rtu_read_aging_bitmap(bitmap);
    trace_printf("rtu_fd.c", 138, 0x10, "set aging time [%d].", aging);;
    aging_time = aging;
    err = pthread_mutex_init(&fd_mutex, ((void *)0));
    if (err)
        return err;
    trace_printf("rtu_fd.c", 145, 0x10, "set hash poly.");;
    rtu_fd_set_hash_poly(poly);
    return 0;
}

variable_types:
aging
unsigned long

variable_types: err int


repo: neub/wr-switch-sw

decompiled_code:
__int64 __fastcall rtu_fd_set_aging_time(__int64 a1)
{
if ( (unsigned __int64)(a1 - 10) > 0xF4236 )
return 4294967274LL;
aging_time = a1;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func19(__int64 a1)
{
if ( (unsigned __int64)(a1 - 10) > 0xF4236 )
return 4294967274LL;
aging_time = a1;
return 0LL;
}

original_code:
int rtu_fd_set_aging_time(unsigned long t)
{
    if ((t < 10) || (t > 1000000))
        return -22;
    aging_time = t;
    return 0;
}

variable_types:
t
unsigned long

return_type: int


decompiled_code:
int rtu_fd_flush()
{
rtu_fd_age_update();
pthread_mutex_lock(&fd_mutex);
rtu_fd_age_out();
return pthread_mutex_unlock(&fd_mutex);
}

canonical_decompiled_code:
int func20()
{
func8();
pthread_mutex_lock(&fd_mutex);
func15();
return pthread_mutex_unlock(&fd_mutex);
}

original_code:
void rtu_fd_flush(void)
{
    rtu_fd_age_update();
    pthread_mutex_lock(&fd_mutex);
    rtu_fd_age_out();
    pthread_mutex_unlock(&fd_mutex);
}

canonical_original_code:
void rtu_fd_flush()
{
    rtu_fd_age_update();
    pthread_mutex_lock(&fd_mutex);
    rtu_fd_age_out();
    pthread_mutex_unlock(&fd_mutex);
}

return_type: void


decompiled_code:
void __fastcall rtu_fd_clear_entries_for_port(char a1)
{
__int64 *v1; // r12
_DWORD *v2; // rbx
__int64 *v3; // rbp

v1 = (__int64 *)&unk_40968;
do
{
v2 = v1 + 11;
v3 = v1 + 44;
do
{
if ( v2[68] && v2[86] )
{
if ( v2[74] == 1 << a1 )
hw_request(1, *v3, (__int64)v3);
else
trace_printf("rtu_fd.c", 489LL, 2LL, "cleaning multicast entries not supported yet...\n");
}
v3 -= 11;
v2 -= 22;
}
while ( v3 != v1 );
v1 = v3 - 44;
}
while ( v3 != (__int64 *)&unk_14AC8 );
rtu_fd_commit();
}

original_code:
void rtu_fd_clear_entries_for_port(int dest_port)
{
    int i;
    int j;
    struct filtering_entry *ent;
    for (i = ((2048)/(4)); i-- > 0;) {
        for (j = 4; j-- > 0;) {
            ent = &rtu_htab[i][j];
            if(ent->valid && ent->dynamic) {
       if(ent->port_mask_dst == (1<<dest_port))
                hw_request(1, ent->addr, ent);
              else {
        trace_printf("rtu_fd.c", 489, 2, "cleaning multicast entries not supported yet...\n");;
              }
            }
        }
    }
    rtu_fd_commit();
}

variable_types: i int

variable_types: j int

variable_types: struct filtering_entry { struct rtu_addr { int hash; int bucket; } addr; int valid; int end_of_bucket; int is_bpdu; unsigned char mac[6]; unsigned char fid; unsigned int port_mask_src; unsigned int port_mask_dst; int drop_when_source; int drop_when_dest; int drop_unmatched_src_ports; unsigned int last_access_t; int force_remove; unsigned char prio_src; int has_prio_src; int prio_override_src; unsigned char prio_dst; int has_prio_dst; int prio_override_dst; int dynamic; int age; } *

user_defined_types: struct filtering_entry { struct rtu_addr addr; int valid; int end_of_bucket; int is_bpdu; unsigned char mac[6]; unsigned char fid; unsigned int port_mask_src; unsigned int port_mask_dst; int drop_when_source; int drop_when_dest; int drop_unmatched_src_ports; unsigned int last_access_t; int force_remove; unsigned char prio_src; int has_prio_src; int prio_override_src; unsigned char prio_dst; int has_prio_dst; int prio_override_dst; int dynamic; int age; }


decompiled_code:
void __fastcall rtu_hash_set_poly(unsigned __int16 a1)
{
hash_poly = (8 * a1) | 0x80000;
}

canonical_decompiled_code:
void __fastcall func1(unsigned __int16 a1)
{
hash_poly = (8 * a1) | 0x80000;
}

original_code:
void rtu_hash_set_poly(uint16_t poly)
{
    hash_poly = ((0x10000 | poly) << 3 );
}

canonical_original_code:
void rtu_hash_set_poly(unsigned short poly)
{
    hash_poly = ((0x10000 | poly) << 3 );
}

variable_types:
poly
unsigned short


original_code:
uint16_t rtu_hash(uint8_t mac[6], uint8_t fid)
{
  uint16_t hash = 0xFFFF;
  hash = crc16(hash, (0xFFFF & fid));
  hash = crc16(hash, ((uint16_t)mac[0] << 8) | mac[1]);
  hash = crc16(hash, ((uint16_t)mac[2] << 8) | mac[3]);
  hash = crc16(hash, ((uint16_t)mac[4] << 8) | mac[5]);
  return hash & (((2048)/(4)) - 1);
}

canonical_original_code:
unsigned short rtu_hash(unsigned char mac[6], unsigned char fid)
{
  unsigned short hash = 0xFFFF;
  hash = crc16(hash, (0xFFFF & fid));
  hash = crc16(hash, ((unsigned short)mac[0] << 8) | mac[1]);
  hash = crc16(hash, ((unsigned short)mac[2] << 8) | mac[3]);
  hash = crc16(hash, ((unsigned short)mac[4] << 8) | mac[5]);
  return hash & (((2048)/(4)) - 1);
}

variable_types:
mac
unsigned char [6]

variable_types:
fid
unsigned char

variable_types:
hash
unsigned short

return_type: unsigned short


decompiled_code:
__int64 __fastcall crc16(__int16 a1, __int16 a2)
{
int v2; // edx
unsigned int v3; // eax

v2 = 20;
v3 = (unsigned __int16)(a1 ^ a2);
while ( 1 )
{
v3 *= 2;
if ( !--v2 )
break;
if ( (v3 & 0x80000) != 0 )
v3 ^= hash_poly;
}
return v3 >> 4;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int16 a1, __int16 a2)
{
int v2; // edx
unsigned int v3; // eax

v2 = 20;
v3 = (unsigned __int16)(a1 ^ a2);
while ( 1 )
{
v3 *= 2;
if ( !--v2 )
break;
if ( (v3 & 0x80000) != 0 )
v3 ^= hash_poly;
}
return v3 >> 4;
}

original_code:
static uint16_t crc16(uint16_t const init_crc, uint16_t const message)
{
    uint32_t remainder;
    int bit;
    remainder = message^init_crc;
    for (bit = 20; bit > 0; --bit) {
        if (remainder & 0x80000) {
            remainder ^= hash_poly;
        }
        remainder = (remainder << 1);
    }
    return (remainder >> 4);
}

canonical_original_code:
static unsigned short crc16(unsigned short init_crc, unsigned short message)
{
    unsigned int remainder;
    int bit;
    remainder = message^init_crc;
    for (bit = 20; bit > 0; --bit) {
        if (remainder & 0x80000) {
            remainder ^= hash_poly;
        }
        remainder = (remainder << 1);
    }
    return (remainder >> 4);
}

variable_types:
message
unsigned short

variable_types:
remainder
unsigned int

variable_types: bit int


decompiled_code:
void __fastcall fpga_writel(int a1, unsigned int a2)
{
*(_DWORD *)(base_fpga + a2) = a1;
}

canonical_decompiled_code:
void __fastcall func0(int a1, unsigned int a2)
{
*(_DWORD *)(base_fpga + a2) = a1;
}

original_code:
static void fpga_writel(uint32_t data, uint32_t addr)
{
 *(volatile uint32_t *)(base_fpga + addr) = data;
}

canonical_original_code:
static void fpga_writel(unsigned int data, unsigned int addr)
{
 *(unsigned int *)(base_fpga + addr) = data;
}

variable_types:
data
unsigned int

variable_types:
addr
unsigned int


decompiled_code:
__int64 __fastcall fpga_readl(unsigned int a1)
{
return *(unsigned int *)(base_fpga + a1);
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1)
{
return *(unsigned int *)(base_fpga + a1);
}

original_code:
static uint32_t fpga_readl(uint32_t addr)
{
 return *(volatile uint32_t *)(base_fpga + addr);
}

canonical_original_code:
static unsigned int fpga_readl(unsigned int addr)
{
 return *(unsigned int *)(base_fpga + addr);
}

return_type: unsigned int


decompiled_code:
void __fastcall rst_lm32(int a1)
{
fpga_writel(4, a1 == 0 ? 66304 : 66308);
}

original_code:
void rst_lm32(int rst)
{
 fpga_writel(1 << 2,
      0x10300 + (rst ? 0x4 : 0x0));
}

variable_types: rst int


decompiled_code:
int __fastcall copy_lm32(__int64 a1, int a2, unsigned int a3)
{
unsigned int v4; // er14
__int64 v5; // rbx
int v6; // eax
__int64 v7; // rbx
unsigned int v8; // eax
unsigned int v9; // er8
int result; // eax

printf("Writing memory: ");
if ( a2 <= 0 )
{
printf("\nVerifing memory: ");
LABEL_11:
result = puts("OK.");
}
else
{
v4 = a3;
v5 = 0LL;
do
{
while ( 1 )
{
v6 = conv_endian(*(_DWORD *)(a1 + 4 * v5));
fpga_writel(v6, v4);
if ( (v5 & 0xFFF) == 0 )
break;
++v5;
v4 += 4;
if ( v5 == a2 )
goto LABEL_6;
}
++v5;
v4 += 4;
putchar(46);
fflush(stdout);
}
while ( v5 != a2 );
LABEL_6:
v7 = 0LL;
printf("\nVerifing memory: ");
while ( 1 )
{
fpga_readl(a3);
v8 = conv_endian(*(_DWORD *)(a1 + 4 * v7));
if ( v8 != v9 )
break;
if ( (v7 & 0xFFF) != 0 )
{
++v7;
a3 += 4;
if ( v7 == a2 )
goto LABEL_11;
}
else
{
++v7;
a3 += 4;
putchar(46);
fflush(stdout);
if ( v7 == a2 )
goto LABEL_11;
}
}
result = printf("Verify failed (%x vs %x)\n", v8, v9);
}
return result;
}

canonical_decompiled_code:
int __fastcall func4(__int64 a1, int a2, unsigned int a3)
{
unsigned int v4; // er14
__int64 v5; // rbx
int v6; // eax
__int64 v7; // rbx
unsigned int v8; // eax
unsigned int v9; // er8
int result; // eax

printf("Writing memory: ");
if ( a2 <= 0 )
{
printf("\nVerifing memory: ");
LABEL_11:
result = puts("OK.");
}
else
{
v4 = a3;
v5 = 0LL;
do
{
while ( 1 )
{
v6 = func2(*(_DWORD *)(a1 + 4 * v5));
func0(v6, v4);
if ( (v5 & 0xFFF) == 0 )
break;
++v5;
v4 += 4;
if ( v5 == a2 )
goto LABEL_6;
}
++v5;
v4 += 4;
putchar(46);
fflush(stdout);
}
while ( v5 != a2 );
LABEL_6:
v7 = 0LL;
printf("\nVerifing memory: ");
while ( 1 )
{
func1(a3);
v8 = func2(*(_DWORD *)(a1 + 4 * v7));
if ( v8 != v9 )
break;
if ( (v7 & 0xFFF) != 0 )
{
++v7;
a3 += 4;
if ( v7 == a2 )
goto LABEL_11;
}
else
{
++v7;
a3 += 4;
putchar(46);
fflush(stdout);
if ( v7 == a2 )
goto LABEL_11;
}
}
result = printf("Verify failed (%x vs %x)\n", v8, v9);
}
return result;
}

original_code:
void copy_lm32(uint32_t *buf, int buf_nwords, uint32_t base_addr)
{
  int i;
  printf("Writing memory: ");
  for(i=0;i<buf_nwords;i++)
  {
   fpga_writel(conv_endian(buf[i]), base_addr + i *4);
   if(!(i & 0xfff)) { printf("."); fflush(stdout); }
  }
  printf("\nVerifing memory: ");
  for(i=0;i<buf_nwords;i++)
  {
   uint32_t x = fpga_readl(base_addr+ i*4);
 if(conv_endian(buf[i]) != x)
 {
   printf("Verify failed (%x vs %x)\n", conv_endian(buf[i]), x);
   return ;
 }
   if(!(i & 0xfff)) { printf("."); fflush(stdout); }
  }
 printf("OK.\n");
}

canonical_original_code:
void copy_lm32(unsigned int *buf, int buf_nwords, unsigned int base_addr)
{
  int i;
  printf("Writing memory: ");
  for(i=0;i<buf_nwords;i++)
  {
   fpga_writel(conv_endian(buf[i]), base_addr + i *4);
   if(!(i & 0xfff)) { printf("."); fflush(stdout); }
  }
  printf("\nVerifing memory: ");
  for(i=0;i<buf_nwords;i++)
  {
   unsigned int x = fpga_readl(base_addr+ i*4);
 if(conv_endian(buf[i]) != x)
 {
   printf("Verify failed (%x vs %x)\n", conv_endian(buf[i]), x);
   return ;
 }
   if(!(i & 0xfff)) { printf("."); fflush(stdout); }
  }
 printf("OK.\n");
}

variable_types:
buf
unsigned int *

variable_types:
x
unsigned int


decompiled_code:
__int64 __fastcall load_lm32_main(char *filename)
{
__pid_t v1; // eax
__int64 result; // rax
int *v3; // rax
char *v4; // rax
int stat_loc; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = fork();
if ( v1 == -1 )
{
v3 = _errno_location();
v4 = strerror(*v3);
fprintf(stderr, "fork(): %s\n", v4);
result = 0xFFFFFFFFLL;
}
else
{
if ( !v1 )
{
load_lm32_child(filename);
exit(0);
}
waitpid(v1, &stat_loc, 0);
result = (unsigned int)-(BYTE1(stat_loc) != 0);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(char *filename)
{
__pid_t v1; // eax
__int64 result; // rax
int *v3; // rax
char *v4; // rax
int stat_loc; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = fork();
if ( v1 == -1 )
{
v3 = _errno_location();
v4 = strerror(*v3);
fprintf(stderr, "fork(): %s\n", v4);
result = 0xFFFFFFFFLL;
}
else
{
if ( !v1 )
{
func5(filename);
exit(0);
}
waitpid(v1, &stat_loc, 0);
result = (unsigned int)-(BYTE1(stat_loc) != 0);
}
return result;
}

original_code:
int load_lm32_main(char *fname)
{
 int pid = fork();
 int status;
 switch(pid) {
 case -1:
  fprintf(stderr, "fork(): %s\n", strerror((*__errno_location ())));
  return -1;
 case 0:
  load_lm32_child(fname);
  exit(0);
 default:
  waitpid(pid, &status, 0);
  if (!(((status) & 0xff00) >> 8))
   return 0;
  return -1;
 }
}

variable_types:
fname
char *

variable_types: pid int

variable_types: status int


original_code:
char *mac_to_string(uint8_t mac[6])
{
  static char str[40];
    snprintf(str, 40, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
    return str;
}

canonical_original_code:
char *mac_to_string(unsigned char mac[6])
{
  static char str[40];
    snprintf(str, 40, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
    return str;
}

variable_types:
str
char [40]

return_type: char *


original_code:
char *mac_to_buffer(uint8_t mac[6],char buffer[18])
{
  if(mac && buffer)
   snprintf(buffer, 18, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
    return buffer;
}

canonical_original_code:
char *mac_to_buffer(unsigned char mac[6], char buffer[18])
{
  if(mac && buffer)
   snprintf(buffer, 18, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
    return buffer;
}

variable_types:
buffer
char [18]


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

if ( a1 && a2 )
result = _isoc99_sscanf(a2, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", a1, a1 + 1, a1 + 2, a1 + 3, a1 + 4, a1 + 5);
else
result = 0xFFFFFFFFLL;
return result;
}

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

if ( a1 && a2 )
result = _isoc99_sscanf(a2, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", a1, a1 + 1, a1 + 2, a1 + 3, a1 + 4, a1 + 5);
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int mac_from_str(uint8_t* tomac, const char *fromstr)
{
 if(tomac==0 || fromstr==0) return -1;
 return sscanf(fromstr,"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",tomac+0,tomac+1,tomac+2,tomac+3,tomac+4,tomac+5);
}

canonical_original_code:
int mac_from_str(unsigned char *tomac, char *fromstr)
{
 if(tomac==0 || fromstr==0) return -1;
 return sscanf(fromstr,"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",tomac+0,tomac+1,tomac+2,tomac+3,tomac+4,tomac+5);
}

variable_types:
tomac
unsigned char *

variable_types:
fromstr
char *


decompiled_code:
__int64 __fastcall pmc_enable_clock(char a1)
{
__int64 result; // rax

result = (unsigned int)(1 << a1);
*(_DWORD *)(sys_base + 15376) = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(char a1)
{
__int64 result; // rax

result = (unsigned int)(1 << a1);
*(_DWORD *)(sys_base + 15376) = result;
return result;
}

original_code:
void pmc_enable_clock(int clock)
{
    *(volatile uint32_t *)(_sys_base + 0xFFFFFC00 - 0xFFFFC000 + 0x10) = ((1<<clock));
}

canonical_original_code:
void pmc_enable_clock(int clock)
{
    *(unsigned int *)(_sys_base + 0xFFFFFC00 - 0xFFFFC000 + 0x10) = ((1<<clock));
}

variable_types: clock int


decompiled_code:
void __fastcall __noreturn shw_pio_toggle_pin(int *a1, unsigned int a2)
{
while ( 1 )
{
shw_pio_set_isra_0(*a1, a1[1], 0);
shw_udelay(a2);
shw_pio_set_isra_0(*a1, a1[1], 1);
shw_udelay(a2);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func3(int *a1, unsigned int a2)
{
while ( 1 )
{
func0(*a1, a1[1], 0);
shw_udelay(a2);
func0(*a1, a1[1], 1);
shw_udelay(a2);
}
}

original_code:
void shw_pio_toggle_pin(pio_pin_t* pin, uint32_t udelay)
{
    while (1)
    {
 shw_pio_set(pin, 0);
 shw_udelay(udelay);
 shw_pio_set(pin, 1);
 shw_udelay(udelay);
    }
}

canonical_original_code:
void shw_pio_toggle_pin(struct pio_pin *pin, unsigned int udelay)
{
    while (1)
    {
 shw_pio_set(pin, 0);
 shw_udelay(udelay);
 shw_pio_set(pin, 1);
 shw_udelay(udelay);
    }
}

variable_types: struct pio_pin { int port; int pin; int mode; int dir; } *

variable_types:
udelay
unsigned int


decompiled_code:
__int64 __fastcall shw_pio_configure(char *a1)
{
__int64 result; // rax
__int64 v2; // rdx
_DWORD *v3; // rcx
int v4; // esi
int v5; // er8
int v6; // edx
int v7; // edx
int v8; // edx

result = (unsigned int)(1 << *((_DWORD *)a1 + 1));
v2 = *(int *)a1;
v3 = (_DWORD *)pio_base[v2];
if ( (int)v2 <= 5 )
{
if ( (int)v2 > 0 )
{
v3[17] = result;
if ( a1[8] < 0 )
v3[25] = result;
else
v3[24] = result;
v7 = *((_DWORD *)a1 + 2) & 3;
switch ( v7 )
{
case 1:
v3[1] = result;
v3[28] = result;
break;
case 2:
v3[1] = result;
v3[29] = result;
break;
case 0:
*v3 = result;
break;
}
v8 = *((_DWORD *)a1 + 3);
if ( v8 == 2 )
{
v3[12] = result;
v3[4] = result;
}
else if ( v8 == 1 )
{
v3[13] = result;
v3[4] = result;
}
else
{
v3[5] = result;
}
}
}
else if ( (_DWORD)v2 == 6 )
{
v4 = *((_DWORD *)a1 + 3);
v5 = v3[2];
if ( v4 == 2 )
{
v3[1] = result;
result = v5 | (unsigned int)result;
v3[2] = result;
}
else
{
v6 = v5 & ~(_DWORD)result;
if ( v4 == 1 )
{
*v3 = result;
result = v5 | (unsigned int)result;
v6 = result;
}
v3[2] = v6;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *a1)
{
__int64 result; // rax
__int64 v2; // rdx
_DWORD *v3; // rcx
int v4; // esi
int v5; // er8
int v6; // edx
int v7; // edx
int v8; // edx

result = (unsigned int)(1 << *((_DWORD *)a1 + 1));
v2 = *(int *)a1;
v3 = (_DWORD *)pio_base[v2];
if ( (int)v2 <= 5 )
{
if ( (int)v2 > 0 )
{
v3[17] = result;
if ( a1[8] < 0 )
v3[25] = result;
else
v3[24] = result;
v7 = *((_DWORD *)a1 + 2) & 3;
switch ( v7 )
{
case 1:
v3[1] = result;
v3[28] = result;
break;
case 2:
v3[1] = result;
v3[29] = result;
break;
case 0:
*v3 = result;
break;
}
v8 = *((_DWORD *)a1 + 3);
if ( v8 == 2 )
{
v3[12] = result;
v3[4] = result;
}
else if ( v8 == 1 )
{
v3[13] = result;
v3[4] = result;
}
else
{
v3[5] = result;
}
}
}
else if ( (_DWORD)v2 == 6 )
{
v4 = *((_DWORD *)a1 + 3);
v5 = v3[2];
if ( v4 == 2 )
{
v3[1] = result;
result = v5 | (unsigned int)result;
v3[2] = result;
}
else
{
v6 = v5 & ~(_DWORD)result;
if ( v4 == 1 )
{
*v3 = result;
result = v5 | (unsigned int)result;
v6 = result;
}
v3[2] = v6;
}
}
return result;
}

original_code:
void shw_pio_configure(const pio_pin_t *pin)
{
    uint32_t mask = (1<<pin->pin);
    uint32_t ddr;
    volatile uint8_t *base = (_pio_base[0][pin->port]);
    switch (pin->port)
    {
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
        *(volatile uint32_t *)(base + 0x44) = (mask);
        if (pin->mode & 0x80)
            *(volatile uint32_t *)(base + 0x64) = (mask);
        else
            *(volatile uint32_t *)(base + 0x60) = (mask);
        switch (pin->mode & 0x3)
        {
        case 0:
            *(volatile uint32_t *)(base + 0x00) = (mask);
            break;
        case 1:
            *(volatile uint32_t *)(base + 0x04) = (mask);
            *(volatile uint32_t *)(base + 0x70) = (mask);
            break;
        case 2:
            *(volatile uint32_t *)(base + 0x04) = (mask);
            *(volatile uint32_t *)(base + 0x74) = (mask);
            break;
        }
        if (pin->dir == 2)
        {
            *(volatile uint32_t *)(base + 0x30) = (mask);
            *(volatile uint32_t *)(base + 0x10) = (mask);
        } else if (pin->dir == 1)
        {
            *(volatile uint32_t *)(base + 0x34) = (mask);
            *(volatile uint32_t *)(base + 0x10) = (mask);
        } else {
            *(volatile uint32_t *)(base + 0x14) = (mask);
        }
        break;
    case 6:
        ddr = (*(volatile uint32_t *)(base + 0x8));
        if (pin->dir == 2)
        {
            *(volatile uint32_t *)(base + 0x4) = (mask);
            ddr |= mask;
        } else if (pin->dir == 1)
        {
            *(volatile uint32_t *)(base + 0x0) = (mask);
            ddr |= mask;
        } else
            ddr &= ~mask;
        *(volatile uint32_t *)(base + 0x8) = (ddr);
        break;
    }
}

canonical_original_code:
void shw_pio_configure(struct pio_pin *pin)
{
    unsigned int mask = (1<<pin->pin);
    unsigned int ddr;
    unsigned char *base = (_pio_base[0][pin->port]);
    switch (pin->port)
    {
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
        *(unsigned int *)(base + 0x44) = (mask);
        if (pin->mode & 0x80)
            *(unsigned int *)(base + 0x64) = (mask);
        else
            *(unsigned int *)(base + 0x60) = (mask);
        switch (pin->mode & 0x3)
        {
        case 0:
            *(unsigned int *)(base + 0x00) = (mask);
            break;
        case 1:
            *(unsigned int *)(base + 0x04) = (mask);
            *(unsigned int *)(base + 0x70) = (mask);
            break;
        case 2:
            *(unsigned int *)(base + 0x04) = (mask);
            *(unsigned int *)(base + 0x74) = (mask);
            break;
        }
        if (pin->dir == 2)
        {
            *(unsigned int *)(base + 0x30) = (mask);
            *(unsigned int *)(base + 0x10) = (mask);
        } else if (pin->dir == 1)
        {
            *(unsigned int *)(base + 0x34) = (mask);
            *(unsigned int *)(base + 0x10) = (mask);
        } else {
            *(unsigned int *)(base + 0x14) = (mask);
        }
        break;
    case 6:
        ddr = (*(unsigned int *)(base + 0x8));
        if (pin->dir == 2)
        {
            *(unsigned int *)(base + 0x4) = (mask);
            ddr |= mask;
        } else if (pin->dir == 1)
        {
            *(unsigned int *)(base + 0x0) = (mask);
            ddr |= mask;
        } else
            ddr &= ~mask;
        *(unsigned int *)(base + 0x8) = (ddr);
        break;
    }
}

variable_types:
mask
unsigned int

variable_types:
ddr
unsigned int

variable_types:
base
unsigned char *

user_defined_types: struct pio_pin { int port; int pin; int mode; int dir; }


decompiled_code:
__int64 shw_pio_configure_all()
{
__int64 v0; // r9
__int64 result; // rax

v0 = (unsigned int)((_DWORD)&all_shw_io + 1);
do
{
while ( *(_BYTE *)v0 != 1 )
{
v0 += 24LL;
if ( (_UNKNOWN *)v0 == (_UNKNOWN *)((char *)&all_shw_io + 265) )
return result;
}
result = shw_pio_configure(*(char **)(v0 + 15));
}
while ( (_UNKNOWN *)v0 != (_UNKNOWN *)((char *)&all_shw_io + 265) );
return result;
}

canonical_decompiled_code:
__int64 func5()
{
__int64 v0; // r9
__int64 result; // rax

v0 = (unsigned int)((_DWORD)&all_shw_io + 1);
do
{
while ( *(_BYTE *)v0 != 1 )
{
v0 += 24LL;
if ( (_UNKNOWN *)v0 == (_UNKNOWN *)((char *)&all_shw_io + 265) )
return result;
}
result = func4(*(char **)(v0 + 15));
}
while ( (_UNKNOWN *)v0 != (_UNKNOWN *)((char *)&all_shw_io + 265) );
return result;
}

original_code:
void shw_pio_configure_all()
{
 int i;
 const shw_io_t* all_io=(shw_io_t*)_all_shw_io;
 for(i=0;i<NUM_SHW_IO_ID;i++)
 {
  if(all_io[i].type==SHW_CPU_PIO)
   shw_pio_configure(all_io[i].ptr);
 }
}

canonical_original_code:
void shw_pio_configure_all()
{
 int i;
 struct shw_io_t *all_io = (shw_io_t*)_all_shw_io;
 for(i=0;i<NUM_SHW_IO_ID;i++)
 {
  if(all_io[i].type==SHW_CPU_PIO)
   shw_pio_configure(all_io[i].ptr);
 }
}

variable_types: i int

variable_types: struct shw_io_t { unsigned char ID; unsigned char type; char *name; void *ptr; } *


decompiled_code:
__int64 shw_fpga_mmap_init()
{
int v0; // eax
int v1; // ebp
__int64 result; // rax

trace_printf("fpga_io.c", 27LL, 16LL, "Initializing FPGA memory mapping.");
v0 = open("/dev/mem", 1052674);
if ( !v0 )
return 0xFFFFFFFFLL;
v1 = v0;
fpga_base_virt = (__int64)mmap(0LL, 0x200000uLL, 3, 1, v0, 0x10000000LL);
if ( fpga_base_virt )
{
trace_printf("fpga_io.c", 40LL, 16LL, "FPGA virtual base = 0x%08x", fpga_base_virt);
result = 0LL;
}
else
{
perror("mmap()");
close(v1);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
int v0; // eax
int v1; // ebp
__int64 result; // rax

trace_printf("fpga_io.c", 27LL, 16LL, "Initializing FPGA memory mapping.");
v0 = open("/dev/mem", 1052674);
if ( !v0 )
return 0xFFFFFFFFLL;
v1 = v0;
fpga_base_virt = (__int64)mmap(0LL, 0x200000uLL, 3, 1, v0, 0x10000000LL);
if ( fpga_base_virt )
{
trace_printf("fpga_io.c", 40LL, 16LL, "FPGA virtual base = 0x%08x", fpga_base_virt);
result = 0LL;
}
else
{
perror("mmap()");
close(v1);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int shw_fpga_mmap_init()
{
  int fd;
    trace_printf("fpga_io.c", 27, 0x10, "Initializing FPGA memory mapping.");;
    fd = open("/dev/mem", 02 | 04010000);
  if(!fd) return -1;
  _fpga_base_virt = mmap(((void *)0), 0x200000, 0x1 | 0x2, 0x01, fd, 0x10000000);
  if(_fpga_base_virt == ((void *)0))
  {
    perror("mmap()");
    close(fd);
    return -1;
  }
    trace_printf("fpga_io.c", 40, 0x10, "FPGA virtual base = 0x%08x", _fpga_base_virt);;
    return 0;
}

variable_types: fd int


decompiled_code:
__int64 __fastcall i2c_bitbang_init_bus(__int64 a1)
{
int **v1; // rbp
__int64 result; // rax

if ( !a1 )
BUG();
v1 = *(int ***)(a1 + 16);
trace_printf("i2c_bitbang.c", 23LL, 16LL, "init: %s (0x%x) ", *(const char **)a1, a1);
shw_pio_configure(*v1);
shw_pio_configure(v1[1]);
shw_pio_setdir_isra_0(**v1, (*v1)[1], 0);
shw_pio_setdir_isra_0(*v1[1], v1[1][1], 0);
v1[2] = (int *)0x6400000032LL;
result = 0LL;
*(_QWORD *)(a1 + 24) = i2c_bitbang_transfer;
*(_QWORD *)(a1 + 32) = i2c_bitbang_scan;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
int **v1; // rbp
__int64 result; // rax

if ( !a1 )
BUG();
v1 = *(int ***)(a1 + 16);
trace_printf("i2c_bitbang.c", 23LL, 16LL, "init: %s (0x%x) ", *(const char **)a1, a1);
shw_pio_configure(*v1);
shw_pio_configure(v1[1]);
func0(**v1, (*v1)[1], 0);
func0(*v1[1], v1[1][1], 0);
v1[2] = (int *)0x6400000032LL;
result = 0LL;
*(_QWORD *)(a1 + 24) = i2c_bitbang_transfer;
*(_QWORD *)(a1 + 32) = i2c_bitbang_scan;
return result;
}

original_code:
int i2c_bitbang_init_bus(struct i2c_bus *bus)
{
 struct i2c_bitbang *priv;
 if (!bus && !bus->type_specific && bus->type != 0)
  return -1;
 priv = (struct i2c_bitbang *)bus->type_specific;
 trace_printf("i2c_bitbang.c", 23, 0x10,"init: %s (0x%x) ",bus->name,bus);;
 shw_pio_configure(priv->scl);
 shw_pio_configure(priv->sda);
 shw_pio_setdir(priv->scl, 0);
 shw_pio_setdir(priv->sda, 0);
 priv->udelay = 50;
 priv->timeout = 100;
 bus->transfer = i2c_bitbang_transfer;
 bus->scan = i2c_bitbang_scan;
 return 0;
}

variable_types: struct i2c_bus { char *name; int type; void *type_specific; int (*transfer)(struct i2c_bus *bus, unsigned int address, unsigned int to_write, unsigned int to_read, unsigned char *data); int (*scan)(struct i2c_bus *bus, unsigned int address); int err; } *

variable_types: struct i2c_bitbang { struct pio_pin { int port; int pin; int mode; int dir; } *scl; struct pio_pin *sda; int udelay; int timeout; } *

user_defined_types: struct i2c_bitbang { struct pio_pin *scl; struct pio_pin *sda; int udelay; int timeout; }


decompiled_code:
__int64 __fastcall mi2c_pin_out(int *a1, int a2)
{
shw_pio_setdir_isra_0(*a1, a1[1], a2 == 0);
return shw_udelay(4LL);
}

canonical_decompiled_code:
__int64 __fastcall func3(int *a1, int a2)
{
func0(*a1, a1[1], a2 == 0);
return shw_udelay(4LL);
}

original_code:
void mi2c_pin_out(pio_pin_t* pin, int state)
{
       shw_pio_setdir(pin, state ? 0 : 1);
       shw_udelay(4);
}

canonical_original_code:
void mi2c_pin_out(struct pio_pin *pin, int state)
{
       shw_pio_setdir(pin, state ? 0 : 1);
       shw_udelay(4);
}

variable_types: state int


decompiled_code:
__int64 __fastcall mi2c_stop(int **a1)
{
mi2c_pin_out(a1[1], 0);
mi2c_pin_out(*a1, 1);
return mi2c_pin_out(a1[1], 1);
}

original_code:
static void mi2c_stop(struct i2c_bitbang* bus)
{
        mi2c_pin_out(bus->sda,0);
        mi2c_pin_out(bus->scl,1);
        mi2c_pin_out(bus->sda,1);
}

canonical_original_code:
static void mi2c_stop(struct i2c_bitbang *bus)
{
        mi2c_pin_out(bus->sda,0);
        mi2c_pin_out(bus->scl,1);
        mi2c_pin_out(bus->sda,1);
}


decompiled_code:
__int64 __fastcall i2c_bitbang_scan(__int64 a1, char a2)
{
int **v2; // rbp
bool v3; // r8
__int64 result; // rax
bool v5; // bl

if ( !a1 )
return 4294967279LL;
v2 = *(int ***)(a1 + 16);
shw_pio_setdir_isra_0(*v2[1], v2[1][1], 0);
v3 = shw_pio_get_isra_0(*v2[1], v2[1][1]);
result = 0LL;
if ( v3 )
{
mi2c_start(v2);
v5 = mi2c_write_byte(v2, (unsigned __int8)(2 * a2));
mi2c_stop(v2);
result = v5;
}
return result;
}

original_code:
int32_t i2c_bitbang_scan(struct i2c_bus* bus, uint32_t address)
{
 if (!bus)
  return -0x11;
 uint8_t state;
 struct i2c_bitbang* ts = (struct i2c_bitbang*)bus->type_specific;
 shw_pio_setdir(ts->sda, 0);
 state=shw_pio_get(ts->sda);
 if(state!=1) return 0;
 mi2c_start(ts);
 state = mi2c_write_byte(ts, (address << 1) | 0);
 mi2c_stop(ts);
 return state ? 1: 0;
}

canonical_original_code:
int i2c_bitbang_scan(struct i2c_bus *bus, unsigned int address)
{
 if (!bus)
  return -0x11;
 unsigned char state;
 struct i2c_bitbang *ts = (struct i2c_bitbang*)bus->type_specific;
 shw_pio_setdir(ts->sda, 0);
 state=shw_pio_get(ts->sda);
 if(state!=1) return 0;
 mi2c_start(ts);
 state = mi2c_write_byte(ts, (address << 1) | 0);
 mi2c_stop(ts);
 return state ? 1: 0;
}

variable_types:
address
unsigned int

variable_types:
state
unsigned char

user_defined_types: struct i2c_bus { char *name; int type; void *type_specific; int (*transfer)(struct i2c_bus *bus, unsigned int address, unsigned int to_write, unsigned int to_read, unsigned char *data); int (*scan)(struct i2c_bus *bus, unsigned int address); int err; }


decompiled_code:
__int64 __fastcall mi2c_get_byte(int **a1, int a2)
{
unsigned int v2; // er12
int v3; // ebp

v2 = 0;
v3 = 8;
mi2c_pin_out(*a1, 0);
shw_pio_setdir_isra_0(*a1[1], a1[1][1], 0);
do
{
v2 *= 2;
mi2c_pin_out(*a1, 1);
if ( shw_pio_get_isra_0(*a1[1], a1[1][1]) )
v2 |= 1u;
mi2c_pin_out(*a1, 0);
--v3;
}
while ( v3 );
mi2c_pin_out(a1[1], a2 ^ 1);
shw_udelay(4LL);
mi2c_pin_out(*a1, 1);
mi2c_pin_out(*a1, 0);
return v2;
}

original_code:
static uint8_t mi2c_get_byte(struct i2c_bitbang *bus, int ack)
{
 int i;
 unsigned char result = 0;
 mi2c_pin_out(bus->scl, 0);
 shw_pio_setdir(bus->sda, 0);
 for (i=0;i<8;i++)
 {
  mi2c_pin_out(bus->scl, 1);
  result <<= 1;
  if (shw_pio_get(bus->sda))
   result |= 0x01;
  mi2c_pin_out(bus->scl, 0);
 }
 mi2c_pin_out(bus->sda, ack ? 0 : 1);
 shw_udelay(4);
 mi2c_pin_out(bus->scl, 1);
 mi2c_pin_out(bus->scl, 0);
 return result;
}

canonical_original_code:
static unsigned char mi2c_get_byte(struct i2c_bitbang *bus, int ack)
{
 int i;
 unsigned char result = 0;
 mi2c_pin_out(bus->scl, 0);
 shw_pio_setdir(bus->sda, 0);
 for (i=0;i<8;i++)
 {
  mi2c_pin_out(bus->scl, 1);
  result <<= 1;
  if (shw_pio_get(bus->sda))
   result |= 0x01;
  mi2c_pin_out(bus->scl, 0);
 }
 mi2c_pin_out(bus->sda, ack ? 0 : 1);
 shw_udelay(4);
 mi2c_pin_out(bus->scl, 1);
 mi2c_pin_out(bus->scl, 0);
 return result;
}

variable_types: ack int

variable_types:
result
unsigned char

return_type: unsigned char


decompiled_code:
__int64 __fastcall i2c_bitbang_transfer(__int64 a1, char a2, int a3, int a4, __int64 a5)
{
unsigned int v5; // er14
int **v6; // r12
__int64 v10; // r15
__int64 v11; // rbx
__int64 v12; // r15
__int64 v13; // rbx

if ( !a1 )
return (unsigned int)-17;
v5 = *(_DWORD *)(a1 + 8);
if ( v5 )
return (unsigned int)-18;
v6 = *(int ***)(a1 + 16);
if ( a3 )
{
mi2c_start(*(int ***)(a1 + 16));
if ( !mi2c_write_byte(v6, (unsigned __int8)(2 * a2)) )
{
LABEL_18:
v5 = -1;
mi2c_stop(v6);
return v5;
}
v10 = (unsigned int)(a3 - 1);
v11 = 1LL;
v12 = v10 + 2;
do
{
if ( !mi2c_write_byte(v6, *(unsigned __int8 *)(a5 + v11 - 1)) )
{
v5 = -2;
mi2c_stop(v6);
return v5;
}
v5 = v11++;
}
while ( v11 != v12 );
mi2c_stop(v6);
if ( !a4 )
return v5;
}
else if ( !a4 )
{
return v5;
}
mi2c_start(v6);
if ( !mi2c_write_byte(v6, (unsigned __int8)(2 * a2 + 1)) )
goto LABEL_18;
v13 = 0LL;
do
{
*(_BYTE *)(a5 + v13) = mi2c_get_byte(v6, a4 - 1 != v13);
++v13;
}
while ( a4 != v13 );
mi2c_stop(v6);
v5 += a4;
return v5;
}

original_code:
int i2c_bitbang_transfer(i2c_bus_t* bus, uint32_t address, uint32_t to_write, uint32_t to_read, uint8_t* data)
{
 if (!bus)
  return -0x11;
 if (bus->type != 0)
  return -0x12;
 struct i2c_bitbang* ts = (struct i2c_bitbang*)bus->type_specific;
 int sent = 0;
 int rcvd = 0;
 int ack = 0;
 uint32_t i;
 if (to_write > 0)
 {
  mi2c_start(ts);
  ack = mi2c_write_byte(ts, address << 1);
  if (!ack)
  {
   mi2c_stop(ts);
   return -1;
  }
  for (i=0; i < to_write; i++)
  {
   ack = mi2c_write_byte(ts, data[i]);
   if (!ack)
   {
    mi2c_stop(ts);
    return -2;
   }
   sent++;
  }
  mi2c_stop(ts);
 }
 if (to_read)
 {
  mi2c_start(ts);
  ack = mi2c_write_byte(ts, (address << 1) | 1);
  if (!ack)
  {
   mi2c_stop(ts);
   return -1;
  }
  uint32_t last_byte = to_read - 1;
  for (i=0; i < to_read; i++)
  {
   data[i] = mi2c_get_byte(ts, i != last_byte);
   rcvd++;
  }
  mi2c_stop(ts);
 }
 return sent+rcvd;
}

canonical_original_code:
int i2c_bitbang_transfer(struct i2c_bus *bus, unsigned int address, unsigned int to_write, unsigned int to_read, unsigned char *data)
{
 if (!bus)
  return -0x11;
 if (bus->type != 0)
  return -0x12;
 struct i2c_bitbang *ts = (struct i2c_bitbang*)bus->type_specific;
 int sent = 0;
 int rcvd = 0;
 int ack = 0;
 unsigned int i;
 if (to_write > 0)
 {
  mi2c_start(ts);
  ack = mi2c_write_byte(ts, address << 1);
  if (!ack)
  {
   mi2c_stop(ts);
   return -1;
  }
  for (i=0; i < to_write; i++)
  {
   ack = mi2c_write_byte(ts, data[i]);
   if (!ack)
   {
    mi2c_stop(ts);
    return -2;
   }
   sent++;
  }
  mi2c_stop(ts);
 }
 if (to_read)
 {
  mi2c_start(ts);
  ack = mi2c_write_byte(ts, (address << 1) | 1);
  if (!ack)
  {
   mi2c_stop(ts);
   return -1;
  }
  unsigned int last_byte = to_read - 1;
  for (i=0; i < to_read; i++)
  {
   data[i] = mi2c_get_byte(ts, i != last_byte);
   rcvd++;
  }
  mi2c_stop(ts);
 }
 return sent+rcvd;
}

variable_types:
data
unsigned char *

variable_types: sent int

variable_types: rcvd int

variable_types:
i
unsigned int


decompiled_code:
__int64 __fastcall mi2c_start(int **a1)
{
mi2c_pin_out(a1[1], 0);
return mi2c_pin_out(*a1, 0);
}

canonical_decompiled_code:
__int64 __fastcall func4(int **a1)
{
func3(a1[1], 0);
return func3(*a1, 0);
}

original_code:
static void mi2c_start(struct i2c_bitbang* bus)
{
       mi2c_pin_out(bus->sda, 0);
       mi2c_pin_out(bus->scl, 0);
}

canonical_original_code:
static void mi2c_start(struct i2c_bitbang *bus)
{
       mi2c_pin_out(bus->sda, 0);
       mi2c_pin_out(bus->scl, 0);
}


decompiled_code:
_BOOL8 __fastcall mi2c_write_byte(int **a1, int a2)
{
int v2; // er12
char v4; // si
_BOOL4 v5; // ebp

v2 = 8;
do
{
v4 = a2;
a2 *= 2;
mi2c_pin_out(a1[1], v4 & 0x80);
mi2c_pin_out(*a1, 1);
mi2c_pin_out(*a1, 0);
--v2;
}
while ( v2 );
mi2c_pin_out(a1[1], 1);
mi2c_pin_out(*a1, 1);
shw_pio_setdir_isra_0(*a1[1], a1[1][1], 0);
shw_udelay(4LL);
v5 = shw_pio_get_isra_0(*a1[1], a1[1][1]);
mi2c_pin_out(*a1, 0);
shw_udelay(4LL);
return !v5;
}

original_code:
static int mi2c_write_byte(struct i2c_bitbang* bus, uint8_t data)
{
       int ack = 0;
       int b;
       for (b = 0; b < 8; b++)
       {
        mi2c_pin_out(bus->sda, data & 0x80);
        mi2c_pin_out(bus->scl, 1);
        mi2c_pin_out(bus->scl, 0);
        data <<= 1;
       }
       mi2c_pin_out(bus->sda, 1);
       mi2c_pin_out(bus->scl, 1);
       shw_pio_setdir(bus->sda, 0);
       shw_udelay(4);
       ack = shw_pio_get(bus->sda);
       mi2c_pin_out(bus->scl, 0);
       shw_udelay(4);
       return (ack == 0) ? 1: 0;
}

canonical_original_code:
static int mi2c_write_byte(struct i2c_bitbang *bus, unsigned char data)
{
       int ack = 0;
       int b;
       for (b = 0; b < 8; b++)
       {
        mi2c_pin_out(bus->sda, data & 0x80);
        mi2c_pin_out(bus->scl, 1);
        mi2c_pin_out(bus->scl, 0);
        data <<= 1;
       }
       mi2c_pin_out(bus->sda, 1);
       mi2c_pin_out(bus->scl, 1);
       shw_pio_setdir(bus->sda, 0);
       shw_udelay(4);
       ack = shw_pio_get(bus->sda);
       mi2c_pin_out(bus->scl, 0);
       shw_udelay(4);
       return (ack == 0) ? 1: 0;
}

variable_types:
data
unsigned char

variable_types: b int


decompiled_code:
__int64 __fastcall shw_i2c_io_scan(__int64 a1)
{
int v1; // ebx
__int64 v2; // r12
int v3; // edx
__int64 v4; // rsi

v1 = *((_DWORD *)&i2c_io_bus + 10);
if ( v1 )
{
LODWORD(v2) = -1;
}
else
{
v2 = (unsigned int)i2c_scan(&i2c_io_bus, a1);
printf("\ni2c_bus: %s: %d devices\n", i2c_io_bus, v2);
do
{
while ( 1 )
{
v3 = *(unsigned __int8 *)(a1 + (v1 >> 3));
if ( _bittest(&v3, v1 & 7) )
break;
if ( ++v1 == 128 )
return (unsigned int)v2;
}
v4 = (unsigned int)v1++;
printf("device at: 0x%02X\n", v4);
}
while ( v1 != 128 );
}
return (unsigned int)v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
int v1; // ebx
__int64 v2; // r12
int v3; // edx
__int64 v4; // rsi

v1 = *((_DWORD *)&i2c_io_bus + 10);
if ( v1 )
{
LODWORD(v2) = -1;
}
else
{
v2 = (unsigned int)i2c_scan(&i2c_io_bus, a1);
printf("\ni2c_bus: %s: %d devices\n", i2c_io_bus, v2);
do
{
while ( 1 )
{
v3 = *(unsigned __int8 *)(a1 + (v1 >> 3));
if ( _bittest(&v3, v1 & 7) )
break;
if ( ++v1 == 128 )
return (unsigned int)v2;
}
v4 = (unsigned int)v1++;
printf("device at: 0x%02X\n", v4);
}
while ( v1 != 128 );
}
return (unsigned int)v2;
}

original_code:
int shw_i2c_io_scan(uint8_t *dev_map)
{
 int i;
 int detect;
 if (i2c_io_bus.err)
   return -1;
 detect = i2c_scan(&i2c_io_bus, dev_map);
 printf("\ni2c_bus: %s: %d devices\n", i2c_io_bus.name, detect);
 for (i = 0; i < 128; i++)
  if (dev_map[i/8] & (1 << (i%8)))
   printf("device at: 0x%02X\n", i);
 return detect;
}

canonical_original_code:
int shw_i2c_io_scan(unsigned char *dev_map)
{
 int i;
 int detect;
 if (i2c_io_bus.err)
   return -1;
 detect = i2c_scan(&i2c_io_bus, dev_map);
 printf("\ni2c_bus: %s: %d devices\n", i2c_io_bus.name, detect);
 for (i = 0; i < 128; i++)
  if (dev_map[i/8] & (1 << (i%8)))
   printf("device at: 0x%02X\n", i);
 return detect;
}

variable_types: detect int


decompiled_code:
__int64 shw_get_hw_ver()
{
int v0; // er8
__int64 result; // rax
unsigned __int8 v2; // al

v0 = ((__int64 (__fastcall *)(char **, __int64))*(&i2c_io_bus + 4))(&i2c_io_bus, 32LL);
result = 320LL;
if ( v0 )
{
v2 = ((unsigned __int8)wrswhw_pca9554_get_input(&i2c_io_bus, 32LL) >> 1) & 7;
if ( v2 > 2u )
{
trace_printf("i2c_io.c", 121LL, 3LL, "Unknown HW version (0x%x), check the DIP switch under the SCB", v2 & 7);
result = 0xFFFFFFFFLL;
}
else
{
result = (unsigned int)CSWTCH_3[v2];
}
}
return result;
}

canonical_decompiled_code:
__int64 func2()
{
int v0; // er8
__int64 result; // rax
unsigned __int8 v2; // al

v0 = ((__int64 (__fastcall *)(char **, __int64))*(&i2c_io_bus + 4))(&i2c_io_bus, 32LL);
result = 320LL;
if ( v0 )
{
v2 = ((unsigned __int8)wrswhw_pca9554_get_input(&i2c_io_bus, 32LL) >> 1) & 7;
if ( v2 > 2u )
{
trace_printf("i2c_io.c", 121LL, 3LL, "Unknown HW version (0x%x), check the DIP switch under the SCB", v2 & 7);
result = 0xFFFFFFFFLL;
}
else
{
result = (unsigned int)CSWTCH_3[v2];
}
}
return result;
}

original_code:
int shw_get_hw_ver()
{
 uint8_t ret;
 struct i2c_bus *bus= &i2c_io_bus;
 if(bus && bus->scan(bus,0x20))
 {
  ret= wrswhw_pca9554_get_input(bus,0x20);
  switch((ret >> 1) & 0x7)
  {
   case 0: return 330;
   case 1: return 340;
   case 2: return 341;
   default:
    trace_printf("i2c_io.c", 121, 3,"Unknown HW version (0x%x), check the DIP switch under the SCB",(ret >> 1) & 0x7);;
    return -1;
  }
 }
 else
 {
  return 320;
 }
}

canonical_original_code:
int shw_get_hw_ver()
{
 unsigned char ret;
 struct i2c_bus *bus = &i2c_io_bus;
 if(bus && bus->scan(bus,0x20))
 {
  ret= wrswhw_pca9554_get_input(bus,0x20);
  switch((ret >> 1) & 0x7)
  {
   case 0: return 330;
   case 1: return 340;
   case 2: return 341;
   default:
    trace_printf("i2c_io.c", 121, 3,"Unknown HW version (0x%x), check the DIP switch under the SCB",(ret >> 1) & 0x7);;
    return -1;
  }
 }
 else
 {
  return 320;
 }
}

variable_types:
ret
unsigned char


decompiled_code:
__int64 shw_get_fpga_type()
{
if ( ((unsigned int (__fastcall *)(char **, __int64))*(&i2c_io_bus + 4))(&i2c_io_bus, 32LL) )
{
if ( (wrswhw_pca9554_get_input(&i2c_io_bus, 32LL) & 1) == 0 )
return 4294967170LL;
}
else if ( access("/wr/etc/lx240t.conf", 0) )
{
return 4294967170LL;
}
return 4294967280LL;
}

canonical_decompiled_code:
__int64 func3()
{
if ( ((unsigned int (__fastcall *)(char **, __int64))*(&i2c_io_bus + 4))(&i2c_io_bus, 32LL) )
{
if ( (wrswhw_pca9554_get_input(&i2c_io_bus, 32LL) & 1) == 0 )
return 4294967170LL;
}
else if ( access("/wr/etc/lx240t.conf", 0) )
{
return 4294967170LL;
}
return 4294967280LL;
}

original_code:
uint8_t shw_get_fpga_type()
{
 struct i2c_bus *bus= &i2c_io_bus;
 if(bus && bus->scan(bus,0x20))
 {
  if(wrswhw_pca9554_get_input(bus,0x20) & 0x1)
   return 240;
  else
   return 130;
 }
 if(access("/wr/etc/lx240t.conf", 0) == 0) return 240;
 return 130;
}

canonical_original_code:
unsigned char shw_get_fpga_type()
{
 struct i2c_bus *bus = &i2c_io_bus;
 if(bus && bus->scan(bus,0x20))
 {
  if(wrswhw_pca9554_get_input(bus,0x20) & 0x1)
   return 240;
  else
   return 130;
 }
 if(access("/wr/etc/lx240t.conf", 0) == 0) return 240;
 return 130;
}


decompiled_code:
char **shw_i2c_io_get_bus()
{
return &i2c_io_bus;
}

canonical_decompiled_code:
char **func4()
{
return &i2c_io_bus;
}

original_code:
struct i2c_bus* shw_i2c_io_get_bus()
{
 return &i2c_io_bus;
}

canonical_original_code:
struct i2c_bus *shw_i2c_io_get_bus()
{
 return &i2c_io_bus;
}

return_type: struct i2c_bus { char *name; int type; void *type_specific; int (*transfer)(struct i2c_bus *bus, unsigned int address, unsigned int to_write, unsigned int to_read, unsigned char *data); int (*scan)(struct i2c_bus *bus, unsigned int address); int err; } *


original_code:
int shw_hwiu_gwver(struct gw_info *info)
{
  uint32_t data[4 +1];
  struct gw_info *s_data;
  int i;
  if( hwiu_read_word(0, data) < 0 )
    return -1;
  s_data = (struct gw_info *)data;
  *info = *s_data;
  if( info->nwords != 4 ) {
    printf("nwords: sw=%u, hw=%u, ver=%u, data=%x\n", info->nwords, 4, info->struct_ver, data[0]);
    return -1;
  }
  for(i=0; i<info->nwords; ++i) {
    if( hwiu_read_word(1 +i, data+i+1) < 0 )
      return -1;
  }
  *info = *( (struct gw_info*) data);
  return 0;
}

canonical_original_code:
int shw_hwiu_gwver(struct gw_info *info)
{
  unsigned int data[-1];
  struct gw_info *s_data;
  int i;
  if( hwiu_read_word(0, data) < 0 )
    return -1;
  s_data = (struct gw_info *)data;
  *info = *s_data;
  if( info->nwords != 4 ) {
    printf("nwords: sw=%u, hw=%u, ver=%u, data=%x\n", info->nwords, 4, info->struct_ver, data[0]);
    return -1;
  }
  for(i=0; i<info->nwords; ++i) {
    if( hwiu_read_word(1 +i, data+i+1) < 0 )
      return -1;
  }
  *info = *( (struct gw_info *) data);
  return 0;
}

variable_types: struct gw_info { unsigned char ver_minor; unsigned char nwords; unsigned char struct_ver; unsigned char build_no; unsigned int switch_hdl_hash; unsigned int general_cores_hash; unsigned int wr_cores_hash; } *

variable_types:
data
unsigned int [-1]

variable_types: i int


decompiled_code:
void __fastcall serial_set_dtr(int a1)
{
int v1; // ebp
int v2; // eax
int v3[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = serial_fd;
if ( serial_fd >= 0 )
{
ioctl(serial_fd, 0x5415uLL, v3);
if ( a1 )
v2 = v3[0] | 2;
else
v2 = v3[0] & 0xFFFFFFFD;
v3[0] = v2;
ioctl(v1, 0x5418uLL, v3);
}
}

canonical_decompiled_code:
void __fastcall func0(int a1)
{
int v1; // ebp
int v2; // eax
int v3[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = serial_fd;
if ( serial_fd >= 0 )
{
ioctl(serial_fd, 0x5415uLL, v3);
if ( a1 )
v2 = v3[0] | 2;
else
v2 = v3[0] & 0xFFFFFFFD;
v3[0] = v2;
ioctl(v1, 0x5418uLL, v3);
}
}

original_code:
void serial_set_dtr(int s)
{
    unsigned int v;
    if (serial_fd<0) return;
    ioctl(serial_fd,0x5415,&v);
    if (s) v|=0x002; else v&=~0x002;
    ioctl(serial_fd,0x5418,&v);
}

variable_types: s int

variable_types:
v
unsigned int


decompiled_code:
int serial_close()
{
return close(serial_fd);
}

canonical_decompiled_code:
int func2()
{
return close(serial_fd);
}

original_code:
void serial_close()
{
    close(serial_fd);
}


decompiled_code:
ssize_t __fastcall serial_write(void *buf, int a2)
{
return write(serial_fd, buf, a2);
}

canonical_decompiled_code:
ssize_t __fastcall func3(void *buf, int a2)
{
return write(serial_fd, buf, a2);
}

original_code:
int serial_write(char *data, int len)
{
    int i;
    return write(serial_fd, data, len);
}

variable_types:
data
char *

variable_types: len int


decompiled_code:
ssize_t __fastcall serial_write_byte(char a1)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a1;
return write(serial_fd, buf, 1uLL);
}

canonical_decompiled_code:
ssize_t __fastcall func5(char a1)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a1;
return write(serial_fd, buf, 1uLL);
}

original_code:
void serial_write_byte(unsigned char b)
{
    write (serial_fd,&b,1);
}

variable_types:
b
unsigned char


decompiled_code:
__int64 serial_read_byte()
{
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

serial_read(buf, 1);
return (unsigned __int8)buf[0];
}

canonical_decompiled_code:
__int64 func6()
{
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

func4(buf, 1);
return (unsigned __int8)buf[0];
}

original_code:
unsigned char serial_read_byte()
{
    unsigned char b;
    serial_read(&b,1);
    return b;
}


decompiled_code:
_BOOL8 serial_data_avail()
{
int v0; // edi
struct timeval v2; // [rsp+0h] [rbp-98h] BYREF
fd_set readfds; // [rsp+10h] [rbp-88h] BYREF

memset(&readfds, 0, sizeof(readfds));
v0 = serial_fd;
v2.tv_sec = 0LL;
v2.tv_usec = 0LL;
readfds.fds_bits[serial_fd / 64] |= 1LL << (serial_fd % 64);
return select(v0 + 1, &readfds, 0LL, 0LL, &v2) > 0;
}

canonical_decompiled_code:
_BOOL8 func7()
{
int v0; // edi
struct timeval v2; // [rsp+0h] [rbp-98h] BYREF
fd_set readfds; // [rsp+10h] [rbp-88h] BYREF

memset(&readfds, 0, sizeof(readfds));
v0 = serial_fd;
v2.tv_sec = 0LL;
v2.tv_usec = 0LL;
readfds.fds_bits[serial_fd / 64] |= 1LL << (serial_fd % 64);
return select(v0 + 1, &readfds, 0LL, 0LL, &v2) > 0;
}

original_code:
int serial_data_avail()
{
    fd_set set;
    struct timeval tv;
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&set)->__fds_bits)[0]) : "memory"); } while (0);
    ((void) (((&set)->__fds_bits)[((serial_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((serial_fd) % (8 * (int) sizeof (__fd_mask)))))));
    tv.tv_sec = 0;
    tv.tv_usec = 0;
    return select(serial_fd+1, &set, ((void *)0), ((void *)0), &tv)>0;
}

canonical_original_code:
int serial_data_avail()
{
    struct fd_set set;
    struct timeval tv;
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&set)->__fds_bits)[0]) : "memory"); } while (0);
    ((void) (((&set)->__fds_bits)[((serial_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((serial_fd) % (8 * (int) sizeof (__fd_mask)))))));
    tv.tv_sec = 0;
    tv.tv_usec = 0;
    return select(serial_fd+1, &set, ((void *)0), ((void *)0), &tv)>0;
}

variable_types:
set
struct fd_set { long __fds_bits[-1]; }

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


decompiled_code:
__int64 sys_get_clock_usec()
{
struct timezone tz; // [rsp+8h] [rbp-20h] BYREF
struct timeval tv; // [rsp+10h] [rbp-18h] BYREF

tz = 0LL;
gettimeofday(&tv, &tz);
return (unsigned int)(LODWORD(tv.tv_usec) + 1000000 * LODWORD(tv.tv_sec));
}

canonical_decompiled_code:
__int64 func8()
{
struct timezone tz; // [rsp+8h] [rbp-20h] BYREF
struct timeval tv; // [rsp+10h] [rbp-18h] BYREF

tz = 0LL;
gettimeofday(&tv, &tz);
return (unsigned int)(LODWORD(tv.tv_usec) + 1000000 * LODWORD(tv.tv_sec));
}

original_code:
unsigned int sys_get_clock_usec()
{
    struct timezone tz={0,0};
    struct timeval tv;
    gettimeofday(&tv,&tz);
    return tv.tv_usec + tv.tv_sec * 1000000;
}

canonical_original_code:
unsigned int sys_get_clock_usec()
{
    struct timezone tz = {0,0};
    struct timeval tv;
    gettimeofday(&tv,&tz);
    return tv.tv_usec + tv.tv_sec * 1000000;
}

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

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


decompiled_code:
int __fastcall sys_delay(int a1)
{
return usleep(1000 * a1);
}

canonical_decompiled_code:
int __fastcall func9(int a1)
{
return usleep(1000 * a1);
}

original_code:
void sys_delay(int msecs)
{
    usleep(msecs*1000);
}

variable_types: msecs int


decompiled_code:
_BOOL8 serial_purge()
{
_BOOL8 result; // rax

while ( 1 )
{
result = serial_data_avail();
if ( !result )
break;
serial_read_byte();
}
return result;
}

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

while ( 1 )
{
result = func7();
if ( !result )
break;
func6();
}
return result;
}

original_code:
void serial_purge()
{
  while(serial_data_avail()) serial_read_byte();
}


decompiled_code:
__int64 shw_io_init()
{
__int64 result; // rax
int v1; // eax

result = shw_pio_mmap_init();
if ( (int)result >= 0 )
{
if ( i2c_io_bus[10] != -17 || (result = shw_i2c_io_init(), (int)result >= 0) )
{
v1 = shw_get_hw_ver();
if ( v1 <= 329 )
{
*((_WORD *)&all_shw_io + 36) = 259;
*((_WORD *)&all_shw_io + 48) = 260;
*((_WORD *)&all_shw_io + 60) = 261;
*((_WORD *)&all_shw_io + 72) = 262;
*((_WORD *)&all_shw_io + 12) = 257;
*((_QWORD *)&all_shw_io + 4) = "shw_io_reset_n";
*((_QWORD *)&all_shw_io + 5) = &PIN_shw_io_reset_n;
*((_WORD *)&all_shw_io + 24) = 258;
*((_QWORD *)&all_shw_io + 7) = "shw_io_box_fan_en";
*((_QWORD *)&all_shw_io + 8) = &PIN_shw_io_box_fan_en;
*((_QWORD *)&all_shw_io + 10) = "shw_io_box_fan_tacho";
*((_QWORD *)&all_shw_io + 11) = &PIN_shw_io_box_fan_tacho;
*((_QWORD *)&all_shw_io + 13) = "shw_io_fpga_fan_en";
*((_QWORD *)&all_shw_io + 14) = &PIN_shw_io_fpga_fan_en;
*((_QWORD *)&all_shw_io + 16) = "shw_io_led_cpu1";
*((_QWORD *)&all_shw_io + 17) = &PIN_shw_io_led_cpu1;
*((_QWORD *)&all_shw_io + 19) = "shw_io_led_cpu2";
*((_QWORD *)&all_shw_io + 20) = &PIN_shw_io_led_cpu2;
*((_WORD *)&all_shw_io + 84) = 263;
*((_QWORD *)&all_shw_io + 22) = "shw_io_arm_boot_sel";
*((_QWORD *)&all_shw_io + 23) = &PIN_shw_io_arm_boot_sel;
*((_WORD *)&all_shw_io + 120) = 266;
*((_QWORD *)&all_shw_io + 31) = "shw_io_arm_gen_but";
*((_QWORD *)&all_shw_io + 32) = &PIN_shw_io_arm_gen_but;
}
else
{
*((_WORD *)&all_shw_io + 12) = 257;
*((_QWORD *)&all_shw_io + 4) = "shw_io_reset_n";
*((_QWORD *)&all_shw_io + 5) = &PIN_shw_io_reset_n;
*((_WORD *)&all_shw_io + 60) = 261;
*((_QWORD *)&all_shw_io + 16) = "shw_io_led_cpu1";
*((_QWORD *)&all_shw_io + 17) = &PIN_shw_io_led_cpu1;
*((_WORD *)&all_shw_io + 72) = 262;
*((_QWORD *)&all_shw_io + 19) = "shw_io_led_cpu2";
*((_QWORD *)&all_shw_io + 20) = &PIN_shw_io_led_cpu2;
*((_WORD *)&all_shw_io + 84) = 263;
*((_QWORD *)&all_shw_io + 22) = "shw_io_arm_boot_sel";
*((_QWORD *)&all_shw_io + 23) = &PIN_shw_io_arm_boot_sel;
*((_WORD *)&all_shw_io + 96) = 520;
*((_QWORD *)&all_shw_io + 25) = "shw_io_led_state_g";
*((_QWORD *)&all_shw_io + 26) = &I2C_shw_io_led_state_g;
*((_WORD *)&all_shw_io + 108) = 521;
*((_QWORD *)&all_shw_io + 28) = "shw_io_led_state_o";
*((_QWORD *)&all_shw_io + 29) = &I2C_shw_io_led_state_o;
}
trace_printf("shw_io.c", 110LL, 16LL, "version=%d (CPUPWN=%d)", (unsigned int)v1, v1 <= 329);
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 result; // rax
int v1; // eax

result = shw_pio_mmap_init();
if ( (int)result >= 0 )
{
if ( i2c_io_bus[10] != -17 || (result = shw_i2c_io_init(), (int)result >= 0) )
{
v1 = shw_get_hw_ver();
if ( v1 <= 329 )
{
*((_WORD *)&all_shw_io + 36) = 259;
*((_WORD *)&all_shw_io + 48) = 260;
*((_WORD *)&all_shw_io + 60) = 261;
*((_WORD *)&all_shw_io + 72) = 262;
*((_WORD *)&all_shw_io + 12) = 257;
*((_QWORD *)&all_shw_io + 4) = "shw_io_reset_n";
*((_QWORD *)&all_shw_io + 5) = &PIN_shw_io_reset_n;
*((_WORD *)&all_shw_io + 24) = 258;
*((_QWORD *)&all_shw_io + 7) = "shw_io_box_fan_en";
*((_QWORD *)&all_shw_io + 8) = &PIN_shw_io_box_fan_en;
*((_QWORD *)&all_shw_io + 10) = "shw_io_box_fan_tacho";
*((_QWORD *)&all_shw_io + 11) = &PIN_shw_io_box_fan_tacho;
*((_QWORD *)&all_shw_io + 13) = "shw_io_fpga_fan_en";
*((_QWORD *)&all_shw_io + 14) = &PIN_shw_io_fpga_fan_en;
*((_QWORD *)&all_shw_io + 16) = "shw_io_led_cpu1";
*((_QWORD *)&all_shw_io + 17) = &PIN_shw_io_led_cpu1;
*((_QWORD *)&all_shw_io + 19) = "shw_io_led_cpu2";
*((_QWORD *)&all_shw_io + 20) = &PIN_shw_io_led_cpu2;
*((_WORD *)&all_shw_io + 84) = 263;
*((_QWORD *)&all_shw_io + 22) = "shw_io_arm_boot_sel";
*((_QWORD *)&all_shw_io + 23) = &PIN_shw_io_arm_boot_sel;
*((_WORD *)&all_shw_io + 120) = 266;
*((_QWORD *)&all_shw_io + 31) = "shw_io_arm_gen_but";
*((_QWORD *)&all_shw_io + 32) = &PIN_shw_io_arm_gen_but;
}
else
{
*((_WORD *)&all_shw_io + 12) = 257;
*((_QWORD *)&all_shw_io + 4) = "shw_io_reset_n";
*((_QWORD *)&all_shw_io + 5) = &PIN_shw_io_reset_n;
*((_WORD *)&all_shw_io + 60) = 261;
*((_QWORD *)&all_shw_io + 16) = "shw_io_led_cpu1";
*((_QWORD *)&all_shw_io + 17) = &PIN_shw_io_led_cpu1;
*((_WORD *)&all_shw_io + 72) = 262;
*((_QWORD *)&all_shw_io + 19) = "shw_io_led_cpu2";
*((_QWORD *)&all_shw_io + 20) = &PIN_shw_io_led_cpu2;
*((_WORD *)&all_shw_io + 84) = 263;
*((_QWORD *)&all_shw_io + 22) = "shw_io_arm_boot_sel";
*((_QWORD *)&all_shw_io + 23) = &PIN_shw_io_arm_boot_sel;
*((_WORD *)&all_shw_io + 96) = 520;
*((_QWORD *)&all_shw_io + 25) = "shw_io_led_state_g";
*((_QWORD *)&all_shw_io + 26) = &I2C_shw_io_led_state_g;
*((_WORD *)&all_shw_io + 108) = 521;
*((_QWORD *)&all_shw_io + 28) = "shw_io_led_state_o";
*((_QWORD *)&all_shw_io + 29) = &I2C_shw_io_led_state_o;
}
trace_printf("shw_io.c", 110LL, 16LL, "version=%d (CPUPWN=%d)", (unsigned int)v1, v1 <= 329);
result = 0LL;
}
}
return result;
}

original_code:
int shw_io_init()
{
 int ret, ver;
 shw_io_t* all_shw_io=(shw_io_t*)_all_shw_io;
 { int ret; if((ret = shw_pio_mmap_init()) < 0) return ret; };
 if(i2c_io_bus.err==-0x11)
  { int ret; if((ret = shw_i2c_io_init()) < 0) return ret; };
 ver=shw_get_hw_ver();
 if(ver<330)
 {
  { all_shw_io[shw_io_reset_n].ID=shw_io_reset_n; all_shw_io[shw_io_reset_n].type=SHW_CPU_PIO; all_shw_io[shw_io_reset_n].name="shw_io_reset_n"; all_shw_io[shw_io_reset_n].ptr=(void*)PIN_shw_io_reset_n; };
  { all_shw_io[shw_io_box_fan_en].ID=shw_io_box_fan_en; all_shw_io[shw_io_box_fan_en].type=SHW_CPU_PIO; all_shw_io[shw_io_box_fan_en].name="shw_io_box_fan_en"; all_shw_io[shw_io_box_fan_en].ptr=(void*)PIN_shw_io_box_fan_en; };
  { all_shw_io[shw_io_box_fan_tacho].ID=shw_io_box_fan_tacho; all_shw_io[shw_io_box_fan_tacho].type=SHW_CPU_PIO; all_shw_io[shw_io_box_fan_tacho].name="shw_io_box_fan_tacho"; all_shw_io[shw_io_box_fan_tacho].ptr=(void*)PIN_shw_io_box_fan_tacho; };
  { all_shw_io[shw_io_fpga_fan_en].ID=shw_io_fpga_fan_en; all_shw_io[shw_io_fpga_fan_en].type=SHW_CPU_PIO; all_shw_io[shw_io_fpga_fan_en].name="shw_io_fpga_fan_en"; all_shw_io[shw_io_fpga_fan_en].ptr=(void*)PIN_shw_io_fpga_fan_en; };
  { all_shw_io[shw_io_led_cpu1].ID=shw_io_led_cpu1; all_shw_io[shw_io_led_cpu1].type=SHW_CPU_PIO; all_shw_io[shw_io_led_cpu1].name="shw_io_led_cpu1"; all_shw_io[shw_io_led_cpu1].ptr=(void*)PIN_shw_io_led_cpu1; };
  { all_shw_io[shw_io_led_cpu2].ID=shw_io_led_cpu2; all_shw_io[shw_io_led_cpu2].type=SHW_CPU_PIO; all_shw_io[shw_io_led_cpu2].name="shw_io_led_cpu2"; all_shw_io[shw_io_led_cpu2].ptr=(void*)PIN_shw_io_led_cpu2; };
  { all_shw_io[shw_io_arm_boot_sel].ID=shw_io_arm_boot_sel; all_shw_io[shw_io_arm_boot_sel].type=SHW_CPU_PIO; all_shw_io[shw_io_arm_boot_sel].name="shw_io_arm_boot_sel"; all_shw_io[shw_io_arm_boot_sel].ptr=(void*)PIN_shw_io_arm_boot_sel; };
  { all_shw_io[shw_io_arm_gen_but].ID=shw_io_arm_gen_but; all_shw_io[shw_io_arm_gen_but].type=SHW_CPU_PIO; all_shw_io[shw_io_arm_gen_but].name="shw_io_arm_gen_but"; all_shw_io[shw_io_arm_gen_but].ptr=(void*)PIN_shw_io_arm_gen_but; };
 }
 else
 {
  { all_shw_io[shw_io_reset_n].ID=shw_io_reset_n; all_shw_io[shw_io_reset_n].type=SHW_CPU_PIO; all_shw_io[shw_io_reset_n].name="shw_io_reset_n"; all_shw_io[shw_io_reset_n].ptr=(void*)PIN_shw_io_reset_n; };
  { all_shw_io[shw_io_led_cpu1].ID=shw_io_led_cpu1; all_shw_io[shw_io_led_cpu1].type=SHW_CPU_PIO; all_shw_io[shw_io_led_cpu1].name="shw_io_led_cpu1"; all_shw_io[shw_io_led_cpu1].ptr=(void*)PIN_shw_io_led_cpu1; };
  { all_shw_io[shw_io_led_cpu2].ID=shw_io_led_cpu2; all_shw_io[shw_io_led_cpu2].type=SHW_CPU_PIO; all_shw_io[shw_io_led_cpu2].name="shw_io_led_cpu2"; all_shw_io[shw_io_led_cpu2].ptr=(void*)PIN_shw_io_led_cpu2; };
  { all_shw_io[shw_io_arm_boot_sel].ID=shw_io_arm_boot_sel; all_shw_io[shw_io_arm_boot_sel].type=SHW_CPU_PIO; all_shw_io[shw_io_arm_boot_sel].name="shw_io_arm_boot_sel"; all_shw_io[shw_io_arm_boot_sel].ptr=(void*)PIN_shw_io_arm_boot_sel; };
  { all_shw_io[shw_io_led_state_g].ID=shw_io_led_state_g; all_shw_io[shw_io_led_state_g].type=SHW_I2C; all_shw_io[shw_io_led_state_g].name="shw_io_led_state_g"; all_shw_io[shw_io_led_state_g].ptr=(void*)I2C_shw_io_led_state_g; };
  { all_shw_io[shw_io_led_state_o].ID=shw_io_led_state_o; all_shw_io[shw_io_led_state_o].type=SHW_I2C; all_shw_io[shw_io_led_state_o].name="shw_io_led_state_o"; all_shw_io[shw_io_led_state_o].ptr=(void*)I2C_shw_io_led_state_o; };
 }
 trace_printf("shw_io.c", 110, 0x10, "version=%d (CPUPWN=%d)",ver, ver<330);;
 return 0;
}

canonical_original_code:
int shw_io_init()
{
 int ret; int ver;
 struct shw_io_t *all_shw_io = (shw_io_t*)_all_shw_io;
 { int ret; if((ret = shw_pio_mmap_init()) < 0) return ret; };
 if(i2c_io_bus.err==-0x11)
  { int ret; if((ret = shw_i2c_io_init()) < 0) return ret; };
 ver=shw_get_hw_ver();
 if(ver<330)
 {
  { all_shw_io[shw_io_reset_n].ID=shw_io_reset_n; all_shw_io[shw_io_reset_n].type=SHW_CPU_PIO; all_shw_io[shw_io_reset_n].name="shw_io_reset_n"; all_shw_io[shw_io_reset_n].ptr=(void *)PIN_shw_io_reset_n; };
  { all_shw_io[shw_io_box_fan_en].ID=shw_io_box_fan_en; all_shw_io[shw_io_box_fan_en].type=SHW_CPU_PIO; all_shw_io[shw_io_box_fan_en].name="shw_io_box_fan_en"; all_shw_io[shw_io_box_fan_en].ptr=(void *)PIN_shw_io_box_fan_en; };
  { all_shw_io[shw_io_box_fan_tacho].ID=shw_io_box_fan_tacho; all_shw_io[shw_io_box_fan_tacho].type=SHW_CPU_PIO; all_shw_io[shw_io_box_fan_tacho].name="shw_io_box_fan_tacho"; all_shw_io[shw_io_box_fan_tacho].ptr=(void *)PIN_shw_io_box_fan_tacho; };
  { all_shw_io[shw_io_fpga_fan_en].ID=shw_io_fpga_fan_en; all_shw_io[shw_io_fpga_fan_en].type=SHW_CPU_PIO; all_shw_io[shw_io_fpga_fan_en].name="shw_io_fpga_fan_en"; all_shw_io[shw_io_fpga_fan_en].ptr=(void *)PIN_shw_io_fpga_fan_en; };
  { all_shw_io[shw_io_led_cpu1].ID=shw_io_led_cpu1; all_shw_io[shw_io_led_cpu1].type=SHW_CPU_PIO; all_shw_io[shw_io_led_cpu1].name="shw_io_led_cpu1"; all_shw_io[shw_io_led_cpu1].ptr=(void *)PIN_shw_io_led_cpu1; };
  { all_shw_io[shw_io_led_cpu2].ID=shw_io_led_cpu2; all_shw_io[shw_io_led_cpu2].type=SHW_CPU_PIO; all_shw_io[shw_io_led_cpu2].name="shw_io_led_cpu2"; all_shw_io[shw_io_led_cpu2].ptr=(void *)PIN_shw_io_led_cpu2; };
  { all_shw_io[shw_io_arm_boot_sel].ID=shw_io_arm_boot_sel; all_shw_io[shw_io_arm_boot_sel].type=SHW_CPU_PIO; all_shw_io[shw_io_arm_boot_sel].name="shw_io_arm_boot_sel"; all_shw_io[shw_io_arm_boot_sel].ptr=(void *)PIN_shw_io_arm_boot_sel; };
  { all_shw_io[shw_io_arm_gen_but].ID=shw_io_arm_gen_but; all_shw_io[shw_io_arm_gen_but].type=SHW_CPU_PIO; all_shw_io[shw_io_arm_gen_but].name="shw_io_arm_gen_but"; all_shw_io[shw_io_arm_gen_but].ptr=(void *)PIN_shw_io_arm_gen_but; };
 }
 else
 {
  { all_shw_io[shw_io_reset_n].ID=shw_io_reset_n; all_shw_io[shw_io_reset_n].type=SHW_CPU_PIO; all_shw_io[shw_io_reset_n].name="shw_io_reset_n"; all_shw_io[shw_io_reset_n].ptr=(void *)PIN_shw_io_reset_n; };
  { all_shw_io[shw_io_led_cpu1].ID=shw_io_led_cpu1; all_shw_io[shw_io_led_cpu1].type=SHW_CPU_PIO; all_shw_io[shw_io_led_cpu1].name="shw_io_led_cpu1"; all_shw_io[shw_io_led_cpu1].ptr=(void *)PIN_shw_io_led_cpu1; };
  { all_shw_io[shw_io_led_cpu2].ID=shw_io_led_cpu2; all_shw_io[shw_io_led_cpu2].type=SHW_CPU_PIO; all_shw_io[shw_io_led_cpu2].name="shw_io_led_cpu2"; all_shw_io[shw_io_led_cpu2].ptr=(void *)PIN_shw_io_led_cpu2; };
  { all_shw_io[shw_io_arm_boot_sel].ID=shw_io_arm_boot_sel; all_shw_io[shw_io_arm_boot_sel].type=SHW_CPU_PIO; all_shw_io[shw_io_arm_boot_sel].name="shw_io_arm_boot_sel"; all_shw_io[shw_io_arm_boot_sel].ptr=(void *)PIN_shw_io_arm_boot_sel; };
  { all_shw_io[shw_io_led_state_g].ID=shw_io_led_state_g; all_shw_io[shw_io_led_state_g].type=SHW_I2C; all_shw_io[shw_io_led_state_g].name="shw_io_led_state_g"; all_shw_io[shw_io_led_state_g].ptr=(void *)I2C_shw_io_led_state_g; };
  { all_shw_io[shw_io_led_state_o].ID=shw_io_led_state_o; all_shw_io[shw_io_led_state_o].type=SHW_I2C; all_shw_io[shw_io_led_state_o].name="shw_io_led_state_o"; all_shw_io[shw_io_led_state_o].ptr=(void *)I2C_shw_io_led_state_o; };
 }
 trace_printf("shw_io.c", 110, 0x10, "version=%d (CPUPWN=%d)",ver, ver<330);;
 return 0;
}

variable_types: ret int

variable_types: ver int

variable_types: struct shw_io_t { unsigned char ID; unsigned char type; char *name; void *ptr; } *


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

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

original_code:
int shw_io_configure_all()
{
 int i;
 const shw_io_bus_t *iobus;
 const shw_io_t* all_io=(shw_io_t*)_all_shw_io;
 for(i=0;i<NUM_SHW_IO_ID;i++)
 {
  const shw_io_t* io=(const shw_io_t*)&_all_shw_io[i];
  switch(io->type)
  {
   case SHW_CPU_PIO:
    shw_pio_configure(all_io[i].ptr);
    break;
   case SHW_I2C:
    iobus = (const shw_io_bus_t *)io->ptr;
    if(iobus->chip && iobus->chip->type==1)
     wrswhw_pca9554_configure(iobus->chip->bus,iobus->chip->addr,iobus->chip->config);
    break;
   case SHW_UNDEF:
    break;
   default:
    trace_printf("shw_io.c", 137, 0x10,"Config not implemented for type %d for io #%d",io->type,i);; break;
  }
 }
 return 0;
}

canonical_original_code:
int shw_io_configure_all()
{
 int i;
 struct shw_io_bus_t *iobus;
 struct shw_io_t *all_io = (shw_io_t*)_all_shw_io;
 for(i=0;i<NUM_SHW_IO_ID;i++)
 {
  struct shw_io_t *io = (const shw_io_t*)&_all_shw_io[i];
  switch(io->type)
  {
   case SHW_CPU_PIO:
    shw_pio_configure(all_io[i].ptr);
    break;
   case SHW_I2C:
    iobus = (struct shw_io_bus_t *)io->ptr;
    if(iobus->chip && iobus->chip->type==1)
     wrswhw_pca9554_configure(iobus->chip->bus,iobus->chip->addr,iobus->chip->config);
    break;
   case SHW_UNDEF:
    break;
   default:
    trace_printf("shw_io.c", 137, 0x10,"Config not implemented for type %d for io #%d",io->type,i);; break;
  }
 }
 return 0;
}

variable_types: struct shw_io_bus_t { struct shw_chip_t { void *bus; unsigned int addr; unsigned int config; unsigned int type; } *chip; unsigned char mask; unsigned char shift; } *

user_defined_types: struct shw_io_bus_t { struct shw_chip_t *chip; unsigned char mask; unsigned char shift; }

user_defined_types: struct shw_io_t { unsigned char ID; unsigned char type; char *name; void *ptr; }


decompiled_code:
__int64 __fastcall get_shw_io(unsigned int a1)
{
if ( a1 - 1 > 9 )
trace_printf("shw_io.c", 159LL, 2LL, "IO %d does not exist", a1);
else
trace_printf("shw_io.c", 157LL, 2LL, "IO %d does not correspond to its ID %s", a1, 0LL);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1)
{
if ( a1 - 1 > 9 )
trace_printf("shw_io.c", 159LL, 2LL, "IO %d does not exist", a1);
else
trace_printf("shw_io.c", 157LL, 2LL, "IO %d does not correspond to its ID %s", a1, 0LL);
return 0LL;
}

original_code:
const shw_io_t* get_shw_io(shw_io_id_t id)
{
 if(0< id && id < NUM_SHW_IO_ID)
 {
  if(_all_shw_io[id].ID==id) return &(_all_shw_io[id]);
  else trace_printf("shw_io.c", 157, 2,"IO %d does not correspond to its ID %s",id,_all_shw_io[id].name);;
 }
 else trace_printf("shw_io.c", 159, 2,"IO %d does not exist",id);;
 return 0;
}

canonical_original_code:
struct shw_io_t *get_shw_io(enum shw_io_id_t id)
{
 if(0< id && id < NUM_SHW_IO_ID)
 {
  if(_all_shw_io[id].ID==id) return &(_all_shw_io[id]);
  else trace_printf("shw_io.c", 157, 2,"IO %d does not correspond to its ID %s",id,_all_shw_io[id].name);;
 }
 else trace_printf("shw_io.c", 159, 2,"IO %d does not exist",id);;
 return 0;
}

variable_types: enum shw_io_id_t { shw_io_reset_n=1, shw_io_box_fan_en=2, shw_io_box_fan_tacho=3, shw_io_fpga_fan_en=4, shw_io_led_cpu1=5, shw_io_led_cpu2=6, shw_io_arm_boot_sel=7, shw_io_led_state_g=8, shw_io_led_state_o=9, shw_io_arm_gen_but=10, NUM_SHW_IO_ID=11 }

return_type: struct shw_io_t { unsigned char ID; unsigned char type; char *name; void *ptr; } *


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

result = get_shw_io(a1);
if ( result )
{
if ( *(_BYTE *)(result + 1) == 1 )
result = *(_QWORD *)(result + 16);
}
return result;
}

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

result = func2(a1);
if ( result )
{
if ( *(_BYTE *)(result + 1) == 1 )
result = *(_QWORD *)(result + 16);
}
return result;
}

original_code:
const pio_pin_t* get_pio_pin(shw_io_id_t id)
{
 const shw_io_t *wrpin=get_shw_io(id);
 if(wrpin && wrpin->type==SHW_CPU_PIO)
  return (wrpin)?(const pio_pin_t*)wrpin->ptr:0;
}

canonical_original_code:
struct pio_pin *get_pio_pin(enum shw_io_id_t id)
{
 struct shw_io_t *wrpin = get_shw_io(id);
 if(wrpin && wrpin->type==SHW_CPU_PIO)
  return (wrpin)?(struct pio_pin *)wrpin->ptr:0;
}

return_type: struct pio_pin { int port; int pin; int mode; int dir; } *

user_defined_types: enum shw_io_id_t { shw_io_reset_n=1, shw_io_box_fan_en=2, shw_io_box_fan_tacho=3, shw_io_fpga_fan_en=4, shw_io_led_cpu1=5, shw_io_led_cpu2=6, shw_io_arm_boot_sel=7, shw_io_led_state_g=8, shw_io_led_state_o=9, shw_io_arm_gen_but=10, NUM_SHW_IO_ID=11 }


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

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

original_code:
uint32_t shw_io_read(shw_io_id_t id)
{
 uint32_t ret;
 int32_t i32data;
 uint8_t u8data[2];
 const shw_io_t* io=&_all_shw_io[id];
 const shw_io_bus_t *iobus;
 if(0< id && id < NUM_SHW_IO_ID && io->ID==id && io->ptr)
 {
  switch(io->type)
  {
  case SHW_CPU_PIO:
   return shw_pio_get((const pio_pin_t*)io->ptr);
  case SHW_I2C:
   iobus = (const shw_io_bus_t *)io->ptr;
   if(iobus->chip && iobus->chip->type==1)
   {
    i32data = wrswhw_pca9554_get_input(iobus->chip->bus,iobus->chip->addr);
    return ((i32data & iobus->mask) >> iobus->shift);
   }
  case SHW_UNDEF:
   trace_printf("shw_io.c", 186, 2,"IO #%d is undef",id);; break;
  default:
   trace_printf("shw_io.c", 188, 2,"Unknow type %d for io #%d",io->type,id);; break;
  }
 }
 return ret;
}

canonical_original_code:
unsigned int shw_io_read(enum shw_io_id_t id)
{
 unsigned int ret;
 int i32data;
 unsigned char u8data[2];
 struct shw_io_t *io = &_all_shw_io[id];
 struct shw_io_bus_t *iobus;
 if(0< id && id < NUM_SHW_IO_ID && io->ID==id && io->ptr)
 {
  switch(io->type)
  {
  case SHW_CPU_PIO:
   return shw_pio_get((struct pio_pin *)io->ptr);
  case SHW_I2C:
   iobus = (struct shw_io_bus_t *)io->ptr;
   if(iobus->chip && iobus->chip->type==1)
   {
    i32data = wrswhw_pca9554_get_input(iobus->chip->bus,iobus->chip->addr);
    return ((i32data & iobus->mask) >> iobus->shift);
   }
  case SHW_UNDEF:
   trace_printf("shw_io.c", 186, 2,"IO #%d is undef",id);; break;
  default:
   trace_printf("shw_io.c", 188, 2,"Unknow type %d for io #%d",io->type,id);; break;
  }
 }
 return ret;
}

variable_types:
ret
unsigned int

user_defined_types: struct shw_chip_t { void *bus; unsigned int addr; unsigned int config; unsigned int type; }


decompiled_code:
__int64 shw_io_write()
{
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func5()
{
return 0xFFFFFFFFLL;
}

original_code:
int shw_io_write(shw_io_id_t id, uint32_t value)
{
 int ret=-1;
 int32_t i32data;
 uint8_t u8data[2];
 const shw_io_t* io=&_all_shw_io[id];
 const shw_io_bus_t *iobus;
 if(0< id && id < NUM_SHW_IO_ID && io->ID==id && io->ptr)
 {
  switch(io->type)
  {
  case SHW_CPU_PIO:
   shw_pio_set((const pio_pin_t*)io->ptr,value);
   return 0;
  case SHW_I2C:
   iobus = (const shw_io_bus_t *)io->ptr;
   if(iobus->chip && iobus->chip->type==1)
   {
    i32data = wrswhw_pca9554_get_input(iobus->chip->bus,iobus->chip->addr);
    i32data &= ~iobus->mask;
    value <<= iobus->shift;
    value &= iobus->mask;
    return wrswhw_pca9554_set_output_reg(iobus->chip->bus,iobus->chip->addr,value | i32data);
   }
  case SHW_UNDEF:
   trace_printf("shw_io.c", 220, 2,"Pin #%d is undef",id);; break;
  default:
   trace_printf("shw_io.c", 222, 2,"Unknow type %d for io #%d",io->type,id);; break;
  }
 }
 return -1;
}

canonical_original_code:
int shw_io_write(enum shw_io_id_t id, unsigned int value)
{
 int ret = -1;
 int i32data;
 unsigned char u8data[2];
 struct shw_io_t *io = &_all_shw_io[id];
 struct shw_io_bus_t *iobus;
 if(0< id && id < NUM_SHW_IO_ID && io->ID==id && io->ptr)
 {
  switch(io->type)
  {
  case SHW_CPU_PIO:
   shw_pio_set((struct pio_pin *)io->ptr,value);
   return 0;
  case SHW_I2C:
   iobus = (struct shw_io_bus_t *)io->ptr;
   if(iobus->chip && iobus->chip->type==1)
   {
    i32data = wrswhw_pca9554_get_input(iobus->chip->bus,iobus->chip->addr);
    i32data &= ~iobus->mask;
    value <<= iobus->shift;
    value &= iobus->mask;
    return wrswhw_pca9554_set_output_reg(iobus->chip->bus,iobus->chip->addr,value | i32data);
   }
  case SHW_UNDEF:
   trace_printf("shw_io.c", 220, 2,"Pin #%d is undef",id);; break;
  default:
   trace_printf("shw_io.c", 222, 2,"Unknow type %d for io #%d",io->type,id);; break;
  }
 }
 return -1;
}

variable_types:
value
unsigned int


decompiled_code:
const char *__fastcall get_shw_info(char a1)
{
const char *result; // rax
int v2; // eax
bool v3; // zf

if ( a1 == 102 )
{
v3 = (unsigned __int8)shw_get_fpga_type() == 0xF0;
result = "LX240T";
if ( !v3 )
result = "LX130T";
}
else
{
result = (const char *)&unk_4A5;
if ( a1 == 112 )
{
v2 = shw_get_hw_ver();
snprintf(str_hwver_0, 0xAuLL, "%d.%d", (unsigned int)(v2 / 100), (unsigned int)(v2 % 100));
result = str_hwver_0;
}
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func6(char a1)
{
const char *result; // rax
int v2; // eax
bool v3; // zf

if ( a1 == 102 )
{
v3 = (unsigned __int8)shw_get_fpga_type() == 0xF0;
result = "LX240T";
if ( !v3 )
result = "LX130T";
}
else
{
result = (const char *)&unk_4A5;
if ( a1 == 112 )
{
v2 = shw_get_hw_ver();
snprintf(str_hwver_0, 0xAuLL, "%d.%d", (unsigned int)(v2 / 100), (unsigned int)(v2 % 100));
result = str_hwver_0;
}
}
return result;
}

original_code:
const char *get_shw_info(const char cmd)
{
 static char str_hwver[10];
 int tmp;
 switch(cmd)
 {
 case 'p':
  tmp = shw_get_hw_ver();
  snprintf(str_hwver,10,"%d.%d",tmp/100,tmp%100);
  return str_hwver;
 case 'f':
  return (shw_get_fpga_type()==240)?"LX240T":"LX130T";
 }
 return "";
}

canonical_original_code:
char *get_shw_info(char cmd)
{
 static char str_hwver[10];
 int tmp;
 switch(cmd)
 {
 case 'p':
  tmp = shw_get_hw_ver();
  snprintf(str_hwver,10,"%d.%d",tmp/100,tmp%100);
  return str_hwver;
 case 'f':
  return (shw_get_fpga_type()==240)?"LX240T":"LX130T";
 }
 return "";
}

variable_types: cmd char

variable_types: tmp int


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

do
result = *(unsigned int *)(fpga_base_virt + (unsigned int)(a1 + 16));
while ( (result & 2) != 0 );
return result;
}

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

do
result = *(unsigned int *)(fpga_base_virt + (unsigned int)(a1 + 16));
while ( (result & 2) != 0 );
return result;
}

original_code:
static void mi2c_wait_busy(uint32_t fpga_address)
{
    while ((*(volatile uint32_t *)(_fpga_base_virt + (fpga_address + 0x10))) & (1<<1));
}

canonical_original_code:
static void mi2c_wait_busy(unsigned int fpga_address)
{
    while ((*(unsigned int *)(_fpga_base_virt + (fpga_address + 0x10))) & (1<<1));
}


decompiled_code:
__int64 __fastcall mi2c_read_byte(int a1, int a2)
{
*(_DWORD *)(fpga_base_virt + (unsigned int)(a1 + 16)) = a2 == 0 ? 32 : 104;
mi2c_wait_busy(a1);
return *(unsigned int *)(fpga_base_virt + (unsigned int)(a1 + 12));
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, int a2)
{
*(_DWORD *)(fpga_base_virt + (unsigned int)(a1 + 16)) = a2 == 0 ? 32 : 104;
func0(a1);
return *(unsigned int *)(fpga_base_virt + (unsigned int)(a1 + 12));
}

original_code:
static int mi2c_read_byte(uint32_t fpga_address, uint32_t last)
{
    uint32_t cmd = (1<<5);
    if (last)
 cmd |= (1<<6) | (1<<3);
    { *(volatile uint32_t *)(_fpga_base_virt + (fpga_address + 0x10)) = (cmd); };
    mi2c_wait_busy(fpga_address);
    return (*(volatile uint32_t *)(_fpga_base_virt + (fpga_address + 0x0C)));
}

canonical_original_code:
static int mi2c_read_byte(unsigned int fpga_address, unsigned int last)
{
    unsigned int cmd = (1<<5);
    if (last)
 cmd |= (1<<6) | (1<<3);
    { *(unsigned int *)(_fpga_base_virt + (fpga_address + 0x10)) = (cmd); };
    mi2c_wait_busy(fpga_address);
    return (*(unsigned int *)(_fpga_base_virt + (fpga_address + 0x0C)));
}

variable_types:
last
unsigned int

variable_types:
cmd
unsigned int


decompiled_code:
__int64 __fastcall i2c_fpga_reg_scan(__int64 a1, char a2)
{
int v2; // edi
__int64 result; // rax
__int64 v4; // rax

if ( !a1 )
return 4294967279LL;
if ( *(_DWORD *)(a1 + 8) != 1 )
return 4294967278LL;
v2 = **(_DWORD **)(a1 + 16);
result = mi2c_start(v2, a2, 1);
if ( (_DWORD)result )
{
v4 = fpga_base_virt;
*(_DWORD *)(fpga_base_virt + (unsigned int)(v2 + 12)) = 0;
*(_DWORD *)((unsigned int)(v2 + 16) + v4) = 80;
mi2c_wait_busy(v2);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, char a2)
{
int v2; // edi
__int64 result; // rax
__int64 v4; // rax

if ( !a1 )
return 4294967279LL;
if ( *(_DWORD *)(a1 + 8) != 1 )
return 4294967278LL;
v2 = **(_DWORD **)(a1 + 16);
result = func1(v2, a2, 1);
if ( (_DWORD)result )
{
v4 = fpga_base_virt;
*(_DWORD *)(fpga_base_virt + (unsigned int)(v2 + 12)) = 0;
*(_DWORD *)((unsigned int)(v2 + 16) + v4) = 80;
func0(v2);
result = 1LL;
}
return result;
}

original_code:
int32_t i2c_fpga_reg_scan(struct i2c_bus* bus, uint32_t i2c_address)
{
    if (!bus)
        return -0x11;
    if (bus->type != 1)
 return -0x12;
    i2c_fpga_reg_t* ts = (i2c_fpga_reg_t*)bus->type_specific;
    uint32_t fpga_address = ts->base_address;
    int ack = mi2c_start(fpga_address, i2c_address, 1);
    if (ack) {
        { *(volatile uint32_t *)(_fpga_base_virt + (fpga_address + 0x0C)) = (0); };
        { *(volatile uint32_t *)(_fpga_base_virt + (fpga_address + 0x10)) = ((1<<6) | (1<<4)); };
        mi2c_wait_busy(fpga_address);
       return 1;
    }
    return 0;
}

canonical_original_code:
int i2c_fpga_reg_scan(struct i2c_bus *bus, unsigned int i2c_address)
{
    if (!bus)
        return -0x11;
    if (bus->type != 1)
 return -0x12;
    struct i2c_fpga_reg_t *ts = (i2c_fpga_reg_t*)bus->type_specific;
    unsigned int fpga_address = ts->base_address;
    int ack = mi2c_start(fpga_address, i2c_address, 1);
    if (ack) {
        { *(unsigned int *)(_fpga_base_virt + (fpga_address + 0x0C)) = (0); };
        { *(unsigned int *)(_fpga_base_virt + (fpga_address + 0x10)) = ((1<<6) | (1<<4)); };
        mi2c_wait_busy(fpga_address);
       return 1;
    }
    return 0;
}

variable_types: struct i2c_bus { char *name; int type; void *type_specific; int (*transfer)(struct i2c_bus *bus, unsigned int address, unsigned int to_write, unsigned int to_read, unsigned char *data); int (*scan)(struct i2c_bus *bus, unsigned int address); int err; } *

variable_types: struct i2c_fpga_reg_t { unsigned int base_address; unsigned int prescaler; } *

variable_types: ack int


decompiled_code:
__int64 __fastcall i2c_fpga_reg_transfer(__int64 a1, char a2, int a3, int a4)
{
unsigned int v5; // er12
int v6; // edi
unsigned __int8 *v8; // r8
unsigned int v9; // er11
unsigned int v10; // er13
unsigned __int8 *v11; // r10
int v12; // er9
int v13; // er9
__int64 v14; // r10
int v15; // er11
int v16; // er10
__int64 v17; // rcx
char v18; // al
__int64 v19; // rcx
__int64 v20; // r8
__int64 v21; // r9

if ( !a1 )
return (unsigned int)-17;
if ( *(_DWORD *)(a1 + 8) != 1 )
return (unsigned int)-18;
v5 = 0;
v6 = **(_DWORD **)(a1 + 16);
if ( !a3 )
{
LABEL_4:
if ( !a4 )
return v5;
if ( (unsigned int)mi2c_start(v6, a2, 1) )
{
v16 = a4 - 1;
LODWORD(v17) = 0;
do
{
v18 = mi2c_read_byte(v6, v16 == v17);
*(_BYTE *)(v20 + v19) = v18;
v17 = v19 + 1;
}
while ( v21 != v17 );
return a4 + v5;
}
return (unsigned int)-1;
}
if ( !(unsigned int)mi2c_start(v6, a2, 0) )
return (unsigned int)-1;
v10 = v9 - 1;
v11 = v8;
v5 = v9;
v12 = 0;
while ( (unsigned int)mi2c_write_byte(v6, *v11, v10 == v12) )
{
v12 = v13 + 1;
v11 = (unsigned __int8 *)(v14 + 1);
if ( v12 == v15 )
goto LABEL_4;
}
return 4294967294LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, char a2, int a3, int a4)
{
unsigned int v5; // er12
int v6; // edi
unsigned __int8 *v8; // r8
unsigned int v9; // er11
unsigned int v10; // er13
unsigned __int8 *v11; // r10
int v12; // er9
int v13; // er9
__int64 v14; // r10
int v15; // er11
int v16; // er10
__int64 v17; // rcx
char v18; // al
__int64 v19; // rcx
__int64 v20; // r8
__int64 v21; // r9

if ( !a1 )
return (unsigned int)-17;
if ( *(_DWORD *)(a1 + 8) != 1 )
return (unsigned int)-18;
v5 = 0;
v6 = **(_DWORD **)(a1 + 16);
if ( !a3 )
{
LABEL_4:
if ( !a4 )
return v5;
if ( (unsigned int)func1(v6, a2, 1) )
{
v16 = a4 - 1;
LODWORD(v17) = 0;
do
{
v18 = func3(v6, v16 == v17);
*(_BYTE *)(v20 + v19) = v18;
v17 = v19 + 1;
}
while ( v21 != v17 );
return a4 + v5;
}
return (unsigned int)-1;
}
if ( !(unsigned int)func1(v6, a2, 0) )
return (unsigned int)-1;
v10 = v9 - 1;
v11 = v8;
v5 = v9;
v12 = 0;
while ( (unsigned int)func2(v6, *v11, v10 == v12) )
{
v12 = v13 + 1;
v11 = (unsigned __int8 *)(v14 + 1);
if ( v12 == v15 )
goto LABEL_4;
}
return 4294967294LL;
}

original_code:
int32_t i2c_fpga_reg_transfer(struct i2c_bus* bus, uint32_t i2c_address, uint32_t to_write, uint32_t to_read, uint8_t* data)
{
    if (!bus)
 return -0x11;
    if (bus->type != 1)
 return -0x12;
    i2c_fpga_reg_t* ts = (i2c_fpga_reg_t*)bus->type_specific;
    uint32_t fpga_address = ts->base_address;
    int ack = 0;
    int sent = 0;
    int rcvd = 0;
    int b;
    if (to_write)
    {
     ack = mi2c_start(fpga_address, i2c_address, 0);
     if (!ack)
         return -1;
     int last = to_write-1;
     for (b = 0; b < to_write; b++)
     {
         ack = mi2c_write_byte(fpga_address, data[b], b == last);
         if (!ack)
      return -2;
         sent++;
     }
    }
    if (to_read)
    {
 ack = mi2c_start(fpga_address, i2c_address, 1);
 if (!ack)
         return -1;
     int last = to_read-1;
     for (b = 0; b < to_read; b++)
     {
         data[b] = mi2c_read_byte(fpga_address, b == last);
         rcvd++;
     }
    }
    return sent + rcvd;
}

canonical_original_code:
int i2c_fpga_reg_transfer(struct i2c_bus *bus, unsigned int i2c_address, unsigned int to_write, unsigned int to_read, unsigned char *data)
{
    if (!bus)
 return -0x11;
    if (bus->type != 1)
 return -0x12;
    struct i2c_fpga_reg_t *ts = (i2c_fpga_reg_t*)bus->type_specific;
    unsigned int fpga_address = ts->base_address;
    int ack = 0;
    int sent = 0;
    int rcvd = 0;
    int b;
    if (to_write)
    {
     ack = mi2c_start(fpga_address, i2c_address, 0);
     if (!ack)
         return -1;
     int last = to_write-1;
     for (b = 0; b < to_write; b++)
     {
         ack = mi2c_write_byte(fpga_address, data[b], b == last);
         if (!ack)
      return -2;
         sent++;
     }
    }
    if (to_read)
    {
 ack = mi2c_start(fpga_address, i2c_address, 1);
 if (!ack)
         return -1;
     int last = to_read-1;
     for (b = 0; b < to_read; b++)
     {
         data[b] = mi2c_read_byte(fpga_address, b == last);
         rcvd++;
     }
    }
    return sent + rcvd;
}

variable_types:
data
unsigned char *

variable_types: sent int

variable_types: rcvd int

variable_types: b int

variable_types: last int

user_defined_types: struct i2c_fpga_reg_t { unsigned int base_address; unsigned int prescaler; }


decompiled_code:
__int64 __fastcall i2c_fpga_reg_init_bus(__int64 a1)
{
unsigned int *v1; // rax
__int64 v2; // rdx
int v3; // ecx
__int64 v4; // rdx
__int64 result; // rax

v1 = *(unsigned int **)(a1 + 16);
if ( v1 )
{
v2 = *(unsigned int *)(a1 + 8);
if ( (_DWORD)v2 == 1 )
{
v3 = *v1;
v4 = fpga_base_virt;
*(_QWORD *)(a1 + 24) = i2c_fpga_reg_transfer;
*(_QWORD *)(a1 + 32) = i2c_fpga_reg_scan;
*(_DWORD *)(v4 + (unsigned int)(v3 + 8)) = 0;
*(_DWORD *)(v4 + *v1) = *((unsigned __int8 *)v1 + 4);
*(_DWORD *)(v4 + *v1 + 4) = v1[1] >> 8;
*(_DWORD *)(v4 + *v1 + 8) = 128;
if ( (*(_DWORD *)(*v1 + 8 + v4) & 0x80u) == 0 )
{
puts("failed to read from control register");
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
else
{
printf("type doesn't match I2C_BUS_TYPE_FPGA_REG(%d): %d\n", 1LL, v2);
result = 0xFFFFFFFFLL;
}
}
else
{
puts("no type specific structure provided");
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
unsigned int *v1; // rax
__int64 v2; // rdx
int v3; // ecx
__int64 v4; // rdx
__int64 result; // rax

v1 = *(unsigned int **)(a1 + 16);
if ( v1 )
{
v2 = *(unsigned int *)(a1 + 8);
if ( (_DWORD)v2 == 1 )
{
v3 = *v1;
v4 = fpga_base_virt;
*(_QWORD *)(a1 + 24) = i2c_fpga_reg_transfer;
*(_QWORD *)(a1 + 32) = i2c_fpga_reg_scan;
*(_DWORD *)(v4 + (unsigned int)(v3 + 8)) = 0;
*(_DWORD *)(v4 + *v1) = *((unsigned __int8 *)v1 + 4);
*(_DWORD *)(v4 + *v1 + 4) = v1[1] >> 8;
*(_DWORD *)(v4 + *v1 + 8) = 128;
if ( (*(_DWORD *)(*v1 + 8 + v4) & 0x80u) == 0 )
{
puts("failed to read from control register");
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
else
{
printf("type doesn't match I2C_BUS_TYPE_FPGA_REG(%d): %d\n", 1LL, v2);
result = 0xFFFFFFFFLL;
}
}
else
{
puts("no type specific structure provided");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int i2c_fpga_reg_init_bus(struct i2c_bus *bus)
{
    i2c_fpga_reg_t *priv;
    if (!bus->type_specific) {
     printf("no type specific structure provided\n");
     return -1;
    }
    if (bus->type != 1) {
     printf("type doesn't match I2C_BUS_TYPE_FPGA_REG(%d): %d\n", 1, bus->type);
 return -1;
    }
    priv = (i2c_fpga_reg_t *)bus->type_specific;
    bus->transfer = i2c_fpga_reg_transfer;
    bus->scan = i2c_fpga_reg_scan;
    { *(volatile uint32_t *)(_fpga_base_virt + (priv->base_address + 0x08)) = (0); };
    { *(volatile uint32_t *)(_fpga_base_virt + (priv->base_address + 0x00)) = (priv->prescaler & 0xFF); };
    { *(volatile uint32_t *)(_fpga_base_virt + (priv->base_address + 0x04)) = (priv->prescaler >> 8); };
    { *(volatile uint32_t *)(_fpga_base_virt + (priv->base_address + 0x08)) = ((1<<7)); };
    if (!((*(volatile uint32_t *)(_fpga_base_virt + (priv->base_address + 0x08))) & (1<<7))) {
     printf("failed to read from control register\n");
 return -1;
    }
    return 0;
}

canonical_original_code:
int i2c_fpga_reg_init_bus(struct i2c_bus *bus)
{
    struct i2c_fpga_reg_t *priv;
    if (!bus->type_specific) {
     printf("no type specific structure provided\n");
     return -1;
    }
    if (bus->type != 1) {
     printf("type doesn't match I2C_BUS_TYPE_FPGA_REG(%d): %d\n", 1, bus->type);
 return -1;
    }
    priv = (struct i2c_fpga_reg_t *)bus->type_specific;
    bus->transfer = i2c_fpga_reg_transfer;
    bus->scan = i2c_fpga_reg_scan;
    { *(unsigned int *)(_fpga_base_virt + (priv->base_address + 0x08)) = (0); };
    { *(unsigned int *)(_fpga_base_virt + (priv->base_address + 0x00)) = (priv->prescaler & 0xFF); };
    { *(unsigned int *)(_fpga_base_virt + (priv->base_address + 0x04)) = (priv->prescaler >> 8); };
    { *(unsigned int *)(_fpga_base_virt + (priv->base_address + 0x08)) = ((1<<7)); };
    if (!((*(unsigned int *)(_fpga_base_virt + (priv->base_address + 0x08))) & (1<<7))) {
     printf("failed to read from control register\n");
 return -1;
    }
    return 0;
}


decompiled_code:
__int64 __fastcall mi2c_start(int a1, char a2, int a3)
{
int v3; // esi
_DWORD *v4; // rax
__int64 v5; // rcx

v3 = (unsigned __int8)(2 * a2);
if ( a3 == 1 )
v3 |= 1u;
v4 = (_DWORD *)((unsigned int)(a1 + 16) + fpga_base_virt);
*(_DWORD *)(fpga_base_virt + (unsigned int)(a1 + 12)) = v3;
*v4 = 144;
mi2c_wait_busy(a1);
return ((unsigned __int8)(*(_DWORD *)(fpga_base_virt + v5) >> 7) ^ 1) & 1;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, char a2, int a3)
{
int v3; // esi
_DWORD *v4; // rax
__int64 v5; // rcx

v3 = (unsigned __int8)(2 * a2);
if ( a3 == 1 )
v3 |= 1u;
v4 = (_DWORD *)((unsigned int)(a1 + 16) + fpga_base_virt);
*(_DWORD *)(fpga_base_virt + (unsigned int)(a1 + 12)) = v3;
*v4 = 144;
func0(a1);
return ((unsigned __int8)(*(_DWORD *)(fpga_base_virt + v5) >> 7) ^ 1) & 1;
}

original_code:
static int mi2c_start(uint32_t fpga_address, uint32_t i2c_address, uint32_t mode)
{
    i2c_address = (i2c_address << 1) & 0xFF;
    if (mode == 1)
 i2c_address |= 1;
    { *(volatile uint32_t *)(_fpga_base_virt + (fpga_address + 0x0C)) = (i2c_address); };
    { *(volatile uint32_t *)(_fpga_base_virt + (fpga_address + 0x10)) = ((1<<7) | (1<<4)); };
    mi2c_wait_busy(fpga_address);
    uint32_t result = (*(volatile uint32_t *)(_fpga_base_virt + (fpga_address + 0x10)));
    return (result & (1<<7)) ? 0 : 1;
}

canonical_original_code:
static int mi2c_start(unsigned int fpga_address, unsigned int i2c_address, unsigned int mode)
{
    i2c_address = (i2c_address << 1) & 0xFF;
    if (mode == 1)
 i2c_address |= 1;
    { *(unsigned int *)(_fpga_base_virt + (fpga_address + 0x0C)) = (i2c_address); };
    { *(unsigned int *)(_fpga_base_virt + (fpga_address + 0x10)) = ((1<<7) | (1<<4)); };
    mi2c_wait_busy(fpga_address);
    unsigned int result = (*(volatile uint32_t *)(_fpga_base_virt + (fpga_address + 0x10)));
    return (result & (1<<7)) ? 0 : 1;
}

variable_types:
mode
unsigned int

variable_types:
result
unsigned int


decompiled_code:
__int64 __fastcall mi2c_write_byte(int a1, unsigned __int8 a2, int a3)
{
bool v3; // cf
__int64 v4; // rdx
__int64 v5; // rcx

v3 = a3 == 0;
v4 = fpga_base_virt;
*(_DWORD *)(fpga_base_virt + (unsigned int)(a1 + 12)) = a2;
*(_DWORD *)((unsigned int)(a1 + 16) + v4) = v3 ? 16 : 80;
mi2c_wait_busy(a1);
return ((unsigned __int8)(*(_DWORD *)(fpga_base_virt + v5) >> 7) ^ 1) & 1;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, unsigned __int8 a2, int a3)
{
bool v3; // cf
__int64 v4; // rdx
__int64 v5; // rcx

v3 = a3 == 0;
v4 = fpga_base_virt;
*(_DWORD *)(fpga_base_virt + (unsigned int)(a1 + 12)) = a2;
*(_DWORD *)((unsigned int)(a1 + 16) + v4) = v3 ? 16 : 80;
func0(a1);
return ((unsigned __int8)(*(_DWORD *)(fpga_base_virt + v5) >> 7) ^ 1) & 1;
}

original_code:
static int mi2c_write_byte(uint32_t fpga_address, uint8_t data, uint32_t last)
{
    uint32_t cmd = (1<<4);
    if (last)
 cmd |= (1<<6);
    { *(volatile uint32_t *)(_fpga_base_virt + (fpga_address + 0x0C)) = (data); };
    { *(volatile uint32_t *)(_fpga_base_virt + (fpga_address + 0x10)) = (cmd); };
    mi2c_wait_busy(fpga_address);
    return !((*(volatile uint32_t *)(_fpga_base_virt + (fpga_address + 0x10))) & (1<<7));
}

canonical_original_code:
static int mi2c_write_byte(unsigned int fpga_address, unsigned char data, unsigned int last)
{
    unsigned int cmd = (1<<4);
    if (last)
 cmd |= (1<<6);
    { *(unsigned int *)(_fpga_base_virt + (fpga_address + 0x0C)) = (data); };
    { *(unsigned int *)(_fpga_base_virt + (fpga_address + 0x10)) = (cmd); };
    mi2c_wait_busy(fpga_address);
    return !((*(unsigned int *)(_fpga_base_virt + (fpga_address + 0x10))) & (1<<7));
}

variable_types:
data
unsigned char


decompiled_code:
double __fastcall nmea_atof(_DWORD *a1, int a2)
{
char *endptr; // [rsp+0h] [rbp-90h] BYREF
char nptr[136]; // [rsp+8h] [rbp-88h] BYREF

if ( a2 > 127 )
return 0.0;
if ( (unsigned __int64)a2 >= 8 )
{
*(char **)((char *)&endptr + a2) = *(char **)((char *)a1 + a2 - 8);
qmemcpy(nptr, a1, 8 * ((unsigned __int64)(a2 - 1LL) >> 3));
}
else if ( (a2 & 4) != 0 )
{
*(_DWORD *)nptr = *a1;
*(_DWORD *)&nptr[a2 - 4] = *(_DWORD *)((char *)a1 + a2 - 4);
}
else if ( a2 )
{
nptr[0] = *(_BYTE *)a1;
if ( (a2 & 2) != 0 )
*(_WORD *)&nptr[a2 - 2] = *(_WORD *)((char *)a1 + a2 - 2);
}
nptr[a2] = 0;
return strtod(nptr, &endptr);
}

canonical_decompiled_code:
double __fastcall func0(_DWORD *a1, int a2)
{
char *endptr; // [rsp+0h] [rbp-90h] BYREF
char nptr[136]; // [rsp+8h] [rbp-88h] BYREF

if ( a2 > 127 )
return 0.0;
if ( (unsigned __int64)a2 >= 8 )
{
*(char **)((char *)&endptr + a2) = *(char **)((char *)a1 + a2 - 8);
qmemcpy(nptr, a1, 8 * ((unsigned __int64)(a2 - 1LL) >> 3));
}
else if ( (a2 & 4) != 0 )
{
*(_DWORD *)nptr = *a1;
*(_DWORD *)&nptr[a2 - 4] = *(_DWORD *)((char *)a1 + a2 - 4);
}
else if ( a2 )
{
nptr[0] = *(_BYTE *)a1;
if ( (a2 & 2) != 0 )
*(_WORD *)&nptr[a2 - 2] = *(_WORD *)((char *)a1 + a2 - 2);
}
nptr[a2] = 0;
return strtod(nptr, &endptr);
}

original_code:
static double nmea_atof(const char *str, int str_sz)
{
    char *tmp_ptr;
    char buff[128];
    double res = 0;
    if(str_sz < 128)
    {
        memcpy(&buff[0], str, str_sz);
        buff[str_sz] = '\0';
        res = strtod(&buff[0], &tmp_ptr);
    }
    return res;
}

canonical_original_code:
static double nmea_atof(char *str, int str_sz)
{
    char *tmp_ptr;
    char buff[128];
    double res = 0;
    if(str_sz < 128)
    {
        memcpy(&buff[0], str, str_sz);
        buff[str_sz] = '\0';
        res = strtod(&buff[0], &tmp_ptr);
    }
    return res;
}

variable_types:
str
char *

variable_types:
buff
char [128]

variable_types: res double

return_type: double


decompiled_code:
__int64 __fastcall nmea_atoi(_DWORD *a1, int a2, int a3)
{
char *endptr; // [rsp+0h] [rbp-90h] BYREF
char nptr[136]; // [rsp+8h] [rbp-88h] BYREF

if ( a2 > 127 )
return 0LL;
if ( (unsigned __int64)a2 >= 8 )
{
*(char **)((char *)&endptr + a2) = *(char **)((char *)a1 + a2 - 8);
qmemcpy(nptr, a1, 8 * ((unsigned __int64)(a2 - 1LL) >> 3));
}
else if ( (a2 & 4) != 0 )
{
*(_DWORD *)nptr = *a1;
*(_DWORD *)&nptr[a2 - 4] = *(_DWORD *)((char *)a1 + a2 - 4);
}
else if ( a2 )
{
nptr[0] = *(_BYTE *)a1;
if ( (a2 & 2) != 0 )
*(_WORD *)&nptr[a2 - 2] = *(_WORD *)((char *)a1 + a2 - 2);
}
nptr[a2] = 0;
return strtol(nptr, &endptr, a3);
}

canonical_decompiled_code:
__int64 __fastcall func1(_DWORD *a1, int a2, int a3)
{
char *endptr; // [rsp+0h] [rbp-90h] BYREF
char nptr[136]; // [rsp+8h] [rbp-88h] BYREF

if ( a2 > 127 )
return 0LL;
if ( (unsigned __int64)a2 >= 8 )
{
*(char **)((char *)&endptr + a2) = *(char **)((char *)a1 + a2 - 8);
qmemcpy(nptr, a1, 8 * ((unsigned __int64)(a2 - 1LL) >> 3));
}
else if ( (a2 & 4) != 0 )
{
*(_DWORD *)nptr = *a1;
*(_DWORD *)&nptr[a2 - 4] = *(_DWORD *)((char *)a1 + a2 - 4);
}
else if ( a2 )
{
nptr[0] = *(_BYTE *)a1;
if ( (a2 & 2) != 0 )
*(_WORD *)&nptr[a2 - 2] = *(_WORD *)((char *)a1 + a2 - 2);
}
nptr[a2] = 0;
return strtol(nptr, &endptr, a3);
}

original_code:
static int nmea_atoi(const char *str, int str_sz, int radix)
{
    char *tmp_ptr;
    char buff[128];
    int res = 0;
    if(str_sz < 128)
    {
        memcpy(&buff[0], str, str_sz);
        buff[str_sz] = '\0';
        res = strtol(&buff[0], &tmp_ptr, radix);
    }
    return res;
}

canonical_original_code:
static int nmea_atoi(char *str, int str_sz, int radix)
{
    char *tmp_ptr;
    char buff[128];
    int res = 0;
    if(str_sz < 128)
    {
        memcpy(&buff[0], str, str_sz);
        buff[str_sz] = '\0';
        res = strtol(&buff[0], &tmp_ptr, radix);
    }
    return res;
}

variable_types: radix int

variable_types: res int


decompiled_code:
__int64 __fastcall nmea_parse_gprmc(void *src, int a2, __int64 a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11)
{
int v13; // eax
double v14; // xmm4_8
double v15; // xmm5_8
char v16[136]; // [rsp+0h] [rbp-88h] BYREF

if ( !src || !a3 )
_assert_fail("buff && pack", "gps_resync/nmea.c", 0x125u, "nmea_parse_gprmc");
*(_QWORD *)a3 = 0LL;
*(_QWORD *)(a3 + 88) = 0LL;
memset(
(void *)((a3 + 8) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)a3 - (((_DWORD)a3 + 8) & 0xFFFFFFF8) + 96) >> 3));
if ( (unsigned int)nmea_scanf(
src,
a2,
"$GPRMC,%s,%C,%f,%C,%f,%C,%f,%f,%2d%2d%2d,%f,%C,%C*",
a4,
a5,
a6,
a7,
a8,
a9,
a10,
a11,
v16,
a3 + 28,
a3 + 32,
a3 + 40,
a3 + 48,
a3 + 56,
a3 + 64,
a3 + 72,
a3 + 8,
a3 + 4,
a3,
a3 + 80,
a3 + 88,
a3 + 89)
- 13 > 1 )
return 0LL;
v13 = strlen(v16);
if ( (unsigned int)nmea_parse_time(v16, v13, a3, a4, a5, a6, a7, v14, v15, a10, a11) )
return 0LL;
if ( *(int *)a3 <= 89 )
*(_DWORD *)a3 += 100;
--*(_DWORD *)(a3 + 4);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(void *src, int a2, __int64 a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11)
{
int v13; // eax
double v14; // xmm4_8
double v15; // xmm5_8
char v16[136]; // [rsp+0h] [rbp-88h] BYREF

if ( !src || !a3 )
_assert_fail("buff && pack", "gps_resync/nmea.c", 0x125u, "nmea_parse_gprmc");
*(_QWORD *)a3 = 0LL;
*(_QWORD *)(a3 + 88) = 0LL;
memset(
(void *)((a3 + 8) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)a3 - (((_DWORD)a3 + 8) & 0xFFFFFFF8) + 96) >> 3));
if ( (unsigned int)func2(
src,
a2,
"$GPRMC,%s,%C,%f,%C,%f,%C,%f,%f,%2d%2d%2d,%f,%C,%C*",
a4,
a5,
a6,
a7,
a8,
a9,
a10,
a11,
v16,
a3 + 28,
a3 + 32,
a3 + 40,
a3 + 48,
a3 + 56,
a3 + 64,
a3 + 72,
a3 + 8,
a3 + 4,
a3,
a3 + 80,
a3 + 88,
a3 + 89)
- 13 > 1 )
return 0LL;
v13 = strlen(v16);
if ( (unsigned int)func3(v16, v13, a3, a4, a5, a6, a7, v14, v15, a10, a11) )
return 0LL;
if ( *(int *)a3 <= 89 )
*(_DWORD *)a3 += 100;
--*(_DWORD *)(a3 + 4);
return 1LL;
}

original_code:
int nmea_parse_gprmc(const char *buff, int buff_sz, nmea_gprmc_t *pack)
{
    int nsen;
    char time_buff[128];
    ((void) sizeof ((buff && pack) ? 1 : 0), __extension__ ({ if (buff && pack) ; else __assert_fail ("buff && pack", "gps_resync/nmea.c", 293, __extension__ __PRETTY_FUNCTION__); }));
    memset(pack, 0, sizeof(nmea_gprmc_t));
    nsen = nmea_scanf(buff, buff_sz,
        "$GPRMC,%s,%C,%f,%C,%f,%C,%f,%f,%2d%2d%2d,%f,%C,%C*",
        &(time_buff[0]),
        &(pack->status), &(pack->lat), &(pack->ns), &(pack->lon), &(pack->ew),
        &(pack->speed), &(pack->direction),
        &(pack->time.day), &(pack->time.mon), &(pack->time.year),
        &(pack->declination), &(pack->declin_ew), &(pack->mode));
    if(nsen != 13 && nsen != 14)
    {
        return 0;
    }
    if(0 != _nmea_parse_time(&time_buff[0], (int)strlen(&time_buff[0]), &(pack->time)))
    {
        return 0;
    }
    if(pack->time.year < 90)
        pack->time.year += 100;
    pack->time.mon -= 1;
    return 1;
}

canonical_original_code:
int nmea_parse_gprmc(char *buff, int buff_sz, struct nmea_gprmc_t *pack)
{
    int nsen;
    char time_buff[128];
    ((void) sizeof ((buff && pack) ? 1 : 0), __extension__ ({ if (buff && pack) ; else __assert_fail ("buff && pack", "gps_resync/nmea.c", 293, __extension__ __PRETTY_FUNCTION__); }));
    memset(pack, 0, sizeof(nmea_gprmc_t));
    nsen = nmea_scanf(buff, buff_sz,
        "$GPRMC,%s,%C,%f,%C,%f,%C,%f,%f,%2d%2d%2d,%f,%C,%C*",
        &(time_buff[0]),
        &(pack->status), &(pack->lat), &(pack->ns), &(pack->lon), &(pack->ew),
        &(pack->speed), &(pack->direction),
        &(pack->time.day), &(pack->time.mon), &(pack->time.year),
        &(pack->declination), &(pack->declin_ew), &(pack->mode));
    if(nsen != 13 && nsen != 14)
    {
        return 0;
    }
    if(0 != _nmea_parse_time(&time_buff[0], (int)strlen(&time_buff[0]), &(pack->time)))
    {
        return 0;
    }
    if(pack->time.year < 90)
        pack->time.year += 100;
    pack->time.mon -= 1;
    return 1;
}

variable_types:
buff
char *

variable_types: struct nmea_gprmc_t { struct nmea_time_t { int year; int mon; int day; int hour; int min; int sec; int hsec; } time; char status; double lat; char ns; double lon; char ew; double speed; double direction; double declination; char declin_ew; char mode; } *

variable_types: nsen int

user_defined_types: struct nmea_gprmc_t { struct nmea_time_t time; char status; double lat; char ns; double lon; char ew; double speed; double direction; double declination; char declin_ew; char mode; }


original_code:
int64_t nmea_time_to_tai(nmea_time_t t)
{
 short month, year;
 int64_t result;
 const int m_to_d[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
 month = t.mon;
 year = t.year + month / 12 + 1900;
 month %= 12;
 if (month < 0)
 {
  year -= 1;
  month += 12;
 }
 result = (year - 1970) * 365 + (year - 1969) / 4 + m_to_d[month];
 result = (year - 1970) * 365 + m_to_d[month];
 if (month <= 1)
  year -= 1;
 result += (year - 1968) / 4;
 result -= (year - 1900) / 100;
 result += (year - 1600) / 400;
 result += t.day;
 result -= 1;
 result *= 24;
 result += t.hour;
 result *= 60;
 result += t.min;
 result *= 60;
 result += t.sec;
 return(result + 19LL);
}

canonical_original_code:
long nmea_time_to_tai(struct nmea_time_t t)
{
 short month; short year;
 long result;
 int m_to_d[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
 month = t.mon;
 year = t.year + month / 12 + 1900;
 month %= 12;
 if (month < 0)
 {
  year -= 1;
  month += 12;
 }
 result = (year - 1970) * 365 + (year - 1969) / 4 + m_to_d[month];
 result = (year - 1970) * 365 + m_to_d[month];
 if (month <= 1)
  year -= 1;
 result += (year - 1968) / 4;
 result -= (year - 1900) / 100;
 result += (year - 1600) / 400;
 result += t.day;
 result -= 1;
 result *= 24;
 result += t.hour;
 result *= 60;
 result += t.min;
 result *= 60;
 result += t.sec;
 return(result + 19LL);
}

variable_types: struct nmea_time_t { int year; int mon; int day; int hour; int min; int sec; int hsec; }

variable_types: month short

variable_types: year short

variable_types: result long

return_type: long