#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;
  int /*<<< orphan*/ sin_family;
};
struct in_addr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ __die__(char *);
struct hostent *gethostbyname(char const *);
scalar_t__ inet_aton(char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, int);
int get_ip_from_host(char *ipbuf, const char *host, int maxlen) {
  struct sockaddr_in sa;
  struct hostent *he;
  sa.sin_family = AF_INET;
  if (inet_aton(host, &sa.sin_addr) == 0) {
    he = gethostbyname(host);
    if (he == NULL)
      __die__("gethostbyname error.");
    memcpy(&sa.sin_addr, he->h_addr, sizeof(struct in_addr));
  }
  strncpy(ipbuf, inet_ntoa(sa.sin_addr), maxlen);
  return 0;
}


x86 assembly (clang, -O0):
<get_ip_from_host>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <inet_aton@plt>
cmp    $0x0,%rax
jne    11ce <get_ip_from_host+0x6e>
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ba <get_ip_from_host+0x5a>
lea    0xe4b(%rip),%rdi
call   1080 <__die__@plt>
lea    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%esi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ip_from_host>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %rdi,%rbx
mov    0x2e70(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   1070 <inet_aton@plt>
test   %rax,%rax
jne    11be <get_ip_from_host+0x5e>
mov    %rbp,%rdi
call   1050 <gethostbyname@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11ac <get_ip_from_host+0x4c>
lea    0xe59(%rip),%rdi
call   1080 <__die__@plt>
mov    0x0(%rbp),%esi
lea    0x8(%rsp),%rdi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    0x8(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %rbx,%rdi
mov    %eax,%esi
mov    %r14d,%edx
call   1030 <strncpy@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.hostent = type { i32 }
@AF_INET = global i32 0
@.str = [21 x i8] c"gethostbyname error.\00"
define i32 @get_ip_from_host(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.hostent*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %9, i32* %10
%11 = load i8*, i8** %5
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%13 = call i64 @inet_aton(i8* %11, i32* %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %28
15: ; preds = %3
%16 = load i8*, i8** %5
%17 = call %struct.hostent* @gethostbyname(i8* %16)
store %struct.hostent* %17, %struct.hostent** %8
%18 = load %struct.hostent*, %struct.hostent** %8
%19 = icmp eq %struct.hostent* %18, null
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @__die__(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20, %15
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%24 = load %struct.hostent*, %struct.hostent** %8
%25 = getelementptr inbounds %struct.hostent, %struct.hostent* %24, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 @memcpy(i32* %23, i32 %26, i32 4)
br label %28
28: ; preds = %22, %3
%29 = load i8*, i8** %4
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @inet_ntoa(i32 %31)
%33 = load i32, i32* %6
%34 = call i32 @strncpy(i8* %29, i32 %32, i32 %33)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.hostent = type { i32 }
@AF_INET = global i32 0
@.str = [21 x i8] c"gethostbyname error.\00"
define i32 @get_ip_from_host(i8* %0, i8* %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%9 = call i64 @inet_aton(i8* %1, i32* %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %20
11: ; preds = %3
%12 = call %struct.hostent* @gethostbyname(i8* %1)
%13 = icmp eq %struct.hostent* %12, null
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 @__die__(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %11
%17 = getelementptr inbounds %struct.hostent, %struct.hostent* %12, i64 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @memcpy(i32* %8, i32 %18, i32 4)
br label %20
20: ; preds = %16, %3
%21 = load i32, i32* %8
%22 = call i32 @inet_ntoa(i32 %21)
%23 = call i32 @strncpy(i8* %0, i32 %22, i32 %2)
ret i32 0
}