decompiled_code:
__pid_t __fastcall post9pservice(int fd, char *s, __int64 a3)
{
int v3; // ebp
__int64 v5; // r14
__pid_t result; // eax
unsigned int v7; // er13
int j; // ebx
__int64 v9; // rax
__int64 v10; // rdi
_BYTE *v11; // rax
int i; // ebx
const char *v13; // rax
const char *v14; // r13

v3 = fd;
if ( !(a3 | (unsigned __int64)s) )
{
close(fd);
werrstr("nothing to do");
return -1;
}
if ( !s )
{
LABEL_17:
result = 0;
if ( a3 )
{
result = p9rfork(84LL);
if ( result != -1 )
{
if ( !result )
{
p9dup((unsigned int)v3, 0LL);
for ( i = 3; i != 20; ++i )
close(i);
execlp("mount9p", "mount9p", "-", a3, 0LL);
if ( chattyfuse )
execlp("9pfuse", "9pfuse", "-D", "-", a3, 0LL);
else
execlp("9pfuse", "9pfuse", "-", a3, 0LL);
fprint(2LL, "exec 9pfuse: %r\n");
exits("exec");
}
close(v3);
result = 0;
}
}
return result;
}
if ( strchr(s, 33) )
{
v5 = p9strdup(s);
}
else
{
v13 = (const char *)getns();
v14 = v13;
if ( !v13 )
return -1;
v5 = smprint("unix!%s/%s", v13, s);
p9free(v14);
}
if ( !v5 )
return -1;
result = fork();
v7 = result;
if ( result != -1 )
{
if ( !result )
{
p9dup((unsigned int)fd, 0LL);
p9dup((unsigned int)fd, 1LL);
for ( j = 3; j != 20; ++j )
close(j);
execlp("9pserve", "9pserve", "-u", v5, 0LL);
fprint(2LL, "exec 9pserve: %r\n");
exits("exec");
}
close(fd);
v9 = p9waitfor(v7);
v10 = v9;
if ( !v9 )
return -1;
v11 = *(_BYTE **)(v9 + 32);
if ( v11 && *v11 )
{
p9free(v10);
werrstr("9pserve failed");
return -1;
}
p9free(v10);
if ( a3 )
{
v3 = p9dial(v5, 0LL, 0LL, 0LL);
if ( v3 < 0 )
{
werrstr("cannot reopen for mount: %r");
return -1;
}
}
p9free(v5);
goto LABEL_17;
}
return result;
}

canonical_decompiled_code:
__pid_t __fastcall func0(int fd, char *s, __int64 a3)
{
int v3; // ebp
__int64 v5; // r14
__pid_t result; // eax
unsigned int v7; // er13
int j; // ebx
__int64 v9; // rax
__int64 v10; // rdi
_BYTE *v11; // rax
int i; // ebx
const char *v13; // rax
const char *v14; // r13

v3 = fd;
if ( !(a3 | (unsigned __int64)s) )
{
close(fd);
werrstr("nothing to do");
return -1;
}
if ( !s )
{
LABEL_17:
result = 0;
if ( a3 )
{
result = p9rfork(84LL);
if ( result != -1 )
{
if ( !result )
{
p9dup((unsigned int)v3, 0LL);
for ( i = 3; i != 20; ++i )
close(i);
execlp("mount9p", "mount9p", "-", a3, 0LL);
if ( chattyfuse )
execlp("9pfuse", "9pfuse", "-D", "-", a3, 0LL);
else
execlp("9pfuse", "9pfuse", "-", a3, 0LL);
fprint(2LL, "exec 9pfuse: %r\n");
exits("exec");
}
close(v3);
result = 0;
}
}
return result;
}
if ( strchr(s, 33) )
{
v5 = p9strdup(s);
}
else
{
v13 = (const char *)getns();
v14 = v13;
if ( !v13 )
return -1;
v5 = smprint("unix!%s/%s", v13, s);
p9free(v14);
}
if ( !v5 )
return -1;
result = fork();
v7 = result;
if ( result != -1 )
{
if ( !result )
{
p9dup((unsigned int)fd, 0LL);
p9dup((unsigned int)fd, 1LL);
for ( j = 3; j != 20; ++j )
close(j);
execlp("9pserve", "9pserve", "-u", v5, 0LL);
fprint(2LL, "exec 9pserve: %r\n");
exits("exec");
}
close(fd);
v9 = p9waitfor(v7);
v10 = v9;
if ( !v9 )
return -1;
v11 = *(_BYTE **)(v9 + 32);
if ( v11 && *v11 )
{
p9free(v10);
werrstr("9pserve failed");
return -1;
}
p9free(v10);
if ( a3 )
{
v3 = p9dial(v5, 0LL, 0LL, 0LL);
if ( v3 < 0 )
{
werrstr("cannot reopen for mount: %r");
return -1;
}
}
p9free(v5);
goto LABEL_17;
}
return result;
}

original_code:
int
post9pservice(int fd, char *name, char *mtpt)
{
 int i, pid;
 char *ns, *addr;
 Waitmsg *w;
 if(name == ((void*)0) && mtpt == ((void*)0)){
  close(fd);
  werrstr("nothing to do");
  return -1;
 }
 if(name){
  if(strchr(name, '!'))
   addr = p9strdup(name);
  else{
   if((ns = getns()) == ((void*)0))
    return -1;
   addr = smprint("unix!%s/%s", ns, name);
   p9free(ns);
  }
  if(addr == ((void*)0))
   return -1;
  switch(pid = fork()){
  case -1:
   return -1;
  case 0:
   p9dup(fd, 0);
   p9dup(fd, 1);
   for(i=3; i<20; i++)
    close(i);
   execlp("9pserve", "9pserve", "-u", addr, (char*)0);
   fprint(2, "exec 9pserve: %r\n");
   _exits("exec");
  }
  close(fd);
  w = p9waitfor(pid);
  if(w == ((void*)0))
   return -1;
  if(w->msg && w->msg[0]){
   p9free(w);
   werrstr("9pserve failed");
   return -1;
  }
  p9free(w);
  if(mtpt){
   if((fd = p9dial(addr, ((void*)0), ((void*)0), ((void*)0))) < 0){
    werrstr("cannot reopen for mount: %r");
    return -1;
   }
  }
  p9free(addr);
 }
 if(mtpt){
  switch(pid = p9rfork(RFFDG|RFPROC|RFNOWAIT)){
  case -1:
   return -1;
  case 0:
   p9dup(fd, 0);
   for(i=3; i<20; i++)
    close(i);
   execlp("mount9p", "mount9p", "-", mtpt, (char*)0);
   if(chattyfuse)
    execlp("9pfuse", "9pfuse", "-D", "-", mtpt, (char*)0);
   else
    execlp("9pfuse", "9pfuse", "-", mtpt, (char*)0);
   fprint(2, "exec 9pfuse: %r\n");
   _exits("exec");
  }
  close(fd);
 }
 return 0;
}

canonical_original_code:
int post9pservice(int fd, char *name, char *mtpt)
{
 int i; int pid;
 char *ns; char *addr;
 struct Waitmsg *w;
 if(name == ((void *)0) && mtpt == ((void *)0)){
  close(fd);
  werrstr("nothing to do");
  return -1;
 }
 if(name){
  if(strchr(name, '!'))
   addr = p9strdup(name);
  else{
   if((ns = getns()) == ((void *)0))
    return -1;
   addr = smprint("unix!%s/%s", ns, name);
   p9free(ns);
  }
  if(addr == ((void *)0))
   return -1;
  switch(pid = fork()){
  case -1:
   return -1;
  case 0:
   p9dup(fd, 0);
   p9dup(fd, 1);
   for(i=3; i<20; i++)
    close(i);
   execlp("9pserve", "9pserve", "-u", addr, (char *)0);
   fprint(2, "exec 9pserve: %r\n");
   _exits("exec");
  }
  close(fd);
  w = p9waitfor(pid);
  if(w == ((void *)0))
   return -1;
  if(w->msg && w->msg[0]){
   p9free(w);
   werrstr("9pserve failed");
   return -1;
  }
  p9free(w);
  if(mtpt){
   if((fd = p9dial(addr, ((void *)0), ((void *)0), ((void *)0))) < 0){
    werrstr("cannot reopen for mount: %r");
    return -1;
   }
  }
  p9free(addr);
 }
 if(mtpt){
  switch(pid = p9rfork(RFFDG|RFPROC|RFNOWAIT)){
  case -1:
   return -1;
  case 0:
   p9dup(fd, 0);
   for(i=3; i<20; i++)
    close(i);
   execlp("mount9p", "mount9p", "-", mtpt, (char *)0);
   if(chattyfuse)
    execlp("9pfuse", "9pfuse", "-D", "-", mtpt, (char *)0);
   else
    execlp("9pfuse", "9pfuse", "-", mtpt, (char *)0);
   fprint(2, "exec 9pfuse: %r\n");
   _exits("exec");
  }
  close(fd);
 }
 return 0;
}

variable_types:
name
char *

variable_types:
mtpt
char *

variable_types: i int

variable_types: pid int

variable_types:
ns
char *

variable_types:
addr
char *

variable_types: struct Waitmsg { int pid; unsigned long p9time[3]; char *msg; } *


decompiled_code:
int __fastcall p9exec(const char *a1, char *const *a2)
{
return execvp(a1, a2);
}

canonical_decompiled_code:
int __fastcall func0(const char *a1, char *const *a2)
{
return execvp(a1, a2);
}

original_code:
int
p9exec(char *prog, char *argv[])
{
 return execvp(prog, argv);
}

variable_types:
prog
char *

variable_types:
argv
char *[]

return_type: int


decompiled_code:
__int64 __fastcall read9pmsg(__int64 a1, unsigned __int8 *a2, unsigned int a3)
{
int v4; // eax
__int64 result; // rax
int v6; // eax
int v7; // er12
int v8; // edx

v4 = readn(a1, a2, 4LL);
if ( v4 != 4 )
return (unsigned int)(v4 >> 31);
v6 = (a2[3] << 24) | *a2 | (a2[2] << 16) | (a2[1] << 8);
if ( v6 <= 4 || v6 > a3 )
{
werrstr("bad length in 9P2000 message header");
result = 0xFFFFFFFFLL;
}
else
{
v7 = v6 - 4;
v8 = readn((unsigned int)a1, a2 + 4, v6 - 4);
result = (unsigned int)(v8 + 4);
if ( v7 > v8 )
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, unsigned __int8 *a2, unsigned int a3)
{
int v4; // eax
__int64 result; // rax
int v6; // eax
int v7; // er12
int v8; // edx

v4 = readn(a1, a2, 4LL);
if ( v4 != 4 )
return (unsigned int)(v4 >> 31);
v6 = (a2[3] << 24) | *a2 | (a2[2] << 16) | (a2[1] << 8);
if ( v6 <= 4 || v6 > a3 )
{
werrstr("bad length in 9P2000 message header");
result = 0xFFFFFFFFLL;
}
else
{
v7 = v6 - 4;
v8 = readn((unsigned int)a1, a2 + 4, v6 - 4);
result = (unsigned int)(v8 + 4);
if ( v7 > v8 )
result = 0LL;
}
return result;
}

original_code:
int
read9pmsg(int fd, void *abuf, uint n)
{
 int m, len;
 uchar *buf;
 buf = abuf;
 m = readn(fd, buf, 4);
 if(m != 4){
  if(m < 0)
   return -1;
  return 0;
 }
 len = ((u32int)((buf)[0]|((buf)[1]<<8)|((buf)[2]<<16)|((buf)[3]<<24)));
 if(len <= 4 || len > n){
  werrstr("bad length in 9P2000 message header");
  return -1;
 }
 len -= 4;
 m = readn(fd, buf+4, len);
 if(m < len)
  return 0;
 return 4 +m;
}

canonical_original_code:
int read9pmsg(int fd, void *abuf, unsigned int n)
{
 int m; int len;
 unsigned char *buf;
 buf = abuf;
 m = readn(fd, buf, 4);
 if(m != 4){
  if(m < 0)
   return -1;
  return 0;
 }
 len = ((u32int)((buf)[0]|((buf)[1]<<8)|((buf)[2]<<16)|((buf)[3]<<24)));
 if(len <= 4 || len > n){
  werrstr("bad length in 9P2000 message header");
  return -1;
 }
 len -= 4;
 m = readn(fd, buf+4, len);
 if(m < len)
  return 0;
 return 4 +m;
}

variable_types: fd int

variable_types:
abuf
void *

variable_types:
n
unsigned int

variable_types: m int

variable_types: len int

variable_types:
buf
unsigned char *


runestrcmp

decompiled_code:
__int64 __fastcall runestrcmp(__int64 a1, __int64 a2)
{
__int64 i; // rax
unsigned int v3; // edx
unsigned int v4; // ecx

for ( i = 0LL; ; i += 4LL )
{
v3 = *(_DWORD *)(a1 + i);
v4 = *(_DWORD *)(a2 + i);
if ( v3 != v4 )
break;
if ( !v3 )
return 0LL;
}
return v4 < v3 ? 1 : -1;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
__int64 i; // rax
unsigned int v3; // edx
unsigned int v4; // ecx

for ( i = 0LL; ; i += 4LL )
{
v3 = *(_DWORD *)(a1 + i);
v4 = *(_DWORD *)(a2 + i);
if ( v3 != v4 )
break;
if ( !v3 )
return 0LL;
}
return v4 < v3 ? 1 : -1;
}

original_code:
int
runestrcmp(Rune *s1, Rune *s2)
{
 Rune c1, c2;
 for(;;) {
  c1 = *s1++;
  c2 = *s2++;
  if(c1 != c2) {
   if(c1 > c2)
    return 1;
   return -1;
  }
  if(c1 == 0)
   return 0;
 }
}

canonical_original_code:
int runestrcmp(unsigned int *s1, unsigned int *s2)
{
 unsigned int c1; unsigned int c2;
 for(;;) {
  c1 = *s1++;
  c2 = *s2++;
  if(c1 != c2) {
   if(c1 > c2)
    return 1;
   return -1;
  }
  if(c1 == 0)
   return 0;
 }
}


decompiled_code:
__int64 p9waitpid()
{
int v0; // eax
__int64 v2[6]; // [rsp+0h] [rbp-238h] BYREF
char v3[520]; // [rsp+30h] [rbp-208h] BYREF

v0 = await(v3, 511LL);
if ( v0 <= 0 )
return 0xFFFFFFFFLL;
v3[v0] = 0;
if ( (unsigned int)tokenize(v3, v2, 5LL) == 5 )
return p9atoi(v2[0]);
werrstr("couldn't parse wait message");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func0()
{
int v0; // eax
__int64 v2[6]; // [rsp+0h] [rbp-238h] BYREF
char v3[520]; // [rsp+30h] [rbp-208h] BYREF

v0 = await(v3, 511LL);
if ( v0 <= 0 )
return 0xFFFFFFFFLL;
v3[v0] = 0;
if ( (unsigned int)tokenize(v3, v2, 5LL) == 5 )
return p9atoi(v2[0]);
werrstr("couldn't parse wait message");
return 0xFFFFFFFFLL;
}

original_code:
int
p9waitpid(void)
{
 int n;
 char buf[512], *fld[5];
 n = await(buf, sizeof buf-1);
 if(n <= 0)
  return -1;
 buf[n] = '\0';
 if(tokenize(buf, fld, (sizeof(fld)/sizeof((fld)[0]))) != (sizeof(fld)/sizeof((fld)[0]))){
  werrstr("couldn't parse wait message");
  return -1;
 }
 return p9atoi(fld[0]);
}

canonical_original_code:
int p9waitpid()
{
 int n;
 char buf[512]; char *fld[5];
 n = await(buf, sizeof buf-1);
 if(n <= 0)
  return -1;
 buf[n] = '\0';
 if(tokenize(buf, fld, (sizeof(fld)/sizeof((fld)[0]))) != (sizeof(fld)/sizeof((fld)[0]))){
  werrstr("couldn't parse wait message");
  return -1;
 }
 return p9atoi(fld[0]);
}

variable_types: n int

variable_types:
buf
char [512]

variable_types:
fld
char *[5]


utfutf

decompiled_code:
char *__fastcall utfutf(char *haystack, char *needle)
{
char *v2; // rbx
int v3; // eax
__int64 v4; // r13
int v5; // er12
size_t v6; // r14
const char *v7; // rax
const char *v8; // rbx
unsigned int v10[11]; // [rsp+Ch] [rbp-2Ch] BYREF

v2 = haystack;
v3 = chartorune(v10);
v4 = v10[0];
if ( v10[0] <= 0x80uLL )
return strstr(haystack, needle);
v5 = v3;
v6 = strlen(needle);
while ( 1 )
{
v7 = (const char *)utfrune(v2, v4);
v8 = v7;
if ( !v7 || !strncmp(v7, needle, v6) )
break;
v2 = (char *)&v8[v5];
}
return (char *)v8;
}

canonical_decompiled_code:
char *__fastcall func0(char *haystack, char *needle)
{
char *v2; // rbx
int v3; // eax
__int64 v4; // r13
int v5; // er12
size_t v6; // r14
const char *v7; // rax
const char *v8; // rbx
unsigned int v10[11]; // [rsp+Ch] [rbp-2Ch] BYREF

v2 = haystack;
v3 = chartorune(v10);
v4 = v10[0];
if ( v10[0] <= 0x80uLL )
return strstr(haystack, needle);
v5 = v3;
v6 = strlen(needle);
while ( 1 )
{
v7 = (const char *)utfrune(v2, v4);
v8 = v7;
if ( !v7 || !strncmp(v7, needle, v6) )
break;
v2 = (char *)&v8[v5];
}
return (char *)v8;
}

original_code:
char*
utfutf(char *s1, char *s2)
{
 char *p;
 long f, n1, n2;
 Rune r;
 n1 = chartorune(&r, s2);
 f = r;
 if(f <= Runesync)
  return strstr(s1, s2);
 n2 = strlen(s2);
 for(p=s1; p=utfrune(p, f); p+=n1)
  if(strncmp(p, s2, n2) == 0)
   return p;
 return 0;
}

canonical_original_code:
char *utfutf(char *s1, char *s2)
{
 char *p;
 long f; long n1; long n2;
 unsigned int r;
 n1 = chartorune(&r, s2);
 f = r;
 if(f <= Runesync)
  return strstr(s1, s2);
 n2 = strlen(s2);
 for(p=s1; p=utfrune(p, f); p+=n1)
  if(strncmp(p, s2, n2) == 0)
   return p;
 return 0;
}

variable_types:
p
char *

variable_types: f long

variable_types:
r
unsigned int

return_type: char *


decompiled_code:
ssize_t __fastcall p9write(int fd, __int64 a2, __int64 a3)
{
ssize_t v4; // rbx
ssize_t v5; // rax

if ( a3 <= 0 )
return 0LL;
v4 = 0LL;
while ( 1 )
{
v5 = write(fd, (const void *)(a2 + v4), a3 - v4);
if ( v5 <= 0 )
break;
v4 += v5;
if ( a3 <= v4 )
return v4;
}
if ( !v4 )
v4 = v5;
return v4;
}

canonical_decompiled_code:
ssize_t __fastcall func0(int fd, __int64 a2, __int64 a3)
{
ssize_t v4; // rbx
ssize_t v5; // rax

if ( a3 <= 0 )
return 0LL;
v4 = 0LL;
while ( 1 )
{
v5 = write(fd, (const void *)(a2 + v4), a3 - v4);
if ( v5 <= 0 )
break;
v4 += v5;
if ( a3 <= v4 )
return v4;
}
if ( !v4 )
v4 = v5;
return v4;
}

original_code:
long
p9write(int f, void *av, long n)
{
 char *a;
 long m, t;
 a = av;
 t = 0;
 while(t < n){
  m = write(f, a+t, n-t);
  if(m <= 0){
   if(t == 0)
    return m;
   break;
  }
  t += m;
 }
 return t;
}

canonical_original_code:
long p9write(int f, void *av, long n)
{
 char *a;
 long m; long t;
 a = av;
 t = 0;
 while(t < n){
  m = write(f, a+t, n-t);
  if(m <= 0){
   if(t == 0)
    return m;
   break;
  }
  t += m;
 }
 return t;
}

variable_types: f int

variable_types:
av
void *

variable_types: n long

variable_types:
a
char *

variable_types: m long

variable_types: t long

return_type: long


Bgetdf

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

result = Bgetc(*(_QWORD *)a1);
if ( (_DWORD)result == -1 )
*(_DWORD *)(a1 + 8) = 1;
return result;
}

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

result = Bgetc(*(_QWORD *)a1);
if ( (_DWORD)result == -1 )
*(_DWORD *)(a1 + 8) = 1;
return result;
}

