#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 sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct addrinfo {
  char *ai_canonname;
  scalar_t__ ai_addr;
  struct addrinfo *ai_next;
  scalar_t__ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  scalar_t__ ai_flags;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int EAI_SYSTEM;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char *, int /*<<< orphan*/ *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ getchar();
char *inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int loop(void) {
  struct addrinfo *ai;
  struct addrinfo *ai_iter;
  struct addrinfo hints;
  char *name;
  int res;
  printf("Press any key to continue...");
  getchar();
  printf("\n");
  hints.ai_flags = 0;
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = 0;
  name = "www.facebook.com";
  res = getaddrinfo(name, NULL, &hints, &ai);
  if (res != 0) {
    if (res == EAI_SYSTEM) {
      perror("getaddrinfo");
    } else {
      fprintf(stderr, "error: getaddrinfo: %d\n", res);
    }
    return 1;
  }
  for (ai_iter = ai; ai_iter != NULL; ai_iter = ai_iter->ai_next) {
    struct sockaddr_in *addr;
    addr = (struct sockaddr_in *)ai_iter->ai_addr;
    printf("%s: %s\n", ai_iter->ai_canonname, inet_ntoa(addr->sin_addr));
  }
  freeaddrinfo(ai);
  return 0;
}


x86 assembly (clang, -O0):
<loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
lea    0xe81(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <getchar@plt>
lea    0xebc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x20(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
lea    0xe53(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x48(%rbp),%rdx
lea    -0x10(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
je     1237 <loop+0xc7>
mov    -0x54(%rbp),%eax
mov    0x2de6(%rip),%rcx
cmp    (%rcx),%eax
jne    1213 <loop+0xa3>
lea    0xe25(%rip),%rdi
call   1070 <perror@plt>
jmp    122b <loop+0xbb>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
mov    -0x54(%rbp),%edx
lea    0xe14(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a2 <loop+0x132>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1292 <loop+0x122>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x60(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x68(%rbp)
mov    -0x60(%rbp),%rax
mov    (%rax),%edi
call   1030 <inet_ntoa@plt>
mov    -0x68(%rbp),%rsi
mov    %rax,%rdx
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    123f <loop+0xcf>
mov    -0x10(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loop>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x38,%rsp
lea    0xe6e(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1060 <getchar@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
movq   $0x0,0x30(%rsp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2c(%rsp)
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
movq   $0x0,0x20(%rsp)
lea    0xe40(%rip),%rdi
lea    0x8(%rsp),%rdx
mov    %rsp,%rcx
xor    %esi,%esi
call   1090 <getaddrinfo@plt>
test   %eax,%eax
je     121a <loop+0x9a>
mov    0x2de9(%rip),%rcx
cmp    (%rcx),%eax
je     1273 <loop+0xf3>
mov    0x2dce(%rip),%rcx
mov    (%rcx),%edi
lea    0xe2f(%rip),%rsi
mov    %eax,%edx
call   1070 <fprintf@plt>
mov    $0x1,%r14d
jmp    1264 <loop+0xe4>
mov    (%rsp),%rbx
xor    %r14d,%r14d
test   %rbx,%rbx
je     125d <loop+0xdd>
lea    0xe25(%rip),%r15
nopl   (%rax)
mov    (%rbx),%r12
mov    0x8(%rbx),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x10(%rbx),%rbx
test   %rbx,%rbx
jne    1230 <loop+0xb0>
mov    (%rsp),%rdi
jmp    125f <loop+0xdf>
xor    %edi,%edi
call   10a0 <freeaddrinfo@plt>
mov    %r14d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdb4(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%r14d
jmp    1264 <loop+0xe4>

LLVM IR (-O0):
%struct.addrinfo = type { i8*, i64, %struct.addrinfo*, i64, i32, i32, i64 }
%struct.sockaddr_in = type { i32 }
@.str = [29 x i8] c"Press any key to continue...\00"
@.str.1 = [2 x i8] c"\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [17 x i8] c"www.facebook.com\00"
@EAI_SYSTEM = global i32 0
@.str.3 = [12 x i8] c"getaddrinfo\00"
@stderr = global i32 0
@.str.4 = [24 x i8] c"error: getaddrinfo: %d\0A\00"
@.str.5 = [8 x i8] c"%s: %s\0A\00"
define i32 @loop() {
%1 = alloca i32
%2 = alloca %struct.addrinfo*
%3 = alloca %struct.addrinfo*
%4 = alloca %struct.addrinfo
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.sockaddr_in*
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%9 = call i32 (...) @getchar()
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 6
store i64 0, i64* %11
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 5
store i32 %12, i32* %13
%14 = load i32, i32* @SOCK_STREAM
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 4
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 3
store i64 0, i64* %16
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8** %5
%17 = load i8*, i8** %5
%18 = call i32 @getaddrinfo(i8* %17, i32* null, %struct.addrinfo* %4, %struct.addrinfo** %2)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %32
21: ; preds = %0
%22 = load i32, i32* %6
%23 = load i32, i32* @EAI_SYSTEM
%24 = icmp eq i32 %22, %23
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %31
27: ; preds = %21
%28 = load i32, i32* @stderr
%29 = load i32, i32* %6
%30 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %29)
br label %31
31: ; preds = %27, %25
store i32 1, i32* %1
br label %57
32: ; preds = %0
%33 = load %struct.addrinfo*, %struct.addrinfo** %2
store %struct.addrinfo* %33, %struct.addrinfo** %3
br label %34
34: ; preds = %50, %32
%35 = load %struct.addrinfo*, %struct.addrinfo** %3
%36 = icmp ne %struct.addrinfo* %35, null
br i1 %36, label %37, label %54
37: ; preds = %34
%38 = load %struct.addrinfo*, %struct.addrinfo** %3
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 1
%40 = load i64, i64* %39
%41 = inttoptr i64 %40 to %struct.sockaddr_in*
store %struct.sockaddr_in* %41, %struct.sockaddr_in** %7
%42 = load %struct.addrinfo*, %struct.addrinfo** %3
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %42, i32 0, i32 0
%44 = load i8*, i8** %43
%45 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %45, i32 0, i32 0
%47 = load i32, i32* %46
%48 = call i8* @inet_ntoa(i32 %47)
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* %44, i8* %48)
br label %50
50: ; preds = %37
%51 = load %struct.addrinfo*, %struct.addrinfo** %3
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i32 0, i32 2
%53 = load %struct.addrinfo*, %struct.addrinfo** %52
store %struct.addrinfo* %53, %struct.addrinfo** %3
br label %34
54: ; preds = %34
%55 = load %struct.addrinfo*, %struct.addrinfo** %2
%56 = call i32 @freeaddrinfo(%struct.addrinfo* %55)
store i32 0, i32* %1
br label %57
57: ; preds = %54, %31
%58 = load i32, i32* %1
ret i32 %58
}

LLVM IR (-O2):
%struct.addrinfo = type { i8*, i64, %struct.addrinfo*, i64, i32, i32, i64 }
%struct.sockaddr_in = type { i32 }
@.str = [29 x i8] c"Press any key to continue...\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [17 x i8] c"www.facebook.com\00"
@EAI_SYSTEM = global i32 0
@.str.3 = [12 x i8] c"getaddrinfo\00"
@stderr = global i32 0
@.str.4 = [24 x i8] c"error: getaddrinfo: %d\0A\00"
@.str.5 = [8 x i8] c"%s: %s\0A\00"
define i32 @loop() {
%1 = alloca %struct.addrinfo*
%2 = alloca %struct.addrinfo
%3 = bitcast %struct.addrinfo** %1 to i8*
%4 = bitcast %struct.addrinfo* %2 to i8*
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 (...) @getchar()
%7 = tail call i32 @putchar(i32 10)
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 6
store i64 0, i64* %8
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 5
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_STREAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 4
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 3
store i64 0, i64* %13
%14 = call i32 @getaddrinfo(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32* null, %struct.addrinfo* %2, %struct.addrinfo** %1)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %0
%17 = load %struct.addrinfo*, %struct.addrinfo** %1
%18 = icmp eq %struct.addrinfo* %17, null
br i1 %18, label %43, label %27
19: ; preds = %0
%20 = load i32, i32* @EAI_SYSTEM
%21 = icmp eq i32 %14, %20
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %46
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %14)
br label %46
27: ; preds = %16, %27
%28 = phi %struct.addrinfo* [ %39, %27 ], [ %17, %16 ]
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 1
%30 = load i64, i64* %29
%31 = inttoptr i64 %30 to %struct.sockaddr_in*
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 0
%33 = load i8*, i8** %32
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %31, i64 0, i32 0
%35 = load i32, i32* %34
%36 = call i8* @inet_ntoa(i32 %35)
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* %33, i8* %36)
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 2
%39 = load %struct.addrinfo*, %struct.addrinfo** %38
%40 = icmp eq %struct.addrinfo* %39, null
br i1 %40, label %41, label %27
41: ; preds = %27
%42 = load %struct.addrinfo*, %struct.addrinfo** %1
br label %43
43: ; preds = %41, %16
%44 = phi %struct.addrinfo* [ %42, %41 ], [ null, %16 ]
%45 = call i32 @freeaddrinfo(%struct.addrinfo* %44)
br label %46
46: ; preds = %22, %24, %43
%47 = phi i32 [ 0, %43 ], [ 1, %24 ], [ 1, %22 ]
ret i32 %47
}