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

/* Variables and functions */
int /*<<< orphan*/ ERANGE;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
struct hostent *get_host_by_name(char const *, unsigned char *, int,
                                 int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(struct in_addr *, int /*<<< orphan*/, int);
int host_name_to_addr4(const char *name, struct in_addr *addr) {
  struct hostent *hptr;
  unsigned char buf[8192];
  assert(name != NULL);
  assert(addr != NULL);
  if (!(hptr = get_host_by_name(name, buf, sizeof(buf), NULL)))
    return (-1);
  if (hptr->h_length > 4) {
    errno = ERANGE;
    return (-1);
  }
  memcpy(addr, hptr->h_addr_list[0], hptr->h_length);
  return (0);
}


x86 assembly (clang, -O0):
<host_name_to_addr4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2020,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rdi
lea    -0x2020(%rbp),%rsi
mov    $0x2000,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1030 <get_host_by_name@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    119a <host_name_to_addr4+0x6a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e5 <host_name_to_addr4+0xb5>
mov    -0x20(%rbp),%rax
cmpl   $0x4,(%rax)
jle    11c5 <host_name_to_addr4+0x95>
mov    0x2e3a(%rip),%rax
mov    (%rax),%ecx
mov    0x2e21(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11e5 <host_name_to_addr4+0xb5>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1050 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<host_name_to_addr4>:
push   %r14
push   %rbx
sub    $0x2008,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1040 <assert@plt>
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1040 <assert@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    $0x2000,%edx
xor    %ecx,%ecx
call   1030 <get_host_by_name@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
je     11a6 <host_name_to_addr4+0x76>
mov    (%rcx),%edx
cmp    $0x5,%edx
jl     1196 <host_name_to_addr4+0x66>
mov    0x2e5f(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e46(%rip),%rdx
mov    %ecx,(%rdx)
jmp    11a6 <host_name_to_addr4+0x76>
mov    0x8(%rcx),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1050 <memcpy@plt>
xor    %eax,%eax
add    $0x2008,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.in_addr = type { i32 }
%struct.hostent = type { i32, i32* }
@ERANGE = global i32 0
@errno = global i32 0
define i32 @host_name_to_addr4(i8* %0, %struct.in_addr* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.in_addr*
%6 = alloca %struct.hostent*
%7 = alloca [8192 x i8]
store i8* %0, i8** %4
store %struct.in_addr* %1, %struct.in_addr** %5
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
%12 = load %struct.in_addr*, %struct.in_addr** %5
%13 = icmp ne %struct.in_addr* %12, null
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = load i8*, i8** %4
%17 = getelementptr inbounds [8192 x i8], [8192 x i8]* %7, i64 0, i64 0
%18 = call %struct.hostent* @get_host_by_name(i8* %16, i8* %17, i32 8192, i32* null)
store %struct.hostent* %18, %struct.hostent** %6
%19 = icmp ne %struct.hostent* %18, null
br i1 %19, label %21, label %20
20: ; preds = %2
store i32 -1, i32* %3
br label %39
21: ; preds = %2
%22 = load %struct.hostent*, %struct.hostent** %6
%23 = getelementptr inbounds %struct.hostent, %struct.hostent* %22, i32 0, i32 0
%24 = load i32, i32* %23
%25 = icmp sgt i32 %24, 4
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = load i32, i32* @ERANGE
store i32 %27, i32* @errno
store i32 -1, i32* %3
br label %39
28: ; preds = %21
%29 = load %struct.in_addr*, %struct.in_addr** %5
%30 = load %struct.hostent*, %struct.hostent** %6
%31 = getelementptr inbounds %struct.hostent, %struct.hostent* %30, i32 0, i32 1
%32 = load i32*, i32** %31
%33 = getelementptr inbounds i32, i32* %32, i64 0
%34 = load i32, i32* %33
%35 = load %struct.hostent*, %struct.hostent** %6
%36 = getelementptr inbounds %struct.hostent, %struct.hostent* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = call i32 @memcpy(%struct.in_addr* %29, i32 %34, i32 %37)
store i32 0, i32* %3
br label %39
39: ; preds = %28, %26, %20
%40 = load i32, i32* %3
ret i32 %40
}

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