decompiled_code:
unsigned __int64 __fastcall spacemouse_device_get_led(__int64 a1)
{
unsigned __int64 result; // rax
unsigned __int64 v2; // [rsp+8h] [rbp-10h] BYREF

v2 = 0LL;
if ( ioctl(*(_DWORD *)(a1 + 4), 0x800F4519uLL, &v2) == -1 )
result = (unsigned int)-*_errno_location();
else
result = (v2 >> 8) & 1;
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func4(__int64 a1)
{
unsigned __int64 result; // rax
unsigned __int64 v2; // [rsp+8h] [rbp-10h] BYREF

v2 = 0LL;
if ( ioctl(*(_DWORD *)(a1 + 4), 0x800F4519uLL, &v2) == -1 )
result = (unsigned int)-*_errno_location();
else
result = (v2 >> 8) & 1;
return result;
}

variable_types:
bits
unsigned long [-1]


repo: polyphemus/libspacemouse

decompiled_code:
int __fastcall spacemouse_device_close(__int64 a1)
{
int result; // eax

result = close(*(_DWORD *)(a1 + 4));
*(_DWORD *)(a1 + 4) = -1;
if ( result == -1 )
result = -*_errno_location();
return result;
}

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

result = close(*(_DWORD *)(a1 + 4));
*(_DWORD *)(a1 + 4) = -1;
if ( result == -1 )
result = -*_errno_location();
return result;
}

original_code:
int spacemouse_device_close(struct spacemouse *mouse)
{
  int ret = close(mouse->fd);
  mouse->fd = -1;
  return ret == -1 ? -(*__errno_location ()) : ret;
}

variable_types: struct spacemouse { int id; int fd; void *data; char *devnode; char *manufacturer; char *product; struct spacemouse_buf { struct spacemouse_event_motion { int type; int x; int rx; unsigned int period; } motion; struct timeval { long tv_sec; long tv_usec; } time; } buf; struct spacemouse *next; } *

variable_types: ret int

return_type: int

user_defined_types: struct spacemouse { int id; int fd; void *data; char *devnode; char *manufacturer; char *product; struct spacemouse_buf buf; struct spacemouse *next; }

user_defined_types: struct spacemouse_buf { struct spacemouse_event_motion motion; struct timeval time; }


repo: poomon/obcpufreq

decompiled_code:
int __fastcall get_human_speed(char *a1, unsigned __int64 a2)
{
int result; // eax

if ( a2 <= 0xF4240 )
{
if ( a2 <= 0x186A0 )
{
if ( a2 <= 0x3E8 )
{
result = snprintf(a1, 0x14uLL, "%lu kHz", a2);
}
else
{
if ( a2
- 20 * (a2 / 0x64 + (((0x28F5C28F5C28F5C3LL * (unsigned __int128)(a2 >> 2)) >> 64) & 0xFFFFFFFFFFFFFFFCLL)) > 0x31 )
a2 += 100LL;
result = snprintf(a1, 0x14uLL, "%u.%01u MHz", (unsigned int)a2 / 0x3E8uLL, a2 % 0x3E8 / 0x64);
}
}
else
{
if ( a2 % 0x3E8 > 0x1F3 )
LODWORD(a2) = a2 + 1000;
result = snprintf(a1, 0x14uLL, "%u MHz", (unsigned int)a2 / 0x3E8uLL);
}
}
else
{
if ( a2 % 0x2710 > 0x1387 )
a2 += 10000LL;
result = snprintf(a1, 0x14uLL, "%u.%02u GHz", (unsigned int)a2 / 0xF4240uLL, a2 % 0xF4240 / 0x2710);
}
return result;
}

canonical_decompiled_code:
int __fastcall func8(char *a1, unsigned __int64 a2)
{
int result; // eax

if ( a2 <= 0xF4240 )
{
if ( a2 <= 0x186A0 )
{
if ( a2 <= 0x3E8 )
{
result = snprintf(a1, 0x14uLL, "%lu kHz", a2);
}
else
{
if ( a2
- 20 * (a2 / 0x64 + (((0x28F5C28F5C28F5C3LL * (unsigned __int128)(a2 >> 2)) >> 64) & 0xFFFFFFFFFFFFFFFCLL)) > 0x31 )
a2 += 100LL;
result = snprintf(a1, 0x14uLL, "%u.%01u MHz", (unsigned int)a2 / 0x3E8uLL, a2 % 0x3E8 / 0x64);
}
}
else
{
if ( a2 % 0x3E8 > 0x1F3 )
LODWORD(a2) = a2 + 1000;
result = snprintf(a1, 0x14uLL, "%u MHz", (unsigned int)a2 / 0x3E8uLL);
}
}
else
{
if ( a2 % 0x2710 > 0x1387 )
a2 += 10000LL;
result = snprintf(a1, 0x14uLL, "%u.%02u GHz", (unsigned int)a2 / 0xF4240uLL, a2 % 0xF4240 / 0x2710);
}
return result;
}

original_code:
void get_human_speed(char* dest, unsigned long speed)
{
    unsigned long tmp;
    if (speed > 1000000) {
        tmp = speed % 10000;
        if (tmp >= 5000)
            speed += 10000;
        snprintf (dest, 20, "%u.%02u GHz", ((unsigned int) speed/1000000),
            ((unsigned int) (speed%1000000)/10000));
    } else if (speed > 100000) {
        tmp = speed % 1000;
        if (tmp >= 500)
            speed += 1000;
        snprintf (dest, 20, "%u MHz", ((unsigned int) speed / 1000));
    } else if (speed > 1000) {
        tmp = speed % 100;
        if (tmp >= 50)
            speed += 100;
        snprintf (dest, 20, "%u.%01u MHz", ((unsigned int) speed/1000),
            ((unsigned int) (speed%1000)/100));
    } else
        snprintf (dest, 20, "%lu kHz", speed);
    return;
}

canonical_original_code:
void get_human_speed(char *dest, unsigned long speed)
{
    unsigned long tmp;
    if (speed > 1000000) {
        tmp = speed % 10000;
        if (tmp >= 5000)
            speed += 10000;
        snprintf (dest, 20, "%u.%02u GHz", ((unsigned int) speed/1000000),
            ((unsigned int) (speed%1000000)/10000));
    } else if (speed > 100000) {
        tmp = speed % 1000;
        if (tmp >= 500)
            speed += 1000;
        snprintf (dest, 20, "%u MHz", ((unsigned int) speed / 1000));
    } else if (speed > 1000) {
        tmp = speed % 100;
        if (tmp >= 50)
            speed += 100;
        snprintf (dest, 20, "%u.%01u MHz", ((unsigned int) speed/1000),
            ((unsigned int) (speed%1000)/100));
    } else
        snprintf (dest, 20, "%lu kHz", speed);
    return;
}

