#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 u_int16_t;
struct in_addr {
  int dummy;
};
typedef int /*<<< orphan*/ reply;
typedef int /*<<< orphan*/ ns_rr;
typedef int /*<<< orphan*/ ns_msg;

/* Variables and functions */
scalar_t__ NO_DATA;
int NS_PACKETSZ;
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
scalar_t__ h_errno;
int /*<<< orphan*/ herror(char *);
int /*<<< orphan*/ ns_c_in;
int ns_initparse(unsigned char *, int, int /*<<< orphan*/ *);
int ns_msg_count(int /*<<< orphan*/, int /*<<< orphan*/);
int ns_parserr(int /*<<< orphan*/ *, int /*<<< orphan*/, int,
               int /*<<< orphan*/ *);
int /*<<< orphan*/ ns_s_an;
int ns_t_a;
int ns_t_mx;
int process_a_record(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                     struct in_addr *);
int process_mx_record(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                      struct in_addr *);
int res_search(char const *, int /*<<< orphan*/, int, unsigned char *, int);
int /*<<< orphan*/ stderr;
int query(const char *domain_name, int type, struct in_addr *results) {
  int status;
  unsigned char reply[NS_PACKETSZ];
  int reply_len;
  reply_len = res_search(domain_name, ns_c_in, type, reply, sizeof(reply));
  if (reply_len == -1) {
    if (h_errno == NO_DATA) {
      return 0;
    } else {
      herror("res_search");
      return -1;
    }
  }
  ns_msg handle;
  status = ns_initparse(reply, reply_len, &handle);
  if (status == -1) {
    fputs("ns_initparse: truncated or malformed packet\n", stderr);
    return -1;
  }
  u_int16_t answers_count = ns_msg_count(handle, ns_s_an);
  ns_rr record;
  for (int i = 0; i < answers_count; ++i) {
    status = ns_parserr(&handle, ns_s_an, i, &record);
    if (status == -1) {
      fputs("ns_parserr: truncated or malformed record\n", stderr);
      return -1;
    }
    if (type == ns_t_mx) {
      status = process_mx_record(&handle, &record, results);
    } else if (type == ns_t_a) {
      status = process_a_record(&handle, &record, results);
    } else {
      return -1;
    }
    if (status == -1) {
      return -1;
    }
  }
  return answers_count;
}


