repo: anlif/micropong

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char v3; // al
int v4; // ebp
char v5; // al
char v6; // al
char v7; // al

hw_init(argc, argv, envp);
pong_init();
while ( 2 )
{
switch ( (unsigned int)hw_get_input() )
{
case 0u:
v3 = pong_get_paddle_state(0LL);
pong_move_paddle(0LL, (unsigned __int8)(v3 - 2));
goto LABEL_3;
case 1u:
v5 = pong_get_paddle_state(0LL);
pong_move_paddle(0LL, (unsigned __int8)(v5 + 2));
goto LABEL_3;
case 2u:
v6 = pong_get_paddle_state(1LL);
pong_move_paddle(1LL, (unsigned __int8)(v6 - 2));
goto LABEL_3;
case 3u:
v7 = pong_get_paddle_state(1LL);
pong_move_paddle(1LL, (unsigned __int8)(v7 + 2));
goto LABEL_3;
case 4u:
if ( (unsigned __int8)pong_move_ball() != 2 )
{
v4 = 0;
LABEL_9:
puts("restart");
pong_restart();
hw_draw();
draw_swap_buffers();
draw_copy_buffers();
if ( !v4 )
goto LABEL_13;
continue;
}
hw_draw();
draw_swap_buffers();
draw_copy_buffers();
LABEL_13:
hw_deinit();
return 0;
default:
LABEL_3:
v4 = 1;
if ( (unsigned __int8)pong_move_ball() != 2 )
goto LABEL_9;
hw_draw();
draw_swap_buffers();
draw_copy_buffers();
continue;
}
}
}

original_code:
int main(){
 hw_init();
 pong_init();
 int run = 1;
 _Bool play = 1;
 while(run){
  if(play){
   switch(hw_get_input()){
    case EXIT:
     run = 0;
     break;
    case ONE_UP:
     pong_move_paddle(PONG_PADDLE_LEFT,
       pong_get_paddle_state(PONG_PADDLE_LEFT) - 2);
     break;
    case ONE_DOWN:
     pong_move_paddle(PONG_PADDLE_LEFT,
       pong_get_paddle_state(PONG_PADDLE_LEFT) + 2);
     break;
    case TWO_UP:
     pong_move_paddle(PONG_PADDLE_RIGHT,
       pong_get_paddle_state(PONG_PADDLE_RIGHT) - 2);
     break;
    case TWO_DOWN:
     pong_move_paddle(PONG_PADDLE_RIGHT,
       pong_get_paddle_state(PONG_PADDLE_RIGHT) + 2);
     break;
   }
   int status = pong_move_ball();
   if( status != PONG_NO_ERROR ){
    printf("restart\n");
    pong_restart();
   }
   hw_draw();
   draw_swap_buffers();
   draw_copy_buffers();
  }
 }
 hw_deinit();
 return 0;
}

variable_types: run int

variable_types: status int


decompiled_code:
__int64 __fastcall hw_set_pixel(__int16 a1)
{
return drawRect(screen, 6 * (unsigned int)(unsigned __int8)a1, 6 * (unsigned int)HIBYTE(a1), 6u, 6, 0x99u);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int16 a1)
{
return func2(screen, 6 * (unsigned int)(unsigned __int8)a1, 6 * (unsigned int)HIBYTE(a1), 6u, 6, 0x99u);
}

original_code:
static void hw_set_pixel(point_t point){
 drawRect(screen, point.x*6, point.y*6, 6, 6, (0462 >> 1));
}

canonical_original_code:
static void hw_set_pixel(struct point_t point){
 drawRect(screen, point.x*6, point.y*6, 6, 6, (0462 >> 1));
}

variable_types: struct point_t { unsigned char x; unsigned char y; }

return_type: void


decompiled_code:
__int64 hw_init()
{
__int64 result; // rax
const char *v1; // rax
const char *v2; // rax

if ( (unsigned int)SDL_Init(32LL) )
{
v1 = (const char *)SDL_GetError();
printf("Error in init video: %s\n", v1);
exit(1);
}
screen = SDL_SetVideoMode(768LL, 384LL, 8LL, 0LL);
if ( !screen )
{
v2 = (const char *)SDL_GetError();
printf("Error in getting surface: %s\n", v2);
exit(1);
}
if ( (unsigned int)SDL_EnableKeyRepeat(1LL, 1LL) )
printf("Error in enabling key repeat");
result = SDL_GetTicks();
ticks_last_draw = result;
return result;
}

canonical_decompiled_code:
__int64 func4()
{
__int64 result; // rax
const char *v1; // rax
const char *v2; // rax

if ( (unsigned int)SDL_Init(32LL) )
{
v1 = (const char *)SDL_GetError();
printf("Error in init video: %s\n", v1);
exit(1);
}
screen = SDL_SetVideoMode(768LL, 384LL, 8LL, 0LL);
if ( !screen )
{
v2 = (const char *)SDL_GetError();
printf("Error in getting surface: %s\n", v2);
exit(1);
}
if ( (unsigned int)SDL_EnableKeyRepeat(1LL, 1LL) )
printf("Error in enabling key repeat");
result = SDL_GetTicks();
ticks_last_draw = result;
return result;
}

original_code:
void hw_init(){
 if(SDL_Init(0x00000020)){
  printf("Error in init video: %s\n", SDL_GetError());
  exit(1);
 }
 screen = SDL_SetVideoMode(128*6, 64*6, 8, 0x00000000);
 if(screen == ((void *)0)){
  printf("Error in getting surface: %s\n", SDL_GetError());
  exit(1);
 }
 if(SDL_EnableKeyRepeat(1, 1)){
  printf("Error in enabling key repeat");
 }
 ticks_last_draw = SDL_GetTicks();
}


decompiled_code:
__int64 hw_draw()
{
unsigned __int8 *v0; // rbp
__int64 v1; // rbx
unsigned __int8 *v2; // rax
int v3; // eax

drawRect(screen, 0, 0, 0x300u, 384, 0);
v0 = (unsigned __int8 *)draw_next_point();
hw_set_pixel(*(_WORD *)v0);
if ( print_0 )
printf("Point: {%i, %i}\n", *v0, v0[1]);
v1 = draw_next_point();
while ( *(_BYTE *)v1 != *v0 || *(_BYTE *)(v1 + 1) != v0[1] )
{
hw_set_pixel(*(_WORD *)v1);
v2 = (unsigned __int8 *)draw_next_point();
v1 = (__int64)v2;
if ( print_0 )
printf("Point: {%i, %i}\n", *v2, v2[1]);
}
print_0 = 0;
SDL_UpdateRect(screen, 0LL, 0LL, 0LL, 0LL);
v3 = SDL_GetTicks();
return SDL_Delay((unsigned int)(50 * ((v3 - ticks_last_draw) / 50) - (v3 - ticks_last_draw) + 50));
}

canonical_decompiled_code:
__int64 func5()
{
unsigned __int8 *v0; // rbp
__int64 v1; // rbx
unsigned __int8 *v2; // rax
int v3; // eax

func2(screen, 0, 0, 0x300u, 384, 0);
v0 = (unsigned __int8 *)draw_next_point();
func3(*(_WORD *)v0);
if ( print_0 )
printf("Point: {%i, %i}\n", *v0, v0[1]);
v1 = draw_next_point();
while ( *(_BYTE *)v1 != *v0 || *(_BYTE *)(v1 + 1) != v0[1] )
{
func3(*(_WORD *)v1);
v2 = (unsigned __int8 *)draw_next_point();
v1 = (__int64)v2;
if ( print_0 )
printf("Point: {%i, %i}\n", *v2, v2[1]);
}
print_0 = 0;
SDL_UpdateRect(screen, 0LL, 0LL, 0LL, 0LL);
v3 = SDL_GetTicks();
return SDL_Delay((unsigned int)(50 * ((v3 - ticks_last_draw) / 50) - (v3 - ticks_last_draw) + 50));
}

original_code:
void hw_draw(){
 static int print = 0;
 drawRect(screen, 0,0,128*6,64*6,0);
 point_t *first_point = draw_next_point();
 hw_set_pixel(*first_point);
 if(print) printf("Point: {%i, %i}\n", first_point->x, first_point->y);
 point_t *current_point = draw_next_point();
 while(!(current_point->x == first_point->x && current_point->y == first_point->y)){
  hw_set_pixel(*current_point);
  current_point = draw_next_point();
  if(print) printf("Point: {%i, %i}\n", current_point->x, current_point->y);
 }
 print = 0;
 SDL_UpdateRect(screen, 0, 0, 0, 0);
 int current_ticks = SDL_GetTicks();
 int delta = (current_ticks - ticks_last_draw) % (1000/20);
 int delay = (1000/20) - delta;
 SDL_Delay(delay);
}

canonical_original_code:
void hw_draw(){
 static int print = 0;
 drawRect(screen, 0,0,128*6,64*6,0);
 struct point_t *first_point = draw_next_point();
 hw_set_pixel(*first_point);
 if(print) printf("Point: {%i, %i}\n", first_point->x, first_point->y);
 struct point_t *current_point = draw_next_point();
 while(!(current_point->x == first_point->x && current_point->y == first_point->y)){
  hw_set_pixel(*current_point);
  current_point = draw_next_point();
  if(print) printf("Point: {%i, %i}\n", current_point->x, current_point->y);
 }
 print = 0;
 SDL_UpdateRect(screen, 0, 0, 0, 0);
 int current_ticks = SDL_GetTicks();
 int delta = (current_ticks - ticks_last_draw) % (1000/20);
 int delay = (1000/20) - delta;
 SDL_Delay(delay);
}

variable_types: print int

variable_types: struct point_t { unsigned char x; unsigned char y; } *

variable_types: delta int

variable_types: delay int


decompiled_code:
__int64 hw_deinit()
{
return SDL_Quit();
}

canonical_decompiled_code:
__int64 func7()
{
return SDL_Quit();
}

original_code:
void hw_deinit(){
 SDL_Quit();
}

canonical_original_code:
void hw_deinit(){
 SDL_Quit();
}


main

canonical_name: main

repo: anomit/loki

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
if ( geteuid() )
{
puts("You need to be uid 0");
exit(1);
}
check(argc, argv);
exec();
return 0;
}

original_code:
int main()
{
    if(geteuid())
    {
        printf("You need to be uid 0\n");
        exit(1);
    }
    check();
    exec();
    return 0;
}

return_type: int


canonical_original_code:
int calculate_key(unsigned char *box, unsigned char *iv, unsigned char key[16])
{
 unsigned char ctx[16];
 unsigned short ctrl1 = 0;
 unsigned short ctrl2 = 0;
 unsigned int x1 = 0;
 unsigned int x2 = 0;
 unsigned int s[16];
 unsigned int t[8];
 unsigned int x[16];
 unsigned int p[4];
 unsigned int n[4];
 unsigned int state_matrix[16];
 unsigned int *ku = (const uint32_t *)box;
 int round;
 int i; int j;
 memset(key, 0, 16);
 memset(ctx, 0, 16);
 for (i = 0; i < 16; ++i) {
  ctx[i] = T1[i * 256 + iv[i]] ^ box[i];
 }
 for (round = 0; round < 5; ++round, ku += 8) {
  for (i = 0; i < 8; ++i) {
   t[i] = swap32(ku[i]);
  }
   for (i = 0; i < 16; ++i) {
    s[i] = swap32(B[ctx[i] + i * 256 + round * 0x2000]);
   }
  for (i = 0; i < 16; ++i) {
   unsigned short ctrl = phase1_ctrl[i];
   if ((ctrl & (1 << 11)) == (1 << 11)) {
    x[i] = t[(((ctrl) >> 0) & 0x1F)];
   } else if ((ctrl & (1 << 10)) == (1 << 10)) {
    x[i] = s[(((ctrl) >> 0) & 0x1F)] ^ s[(((ctrl) >> 5) & 0x1F)];
   } else {
    x[i] = s[(((ctrl) >> 0) & 0x1F)];
   }
  }
  for (i = 0; i < 4; ++i) {
   ((void) sizeof (((phase2_ctrl1[i] & (1 << 10)) == (1 << 10)) ? 1 : 0), __extension__ ({ if ((phase2_ctrl1[i] & (1 << 10)) == (1 << 10)) ; else __assert_fail ("(phase2_ctrl1[i] & RCTRL_X_BIT) == RCTRL_X_BIT", "crypt.c", 120, __extension__ __PRETTY_FUNCTION__); }));
   ((void) sizeof (((phase2_ctrl2[i] & (1 << 10)) == (1 << 10)) ? 1 : 0), __extension__ ({ if ((phase2_ctrl2[i] & (1 << 10)) == (1 << 10)) ; else __assert_fail ("(phase2_ctrl2[i] & RCTRL_X_BIT) == RCTRL_X_BIT", "crypt.c", 121, __extension__ __PRETTY_FUNCTION__); }));
   ctrl1 = phase2_ctrl1[i];
   ctrl2 = phase2_ctrl2[i];
   x1 = s[(((ctrl1) >> 0) & 0x1F)] ^ x[(((ctrl1) >> 5) & 0x1F)];
   x2 = s[(((ctrl2) >> 0) & 0x1F)] ^ t[(((ctrl2) >> 5) & 0x1F)];
   p[i] = x1 ^ x2;
  }
  for (i = 0; i < 4; ++i) {
   n[i] = swap32(ku[i + 8]);
  }
  if (round < 5 - 1) {
    for (i = 0; i < 4; ++i) {
     for (j = 0; j < 4; ++j) {
      state_matrix[i * 4 + j] = swap32(B[(((p[i]) >> 8 * (4 - (j) - 1)) & 0xFF) + i * 1024 + j * 256 + ((round * 2 + 1) << 12)]);
     }
    }
   for (i = 0; i < 4; ++i) {
    unsigned int x1 = state_matrix[((i + 0) & 3) * 4 + ((0) & 3)];
    unsigned int x2 = state_matrix[((i + 1) & 3) * 4 + ((1) & 3)];
    unsigned int x3 = state_matrix[((i + 2) & 3) * 4 + ((2) & 3)];
    unsigned int x4 = state_matrix[((i + 3) & 3) * 4 + ((3) & 3)];
    unsigned int xxx = x1 ^ x2 ^ x3 ^ x4 ^ n[i];
    for (j = 0; j < 4; ++j) {
     ctx[i * 4 + j] = (((xxx) >> 8 * (4 - (j) - 1)) & 0xFF);
    }
   }
  } else {
   for (i = 0; i < 4; ++i) {
    for (j = 0; j < 4; ++j) {
     ctx[i * 4 + j] = (((p[i]) >> 8 * (4 - (j) - 1)) & 0xFF);
    }
   }
  }
 }
 for (i = 0; i < 4; ++i) {
  unsigned int a = swap32(B[ctx[FT[i * 4 + 0]] + FT[i * 4 + 0] * 256 + 0x9000]) & 0xFF000000;
  unsigned int b = swap32(B[ctx[FT[i * 4 + 1]] + FT[i * 4 + 1] * 256 + 0x9000]) & 0xFF0000;
  unsigned int c = swap32(B[ctx[FT[i * 4 + 2]] + FT[i * 4 + 2] * 256 + 0x9000]) & 0xFF00;
  unsigned int d = swap32(B[ctx[FT[i * 4 + 3]] + FT[i * 4 + 3] * 256 + 0x9000]) & 0xFF;
  unsigned int f = n[i] ^ a ^ b ^ c ^ d;
   for (j = 0; j < 4; ++j) {
    key[((i + 1) * 4 + j) & 15] = T2[(((f) >> 8 * (4 - (j) - 1)) & 0xFF) + i * 1024 + j * 256];
   }
 }
 return 0;
}

variable_types:
box
unsigned char *

variable_types:
iv
unsigned char *

variable_types:
key
unsigned char [16]

variable_types:
ctx
unsigned char [16]

variable_types:
s
unsigned int [16]

variable_types:
t
unsigned int [8]

variable_types:
x
unsigned int [16]

variable_types:
p
unsigned int [4]