original_code:
static int
Bgetdf(void *vp)
{
 int c;
 struct bgetd *bg = vp;
 c = Bgetc(bg->b);
 if(c == Beof)
  bg->eof = 1;
 return c;
}

variable_types:
vp
void *

variable_types: c int

variable_types: struct bgetd { struct Biobuf { int icount; int ocount; int rdline; int runesize; int state; int fid; int flag; long long offset; int bsize; unsigned char *bbuf; unsigned char *ebuf; unsigned char *gbuf; unsigned char b[-1]; } *b; int eof; } *

user_defined_types: struct bgetd { struct Biobuf *b; int eof; }


Bgetd

decompiled_code:
__int64 __fastcall Bgetd(__int64 a1, double *a2, double a3)
{
__int64 result; // rax
__int64 v4; // [rsp+0h] [rbp-28h] BYREF
int v5; // [rsp+8h] [rbp-20h]

v4 = a1;
v5 = 0;
fmtcharstod(Bgetdf, &v4);
result = 0xFFFFFFFFLL;
if ( !v5 )
{
Bungetc(a1);
*a2 = a3;
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, double *a2, double a3)
{
__int64 result; // rax
__int64 v4; // [rsp+0h] [rbp-28h] BYREF
int v5; // [rsp+8h] [rbp-20h]

v4 = a1;
v5 = 0;
fmtcharstod(Bgetdf, &v4);
result = 0xFFFFFFFFLL;
if ( !v5 )
{
Bungetc(a1);
*a2 = a3;
result = 1LL;
}
return result;
}

original_code:
int
Bgetd(Biobuf *bp, double *dp)
{
 double d;
 struct bgetd b;
 b.b = bp;
 b.eof = 0;
 d = fmtcharstod(Bgetdf, &b);
 if(b.eof)
  return -1;
 Bungetc(bp);
 *dp = d;
 return 1;
}

canonical_original_code:
int Bgetd(struct Biobuf *bp, double *dp)
{
 double d;
 struct bgetd b;
 b.b = bp;
 b.eof = 0;
 d = fmtcharstod(Bgetdf, &b);
 if(b.eof)
  return -1;
 Bungetc(bp);
 *dp = d;
 return 1;
}

variable_types:
dp
double *

variable_types: d double

variable_types: struct bgetd { struct Biobuf { int icount; int ocount; int rdline; int runesize; int state; int fid; int flag; long long offset; int bsize; unsigned char *bbuf; unsigned char *ebuf; unsigned char *gbuf; unsigned char b[-1]; } *b; int eof; }


runevsnprint

original_code:
int
runevsnprint(Rune *buf, int len, char *fmt, va_list args)
{
 Fmt f;
 if(len <= 0)
  return -1;
 f.runes = 1;
 f.start = buf;
 f.to = buf;
 f.stop = buf + len - 1;
 f.flush = 0;
 f.farg = 0;
 f.nfmt = 0;
 __builtin_va_copy(f.args,args);
 fmtlocaleinit(&f, 0, 0, 0);
 dofmt(&f, fmt);
 __builtin_va_end(f.args);
 *(Rune*)f.to = '\0';
 return (Rune*)f.to - buf;
}

canonical_original_code:
int runevsnprint(unsigned int *buf, int len, char *fmt, __builtin_va_list args)
{
 struct Fmt f;
 if(len <= 0)
  return -1;
 f.runes = 1;
 f.start = buf;
 f.to = buf;
 f.stop = buf + len - 1;
 f.flush = 0;
 f.farg = 0;
 f.nfmt = 0;
 __builtin_va_copy(f.args,args);
 fmtlocaleinit(&f, 0, 0, 0);
 dofmt(&f, fmt);
 __builtin_va_end(f.args);
 *(unsigned int *)f.to = '\0';
 return (unsigned int *)f.to - buf;
}

variable_types:
buf
unsigned int *

variable_types: len int

variable_types:
fmt
char *

variable_types: struct Fmt { unsigned char runes; void *start; void *to; void *stop; int (*flush)(struct Fmt *); void *farg; int nfmt; __builtin_va_list args; unsigned int r; int width; int prec; unsigned long flags; char *decimal; char *thousands; char *grouping; }


decompiled_code:
__off64_t __fastcall p9seek(int a1, __off64_t a2, int a3)
{
return lseek64(a1, a2, a3);
}

canonical_decompiled_code:
__off64_t __fastcall func0(int a1, __off64_t a2, int a3)
{
return lseek64(a1, a2, a3);
}

original_code:
vlong
p9seek(int fd, vlong offset, int whence)
{
 return lseek(fd, offset, whence);
}

canonical_original_code:
long long p9seek(int fd, long long offset, int whence)
{
 return lseek(fd, offset, whence);
}

variable_types: fd int

variable_types:
offset
long long

variable_types: whence int

return_type: long long


decompiled_code:
__int64 __fastcall p9atoll(const char *a1)
{
return strtoll(a1, 0LL, 0);
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
return strtoll(a1, 0LL, 0);
}

original_code:
vlong
p9atoll(char *s)
{
 return strtoll(s, 0, 0);
}

canonical_original_code:
long long p9atoll(char *s)
{
 return strtoll(s, 0, 0);
}

variable_types:
s
char *


sysfatal

decompiled_code:
__int64 sysfatal(__int64 a1, ...)
{
const char *v1; // rdx
gcc_va_list va; // [rsp+8h] [rbp-1D0h] BYREF
char v4[256]; // [rsp+20h] [rbp-1B8h] BYREF
char v5[8]; // [rsp+120h] [rbp-B8h] BYREF

va_start(va, a1);
if ( sysfatal )
sysfatal(a1, va);
vseprint(v4, v5, a1, va);
_fixargv0();
v1 = (const char *)argv0;
if ( !argv0 )
v1 = "<prog>";
fprint(2LL, "%s: %s\n", v1, v4);
return exits("fatal");
}

canonical_decompiled_code:
__int64 func0(__int64 a1, ...)
{
const char *v1; // rdx
gcc_va_list va; // [rsp+8h] [rbp-1D0h] BYREF
char v4[256]; // [rsp+20h] [rbp-1B8h] BYREF
char v5[8]; // [rsp+120h] [rbp-B8h] BYREF

va_start(va, a1);
if ( sysfatal )
func0(a1, va);
vseprint(v4, v5, a1, va);
_fixargv0();
v1 = (const char *)argv0;
if ( !argv0 )
v1 = "<prog>";
fprint(2LL, "%s: %s\n", v1, v4);
return exits("fatal");
}

original_code:
void
sysfatal(char *fmt, ...)
{
 char buf[256];
 va_list arg;
 __builtin_va_start(arg,fmt);
 if(_sysfatal)
  (*_sysfatal)(fmt, arg);
 vseprint(buf, buf+sizeof buf, fmt, arg);
 __builtin_va_end(arg);
 __fixargv0();
 fprint(2, "%s: %s\n", argv0 ? argv0 : "<prog>", buf);
 exits("fatal");
}

canonical_original_code:
void sysfatal(char *fmt, ...)
{
 char buf[256];
 __builtin_va_list arg;
 __builtin_va_start(arg,fmt);
 if(_sysfatal)
  (*_sysfatal)(fmt, arg);
 vseprint(buf, buf+sizeof buf, fmt, arg);
 __builtin_va_end(arg);
 __fixargv0();
 fprint(2, "%s: %s\n", argv0 ? argv0 : "<prog>", buf);
 exits("fatal");
}

variable_types:
buf
char [256]

return_type: void


runeseprint

decompiled_code:
__int64 runeseprint(__int64 a1, __int64 a2, __int64 a3, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a3);
return runevseprint(a1, a2, a3, va);
}

canonical_decompiled_code:
__int64 func0(__int64 a1, __int64 a2, __int64 a3, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a3);
return runevseprint(a1, a2, a3, va);
}

original_code:
Rune*
runeseprint(Rune *buf, Rune *e, char *fmt, ...)
{
 Rune *p;
 va_list args;
 __builtin_va_start(args,fmt);
 p = runevseprint(buf, e, fmt, args);
 __builtin_va_end(args);
 return p;
}

canonical_original_code:
unsigned int *runeseprint(unsigned int *buf, unsigned int *e, char *fmt, ...)
{
 unsigned int *p;
 __builtin_va_list args;
 __builtin_va_start(args,fmt);
 p = runevseprint(buf, e, fmt, args);
 __builtin_va_end(args);
 return p;
}

variable_types:
e
unsigned int *

variable_types:
p
unsigned int *

return_type: unsigned int *


utfrune

