#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int uint8_t;
typedef int uint16_t;
typedef int /*<<< orphan*/ dns_query;

/* Variables and functions */
int EAI_SYSTEM;
int *dns_query_append16(int *, int);
int /*<<< orphan*/ memset(int *, int /*<<< orphan*/, int);
int rand();
scalar_t__ send(int, int *, size_t, int /*<<< orphan*/);
int send_dns_query(int sock, const char *nodename, uint16_t *id) {
  uint8_t dns_query[512];
  size_t dns_query_len;
  uint8_t *pq;
  uint8_t *plen;
  const char *pname;
  *id = rand();
  memset(dns_query, 0, sizeof(dns_query));
  dns_query_append16(&dns_query[0], *id);
  dns_query[2] = (0 << 3) | (1 << 0);
  dns_query[3] = 0x00;
  dns_query_append16(&dns_query[4], 1);
  dns_query_append16(&dns_query[6], 0);
  dns_query_append16(&dns_query[8], 0);
  dns_query_append16(&dns_query[10], 0);
  pq = &dns_query[12];
  pname = nodename;
  while (*pname != '\0') {
    plen = pq;
    pq++;
    while ((*pname != '.') && (*pname != '\0')) {
      *pq++ = *pname++;
    }
    *plen = pq - plen - 1;
    if (*pname == '.') {
      pname++;
    }
  }
  *pq++ = 0x00;
  pq = dns_query_append16(pq, 1);
  pq = dns_query_append16(pq, 1);
  dns_query_len = (size_t)(pq - &dns_query[0]);
  if (send(sock, dns_query, dns_query_len, 0) < 0) {
    return EAI_SYSTEM;
  }
  return 0;
}


