#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_addr_list;
};

/* Variables and functions */
int AF_INET6;
int /*<<< orphan*/ EINVAL;
int INET6_ADDRSTRLEN;
int INET_ADDRSTRLEN;
int /*<<< orphan*/ errno;
struct hostent *gethostbyname2(char const *, int);
int /*<<< orphan*/ *inet_ntop(int, void *, char *, int);
int get_ip_of_host(const char *host_name, int af, char *IP) {
  struct hostent *host;
  int addrstr_len;
  if (!host_name || !IP) {
    errno = EINVAL;
    return -1;
  }
  if (af == AF_INET6)
    addrstr_len = INET6_ADDRSTRLEN;
  else
    addrstr_len = INET_ADDRSTRLEN;
  host = gethostbyname2(host_name, af);
  if (!host)
    return -1;
  if (!host->h_addr_list[0])
    return -1;
  if (inet_ntop(af, (void *)host->h_addr_list[0], IP, addrstr_len) == NULL)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<get_ip_of_host>:
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 <get_ip_of_host+0x29>
cmpq   $0x0,-0x20(%rbp)
jne    1167 <get_ip_of_host+0x47>
mov    0x2e70(%rip),%rax
mov    (%rax),%ecx
mov    0x2e77(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1210 <get_ip_of_host+0xf0>
mov    -0x14(%rbp),%eax
mov    0x2e6f(%rip),%rcx
cmp    (%rcx),%eax
jne    118a <get_ip_of_host+0x6a>
mov    0x2e68(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    1196 <get_ip_of_host+0x76>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <gethostbyname2@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11bd <get_ip_of_host+0x9d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1210 <get_ip_of_host+0xf0>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
cmpq   $0x0,(%rax)
jne    11da <get_ip_of_host+0xba>
movl   $0xffffffff,-0x4(%rbp)
jmp    1210 <get_ip_of_host+0xf0>
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
call   1030 <inet_ntop@plt>
cmp    $0x0,%rax
jne    1209 <get_ip_of_host+0xe9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1210 <get_ip_of_host+0xf0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ip_of_host>:
push   %rbp
push   %r14
push   %rbx
test   %rdi,%rdi
je     1147 <get_ip_of_host+0x27>
mov    %rdx,%rbx
test   %rdx,%rdx
je     1147 <get_ip_of_host+0x27>
mov    %esi,%ebp
mov    0x2ea6(%rip),%rax
cmp    %esi,(%rax)
jne    1160 <get_ip_of_host+0x40>
mov    0x2ea3(%rip),%rax
jmp    1167 <get_ip_of_host+0x47>
mov    0x2e72(%rip),%rax
mov    (%rax),%eax
mov    0x2e79(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
jmp    119c <get_ip_of_host+0x7c>
mov    0x2e61(%rip),%rax
mov    (%rax),%r14d
mov    %ebp,%esi
call   1040 <gethostbyname2@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
je     119c <get_ip_of_host+0x7c>
mov    (%rcx),%rcx
mov    (%rcx),%rsi
test   %rsi,%rsi
je     119c <get_ip_of_host+0x7c>
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %r14d,%ecx
call   1030 <inet_ntop@plt>
cmp    $0x1,%rax
sbb    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i64* }
@EINVAL = global i32 0
@errno = global i32 0
@AF_INET6 = global i32 0
@INET6_ADDRSTRLEN = global i32 0
@INET_ADDRSTRLEN = global i32 0
define i32 @get_ip_of_host(i8* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.hostent*
%9 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = load i8*, i8** %7
%14 = icmp ne i8* %13, null
br i1 %14, label %17, label %15
15: ; preds = %12, %3
%16 = load i32, i32* @EINVAL
store i32 %16, i32* @errno
store i32 -1, i32* %4
br label %54
17: ; preds = %12
%18 = load i32, i32* %6
%19 = load i32, i32* @AF_INET6
%20 = icmp eq i32 %18, %19
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = load i32, i32* @INET6_ADDRSTRLEN
store i32 %22, i32* %9
br label %25
23: ; preds = %17
%24 = load i32, i32* @INET_ADDRSTRLEN
store i32 %24, i32* %9
br label %25
25: ; preds = %23, %21
%26 = load i8*, i8** %5
%27 = load i32, i32* %6
%28 = call %struct.hostent* @gethostbyname2(i8* %26, i32 %27)
store %struct.hostent* %28, %struct.hostent** %8
%29 = load %struct.hostent*, %struct.hostent** %8
%30 = icmp ne %struct.hostent* %29, null
br i1 %30, label %32, label %31
31: ; preds = %25
store i32 -1, i32* %4
br label %54
32: ; preds = %25
%33 = load %struct.hostent*, %struct.hostent** %8
%34 = getelementptr inbounds %struct.hostent, %struct.hostent* %33, i32 0, i32 0
%35 = load i64*, i64** %34
%36 = getelementptr inbounds i64, i64* %35, i64 0
%37 = load i64, i64* %36
%38 = icmp ne i64 %37, 0
br i1 %38, label %40, label %39
39: ; preds = %32
store i32 -1, i32* %4
br label %54
40: ; preds = %32
%41 = load i32, i32* %6
%42 = load %struct.hostent*, %struct.hostent** %8
%43 = getelementptr inbounds %struct.hostent, %struct.hostent* %42, i32 0, i32 0
%44 = load i64*, i64** %43
%45 = getelementptr inbounds i64, i64* %44, i64 0
%46 = load i64, i64* %45
%47 = inttoptr i64 %46 to i8*
%48 = load i8*, i8** %7
%49 = load i32, i32* %9
%50 = call i32* @inet_ntop(i32 %41, i8* %47, i8* %48, i32 %49)
%51 = icmp eq i32* %50, null
br i1 %51, label %52, label %53
52: ; preds = %40
store i32 -1, i32* %4
br label %54
53: ; preds = %40
store i32 0, i32* %4
br label %54
54: ; preds = %53, %52, %39, %31, %15
%55 = load i32, i32* %4
ret i32 %55
}

LLVM IR (-O2):
%struct.hostent = type { i64* }
@EINVAL = global i32 0
@errno = global i32 0
@AF_INET6 = global i32 0
@INET6_ADDRSTRLEN = global i32 0
@INET_ADDRSTRLEN = global i32 0
define i32 @get_ip_of_host(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 %27
9: ; preds = %3
%10 = load i32, i32* @AF_INET6
%11 = icmp eq i32 %10, %1
%12 = load i32, i32* @INET6_ADDRSTRLEN
%13 = load i32, i32* @INET_ADDRSTRLEN
%14 = select i1 %11, i32 %12, i32 %13
%15 = tail call %struct.hostent* @gethostbyname2(i8* %0, i32 %1)
%16 = icmp eq %struct.hostent* %15, null
br i1 %16, label %27, label %17
17: ; preds = %9
%18 = getelementptr inbounds %struct.hostent, %struct.hostent* %15, i64 0, i32 0
%19 = load i64*, i64** %18
%20 = load i64, i64* %19
%21 = icmp eq i64 %20, 0
br i1 %21, label %27, label %22
22: ; preds = %17
%23 = inttoptr i64 %20 to i8*
%24 = tail call i32* @inet_ntop(i32 %1, i8* %23, i8* %2, i32 %14)
%25 = icmp eq i32* %24, null
%26 = sext i1 %25 to i32
br label %27
27: ; preds = %22, %17, %9, %7
%28 = phi i32 [ -1, %7 ], [ -1, %9 ], [ -1, %17 ], [ %26, %22 ]
ret i32 %28
}