#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_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char *, int /*<<< orphan*/ *, struct addrinfo *,
                struct addrinfo **);
scalar_t__ inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
char *host_to_ipaddr(char *host) {
  int rv;
  char *ip_addr;
  struct addrinfo hints, *servinfo, *p;
  struct sockaddr_in *info;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  rv = getaddrinfo(host, NULL, &hints, &servinfo);
  if (rv != 0) {
    printf("Error with getaddrinfo: %s\n", gai_strerror(rv));
    return NULL;
  }
  for (p = servinfo; p != NULL; p = p->ai_next) {
    info = (struct sockaddr_in *)p->ai_addr;
    ip_addr = (char *)inet_ntoa(info->sin_addr);
  }
  freeaddrinfo(servinfo);
  return ip_addr;
}


x86 assembly (clang, -O0):
<host_to_ipaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    0x2e65(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11da <host_to_ipaddr+0x7a>
mov    -0x14(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    %rax,%rsi
lea    0xe38(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1229 <host_to_ipaddr+0xc9>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     1218 <host_to_ipaddr+0xb8>
mov    -0x48(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
mov    (%rax),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0x20(%rbp)
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    11e2 <host_to_ipaddr+0x82>
mov    -0x40(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<host_to_ipaddr>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1070 <getaddrinfo@plt>
test   %eax,%eax
je     11c9 <host_to_ipaddr+0x69>
mov    %eax,%edi
call   1050 <gai_strerror@plt>
lea    0xe44(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
xor    %r14d,%r14d
jmp    1204 <host_to_ipaddr+0xa4>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     11fd <host_to_ipaddr+0x9d>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%rbx),%rax
mov    (%rax),%edi
call   1030 <inet_ntoa@plt>
mov    0x8(%rbx),%rbx
test   %rbx,%rbx
jne    11e0 <host_to_ipaddr+0x80>
mov    %rax,%r14
mov    0x8(%rsp),%rdi
jmp    11ff <host_to_ipaddr+0x9f>
xor    %edi,%edi
call   1080 <freeaddrinfo@plt>
mov    %r14,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

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

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