variable_types:
n
unsigned int [4]

variable_types:
ku
unsigned int *

variable_types: round int

variable_types: i int

variable_types: j int

variable_types:
ctrl
unsigned short

variable_types:
xxx
unsigned int

variable_types:
a
unsigned int

variable_types:
b
unsigned int

variable_types:
c
unsigned int

variable_types:
d
unsigned int

variable_types:
f
unsigned int


original_code:
int sfc_process_data(sfc_context_t *ctx, const uint8_t *in, uint8_t *out, uint32_t size) {
 sfc_context_prv_t *real_ctx = ((void *)0);
 uint8_t final_key[16] = {0};
 uint32_t num_blocks = 0;
 uint32_t i, j = 0;
 if (ctx == ((void *)0) || in == ((void *)0) || out == ((void *)0))
  return (-1);
 real_ctx = (sfc_context_prv_t *)ctx;
 num_blocks = (uint32_t)(size / 16);
 for (i = 0; i < num_blocks; ++i) {
  calculate_key(real_ctx->erk, real_ctx->riv, final_key);
  for (j = 0; j < 16; ++j)
   out[j] = in[j] ^ final_key[j];
  for (j = (16 - 1); j > 0; --j) {
   real_ctx->riv[j]++;
   if (real_ctx->riv[j])
    break;
  }
  in += 16;
  out += 16;
 }
 return ( 0);
}

canonical_original_code:
int sfc_process_data(struct sfc_context_t *ctx, unsigned char *in, unsigned char *out, unsigned int size) {
 struct sfc_context_prv_t *real_ctx = ((void *)0);
 unsigned char final_key[16] = {0};
 unsigned int num_blocks = 0;
 unsigned int i; unsigned int j = 0;
 if (ctx == ((void *)0) || in == ((void *)0) || out == ((void *)0))
  return (-1);
 real_ctx = (struct sfc_context_prv_t *)ctx;
 num_blocks = (unsigned int)(size / 16);
 for (i = 0; i < num_blocks; ++i) {
  calculate_key(real_ctx->erk, real_ctx->riv, final_key);
  for (j = 0; j < 16; ++j)
   out[j] = in[j] ^ final_key[j];
  for (j = (16 - 1); j > 0; --j) {
   real_ctx->riv[j]++;
   if (real_ctx->riv[j])
    break;
  }
  in += 16;
  out += 16;
 }
 return ( 0);
}

variable_types: struct sfc_context_t { void *unused; } *

variable_types:
in
unsigned char *

variable_types:
out
unsigned char *

variable_types:
size
unsigned int

variable_types: struct sfc_context_prv_t { unsigned char erk[-1]; unsigned char riv[16]; } *

variable_types:
i
unsigned int

variable_types:
j
unsigned int


decompiled_code:
void __fastcall sfc_destroy_context(void *a1)
{
if ( a1 )
free(a1);
}

canonical_decompiled_code:
void __fastcall func2(void *a1)
{
if ( a1 )
free(a1);
}

original_code:
void sfc_destroy_context(sfc_context_t *ctx) {
 sfc_context_prv_t *real_ctx;
 if (ctx != ((void *)0)) {
  real_ctx = (sfc_context_prv_t *)ctx;
  free(real_ctx);
 }
}

canonical_original_code:
void sfc_destroy_context(struct sfc_context_t *ctx) {
 struct sfc_context_prv_t *real_ctx;
 if (ctx != ((void *)0)) {
  real_ctx = (struct sfc_context_prv_t *)ctx;
  free(real_ctx);
 }
}

user_defined_types: struct sfc_context_prv_t { unsigned char erk[-1]; unsigned char riv[16]; }


repo: anre/lielas

decompiled_code:
int print_help()
{
printf("Version: %s\n", ver);
puts("Usage:");
puts("ipchanger get");
puts("...returns current ip");
putchar(10);
puts("ipchanger set dhcp");
puts("...sets eth0 to dhcp");
putchar(10);
puts("ipchanger set static <address> <netmask> <gateway>");
puts("...sets eth0 to static ip");
putchar(10);
puts("ipchanger set dns <dns1> <dns2>");
puts("...sets dns1 and if given dns2");
putchar(10);
puts("ipchanger restore");
puts("...restores settings from interfaces.old");
return putchar(10);
}

canonical_decompiled_code:
int func8()
{
printf("Version: %s\n", ver);
puts("Usage:");
puts("ipchanger get");
puts("...returns current ip");
putchar(10);
puts("ipchanger set dhcp");
puts("...sets eth0 to dhcp");
putchar(10);
puts("ipchanger set static <address> <netmask> <gateway>");
puts("...sets eth0 to static ip");
putchar(10);
puts("ipchanger set dns <dns1> <dns2>");
puts("...sets dns1 and if given dns2");
putchar(10);
puts("ipchanger restore");
puts("...restores settings from interfaces.old");
return putchar(10);
}

original_code:
void print_help(){
  printf("Version: %s\n", ver);
  printf("Usage:\n");
  printf("ipchanger get\n");
  printf("...returns current ip\n");
  printf("\n");
  printf("ipchanger set dhcp\n");
  printf("...sets eth0 to dhcp\n");
  printf("\n");
  printf("ipchanger set static <address> <netmask> <gateway>\n");
  printf("...sets eth0 to static ip\n");
  printf("\n");
  printf("ipchanger set dns <dns1> <dns2>\n");
  printf("...sets dns1 and if given dns2\n");
  printf("\n");
  printf("ipchanger restore\n");
  printf("...restores settings from interfaces.old\n");
  printf("\n");
}


decompiled_code:
unsigned int restart_net_interface()
{
write_log("resetting eth0", 2);
sleep(2u);
if ( system("/sbin/ifdown eth0") )
{
write_log("failed to shut down eth0", 0);
exit(-1);
}
sleep(5u);
if ( system("/sbin/ifup eth0") )
{
write_log("failed to bring eth0 up", 0);
exit(-1);
}
return sleep(2u);
}

canonical_decompiled_code:
unsigned int func9()
{
func7("resetting eth0", 2);
sleep(2u);
if ( system("/sbin/ifdown eth0") )
{
func7("failed to shut down eth0", 0);
exit(-1);
}
sleep(5u);
if ( system("/sbin/ifup eth0") )
{
func7("failed to bring eth0 up", 0);
exit(-1);
}
return sleep(2u);
}

original_code:
void restart_net_interface(){
    write_log("resetting eth0", 2);
    sleep(2);
    if(system("/sbin/ifdown eth0")){
      write_log("failed to shut down eth0", 0);
      exit(-1);
    }
    sleep(5);
    if(system("/sbin/ifup eth0")){
      write_log("failed to bring eth0 up", 0);
      exit(-1);
    }
    sleep(2);
}


decompiled_code:
int save_backup()
{
char v1[520]; // [rsp+0h] [rbp-208h] BYREF

sprintf(v1, "cp %s %s.bak", "/etc/network/interfaces", "/etc/network/interfaces");
return system(v1);
}

canonical_decompiled_code:
int func12()
{
char v1[520]; // [rsp+0h] [rbp-208h] BYREF

sprintf(v1, "cp %s %s.bak", "/etc/network/interfaces", "/etc/network/interfaces");
return system(v1);
}

original_code:
int save_backup(){
  char cmd[500];
  sprintf(cmd, "cp %s %s.bak", "/etc/network/interfaces", "/etc/network/interfaces");
  return system(cmd);
}

variable_types:
cmd
char [500]


decompiled_code:
int restore_backup()
{
char v1[520]; // [rsp+0h] [rbp-208h] BYREF

sprintf(v1, "cp %s.bak %s", "/etc/network/interfaces", "/etc/network/interfaces");
return system(v1);
}

canonical_decompiled_code:
int func15()
{
char v1[520]; // [rsp+0h] [rbp-208h] BYREF

sprintf(v1, "cp %s.bak %s", "/etc/network/interfaces", "/etc/network/interfaces");
return system(v1);
}

original_code:
int restore_backup(){
  char cmd[500];
  sprintf(cmd, "cp %s.bak %s", "/etc/network/interfaces", "/etc/network/interfaces");
  return system(cmd);
 }


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v5; // r12
const char *v6; // r12

if ( argc <= 1 )
{
print_help();
exit(0);
}
v5 = argv[1];
if ( !memcmp(v5, "get", 3uLL) )
{
write_log("get network settings", 2);
get_settings();
}
else if ( !strncmp(v5, "set", 3uLL) )
{
v6 = argv[2];
if ( !strncmp(v6, "dhcp", 4uLL) )
{
write_log("set network settings dhcp", 2);
set_dhcp();
restart_net_interface();
}
else if ( !strncmp(v6, "static", 6uLL) )
{
write_log("set network settings static", 2);
set_static(argv[3], argv[4], argv[5]);
restart_net_interface();
}
else
{
if ( strncmp(v6, "dns", 3uLL) )
exit(-1);
write_log("set dns server", 2);
if ( argc == 4 )
{
set_dns(argv[3], 0LL);
}
else if ( argc == 5 )
{
set_dns(argv[3], argv[4]);
}
}
}
else if ( !strncmp(v5, "restore", 7uLL) )
{
write_log("restoring network settings", 2);
if ( restore_backup() )
{
write_log("failed to restore network settings", 0);
exit(-1);
}
restart_net_interface();
}
else
{
print_help();
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v5; // r12
const char *v6; // r12

if ( argc <= 1 )
{
func8();
exit(0);
}
v5 = argv[1];
if ( !memcmp(v5, "get", 3uLL) )
{
func7("get network settings", 2);
func10();
}
else if ( !strncmp(v5, "set", 3uLL) )
{
v6 = argv[2];
if ( !strncmp(v6, "dhcp", 4uLL) )
{
func7("set network settings dhcp", 2);
func13();
func9();
}
else if ( !strncmp(v6, "static", 6uLL) )
{
func7("set network settings static", 2);
func14(argv[3], argv[4], argv[5]);
func9();
}
else
{
if ( strncmp(v6, "dns", 3uLL) )
exit(-1);
func7("set dns server", 2);
if ( argc == 4 )
{
func11(argv[3], 0LL);
}
else if ( argc == 5 )
{
func11(argv[3], argv[4]);
}
}
}
else if ( !strncmp(v5, "restore", 7uLL) )
{
func7("restoring network settings", 2);
if ( func15() )
{
func7("failed to restore network settings", 0);
exit(-1);
}
func9();
}
else
{
func8();
}
return 0;
}

original_code:
int main(int argc, char *argv[]){
  if(argc < 2){
    print_help();
    exit(0);
  }
  if(!strncmp(argv[1], "get", 3)){
    write_log("get network settings", 2);
    get_settings();
  }else if(!strncmp(argv[1], "set", 3)){
    if(!strncmp(argv[2], "dhcp", 4)){
      write_log("set network settings dhcp", 2);
      set_dhcp();
      restart_net_interface();
    }else if(!strncmp(argv[2], "static", 6)){
      write_log("set network settings static", 2);
      set_static(argv[3], argv[4], argv[5]);
      restart_net_interface();
    }else if(!strncmp(argv[2], "dns", 3)){
      write_log("set dns server", 2);
      if( argc == 4 ){
        set_dns(argv[3], ((void *)0));
      }else if(argc == 5){
        set_dns(argv[3], argv[4]);
      }
    }else{
      exit(-1);
    }
  }else if(!strncmp(argv[1], "restore", 7)){
    write_log("restoring network settings", 2);
    if(restore_backup()){
      write_log("failed to restore network settings", 0);
      exit(-1);
    }
    restart_net_interface();
  }else{
    print_help();
  }
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


decompiled_code:
void __fastcall lwp_init_resource(__int64 a1)
{
*(_BYTE *)a1 = 0;
*(_DWORD *)(a1 + 100) = 0;
}

original_code:
void lwp_init_resource(lwp_resource *res){
  res->name[0] = 0;
  res->attributes = 0;
}

canonical_original_code:
void lwp_init_resource(struct lwp_resource_struct *res){
  res->name[0] = 0;
  res->attributes = 0;
}

variable_types: struct lwp_resource_struct { char name[100]; int attributes; struct lwp_attr_struct { char name[100]; } attr[10]; } *

user_defined_types: struct lwp_resource_struct { char name[100]; int attributes; struct lwp_attr_struct attr[10]; }


decompiled_code:
void __fastcall lwp_init_wkc(__int64 a1)
{
lwp_init_resource(a1);
lwp_init_resource(a1 + 1104);
lwp_init_resource(a1 + 2208);
lwp_init_resource(a1 + 3312);
lwp_init_resource(a1 + 4416);
lwp_init_resource(a1 + 5520);
*(_DWORD *)(a1 + 28704) = 0;
}

original_code:
void lwp_init_wkc(lwp_wkc *wkc){
  lwp_init_resource(&wkc->info);
  lwp_init_resource(&wkc->device);
  lwp_init_resource(&wkc->modul);
  lwp_init_resource(&wkc->network);
  lwp_init_resource(&wkc->database);
  lwp_init_resource(&wkc->logger);
  wkc->channels = 0;
}

canonical_original_code:
void lwp_init_wkc(struct lwp_wkc_struct *wkc){
  lwp_init_resource(&wkc->info);
  lwp_init_resource(&wkc->device);
  lwp_init_resource(&wkc->modul);
  lwp_init_resource(&wkc->network);
  lwp_init_resource(&wkc->database);
  lwp_init_resource(&wkc->logger);
  wkc->channels = 0;
}

variable_types: struct lwp_wkc_struct { struct lwp_resource_struct { char name[100]; int attributes; struct lwp_attr_struct { char name[100]; } attr[10]; } info; struct lwp_resource_struct device; struct lwp_resource_struct modul; struct lwp_resource_struct network; struct lwp_resource_struct database; struct lwp_resource_struct logger; struct lwp_resource_struct channel[20]; int channels; } *

user_defined_types: struct lwp_wkc_struct { struct lwp_resource_struct info; struct lwp_resource_struct device; struct lwp_resource_struct modul; struct lwp_resource_struct network; struct lwp_resource_struct database; struct lwp_resource_struct logger; struct lwp_resource_struct channel[20]; int channels; }


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

result = a2;
if ( !memcmp(a1, "channel", 7uLL) )
{
result += 1104LL * (*(_DWORD *)(result + 28704) + 1) + 6624;
}
else if ( !strcmp(a1, "database") )
{
result += 4416LL;
}
else if ( !strcmp(a1, "device") )
{
result += 1104LL;
}
else if ( strcmp(a1, "info") )
{
if ( !strcmp(a1, "logger") )
{
result += 5520LL;
}
else if ( !strcmp(a1, "modul") )
{
result += 2208LL;
}
else
{
result += 3312LL;
if ( strcmp(a1, "network") )
result = 0LL;
}
}
return result;
}

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

result = a2;
if ( !memcmp(a1, "channel", 7uLL) )
{
result += 1104LL * (*(_DWORD *)(result + 28704) + 1) + 6624;
}
else if ( !strcmp(a1, "database") )
{
result += 4416LL;
}
else if ( !strcmp(a1, "device") )
{
result += 1104LL;
}
else if ( strcmp(a1, "info") )
{
if ( !strcmp(a1, "logger") )
{
result += 5520LL;
}
else if ( !strcmp(a1, "modul") )
{
result += 2208LL;
}
else
{
result += 3312LL;
if ( strcmp(a1, "network") )
result = 0LL;
}
}
return result;
}

original_code:
lwp_resource *lwp_get_res_by_name( char* str, lwp_wkc *wkc){
  lwp_resource *res = 0;
  if(!strncmp(str, "channel", strlen("channel"))){
    res = &wkc->channel[wkc->channels+1];
  }else if(!strcmp(str, "database")){
    res = &wkc->database;
  }else if(!strcmp(str, "device")){
    res = &wkc->device;
  }else if(!strcmp(str, "info")){
    res = &wkc->info;
  }else if(!strcmp(str, "logger")){
    res = &wkc->logger;
  }else if(!strcmp(str, "modul")){
    res = &wkc->modul;
  }else if(!strcmp(str, "network")){
    res = &wkc->network;
  }
  return res;
}

