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

/* Variables and functions */
struct hostent *gethostbyname(char *);
int inet_addr(char *);
int /*<<< orphan*/ memcpy(int *, int /*<<< orphan*/, int /*<<< orphan*/);
u_int32_t host_addr(char *name) {
  struct hostent *e = gethostbyname(name);
  int addr;
  if (e)
    memcpy(&addr, e->h_addr_list[0], e->h_length);
  else {
    addr = inet_addr(name);
    if (addr == -1)
      addr = 0;
  }
  return addr;
}


x86 assembly (clang, -O0):
<host_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1172 <host_addr+0x42>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
lea    -0x14(%rbp),%rdi
call   1050 <memcpy@plt>
jmp    1194 <host_addr+0x64>
mov    -0x8(%rbp),%rdi
call   1030 <inet_addr@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    118f <host_addr+0x5f>
movl   $0x0,-0x14(%rbp)
jmp    1194 <host_addr+0x64>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<host_addr>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     115a <host_addr+0x2a>
mov    0x8(%rax),%rcx
mov    (%rcx),%esi
mov    (%rax),%edx
lea    0xc(%rsp),%rdi
call   1050 <memcpy@plt>
mov    0xc(%rsp),%eax
jmp    116c <host_addr+0x3c>
mov    %rbx,%rdi
call   1030 <inet_addr@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
cmovne %ecx,%eax
add    $0x10,%rsp
pop    %rbx
ret

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

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