#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct sockaddr_in6 {
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct addrinfo {
  int ai_family;
  char *ai_canonname;
  struct addrinfo *ai_next;
  TYPE_1__ *ai_addr;
  int /*<<< orphan*/ ai_flags;
  int /*<<< orphan*/ ai_socktype;
};
typedef int /*<<< orphan*/ hints;
struct TYPE_2__ {
  void *sa_data;
};

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int /*<<< orphan*/ AI_CANONNAME;
int PF_INET6;
int PF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char const *, int /*<<< orphan*/ *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ inet_ntop(int, void *, char *, size_t);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int, ...);
char *lookup_host(char *foundIP, size_t bufSize, const char *host) {
  struct addrinfo hints, *res;
  int errcode;
  void *ptr;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = PF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags |= AI_CANONNAME;
  errcode = getaddrinfo(host, NULL, &hints, &res);
  if (errcode != 0) {
    printf("%03d: ERROR: getaddrinfo() returned an error, %s \n", 40,
           gai_strerror(errcode));
    return "";
  }
  printf("%03d: Host: %s \n", 44, host);
  while (res) {
    inet_ntop(res->ai_family, res->ai_addr->sa_data, foundIP, bufSize);
    switch (res->ai_family) {
    case AF_INET:
      ptr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
      break;
    case AF_INET6:
      ptr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
      break;
    }
    inet_ntop(res->ai_family, ptr, foundIP, bufSize);
    printf("%03d: IPv%d address: %s (%s) \n", 59,
           res->ai_family == PF_INET6 ? 6 : 4, foundIP, res->ai_canonname);
    res = res->ai_next;
  }
  freeaddrinfo(res);
  return foundIP;
}


