#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 hostent {
  int /*<<< orphan*/ *h_addr_list;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
unsigned long INADDR_NONE;
struct hostent *gethostbyname(char const *);
unsigned long inet_addr(char const *);
int /*<<< orphan*/ memcpy(unsigned long *, int /*<<< orphan*/, int);
unsigned long inet_hostaddr(const char *hostname) {
  unsigned long addr = inet_addr(hostname);
  if (addr == INADDR_NONE) {
    struct hostent *host = gethostbyname(hostname);
    if (host)
      memcpy(&addr, host->h_addr_list[0], sizeof(addr));
  }
  return addr;
}


x86 assembly (clang, -O0):
<inet_hostaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <inet_addr@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e94(%rip),%rcx
cmp    (%rcx),%rax
jne    1191 <inet_hostaddr+0x61>
mov    -0x8(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     118c <inet_hostaddr+0x5c>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdi
mov    $0x8,%edx
call   1050 <memcpy@plt>
jmp    1191 <inet_hostaddr+0x61>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<inet_hostaddr>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <inet_addr@plt>
mov    %rax,%rbx
mov    %rax,(%rsp)
mov    0x2e9e(%rip),%rax
cmp    (%rax),%rbx
jne    1172 <inet_hostaddr+0x42>
mov    %r14,%rdi
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     1172 <inet_hostaddr+0x42>
mov    (%rax),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
mov    $0x8,%edx
call   1050 <memcpy@plt>
mov    (%rsp),%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.hostent = type { i32* }
@INADDR_NONE = global i64 0
define i64 @inet_hostaddr(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca %struct.hostent*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i64 @inet_addr(i8* %5)
store i64 %6, i64* %3
%7 = load i64, i64* %3
%8 = load i64, i64* @INADDR_NONE
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %23
10: ; preds = %1
%11 = load i8*, i8** %2
%12 = call %struct.hostent* @gethostbyname(i8* %11)
store %struct.hostent* %12, %struct.hostent** %4
%13 = load %struct.hostent*, %struct.hostent** %4
%14 = icmp ne %struct.hostent* %13, null
br i1 %14, label %15, label %22
15: ; preds = %10
%16 = load %struct.hostent*, %struct.hostent** %4
%17 = getelementptr inbounds %struct.hostent, %struct.hostent* %16, i32 0, i32 0
%18 = load i32*, i32** %17
%19 = getelementptr inbounds i32, i32* %18, i64 0
%20 = load i32, i32* %19
%21 = call i32 @memcpy(i64* %3, i32 %20, i32 8)
br label %22
22: ; preds = %15, %10
br label %23
23: ; preds = %22, %1
%24 = load i64, i64* %3
ret i64 %24
}

LLVM IR (-O2):
%struct.hostent = type { i32* }
@INADDR_NONE = global i64 0
define i64 @inet_hostaddr(i8* %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to i8*
%4 = tail call i64 @inet_addr(i8* %0)
store i64 %4, i64* %2
%5 = load i64, i64* @INADDR_NONE
%6 = icmp eq i64 %4, %5
br i1 %6, label %7, label %16
7: ; preds = %1
%8 = tail call %struct.hostent* @gethostbyname(i8* %0)
%9 = icmp eq %struct.hostent* %8, null
br i1 %9, label %16, label %10
10: ; preds = %7
%11 = getelementptr inbounds %struct.hostent, %struct.hostent* %8, i64 0, i32 0
%12 = load i32*, i32** %11
%13 = load i32, i32* %12
%14 = call i32 @memcpy(i64* %2, i32 %13, i32 8)
%15 = load i64, i64* %2
br label %16
16: ; preds = %7, %10, %1
%17 = phi i64 [ %4, %7 ], [ %15, %10 ], [ %4, %1 ]
ret i64 %17
}