sendfd

decompiled_code:
__int64 __fastcall sendfd(int a1, int a2)
{
int v2; // eax
char v4; // [rsp+Fh] [rbp-79h] BYREF
char *v5; // [rsp+10h] [rbp-78h] BYREF
__int64 v6; // [rsp+18h] [rbp-70h]
__int64 v7[2]; // [rsp+20h] [rbp-68h] BYREF
int v8; // [rsp+30h] [rbp-58h]
struct msghdr message; // [rsp+40h] [rbp-48h] BYREF

v8 = a2;
v5 = &v4;
message.msg_iov = (iovec *)&v5;
message.msg_control = v7;
v4 = 0;
v6 = 1LL;
*(_QWORD *)&message.msg_flags = 0LL;
message.msg_iovlen = 1LL;
message.msg_controllen = 20LL;
v7[0] = 20LL;
v7[1] = 0x100000001LL;
*(_OWORD *)&message.msg_name = 0LL;
v2 = sendmsg(a1, &message, 0);
return (unsigned int)-(v6 != v2);
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, int a2)
{
int v2; // eax
char v4; // [rsp+Fh] [rbp-79h] BYREF
char *v5; // [rsp+10h] [rbp-78h] BYREF
__int64 v6; // [rsp+18h] [rbp-70h]
__int64 v7[2]; // [rsp+20h] [rbp-68h] BYREF
int v8; // [rsp+30h] [rbp-58h]
struct msghdr message; // [rsp+40h] [rbp-48h] BYREF

v8 = a2;
v5 = &v4;
message.msg_iov = (iovec *)&v5;
message.msg_control = v7;
v4 = 0;
v6 = 1LL;
*(_QWORD *)&message.msg_flags = 0LL;
message.msg_iovlen = 1LL;
message.msg_controllen = 20LL;
v7[0] = 20LL;
v7[1] = 0x100000001LL;
*(_OWORD *)&message.msg_name = 0LL;
v2 = sendmsg(a1, &message, 0);
return (unsigned int)-(v6 != v2);
}