variable_types:
dest
char *

variable_types:
speed
unsigned long

variable_types:
tmp
unsigned long

return_type: void


decompiled_code:
void __fastcall print_cpu(unsigned int a1, unsigned int a2)
{
unsigned int v2; // ebp
struct cpufreq_policy *v3; // r13
struct cpufreq_available_governors *v4; // rbx
struct cpufreq_available_frequencies *v5; // rbx
unsigned int v6; // er14
unsigned __int64 v7; // rbp
char v8; // al
unsigned __int64 v9; // [rsp+8h] [rbp-A0h]
char v10[32]; // [rsp+10h] [rbp-98h] BYREF
char v11; // [rsp+30h] [rbp-78h] BYREF
char v12; // [rsp+31h] [rbp-77h]
char v13[88]; // [rsp+50h] [rbp-58h] BYREF

v2 = a1 + 1;
if ( a1 + 1 == a2 )
{
printf("<item label='cpu %u'/>\n", a1);
}
else if ( v2 < a2 )
{
printf("<menu label='cpu %u' id='obcpufreq-cpu%u'>\n", a1, v2);
print_cpu(v2, a2);
puts("</menu>");
}
puts(" <separator/>");
v9 = cpufreq_get_freq_kernel(a1);
v3 = cpufreq_get_policy(a1);
v4 = cpufreq_get_available_governors(a1);
if ( v4 )
{
printf("<menu label='governors' id='obcpufreq-governors%u'>\n", a1);
do
{
v11 = 32;
v12 = 0;
if ( !strcmp(v4->governor, v3->governor) )
v11 = 42;
printf(
" <item label='%s%s'><action name='Execute'><command>sudo -n cpufreq-set --cpu %u -g %s</command></action></item>\n",
&v11,
v4->governor,
a1,
v4->governor);
v4 = v4->next;
}
while ( v4 );
puts("</menu>");
cpufreq_put_available_governors(0LL);
}
else
{
printf("<item label='%s'/>\n", "no available governors");
}
puts(" <separator/>");
v5 = cpufreq_get_available_frequencies(a1);
if ( v5 )
{
v6 = 0;
do
{
v7 = v5->frequency;
get_human_speed(v13, v5->frequency);
v12 = 0;
v8 = 32;
if ( v9 == v7 )
v8 = 42;
v11 = v8;
v10[0] = 0;
if ( v3 )
{
if ( v3->max == v7 )
{
if ( v3->min == v7 )
strcpy(v10, " (max,min)");
else
strcpy(v10, " (max)");
}
else if ( v3->min == v7 )
{
strcpy(v10, " (min)");
}
}
printf(
"<menu label='%s%s%s' id='obcpufreq-freq%d'>\n"
" <item label='set as max'><action name='Execute'><command>sudo -n cpufreq-set --cpu %u --max %lu</command></act"
"ion></item>\n"
" <item label='set as min'><action name='Execute'><command>sudo -n cpufreq-set --cpu %u --min %lu</command></act"
"ion></item>\n"
"</menu>\n",
&v11,
v13,
v10,
v6,
a1,
v5->frequency,
a1,
v5->frequency);
v5 = v5->next;
++v6;
}
while ( v5 );
cpufreq_put_available_frequencies(0LL);
}
else
{
printf("<item label='%s'/>\n", "no available freqs");
}
cpufreq_put_policy(v3);
}

canonical_decompiled_code:
void __fastcall func9(unsigned int a1, unsigned int a2)
{
unsigned int v2; // ebp
struct cpufreq_policy *v3; // r13
struct cpufreq_available_governors *v4; // rbx
struct cpufreq_available_frequencies *v5; // rbx
unsigned int v6; // er14
unsigned __int64 v7; // rbp
char v8; // al
unsigned __int64 v9; // [rsp+8h] [rbp-A0h]
char v10[32]; // [rsp+10h] [rbp-98h] BYREF
char v11; // [rsp+30h] [rbp-78h] BYREF
char v12; // [rsp+31h] [rbp-77h]
char v13[88]; // [rsp+50h] [rbp-58h] BYREF

v2 = a1 + 1;
if ( a1 + 1 == a2 )
{
printf("<item label='cpu %u'/>\n", a1);
}
else if ( v2 < a2 )
{
printf("<menu label='cpu %u' id='obcpufreq-cpu%u'>\n", a1, v2);
func9(v2, a2);
puts("</menu>");
}
puts(" <separator/>");
v9 = cpufreq_get_freq_kernel(a1);
v3 = cpufreq_get_policy(a1);
v4 = cpufreq_get_available_governors(a1);
if ( v4 )
{
printf("<menu label='governors' id='obcpufreq-governors%u'>\n", a1);
do
{
v11 = 32;
v12 = 0;
if ( !strcmp(v4->governor, v3->governor) )
v11 = 42;
printf(
" <item label='%s%s'><action name='Execute'><command>sudo -n cpufreq-set --cpu %u -g %s</command></action></item>\n",
&v11,
v4->governor,
a1,
v4->governor);
v4 = v4->next;
}
while ( v4 );
puts("</menu>");
cpufreq_put_available_governors(0LL);
}
else
{
printf("<item label='%s'/>\n", "no available governors");
}
puts(" <separator/>");
v5 = cpufreq_get_available_frequencies(a1);
if ( v5 )
{
v6 = 0;
do
{
v7 = v5->frequency;
func8(v13, v5->frequency);
v12 = 0;
v8 = 32;
if ( v9 == v7 )
v8 = 42;
v11 = v8;
v10[0] = 0;
if ( v3 )
{
if ( v3->max == v7 )
{
if ( v3->min == v7 )
strcpy(v10, " (max,min)");
else
strcpy(v10, " (max)");
}
else if ( v3->min == v7 )
{
strcpy(v10, " (min)");
}
}
printf(
"<menu label='%s%s%s' id='obcpufreq-freq%d'>\n"
" <item label='set as max'><action name='Execute'><command>sudo -n cpufreq-set --cpu %u --max %lu</command></act"
"ion></item>\n"
" <item label='set as min'><action name='Execute'><command>sudo -n cpufreq-set --cpu %u --min %lu</command></act"
"ion></item>\n"
"</menu>\n",
&v11,
v13,
v10,
v6,
a1,
v5->frequency,
a1,
v5->frequency);
v5 = v5->next;
++v6;
}
while ( v5 );
cpufreq_put_available_frequencies(0LL);
}
else
{
printf("<item label='%s'/>\n", "no available freqs");
}
cpufreq_put_policy(v3);
}

