#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 /*<<< orphan*/ sin_addr;
};
struct addrinfo {
  scalar_t__ ai_addr;
  struct addrinfo *ai_next;
  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 getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int hostnameToIp(char *hostname, char *ip) {
  struct addrinfo hints, *servinfo, *p;
  struct sockaddr_in *h;
  int rv;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if ((rv = getaddrinfo(hostname, "http", &hints, &servinfo)) != 0) {
    return 1;
  }
  for (p = servinfo; p != NULL; p = p->ai_next) {
    h = (struct sockaddr_in *)p->ai_addr;
    strcpy(ip, inet_ntoa(h->sin_addr));
  }
  freeaddrinfo(servinfo);
  return 0;
}


x86 assembly (clang, -O0):
<hostnameToIp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe6d(%rip),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1060 <getaddrinfo@plt>
mov    %eax,-0x4c(%rbp)
cmp    $0x0,%eax
je     11b8 <hostnameToIp+0x68>
movl   $0x1,-0x4(%rbp)
jmp    1215 <hostnameToIp+0xc5>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1205 <hostnameToIp+0xb5>
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x48(%rbp),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    -0x58(%rbp),%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    11c0 <hostnameToIp+0x70>
mov    -0x38(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hostnameToIp>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r12
mov    %rdi,%r14
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e6c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
lea    0xe6a(%rip),%rsi
lea    0x8(%rsp),%rcx
mov    %r14,%rdi
mov    %r15,%rdx
call   1060 <getaddrinfo@plt>
mov    $0x1,%r14d
test   %eax,%eax
jne    11eb <hostnameToIp+0x9b>
mov    0x8(%rsp),%rbx
xor    %r14d,%r14d
test   %rbx,%rbx
je     11e4 <hostnameToIp+0x94>
nopl   (%rax)
mov    (%rbx),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    %r12,%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
mov    0x8(%rbx),%rbx
test   %rbx,%rbx
jne    11c0 <hostnameToIp+0x70>
mov    0x8(%rsp),%rdi
jmp    11e6 <hostnameToIp+0x96>
xor    %edi,%edi
call   1070 <freeaddrinfo@plt>
mov    %r14d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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