x86 assembly (clang, -O0):
<lookup_host>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x48(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
je     11fb <lookup_host+0x9b>
mov    -0x54(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    %rax,%rdx
lea    0xe21(%rip),%rdi
mov    $0x28,%esi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe70(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    12f6 <lookup_host+0x196>
mov    -0x20(%rbp),%rdx
lea    0xe2d(%rip),%rdi
mov    $0x2c,%esi
mov    $0x0,%al
call   1040 <printf@plt>
cmpq   $0x0,-0x50(%rbp)
je     12e5 <lookup_host+0x185>
mov    -0x50(%rbp),%rax
mov    (%rax),%edi
mov    0x18(%rax),%rax
mov    (%rax),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1030 <inet_ntop@plt>
mov    -0x50(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
sub    $0x80,%eax
je     1274 <lookup_host+0x114>
jmp    1250 <lookup_host+0xf0>
mov    -0x64(%rbp),%eax
sub    $0x81,%eax
jne    1280 <lookup_host+0x120>
jmp    1263 <lookup_host+0x103>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x60(%rbp)
jmp    1280 <lookup_host+0x120>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x60(%rbp)
mov    -0x50(%rbp),%rax
mov    (%rax),%edi
mov    -0x60(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1030 <inet_ntop@plt>
mov    -0x50(%rbp),%rax
mov    (%rax),%ecx
mov    0x2d3c(%rip),%rax
mov    (%rax),%esi
mov    $0x4,%edx
mov    $0x6,%eax
cmp    %esi,%ecx
cmove  %eax,%edx
mov    -0x10(%rbp),%rcx
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%r8
lea    0xd7c(%rip),%rdi
mov    $0x3b,%esi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x50(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    1212 <lookup_host+0xb2>
mov    -0x50(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lookup_host>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x8(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2c(%rsp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
or     %eax,0x28(%rsp)
mov    %rsp,%rcx
mov    %r15,%rdi
xor    %esi,%esi
mov    %r12,%rdx
call   1070 <getaddrinfo@plt>
test   %eax,%eax
je     11ee <lookup_host+0x8e>
mov    %eax,%edi
call   1050 <gai_strerror@plt>
lea    0xe2d(%rip),%rdi
mov    $0x28,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe79(%rip),%rbx
jmp    12ad <lookup_host+0x14d>
lea    0xe3e(%rip),%rdi
mov    $0x2c,%esi
mov    %r15,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%rsp),%rax
test   %rax,%rax
je     12a6 <lookup_host+0x146>
mov    0x2dc8(%rip),%r13
lea    0xe25(%rip),%r15
jmp    1279 <lookup_host+0x119>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12,%rsi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1030 <inet_ntop@plt>
mov    (%rsp),%rax
mov    (%rax),%ecx
xor    %edx,%edx
cmp    0x0(%r13),%ecx
sete   %dl
add    %edx,%edx
add    $0x4,%edx
mov    0x8(%rax),%r8
mov    %r15,%rdi
mov    $0x3b,%esi
mov    %rbx,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%rsp),%rax
mov    0x10(%rax),%rax
mov    %rax,(%rsp)
test   %rax,%rax
je     12a6 <lookup_host+0x146>
mov    (%rax),%edi
mov    0x18(%rax),%rax
mov    (%rax),%rsi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1030 <inet_ntop@plt>
mov    (%rsp),%rax
mov    (%rax),%edi
mov    %edi,%ecx
and    $0xfffffffe,%ecx
cmp    $0x80,%ecx
jne    1230 <lookup_host+0xd0>
mov    0x18(%rax),%r12
jmp    1230 <lookup_host+0xd0>
xor    %edi,%edi
call   1080 <freeaddrinfo@plt>
mov    %rbx,%rax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i8*, %struct.addrinfo*, %struct.TYPE_2__*, i32, i32 }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@PF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@AI_CANONNAME = global i32 0
@.str = [51 x i8] c"%03d: ERROR: getaddrinfo() returned an error, %s \0A\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [17 x i8] c"%03d: Host: %s \0A\00"
@.str.3 = [31 x i8] c"%03d: IPv%d address: %s (%s) \0A\00"
@PF_INET6 = global i32 0
define i8* @lookup_host(i8* %0, i64 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca %struct.addrinfo
%9 = alloca %struct.addrinfo*
%10 = alloca i32
%11 = alloca i8*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%12 = call i32 @memset(%struct.addrinfo* %8, i32 0, i32 40)
%13 = load i32, i32* @PF_UNSPEC
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 0
store i32 %13, i32* %14
%15 = load i32, i32* @SOCK_STREAM
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 5
store i32 %15, i32* %16
%17 = load i32, i32* @AI_CANONNAME
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 4
%19 = load i32, i32* %18
%20 = or i32 %19, %17
store i32 %20, i32* %18
%21 = load i8*, i8** %7
%22 = call i32 @getaddrinfo(i8* %21, i32* null, %struct.addrinfo* %8, %struct.addrinfo** %9)
store i32 %22, i32* %10
%23 = load i32, i32* %10
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %3
%26 = load i32, i32* %10
%27 = call i8* @gai_strerror(i32 %26)
%28 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i32 40, i8* %27)
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8** %4
br label %91
29: ; preds = %3
%30 = load i8*, i8** %7
%31 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 44, i8* %30)
br label %32
32: ; preds = %64, %29
%33 = load %struct.addrinfo*, %struct.addrinfo** %9
%34 = icmp ne %struct.addrinfo* %33, null
br i1 %34, label %35, label %87
35: ; preds = %32
%36 = load %struct.addrinfo*, %struct.addrinfo** %9
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %36, i32 0, i32 0
%38 = load i32, i32* %37
%39 = load %struct.addrinfo*, %struct.addrinfo** %9
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i32 0, i32 3
%41 = load %struct.TYPE_2__*, %struct.TYPE_2__** %40
%42 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %41, i32 0, i32 0
%43 = load i8*, i8** %42
%44 = load i8*, i8** %5
%45 = load i64, i64* %6
%46 = call i32 @inet_ntop(i32 %38, i8* %43, i8* %44, i64 %45)
%47 = load %struct.addrinfo*, %struct.addrinfo** %9
%48 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %47, i32 0, i32 0
%49 = load i32, i32* %48
switch i32 %49, label %64 [
i32 129, label %50
i32 128, label %57
]
50: ; preds = %35
%51 = load %struct.addrinfo*, %struct.addrinfo** %9
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i32 0, i32 3
%53 = load %struct.TYPE_2__*, %struct.TYPE_2__** %52
%54 = bitcast %struct.TYPE_2__* %53 to %struct.sockaddr_in*
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %54, i32 0, i32 0
%56 = bitcast i32* %55 to i8*
store i8* %56, i8** %11
br label %64
57: ; preds = %35
%58 = load %struct.addrinfo*, %struct.addrinfo** %9
%59 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %58, i32 0, i32 3
%60 = load %struct.TYPE_2__*, %struct.TYPE_2__** %59
%61 = bitcast %struct.TYPE_2__* %60 to %struct.sockaddr_in6*
%62 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %61, i32 0, i32 0
%63 = bitcast i32* %62 to i8*
store i8* %63, i8** %11
br label %64
64: ; preds = %35, %57, %50
%65 = load %struct.addrinfo*, %struct.addrinfo** %9
%66 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %65, i32 0, i32 0
%67 = load i32, i32* %66
%68 = load i8*, i8** %11
%69 = load i8*, i8** %5
%70 = load i64, i64* %6
%71 = call i32 @inet_ntop(i32 %67, i8* %68, i8* %69, i64 %70)
%72 = load %struct.addrinfo*, %struct.addrinfo** %9
%73 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %72, i32 0, i32 0
%74 = load i32, i32* %73
%75 = load i32, i32* @PF_INET6
%76 = icmp eq i32 %74, %75
%77 = zext i1 %76 to i64
%78 = select i1 %76, i32 6, i32 4
%79 = load i8*, i8** %5
%80 = load %struct.addrinfo*, %struct.addrinfo** %9
%81 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %80, i32 0, i32 1
%82 = load i8*, i8** %81
%83 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 59, i32 %78, i8* %79, i8* %82)
%84 = load %struct.addrinfo*, %struct.addrinfo** %9
%85 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %84, i32 0, i32 2
%86 = load %struct.addrinfo*, %struct.addrinfo** %85
store %struct.addrinfo* %86, %struct.addrinfo** %9
br label %32
87: ; preds = %32
%88 = load %struct.addrinfo*, %struct.addrinfo** %9
%89 = call i32 @freeaddrinfo(%struct.addrinfo* %88)
%90 = load i8*, i8** %5
store i8* %90, i8** %4
br label %91
91: ; preds = %87, %25
%92 = load i8*, i8** %4
ret i8* %92
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i8*, %struct.addrinfo*, %struct.TYPE_2__*, i32, i32 }
%struct.TYPE_2__ = type { i8* }
@PF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@AI_CANONNAME = global i32 0
@.str = [51 x i8] c"%03d: ERROR: getaddrinfo() returned an error, %s \0A\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [17 x i8] c"%03d: Host: %s \0A\00"
@.str.3 = [31 x i8] c"%03d: IPv%d address: %s (%s) \0A\00"
@PF_INET6 = global i32 0
define i8* @lookup_host(i8* %0, i64 %1, i8* %2) {
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = bitcast %struct.addrinfo* %4 to i8*
%7 = bitcast %struct.addrinfo** %5 to i8*
%8 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 40)
%9 = load i32, i32* @PF_UNSPEC
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 0
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_STREAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 5
store i32 %11, i32* %12
%13 = load i32, i32* @AI_CANONNAME
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
%15 = load i32, i32* %14
%16 = or i32 %15, %13
store i32 %16, i32* %14
%17 = call i32 @getaddrinfo(i8* %2, i32* null, %struct.addrinfo* %4, %struct.addrinfo** %5)
%18 = icmp eq i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %3
%20 = call i8* @gai_strerror(i32 %17)
%21 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i32 40, i8* %20)
br label %63
22: ; preds = %3
%23 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 44, i8* %2)
%24 = load %struct.addrinfo*, %struct.addrinfo** %5
%25 = icmp eq %struct.addrinfo* %24, null
br i1 %25, label %61, label %26
26: ; preds = %22, %45
%27 = phi i8* [ %46, %45 ], [ undef, %22 ]
%28 = phi %struct.addrinfo* [ %59, %45 ], [ %24, %22 ]
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 0
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 3
%32 = load %struct.TYPE_2__*, %struct.TYPE_2__** %31
%33 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %32, i64 0, i32 0
%34 = load i8*, i8** %33
%35 = call i32 @inet_ntop(i32 %30, i8* %34, i8* %0, i64 %1)
%36 = load %struct.addrinfo*, %struct.addrinfo** %5
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %36, i64 0, i32 0
%38 = load i32, i32* %37
%39 = and i32 %38, -2
%40 = icmp eq i32 %39, 128
br i1 %40, label %41, label %45
41: ; preds = %26
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %36, i64 0, i32 3
%43 = bitcast %struct.TYPE_2__** %42 to i8**
%44 = load i8*, i8** %43
br label %45
45: ; preds = %26, %41
%46 = phi i8* [ %27, %26 ], [ %44, %41 ]
%47 = call i32 @inet_ntop(i32 %38, i8* %46, i8* %0, i64 %1)
%48 = load %struct.addrinfo*, %struct.addrinfo** %5
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %48, i64 0, i32 0
%50 = load i32, i32* %49
%51 = load i32, i32* @PF_INET6
%52 = icmp eq i32 %50, %51
%53 = select i1 %52, i32 6, i32 4
%54 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %48, i64 0, i32 1
%55 = load i8*, i8** %54
%56 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 59, i32 %53, i8* %0, i8* %55)
%57 = load %struct.addrinfo*, %struct.addrinfo** %5
%58 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %57, i64 0, i32 2
%59 = load %struct.addrinfo*, %struct.addrinfo** %58
store %struct.addrinfo* %59, %struct.addrinfo** %5
%60 = icmp eq %struct.addrinfo* %59, null
br i1 %60, label %61, label %26
61: ; preds = %45, %22
%62 = call i32 @freeaddrinfo(%struct.addrinfo* null)
br label %63
63: ; preds = %61, %19
%64 = phi i8* [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), %19 ], [ %0, %61 ]
ret i8* %64
}