#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;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int hostname_to_ip(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) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
    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):
<hostname_to_ip>:
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   1060 <memset@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe4d(%rip),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x4c(%rbp)
cmp    $0x0,%eax
je     11fe <hostname_to_ip+0x8e>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    -0x4c(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    -0x50(%rbp),%edi
mov    %rax,%rdx
lea    0xe18(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    125b <hostname_to_ip+0xeb>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     124b <hostname_to_ip+0xdb>
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    1206 <hostname_to_ip+0x96>
mov    -0x38(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hostname_to_ip>:
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   1060 <memset@plt>
mov    0x2e4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
lea    0xe4a(%rip),%rsi
lea    0x8(%rsp),%rcx
mov    %r14,%rdi
mov    %r15,%rdx
call   1080 <getaddrinfo@plt>
test   %eax,%eax
je     11f3 <hostname_to_ip+0x83>
mov    0x2e07(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1050 <gai_strerror@plt>
lea    0xe24(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    $0x1,%r14d
jmp    122b <hostname_to_ip+0xbb>
mov    0x8(%rsp),%rbx
xor    %r14d,%r14d
test   %rbx,%rbx
je     1224 <hostname_to_ip+0xb4>
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    1200 <hostname_to_ip+0x90>
mov    0x8(%rsp),%rdi
jmp    1226 <hostname_to_ip+0xb6>
xor    %edi,%edi
call   1090 <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"
@stderr = global i32 0
@.str.1 = [17 x i8] c"getaddrinfo: %s\0A\00"
define i32 @hostname_to_ip(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 %24
19: ; preds = %2
%20 = load i32, i32* @stderr
%21 = load i32, i32* %10
%22 = call i8* @gai_strerror(i32 %21)
%23 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %22)
store i32 1, i32* %3
br label %47
24: ; preds = %2
%25 = load %struct.addrinfo*, %struct.addrinfo** %7
store %struct.addrinfo* %25, %struct.addrinfo** %8
br label %26
26: ; preds = %40, %24
%27 = load %struct.addrinfo*, %struct.addrinfo** %8
%28 = icmp ne %struct.addrinfo* %27, null
br i1 %28, label %29, label %44
29: ; preds = %26
%30 = load %struct.addrinfo*, %struct.addrinfo** %8
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i32 0, i32 0
%32 = load i64, i64* %31
%33 = inttoptr i64 %32 to %struct.sockaddr_in*
store %struct.sockaddr_in* %33, %struct.sockaddr_in** %9
%34 = load i8*, i8** %5
%35 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = call i32 @inet_ntoa(i32 %37)
%39 = call i32 @strcpy(i8* %34, i32 %38)
br label %40
40: ; preds = %29
%41 = load %struct.addrinfo*, %struct.addrinfo** %8
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %41, i32 0, i32 1
%43 = load %struct.addrinfo*, %struct.addrinfo** %42
store %struct.addrinfo* %43, %struct.addrinfo** %8
br label %26
44: ; preds = %26
%45 = load %struct.addrinfo*, %struct.addrinfo** %7
%46 = call i32 @freeaddrinfo(%struct.addrinfo* %45)
store i32 0, i32* %3
br label %47
47: ; preds = %44, %19
%48 = load i32, i32* %3
ret i32 %48
}

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"
@stderr = global i32 0
@.str.1 = [17 x i8] c"getaddrinfo: %s\0A\00"
define i32 @hostname_to_ip(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 %17
14: ; preds = %2
%15 = load %struct.addrinfo*, %struct.addrinfo** %4
%16 = icmp eq %struct.addrinfo* %15, null
br i1 %16, label %35, label %21
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = call i8* @gai_strerror(i32 %12)
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %19)
br label %38
21: ; preds = %14, %21
%22 = phi %struct.addrinfo* [ %31, %21 ], [ %15, %14 ]
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 0
%24 = load i64, i64* %23
%25 = inttoptr i64 %24 to %struct.sockaddr_in*
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %25, i64 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @inet_ntoa(i32 %27)
%29 = call i32 @strcpy(i8* %1, i32 %28)
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 1
%31 = load %struct.addrinfo*, %struct.addrinfo** %30
%32 = icmp eq %struct.addrinfo* %31, null
br i1 %32, label %33, label %21
33: ; preds = %21
%34 = load %struct.addrinfo*, %struct.addrinfo** %4
br label %35
35: ; preds = %33, %14
%36 = phi %struct.addrinfo* [ %34, %33 ], [ null, %14 ]
%37 = call i32 @freeaddrinfo(%struct.addrinfo* %36)
br label %38
38: ; preds = %35, %17
%39 = phi i32 [ 1, %17 ], [ 0, %35 ]
ret i32 %39
}