#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 /*<<< orphan*/ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ errx(int, char *, char const *, int /*<<< orphan*/);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ h_errno;
int /*<<< orphan*/ hstrerror(int /*<<< orphan*/);
scalar_t__ inet_aton(char const *, struct in_addr *);
int /*<<< orphan*/ memcpy(struct in_addr *, int /*<<< orphan*/, int);
void gethostaddr(const char *host, struct in_addr *ia) {
  struct hostent *hp;
  if (inet_aton(host, ia) != 0)
    return;
  hp = gethostbyname(host);
  if (hp == NULL)
    errx(1, "Cannot get host address for %s: %s", host, hstrerror(h_errno));
  (void)memcpy(ia, hp->h_addr, sizeof(*ia));
}


x86 assembly (clang, -O0):
<gethostaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <inet_aton@plt>
cmp    $0x0,%rax
je     117c <gethostaddr+0x2c>
jmp    11d5 <gethostaddr+0x85>
mov    -0x8(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c1 <gethostaddr+0x71>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
call   1070 <hstrerror@plt>
mov    -0x20(%rbp),%rdx
mov    %eax,%ecx
mov    $0x1,%edi
lea    0xe44(%rip),%rsi
call   1030 <errx@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   1050 <memcpy@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gethostaddr>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
call   1060 <inet_aton@plt>
test   %rax,%rax
je     116b <gethostaddr+0x1b>
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %rbx,%rdi
call   1040 <gethostbyname@plt>
mov    %rax,%r15
test   %rax,%rax
jne    119f <gethostaddr+0x4f>
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
call   1070 <hstrerror@plt>
lea    0xe70(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
mov    %eax,%ecx
call   1030 <errx@plt>
mov    (%r15),%esi
mov    %r14,%rdi
mov    $0x4,%edx
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <memcpy@plt>

LLVM IR (-O0):
%struct.in_addr = type { i32 }
%struct.hostent = type { i32 }
@.str = [35 x i8] c"Cannot get host address for %s: %s\00"
@h_errno = global i32 0
define void @gethostaddr(i8* %0, %struct.in_addr* %1) {
%3 = alloca i8*
%4 = alloca %struct.in_addr*
%5 = alloca %struct.hostent*
store i8* %0, i8** %3
store %struct.in_addr* %1, %struct.in_addr** %4
%6 = load i8*, i8** %3
%7 = load %struct.in_addr*, %struct.in_addr** %4
%8 = call i64 @inet_aton(i8* %6, %struct.in_addr* %7)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %2
br label %27
11: ; preds = %2
%12 = load i8*, i8** %3
%13 = call %struct.hostent* @gethostbyname(i8* %12)
store %struct.hostent* %13, %struct.hostent** %5
%14 = load %struct.hostent*, %struct.hostent** %5
%15 = icmp eq %struct.hostent* %14, null
br i1 %15, label %16, label %21
16: ; preds = %11
%17 = load i8*, i8** %3
%18 = load i32, i32* @h_errno
%19 = call i32 @hstrerror(i32 %18)
%20 = call i32 @errx(i32 1, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %17, i32 %19)
br label %21
21: ; preds = %16, %11
%22 = load %struct.in_addr*, %struct.in_addr** %4
%23 = load %struct.hostent*, %struct.hostent** %5
%24 = getelementptr inbounds %struct.hostent, %struct.hostent* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @memcpy(%struct.in_addr* %22, i32 %25, i32 4)
br label %27
27: ; preds = %21, %10
ret void
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
%struct.hostent = type { i32 }
@.str = [35 x i8] c"Cannot get host address for %s: %s\00"
@h_errno = global i32 0
define void @gethostaddr(i8* %0, %struct.in_addr* %1) {
%3 = tail call i64 @inet_aton(i8* %0, %struct.in_addr* %1)
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %16
5: ; preds = %2
%6 = tail call %struct.hostent* @gethostbyname(i8* %0)
%7 = icmp eq %struct.hostent* %6, null
br i1 %7, label %8, label %12
8: ; preds = %5
%9 = load i32, i32* @h_errno
%10 = tail call i32 @hstrerror(i32 %9)
%11 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %10)
br label %12
12: ; preds = %8, %5
%13 = getelementptr inbounds %struct.hostent, %struct.hostent* %6, i64 0, i32 0
%14 = load i32, i32* %13
%15 = tail call i32 @memcpy(%struct.in_addr* %1, i32 %14, i32 4)
br label %16
16: ; preds = %2, %12
ret void
}