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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int get_address(char *host, char *port, struct sockaddr *addr) {
  struct addrinfo hints;
  struct addrinfo *result, *rp;
  int s;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  s = getaddrinfo(host, port, &hints, &result);
  if (s != 0) {
    printf("getaddrinfo: %s\n", gai_strerror(s));
    exit(EXIT_FAILURE);
  }
  for (rp = result; rp != NULL; rp = rp->ai_next) {
    *addr = *rp->ai_addr;
    freeaddrinfo(result);
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<get_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%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    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
je     11e3 <get_address+0x83>
mov    -0x4c(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    %rax,%rsi
lea    0xe30(%rip),%rdi
call   1030 <printf@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     121a <get_address+0xba>
mov    -0x20(%rbp),%rax
mov    -0x48(%rbp),%rcx
mov    (%rcx),%rcx
mov    (%rcx),%ecx
mov    %ecx,(%rax)
mov    -0x40(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
movl   $0x0,-0x4(%rbp)
jmp    1221 <get_address+0xc1>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_address>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0x10(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1070 <getaddrinfo@plt>
test   %eax,%eax
jne    11e5 <get_address+0x85>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     11d4 <get_address+0x74>
mov    (%rdi),%rax
mov    (%rax),%eax
mov    %eax,(%r14)
call   1080 <freeaddrinfo@plt>
xor    %eax,%eax
jmp    11d9 <get_address+0x79>
mov    $0x1,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xe0d(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.addrinfo = type { %struct.sockaddr*, %struct.addrinfo*, i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [17 x i8] c"getaddrinfo: %s\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @get_address(i8* %0, i8* %1, %struct.sockaddr* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca %struct.sockaddr*
%8 = alloca %struct.addrinfo
%9 = alloca %struct.addrinfo*
%10 = alloca %struct.addrinfo*
%11 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store %struct.sockaddr* %2, %struct.sockaddr** %7
%12 = call i32 @memset(%struct.addrinfo* %8, i32 0, i32 24)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 3
store i32 %13, i32* %14
%15 = load i32, i32* @SOCK_STREAM
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 2
store i32 %15, i32* %16
%17 = load i8*, i8** %5
%18 = load i8*, i8** %6
%19 = call i32 @getaddrinfo(i8* %17, i8* %18, %struct.addrinfo* %8, %struct.addrinfo** %9)
store i32 %19, i32* %11
%20 = load i32, i32* %11
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %3
%23 = load i32, i32* %11
%24 = call i8* @gai_strerror(i32 %23)
%25 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %24)
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %3
%29 = load %struct.addrinfo*, %struct.addrinfo** %9
store %struct.addrinfo* %29, %struct.addrinfo** %10
br label %30
30: ; preds = %42, %28
%31 = load %struct.addrinfo*, %struct.addrinfo** %10
%32 = icmp ne %struct.addrinfo* %31, null
br i1 %32, label %33, label %46
33: ; preds = %30
%34 = load %struct.sockaddr*, %struct.sockaddr** %7
%35 = load %struct.addrinfo*, %struct.addrinfo** %10
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %35, i32 0, i32 0
%37 = load %struct.sockaddr*, %struct.sockaddr** %36
%38 = bitcast %struct.sockaddr* %34 to i8*
%39 = bitcast %struct.sockaddr* %37 to i8*
%40 = load %struct.addrinfo*, %struct.addrinfo** %9
%41 = call i32 @freeaddrinfo(%struct.addrinfo* %40)
store i32 0, i32* %4
br label %47
42: ; No predecessors!
%43 = load %struct.addrinfo*, %struct.addrinfo** %10
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %43, i32 0, i32 1
%45 = load %struct.addrinfo*, %struct.addrinfo** %44
store %struct.addrinfo* %45, %struct.addrinfo** %10
br label %30
46: ; preds = %30
store i32 1, i32* %4
br label %47
47: ; preds = %46, %33
%48 = load i32, i32* %4
ret i32 %48
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.addrinfo = type { %struct.sockaddr*, %struct.addrinfo*, i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [17 x i8] c"getaddrinfo: %s\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @get_address(i8* %0, i8* %1, %struct.sockaddr* nocapture writeonly %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 24)
%9 = load i32, i32* @AF_INET
%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 %20, label %15
15: ; preds = %3
%16 = call i8* @gai_strerror(i32 %13)
%17 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %3
%21 = load %struct.addrinfo*, %struct.addrinfo** %5
%22 = icmp eq %struct.addrinfo* %21, null
br i1 %22, label %29, label %23
23: ; preds = %20
%24 = bitcast %struct.addrinfo* %21 to i32**
%25 = load i32*, i32** %24
%26 = getelementptr %struct.sockaddr, %struct.sockaddr* %2, i64 0, i32 0
%27 = load i32, i32* %25
store i32 %27, i32* %26
%28 = call i32 @freeaddrinfo(%struct.addrinfo* %21)
br label %29
29: ; preds = %20, %23
%30 = phi i32 [ 0, %23 ], [ 1, %20 ]
ret i32 %30
}