original_code:
void print_cpu(unsigned int cpu, unsigned int n_cpus)
{
    int i;
    char str[20];
    char premark[20];
    char postmark[20];
    unsigned long curfreq, freq;
    struct cpufreq_available_frequencies* freqs;
    struct cpufreq_available_governors* govs;
    struct cpufreq_policy* policy;
    if (cpu + 1 == n_cpus)
        printf("<item label='cpu %u'/>\n", cpu);
    else if (cpu + 1 < n_cpus)
    {
        printf("<menu label='cpu %u' id='obcpufreq-cpu%u'>\n", cpu, cpu + 1);
        print_cpu(cpu + 1, n_cpus);
        printf("</menu>\n");
    }
    printf(" <separator/>\n");
    curfreq = cpufreq_get_freq_kernel(cpu);
    policy = cpufreq_get_policy(cpu);
    govs = cpufreq_get_available_governors(cpu);
    if (govs)
    {
        printf("<menu label='governors' id='obcpufreq-governors%u'>\n", cpu);
        for (; govs; govs = govs->next)
        {
            premark[0] = ' ';
            premark[1] = '\0';
            if (!strcmp(govs->governor, policy->governor))
                premark[0] = '*';
            printf("  <item label='%s%s'><action name='Execute'><command>"
                   "sudo -n cpufreq-set" " --cpu %u -g %s</command></action></item>\n",
                   premark, govs->governor,
                   cpu, govs->governor);
        }
        printf("</menu>\n");
        cpufreq_put_available_governors(govs);
    }
    else
        printf("<item label='%s'/>\n", "no available governors");
    printf(" <separator/>\n");
    freqs = cpufreq_get_available_frequencies(cpu);
    if (freqs)
    {
        for (i = 0; freqs; freqs = freqs->next, i++)
        {
            freq = freqs->frequency;
            get_human_speed(str, freq);
            premark[0] = ' ';
            premark[1] = '\0';
            if (freq == curfreq)
                premark[0] = '*';
            postmark[0] = '\0';
            if (policy)
            {
                if (freq == policy->max && freq == policy->min)
                    strcpy(postmark, " (max,min)");
                else if (freq == policy->max)
                    strcpy(postmark, " (max)");
                else if (freq == policy->min)
                    strcpy(postmark, " (min)");
            }
            printf("<menu label='%s%s%s' id='obcpufreq-freq%d'>\n"
                   "  <item label='set as max'><action name='Execute'><command>"
                       "sudo -n cpufreq-set" " --cpu %u --max %lu</command></action></item>\n"
                   "  <item label='set as min'><action name='Execute'><command>"
                       "sudo -n cpufreq-set" " --cpu %u --min %lu</command></action></item>\n</menu>\n",
                   premark, str, postmark, i,
                   cpu, freqs->frequency,
                   cpu, freqs->frequency);
        }
        cpufreq_put_available_frequencies(freqs);
    }
    else
        printf("<item label='%s'/>\n", "no available freqs");
    cpufreq_put_policy(policy);
    return;
}

canonical_original_code:
void print_cpu(unsigned int cpu, unsigned int n_cpus)
{
    int i;
    char str[20];
    char premark[20];
    char postmark[20];
    unsigned long curfreq; unsigned long freq;
    struct cpufreq_available_frequencies *freqs;
    struct cpufreq_available_governors *govs;
    struct cpufreq_policy *policy;
    if (cpu + 1 == n_cpus)
        printf("<item label='cpu %u'/>\n", cpu);
    else if (cpu + 1 < n_cpus)
    {
        printf("<menu label='cpu %u' id='obcpufreq-cpu%u'>\n", cpu, cpu + 1);
        print_cpu(cpu + 1, n_cpus);
        printf("</menu>\n");
    }
    printf(" <separator/>\n");
    curfreq = cpufreq_get_freq_kernel(cpu);
    policy = cpufreq_get_policy(cpu);
    govs = cpufreq_get_available_governors(cpu);
    if (govs)
    {
        printf("<menu label='governors' id='obcpufreq-governors%u'>\n", cpu);
        for (; govs; govs = govs->next)
        {
            premark[0] = ' ';
            premark[1] = '\0';
            if (!strcmp(govs->governor, policy->governor))
                premark[0] = '*';
            printf("  <item label='%s%s'><action name='Execute'><command>"
                   "sudo -n cpufreq-set" " --cpu %u -g %s</command></action></item>\n",
                   premark, govs->governor,
                   cpu, govs->governor);
        }
        printf("</menu>\n");
        cpufreq_put_available_governors(govs);
    }
    else
        printf("<item label='%s'/>\n", "no available governors");
    printf(" <separator/>\n");
    freqs = cpufreq_get_available_frequencies(cpu);
    if (freqs)
    {
        for (i = 0; freqs; freqs = freqs->next, i++)
        {
            freq = freqs->frequency;
            get_human_speed(str, freq);
            premark[0] = ' ';
            premark[1] = '\0';
            if (freq == curfreq)
                premark[0] = '*';
            postmark[0] = '\0';
            if (policy)
            {
                if (freq == policy->max && freq == policy->min)
                    strcpy(postmark, " (max,min)");
                else if (freq == policy->max)
                    strcpy(postmark, " (max)");
                else if (freq == policy->min)
                    strcpy(postmark, " (min)");
            }
            printf("<menu label='%s%s%s' id='obcpufreq-freq%d'>\n"
                   "  <item label='set as max'><action name='Execute'><command>"
                       "sudo -n cpufreq-set" " --cpu %u --max %lu</command></action></item>\n"
                   "  <item label='set as min'><action name='Execute'><command>"
                       "sudo -n cpufreq-set" " --cpu %u --min %lu</command></action></item>\n</menu>\n",
                   premark, str, postmark, i,
                   cpu, freqs->frequency,
                   cpu, freqs->frequency);
        }
        cpufreq_put_available_frequencies(freqs);
    }
    else
        printf("<item label='%s'/>\n", "no available freqs");
    cpufreq_put_policy(policy);
    return;
}

variable_types:
cpu
unsigned int

variable_types: i int

variable_types:
str
char [20]

variable_types:
premark
char [20]

variable_types:
postmark
char [20]

variable_types:
curfreq
unsigned long

variable_types:
freq
unsigned long