canonical_original_code:
struct lwp_resource_struct *lwp_get_res_by_name(char *str, struct lwp_wkc_struct *wkc){
  struct lwp_resource_struct *res = 0;
  if(!strncmp(str, "channel", strlen("channel"))){
    res = &wkc->channel[wkc->channels+1];
  }else if(!strcmp(str, "database")){
    res = &wkc->database;
  }else if(!strcmp(str, "device")){
    res = &wkc->device;
  }else if(!strcmp(str, "info")){
    res = &wkc->info;
  }else if(!strcmp(str, "logger")){
    res = &wkc->logger;
  }else if(!strcmp(str, "modul")){
    res = &wkc->modul;
  }else if(!strcmp(str, "network")){
    res = &wkc->network;
  }
  return res;
}

variable_types:
str
char *

variable_types: struct lwp_wkc_struct { struct lwp_resource_struct info; struct lwp_resource_struct device; struct lwp_resource_struct modul; struct lwp_resource_struct network; struct lwp_resource_struct database; struct lwp_resource_struct logger; struct lwp_resource_struct channel[20]; int channels; } *

return_type: struct lwp_resource_struct { char name[100]; int attributes; struct lwp_attr_struct { char name[100]; } attr[10]; } *


decompiled_code:
__int64 __fastcall lwp_add_attr(char *src, __int64 a2)
{
int v2; // eax
__int64 result; // rax

v2 = *(_DWORD *)(a2 + 100);
*(_DWORD *)(a2 + 100) = v2 + 1;
if ( v2 + 1 > 9 )
{
*(_DWORD *)(a2 + 100) = v2;
result = 0xFFFFFFFFLL;
}
else
{
strncpy((char *)(a2 + 100LL * v2 + 104), src, 0x64uLL);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *src, __int64 a2)
{
int v2; // eax
__int64 result; // rax

v2 = *(_DWORD *)(a2 + 100);
*(_DWORD *)(a2 + 100) = v2 + 1;
if ( v2 + 1 > 9 )
{
*(_DWORD *)(a2 + 100) = v2;
result = 0xFFFFFFFFLL;
}
else
{
strncpy((char *)(a2 + 100LL * v2 + 104), src, 0x64uLL);
result = 0LL;
}
return result;
}

original_code:
int lwp_add_attr(char* name, lwp_resource *res){
  res->attributes += 1;
  if(res->attributes >= 10){
    res->attributes -= 1;
    return -1;
  }
  strncpy(res->attr[res->attributes -1].name, name, 100);
  return 0;
}

canonical_original_code:
int lwp_add_attr(char *name, struct lwp_resource_struct *res){
  res->attributes += 1;
  if(res->attributes >= 10){
    res->attributes -= 1;
    return -1;
  }
  strncpy(res->attr[res->attributes -1].name, name, 100);
  return 0;
}

variable_types:
name
char *

user_defined_types: struct lwp_attr_struct { char name[100]; }


decompiled_code:
__int64 __fastcall lwp_parse_wkc(__int64 a1, __int64 a2)
{
__int64 v2; // r14
__int64 v3; // rbx
char *v4; // rbp
int i; // ebx
__int64 j; // rax
int v8; // esi
char v9; // cl
int v10; // eax
char *v11; // rdi
char *v12; // rax
__int64 k; // rax
int v14; // ecx
char v15; // dl
int v16; // er14
char *v17; // rdx
char *dest; // [rsp+8h] [rbp-520h]
int v19; // [rsp+10h] [rbp-518h]
__int64 v20; // [rsp+18h] [rbp-510h]
char s[1008]; // [rsp+20h] [rbp-508h] BYREF
char v22[112]; // [rsp+410h] [rbp-118h] BYREF
char src[168]; // [rsp+480h] [rbp-A8h] BYREF

if ( !*(_BYTE *)a1 )
{
lielas_log("error parsing wkc, empty wkc string", 1LL);
return 0xFFFFFFFFLL;
}
v2 = a2;
*(_DWORD *)(a2 + 28704) = 0;
lwp_init_resource(a2);
lwp_init_resource(a2 + 1104);
lwp_init_resource(a2 + 2208);
lwp_init_resource(a2 + 3312);
lwp_init_resource(a2 + 4416);
lwp_init_resource(a2 + 5520);
v3 = a2 + 6624;
do
{
lwp_init_resource(v3);
v3 += 1104LL;
}
while ( v3 != a2 + 28704 );
v4 = strchr((const char *)(a1 + 1), 60);
v19 = strlen(v4);
for ( i = 0; ; ++i )
{
v17 = &v4[i];
if ( *v17 == 60 && v4[i + 1] == 47 )
{
if ( v19 <= i + 2 )
{
v8 = 0;
}
else
{
for ( j = 0LL; ; ++j )
{
v8 = j;
v9 = v17[j + 2];
if ( v9 == 62 )
break;
src[j] = v9;
v8 = j + 1;
if ( j == 99 || j == v19 - 3 - i )
break;
}
}
src[v8] = 0;
dest = (char *)lwp_get_res_by_name(src, v2);
if ( !memcmp(src, "channel", 7uLL) )
{
v10 = *(_DWORD *)(v2 + 28704) + 1;
if ( v10 > 19 )
goto LABEL_17;
*(_DWORD *)(v2 + 28704) = v10;
}
if ( !dest )
{
LABEL_17:
snprintf(s, 0x3E8uLL, "unknown resource parsing .well-known/core: %s", src);
lielas_log(s, 1LL);
i += 2;
goto LABEL_45;
}
strncpy(dest, src, 0x64uLL);
v11 = strstr(&v4[i + 2], "title");
if ( !v11 )
{
lielas_log("error parsing wkc, title token not found", 1LL);
return 0xFFFFFFFFLL;
}
v12 = strchr(v11, 34);
if ( !v12 )
{
lielas_log("error parsing wkc, title value not found", 1LL);
return 0xFFFFFFFFLL;
}
i = (_DWORD)v12 - (_DWORD)v4 + 1;
v20 = v2;
do
{
if ( v19 <= i )
{
v14 = 0;
}
else
{
for ( k = 0LL; ; ++k )
{
v14 = k;
v15 = v4[i + k];
if ( v15 == 32 )
break;
if ( v15 == 10 || v15 == 34 || v15 == 124 )
{
v22[(int)k] = 0;
i += k + 1;
if ( v22[0] )
{
v16 = 1;
goto LABEL_37;
}
v2 = v20;
goto LABEL_45;
}
v22[k] = v15;
v14 = k + 1;
if ( k == 99 || v19 - 1 - i == k )
break;
}
}
v22[v14] = 0;
i += v14 + 1;
if ( v22[0] )
{
v16 = 0;
LABEL_37:
if ( (unsigned int)lwp_add_attr(v22, (__int64)dest) )
{
lielas_log("error parsing wkc, can't add attribute", 1LL);
return 0xFFFFFFFFLL;
}
}
else
{
v16 = 0;
}
}
while ( !v16 );
v2 = v20;
}
LABEL_45:
if ( !v4[i] || i > v19 )
break;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2)
{
__int64 v2; // r14
__int64 v3; // rbx
char *v4; // rbp
int i; // ebx
__int64 j; // rax
int v8; // esi
char v9; // cl
int v10; // eax
char *v11; // rdi
char *v12; // rax
__int64 k; // rax
int v14; // ecx
char v15; // dl
int v16; // er14
char *v17; // rdx
char *dest; // [rsp+8h] [rbp-520h]
int v19; // [rsp+10h] [rbp-518h]
__int64 v20; // [rsp+18h] [rbp-510h]
char s[1008]; // [rsp+20h] [rbp-508h] BYREF
char v22[112]; // [rsp+410h] [rbp-118h] BYREF
char src[168]; // [rsp+480h] [rbp-A8h] BYREF

if ( !*(_BYTE *)a1 )
{
lielas_log("error parsing wkc, empty wkc string", 1LL);
return 0xFFFFFFFFLL;
}
v2 = a2;
*(_DWORD *)(a2 + 28704) = 0;
func0(a2);
func0(a2 + 1104);
func0(a2 + 2208);
func0(a2 + 3312);
func0(a2 + 4416);
func0(a2 + 5520);
v3 = a2 + 6624;
do
{
func0(v3);
v3 += 1104LL;
}
while ( v3 != a2 + 28704 );
v4 = strchr((const char *)(a1 + 1), 60);
v19 = strlen(v4);
for ( i = 0; ; ++i )
{
v17 = &v4[i];
if ( *v17 == 60 && v4[i + 1] == 47 )
{
if ( v19 <= i + 2 )
{
v8 = 0;
}
else
{
for ( j = 0LL; ; ++j )
{
v8 = j;
v9 = v17[j + 2];
if ( v9 == 62 )
break;
src[j] = v9;
v8 = j + 1;
if ( j == 99 || j == v19 - 3 - i )
break;
}
}
src[v8] = 0;
dest = (char *)func3(src, v2);
if ( !memcmp(src, "channel", 7uLL) )
{
v10 = *(_DWORD *)(v2 + 28704) + 1;
if ( v10 > 19 )
goto LABEL_17;
*(_DWORD *)(v2 + 28704) = v10;
}
if ( !dest )
{
LABEL_17:
snprintf(s, 0x3E8uLL, "unknown resource parsing .well-known/core: %s", src);
lielas_log(s, 1LL);
i += 2;
goto LABEL_45;
}
strncpy(dest, src, 0x64uLL);
v11 = strstr(&v4[i + 2], "title");
if ( !v11 )
{
lielas_log("error parsing wkc, title token not found", 1LL);
return 0xFFFFFFFFLL;
}
v12 = strchr(v11, 34);
if ( !v12 )
{
lielas_log("error parsing wkc, title value not found", 1LL);
return 0xFFFFFFFFLL;
}
i = (_DWORD)v12 - (_DWORD)v4 + 1;
v20 = v2;
do
{
if ( v19 <= i )
{
v14 = 0;
}
else
{
for ( k = 0LL; ; ++k )
{
v14 = k;
v15 = v4[i + k];
if ( v15 == 32 )
break;
if ( v15 == 10 || v15 == 34 || v15 == 124 )
{
v22[(int)k] = 0;
i += k + 1;
if ( v22[0] )
{
v16 = 1;
goto LABEL_37;
}
v2 = v20;
goto LABEL_45;
}
v22[k] = v15;
v14 = k + 1;
if ( k == 99 || v19 - 1 - i == k )
break;
}
}
v22[v14] = 0;
i += v14 + 1;
if ( v22[0] )
{
v16 = 0;
LABEL_37:
if ( (unsigned int)func4(v22, (__int64)dest) )
{
lielas_log("error parsing wkc, can't add attribute", 1LL);
return 0xFFFFFFFFLL;
}
}
else
{
v16 = 0;
}
}
while ( !v16 );
v2 = v20;
}
LABEL_45:
if ( !v4[i] || i > v19 )
break;
}
return 0LL;
}

original_code:
int lwp_parse_wkc(char *str, lwp_wkc *wkc){
  char resname[100];
  char attrname[100];
  char *title;
  char log[1000];
  int pos = 0;
  int eof = 0;
  int eoa = 0;
  int len;
  int i;
  lwp_resource *res;
  if(str[0] == 0){
    lielas_log((unsigned char*)"error parsing wkc, empty wkc string", 1);
    return -1;
  }
  wkc->channels = 0;
  lwp_init_resource(&wkc->info);
  lwp_init_resource(&wkc->device);
  lwp_init_resource(&wkc->modul);
  lwp_init_resource(&wkc->network);
  lwp_init_resource(&wkc->database);
  lwp_init_resource(&wkc->logger);
  for(i = 0; i < 20; i++){
    lwp_init_resource(&wkc->channel[i]);
  }
  str = strchr(&str[1], '<');
  len = strlen(str);
  while(!eof){
    if(str[pos] == '<' && str[pos+1] == '/'){
      pos += 2;
      for(i = 0; i < 100 && ((pos+i) < len); i++){
        if(str[pos+i] == '>'){
          break;
        }
        resname[i] = str[pos+i];
      }
      resname[i] = 0;
      res = lwp_get_res_by_name(resname, wkc);
      if(strncmp(resname, "channel", strlen("channel")) == 0){
        wkc->channels += 1;
        if( wkc->channels >= 20){
          wkc->channels -= 1;
          res = 0;
        }
      }
      if(res == 0){
        snprintf(log, 1000, "unknown resource parsing .well-known/core: %s", resname);
        lielas_log((unsigned char*)log, 1);
      }else{
        strncpy(res->name, resname, 100);
        title = strstr(&str[pos], "title");
        if(title == ((void *)0)){
          lielas_log((unsigned char*)"error parsing wkc, title token not found", 1);
          return -1;
        }
        title = strchr(title, '"');
        if(title == ((void *)0)){
          lielas_log((unsigned char*)"error parsing wkc, title value not found", 1);
          return -1;
        }
        pos = title - str + 1;
        eoa = 0;
        i = 0;
        while(!eoa){
          for(i = 0; i < 100 && ((pos+i) < len); i++){
            if( str[pos+i] == ' '){
              break;
            }else if(str[pos+i] == '"' || str[pos+i] == ' ' || str[pos+i] == '\n' || str[pos+i] == '|'){
              eoa = 1;
              break;
            }
            attrname[i] = str[pos+i];
          }
          attrname[i] = 0;
          pos += i + 1;
          if( strlen(attrname) > 0){
            if(lwp_add_attr(attrname, res) != 0){
              lielas_log((unsigned char*)"error parsing wkc, can't add attribute", 1);
              return -1;
            }
          }
        }
      }
    }
    if(str[pos] == 0 || pos > len){
      eof = 1;
    }
    pos += 1;
  }
  return 0;
}

canonical_original_code:
int lwp_parse_wkc(char *str, struct lwp_wkc_struct *wkc){
  char resname[100];
  char attrname[100];
  char *title;
  char log[1000];
  int pos = 0;
  int eof = 0;
  int eoa = 0;
  int len;
  int i;
  struct lwp_resource_struct *res;
  if(str[0] == 0){
    lielas_log((unsigned char *)"error parsing wkc, empty wkc string", 1);
    return -1;
  }
  wkc->channels = 0;
  lwp_init_resource(&wkc->info);
  lwp_init_resource(&wkc->device);
  lwp_init_resource(&wkc->modul);
  lwp_init_resource(&wkc->network);
  lwp_init_resource(&wkc->database);
  lwp_init_resource(&wkc->logger);
  for(i = 0; i < 20; i++){
    lwp_init_resource(&wkc->channel[i]);
  }
  str = strchr(&str[1], '<');
  len = strlen(str);
  while(!eof){
    if(str[pos] == '<' && str[pos+1] == '/'){
      pos += 2;
      for(i = 0; i < 100 && ((pos+i) < len); i++){
        if(str[pos+i] == '>'){
          break;
        }
        resname[i] = str[pos+i];
      }
      resname[i] = 0;
      res = lwp_get_res_by_name(resname, wkc);
      if(strncmp(resname, "channel", strlen("channel")) == 0){
        wkc->channels += 1;
        if( wkc->channels >= 20){
          wkc->channels -= 1;
          res = 0;
        }
      }
      if(res == 0){
        snprintf(log, 1000, "unknown resource parsing .well-known/core: %s", resname);
        lielas_log((unsigned char *)log, 1);
      }else{
        strncpy(res->name, resname, 100);
        title = strstr(&str[pos], "title");
        if(title == ((void *)0)){
          lielas_log((unsigned char *)"error parsing wkc, title token not found", 1);
          return -1;
        }
        title = strchr(title, '"');
        if(title == ((void *)0)){
          lielas_log((unsigned char *)"error parsing wkc, title value not found", 1);
          return -1;
        }
        pos = title - str + 1;
        eoa = 0;
        i = 0;
        while(!eoa){
          for(i = 0; i < 100 && ((pos+i) < len); i++){
            if( str[pos+i] == ' '){
              break;
            }else if(str[pos+i] == '"' || str[pos+i] == ' ' || str[pos+i] == '\n' || str[pos+i] == '|'){
              eoa = 1;
              break;
            }
            attrname[i] = str[pos+i];
          }
          attrname[i] = 0;
          pos += i + 1;
          if( strlen(attrname) > 0){
            if(lwp_add_attr(attrname, res) != 0){
              lielas_log((unsigned char *)"error parsing wkc, can't add attribute", 1);
              return -1;
            }
          }
        }
      }
    }
    if(str[pos] == 0 || pos > len){
      eof = 1;
    }
    pos += 1;
  }
  return 0;
}

variable_types:
resname
char [100]

variable_types:
attrname
char [100]

variable_types:
title
char *

variable_types:
log
char [1000]

variable_types: pos int

variable_types: eof int

variable_types: eoa int

variable_types: len int

variable_types: i int


original_code:
void lwp_print_wkc(lwp_wkc *wkc, char* str){
  int i, j;
  int pos = 0;
  lwp_resource *res;
  pos += snprintf(&str[pos], 2000 - pos, ".well-known/core:\n");
  res = &wkc->info;
  pos += snprintf(&str[pos], 2000 - pos, "Resource %s:\n", res->name);
  for(i = 0; i < res->attributes; i++){
    pos += snprintf(&str[pos], 2000 - pos, "\t Attribute %s with rank %i of %i\n", res->attr[i].name, i, res->attributes);
  }
  res = &wkc->device;
  pos += snprintf(&str[pos], 2000 - pos, "Resource %s:\n", res->name);
  for(i = 0; i < res->attributes; i++){
    pos += snprintf(&str[pos], 2000 - pos, "\t Attribute %s with rank %i of %i\n", res->attr[i].name, i, res->attributes);
  }
  res = &wkc->modul;
  pos += snprintf(&str[pos], 2000 - pos, "Resource %s:\n", res->name);
  for(i = 0; i < res->attributes; i++){
    pos += snprintf(&str[pos], 2000 - pos, "\t Attribute %s with rank %i of %i\n", res->attr[i].name, i, res->attributes);
  }
  res = &wkc->network;
  pos += snprintf(&str[pos], 2000 - pos, "Resource %s:\n", res->name);
  for(i = 0; i < res->attributes; i++){
    pos += snprintf(&str[pos], 2000 - pos, "\t Attribute %s with rank %i of %i\n", res->attr[i].name, i, res->attributes);
  }
  res = &wkc->database;
  pos += snprintf(&str[pos], 2000 - pos, "Resource %s:\n", res->name);
  for(i = 0; i < res->attributes; i++){
    pos += snprintf(&str[pos], 2000 - pos, "\t Attribute %s with rank %i of %i\n", res->attr[i].name, i, res->attributes);
  }
  for(j = 1; j <= wkc->channels && j < 20; j++){
    res = &wkc->channel[j];
    pos += snprintf(&str[pos], 2000 - pos, "Resource %s:\n", res->name);
    for(i = 0; i < res->attributes; i++){
      pos += snprintf(&str[pos], 2000 - pos, "\t Attribute %s with rank %i of %i\n", res->attr[i].name, i, res->attributes);
    }
  }
  res = &wkc->logger;
  pos += snprintf(&str[pos], 2000 - pos, "Resource %s:\n", res->name);
  for(i = 0; i < res->attributes; i++){
    pos += snprintf(&str[pos], 2000 - pos, "\t Attribute %s with rank %i of %i\n", res->attr[i].name, i, res->attributes);
  }
}

canonical_original_code:
void lwp_print_wkc(struct lwp_wkc_struct *wkc, char *str){
  int i; int j;
  int pos = 0;
  struct lwp_resource_struct *res;
  pos += snprintf(&str[pos], 2000 - pos, ".well-known/core:\n");
  res = &wkc->info;
  pos += snprintf(&str[pos], 2000 - pos, "Resource %s:\n", res->name);
  for(i = 0; i < res->attributes; i++){
    pos += snprintf(&str[pos], 2000 - pos, "\t Attribute %s with rank %i of %i\n", res->attr[i].name, i, res->attributes);
  }
  res = &wkc->device;
  pos += snprintf(&str[pos], 2000 - pos, "Resource %s:\n", res->name);
  for(i = 0; i < res->attributes; i++){
    pos += snprintf(&str[pos], 2000 - pos, "\t Attribute %s with rank %i of %i\n", res->attr[i].name, i, res->attributes);
  }
  res = &wkc->modul;
  pos += snprintf(&str[pos], 2000 - pos, "Resource %s:\n", res->name);
  for(i = 0; i < res->attributes; i++){
    pos += snprintf(&str[pos], 2000 - pos, "\t Attribute %s with rank %i of %i\n", res->attr[i].name, i, res->attributes);
  }
  res = &wkc->network;
  pos += snprintf(&str[pos], 2000 - pos, "Resource %s:\n", res->name);
  for(i = 0; i < res->attributes; i++){
    pos += snprintf(&str[pos], 2000 - pos, "\t Attribute %s with rank %i of %i\n", res->attr[i].name, i, res->attributes);
  }
  res = &wkc->database;
  pos += snprintf(&str[pos], 2000 - pos, "Resource %s:\n", res->name);
  for(i = 0; i < res->attributes; i++){
    pos += snprintf(&str[pos], 2000 - pos, "\t Attribute %s with rank %i of %i\n", res->attr[i].name, i, res->attributes);
  }
  for(j = 1; j <= wkc->channels && j < 20; j++){
    res = &wkc->channel[j];
    pos += snprintf(&str[pos], 2000 - pos, "Resource %s:\n", res->name);
    for(i = 0; i < res->attributes; i++){
      pos += snprintf(&str[pos], 2000 - pos, "\t Attribute %s with rank %i of %i\n", res->attr[i].name, i, res->attributes);
    }
  }
  res = &wkc->logger;
  pos += snprintf(&str[pos], 2000 - pos, "Resource %s:\n", res->name);
  for(i = 0; i < res->attributes; i++){
    pos += snprintf(&str[pos], 2000 - pos, "\t Attribute %s with rank %i of %i\n", res->attr[i].name, i, res->attributes);
  }
}

variable_types: j int


decompiled_code:
__int64 __fastcall lwp_get_attr_value(__int64 a1, __int64 a2, const char *a3, _BYTE *a4, int a5)
{
int v5; // er14
const char *v8; // rbp
int v9; // ebx
__int64 v10; // rax
int v11; // ecx
char v12; // dl
__int64 i; // rax
char v14; // dl
__int64 result; // rax

v5 = *(_DWORD *)(a2 + 100);
if ( v5 <= 0 )
return 0xFFFFFFFFLL;
v8 = (const char *)(a2 + 104);
v9 = 0;
do
{
if ( !strcmp(v8, a3) )
break;
++v9;
v8 += 100;
if ( v9 > 9 )
break;
}
while ( v9 < v5 );
if ( v5 <= v9 || v9 > 9 )
return 0xFFFFFFFFLL;
v10 = 0LL;
while ( 1 )
{
v11 = v10;
if ( v9 <= 0 )
break;
v12 = *(_BYTE *)(a1 + v10);
if ( (unsigned __int8)(v12 - 9) <= 1u || !v12 )
--v9;
if ( a5 <= (int)++v10 )
return 0xFFFFFFFFLL;
}
if ( (int)v10 >= a5 )
goto LABEL_22;
for ( i = 0LL; ; a4[i++] = v14 )
{
v14 = *(_BYTE *)(v11 + a1 + i);
if ( (unsigned __int8)(v14 - 9) <= 1u || !v14 )
break;
}
if ( (int)i + v11 >= a5 )
{
LABEL_22:
*a4 = 0;
result = 0xFFFFFFFFLL;
}
else
{
a4[(int)i] = 0;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2, const char *a3, _BYTE *a4, int a5)
{
int v5; // er14
const char *v8; // rbp
int v9; // ebx
__int64 v10; // rax
int v11; // ecx
char v12; // dl
__int64 i; // rax
char v14; // dl
__int64 result; // rax

v5 = *(_DWORD *)(a2 + 100);
if ( v5 <= 0 )
return 0xFFFFFFFFLL;
v8 = (const char *)(a2 + 104);
v9 = 0;
do
{
if ( !strcmp(v8, a3) )
break;
++v9;
v8 += 100;
if ( v9 > 9 )
break;
}
while ( v9 < v5 );
if ( v5 <= v9 || v9 > 9 )
return 0xFFFFFFFFLL;
v10 = 0LL;
while ( 1 )
{
v11 = v10;
if ( v9 <= 0 )
break;
v12 = *(_BYTE *)(a1 + v10);
if ( (unsigned __int8)(v12 - 9) <= 1u || !v12 )
--v9;
if ( a5 <= (int)++v10 )
return 0xFFFFFFFFLL;
}
if ( (int)v10 >= a5 )
goto LABEL_22;
for ( i = 0LL; ; a4[i++] = v14 )
{
v14 = *(_BYTE *)(v11 + a1 + i);
if ( (unsigned __int8)(v14 - 9) <= 1u || !v14 )
break;
}
if ( (int)i + v11 >= a5 )
{
LABEL_22:
*a4 = 0;
result = 0xFFFFFFFFLL;
}
else
{
a4[(int)i] = 0;
result = 0LL;
}
return result;
}

original_code:
int lwp_get_attr_value(char *str, lwp_resource *res, char *attr_name, char *val, int size){
  int rank;
  int pos =0 ;
  int i;
  for(rank=0; rank < res->attributes && rank < 10; rank++){
    if(strcmp(res->attr[rank].name, attr_name)== 0){
      break;
    }
  }
  if(rank >= 10 || rank >= res->attributes){
    return -1;
  }
  while(rank > 0){
    if(str[pos] == '\t' || str[pos] == '\n' || str[pos] == 0){
      rank -= 1;
    }
    pos += 1;
    if(pos >= size){
      return -1;
    }
  }
  for( i = 0; pos < size; i++){
    if(str[pos+i] == '\t' || str[pos+i] == '\n' || str[pos+i] == 0){
      break;
    }
    val[i] = str[pos + i];
  }
  if((pos+i) >= size){
    val[0] = 0;
    return -1;
  }
  val[i] = 0;
  return 0;
}

canonical_original_code:
int lwp_get_attr_value(char *str, struct lwp_resource_struct *res, char *attr_name, char *val, int size){
  int rank;
  int pos = 0;
  int i;
  for(rank=0; rank < res->attributes && rank < 10; rank++){
    if(strcmp(res->attr[rank].name, attr_name)== 0){
      break;
    }
  }
  if(rank >= 10 || rank >= res->attributes){
    return -1;
  }
  while(rank > 0){
    if(str[pos] == '\t' || str[pos] == '\n' || str[pos] == 0){
      rank -= 1;
    }
    pos += 1;
    if(pos >= size){
      return -1;
    }
  }
  for( i = 0; pos < size; i++){
    if(str[pos+i] == '\t' || str[pos+i] == '\n' || str[pos+i] == 0){
      break;
    }
    val[i] = str[pos + i];
  }
  if((pos+i) >= size){
    val[0] = 0;
    return -1;
  }
  val[i] = 0;
  return 0;
}

variable_types:
val
char *

variable_types: size int

variable_types: rank int


variable_types:
cdt
unsigned char *

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; } *


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

pthread_mutex_lock(&rtcmutex);
v0 = rtc_state;
pthread_mutex_unlock(&rtcmutex);
return v0;
}

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

pthread_mutex_lock(&rtcmutex);
v0 = rtc_state;
pthread_mutex_unlock(&rtcmutex);
return v0;
}