x86 assembly (clang, -O0):
<send_dns_query>:
push   %rbp
mov    %rsp,%rbp
sub    $0x850,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x0,%al
call   1060 <rand@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
lea    -0x820(%rbp),%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1040 <memset@plt>
lea    -0x820(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
call   1050 <dns_query_append16@plt>
movl   $0x1,-0x818(%rbp)
movl   $0x0,-0x814(%rbp)
lea    -0x820(%rbp),%rdi
add    $0x10,%rdi
mov    $0x1,%esi
call   1050 <dns_query_append16@plt>
lea    -0x820(%rbp),%rdi
add    $0x18,%rdi
xor    %esi,%esi
call   1050 <dns_query_append16@plt>
lea    -0x820(%rbp),%rdi
add    $0x20,%rdi
xor    %esi,%esi
call   1050 <dns_query_append16@plt>
lea    -0x820(%rbp),%rdi
add    $0x28,%rdi
xor    %esi,%esi
call   1050 <dns_query_append16@plt>
lea    -0x820(%rbp),%rax
add    $0x30,%rax
mov    %rax,-0x830(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x840(%rbp)
mov    -0x840(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12ff <send_dns_query+0x1bf>
mov    -0x830(%rbp),%rax
mov    %rax,-0x838(%rbp)
mov    -0x830(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x830(%rbp)
mov    -0x840(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x2e,%ecx
mov    %al,-0x841(%rbp)
je     126a <send_dns_query+0x12a>
mov    -0x840(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x841(%rbp)
mov    -0x841(%rbp),%al
test   $0x1,%al
jne    127d <send_dns_query+0x13d>
jmp    12b1 <send_dns_query+0x171>
mov    -0x840(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x840(%rbp)
movsbl (%rax),%ecx
mov    -0x830(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x830(%rbp)
mov    %ecx,(%rax)
jmp    1239 <send_dns_query+0xf9>
mov    -0x830(%rbp),%rax
mov    -0x838(%rbp),%rcx
sub    %rcx,%rax
sar    $0x2,%rax
sub    $0x1,%rax
mov    %eax,%ecx
mov    -0x838(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x840(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    12fa <send_dns_query+0x1ba>
mov    -0x840(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x840(%rbp)
jmp    1206 <send_dns_query+0xc6>
mov    -0x830(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x830(%rbp)
movl   $0x0,(%rax)
mov    -0x830(%rbp),%rdi
mov    $0x1,%esi
call   1050 <dns_query_append16@plt>
mov    %rax,-0x830(%rbp)
mov    -0x830(%rbp),%rdi
mov    $0x1,%esi
call   1050 <dns_query_append16@plt>
mov    %rax,-0x830(%rbp)
mov    -0x830(%rbp),%rax
lea    -0x820(%rbp),%rcx
sub    %rcx,%rax
sar    $0x2,%rax
mov    %rax,-0x828(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x820(%rbp),%rsi
mov    -0x828(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <send@plt>
cmp    $0x0,%rax
jge    1399 <send_dns_query+0x259>
mov    0x2c59(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13a0 <send_dns_query+0x260>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x850,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_dns_query>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x808,%rsp
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r14d
xor    %eax,%eax
call   1060 <rand@plt>
mov    %eax,(%rbx)
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1040 <memset@plt>
mov    (%rbx),%esi
mov    %r15,%rdi
call   1050 <dns_query_append16@plt>
movq   $0x1,0x8(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
call   1050 <dns_query_append16@plt>
lea    0x18(%rsp),%rdi
xor    %esi,%esi
call   1050 <dns_query_append16@plt>
lea    0x20(%rsp),%rdi
xor    %esi,%esi
call   1050 <dns_query_append16@plt>
lea    0x28(%rsp),%rdi
xor    %esi,%esi
call   1050 <dns_query_append16@plt>
lea    0x30(%rsp),%rdi
mov    0x0(%rbp),%dl
test   %dl,%dl
jne    11f3 <send_dns_query+0xb3>
jmp    1233 <send_dns_query+0xf3>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rcx,%rdx
shr    $0x2,%rdx
add    $0xffffffff,%edx
mov    %edx,(%rax)
xor    %edx,%edx
cmpb   $0x2e,(%rsi)
sete   %dl
lea    (%rsi,%rdx,1),%rbp
mov    (%rsi,%rdx,1),%dl
lea    (%rax,%rcx,1),%rdi
test   %dl,%dl
je     122d <send_dns_query+0xed>
mov    %rdi,%rax
mov    $0x4,%ecx
mov    %rbp,%rsi
test   %dl,%dl
je     11d0 <send_dns_query+0x90>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmp    $0x2e,%dl
je     11d0 <send_dns_query+0x90>
movsbl %dl,%edx
mov    %edx,(%rax,%rcx,1)
movzbl 0x1(%rsi),%edx
add    $0x1,%rsi
add    $0x4,%rcx
test   %dl,%dl
jne    1210 <send_dns_query+0xd0>
jmp    11d0 <send_dns_query+0x90>
add    %rcx,%rax
mov    %rax,%rdi
movl   $0x0,(%rdi)
add    $0x4,%rdi
mov    $0x1,%esi
call   1050 <dns_query_append16@plt>
mov    %rax,%rdi
mov    $0x1,%esi
call   1050 <dns_query_append16@plt>
sub    %r15,%rax
sar    $0x2,%rax
mov    %r14d,%edi
mov    %r15,%rsi
mov    %rax,%rdx
xor    %ecx,%ecx
call   1030 <send@plt>
mov    0x2d76(%rip),%rcx
sar    $0x3f,%rax
and    (%rcx),%eax
add    $0x808,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EAI_SYSTEM = global i32 0
define i32 @send_dns_query(i32 %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca [512 x i32]
%9 = alloca i64
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i8*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32* %2, i32** %7
%13 = call i32 (...) @rand()
%14 = load i32*, i32** %7
store i32 %13, i32* %14
%15 = getelementptr inbounds [512 x i32], [512 x i32]* %8, i64 0, i64 0
%16 = call i32 @memset(i32* %15, i32 0, i32 2048)
%17 = getelementptr inbounds [512 x i32], [512 x i32]* %8, i64 0, i64 0
%18 = load i32*, i32** %7
%19 = load i32, i32* %18
%20 = call i32* @dns_query_append16(i32* %17, i32 %19)
%21 = getelementptr inbounds [512 x i32], [512 x i32]* %8, i64 0, i64 2
store i32 1, i32* %21
%22 = getelementptr inbounds [512 x i32], [512 x i32]* %8, i64 0, i64 3
store i32 0, i32* %22
%23 = getelementptr inbounds [512 x i32], [512 x i32]* %8, i64 0, i64 4
%24 = call i32* @dns_query_append16(i32* %23, i32 1)
%25 = getelementptr inbounds [512 x i32], [512 x i32]* %8, i64 0, i64 6
%26 = call i32* @dns_query_append16(i32* %25, i32 0)
%27 = getelementptr inbounds [512 x i32], [512 x i32]* %8, i64 0, i64 8
%28 = call i32* @dns_query_append16(i32* %27, i32 0)
%29 = getelementptr inbounds [512 x i32], [512 x i32]* %8, i64 0, i64 10
%30 = call i32* @dns_query_append16(i32* %29, i32 0)
%31 = getelementptr inbounds [512 x i32], [512 x i32]* %8, i64 0, i64 12
store i32* %31, i32** %10
%32 = load i8*, i8** %6
store i8* %32, i8** %12
br label %33
33: ; preds = %78, %3
%34 = load i8*, i8** %12
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %79
38: ; preds = %33
%39 = load i32*, i32** %10
store i32* %39, i32** %11
%40 = load i32*, i32** %10
%41 = getelementptr inbounds i32, i32* %40, i32 1
store i32* %41, i32** %10
br label %42
42: ; preds = %54, %38
%43 = load i8*, i8** %12
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = icmp ne i32 %45, 46
br i1 %46, label %47, label %52
47: ; preds = %42
%48 = load i8*, i8** %12
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
%51 = icmp ne i32 %50, 0
br label %52
52: ; preds = %47, %42
%53 = phi i1 [ false, %42 ], [ %51, %47 ]
br i1 %53, label %54, label %61
54: ; preds = %52
%55 = load i8*, i8** %12
%56 = getelementptr inbounds i8, i8* %55, i32 1
store i8* %56, i8** %12
%57 = load i8, i8* %55
%58 = sext i8 %57 to i32
%59 = load i32*, i32** %10
%60 = getelementptr inbounds i32, i32* %59, i32 1
store i32* %60, i32** %10
store i32 %58, i32* %59
br label %42
61: ; preds = %52
%62 = load i32*, i32** %10
%63 = load i32*, i32** %11
%64 = ptrtoint i32* %62 to i64
%65 = ptrtoint i32* %63 to i64
%66 = sub i64 %64, %65
%67 = sdiv exact i64 %66, 4
%68 = sub nsw i64 %67, 1
%69 = trunc i64 %68 to i32
%70 = load i32*, i32** %11
store i32 %69, i32* %70
%71 = load i8*, i8** %12
%72 = load i8, i8* %71
%73 = sext i8 %72 to i32
%74 = icmp eq i32 %73, 46
br i1 %74, label %75, label %78
75: ; preds = %61
%76 = load i8*, i8** %12
%77 = getelementptr inbounds i8, i8* %76, i32 1
store i8* %77, i8** %12
br label %78
78: ; preds = %75, %61
br label %33
79: ; preds = %33
%80 = load i32*, i32** %10
%81 = getelementptr inbounds i32, i32* %80, i32 1
store i32* %81, i32** %10
store i32 0, i32* %80
%82 = load i32*, i32** %10
%83 = call i32* @dns_query_append16(i32* %82, i32 1)
store i32* %83, i32** %10
%84 = load i32*, i32** %10
%85 = call i32* @dns_query_append16(i32* %84, i32 1)
store i32* %85, i32** %10
%86 = load i32*, i32** %10
%87 = getelementptr inbounds [512 x i32], [512 x i32]* %8, i64 0, i64 0
%88 = ptrtoint i32* %86 to i64
%89 = ptrtoint i32* %87 to i64
%90 = sub i64 %88, %89
%91 = sdiv exact i64 %90, 4
store i64 %91, i64* %9
%92 = load i32, i32* %5
%93 = getelementptr inbounds [512 x i32], [512 x i32]* %8, i64 0, i64 0
%94 = load i64, i64* %9
%95 = call i64 @send(i32 %92, i32* %93, i64 %94, i32 0)
%96 = icmp slt i64 %95, 0
br i1 %96, label %97, label %99
97: ; preds = %79
%98 = load i32, i32* @EAI_SYSTEM
store i32 %98, i32* %4
br label %100
99: ; preds = %79
store i32 0, i32* %4
br label %100
100: ; preds = %99, %97
%101 = load i32, i32* %4
ret i32 %101
}

LLVM IR (-O2):
@EAI_SYSTEM = global i32 0
define i32 @send_dns_query(i32 %0, i8* nocapture %1, i32* nocapture %2) {
%4 = alloca [512 x i32]
%5 = bitcast [512 x i32]* %4 to i8*
%6 = tail call i32 (...) @rand()
store i32 %6, i32* %2
%7 = getelementptr inbounds [512 x i32], [512 x i32]* %4, i64 0, i64 0
%8 = call i32 @memset(i32* %7, i32 0, i32 2048)
%9 = load i32, i32* %2
%10 = call i32* @dns_query_append16(i32* %7, i32 %9)
%11 = getelementptr inbounds [512 x i32], [512 x i32]* %4, i64 0, i64 2
store i32 1, i32* %11
%12 = getelementptr inbounds [512 x i32], [512 x i32]* %4, i64 0, i64 3
store i32 0, i32* %12
%13 = getelementptr inbounds [512 x i32], [512 x i32]* %4, i64 0, i64 4
%14 = call i32* @dns_query_append16(i32* %13, i32 1)
%15 = getelementptr inbounds [512 x i32], [512 x i32]* %4, i64 0, i64 6
%16 = call i32* @dns_query_append16(i32* %15, i32 0)
%17 = getelementptr inbounds [512 x i32], [512 x i32]* %4, i64 0, i64 8
%18 = call i32* @dns_query_append16(i32* %17, i32 0)
%19 = getelementptr inbounds [512 x i32], [512 x i32]* %4, i64 0, i64 10
%20 = call i32* @dns_query_append16(i32* %19, i32 0)
%21 = getelementptr inbounds [512 x i32], [512 x i32]* %4, i64 0, i64 12
%22 = load i8, i8* %1
%23 = icmp eq i8 %22, 0
br i1 %23, label %50, label %24
24: ; preds = %3, %37
%25 = phi i8 [ %48, %37 ], [ %22, %3 ]
%26 = phi i8* [ %47, %37 ], [ %1, %3 ]
%27 = phi i32* [ %32, %37 ], [ %21, %3 ]
br label %28
28: ; preds = %24, %33
%29 = phi i8 [ %36, %33 ], [ %25, %24 ]
%30 = phi i32* [ %32, %33 ], [ %27, %24 ]
%31 = phi i8* [ %35, %33 ], [ %26, %24 ]
%32 = getelementptr inbounds i32, i32* %30, i64 1
switch i8 %29, label %33 [
i8 46, label %37
i8 0, label %37
]
33: ; preds = %28
%34 = sext i8 %29 to i32
%35 = getelementptr inbounds i8, i8* %31, i64 1
store i32 %34, i32* %32
%36 = load i8, i8* %35
br label %28
37: ; preds = %28, %28
%38 = ptrtoint i32* %32 to i64
%39 = ptrtoint i32* %27 to i64
%40 = sub i64 %38, %39
%41 = lshr exact i64 %40, 2
%42 = trunc i64 %41 to i32
%43 = add i32 %42, -1
store i32 %43, i32* %27
%44 = load i8, i8* %31
%45 = icmp eq i8 %44, 46
%46 = zext i1 %45 to i64
%47 = getelementptr i8, i8* %31, i64 %46
%48 = load i8, i8* %47
%49 = icmp eq i8 %48, 0
br i1 %49, label %50, label %24
50: ; preds = %37, %3
%51 = phi i32* [ %21, %3 ], [ %32, %37 ]
%52 = getelementptr inbounds i32, i32* %51, i64 1
store i32 0, i32* %51
%53 = call i32* @dns_query_append16(i32* %52, i32 1)
%54 = call i32* @dns_query_append16(i32* %53, i32 1)
%55 = ptrtoint i32* %54 to i64
%56 = ptrtoint [512 x i32]* %4 to i64
%57 = sub i64 %55, %56
%58 = ashr exact i64 %57, 2
%59 = call i64 @send(i32 %0, i32* %7, i64 %58, i32 0)
%60 = icmp slt i64 %59, 0
%61 = load i32, i32* @EAI_SYSTEM
%62 = select i1 %60, i32 %61, i32 0
ret i32 %62
}