#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 EAI_SYSTEM;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
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*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
int main(void) {
  struct addrinfo *result;
  struct addrinfo *res;
  int error;
  error = getaddrinfo("www.baidu.com", NULL, NULL, &result);
  if (error != 0) {
    if (error == EAI_SYSTEM) {
      perror("getaddrinfo");
    } else {
      fprintf(stderr, "error in getaddrinfo: %s\n", gai_strerror(error));
    }
    exit(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 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe57(%rip),%rdi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x10(%rbp),%rcx
mov    %rdx,%rsi
call   10a0 <getaddrinfo@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1223 <main+0x93>
mov    -0x1c(%rbp),%eax
mov    0x2e18(%rip),%rcx
cmp    (%rcx),%eax
jne    11e9 <main+0x59>
lea    0xe2f(%rip),%rdi
call   1080 <perror@plt>
jmp    1215 <main+0x85>
mov    0x2de8(%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    0xe0a(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12ed <main+0x15d>
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     12b4 <main+0x124>
mov    0x2d4e(%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    0xd8a(%rip),%rsi
call   1070 <fprintf@plt>
jmp    12dc <main+0x14c>
movsbl -0x420(%rbp),%eax
cmp    $0x0,%eax
je     12d7 <main+0x147>
lea    -0x420(%rbp),%rsi
lea    0xd7c(%rip),%rdi
call   1030 <printf@plt>
jmp    12dc <main+0x14c>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    122b <main+0x9b>
mov    -0x10(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
xor    %eax,%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    0xe58(%rip),%rdi
lea    0x8(%rsp),%rcx
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <getaddrinfo@plt>
test   %eax,%eax
jne    1279 <main+0xe9>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     125e <main+0xce>
lea    0x10(%rsp),%r12
lea    0xe76(%rip),%r14
mov    0x2df9(%rip),%r13
lea    0xe4e(%rip),%r15
jmp    1211 <main+0x81>
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     1257 <main+0xc7>
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    11f0 <main+0x60>
cmpb   $0x0,0x10(%rsp)
je     1208 <main+0x78>
mov    %r14,%rdi
mov    %r12,%rsi
call   1030 <printf@plt>
jmp    1208 <main+0x78>
mov    0x8(%rsp),%rdi
jmp    1260 <main+0xd0>
xor    %edi,%edi
call   10b0 <freeaddrinfo@plt>
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d68(%rip),%rcx
cmp    (%rcx),%eax
jne    129e <main+0x10e>
lea    0xd83(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2d33(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xd65(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
jmp    1290 <main+0x100>

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

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