original_code:
int rtc_get_state(){
  int state;
  pthread_mutex_lock(&rtcmutex);
  state = rtc_state;
  pthread_mutex_unlock(&rtcmutex);
  return state;
}

variable_types: state int


decompiled_code:
const char *rtc_get_state_text()
{
int v0; // ebx
const char *result; // rax

pthread_mutex_lock(&rtcmutex);
v0 = rtc_state;
pthread_mutex_unlock(&rtcmutex);
result = "uninitialized";
if ( v0 != 1 )
{
result = "not synced";
if ( v0 != 2 )
{
result = "running";
if ( v0 )
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
const char *func1()
{
int v0; // ebx
const char *result; // rax

pthread_mutex_lock(&rtcmutex);
v0 = rtc_state;
pthread_mutex_unlock(&rtcmutex);
result = "uninitialized";
if ( v0 != 1 )
{
result = "not synced";
if ( v0 != 2 )
{
result = "running";
if ( v0 )
result = 0LL;
}
}
return result;
}

original_code:
const char *rtc_get_state_text(){
  int state;
  pthread_mutex_lock(&rtcmutex);
  state = rtc_state;
  pthread_mutex_unlock(&rtcmutex);
  switch(state){
    case 1:
      return rtc_state_text_uninitialized;
    case 0:
      return rtc_state_text_ok;
    case 2:
      return rtc_state_text_not_synced;
  }
  return ((void *)0);
}

return_type: char *


testdt

decompiled_code:
__int64 __fastcall testdt(_DWORD *a1)
{
if ( a1[2] > 0x17u )
return 0xFFFFFFFFLL;
if ( a1[1] > 0x3Bu )
return 0xFFFFFFFFLL;
if ( *a1 > 0x3Bu )
return 0xFFFFFFFFLL;
if ( (unsigned int)(a1[5] - 100) > 0x64 )
return 0xFFFFFFFFLL;
if ( a1[4] > 0xBu )
return 0xFFFFFFFFLL;
return (unsigned int)-((unsigned int)(a1[3] - 1) > 0x1E);
}

canonical_decompiled_code:
__int64 __fastcall func2(_DWORD *a1)
{
if ( a1[2] > 0x17u )
return 0xFFFFFFFFLL;
if ( a1[1] > 0x3Bu )
return 0xFFFFFFFFLL;
if ( *a1 > 0x3Bu )
return 0xFFFFFFFFLL;
if ( (unsigned int)(a1[5] - 100) > 0x64 )
return 0xFFFFFFFFLL;
if ( a1[4] > 0xBu )
return 0xFFFFFFFFLL;
return (unsigned int)-((unsigned int)(a1[3] - 1) > 0x1E);
}

original_code:
int testdt(struct tm *dt){
 if(dt->tm_hour < 0 || dt->tm_hour > 23)
  return -1;
 if(dt->tm_min < 0 || dt->tm_min > 59)
  return -1;
 if(dt->tm_sec < 0 || dt->tm_sec > 59)
  return -1;
 if(dt->tm_year < 100 || dt->tm_year > 200)
  return -1;
 if(dt->tm_mon < 0 || dt->tm_mon > 11)
  return -1;
 if(dt->tm_mday < 1 || dt->tm_mday > 31)
  return -1;
 return 0;
}

user_defined_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; }


decompiled_code:
__int64 rtc_init()
{
unsigned int v0; // ebx
struct tm *v1; // rax
int v2; // eax
char v4[100]; // [rsp+0h] [rbp-108h] BYREF
pthread_mutexattr_t attr; // [rsp+64h] [rbp-A4h] BYREF
time_t timer; // [rsp+68h] [rbp-A0h] BYREF
char v7[80]; // [rsp+70h] [rbp-98h] BYREF
int v8[18]; // [rsp+C0h] [rbp-48h] BYREF

attr.__align = 0;
rtc_state = 1;
pthread_mutexattr_settype(&attr, 1);
if ( pthread_mutex_init(&rtcmutex, &attr) )
{
lielas_log("Couldn't initialize mutex for rtc", 0LL);
return (unsigned int)-1;
}
if ( (unsigned int)rtc4162_init() )
return (unsigned int)-1;
if ( !(unsigned int)rtc4162_get(v8) && !(unsigned int)testdt(v8) )
{
lielas_log("rtc: rtc time valid, setting system time", 2LL);
snprintf(
v4,
0x64uLL,
"sudo date --set %.4i-%.2i-%.2i",
(unsigned int)(v8[5] + 1900),
(unsigned int)(v8[4] + 1),
(unsigned int)v8[3]);
if ( system(v4) )
return (unsigned int)-1;
snprintf(
v4,
0x64uLL,
"sudo date --set %.2i:%.2i:%.2i",
(unsigned int)v8[2],
(unsigned int)v8[1],
(unsigned int)v8[0]);
v0 = system(v4);
if ( v0 )
return (unsigned int)-1;
lielas_log("rtc: system time set", 2LL);
rtc_state = 0;
return v0;
}
lielas_log("rtc: rtc-module time not valid, check if system time is synced", 2LL);
if ( (unsigned int)ntp_gettimex(v7) )
{
lielas_log("rtc: error executing ntp_gettime()", 1LL);
goto LABEL_17;
}
if ( *_errno_location() == 5 )
{
LABEL_17:
lielas_log("rtc: rtc-module time not valid and ntp not synced", 1LL);
rtc_state = 2;
return (unsigned int)-1;
}
lielas_log("rtc: rtc-module time not valid but ntp synced, setting rtc-module time", 2LL);
time(&timer);
v1 = gmtime(&timer);
rtc4162_set(v1);
sleep(2u);
if ( (unsigned int)rtc4162_init() )
return (unsigned int)-1;
v2 = rtc4162_get(v8);
if ( !v2 )
v2 = testdt(v8);
return (unsigned int)-(v2 != 0);
}

canonical_decompiled_code:
__int64 func3()
{
unsigned int v0; // ebx
struct tm *v1; // rax
int v2; // eax
char v4[100]; // [rsp+0h] [rbp-108h] BYREF
pthread_mutexattr_t attr; // [rsp+64h] [rbp-A4h] BYREF
time_t timer; // [rsp+68h] [rbp-A0h] BYREF
char v7[80]; // [rsp+70h] [rbp-98h] BYREF
int v8[18]; // [rsp+C0h] [rbp-48h] BYREF

attr.__align = 0;
rtc_state = 1;
pthread_mutexattr_settype(&attr, 1);
if ( pthread_mutex_init(&rtcmutex, &attr) )
{
lielas_log("Couldn't initialize mutex for rtc", 0LL);
return (unsigned int)-1;
}
if ( (unsigned int)rtc4162_init() )
return (unsigned int)-1;
if ( !(unsigned int)rtc4162_get(v8) && !(unsigned int)func2(v8) )
{
lielas_log("rtc: rtc time valid, setting system time", 2LL);
snprintf(
v4,
0x64uLL,
"sudo date --set %.4i-%.2i-%.2i",
(unsigned int)(v8[5] + 1900),
(unsigned int)(v8[4] + 1),
(unsigned int)v8[3]);
if ( system(v4) )
return (unsigned int)-1;
snprintf(
v4,
0x64uLL,
"sudo date --set %.2i:%.2i:%.2i",
(unsigned int)v8[2],
(unsigned int)v8[1],
(unsigned int)v8[0]);
v0 = system(v4);
if ( v0 )
return (unsigned int)-1;
lielas_log("rtc: system time set", 2LL);
rtc_state = 0;
return v0;
}
lielas_log("rtc: rtc-module time not valid, check if system time is synced", 2LL);
if ( (unsigned int)ntp_gettimex(v7) )
{
lielas_log("rtc: error executing ntp_gettime()", 1LL);
goto LABEL_17;
}
if ( *_errno_location() == 5 )
{
LABEL_17:
lielas_log("rtc: rtc-module time not valid and ntp not synced", 1LL);
rtc_state = 2;
return (unsigned int)-1;
}
lielas_log("rtc: rtc-module time not valid but ntp synced, setting rtc-module time", 2LL);
time(&timer);
v1 = gmtime(&timer);
rtc4162_set(v1);
sleep(2u);
if ( (unsigned int)rtc4162_init() )
return (unsigned int)-1;
v2 = rtc4162_get(v8);
if ( !v2 )
v2 = func2(v8);
return (unsigned int)-(v2 != 0);
}

original_code:
int rtc_init(){
  struct tm dt;
  struct ntptimeval ntv;
 time_t rawtime;
 struct tm *now;
  int timenotvalid = 0;
  int ntpnotsynced = 0;
  pthread_mutexattr_t mutexAttr = {};
  char cmd[100];
  rtc_state = 1;
  pthread_mutexattr_settype(&mutexAttr, PTHREAD_MUTEX_RECURSIVE);
  if(pthread_mutex_init(&rtcmutex, &mutexAttr) != 0){
  lielas_log((unsigned char*)"Couldn't initialize mutex for rtc", 0);
  return -1;
  }
  if(rtc4162_init()){
    return -1;;
  }
  timenotvalid = rtc4162_get(&dt);
  if(!timenotvalid){
    timenotvalid = testdt(&dt);
  }
  if(timenotvalid){
    lielas_log((unsigned char*)"rtc: rtc-module time not valid, check if system time is synced", 2);
    if(ntp_gettime(&ntv)){
      lielas_log((unsigned char*)"rtc: error executing ntp_gettime()", 1);
      ntpnotsynced = 1;
    }
    if(ntpnotsynced || (*__errno_location ()) == 5){
      lielas_log((unsigned char*)"rtc: rtc-module time not valid and ntp not synced", 1);
      rtc_state = 2;
      return -1;;
    }else{
      lielas_log((unsigned char*)"rtc: rtc-module time not valid but ntp synced, setting rtc-module time", 2);
      time(&rawtime);
      now = gmtime(&rawtime);
      rtc4162_set(now);
      sleep(2);
      if(rtc4162_init()){
        return -1;;
      }
      timenotvalid = rtc4162_get(&dt);
      if(!timenotvalid){
        timenotvalid = testdt(&dt);
      }
      if(timenotvalid){
        return -1;;
      }
      return 0;
    }
    return -1;;
  }
  lielas_log((unsigned char*)"rtc: rtc time valid, setting system time", 2);
  snprintf(cmd, 100, "sudo date --set %.4i-%.2i-%.2i", (dt.tm_year+1900), (dt.tm_mon+1), dt.tm_mday);
  if(system(cmd)){
    return -1;;
  }
  snprintf(cmd, 100, "sudo date --set %.2i:%.2i:%.2i", dt.tm_hour, dt.tm_min, dt.tm_sec);
  if(system(cmd)){
    return -1;;
  }
  lielas_log((unsigned char*)"rtc: system time set", 2);
  rtc_state = 0;
  return 0;
}

canonical_original_code:
int rtc_init(){
  struct tm dt;
  struct ntptimeval ntv;
 long rawtime;
 struct tm *now;
  int timenotvalid = 0;
  int ntpnotsynced = 0;
  union pthread_mutexattr_t mutexAttr = {};
  char cmd[100];
  rtc_state = 1;
  pthread_mutexattr_settype(&mutexAttr, PTHREAD_MUTEX_RECURSIVE);
  if(pthread_mutex_init(&rtcmutex, &mutexAttr) != 0){
  lielas_log((unsigned char *)"Couldn't initialize mutex for rtc", 0);
  return -1;
  }
  if(rtc4162_init()){
    return -1;;
  }
  timenotvalid = rtc4162_get(&dt);
  if(!timenotvalid){
    timenotvalid = testdt(&dt);
  }
  if(timenotvalid){
    lielas_log((unsigned char *)"rtc: rtc-module time not valid, check if system time is synced", 2);
    if(ntp_gettime(&ntv)){
      lielas_log((unsigned char *)"rtc: error executing ntp_gettime()", 1);
      ntpnotsynced = 1;
    }
    if(ntpnotsynced || (*__errno_location ()) == 5){
      lielas_log((unsigned char *)"rtc: rtc-module time not valid and ntp not synced", 1);
      rtc_state = 2;
      return -1;;
    }else{
      lielas_log((unsigned char *)"rtc: rtc-module time not valid but ntp synced, setting rtc-module time", 2);
      time(&rawtime);
      now = gmtime(&rawtime);
      rtc4162_set(now);
      sleep(2);
      if(rtc4162_init()){
        return -1;;
      }
      timenotvalid = rtc4162_get(&dt);
      if(!timenotvalid){
        timenotvalid = testdt(&dt);
      }
      if(timenotvalid){
        return -1;;
      }
      return 0;
    }
    return -1;;
  }
  lielas_log((unsigned char *)"rtc: rtc time valid, setting system time", 2);
  snprintf(cmd, 100, "sudo date --set %.4i-%.2i-%.2i", (dt.tm_year+1900), (dt.tm_mon+1), dt.tm_mday);
  if(system(cmd)){
    return -1;;
  }
  snprintf(cmd, 100, "sudo date --set %.2i:%.2i:%.2i", dt.tm_hour, dt.tm_min, dt.tm_sec);
  if(system(cmd)){
    return -1;;
  }
  lielas_log((unsigned char *)"rtc: system time set", 2);
  rtc_state = 0;
  return 0;
}

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; }

variable_types: struct ntptimeval { struct timeval { long tv_sec; long tv_usec; } time; long maxerror; long esterror; long tai; long __glibc_reserved1; long __glibc_reserved2; long __glibc_reserved3; long __glibc_reserved4; }

variable_types: rawtime long

variable_types: timenotvalid int

variable_types: ntpnotsynced int

variable_types: union pthread_mutexattr_t { char __size[4]; int __align; }

variable_types:
cmd
char [100]

user_defined_types: struct ntptimeval { struct timeval time; long maxerror; long esterror; long tai; long __glibc_reserved1; long __glibc_reserved2; long __glibc_reserved3; long __glibc_reserved4; }


decompiled_code:
__int64 __fastcall rtc_set_dt(unsigned int *a1)
{
unsigned int v1; // ebp
char v3[136]; // [rsp+0h] [rbp-88h] BYREF

pthread_mutex_lock(&rtcmutex);
lielas_log("rtc: setting rtc time", 2LL);
rtc4162_set(a1);
v1 = rtc4162_get(a1);
if ( !v1 )
v1 = testdt(a1);
lielas_log("rtc: setting system time", 2LL);
snprintf(v3, 0x64uLL, "sudo date --set %.4i-%.2i-%.2i", a1[5] + 1900, a1[4] + 1, a1[3]);
if ( system(v3) )
return (unsigned int)-1;
snprintf(v3, 0x64uLL, "sudo date --set %.2i:%.2i:%.2i", a1[2], a1[1], *a1);
if ( system(v3) )
return (unsigned int)-1;
rtc_state = 0;
pthread_mutex_unlock(&rtcmutex);
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int *a1)
{
unsigned int v1; // ebp
char v3[136]; // [rsp+0h] [rbp-88h] BYREF

pthread_mutex_lock(&rtcmutex);
lielas_log("rtc: setting rtc time", 2LL);
rtc4162_set(a1);
v1 = rtc4162_get(a1);
if ( !v1 )
v1 = func2(a1);
lielas_log("rtc: setting system time", 2LL);
snprintf(v3, 0x64uLL, "sudo date --set %.4i-%.2i-%.2i", a1[5] + 1900, a1[4] + 1, a1[3]);
if ( system(v3) )
return (unsigned int)-1;
snprintf(v3, 0x64uLL, "sudo date --set %.2i:%.2i:%.2i", a1[2], a1[1], *a1);
if ( system(v3) )
return (unsigned int)-1;
rtc_state = 0;
pthread_mutex_unlock(&rtcmutex);
return v1;
}

original_code:
int rtc_set_dt(struct tm *dt){
  int timenotvalid = 0;
  char cmd[100];
  pthread_mutex_lock(&rtcmutex);
  lielas_log((unsigned char*)"rtc: setting rtc time", 2);
  rtc4162_set(dt);
  timenotvalid = rtc4162_get(dt);
  if(!timenotvalid){
    timenotvalid = testdt(dt);
  }
  lielas_log((unsigned char*)"rtc: setting system time", 2);
  snprintf(cmd, 100, "sudo date --set %.4i-%.2i-%.2i", (dt->tm_year+1900), (dt->tm_mon+1), dt->tm_mday);
  if(system(cmd)){
    return -1;;
  }
  snprintf(cmd, 100, "sudo date --set %.2i:%.2i:%.2i", dt->tm_hour, dt->tm_min, dt->tm_sec);
  if(system(cmd)){
    return -1;;
  }
  rtc_state = 0;
  pthread_mutex_unlock(&rtcmutex);
  return timenotvalid;
}

canonical_original_code:
int rtc_set_dt(struct tm *dt){
  int timenotvalid = 0;
  char cmd[100];
  pthread_mutex_lock(&rtcmutex);
  lielas_log((unsigned char *)"rtc: setting rtc time", 2);
  rtc4162_set(dt);
  timenotvalid = rtc4162_get(dt);
  if(!timenotvalid){
    timenotvalid = testdt(dt);
  }
  lielas_log((unsigned char *)"rtc: setting system time", 2);
  snprintf(cmd, 100, "sudo date --set %.4i-%.2i-%.2i", (dt->tm_year+1900), (dt->tm_mon+1), dt->tm_mday);
  if(system(cmd)){
    return -1;;
  }
  snprintf(cmd, 100, "sudo date --set %.2i:%.2i:%.2i", dt->tm_hour, dt->tm_min, dt->tm_sec);
  if(system(cmd)){
    return -1;;
  }
  rtc_state = 0;
  pthread_mutex_unlock(&rtcmutex);
  return timenotvalid;
}


LcreateChannel

decompiled_code:
_BYTE *LcreateChannel()
{
_BYTE *result; // rax

result = malloc(0x108uLL);
*(_DWORD *)result = 0;
result[4] = 0;
result[54] = 0;
result[154] = 0;
result[204] = 0;
*((_QWORD *)result + 32) = 0LL;
return result;
}

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

result = malloc(0x108uLL);
*(_DWORD *)result = 0;
result[4] = 0;
result[54] = 0;
result[154] = 0;
result[204] = 0;
*((_QWORD *)result + 32) = 0LL;
return result;
}

