original_code:
int adns_beforepoll(adns_state ads, struct pollfd *fds, int *nfds_io,
      int *timeout_io, const struct timeval *now) {
  struct timeval tv_nowbuf, tv_tobuf, *tv_to;
  int space, found, timeout_ms, r;
  struct pollfd fds_tmp[2];
  adns__consistency(ads,0,cc_entex);
  if (timeout_io) {
    adns__must_gettimeofday(ads,&now,&tv_nowbuf);
    if (!now) { *nfds_io= 0; r= 0; goto xit; }
    timeout_ms= *timeout_io;
    if (timeout_ms == -1) {
      tv_to= 0;
    } else {
      tv_tobuf.tv_sec= timeout_ms / 1000;
      tv_tobuf.tv_usec= (timeout_ms % 1000)*1000;
      tv_to= &tv_tobuf;
    }
    adns__timeouts(ads, 0, &tv_to,&tv_tobuf, *now);
    if (tv_to) {
      ((void) sizeof ((tv_to == &tv_tobuf) ? 1 : 0), __extension__ ({ if (tv_to == &tv_tobuf) ; else __assert_fail ("tv_to == &tv_tobuf", "poll.c", 58, __extension__ __PRETTY_FUNCTION__); }));
      timeout_ms= (tv_tobuf.tv_usec+999)/1000;
      ((void) sizeof ((tv_tobuf.tv_sec < (0x7fffffff -timeout_ms)/1000) ? 1 : 0), __extension__ ({ if (tv_tobuf.tv_sec < (0x7fffffff -timeout_ms)/1000) ; else __assert_fail ("tv_tobuf.tv_sec < (INT_MAX-timeout_ms)/1000", "poll.c", 60, __extension__ __PRETTY_FUNCTION__); }));
      timeout_ms += tv_tobuf.tv_sec*1000;
    } else {
      timeout_ms= -1;
    }
    *timeout_io= timeout_ms;
  }
  space= *nfds_io;
  if (space >= 2) {
    found= adns__pollfds(ads,fds);
    *nfds_io= found;
  } else {
    found= adns__pollfds(ads,fds_tmp);
    *nfds_io= found;
    if (space < found) { r= 34; goto xit; }
    memcpy(fds,fds_tmp,sizeof(struct pollfd)*found);
  }
  r= 0;
xit:
  adns__consistency(ads,0,cc_entex);
  return r;
}

canonical_original_code:
int adns_beforepoll(struct adns__state *ads, struct pollfd *fds, int *nfds_io, int *timeout_io, struct timeval *now) {
  struct timeval tv_nowbuf; struct timeval tv_tobuf; struct timeval *tv_to;
  int space; int found; int timeout_ms; int r;
  struct pollfd fds_tmp[2];
  adns__consistency(ads,0,cc_entex);
  if (timeout_io) {
    adns__must_gettimeofday(ads,&now,&tv_nowbuf);
    if (!now) { *nfds_io= 0; r= 0; goto xit; }
    timeout_ms= *timeout_io;
    if (timeout_ms == -1) {
      tv_to= 0;
    } else {
      tv_tobuf.tv_sec= timeout_ms / 1000;
      tv_tobuf.tv_usec= (timeout_ms % 1000)*1000;
      tv_to= &tv_tobuf;
    }
    adns__timeouts(ads, 0, &tv_to,&tv_tobuf, *now);
    if (tv_to) {
      ((void) sizeof ((tv_to == &tv_tobuf) ? 1 : 0), __extension__ ({ if (tv_to == &tv_tobuf) ; else __assert_fail ("tv_to == &tv_tobuf", "poll.c", 58, __extension__ __PRETTY_FUNCTION__); }));
      timeout_ms= (tv_tobuf.tv_usec+999)/1000;
      ((void) sizeof ((tv_tobuf.tv_sec < (0x7fffffff -timeout_ms)/1000) ? 1 : 0), __extension__ ({ if (tv_tobuf.tv_sec < (0x7fffffff -timeout_ms)/1000) ; else __assert_fail ("tv_tobuf.tv_sec < (INT_MAX-timeout_ms)/1000", "poll.c", 60, __extension__ __PRETTY_FUNCTION__); }));
      timeout_ms += tv_tobuf.tv_sec*1000;
    } else {
      timeout_ms= -1;
    }
    *timeout_io= timeout_ms;
  }
  space= *nfds_io;
  if (space >= 2) {
    found= adns__pollfds(ads,fds);
    *nfds_io= found;
  } else {
    found= adns__pollfds(ads,fds_tmp);
    *nfds_io= found;
    if (space < found) { r= 34; goto xit; }
    memcpy(fds,fds_tmp,sizeof(struct pollfd)*found);
  }
  r= 0;
xit:
  adns__consistency(ads,0,cc_entex);
  return r;
}

variable_types: struct adns__state { enum adns_initflags { adns_if_none=0, adns_if_noenv=1, adns_if_noerrprint=2, adns_if_noserverwarn=4, adns_if_debug=8, adns_if_logpid=128, adns_if_noautosys=16, adns_if_eintr=32, adns_if_nosigpipe=64, adns_if_checkc_entex=256, adns_if_checkc_freq=768 } iflags; void (*logfn)(struct adns__state *ads, void *logfndata, char *fmt, __builtin_va_list al); void *logfndata; int configerrno; struct query_queue { struct adns__query { struct adns__state *ads; enum { query_tosend=0, query_tcpw=1, query_childw=2, query_done=3 } state; struct adns__query *back; struct { struct adns__query *head; } children; struct { struct adns__query *back; } siblings; struct { struct allocnode { struct allocnode *next; } *head; } allocations; int interim_allocd; void *final_allocspace; struct typeinfo { enum adns_rrtype { adns_rrt_typemask=65535, adns__qtf_deref=65536, adns__qtf_mail822=131072, adns_r_unknown=262144, adns_r_none=0, adns_r_a=1, adns_r_ns_raw=2, adns_r_ns=expr, adns_r_cname=5, adns_r_soa_raw=6, adns_r_soa=expr, adns_r_ptr_raw=12, adns_r_ptr=expr, adns_r_hinfo=13, adns_r_mx_raw=15, adns_r_mx=expr, adns_r_txt=16, adns_r_rp_raw=17, adns_r_rp=expr, adns_r_srv_raw=33, adns_r_srv=expr, adns_r_addr=expr } typekey; char *rrtname; char *fmtname; int rrsz; void (*makefinal)(struct adns__query *qu, void *data); enum adns_status { adns_s_ok=0, adns_s_nomemory=1, adns_s_unknownrrtype=2, adns_s_systemfail=3, adns_s_max_localfail=29, adns_s_timeout=30, adns_s_allservfail=31, adns_s_norecurse=32, adns_s_invalidresponse=33, adns_s_unknownformat=34, adns_s_max_remotefail=59, adns_s_rcodeservfail=60, adns_s_rcodeformaterror=61, adns_s_rcodenotimplemented=62, adns_s_rcoderefused=63, adns_s_rcodeunknown=64, adns_s_max_tempfail=99, adns_s_inconsistent=100, adns_s_prohibitedcname=101, adns_s_answerdomaininvalid=102, adns_s_answerdomaintoolong=103, adns_s_invaliddata=104, adns_s_max_misconfig=199, adns_s_querydomainwrong=200, adns_s_querydomaininvalid=201, adns_s_querydomaintoolong=202, adns_s_max_misquery=299, adns_s_nxdomain=300, adns_s_nodata=301, adns_s_max_permfail=499 } (*convstring)(struct vbuf { int used; unsigned char *buf; } *vb, void *data); enum adns_status { adns_s_ok=0, adns_s_nomemory=1, adns_s_unknownrrtype=2, adns_s_systemfail=3, adns_s_max_localfail=29, adns_s_timeout=30, adns_s_allservfail=31, adns_s_norecurse=32, adns_s_invalidresponse=33, adns_s_unknownformat=34, adns_s_max_remotefail=59, adns_s_rcodeservfail=60, adns_s_rcodeformaterror=61, adns_s_rcodenotimplemented=62, adns_s_rcoderefused=63, adns_s_rcodeunknown=64, adns_s_max_tempfail=99, adns_s_inconsistent=100, adns_s_prohibitedcname=101, adns_s_answerdomaininvalid=102, adns_s_answerdomaintoolong=103, adns_s_invaliddata=104, adns_s_max_misconfig=199, adns_s_querydomainwrong=200, adns_s_querydomaininvalid=201, adns_s_querydomaintoolong=202, adns_s_max_misquery=299, adns_s_nxdomain=300, adns_s_nodata=301, adns_s_max_permfail=499 } (*parse)(struct parseinfo { struct adns__state *ads; struct adns__query *qu; int serv; unsigned char *dgram; int dglen; struct timeval { long tv_sec; long tv_usec; } now; } *pai, int cbyte, int max, void *store_r); int (*diff_needswap)(struct adns__state *ads, void *datap_a, void *datap_b); enum adns_status { adns_s_ok=0, adns_s_nomemory=1, adns_s_unknownrrtype=2, adns_s_systemfail=3, adns_s_max_localfail=29, adns_s_timeout=30, adns_s_allservfail=31, adns_s_norecurse=32, adns_s_invalidresponse=33, adns_s_unknownformat=34, adns_s_max_remotefail=59, adns_s_rcodeservfail=60, adns_s_rcodeformaterror=61, adns_s_rcodenotimplemented=62, adns_s_rcoderefused=63, adns_s_rcodeunknown=64, adns_s_max_tempfail=99, adns_s_inconsistent=100, adns_s_prohibitedcname=101, adns_s_answerdomaininvalid=102, adns_s_answerdomaintoolong=103, adns_s_invaliddata=104, adns_s_max_misconfig=199, adns_s_querydomainwrong=200, adns_s_querydomaininvalid=201, adns_s_querydomaintoolong=202, adns_s_max_misquery=299, adns_s_nxdomain=300, adns_s_nodata=301, adns_s_max_permfail=499 } (*qdparselabel)(struct adns__state *ads, char **p_io, char *pe, int labelnum, char label_r[255], int *ll_io, enum adns_queryflags { adns_qf_none=0, adns_qf_search=1, adns_qf_usevc=2, adns_qf_owner=4, adns_qf_quoteok_query=16, adns_qf_quoteok_cname=0, adns_qf_quoteok_anshost=64, adns_qf_quotefail_cname=128, adns_qf_cname_loose=256, adns_qf_cname_forbid=512, adns__qf_internalmask=267386880 } flags, struct typeinfo *typei); void (*postsort)(struct adns__state *ads, void *array, int nrrs, struct typeinfo *typei); } *typei; unsigned char *query_dgram; int query_dglen; struct vbuf vb; struct adns_answer { enum adns_status { adns_s_ok=0, adns_s_nomemory=1, adns_s_unknownrrtype=2, adns_s_systemfail=3, adns_s_max_localfail=29, adns_s_timeout=30, adns_s_allservfail=31, adns_s_norecurse=32, adns_s_invalidresponse=33, adns_s_unknownformat=34, adns_s_max_remotefail=59, adns_s_rcodeservfail=60, adns_s_rcodeformaterror=61, adns_s_rcodenotimplemented=62, adns_s_rcoderefused=63, adns_s_rcodeunknown=64, adns_s_max_tempfail=99, adns_s_inconsistent=100, adns_s_prohibitedcname=101, adns_s_answerdomaininvalid=102, adns_s_answerdomaintoolong=103, adns_s_invaliddata=104, adns_s_max_misconfig=199, adns_s_querydomainwrong=200, adns_s_querydomaininvalid=201, adns_s_querydomaintoolong=202, adns_s_max_misquery=299, adns_s_nxdomain=300, adns_s_nodata=301, adns_s_max_permfail=499 } status; char *cname; char *owner; enum adns_rrtype { adns_rrt_typemask=65535, adns__qtf_deref=65536, adns__qtf_mail822=131072, adns_r_unknown=262144, adns_r_none=0, adns_r_a=1, adns_r_ns_raw=2, adns_r_ns=expr, adns_r_cname=5, adns_r_soa_raw=6, adns_r_soa=expr, adns_r_ptr_raw=12, adns_r_ptr=expr, adns_r_hinfo=13, adns_r_mx_raw=15, adns_r_mx=expr, adns_r_txt=16, adns_r_rp_raw=17, adns_r_rp=expr, adns_r_srv_raw=33, adns_r_srv=expr, adns_r_addr=expr } type; long expires; int nrrs; union { void *untyped; unsigned char *bytes; char **str; struct adns_rr_intstr { int i; char *str; } **manyistr; struct adns_rr_addr { int len; union { struct sockaddr { unsigned short sa_family; char sa_data[14]; } sa; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } inet; } addr; } *addr; struct in_addr *inaddr; struct adns_rr_hostaddr { char *host; enum adns_status { adns_s_ok=0, adns_s_nomemory=1, adns_s_unknownrrtype=2, adns_s_systemfail=3, adns_s_max_localfail=29, adns_s_timeout=30, adns_s_allservfail=31, adns_s_norecurse=32, adns_s_invalidresponse=33, adns_s_unknownformat=34, adns_s_max_remotefail=59, adns_s_rcodeservfail=60, adns_s_rcodeformaterror=61, adns_s_rcodenotimplemented=62, adns_s_rcoderefused=63, adns_s_rcodeunknown=64, adns_s_max_tempfail=99, adns_s_inconsistent=100, adns_s_prohibitedcname=101, adns_s_answerdomaininvalid=102, adns_s_answerdomaintoolong=103, adns_s_invaliddata=104, adns_s_max_misconfig=199, adns_s_querydomainwrong=200, adns_s_querydomaininvalid=201, adns_s_querydomaintoolong=202, adns_s_max_misquery=299, adns_s_nxdomain=300, adns_s_nodata=301, adns_s_max_permfail=499 } astatus; int naddrs; struct adns_rr_addr *addrs; } *hostaddr; struct adns_rr_intstrpair { struct adns_rr_intstr array[2]; } *intstrpair; struct adns_rr_strpair { char *array[2]; } *strpair; struct adns_rr_inthostaddr { int i; struct adns_rr_hostaddr ha; } *inthostaddr; struct adns_rr_intstr *intstr; struct adns_rr_soa { char *mname; unsigned long serial; } *soa; struct adns_rr_srvraw { int priority; char *host; } *srvraw; struct adns_rr_srvha { int priority; struct adns_rr_hostaddr ha; } *srvha; struct adns_rr_byteblock { int len; unsigned char *data; } *byteblock; } rrs; } *answer; unsigned char *cname_dgram; int cname_dglen; struct vbuf search_vb; int search_origlen; int id; int udpnextserver; unsigned long udpsent; struct timeval timeout; long expires; struct qcontext { void *ext; void (*callback)(struct adns__query *parent, struct adns__query *child); union { struct adns_rr_addr ptr_parent_addr; struct adns_rr_hostaddr *hostaddr; } info; } ctx; } *head; } udpw; struct adns__query *forallnext; int nextid; struct vbuf tcpsend; int nservers; enum adns__tcpstate { server_disconnected=0, server_connecting=1, server_ok=2, server_broken=3 } tcpstate; struct timeval tcptimeout; struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); } stdsigpipe; struct __sigset_t stdsigmask; struct pollfd { int fd; short events; short revents; } pollfds_buf[2]; struct server { struct in_addr addr; } servers[5]; struct sortlist { struct in_addr base; } sortlist[15]; char **searchlist; unsigned short rand48xsubi[3]; } *

variable_types:
fds
struct pollfd *

variable_types:
now
struct timeval *

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

