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

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char *, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                struct addrinfo **);
int getnameinfo(int /*<<< orphan*/, int /*<<< orphan*/, char *, int,
                int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
int main(void) {
  struct addrinfo *result;
  struct addrinfo *res;
  int error;
  error = getaddrinfo("www.example.com", NULL, NULL, &result);
  if (error != 0) {
    fprintf(stderr, "error in getaddrinfo: %s\n", gai_strerror(error));
    return EXIT_FAILURE;
  }
  for (res = result; res != NULL; res = res->ai_next) {
    char hostname[1025] = "";
    error =
        getnameinfo(res->ai_addr, res->ai_addrlen, hostname, 1025, NULL, 0, 0);
    if (error != 0) {
      fprintf(stderr, "error in getnameinfo: %s\n", gai_strerror(error));
      continue;
    }
    if (*hostname != '\0')
      printf("hostname: %s\n", hostname);
  }
  freeaddrinfo(result);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe77(%rip),%rdi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x10(%rbp),%rcx
mov    %rdx,%rsi
call   1080 <getaddrinfo@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11e3 <main+0x73>
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x424(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x424(%rbp),%edi
mov    %rax,%rdx
lea    0xe43(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12c2 <main+0x152>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12ad <main+0x13d>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x401,%edx
call   1050 <memset@plt>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
lea    -0x420(%rbp),%rdx
mov    $0x401,%ecx
xor    %eax,%eax
mov    %eax,%r8d
xor    %r9d,%r9d
movl   $0x0,(%rsp)
call   1060 <getnameinfo@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1274 <main+0x104>
mov    0x2d96(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x428(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x428(%rbp),%edi
mov    %rax,%rdx
lea    0xdc0(%rip),%rsi
call   1070 <fprintf@plt>
jmp    129c <main+0x12c>
movsbl -0x420(%rbp),%eax
cmp    $0x0,%eax
je     1297 <main+0x127>
lea    -0x420(%rbp),%rsi
lea    0xdb2(%rip),%rdi
call   1030 <printf@plt>
jmp    129c <main+0x12c>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    11eb <main+0x7b>
mov    -0x10(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    0x2d13(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
lea    0xe78(%rip),%rdi
lea    0x8(%rsp),%rcx
xor    %esi,%esi
xor    %edx,%edx
call   1080 <getaddrinfo@plt>
test   %eax,%eax
je     11c7 <main+0x57>
mov    0x2e3f(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xe5f(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    0x2e16(%rip),%rax
jmp    127c <main+0x10c>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     126e <main+0xfe>
lea    0x10(%rsp),%r12
lea    0xe63(%rip),%r14
mov    0x2df8(%rip),%r13
lea    0xe3b(%rip),%r15
jmp    1221 <main+0xb1>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x0(%r13),%ebp
mov    %eax,%edi
call   1040 <gai_strerror@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    0x8(%rbx),%rbx
test   %rbx,%rbx
je     1267 <main+0xf7>
mov    $0x401,%edx
mov    %r12,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    (%rbx),%esi
mov    0x4(%rbx),%edi
movl   $0x0,(%rsp)
mov    %r12,%rdx
mov    $0x401,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1060 <getnameinfo@plt>
test   %eax,%eax
jne    1200 <main+0x90>
cmpb   $0x0,0x10(%rsp)
je     1218 <main+0xa8>
mov    %r14,%rdi
mov    %r12,%rsi
call   1030 <printf@plt>
jmp    1218 <main+0xa8>
mov    0x8(%rsp),%rdi
jmp    1270 <main+0x100>
xor    %edi,%edi
call   1090 <freeaddrinfo@plt>
mov    0x2d54(%rip),%rax
mov    (%rax),%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, %struct.addrinfo* }
@.str = [16 x i8] c"www.example.com\00"
@stderr = global i32 0
@.str.1 = [26 x i8] c"error in getaddrinfo: %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [26 x i8] c"error in getnameinfo: %s\0A\00"
@.str.3 = [14 x i8] c"hostname: %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.addrinfo*
%3 = alloca %struct.addrinfo*
%4 = alloca i32
%5 = alloca [1025 x i8]
store i32 0, i32* %1
%6 = call i32 @getaddrinfo(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32* null, i32* null, %struct.addrinfo** %2)
store i32 %6, i32* %4
%7 = load i32, i32* %4
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %15
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = load i32, i32* %4
%12 = call i8* @gai_strerror(i32 %11)
%13 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %12)
%14 = load i32, i32* @EXIT_FAILURE
store i32 %14, i32* %1
br label %54
15: ; preds = %0
%16 = load %struct.addrinfo*, %struct.addrinfo** %2
store %struct.addrinfo* %16, %struct.addrinfo** %3
br label %17
17: ; preds = %46, %15
%18 = load %struct.addrinfo*, %struct.addrinfo** %3
%19 = icmp ne %struct.addrinfo* %18, null
br i1 %19, label %20, label %50
20: ; preds = %17
%21 = bitcast [1025 x i8]* %5 to i8*
%22 = load %struct.addrinfo*, %struct.addrinfo** %3
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i32 0, i32 1
%24 = load i32, i32* %23
%25 = load %struct.addrinfo*, %struct.addrinfo** %3
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i32 0, i32 0
%27 = load i32, i32* %26
%28 = getelementptr inbounds [1025 x i8], [1025 x i8]* %5, i64 0, i64 0
%29 = call i32 @getnameinfo(i32 %24, i32 %27, i8* %28, i32 1025, i32* null, i32 0, i32 0)
store i32 %29, i32* %4
%30 = load i32, i32* %4
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %20
%33 = load i32, i32* @stderr
%34 = load i32, i32* %4
%35 = call i8* @gai_strerror(i32 %34)
%36 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %35)
br label %46
37: ; preds = %20
%38 = getelementptr inbounds [1025 x i8], [1025 x i8]* %5, i64 0, i64 0
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %37
%43 = getelementptr inbounds [1025 x i8], [1025 x i8]* %5, i64 0, i64 0
%44 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %43)
br label %45
45: ; preds = %42, %37
br label %46
46: ; preds = %45, %32
%47 = load %struct.addrinfo*, %struct.addrinfo** %3
%48 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %47, i32 0, i32 2
%49 = load %struct.addrinfo*, %struct.addrinfo** %48
store %struct.addrinfo* %49, %struct.addrinfo** %3
br label %17
50: ; preds = %17
%51 = load %struct.addrinfo*, %struct.addrinfo** %2
%52 = call i32 @freeaddrinfo(%struct.addrinfo* %51)
%53 = load i32, i32* @EXIT_SUCCESS
store i32 %53, i32* %1
br label %54
54: ; preds = %50, %9
%55 = load i32, i32* %1
ret i32 %55
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, %struct.addrinfo* }
@.str = [16 x i8] c"www.example.com\00"
@stderr = global i32 0
@.str.1 = [26 x i8] c"error in getaddrinfo: %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [26 x i8] c"error in getnameinfo: %s\0A\00"
@.str.3 = [14 x i8] c"hostname: %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca %struct.addrinfo*
%2 = alloca [1025 x i8]
%3 = bitcast %struct.addrinfo** %1 to i8*
%4 = call i32 @getaddrinfo(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32* null, i32* null, %struct.addrinfo** %1)
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %11
6: ; preds = %0
%7 = load %struct.addrinfo*, %struct.addrinfo** %1
%8 = icmp eq %struct.addrinfo* %7, null
br i1 %8, label %38, label %9
9: ; preds = %6
%10 = getelementptr inbounds [1025 x i8], [1025 x i8]* %2, i64 0, i64 0
br label %15
11: ; preds = %0
%12 = load i32, i32* @stderr
%13 = call i8* @gai_strerror(i32 %4)
%14 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %13)
br label %41
15: ; preds = %9, %32
%16 = phi %struct.addrinfo* [ %7, %9 ], [ %34, %32 ]
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i64 0, i32 1
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i64 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @getnameinfo(i32 %18, i32 %20, i8* %10, i32 1025, i32* null, i32 0, i32 0)
%22 = icmp eq i32 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %15
%24 = load i32, i32* @stderr
%25 = call i8* @gai_strerror(i32 %21)
%26 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %25)
br label %32
27: ; preds = %15
%28 = load i8, i8* %10
%29 = icmp eq i8 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %27
%31 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %10)
br label %32
32: ; preds = %27, %30, %23
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i64 0, i32 2
%34 = load %struct.addrinfo*, %struct.addrinfo** %33
%35 = icmp eq %struct.addrinfo* %34, null
br i1 %35, label %36, label %15
36: ; preds = %32
%37 = load %struct.addrinfo*, %struct.addrinfo** %1
br label %38
38: ; preds = %36, %6
%39 = phi %struct.addrinfo* [ %37, %36 ], [ null, %6 ]
%40 = call i32 @freeaddrinfo(%struct.addrinfo* %39)
br label %41
41: ; preds = %38, %11
%42 = phi i32* [ @EXIT_FAILURE, %11 ], [ @EXIT_SUCCESS, %38 ]
%43 = load i32, i32* %42
ret i32 %43
}