#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_in {
  int dummy;
};
struct addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  scalar_t__ ai_addr;
  int /*<<< orphan*/ ai_flags;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AI_CANONNAME;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ exit_on_error(int /*<<< orphan*/ *, char *, char *,
                                 int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ gai_strerror(int);
int getaddrinfo(char *, int /*<<< orphan*/ *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memcpy(struct sockaddr_in *, struct sockaddr_in *,
                          int /*<<< orphan*/);
void resolve_dst(char dst[], struct sockaddr_in *addr) {
  int status;
  struct addrinfo hints = {0};
  struct addrinfo *res;
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = IPPROTO_TCP;
  hints.ai_flags = AI_CANONNAME;
  if ((status = getaddrinfo(dst, NULL, &hints, &res)) != 0) {
    exit_on_error(NULL, "getaddrinfo() failed to resolve destination %s: %s\n",
                  dst, gai_strerror(status));
  }
  memcpy(addr, (struct sockaddr_in *)res->ai_addr, res->ai_addrlen);
  freeaddrinfo(res);
}


x86 assembly (clang, -O0):
<resolve_dst>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     11f7 <resolve_dst+0x97>
mov    -0x8(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x50(%rbp),%rdx
mov    %eax,%ecx
xor    %eax,%eax
mov    %eax,%edi
lea    0xe0e(%rip),%rsi
call   1050 <exit_on_error@plt>
mov    -0x10(%rbp),%rdi
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   1060 <memcpy@plt>
mov    -0x48(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve_dst>:
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movq   $0x0,0x30(%rsp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2c(%rsp)
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%rcx
xor    %esi,%esi
call   1060 <getaddrinfo@plt>
test   %eax,%eax
je     11d6 <resolve_dst+0x86>
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xe36(%rip),%rsi
xor    %edi,%edi
mov    %rbx,%rdx
mov    %eax,%ecx
call   1040 <exit_on_error@plt>
mov    0x8(%rsp),%rax
mov    0x8(%rax),%rsi
mov    (%rax),%edx
mov    %r14,%rdi
call   1050 <memcpy@plt>
mov    0x8(%rsp),%rdi
call   1070 <freeaddrinfo@plt>
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.addrinfo = type { i32, i64, i32, i32, i32, i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@AI_CANONNAME = global i32 0
@.str = [52 x i8] c"getaddrinfo() failed to resolve destination %s: %s\0A\00"
define void @resolve_dst(i8* %0, %struct.sockaddr_in* %1) {
%3 = alloca i8*
%4 = alloca %struct.sockaddr_in*
%5 = alloca i32
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
store i8* %0, i8** %3
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %4
%8 = bitcast %struct.addrinfo* %6 to i8*
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 5
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_STREAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 4
store i32 %11, i32* %12
%13 = load i32, i32* @IPPROTO_TCP
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 3
store i32 %13, i32* %14
%15 = load i32, i32* @AI_CANONNAME
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 2
store i32 %15, i32* %16
%17 = load i8*, i8** %3
%18 = call i32 @getaddrinfo(i8* %17, i32* null, %struct.addrinfo* %6, %struct.addrinfo** %7)
store i32 %18, i32* %5
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %2
%21 = load i8*, i8** %3
%22 = load i32, i32* %5
%23 = call i32 @gai_strerror(i32 %22)
%24 = call i32 @exit_on_error(i32* null, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i8* %21, i32 %23)
br label %25
25: ; preds = %20, %2
%26 = load %struct.sockaddr_in*, %struct.sockaddr_in** %4
%27 = load %struct.addrinfo*, %struct.addrinfo** %7
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i32 0, i32 1
%29 = load i64, i64* %28
%30 = inttoptr i64 %29 to %struct.sockaddr_in*
%31 = load %struct.addrinfo*, %struct.addrinfo** %7
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @memcpy(%struct.sockaddr_in* %26, %struct.sockaddr_in* %30, i32 %33)
%35 = load %struct.addrinfo*, %struct.addrinfo** %7
%36 = call i32 @freeaddrinfo(%struct.addrinfo* %35)
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.addrinfo = type { i32, i64, i32, i32, i32, i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@AI_CANONNAME = global i32 0
@.str = [52 x i8] c"getaddrinfo() failed to resolve destination %s: %s\0A\00"
define void @resolve_dst(i8* %0, %struct.sockaddr_in* %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = bitcast %struct.addrinfo* %3 to i8*
%6 = bitcast %struct.addrinfo** %4 to i8*
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 5
store i32 %7, i32* %8
%9 = load i32, i32* @SOCK_STREAM
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %9, i32* %10
%11 = load i32, i32* @IPPROTO_TCP
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %11, i32* %12
%13 = load i32, i32* @AI_CANONNAME
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 2
store i32 %13, i32* %14
%15 = call i32 @getaddrinfo(i8* %0, i32* null, %struct.addrinfo* %3, %struct.addrinfo** %4)
%16 = icmp eq i32 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %2
%18 = call i32 @gai_strerror(i32 %15)
%19 = call i32 @exit_on_error(i32* null, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %18)
br label %20
20: ; preds = %17, %2
%21 = load %struct.addrinfo*, %struct.addrinfo** %4
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 1
%23 = load i64, i64* %22
%24 = inttoptr i64 %23 to %struct.sockaddr_in*
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 @memcpy(%struct.sockaddr_in* %1, %struct.sockaddr_in* %24, i32 %26)
%28 = load %struct.addrinfo*, %struct.addrinfo** %4
%29 = call i32 @freeaddrinfo(%struct.addrinfo* %28)
ret void
}