#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 h_addrtype;
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ *h_addr_list;
};

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ errno;
struct hostent *gethostbyname2(char const *, int);
int /*<<< orphan*/ memcpy(void *, int /*<<< orphan*/, int /*<<< orphan*/);
int host_or_ip_to_addr(const char *host_or_IP, int af, void *addr) {
  struct hostent *host;
  if (!host_or_IP || !addr) {
    errno = EINVAL;
    return -1;
  }
  host = gethostbyname2(host_or_IP, af);
  if (!host)
    return -1;
  if (host->h_addrtype != af)
    return -1;
  if (!host->h_addr_list[0])
    return -1;
  memcpy(addr, host->h_addr_list[0], host->h_length);
  return 0;
}


x86 assembly (clang, -O0):
<host_or_ip_to_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1149 <host_or_ip_to_addr+0x29>
cmpq   $0x0,-0x20(%rbp)
jne    1167 <host_or_ip_to_addr+0x47>
mov    0x2e88(%rip),%rax
mov    (%rax),%ecx
mov    0x2e87(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11e7 <host_or_ip_to_addr+0xc7>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <gethostbyname2@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    118e <host_or_ip_to_addr+0x6e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e7 <host_or_ip_to_addr+0xc7>
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
cmp    -0x14(%rbp),%eax
je     11a9 <host_or_ip_to_addr+0x89>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e7 <host_or_ip_to_addr+0xc7>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rax
cmpl   $0x0,(%rax)
jne    11c6 <host_or_ip_to_addr+0xa6>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e7 <host_or_ip_to_addr+0xc7>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%edx
call   1040 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<host_or_ip_to_addr>:
push   %rbp
push   %rbx
push   %rax
test   %rdi,%rdi
je     1161 <host_or_ip_to_addr+0x41>
mov    %rdx,%rbx
test   %rdx,%rdx
je     1161 <host_or_ip_to_addr+0x41>
mov    %esi,%ebp
call   1030 <gethostbyname2@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
je     1178 <host_or_ip_to_addr+0x58>
cmp    %ebp,(%rcx)
jne    1178 <host_or_ip_to_addr+0x58>
mov    0x8(%rcx),%rdx
mov    (%rdx),%esi
test   %esi,%esi
je     1178 <host_or_ip_to_addr+0x58>
mov    0x4(%rcx),%edx
mov    %rbx,%rdi
call   1040 <memcpy@plt>
xor    %eax,%eax
jmp    1178 <host_or_ip_to_addr+0x58>
mov    0x2e70(%rip),%rax
mov    (%rax),%eax
mov    0x2e6f(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i32, i32, i32* }
@EINVAL = global i32 0
@errno = global i32 0
define i32 @host_or_ip_to_addr(i8* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.hostent*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%9 = load i8*, i8** %5
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %14
11: ; preds = %3
%12 = load i8*, i8** %7
%13 = icmp ne i8* %12, null
br i1 %13, label %16, label %14
14: ; preds = %11, %3
%15 = load i32, i32* @EINVAL
store i32 %15, i32* @errno
store i32 -1, i32* %4
br label %49
16: ; preds = %11
%17 = load i8*, i8** %5
%18 = load i32, i32* %6
%19 = call %struct.hostent* @gethostbyname2(i8* %17, i32 %18)
store %struct.hostent* %19, %struct.hostent** %8
%20 = load %struct.hostent*, %struct.hostent** %8
%21 = icmp ne %struct.hostent* %20, null
br i1 %21, label %23, label %22
22: ; preds = %16
store i32 -1, i32* %4
br label %49
23: ; preds = %16
%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 = load i32, i32* %6
%28 = icmp ne i32 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %23
store i32 -1, i32* %4
br label %49
30: ; preds = %23
%31 = load %struct.hostent*, %struct.hostent** %8
%32 = getelementptr inbounds %struct.hostent, %struct.hostent* %31, i32 0, i32 2
%33 = load i32*, i32** %32
%34 = getelementptr inbounds i32, i32* %33, i64 0
%35 = load i32, i32* %34
%36 = icmp ne i32 %35, 0
br i1 %36, label %38, label %37
37: ; preds = %30
store i32 -1, i32* %4
br label %49
38: ; preds = %30
%39 = load i8*, i8** %7
%40 = load %struct.hostent*, %struct.hostent** %8
%41 = getelementptr inbounds %struct.hostent, %struct.hostent* %40, i32 0, i32 2
%42 = load i32*, i32** %41
%43 = getelementptr inbounds i32, i32* %42, i64 0
%44 = load i32, i32* %43
%45 = load %struct.hostent*, %struct.hostent** %8
%46 = getelementptr inbounds %struct.hostent, %struct.hostent* %45, i32 0, i32 1
%47 = load i32, i32* %46
%48 = call i32 @memcpy(i8* %39, i32 %44, i32 %47)
store i32 0, i32* %4
br label %49
49: ; preds = %38, %37, %29, %22, %14
%50 = load i32, i32* %4
ret i32 %50
}

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