variable_types: space int

variable_types: found int

variable_types: r int

variable_types: struct pollfd { int fd; short events; short revents; } [2]

user_defined_types: struct adns__state { enum adns_initflags iflags; void (*logfn)(struct adns__state *ads, void *logfndata, char *fmt, __builtin_va_list al); void *logfndata; int configerrno; struct query_queue udpw; struct adns__query *forallnext; int nextid; struct vbuf tcpsend; int nservers; enum adns__tcpstate tcpstate; struct timeval tcptimeout; struct sigaction stdsigpipe; struct __sigset_t stdsigmask; struct pollfd pollfds_buf[2]; struct server servers[5]; struct sortlist sortlist[15]; char **searchlist; unsigned short rand48xsubi[3]; }

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct adns__query { struct adns__state *ads; enum { query_tosend=0, query_tcpw=1, query_childw=2, query_done=3 } state; struct adns__query *back; struct { struct adns__query *head; } children; struct { struct adns__query *back; } siblings; struct { struct allocnode *head; } allocations; int interim_allocd; void *final_allocspace; struct typeinfo *typei; unsigned char *query_dgram; int query_dglen; struct vbuf vb; struct adns_answer *answer; unsigned char *cname_dgram; int cname_dglen; struct vbuf search_vb; int search_origlen; int id; int udpnextserver; unsigned long udpsent; struct timeval timeout; long expires; struct qcontext ctx; }

user_defined_types: struct adns_answer { enum adns_status status; char *cname; char *owner; enum adns_rrtype type; long expires; int nrrs; union { void *untyped; unsigned char *bytes; char **str; struct adns_rr_intstr **manyistr; struct adns_rr_addr *addr; struct in_addr *inaddr; struct adns_rr_hostaddr *hostaddr; struct adns_rr_intstrpair *intstrpair; struct adns_rr_strpair *strpair; struct adns_rr_inthostaddr *inthostaddr; struct adns_rr_intstr *intstr; struct adns_rr_soa *soa; struct adns_rr_srvraw *srvraw; struct adns_rr_srvha *srvha; struct adns_rr_byteblock *byteblock; } rrs; }

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }

user_defined_types: struct adns_rr_addr { int len; union { struct sockaddr sa; struct sockaddr_in inet; } addr; }

user_defined_types: struct adns_rr_hostaddr { char *host; enum adns_status astatus; int naddrs; struct adns_rr_addr *addrs; }

user_defined_types: struct parseinfo { struct adns__state *ads; struct adns__query *qu; int serv; unsigned char *dgram; int dglen; struct timeval now; }

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }


decompiled_code:
__int64 __fastcall adns_afterpoll(__int64 a1, __int64 a2, unsigned int a3, _QWORD *a4)
{
_QWORD *v6; // [rsp+8h] [rbp-30h] BYREF
char v7[40]; // [rsp+10h] [rbp-28h] BYREF

v6 = a4;
adns__consistency(a1, 0LL, 1LL);
adns__must_gettimeofday(a1, &v6, v7);
if ( v6 )
{
adns__timeouts(a1, 1LL, 0LL, 0LL, *v6, v6[1]);
adns__fdevents(a1, a2, a3, 0LL, 0LL, 0LL, 0LL, *v6, v6[1], 0LL);
}
return adns__consistency(a1, 0LL, 1LL);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, unsigned int a3, _QWORD *a4)
{
_QWORD *v6; // [rsp+8h] [rbp-30h] BYREF
char v7[40]; // [rsp+10h] [rbp-28h] BYREF

v6 = a4;
adns__consistency(a1, 0LL, 1LL);
adns__must_gettimeofday(a1, &v6, v7);
if ( v6 )
{
adns__timeouts(a1, 1LL, 0LL, 0LL, *v6, v6[1]);
adns__fdevents(a1, a2, a3, 0LL, 0LL, 0LL, 0LL, *v6, v6[1], 0LL);
}
return adns__consistency(a1, 0LL, 1LL);
}

original_code:
void adns_afterpoll(adns_state ads, const struct pollfd *fds, int nfds,
      const struct timeval *now) {
  struct timeval tv_buf;
  adns__consistency(ads,0,cc_entex);
  adns__must_gettimeofday(ads,&now,&tv_buf);
  if (now) {
    adns__timeouts(ads, 1, 0,0, *now);
    adns__fdevents(ads, fds,nfds, 0,0,0,0, *now,0);
  }
  adns__consistency(ads,0,cc_entex);
}

canonical_original_code:
void adns_afterpoll(struct adns__state *ads, struct pollfd *fds, int nfds, struct timeval *now) {
  struct timeval tv_buf;
  adns__consistency(ads,0,cc_entex);
  adns__must_gettimeofday(ads,&now,&tv_buf);
  if (now) {
    adns__timeouts(ads, 1, 0,0, *now);
    adns__fdevents(ads, fds,nfds, 0,0,0,0, *now,0);
  }
  adns__consistency(ads,0,cc_entex);
}

variable_types: nfds int

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

return_type: void

user_defined_types: enum adns_initflags { adns_if_none=0, adns_if_noenv=1, adns_if_noerrprint=2, adns_if_noserverwarn=4, adns_if_debug=8, adns_if_logpid=128, adns_if_noautosys=16, adns_if_eintr=32, adns_if_nosigpipe=64, adns_if_checkc_entex=256, adns_if_checkc_freq=768 }

user_defined_types: struct query_queue { struct adns__query *head; }

user_defined_types: enum adns__tcpstate { server_disconnected=0, server_connecting=1, server_ok=2, server_broken=3 }

user_defined_types: struct __sigset_t { unsigned long __val[-1]; }

user_defined_types: struct pollfd { int fd; short events; short revents; }

user_defined_types: struct sortlist { struct in_addr base; }

user_defined_types: struct qcontext { void *ext; void (*callback)(struct adns__query *parent, struct adns__query *child); union { struct adns_rr_addr ptr_parent_addr; struct adns_rr_hostaddr *hostaddr; } info; }

user_defined_types: struct typeinfo { enum adns_rrtype typekey; char *rrtname; char *fmtname; int rrsz; void (*makefinal)(struct adns__query *qu, void *data); enum adns_status (*convstring)(struct vbuf *vb, void *data); enum adns_status (*parse)(struct parseinfo *pai, int cbyte, int max, void *store_r); int (*diff_needswap)(struct adns__state *ads, void *datap_a, void *datap_b); enum adns_status (*qdparselabel)(struct adns__state *ads, char **p_io, char *pe, int labelnum, char label_r[255], int *ll_io, enum adns_queryflags flags, struct typeinfo *typei); void (*postsort)(struct adns__state *ads, void *array, int nrrs, struct typeinfo *typei); }

user_defined_types: struct allocnode { struct allocnode *next; }

user_defined_types: enum adns_rrtype { adns_rrt_typemask=65535, adns__qtf_deref=65536, adns__qtf_mail822=131072, adns_r_unknown=262144, adns_r_none=0, adns_r_a=1, adns_r_ns_raw=2, adns_r_ns=expr, adns_r_cname=5, adns_r_soa_raw=6, adns_r_soa=expr, adns_r_ptr_raw=12, adns_r_ptr=expr, adns_r_hinfo=13, adns_r_mx_raw=15, adns_r_mx=expr, adns_r_txt=16, adns_r_rp_raw=17, adns_r_rp=expr, adns_r_srv_raw=33, adns_r_srv=expr, adns_r_addr=expr }

user_defined_types: enum adns_status { adns_s_ok=0, adns_s_nomemory=1, adns_s_unknownrrtype=2, adns_s_systemfail=3, adns_s_max_localfail=29, adns_s_timeout=30, adns_s_allservfail=31, adns_s_norecurse=32, adns_s_invalidresponse=33, adns_s_unknownformat=34, adns_s_max_remotefail=59, adns_s_rcodeservfail=60, adns_s_rcodeformaterror=61, adns_s_rcodenotimplemented=62, adns_s_rcoderefused=63, adns_s_rcodeunknown=64, adns_s_max_tempfail=99, adns_s_inconsistent=100, adns_s_prohibitedcname=101, adns_s_answerdomaininvalid=102, adns_s_answerdomaintoolong=103, adns_s_invaliddata=104, adns_s_max_misconfig=199, adns_s_querydomainwrong=200, adns_s_querydomaininvalid=201, adns_s_querydomaintoolong=202, adns_s_max_misquery=299, adns_s_nxdomain=300, adns_s_nodata=301, adns_s_max_permfail=499 }

user_defined_types: enum adns_queryflags { adns_qf_none=0, adns_qf_search=1, adns_qf_usevc=2, adns_qf_owner=4, adns_qf_quoteok_query=16, adns_qf_quoteok_cname=0, adns_qf_quoteok_anshost=64, adns_qf_quotefail_cname=128, adns_qf_cname_loose=256, adns_qf_cname_forbid=512, adns__qf_internalmask=267386880 }

user_defined_types: struct adns_rr_intstrpair { struct adns_rr_intstr array[2]; }

user_defined_types: struct adns_rr_strpair { char *array[2]; }

user_defined_types: struct adns_rr_inthostaddr { int i; struct adns_rr_hostaddr ha; }

user_defined_types: struct adns_rr_soa { char *mname; unsigned long serial; }

user_defined_types: struct adns_rr_srvraw { int priority; char *host; }

user_defined_types: struct adns_rr_srvha { int priority; struct adns_rr_hostaddr ha; }

user_defined_types: struct adns_rr_byteblock { int len; unsigned char *data; }

user_defined_types: struct vbuf { int used; unsigned char *buf; }

user_defined_types: struct adns_rr_intstr { int i; char *str; }

user_defined_types: union sigval { int sival_int; void *sival_ptr; }

user_defined_types: struct sockaddr { unsigned short sa_family; char sa_data[14]; }


decompiled_code:
__int64 __fastcall adns_wait_poll(_BYTE *a1, __int64 a2, __int64 a3, __int64 a4)
{
int v6; // eax
int v7; // eax
char *v9; // rax
unsigned int v10; // [rsp+Ch] [rbp-4Ch]
int v11; // [rsp+18h] [rbp-40h] BYREF
int timeout; // [rsp+1Ch] [rbp-3Ch] BYREF
struct pollfd fds; // [rsp+20h] [rbp-38h] BYREF

adns__consistency(a1, 0LL, 1LL);
while ( 1 )
{
v6 = adns__internal_check(a1, a2, a3, a4);
if ( v6 != 11 )
break;
v11 = 2;
timeout = -1;
adns_beforepoll((__int64)a1, &fds, &v11, &timeout, 0LL);
v7 = poll(&fds, v11, timeout);
if ( v7 == -1 )
{
v6 = *_errno_location();
if ( v6 == 4 )
{
if ( (*a1 & 0x20) != 0 )
break;
}
else
{
v9 = strerror(v6);
adns__diag(a1, 0xFFFFFFFFLL, 0LL, "poll failed in wait: %s", v9);
adns_globalsystemfailure(a1);
}
}
else
{
if ( v7 < 0 )
_assert_fail("r >= 0", "poll.c", 0x78u, "adns_wait_poll");
adns_afterpoll((__int64)a1, (__int64)&fds, v11, 0LL);
}
}
v10 = v6;
adns__consistency(a1, 0LL, 1LL);
return v10;
}

canonical_decompiled_code:
__int64 __fastcall func2(_BYTE *a1, __int64 a2, __int64 a3, __int64 a4)
{
int v6; // eax
int v7; // eax
char *v9; // rax
unsigned int v10; // [rsp+Ch] [rbp-4Ch]
int v11; // [rsp+18h] [rbp-40h] BYREF
int timeout; // [rsp+1Ch] [rbp-3Ch] BYREF
struct pollfd fds; // [rsp+20h] [rbp-38h] BYREF

adns__consistency(a1, 0LL, 1LL);
while ( 1 )
{
v6 = adns__internal_check(a1, a2, a3, a4);
if ( v6 != 11 )
break;
v11 = 2;
timeout = -1;
func0((__int64)a1, &fds, &v11, &timeout, 0LL);
v7 = poll(&fds, v11, timeout);
if ( v7 == -1 )
{
v6 = *_errno_location();
if ( v6 == 4 )
{
if ( (*a1 & 0x20) != 0 )
break;
}
else
{
v9 = strerror(v6);
adns__diag(a1, 0xFFFFFFFFLL, 0LL, "poll failed in wait: %s", v9);
adns_globalsystemfailure(a1);
}
}
else
{
if ( v7 < 0 )
_assert_fail("r >= 0", "poll.c", 0x78u, "adns_wait_poll");
func1((__int64)a1, (__int64)&fds, v11, 0LL);
}
}
v10 = v6;
adns__consistency(a1, 0LL, 1LL);
return v10;
}

original_code:
int adns_wait_poll(adns_state ads,
     adns_query *query_io,
     adns_answer **answer_r,
     void **context_r) {
  int r, nfds, to;
  struct pollfd fds[2];
  adns__consistency(ads,0,cc_entex);
  for (;;) {
    r= adns__internal_check(ads,query_io,answer_r,context_r);
    if (r != 11) goto xit;
    nfds= 2; to= -1;
    adns_beforepoll(ads,fds,&nfds,&to,0);
    r= poll(fds,nfds,to);
    if (r == -1) {
      if ((*__errno_location ()) == 4) {
 if (ads->iflags & adns_if_eintr) { r= 4; goto xit; }
      } else {
 adns__diag(ads,-1,0,"poll failed in wait: %s",strerror((*__errno_location ())));
 adns_globalsystemfailure(ads);
      }
    } else {
      ((void) sizeof ((r >= 0) ? 1 : 0), __extension__ ({ if (r >= 0) ; else __assert_fail ("r >= 0", "poll.c", 120, __extension__ __PRETTY_FUNCTION__); }));
      adns_afterpoll(ads,fds,nfds,0);
    }
  }
 xit:
  adns__consistency(ads,0,cc_entex);
  return r;
}

canonical_original_code:
int adns_wait_poll(struct adns__state *ads, struct adns__query **query_io, struct adns_answer **answer_r, void **context_r) {
  int r; int nfds; int to;
  struct pollfd fds[2];
  adns__consistency(ads,0,cc_entex);
  for (;;) {
    r= adns__internal_check(ads,query_io,answer_r,context_r);
    if (r != 11) goto xit;
    nfds= 2; to= -1;
    adns_beforepoll(ads,fds,&nfds,&to,0);
    r= poll(fds,nfds,to);
    if (r == -1) {
      if ((*__errno_location ()) == 4) {
 if (ads->iflags & adns_if_eintr) { r= 4; goto xit; }
      } else {
 adns__diag(ads,-1,0,"poll failed in wait: %s",strerror((*__errno_location ())));
 adns_globalsystemfailure(ads);
      }
    } else {
      ((void) sizeof ((r >= 0) ? 1 : 0), __extension__ ({ if (r >= 0) ; else __assert_fail ("r >= 0", "poll.c", 120, __extension__ __PRETTY_FUNCTION__); }));
      adns_afterpoll(ads,fds,nfds,0);
    }
  }
 xit:
  adns__consistency(ads,0,cc_entex);
  return r;
}

variable_types: to int

return_type: int

user_defined_types: struct adns__state { enum adns_initflags iflags; void (*logfn)(struct adns__state *ads, void *logfndata, char *fmt, __builtin_va_list al); void *logfndata; int configerrno; struct query_queue udpw; struct adns__query *forallnext; int nextid; struct vbuf tcpsend; int nservers; enum adns__tcpstate tcpstate; struct timeval tcptimeout; struct sigaction stdsigpipe; struct __sigset_t stdsigmask; struct pollfd pollfds_buf[2]; struct server servers[5]; struct sortlist sortlist[15]; char **searchlist; unsigned short rand48xsubi[3]; }

