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

/* Variables and functions */
scalar_t__ AF_INET;
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ memcpy(struct in_addr *, int /*<<< orphan*/, int);
int host_to_addr(const char *name, struct in_addr *addr) {
  struct hostent *host;
  if ((host = gethostbyname(name)) != NULL) {
    if (host->h_addrtype != AF_INET || host->h_length != sizeof(struct in_addr))
      return -1;
    memcpy(addr, host->h_addr_list[0], sizeof(struct in_addr));
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<host_to_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11a0 <host_to_addr+0x80>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x2e93(%rip),%rcx
cmp    (%rcx),%rax
jne    1170 <host_to_addr+0x50>
mov    -0x20(%rbp),%rax
movslq 0x8(%rax),%rax
cmp    $0x4,%rax
je     117c <host_to_addr+0x5c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a7 <host_to_addr+0x87>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   1040 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
jmp    11a7 <host_to_addr+0x87>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<host_to_addr>:
push   %rbx
mov    %rsi,%rbx
call   1030 <gethostbyname@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
je     1160 <host_to_addr+0x40>
mov    (%rcx),%rdx
mov    0x2ea8(%rip),%rsi
cmp    (%rsi),%rdx
jne    1160 <host_to_addr+0x40>
cmpl   $0x4,0x8(%rcx)
jne    1160 <host_to_addr+0x40>
mov    0x10(%rcx),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1040 <memcpy@plt>
xor    %eax,%eax
pop    %rbx
ret

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

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