original_code:
Lchannel *LcreateChannel(){
 Lchannel *channel = malloc(sizeof(Lchannel));
 channel->id = 0;
 channel->address[0] = 0;
 channel->type[0] = 0;
 channel->unit[0] = 0;
  channel->class[0] = 0;
  channel->exponent = 0.0;
 return channel;
}

canonical_original_code:
struct Lchannel_struct *LcreateChannel(){
 struct Lchannel_struct *channel = malloc(sizeof(Lchannel));
 channel->id = 0;
 channel->address[0] = 0;
 channel->type[0] = 0;
 channel->unit[0] = 0;
  channel->class[0] = 0;
  channel->exponent = 0.0;
 return channel;
}

variable_types: struct Lchannel_struct { unsigned int id; char address[50]; char type[50]; char sensor[50]; char unit[50]; char class[50]; double exponent; } *


LdeleteChannel

decompiled_code:
void __fastcall LdeleteChannel(void *a1)
{
free(a1);
}

canonical_decompiled_code:
void __fastcall func1(void *a1)
{
free(a1);
}

original_code:
void LdeleteChannel(Lchannel *c){
 free(c);
}

canonical_original_code:
void LdeleteChannel(struct Lchannel_struct *c){
 free(c);
}

user_defined_types: struct Lchannel_struct { unsigned int id; char address[50]; char type[50]; char sensor[50]; char unit[50]; char class[50]; double exponent; }


LcreateDevice

original_code:
Ldevice *LcreateDevice(){
 Ldevice *d = malloc(sizeof(Ldevice));
 if(d == ((void *)0)){
  lielas_log((unsigned char*)"Can't allocate memory for new device", 0);
  return ((void *)0);
 }
 d->id = 0;
 d->registered = 0;
 d->address[0] = 0;
  d->mac[0] = 0;
 d->mint[0] = 0;
 d->pint[0] = 0;
 d->aint[0] = 0;
 d->moduls = 0;
 d->modul[0] = 0;
  d->name[0] = 0;
  d->sw_ver[0] = 0;
  d->supply[0] = 0;
 d->event = 0;
  lwp_init_wkc(&d->wkc);
 return d;
}