user_defined_types: struct pollfd { int fd; short events; short revents; }

user_defined_types: enum adns_initflags { adns_if_none=0, adns_if_noenv=1, adns_if_noerrprint=2, adns_if_noserverwarn=4, adns_if_debug=8, adns_if_logpid=128, adns_if_noautosys=16, adns_if_eintr=32, adns_if_nosigpipe=64, adns_if_checkc_entex=256, adns_if_checkc_freq=768 }

user_defined_types: struct query_queue { struct adns__query *head; }

user_defined_types: enum adns__tcpstate { server_disconnected=0, server_connecting=1, server_ok=2, server_broken=3 }

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct __sigset_t { unsigned long __val[-1]; }

user_defined_types: struct sortlist { struct in_addr base; }

user_defined_types: struct adns__query { struct adns__state *ads; enum { query_tosend=0, query_tcpw=1, query_childw=2, query_done=3 } state; struct adns__query *back; struct { struct adns__query *head; } children; struct { struct adns__query *back; } siblings; struct { struct allocnode *head; } allocations; int interim_allocd; void *final_allocspace; struct typeinfo *typei; unsigned char *query_dgram; int query_dglen; struct vbuf vb; struct adns_answer *answer; unsigned char *cname_dgram; int cname_dglen; struct vbuf search_vb; int search_origlen; int id; int udpnextserver; unsigned long udpsent; struct timeval timeout; long expires; struct qcontext ctx; }

user_defined_types: struct qcontext { void *ext; void (*callback)(struct adns__query *parent, struct adns__query *child); union { struct adns_rr_addr ptr_parent_addr; struct adns_rr_hostaddr *hostaddr; } info; }

user_defined_types: struct adns_answer { enum adns_status status; char *cname; char *owner; enum adns_rrtype type; long expires; int nrrs; union { void *untyped; unsigned char *bytes; char **str; struct adns_rr_intstr **manyistr; struct adns_rr_addr *addr; struct in_addr *inaddr; struct adns_rr_hostaddr *hostaddr; struct adns_rr_intstrpair *intstrpair; struct adns_rr_strpair *strpair; struct adns_rr_inthostaddr *inthostaddr; struct adns_rr_intstr *intstr; struct adns_rr_soa *soa; struct adns_rr_srvraw *srvraw; struct adns_rr_srvha *srvha; struct adns_rr_byteblock *byteblock; } rrs; }

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }

user_defined_types: struct adns_rr_addr { int len; union { struct sockaddr sa; struct sockaddr_in inet; } addr; }

user_defined_types: struct adns_rr_hostaddr { char *host; enum adns_status astatus; int naddrs; struct adns_rr_addr *addrs; }

user_defined_types: struct parseinfo { struct adns__state *ads; struct adns__query *qu; int serv; unsigned char *dgram; int dglen; struct timeval now; }

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

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }


insert

decompiled_code:
unsigned __int64 __fastcall insert(__int64 a1, __int64 a2)
{
int v3; // er12
unsigned int v4; // edx
_QWORD *v5; // r15
unsigned __int64 result; // rax
__int64 v7; // rbx
unsigned int v8; // er12
__int64 v9; // r13
bool v10; // cc
__int64 v11; // rdx
_QWORD *v12; // rbx
__int64 v13; // rax
void *v14; // rdi

v3 = *(_DWORD *)(a1 + 24);
v4 = *(_DWORD *)(a1 + 28);
v5 = *(_QWORD **)a1;
result = (unsigned int)(v3 + 1);
*(_DWORD *)(a1 + 24) = result;
if ( (unsigned int)result >= v4 )
{
v11 = 2 * v4;
*(_DWORD *)(a1 + 28) = v11;
v12 = malloc(8 * v11);
v13 = 0LL;
if ( v3 )
{
do
{
v12[v13] = v5[v13];
++v13;
}
while ( v3 != v13 );
}
v14 = v5;
v5 = v12;
free(v14);
*(_QWORD *)a1 = v12;
result = *(unsigned int *)(a1 + 24);
}
v7 = (unsigned int)(result - 1);
if ( (_DWORD)result == 1 )
{
*v5 = a2;
}
else
{
while ( 1 )
{
v8 = (unsigned int)v7 >> 1;
v9 = (unsigned int)v7 >> 1;
v10 = (*(int (__fastcall **)(__int64, _QWORD))(a1 + 8))(a2, v5[v9]) <= 0;
result = *(_QWORD *)a1;
if ( !v10 )
break;
*(_QWORD *)(result + 8 * v7) = *(_QWORD *)(result + 8 * v9);
if ( !v8 )
{
result = *(_QWORD *)a1;
**(_QWORD **)a1 = a2;
return result;
}
v5 = *(_QWORD **)a1;
v7 = v8;
}
*(_QWORD *)(result + 8 * v7) = a2;
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func0(__int64 a1, __int64 a2)
{
int v3; // er12
unsigned int v4; // edx
_QWORD *v5; // r15
unsigned __int64 result; // rax
__int64 v7; // rbx
unsigned int v8; // er12
__int64 v9; // r13
bool v10; // cc
__int64 v11; // rdx
_QWORD *v12; // rbx
__int64 v13; // rax
void *v14; // rdi

v3 = *(_DWORD *)(a1 + 24);
v4 = *(_DWORD *)(a1 + 28);
v5 = *(_QWORD **)a1;
result = (unsigned int)(v3 + 1);
*(_DWORD *)(a1 + 24) = result;
if ( (unsigned int)result >= v4 )
{
v11 = 2 * v4;
*(_DWORD *)(a1 + 28) = v11;
v12 = malloc(8 * v11);
v13 = 0LL;
if ( v3 )
{
do
{
v12[v13] = v5[v13];
++v13;
}
while ( v3 != v13 );
}
v14 = v5;
v5 = v12;
free(v14);
*(_QWORD *)a1 = v12;
result = *(unsigned int *)(a1 + 24);
}
v7 = (unsigned int)(result - 1);
if ( (_DWORD)result == 1 )
{
*v5 = a2;
}
else
{
while ( 1 )
{
v8 = (unsigned int)v7 >> 1;
v9 = (unsigned int)v7 >> 1;
v10 = (*(int (__fastcall **)(__int64, _QWORD))(a1 + 8))(a2, v5[v9]) <= 0;
result = *(_QWORD *)a1;
if ( !v10 )
break;
*(_QWORD *)(result + 8 * v7) = *(_QWORD *)(result + 8 * v9);
if ( !v8 )
{
result = *(_QWORD *)a1;
**(_QWORD **)a1 = a2;
return result;
}
v5 = *(_QWORD **)a1;
v7 = v8;
}
*(_QWORD *)(result + 8 * v7) = a2;
}
return result;
}

original_code:
void insert(struct heap* hp, void* data) {
    hp->count++;
    if (hp->count >= hp->max) {
        hp->max *= 2;
        void **buffer = malloc(sizeof(void *[hp->max]));
        unsigned i;
        for (i = 0; i < hp->count - 1; i++)
            buffer[i] = hp->heap[i];
        free(hp->heap);
        hp->heap = buffer;
    }
    if (hp->count == 1) {
        hp->heap[0] = data;
    } else {
        unsigned pos = hp->count - 1;
        int cont = 1;
        while (cont) {
            if (pos == 0) {
                hp->heap[0] = data;
                break;
            }
            if (hp->compare(data, hp->heap[pos / 2]) > 0) {
                hp->heap[pos] = data;
                cont = 0;
            } else {
                hp->heap[pos] = hp->heap[pos / 2];
                pos /= 2;
            }
        }
    }
}

canonical_original_code:
void insert(struct heap *hp, void *data) {
    hp->count++;
    if (hp->count >= hp->max) {
        hp->max *= 2;
        void **buffer = malloc(sizeof(void *[hp->max]));
        unsigned int i;
        for (i = 0; i < hp->count - 1; i++)
            buffer[i] = hp->heap[i];
        free(hp->heap);
        hp->heap = buffer;
    }
    if (hp->count == 1) {
        hp->heap[0] = data;
    } else {
        unsigned int pos = hp->count - 1;
        int cont = 1;
        while (cont) {
            if (pos == 0) {
                hp->heap[0] = data;
                break;
            }
            if (hp->compare(data, hp->heap[pos / 2]) > 0) {
                hp->heap[pos] = data;
                cont = 0;
            } else {
                hp->heap[pos] = hp->heap[pos / 2];
                pos /= 2;
            }
        }
    }
}

variable_types: struct heap { void **heap; int (*compare)(void *, void *); void (*destruct)(void *); unsigned int count; } *

variable_types:
data
void *

variable_types:
buffer
void **

variable_types:
i
unsigned int

variable_types:
pos
unsigned int

variable_types: cont int


peek

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

result = 0LL;
if ( *((_DWORD *)a1 + 6) )
result = **a1;
return result;
}

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

result = 0LL;
if ( *((_DWORD *)a1 + 6) )
result = **a1;
return result;
}

original_code:
void *peek(struct heap *hp) {
    if (hp->count > 0)
        return hp->heap[0];
    return ((void *)0);
}

return_type: void *

user_defined_types: struct heap { void **heap; int (*compare)(void *, void *); void (*destruct)(void *); unsigned int count; }