variable_types: struct cpufreq_available_frequencies { unsigned long frequency; struct cpufreq_available_frequencies *next; struct cpufreq_available_frequencies *first; } *

variable_types: struct cpufreq_available_governors { char *governor; struct cpufreq_available_governors *next; struct cpufreq_available_governors *first; } *

variable_types: struct cpufreq_policy { unsigned long min; unsigned long max; char *governor; } *


main

canonical_name: main

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

puts("<?xml version='1.0' encoding='UTF-8'?>\n<openbox_pipe_menu>");
v3 = count_cpus("<?xml version='1.0' encoding='UTF-8'?>\n<openbox_pipe_menu>", argv);
print_cpu(0, v3);
puts("</openbox_pipe_menu>");
return 0;
}

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

puts("<?xml version='1.0' encoding='UTF-8'?>\n<openbox_pipe_menu>");
v3 = func7("<?xml version='1.0' encoding='UTF-8'?>\n<openbox_pipe_menu>", argv);
func9(0, v3);
puts("</openbox_pipe_menu>");
return 0;
}

original_code:
int main(int argc, char** argv)
{
    printf("<?xml version='1.0' encoding='UTF-8'?>\n<openbox_pipe_menu>\n");
    print_cpu(0, count_cpus());
    printf("</openbox_pipe_menu>\n");
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    printf("<?xml version='1.0' encoding='UTF-8'?>\n<openbox_pipe_menu>\n");
    print_cpu(0, count_cpus());
    printf("</openbox_pipe_menu>\n");
    return 0;
}

variable_types: argc int

variable_types:
argv
char **


