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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char const *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memcpy(struct sockaddr_in *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int voip_lookupAddr(const char *host, int port, struct sockaddr_in *addr) {
  struct addrinfo hints;
  struct addrinfo *result, *rp;
  int s;
  char buf[32];
  snprintf(buf, 32, "%d", port);
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = IPPROTO_TCP;
  hints.ai_flags = 0;
  s = getaddrinfo(host, buf, &hints, &result);
  if (s != 0) {
    return -1;
  }
  for (rp = result; rp != NULL; rp = rp->ai_next) {
    memcpy(addr, rp->ai_addr, rp->ai_addrlen);
    break;
  }
  freeaddrinfo(result);
  return 0;
}


x86 assembly (clang, -O0):
<voip_lookupAddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x20,%esi
lea    0xe87(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x80(%rbp),%rsi
lea    -0x48(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   1060 <getaddrinfo@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
je     11e8 <voip_lookupAddr+0x98>
movl   $0xffffffff,-0x4(%rbp)
jmp    1221 <voip_lookupAddr+0xd1>
mov    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     1211 <voip_lookupAddr+0xc1>
mov    -0x20(%rbp),%rdi
mov    -0x58(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x58(%rbp),%rax
mov    (%rax),%edx
call   1050 <memcpy@plt>
mov    -0x50(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<voip_lookupAddr>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %rdx,%r14
mov    %esi,%ecx
mov    %rdi,%rbx
lea    0xe96(%rip),%rdx
lea    0x30(%rsp),%r15
mov    %r15,%rdi
mov    $0x20,%esi
call   1030 <snprintf@plt>
lea    0x8(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
movq   $0x0,0x18(%rsp)
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1060 <getaddrinfo@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
jne    11ff <voip_lookupAddr+0xaf>
mov    (%rsp),%rax
xor    %ebx,%ebx
test   %rax,%rax
je     11f8 <voip_lookupAddr+0xa8>
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %r14,%rdi
call   1050 <memcpy@plt>
mov    (%rsp),%rdi
jmp    11fa <voip_lookupAddr+0xaa>
xor    %edi,%edi
call   1070 <freeaddrinfo@plt>
mov    %ebx,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.addrinfo = type { i32, i32, %struct.addrinfo*, i64, i32, i32, i32 }
@.str = [3 x i8] c"%d\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
define i32 @voip_lookupAddr(i8* %0, i32 %1, %struct.sockaddr_in* %2) {
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = alloca [32 x i8]
%7 = bitcast %struct.addrinfo* %4 to i8*
%8 = bitcast %struct.addrinfo** %5 to i8*
%9 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%10 = call i32 @snprintf(i8* %9, i32 32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %1)
%11 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 40)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 6
store i32 %12, i32* %13
%14 = load i32, i32* @SOCK_STREAM
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 5
store i32 %14, i32* %15
%16 = load i32, i32* @IPPROTO_TCP
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i64 0, i64* %18
%19 = call i32 @getaddrinfo(i8* %0, i8* %9, %struct.addrinfo* %4, %struct.addrinfo** %5)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %34
21: ; preds = %3
%22 = load %struct.addrinfo*, %struct.addrinfo** %5
%23 = icmp eq %struct.addrinfo* %22, null
br i1 %23, label %31, label %24
24: ; preds = %21
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 1
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @memcpy(%struct.sockaddr_in* %2, i32 %26, i32 %28)
%30 = load %struct.addrinfo*, %struct.addrinfo** %5
br label %31
31: ; preds = %24, %21
%32 = phi %struct.addrinfo* [ %30, %24 ], [ null, %21 ]
%33 = call i32 @freeaddrinfo(%struct.addrinfo* %32)
br label %34
34: ; preds = %3, %31
%35 = phi i32 [ 0, %31 ], [ -1, %3 ]
ret i32 %35
}