#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 {
  scalar_t__ h_addrtype;
  scalar_t__ h_addr;
};

/* Variables and functions */
scalar_t__ AF_INET;
int /*<<< orphan*/ EINVAL;
unsigned long INADDR_ANY;
int /*<<< orphan*/ errno;
struct hostent *gethostbyname(char *);
unsigned long inet_addr(char *);
unsigned long host_addr(char *host) {
  unsigned long bhost = 0;
  struct hostent *hostp;
  if ((host == NULL) || (*host == '\0')) {
    return (INADDR_ANY);
  }
  if ((bhost = inet_addr(host)) > -1) {
    return (bhost);
  }
  if ((hostp = gethostbyname(host)) == NULL) {
    errno = EINVAL;
    return (-1);
  }
  if (hostp->h_addrtype != AF_INET) {
    errno = EINVAL;
    return (-1);
  }
  return (*((unsigned long *)hostp->h_addr));
}


x86 assembly (clang, -O0):
<host_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     114f <host_addr+0x2f>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1162 <host_addr+0x42>
mov    0x2e8a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    1201 <host_addr+0xe1>
mov    -0x10(%rbp),%rdi
call   1030 <inet_addr@plt>
mov    %rax,-0x18(%rbp)
cmp    $0xffffffffffffffff,%rax
jbe    1186 <host_addr+0x66>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1201 <host_addr+0xe1>
mov    -0x10(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11bc <host_addr+0x9c>
mov    0x2e24(%rip),%rax
mov    (%rax),%ecx
mov    0x2e23(%rip),%rax
mov    %ecx,(%rax)
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1201 <host_addr+0xe1>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x2e1e(%rip),%rcx
cmp    (%rcx),%rax
je     11f2 <host_addr+0xd2>
mov    0x2dee(%rip),%rax
mov    (%rax),%ecx
mov    0x2ded(%rip),%rax
mov    %ecx,(%rax)
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1201 <host_addr+0xe1>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<host_addr>:
push   %rbx
test   %rdi,%rdi
je     1158 <host_addr+0x38>
mov    %rdi,%rbx
cmpb   $0x0,(%rdi)
je     1158 <host_addr+0x38>
mov    %rbx,%rdi
call   1030 <inet_addr@plt>
mov    %rbx,%rdi
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     1164 <host_addr+0x44>
mov    (%rax),%rcx
mov    0x2e9b(%rip),%rdx
cmp    (%rdx),%rcx
jne    1164 <host_addr+0x44>
mov    0x8(%rax),%rax
jmp    115f <host_addr+0x3f>
mov    0x2e81(%rip),%rax
mov    (%rax),%rax
pop    %rbx
ret
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    0x2e5c(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffffffffffff,%rax
pop    %rbx
ret

LLVM IR (-O0):
%struct.hostent = type { i64, i64 }
@INADDR_ANY = global i64 0
@EINVAL = global i32 0
@errno = global i32 0
@AF_INET = global i64 0
define i64 @host_addr(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i64
%5 = alloca %struct.hostent*
store i8* %0, i8** %3
store i64 0, i64* %4
%6 = load i8*, i8** %3
%7 = icmp eq i8* %6, null
br i1 %7, label %13, label %8
8: ; preds = %1
%9 = load i8*, i8** %3
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %8, %1
%14 = load i64, i64* @INADDR_ANY
store i64 %14, i64* %2
br label %41
15: ; preds = %8
%16 = load i8*, i8** %3
%17 = call i64 @inet_addr(i8* %16)
store i64 %17, i64* %4
%18 = icmp ugt i64 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = load i64, i64* %4
store i64 %20, i64* %2
br label %41
21: ; preds = %15
%22 = load i8*, i8** %3
%23 = call %struct.hostent* @gethostbyname(i8* %22)
store %struct.hostent* %23, %struct.hostent** %5
%24 = icmp eq %struct.hostent* %23, null
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = load i32, i32* @EINVAL
store i32 %26, i32* @errno
store i64 -1, i64* %2
br label %41
27: ; preds = %21
%28 = load %struct.hostent*, %struct.hostent** %5
%29 = getelementptr inbounds %struct.hostent, %struct.hostent* %28, i32 0, i32 0
%30 = load i64, i64* %29
%31 = load i64, i64* @AF_INET
%32 = icmp ne i64 %30, %31
br i1 %32, label %33, label %35
33: ; preds = %27
%34 = load i32, i32* @EINVAL
store i32 %34, i32* @errno
store i64 -1, i64* %2
br label %41
35: ; preds = %27
%36 = load %struct.hostent*, %struct.hostent** %5
%37 = getelementptr inbounds %struct.hostent, %struct.hostent* %36, i32 0, i32 1
%38 = load i64, i64* %37
%39 = inttoptr i64 %38 to i64*
%40 = load i64, i64* %39
store i64 %40, i64* %2
br label %41
41: ; preds = %35, %33, %25, %19, %13
%42 = load i64, i64* %2
ret i64 %42
}

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