#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 */

/* Variables and functions */
#define EAI_AGAIN 130
#define EAI_FAIL 129
#define EAI_NONAME 128
int /*<<< orphan*/ HOST_NOT_FOUND;
int /*<<< orphan*/ NO_RECOVERY;
int /*<<< orphan*/ TRY_AGAIN;
int __dns_doqueries(unsigned char *, char const *, int *, int);
int /*<<< orphan*/ h_errno;
int res_query(const char *name, int class, int type, unsigned char *dest,
              int len) {
  if (class != 1 || len < 512)
    return -1;
  switch (__dns_doqueries(dest, name, &type, 1)) {
  case EAI_NONAME:
    h_errno = HOST_NOT_FOUND;
    return -1;
  case EAI_AGAIN:
    h_errno = TRY_AGAIN;
    return -1;
  case EAI_FAIL:
    h_errno = NO_RECOVERY;
    return -1;
  }
  return 512;
}


x86 assembly (clang, -O0):
<res_query>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
cmpl   $0x1,-0x14(%rbp)
jne    1141 <res_query+0x31>
cmpl   $0x200,-0x24(%rbp)
jge    114d <res_query+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ff <res_query+0xef>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdx
mov    $0x1,%ecx
call   1030 <__dns_doqueries@plt>
mov    %eax,%ecx
mov    %ecx,-0x28(%rbp)
sub    $0x80,%eax
je     119e <res_query+0x8e>
jmp    1178 <res_query+0x68>
mov    -0x28(%rbp),%eax
sub    $0x81,%eax
je     11da <res_query+0xca>
jmp    118b <res_query+0x7b>
mov    -0x28(%rbp),%eax
sub    $0x82,%eax
je     11bc <res_query+0xac>
jmp    11f8 <res_query+0xe8>
mov    0x2e3b(%rip),%rax
mov    (%rax),%ecx
mov    0x2e3a(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11ff <res_query+0xef>
mov    0x2e0d(%rip),%rax
mov    (%rax),%ecx
mov    0x2e1c(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11ff <res_query+0xef>
mov    0x2de7(%rip),%rax
mov    (%rax),%ecx
mov    0x2dfe(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11ff <res_query+0xef>
movl   $0x200,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<res_query>:
push   %rax
mov    %edx,0x4(%rsp)
mov    $0xffffffff,%eax
cmp    $0x1,%esi
jne    116d <res_query+0x5d>
cmp    $0x200,%r8d
jl     116d <res_query+0x5d>
mov    %rdi,%r9
lea    0x4(%rsp),%rdx
mov    %rcx,%rdi
mov    %r9,%rsi
mov    $0x1,%ecx
call   1030 <__dns_doqueries@plt>
mov    %eax,%ecx
add    $0xffffff80,%ecx
mov    $0x200,%eax
cmp    $0x2,%ecx
ja     116d <res_query+0x5d>
movslq %ecx,%rax
lea    0x2ca7(%rip),%rcx
mov    (%rcx,%rax,8),%rax
mov    (%rax),%eax
mov    0x2e82(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
pop    %rcx
ret

LLVM IR (-O0):
@HOST_NOT_FOUND = global i32 0
@h_errno = global i32 0
@TRY_AGAIN = global i32 0
@NO_RECOVERY = global i32 0
define i32 @res_query(i8* %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
store i8* %0, i8** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i32 %4, i32* %11
%12 = load i32, i32* %8
%13 = icmp ne i32 %12, 1
br i1 %13, label %17, label %14
14: ; preds = %5
%15 = load i32, i32* %11
%16 = icmp slt i32 %15, 512
br i1 %16, label %17, label %18
17: ; preds = %14, %5
store i32 -1, i32* %6
br label %29
18: ; preds = %14
%19 = load i8*, i8** %10
%20 = load i8*, i8** %7
%21 = call i32 @__dns_doqueries(i8* %19, i8* %20, i32* %9, i32 1)
switch i32 %21, label %28 [
i32 128, label %22
i32 130, label %24
i32 129, label %26
]
22: ; preds = %18
%23 = load i32, i32* @HOST_NOT_FOUND
store i32 %23, i32* @h_errno
store i32 -1, i32* %6
br label %29
24: ; preds = %18
%25 = load i32, i32* @TRY_AGAIN
store i32 %25, i32* @h_errno
store i32 -1, i32* %6
br label %29
26: ; preds = %18
%27 = load i32, i32* @NO_RECOVERY
store i32 %27, i32* @h_errno
store i32 -1, i32* %6
br label %29
28: ; preds = %18
store i32 512, i32* %6
br label %29
29: ; preds = %28, %26, %24, %22, %17
%30 = load i32, i32* %6
ret i32 %30
}

LLVM IR (-O2):
@HOST_NOT_FOUND = global i32 0
@h_errno = global i32 0
@TRY_AGAIN = global i32 0
@NO_RECOVERY = global i32 0
@switch.table.res_query = [3 x i32*] [i32* @HOST_NOT_FOUND, i32* @NO_RECOVERY, i32* @TRY_AGAIN]
define i32 @res_query(i8* %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i32
store i32 %2, i32* %6
%7 = icmp ne i32 %1, 1
%8 = icmp slt i32 %4, 512
%9 = or i1 %7, %8
br i1 %9, label %19, label %10
10: ; preds = %5
%11 = call i32 @__dns_doqueries(i8* %3, i8* %0, i32* %6, i32 1)
%12 = add i32 %11, -128
%13 = icmp ult i32 %12, 3
br i1 %13, label %14, label %19
14: ; preds = %10
%15 = sext i32 %12 to i64
%16 = getelementptr inbounds [3 x i32*], [3 x i32*]* @switch.table.res_query, i64 0, i64 %15
%17 = load i32*, i32** %16
%18 = load i32, i32* %17
store i32 %18, i32* @h_errno
br label %19
19: ; preds = %10, %14, %5
%20 = phi i32 [ -1, %5 ], [ 512, %10 ], [ -1, %14 ]
ret i32 %20
}