decompiled_code:
_BOOL8 __fastcall ctype_digit(int a1)
{
return (unsigned int)(a1 - 48) <= 9;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(int a1)
{
return (unsigned int)(a1 - 48) <= 9;
}

original_code: static inline int ctype_digit(int c) { return c>='0' && c<='9'; }

variable_types: c int


decompiled_code:
_BOOL8 __fastcall ctype_alpha(int a1)
{
return (a1 & 0xFFFFFFDF) - 65 <= 0x19;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(int a1)
{
return (a1 & 0xFFFFFFDF) - 65 <= 0x19;
}

original_code:
static inline int ctype_alpha(int c) {
  return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}


canonical_original_code:
static enum adns_status mkquery_header(struct adns__state *ads, struct vbuf *vb, int *id_r, int qdlen) {
  int id;
  unsigned char *rqp;
  if (!adns__vbuf_ensure(vb,12 +qdlen+4)) return adns_s_nomemory;
  vb->used= 0;
  (rqp= (vb)->buf+(vb)->used);
  *id_r= id= (ads->nextid++) & 0x0ffff;
  (*rqp++= (((id)>>8)&0x0ff), *rqp++= ((id)&0x0ff));
  *rqp++= (0x01);
  *rqp++= (0x00);
  (*rqp++= (((1)>>8)&0x0ff), *rqp++= ((1)&0x0ff));
  (*rqp++= (((0)>>8)&0x0ff), *rqp++= ((0)&0x0ff));
  (*rqp++= (((0)>>8)&0x0ff), *rqp++= ((0)&0x0ff));
  (*rqp++= (((0)>>8)&0x0ff), *rqp++= ((0)&0x0ff));
  ((vb)->used= rqp-(vb)->buf);
  return adns_s_ok;
}

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:
vb
struct vbuf *

variable_types: qdlen int

variable_types: id int

variable_types:
rqp
unsigned char *

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 query_queue { struct adns__query *head; }

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


timevaladd

original_code:
static inline void timevaladd(struct timeval *tv_io, long ms) {
  struct timeval tmp;
  ((void) sizeof ((ms>=0) ? 1 : 0), __extension__ ({ if (ms>=0) ; else __assert_fail ("ms>=0", "tvarith.h", 32, __extension__ __PRETTY_FUNCTION__); }));
  tmp= *tv_io;
  tmp.tv_usec += (ms%1000)*1000;
  tmp.tv_sec += ms/1000;
  if (tmp.tv_usec >= 1000000) { tmp.tv_sec++; tmp.tv_usec -= 1000000; }
  *tv_io= tmp;
}

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

variable_types: ms long


original_code:
adns_status adns__qdpl_normal(adns_state ads,
         const char **p_io, const char *pe, int labelnum,
         char label_r[], int *ll_io,
         adns_queryflags flags,
         const typeinfo *typei) {
  int ll, c;
  const char *p;
  ll= 0;
  p= *p_io;
  while (p!=pe && (c= *p++)!='.') {
    if (c=='\\') {
      if (!(flags & adns_qf_quoteok_query)) return adns_s_querydomaininvalid;
      if (ctype_digit(p[0])) {
 if (p+1==pe || p+2==pe) return adns_s_querydomaininvalid;
 if (ctype_digit(p[1]) && ctype_digit(p[2])) {
   c= (*p++ - '0')*100;
   c += (*p++ - '0')*10;
   c += (*p++ - '0');
   if (c >= 256) return adns_s_querydomaininvalid;
 } else {
   return adns_s_querydomaininvalid;
 }
      } else if (!(c= *p++)) {
 return adns_s_querydomaininvalid;
      }
    }
    if (!(flags & adns_qf_quoteok_query)) {
      if (c == '-') {
 if (!ll) return adns_s_querydomaininvalid;
      } else if (!ctype_alpha(c) && !ctype_digit(c)) {
 return adns_s_querydomaininvalid;
      }
    }
    if (ll == *ll_io) return adns_s_querydomaininvalid;
    label_r[ll++]= c;
  }
  *p_io= p;
  *ll_io= ll;
  return adns_s_ok;
}

canonical_original_code:
enum adns_status adns__qdpl_normal(struct adns__state *ads, char **p_io, char *pe, int labelnum, char label_r[], int *ll_io, enum adns_queryflags flags, struct typeinfo *typei) {
  int ll; int c;
  char *p;
  ll= 0;
  p= *p_io;
  while (p!=pe && (c= *p++)!='.') {
    if (c=='\\') {
      if (!(flags & adns_qf_quoteok_query)) return adns_s_querydomaininvalid;
      if (ctype_digit(p[0])) {
 if (p+1==pe || p+2==pe) return adns_s_querydomaininvalid;
 if (ctype_digit(p[1]) && ctype_digit(p[2])) {
   c= (*p++ - '0')*100;
   c += (*p++ - '0')*10;
   c += (*p++ - '0');
   if (c >= 256) return adns_s_querydomaininvalid;
 } else {
   return adns_s_querydomaininvalid;
 }
      } else if (!(c= *p++)) {
 return adns_s_querydomaininvalid;
      }
    }
    if (!(flags & adns_qf_quoteok_query)) {
      if (c == '-') {
 if (!ll) return adns_s_querydomaininvalid;
      } else if (!ctype_alpha(c) && !ctype_digit(c)) {
 return adns_s_querydomaininvalid;
      }
    }
    if (ll == *ll_io) return adns_s_querydomaininvalid;
    label_r[ll++]= c;
  }
  *p_io= p;
  *ll_io= ll;
  return adns_s_ok;
}

variable_types:
pe
char *

variable_types: labelnum int

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

variable_types:
typei
struct typeinfo *

variable_types: ll int

variable_types:
p
char *

return_type: 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_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: 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 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: 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 timeval { long tv_sec; long tv_usec; }

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


original_code:
adns_status adns__mkquery(adns_state ads, vbuf *vb, int *id_r,
     const char *owner, int ol,
     const typeinfo *typei, adns_rrtype type,
     adns_queryflags flags) {
  int labelnum, ll, nbytes;
  byte label[255];
  byte *rqp;
  const char *p, *pe;
  adns_status st;
  st= mkquery_header(ads,vb,id_r,ol+2); if (st) return st;
  (rqp= (vb)->buf+(vb)->used);
  p= owner; pe= owner+ol;
  nbytes= 0;
  labelnum= 0;
  while (p!=pe) {
    ll= sizeof(label);
    st= typei->qdparselabel(ads, &p,pe, labelnum++, label, &ll, flags, typei);
    if (st) return st;
    if (!ll) return adns_s_querydomaininvalid;
    if (ll > 63) return adns_s_querydomaintoolong;
    nbytes+= ll+1;
    if (nbytes >= 255) return adns_s_querydomaintoolong;
    *rqp++= (ll);
    memcpy(rqp,label,ll); rqp+= ll;
  }
  *rqp++= (0);
  ((vb)->used= rqp-(vb)->buf);
  st= mkquery_footer(vb,type);
  return adns_s_ok;
}

canonical_original_code:
enum adns_status adns__mkquery(struct adns__state *ads, struct vbuf *vb, int *id_r, char *owner, int ol, struct typeinfo *typei, enum adns_rrtype type, enum adns_queryflags flags) {
  int labelnum; int ll; int nbytes;
  unsigned char label[255];
  unsigned char *rqp;
  char *p; char *pe;
  enum adns_status st;
  st= mkquery_header(ads,vb,id_r,ol+2); if (st) return st;
  (rqp= (vb)->buf+(vb)->used);
  p= owner; pe= owner+ol;
  nbytes= 0;
  labelnum= 0;
  while (p!=pe) {
    ll= sizeof(label);
    st= typei->qdparselabel(ads, &p,pe, labelnum++, label, &ll, flags, typei);
    if (st) return st;
    if (!ll) return adns_s_querydomaininvalid;
    if (ll > 63) return adns_s_querydomaintoolong;
    nbytes+= ll+1;
    if (nbytes >= 255) return adns_s_querydomaintoolong;
    *rqp++= (ll);
    memcpy(rqp,label,ll); rqp+= ll;
  }
  *rqp++= (0);
  ((vb)->used= rqp-(vb)->buf);
  st= mkquery_footer(vb,type);
  return adns_s_ok;
}

variable_types:
owner
char *

variable_types: ol int

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

variable_types: nbytes int

variable_types:
label
unsigned char [255]

variable_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__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: 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 pollfd { int fd; short events; short revents; }

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 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 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__mkquery_frdgram(__int64 a1, int *a2, _DWORD *a3, __int64 a4, unsigned int a5, unsigned int a6, __int16 a7)
{
__int64 v8; // r14
unsigned int v9; // ebp
_BYTE *v11; // rbx
int v13; // [rsp+18h] [rbp-80h] BYREF
int v14; // [rsp+1Ch] [rbp-7Ch] BYREF
char v15[120]; // [rsp+20h] [rbp-78h] BYREF

v8 = a5;
v9 = mkquery_header(a1, (__int64)a2, a3, a5);
if ( !v9 )
{
v11 = (_BYTE *)(*((_QWORD *)a2 + 1) + *a2);
adns__findlabel_start(v15, a1, 0xFFFFFFFFLL, 0LL, a4, (unsigned int)v8, v8, a6, 0LL);
while ( 1 )
{
v9 = adns__findlabel_next(v15, &v14, &v13);
if ( v9 )
_assert_fail("!st", "transmit.c", 0xADu, "adns__mkquery_frdgram");
if ( !v14 )
break;
if ( v14 > 254 )
_assert_fail("lablen<255", "transmit.c", 0xAFu, "adns__mkquery_frdgram");
*v11 = v14;
memcpy(v11 + 1, (const void *)(a4 + v13), v14);
v11 += v14 + 1;
}
*v11 = 0;
*a2 = (_DWORD)v11 + 1 - a2[2];
mkquery_footer(a2, a7);
}
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, int *a2, _DWORD *a3, __int64 a4, unsigned int a5, unsigned int a6, __int16 a7)
{
__int64 v8; // r14
unsigned int v9; // ebp
_BYTE *v11; // rbx
int v13; // [rsp+18h] [rbp-80h] BYREF
int v14; // [rsp+1Ch] [rbp-7Ch] BYREF
char v15[120]; // [rsp+20h] [rbp-78h] BYREF

v8 = a5;
v9 = func2(a1, (__int64)a2, a3, a5);
if ( !v9 )
{
v11 = (_BYTE *)(*((_QWORD *)a2 + 1) + *a2);
adns__findlabel_start(v15, a1, 0xFFFFFFFFLL, 0LL, a4, (unsigned int)v8, v8, a6, 0LL);
while ( 1 )
{
v9 = adns__findlabel_next(v15, &v14, &v13);
if ( v9 )
_assert_fail("!st", "transmit.c", 0xADu, "adns__mkquery_frdgram");
if ( !v14 )
break;
if ( v14 > 254 )
_assert_fail("lablen<255", "transmit.c", 0xAFu, "adns__mkquery_frdgram");
*v11 = v14;
memcpy(v11 + 1, (const void *)(a4 + v13), v14);
v11 += v14 + 1;
}
*v11 = 0;
*a2 = (_DWORD)v11 + 1 - a2[2];
func3(a2, a7);
}
return v9;
}

original_code:
adns_status adns__mkquery_frdgram(adns_state ads, vbuf *vb, int *id_r,
      const byte *qd_dgram, int qd_dglen,
      int qd_begin,
      adns_rrtype type, adns_queryflags flags) {
  byte *rqp;
  findlabel_state fls;
  int lablen, labstart;
  adns_status st;
  st= mkquery_header(ads,vb,id_r,qd_dglen); if (st) return st;
  (rqp= (vb)->buf+(vb)->used);
  adns__findlabel_start(&fls,ads,-1,0,qd_dgram,qd_dglen,qd_dglen,qd_begin,0);
  for (;;) {
    st= adns__findlabel_next(&fls,&lablen,&labstart); ((void) sizeof ((!st) ? 1 : 0), __extension__ ({ if (!st) ; else __assert_fail ("!st", "transmit.c", 173, __extension__ __PRETTY_FUNCTION__); }));
    if (!lablen) break;
    ((void) sizeof ((lablen<255) ? 1 : 0), __extension__ ({ if (lablen<255) ; else __assert_fail ("lablen<255", "transmit.c", 175, __extension__ __PRETTY_FUNCTION__); }));
    *rqp++= (lablen);
    memcpy(rqp,qd_dgram+labstart,lablen);
    rqp+= lablen;
  }
  *rqp++= (0);
  ((vb)->used= rqp-(vb)->buf);
  st= mkquery_footer(vb,type);
  return adns_s_ok;
}

canonical_original_code:
enum adns_status adns__mkquery_frdgram(struct adns__state *ads, struct vbuf *vb, int *id_r, unsigned char *qd_dgram, int qd_dglen, int qd_begin, enum adns_rrtype type, enum adns_queryflags flags) {
  unsigned char *rqp;
  struct findlabel_state fls;
  int lablen; int labstart;
  enum adns_status st;
  st= mkquery_header(ads,vb,id_r,qd_dglen); if (st) return st;
  (rqp= (vb)->buf+(vb)->used);
  adns__findlabel_start(&fls,ads,-1,0,qd_dgram,qd_dglen,qd_dglen,qd_begin,0);
  for (;;) {
    st= adns__findlabel_next(&fls,&lablen,&labstart); ((void) sizeof ((!st) ? 1 : 0), __extension__ ({ if (!st) ; else __assert_fail ("!st", "transmit.c", 173, __extension__ __PRETTY_FUNCTION__); }));
    if (!lablen) break;
    ((void) sizeof ((lablen<255) ? 1 : 0), __extension__ ({ if (lablen<255) ; else __assert_fail ("lablen<255", "transmit.c", 175, __extension__ __PRETTY_FUNCTION__); }));
    *rqp++= (lablen);
    memcpy(rqp,qd_dgram+labstart,lablen);
    rqp+= lablen;
  }
  *rqp++= (0);
  ((vb)->used= rqp-(vb)->buf);
  st= mkquery_footer(vb,type);
  return adns_s_ok;
}

variable_types:
vb
struct vbuf *

variable_types: struct findlabel_state { 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]; } *ads; struct adns__query *qu; int serv; unsigned char *dgram; int dglen; int *dmend_r; }