canonical_original_code:
struct Ldevice_struct *LcreateDevice(){
 struct Ldevice_struct *d = malloc(sizeof(Ldevice));
 if(d == ((void *)0)){
  lielas_log((unsigned char *)"Can't allocate memory for new device", 0);
  return ((void *)0);
 }
 d->id = 0;
 d->registered = 0;
 d->address[0] = 0;
  d->mac[0] = 0;
 d->mint[0] = 0;
 d->pint[0] = 0;
 d->aint[0] = 0;
 d->moduls = 0;
 d->modul[0] = 0;
  d->name[0] = 0;
  d->sw_ver[0] = 0;
  d->supply[0] = 0;
 d->event = 0;
  lwp_init_wkc(&d->wkc);
 return d;
}

variable_types: struct Ldevice_struct { unsigned int id; int registered; char address[50]; char mac[50]; char mint[50]; char pint[50]; char aint[50]; char name[64]; char sw_ver[64]; char supply[64]; int moduls; struct Lmodul_struct { unsigned int id; char address[50]; int channels; char mint[50]; char pint[50]; char aint[50]; struct Lchannel_struct { unsigned int id; char address[50]; char type[50]; char sensor[50]; char unit[50]; char class[50]; double exponent; } *channel[20]; } *modul[20]; struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; } lastProcessed; int hasLogger; int event; struct lwp_wkc_struct { struct lwp_resource_struct { char name[100]; int attributes; struct lwp_attr_struct { char name[100]; } attr[10]; } info; struct lwp_resource_struct device; struct lwp_resource_struct modul; struct lwp_resource_struct network; struct lwp_resource_struct database; struct lwp_resource_struct logger; struct lwp_resource_struct channel[20]; int channels; } wkc; } *

user_defined_types: struct Ldevice_struct { unsigned int id; int registered; char address[50]; char mac[50]; char mint[50]; char pint[50]; char aint[50]; char name[64]; char sw_ver[64]; char supply[64]; int moduls; struct Lmodul_struct *modul[20]; struct tm lastProcessed; int hasLogger; int event; struct lwp_wkc_struct wkc; }

user_defined_types: struct Lmodul_struct { unsigned int id; char address[50]; int channels; char mint[50]; char pint[50]; char aint[50]; struct Lchannel_struct *channel[20]; }


LdeleteDevice

decompiled_code:
void __fastcall LdeleteDevice(void *ptr)
{
__int64 v1; // rbx

if ( *((int *)ptr + 113) > 0 )
{
v1 = 0LL;
do
LdeleteModul(*((_QWORD *)ptr + v1++ + 57));
while ( *((_DWORD *)ptr + 113) > (int)v1 );
}
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
__int64 v1; // rbx

if ( *((int *)ptr + 113) > 0 )
{
v1 = 0LL;
do
LdeleteModul(*((_QWORD *)ptr + v1++ + 57));
while ( *((_DWORD *)ptr + 113) > (int)v1 );
}
free(ptr);
}

original_code:
void LdeleteDevice(Ldevice *d){
 int i;
 for(i = 0; i < d->moduls; i++){
  LdeleteModul(d->modul[i]);
 }
 free(d);
 d = ((void *)0);
}

canonical_original_code:
void LdeleteDevice(struct Ldevice_struct *d){
 int i;
 for(i = 0; i < d->moduls; i++){
  LdeleteModul(d->modul[i]);
 }
 free(d);
 d = ((void *)0);
}

variable_types: i int


LaddModul

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

result = *(unsigned int *)(a1 + 452);
if ( (_DWORD)result == 20 )
return lielas_log("Can't add modul, maximum number of moduls reached", 1LL);
*(_QWORD *)(a1 + 8LL * ((int)result + 1) + 456) = a2;
*(_DWORD *)(a1 + 452) = result + 1;
if ( (int)result + 1 <= 20 )
{
result = (int)result + 2;
*(_QWORD *)(a1 + 8 * result + 456) = 0LL;
}
return result;
}

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

result = *(unsigned int *)(a1 + 452);
if ( (_DWORD)result == 20 )
return lielas_log("Can't add modul, maximum number of moduls reached", 1LL);
*(_QWORD *)(a1 + 8LL * ((int)result + 1) + 456) = a2;
*(_DWORD *)(a1 + 452) = result + 1;
if ( (int)result + 1 <= 20 )
{
result = (int)result + 2;
*(_QWORD *)(a1 + 8 * result + 456) = 0LL;
}
return result;
}

original_code:
void LaddModul(Ldevice *device, Lmodul *modul){
 if(device->moduls == 20){
  lielas_log((unsigned char*)"Can't add modul, maximum number of moduls reached", 1);
  return;
 }
 device->modul[device->moduls+1] = modul;
  device->moduls+=1;
 if(device->moduls <= 20){
  device->modul[device->moduls+1] = ((void *)0);
 }
}

canonical_original_code:
void LaddModul(struct Ldevice_struct *device, struct Lmodul_struct *modul){
 if(device->moduls == 20){
  lielas_log((unsigned char *)"Can't add modul, maximum number of moduls reached", 1);
  return;
 }
 device->modul[device->moduls+1] = modul;
  device->moduls+=1;
 if(device->moduls <= 20){
  device->modul[device->moduls+1] = ((void *)0);
 }
}

variable_types:
modul
struct Lmodul_struct *


LremoveModul

decompiled_code:
__int64 __fastcall LremoveModul(__int64 a1, __int64 a2)
{
_QWORD *v2; // rdx
int i; // eax
__int64 result; // rax
int v5; // ecx
__int64 v6; // rdx
__int64 v7; // rcx

v2 = (_QWORD *)(a1 + 456);
for ( i = 0; i != 20; ++i )
{
if ( *(_DWORD *)(a2 + 56) <= i )
break;
if ( *v2 == a2 )
goto LABEL_7;
++v2;
}
if ( *(_QWORD *)(a1 + 8LL * i + 456) != a2 )
return lielas_log("Can't remove modul, modul not found", 1LL);
LABEL_7:
v5 = *(_DWORD *)(a1 + 452);
if ( v5 - 1 != i && v5 > i )
{
v6 = a1 + 8LL * i;
v7 = a1 + 8 * ((unsigned int)(v5 - 1 - i) + (__int64)i) + 8;
do
{
*(_QWORD *)(v6 + 456) = *(_QWORD *)(v6 + 464);
v6 += 8LL;
}
while ( v6 != v7 );
}
result = LdeleteModul(a2);
--*(_DWORD *)(a1 + 452);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2)
{
_QWORD *v2; // rdx
int i; // eax
__int64 result; // rax
int v5; // ecx
__int64 v6; // rdx
__int64 v7; // rcx

v2 = (_QWORD *)(a1 + 456);
for ( i = 0; i != 20; ++i )
{
if ( *(_DWORD *)(a2 + 56) <= i )
break;
if ( *v2 == a2 )
goto LABEL_7;
++v2;
}
if ( *(_QWORD *)(a1 + 8LL * i + 456) != a2 )
return lielas_log("Can't remove modul, modul not found", 1LL);
LABEL_7:
v5 = *(_DWORD *)(a1 + 452);
if ( v5 - 1 != i && v5 > i )
{
v6 = a1 + 8LL * i;
v7 = a1 + 8 * ((unsigned int)(v5 - 1 - i) + (__int64)i) + 8;
do
{
*(_QWORD *)(v6 + 456) = *(_QWORD *)(v6 + 464);
v6 += 8LL;
}
while ( v6 != v7 );
}
result = LdeleteModul(a2);
--*(_DWORD *)(a1 + 452);
return result;
}

original_code:
void LremoveModul(Ldevice *device, Lmodul *modul){
 Lmodul *m;
 int i;
 int mIndex;
 for( mIndex = 0; mIndex < 20 && mIndex < modul->channels; mIndex++){
  if(device->modul[mIndex] == modul){
   break;
  }
 }
 if(device->modul[mIndex] != modul){
  lielas_log((unsigned char*)"Can't remove modul, modul not found", 1);
  return;
 }
 m = device->modul[mIndex];
 if(mIndex != (device->moduls - 1)){
  for( i = mIndex; i < device->moduls; i++){
   device->modul[i] = device->modul[i+1];
  }
 }
 LdeleteModul(m);
 device->moduls -= 1;
}

canonical_original_code:
void LremoveModul(struct Ldevice_struct *device, struct Lmodul_struct *modul){
 struct Lmodul_struct *m;
 int i;
 int mIndex;
 for( mIndex = 0; mIndex < 20 && mIndex < modul->channels; mIndex++){
  if(device->modul[mIndex] == modul){
   break;
  }
 }
 if(device->modul[mIndex] != modul){
  lielas_log((unsigned char *)"Can't remove modul, modul not found", 1);
  return;
 }
 m = device->modul[mIndex];
 if(mIndex != (device->moduls - 1)){
  for( i = mIndex; i < device->moduls; i++){
   device->modul[i] = device->modul[i+1];
  }
 }
 LdeleteModul(m);
 device->moduls -= 1;
}

variable_types: struct Lmodul_struct { unsigned int id; char address[50]; int channels; char mint[50]; char pint[50]; char aint[50]; struct Lchannel_struct { unsigned int id; char address[50]; char type[50]; char sensor[50]; char unit[50]; char class[50]; double exponent; } *channel[20]; } *

variable_types: mIndex int


LprintDeviceStructure

decompiled_code:
int __fastcall LprintDeviceStructure(unsigned int *a1, char *a2, int a3, int a4)
{
int v5; // ebx
int v6; // ebx
int v7; // ebx
int v8; // ebx
int v9; // ebx
int v10; // ebx
int result; // eax
int v12; // ebx
unsigned int **v13; // r15
int v14; // ebx
int v15; // ebx
int v16; // ebx
unsigned int v17; // er12
__int64 v18; // rbp
const char *v19; // rax
int v20; // ebx
int v21; // ebx
int v22; // ebx
unsigned int v23; // [rsp+18h] [rbp-40h]

if ( !a1 )
return snprintf(a2, a3, "Device is null\n");
v5 = snprintf(a2, a3, "Device %s with id %u:\n", (const char *)a1 + 8, *a1);
v6 = snprintf(&a2[v5], a3 - v5, " name: %s\n", (const char *)a1 + 258) + v5;
v7 = snprintf(&a2[v6], a3 - v6, " software version: %s\n", (const char *)a1 + 322) + v6;
v8 = snprintf(&a2[v7], a3 - v7, " supply: %s\n", (const char *)a1 + 386) + v7;
v9 = snprintf(&a2[v8], a3 - v8, " meassurement interval: %s\n", (const char *)a1 + 108) + v8;
v10 = snprintf(&a2[v9], a3 - v9, " process interval: %s\n", (const char *)a1 + 158) + v9;
result = snprintf(&a2[v10], a3 - v10, " alarm interval: %s\n", (const char *)a1 + 208);
v12 = result + v10;
v13 = (unsigned int **)(a1 + 114);
v23 = 0;
while ( *v13 )
{
snprintf(&a2[v12], a3 - v12, "*************************************************\n");
v14 = snprintf(
&a2[v12 + 50],
a3 - (v12 + 50),
" Modul %i of %u with id %u and address %s:\n",
v23,
a1[113],
**v13,
(const char *)*v13 + 4)
+ v12
+ 50;
v15 = snprintf(&a2[v14], a3 - v14, " meassurement interval: %s\n", (const char *)*v13 + 60) + v14;
v16 = snprintf(&a2[v15], a3 - v15, " process interval: %s\n", (const char *)*v13 + 110) + v15;
v12 = snprintf(&a2[v16], a3 - v16, " alarm interval: %s\n", (const char *)*v13 + 160) + v16;
v17 = 0;
while ( *(_QWORD *)&(*v13)[2 * v17 + 54] )
{
snprintf(&a2[v12], a3 - v12, "-------------------------------------------------\n");
v18 = (int)v17 + 26LL;
v19 = *(const char **)&(*v13)[2 * v18 + 2];
v20 = snprintf(
&a2[v12 + 50],
a3 - (v12 + 50),
" Channel %i of %u with id %u and address %s:\n",
v17,
(*v13)[14],
*(unsigned int *)v19,
v19 + 4)
+ v12
+ 50;
v21 = snprintf(&a2[v20], a3 - v20, " type: %s\n", (const char *)(*(_QWORD *)&(*v13)[2 * v18 + 2] + 54LL))
+ v20;
v22 = snprintf(&a2[v21], a3 - v21, " class: %s\n", (const char *)(*(_QWORD *)&(*v13)[2 * v18 + 2] + 204LL))
+ v21;
v12 = snprintf(&a2[v22], a3 - v22, " unit: %s\n", (const char *)(*(_QWORD *)&(*v13)[2 * v18 + 2] + 154LL))
+ v22;
if ( ++v17 == 20 )
goto LABEL_9;
}
if ( a4 )
{
snprintf(&a2[v12], a3 - v12, "-------------------------------------------------\n");
v12 += snprintf(&a2[v12 + 50], a3 - (v12 + 50), " channel %i is null\n", v17) + 50;
}
LABEL_9:
result = ++v23;
++v13;
if ( v23 == 20 )
return result;
}
if ( a4 )
{
snprintf(&a2[v12], a3 - v12, "*************************************************\n");
result = snprintf(&a2[v12 + 50], a3 - (v12 + 50), " modul %i is null\n", v23);
}
return result;
}

canonical_decompiled_code:
int __fastcall func4(unsigned int *a1, char *a2, int a3, int a4)
{
int v5; // ebx
int v6; // ebx
int v7; // ebx
int v8; // ebx
int v9; // ebx
int v10; // ebx
int result; // eax
int v12; // ebx
unsigned int **v13; // r15
int v14; // ebx
int v15; // ebx
int v16; // ebx
unsigned int v17; // er12
__int64 v18; // rbp
const char *v19; // rax
int v20; // ebx
int v21; // ebx
int v22; // ebx
unsigned int v23; // [rsp+18h] [rbp-40h]

if ( !a1 )
return snprintf(a2, a3, "Device is null\n");
v5 = snprintf(a2, a3, "Device %s with id %u:\n", (const char *)a1 + 8, *a1);
v6 = snprintf(&a2[v5], a3 - v5, " name: %s\n", (const char *)a1 + 258) + v5;
v7 = snprintf(&a2[v6], a3 - v6, " software version: %s\n", (const char *)a1 + 322) + v6;
v8 = snprintf(&a2[v7], a3 - v7, " supply: %s\n", (const char *)a1 + 386) + v7;
v9 = snprintf(&a2[v8], a3 - v8, " meassurement interval: %s\n", (const char *)a1 + 108) + v8;
v10 = snprintf(&a2[v9], a3 - v9, " process interval: %s\n", (const char *)a1 + 158) + v9;
result = snprintf(&a2[v10], a3 - v10, " alarm interval: %s\n", (const char *)a1 + 208);
v12 = result + v10;
v13 = (unsigned int **)(a1 + 114);
v23 = 0;
while ( *v13 )
{
snprintf(&a2[v12], a3 - v12, "*************************************************\n");
v14 = snprintf(
&a2[v12 + 50],
a3 - (v12 + 50),
" Modul %i of %u with id %u and address %s:\n",
v23,
a1[113],
**v13,
(const char *)*v13 + 4)
+ v12
+ 50;
v15 = snprintf(&a2[v14], a3 - v14, " meassurement interval: %s\n", (const char *)*v13 + 60) + v14;
v16 = snprintf(&a2[v15], a3 - v15, " process interval: %s\n", (const char *)*v13 + 110) + v15;
v12 = snprintf(&a2[v16], a3 - v16, " alarm interval: %s\n", (const char *)*v13 + 160) + v16;
v17 = 0;
while ( *(_QWORD *)&(*v13)[2 * v17 + 54] )
{
snprintf(&a2[v12], a3 - v12, "-------------------------------------------------\n");
v18 = (int)v17 + 26LL;
v19 = *(const char **)&(*v13)[2 * v18 + 2];
v20 = snprintf(
&a2[v12 + 50],
a3 - (v12 + 50),
" Channel %i of %u with id %u and address %s:\n",
v17,
(*v13)[14],
*(unsigned int *)v19,
v19 + 4)
+ v12
+ 50;
v21 = snprintf(&a2[v20], a3 - v20, " type: %s\n", (const char *)(*(_QWORD *)&(*v13)[2 * v18 + 2] + 54LL))
+ v20;
v22 = snprintf(&a2[v21], a3 - v21, " class: %s\n", (const char *)(*(_QWORD *)&(*v13)[2 * v18 + 2] + 204LL))
+ v21;
v12 = snprintf(&a2[v22], a3 - v22, " unit: %s\n", (const char *)(*(_QWORD *)&(*v13)[2 * v18 + 2] + 154LL))
+ v22;
if ( ++v17 == 20 )
goto LABEL_9;
}
if ( a4 )
{
snprintf(&a2[v12], a3 - v12, "-------------------------------------------------\n");
v12 += snprintf(&a2[v12 + 50], a3 - (v12 + 50), " channel %i is null\n", v17) + 50;
}
LABEL_9:
result = ++v23;
++v13;
if ( v23 == 20 )
return result;
}
if ( a4 )
{
snprintf(&a2[v12], a3 - v12, "*************************************************\n");
result = snprintf(&a2[v12 + 50], a3 - (v12 + 50), " modul %i is null\n", v23);
}
return result;
}

