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

/* Variables and functions */
int AI_ADDRCONFIG;
int AI_NUMERICSERV;
int AI_PASSIVE;
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, int family, int type, int proto) {
  struct addrinfo hints, *res;
  int err;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = family;
  hints.ai_socktype = type;
  hints.ai_protocol = proto;
  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    $0x80,%rsp
mov    0x10(%rbp),%eax
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
mov    %r9d,-0x30(%rbp)
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    0x10(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
mov    0x2e40(%rip),%rcx
or     (%rcx),%eax
mov    0x2e1f(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
lea    -0x48(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x54(%rbp)
cmp    $0x0,%eax
jne    11f2 <resolve_address+0x92>
cmpq   $0x0,-0x50(%rbp)
jne    1260 <resolve_address+0x100>
mov    0x2dd7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x60(%rbp)
cmpl   $0x0,-0x54(%rbp)
je     1229 <resolve_address+0xc9>
mov    -0x54(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    %rax,-0x78(%rbp)
jmp    1239 <resolve_address+0xd9>
lea    0xdf4(%rip),%rax
mov    %rax,-0x78(%rbp)
jmp    1239 <resolve_address+0xd9>
mov    -0x60(%rbp),%rcx
mov    -0x68(%rbp),%rdx
mov    -0x6c(%rbp),%edi
mov    -0x78(%rbp),%r8
lea    0xdb1(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1294 <resolve_address+0x134>
mov    -0x10(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    0x14(%rax),%esi
mov    -0x50(%rbp),%rax
mov    0x10(%rax),%edx
call   1060 <memcpy@plt>
mov    -0x50(%rbp),%rax
mov    0x10(%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x50(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve_address>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r9d,%ebp
mov    %r8d,%ebx
mov    %rcx,%r14
mov    %rdx,%r13
mov    %rsi,0x18(%rsp)
mov    %rdi,0x10(%rsp)
mov    0x70(%rsp),%r12d
lea    0x20(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    %ebx,0x20(%rsp)
mov    %ebp,0x24(%rsp)
mov    %r12d,0x28(%rsp)
mov    0x2e0f(%rip),%rax
mov    0x2e40(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e1d(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x2c(%rsp)
lea    0x8(%rsp),%rcx
mov    %r13,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1070 <getaddrinfo@plt>
test   %eax,%eax
jne    1212 <resolve_address+0xb2>
mov    0x8(%rsp),%rcx
test   %rcx,%rcx
je     1212 <resolve_address+0xb2>
mov    0x10(%rcx),%edx
mov    0x14(%rcx),%esi
mov    0x10(%rsp),%rdi
call   1060 <memcpy@plt>
mov    0x8(%rsp),%rdi
mov    0x10(%rdi),%eax
mov    0x18(%rsp),%rcx
mov    %eax,(%rcx)
call   1080 <freeaddrinfo@plt>
xor    %eax,%eax
jmp    124b <resolve_address+0xeb>
mov    0x2db7(%rip),%rcx
mov    (%rcx),%ebp
test   %eax,%eax
je     122b <resolve_address+0xcb>
mov    %eax,%edi
call   1030 <gai_strerror@plt>
mov    %rax,%r8
jmp    1232 <resolve_address+0xd2>
lea    0xdf2(%rip),%r8
lea    0xdc7(%rip),%rsi
mov    %ebp,%edi
mov    %r13,%rdx
mov    %r14,%rcx
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.addrinfo = type { i32, i32, i32, i32, i32, i32 }
@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, i32 %4, i32 %5, i32 %6) {
%8 = alloca i32
%9 = alloca %struct.sockaddr*
%10 = alloca i32*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca %struct.addrinfo
%17 = alloca %struct.addrinfo*
%18 = alloca i32
store %struct.sockaddr* %0, %struct.sockaddr** %9
store i32* %1, i32** %10
store i8* %2, i8** %11
store i8* %3, i8** %12
store i32 %4, i32* %13
store i32 %5, i32* %14
store i32 %6, i32* %15
%19 = call i32 @memset(%struct.addrinfo* %16, i32 0, i32 24)
%20 = load i32, i32* %13
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i32 0, i32 0
store i32 %20, i32* %21
%22 = load i32, i32* %14
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i32 0, i32 1
store i32 %22, i32* %23
%24 = load i32, i32* %15
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i32 0, i32 2
store i32 %24, i32* %25
%26 = load i32, i32* @AI_ADDRCONFIG
%27 = load i32, i32* @AI_NUMERICSERV
%28 = or i32 %26, %27
%29 = load i32, i32* @AI_PASSIVE
%30 = or i32 %28, %29
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i32 0, i32 3
store i32 %30, i32* %31
%32 = load i8*, i8** %11
%33 = load i8*, i8** %12
%34 = call i32 @getaddrinfo(i8* %32, i8* %33, %struct.addrinfo* %16, %struct.addrinfo** %17)
store i32 %34, i32* %18
%35 = icmp ne i32 %34, 0
br i1 %35, label %39, label %36
36: ; preds = %7
%37 = load %struct.addrinfo*, %struct.addrinfo** %17
%38 = icmp eq %struct.addrinfo* %37, null
br i1 %38, label %39, label %52
39: ; preds = %36, %7
%40 = load i32, i32* @stderr
%41 = load i8*, i8** %11
%42 = load i8*, i8** %12
%43 = load i32, i32* %18
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %39
%46 = load i32, i32* %18
%47 = call i8* @gai_strerror(i32 %46)
br label %49
48: ; preds = %39
br label %49
49: ; preds = %48, %45
%50 = phi i8* [ %47, %45 ], [ getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), %48 ]
%51 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %41, i8* %42, i8* %50)
store i32 -1, i32* %8
br label %67
52: ; preds = %36
%53 = load %struct.sockaddr*, %struct.sockaddr** %9
%54 = load %struct.addrinfo*, %struct.addrinfo** %17
%55 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %54, i32 0, i32 5
%56 = load i32, i32* %55
%57 = load %struct.addrinfo*, %struct.addrinfo** %17
%58 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %57, i32 0, i32 4
%59 = load i32, i32* %58
%60 = call i32 @memcpy(%struct.sockaddr* %53, i32 %56, i32 %59)
%61 = load %struct.addrinfo*, %struct.addrinfo** %17
%62 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %61, i32 0, i32 4
%63 = load i32, i32* %62
%64 = load i32*, i32** %10
store i32 %63, i32* %64
%65 = load %struct.addrinfo*, %struct.addrinfo** %17
%66 = call i32 @freeaddrinfo(%struct.addrinfo* %65)
store i32 0, i32* %8
br label %67
67: ; preds = %52, %49
%68 = load i32, i32* %8
ret i32 %68
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.addrinfo = type { i32, i32, i32, i32, i32, i32 }
@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, i32 %4, i32 %5, i32 %6) {
%8 = alloca %struct.addrinfo
%9 = alloca %struct.addrinfo*
%10 = bitcast %struct.addrinfo* %8 to i8*
%11 = bitcast %struct.addrinfo** %9 to i8*
%12 = call i32 @memset(%struct.addrinfo* %8, i32 0, i32 24)
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i64 0, i32 0
store i32 %4, i32* %13
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i64 0, i32 1
store i32 %5, i32* %14
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i64 0, i32 2
store i32 %6, i32* %15
%16 = load i32, i32* @AI_ADDRCONFIG
%17 = load i32, i32* @AI_NUMERICSERV
%18 = or i32 %17, %16
%19 = load i32, i32* @AI_PASSIVE
%20 = or i32 %18, %19
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i64 0, i32 3
store i32 %20, i32* %21
%22 = call i32 @getaddrinfo(i8* %2, i8* %3, %struct.addrinfo* %8, %struct.addrinfo** %9)
%23 = icmp ne i32 %22, 0
%24 = load %struct.addrinfo*, %struct.addrinfo** %9
%25 = icmp eq %struct.addrinfo* %24, null
%26 = select i1 %23, i1 true, i1 %25
br i1 %26, label %27, label %34
27: ; preds = %7
%28 = load i32, i32* @stderr
br i1 %23, label %29, label %31
29: ; preds = %27
%30 = call i8* @gai_strerror(i32 %22)
br label %31
31: ; preds = %27, %29
%32 = phi i8* [ %30, %29 ], [ getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), %27 ]
%33 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %2, i8* %3, i8* %32)
br label %44
34: ; preds = %7
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 5
%36 = load i32, i32* %35
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 4
%38 = load i32, i32* %37
%39 = call i32 @memcpy(%struct.sockaddr* %0, i32 %36, i32 %38)
%40 = load %struct.addrinfo*, %struct.addrinfo** %9
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i64 0, i32 4
%42 = load i32, i32* %41
store i32 %42, i32* %1
%43 = call i32 @freeaddrinfo(%struct.addrinfo* %40)
br label %44
44: ; preds = %34, %31
%45 = phi i32 [ -1, %31 ], [ 0, %34 ]
ret i32 %45
}