variable_types: lablen int

variable_types: labstart int

user_defined_types: struct findlabel_state { struct adns__state *ads; struct adns__query *qu; int serv; unsigned char *dgram; int dglen; int *dmend_r; }


decompiled_code:
void __fastcall adns__querysend_tcp(_QWORD *a1)
{
__int64 v1; // rbp
int v2; // eax
int v3; // er13
int v4; // er14
int v5; // esi
unsigned int v6; // ecx
char v7; // al
char *v8; // rax
int v9; // edx
struct iovec v10; // [rsp+0h] [rbp-58h] BYREF
__int64 v11; // [rsp+10h] [rbp-48h]
__int64 v12; // [rsp+18h] [rbp-40h]
char v13[42]; // [rsp+2Eh] [rbp-2Ah] BYREF

v1 = *a1;
if ( *(_DWORD *)(*a1 + 176LL) == 2 )
{
if ( *((_DWORD *)a1 + 2) != 1 )
_assert_fail("qu->state == query_tcpw", "transmit.c", 0xC5u, "adns__querysend_tcp");
v2 = *((_DWORD *)a1 + 30);
v13[0] = BYTE1(v2);
v13[1] = v2;
if ( (unsigned int)adns__vbuf_ensure(v1 + 120, (unsigned int)(*(_DWORD *)(v1 + 120) + v2 + 2)) )
{
++*((_DWORD *)a1 + 51);
*(_QWORD *)(v1 + 192) = 0LL;
*(_QWORD *)(v1 + 184) = 0LL;
v3 = *(_DWORD *)(v1 + 120);
if ( v3 )
{
v3 = 0;
}
else
{
v10.iov_base = v13;
v10.iov_len = 2LL;
v11 = a1[14];
v12 = *((int *)a1 + 30);
adns__sigpipe_protect(*a1);
v4 = writev(*(_DWORD *)(*a1 + 112LL), &v10, 2);
adns__sigpipe_unprotect(*a1);
if ( v4 < 0 )
{
v6 = *_errno_location();
v7 = 1;
if ( v6 <= 0x1C )
v7 = ((0x10001810uLL >> v6) ^ 1) & 1;
if ( v6 != 105 && v7 )
{
v8 = strerror(v6);
adns__tcp_broken(v1, "write", v8);
return;
}
}
else
{
v5 = v4 - 2;
v3 = v4;
if ( v4 > 1 )
{
LABEL_17:
v9 = *((_DWORD *)a1 + 30);
if ( v9 > v5 && !(unsigned int)adns__vbuf_append(v1 + 120, a1[14] + v5, (unsigned int)(v9 - v5)) )
_assert_fail("r", "transmit.c", 0xEFu, "adns__querysend_tcp");
return;
}
}
}
if ( !(unsigned int)adns__vbuf_append(v1 + 120, v13, (unsigned int)(2 - v3)) )
_assert_fail("r", "transmit.c", 0xE8u, "adns__querysend_tcp");
v5 = 0;
goto LABEL_17;
}
}
}