original_code:
void LprintDeviceStructure(Ldevice *d, char *str, int size, int all){
  int pos = 0;
  int i, j;
  if(d == ((void *)0)){
    pos += snprintf(&str[pos], size - pos, "Device is null\n");
    return;
  }
  pos += snprintf(&str[pos], size - pos, "Device %s with id %u:\n", d->address, d->id);
  pos += snprintf(&str[pos], size - pos, "  name: %s\n", d->name);
  pos += snprintf(&str[pos], size - pos, "  software version: %s\n", d->sw_ver);
  pos += snprintf(&str[pos], size - pos, "  supply: %s\n", d->supply);
  pos += snprintf(&str[pos], size - pos, "  meassurement interval: %s\n", d->mint);
  pos += snprintf(&str[pos], size - pos, "  process interval: %s\n", d->pint);
  pos += snprintf(&str[pos], size - pos, "  alarm interval: %s\n", d->aint);
  for(i=0; i < 20; i++){
    if(d->modul[i] == ((void *)0)){
      if(all){
          pos += snprintf(&str[pos], size - pos, "*************************************************\n");
          pos += snprintf(&str[pos], size - pos, "      modul %i is null\n", i);
      }
      break;
    }else{
      pos += snprintf(&str[pos], size - pos, "*************************************************\n");
      pos += snprintf(&str[pos], size - pos, "  Modul %i of %u with id %u and address %s:\n", i, d->moduls, d->modul[i]->id, d->modul[i]->address);
      pos += snprintf(&str[pos], size - pos, "    meassurement interval: %s\n", d->modul[i]->mint);
      pos += snprintf(&str[pos], size - pos, "    process interval: %s\n", d->modul[i]->pint);
      pos += snprintf(&str[pos], size - pos, "    alarm interval: %s\n", d->modul[i]->aint);
      for(j=0; j < 20; j++){
        if(d->modul[i]->channel[j] == ((void *)0)){
          if(all){
            pos += snprintf(&str[pos], size - pos, "-------------------------------------------------\n");
            pos += snprintf(&str[pos], size - pos, "      channel %i is null\n", j);
          }
          break;
        }else{
          pos += snprintf(&str[pos], size - pos, "-------------------------------------------------\n");
          pos += snprintf(&str[pos], size - pos, "      Channel %i of %u with id %u and address %s:\n",
                           j, d->modul[i]->channels, d->modul[i]->channel[j]->id, d->modul[i]->channel[j]->address);
          pos += snprintf(&str[pos], size - pos, "      type: %s\n", d->modul[i]->channel[j]->type);
          pos += snprintf(&str[pos], size - pos, "      class: %s\n", d->modul[i]->channel[j]->class);
          pos += snprintf(&str[pos], size - pos, "      unit: %s\n", d->modul[i]->channel[j]->unit);
        }
      }
    }
  }
}

canonical_original_code:
void LprintDeviceStructure(struct Ldevice_struct *d, char *str, int size, int all){
  int pos = 0;
  int i; int j;
  if(d == ((void *)0)){
    pos += snprintf(&str[pos], size - pos, "Device is null\n");
    return;
  }
  pos += snprintf(&str[pos], size - pos, "Device %s with id %u:\n", d->address, d->id);
  pos += snprintf(&str[pos], size - pos, "  name: %s\n", d->name);
  pos += snprintf(&str[pos], size - pos, "  software version: %s\n", d->sw_ver);
  pos += snprintf(&str[pos], size - pos, "  supply: %s\n", d->supply);
  pos += snprintf(&str[pos], size - pos, "  meassurement interval: %s\n", d->mint);
  pos += snprintf(&str[pos], size - pos, "  process interval: %s\n", d->pint);
  pos += snprintf(&str[pos], size - pos, "  alarm interval: %s\n", d->aint);
  for(i=0; i < 20; i++){
    if(d->modul[i] == ((void *)0)){
      if(all){
          pos += snprintf(&str[pos], size - pos, "*************************************************\n");
          pos += snprintf(&str[pos], size - pos, "      modul %i is null\n", i);
      }
      break;
    }else{
      pos += snprintf(&str[pos], size - pos, "*************************************************\n");
      pos += snprintf(&str[pos], size - pos, "  Modul %i of %u with id %u and address %s:\n", i, d->moduls, d->modul[i]->id, d->modul[i]->address);
      pos += snprintf(&str[pos], size - pos, "    meassurement interval: %s\n", d->modul[i]->mint);
      pos += snprintf(&str[pos], size - pos, "    process interval: %s\n", d->modul[i]->pint);
      pos += snprintf(&str[pos], size - pos, "    alarm interval: %s\n", d->modul[i]->aint);
      for(j=0; j < 20; j++){
        if(d->modul[i]->channel[j] == ((void *)0)){
          if(all){
            pos += snprintf(&str[pos], size - pos, "-------------------------------------------------\n");
            pos += snprintf(&str[pos], size - pos, "      channel %i is null\n", j);
          }
          break;
        }else{
          pos += snprintf(&str[pos], size - pos, "-------------------------------------------------\n");
          pos += snprintf(&str[pos], size - pos, "      Channel %i of %u with id %u and address %s:\n",
                           j, d->modul[i]->channels, d->modul[i]->channel[j]->id, d->modul[i]->channel[j]->address);
          pos += snprintf(&str[pos], size - pos, "      type: %s\n", d->modul[i]->channel[j]->type);
          pos += snprintf(&str[pos], size - pos, "      class: %s\n", d->modul[i]->channel[j]->class);
          pos += snprintf(&str[pos], size - pos, "      unit: %s\n", d->modul[i]->channel[j]->unit);
        }
      }
    }
  }
}

variable_types:
str
char *

variable_types: size int

variable_types: all int

variable_types: pos int

variable_types: j int


decompiled_code:
__int64 __fastcall jsmn_parse_primitive(int *a1, __int64 a2, __int64 a3, unsigned __int64 a4)
{
int v6; // ebp
char v7; // cl
unsigned int v8; // eax
_DWORD *v9; // rdi
__int64 result; // rax

v6 = *a1;
v7 = *(_BYTE *)(a2 + (unsigned int)*a1);
if ( v7 )
{
v8 = v6 + 1;
while ( 1 )
{
if ( v7 > 58 )
{
if ( (v7 & 0xDF) == 93 )
goto LABEL_9;
}
else if ( v7 > 8 && ((1LL << v7) & 0x400100100002600LL) != 0 )
{
goto LABEL_9;
}
if ( (unsigned __int8)(v7 - 32) > 0x5Eu )
break;
*a1 = v8;
v7 = *(_BYTE *)(a2 + v8++);
if ( !v7 )
goto LABEL_9;
}
*a1 = v6;
result = 4294967294LL;
}
else
{
LABEL_9:
v9 = jsmn_alloc_token((__int64)a1, a3, a4);
if ( v9 )
{
jsmn_fill_token(v9, 0, v6, *a1);
--*a1;
result = 0LL;
}
else
{
*a1 = v6;
result = 0xFFFFFFFFLL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int *a1, __int64 a2, __int64 a3, unsigned __int64 a4)
{
int v6; // ebp
char v7; // cl
unsigned int v8; // eax
_DWORD *v9; // rdi
__int64 result; // rax

v6 = *a1;
v7 = *(_BYTE *)(a2 + (unsigned int)*a1);
if ( v7 )
{
v8 = v6 + 1;
while ( 1 )
{
if ( v7 > 58 )
{
if ( (v7 & 0xDF) == 93 )
goto LABEL_9;
}
else if ( v7 > 8 && ((1LL << v7) & 0x400100100002600LL) != 0 )
{
goto LABEL_9;
}
if ( (unsigned __int8)(v7 - 32) > 0x5Eu )
break;
*a1 = v8;
v7 = *(_BYTE *)(a2 + v8++);
if ( !v7 )
goto LABEL_9;
}
*a1 = v6;
result = 4294967294LL;
}
else
{
LABEL_9:
v9 = jsmn_alloc_token((__int64)a1, a3, a4);
if ( v9 )
{
func0(v9, 0, v6, *a1);
--*a1;
result = 0LL;
}
else
{
*a1 = v6;
result = 0xFFFFFFFFLL;
}
}
return result;
}

original_code:
static jsmnerr_t jsmn_parse_primitive(jsmn_parser *parser, const char *js,
  jsmntok_t *tokens, size_t num_tokens) {
 jsmntok_t *token;
 int start;
 start = parser->pos;
 for (; js[parser->pos] != '\0'; parser->pos++) {
  switch (js[parser->pos]) {
   case ':':
   case '\t' : case '\r' : case '\n' : case ' ' :
   case ',' : case ']' : case '}' :
    goto found;
  }
  if (js[parser->pos] < 32 || js[parser->pos] >= 127) {
   parser->pos = start;
   return JSMN_ERROR_INVAL;
  }
 }
found:
 token = jsmn_alloc_token(parser, tokens, num_tokens);
 if (token == ((void *)0)) {
  parser->pos = start;
  return JSMN_ERROR_NOMEM;
 }
 jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos);
 parser->pos--;
 return JSMN_SUCCESS;
}

canonical_original_code:
static enum jsmnerr_t jsmn_parse_primitive(struct jsmn_parser *parser, char *js, struct jsmntok_t *tokens, unsigned long num_tokens) {
 struct jsmntok_t *token;
 int start;
 start = parser->pos;
 for (; js[parser->pos] != '\0'; parser->pos++) {
  switch (js[parser->pos]) {
   case ':':
   case '\t' : case '\r' : case '\n' : case ' ' :
   case ',' : case ']' : case '}' :
    goto found;
  }
  if (js[parser->pos] < 32 || js[parser->pos] >= 127) {
   parser->pos = start;
   return JSMN_ERROR_INVAL;
  }
 }
found:
 token = jsmn_alloc_token(parser, tokens, num_tokens);
 if (token == ((void *)0)) {
  parser->pos = start;
  return JSMN_ERROR_NOMEM;
 }
 jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos);
 parser->pos--;
 return JSMN_SUCCESS;
}

variable_types: struct jsmn_parser { unsigned int pos; int toknext; int toksuper; } *

variable_types:
js
char *

variable_types: struct jsmntok_t { enum jsmntype_t { JSMN_PRIMITIVE=0, JSMN_OBJECT=1, JSMN_ARRAY=2, JSMN_STRING=3 } type; int start; int end; int size; } *

variable_types: start int

return_type: enum jsmnerr_t { JSMN_ERROR_NOMEM=-1, JSMN_ERROR_INVAL=-2, JSMN_ERROR_PART=-3, JSMN_SUCCESS=0 }

user_defined_types: struct jsmntok_t { enum jsmntype_t type; int start; int end; int size; }