decompiled_code:
__int64 __fastcall min_heapify(__int64 a1, int a2)
{
unsigned int v2; // er15
__int64 result; // rax
unsigned int v4; // ebp
unsigned int v5; // er14
__int64 v6; // r12
__int64 v7; // r13
__int64 *v8; // r12
__int64 v9; // rdx
__int64 *v10; // r12
__int64 v11; // rcx

v2 = a2 + 1;
result = *(unsigned int *)(a1 + 24);
LABEL_2:
v4 = 2 * v2;
v5 = 2 * v2 - 1;
if ( 2 * v2 < (unsigned int)result )
{
LABEL_3:
v6 = 8LL * (v2 - 1);
v7 = 8LL * v5;
if ( (*(int (__fastcall **)(_QWORD, _QWORD))(a1 + 8))(
*(_QWORD *)(*(_QWORD *)a1 + v6),
*(_QWORD *)(*(_QWORD *)a1 + 8LL * v4)) <= 0 )
{
result = *(unsigned int *)(a1 + 24);
goto LABEL_7;
}
goto LABEL_4;
}
LABEL_7:
while ( (unsigned int)result > v5 )
{
v7 = 8LL * v5;
v6 = 8LL * (v2 - 1);
result = (*(__int64 (__fastcall **)(_QWORD, _QWORD))(a1 + 8))(
*(_QWORD *)(*(_QWORD *)a1 + v6),
*(_QWORD *)(*(_QWORD *)a1 + v7));
if ( (int)result <= 0 )
break;
LABEL_4:
result = *(unsigned int *)(a1 + 24);
if ( (unsigned int)result <= v4 )
goto LABEL_11;
if ( (*(int (__fastcall **)(_QWORD, _QWORD))(a1 + 8))(
*(_QWORD *)(*(_QWORD *)a1 + v7),
*(_QWORD *)(*(_QWORD *)a1 + 8LL * v4)) < 0 )
{
result = *(unsigned int *)(a1 + 24);
LABEL_11:
v2 = v5;
v10 = (__int64 *)(*(_QWORD *)a1 + v6);
v11 = *v10;
*v10 = *(_QWORD *)(*(_QWORD *)a1 + v7);
*(_QWORD *)(*(_QWORD *)a1 + v7) = v11;
goto LABEL_2;
}
v8 = (__int64 *)(*(_QWORD *)a1 + v6);
v9 = *v8;
*v8 = *(_QWORD *)(*(_QWORD *)a1 + 8LL * v4);
*(_QWORD *)(*(_QWORD *)a1 + 8LL * v4) = v9;
v2 = v4;
result = *(unsigned int *)(a1 + 24);
v4 *= 2;
v5 = v4 - 1;
if ( v4 < (unsigned int)result )
goto LABEL_3;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2)
{
unsigned int v2; // er15
__int64 result; // rax
unsigned int v4; // ebp
unsigned int v5; // er14
__int64 v6; // r12
__int64 v7; // r13
__int64 *v8; // r12
__int64 v9; // rdx
__int64 *v10; // r12
__int64 v11; // rcx

v2 = a2 + 1;
result = *(unsigned int *)(a1 + 24);
LABEL_2:
v4 = 2 * v2;
v5 = 2 * v2 - 1;
if ( 2 * v2 < (unsigned int)result )
{
LABEL_3:
v6 = 8LL * (v2 - 1);
v7 = 8LL * v5;
if ( (*(int (__fastcall **)(_QWORD, _QWORD))(a1 + 8))(
*(_QWORD *)(*(_QWORD *)a1 + v6),
*(_QWORD *)(*(_QWORD *)a1 + 8LL * v4)) <= 0 )
{
result = *(unsigned int *)(a1 + 24);
goto LABEL_7;
}
goto LABEL_4;
}
LABEL_7:
while ( (unsigned int)result > v5 )
{
v7 = 8LL * v5;
v6 = 8LL * (v2 - 1);
result = (*(__int64 (__fastcall **)(_QWORD, _QWORD))(a1 + 8))(
*(_QWORD *)(*(_QWORD *)a1 + v6),
*(_QWORD *)(*(_QWORD *)a1 + v7));
if ( (int)result <= 0 )
break;
LABEL_4:
result = *(unsigned int *)(a1 + 24);
if ( (unsigned int)result <= v4 )
goto LABEL_11;
if ( (*(int (__fastcall **)(_QWORD, _QWORD))(a1 + 8))(
*(_QWORD *)(*(_QWORD *)a1 + v7),
*(_QWORD *)(*(_QWORD *)a1 + 8LL * v4)) < 0 )
{
result = *(unsigned int *)(a1 + 24);
LABEL_11:
v2 = v5;
v10 = (__int64 *)(*(_QWORD *)a1 + v6);
v11 = *v10;
*v10 = *(_QWORD *)(*(_QWORD *)a1 + v7);
*(_QWORD *)(*(_QWORD *)a1 + v7) = v11;
goto LABEL_2;
}
v8 = (__int64 *)(*(_QWORD *)a1 + v6);
v9 = *v8;
*v8 = *(_QWORD *)(*(_QWORD *)a1 + 8LL * v4);
*(_QWORD *)(*(_QWORD *)a1 + 8LL * v4) = v9;
v2 = v4;
result = *(unsigned int *)(a1 + 24);
v4 *= 2;
v5 = v4 - 1;
if ( v4 < (unsigned int)result )
goto LABEL_3;
}
return result;
}

original_code:
void min_heapify(struct heap *hp, int index) {
    unsigned pos = index + 1;
    while ((pos * 2 < hp->count && hp->compare(hp->heap[pos-1], hp->heap[pos*2]) > 0) ||
            (pos*2 -1 < hp->count && hp->compare(hp->heap[pos-1], hp->heap[pos*2 - 1]) > 0)) {
        void *buffer;
        if (pos * 2 >= hp->count || hp->compare(hp->heap[pos*2 - 1], hp->heap[pos*2]) < 0) {
            buffer = hp->heap[pos-1];
            hp->heap[pos-1] = hp->heap[pos*2 - 1];
            hp->heap[pos*2 - 1] = buffer;
            pos = pos*2 - 1;
        } else {
            buffer = hp->heap[pos-1];
            hp->heap[pos-1] = hp->heap[pos*2];
            hp->heap[pos*2] = buffer;
            pos = pos * 2;
        }
    }
}

canonical_original_code:
void min_heapify(struct heap *hp, int index) {
    unsigned int pos = index + 1;
    while ((pos * 2 < hp->count && hp->compare(hp->heap[pos-1], hp->heap[pos*2]) > 0) ||
            (pos*2 -1 < hp->count && hp->compare(hp->heap[pos-1], hp->heap[pos*2 - 1]) > 0)) {
        void *buffer;
        if (pos * 2 >= hp->count || hp->compare(hp->heap[pos*2 - 1], hp->heap[pos*2]) < 0) {
            buffer = hp->heap[pos-1];
            hp->heap[pos-1] = hp->heap[pos*2 - 1];
            hp->heap[pos*2 - 1] = buffer;
            pos = pos*2 - 1;
        } else {
            buffer = hp->heap[pos-1];
            hp->heap[pos-1] = hp->heap[pos*2];
            hp->heap[pos*2] = buffer;
            pos = pos * 2;
        }
    }
}

variable_types: index int

variable_types:
buffer
void *


pop

original_code:
void *pop(struct heap *hp) {
    if (hp->count == 0)
        return ((void *)0);
    hp->count--;
    void *top = hp->heap[0];
    hp->heap[0] = hp->heap[hp->count];
    hp->heap[hp->count] = ((void *)0);
    min_heapify(hp, 0);
    return top;
}

variable_types:
top
void *


decompiled_code:
__int64 __fastcall remove_if(__int64 a1, __int64 (__fastcall *a2)(_QWORD))
{
__int64 result; // rax
__int64 v3; // rbp
__int64 v4; // rax
__int64 v5; // rax

result = *(unsigned int *)(a1 + 24);
if ( (_DWORD)result )
{
v3 = 0LL;
do
{
result = a2(*(_QWORD *)(*(_QWORD *)a1 + 8 * v3));
if ( (_DWORD)result )
{
v4 = *(_QWORD *)a1;
--*(_DWORD *)(a1 + 24);
(*(void (__fastcall **)(_QWORD))(a1 + 16))(*(_QWORD *)(v4 + 8 * v3));
v5 = *(unsigned int *)(a1 + 24);
*(_QWORD *)(*(_QWORD *)a1 + 8 * v3) = *(_QWORD *)(*(_QWORD *)a1 + 8 * v5);
*(_QWORD *)(*(_QWORD *)a1 + 8 * v5) = 0LL;
result = min_heapify(a1, v3);
}
++v3;
}
while ( *(_DWORD *)(a1 + 24) > (unsigned int)v3 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 (__fastcall *a2)(_QWORD))
{
__int64 result; // rax
__int64 v3; // rbp
__int64 v4; // rax
__int64 v5; // rax

result = *(unsigned int *)(a1 + 24);
if ( (_DWORD)result )
{
v3 = 0LL;
do
{
result = a2(*(_QWORD *)(*(_QWORD *)a1 + 8 * v3));
if ( (_DWORD)result )
{
v4 = *(_QWORD *)a1;
--*(_DWORD *)(a1 + 24);
(*(void (__fastcall **)(_QWORD))(a1 + 16))(*(_QWORD *)(v4 + 8 * v3));
v5 = *(unsigned int *)(a1 + 24);
*(_QWORD *)(*(_QWORD *)a1 + 8 * v3) = *(_QWORD *)(*(_QWORD *)a1 + 8 * v5);
*(_QWORD *)(*(_QWORD *)a1 + 8 * v5) = 0LL;
result = func2(a1, v3);
}
++v3;
}
while ( *(_DWORD *)(a1 + 24) > (unsigned int)v3 );
}
return result;
}

original_code:
void remove_if(struct heap* hp, int (*to_remove)(void*)) {
    int i;
    for (i = 0; i < hp->count; i++) {
        if ( to_remove(hp->heap[i]) ) {
            hp->count--;
            hp->destruct(hp->heap[i]);
            hp->heap[i] = hp->heap[hp->count];
            hp->heap[hp->count] = ((void *)0);
            min_heapify(hp, i);
        }
    }
}

canonical_original_code:
void remove_if(struct heap *hp, int (*to_remove)(void *)) {
    int i;
    for (i = 0; i < hp->count; i++) {
        if ( to_remove(hp->heap[i]) ) {
            hp->count--;
            hp->destruct(hp->heap[i]);
            hp->heap[i] = hp->heap[hp->count];
            hp->heap[hp->count] = ((void *)0);
            min_heapify(hp, i);
        }
    }
}

variable_types: i int


size

decompiled_code:
__int64 __fastcall size(__int64 a1)
{
return *(unsigned int *)(a1 + 24);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
return *(unsigned int *)(a1 + 24);
}

original_code:
unsigned size(struct heap *hp) {
    return hp->count;
}

canonical_original_code:
unsigned int size(struct heap *hp) {
    return hp->count;
}

return_type: unsigned int


traverse

decompiled_code:
__int64 __fastcall traverse(__int64 a1, __int64 (__fastcall *a2)(__int64))
{
__int64 result; // rax
__int64 v4; // rbx
__int64 v5; // rdi

result = *(unsigned int *)(a1 + 24);
if ( (_DWORD)result )
{
v4 = 0LL;
do
{
v5 = *(_QWORD *)(*(_QWORD *)a1 + 8 * v4++);
result = a2(v5);
}
while ( *(_DWORD *)(a1 + 24) > (unsigned int)v4 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 (__fastcall *a2)(__int64))
{
__int64 result; // rax
__int64 v4; // rbx
__int64 v5; // rdi

result = *(unsigned int *)(a1 + 24);
if ( (_DWORD)result )
{
v4 = 0LL;
do
{
v5 = *(_QWORD *)(*(_QWORD *)a1 + 8 * v4++);
result = a2(v5);
}
while ( *(_DWORD *)(a1 + 24) > (unsigned int)v4 );
}
return result;
}

original_code:
void traverse(struct heap* hp, void (*func)(void*)) {
    int i;
    for (i = 0; i < hp->count; i++) {
        func( hp->heap[i] );
    }
}

canonical_original_code:
void traverse(struct heap *hp, void (*func)(void *)) {
    int i;
    for (i = 0; i < hp->count; i++) {
        func( hp->heap[i] );
    }
}

variable_types:
func
void (*)(void *)


decompiled_code:
__int64 __fastcall job_compare(__int64 a1, __int64 a2)
{
return 2 * (unsigned int)(*(_DWORD *)(a1 + 20) <= *(_DWORD *)(a2 + 20)) - 1;
}

original_code:
int job_compare( jobinfo j1, jobinfo j2) {
    if (j1->current_pri > j2->current_pri)
        return -1;
    else
        return 1;
}

canonical_original_code:
int job_compare(struct jobinfo *j1, struct jobinfo *j2) {
    if (j1->current_pri > j2->current_pri)
        return -1;
    else
        return 1;
}

variable_types: struct jobinfo { int jid; int pid; char **argv; int default_pri; int current_pri; int owner; int wait_time; long create_time; int run_time; int state; } *


decompiled_code:
void __fastcall job_destruct(void *ptr)
{
void **v2; // rdi
void *v3; // r8
__int64 v4; // rbx

v2 = (void **)*((_QWORD *)ptr + 1);
v3 = *v2;
if ( *v2 )
{
v4 = 0LL;
do
{
free(v3);
v2 = (void **)*((_QWORD *)ptr + 1);
v2[v4++] = 0LL;
v3 = v2[v4];
}
while ( v3 );
}
free(v2);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func8(void *ptr)
{
void **v2; // rdi
void *v3; // r8
__int64 v4; // rbx

v2 = (void **)*((_QWORD *)ptr + 1);
v3 = *v2;
if ( *v2 )
{
v4 = 0LL;
do
{
free(v3);
v2 = (void **)*((_QWORD *)ptr + 1);
v2[v4++] = 0LL;
v3 = v2[v4];
}
while ( v3 );
}
free(v2);
free(ptr);
}

original_code:
void job_destruct( jobinfo job ) {
    int i;
    for (i = 0; job->argv[i] != ((void *)0); i++) {
        free( job->argv[i] );
        job->argv[i] = ((void *)0);
    }
    free(job->argv);
    free(job);
}

canonical_original_code:
void job_destruct(struct jobinfo *job) {
    int i;
    for (i = 0; job->argv[i] != ((void *)0); i++) {
        free( job->argv[i] );
        job->argv[i] = ((void *)0);
    }
    free(job->argv);
    free(job);
}

user_defined_types: struct jobinfo { int jid; int pid; char **argv; int default_pri; int current_pri; int owner; int wait_time; long create_time; int run_time; int state; }


original_code:
struct command *create_cmd( int type, int pri, int uid, int argc, char *argv[] ) {
    char **p;
    struct command *cmd = malloc(sizeof(struct command));
    cmd->info = type;
    cmd->pri = pri;
    cmd->owner = uid;
    cmd->argc = argc;
    strcat(cmd->buf, *argv++);
    argc--;
    while (argc-- > 0) {
        strcat(cmd->buf, ":");
        strcat(cmd->buf, *argv++);
    }
    return cmd;
}

canonical_original_code:
struct command *create_cmd(int type, int pri, int uid, int argc, char *argv[]) {
    char **p;
    struct command *cmd = malloc(sizeof(struct command));
    cmd->info = type;
    cmd->pri = pri;
    cmd->owner = uid;
    cmd->argc = argc;
    strcat(cmd->buf, *argv++);
    argc--;
    while (argc-- > 0) {
        strcat(cmd->buf, ":");
        strcat(cmd->buf, *argv++);
    }
    return cmd;
}

variable_types: type int

variable_types: pri int

variable_types: uid int

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
p
char **

variable_types: struct command { int info; int state; int argc; int owner; int pri; char buf[100]; } *


return_type: struct command { int info; int state; int argc; int owner; int pri; char buf[100]; } *


yyerror

decompiled_code:
int __fastcall yyerror(const char *a1)
{
return fprintf(stderr, "%s\n", a1);
}

canonical_decompiled_code:
int __fastcall func0(const char *a1)
{
return fprintf(stderr, "%s\n", a1);
}

original_code:
void yyerror(char *s) {
    fprintf(stderr, "%s\n", s);
}

variable_types:
s
char *


decompiled_code:
_QWORD *init_temp_cmd()
{
_QWORD *result; // rax

result = malloc(0x20uLL);
cmd = (__int64)result;
*result = 0LL;
result[1] = 0LL;
result[2] = 0LL;
result[3] = 0LL;
return result;
}

canonical_decompiled_code:
_QWORD *func1()
{
_QWORD *result; // rax

result = malloc(0x20uLL);
cmd = (__int64)result;
*result = 0LL;
result[1] = 0LL;
result[2] = 0LL;
result[3] = 0LL;
return result;
}

original_code:
void init_temp_cmd() {
    cmd = malloc(sizeof(struct simple_cmd));
    cmd->argc = 0;
    cmd->args = ((void *)0);
    cmd->append = 0;
    cmd->input = ((void *)0);
    cmd->output = ((void *)0);
}

user_defined_types: struct simple_cmd { int argc; int append; char **args; char *input; char *output; }


yyparse

decompiled_code:
__int64 yyparse()
{
__int64 v0; // r12
unsigned __int64 v1; // rbx
char *v2; // rdx
__int16 *v3; // rbp
__int16 *v4; // r14
unsigned int v5; // er15
unsigned int v6; // er13
int v7; // eax
int v8; // eax
int v9; // ecx
unsigned int v10; // er13
__int64 v11; // rax
unsigned int v12; // er15
__int64 v14; // r12
__int64 v15; // r15
__int64 v16; // r13
__int64 v17; // rax
char *v18; // rdx
int v19; // eax
int v20; // edi
char *v21; // rsi
__int64 v22; // rax
unsigned int v23; // edx
unsigned __int64 v24; // rdx
__int16 *v25; // rax
signed __int64 v26; // rbp
__int64 v27; // r15
__int16 *v28; // rax
__int16 *v29; // r13
__int64 v30; // rdx
_DWORD *v31; // rcx
_QWORD *v32; // rsi
int v33; // eax
void *v34; // rax
__int64 v35; // rsi
_QWORD *v36; // rax
char *src; // [rsp+0h] [rbp-828h]
char *srca; // [rsp+0h] [rbp-828h]
int v39; // [rsp+8h] [rbp-820h]
char *v40; // [rsp+8h] [rbp-820h]
char *v41; // [rsp+10h] [rbp-818h]
char *v42; // [rsp+10h] [rbp-818h]
__int64 v43; // [rsp+10h] [rbp-818h]
__int64 v44; // [rsp+10h] [rbp-818h]
char *v45; // [rsp+18h] [rbp-810h]
__int16 ptr[200]; // [rsp+20h] [rbp-808h] BYREF
char v47[1656]; // [rsp+1B0h] [rbp-678h] BYREF

v0 = 0LL;
v1 = 200LL;
yynerrs = 0;
v2 = v47;
ptr[0] = 0;
v3 = ptr;
yychar = -2;
v4 = ptr;
v39 = 0;
src = v47;
while ( 1 )
{
v5 = yypact[v0];
v6 = v5;
if ( v5 == -13 )
goto LABEL_8;
v7 = yychar;
if ( yychar == -2 )
{
v42 = v2;
v7 = yylex();
v2 = v42;
yychar = v7;
}
if ( v7 <= 0 )
{
yychar = 0;
v8 = 0;
goto LABEL_26;
}
if ( v7 > 258 )
{
v6 = v5 + 2;
v8 = 2;
LABEL_26:
if ( v6 > 0x14 )
goto LABEL_8;
goto LABEL_27;
}
v8 = (unsigned __int8)yytranslate[v7];
v6 = v5 + v8;
if ( v5 + v8 > 0x14 )
goto LABEL_8;
LABEL_27:
if ( yycheck[v6] == v8 )
{
v0 = yytable[v6];
v9 = -yytable[v6];
if ( (int)v0 > 0 )
{
v21 = v2 + 8;
yychar = -2;
v39 = (v39 == 0) + v39 - 1;
*((_QWORD *)v2 + 1) = yylval;
goto LABEL_33;
}
goto LABEL_29;
}
LABEL_8:
v9 = (unsigned __int8)yydefact[v0];
if ( !yydefact[v0] )
{
if ( !v39 )
{
v40 = v2;
++yynerrs;
yyerror("syntax error");
v2 = v40;
goto LABEL_17;
}
if ( v39 == 3 )
{
if ( yychar > 0 )
{
yychar = -2;
goto LABEL_17;
}
if ( !yychar )
{
v3 = v4;
v10 = 1;
goto LABEL_22;
}
}
while ( 1 )
{
LABEL_17:
if ( v5 == -13 )
goto LABEL_15;
v12 = v5 + 1;
if ( v12 > 0x14 )
goto LABEL_15;
if ( yycheck[v12] == 1 )
break;
if ( v3 == v4 )
{
LABEL_21:
v10 = 1;
goto LABEL_22;
}
LABEL_16:
v11 = *--v3;
v2 -= 8;
v5 = yypact[v11];
}
v0 = yytable[v12];
if ( (int)v0 > 0 )
{
v39 = 3;
v21 = v2 + 8;
*((_QWORD *)v2 + 1) = yylval;
goto LABEL_33;
}
LABEL_15:
if ( v3 == v4 )
goto LABEL_21;
goto LABEL_16;
}
LABEL_29:
v14 = v9;
v15 = (unsigned __int8)yyr2[v9];
v16 = *(_QWORD *)&v2[8 * (1 - (unsigned __int8)yyr2[v9])];
switch ( v9 )
{
case 2:
return 0;
case 3:
case 4:
return 1;
case 6:
is_bg = 0;
goto LABEL_31;
case 7:
is_bg = 1;
goto LABEL_31;
case 8:
case 9:
v41 = v2;
v17 = create_node(cmd);
insert(head, v17);
init_temp_cmd();
v2 = v41;
goto LABEL_31;
case 11:
v31 = (_DWORD *)cmd;
v32 = *(_QWORD **)(cmd + 8);
if ( !v32 )
{
v45 = v2;
v44 = cmd;
v36 = malloc(8uLL);
v31 = (_DWORD *)v44;
v2 = v45;
v32 = v36;
*(_QWORD *)(v44 + 8) = v36;
}
*v32 = *((_QWORD *)v2 - 1);
v33 = *v31 + 1;
*v31 = v33;
v32[v33] = 0LL;
goto LABEL_31;
case 13:
if ( !*(_QWORD *)(cmd + 16) )
*(_QWORD *)(cmd + 16) = *((_QWORD *)v2 - 1);
goto LABEL_31;
case 14:
if ( !*(_QWORD *)(cmd + 24) )
*(_QWORD *)(cmd + 24) = *((_QWORD *)v2 - 1);
goto LABEL_31;
case 16:
*(_DWORD *)(cmd + 4) = 0;
goto LABEL_31;
case 17:
*(_DWORD *)(cmd + 4) = 1;
goto LABEL_31;
case 19:
v45 = v2;
v43 = cmd;
v34 = realloc(*(void **)(cmd + 8), 8LL * (*(_DWORD *)cmd + 3));
v2 = v45;
*(_QWORD *)(v43 + 8) = v34;
v35 = *(_QWORD *)(cmd + 8);
LODWORD(v34) = *(_DWORD *)cmd + 1;
*(_DWORD *)cmd = (_DWORD)v34;
*(_QWORD *)(v35 + 8LL * (int)v34) = *(_QWORD *)v2;
goto LABEL_31;
default:
LABEL_31:
v18 = &v2[-8 * v15];
v3 -= v15;
v19 = (unsigned __int8)yyr1[v14];
v20 = *v3;
*((_QWORD *)v18 + 1) = v16;
v21 = v18 + 8;
v22 = v19 - 9;
v23 = v20 + yypgoto[v22];
if ( v23 <= 0x14 && (_WORD)v20 == yycheck[v23] )
v0 = yytable[v23];
else
v0 = yydefgoto[v22];
break;
}
LABEL_33:
v24 = 2 * v1;
v25 = v3 + 1;
v3[1] = v0;
if ( v3 + 1 < &v4[v1 - 1] )
{
v2 = v21;
++v3;
goto LABEL_42;
}
v26 = (char *)v25 - (char *)v4;
v27 = v25 - v4 + 1;
if ( v1 > 0x270F )
break;
v1 = 10000LL;
if ( v24 <= 0x2710 )
v1 = v24;
v28 = (__int16 *)malloc(10 * v1 + 7);
v29 = v28;
if ( !v28 )
break;
memcpy(v28, v4, v26 + 2);
v45 = (char *)(8 * v27);
memcpy(&v29[v1], src, 8 * v27);
v30 = (__int64)v45;
if ( v4 != ptr )
{
srca = v45;
free(v4);
v30 = (__int64)srca;
}
v3 = (__int16 *)((char *)v29 + v26);
v2 = (char *)&v29[v1 - 4] + v30;
if ( v3 >= &v29[v1 - 1] )
{
v3 = v29;
v10 = 1;
goto LABEL_23;
}
src = (char *)&v29[v1];
v4 = v29;
LABEL_42:
if ( (_DWORD)v0 == 11 )
{
v3 = v4;
v10 = 0;
goto LABEL_22;
}
}
v3 = v4;
v10 = 2;
yyerror("memory exhausted");
LABEL_22:
if ( v3 != ptr )
LABEL_23:
free(v3);
return v10;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 v0; // r12
unsigned __int64 v1; // rbx
char *v2; // rdx
__int16 *v3; // rbp
__int16 *v4; // r14
unsigned int v5; // er15
unsigned int v6; // er13
int v7; // eax
int v8; // eax
int v9; // ecx
unsigned int v10; // er13
__int64 v11; // rax
unsigned int v12; // er15
__int64 v14; // r12
__int64 v15; // r15
__int64 v16; // r13
__int64 v17; // rax
char *v18; // rdx
int v19; // eax
int v20; // edi
char *v21; // rsi
__int64 v22; // rax
unsigned int v23; // edx
unsigned __int64 v24; // rdx
__int16 *v25; // rax
signed __int64 v26; // rbp
__int64 v27; // r15
__int16 *v28; // rax
__int16 *v29; // r13
__int64 v30; // rdx
_DWORD *v31; // rcx
_QWORD *v32; // rsi
int v33; // eax
void *v34; // rax
__int64 v35; // rsi
_QWORD *v36; // rax
char *src; // [rsp+0h] [rbp-828h]
char *srca; // [rsp+0h] [rbp-828h]
int v39; // [rsp+8h] [rbp-820h]
char *v40; // [rsp+8h] [rbp-820h]
char *v41; // [rsp+10h] [rbp-818h]
char *v42; // [rsp+10h] [rbp-818h]
__int64 v43; // [rsp+10h] [rbp-818h]
__int64 v44; // [rsp+10h] [rbp-818h]
char *v45; // [rsp+18h] [rbp-810h]
__int16 ptr[200]; // [rsp+20h] [rbp-808h] BYREF
char v47[1656]; // [rsp+1B0h] [rbp-678h] BYREF

v0 = 0LL;
v1 = 200LL;
yynerrs = 0;
v2 = v47;
ptr[0] = 0;
v3 = ptr;
yychar = -2;
v4 = ptr;
v39 = 0;
src = v47;
while ( 1 )
{
v5 = yypact[v0];
v6 = v5;
if ( v5 == -13 )
goto LABEL_8;
v7 = yychar;
if ( yychar == -2 )
{
v42 = v2;
v7 = yylex();
v2 = v42;
yychar = v7;
}
if ( v7 <= 0 )
{
yychar = 0;
v8 = 0;
goto LABEL_26;
}
if ( v7 > 258 )
{
v6 = v5 + 2;
v8 = 2;
LABEL_26:
if ( v6 > 0x14 )
goto LABEL_8;
goto LABEL_27;
}
v8 = (unsigned __int8)yytranslate[v7];
v6 = v5 + v8;
if ( v5 + v8 > 0x14 )
goto LABEL_8;
LABEL_27:
if ( yycheck[v6] == v8 )
{
v0 = yytable[v6];
v9 = -yytable[v6];
if ( (int)v0 > 0 )
{
v21 = v2 + 8;
yychar = -2;
v39 = (v39 == 0) + v39 - 1;
*((_QWORD *)v2 + 1) = yylval;
goto LABEL_33;
}
goto LABEL_29;
}
LABEL_8:
v9 = (unsigned __int8)yydefact[v0];
if ( !yydefact[v0] )
{
if ( !v39 )
{
v40 = v2;
++yynerrs;
func0("syntax error");
v2 = v40;
goto LABEL_17;
}
if ( v39 == 3 )
{
if ( yychar > 0 )
{
yychar = -2;
goto LABEL_17;
}
if ( !yychar )
{
v3 = v4;
v10 = 1;
goto LABEL_22;
}
}
while ( 1 )
{
LABEL_17:
if ( v5 == -13 )
goto LABEL_15;
v12 = v5 + 1;
if ( v12 > 0x14 )
goto LABEL_15;
if ( yycheck[v12] == 1 )
break;
if ( v3 == v4 )
{
LABEL_21:
v10 = 1;
goto LABEL_22;
}
LABEL_16:
v11 = *--v3;
v2 -= 8;
v5 = yypact[v11];
}
v0 = yytable[v12];
if ( (int)v0 > 0 )
{
v39 = 3;
v21 = v2 + 8;
*((_QWORD *)v2 + 1) = yylval;
goto LABEL_33;
}
LABEL_15:
if ( v3 == v4 )
goto LABEL_21;
goto LABEL_16;
}
LABEL_29:
v14 = v9;
v15 = (unsigned __int8)yyr2[v9];
v16 = *(_QWORD *)&v2[8 * (1 - (unsigned __int8)yyr2[v9])];
switch ( v9 )
{
case 2:
return 0;
case 3:
case 4:
return 1;
case 6:
is_bg = 0;
goto LABEL_31;
case 7:
is_bg = 1;
goto LABEL_31;
case 8:
case 9:
v41 = v2;
v17 = create_node(cmd);
insert(head, v17);
func1();
v2 = v41;
goto LABEL_31;
case 11:
v31 = (_DWORD *)cmd;
v32 = *(_QWORD **)(cmd + 8);
if ( !v32 )
{
v45 = v2;
v44 = cmd;
v36 = malloc(8uLL);
v31 = (_DWORD *)v44;
v2 = v45;
v32 = v36;
*(_QWORD *)(v44 + 8) = v36;
}
*v32 = *((_QWORD *)v2 - 1);
v33 = *v31 + 1;
*v31 = v33;
v32[v33] = 0LL;
goto LABEL_31;
case 13:
if ( !*(_QWORD *)(cmd + 16) )
*(_QWORD *)(cmd + 16) = *((_QWORD *)v2 - 1);
goto LABEL_31;
case 14:
if ( !*(_QWORD *)(cmd + 24) )
*(_QWORD *)(cmd + 24) = *((_QWORD *)v2 - 1);
goto LABEL_31;
case 16:
*(_DWORD *)(cmd + 4) = 0;
goto LABEL_31;
case 17:
*(_DWORD *)(cmd + 4) = 1;
goto LABEL_31;
case 19:
v45 = v2;
v43 = cmd;
v34 = realloc(*(void **)(cmd + 8), 8LL * (*(_DWORD *)cmd + 3));
v2 = v45;
*(_QWORD *)(v43 + 8) = v34;
v35 = *(_QWORD *)(cmd + 8);
LODWORD(v34) = *(_DWORD *)cmd + 1;
*(_DWORD *)cmd = (_DWORD)v34;
*(_QWORD *)(v35 + 8LL * (int)v34) = *(_QWORD *)v2;
goto LABEL_31;
default:
LABEL_31:
v18 = &v2[-8 * v15];
v3 -= v15;
v19 = (unsigned __int8)yyr1[v14];
v20 = *v3;
*((_QWORD *)v18 + 1) = v16;
v21 = v18 + 8;
v22 = v19 - 9;
v23 = v20 + yypgoto[v22];
if ( v23 <= 0x14 && (_WORD)v20 == yycheck[v23] )
v0 = yytable[v23];
else
v0 = yydefgoto[v22];
break;
}
LABEL_33:
v24 = 2 * v1;
v25 = v3 + 1;
v3[1] = v0;
if ( v3 + 1 < &v4[v1 - 1] )
{
v2 = v21;
++v3;
goto LABEL_42;
}
v26 = (char *)v25 - (char *)v4;
v27 = v25 - v4 + 1;
if ( v1 > 0x270F )
break;
v1 = 10000LL;
if ( v24 <= 0x2710 )
v1 = v24;
v28 = (__int16 *)malloc(10 * v1 + 7);
v29 = v28;
if ( !v28 )
break;
memcpy(v28, v4, v26 + 2);
v45 = (char *)(8 * v27);
memcpy(&v29[v1], src, 8 * v27);
v30 = (__int64)v45;
if ( v4 != ptr )
{
srca = v45;
free(v4);
v30 = (__int64)srca;
}
v3 = (__int16 *)((char *)v29 + v26);
v2 = (char *)&v29[v1 - 4] + v30;
if ( v3 >= &v29[v1 - 1] )
{
v3 = v29;
v10 = 1;
goto LABEL_23;
}
src = (char *)&v29[v1];
v4 = v29;
LABEL_42:
if ( (_DWORD)v0 == 11 )
{
v3 = v4;
v10 = 0;
goto LABEL_22;
}
}
v3 = v4;
v10 = 2;
func0("memory exhausted");
LABEL_22:
if ( v3 != ptr )
LABEL_23:
free(v3);
return v10;
}

original_code:
int
yyparse (void)
{
    int yystate;
    int yyerrstatus;
    yytype_int16 yyssa[200];
    yytype_int16 *yyss;
    yytype_int16 *yyssp;
    char * yyvsa[200];
    char * *yyvs;
    char * *yyvsp;
    long unsigned int yystacksize;
  int yyn;
  int yyresult;
  int yytoken = 0;
  char * yyval;
  int yylen = 0;
  yyssp = yyss = yyssa;
  yyvsp = yyvs = yyvsa;
  yystacksize = 200;
  ;
  yystate = 0;
  yyerrstatus = 0;
  yynerrs = 0;
  yychar = (-2);
  goto yysetstate;
yynewstate:
  yyssp++;
yysetstate:
  *yyssp = (yytype_int16) yystate;
  if (yyss + yystacksize - 1 <= yyssp)
    {
      long unsigned int yysize = (long unsigned int) (yyssp - yyss + 1);
      if (10000 <= yystacksize)
        goto yyexhaustedlab;
      yystacksize *= 2;
      if (10000 < yystacksize)
        yystacksize = 10000;
      {
        yytype_int16 *yyss1 = yyss;
        union yyalloc *yyptr =
          (union yyalloc *) malloc (((yystacksize) * (sizeof (yytype_int16) + sizeof (char *)) + (sizeof (union yyalloc) - 1)));
        if (! yyptr)
          goto yyexhaustedlab;
        do { long unsigned int yynewbytes; __builtin_memcpy (&yyptr->yyss_alloc, yyss, (yysize) * sizeof (*(yyss))); yyss = &yyptr->yyss_alloc; yynewbytes = yystacksize * sizeof (*yyss) + (sizeof (union yyalloc) - 1); yyptr += yynewbytes / sizeof (*yyptr); } while (0);
        do { long unsigned int yynewbytes; __builtin_memcpy (&yyptr->yyvs_alloc, yyvs, (yysize) * sizeof (*(yyvs))); yyvs = &yyptr->yyvs_alloc; yynewbytes = yystacksize * sizeof (*yyvs) + (sizeof (union yyalloc) - 1); yyptr += yynewbytes / sizeof (*yyptr); } while (0);
        if (yyss1 != yyssa)
          free (yyss1);
      }
      yyssp = yyss + yysize - 1;
      yyvsp = yyvs + yysize - 1;
      ;
      if (yyss + yystacksize - 1 <= yyssp)
        goto yyabortlab;
    }
  ;
  if (yystate == 11)
    goto yyacceptlab;
  goto yybackup;
yybackup:
  yyn = yypact[yystate];
  if ((!!((yyn) == (-13))))
    goto yydefault;
  if (yychar == (-2))
    {
      ;
      yychar = yylex ();
    }
  if (yychar <= 0)
    {
      yychar = yytoken = 0;
      ;
    }
  else
    {
      yytoken = ((unsigned) (yychar) <= 258 ? yytranslate[yychar] : 2);
      ;
    }
  yyn += yytoken;
  if (yyn < 0 || 20 < yyn || yycheck[yyn] != yytoken)
    goto yydefault;
  yyn = yytable[yyn];
  if (yyn <= 0)
    {
      if (0)
        goto yyerrlab;
      yyn = -yyn;
      goto yyreduce;
    }
  if (yyerrstatus)
    yyerrstatus--;
  ;
  yychar = (-2);
  yystate = yyn;

#pragma GCC diagnostic push

#pragma GCC diagnostic ignored "-Wuninitialized"

#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"

  *++yyvsp = yylval;

#pragma GCC diagnostic pop

  goto yynewstate;
yydefault:
  yyn = yydefact[yystate];
  if (yyn == 0)
    goto yyerrlab;
  goto yyreduce;
yyreduce:
  yylen = yyr2[yyn];
  yyval = yyvsp[1-yylen];
  ;
  switch (yyn)
    {
        case 2:
    { return 0; }
    break;
  case 3:
    { (yyerrstatus = 0);
       return 1;
   }
    break;
  case 4:
    { return 1; }
    break;
  case 6:
    { is_bg = 0; }
    break;
  case 7:
    { is_bg = 1; }
    break;
  case 8:
    {
        insert(head, create_node(cmd));
        init_temp_cmd();
    }
    break;
  case 9:
    {
        insert(head, create_node(cmd));
        init_temp_cmd();
    }
    break;
  case 11:
    {
       if (!cmd->args) cmd->args = malloc(sizeof(char *));
       cmd->args[0] = yyvsp[-1];
       cmd->argc++;
       cmd->args[cmd->argc] = ((void *)0);
   }
    break;
  case 13:
    { if (!cmd->input) cmd->input = yyvsp[-1]; }
    break;
  case 14:
    { if (!cmd->output) cmd->output = yyvsp[-1]; }
    break;
  case 16:
    { cmd->append = 0; }
    break;
  case 17:
    { cmd->append = 1; }
    break;
  case 19:
    {
       cmd->args = (char**) realloc(cmd->args, sizeof(char *) * (cmd->argc + 3));
       cmd->args[1 + (cmd->argc)++] = yyvsp[0];
   }
    break;
      default: break;
    }
  ;
  (yyvsp -= (yylen), yyssp -= (yylen));
  yylen = 0;
  ;
  *++yyvsp = yyval;
  {
    const int yylhs = yyr1[yyn] - 9;
    const int yyi = yypgoto[yylhs] + *yyssp;
    yystate = (0 <= yyi && yyi <= 20 && yycheck[yyi] == *yyssp
               ? yytable[yyi]
               : yydefgoto[yylhs]);
  }
  goto yynewstate;
yyerrlab:
  yytoken = yychar == (-2) ? (-2) : ((unsigned) (yychar) <= 258 ? yytranslate[yychar] : 2);
  if (!yyerrstatus)
    {
      ++yynerrs;
      yyerror ("syntax error");
    }
  if (yyerrstatus == 3)
    {
      if (yychar <= 0)
        {
          if (yychar == 0)
            goto yyabortlab;
        }
      else
        {
          yydestruct ("Error: discarding",
                      yytoken, &yylval);
          yychar = (-2);
        }
    }
  goto yyerrlab1;
yyerrorlab:
  if (0)
    goto yyerrorlab;
  (yyvsp -= (yylen), yyssp -= (yylen));
  yylen = 0;
  ;
  yystate = *yyssp;
  goto yyerrlab1;
yyerrlab1:
  yyerrstatus = 3;
  for (;;)
    {
      yyn = yypact[yystate];
      if (!(!!((yyn) == (-13))))
        {
          yyn += 1;
          if (0 <= yyn && yyn <= 20 && yycheck[yyn] == 1)
            {
              yyn = yytable[yyn];
              if (0 < yyn)
                break;
            }
        }
      if (yyssp == yyss)
        goto yyabortlab;
      yydestruct ("Error: popping",
                  yystos[yystate], yyvsp);
      (yyvsp -= (1), yyssp -= (1));
      yystate = *yyssp;
      ;
    }

#pragma GCC diagnostic push

#pragma GCC diagnostic ignored "-Wuninitialized"

#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"

  *++yyvsp = yylval;

#pragma GCC diagnostic pop

  ;
  yystate = yyn;
  goto yynewstate;
yyacceptlab:
  yyresult = 0;
  goto yyreturn;
yyabortlab:
  yyresult = 1;
  goto yyreturn;
yyexhaustedlab:
  yyerror ("memory exhausted");
  yyresult = 2;
yyreturn:
  if (yychar != (-2))
    {
      yytoken = ((unsigned) (yychar) <= 258 ? yytranslate[yychar] : 2);
      yydestruct ("Cleanup: discarding lookahead",
                  yytoken, &yylval);
    }
  (yyvsp -= (yylen), yyssp -= (yylen));
  ;
  while (yyssp != yyss)
    {
      yydestruct ("Cleanup: popping",
                  yystos[*yyssp], yyvsp);
      (yyvsp -= (1), yyssp -= (1));
    }
  if (yyss != yyssa)
    free (yyss);
  return yyresult;
}

canonical_original_code:
int yyparse()
{
    int yystate;
    int yyerrstatus;
    short yyssa[200];
    short *yyss;
    short *yyssp;
    char *yyvsa[200];
    char **yyvs;
    char **yyvsp;
    unsigned long yystacksize;
  int yyn;
  int yyresult;
  int yytoken = 0;
  char *yyval;
  int yylen = 0;
  yyssp = yyss = yyssa;
  yyvsp = yyvs = yyvsa;
  yystacksize = 200;
  ;
  yystate = 0;
  yyerrstatus = 0;
  yynerrs = 0;
  yychar = (-2);
  goto yysetstate;
yynewstate:
  yyssp++;
yysetstate:
  *yyssp = (short) yystate;
  if (yyss + yystacksize - 1 <= yyssp)
    {
      unsigned long yysize = (long unsigned int) (yyssp - yyss + 1);
      if (10000 <= yystacksize)
        goto yyexhaustedlab;
      yystacksize *= 2;
      if (10000 < yystacksize)
        yystacksize = 10000;
      {
        short *yyss1 = yyss;
        union yyalloc *yyptr = (union yyalloc *) malloc (((yystacksize) * (sizeof (yytype_int16) + sizeof (char *)) + (sizeof (union yyalloc) - 1)));
        if (! yyptr)
          goto yyexhaustedlab;
        do { unsigned long yynewbytes; __builtin_memcpy (&yyptr->yyss_alloc, yyss, (yysize) * sizeof (*(yyss))); yyss = &yyptr->yyss_alloc; yynewbytes = yystacksize * sizeof (*yyss) + (sizeof (union yyalloc) - 1); yyptr += yynewbytes / sizeof (*yyptr); } while (0);
        do { unsigned long yynewbytes; __builtin_memcpy (&yyptr->yyvs_alloc, yyvs, (yysize) * sizeof (*(yyvs))); yyvs = &yyptr->yyvs_alloc; yynewbytes = yystacksize * sizeof (*yyvs) + (sizeof (union yyalloc) - 1); yyptr += yynewbytes / sizeof (*yyptr); } while (0);
        if (yyss1 != yyssa)
          free (yyss1);
      }
      yyssp = yyss + yysize - 1;
      yyvsp = yyvs + yysize - 1;
      ;
      if (yyss + yystacksize - 1 <= yyssp)
        goto yyabortlab;
    }
  ;
  if (yystate == 11)
    goto yyacceptlab;
  goto yybackup;
yybackup:
  yyn = yypact[yystate];
  if ((!!((yyn) == (-13))))
    goto yydefault;
  if (yychar == (-2))
    {
      ;
      yychar = yylex ();
    }
  if (yychar <= 0)
    {
      yychar = yytoken = 0;
      ;
    }
  else
    {
      yytoken = ((unsigned int) (yychar) <= 258 ? yytranslate[yychar] : 2);
      ;
    }
  yyn += yytoken;
  if (yyn < 0 || 20 < yyn || yycheck[yyn] != yytoken)
    goto yydefault;
  yyn = yytable[yyn];
  if (yyn <= 0)
    {
      if (0)
        goto yyerrlab;
      yyn = -yyn;
      goto yyreduce;
    }
  if (yyerrstatus)
    yyerrstatus--;
  ;
  yychar = (-2);
  yystate = yyn;

#pragma GCC diagnostic push

#pragma GCC diagnostic ignored "-Wuninitialized"

#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"

  *++yyvsp = yylval;

#pragma GCC diagnostic pop

  goto yynewstate;
yydefault:
  yyn = yydefact[yystate];
  if (yyn == 0)
    goto yyerrlab;
  goto yyreduce;
yyreduce:
  yylen = yyr2[yyn];
  yyval = yyvsp[1-yylen];
  ;
  switch (yyn)
    {
        case 2:
    { return 0; }
    break;
  case 3:
    { (yyerrstatus = 0);
       return 1;
   }
    break;
  case 4:
    { return 1; }
    break;
  case 6:
    { is_bg = 0; }
    break;
  case 7:
    { is_bg = 1; }
    break;
  case 8:
    {
        insert(head, create_node(cmd));
        init_temp_cmd();
    }
    break;
  case 9:
    {
        insert(head, create_node(cmd));
        init_temp_cmd();
    }
    break;
  case 11:
    {
       if (!cmd->args) cmd->args = malloc(sizeof(char *));
       cmd->args[0] = yyvsp[-1];
       cmd->argc++;
       cmd->args[cmd->argc] = ((void *)0);
   }
    break;
  case 13:
    { if (!cmd->input) cmd->input = yyvsp[-1]; }
    break;
  case 14:
    { if (!cmd->output) cmd->output = yyvsp[-1]; }
    break;
  case 16:
    { cmd->append = 0; }
    break;
  case 17:
    { cmd->append = 1; }
    break;
  case 19:
    {
       cmd->args = (char **) realloc(cmd->args, sizeof(char *) * (cmd->argc + 3));
       cmd->args[1 + (cmd->argc)++] = yyvsp[0];
   }
    break;
      default: break;
    }
  ;
  (yyvsp -= (yylen), yyssp -= (yylen));
  yylen = 0;
  ;
  *++yyvsp = yyval;
  {
    int yylhs = yyr1[yyn] - 9;
    int yyi = yypgoto[yylhs] + *yyssp;
    yystate = (0 <= yyi && yyi <= 20 && yycheck[yyi] == *yyssp
               ? yytable[yyi]
               : yydefgoto[yylhs]);
  }
  goto yynewstate;
yyerrlab:
  yytoken = yychar == (-2) ? (-2) : ((unsigned int) (yychar) <= 258 ? yytranslate[yychar] : 2);
  if (!yyerrstatus)
    {
      ++yynerrs;
      yyerror ("syntax error");
    }
  if (yyerrstatus == 3)
    {
      if (yychar <= 0)
        {
          if (yychar == 0)
            goto yyabortlab;
        }
      else
        {
          yydestruct ("Error: discarding",
                      yytoken, &yylval);
          yychar = (-2);
        }
    }
  goto yyerrlab1;
yyerrorlab:
  if (0)
    goto yyerrorlab;
  (yyvsp -= (yylen), yyssp -= (yylen));
  yylen = 0;
  ;
  yystate = *yyssp;
  goto yyerrlab1;
yyerrlab1:
  yyerrstatus = 3;
  for (;;)
    {
      yyn = yypact[yystate];
      if (!(!!((yyn) == (-13))))
        {
          yyn += 1;
          if (0 <= yyn && yyn <= 20 && yycheck[yyn] == 1)
            {
              yyn = yytable[yyn];
              if (0 < yyn)
                break;
            }
        }
      if (yyssp == yyss)
        goto yyabortlab;
      yydestruct ("Error: popping",
                  yystos[yystate], yyvsp);
      (yyvsp -= (1), yyssp -= (1));
      yystate = *yyssp;
      ;
    }

#pragma GCC diagnostic push

#pragma GCC diagnostic ignored "-Wuninitialized"

#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"

  *++yyvsp = yylval;

#pragma GCC diagnostic pop

  ;
  yystate = yyn;
  goto yynewstate;
yyacceptlab:
  yyresult = 0;
  goto yyreturn;
yyabortlab:
  yyresult = 1;
  goto yyreturn;
yyexhaustedlab:
  yyerror ("memory exhausted");
  yyresult = 2;
yyreturn:
  if (yychar != (-2))
    {
      yytoken = ((unsigned int) (yychar) <= 258 ? yytranslate[yychar] : 2);
      yydestruct ("Cleanup: discarding lookahead",
                  yytoken, &yylval);
    }
  (yyvsp -= (yylen), yyssp -= (yylen));
  ;
  while (yyssp != yyss)
    {
      yydestruct ("Cleanup: popping",
                  yystos[*yyssp], yyvsp);
      (yyvsp -= (1), yyssp -= (1));
    }
  if (yyss != yyssa)
    free (yyss);
  return yyresult;
}

variable_types: yystate int

variable_types: yyerrstatus int

variable_types:
yyssa
short [200]

variable_types:
yyss
short *

variable_types:
yyssp
short *

variable_types:
yyvsa
char *[200]

variable_types:
yyvs
char **

variable_types:
yyvsp
char **

variable_types:
yystacksize
unsigned long

variable_types: yyn int

variable_types: yyresult int

variable_types: yytoken int

variable_types:
yyval
char *

variable_types: yylen int

variable_types:
yysize
unsigned long

variable_types: union yyalloc { short yyss_alloc; char *yyvs_alloc; } *

variable_types:
yynewbytes
unsigned long

variable_types: yylhs int

variable_types: yyi int


stop

decompiled_code:
int stop()
{
return puts("stop here");
}

canonical_decompiled_code:
int func3()
{
return puts("stop here");
}

original_code:
void stop() {
    printf("stop here\n");
}

canonical_original_code:
void stop() {
    printf("stop here\n");
}


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx

init(argc, argv, envp);
init_temp_cmd();
while ( 1 )
{
prompt();
if ( !(unsigned int)yyparse() )
{
v3 = cmd_input;
v3[strlen(cmd_input) - 1] = 0;
execute();
}
check_job_list();
*cmd_input = 0;
}
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx

init(argc, argv, envp);
func1();
while ( 1 )
{
prompt();
if ( !(unsigned int)func2() )
{
v3 = cmd_input;
v3[strlen(cmd_input) - 1] = 0;
execute();
}
check_job_list();
*cmd_input = 0;
}
}

original_code:
int main(void) {
    init();
    init_temp_cmd();
    while (1) {
        prompt();
        if (yyparse() == 0) {
            cmd_input[strlen(cmd_input) - 1] = '\0';
            execute();
        }
        check_job_list();
        cmd_input[0] = '\0';
    }
    return 0;
}

canonical_original_code:
int main() {
    init();
    init_temp_cmd();
    while (1) {
        prompt();
        if (yyparse() == 0) {
            cmd_input[strlen(cmd_input) - 1] = '\0';
            execute();
        }
        check_job_list();
        cmd_input[0] = '\0';
    }
    return 0;
}


setGoon

decompiled_code:
void setGoon()
{
goon = 1;
}

canonical_decompiled_code:
void func0()
{
goon = 1;
}

original_code:
void setGoon() {
    goon = 1;
}

canonical_original_code:
void setGoon() {
    goon = 1;
}


decompiled_code:
int __fastcall error_handler(unsigned int a1)
{
if ( a1 == 2 )
return puts("user-sh: command or filename not found");
if ( a1 == 13 )
return puts("user-sh: permission denied");
return printf("user-sh: error: %d\n", a1);
}

canonical_decompiled_code:
int __fastcall func1(unsigned int a1)
{
if ( a1 == 2 )
return puts("user-sh: command or filename not found");
if ( a1 == 13 )
return puts("user-sh: permission denied");
return printf("user-sh: error: %d\n", a1);
}

original_code:
void error_handler(int err) {
    switch (err) {
 case 2: printf("user-sh: command or filename not found\n");
   break;
 case 13: printf("user-sh: permission denied\n");
   break;
 default: printf("user-sh: error: %d\n", err);
    }
}

variable_types: err int


decompiled_code:
int __fastcall redirect2(int fd, char *file, int a3)
{
int v3; // er12

if ( fd )
v3 = open(file, a3 == 0 ? 577 : 1089, 420LL);
else
v3 = open(file, 64, 448LL);
close(fd);
return dup(v3);
}

canonical_decompiled_code:
int __fastcall func2(int fd, char *file, int a3)
{
int v3; // er12

if ( fd )
v3 = open(file, a3 == 0 ? 577 : 1089, 420LL);
else
v3 = open(file, 64, 448LL);
close(fd);
return dup(v3);
}

original_code:
void redirect2(int file_desc, char* file, int is_append) {
    int f;
    if (file_desc == 0) {
        f = open(file, 0100 | 00, (0400|0200|0100));
    } else {
        f = open(file, 0100 | 01 | (is_append?02000:01000), 0644);
    }
    close(file_desc);
    dup(f);
}

canonical_original_code:
void redirect2(int file_desc, char *file, int is_append) {
    int f;
    if (file_desc == 0) {
        f = open(file, 0100 | 00, (0400|0200|0100));
    } else {
        f = open(file, 0100 | 01 | (is_append?02000:01000), 0644);
    }
    close(file_desc);
    dup(f);
}

variable_types:
file
char *

variable_types: f int


decompiled_code:
void connect_pipe()
{
if ( count == 1 )
{
if ( len == 1 )
return;
goto LABEL_3;
}
close(0);
dup2(pre_pipe, 0);
close(pre_pipe);
close(*(&pre_pipe + 1));
if ( len != 1 && len != count )
{
LABEL_3:
close(1);
dup2(current_pipe[1], 1);
close(current_pipe[1]);
close(current_pipe[0]);
}
}

canonical_decompiled_code:
void func3()
{
if ( count == 1 )
{
if ( len == 1 )
return;
goto LABEL_3;
}
close(0);
dup2(pre_pipe, 0);
close(pre_pipe);
close(*(&pre_pipe + 1));
if ( len != 1 && len != count )
{
LABEL_3:
close(1);
dup2(current_pipe[1], 1);
close(current_pipe[1]);
close(current_pipe[0]);
}
}

original_code:
void connect_pipe() {
    if (count != 1) {
        close(0);
        dup2(pre_pipe[0], 0);
        close(pre_pipe[0]);
        close(pre_pipe[1]);
    }
    if (len != 1 && count != len) {
        close(1);
        dup2(current_pipe[1], 1);
        close(current_pipe[1]);
        close(current_pipe[0]);
    }
}


decompiled_code:
int __fastcall reset_fd(int a1)
{
dup2(STDIO[a1], a1);
return close(STDIO[a1]);
}

canonical_decompiled_code:
int __fastcall func4(int a1)
{
dup2(STDIO[a1], a1);
return close(STDIO[a1]);
}

original_code:
void reset_fd(int file_desc) {
    dup2(STDIO[file_desc], file_desc);
    close(STDIO[file_desc]);
}


decompiled_code:
__int64 __fastcall is_int_cmd(char *s2)
{
char **v1; // rbx

v1 = &INT_CMD;
while ( strcmp(*v1, s2) )
{
if ( ++v1 == &INT_CMD + 4 )
return 0LL;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(char *s2)
{
char **v1; // rbx

v1 = &INT_CMD;
while ( strcmp(*v1, s2) )
{
if ( ++v1 == &INT_CMD + 4 )
return 0LL;
}
return 1LL;
}

original_code:
int is_int_cmd(char* path) {
    int i;
    for (i = 0; i < sizeof(_INT_CMD)/sizeof((_INT_CMD)[0]); i++) {
 if (strcmp(_INT_CMD[i], path) == 0)
     return 1;
    }
    return 0;
}

canonical_original_code:
int is_int_cmd(char *path) {
    int i;
    for (i = 0; i < sizeof(_INT_CMD)/sizeof((_INT_CMD)[0]); i++) {
 if (strcmp(_INT_CMD[i], path) == 0)
     return 1;
    }
    return 0;
}

variable_types:
path
char *

variable_types: i int


decompiled_code:
int __fastcall exec_ext_command(__int64 a1)
{
int result; // eax
int *v2; // rax

result = execvp(**(const char ***)(a1 + 8), *(char *const **)(a1 + 8));
if ( result < 0 )
{
v2 = _errno_location();
result = error_handler(*v2);
}
return result;
}

canonical_decompiled_code:
int __fastcall func6(__int64 a1)
{
int result; // eax
int *v2; // rax

result = execvp(**(const char ***)(a1 + 8), *(char *const **)(a1 + 8));
if ( result < 0 )
{
v2 = _errno_location();
result = func1(*v2);
}
return result;
}

original_code:
void exec_ext_command(command cmd) {
    if (execvp(cmd->args[0], cmd->args) < 0) {
        error_handler((*__errno_location ()));
    }
}

canonical_original_code:
void exec_ext_command(struct simple_cmd *cmd) {
    if (execvp(cmd->args[0], cmd->args) < 0) {
        error_handler((*__errno_location ()));
    }
}

variable_types: struct simple_cmd { int argc; int append; char **args; char *input; char *output; } *


original_code:
void exec_int_command(command cmd) {
    char* path = cmd->args[0];
    if (strcmp(path, "jobs") == 0) {
        jobs();
    } else if (strcmp(path, "history") == 0) {
        history();
    } else if (strcmp(path, "fg") == 0) {
        fg(atoi(cmd->args[1]));
    } else if (strcmp(path, "bg") == 0) {
        bg(atoi(cmd->args[1]));
    } else if (strcmp(path, "pwd") == 0) {
        pwd();
    } else {
        printf("error internal command\n");
    }
}

canonical_original_code:
void exec_int_command(struct simple_cmd *cmd) {
    char *path = cmd->args[0];
    if (strcmp(path, "jobs") == 0) {
        jobs();
    } else if (strcmp(path, "history") == 0) {
        history();
    } else if (strcmp(path, "fg") == 0) {
        fg(atoi(cmd->args[1]));
    } else if (strcmp(path, "bg") == 0) {
        bg(atoi(cmd->args[1]));
    } else if (strcmp(path, "pwd") == 0) {
        pwd();
    } else {
        printf("error internal command\n");
    }
}


waitchild

decompiled_code:
int waitchild()
{
int result; // eax
__int64 v1; // rbp
__int64 v2; // rax
__int64 i; // rsi
__int64 j; // rbp
int stat_loc[3]; // [rsp+Ch] [rbp-Ch] BYREF

result = is_bg;
v1 = plist;
if ( is_bg )
{
add_job(plist, cmd_input);
v2 = jlist;
for ( i = 0LL; v2; i = (unsigned int)(i + 1) )
v2 = *(_QWORD *)(v2 + 24);
result = printf("[%d]\t%d\tRUNNING\t\t%s\n", i, *(unsigned int *)plist, cmd_input);
for ( j = plist; j; j = *(_QWORD *)(j + 8) )
result = kill(*(_DWORD *)j, 10);
}
else
{
while ( v1 )
{
result = waitpid(*(_DWORD *)v1, stat_loc, 2);
v1 = *(_QWORD *)(v1 + 8);
}
}
return result;
}

canonical_decompiled_code:
int func8()
{
int result; // eax
__int64 v1; // rbp
__int64 v2; // rax
__int64 i; // rsi
__int64 j; // rbp
int stat_loc[3]; // [rsp+Ch] [rbp-Ch] BYREF

result = is_bg;
v1 = plist;
if ( is_bg )
{
add_job(plist, cmd_input);
v2 = jlist;
for ( i = 0LL; v2; i = (unsigned int)(i + 1) )
v2 = *(_QWORD *)(v2 + 24);
result = printf("[%d]\t%d\tRUNNING\t\t%s\n", i, *(unsigned int *)plist, cmd_input);
for ( j = plist; j; j = *(_QWORD *)(j + 8) )
result = kill(*(_DWORD *)j, 10);
}
else
{
while ( v1 )
{
result = waitpid(*(_DWORD *)v1, stat_loc, 2);
v1 = *(_QWORD *)(v1 + 8);
}
}
return result;
}

original_code:
void waitchild() {
    struct pid_node *p = plist;
    joblist j;
    int count = 0;
    if (is_bg) {
        add_job(plist, cmd_input);
        for (count = 0, j = jlist; j != ((void *)0); j = j->next, count++);
        printf("[%d]\t%d\tRUNNING\t\t%s\n", count, plist->pid, cmd_input);
        for (p = plist; p != ((void *)0); p = p->next)
            kill(p->pid, 10);
    } else {
        for (p = plist; p != ((void *)0); p = p->next) {
            int status;
            waitpid(p->pid, &status, 2);
        }
    }
}

canonical_original_code:
void waitchild() {
    struct pid_node *p = plist;
    struct jobs_node *j;
    int count = 0;
    if (is_bg) {
        add_job(plist, cmd_input);
        for (count = 0, j = jlist; j != ((void *)0); j = j->next, count++);
        printf("[%d]\t%d\tRUNNING\t\t%s\n", count, plist->pid, cmd_input);
        for (p = plist; p != ((void *)0); p = p->next)
            kill(p->pid, 10);
    } else {
        for (p = plist; p != ((void *)0); p = p->next) {
            int status;
            waitpid(p->pid, &status, 2);
        }
    }
}

variable_types: struct jobs_node { struct pid_node { int pid; struct pid_node *next; } *plist; char *cmd; int state; struct jobs_node *next; } *

variable_types: count int

variable_types: status int

user_defined_types: struct jobs_node { struct pid_node *plist; char *cmd; int state; struct jobs_node *next; }


decompiled_code:
char *add_to_history()
{
int *v0; // rbp
int v1; // eax
__int64 v2; // rbx
char *result; // rax

v0 = (int *)envhis;
v1 = (*(_DWORD *)(envhis + 4LL) + 1) % 100;
v2 = v1;
*(_DWORD *)(envhis + 4LL) = v1;
if ( v1 == *v0 )
*v0 = (v1 + 1) % 100;
result = strdup(cmd_input);
*(_QWORD *)&v0[2 * v2 + 2] = result;
return result;
}

canonical_decompiled_code:
char *func9()
{
int *v0; // rbp
int v1; // eax
__int64 v2; // rbx
char *result; // rax

v0 = (int *)envhis;
v1 = (*(_DWORD *)(envhis + 4LL) + 1) % 100;
v2 = v1;
*(_DWORD *)(envhis + 4LL) = v1;
if ( v1 == *v0 )
*v0 = (v1 + 1) % 100;
result = strdup(cmd_input);
*(_QWORD *)&v0[2 * v2 + 2] = result;
return result;
}

original_code:
void add_to_history() {
    envhis->end = (envhis->end + 1) % 100;
    if (envhis->end == envhis->start)
        envhis->start = (envhis->start + 1) % 100;
    envhis->his_cmd[envhis->end] = strdup(cmd_input);
}


execute

original_code:
void execute() {
    cmd_list p = head->next;
    command cmd = ((void *)0);
    count = 0;
    len = length(head);
    STDIO[0] = dup(0);
    STDIO[1] = dup(1);
    add_to_history();
    while (p) {
        count++;
        cmd = p->cmd;
        pre_pipe[0] = current_pipe[0];
        pre_pipe[1] = current_pipe[1];
        if (len != 1 && count != len) {
            if (pipe(current_pipe) != 0)
                printf("error in making pipe\n");
        }
        if (cmd->input)
            redirect2(0, cmd->input, 0);
        if (cmd->output)
            redirect2(1, cmd->output, cmd->append);
        if (strcmp(cmd->args[0], "exit") == 0)
            exit(0);
        if (strcmp(cmd->args[0], "fg") == 0) {
            fg(atoi(cmd->args[1]));
        } else if (strcmp(cmd->args[0], "bg") == 0) {
            bg(atoi(cmd->args[1]));
        } else if (strcmp(cmd->args[0], "cd") == 0) {
            cd(cmd->args[1]);
        } else {
            exec_command( cmd );
        }
        if (cmd->input)
            reset_fd(0);
        if (cmd->output)
            reset_fd(1);
        if (len != 1 && count != len)
            close(current_pipe[1]);
        p = p->next;
    }
    waitchild();
    clear_plist();
    clear_list(head);
}

canonical_original_code:
void execute() {
    struct command_list *p = head->next;
    struct simple_cmd *cmd = ((void *)0);
    count = 0;
    len = length(head);
    STDIO[0] = dup(0);
    STDIO[1] = dup(1);
    add_to_history();
    while (p) {
        count++;
        cmd = p->cmd;
        pre_pipe[0] = current_pipe[0];
        pre_pipe[1] = current_pipe[1];
        if (len != 1 && count != len) {
            if (pipe(current_pipe) != 0)
                printf("error in making pipe\n");
        }
        if (cmd->input)
            redirect2(0, cmd->input, 0);
        if (cmd->output)
            redirect2(1, cmd->output, cmd->append);
        if (strcmp(cmd->args[0], "exit") == 0)
            exit(0);
        if (strcmp(cmd->args[0], "fg") == 0) {
            fg(atoi(cmd->args[1]));
        } else if (strcmp(cmd->args[0], "bg") == 0) {
            bg(atoi(cmd->args[1]));
        } else if (strcmp(cmd->args[0], "cd") == 0) {
            cd(cmd->args[1]);
        } else {
            exec_command( cmd );
        }
        if (cmd->input)
            reset_fd(0);
        if (cmd->output)
            reset_fd(1);
        if (len != 1 && count != len)
            close(current_pipe[1]);
        p = p->next;
    }
    waitchild();
    clear_plist();
    clear_list(head);
}

variable_types: struct command_list { struct simple_cmd { int argc; int append; char **args; char *input; char *output; } *cmd; struct command_list *next; } *

user_defined_types: struct command_list { struct simple_cmd *cmd; struct command_list *next; }


original_code:
void clear_temp_cmd(command cmd) {
    int i;
    for (i=0; i<cmd->argc; i++) {
 free(cmd->args[i]);
    }
    cmd->argc = 0;
    cmd->args = ((void *)0);
    cmd->append = 0;
    cmd->input = ((void *)0);
    cmd->output = ((void *)0);
}

canonical_original_code:
void clear_temp_cmd(struct simple_cmd *cmd) {
    int i;
    for (i=0; i<cmd->argc; i++) {
 free(cmd->args[i]);
    }
    cmd->argc = 0;
    cmd->args = ((void *)0);
    cmd->append = 0;
    cmd->input = ((void *)0);
    cmd->output = ((void *)0);
}


decompiled_code:
void check_job_list()
{
_QWORD *v0; // rbp
unsigned int v1; // ebx
_QWORD *v2; // r12
void *v3; // r13
void *v4; // rdi
void *v5; // rdi

v0 = jlist;
if ( jlist )
{
v1 = 1;
v2 = 0LL;
do
{
while ( *((_DWORD *)v0 + 4) != -1 )
{
v2 = v0;
v0 = (_QWORD *)v0[3];
LABEL_4:
++v1;
if ( !v0 )
return;
}
printf("[%d]\tDone\t\t%s\n", v1, (const char *)v0[1]);
v3 = (void *)v0[3];
if ( jlist == v0 )
{
v5 = v0;
v0 = (_QWORD *)v0[3];
free(v5);
jlist = v3;
goto LABEL_4;
}
v2[3] = v3;
v4 = v0;
v0 = v3;
++v1;
free(v4);
}
while ( v3 );
}
}

canonical_decompiled_code:
void func1()
{
_QWORD *v0; // rbp
unsigned int v1; // ebx
_QWORD *v2; // r12
void *v3; // r13
void *v4; // rdi
void *v5; // rdi

v0 = jlist;
if ( jlist )
{
v1 = 1;
v2 = 0LL;
do
{
while ( *((_DWORD *)v0 + 4) != -1 )
{
v2 = v0;
v0 = (_QWORD *)v0[3];
LABEL_4:
++v1;
if ( !v0 )
return;
}
printf("[%d]\tDone\t\t%s\n", v1, (const char *)v0[1]);
v3 = (void *)v0[3];
if ( jlist == v0 )
{
v5 = v0;
v0 = (_QWORD *)v0[3];
free(v5);
jlist = v3;
goto LABEL_4;
}
v2[3] = v3;
v4 = v0;
v0 = v3;
++v1;
free(v4);
}
while ( v3 );
}
}

original_code:
void check_job_list() {
    joblist pre, j, temp;
    int count = 0;
    count = 1;
    pre = ((void *)0);
    j = jlist;
    while (j != ((void *)0)) {
        if (j->state == -1) {
            printf("[%d]\tDone\t\t%s\n", count, j->cmd);
            if (j == jlist) {
                j = j->next;
                free(jlist);
                jlist = j;
            } else {
                temp = j;
                pre->next = j->next;
                j = j->next;
                free(temp);
            }
        } else {
            pre = j;
            j = j->next;
        }
        count++;
    }
}

canonical_original_code:
void check_job_list() {
    struct jobs_node *pre; struct jobs_node *j; struct jobs_node *temp;
    int count = 0;
    count = 1;
    pre = ((void *)0);
    j = jlist;
    while (j != ((void *)0)) {
        if (j->state == -1) {
            printf("[%d]\tDone\t\t%s\n", count, j->cmd);
            if (j == jlist) {
                j = j->next;
                free(jlist);
                jlist = j;
            } else {
                temp = j;
                pre->next = j->next;
                j = j->next;
                free(temp);
            }
        } else {
            pre = j;
            j = j->next;
        }
        count++;
    }
}

user_defined_types: struct pid_node { int pid; struct pid_node *next; }


decompiled_code:
char *get_current_dir()
{
char *v0; // rbx
char *v1; // r12

v0 = (char *)malloc(0x100uLL);
getcwd(v0, 0x100uLL);
do
{
v1 = v0 + 1;
v0 = strchr(v0 + 1, 47);
}
while ( v0 );
return v1;
}

canonical_decompiled_code:
char *func2()
{
char *v0; // rbx
char *v1; // r12

v0 = (char *)malloc(0x100uLL);
getcwd(v0, 0x100uLL);
do
{
v1 = v0 + 1;
v0 = strchr(v0 + 1, 47);
}
while ( v0 );
return v1;
}

original_code:
char * get_current_dir() {
    char *pwd = malloc(256 * sizeof(char));
    getcwd(pwd, 256);
    while (strstr(pwd+1, "/") != 0x0) {
     pwd = strstr(pwd+1, "/");
    }
    pwd++;
    return pwd;
}

canonical_original_code:
char *get_current_dir() {
    char *pwd = malloc(256 * sizeof(char));
    getcwd(pwd, 256);
    while (strstr(pwd+1, "/") != 0x0) {
     pwd = strstr(pwd+1, "/");
    }
    pwd++;
    return pwd;
}

variable_types:
pwd
char *

return_type: char *


decompiled_code:
__int64 init_history()
{
_QWORD *v0; // rax
void *v1; // rdi
unsigned int v2; // ecx
__int64 result; // rax

v0 = malloc(0x328uLL);
envhis = v0;
v1 = (void *)((unsigned __int64)(v0 + 2) & 0xFFFFFFFFFFFFFFF8LL);
*v0 = 0LL;
v0[1] = 0LL;
v0[100] = 0LL;
v2 = (_DWORD)v0 - (_DWORD)v1 + 808;
result = 0LL;
memset(v1, 0, 8LL * (v2 >> 3));
return result;
}

canonical_decompiled_code:
__int64 func3()
{
_QWORD *v0; // rax
void *v1; // rdi
unsigned int v2; // ecx
__int64 result; // rax

v0 = malloc(0x328uLL);
envhis = v0;
v1 = (void *)((unsigned __int64)(v0 + 2) & 0xFFFFFFFFFFFFFFF8LL);
*v0 = 0LL;
v0[1] = 0LL;
v0[100] = 0LL;
v2 = (_DWORD)v0 - (_DWORD)v1 + 808;
result = 0LL;
memset(v1, 0, 8LL * (v2 >> 3));
return result;
}

original_code:
void init_history() {
    int i;
    envhis = malloc(sizeof(struct ENV_HISTORY));
    envhis->start = 0;
    envhis->end = 0;
    for (i=0; i<100; i++) {
    envhis->his_cmd[i] = ((void *)0);
    }
}

user_defined_types: struct ENV_HISTORY { int start; int end; char *his_cmd[100]; }


prompt

decompiled_code:
int prompt()
{
char *v0; // rax

v0 = get_current_dir();
return printf("%s@%s> ", (const char *)USER, v0);
}

canonical_decompiled_code:
int func4()
{
char *v0; // rax

v0 = func2();
return printf("%s@%s> ", (const char *)USER, v0);
}

original_code:
void prompt() {
    printf("%s@%s> ", USER, get_current_dir());
}


init

decompiled_code:
int init()
{
char *v0; // rax
int result; // eax
struct sigaction v2; // [rsp+0h] [rbp-A8h] BYREF

v0 = getenv("USER");
jlist = 0LL;
USER = (__int64)v0;
cmd_input = malloc(0x400uLL);
head = init_cmd_list();
init_history();
signal(20, (__sighandler_t)&sigstp_handler);
v2.sa_handler = (__sighandler_t)&remove_job;
sigfillset(&v2.sa_mask);
v2.sa_flags = 268435462;
result = sigaction(17, &v2, 0LL);
if ( result )
result = puts("signal error");
return result;
}

canonical_decompiled_code:
int func5()
{
char *v0; // rax
int result; // eax
struct sigaction v2; // [rsp+0h] [rbp-A8h] BYREF

v0 = getenv("USER");
jlist = 0LL;
USER = (__int64)v0;
cmd_input = malloc(0x400uLL);
head = init_cmd_list();
func3();
signal(20, (__sighandler_t)&sigstp_handler);
v2.sa_handler = (__sighandler_t)&remove_job;
sigfillset(&v2.sa_mask);
v2.sa_flags = 268435462;
result = sigaction(17, &v2, 0LL);
if ( result )
result = puts("signal error");
return result;
}

original_code:
void init() {
    USER = getenv("USER");
    jlist = ((void *)0);
    cmd_input = malloc(1024 * sizeof(char));
    head = init_cmd_list();
    init_history();
    signal(20, sigstp_handler);
    struct sigaction act;
    act.__sigaction_handler.sa_sigaction = remove_job;
    sigfillset(&act.sa_mask);
    act.sa_flags = 4 | 0x10000000 | 2;
    if (sigaction(17, &act, ((void *)0))) {
        printf("signal error\n");
    }
}

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }


cd

decompiled_code:
int __fastcall cd(const char *a1)
{
int result; // eax

result = chdir(a1);
if ( result < 0 )
result = printf("%s\t the wrong path name.\n", a1);
return result;
}

canonical_decompiled_code:
int __fastcall func0(const char *a1)
{
int result; // eax

result = chdir(a1);
if ( result < 0 )
result = printf("%s\t the wrong path name.\n", a1);
return result;
}

original_code:
void cd(char* dir) {
    if(chdir(dir) < 0)
        printf("%s\t the wrong path name.\n",dir);
}

canonical_original_code:
void cd(char *dir) {
    if(chdir(dir) < 0)
        printf("%s\t the wrong path name.\n",dir);
}

variable_types:
dir
char *


history

decompiled_code:
__int64 history()
{
__int64 result; // rax
int v1; // ebp
int v2; // ecx
int v3; // ebx
int v4; // er12
__int64 v5; // rdx
__int64 v6; // rsi
int v7; // ebx
__int64 v8; // rdx
__int64 v9; // rsi
const char *v10; // rdx
int v11; // ebx
int v12; // ebp
__int64 v13; // rsi

result = envhis;
v1 = *(_DWORD *)envhis;
v2 = *(_DWORD *)(envhis + 4);
if ( *(_DWORD *)envhis <= v2 )
{
v10 = *(const char **)(envhis + 8LL * v1 + 8);
if ( !v10 )
{
if ( v2 < ++v1 )
return result;
v10 = *(const char **)(envhis + 8LL * v1 + 8);
}
v11 = v1;
v12 = 1 - v1;
while ( 1 )
{
v13 = (unsigned int)(v12 + v11++);
printf("%d\t%s\n", v13, v10);
result = envhis;
if ( *(_DWORD *)(envhis + 4) < v11 )
break;
v10 = *(const char **)(envhis + 8LL * v11 + 8);
}
}
else
{
v3 = *(_DWORD *)envhis;
if ( v1 <= 99 )
{
while ( 1 )
{
v5 = v3;
v6 = (unsigned int)(1 - v1 + v3++);
printf("%d\t%s\n", v6, *(const char **)(result + 8 * v5 + 8));
if ( v3 == 100 )
break;
result = envhis;
}
result = envhis;
v4 = 101 - v1;
v2 = *(_DWORD *)(envhis + 4);
}
else
{
v4 = 1;
}
if ( v2 >= 0 )
{
v7 = 0;
do
{
v8 = v7;
v9 = (unsigned int)(v7 + v4);
++v7;
printf("%d\t%s\n", v9, *(const char **)(result + 8 * v8 + 8));
result = envhis;
}
while ( *(_DWORD *)(envhis + 4) >= v7 );
}
}
return result;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 result; // rax
int v1; // ebp
int v2; // ecx
int v3; // ebx
int v4; // er12
__int64 v5; // rdx
__int64 v6; // rsi
int v7; // ebx
__int64 v8; // rdx
__int64 v9; // rsi
const char *v10; // rdx
int v11; // ebx
int v12; // ebp
__int64 v13; // rsi

result = envhis;
v1 = *(_DWORD *)envhis;
v2 = *(_DWORD *)(envhis + 4);
if ( *(_DWORD *)envhis <= v2 )
{
v10 = *(const char **)(envhis + 8LL * v1 + 8);
if ( !v10 )
{
if ( v2 < ++v1 )
return result;
v10 = *(const char **)(envhis + 8LL * v1 + 8);
}
v11 = v1;
v12 = 1 - v1;
while ( 1 )
{
v13 = (unsigned int)(v12 + v11++);
printf("%d\t%s\n", v13, v10);
result = envhis;
if ( *(_DWORD *)(envhis + 4) < v11 )
break;
v10 = *(const char **)(envhis + 8LL * v11 + 8);
}
}
else
{
v3 = *(_DWORD *)envhis;
if ( v1 <= 99 )
{
while ( 1 )
{
v5 = v3;
v6 = (unsigned int)(1 - v1 + v3++);
printf("%d\t%s\n", v6, *(const char **)(result + 8 * v5 + 8));
if ( v3 == 100 )
break;
result = envhis;
}
result = envhis;
v4 = 101 - v1;
v2 = *(_DWORD *)(envhis + 4);
}
else
{
v4 = 1;
}
if ( v2 >= 0 )
{
v7 = 0;
do
{
v8 = v7;
v9 = (unsigned int)(v7 + v4);
++v7;
printf("%d\t%s\n", v9, *(const char **)(result + 8 * v8 + 8));
result = envhis;
}
while ( *(_DWORD *)(envhis + 4) >= v7 );
}
}
return result;
}

original_code:
void history() {
    int i,j=1;
    int start =0;
    if(envhis->start <= envhis->end)
    {
        if(envhis->his_cmd[envhis->start] == ((void *)0))
            start = envhis->start+1;
        else
            start = envhis->start;
        for(i=start;i <= envhis->end;i++)
        {
            printf("%d\t%s\n",j,envhis->his_cmd[i]);
            j++;
        }
    }
    else
    {
        for(i=envhis->start;i < 100;i++)
        {
            printf("%d\t%s\n",j,envhis->his_cmd[i]);
            j++;
        }
        for(i=0;i <= envhis->end;i++)
        {
            printf("%d\t%s\n",j,envhis->his_cmd[i]);
            j++;
        }
    }
}

canonical_original_code:
void history() {
    int i; int j = 1;
    int start = 0;
    if(envhis->start <= envhis->end)
    {
        if(envhis->his_cmd[envhis->start] == ((void *)0))
            start = envhis->start+1;
        else
            start = envhis->start;
        for(i=start;i <= envhis->end;i++)
        {
            printf("%d\t%s\n",j,envhis->his_cmd[i]);
            j++;
        }
    }
    else
    {
        for(i=envhis->start;i < 100;i++)
        {
            printf("%d\t%s\n",j,envhis->his_cmd[i]);
            j++;
        }
        for(i=0;i <= envhis->end;i++)
        {
            printf("%d\t%s\n",j,envhis->his_cmd[i]);
            j++;
        }
    }
}

variable_types: j int

variable_types: start int


jobs

decompiled_code:
int jobs()
{
unsigned int v0; // ebp
__int64 v1; // rbx
int v2; // eax
const char *v3; // rdx
int result; // eax

v0 = 1;
v1 = jlist;
if ( !jlist )
return printf("There is no jobs!");
do
{
v2 = *(_DWORD *)(v1 + 16);
v3 = "RUNNING";
if ( v2 != 1 )
{
v3 = "STOPPED";
if ( v2 )
v3 = "DONE";
}
result = printf("[%d]\t%s\t\t%s\n", v0, v3, *(const char **)(v1 + 8));
v1 = *(_QWORD *)(v1 + 24);
++v0;
}
while ( v1 );
return result;
}

canonical_decompiled_code:
int func2()
{
unsigned int v0; // ebp
__int64 v1; // rbx
int v2; // eax
const char *v3; // rdx
int result; // eax

v0 = 1;
v1 = jlist;
if ( !jlist )
return printf("There is no jobs!");
do
{
v2 = *(_DWORD *)(v1 + 16);
v3 = "RUNNING";
if ( v2 != 1 )
{
v3 = "STOPPED";
if ( v2 )
v3 = "DONE";
}
result = printf("[%d]\t%s\t\t%s\n", v0, v3, *(const char **)(v1 + 8));
v1 = *(_QWORD *)(v1 + 24);
++v0;
}
while ( v1 );
return result;
}

original_code:
void jobs() {
    int i = 0;
    struct pid_node* p;
    joblist j;
    if (jlist == ((void *)0))
        printf("There is no jobs!");
    else {
        for (i=1, j = jlist; j != ((void *)0); j = j->next, i++) {
           printf("[%d]\t%s\t\t%s\n", i, j->state==1?"RUNNING":j->state==0?"STOPPED":"DONE", j->cmd);
        }
    }
}

canonical_original_code:
void jobs() {
    int i = 0;
    struct pid_node *p;
    struct jobs_node *j;
    if (jlist == ((void *)0))
        printf("There is no jobs!");
    else {
        for (i=1, j = jlist; j != ((void *)0); j = j->next, i++) {
           printf("[%d]\t%s\t\t%s\n", i, j->state==1?"RUNNING":j->state==0?"STOPPED":"DONE", j->cmd);
        }
    }
}

variable_types: struct pid_node { int pid; struct pid_node *next; } *