#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_family;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ gai_strerror(int);
int getaddrinfo(char const *, char *, struct addrinfo *, struct addrinfo **);
char *gettext(char *);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ set_error(char *, char const *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int resolve_host(const char *host, struct addrinfo **ai, char use_ipv6,
                 int portnr) {
  int rc = -1;
  char servname[10];
  struct addrinfo myaddr;
  memset(&myaddr, 0, sizeof myaddr);
  myaddr.ai_socktype = SOCK_STREAM;
  myaddr.ai_protocol = IPPROTO_TCP;
  myaddr.ai_family = use_ipv6 ? AF_INET6 : AF_INET;
  snprintf(servname, sizeof servname, "%d", portnr);
  rc = getaddrinfo(host, servname, &myaddr, ai);
  if (rc != 0)
    set_error(gettext("Resolving %s %sfailed: %s"), host,
              use_ipv6 ? gettext("(IPv6) ") : "", gai_strerror(rc));
  return rc;
}


x86 assembly (clang, -O0):
<resolve_host>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %dl,%al
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
movsbl -0x11(%rbp),%eax
cmp    $0x0,%eax
je     11c5 <resolve_host+0x65>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
jmp    11d1 <resolve_host+0x71>
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x38(%rbp)
lea    -0x26(%rbp),%rdi
mov    -0x18(%rbp),%ecx
mov    $0xa,%esi
lea    0xe16(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x26(%rbp),%rsi
mov    -0x10(%rbp),%rcx
lea    -0x38(%rbp),%rdx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     127e <resolve_host+0x11e>
lea    0xdeb(%rip),%rdi
call   1030 <gettext@plt>
mov    %rax,-0x50(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
movsbl -0x11(%rbp),%eax
cmp    $0x0,%eax
je     124b <resolve_host+0xeb>
lea    0xde0(%rip),%rdi
call   1030 <gettext@plt>
mov    %rax,-0x58(%rbp)
jmp    125b <resolve_host+0xfb>
lea    0xdd2(%rip),%rax
mov    %rax,-0x58(%rbp)
jmp    125b <resolve_host+0xfb>
mov    -0x58(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x60(%rbp),%rdx
mov    %eax,%ecx
call   1070 <set_error@plt>
mov    -0x1c(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve_host>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%ebx
mov    %edx,%r13d
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsp,%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
test   %r13b,%r13b
jne    11b3 <resolve_host+0x53>
mov    0x2e37(%rip),%rax
jmp    11ba <resolve_host+0x5a>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
lea    0xe3a(%rip),%rdx
lea    0xe(%rsp),%rbp
mov    %rbp,%rdi
mov    $0xa,%esi
mov    %ebx,%ecx
call   1040 <snprintf@plt>
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %r12,%rdx
mov    %r15,%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,%ebx
test   %eax,%eax
je     1234 <resolve_host+0xd4>
lea    0xe0b(%rip),%rdi
call   1030 <gettext@plt>
mov    %rax,%r15
test   %r13b,%r13b
je     1216 <resolve_host+0xb6>
lea    0xe11(%rip),%rdi
call   1030 <gettext@plt>
mov    %rax,%rbp
jmp    121d <resolve_host+0xbd>
lea    0xe07(%rip),%rbp
mov    %ebx,%edi
call   1050 <gai_strerror@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbp,%rdx
mov    %eax,%ecx
call   1070 <set_error@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32 }
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@AF_INET6 = global i32 0
@AF_INET = global i32 0
@.str = [3 x i8] c"%d\00"
@.str.1 = [26 x i8] c"Resolving %s %sfailed: %s\00"
@.str.2 = [8 x i8] c"(IPv6) \00"
@.str.3 = [1 x i8] zeroinitializer
define i32 @resolve_host(i8* %0, %struct.addrinfo** %1, i8 signext %2, i32 %3) {
%5 = alloca i8*
%6 = alloca %struct.addrinfo**
%7 = alloca i8
%8 = alloca i32
%9 = alloca i32
%10 = alloca [10 x i8]
%11 = alloca %struct.addrinfo
store i8* %0, i8** %5
store %struct.addrinfo** %1, %struct.addrinfo*** %6
store i8 %2, i8* %7
store i32 %3, i32* %8
store i32 -1, i32* %9
%12 = call i32 @memset(%struct.addrinfo* %11, i32 0, i32 12)
%13 = load i32, i32* @SOCK_STREAM
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %11, i32 0, i32 2
store i32 %13, i32* %14
%15 = load i32, i32* @IPPROTO_TCP
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %11, i32 0, i32 1
store i32 %15, i32* %16
%17 = load i8, i8* %7
%18 = sext i8 %17 to i32
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %4
%21 = load i32, i32* @AF_INET6
br label %24
22: ; preds = %4
%23 = load i32, i32* @AF_INET
br label %24
24: ; preds = %22, %20
%25 = phi i32 [ %21, %20 ], [ %23, %22 ]
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %11, i32 0, i32 0
store i32 %25, i32* %26
%27 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%28 = load i32, i32* %8
%29 = call i32 @snprintf(i8* %27, i32 10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %28)
%30 = load i8*, i8** %5
%31 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%32 = load %struct.addrinfo**, %struct.addrinfo*** %6
%33 = call i32 @getaddrinfo(i8* %30, i8* %31, %struct.addrinfo* %11, %struct.addrinfo** %32)
store i32 %33, i32* %9
%34 = load i32, i32* %9
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %50
36: ; preds = %24
%37 = call i8* @gettext(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%38 = load i8*, i8** %5
%39 = load i8, i8* %7
%40 = sext i8 %39 to i32
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %36
%43 = call i8* @gettext(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %45
44: ; preds = %36
br label %45
45: ; preds = %44, %42
%46 = phi i8* [ %43, %42 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), %44 ]
%47 = load i32, i32* %9
%48 = call i32 @gai_strerror(i32 %47)
%49 = call i32 @set_error(i8* %37, i8* %38, i8* %46, i32 %48)
br label %50
50: ; preds = %45, %24
%51 = load i32, i32* %9
ret i32 %51
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32 }
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@AF_INET6 = global i32 0
@AF_INET = global i32 0
@.str = [3 x i8] c"%d\00"
@.str.1 = [26 x i8] c"Resolving %s %sfailed: %s\00"
@.str.2 = [8 x i8] c"(IPv6) \00"
@.str.3 = [1 x i8] zeroinitializer
define i32 @resolve_host(i8* %0, %struct.addrinfo** %1, i8 signext %2, i32 %3) {
%5 = alloca [10 x i8]
%6 = alloca %struct.addrinfo
%7 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%8 = bitcast %struct.addrinfo* %6 to i8*
%9 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 12)
%10 = load i32, i32* @SOCK_STREAM
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i64 0, i32 2
store i32 %10, i32* %11
%12 = load i32, i32* @IPPROTO_TCP
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i64 0, i32 1
store i32 %12, i32* %13
%14 = icmp eq i8 %2, 0
%15 = load i32, i32* @AF_INET6
%16 = load i32, i32* @AF_INET
%17 = select i1 %14, i32 %16, i32 %15
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i64 0, i32 0
store i32 %17, i32* %18
%19 = call i32 @snprintf(i8* %7, i32 10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %3)
%20 = call i32 @getaddrinfo(i8* %0, i8* %7, %struct.addrinfo* %6, %struct.addrinfo** %1)
%21 = icmp eq i32 %20, 0
br i1 %21, label %30, label %22
22: ; preds = %4
%23 = call i8* @gettext(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br i1 %14, label %26, label %24
24: ; preds = %22
%25 = call i8* @gettext(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %22, %24
%27 = phi i8* [ %25, %24 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), %22 ]
%28 = call i32 @gai_strerror(i32 %20)
%29 = call i32 @set_error(i8* %23, i8* %0, i8* %27, i32 %28)
br label %30
30: ; preds = %26, %4
ret i32 %20
}