original_code:
int
sendfd(int s, int fd)
{
 char buf[1];
 struct iovec iov;
 struct msghdr msg;
 struct cmsghdr *cmsg;
 int n;
 char cms[((((sizeof(int)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)))];
 buf[0] = 0;
 iov.iov_base = buf;
 iov.iov_len = 1;
 memset(&msg, 0, sizeof msg);
 msg.msg_iov = &iov;
 msg.msg_iovlen = 1;
 msg.msg_control = (caddr_t)cms;
 msg.msg_controllen = ((((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (sizeof(int)));
 cmsg = ((size_t) (&msg)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (&msg)->msg_control : (struct cmsghdr *) 0);
 cmsg->cmsg_len = ((((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (sizeof(int)));
 cmsg->cmsg_level = 1;
 cmsg->cmsg_type = SCM_RIGHTS;
 memmove(((cmsg)->__cmsg_data), &fd, sizeof(int));
 if((n=sendmsg(s, &msg, 0)) != iov.iov_len)
  return -1;
 return 0;
}

canonical_original_code:
int sendfd(int s, int fd)
{
 char buf[1];
 struct iovec iov;
 struct msghdr msg;
 struct cmsghdr *cmsg;
 int n;
 char cms[-1];
 buf[0] = 0;
 iov.iov_base = buf;
 iov.iov_len = 1;
 memset(&msg, 0, sizeof msg);
 msg.msg_iov = &iov;
 msg.msg_iovlen = 1;
 msg.msg_control = (char *)cms;
 msg.msg_controllen = ((((sizeof (struct cmsghdr)) + sizeof (unsigned long) - 1) & (unsigned long) ~(sizeof (unsigned long) - 1)) + (sizeof(int)));
 cmsg = ((unsigned long) (&msg)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (&msg)->msg_control : (struct cmsghdr *) 0);
 cmsg->cmsg_len = ((((sizeof (struct cmsghdr)) + sizeof (unsigned long) - 1) & (unsigned long) ~(sizeof (unsigned long) - 1)) + (sizeof(int)));
 cmsg->cmsg_level = 1;
 cmsg->cmsg_type = SCM_RIGHTS;
 memmove(((cmsg)->__cmsg_data), &fd, sizeof(int));
 if((n=sendmsg(s, &msg, 0)) != iov.iov_len)
  return -1;
 return 0;
}

variable_types: s int

variable_types: fd int

variable_types:
buf
char [1]

variable_types: struct msghdr { void *msg_name; unsigned int msg_namelen; struct iovec { void *iov_base; unsigned long iov_len; } *msg_iov; unsigned long msg_iovlen; void *msg_control; unsigned long msg_controllen; int msg_flags; }

variable_types: struct cmsghdr { unsigned long cmsg_len; int cmsg_level; int cmsg_type; unsigned char __cmsg_data[]; } *

variable_types: n int

variable_types:
cms
char [-1]

user_defined_types: struct msghdr { void *msg_name; unsigned int msg_namelen; struct iovec *msg_iov; unsigned long msg_iovlen; void *msg_control; unsigned long msg_controllen; int msg_flags; }


recvfd

decompiled_code:
__int64 __fastcall recvfd(int a1)
{
int v1; // eax
__int64 result; // rax
char v3; // [rsp+Fh] [rbp-79h] BYREF
__int64 v4[2]; // [rsp+10h] [rbp-78h] BYREF
char v5; // [rsp+20h] [rbp-68h] BYREF
struct msghdr message; // [rsp+40h] [rbp-48h] BYREF

v4[1] = 1LL;
v4[0] = (__int64)&v3;
message.msg_iov = (iovec *)v4;
*(_QWORD *)&message.msg_flags = 0LL;
message.msg_iovlen = 1LL;
message.msg_control = &v5;
message.msg_controllen = 24LL;
*(_OWORD *)&message.msg_name = 0LL;
v1 = recvmsg(a1, &message, 0);
if ( v1 < 0 )
return 0xFFFFFFFFLL;
if ( v1 )
{
if ( message.msg_controllen <= 0xF )
recvfd_cold();
result = *((unsigned int *)message.msg_control + 4);
}
else
{
werrstr("unexpected EOF");
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
int v1; // eax
__int64 result; // rax
char v3; // [rsp+Fh] [rbp-79h] BYREF
__int64 v4[2]; // [rsp+10h] [rbp-78h] BYREF
char v5; // [rsp+20h] [rbp-68h] BYREF
struct msghdr message; // [rsp+40h] [rbp-48h] BYREF

v4[1] = 1LL;
v4[0] = (__int64)&v3;
message.msg_iov = (iovec *)v4;
*(_QWORD *)&message.msg_flags = 0LL;
message.msg_iovlen = 1LL;
message.msg_control = &v5;
message.msg_controllen = 24LL;
*(_OWORD *)&message.msg_name = 0LL;
v1 = recvmsg(a1, &message, 0);
if ( v1 < 0 )
return 0xFFFFFFFFLL;
if ( v1 )
{
if ( message.msg_controllen <= 0xF )
func2();
result = *((unsigned int *)message.msg_control + 4);
}
else
{
werrstr("unexpected EOF");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int
recvfd(int s)
{
 int n;
 int fd;
 char buf[1];
 struct iovec iov;
 struct msghdr msg;
 struct cmsghdr *cmsg;
 char cms[((((sizeof(int)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)))];
 iov.iov_base = buf;
 iov.iov_len = 1;
 memset(&msg, 0, sizeof msg);
 msg.msg_name = 0;
 msg.msg_namelen = 0;
 msg.msg_iov = &iov;
 msg.msg_iovlen = 1;
 msg.msg_control = (caddr_t)cms;
 msg.msg_controllen = sizeof cms;
 if((n=recvmsg(s, &msg, 0)) < 0)
  return -1;
 if(n == 0){
  werrstr("unexpected EOF");
  return -1;
 }
 cmsg = ((size_t) (&msg)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (&msg)->msg_control : (struct cmsghdr *) 0);
 memmove(&fd, ((cmsg)->__cmsg_data), sizeof(int));
 return fd;
}

canonical_original_code:
int recvfd(int s)
{
 int n;
 int fd;
 char buf[1];
 struct iovec iov;
 struct msghdr msg;
 struct cmsghdr *cmsg;
 char cms[-1];
 iov.iov_base = buf;
 iov.iov_len = 1;
 memset(&msg, 0, sizeof msg);
 msg.msg_name = 0;
 msg.msg_namelen = 0;
 msg.msg_iov = &iov;
 msg.msg_iovlen = 1;
 msg.msg_control = (char *)cms;
 msg.msg_controllen = sizeof cms;
 if((n=recvmsg(s, &msg, 0)) < 0)
  return -1;
 if(n == 0){
  werrstr("unexpected EOF");
  return -1;
 }
 cmsg = ((unsigned long) (&msg)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (&msg)->msg_control : (struct cmsghdr *) 0);
 memmove(&fd, ((cmsg)->__cmsg_data), sizeof(int));
 return fd;
}

variable_types: struct iovec { void *iov_base; unsigned long iov_len; }

return_type: int

user_defined_types: struct cmsghdr { unsigned long cmsg_len; int cmsg_level; int cmsg_type; unsigned char __cmsg_data[]; }


fmtBflush

original_code:
static int
fmtBflush(Fmt *f)
{
 Biobuf *bp;
 bp = f->farg;
 bp->ocount = (char*)f->to - (char*)f->stop;
 if(Bflush(bp) < 0)
  return 0;
 f->stop = bp->ebuf;
 f->to = (char*)f->stop + bp->ocount;
 f->start = f->to;
 return 1;
}

canonical_original_code:
static int fmtBflush(struct Fmt *f)
{
 struct Biobuf *bp;
 bp = f->farg;
 bp->ocount = (char *)f->to - (char *)f->stop;
 if(Bflush(bp) < 0)
  return 0;
 f->stop = bp->ebuf;
 f->to = (char *)f->stop + bp->ocount;
 f->start = f->to;
 return 1;
}

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

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


Bvprint

original_code:
int
Bvprint(Biobuf *bp, char *fmt, va_list arg)
{
 int n;
 Fmt f;
 f.runes = 0;
 f.stop = bp->ebuf;
 f.start = (char*)f.stop + bp->ocount;
 f.to = f.start;
 f.flush = fmtBflush;
 f.farg = bp;
 f.nfmt = 0;
 fmtlocaleinit(&f, ((void*)0), ((void*)0), ((void*)0));
 n = fmtvprint(&f, fmt, arg);
 bp->ocount = (char*)f.to - (char*)f.stop;
 if(n == 0)
  n = f.nfmt;
 return n;
}

canonical_original_code:
int Bvprint(struct Biobuf *bp, char *fmt, __builtin_va_list arg)
{
 int n;
 struct Fmt f;
 f.runes = 0;
 f.stop = bp->ebuf;
 f.start = (char *)f.stop + bp->ocount;
 f.to = f.start;
 f.flush = fmtBflush;
 f.farg = bp;
 f.nfmt = 0;
 fmtlocaleinit(&f, ((void *)0), ((void *)0), ((void *)0));
 n = fmtvprint(&f, fmt, arg);
 bp->ocount = (char *)f.to - (char *)f.stop;
 if(n == 0)
  n = f.nfmt;
 return n;
}

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

user_defined_types: 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]; }


fmtprint

original_code:
int
fmtprint(Fmt *f, char *fmt, ...)
{
 va_list va;
 int n;
 f->flags = 0;
 f->width = 0;
 f->prec = 0;
 __builtin_va_copy(va,f->args);
 __builtin_va_end(f->args);
 __builtin_va_start(f->args,fmt);
 n = dofmt(f, fmt);
 __builtin_va_end(f->args);
 f->flags = 0;
 f->width = 0;
 f->prec = 0;
 __builtin_va_copy(f->args,va);
 __builtin_va_end(va);
 if(n >= 0)
  return 0;
 return n;
}

canonical_original_code:
int fmtprint(struct Fmt *f, char *fmt, ...)
{
 __builtin_va_list va;
 int n;
 f->flags = 0;
 f->width = 0;
 f->prec = 0;
 __builtin_va_copy(va,f->args);
 __builtin_va_end(f->args);
 __builtin_va_start(f->args,fmt);
 n = dofmt(f, fmt);
 __builtin_va_end(f->args);
 f->flags = 0;
 f->width = 0;
 f->prec = 0;
 __builtin_va_copy(f->args,va);
 __builtin_va_end(va);
 if(n >= 0)
  return 0;
 return n;
}

user_defined_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; }


cistrcmp

decompiled_code:
__int64 __fastcall cistrcmp(unsigned __int8 *a1, unsigned __int8 *a2)
{
int v2; // eax
int v3; // edx
unsigned int v4; // er8

while ( 1 )
{
v2 = *a1;
if ( !(_BYTE)v2 )
return (unsigned int)-(char)*a2;
v3 = *a2++;
++a1;
if ( v2 != v3 )
{
v4 = v3 - 65;
if ( (unsigned int)(v2 - 65) <= 0x19 )
{
v2 += 32;
if ( v4 > 0x19 )
goto LABEL_6;
}
else if ( v4 > 0x19 )
{
return (unsigned int)(v2 - v3);
}
v3 += 32;
LABEL_6:
if ( v3 != v2 )
return (unsigned int)(v2 - v3);
}
}
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int8 *a1, unsigned __int8 *a2)
{
int v2; // eax
int v3; // edx
unsigned int v4; // er8

while ( 1 )
{
v2 = *a1;
if ( !(_BYTE)v2 )
return (unsigned int)-(char)*a2;
v3 = *a2++;
++a1;
if ( v2 != v3 )
{
v4 = v3 - 65;
if ( (unsigned int)(v2 - 65) <= 0x19 )
{
v2 += 32;
if ( v4 > 0x19 )
goto LABEL_6;
}
else if ( v4 > 0x19 )
{
return (unsigned int)(v2 - v3);
}
v3 += 32;
LABEL_6:
if ( v3 != v2 )
return (unsigned int)(v2 - v3);
}
}
}

canonical_original_code:
int cistrcmp(char *s1, char *s2)
{
 int c1; int c2;
 while(*s1){
  c1 = *(unsigned char *)s1++;
  c2 = *(unsigned char *)s2++;
  if(c1 == c2)
   continue;
  if(c1 >= 'A' && c1 <= 'Z')
   c1 -= 'A' - 'a';
  if(c2 >= 'A' && c2 <= 'Z')
   c2 -= 'A' - 'a';
  if(c1 != c2)
   return c1 - c2;
 }
 return -*s2;
}


decompiled_code:
int __fastcall p9dup(int fd, int fd2)
{
int result; // eax

if ( fd2 == -1 )
result = dup(fd);
else
result = dup2(fd, fd2);
return result;
}

canonical_decompiled_code:
int __fastcall func0(int fd, int fd2)
{
int result; // eax

if ( fd2 == -1 )
result = dup(fd);
else
result = dup2(fd, fd2);
return result;
}

original_code:
int
p9dup(int old, int new)
{
 if(new == -1)
  return dup(old);
 return dup2(old, new);
}

variable_types: old int

variable_types: new int


Bprint

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

va_start(va, a2);
return Bvprint(a1, a2, va);
}

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

va_start(va, a2);
return Bvprint(a1, a2, va);
}

original_code:
int
Bprint(Biobuf *bp, char *fmt, ...)
{
 int n;
 va_list arg;
 __builtin_va_start(arg,fmt);
 n = Bvprint(bp, fmt, arg);
 __builtin_va_end(arg);
 return n;
}

canonical_original_code:
int Bprint(struct Biobuf *bp, char *fmt, ...)
{
 int n;
 __builtin_va_list arg;
 __builtin_va_start(arg,fmt);
 n = Bvprint(bp, fmt, arg);
 __builtin_va_end(arg);
 return n;
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
p9main(argc, argv, envp);
exits("main");
return 99;
}

original_code:
int
main(int argc, char **argv)
{
 p9main(argc, argv);
 exits("main");
 return 99;
}

variable_types: argc int

variable_types:
argv
char **


dirfstat

decompiled_code:
__int64 __fastcall dirfstat(unsigned int a1)
{
__int64 v1; // r12
__int64 v2; // r13
__int64 v3; // rax
__int64 v5; // [rsp+8h] [rbp-120h] BYREF
char v6[112]; // [rsp+10h] [rbp-118h] BYREF
struct stat64 buf; // [rsp+80h] [rbp-A8h] BYREF

v1 = 0LL;
if ( fstat64(a1, &buf) >= 0 )
{
snprint(v6, 100LL, "/dev/fd/%d", a1);
v2 = (int)p9dir(&buf, &buf, v6, 0LL, 0LL, 0LL);
v3 = mallocz(v2 + 120, 1LL);
v1 = v3;
if ( v3 )
{
v5 = v3 + 120;
p9dir(&buf, &buf, v6, v3, &v5, v3 + 120 + v2);
}
}
return v1;
}

original_code:
Dir*
dirfstat(int fd)
{
 struct stat st;
 int nstr;
 Dir *d;
 char *str, tmp[100];
 if(fstat(fd, &st) < 0)
  return ((void*)0);
 snprint(tmp, sizeof tmp, "/dev/fd/%d", fd);
 nstr = _p9dir(&st, &st, tmp, ((void*)0), ((void*)0), ((void*)0));
 d = mallocz(sizeof(Dir)+nstr, 1);
 if(d == ((void*)0))
  return ((void*)0);
 str = (char*)&d[1];
 _p9dir(&st, &st, tmp, d, &str, str+nstr);
 return d;
}

canonical_original_code:
struct Dir *dirfstat(int fd)
{
 struct stat st;
 int nstr;
 struct Dir *d;
 char *str; char tmp[100];
 if(fstat(fd, &st) < 0)
  return ((void *)0);
 snprint(tmp, sizeof tmp, "/dev/fd/%d", fd);
 nstr = _p9dir(&st, &st, tmp, ((void *)0), ((void *)0), ((void *)0));
 d = mallocz(sizeof(Dir)+nstr, 1);
 if(d == ((void *)0))
  return ((void *)0);
 str = (char *)&d[1];
 _p9dir(&st, &st, tmp, d, &str, str+nstr);
 return d;
}

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

variable_types: nstr int

variable_types: struct Dir { unsigned short type; unsigned int dev; struct Qid { unsigned long long path; unsigned long vers; unsigned char type; } qid; unsigned long mode; unsigned long atime; unsigned long mtime; long long length; char *name; char *uid; char *gid; char *muid; unsigned int uidnum; unsigned int gidnum; unsigned int muidnum; char *ext; } *

variable_types:
str
char *

variable_types:
tmp
char [100]

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

user_defined_types: struct Dir { unsigned short type; unsigned int dev; struct Qid qid; unsigned long mode; unsigned long atime; unsigned long mtime; long long length; char *name; char *uid; char *gid; char *muid; unsigned int uidnum; unsigned int gidnum; unsigned int muidnum; char *ext; }


dirfwstat

original_code:
int
dirfwstat(int fd, Dir *dir)
{
 int ret;
 struct timeval tv[2];
 ret = 0;
 if(~dir->mode != 0){
  if(fchmod(fd, dir->mode) < 0)
   ret = -1;
 }
 if(~dir->mtime != 0){
  tv[0].tv_sec = dir->mtime;
  tv[0].tv_usec = 0;
  tv[1].tv_sec = dir->mtime;
  tv[1].tv_usec = 0;
  if(futimes(fd, tv) < 0)
   ret = -1;
 }
 if(~dir->length != 0){
  if(ftruncate(fd, dir->length) < 0)
   ret = -1;
 }
 return ret;
}

canonical_original_code:
int dirfwstat(int fd, struct Dir *dir)
{
 int ret;
 struct timeval tv[2];
 ret = 0;
 if(~dir->mode != 0){
  if(fchmod(fd, dir->mode) < 0)
   ret = -1;
 }
 if(~dir->mtime != 0){
  tv[0].tv_sec = dir->mtime;
  tv[0].tv_usec = 0;
  tv[1].tv_sec = dir->mtime;
  tv[1].tv_usec = 0;
  if(futimes(fd, tv) < 0)
   ret = -1;
 }
 if(~dir->length != 0){
  if(ftruncate(fd, dir->length) < 0)
   ret = -1;
 }
 return ret;
}

variable_types: fd int

variable_types: ret int

variable_types: struct timeval { long tv_sec; long tv_usec; } [2]

user_defined_types: struct Qid { unsigned long long path; unsigned long vers; unsigned char type; }


cistrncmp

decompiled_code:
__int64 __fastcall cistrncmp(unsigned __int8 *a1, unsigned __int8 *a2, int a3)
{
int v3; // eax
int v4; // ecx
int v6; // edx
unsigned int v7; // er8
__int64 result; // rax

v3 = *a1;
if ( !(_BYTE)v3 )
{
v4 = a3;
goto LABEL_13;
}
v4 = a3 - 1;
if ( a3 > 0 )
{
while ( 1 )
{
v6 = *a2++;
++a1;
if ( v3 == v6 )
goto LABEL_6;
v7 = v6 - 65;
if ( (unsigned int)(v3 - 65) <= 0x19 )
{
v3 += 32;
if ( v7 > 0x19 )
goto LABEL_5;
}
else if ( v7 > 0x19 )
{
return (unsigned int)(v3 - v6);
}
v6 += 32;
LABEL_5:
if ( v6 != v3 )
return (unsigned int)(v3 - v6);
LABEL_6:
v3 = *a1;
if ( !(_BYTE)v3 )
break;
if ( v4-- == 0 )
goto LABEL_15;
}
}
LABEL_13:
if ( v4 <= 0 )
LABEL_15:
result = 0LL;
else
result = (unsigned int)-(char)*a2;
return result;
}

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

v3 = *a1;
if ( !(_BYTE)v3 )
{
v4 = a3;
goto LABEL_13;
}
v4 = a3 - 1;
if ( a3 > 0 )
{
while ( 1 )
{
v6 = *a2++;
++a1;
if ( v3 == v6 )
goto LABEL_6;
v7 = v6 - 65;
if ( (unsigned int)(v3 - 65) <= 0x19 )
{
v3 += 32;
if ( v7 > 0x19 )
goto LABEL_5;
}
else if ( v7 > 0x19 )
{
return (unsigned int)(v3 - v6);
}
v6 += 32;
LABEL_5:
if ( v6 != v3 )
return (unsigned int)(v3 - v6);
LABEL_6:
v3 = *a1;
if ( !(_BYTE)v3 )
break;
if ( v4-- == 0 )
goto LABEL_15;
}
}
LABEL_13:
if ( v4 <= 0 )
LABEL_15:
result = 0LL;
else
result = (unsigned int)-(char)*a2;
return result;
}

canonical_original_code:
int cistrncmp(char *s1, char *s2, int n)
{
 int c1; int c2;
 while(*s1 && n-- > 0){
  c1 = *(unsigned char *)s1++;
  c2 = *(unsigned char *)s2++;
  if(c1 == c2)
   continue;
  if(c1 >= 'A' && c1 <= 'Z')
   c1 -= 'A' - 'a';
  if(c2 >= 'A' && c2 <= 'Z')
   c2 -= 'A' - 'a';
  if(c1 != c2)
   return c1 - c2;
 }
 if(n <= 0)
  return 0;
 return -*s2;
}

variable_types: n int


decompiled_code:
__int64 __fastcall post9pservice(int fd, char *s, __int64 a3)
{
int v3; // er12
__int64 v5; // r14
__pid_t v6; // eax
unsigned int v7; // ebp
int v8; // ebx
int v9; // edi
__int64 v10; // rax
__int64 v11; // rdi
_BYTE *v12; // rax
__int64 result; // rax
int v14; // eax
int v15; // ebx
int v16; // edi
const char *v17; // rax
const char *v18; // r15

v3 = fd;
if ( !(a3 | (unsigned __int64)s) )
{
close(fd);
werrstr("nothing to do");
return 0xFFFFFFFFLL;
}
if ( s )
{
if ( strchr(s, 33) )
{
v5 = p9strdup(s);
}
else
{
v17 = (const char *)getns();
v18 = v17;
if ( !v17 )
return 0xFFFFFFFFLL;
v5 = smprint("unix!%s/%s", v17, s);
p9free(v18);
}
if ( !v5 )
return 0xFFFFFFFFLL;
v6 = fork();
v7 = v6;
if ( v6 == -1 )
return 0xFFFFFFFFLL;
if ( !v6 )
{
v8 = 3;
p9dup((unsigned int)fd, 0LL);
p9dup((unsigned int)fd, 1LL);
do
{
v9 = v8++;
close(v9);
}
while ( v8 != 20 );
execlp("9pserve", "9pserve", "-u", v5, 0LL);
fprint(2LL, "exec 9pserve: %r\n");
exits("exec");
}
close(v3);
v10 = p9waitfor(v7);
v11 = v10;
if ( !v10 )
return 0xFFFFFFFFLL;
v12 = *(_BYTE **)(v10 + 32);
if ( v12 && *v12 )
{
p9free(v11);
werrstr("9pserve failed");
return 0xFFFFFFFFLL;
}
p9free(v11);
if ( a3 )
{
v3 = p9dial(v5, 0LL, 0LL, 0LL);
if ( v3 < 0 )
{
werrstr("cannot reopen for mount: %r");
return 0xFFFFFFFFLL;
}
}
p9free(v5);
}
result = 0LL;
if ( !a3 )
return result;
v14 = p9rfork(84LL);
if ( v14 == -1 )
return 0xFFFFFFFFLL;
if ( !v14 )
{
v15 = 3;
p9dup((unsigned int)v3, 0LL);
do
{
v16 = v15++;
close(v16);
}
while ( v15 != 20 );
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);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(int fd, char *s, __int64 a3)
{
int v3; // er12
__int64 v5; // r14
__pid_t v6; // eax
unsigned int v7; // ebp
int v8; // ebx
int v9; // edi
__int64 v10; // rax
__int64 v11; // rdi
_BYTE *v12; // rax
__int64 result; // rax
int v14; // eax
int v15; // ebx
int v16; // edi
const char *v17; // rax
const char *v18; // r15

v3 = fd;
if ( !(a3 | (unsigned __int64)s) )
{
close(fd);
werrstr("nothing to do");
return 0xFFFFFFFFLL;
}
if ( s )
{
if ( strchr(s, 33) )
{
v5 = p9strdup(s);
}
else
{
v17 = (const char *)getns();
v18 = v17;
if ( !v17 )
return 0xFFFFFFFFLL;
v5 = smprint("unix!%s/%s", v17, s);
p9free(v18);
}
if ( !v5 )
return 0xFFFFFFFFLL;
v6 = fork();
v7 = v6;
if ( v6 == -1 )
return 0xFFFFFFFFLL;
if ( !v6 )
{
v8 = 3;
p9dup((unsigned int)fd, 0LL);
p9dup((unsigned int)fd, 1LL);
do
{
v9 = v8++;
close(v9);
}
while ( v8 != 20 );
execlp("9pserve", "9pserve", "-u", v5, 0LL);
fprint(2LL, "exec 9pserve: %r\n");
exits("exec");
}
close(v3);
v10 = p9waitfor(v7);
v11 = v10;
if ( !v10 )
return 0xFFFFFFFFLL;
v12 = *(_BYTE **)(v10 + 32);
if ( v12 && *v12 )
{
p9free(v11);
werrstr("9pserve failed");
return 0xFFFFFFFFLL;
}
p9free(v11);
if ( a3 )
{
v3 = p9dial(v5, 0LL, 0LL, 0LL);
if ( v3 < 0 )
{
werrstr("cannot reopen for mount: %r");
return 0xFFFFFFFFLL;
}
}
p9free(v5);
}
result = 0LL;
if ( !a3 )
return result;
v14 = p9rfork(84LL);
if ( v14 == -1 )
return 0xFFFFFFFFLL;
if ( !v14 )
{
v15 = 3;
p9dup((unsigned int)v3, 0LL);
do
{
v16 = v15++;
close(v16);
}
while ( v15 != 20 );
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);
return 0LL;
}

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:
// attributes: thunk
int p9exec(const char *file, char *const argv[])
{
return execvp(file, argv);
}

canonical_decompiled_code:
int func0(const char *file, char *const argv[])
{
return execvp(file, argv);
}

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

variable_types:
prog
char *

variable_types:
argv
char *[]


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

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

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

v4 = readn(a1, a2, 4LL);
if ( v4 != 4 )
return (unsigned int)(v4 >> 31);
v6 = *a2;
if ( (int)*a2 <= 4 || v6 > a3 )
{
werrstr("bad length in 9P2000 message header");
result = 0xFFFFFFFFLL;
}
else
{
v7 = v6 - 4;
v8 = readn((unsigned int)a1, a2 + 1, (int)(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:
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 v2; // rax
unsigned int v3; // edx
unsigned int v4; // ecx

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

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

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

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:
buf
char [512]

variable_types:
fld
char *[5]


utfutf

decompiled_code:
char *__fastcall utfutf(char *haystack, char *needle)
{
char *v2; // r12
int v3; // eax
__int64 v4; // r13
__int64 v5; // rbx
size_t v6; // r14
const char *v7; // rax
const char *v8; // r12
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; // r12
int v3; // eax
__int64 v4; // r13
__int64 v5; // rbx
size_t v6; // r14
const char *v7; // rax
const char *v8; // r12
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; // r12
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; // r12
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 v2; // xmm0_8
__int64 result; // rax
__int64 v4; // [rsp+10h] [rbp-28h] BYREF
int v5; // [rsp+18h] [rbp-20h]

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

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

v4 = a1;
v5 = 0;
v2 = fmtcharstod(Bgetdf, &v4);
if ( v5 )
return 0xFFFFFFFFLL;
Bungetc(a1);
result = 1LL;
*a2 = v2;
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:
// attributes: thunk
__off64_t p9seek(int fd, __off64_t offset, int whence)
{
return lseek64(fd, offset, whence);
}

canonical_decompiled_code:
__off64_t func0(int fd, __off64_t offset, int whence)
{
return lseek64(fd, offset, whence);
}

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 *c, __int64 a2)
{
const char *v2; // rbp
__int64 v3; // rax
int v4; // eax
unsigned int s[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v2 = c;
if ( a2 <= 127 )
return strchr(c, a2);
while ( 1 )
{
v3 = *(unsigned __int8 *)v2;
if ( (unsigned __int64)v3 <= 0x7F )
break;
LABEL_5:
v4 = chartorune(s, v2);
if ( s[0] == a2 )
return (char *)v2;
v2 += v4;
}
while ( v3 )
{
v3 = *(unsigned __int8 *)++v2;
if ( v3 > 127 )
goto LABEL_5;
}
return 0LL;
}

canonical_decompiled_code:
char *__fastcall func0(const char *c, __int64 a2)
{
const char *v2; // rbp
__int64 v3; // rax
int v4; // eax
unsigned int s[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v2 = c;
if ( a2 <= 127 )
return strchr(c, a2);
while ( 1 )
{
v3 = *(unsigned __int8 *)v2;
if ( (unsigned __int64)v3 <= 0x7F )
break;
LABEL_5:
v4 = chartorune(s, v2);
if ( s[0] == a2 )
return (char *)v2;
v2 += v4;
}
while ( v3 )
{
v3 = *(unsigned __int8 *)++v2;
if ( v3 > 127 )
goto LABEL_5;
}
return 0LL;
}

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()
{
__int64 v0; // rdi
unsigned int v1; // ebp
int v2; // eax
ssize_t v4; // rax
__int64 v5; // rdi
__int64 buf[4]; // [rsp+8h] [rbp-20h] BYREF

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

canonical_decompiled_code:
__int64 func0()
{
__int64 v0; // rdi
unsigned int v1; // ebp
int v2; // eax
ssize_t v4; // rax
__int64 v5; // rdi
__int64 buf[4]; // [rsp+8h] [rbp-20h] BYREF

v0 = (unsigned int)randfd_1;
if ( randfd_1 < 0 )
{
randfile_0 = (__int64)"/dev/random";
randfd_1 = p9open("/dev/random", 0LL);
if ( randfd_1 < 0 || (v4 = read(randfd_1, buf, 1uLL), v5 = (unsigned int)randfd_1, v4 != 1) )
{
randfile_0 = (__int64)"/dev/srandom";
randfd_1 = p9open("/dev/srandom", 0LL);
v5 = (unsigned int)randfd_1;
}
if ( (int)v5 < 0 )
{
sysfatal("can't open %s: %r", randfile_0);
v5 = (unsigned int)randfd_1;
}
fcntl64(v5, 2LL, 1LL);
v0 = (unsigned int)randfd_1;
}
v1 = 0;
while ( 1 )
{
v2 = readn(v0, (char *)buf + (int)v1, 8LL - (int)v1);
if ( v2 < 0 )
break;
v1 += v2;
if ( v1 > 7 )
return buf[0];
LABEL_4:
v0 = (unsigned int)randfd_1;
}
v1 += v2;
sysfatal("can't read %s: %r", randfile_0);
if ( v1 <= 7 )
goto LABEL_4;
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 *result; // rax
char *v4; // rdx

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

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

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

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; // r12
_BYTE *v3; // rbx
unsigned int v4; // ebp
char v6[28]; // [rsp+Ch] [rbp-1Ch] BYREF

v2 = (unsigned __int64)&a1[a2];
if ( a1 >= &a1[a2] )
return 0LL;
v3 = a1;
v4 = 0;
do
{
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;
}
while ( (unsigned __int64)v3 < v2 );
return v4;
}

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

v2 = (unsigned __int64)&a1[a2];
if ( a1 >= &a1[a2] )
return 0LL;
v3 = a1;
v4 = 0;
do
{
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;
}
while ( (unsigned __int64)v3 < v2 );
return v4;
}

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 ( 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;
}
return result;
}

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

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;
}
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 func1(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_constprop_0_isra_0((unsigned int)spiFd);
*(_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)
{
func3((unsigned int)spiFd);
*(_DWORD *)(a2 + 8) = 1;
if ( (int)func0(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_constprop_0_isra_0((unsigned int)spiFd);
*(_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)
{
func3((unsigned int)spiFd);
*(_DWORD *)(a2 + 56) = 1;
if ( (int)func0(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_constprop_0_isra_0((unsigned int)spiFd);
*(_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)
{
func3((unsigned int)spiFd);
*(_DWORD *)(a2 + 16) = 1;
if ( (int)func0(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)
{
unsigned int v3; // edi

dummyIo_constprop_0_isra_0((unsigned int)spiFd);
v3 = spiFd;
*(_DWORD *)(a2 + 16) = 1;
if ( (int)addressWriteCmd_constprop_0(v3, a1, a2) >= 0 )
return 0LL;
perror("addressReadCmd");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned __int8 a1, __int64 a2)
{
unsigned int v3; // edi

func3((unsigned int)spiFd);
v3 = spiFd;
*(_DWORD *)(a2 + 16) = 1;
if ( (int)func2(v3, a1, 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 v5; // edi
__int64 result; // rax

dummyIo_constprop_0_isra_0((unsigned int)spiFd);
if ( (unsigned __int8)(a2 - 65) > 3u )
{
printf("ltc6804_readCellVoltageGroup(): cannot read from group %c.\r\n", (unsigned int)a2);
result = 0xFFFFFFFFLL;
}
else
{
v5 = spiFd;
*(_DWORD *)(a3 + 8) = 1;
if ( (int)addressReadCmd(v5, a1, 2 * a2 - 126, (void (__fastcall *)(__int64, _QWORD))&ltc6804util_decodeCV, a3) < 0 )
{
perror("addressReadCmd");
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned __int8 a1, char a2, __int64 a3)
{
unsigned int v5; // edi
__int64 result; // rax

func3((unsigned int)spiFd);
if ( (unsigned __int8)(a2 - 65) > 3u )
{
printf("ltc6804_readCellVoltageGroup(): cannot read from group %c.\r\n", (unsigned int)a2);
result = 0xFFFFFFFFLL;
}
else
{
v5 = spiFd;
*(_DWORD *)(a3 + 8) = 1;
if ( (int)func0(v5, a1, 2 * a2 - 126, (void (__fastcall *)(__int64, _QWORD))&ltc6804util_decodeCV, a3) < 0 )
{
perror("addressReadCmd");
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
return result;
}

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, __int64 a2)
{
__int64 v2; // rbx
unsigned int *v3; // r12
unsigned int v4; // edx
__int16 v6[4]; // [rsp+4h] [rbp-44h] BYREF
int v7; // [rsp+Ch] [rbp-3Ch]
char v8; // [rsp+10h] [rbp-38h] BYREF
__int64 v9; // [rsp+14h] [rbp-34h]
int v10; // [rsp+1Ch] [rbp-2Ch]

v2 = a2;
v3 = (unsigned int *)&v8;
dummyIo_constprop_0_isra_0((unsigned int)spiFd);
v10 = 10;
v4 = 4;
v9 = 0x800000006LL;
*(_DWORD *)(a2 + 24) = 1;
while ( 1 )
{
v7 = 1;
if ( (int)addressReadCmd(spiFd, a1, v4, (void (__fastcall *)(__int64, _QWORD))&ltc6804util_decodeCV, (__int64)v6) < 0 )
break;
v2 += 6LL;
++v3;
*(_WORD *)(v2 - 6) = v6[0];
*(_WORD *)(v2 - 4) = v6[1];
*(_WORD *)(v2 - 2) = v6[2];
*(_DWORD *)(a2 + 24) &= v7;
if ( v2 == a2 + 24 )
return 0LL;
v4 = *v3;
}
perror("addressReadCmd");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned __int8 a1, __int64 a2)
{
__int64 v2; // rbx
unsigned int *v3; // r12
unsigned int v4; // edx
__int16 v6[4]; // [rsp+4h] [rbp-44h] BYREF
int v7; // [rsp+Ch] [rbp-3Ch]
char v8; // [rsp+10h] [rbp-38h] BYREF
__int64 v9; // [rsp+14h] [rbp-34h]
int v10; // [rsp+1Ch] [rbp-2Ch]

v2 = a2;
v3 = (unsigned int *)&v8;
func3((unsigned int)spiFd);
v10 = 10;
v4 = 4;
v9 = 0x800000006LL;
*(_DWORD *)(a2 + 24) = 1;
while ( 1 )
{
v7 = 1;
if ( (int)func0(spiFd, a1, v4, (void (__fastcall *)(__int64, _QWORD))&ltc6804util_decodeCV, (__int64)v6) < 0 )
break;
v2 += 6LL;
++v3;
*(_WORD *)(v2 - 6) = v6[0];
*(_WORD *)(v2 - 4) = v6[1];
*(_WORD *)(v2 - 2) = v6[2];
*(_DWORD *)(a2 + 24) &= v7;
if ( v2 == a2 + 24 )
return 0LL;
v4 = *v3;
}
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_constprop_0_isra_0((unsigned int)spiFd);
*(_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)
{
func3((unsigned int)spiFd);
*(_DWORD *)(a2 + 8) = 1;
if ( (int)func0(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_constprop_0_isra_0((unsigned int)spiFd);
*(_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)
{
func3((unsigned int)spiFd);
*(_DWORD *)(a2 + 8) = 1;
if ( (int)func0(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)
{
int v3; // ebx

v3 = a3 | a2;
dummyIo_constprop_0_isra_0((unsigned int)spiFd);
if ( (int)sendCmd(spiFd, a1, v3 | 0x260u) >= 0 )
return 0LL;
perror("sendCmd");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func13(unsigned __int8 a1, int a2, int a3)
{
int v3; // ebx

v3 = a3 | a2;
func3((unsigned int)spiFd);
if ( (int)func1(spiFd, a1, v3 | 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)
{
int v3; // ebx

v3 = a3 | a2;
dummyIo_constprop_0_isra_0((unsigned int)spiFd);
if ( (int)sendCmd(spiFd, a1, v3 | 0x460u) >= 0 )
return 0LL;
perror("sendCmd");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func14(unsigned __int8 a1, int a2, int a3)
{
int v3; // ebx

v3 = a3 | a2;
func3((unsigned int)spiFd);
if ( (int)func1(spiFd, a1, v3 | 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)
{
int v3; // ebx

v3 = a3 | a2;
dummyIo_constprop_0_isra_0((unsigned int)spiFd);
if ( (int)sendCmd(spiFd, a1, v3 | 0x468u) >= 0 )
return 0LL;
perror("sendCmd");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func15(unsigned __int8 a1, int a2, int a3)
{
int v3; // ebx

v3 = a3 | a2;
func3((unsigned int)spiFd);
if ( (int)func1(spiFd, a1, v3 | 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_constprop_0_isra_0((unsigned int)spiFd);
if ( (int)sendCmd(spiFd, a1, 0x715u) >= 0 )
return 0LL;
perror("sendCmd");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func16(unsigned __int8 a1)
{
func3((unsigned int)spiFd);
if ( (int)func1(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 v4; // rdi
__int128 v7; // [rsp+0h] [rbp-38h] BYREF
int v8; // [rsp+10h] [rbp-28h]

v4 = (unsigned int)spiFd;
if ( spiFd )
goto LABEL_4;
ltc6804util_initPec();
if ( (int)spiInit(&spiFd, a2, a3) >= 0 )
{
v4 = (unsigned int)spiFd;
LABEL_4:
dummyIo_constprop_0_isra_0(v4);
usleep(0x1F4u);
ltc6804_startMuxDiag(a1);
usleep(0x1388u);
v8 = 0;
v7 = 0xA01075200000101uLL;
ltc6804_writeConfig(a1, (__int64)&v7);
return 1LL;
}
perror("spiInit");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func17(unsigned __int8 a1, __int64 a2, unsigned int a3)
{
__int64 v4; // rdi
__int128 v7; // [rsp+0h] [rbp-38h] BYREF
int v8; // [rsp+10h] [rbp-28h]

v4 = (unsigned int)spiFd;
if ( spiFd )
goto LABEL_4;
ltc6804util_initPec();
if ( (int)spiInit(&spiFd, a2, a3) >= 0 )
{
v4 = (unsigned int)spiFd;
LABEL_4:
func3(v4);
usleep(0x1F4u);
func16(a1);
usleep(0x1388u);
v8 = 0;
v7 = 0xA01075200000101uLL;
func8(a1, (__int64)&v7);
return 1LL;
}
perror("spiInit");
return 0xFFFFFFFFLL;
}

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_constprop_0_isra_0((unsigned int)spiFd);
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)
{
func3((unsigned int)spiFd);
if ( (int)func1(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 func2(__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 v3; // xmm0_8
double result; // xmm0_8

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

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

v2 = a1[3] * a2;
if ( v2 < 0.0 )
v2 = v2 * a1[4];
v3 = (*a1 * a1[10] - v2 / 3600.0) / a1[10];
if ( v3 > 1.0 )
result = *(double *)&LC2;
else
result = fmax(0.0, v3);
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 )
{
v6 = 3;
v7 = 0;
v8[0] = 0;
if ( ioctl(v3, 0x40016B01uLL, &v6) < 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, v8) < 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 )
{
v6 = 3;
v7 = 0;
v8[0] = 0;
if ( ioctl(v3, 0x40016B01uLL, &v6) < 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, v8) < 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:
int __fastcall spiIo(int a1, __int64 a2, __int64 a3, int a4)
{
int result; // eax
int v5; // [rsp+Ch] [rbp-2Ch]
__int64 v6[2]; // [rsp+10h] [rbp-28h] BYREF
__int128 v7; // [rsp+20h] [rbp-18h]

if ( a1 )
{
v6[0] = a2;
v7 = 0LL;
v6[1] = a3;
WORD4(v7) = 10;
LODWORD(v7) = a4;
BYTE11(v7) = 1;
result = ioctl(a1, 0x40206B00uLL, v6);
if ( result < 0 )
{
v5 = result;
perror("ioctl, SPI_IOC_MESSAGE(1)");
result = v5;
}
}
else
{
perror("spiIo");
result = -1;
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(int a1, __int64 a2, __int64 a3, int a4)
{
int result; // eax
int v5; // [rsp+Ch] [rbp-2Ch]
__int64 v6[2]; // [rsp+10h] [rbp-28h] BYREF
__int128 v7; // [rsp+20h] [rbp-18h]

if ( a1 )
{
v6[0] = a2;
v7 = 0LL;
v6[1] = a3;
WORD4(v7) = 10;
LODWORD(v7) = a4;
BYTE11(v7) = 1;
result = ioctl(a1, 0x40206B00uLL, v6);
if ( result < 0 )
{
v5 = result;
perror("ioctl, SPI_IOC_MESSAGE(1)");
result = v5;
}
}
else
{
perror("spiIo");
result = -1;
}
return result;
}

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);
}