decompiled_code:
__int64 __fastcall jsmn_parse_string(int *a1, __int64 a2, __int64 a3, unsigned __int64 a4)
{
int v6; // ebp
unsigned int v7; // eax
char v8; // dl
__int64 result; // rax
_DWORD *v10; // rdi
unsigned int v11; // eax
char v12; // al

v6 = *a1;
v7 = v6 + 1;
++*a1;
v8 = *(_BYTE *)(a2 + (unsigned int)(v6 + 1));
if ( v8 )
{
while ( v8 != 34 )
{
if ( v8 == 92 )
{
v11 = v7 + 1;
*a1 = v11;
v12 = *(_BYTE *)(a2 + v11);
if ( v12 > 117 )
goto LABEL_14;
if ( v12 > 91 )
{
switch ( v12 )
{
case '\\':
case 'b':
case 'f':
case 'n':
case 'r':
case 't':
case 'u':
goto LABEL_8;
default:
goto LABEL_14;
}
}
if ( v12 != 34 && v12 != 47 )
{
LABEL_14:
*a1 = v6;
return 4294967294LL;
}
}
LABEL_8:
v7 = *a1 + 1;
*a1 = v7;
v8 = *(_BYTE *)(a2 + v7);
if ( !v8 )
goto LABEL_2;
}
v10 = jsmn_alloc_token((__int64)a1, a3, a4);
if ( v10 )
{
jsmn_fill_token(v10, 3, v6 + 1, *a1);
result = 0LL;
}
else
{
*a1 = v6;
result = 0xFFFFFFFFLL;
}
}
else
{
LABEL_2:
*a1 = v6;
result = 4294967293LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int *a1, __int64 a2, __int64 a3, unsigned __int64 a4)
{
int v6; // ebp
unsigned int v7; // eax
char v8; // dl
__int64 result; // rax
_DWORD *v10; // rdi
unsigned int v11; // eax
char v12; // al

v6 = *a1;
v7 = v6 + 1;
++*a1;
v8 = *(_BYTE *)(a2 + (unsigned int)(v6 + 1));
if ( v8 )
{
while ( v8 != 34 )
{
if ( v8 == 92 )
{
v11 = v7 + 1;
*a1 = v11;
v12 = *(_BYTE *)(a2 + v11);
if ( v12 > 117 )
goto LABEL_14;
if ( v12 > 91 )
{
switch ( v12 )
{
case '\\':
case 'b':
case 'f':
case 'n':
case 'r':
case 't':
case 'u':
goto LABEL_8;
default:
goto LABEL_14;
}
}
if ( v12 != 34 && v12 != 47 )
{
LABEL_14:
*a1 = v6;
return 4294967294LL;
}
}
LABEL_8:
v7 = *a1 + 1;
*a1 = v7;
v8 = *(_BYTE *)(a2 + v7);
if ( !v8 )
goto LABEL_2;
}
v10 = jsmn_alloc_token((__int64)a1, a3, a4);
if ( v10 )
{
func0(v10, 3, v6 + 1, *a1);
result = 0LL;
}
else
{
*a1 = v6;
result = 0xFFFFFFFFLL;
}
}
else
{
LABEL_2:
*a1 = v6;
result = 4294967293LL;
}
return result;
}

original_code:
static jsmnerr_t jsmn_parse_string(jsmn_parser *parser, const char *js,
  jsmntok_t *tokens, size_t num_tokens) {
 jsmntok_t *token;
 int start = parser->pos;
 parser->pos++;
 for (; js[parser->pos] != '\0'; parser->pos++) {
  char c = js[parser->pos];
  if (c == '\"') {
   token = jsmn_alloc_token(parser, tokens, num_tokens);
   if (token == ((void *)0)) {
    parser->pos = start;
    return JSMN_ERROR_NOMEM;
   }
   jsmn_fill_token(token, JSMN_STRING, start+1, parser->pos);
   return JSMN_SUCCESS;
  }
  if (c == '\\') {
   parser->pos++;
   switch (js[parser->pos]) {
    case '\"': case '/' : case '\\' : case 'b' :
    case 'f' : case 'r' : case 'n' : case 't' :
     break;
    case 'u':
     break;
    default:
     parser->pos = start;
     return JSMN_ERROR_INVAL;
   }
  }
 }
 parser->pos = start;
 return JSMN_ERROR_PART;
}

canonical_original_code:
static enum jsmnerr_t jsmn_parse_string(struct jsmn_parser *parser, char *js, struct jsmntok_t *tokens, unsigned long num_tokens) {
 struct jsmntok_t *token;
 int start = parser->pos;
 parser->pos++;
 for (; js[parser->pos] != '\0'; parser->pos++) {
  char c = js[parser->pos];
  if (c == '\"') {
   token = jsmn_alloc_token(parser, tokens, num_tokens);
   if (token == ((void *)0)) {
    parser->pos = start;
    return JSMN_ERROR_NOMEM;
   }
   jsmn_fill_token(token, JSMN_STRING, start+1, parser->pos);
   return JSMN_SUCCESS;
  }
  if (c == '\\') {
   parser->pos++;
   switch (js[parser->pos]) {
    case '\"': case '/' : case '\\' : case 'b' :
    case 'f' : case 'r' : case 'n' : case 't' :
     break;
    case 'u':
     break;
    default:
     parser->pos = start;
     return JSMN_ERROR_INVAL;
   }
  }
 }
 parser->pos = start;
 return JSMN_ERROR_PART;
}

variable_types: c char

user_defined_types: enum jsmnerr_t { JSMN_ERROR_NOMEM=-1, JSMN_ERROR_INVAL=-2, JSMN_ERROR_PART=-3, JSMN_SUCCESS=0 }

user_defined_types: struct jsmn_parser { unsigned int pos; int toknext; int toksuper; }

user_defined_types: enum jsmntype_t { JSMN_PRIMITIVE=0, JSMN_OBJECT=1, JSMN_ARRAY=2, JSMN_STRING=3 }


original_code:
jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, jsmntok_t *tokens,
  unsigned int num_tokens) {
 jsmnerr_t r;
 int i;
 jsmntok_t *token;
 for (; js[parser->pos] != '\0'; parser->pos++) {
  char c;
  jsmntype_t type;
  c = js[parser->pos];
  switch (c) {
   case '{': case '[':
    token = jsmn_alloc_token(parser, tokens, num_tokens);
    if (token == ((void *)0))
     return JSMN_ERROR_NOMEM;
    if (parser->toksuper != -1) {
     tokens[parser->toksuper].size++;
    }
    token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
    token->start = parser->pos;
    parser->toksuper = parser->toknext - 1;
    break;
   case '}': case ']':
    type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY);
    for (i = parser->toknext - 1; i >= 0; i--) {
     token = &tokens[i];
     if (token->start != -1 && token->end == -1) {
      if (token->type != type) {
       return JSMN_ERROR_INVAL;
      }
      parser->toksuper = -1;
      token->end = parser->pos + 1;
      break;
     }
    }
    if (i == -1) return JSMN_ERROR_INVAL;
    for (; i >= 0; i--) {
     token = &tokens[i];
     if (token->start != -1 && token->end == -1) {
      parser->toksuper = i;
      break;
     }
    }
    break;
   case '\"':
    r = jsmn_parse_string(parser, js, tokens, num_tokens);
    if (r < 0) return r;
    if (parser->toksuper != -1)
     tokens[parser->toksuper].size++;
    break;
   case '\t' : case '\r' : case '\n' : case ':' : case ',': case ' ':
    break;
   default:
    r = jsmn_parse_primitive(parser, js, tokens, num_tokens);
    if (r < 0) return r;
    if (parser->toksuper != -1)
     tokens[parser->toksuper].size++;
    break;
  }
 }
 for (i = parser->toknext - 1; i >= 0; i--) {
  if (tokens[i].start != -1 && tokens[i].end == -1) {
   return JSMN_ERROR_PART;
  }
 }
 return JSMN_SUCCESS;
}

canonical_original_code:
enum jsmnerr_t jsmn_parse(struct jsmn_parser *parser, char *js, struct jsmntok_t *tokens, unsigned int num_tokens) {
 enum jsmnerr_t r;
 int i;
 struct jsmntok_t *token;
 for (; js[parser->pos] != '\0'; parser->pos++) {
  char c;
  enum jsmntype_t type;
  c = js[parser->pos];
  switch (c) {
   case '{': case '[':
    token = jsmn_alloc_token(parser, tokens, num_tokens);
    if (token == ((void *)0))
     return JSMN_ERROR_NOMEM;
    if (parser->toksuper != -1) {
     tokens[parser->toksuper].size++;
    }
    token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
    token->start = parser->pos;
    parser->toksuper = parser->toknext - 1;
    break;
   case '}': case ']':
    type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY);
    for (i = parser->toknext - 1; i >= 0; i--) {
     token = &tokens[i];
     if (token->start != -1 && token->end == -1) {
      if (token->type != type) {
       return JSMN_ERROR_INVAL;
      }
      parser->toksuper = -1;
      token->end = parser->pos + 1;
      break;
     }
    }
    if (i == -1) return JSMN_ERROR_INVAL;
    for (; i >= 0; i--) {
     token = &tokens[i];
     if (token->start != -1 && token->end == -1) {
      parser->toksuper = i;
      break;
     }
    }
    break;
   case '\"':
    r = jsmn_parse_string(parser, js, tokens, num_tokens);
    if (r < 0) return r;
    if (parser->toksuper != -1)
     tokens[parser->toksuper].size++;
    break;
   case '\t' : case '\r' : case '\n' : case ':' : case ',': case ' ':
    break;
   default:
    r = jsmn_parse_primitive(parser, js, tokens, num_tokens);
    if (r < 0) return r;
    if (parser->toksuper != -1)
     tokens[parser->toksuper].size++;
    break;
  }
 }
 for (i = parser->toknext - 1; i >= 0; i--) {
  if (tokens[i].start != -1 && tokens[i].end == -1) {
   return JSMN_ERROR_PART;
  }
 }
 return JSMN_SUCCESS;
}

variable_types: enum jsmnerr_t { JSMN_ERROR_NOMEM=-1, JSMN_ERROR_INVAL=-2, JSMN_ERROR_PART=-3, JSMN_SUCCESS=0 }

variable_types: enum jsmntype_t { JSMN_PRIMITIVE=0, JSMN_OBJECT=1, JSMN_ARRAY=2, JSMN_STRING=3 }


LcreateModul

decompiled_code:
_BYTE *LcreateModul()
{
_BYTE *v0; // rax
_BYTE *v1; // rbx

v0 = malloc(0x178uLL);
v1 = v0;
if ( v0 )
{
*(_DWORD *)v0 = 0;
v0[4] = 0;
*((_DWORD *)v0 + 14) = 0;
*((_QWORD *)v0 + 27) = 0LL;
}
else
{
lielas_log("Can't allocate memory for new modul", 0LL);
}
return v1;
}

canonical_decompiled_code:
_BYTE *func0()
{
_BYTE *v0; // rax
_BYTE *v1; // rbx

v0 = malloc(0x178uLL);
v1 = v0;
if ( v0 )
{
*(_DWORD *)v0 = 0;
v0[4] = 0;
*((_DWORD *)v0 + 14) = 0;
*((_QWORD *)v0 + 27) = 0LL;
}
else
{
lielas_log("Can't allocate memory for new modul", 0LL);
}
return v1;
}

original_code:
Lmodul *LcreateModul(){
 Lmodul *m = malloc(sizeof(Lmodul));
 if(m == ((void *)0)){
  lielas_log((unsigned char*)"Can't allocate memory for new modul", 0);
  return ((void *)0);
 }
 m->id = 0;
 m->address[0] = 0;
 m->channels = 0;
 m->channel[0] = ((void *)0);
 return m;
}

canonical_original_code:
struct Lmodul_struct *LcreateModul(){
 struct Lmodul_struct *m = malloc(sizeof(Lmodul));
 if(m == ((void *)0)){
  lielas_log((unsigned char *)"Can't allocate memory for new modul", 0);
  return ((void *)0);
 }
 m->id = 0;
 m->address[0] = 0;
 m->channels = 0;
 m->channel[0] = ((void *)0);
 return m;
}

return_type: struct Lmodul_struct { unsigned int id; char address[50]; int channels; char mint[50]; char pint[50]; char aint[50]; struct Lchannel_struct { unsigned int id; char address[50]; char type[50]; char sensor[50]; char unit[50]; char class[50]; double exponent; } *channel[20]; } *


LdeleteModul

decompiled_code:
void __fastcall LdeleteModul(void *ptr)
{
__int64 v1; // rbx

if ( *((int *)ptr + 14) > 0 )
{
v1 = 0LL;
do
LdeleteChannel(*((_QWORD *)ptr + v1++ + 27));
while ( *((_DWORD *)ptr + 14) > (int)v1 );
}
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
__int64 v1; // rbx

if ( *((int *)ptr + 14) > 0 )
{
v1 = 0LL;
do
LdeleteChannel(*((_QWORD *)ptr + v1++ + 27));
while ( *((_DWORD *)ptr + 14) > (int)v1 );
}
free(ptr);
}

original_code:
void LdeleteModul(Lmodul *m){
 int i;
 for( i = 0; i < m->channels; i++){
  LdeleteChannel(m->channel[i]);
 }
 free(m);
}

canonical_original_code:
void LdeleteModul(struct Lmodul_struct *m){
 int i;
 for( i = 0; i < m->channels; i++){
  LdeleteChannel(m->channel[i]);
 }
 free(m);
}


LaddChannel

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

result = *(unsigned int *)(a1 + 56);
if ( (_DWORD)result == 20 )
return lielas_log("Can't add channel, maximum number of channels reached", 1LL);
*(_QWORD *)(a1 + 8LL * ((int)result + 1) + 216) = a2;
*(_DWORD *)(a1 + 56) = result + 1;
if ( (int)result + 1 <= 20 )
{
result = (int)result + 2;
*(_QWORD *)(a1 + 8 * result + 216) = 0LL;
}
return result;
}

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

result = *(unsigned int *)(a1 + 56);
if ( (_DWORD)result == 20 )
return lielas_log("Can't add channel, maximum number of channels reached", 1LL);
*(_QWORD *)(a1 + 8LL * ((int)result + 1) + 216) = a2;
*(_DWORD *)(a1 + 56) = result + 1;
if ( (int)result + 1 <= 20 )
{
result = (int)result + 2;
*(_QWORD *)(a1 + 8 * result + 216) = 0LL;
}
return result;
}

original_code:
void LaddChannel(Lmodul *m, Lchannel *channel){
 if(m->channels == 20){
  lielas_log((unsigned char*)"Can't add channel, maximum number of channels reached", 1);
  return;
 }
 m->channel[m->channels+1] = channel;
  m->channels += 1;
 if(m->channels <= 20){
  m->channel[m->channels+1] = ((void *)0);
 }
}

canonical_original_code:
void LaddChannel(struct Lmodul_struct *m, struct Lchannel_struct *channel){
 if(m->channels == 20){
  lielas_log((unsigned char *)"Can't add channel, maximum number of channels reached", 1);
  return;
 }
 m->channel[m->channels+1] = channel;
  m->channels += 1;
 if(m->channels <= 20){
  m->channel[m->channels+1] = ((void *)0);
 }
}

variable_types:
channel
struct Lchannel_struct *


LremoveChannel

decompiled_code:
__int64 __fastcall LremoveChannel(__int64 a1, __int64 a2)
{
int v2; // ecx
_QWORD *v3; // rdx
int v4; // eax
__int64 result; // rax
__int64 v6; // rdx
__int64 v7; // rcx

v2 = *(_DWORD *)(a1 + 56);
v3 = (_QWORD *)(a1 + 216);
v4 = 0;
while ( v2 > v4 )
{
if ( *v3 == a2 )
{
if ( v4 == v2 - 1 )
goto LABEL_10;
goto LABEL_8;
}
++v4;
++v3;
if ( v4 == 20 )
break;
}
if ( *(_QWORD *)(a1 + 8LL * v4 + 216) != a2 )
return lielas_log("Can't remove channel, channel not found", 1LL);
if ( v2 - 1 != v4 && v2 > v4 )
{
LABEL_8:
v6 = a1 + 8LL * v4;
v7 = a1 + 8 * ((unsigned int)(v2 - 1 - v4) + (__int64)v4) + 8;
do
{
*(_QWORD *)(v6 + 216) = *(_QWORD *)(v6 + 224);
v6 += 8LL;
}
while ( v6 != v7 );
}
LABEL_10:
result = LdeleteChannel(a2);
--*(_DWORD *)(a1 + 56);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2)
{
int v2; // ecx
_QWORD *v3; // rdx
int v4; // eax
__int64 result; // rax
__int64 v6; // rdx
__int64 v7; // rcx

v2 = *(_DWORD *)(a1 + 56);
v3 = (_QWORD *)(a1 + 216);
v4 = 0;
while ( v2 > v4 )
{
if ( *v3 == a2 )
{
if ( v4 == v2 - 1 )
goto LABEL_10;
goto LABEL_8;
}
++v4;
++v3;
if ( v4 == 20 )
break;
}
if ( *(_QWORD *)(a1 + 8LL * v4 + 216) != a2 )
return lielas_log("Can't remove channel, channel not found", 1LL);
if ( v2 - 1 != v4 && v2 > v4 )
{
LABEL_8:
v6 = a1 + 8LL * v4;
v7 = a1 + 8 * ((unsigned int)(v2 - 1 - v4) + (__int64)v4) + 8;
do
{
*(_QWORD *)(v6 + 216) = *(_QWORD *)(v6 + 224);
v6 += 8LL;
}
while ( v6 != v7 );
}
LABEL_10:
result = LdeleteChannel(a2);
--*(_DWORD *)(a1 + 56);
return result;
}

original_code:
void LremoveChannel(Lmodul *m, Lchannel *channel){
 Lchannel *c;
 int i;
 int cIndex;
 for( cIndex = 0; cIndex < 20 && cIndex < m->channels; cIndex++){
  if(m->channel[cIndex] == channel){
   break;
  }
 }
 if(m->channel[cIndex] != channel){
  lielas_log((unsigned char*)"Can't remove channel, channel not found", 1);
  return;
 }
 c = m->channel[cIndex];
 if(cIndex != (m->channels - 1)){
  for( i = cIndex; i < m->channels; i++){
   m->channel[i] = m->channel[i+1];
  }
 }
 LdeleteChannel(c);
 m->channels -= 1;
}

canonical_original_code:
void LremoveChannel(struct Lmodul_struct *m, struct Lchannel_struct *channel){
 struct Lchannel_struct *c;
 int i;
 int cIndex;
 for( cIndex = 0; cIndex < 20 && cIndex < m->channels; cIndex++){
  if(m->channel[cIndex] == channel){
   break;
  }
 }
 if(m->channel[cIndex] != channel){
  lielas_log((unsigned char *)"Can't remove channel, channel not found", 1);
  return;
 }
 c = m->channel[cIndex];
 if(cIndex != (m->channels - 1)){
  for( i = cIndex; i < m->channels; i++){
   m->channel[i] = m->channel[i+1];
  }
 }
 LdeleteChannel(c);
 m->channels -= 1;
}

variable_types: struct Lchannel_struct { unsigned int id; char address[50]; char type[50]; char sensor[50]; char unit[50]; char class[50]; double exponent; } *

variable_types: i int

variable_types: cIndex int


decompiled_code:
void __fastcall i2c_init(int a1, int a2)
{
i2c_adr = a1;
i2c_nr = a2;
}

canonical_decompiled_code:
void __fastcall func0(int a1, int a2)
{
i2c_adr = a1;
i2c_nr = a2;
}

original_code:
void i2c_init(int address, int i2c){
  i2c_adr = address;
  i2c_nr = i2c;
}

variable_types: address int