#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 {
  int dummy;
};
struct addrinfo {
  int ai_flags;
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
};
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int AI_ADDRCONFIG;
int AI_NUMERICSERV;
int AI_PASSIVE;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *,
                           char const *, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memcpy(struct sockaddr *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
int resolve_address(struct sockaddr *sa, socklen_t *salen, const char *host,
                    const char *port) {
  struct addrinfo hints, *res;
  int err;
  memset(&hints, 0, sizeof(hints));
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = IPPROTO_TCP;
  hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV | AI_PASSIVE;
  if ((err = getaddrinfo(host, port, &hints, &res)) != 0 || res == NULL) {
    fprintf(stderr, "failed to resolve address:%s:%s:%s\n", host, port,
            err != 0 ? gai_strerror(err) : "getaddrinfo returned NULL");
    return -1;
  }
  memcpy(sa, res->ai_addr, res->ai_addrlen);
  *salen = res->ai_addrlen;
  freeaddrinfo(res);
  return 0;
}


x86 assembly (clang, -O0):
<resolve_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1040 <memset@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    0x2e48(%rip),%rcx
or     (%rcx),%eax
mov    0x2e27(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x4c(%rbp)
cmp    $0x0,%eax
jne    11ea <resolve_address+0x8a>
cmpq   $0x0,-0x48(%rbp)
jne    1258 <resolve_address+0xf8>
mov    0x2dd7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x58(%rbp)
cmpl   $0x0,-0x4c(%rbp)
je     1221 <resolve_address+0xc1>
mov    -0x4c(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    %rax,-0x70(%rbp)
jmp    1231 <resolve_address+0xd1>
lea    0xdfc(%rip),%rax
mov    %rax,-0x70(%rbp)
jmp    1231 <resolve_address+0xd1>
mov    -0x58(%rbp),%rcx
mov    -0x60(%rbp),%rdx
mov    -0x64(%rbp),%edi
mov    -0x70(%rbp),%r8
lea    0xdb9(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128c <resolve_address+0x12c>
mov    -0x10(%rbp),%rdi
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%esi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%edx
call   1060 <memcpy@plt>
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x48(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve_address>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r12
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1040 <memset@plt>
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e38(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e03(%rip),%rax
mov    0x2e44(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e21(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %rbp,%rdx
call   1070 <getaddrinfo@plt>
test   %eax,%eax
jne    1204 <resolve_address+0xa4>
mov    (%rsp),%rcx
test   %rcx,%rcx
je     1204 <resolve_address+0xa4>
mov    0x4(%rcx),%edx
mov    0x8(%rcx),%esi
mov    %r12,%rdi
call   1060 <memcpy@plt>
mov    (%rsp),%rdi
mov    0x4(%rdi),%eax
mov    %eax,(%r15)
call   1080 <freeaddrinfo@plt>
xor    %eax,%eax
jmp    123d <resolve_address+0xdd>
mov    0x2dbd(%rip),%rcx
mov    (%rcx),%ebp
test   %eax,%eax
je     121d <resolve_address+0xbd>
mov    %eax,%edi
call   1030 <gai_strerror@plt>
mov    %rax,%r8
jmp    1224 <resolve_address+0xc4>
lea    0xe00(%rip),%r8
lea    0xdd5(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@AI_ADDRCONFIG = global i32 0
@AI_NUMERICSERV = global i32 0
@AI_PASSIVE = global i32 0
@stderr = global i32 0
@.str = [36 x i8] c"failed to resolve address:%s:%s:%s\0A\00"
@.str.1 = [26 x i8] c"getaddrinfo returned NULL\00"
define i32 @resolve_address(%struct.sockaddr* %0, i32* %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca %struct.sockaddr*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca %struct.addrinfo
%11 = alloca %struct.addrinfo*
%12 = alloca i32
store %struct.sockaddr* %0, %struct.sockaddr** %6
store i32* %1, i32** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
%13 = call i32 @memset(%struct.addrinfo* %10, i32 0, i32 20)
%14 = load i32, i32* @SOCK_STREAM
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i32 0, i32 4
store i32 %14, i32* %15
%16 = load i32, i32* @IPPROTO_TCP
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i32 0, i32 3
store i32 %16, i32* %17
%18 = load i32, i32* @AI_ADDRCONFIG
%19 = load i32, i32* @AI_NUMERICSERV
%20 = or i32 %18, %19
%21 = load i32, i32* @AI_PASSIVE
%22 = or i32 %20, %21
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i32 0, i32 0
store i32 %22, i32* %23
%24 = load i8*, i8** %8
%25 = load i8*, i8** %9
%26 = call i32 @getaddrinfo(i8* %24, i8* %25, %struct.addrinfo* %10, %struct.addrinfo** %11)
store i32 %26, i32* %12
%27 = icmp ne i32 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %4
%29 = load %struct.addrinfo*, %struct.addrinfo** %11
%30 = icmp eq %struct.addrinfo* %29, null
br i1 %30, label %31, label %44
31: ; preds = %28, %4
%32 = load i32, i32* @stderr
%33 = load i8*, i8** %8
%34 = load i8*, i8** %9
%35 = load i32, i32* %12
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %31
%38 = load i32, i32* %12
%39 = call i8* @gai_strerror(i32 %38)
br label %41
40: ; preds = %31
br label %41
41: ; preds = %40, %37
%42 = phi i8* [ %39, %37 ], [ getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), %40 ]
%43 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %33, i8* %34, i8* %42)
store i32 -1, i32* %5
br label %59
44: ; preds = %28
%45 = load %struct.sockaddr*, %struct.sockaddr** %6
%46 = load %struct.addrinfo*, %struct.addrinfo** %11
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %46, i32 0, i32 2
%48 = load i32, i32* %47
%49 = load %struct.addrinfo*, %struct.addrinfo** %11
%50 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %49, i32 0, i32 1
%51 = load i32, i32* %50
%52 = call i32 @memcpy(%struct.sockaddr* %45, i32 %48, i32 %51)
%53 = load %struct.addrinfo*, %struct.addrinfo** %11
%54 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %53, i32 0, i32 1
%55 = load i32, i32* %54
%56 = load i32*, i32** %7
store i32 %55, i32* %56
%57 = load %struct.addrinfo*, %struct.addrinfo** %11
%58 = call i32 @freeaddrinfo(%struct.addrinfo* %57)
store i32 0, i32* %5
br label %59
59: ; preds = %44, %41
%60 = load i32, i32* %5
ret i32 %60
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@AI_ADDRCONFIG = global i32 0
@AI_NUMERICSERV = global i32 0
@AI_PASSIVE = global i32 0
@stderr = global i32 0
@.str = [36 x i8] c"failed to resolve address:%s:%s:%s\0A\00"
@.str.1 = [26 x i8] c"getaddrinfo returned NULL\00"
define i32 @resolve_address(%struct.sockaddr* %0, i32* nocapture writeonly %1, i8* %2, i8* %3) {
%5 = alloca %struct.addrinfo
%6 = alloca %struct.addrinfo*
%7 = bitcast %struct.addrinfo* %5 to i8*
%8 = bitcast %struct.addrinfo** %6 to i8*
%9 = call i32 @memset(%struct.addrinfo* %5, i32 0, i32 20)
%10 = load i32, i32* @SOCK_STREAM
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 4
store i32 %10, i32* %11
%12 = load i32, i32* @IPPROTO_TCP
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 3
store i32 %12, i32* %13
%14 = load i32, i32* @AI_ADDRCONFIG
%15 = load i32, i32* @AI_NUMERICSERV
%16 = or i32 %15, %14
%17 = load i32, i32* @AI_PASSIVE
%18 = or i32 %16, %17
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 0
store i32 %18, i32* %19
%20 = call i32 @getaddrinfo(i8* %2, i8* %3, %struct.addrinfo* %5, %struct.addrinfo** %6)
%21 = icmp ne i32 %20, 0
%22 = load %struct.addrinfo*, %struct.addrinfo** %6
%23 = icmp eq %struct.addrinfo* %22, null
%24 = select i1 %21, i1 true, i1 %23
br i1 %24, label %25, label %32
25: ; preds = %4
%26 = load i32, i32* @stderr
br i1 %21, label %27, label %29
27: ; preds = %25
%28 = call i8* @gai_strerror(i32 %20)
br label %29
29: ; preds = %25, %27
%30 = phi i8* [ %28, %27 ], [ getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), %25 ]
%31 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %2, i8* %3, i8* %30)
br label %42
32: ; preds = %4
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 2
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 1
%36 = load i32, i32* %35
%37 = call i32 @memcpy(%struct.sockaddr* %0, i32 %34, i32 %36)
%38 = load %struct.addrinfo*, %struct.addrinfo** %6
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i64 0, i32 1
%40 = load i32, i32* %39
store i32 %40, i32* %1
%41 = call i32 @freeaddrinfo(%struct.addrinfo* %38)
br label %42
42: ; preds = %32, %29
%43 = phi i32 [ -1, %29 ], [ 0, %32 ]
ret i32 %43
}