#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;
  int /*<<< orphan*/ ai_next;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ inet_ntoa(int /*<<< orphan*/);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *strcpy(char *, int /*<<< orphan*/);
int hostname_to_ip(char *domain, struct sockaddr_in *_addr) {
  struct addrinfo hint, *serv_addr, *iter;
  struct sockaddr_in *h;
  int rv;
  memset(&hint, 0, sizeof hint);
  hint.ai_family = AF_INET;
  hint.ai_socktype = SOCK_STREAM;
  if ((rv = getaddrinfo(domain, "http", &hint, &serv_addr)) != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
    return 1;
  }
  char *ip;
  ip = (char *)malloc(sizeof(char) * 100);
  for (iter = serv_addr; iter != NULL; iter->ai_next) {
    h = (struct sockaddr_in *)iter->ai_addr;
    if (strcpy(ip, inet_ntoa(h->sin_addr)) != NULL) {
      *_addr = *h;
      break;
    }
  }
  freeaddrinfo(serv_addr);
  free(ip);
  return 0;
}


x86 assembly (clang, -O0):
<hostname_to_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1070 <memset@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe2d(%rip),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   10a0 <getaddrinfo@plt>
mov    %eax,-0x4c(%rbp)
cmp    $0x0,%eax
je     121e <hostname_to_ip+0x8e>
mov    0x2de5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x4c(%rbp),%edi
call   1060 <gai_strerror@plt>
mov    -0x5c(%rbp),%edi
mov    %rax,%rdx
lea    0xdf8(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a6 <hostname_to_ip+0x116>
mov    $0x64,%edi
call   1090 <malloc@plt>
mov    %rax,-0x58(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     128d <hostname_to_ip+0xfd>
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    -0x58(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x48(%rbp),%rax
mov    (%rax),%edi
call   1050 <inet_ntoa@plt>
mov    -0x68(%rbp),%rdi
mov    %eax,%esi
call   1040 <strcpy@plt>
cmp    $0x0,%rax
je     1283 <hostname_to_ip+0xf3>
mov    -0x18(%rbp),%rax
mov    -0x48(%rbp),%rcx
mov    (%rcx),%ecx
mov    %ecx,(%rax)
jmp    128d <hostname_to_ip+0xfd>
jmp    1288 <hostname_to_ip+0xf8>
jmp    1234 <hostname_to_ip+0xa4>
mov    -0x38(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hostname_to_ip>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1070 <memset@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
lea    0xe28(%rip),%rsi
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %r15,%rdx
call   10a0 <getaddrinfo@plt>
test   %eax,%eax
je     1213 <hostname_to_ip+0x83>
mov    0x2de7(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1060 <gai_strerror@plt>
lea    0xe04(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1080 <fprintf@plt>
mov    $0x1,%r15d
jmp    1264 <hostname_to_ip+0xd4>
mov    $0x64,%edi
call   1090 <malloc@plt>
mov    %rax,%r12
mov    (%rsp),%r13
xor    %r15d,%r15d
test   %r13,%r13
je     1255 <hostname_to_ip+0xc5>
nopl   0x0(%rax)
mov    0x0(%r13),%rbx
mov    (%rbx),%edi
call   1050 <inet_ntoa@plt>
mov    %r12,%rdi
mov    %eax,%esi
call   1040 <strcpy@plt>
test   %rax,%rax
je     1230 <hostname_to_ip+0xa0>
mov    (%rbx),%eax
mov    %eax,(%r14)
mov    (%rsp),%rdi
jmp    1257 <hostname_to_ip+0xc7>
xor    %edi,%edi
call   10b0 <freeaddrinfo@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r15d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.addrinfo = type { i64, i32, i32, i32 }
@AF_INET = 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, %struct.sockaddr_in* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.sockaddr_in*
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = alloca %struct.addrinfo*
%9 = alloca %struct.sockaddr_in*
%10 = alloca i32
%11 = alloca i8*
store i8* %0, i8** %4
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %5
%12 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 24)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 3
store i32 %13, i32* %14
%15 = load i32, i32* @SOCK_STREAM
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 2
store i32 %15, i32* %16
%17 = load i8*, i8** %4
%18 = call i32 @getaddrinfo(i8* %17, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %6, %struct.addrinfo** %7)
store i32 %18, i32* %10
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %2
%21 = load i32, i32* @stderr
%22 = load i32, i32* %10
%23 = call i8* @gai_strerror(i32 %22)
%24 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %23)
store i32 1, i32* %3
br label %59
25: ; preds = %2
%26 = call i64 @malloc(i32 100)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %11
%28 = load %struct.addrinfo*, %struct.addrinfo** %7
store %struct.addrinfo* %28, %struct.addrinfo** %8
br label %29
29: ; preds = %50, %25
%30 = load %struct.addrinfo*, %struct.addrinfo** %8
%31 = icmp ne %struct.addrinfo* %30, null
br i1 %31, label %32, label %54
32: ; preds = %29
%33 = load %struct.addrinfo*, %struct.addrinfo** %8
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %33, i32 0, i32 0
%35 = load i64, i64* %34
%36 = inttoptr i64 %35 to %struct.sockaddr_in*
store %struct.sockaddr_in* %36, %struct.sockaddr_in** %9
%37 = load i8*, i8** %11
%38 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %38, i32 0, i32 0
%40 = load i32, i32* %39
%41 = call i32 @inet_ntoa(i32 %40)
%42 = call i32* @strcpy(i8* %37, i32 %41)
%43 = icmp ne i32* %42, null
br i1 %43, label %44, label %49
44: ; preds = %32
%45 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%46 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%47 = bitcast %struct.sockaddr_in* %45 to i8*
%48 = bitcast %struct.sockaddr_in* %46 to i8*
br label %54
49: ; preds = %32
br label %50
50: ; preds = %49
%51 = load %struct.addrinfo*, %struct.addrinfo** %8
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i32 0, i32 1
%53 = load i32, i32* %52
br label %29
54: ; preds = %44, %29
%55 = load %struct.addrinfo*, %struct.addrinfo** %7
%56 = call i32 @freeaddrinfo(%struct.addrinfo* %55)
%57 = load i8*, i8** %11
%58 = call i32 @free(i8* %57)
store i32 0, i32* %3
br label %59
59: ; preds = %54, %20
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.addrinfo = type { i64, i32, i32, i32 }
@AF_INET = 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, %struct.sockaddr_in* nocapture writeonly %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_INET
%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 %18, label %14
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i8* @gai_strerror(i32 %12)
%17 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %16)
br label %41
18: ; preds = %2
%19 = call i64 @malloc(i32 100)
%20 = inttoptr i64 %19 to i8*
%21 = load %struct.addrinfo*, %struct.addrinfo** %4
%22 = icmp eq %struct.addrinfo* %21, null
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 0
br i1 %22, label %37, label %24
24: ; preds = %18, %24
%25 = load i64, i64* %23
%26 = inttoptr i64 %25 to %struct.sockaddr_in*
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %26, i64 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @inet_ntoa(i32 %28)
%30 = call i32* @strcpy(i8* %20, i32 %29)
%31 = icmp eq i32* %30, null
br i1 %31, label %24, label %32
32: ; preds = %24
%33 = inttoptr i64 %25 to i32*
%34 = getelementptr %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%35 = load i32, i32* %33
store i32 %35, i32* %34
%36 = load %struct.addrinfo*, %struct.addrinfo** %4
br label %37
37: ; preds = %18, %32
%38 = phi %struct.addrinfo* [ null, %18 ], [ %36, %32 ]
%39 = call i32 @freeaddrinfo(%struct.addrinfo* %38)
%40 = call i32 @free(i8* %20)
br label %41
41: ; preds = %37, %14
%42 = phi i32 [ 1, %14 ], [ 0, %37 ]
ret i32 %42
}