#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_storage {
  int dummy;
};
struct addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ gai_strerror(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ log_error(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(struct sockaddr_storage *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int resolve_address(char *host, char *port, struct sockaddr_storage *ss) {
  struct addrinfo hints, *res;
  int rc;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if ((rc = getaddrinfo(host, port, &hints, &res))) {
    log_error("Cannot resolve host %s. getaddrinfo error: "
              "[%s]",
              host, gai_strerror(rc));
    return rc;
  }
  memcpy(ss, res->ai_addr, res->ai_addrlen);
  freeaddrinfo(res);
  return rc;
}


x86 assembly (clang, -O0):
<resolve_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
je     11ea <resolve_address+0x8a>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x3c(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
lea    0xe26(%rip),%rdi
call   1030 <log_error@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120f <resolve_address+0xaf>
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x38(%rbp),%rax
mov    (%rax),%edx
call   1060 <memcpy@plt>
mov    -0x38(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve_address>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%rbp
mov    %rdi,%rbx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r15,%rdx
call   1070 <getaddrinfo@plt>
mov    %eax,%ebp
test   %eax,%eax
je     11d2 <resolve_address+0x72>
mov    %ebp,%edi
call   1040 <gai_strerror@plt>
lea    0xe3a(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <log_error@plt>
jmp    11ec <resolve_address+0x8c>
mov    (%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %r14,%rdi
call   1060 <memcpy@plt>
mov    (%rsp),%rdi
call   1080 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.addrinfo = type { i32, i32, i32, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [48 x i8] c"Cannot resolve host %s. getaddrinfo error: [%s]\00"
define i32 @resolve_address(i8* %0, i8* %1, %struct.sockaddr_storage* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca %struct.sockaddr_storage*
%8 = alloca %struct.addrinfo
%9 = alloca %struct.addrinfo*
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store %struct.sockaddr_storage* %2, %struct.sockaddr_storage** %7
%11 = call i32 @memset(%struct.addrinfo* %8, i32 0, i32 16)
%12 = load i32, i32* @AF_UNSPEC
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 3
store i32 %12, i32* %13
%14 = load i32, i32* @SOCK_STREAM
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 2
store i32 %14, i32* %15
%16 = load i8*, i8** %5
%17 = load i8*, i8** %6
%18 = call i32 @getaddrinfo(i8* %16, i8* %17, %struct.addrinfo* %8, %struct.addrinfo** %9)
store i32 %18, i32* %10
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %3
%21 = load i8*, i8** %5
%22 = load i32, i32* %10
%23 = call i32 @gai_strerror(i32 %22)
%24 = call i32 @log_error(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8* %21, i32 %23)
%25 = load i32, i32* %10
store i32 %25, i32* %4
br label %38
26: ; preds = %3
%27 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %7
%28 = load %struct.addrinfo*, %struct.addrinfo** %9
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i32 0, i32 1
%30 = load i32, i32* %29
%31 = load %struct.addrinfo*, %struct.addrinfo** %9
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @memcpy(%struct.sockaddr_storage* %27, i32 %30, i32 %33)
%35 = load %struct.addrinfo*, %struct.addrinfo** %9
%36 = call i32 @freeaddrinfo(%struct.addrinfo* %35)
%37 = load i32, i32* %10
store i32 %37, i32* %4
br label %38
38: ; preds = %26, %20
%39 = load i32, i32* %4
ret i32 %39
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.addrinfo = type { i32, i32, i32, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [48 x i8] c"Cannot resolve host %s. getaddrinfo error: [%s]\00"
define i32 @resolve_address(i8* %0, i8* %1, %struct.sockaddr_storage* %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 16)
%9 = load i32, i32* @AF_UNSPEC
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_STREAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 2
store i32 %11, i32* %12
%13 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %4, %struct.addrinfo** %5)
%14 = icmp eq i32 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %3
%16 = call i32 @gai_strerror(i32 %13)
%17 = call i32 @log_error(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %16)
br label %27
18: ; preds = %3
%19 = load %struct.addrinfo*, %struct.addrinfo** %5
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i64 0, i32 1
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i64 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @memcpy(%struct.sockaddr_storage* %2, i32 %21, i32 %23)
%25 = load %struct.addrinfo*, %struct.addrinfo** %5
%26 = call i32 @freeaddrinfo(%struct.addrinfo* %25)
br label %27
27: ; preds = %18, %15
ret i32 %13
}