#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_DGRAM;
char *gai_strerror(int);
int getaddrinfo(char const *, int /*<<< orphan*/ *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ inet_ntop(int, void *, char *, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int lookup_host(const char *host) {
  struct addrinfo hints, *res;
  int errcode;
  char addrstr[100];
  void *ptr;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = PF_UNSPEC;
  hints.ai_socktype = SOCK_DGRAM;
  hints.ai_flags |= AI_CANONNAME;
  errcode = getaddrinfo(host, NULL, &hints, &res);
  if (errcode != 0) {
    printf("errcode %d : %s\n", errcode, gai_strerror(errcode));
    return -1;
  }
  printf("Host: %s\n", host);
  while (res) {
    inet_ntop(res->ai_family, res->ai_addr->sa_data, addrstr, 100);
    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, addrstr, 100);
    printf("IPv%d address: %s (%s)\n", res->ai_family == PF_INET6 ? 6 : 4,
           addrstr, res->ai_canonname);
    res = res->ai_next;
  }
  return 0;
}


x86 assembly (clang, -O0):
<lookup_host>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
mov    0x2e72(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     11ec <lookup_host+0x9c>
mov    -0x44(%rbp),%eax
mov    %eax,-0xbc(%rbp)
mov    -0x44(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    -0xbc(%rbp),%esi
mov    %rax,%rdx
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ed <lookup_host+0x19d>
mov    -0x10(%rbp),%rsi
lea    0xe1a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpq   $0x0,-0x40(%rbp)
je     12e6 <lookup_host+0x196>
mov    -0x40(%rbp),%rax
mov    (%rax),%edi
mov    0x18(%rax),%rax
mov    (%rax),%rsi
lea    -0xb0(%rbp),%rdx
mov    $0x64,%ecx
call   1030 <inet_ntop@plt>
mov    -0x40(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0xc0(%rbp)
sub    $0x80,%eax
je     126d <lookup_host+0x11d>
jmp    1243 <lookup_host+0xf3>
mov    -0xc0(%rbp),%eax
sub    $0x81,%eax
jne    127c <lookup_host+0x12c>
jmp    1259 <lookup_host+0x109>
mov    -0x40(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0xb8(%rbp)
jmp    127c <lookup_host+0x12c>
mov    -0x40(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0xb8(%rbp)
mov    -0x40(%rbp),%rax
mov    (%rax),%edi
mov    -0xb8(%rbp),%rsi
lea    -0xb0(%rbp),%rdx
mov    $0x64,%ecx
call   1030 <inet_ntop@plt>
mov    -0x40(%rbp),%rax
mov    (%rax),%ecx
mov    0x2d39(%rip),%rax
mov    (%rax),%edx
mov    $0x4,%esi
mov    $0x6,%eax
cmp    %edx,%ecx
cmove  %eax,%esi
lea    -0xb0(%rbp),%rdx
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rcx
lea    0xd4d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    11fe <lookup_host+0xae>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lookup_host>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0xa0,%rsp
mov    %rdi,%r14
lea    0x8(%rsp),%rbp
xor    %ebx,%ebx
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
mov    0x2e69(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2c(%rsp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
or     %eax,0x28(%rsp)
mov    %rsp,%rcx
mov    %r14,%rdi
xor    %esi,%esi
mov    %rbp,%rdx
call   1070 <getaddrinfo@plt>
test   %eax,%eax
je     11e6 <lookup_host+0x96>
mov    %eax,%ebp
mov    %eax,%edi
call   1050 <gai_strerror@plt>
lea    0xe3d(%rip),%rdi
mov    %ebp,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0xa0,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe24(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%rsp),%rax
test   %rax,%rax
je     11d4 <lookup_host+0x84>
lea    0x30(%rsp),%r14
mov    0x2dd4(%rip),%rbp
lea    0xe08(%rip),%r15
jmp    126c <lookup_host+0x11c>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,%rsi
mov    %r14,%rdx
mov    $0x64,%ecx
call   1030 <inet_ntop@plt>
mov    (%rsp),%rax
mov    (%rax),%ecx
xor    %edx,%edx
cmp    0x0(%rbp),%ecx
sete   %dl
lea    (%rdx,%rdx,1),%esi
add    $0x4,%esi
mov    0x8(%rax),%rcx
xor    %ebx,%ebx
mov    %r15,%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%rsp),%rax
mov    0x10(%rax),%rax
mov    %rax,(%rsp)
test   %rax,%rax
je     11d4 <lookup_host+0x84>
mov    (%rax),%edi
mov    0x18(%rax),%rax
mov    (%rax),%rsi
mov    %r14,%rdx
mov    $0x64,%ecx
call   1030 <inet_ntop@plt>
mov    (%rsp),%rax
mov    (%rax),%edi
mov    %edi,%ecx
and    $0xfffffffe,%ecx
cmp    $0x80,%ecx
jne    1220 <lookup_host+0xd0>
mov    0x18(%rax),%r12
jmp    1220 <lookup_host+0xd0>

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

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_DGRAM = global i32 0
@AI_CANONNAME = global i32 0
@.str = [17 x i8] c"errcode %d : %s\0A\00"
@.str.1 = [10 x i8] c"Host: %s\0A\00"
@.str.2 = [24 x i8] c"IPv%d address: %s (%s)\0A\00"
@PF_INET6 = global i32 0
define i32 @lookup_host(i8* %0) {
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = alloca [100 x i8]
%5 = bitcast %struct.addrinfo* %2 to i8*
%6 = bitcast %struct.addrinfo** %3 to i8*
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%8 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 40)
%9 = load i32, i32* @PF_UNSPEC
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 0
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_DGRAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 5
store i32 %11, i32* %12
%13 = load i32, i32* @AI_CANONNAME
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 4
%15 = load i32, i32* %14
%16 = or i32 %15, %13
store i32 %16, i32* %14
%17 = call i32 @getaddrinfo(i8* %0, i32* null, %struct.addrinfo* %2, %struct.addrinfo** %3)
%18 = icmp eq i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %1
%20 = call i8* @gai_strerror(i32 %17)
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %17, i8* %20)
br label %61
22: ; preds = %1
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%24 = load %struct.addrinfo*, %struct.addrinfo** %3
%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* %7, i32 100)
%36 = load %struct.addrinfo*, %struct.addrinfo** %3
%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* %7, i32 100)
%48 = load %struct.addrinfo*, %struct.addrinfo** %3
%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*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %53, i8* %7, i8* %55)
%57 = load %struct.addrinfo*, %struct.addrinfo** %3
%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** %3
%60 = icmp eq %struct.addrinfo* %59, null
br i1 %60, label %61, label %26
61: ; preds = %45, %22, %19
%62 = phi i32 [ -1, %19 ], [ 0, %22 ], [ 0, %45 ]
ret i32 %62
}