decompiled_code:
char *__fastcall utfrune(const char *a1, __int64 a2)
{
const char *v2; // rbx
int v4; // eax
int v5[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v2 = a1;
if ( a2 <= 127 )
return strchr(a1, a2);
while ( 1 )
{
while ( *(unsigned __int8 *)v2 <= 0x7FuLL )
{
if ( !*v2 )
return 0LL;
++v2;
}
v4 = chartorune(v5, v2);
if ( v5[0] == a2 )
break;
v2 += v4;
}
return (char *)v2;
}

canonical_decompiled_code:
char *__fastcall func0(const char *a1, __int64 a2)
{
const char *v2; // rbx
int v4; // eax
int v5[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v2 = a1;
if ( a2 <= 127 )
return strchr(a1, a2);
while ( 1 )
{
while ( *(unsigned __int8 *)v2 <= 0x7FuLL )
{
if ( !*v2 )
return 0LL;
++v2;
}
v4 = chartorune(v5, v2);
if ( v5[0] == a2 )
break;
v2 += v4;
}
return (char *)v2;
}

original_code:
char*
utfrune(char *s, long c)
{
 long c1;
 Rune r;
 int n;
 if(c < Runesync)
  return strchr(s, c);
 for(;;) {
  c1 = *(_utfuchar*)s;
  if(c1 < Runeself) {
   if(c1 == 0)
    return 0;
   if(c1 == c)
    return s;
   s++;
   continue;
  }
  n = chartorune(&r, s);
  if(r == c)
   return s;
  s += n;
 }
}

canonical_original_code:
char *utfrune(char *s, long c)
{
 long c1;
 unsigned int r;
 int n;
 if(c < Runesync)
  return strchr(s, c);
 for(;;) {
  c1 = *(unsigned char *)s;
  if(c1 < Runeself) {
   if(c1 == 0)
    return 0;
   if(c1 == c)
    return s;
   s++;
   continue;
  }
  n = chartorune(&r, s);
  if(r == c)
   return s;
  s += n;
 }
}

variable_types: c long

variable_types:
r
unsigned int

variable_types: n int


truerand

decompiled_code:
__int64 truerand()
{
signed int i; // ebp
int v1; // eax
int v2; // ebx
__int64 buf[4]; // [rsp+8h] [rbp-20h] BYREF

if ( randfd_1 < 0 )
{
randfile_0 = (__int64)"/dev/random";
v1 = p9open("/dev/random", 0LL);
randfd_1 = v1;
if ( v1 < 0 || read(v1, buf, 1uLL) != 1 )
{
randfile_0 = (__int64)"/dev/srandom";
randfd_1 = p9open("/dev/srandom", 0LL);
}
if ( randfd_1 < 0 )
sysfatal("can't open %s: %r", randfile_0);
fcntl64((unsigned int)randfd_1, 2LL, 1LL);
}
for ( i = 0; (unsigned int)i <= 7; i += v2 )
{
v2 = readn((unsigned int)randfd_1, (char *)buf + i, 8LL - i);
if ( v2 < 0 )
sysfatal("can't read %s: %r", randfile_0);
}
return buf[0];
}

canonical_decompiled_code:
__int64 func0()
{
signed int i; // ebp
int v1; // eax
int v2; // ebx
__int64 buf[4]; // [rsp+8h] [rbp-20h] BYREF

if ( randfd_1 < 0 )
{
randfile_0 = (__int64)"/dev/random";
v1 = p9open("/dev/random", 0LL);
randfd_1 = v1;
if ( v1 < 0 || read(v1, buf, 1uLL) != 1 )
{
randfile_0 = (__int64)"/dev/srandom";
randfd_1 = p9open("/dev/srandom", 0LL);
}
if ( randfd_1 < 0 )
sysfatal("can't open %s: %r", randfile_0);
fcntl64((unsigned int)randfd_1, 2LL, 1LL);
}
for ( i = 0; (unsigned int)i <= 7; i += v2 )
{
v2 = readn((unsigned int)randfd_1, (char *)buf + i, 8LL - i);
if ( v2 < 0 )
sysfatal("can't read %s: %r", randfile_0);
}
return buf[0];
}

original_code:
ulong
truerand(void)
{
 int i, n;
 uchar buf[sizeof(ulong)];
 ulong x;
 static int randfd = -1;
 static char *randfile;
 if(randfd < 0){
  randfd = p9open(randfile="/dev/random", 0);
  if(randfd < 0 || read(randfd, buf, 1) != 1)
   randfd = p9open(randfile="/dev/srandom", 0);
  if(randfd < 0)
   sysfatal("can't open %s: %r", randfile);
  fcntl(randfd, 2, 1);
 }
 for(i=0; i<sizeof(buf); i += n)
  if((n = readn(randfd, buf+i, sizeof(buf)-i)) < 0)
   sysfatal("can't read %s: %r", randfile);
 memmove(&x, buf, sizeof x);
 return x;
}

canonical_original_code:
unsigned long truerand()
{
 int i; int n;
 unsigned char buf[-1];
 unsigned long x;
 static int randfd = -1;
 static char *randfile;
 if(randfd < 0){
  randfd = p9open(randfile="/dev/random", 0);
  if(randfd < 0 || read(randfd, buf, 1) != 1)
   randfd = p9open(randfile="/dev/srandom", 0);
  if(randfd < 0)
   sysfatal("can't open %s: %r", randfile);
  fcntl(randfd, 2, 1);
 }
 for(i=0; i<sizeof(buf); i += n)
  if((n = readn(randfd, buf+i, sizeof(buf)-i)) < 0)
   sysfatal("can't read %s: %r", randfile);
 memmove(&x, buf, sizeof x);
 return x;
}

variable_types: i int

variable_types:
buf
unsigned char [-1]

variable_types:
x
unsigned long

variable_types: randfd int

variable_types:
randfile
char *

return_type: unsigned long


strecpy

decompiled_code:
char *__fastcall strecpy(char *a1, unsigned __int64 a2, const void *a3)
{
char *v4; // rax

if ( (unsigned __int64)a1 < a2 )
{
v4 = (char *)memccpy(a1, a3, 0, a2 - (_QWORD)a1);
a1 = v4 - 1;
if ( !v4 )
{
a1 = (char *)(a2 - 1);
*(_BYTE *)(a2 - 1) = 0;
}
}
return a1;
}

canonical_decompiled_code:
char *__fastcall func0(char *a1, unsigned __int64 a2, const void *a3)
{
char *v4; // rax

if ( (unsigned __int64)a1 < a2 )
{
v4 = (char *)memccpy(a1, a3, 0, a2 - (_QWORD)a1);
a1 = v4 - 1;
if ( !v4 )
{
a1 = (char *)(a2 - 1);
*(_BYTE *)(a2 - 1) = 0;
}
}
return a1;
}

original_code:
char*
strecpy(char *to, char *e, char *from)
{
 if(to >= e)
  return to;
 to = memccpy(to, from, '\0', e - to);
 if(to == ((void*)0)){
  to = e - 1;
  *to = '\0';
 }else{
  to--;
 }
 return to;
}

canonical_original_code:
char *strecpy(char *to, char *e, char *from)
{
 if(to >= e)
  return to;
 to = memccpy(to, from, '\0', e - to);
 if(to == ((void *)0)){
  to = e - 1;
  *to = '\0';
 }else{
  to--;
 }
 return to;
}

variable_types:
to
char *

variable_types:
e
char *

variable_types:
from
char *


utfnlen

decompiled_code:
__int64 __fastcall utfnlen(_BYTE *a1, __int64 a2)
{
unsigned __int64 v2; // rbp
_BYTE *v3; // rbx
unsigned int v4; // er12
char v6[28]; // [rsp+Ch] [rbp-1Ch] BYREF

v2 = (unsigned __int64)&a1[a2];
if ( a1 < &a1[a2] )
{
v3 = a1;
v4 = 0;
while ( 1 )
{
if ( (unsigned __int8)*v3 > 0x7Fu )
{
if ( !(unsigned int)fullrune(v3, v2 - (_QWORD)v3) )
return v4;
v3 += (int)chartorune(v6, v3);
}
else
{
if ( !*v3 )
return v4;
++v3;
}
++v4;
if ( (unsigned __int64)v3 >= v2 )
return v4;
}
}
return 0;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1, __int64 a2)
{
unsigned __int64 v2; // rbp
_BYTE *v3; // rbx
unsigned int v4; // er12
char v6[28]; // [rsp+Ch] [rbp-1Ch] BYREF

v2 = (unsigned __int64)&a1[a2];
if ( a1 < &a1[a2] )
{
v3 = a1;
v4 = 0;
while ( 1 )
{
if ( (unsigned __int8)*v3 > 0x7Fu )
{
if ( !(unsigned int)fullrune(v3, v2 - (_QWORD)v3) )
return v4;
v3 += (int)chartorune(v6, v3);
}
else
{
if ( !*v3 )
return v4;
++v3;
}
++v4;
if ( (unsigned __int64)v3 >= v2 )
return v4;
}
}
return 0;
}

original_code:
int
utfnlen(char *s, long m)
{
 int c;
 long n;
 Rune rune;
 char *es;
 es = s + m;
 for(n = 0; s < es; n++) {
  c = *(_utfuchar*)s;
  if(c < Runeself){
   if(c == '\0')
    break;
   s++;
   continue;
  }
  if(!fullrune(s, es-s))
   break;
  s += chartorune(&rune, s);
 }
 return n;
}

canonical_original_code:
int utfnlen(char *s, long m)
{
 int c;
 long n;
 unsigned int rune;
 char *es;
 es = s + m;
 for(n = 0; s < es; n++) {
  c = *(unsigned char *)s;
  if(c < Runeself){
   if(c == '\0')
    break;
   s++;
   continue;
  }
  if(!fullrune(s, es-s))
   break;
  s += chartorune(&rune, s);
 }
 return n;
}

variable_types: m long

variable_types: c int

variable_types: n long

variable_types:
rune
unsigned int

variable_types:
es
char *


exits

decompiled_code:
void __fastcall __noreturn exits(_BYTE *a1)
{
int v1; // eax

if ( a1 )
{
if ( *a1 )
{
v1 = exitcode();
exit(v1);
}
}
exit(0);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(_BYTE *a1)
{
int v1; // eax

if ( a1 )
{
if ( *a1 )
{
v1 = exitcode();
exit(v1);
}
}
exit(0);
}

original_code:
void
exits(char *s)
{
 int i, pid;
 void (*f)(void);
 pid = getpid();
 for(i = 33 -1; i >= 0; i--)
  if((f = onex[i].f) && pid == onex[i].pid) {
   onex[i].f = 0;
   (*f)();
  }
 if(s == 0 || *s == 0)
  exit(0);
 exit(exitcode(s));
}

canonical_original_code:
void exits(char *s)
{
 int i; int pid;
 void (*f)();
 pid = getpid();
 for(i = 33 -1; i >= 0; i--)
  if((f = onex[i].f) && pid == onex[i].pid) {
   onex[i].f = 0;
   (*f)();
  }
 if(s == 0 || *s == 0)
  exit(0);
 exit(exitcode(s));
}

variable_types:
s
char *

variable_types: pid int

variable_types:
f
void (*)()


addressReadCmd

repo: martin-c/beagle-bms

decompiled_code:
__int64 __fastcall addressReadCmd(unsigned int a1, unsigned __int8 a2, unsigned int a3, void (__fastcall *a4)(__int64, _QWORD), __int64 a5)
{
int v7; // eax
__int64 result; // rax

if ( a4 )
{
if ( a5 )
{
txB = 0LL;
*((_DWORD *)&txB + 2) = 0;
if ( a2 > 0xFu )
{
printf("addressReadCmd() with invalid 6804-2 address: %u\r\n", a2);
result = 0xFFFFFFFFLL;
}
else
{
ltc6804util_encodeCommand(a2, a3, &txB);
v7 = spiIo(a1, &txB, &rxB, 12LL);
if ( v7 < 0 )
{
printf("addressWriteCmd(): ioctl() returned %d\r\n", (unsigned int)v7);
result = 0xFFFFFFFFLL;
}
else
{
a4(a5, (unsigned int)((_DWORD)&rxB + 4));
result = 0LL;
}
}
}
else
{
puts("addressReadCmd() with NULL data_s parameter.\r");
result = 0xFFFFFFFFLL;
}
}
else
{
puts("addressReadCmd() with NULL decode function.\r");
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1, unsigned __int8 a2, unsigned int a3, void (__fastcall *a4)(__int64, _QWORD), __int64 a5)
{
int v7; // eax
__int64 result; // rax

if ( a4 )
{
if ( a5 )
{
txB = 0LL;
*((_DWORD *)&txB + 2) = 0;
if ( a2 > 0xFu )
{
printf("addressReadCmd() with invalid 6804-2 address: %u\r\n", a2);
result = 0xFFFFFFFFLL;
}
else
{
ltc6804util_encodeCommand(a2, a3, &txB);
v7 = spiIo(a1, &txB, &rxB, 12LL);
if ( v7 < 0 )
{
printf("addressWriteCmd(): ioctl() returned %d\r\n", (unsigned int)v7);
result = 0xFFFFFFFFLL;
}
else
{
a4(a5, (unsigned int)((_DWORD)&rxB + 4));
result = 0LL;
}
}
}
else
{
puts("addressReadCmd() with NULL data_s parameter.\r");
result = 0xFFFFFFFFLL;
}
}
else
{
puts("addressReadCmd() with NULL decode function.\r");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
static int addressReadCmd(int fd,
        u_int8_t address,
        enum ltc6804_command_codes_e cmd,
        void (*decode)(void *, u_int8_t *),
        void *data_s) {
    if (decode == ((void *)0)) {
        printf("addressReadCmd() with NULL decode function.\r\n");
        return -1;
    }
    if (data_s == ((void *)0)) {
        printf("addressReadCmd() with NULL data_s parameter.\r\n");
        return -1;
    }
    memset(txB, 0, 12);
    if (address > 0x0F) {
        printf("addressReadCmd() with invalid 6804-2 address: %u\r\n", (unsigned int)address);
        return -1;
    }
    ltc6804util_encodeCommand(address, cmd, txB);
    int stat = spiIo(fd, txB, rxB, 12);
    if (stat < 0) {
        printf("addressWriteCmd(): ioctl() returned %d\r\n", stat);
        return -1;
    }
    decode(data_s, &rxB[4]);
    return 0;
}

canonical_original_code:
static int addressReadCmd(int fd, unsigned int address, enum ltc6804_command_codes_e cmd, void (*decode)(void *, unsigned int *), void *data_s) {
    if (decode == ((void *)0)) {
        printf("addressReadCmd() with NULL decode function.\r\n");
        return -1;
    }
    if (data_s == ((void *)0)) {
        printf("addressReadCmd() with NULL data_s parameter.\r\n");
        return -1;
    }
    memset(txB, 0, 12);
    if (address > 0x0F) {
        printf("addressReadCmd() with invalid 6804-2 address: %u\r\n", (unsigned int)address);
        return -1;
    }
    ltc6804util_encodeCommand(address, cmd, txB);
    int stat = spiIo(fd, txB, rxB, 12);
    if (stat < 0) {
        printf("addressWriteCmd(): ioctl() returned %d\r\n", stat);
        return -1;
    }
    decode(data_s, &rxB[4]);
    return 0;
}

variable_types: fd int

variable_types:
address
unsigned int

variable_types: enum ltc6804_command_codes_e { CMD_WRCFG=1, CMD_RDCFG=2, CMD_RDCVA=4, CMD_RDCVB=6, CMD_RDCVC=8, CMD_RDCVD=10, CMD_RDAUXA=12, CMD_RDAUXB=14, CMD_RDSTATA=16, CMD_RDSTATB=18, CMD_ADCV=608, CMD_ADOW=552, CMD_CVST=519, CMD_ADAX=1120, CMD_AXST=1031, CMD_ADSTAT=1128, CMD_STATST=1039, CMD_ADCVAX=1135, CMD_CLRCELL=1809, CMD_CLRAUX=1810, CMD_CLRSTAT=1811, CMD_PLADC=1812, CMD_DIAGN=1813, CMD_WRCOMM=1825, CMD_RDCOMM=1826, CMD_STCOMM=1827 }

variable_types:
decode
void (*)(void *, unsigned int *)

variable_types: stat int


sendCmd

decompiled_code:
__int64 __fastcall sendCmd(unsigned int a1, unsigned __int8 a2, unsigned int a3)
{
int v3; // eax
__int64 result; // rax

if ( a2 > 0xFu )
{
printf("sendCmd() with invalid 6804-2 address:%u\r\n", a2);
result = 0xFFFFFFFFLL;
}
else
{
ltc6804util_encodeCommand(a2, a3, &txB);
v3 = spiIo(a1, &txB, &rxB, 4LL);
if ( v3 < 0 )
{
printf("sendCmd(): ioctl() returned %d\r\n", (unsigned int)v3);
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1, unsigned __int8 a2, unsigned int a3)
{
int v3; // eax
__int64 result; // rax

if ( a2 > 0xFu )
{
printf("sendCmd() with invalid 6804-2 address:%u\r\n", a2);
result = 0xFFFFFFFFLL;
}
else
{
ltc6804util_encodeCommand(a2, a3, &txB);
v3 = spiIo(a1, &txB, &rxB, 4LL);
if ( v3 < 0 )
{
printf("sendCmd(): ioctl() returned %d\r\n", (unsigned int)v3);
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
return result;
}

original_code:
static int sendCmd(int fd, u_int8_t address, enum ltc6804_command_codes_e cmd) {
    if (address > 0x0F) {
        printf("sendCmd() with invalid 6804-2 address:%u\r\n", (unsigned int)address);
        return -1;
    }
    ltc6804util_encodeCommand(address, cmd, txB);
    int stat = spiIo(fd, txB, rxB, 4);
    if (stat < 0) {
        printf("sendCmd(): ioctl() returned %d\r\n", stat);
        return -1;
    }
    return 0;
}

canonical_original_code:
static int sendCmd(int fd, unsigned int address, enum ltc6804_command_codes_e cmd) {
    if (address > 0x0F) {
        printf("sendCmd() with invalid 6804-2 address:%u\r\n", (unsigned int)address);
        return -1;
    }
    ltc6804util_encodeCommand(address, cmd, txB);
    int stat = spiIo(fd, txB, rxB, 4);
    if (stat < 0) {
        printf("sendCmd(): ioctl() returned %d\r\n", stat);
        return -1;
    }
    return 0;
}

user_defined_types: enum ltc6804_command_codes_e { CMD_WRCFG=1, CMD_RDCFG=2, CMD_RDCVA=4, CMD_RDCVB=6, CMD_RDCVC=8, CMD_RDCVD=10, CMD_RDAUXA=12, CMD_RDAUXB=14, CMD_RDSTATA=16, CMD_RDSTATB=18, CMD_ADCV=608, CMD_ADOW=552, CMD_CVST=519, CMD_ADAX=1120, CMD_AXST=1031, CMD_ADSTAT=1128, CMD_STATST=1039, CMD_ADCVAX=1135, CMD_CLRCELL=1809, CMD_CLRAUX=1810, CMD_CLRSTAT=1811, CMD_PLADC=1812, CMD_DIAGN=1813, CMD_WRCOMM=1825, CMD_RDCOMM=1826, CMD_STCOMM=1827 }


decompiled_code:
__int64 ltc6804_close()
{
return spiClose(&spiFd);
}

canonical_decompiled_code:
__int64 func4()
{
return spiClose(&spiFd);
}

original_code:
void ltc6804_close(void) {
    spiClose(&spiFd);
}

canonical_original_code:
void ltc6804_close() {
    spiClose(&spiFd);
}


decompiled_code:
__int64 __fastcall ltc6804_readStatA(unsigned __int8 a1, __int64 a2)
{
dummyIo(spiFd, 1u);
*(_DWORD *)(a2 + 8) = 1;
if ( (int)addressReadCmd(spiFd, a1, 0x10u, (void (__fastcall *)(__int64, _QWORD))&ltc6804util_decodeStatA, a2) >= 0 )
return 0LL;
perror("addressReadCmd");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned __int8 a1, __int64 a2)
{
func0(spiFd, 1u);
*(_DWORD *)(a2 + 8) = 1;
if ( (int)func1(spiFd, a1, 0x10u, (void (__fastcall *)(__int64, _QWORD))&ltc6804util_decodeStatA, a2) >= 0 )
return 0LL;
perror("addressReadCmd");
return 0xFFFFFFFFLL;
}

original_code:
int ltc6804_readStatA(u_int8_t address, struct ltc6804_stat_reg_a_s *g) {
    dummyIo(spiFd, 1);
    g->pecOk = PEC_OK;
    int s = addressReadCmd(spiFd, address, CMD_RDSTATA, ltc6804util_decodeStatA, g);
    if (s < 0) {
        perror("addressReadCmd");
        return -1;
    }
    return 0;
}

canonical_original_code:
int ltc6804_readStatA(unsigned int address, struct ltc6804_stat_reg_a_s *g) {
    dummyIo(spiFd, 1);
    g->pecOk = PEC_OK;
    int s = addressReadCmd(spiFd, address, CMD_RDSTATA, ltc6804util_decodeStatA, g);
    if (s < 0) {
        perror("addressReadCmd");
        return -1;
    }
    return 0;
}

variable_types: struct ltc6804_stat_reg_a_s { unsigned int soc; unsigned int itmp; unsigned int va; enum ltc6804_PEC_e { PEC_ERROR=0, PEC_OK=1 } pecOk; } *

variable_types: s int

user_defined_types: struct ltc6804_stat_reg_a_s { unsigned int soc; unsigned int itmp; unsigned int va; enum ltc6804_PEC_e pecOk; }


decompiled_code:
__int64 __fastcall ltc6804_readStatB(unsigned __int8 a1, __int64 a2)
{
dummyIo(spiFd, 1u);
*(_DWORD *)(a2 + 56) = 1;
if ( (int)addressReadCmd(spiFd, a1, 0x12u, (void (__fastcall *)(__int64, _QWORD))&ltc6804util_decodeStatB, a2) >= 0 )
return 0LL;
perror("addressReadCmd");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned __int8 a1, __int64 a2)
{
func0(spiFd, 1u);
*(_DWORD *)(a2 + 56) = 1;
if ( (int)func1(spiFd, a1, 0x12u, (void (__fastcall *)(__int64, _QWORD))&ltc6804util_decodeStatB, a2) >= 0 )
return 0LL;
perror("addressReadCmd");
return 0xFFFFFFFFLL;
}

original_code:
int ltc6804_readStatB(u_int8_t address, struct ltc6804_stat_reg_b_s *g) {
    dummyIo(spiFd, 1);
    g->pecOk = PEC_OK;
    int s = addressReadCmd(spiFd, address, CMD_RDSTATB, ltc6804util_decodeStatB, g);
    if (s < 0) {
        perror("addressReadCmd");
        return -1;
    }
    return 0;
}

canonical_original_code:
int ltc6804_readStatB(unsigned int address, struct ltc6804_stat_reg_b_s *g) {
    dummyIo(spiFd, 1);
    g->pecOk = PEC_OK;
    int s = addressReadCmd(spiFd, address, CMD_RDSTATB, ltc6804util_decodeStatB, g);
    if (s < 0) {
        perror("addressReadCmd");
        return -1;
    }
    return 0;
}

variable_types: struct ltc6804_stat_reg_b_s { unsigned int vd; enum ltc6804_cell_flags_e { CF_UNDERVOLTAGE=1, CF_OVERVOLTAGE=2 } cell_flags[12]; unsigned int rev; unsigned int muxfail; unsigned int thsd; enum ltc6804_PEC_e { PEC_ERROR=0, PEC_OK=1 } pecOk; } *

user_defined_types: struct ltc6804_stat_reg_b_s { unsigned int vd; enum ltc6804_cell_flags_e cell_flags[12]; unsigned int rev; unsigned int muxfail; unsigned int thsd; enum ltc6804_PEC_e pecOk; }


decompiled_code:
__int64 __fastcall ltc6804_readConfig(unsigned __int8 a1, __int64 a2)
{
dummyIo(spiFd, 1u);
*(_DWORD *)(a2 + 16) = 1;
if ( (int)addressReadCmd(spiFd, a1, 2u, (void (__fastcall *)(__int64, _QWORD))&ltc6804util_decodeCfg, a2) >= 0 )
return 0LL;
perror("addressReadCmd");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned __int8 a1, __int64 a2)
{
func0(spiFd, 1u);
*(_DWORD *)(a2 + 16) = 1;
if ( (int)func1(spiFd, a1, 2u, (void (__fastcall *)(__int64, _QWORD))&ltc6804util_decodeCfg, a2) >= 0 )
return 0LL;
perror("addressReadCmd");
return 0xFFFFFFFFLL;
}

original_code:
int ltc6804_readConfig(u_int8_t address, struct ltc6804_cfg_reg_s *g) {
    dummyIo(spiFd, 1);
    g->pecOk = PEC_OK;
    int s = addressReadCmd(spiFd, address, CMD_RDCFG, ltc6804util_decodeCfg, g);
    if (s < 0) {
        perror("addressReadCmd");
        return -1;
    }
    return 0;
}

canonical_original_code:
int ltc6804_readConfig(unsigned int address, struct ltc6804_cfg_reg_s *g) {
    dummyIo(spiFd, 1);
    g->pecOk = PEC_OK;
    int s = addressReadCmd(spiFd, address, CMD_RDCFG, ltc6804util_decodeCfg, g);
    if (s < 0) {
        perror("addressReadCmd");
        return -1;
    }
    return 0;
}

variable_types: struct ltc6804_cfg_reg_s { unsigned int gpio; unsigned int refon; unsigned int swtrd; unsigned int adcopt; unsigned int vuv; unsigned int vov; unsigned int dcc; enum ltc6804_DCTO_e { DCTO_DISABLED_gc=0, DCTO_30_SEC_gc=1, DCTO_1_MIN_gc=2, DCTO_2_MIN_gc=3, DCTO_3_MIN_gc=4, DCTO_4_MIN_gc=5, DCTO_5_MIN_gc=6, DCTO_10_MIN_gc=7, DCTO_15_MIN_gc=8, DCTO_20_MIN_gc=9, DCTO_30_MIN_gc=10, DCTO_40_MIN_gc=11, DCTO_60_MIN_gc=12, DCTO_75_MIN_gc=13, DCTO_90_MIN_gc=14, DCTO_120_MIN_gc=15 } dcto; enum ltc6804_PEC_e { PEC_ERROR=0, PEC_OK=1 } pecOk; } *

user_defined_types: struct ltc6804_cfg_reg_s { unsigned int gpio; unsigned int refon; unsigned int swtrd; unsigned int adcopt; unsigned int vuv; unsigned int vov; unsigned int dcc; enum ltc6804_DCTO_e dcto; enum ltc6804_PEC_e pecOk; }


decompiled_code:
__int64 __fastcall ltc6804_writeConfig(unsigned __int8 a1, __int64 a2)
{
dummyIo(spiFd, 1u);
*(_DWORD *)(a2 + 16) = 1;
if ( (int)addressWriteCmd(spiFd, a1, 1u, (void (__fastcall *)(_QWORD, __int64))&ltc6804util_encodeCfg, a2) >= 0 )
return 0LL;
perror("addressReadCmd");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned __int8 a1, __int64 a2)
{
func0(spiFd, 1u);
*(_DWORD *)(a2 + 16) = 1;
if ( (int)func2(spiFd, a1, 1u, (void (__fastcall *)(_QWORD, __int64))&ltc6804util_encodeCfg, a2) >= 0 )
return 0LL;
perror("addressReadCmd");
return 0xFFFFFFFFLL;
}

original_code:
int ltc6804_writeConfig(u_int8_t address, struct ltc6804_cfg_reg_s *g) {
    dummyIo(spiFd, 1);
    g->pecOk = PEC_OK;
    int s = addressWriteCmd(spiFd, address, CMD_WRCFG, ltc6804util_encodeCfg, g);
    if (s < 0) {
        perror("addressReadCmd");
        return -1;
    }
    return 0;
}

canonical_original_code:
int ltc6804_writeConfig(unsigned int address, struct ltc6804_cfg_reg_s *g) {
    dummyIo(spiFd, 1);
    g->pecOk = PEC_OK;
    int s = addressWriteCmd(spiFd, address, CMD_WRCFG, ltc6804util_encodeCfg, g);
    if (s < 0) {
        perror("addressReadCmd");
        return -1;
    }
    return 0;
}

user_defined_types: enum ltc6804_DCTO_e { DCTO_DISABLED_gc=0, DCTO_30_SEC_gc=1, DCTO_1_MIN_gc=2, DCTO_2_MIN_gc=3, DCTO_3_MIN_gc=4, DCTO_4_MIN_gc=5, DCTO_5_MIN_gc=6, DCTO_10_MIN_gc=7, DCTO_15_MIN_gc=8, DCTO_20_MIN_gc=9, DCTO_30_MIN_gc=10, DCTO_40_MIN_gc=11, DCTO_60_MIN_gc=12, DCTO_75_MIN_gc=13, DCTO_90_MIN_gc=14, DCTO_120_MIN_gc=15 }

user_defined_types: enum ltc6804_PEC_e { PEC_ERROR=0, PEC_OK=1 }


decompiled_code:
__int64 __fastcall ltc6804_readCellVoltageGroup(unsigned __int8 a1, char a2, __int64 a3)
{
unsigned int v4; // edx

dummyIo(spiFd, 1u);
if ( a2 == 67 )
{
v4 = 8;
}
else if ( a2 > 67 )
{
v4 = 10;
if ( a2 != 68 )
goto LABEL_9;
}
else
{
if ( a2 != 65 )
{
if ( a2 == 66 )
{
v4 = 6;
goto LABEL_6;
}
LABEL_9:
printf("ltc6804_readCellVoltageGroup(): cannot read from group %c.\r\n", (unsigned int)a2);
return 0xFFFFFFFFLL;
}
v4 = 4;
}
LABEL_6:
*(_DWORD *)(a3 + 8) = 1;
if ( (int)addressReadCmd(spiFd, a1, v4, (void (__fastcall *)(__int64, _QWORD))&ltc6804util_decodeCV, a3) >= 0 )
return 0LL;
perror("addressReadCmd");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned __int8 a1, char a2, __int64 a3)
{
unsigned int v4; // edx

func0(spiFd, 1u);
if ( a2 == 67 )
{
v4 = 8;
}
else if ( a2 > 67 )
{
v4 = 10;
if ( a2 != 68 )
goto LABEL_9;
}
else
{
if ( a2 != 65 )
{
if ( a2 == 66 )
{
v4 = 6;
goto LABEL_6;
}
LABEL_9:
printf("ltc6804_readCellVoltageGroup(): cannot read from group %c.\r\n", (unsigned int)a2);
return 0xFFFFFFFFLL;
}
v4 = 4;
}
LABEL_6:
*(_DWORD *)(a3 + 8) = 1;
if ( (int)func1(spiFd, a1, v4, (void (__fastcall *)(__int64, _QWORD))&ltc6804util_decodeCV, a3) >= 0 )
return 0LL;
perror("addressReadCmd");
return 0xFFFFFFFFLL;
}

original_code:
int ltc6804_readCellVoltageGroup(u_int8_t address, char group, struct ltc6804_cell_voltage_reg_s *g) {
    dummyIo(spiFd, 1);
    enum ltc6804_command_codes_e cmd;
    switch (group) {
        case 'A':
            cmd = CMD_RDCVA;
            break;
        case 'B':
            cmd = CMD_RDCVB;
            break;
        case 'C':
            cmd = CMD_RDCVC;
            break;
        case 'D':
            cmd = CMD_RDCVD;
            break;
        default:
            printf("ltc6804_readCellVoltageGroup(): cannot read from group %c.\r\n", (int)group);
            return -1;
    }
    g->pecOk = PEC_OK;
    int s = addressReadCmd(spiFd, address, cmd, ltc6804util_decodeCV, g);
    if (s < 0) {
        perror("addressReadCmd");
        return -1;
    }
    return 0;
}

canonical_original_code:
int ltc6804_readCellVoltageGroup(unsigned int address, char group, struct ltc6804_cell_voltage_reg_s *g) {
    dummyIo(spiFd, 1);
    enum ltc6804_command_codes_e cmd;
    switch (group) {
        case 'A':
            cmd = CMD_RDCVA;
            break;
        case 'B':
            cmd = CMD_RDCVB;
            break;
        case 'C':
            cmd = CMD_RDCVC;
            break;
        case 'D':
            cmd = CMD_RDCVD;
            break;
        default:
            printf("ltc6804_readCellVoltageGroup(): cannot read from group %c.\r\n", (int)group);
            return -1;
    }
    g->pecOk = PEC_OK;
    int s = addressReadCmd(spiFd, address, cmd, ltc6804util_decodeCV, g);
    if (s < 0) {
        perror("addressReadCmd");
        return -1;
    }
    return 0;
}

variable_types: group char

variable_types: struct ltc6804_cell_voltage_reg_s { unsigned int offset[3]; enum ltc6804_PEC_e { PEC_ERROR=0, PEC_OK=1 } pecOk; } *

user_defined_types: struct ltc6804_cell_voltage_reg_s { unsigned int offset[3]; enum ltc6804_PEC_e pecOk; }


decompiled_code:
__int64 __fastcall ltc6804_readCellVoltageGroups(unsigned __int8 a1, _WORD *a2)
{
unsigned int *v2; // r12
_WORD *v3; // rbx
__int16 v5[4]; // [rsp+4h] [rbp-44h] BYREF
int v6; // [rsp+Ch] [rbp-3Ch]
int v7[14]; // [rsp+10h] [rbp-38h] BYREF

dummyIo(spiFd, 1u);
v7[0] = 4;
v7[1] = 6;
v7[2] = 8;
v7[3] = 10;
*((_DWORD *)a2 + 6) = 1;
v2 = (unsigned int *)v7;
v3 = a2;
while ( 1 )
{
v6 = 1;
if ( (int)addressReadCmd(spiFd, a1, *v2, (void (__fastcall *)(__int64, _QWORD))&ltc6804util_decodeCV, (__int64)v5) < 0 )
break;
*v3 = v5[0];
v3[1] = v5[1];
v3[2] = v5[2];
*((_DWORD *)a2 + 6) &= v6;
++v2;
v3 += 3;
if ( v3 == a2 + 12 )
return 0LL;
}
perror("addressReadCmd");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned __int8 a1, _WORD *a2)
{
unsigned int *v2; // r12
_WORD *v3; // rbx
__int16 v5[4]; // [rsp+4h] [rbp-44h] BYREF
int v6; // [rsp+Ch] [rbp-3Ch]
int v7[14]; // [rsp+10h] [rbp-38h] BYREF

func0(spiFd, 1u);
v7[0] = 4;
v7[1] = 6;
v7[2] = 8;
v7[3] = 10;
*((_DWORD *)a2 + 6) = 1;
v2 = (unsigned int *)v7;
v3 = a2;
while ( 1 )
{
v6 = 1;
if ( (int)func1(spiFd, a1, *v2, (void (__fastcall *)(__int64, _QWORD))&ltc6804util_decodeCV, (__int64)v5) < 0 )
break;
*v3 = v5[0];
v3[1] = v5[1];
v3[2] = v5[2];
*((_DWORD *)a2 + 6) &= v6;
++v2;
v3 += 3;
if ( v3 == a2 + 12 )
return 0LL;
}
perror("addressReadCmd");
return 0xFFFFFFFFLL;
}

original_code:
int ltc6804_readCellVoltageGroups(u_int8_t address, struct ltc6804_cell_voltage_s *c) {
    dummyIo(spiFd, 1);
    enum ltc6804_command_codes_e cmd[] = {CMD_RDCVA, CMD_RDCVB, CMD_RDCVC, CMD_RDCVD};
    struct ltc6804_cell_voltage_reg_s g;
    int i;
    int j=0;
    c->pecOk = PEC_OK;
    for (i=0; i<4; i++) {
        g.pecOk = PEC_OK;
        int s = addressReadCmd(spiFd, address, cmd[i], ltc6804util_decodeCV, &g);
        if (s < 0) {
            perror("addressReadCmd");
            return -1;
        }
        c->cell[j++] = g.offset[0];
        c->cell[j++] = g.offset[1];
        c->cell[j++] = g.offset[2];
        c->pecOk &= g.pecOk;
    }
    return 0;
}

canonical_original_code:
int ltc6804_readCellVoltageGroups(unsigned int address, struct ltc6804_cell_voltage_s *c) {
    dummyIo(spiFd, 1);
    enum ltc6804_command_codes_e cmd[] = {CMD_RDCVA, CMD_RDCVB, CMD_RDCVC, CMD_RDCVD};
    struct ltc6804_cell_voltage_reg_s g;
    int i;
    int j = 0;
    c->pecOk = PEC_OK;
    for (i=0; i<4; i++) {
        g.pecOk = PEC_OK;
        int s = addressReadCmd(spiFd, address, cmd[i], ltc6804util_decodeCV, &g);
        if (s < 0) {
            perror("addressReadCmd");
            return -1;
        }
        c->cell[j++] = g.offset[0];
        c->cell[j++] = g.offset[1];
        c->cell[j++] = g.offset[2];
        c->pecOk &= g.pecOk;
    }
    return 0;
}

variable_types: struct ltc6804_cell_voltage_s { unsigned int cell[12]; enum ltc6804_PEC_e { PEC_ERROR=0, PEC_OK=1 } pecOk; } *

variable_types: enum ltc6804_command_codes_e { CMD_WRCFG=1, CMD_RDCFG=2, CMD_RDCVA=4, CMD_RDCVB=6, CMD_RDCVC=8, CMD_RDCVD=10, CMD_RDAUXA=12, CMD_RDAUXB=14, CMD_RDSTATA=16, CMD_RDSTATB=18, CMD_ADCV=608, CMD_ADOW=552, CMD_CVST=519, CMD_ADAX=1120, CMD_AXST=1031, CMD_ADSTAT=1128, CMD_STATST=1039, CMD_ADCVAX=1135, CMD_CLRCELL=1809, CMD_CLRAUX=1810, CMD_CLRSTAT=1811, CMD_PLADC=1812, CMD_DIAGN=1813, CMD_WRCOMM=1825, CMD_RDCOMM=1826, CMD_STCOMM=1827 } []

variable_types: struct ltc6804_cell_voltage_reg_s { unsigned int offset[3]; enum ltc6804_PEC_e { PEC_ERROR=0, PEC_OK=1 } pecOk; }

variable_types: i int

variable_types: j int

user_defined_types: struct ltc6804_cell_voltage_s { unsigned int cell[12]; enum ltc6804_PEC_e pecOk; }


decompiled_code:
__int64 __fastcall ltc6804_readAuxGroupA(unsigned __int8 a1, __int64 a2)
{
dummyIo(spiFd, 1u);
*(_DWORD *)(a2 + 8) = 1;
if ( (int)addressReadCmd(spiFd, a1, 0xCu, (void (__fastcall *)(__int64, _QWORD))&ltc6804util_decodeAuxA, a2) >= 0 )
return 0LL;
perror("addressReadCmd");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func11(unsigned __int8 a1, __int64 a2)
{
func0(spiFd, 1u);
*(_DWORD *)(a2 + 8) = 1;
if ( (int)func1(spiFd, a1, 0xCu, (void (__fastcall *)(__int64, _QWORD))&ltc6804util_decodeAuxA, a2) >= 0 )
return 0LL;
perror("addressReadCmd");
return 0xFFFFFFFFLL;
}

original_code:
int ltc6804_readAuxGroupA(u_int8_t address, struct ltc6804_aux_reg_a_s *g) {
    dummyIo(spiFd, 1);
    g->pecOk = PEC_OK;
    int s = addressReadCmd(spiFd, address, CMD_RDAUXA, ltc6804util_decodeAuxA, g);
    if (s < 0) {
        perror("addressReadCmd");
        return -1;
    }
    return 0;
}

canonical_original_code:
int ltc6804_readAuxGroupA(unsigned int address, struct ltc6804_aux_reg_a_s *g) {
    dummyIo(spiFd, 1);
    g->pecOk = PEC_OK;
    int s = addressReadCmd(spiFd, address, CMD_RDAUXA, ltc6804util_decodeAuxA, g);
    if (s < 0) {
        perror("addressReadCmd");
        return -1;
    }
    return 0;
}

variable_types: struct ltc6804_aux_reg_a_s { unsigned int g1v; unsigned int g2v; unsigned int g3v; enum ltc6804_PEC_e { PEC_ERROR=0, PEC_OK=1 } pecOk; } *

user_defined_types: struct ltc6804_aux_reg_a_s { unsigned int g1v; unsigned int g2v; unsigned int g3v; enum ltc6804_PEC_e pecOk; }


decompiled_code:
__int64 __fastcall ltc6804_readAuxGroupB(unsigned __int8 a1, __int64 a2)
{
dummyIo(spiFd, 1u);
*(_DWORD *)(a2 + 8) = 1;
if ( (int)addressReadCmd(spiFd, a1, 0xEu, (void (__fastcall *)(__int64, _QWORD))&ltc6804util_decodeAuxB, a2) >= 0 )
return 0LL;
perror("addressReadCmd");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func12(unsigned __int8 a1, __int64 a2)
{
func0(spiFd, 1u);
*(_DWORD *)(a2 + 8) = 1;
if ( (int)func1(spiFd, a1, 0xEu, (void (__fastcall *)(__int64, _QWORD))&ltc6804util_decodeAuxB, a2) >= 0 )
return 0LL;
perror("addressReadCmd");
return 0xFFFFFFFFLL;
}

original_code:
int ltc6804_readAuxGroupB(u_int8_t address, struct ltc6804_aux_reg_b_s *g) {
    dummyIo(spiFd, 1);
    g->pecOk = PEC_OK;
    int s = addressReadCmd(spiFd, address, CMD_RDAUXB, ltc6804util_decodeAuxB, g);
    if (s < 0) {
        perror("addressReadCmd");
        return -1;
    }
    return 0;
}

canonical_original_code:
int ltc6804_readAuxGroupB(unsigned int address, struct ltc6804_aux_reg_b_s *g) {
    dummyIo(spiFd, 1);
    g->pecOk = PEC_OK;
    int s = addressReadCmd(spiFd, address, CMD_RDAUXB, ltc6804util_decodeAuxB, g);
    if (s < 0) {
        perror("addressReadCmd");
        return -1;
    }
    return 0;
}

variable_types: struct ltc6804_aux_reg_b_s { unsigned int g4v; unsigned int g5v; unsigned int ref; enum ltc6804_PEC_e { PEC_ERROR=0, PEC_OK=1 } pecOk; } *

user_defined_types: struct ltc6804_aux_reg_b_s { unsigned int g4v; unsigned int g5v; unsigned int ref; enum ltc6804_PEC_e pecOk; }


decompiled_code:
__int64 __fastcall ltc6804_startCellVoltageConversion(unsigned __int8 a1, int a2, int a3)
{
dummyIo(spiFd, 1u);
if ( (int)sendCmd(spiFd, a1, a3 | a2 | 0x260u) >= 0 )
return 0LL;
perror("sendCmd");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func13(unsigned __int8 a1, int a2, int a3)
{
func0(spiFd, 1u);
if ( (int)func3(spiFd, a1, a3 | a2 | 0x260u) >= 0 )
return 0LL;
perror("sendCmd");
return 0xFFFFFFFFLL;
}

original_code:
int ltc6804_startCellVoltageConversion(u_int8_t address,
        enum ltc6804_ADC_modes_e mode,
        enum ltc6804_CH_e cells) {
    dummyIo(spiFd, 1);
    int s = sendCmd(spiFd, address, CMD_ADCV | mode | cells);
    if (s < 0) {
        perror("sendCmd");
        return -1;
    }
    return 0;
}

canonical_original_code:
int ltc6804_startCellVoltageConversion(unsigned int address, enum ltc6804_ADC_modes_e mode, enum ltc6804_CH_e cells) {
    dummyIo(spiFd, 1);
    int s = sendCmd(spiFd, address, CMD_ADCV | mode | cells);
    if (s < 0) {
        perror("sendCmd");
        return -1;
    }
    return 0;
}

variable_types:
address
unsigned int

variable_types: enum ltc6804_ADC_modes_e { ADC_MD_FAST_gc=expr, ADC_MD_NORM_gc=expr, ADC_MD_FILTERED_gc=expr }

variable_types: enum ltc6804_CH_e { CH_ALL_CELLS_gc=0, CH_CELL_1_7_gc=1, CH_CELL_2_8_gc=2, CH_CELL_3_9_gc=3, CH_CELL_4_10_gc=4, CH_CELL_5_11_gc=5, CH_CELL_6_12_gc=6 }


decompiled_code:
__int64 __fastcall ltc6804_startGpioVoltageConversion(unsigned __int8 a1, int a2, int a3)
{
dummyIo(spiFd, 1u);
if ( (int)sendCmd(spiFd, a1, a3 | a2 | 0x460u) >= 0 )
return 0LL;
perror("sendCmd");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func14(unsigned __int8 a1, int a2, int a3)
{
func0(spiFd, 1u);
if ( (int)func3(spiFd, a1, a3 | a2 | 0x460u) >= 0 )
return 0LL;
perror("sendCmd");
return 0xFFFFFFFFLL;
}

original_code:
int ltc6804_startGpioVoltageConversion(u_int8_t address,
        enum ltc6804_ADC_modes_e mode,
        enum ltc6804_CHG_e chans) {
    dummyIo(spiFd, 1);
    int s = sendCmd(spiFd, address, CMD_ADAX | mode | chans);
    if (s < 0) {
        perror("sendCmd");
        return -1;
    }
    return 0;
}

canonical_original_code:
int ltc6804_startGpioVoltageConversion(unsigned int address, enum ltc6804_ADC_modes_e mode, enum ltc6804_CHG_e chans) {
    dummyIo(spiFd, 1);
    int s = sendCmd(spiFd, address, CMD_ADAX | mode | chans);
    if (s < 0) {
        perror("sendCmd");
        return -1;
    }
    return 0;
}

variable_types: enum ltc6804_CHG_e { CHG_GPIO_ALL_gc=0, CHG_GPIO_1_gc=1, CHG_GPIO_2_gc=2, CHG_GPIO_3_gc=3, CHG_GPIO_4_gc=4, CHG_GPIO_5_gc=5, CHG_GPIO_REF_2_gc=6 }

user_defined_types: enum ltc6804_ADC_modes_e { ADC_MD_FAST_gc=expr, ADC_MD_NORM_gc=expr, ADC_MD_FILTERED_gc=expr }


decompiled_code:
__int64 __fastcall ltc6804_startStatVoltageConversion(unsigned __int8 a1, int a2, int a3)
{
dummyIo(spiFd, 1u);
if ( (int)sendCmd(spiFd, a1, a3 | a2 | 0x468u) >= 0 )
return 0LL;
perror("sendCmd");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func15(unsigned __int8 a1, int a2, int a3)
{
func0(spiFd, 1u);
if ( (int)func3(spiFd, a1, a3 | a2 | 0x468u) >= 0 )
return 0LL;
perror("sendCmd");
return 0xFFFFFFFFLL;
}

original_code:
int ltc6804_startStatVoltageConversion(u_int8_t address,
        enum ltc6804_ADC_modes_e mode,
        enum ltc6804_CHST_e chans) {
    dummyIo(spiFd, 1);
    int s = sendCmd(spiFd, address, CMD_ADSTAT | mode | chans);
    if (s < 0) {
        perror("sendCmd");
        return -1;
    }
    return 0;
}

canonical_original_code:
int ltc6804_startStatVoltageConversion(unsigned int address, enum ltc6804_ADC_modes_e mode, enum ltc6804_CHST_e chans) {
    dummyIo(spiFd, 1);
    int s = sendCmd(spiFd, address, CMD_ADSTAT | mode | chans);
    if (s < 0) {
        perror("sendCmd");
        return -1;
    }
    return 0;
}

variable_types: enum ltc6804_CHST_e { CHST_ALL_gc=0, CHST_SOC_gc=1, CHST_ITMP_gc=2, CHST_VA_gc=3, CHST_VD_gc=4 }


decompiled_code:
__int64 __fastcall ltc6804_startMuxDiag(unsigned __int8 a1)
{
dummyIo(spiFd, 1u);
if ( (int)sendCmd(spiFd, a1, 0x715u) >= 0 )
return 0LL;
perror("sendCmd");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func16(unsigned __int8 a1)
{
func0(spiFd, 1u);
if ( (int)func3(spiFd, a1, 0x715u) >= 0 )
return 0LL;
perror("sendCmd");
return 0xFFFFFFFFLL;
}

original_code:
int ltc6804_startMuxDiag(u_int8_t address) {
    dummyIo(spiFd, 1);
    int s = sendCmd(spiFd, address, CMD_DIAGN);
    if (s < 0) {
        perror("sendCmd");
        return -1;
    }
    return 0;
}

canonical_original_code:
int ltc6804_startMuxDiag(unsigned int address) {
    dummyIo(spiFd, 1);
    int s = sendCmd(spiFd, address, CMD_DIAGN);
    if (s < 0) {
        perror("sendCmd");
        return -1;
    }
    return 0;
}

variable_types: s int


decompiled_code:
__int64 __fastcall ltc6804_init(unsigned __int8 a1, __int64 a2, unsigned int a3)
{
__int64 result; // rax
__int64 v5[2]; // [rsp+0h] [rbp-38h] BYREF
int v6; // [rsp+10h] [rbp-28h]

if ( spiFd || (ltc6804util_initPec(), (int)spiInit(&spiFd, a2, a3) >= 0) )
{
dummyIo(spiFd, 1u);
usleep(0x1F4u);
ltc6804_startMuxDiag(a1);
usleep(0x1388u);
v5[0] = 0xA01075200000101LL;
v5[1] = 0LL;
v6 = 0;
ltc6804_writeConfig(a1, (__int64)v5);
result = 1LL;
}
else
{
perror("spiInit");
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func17(unsigned __int8 a1, __int64 a2, unsigned int a3)
{
__int64 result; // rax
__int64 v5[2]; // [rsp+0h] [rbp-38h] BYREF
int v6; // [rsp+10h] [rbp-28h]

if ( spiFd || (ltc6804util_initPec(), (int)spiInit(&spiFd, a2, a3) >= 0) )
{
func0(spiFd, 1u);
usleep(0x1F4u);
func16(a1);
usleep(0x1388u);
v5[0] = 0xA01075200000101LL;
v5[1] = 0LL;
v6 = 0;
func8(a1, (__int64)v5);
result = 1LL;
}
else
{
perror("spiInit");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int ltc6804_init(u_int8_t address, const char *SPI_interface, u_int32_t SPI_speed) {
    if (spiFd == 0) {
        ltc6804util_initPec();
        int stat = spiInit(&spiFd, SPI_interface, SPI_speed);
        if (stat < 0) {
            perror("spiInit");
            return -1;
        }
    }
    dummyIo(spiFd, 1);
    usleep(500);
    ltc6804_startMuxDiag(address);
    usleep(5000);
    struct ltc6804_cfg_reg_s cfg = {
        .gpio = 0x01,
        .refon = 1,
        .swtrd = 0,
        .adcopt = 0,
        .vuv = 1874,
        .vov = 2561,
        .dcc = 0,
        .dcto = DCTO_DISABLED_gc,
    };
    ltc6804_writeConfig(address, &cfg);
    return 1;
}

canonical_original_code:
int ltc6804_init(unsigned int address, char *SPI_interface, unsigned int SPI_speed) {
    if (spiFd == 0) {
        ltc6804util_initPec();
        int stat = spiInit(&spiFd, SPI_interface, SPI_speed);
        if (stat < 0) {
            perror("spiInit");
            return -1;
        }
    }
    dummyIo(spiFd, 1);
    usleep(500);
    ltc6804_startMuxDiag(address);
    usleep(5000);
    struct ltc6804_cfg_reg_s cfg = {
        .gpio = 0x01,
        .refon = 1,
        .swtrd = 0,
        .adcopt = 0,
        .vuv = 1874,
        .vov = 2561,
        .dcc = 0,
        .dcto = DCTO_DISABLED_gc,
    };
    ltc6804_writeConfig(address, &cfg);
    return 1;
}

variable_types: stat int

variable_types: struct ltc6804_cfg_reg_s { unsigned int gpio; unsigned int refon; unsigned int swtrd; unsigned int adcopt; unsigned int vuv; unsigned int vov; unsigned int dcc; enum ltc6804_DCTO_e { DCTO_DISABLED_gc=0, DCTO_30_SEC_gc=1, DCTO_1_MIN_gc=2, DCTO_2_MIN_gc=3, DCTO_3_MIN_gc=4, DCTO_4_MIN_gc=5, DCTO_5_MIN_gc=6, DCTO_10_MIN_gc=7, DCTO_15_MIN_gc=8, DCTO_20_MIN_gc=9, DCTO_30_MIN_gc=10, DCTO_40_MIN_gc=11, DCTO_60_MIN_gc=12, DCTO_75_MIN_gc=13, DCTO_90_MIN_gc=14, DCTO_120_MIN_gc=15 } dcto; enum ltc6804_PEC_e { PEC_ERROR=0, PEC_OK=1 } pecOk; }


decompiled_code:
__int64 __fastcall ltc6804_startCellGpioVoltageConversion(unsigned __int8 a1, int a2)
{
dummyIo(spiFd, 1u);
if ( (int)sendCmd(spiFd, a1, a2 | 0x46Fu) >= 0 )
return 0LL;
perror("sendCmd");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func18(unsigned __int8 a1, int a2)
{
func0(spiFd, 1u);
if ( (int)func3(spiFd, a1, a2 | 0x46Fu) >= 0 )
return 0LL;
perror("sendCmd");
return 0xFFFFFFFFLL;
}

original_code:
int ltc6804_startCellGpioVoltageConversion(u_int8_t address,
        enum ltc6804_ADC_modes_e mode) {
    dummyIo(spiFd, 1);
    int s = sendCmd(spiFd, address, CMD_ADCVAX | mode);
    if (s < 0) {
        perror("sendCmd");
        return -1;
    }
    return 0;
}

canonical_original_code:
int ltc6804_startCellGpioVoltageConversion(unsigned int address, enum ltc6804_ADC_modes_e mode) {
    dummyIo(spiFd, 1);
    int s = sendCmd(spiFd, address, CMD_ADCVAX | mode);
    if (s < 0) {
        perror("sendCmd");
        return -1;
    }
    return 0;
}


decompiled_code:
double __fastcall lpf_s(double a1, double a2, double a3, double a4)
{
return a1 / (a2 + a1) * (a3 - a4) + a4;
}

canonical_decompiled_code:
double __fastcall func0(double a1, double a2, double a3, double a4)
{
return a1 / (a2 + a1) * (a3 - a4) + a4;
}

original_code:
static double lpf_s(double dt, double tau, double in, double prev) {
    double alpha = dt / (tau + dt);
    return prev + alpha * (in - prev);
}

variable_types: dt double

variable_types: tau double

variable_types: in double

variable_types: prev double

variable_types: alpha double

return_type: double


decompiled_code:
double __fastcall v_s(double a1, double a2)
{
return a1 * a2;
}

canonical_decompiled_code:
double __fastcall func1(double a1, double a2)
{
return a1 * a2;
}

original_code:
static double v_s(double rs, double icell) {
    return rs * icell;
}

variable_types: rs double

variable_types: icell double


decompiled_code:
__m128d __fastcall v_m(__m128d result, double a2)
{
if ( a2 <= 0.0 )
result = _mm_xor_pd(result, (__m128d)LC1);
return result;
}

canonical_decompiled_code:
__m128d __fastcall func3(__m128d result, double a2)
{
if ( a2 <= 0.0 )
result = _mm_xor_pd(result, (__m128d)LC1);
return result;
}

original_code:
static double v_m(double vm, double icell) {
    return (icell > 0) ? vm : vm * -1;
}

variable_types: vm double


variable_types: voc double

variable_types:
tbl
unsigned int []

variable_types:
n
unsigned int

variable_types:
index
unsigned int

variable_types: delta double


decompiled_code:
double __fastcall cell_integrate_i(double *a1, double a2)
{
double v2; // xmm1_8
double result; // xmm0_8

v2 = a2 * a1[3];
if ( v2 < 0.0 )
v2 = v2 * a1[4];
result = (a1[10] * *a1 - v2 / 3600.0) / a1[10];
if ( result > 1.0 )
return *(double *)&LC2;
if ( result < 0.0 )
result = 0.0;
return result;
}

canonical_decompiled_code:
double __fastcall func5(double *a1, double a2)
{
double v2; // xmm1_8
double result; // xmm0_8

v2 = a2 * a1[3];
if ( v2 < 0.0 )
v2 = v2 * a1[4];
result = (a1[10] * *a1 - v2 / 3600.0) / a1[10];
if ( result > 1.0 )
return *(double *)&LC2;
if ( result < 0.0 )
result = 0.0;
return result;
}

original_code:
double cell_integrate_i(struct batt_cell_s *c, double dt) {
    double delta_c = c->i * dt;
    if (delta_c < 0.0) {
        delta_c *= c->eta_c;
    }
    const double new_soc = (c->capacity * c->soc - delta_c / 3600) / c->capacity;
    if (new_soc > 1.0) {
        return 1.0;
    }
    if (new_soc < 0.0) {
        return 0.0;
    }
    return new_soc;
}

canonical_original_code:
double cell_integrate_i(struct batt_cell_s *c, double dt) {
    double delta_c = c->i * dt;
    if (delta_c < 0.0) {
        delta_c *= c->eta_c;
    }
    double new_soc = (c->capacity * c->soc - delta_c / 3600) / c->capacity;
    if (new_soc > 1.0) {
        return 1.0;
    }
    if (new_soc < 0.0) {
        return 0.0;
    }
    return new_soc;
}

variable_types: struct batt_cell_s { double soc; double voc; double termv; double i; double eta_c; double rs; double r1; double tau1; double vm; double taum; double capacity; enum ltc6804_cell_flags_e { CF_UNDERVOLTAGE=1, CF_OVERVOLTAGE=2 } flags; double lpf_vr; double lpf_vh; } *

user_defined_types: struct batt_cell_s { double soc; double voc; double termv; double i; double eta_c; double rs; double r1; double tau1; double vm; double taum; double capacity; enum ltc6804_cell_flags_e flags; double lpf_vr; double lpf_vh; }


original_code:
void cell_compute_voc(struct batt_cell_s *c, double dt) {
    const double vs = v_s(c->rs, c->i);
    const double vr_inst = v_r(c->r1, c->i);
    const double vm_inst = v_m(c->vm, c->i);
    c->lpf_vr = lpf_s(dt, c->tau1, vr_inst, c->lpf_vr);
    c->lpf_vh = lpf_s(dt, c->taum, vm_inst, c->lpf_vh);
    c->voc = c->termv + vs + c->lpf_vr + c->lpf_vh;
}

canonical_original_code:
void cell_compute_voc(struct batt_cell_s *c, double dt) {
    double vs = v_s(c->rs, c->i);
    double vr_inst = v_r(c->r1, c->i);
    double vm_inst = v_m(c->vm, c->i);
    c->lpf_vr = lpf_s(dt, c->tau1, vr_inst, c->lpf_vr);
    c->lpf_vh = lpf_s(dt, c->taum, vm_inst, c->lpf_vh);
    c->voc = c->termv + vs + c->lpf_vr + c->lpf_vh;
}

variable_types: vs double

user_defined_types: enum ltc6804_cell_flags_e { CF_UNDERVOLTAGE=1, CF_OVERVOLTAGE=2 }


spiInit

decompiled_code:
__int64 __fastcall spiInit(int *a1, const char *a2, int a3)
{
int v3; // eax
__int64 result; // rax
int v5[4]; // [rsp+Ch] [rbp-1Ch] BYREF
char v6; // [rsp+1Dh] [rbp-Bh] BYREF
char v7; // [rsp+1Eh] [rbp-Ah] BYREF
char v8[9]; // [rsp+1Fh] [rbp-9h] BYREF

v5[0] = a3;
v3 = open(a2, 2);
*a1 = v3;
if ( v3 )
{
v8[0] = 3;
v7 = 0;
v6 = 0;
if ( ioctl(v3, 0x40016B01uLL, v8) < 0 )
{
perror("SPI_IOC_WR_MODE");
result = 0xFFFFFFFFLL;
}
else if ( ioctl(*a1, 0x40016B02uLL, &v7) < 0 )
{
perror("SPI_IOC_WR_LSB_FIRST");
result = 0xFFFFFFFFLL;
}
else if ( ioctl(*a1, 0x40016B03uLL, &v6) < 0 )
{
perror("SPI_IOC_WR_BITS_PER_WORD");
result = 0xFFFFFFFFLL;
}
else if ( ioctl(*a1, 0x40046B04uLL, v5) < 0 )
{
perror("SPI_IOC_WR_MAX_SPEED_HZ");
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
else
{
perror("open");
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int *a1, const char *a2, int a3)
{
int v3; // eax
__int64 result; // rax
int v5[4]; // [rsp+Ch] [rbp-1Ch] BYREF
char v6; // [rsp+1Dh] [rbp-Bh] BYREF
char v7; // [rsp+1Eh] [rbp-Ah] BYREF
char v8[9]; // [rsp+1Fh] [rbp-9h] BYREF

v5[0] = a3;
v3 = open(a2, 2);
*a1 = v3;
if ( v3 )
{
v8[0] = 3;
v7 = 0;
v6 = 0;
if ( ioctl(v3, 0x40016B01uLL, v8) < 0 )
{
perror("SPI_IOC_WR_MODE");
result = 0xFFFFFFFFLL;
}
else if ( ioctl(*a1, 0x40016B02uLL, &v7) < 0 )
{
perror("SPI_IOC_WR_LSB_FIRST");
result = 0xFFFFFFFFLL;
}
else if ( ioctl(*a1, 0x40016B03uLL, &v6) < 0 )
{
perror("SPI_IOC_WR_BITS_PER_WORD");
result = 0xFFFFFFFFLL;
}
else if ( ioctl(*a1, 0x40046B04uLL, v5) < 0 )
{
perror("SPI_IOC_WR_MAX_SPEED_HZ");
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
else
{
perror("open");
result = 0xFFFFFFFFLL;
}
return result;
}

variable_types:
fd
int *

variable_types:
interface
char *

variable_types:
speed
unsigned int

variable_types:
mode
unsigned int

variable_types:
byteOrder
unsigned int

variable_types:
bitsPerWord
unsigned int


spiIo

decompiled_code:
__int64 __fastcall spiIo(int a1, __int64 a2, __int64 a3, unsigned int a4)
{
int v4; // ebx
__int64 v6[5]; // [rsp+0h] [rbp-28h] BYREF

if ( a1 )
{
v6[3] = 16777226LL;
v6[0] = a2;
v6[1] = a3;
v6[2] = a4;
v4 = ioctl(a1, 0x40206B00uLL, v6);
if ( v4 < 0 )
perror("ioctl, SPI_IOC_MESSAGE(1)");
}
else
{
perror("spiIo");
v4 = -1;
}
return (unsigned int)v4;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, __int64 a2, __int64 a3, unsigned int a4)
{
int v4; // ebx
__int64 v6[5]; // [rsp+0h] [rbp-28h] BYREF

if ( a1 )
{
v6[3] = 16777226LL;
v6[0] = a2;
v6[1] = a3;
v6[2] = a4;
v4 = ioctl(a1, 0x40206B00uLL, v6);
if ( v4 < 0 )
perror("ioctl, SPI_IOC_MESSAGE(1)");
}
else
{
perror("spiIo");
v4 = -1;
}
return (unsigned int)v4;
}

original_code:
int spiIo(int fd, u_int8_t *tx, u_int8_t *rx, unsigned int bytes) {
    if (fd == 0) {
        perror("spiIo");
        return -1;
    }
    struct spi_ioc_transfer xfer = {
        .tx_buf = (intptr_t)tx,
        .rx_buf = (intptr_t)rx,
        .len = (__u32)bytes,
        .speed_hz = 0,
        .delay_usecs = 10,
        .bits_per_word = 0,
        .cs_change = 1,
    };
    int status = ioctl(fd, (((1U) << (((0 +8)+8)+14)) | ((('k')) << (0 +8)) | (((0)) << 0) | ((((sizeof(char[((((1)*(sizeof (struct spi_ioc_transfer))) < (1 << 14)) ? ((1)*(sizeof (struct spi_ioc_transfer))) : 0)])))) << ((0 +8)+8))), &xfer);
    if (status < 0) {
        perror("ioctl, SPI_IOC_MESSAGE(1)");
    }
    return status;
}

canonical_original_code:
int spiIo(int fd, unsigned int *tx, unsigned int *rx, unsigned int bytes) {
    if (fd == 0) {
        perror("spiIo");
        return -1;
    }
    struct spi_ioc_transfer xfer = {
        .tx_buf = (intptr_t)tx,
        .rx_buf = (intptr_t)rx,
        .len = (__u32)bytes,
        .speed_hz = 0,
        .delay_usecs = 10,
        .bits_per_word = 0,
        .cs_change = 1,
    };
    int status = ioctl(fd, (((1U) << (((0 +8)+8)+14)) | ((('k')) << (0 +8)) | (((0)) << 0) | ((((sizeof(char[((((1)*(sizeof (struct spi_ioc_transfer))) < (1 << 14)) ? ((1)*(sizeof (struct spi_ioc_transfer))) : 0)])))) << ((0 +8)+8))), &xfer);
    if (status < 0) {
        perror("ioctl, SPI_IOC_MESSAGE(1)");
    }
    return status;
}

variable_types: fd int

variable_types:
tx
unsigned int *

variable_types:
rx
unsigned int *

variable_types:
bytes
unsigned int

variable_types: struct spi_ioc_transfer { unsigned long long tx_buf; unsigned long long rx_buf; unsigned int len; unsigned int speed_hz; unsigned short delay_usecs; unsigned char bits_per_word; unsigned char cs_change; unsigned char tx_nbits; unsigned char rx_nbits; unsigned short pad; }

variable_types: status int


spiClose

decompiled_code:
int __fastcall spiClose(int *a1)
{
int v1; // edi
int result; // eax

v1 = *a1;
if ( v1 )
result = close(v1);
return result;
}

canonical_decompiled_code:
int __fastcall func2(int *a1)
{
int v1; // edi
int result; // eax

v1 = *a1;
if ( v1 )
result = close(v1);
return result;
}

original_code:
void spiClose(int *fd) {
    if (*fd != 0) {
        close(*fd);
    }
}


repo: martinamaggio/gtrm

decompiled_code:
__int64 __fastcall sched_setscheduler2(unsigned int a1, unsigned int a2, __int64 a3)
{
return syscall(315LL, a1, a2, a3);
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1, unsigned int a2, __int64 a3)
{
return syscall(315LL, a1, a2, a3);
}

original_code:
int sched_setscheduler2(pid_t pid, int policy,
     const struct sched_param2 *param)
{
 return syscall(315, pid, policy, param);
}

canonical_original_code:
int sched_setscheduler2(int pid, int policy, struct sched_param2 *param)
{
 return syscall(315, pid, policy, param);
}

variable_types: pid int

variable_types: policy int

variable_types: struct sched_param2 { int sched_priority; unsigned int sched_flags; unsigned long long sched_runtime; unsigned long long sched_deadline; unsigned long long sched_period; unsigned long long __unused[12]; } *


decompiled_code:
__int64 __fastcall sched_setparam2(unsigned int a1, __int64 a2)
{
return syscall(313LL, a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1, __int64 a2)
{
return syscall(313LL, a1, a2);
}

original_code:
int sched_setparam2(pid_t pid,
        const struct sched_param2 *param)
{
 return syscall(313, pid, param);
}

canonical_original_code:
int sched_setparam2(int pid, struct sched_param2 *param)
{
 return syscall(313, pid, param);
}

user_defined_types: struct sched_param2 { int sched_priority; unsigned int sched_flags; unsigned long long sched_runtime; unsigned long long sched_deadline; unsigned long long sched_period; unsigned long long __unused[12]; }


decompiled_code:
__int64 __fastcall sched_getparam2(unsigned int a1, __int64 a2)
{
return syscall(314LL, a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1, __int64 a2)
{
return syscall(314LL, a1, a2);
}

original_code:
int sched_getparam2(pid_t pid, struct sched_param2 *param)
{
 return syscall(314, pid, param);
}

canonical_original_code:
int sched_getparam2(int pid, struct sched_param2 *param)
{
 return syscall(314, pid, param);
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
float v3; // xmm7_4
float v4; // xmm2_4
float v5; // xmm6_4
float v6; // xmm7_4
float v7; // xmm2_4
double v8; // r12
double v9; // rbx
unsigned int *v10; // rbp
int v11; // er13
__int64 v12; // r14
__int64 v13; // r15
__syscall_slong_t v14; // r12
FILE *v15; // rax
__syscall_slong_t v16; // rdx
FILE *v17; // r12
unsigned int v18; // ebx
float v19; // xmm5_4
float v21; // [rsp+Ch] [rbp-8Ch]
double v22; // [rsp+10h] [rbp-88h]
float v23; // [rsp+18h] [rbp-80h]
double v24; // [rsp+28h] [rbp-70h]
char s[10]; // [rsp+36h] [rbp-62h] BYREF
struct timespec tp; // [rsp+40h] [rbp-58h] BYREF
__int64 v27[8]; // [rsp+58h] [rbp-40h] BYREF

if ( argc != 9 )
{
fwrite("[application] usage:\n", 1uLL, 0x15uLL, stderr);
fwrite("<a> sl a_cpu b_cpu a_mem b_mem eps weight deadline\n", 1uLL, 0x33uLL, stderr);
exit(-1);
}
v3 = atof(argv[1]);
v21 = v3;
v4 = atof(argv[2]);
v23 = v4;
v5 = atof(argv[3]);
v6 = atof(argv[4]);
v7 = atof(argv[5]);
v24 = atof(argv[6]);
v8 = atof(argv[7]);
v9 = atof(argv[8]);
v10 = (unsigned int *)jobsignaler_registration();
v27[0] = (unsigned int)(int)(v9 * 1000000000.0);
jobsignaler_set(v10, 1LL, v27);
set_weight(*v10, v8);
v11 = 100;
do
{
v18 = jobsignaler_signalstart(v10, 0LL);
v22 = get_performance_number(v10, 0LL);
if ( v22 != 0.0 )
{
v19 = v22 * v21 * v24 + v21;
v21 = v19;
if ( v19 < 0.0001 )
v21 = *(float *)&LC3;
}
v12 = (unsigned int)(int)(float)((float)(v21 * v23) + v5);
v13 = (unsigned int)(int)(float)((float)(v21 * v6) + v7);
clock_gettime(0, &tp);
v14 = tp.tv_nsec + 1000000000 * tp.tv_sec;
sprintf(s, "%u.log", *v10);
v15 = fopen(s, "a+");
v16 = v14;
v17 = v15;
fprintf(v15, "%lld, %f, %f, %lld, %lld, %u\n", v16, v22, v21, v12, v13, v18);
fclose(v17);
do_work(v12, v13, 0.0);
jobsignaler_signalend(v10, v18);
--v11;
}
while ( v11 );
jobsignaler_terminate(v10);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
float v3; // xmm7_4
float v4; // xmm2_4
float v5; // xmm6_4
float v6; // xmm7_4
float v7; // xmm2_4
double v8; // r12
double v9; // rbx
unsigned int *v10; // rbp
int v11; // er13
__int64 v12; // r14
__int64 v13; // r15
__syscall_slong_t v14; // r12
FILE *v15; // rax
__syscall_slong_t v16; // rdx
FILE *v17; // r12
unsigned int v18; // ebx
float v19; // xmm5_4
float v21; // [rsp+Ch] [rbp-8Ch]
double v22; // [rsp+10h] [rbp-88h]
float v23; // [rsp+18h] [rbp-80h]
double v24; // [rsp+28h] [rbp-70h]
char s[10]; // [rsp+36h] [rbp-62h] BYREF
struct timespec tp; // [rsp+40h] [rbp-58h] BYREF
__int64 v27[8]; // [rsp+58h] [rbp-40h] BYREF

if ( argc != 9 )
{
fwrite("[application] usage:\n", 1uLL, 0x15uLL, stderr);
fwrite("<a> sl a_cpu b_cpu a_mem b_mem eps weight deadline\n", 1uLL, 0x33uLL, stderr);
exit(-1);
}
v3 = atof(argv[1]);
v21 = v3;
v4 = atof(argv[2]);
v23 = v4;
v5 = atof(argv[3]);
v6 = atof(argv[4]);
v7 = atof(argv[5]);
v24 = atof(argv[6]);
v8 = atof(argv[7]);
v9 = atof(argv[8]);
v10 = (unsigned int *)jobsignaler_registration();
v27[0] = (unsigned int)(int)(v9 * 1000000000.0);
jobsignaler_set(v10, 1LL, v27);
set_weight(*v10, v8);
v11 = 100;
do
{
v18 = jobsignaler_signalstart(v10, 0LL);
v22 = get_performance_number(v10, 0LL);
if ( v22 != 0.0 )
{
v19 = v22 * v21 * v24 + v21;
v21 = v19;
if ( v19 < 0.0001 )
v21 = *(float *)&LC3;
}
v12 = (unsigned int)(int)(float)((float)(v21 * v23) + v5);
v13 = (unsigned int)(int)(float)((float)(v21 * v6) + v7);
clock_gettime(0, &tp);
v14 = tp.tv_nsec + 1000000000 * tp.tv_sec;
sprintf(s, "%u.log", *v10);
v15 = fopen(s, "a+");
v16 = v14;
v17 = v15;
fprintf(v15, "%lld, %f, %f, %lld, %lld, %u\n", v16, v22, v21, v12, v13, v18);
fclose(v17);
func1(v12, v13, 0.0);
jobsignaler_signalend(v10, v18);
--v11;
}
while ( v11 );
jobsignaler_terminate(v10);
return 0;
}

original_code:
int main(int argc, char* argv[]) {
  if (argc != 3) {
    exit(-1);
  }
  double c1 = atof(argv[1]);
  double c2 = atof(argv[2]);
  gtrm_init();
  _rm_application_h apps[100];
  int i = 0;
  int prev_num_apps = 0;
  while(1) {
    int num_apps = update_applications(apps);
    if (num_apps > 0) {
      compute_virtual_platforms(apps, num_apps, i, num_apps != prev_num_apps, c1, c2);
      rescale_virtual_platforms(apps, num_apps);
      rmset_performance_multiplier(apps, num_apps);
      write_log(apps, num_apps);
      apply_virtual_platforms(apps, num_apps);
      prev_num_apps = num_apps;
      ++i;
    }
  }
  ({ fprintf(stderr, "Fatal: "); fprintf(stderr, "unreachable"); fprintf(stderr, "\n"); exit(1); });;
}

canonical_original_code:
int main(int argc, char *argv[]) {
  if (argc != 3) {
    exit(-1);
  }
  double c1 = atof(argv[1]);
  double c2 = atof(argv[2]);
  gtrm_init();
  struct _rm_application_h apps[100];
  int i = 0;
  int prev_num_apps = 0;
  while(1) {
    int num_apps = update_applications(apps);
    if (num_apps > 0) {
      compute_virtual_platforms(apps, num_apps, i, num_apps != prev_num_apps, c1, c2);
      rescale_virtual_platforms(apps, num_apps);
      rmset_performance_multiplier(apps, num_apps);
      write_log(apps, num_apps);
      apply_virtual_platforms(apps, num_apps);
      prev_num_apps = num_apps;
      ++i;
    }
  }
  ({ fprintf(stderr, "Fatal: "); fprintf(stderr, "unreachable"); fprintf(stderr, "\n"); exit(1); });;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct _rm_application_h { int tid; float vp; float vp_old; float performance; float weight; } [100]

variable_types: i int


decompiled_code:
int __fastcall cg_destroy(const char *a1)
{
int result; // eax
char v2[4104]; // [rsp+0h] [rbp-1008h] BYREF

if ( !a1 )
_assert_fail("group != NULL", "src/cg.c", 0x36u, "cg_destroy");
cg_detach_all(a1);
snprintf(v2, 0x1000uLL, "/sys/fs/cgroup/%s", a1);
result = rmdir(v2);
if ( result )
_assert_fail("retval == 0", "src/cg.c", 0x3Eu, "cg_destroy");
return result;
}

canonical_decompiled_code:
int __fastcall func5(const char *a1)
{
int result; // eax
char v2[4104]; // [rsp+0h] [rbp-1008h] BYREF

if ( !a1 )
_assert_fail("group != NULL", "src/cg.c", 0x36u, "cg_destroy");
func4(a1);
snprintf(v2, 0x1000uLL, "/sys/fs/cgroup/%s", a1);
result = rmdir(v2);
if ( result )
_assert_fail("retval == 0", "src/cg.c", 0x3Eu, "cg_destroy");
return result;
}

original_code:
void cg_destroy(const char *group)
{
	assert(group != NULL);

	cg_detach_all(group);

	char cg[PATH_SIZE_MAX];
	snprintf(cg, PATH_SIZE_MAX, MOUNT_POINT "/%s", group);

	int retval = rmdir(cg);
	assert(retval == 0);
}

canonical_original_code:
void cg_destroy(char *group)
{
	assert(group != NULL);

	cg_detach_all(group);

	char cg[-1];
	snprintf(cg, PATH_SIZE_MAX, MOUNT_POINT "/%s", group);

	int retval = rmdir(cg);
	assert(retval == 0);
}

variable_types:
group
char *

variable_types:
cg
char [-1]

variable_types: retval int


repo: martinliptak/kernel-from-scratch

decompiled_code:
void kthread_exit()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
void kthread_exit()
{
}

canonical_original_code:
void kthread_exit()
{
}


original_code:
process_t *kthread_start(void (*func)())
{
 ulong thr_pgdir_page, thr_stack_page;
 ulong *thr_pgdir, *thr_stack, *thr_stack_top;
 process_t *thr;
 process_queue_t *qthr;
 thr_pgdir_page = phys_alloc(PA_KERNEL);
 thr_stack_page = phys_alloc(PA_KERNEL);
 thr_pgdir = kaddr_alloc(1);
 thr_stack = kaddr_alloc(1);
 page_map_4k(pagedir, (ulong)thr_pgdir, thr_pgdir_page, PAGE_PRESENT | PAGE_WRITE);
 page_map_4k(pagedir, (ulong)thr_stack, thr_stack_page, PAGE_PRESENT | PAGE_WRITE);
 thr_stack_top = thr_stack + 1024;
 *--thr_stack_top = (ulong)kthread_exit;
 *--thr_stack_top = 0x246;
 *--thr_stack_top = 0x8;
 *--thr_stack_top = (ulong)func;
 *--thr_stack_top = 0;
 *--thr_stack_top = 0;
 *--thr_stack_top = 0;
 *--thr_stack_top = 0;
 --thr_stack_top;
        *thr_stack_top = (ulong)(thr_stack_top + 5);
 *--thr_stack_top = 0;
 *--thr_stack_top = 0;
 *--thr_stack_top = 0;
 memcpy(thr_pgdir, pagedir, 4096);
 thr_pgdir[1022] = thr_pgdir_page | PAGE_WRITE | PAGE_PRESENT;
 page_map_4k(thr_pgdir, 0xc0000000 - 4096, thr_stack_page,
   PAGE_PRESENT | PAGE_WRITE);
 thr = malloc(sizeof(process_t));
 memset(thr, 0, sizeof(thr));
 thr->esp = (ulong)0xc0000000 - ((ulong)thr_stack + 4096
  - (ulong)thr_stack_top);
 thr->ss = 0x10;
 thr->cr3 = thr_pgdir_page;
 qthr = malloc(sizeof(process_queue_t));
 qthr->proc = thr;
 lock();
 qthr->next = run_queue->next;
 run_queue->next = qthr;
 unlock();
}

canonical_original_code:
struct process *kthread_start(void (*func)())
{
 unsigned long thr_pgdir_page; unsigned long thr_stack_page;
 unsigned long *thr_pgdir; unsigned long *thr_stack; unsigned long *thr_stack_top;
 struct process *thr;
 struct process_queue *qthr;
 thr_pgdir_page = phys_alloc(PA_KERNEL);
 thr_stack_page = phys_alloc(PA_KERNEL);
 thr_pgdir = kaddr_alloc(1);
 thr_stack = kaddr_alloc(1);
 page_map_4k(pagedir, (unsigned long)thr_pgdir, thr_pgdir_page, PAGE_PRESENT | PAGE_WRITE);
 page_map_4k(pagedir, (unsigned long)thr_stack, thr_stack_page, PAGE_PRESENT | PAGE_WRITE);
 thr_stack_top = thr_stack + 1024;
 *--thr_stack_top = (unsigned long)kthread_exit;
 *--thr_stack_top = 0x246;
 *--thr_stack_top = 0x8;
 *--thr_stack_top = (unsigned long)func;
 *--thr_stack_top = 0;
 *--thr_stack_top = 0;
 *--thr_stack_top = 0;
 *--thr_stack_top = 0;
 --thr_stack_top;
        *thr_stack_top = (ulong)(thr_stack_top + 5);
 *--thr_stack_top = 0;
 *--thr_stack_top = 0;
 *--thr_stack_top = 0;
 memcpy(thr_pgdir, pagedir, 4096);
 thr_pgdir[1022] = thr_pgdir_page | PAGE_WRITE | PAGE_PRESENT;
 page_map_4k(thr_pgdir, 0xc0000000 - 4096, thr_stack_page,
   PAGE_PRESENT | PAGE_WRITE);
 thr = malloc(sizeof(process_t));
 memset(thr, 0, sizeof(thr));
 thr->esp = (unsigned long)0xc0000000 - ((unsigned long)thr_stack + 4096
  - (unsigned long)thr_stack_top);
 thr->ss = 0x10;
 thr->cr3 = thr_pgdir_page;
 qthr = malloc(sizeof(process_queue_t));
 qthr->proc = thr;
 lock();
 qthr->next = run_queue->next;
 run_queue->next = qthr;
 unlock();
}

variable_types:
func
void (*)()

variable_types: struct process_queue { struct process { unsigned long esp; unsigned long ss; unsigned long cr3; } *proc; struct process_queue *next; } *

user_defined_types: struct process_queue { struct process *proc; struct process_queue *next; }


strcmp

decompiled_code:
__int64 __fastcall strcmp(char *a1, _BYTE *a2)
{
char i; // al

for ( i = *a1; *a1; i = *a1 )
{
if ( *a2 != i )
break;
if ( !*a2 )
break;
++a1;
++a2;
}
return (unsigned int)(i - (char)*a2);
}

canonical_decompiled_code:
__int64 __fastcall func2(char *a1, _BYTE *a2)
{
char i; // al

for ( i = *a1; *a1; i = *a1 )
{
if ( *a2 != i )
break;
if ( !*a2 )
break;
++a1;
++a2;
}
return (unsigned int)(i - (char)*a2);
}

original_code:
int strcmp(const char *s1, const char *s2)
{
 while (*s1 && *s2 && (*s1 == *s2))
 {
  ++s1;
  ++s2;
 }
 return *s1 - *s2;
}


strlen

decompiled_code:
__int64 __fastcall strlen(_BYTE *a1)
{
_BYTE *v1; // rax
unsigned int v2; // ecx

if ( !*a1 )
return 0;
v1 = a1 + 1;
do
v2 = (_DWORD)v1++ - (_DWORD)a1;
while ( *(v1 - 1) );
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func3(_BYTE *a1)
{
_BYTE *v1; // rax
unsigned int v2; // ecx

if ( !*a1 )
return 0;
v1 = a1 + 1;
do
v2 = (_DWORD)v1++ - (_DWORD)a1;
while ( *(v1 - 1) );
return v2;
}

original_code:
int strlen(const char *s)
{
 int len = 0;
 while (*s++)
  ++len;
 return len;
}

canonical_original_code:
int strlen(char *s)
{
 int len = 0;
 while (*s++)
  ++len;
 return len;
}

variable_types:
s
char *

variable_types: len int


decompiled_code:
int __fastcall parse_option(char *s2, char *s)
{
int v2; // ebx
char *v3; // rax

if ( !options )
goto LABEL_23;
v2 = strcmp(options, s2);
if ( !v2 )
goto LABEL_5;
if ( !*(&options + 3) )
{
LABEL_23:
LODWORD(v3) = panic("Bad option name '%s'", s2);
return (int)v3;
}
v2 = 1;
LABEL_5:
LODWORD(v3) = (&options)[3 * v2 + 1];
switch ( (_DWORD)v3 )
{
case 1:
if ( !s )
panic("Option '%s' requires a string argument", s2);
LODWORD(v3) = (unsigned int)strcpy((&options)[3 * v2 + 2], s);
break;
case 2:
if ( !s || (LODWORD(v3) = sscanf(s, "%i", (&options)[3 * v2 + 2]), !(_DWORD)v3) )
LODWORD(v3) = panic("Option '%s' requires a signed numurical argument", s2);
break;
case 0:
if ( s )
{
if ( !strcmp(s, "no") || !strcmp(s, "0") )
{
v3 = (&options)[3 * v2 + 2];
*(_DWORD *)v3 = 0;
}
else if ( !strcmp(s, "yes") || !strcmp(s, "1") )
{
v3 = (&options)[3 * v2 + 2];
*(_DWORD *)v3 = 1;
}
else
{
LODWORD(v3) = panic("Option '%s' requires a boolean argument (yes | no, 1 | 0)", s2);
}
}
else
{
v3 = (&options)[3 * v2 + 2];
*(_DWORD *)v3 = 1;
}
break;
}
return (int)v3;
}

canonical_decompiled_code:
int __fastcall func0(char *s2, char *s)
{
int v2; // ebx
char *v3; // rax

if ( !options )
goto LABEL_23;
v2 = strcmp(options, s2);
if ( !v2 )
goto LABEL_5;
if ( !*(&options + 3) )
{
LABEL_23:
LODWORD(v3) = panic("Bad option name '%s'", s2);
return (int)v3;
}
v2 = 1;
LABEL_5:
LODWORD(v3) = (&options)[3 * v2 + 1];
switch ( (_DWORD)v3 )
{
case 1:
if ( !s )
panic("Option '%s' requires a string argument", s2);
LODWORD(v3) = (unsigned int)strcpy((&options)[3 * v2 + 2], s);
break;
case 2:
if ( !s || (LODWORD(v3) = sscanf(s, "%i", (&options)[3 * v2 + 2]), !(_DWORD)v3) )
LODWORD(v3) = panic("Option '%s' requires a signed numurical argument", s2);
break;
case 0:
if ( s )
{
if ( !strcmp(s, "no") || !strcmp(s, "0") )
{
v3 = (&options)[3 * v2 + 2];
*(_DWORD *)v3 = 0;
}
else if ( !strcmp(s, "yes") || !strcmp(s, "1") )
{
v3 = (&options)[3 * v2 + 2];
*(_DWORD *)v3 = 1;
}
else
{
LODWORD(v3) = panic("Option '%s' requires a boolean argument (yes | no, 1 | 0)", s2);
}
}
else
{
v3 = (&options)[3 * v2 + 2];
*(_DWORD *)v3 = 1;
}
break;
}
return (int)v3;
}

original_code:
static void parse_option(const char *opt, const char *arg)
{
 int i;
 int sn;
 for (i = 0; options[i].name; ++i)
 {
  if (strcmp(options[i].name, opt) == 0)
  {
   switch (options[i].type)
   {
   case OPT_TYPE_BOOL:
    if (arg)
    {
     if (strcmp(arg, "no") == 0 || strcmp(arg, "0") == 0)
      *(bool *)options[i].value = 0;
     else if (strcmp(arg, "yes") == 0
       || strcmp(arg, "1") == 0)
      *(bool *)options[i].value = 1;
     else
      panic("Option \'%s\' requires a boolean "
        "argument (yes | no, 1 | 0)",
        opt);
    }
    else
     *(bool *)options[i].value = 1;
    break;
   case OPT_TYPE_STRING:
    if (!arg)
     panic("Option \'%s\' requires a string argument",
       opt);
    strcpy((char *)options[i].value, arg);
    break;
   case OPT_TYPE_SIGNED:
    if (!arg || sscanf(arg, "%i", (int *)options[i].value) == 0)
     panic("Option \'%s\' requires a signed numurical "
       "argument", opt);
    break;
   }
   return;
  }
 }
 panic("Bad option name \'%s\'", opt);
}

canonical_original_code:
static void parse_option(char *opt, char *arg)
{
 int i;
 int sn;
 for (i = 0; options[i].name; ++i)
 {
  if (strcmp(options[i].name, opt) == 0)
  {
   switch (options[i].type)
   {
   case OPT_TYPE_BOOL:
    if (arg)
    {
     if (strcmp(arg, "no") == 0 || strcmp(arg, "0") == 0)
      *(unsigned int *)options[i].value = 0;
     else if (strcmp(arg, "yes") == 0
       || strcmp(arg, "1") == 0)
      *(unsigned int *)options[i].value = 1;
     else
      panic("Option \'%s\' requires a boolean "
        "argument (yes | no, 1 | 0)",
        opt);
    }
    else
     *(unsigned int *)options[i].value = 1;
    break;
   case OPT_TYPE_STRING:
    if (!arg)
     panic("Option \'%s\' requires a string argument",
       opt);
    strcpy((char *)options[i].value, arg);
    break;
   case OPT_TYPE_SIGNED:
    if (!arg || sscanf(arg, "%i", (int *)options[i].value) == 0)
     panic("Option \'%s\' requires a signed numurical "
       "argument", opt);
    break;
   }
   return;
  }
 }
 panic("Bad option name \'%s\'", opt);
}

variable_types:
opt
char *

variable_types:
arg
char *

variable_types: sn int


decompiled_code:
int __fastcall option_found(char *a1)
{
char v1; // dl
__int64 v2; // rax
char *v3; // rsi

v1 = *a1;
if ( *a1 )
{
v2 = 0LL;
while ( v1 != 61 )
{
v1 = a1[++v2];
if ( !v1 )
{
v3 = 0LL;
return parse_option(a1, v3);
}
}
v3 = &a1[v2 + 1];
a1[v2] = 0;
}
else
{
v3 = 0LL;
}
return parse_option(a1, v3);
}

original_code:
static void option_found(char *opt)
{
 int i;
 const char *arg;
 for (i = 0, arg = 0; opt[i]; ++i)
  if (opt[i] == '=')
  {
   arg = opt + i + 1;
   opt[i] = 0;
   break;
  }
 parse_option(opt, arg);
}

canonical_original_code:
static void option_found(char *opt)
{
 int i;
 char *arg;
 for (i = 0, arg = 0; opt[i]; ++i)
  if (opt[i] == '=')
  {
   arg = opt + i + 1;
   opt[i] = 0;
   break;
  }
 parse_option(opt, arg);
}


decompiled_code:
__int64 __fastcall parse_cmdline(const char *a1)
{
const char *v1; // rbx
char **v2; // rbp
char v3; // al
char i; // di
const char *v6; // rax

v1 = a1;
v2 = (char **)dstring_new(16LL);
printk("<1>Command line: %s\n", a1);
v3 = *a1;
if ( (*a1 & 0xDF) != 0 )
{
do
v3 = *++v1;
while ( (*v1 & 0xDF) != 0 );
}
if ( !v3 )
return dstring_free(v2);
for ( ; *v1 == 32; ++v1 )
;
for ( i = *v1; i; i = v6[1] )
{
if ( !isblank(i) || isblank(v1[1]) )
{
dstring_append_char(v2, (unsigned int)*v1);
v6 = v1;
}
else
{
dstring_append_char(v2, 0LL);
option_found(*v2);
dstring_free(v2);
v2 = (char **)dstring_new(16LL);
if ( *v1 == 32 )
{
do
++v1;
while ( *v1 == 32 );
v6 = v1;
}
else
{
v6 = v1;
}
}
v1 = v6 + 1;
}
dstring_append_char(v2, 0LL);
option_found(*v2);
return dstring_free(v2);
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1)
{
const char *v1; // rbx
char **v2; // rbp
char v3; // al
char i; // di
const char *v6; // rax

v1 = a1;
v2 = (char **)dstring_new(16LL);
printk("<1>Command line: %s\n", a1);
v3 = *a1;
if ( (*a1 & 0xDF) != 0 )
{
do
v3 = *++v1;
while ( (*v1 & 0xDF) != 0 );
}
if ( !v3 )
return dstring_free(v2);
for ( ; *v1 == 32; ++v1 )
;
for ( i = *v1; i; i = v6[1] )
{
if ( !isblank(i) || isblank(v1[1]) )
{
dstring_append_char(v2, (unsigned int)*v1);
v6 = v1;
}
else
{
dstring_append_char(v2, 0LL);
func1(*v2);
dstring_free(v2);
v2 = (char **)dstring_new(16LL);
if ( *v1 == 32 )
{
do
++v1;
while ( *v1 == 32 );
v6 = v1;
}
else
{
v6 = v1;
}
}
v1 = v6 + 1;
}
dstring_append_char(v2, 0LL);
func1(*v2);
return dstring_free(v2);
}

original_code:
void parse_cmdline(const char *cmdline)
{
 const char *c;
 dstring_t *opt = dstring_new(16);
 printk("<1>Command line: %s\n", cmdline);
 for (c = cmdline; *c != ' ' && *c != 0; ++c)
  ;
 if (*c == 0)
 {
  dstring_free(opt);
  return;
 }
 while (*c == ' ')
  ++c;
 for (; *c; ++c)
 {
  if (isblank(*c) && !isblank(c[1]))
  {
   dstring_append_char(opt, 0);
   option_found(opt->str);
   dstring_free(opt);
   opt = dstring_new(16);
   while (*c == ' ')
    ++c;
  }
  else
   dstring_append_char(opt, *c);
 }
 dstring_append_char(opt, *c);
 option_found(opt->str);
 dstring_free(opt);
}

canonical_original_code:
void parse_cmdline(char *cmdline)
{
 char *c;
 struct dstring_t *opt = dstring_new(16);
 printk("<1>Command line: %s\n", cmdline);
 for (c = cmdline; *c != ' ' && *c != 0; ++c)
  ;
 if (*c == 0)
 {
  dstring_free(opt);
  return;
 }
 while (*c == ' ')
  ++c;
 for (; *c; ++c)
 {
  if (isblank(*c) && !isblank(c[1]))
  {
   dstring_append_char(opt, 0);
   option_found(opt->str);
   dstring_free(opt);
   opt = dstring_new(16);
   while (*c == ' ')
    ++c;
  }
  else
   dstring_append_char(opt, *c);
 }
 dstring_append_char(opt, *c);
 option_found(opt->str);
 dstring_free(opt);
}

variable_types:
cmdline
char *

variable_types:
c
char *

variable_types: struct dstring_t { char *str; unsigned long len; unsigned long allocated; } *


spaces

decompiled_code:
__int64 __fastcall spaces(__int64 (__fastcall *a1)(__int64, __int64), __int64 a2, int a3)
{
int v3; // ebx
__int64 result; // rax

v3 = a3 - 1;
if ( a3 > 0 )
{
do
{
result = a1(a2, 32LL);
--v3;
}
while ( v3 != -1 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 (__fastcall *a1)(__int64, __int64), __int64 a2, int a3)
{
int v3; // ebx
__int64 result; // rax

v3 = a3 - 1;
if ( a3 > 0 )
{
do
{
result = a1(a2, 32LL);
--v3;
}
while ( v3 != -1 );
}
return result;
}

original_code:
static void spaces(putchr_t putchr, void *putchr_arg, int n)
{
 while (n-- > 0)
  putchr(putchr_arg, ' ');
}

canonical_original_code:
static void spaces(void (*putchr)(void *arg, char c), void *putchr_arg, int n)
{
 while (n-- > 0)
  putchr(putchr_arg, ' ');
}

variable_types:
putchr
void (*)(void *arg, char c)

variable_types: n int


putstr

decompiled_code:
__int64 __fastcall putstr(__int64 (__fastcall *a1)(__int64, _QWORD), __int64 a2, char *a3)
{
char *v4; // rbx
char i; // si
__int64 result; // rax

v4 = a3;
for ( i = *a3; *v4; i = *v4 )
{
++v4;
result = a1(a2, (unsigned int)i);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 (__fastcall *a1)(__int64, _QWORD), __int64 a2, char *a3)
{
char *v4; // rbx
char i; // si
__int64 result; // rax

v4 = a3;
for ( i = *a3; *v4; i = *v4 )
{
++v4;
result = a1(a2, (unsigned int)i);
}
return result;
}

original_code:
static void putstr(putchr_t putchr, void *putchr_arg, const char *str)
{
 while (*str)
  putchr(putchr_arg, *str++);
}

canonical_original_code:
static void putstr(void (*putchr)(void *arg, char c), void *putchr_arg, char *str)
{
 while (*str)
  putchr(putchr_arg, *str++);
}

variable_types:
str
char *


decompiled_code:
__int64 __fastcall kaddr_free(__int64 a1)
{
_QWORD *v1; // rax

lock();
v1 = &kaddr_block_head;
do
{
v1 = (_QWORD *)v1[3];
if ( !v1 )
{
bug("kaddr_free(): Trying to free a block which is not allocated.");
return unlock();
}
}
while ( *v1 != a1 );
if ( *((_DWORD *)v1 + 4) )
*((_DWORD *)v1 + 4) = 0;
else
bug("kaddr_free(): Trying to free a block twice.");
return unlock();
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
_QWORD *v1; // rax

lock();
v1 = &kaddr_block_head;
do
{
v1 = (_QWORD *)v1[3];
if ( !v1 )
{
bug("kaddr_free(): Trying to free a block which is not allocated.");
return unlock();
}
}
while ( *v1 != a1 );
if ( *((_DWORD *)v1 + 4) )
*((_DWORD *)v1 + 4) = 0;
else
bug("kaddr_free(): Trying to free a block twice.");
return unlock();
}

original_code:
void kaddr_free(void *base)
{
 kaddr_block_t *block;
 bool found;
 lock();
 for (found = 0, block = &kaddr_block_head; block->next; block = block->next)
 {
  if (block->next->base == base)
  {
   found = 1;
   break;
  }
 }
 if (found)
 {
  if (block->next->used == 0)
   bug("kaddr_free(): Trying to free a block twice.");
  else
   block->next->used = 0;
 }
 else
  bug("kaddr_free(): Trying to free a block which is not allocated.");
 unlock();
}

canonical_original_code:
void kaddr_free(void *base)
{
 struct _kaddr_block *block;
 unsigned int found;
 lock();
 for (found = 0, block = &kaddr_block_head; block->next; block = block->next)
 {
  if (block->next->base == base)
  {
   found = 1;
   break;
  }
 }
 if (found)
 {
  if (block->next->used == 0)
   bug("kaddr_free(): Trying to free a block twice.");
  else
   block->next->used = 0;
 }
 else
  bug("kaddr_free(): Trying to free a block which is not allocated.");
 unlock();
}

variable_types:
base
void *

variable_types: struct _kaddr_block { void *base; unsigned long pages; unsigned int used; struct _kaddr_block *next; } *

variable_types:
found
unsigned int


decompiled_code:
__int64 sched_init()
{
_QWORD *v0; // rbx
_QWORD *v1; // rax

v0 = malloc(0x10uLL);
run_queue = (__int64)v0;
v1 = malloc(0x18uLL);
*v0 = v1;
*v1 = 0LL;
v0[1] = v0;
current = (__int64)v1;
return pic_unmask(0LL);
}

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

v0 = malloc(0x10uLL);
run_queue = (__int64)v0;
v1 = malloc(0x18uLL);
*v0 = v1;
*v1 = 0LL;
v0[1] = v0;
current = (__int64)v1;
return pic_unmask(0LL);
}

original_code:
void sched_init()
{
 run_queue = malloc(sizeof(process_queue_t));
 run_queue[0].proc = malloc(sizeof(process_t));
 memset(run_queue[0].proc, 0, sizeof(run_queue[0].proc));
 run_queue[0].next = run_queue;
 current = run_queue->proc;
 pic_unmask(0);
}


original_code:
static inline void merge_free_blocks(malloc_block_t *blk)
{
 while (blk->used == 0 && blk->next && blk->next->used == 0)
 {
  if (blk->magic != 0x7ABCDEF7)
  {
   panic("Heap corrupted");
   return;
  }
  blk->size += blk->next->size + sizeof(malloc_block_t);
  blk->next = blk->next->next;
 }
}

canonical_original_code:
static inline void merge_free_blocks(struct _malloc_block *blk)
{
 while (blk->used == 0 && blk->next && blk->next->used == 0)
 {
  if (blk->magic != 0x7ABCDEF7)
  {
   panic("Heap corrupted");
   return;
  }
  blk->size += blk->next->size + sizeof(malloc_block_t);
  blk->next = blk->next->next;
 }
}

variable_types: struct _malloc_block { unsigned int magic; unsigned int used; unsigned long size; struct _malloc_block *next; } *


original_code:
static malloc_block_t *cut_block(malloc_block_t *blk, ulong size)
{
 malloc_block_t *nblk;
 if (blk->size > size + sizeof(malloc_block_t))
 {
  nblk = (malloc_block_t *)((char *)blk + sizeof(malloc_block_t) + size);
  nblk->size = blk->size - size - sizeof(malloc_block_t);
  nblk->used = 0;
  nblk->magic = 0x7ABCDEF7;
  nblk->next = blk->next;
  merge_free_blocks(nblk);
  blk->size = size;
  blk->used = 1;
  blk->next = nblk;
  return (malloc_block_t *)((char *)blk + sizeof(malloc_block_t));
 }
 else if (blk->size >= size && blk->size <= size + sizeof(malloc_block_t))
 {
  blk->used = 1;
  return (malloc_block_t *)((char *)blk + sizeof(malloc_block_t));
 }
 else
  return 0;
}

canonical_original_code:
static struct _malloc_block *cut_block(struct _malloc_block *blk, unsigned long size)
{
 struct _malloc_block *nblk;
 if (blk->size > size + sizeof(malloc_block_t))
 {
  nblk = (struct _malloc_block *)((char *)blk + sizeof(malloc_block_t) + size);
  nblk->size = blk->size - size - sizeof(malloc_block_t);
  nblk->used = 0;
  nblk->magic = 0x7ABCDEF7;
  nblk->next = blk->next;
  merge_free_blocks(nblk);
  blk->size = size;
  blk->used = 1;
  blk->next = nblk;
  return (struct _malloc_block *)((char *)blk + sizeof(malloc_block_t));
 }
 else if (blk->size >= size && blk->size <= size + sizeof(malloc_block_t))
 {
  blk->used = 1;
  return (struct _malloc_block *)((char *)blk + sizeof(malloc_block_t));
 }
 else
  return 0;
}

variable_types:
blk
struct _malloc_block *

variable_types:
size
unsigned long

return_type: struct _malloc_block { unsigned int magic; unsigned int used; unsigned long size; struct _malloc_block *next; } *