canonical_decompiled_code:
void __fastcall func9(_QWORD *a1)
{
__int64 v1; // rbp
int v2; // eax
int v3; // er13
int v4; // er14
int v5; // esi
unsigned int v6; // ecx
char v7; // al
char *v8; // rax
int v9; // edx
struct iovec v10; // [rsp+0h] [rbp-58h] BYREF
__int64 v11; // [rsp+10h] [rbp-48h]
__int64 v12; // [rsp+18h] [rbp-40h]
char v13[42]; // [rsp+2Eh] [rbp-2Ah] BYREF

v1 = *a1;
if ( *(_DWORD *)(*a1 + 176LL) == 2 )
{
if ( *((_DWORD *)a1 + 2) != 1 )
_assert_fail("qu->state == query_tcpw", "transmit.c", 0xC5u, "adns__querysend_tcp");
v2 = *((_DWORD *)a1 + 30);
v13[0] = BYTE1(v2);
v13[1] = v2;
if ( (unsigned int)adns__vbuf_ensure(v1 + 120, (unsigned int)(*(_DWORD *)(v1 + 120) + v2 + 2)) )
{
++*((_DWORD *)a1 + 51);
*(_QWORD *)(v1 + 192) = 0LL;
*(_QWORD *)(v1 + 184) = 0LL;
v3 = *(_DWORD *)(v1 + 120);
if ( v3 )
{
v3 = 0;
}
else
{
v10.iov_base = v13;
v10.iov_len = 2LL;
v11 = a1[14];
v12 = *((int *)a1 + 30);
adns__sigpipe_protect(*a1);
v4 = writev(*(_DWORD *)(*a1 + 112LL), &v10, 2);
adns__sigpipe_unprotect(*a1);
if ( v4 < 0 )
{
v6 = *_errno_location();
v7 = 1;
if ( v6 <= 0x1C )
v7 = ((0x10001810uLL >> v6) ^ 1) & 1;
if ( v6 != 105 && v7 )
{
v8 = strerror(v6);
adns__tcp_broken(v1, "write", v8);
return;
}
}
else
{
v5 = v4 - 2;
v3 = v4;
if ( v4 > 1 )
{
LABEL_17:
v9 = *((_DWORD *)a1 + 30);
if ( v9 > v5 && !(unsigned int)adns__vbuf_append(v1 + 120, a1[14] + v5, (unsigned int)(v9 - v5)) )
_assert_fail("r", "transmit.c", 0xEFu, "adns__querysend_tcp");
return;
}
}
}
if ( !(unsigned int)adns__vbuf_append(v1 + 120, v13, (unsigned int)(2 - v3)) )
_assert_fail("r", "transmit.c", 0xE8u, "adns__querysend_tcp");
v5 = 0;
goto LABEL_17;
}
}
}

original_code:
void adns__querysend_tcp(adns_query qu, struct timeval now) {
  byte length[2];
  struct iovec iov[2];
  int wr, r;
  adns_state ads;
  if (qu->ads->tcpstate != server_ok) return;
  ((void) sizeof ((qu->state == query_tcpw) ? 1 : 0), __extension__ ({ if (qu->state == query_tcpw) ; else __assert_fail ("qu->state == query_tcpw", "transmit.c", 197, __extension__ __PRETTY_FUNCTION__); }));
  length[0]= (qu->query_dglen&0x0ff00U) >>8;
  length[1]= (qu->query_dglen&0x0ff);
  ads= qu->ads;
  if (!adns__vbuf_ensure(&ads->tcpsend,ads->tcpsend.used+qu->query_dglen+2))
    return;
  qu->retries++;
  ads->tcptimeout.tv_sec= ads->tcptimeout.tv_usec= 0;
  if (ads->tcpsend.used) {
    wr= 0;
  } else {
    iov[0].iov_base= length;
    iov[0].iov_len= 2;
    iov[1].iov_base= qu->query_dgram;
    iov[1].iov_len= qu->query_dglen;
    adns__sigpipe_protect(qu->ads);
    wr= writev(qu->ads->tcpsocket,iov,2);
    adns__sigpipe_unprotect(qu->ads);
    if (wr < 0) {
      if (!((*__errno_location ()) == 11 || (*__errno_location ()) == 4 || (*__errno_location ()) == 28 ||
     (*__errno_location ()) == 105 || (*__errno_location ()) == 12)) {
 adns__tcp_broken(ads,"write",strerror((*__errno_location ())));
 return;
      }
      wr= 0;
    }
  }
  if (wr<2) {
    r= adns__vbuf_append(&ads->tcpsend,length,2-wr); ((void) sizeof ((r) ? 1 : 0), __extension__ ({ if (r) ; else __assert_fail ("r", "transmit.c", 232, __extension__ __PRETTY_FUNCTION__); }));
    wr= 0;
  } else {
    wr-= 2;
  }
  if (wr<qu->query_dglen) {
    r= adns__vbuf_append(&ads->tcpsend,qu->query_dgram+wr,qu->query_dglen-wr);
    ((void) sizeof ((r) ? 1 : 0), __extension__ ({ if (r) ; else __assert_fail ("r", "transmit.c", 239, __extension__ __PRETTY_FUNCTION__); }));
  }
}

canonical_original_code:
void adns__querysend_tcp(struct adns__query *qu, struct timeval now) {
  unsigned char length[2];
  struct iovec iov[2];
  int wr; int r;
  struct adns__state *ads;
  if (qu->ads->tcpstate != server_ok) return;
  ((void) sizeof ((qu->state == query_tcpw) ? 1 : 0), __extension__ ({ if (qu->state == query_tcpw) ; else __assert_fail ("qu->state == query_tcpw", "transmit.c", 197, __extension__ __PRETTY_FUNCTION__); }));
  length[0]= (qu->query_dglen&0x0ff00U) >>8;
  length[1]= (qu->query_dglen&0x0ff);
  ads= qu->ads;
  if (!adns__vbuf_ensure(&ads->tcpsend,ads->tcpsend.used+qu->query_dglen+2))
    return;
  qu->retries++;
  ads->tcptimeout.tv_sec= ads->tcptimeout.tv_usec= 0;
  if (ads->tcpsend.used) {
    wr= 0;
  } else {
    iov[0].iov_base= length;
    iov[0].iov_len= 2;
    iov[1].iov_base= qu->query_dgram;
    iov[1].iov_len= qu->query_dglen;
    adns__sigpipe_protect(qu->ads);
    wr= writev(qu->ads->tcpsocket,iov,2);
    adns__sigpipe_unprotect(qu->ads);
    if (wr < 0) {
      if (!((*__errno_location ()) == 11 || (*__errno_location ()) == 4 || (*__errno_location ()) == 28 ||
     (*__errno_location ()) == 105 || (*__errno_location ()) == 12)) {
 adns__tcp_broken(ads,"write",strerror((*__errno_location ())));
 return;
      }
      wr= 0;
    }
  }
  if (wr<2) {
    r= adns__vbuf_append(&ads->tcpsend,length,2-wr); ((void) sizeof ((r) ? 1 : 0), __extension__ ({ if (r) ; else __assert_fail ("r", "transmit.c", 232, __extension__ __PRETTY_FUNCTION__); }));
    wr= 0;
  } else {
    wr-= 2;
  }
  if (wr<qu->query_dglen) {
    r= adns__vbuf_append(&ads->tcpsend,qu->query_dgram+wr,qu->query_dglen-wr);
    ((void) sizeof ((r) ? 1 : 0), __extension__ ({ if (r) ; else __assert_fail ("r", "transmit.c", 239, __extension__ __PRETTY_FUNCTION__); }));
  }
}

