#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 */
struct addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
int NI_MAXHOST;
int /*<<< orphan*/ NI_NUMERICHOST;
int /*<<< orphan*/ PF_UNSPEC;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char *, int /*<<< orphan*/ *, struct addrinfo *,
                struct addrinfo **);
int getnameinfo(int /*<<< orphan*/, int /*<<< orphan*/, char *, int,
                int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
void dnsquery(char *addr) {
  struct addrinfo hints, *query, *tmpquery;
  int error;
  char host[NI_MAXHOST];
  memset(&hints, 0, sizeof hints);
  hints.ai_family = PF_UNSPEC;
  hints.ai_socktype = SOCK_DGRAM;
  error = getaddrinfo(addr, NULL, &hints, &tmpquery);
  if (error) {
    fprintf(stderr, "%s\n", gai_strerror(error));
    exit(1);
  }
  for (query = tmpquery; query; query = query->ai_next) {
    error = getnameinfo(query->ai_addr, query->ai_addrlen, host, sizeof host,
                        NULL, 0, NI_NUMERICHOST);
    if (error)
      fprintf(stderr, "%s\n", gai_strerror(error));
    else
      printf("%s\n", host);
  }
  freeaddrinfo(tmpquery);
}


x86 assembly (clang, -O0):
<dnsquery>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x58(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x48(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x20(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     1236 <dnsquery+0xb6>
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x34(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x5c(%rbp),%edi
mov    %rax,%rdx
lea    0xdd9(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12dd <dnsquery+0x15d>
mov    -0x50(%rbp),%rdx
mov    -0x58(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    0x4(%rcx),%edi
mov    -0x28(%rbp),%rcx
mov    (%rcx),%esi
mov    %eax,%ecx
mov    0x2d61(%rip),%rax
mov    (%rax),%eax
xor    %r8d,%r8d
xor    %r9d,%r9d
sub    $0x10,%rsp
mov    %eax,(%rsp)
call   1060 <getnameinfo@plt>
add    $0x10,%rsp
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     12b7 <dnsquery+0x137>
mov    0x2d3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x60(%rbp)
mov    -0x34(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x60(%rbp),%edi
mov    %rax,%rdx
lea    0xd53(%rip),%rsi
call   1070 <fprintf@plt>
jmp    12c7 <dnsquery+0x147>
mov    -0x50(%rbp),%rsi
lea    0xd3e(%rip),%rdi
call   1030 <printf@plt>
jmp    12cc <dnsquery+0x14c>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    123e <dnsquery+0xbe>
mov    -0x30(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    -0x40(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dnsquery>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r15
mov    %rsp,-0x38(%rbp)
mov    0x2e21(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%r13
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
lea    -0x50(%rbp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2e07(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
lea    -0x30(%rbp),%rcx
mov    %r15,%rdi
xor    %esi,%esi
mov    %r12,%rdx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
jne    1291 <dnsquery+0x111>
mov    -0x30(%rbp),%rbx
test   %rbx,%rbx
je     1277 <dnsquery+0xf7>
lea    0xdf9(%rip),%r15
jmp    1224 <dnsquery+0xa4>
nopl   0x0(%rax)
mov    %r15,%rdi
mov    %r13,%rsi
call   1030 <printf@plt>
mov    0x8(%rbx),%rbx
test   %rbx,%rbx
je     1271 <dnsquery+0xf1>
mov    (%rbx),%esi
mov    0x4(%rbx),%edi
mov    0x2d98(%rip),%rax
mov    (%rax),%eax
sub    $0x8,%rsp
mov    %r13,%rdx
mov    %r14d,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
push   %rax
call   1060 <getnameinfo@plt>
add    $0x10,%rsp
test   %eax,%eax
je     1210 <dnsquery+0x90>
mov    0x2d79(%rip),%rcx
mov    (%rcx),%r12d
mov    %eax,%edi
call   1040 <gai_strerror@plt>
mov    %r12d,%edi
mov    %r15,%rsi
mov    %rax,%rdx
call   1070 <fprintf@plt>
jmp    121b <dnsquery+0x9b>
mov    -0x30(%rbp),%rdi
jmp    1279 <dnsquery+0xf9>
xor    %edi,%edi
call   10a0 <freeaddrinfo@plt>
mov    -0x38(%rbp),%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d38(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xd58(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, %struct.addrinfo*, i32, i32 }
@NI_MAXHOST = global i32 0
@PF_UNSPEC = global i32 0
@SOCK_DGRAM = global i32 0
@stderr = global i32 0
@.str = [4 x i8] c"%s\0A\00"
@NI_NUMERICHOST = global i32 0
define void @dnsquery(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = alloca %struct.addrinfo*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
store i8* %0, i8** %2
%9 = load i32, i32* @NI_MAXHOST
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %7
%12 = alloca i8, i64 %10
store i64 %10, i64* %8
%13 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 24)
%14 = load i32, i32* @PF_UNSPEC
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i32 0, i32 4
store i32 %14, i32* %15
%16 = load i32, i32* @SOCK_DGRAM
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i32 0, i32 3
store i32 %16, i32* %17
%18 = load i8*, i8** %2
%19 = call i32 @getaddrinfo(i8* %18, i32* null, %struct.addrinfo* %3, %struct.addrinfo** %5)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %1
%23 = load i32, i32* @stderr
%24 = load i32, i32* %6
%25 = call i8* @gai_strerror(i32 %24)
%26 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %25)
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %1
%29 = load %struct.addrinfo*, %struct.addrinfo** %5
store %struct.addrinfo* %29, %struct.addrinfo** %4
br label %30
30: ; preds = %53, %28
%31 = load %struct.addrinfo*, %struct.addrinfo** %4
%32 = icmp ne %struct.addrinfo* %31, null
br i1 %32, label %33, label %57
33: ; preds = %30
%34 = load %struct.addrinfo*, %struct.addrinfo** %4
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i32 0, i32 1
%36 = load i32, i32* %35
%37 = load %struct.addrinfo*, %struct.addrinfo** %4
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i32 0, i32 0
%39 = load i32, i32* %38
%40 = trunc i64 %10 to i32
%41 = load i32, i32* @NI_NUMERICHOST
%42 = call i32 @getnameinfo(i32 %36, i32 %39, i8* %12, i32 %40, i32* null, i32 0, i32 %41)
store i32 %42, i32* %6
%43 = load i32, i32* %6
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %50
45: ; preds = %33
%46 = load i32, i32* @stderr
%47 = load i32, i32* %6
%48 = call i8* @gai_strerror(i32 %47)
%49 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %48)
br label %52
50: ; preds = %33
%51 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %52
52: ; preds = %50, %45
br label %53
53: ; preds = %52
%54 = load %struct.addrinfo*, %struct.addrinfo** %4
%55 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %54, i32 0, i32 2
%56 = load %struct.addrinfo*, %struct.addrinfo** %55
store %struct.addrinfo* %56, %struct.addrinfo** %4
br label %30
57: ; preds = %30
%58 = load %struct.addrinfo*, %struct.addrinfo** %5
%59 = call i32 @freeaddrinfo(%struct.addrinfo* %58)
%60 = load i8*, i8** %7
ret void
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, %struct.addrinfo*, i32, i32 }
@NI_MAXHOST = global i32 0
@PF_UNSPEC = global i32 0
@SOCK_DGRAM = global i32 0
@stderr = global i32 0
@.str = [4 x i8] c"%s\0A\00"
@NI_NUMERICHOST = global i32 0
define void @dnsquery(i8* %0) {
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = bitcast %struct.addrinfo* %2 to i8*
%5 = bitcast %struct.addrinfo** %3 to i8*
%6 = load i32, i32* @NI_MAXHOST
%7 = zext i32 %6 to i64
%8 = tail call i8* @llvm.stacksave()
%9 = alloca i8, i64 %7
%10 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 24)
%11 = load i32, i32* @PF_UNSPEC
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 4
store i32 %11, i32* %12
%13 = load i32, i32* @SOCK_DGRAM
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 3
store i32 %13, i32* %14
%15 = call i32 @getaddrinfo(i8* %0, i32* null, %struct.addrinfo* %2, %struct.addrinfo** %3)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %1
%18 = load %struct.addrinfo*, %struct.addrinfo** %3
%19 = icmp eq %struct.addrinfo* %18, null
br i1 %19, label %46, label %25
20: ; preds = %1
%21 = load i32, i32* @stderr
%22 = call i8* @gai_strerror(i32 %15)
%23 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %22)
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %17, %40
%26 = phi %struct.addrinfo* [ %42, %40 ], [ %18, %17 ]
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 1
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 0
%30 = load i32, i32* %29
%31 = load i32, i32* @NI_NUMERICHOST
%32 = call i32 @getnameinfo(i32 %28, i32 %30, i8* %9, i32 %6, i32* null, i32 0, i32 %31)
%33 = icmp eq i32 %32, 0
br i1 %33, label %38, label %34
34: ; preds = %25
%35 = load i32, i32* @stderr
%36 = call i8* @gai_strerror(i32 %32)
%37 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %36)
br label %40
38: ; preds = %25
%39 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %40
40: ; preds = %34, %38
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 2
%42 = load %struct.addrinfo*, %struct.addrinfo** %41
%43 = icmp eq %struct.addrinfo* %42, null
br i1 %43, label %44, label %25
44: ; preds = %40
%45 = load %struct.addrinfo*, %struct.addrinfo** %3
br label %46
46: ; preds = %44, %17
%47 = phi %struct.addrinfo* [ %45, %44 ], [ null, %17 ]
%48 = call i32 @freeaddrinfo(%struct.addrinfo* %47)
ret void
}