x86 assembly (clang, -O0):
<query>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e0e(%rip),%rdx
mov    (%rdx),%esi
mov    -0x14(%rbp),%edx
mov    %eax,%r8d
call   1030 <res_search@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    1230 <query+0xb0>
mov    0x2df5(%rip),%rax
mov    (%rax),%rax
mov    0x2dc3(%rip),%rcx
cmp    (%rcx),%rax
jne    1211 <query+0x91>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    1395 <query+0x215>
lea    0xde8(%rip),%rdi
call   10a0 <herror@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    1395 <query+0x215>
mov    -0x58(%rbp),%rdi
mov    -0x3c(%rbp),%esi
lea    -0x44(%rbp),%rdx
call   1040 <ns_initparse@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1275 <query+0xf5>
mov    0x2d6c(%rip),%rax
mov    (%rax),%esi
lea    0xdae(%rip),%rdi
call   1080 <fputs@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    1395 <query+0x215>
mov    -0x44(%rbp),%edi
mov    0x2d31(%rip),%rax
mov    (%rax),%esi
call   1050 <ns_msg_count@plt>
mov    %eax,-0x48(%rbp)
movl   $0x0,-0x50(%rbp)
mov    -0x50(%rbp),%eax
cmp    -0x48(%rbp),%eax
jge    1388 <query+0x208>
mov    0x2d0d(%rip),%rax
mov    (%rax),%esi
mov    -0x50(%rbp),%edx
lea    -0x44(%rbp),%rdi
lea    -0x4c(%rbp),%rcx
call   1060 <ns_parserr@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    12ea <query+0x16a>
mov    0x2cf7(%rip),%rax
mov    (%rax),%esi
lea    0xd66(%rip),%rdi
call   1080 <fputs@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    1395 <query+0x215>
mov    -0x14(%rbp),%eax
mov    0x2cd4(%rip),%rcx
cmp    (%rcx),%eax
jne    1315 <query+0x195>
mov    -0x20(%rbp),%rdx
lea    -0x44(%rbp),%rdi
lea    -0x4c(%rbp),%rsi
call   1090 <process_mx_record@plt>
mov    %eax,-0x24(%rbp)
jmp    1358 <query+0x1d8>
mov    -0x14(%rbp),%eax
mov    0x2cc9(%rip),%rcx
cmp    (%rcx),%eax
jne    1340 <query+0x1c0>
mov    -0x20(%rbp),%rdx
lea    -0x44(%rbp),%rdi
lea    -0x4c(%rbp),%rsi
call   1070 <process_a_record@plt>
mov    %eax,-0x24(%rbp)
jmp    1353 <query+0x1d3>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    1395 <query+0x215>
jmp    1358 <query+0x1d8>
cmpl   $0xffffffff,-0x24(%rbp)
jne    1375 <query+0x1f5>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    1395 <query+0x215>
jmp    137a <query+0x1fa>
mov    -0x50(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x50(%rbp)
jmp    1290 <query+0x110>
mov    -0x48(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<query>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,-0x38(%rbp)
mov    %esi,%r15d
mov    0x2e09(%rip),%rax
mov    (%rax),%r8d
mov    %rsp,%rbx
mov    %r8,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edx
mov    %rbx,%rcx
call   1030 <res_search@plt>
cmp    $0xffffffff,%eax
je     128d <query+0x10d>
lea    -0x2c(%rbp),%rdx
mov    %rbx,%rdi
mov    %eax,%esi
call   1040 <ns_initparse@plt>
cmp    $0xffffffff,%eax
je     12b4 <query+0x134>
mov    -0x2c(%rbp),%edi
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
call   1050 <ns_msg_count@plt>
mov    %eax,%r12d
test   %eax,%eax
jle    12e1 <query+0x161>
xor    %ebx,%ebx
lea    -0x2c(%rbp),%r13
lea    -0x3c(%rbp),%r14
jmp    1244 <query+0xc4>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r13,%rdi
mov    %r14,%rsi
mov    -0x38(%rbp),%rdx
call   1090 <process_mx_record@plt>
cmp    $0xffffffff,%eax
je     12db <query+0x15b>
add    $0x1,%ebx
cmp    %ebx,%r12d
je     12e1 <query+0x161>
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
mov    %r13,%rdi
mov    %ebx,%edx
mov    %r14,%rcx
call   1060 <ns_parserr@plt>
cmp    $0xffffffff,%eax
je     12c6 <query+0x146>
mov    0x2d62(%rip),%rax
cmp    %r15d,(%rax)
je     1220 <query+0xa0>
mov    0x2d76(%rip),%rax
cmp    %r15d,(%rax)
jne    12db <query+0x15b>
mov    %r13,%rdi
mov    %r14,%rsi
mov    -0x38(%rbp),%rdx
call   1070 <process_a_record@plt>
cmp    $0xffffffff,%eax
jne    1238 <query+0xb8>
jmp    12db <query+0x15b>
mov    0x2d4c(%rip),%rax
mov    (%rax),%rax
mov    0x2d1a(%rip),%rcx
xor    %r12d,%r12d
cmp    (%rcx),%rax
je     12e1 <query+0x161>
lea    0xd53(%rip),%rdi
call   10a0 <herror@plt>
jmp    12db <query+0x15b>
mov    0x2d05(%rip),%rax
mov    (%rax),%esi
lea    0xd47(%rip),%rdi
jmp    12d6 <query+0x156>
mov    0x2cf3(%rip),%rax
mov    (%rax),%esi
lea    0xd62(%rip),%rdi
call   1080 <fputs@plt>
mov    $0xffffffff,%r12d
mov    %r12d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.in_addr = type { i32 }
@NS_PACKETSZ = global i32 0
@ns_c_in = global i32 0
@h_errno = global i64 0
@NO_DATA = global i64 0
@.str = [11 x i8] c"res_search\00"
@.str.1 = [45 x i8] c"ns_initparse: truncated or malformed packet\0A\00"
@stderr = global i32 0
@ns_s_an = global i32 0
@.str.2 = [43 x i8] c"ns_parserr: truncated or malformed record\0A\00"
@ns_t_mx = global i32 0
@ns_t_a = global i32 0
define i32 @query(i8* %0, i32 %1, %struct.in_addr* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.in_addr*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store %struct.in_addr* %2, %struct.in_addr** %7
%17 = load i32, i32* @NS_PACKETSZ
%18 = zext i32 %17 to i64
%19 = call i8* @llvm.stacksave()
store i8* %19, i8** %9
%20 = alloca i8, i64 %18
store i64 %18, i64* %10
%21 = load i8*, i8** %5
%22 = load i32, i32* @ns_c_in
%23 = load i32, i32* %6
%24 = trunc i64 %18 to i32
%25 = call i32 @res_search(i8* %21, i32 %22, i32 %23, i8* %20, i32 %24)
store i32 %25, i32* %11
%26 = load i32, i32* %11
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %35
28: ; preds = %3
%29 = load i64, i64* @h_errno
%30 = load i64, i64* @NO_DATA
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %28
store i32 0, i32* %4
store i32 1, i32* %12
br label %86
33: ; preds = %28
%34 = call i32 @herror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
store i32 1, i32* %12
br label %86
35: ; preds = %3
%36 = load i32, i32* %11
%37 = call i32 @ns_initparse(i8* %20, i32 %36, i32* %13)
store i32 %37, i32* %8
%38 = load i32, i32* %8
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %35
%41 = load i32, i32* @stderr
%42 = call i32 @fputs(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %41)
store i32 -1, i32* %4
store i32 1, i32* %12
br label %86
43: ; preds = %35
%44 = load i32, i32* %13
%45 = load i32, i32* @ns_s_an
%46 = call i32 @ns_msg_count(i32 %44, i32 %45)
store i32 %46, i32* %14
store i32 0, i32* %16
br label %47
47: ; preds = %81, %43
%48 = load i32, i32* %16
%49 = load i32, i32* %14
%50 = icmp slt i32 %48, %49
br i1 %50, label %51, label %84
51: ; preds = %47
%52 = load i32, i32* @ns_s_an
%53 = load i32, i32* %16
%54 = call i32 @ns_parserr(i32* %13, i32 %52, i32 %53, i32* %15)
store i32 %54, i32* %8
%55 = load i32, i32* %8
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %60
57: ; preds = %51
%58 = load i32, i32* @stderr
%59 = call i32 @fputs(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %58)
store i32 -1, i32* %4
store i32 1, i32* %12
br label %86
60: ; preds = %51
%61 = load i32, i32* %6
%62 = load i32, i32* @ns_t_mx
%63 = icmp eq i32 %61, %62
br i1 %63, label %64, label %67
64: ; preds = %60
%65 = load %struct.in_addr*, %struct.in_addr** %7
%66 = call i32 @process_mx_record(i32* %13, i32* %15, %struct.in_addr* %65)
store i32 %66, i32* %8
br label %76
67: ; preds = %60
%68 = load i32, i32* %6
%69 = load i32, i32* @ns_t_a
%70 = icmp eq i32 %68, %69
br i1 %70, label %71, label %74
71: ; preds = %67
%72 = load %struct.in_addr*, %struct.in_addr** %7
%73 = call i32 @process_a_record(i32* %13, i32* %15, %struct.in_addr* %72)
store i32 %73, i32* %8
br label %75
74: ; preds = %67
store i32 -1, i32* %4
store i32 1, i32* %12
br label %86
75: ; preds = %71
br label %76
76: ; preds = %75, %64
%77 = load i32, i32* %8
%78 = icmp eq i32 %77, -1
br i1 %78, label %79, label %80
79: ; preds = %76
store i32 -1, i32* %4
store i32 1, i32* %12
br label %86
80: ; preds = %76
br label %81
81: ; preds = %80
%82 = load i32, i32* %16
%83 = add nsw i32 %82, 1
store i32 %83, i32* %16
br label %47
84: ; preds = %47
%85 = load i32, i32* %14
store i32 %85, i32* %4
store i32 1, i32* %12
br label %86
86: ; preds = %84, %79, %74, %57, %40, %33, %32
%87 = load i8*, i8** %9
%88 = load i32, i32* %4
ret i32 %88
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
@NS_PACKETSZ = global i32 0
@ns_c_in = global i32 0
@h_errno = global i64 0
@NO_DATA = global i64 0
@.str = [11 x i8] c"res_search\00"
@.str.1 = [45 x i8] c"ns_initparse: truncated or malformed packet\0A\00"
@stderr = global i32 0
@ns_s_an = global i32 0
@.str.2 = [43 x i8] c"ns_parserr: truncated or malformed record\0A\00"
@ns_t_mx = global i32 0
@ns_t_a = global i32 0
define i32 @query(i8* %0, i32 %1, %struct.in_addr* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = load i32, i32* @NS_PACKETSZ
%7 = zext i32 %6 to i64
%8 = alloca i8, i64 %7
%9 = load i32, i32* @ns_c_in
%10 = call i32 @res_search(i8* %0, i32 %9, i32 %1, i8* %8, i32 %6)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %18
12: ; preds = %3
%13 = load i64, i64* @h_errno
%14 = load i64, i64* @NO_DATA
%15 = icmp eq i64 %13, %14
br i1 %15, label %59, label %16
16: ; preds = %12
%17 = call i32 @herror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %59
18: ; preds = %3
%19 = bitcast i32* %4 to i8*
%20 = call i32 @ns_initparse(i8* %8, i32 %10, i32* %4)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i32, i32* @stderr
%24 = call i32 @fputs(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %23)
br label %57
25: ; preds = %18
%26 = load i32, i32* %4
%27 = load i32, i32* @ns_s_an
%28 = call i32 @ns_msg_count(i32 %26, i32 %27)
%29 = bitcast i32* %5 to i8*
%30 = icmp sgt i32 %28, 0
br i1 %30, label %33, label %55
31: ; preds = %51
%32 = icmp eq i32 %54, %28
br i1 %32, label %55, label %33
33: ; preds = %25, %31
%34 = phi i32 [ %54, %31 ], [ 0, %25 ]
%35 = load i32, i32* @ns_s_an
%36 = call i32 @ns_parserr(i32* %4, i32 %35, i32 %34, i32* %5)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %33
%39 = load i32, i32* @stderr
%40 = call i32 @fputs(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %39)
br label %55
41: ; preds = %33
%42 = load i32, i32* @ns_t_mx
%43 = icmp eq i32 %42, %1
br i1 %43, label %44, label %46
44: ; preds = %41
%45 = call i32 @process_mx_record(i32* %4, i32* %5, %struct.in_addr* %2)
br label %51
46: ; preds = %41
%47 = load i32, i32* @ns_t_a
%48 = icmp eq i32 %47, %1
br i1 %48, label %49, label %55
49: ; preds = %46
%50 = call i32 @process_a_record(i32* %4, i32* %5, %struct.in_addr* %2)
br label %51
51: ; preds = %49, %44
%52 = phi i32 [ %45, %44 ], [ %50, %49 ]
%53 = icmp eq i32 %52, -1
%54 = add nuw nsw i32 %34, 1
br i1 %53, label %55, label %31
55: ; preds = %31, %46, %51, %25, %38
%56 = phi i32 [ -1, %38 ], [ %28, %25 ], [ %28, %31 ], [ -1, %46 ], [ -1, %51 ]
br label %57
57: ; preds = %55, %22
%58 = phi i32 [ -1, %22 ], [ %56, %55 ]
br label %59
59: ; preds = %12, %57, %16
%60 = phi i32 [ -1, %16 ], [ %58, %57 ], [ 0, %12 ]
ret i32 %60
}