variable_types: struct adns__query { 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 *head; } udpw; struct adns__query *forallnext; int nextid; struct vbuf { int used; unsigned char *buf; } tcpsend; int nservers; enum adns__tcpstate { server_disconnected=0, server_connecting=1, server_ok=2, server_broken=3 } tcpstate; struct timeval { long tv_sec; long tv_usec; } 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 { unsigned int s_addr; } addr; } servers[5]; struct sortlist { struct in_addr base; } sortlist[15]; char **searchlist; unsigned short rand48xsubi[3]; } *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 *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 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 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; } *

variable_types:
now
struct timeval

variable_types:
length
unsigned char [2]

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

variable_types: wr int

variable_types: r int

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


original_code:
static void query_usetcp(adns_query qu, struct timeval now) {
  qu->state= query_tcpw;
  qu->timeout= now;
  timevaladd(&qu->timeout,30000);
  do { (qu)-> next= 0; (qu)-> back= (qu->ads->tcpw).tail; if ((qu->ads->tcpw).tail) (qu->ads->tcpw).tail-> next= (qu); else (qu->ads->tcpw).head= (qu); (qu->ads->tcpw).tail= (qu); } while(0);
  adns__querysend_tcp(qu,now);
  adns__tcp_tryconnect(qu->ads,now);
}

canonical_original_code:
static void query_usetcp(struct adns__query *qu, struct timeval now) {
  qu->state= query_tcpw;
  qu->timeout= now;
  timevaladd(&qu->timeout,30000);
  do { (qu)-> next= 0; (qu)-> back= (qu->ads->tcpw).tail; if ((qu->ads->tcpw).tail) (qu->ads->tcpw).tail-> next= (qu); else (qu->ads->tcpw).head= (qu); (qu->ads->tcpw).tail= (qu); } while(0);
  adns__querysend_tcp(qu,now);
  adns__tcp_tryconnect(qu->ads,now);
}

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 vbuf { int used; unsigned char *buf; }

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

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: struct sortlist { struct in_addr base; }

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 adns_rr_intstr { int i; char *str; }

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

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


original_code:
void adns__query_send(adns_query qu, struct timeval now) {
  struct sockaddr_in servaddr;
  int serv, r;
  adns_state ads;
  ((void) sizeof ((qu->state == query_tosend) ? 1 : 0), __extension__ ({ if (qu->state == query_tosend) ; else __assert_fail ("qu->state == query_tosend", "transmit.c", 257, __extension__ __PRETTY_FUNCTION__); }));
  if ((qu->flags & adns_qf_usevc) || (qu->query_dglen > 512)) {
    query_usetcp(qu,now);
    return;
  }
  if (qu->retries >= 15) {
    adns__query_fail(qu,adns_s_timeout);
    return;
  }
  serv= qu->udpnextserver;
  memset(&servaddr,0,sizeof(servaddr));
  ads= qu->ads;
  servaddr.sin_family= 2;
  servaddr.sin_addr= ads->servers[serv].addr;
  servaddr.sin_port= htons(53);
  r= sendto(ads->udpsocket,qu->query_dgram,qu->query_dglen,0,
     (const struct sockaddr*)&servaddr,sizeof(servaddr));
  if (r<0 && (*__errno_location ()) == 90) {
    qu->retries= 0;
    query_usetcp(qu,now);
    return;
  }
  if (r<0 && (*__errno_location ()) != 11)
    adns__warn(ads,serv,0,"sendto failed: %s",strerror((*__errno_location ())));
  qu->timeout= now;
  timevaladd(&qu->timeout,2000);
  qu->udpsent |= (1<<serv);
  qu->udpnextserver= (serv+1)%ads->nservers;
  qu->retries++;
  do { (qu)-> next= 0; (qu)-> back= (ads->udpw).tail; if ((ads->udpw).tail) (ads->udpw).tail-> next= (qu); else (ads->udpw).head= (qu); (ads->udpw).tail= (qu); } while(0);
}

canonical_original_code:
void adns__query_send(struct adns__query *qu, struct timeval now) {
  struct sockaddr_in servaddr;
  int serv; int r;
  struct adns__state *ads;
  ((void) sizeof ((qu->state == query_tosend) ? 1 : 0), __extension__ ({ if (qu->state == query_tosend) ; else __assert_fail ("qu->state == query_tosend", "transmit.c", 257, __extension__ __PRETTY_FUNCTION__); }));
  if ((qu->flags & adns_qf_usevc) || (qu->query_dglen > 512)) {
    query_usetcp(qu,now);
    return;
  }
  if (qu->retries >= 15) {
    adns__query_fail(qu,adns_s_timeout);
    return;
  }
  serv= qu->udpnextserver;
  memset(&servaddr,0,sizeof(servaddr));
  ads= qu->ads;
  servaddr.sin_family= 2;
  servaddr.sin_addr= ads->servers[serv].addr;
  servaddr.sin_port= htons(53);
  r= sendto(ads->udpsocket,qu->query_dgram,qu->query_dglen,0,
     (struct sockaddr *)&servaddr,sizeof(servaddr));
  if (r<0 && (*__errno_location ()) == 90) {
    qu->retries= 0;
    query_usetcp(qu,now);
    return;
  }
  if (r<0 && (*__errno_location ()) != 11)
    adns__warn(ads,serv,0,"sendto failed: %s",strerror((*__errno_location ())));
  qu->timeout= now;
  timevaladd(&qu->timeout,2000);
  qu->udpsent |= (1<<serv);
  qu->udpnextserver= (serv+1)%ads->nservers;
  qu->retries++;
  do { (qu)-> next= 0; (qu)-> back= (ads->udpw).tail; if ((ads->udpw).tail) (ads->udpw).tail-> next= (qu); else (ads->udpw).head= (qu); (ads->udpw).tail= (qu); } while(0);
}

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

variable_types: serv int

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

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__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 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: 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 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 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 __sigset_t { unsigned long __val[-1]; }

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

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

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