#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 sockaddr_in {
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct hostent {
  scalar_t__ *h_addr_list;
  int /*<<< orphan*/ h_addrtype;
};

/* Variables and functions */
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ gettext(char *);
int /*<<< orphan*/ h_errno;
int /*<<< orphan*/ hstrerror(int /*<<< orphan*/);
int /*<<< orphan*/ set_error(int /*<<< orphan*/, char const *,
                             int /*<<< orphan*/);
int resolve_host_ipv4(const char *host, struct sockaddr_in *addr) {
  struct hostent *hostdnsentries = gethostbyname(host);
  if (hostdnsentries == NULL) {
    set_error(gettext("Problem resolving %s (IPv4): %s"), host,
              hstrerror(h_errno));
    return -1;
  }
  addr->sin_family = hostdnsentries->h_addrtype;
  addr->sin_addr = *((struct in_addr *)hostdnsentries->h_addr_list[0]);
  return 0;
}


x86 assembly (clang, -O0):
<resolve_host_ipv4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a7 <resolve_host_ipv4+0x67>
lea    0xe91(%rip),%rdi
call   1030 <gettext@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
call   1060 <hstrerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
call   1040 <set_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ce <resolve_host_ipv4+0x8e>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rcx),%rcx
mov    (%rcx),%rcx
mov    (%rcx),%ecx
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve_host_ipv4>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     1168 <resolve_host_ipv4+0x28>
mov    0x8(%rax),%ecx
mov    %ecx,0x4(%rbx)
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,(%rbx)
xor    %eax,%eax
jmp    1195 <resolve_host_ipv4+0x55>
lea    0xe91(%rip),%rdi
call   1030 <gettext@plt>
mov    %eax,%ebx
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
call   1060 <hstrerror@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <set_error@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.in_addr, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64*, i32 }
@.str = [32 x i8] c"Problem resolving %s (IPv4): %s\00"
@h_errno = global i32 0
define i32 @resolve_host_ipv4(i8* %0, %struct.sockaddr_in* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.sockaddr_in*
%6 = alloca %struct.hostent*
store i8* %0, i8** %4
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %5
%7 = load i8*, i8** %4
%8 = call %struct.hostent* @gethostbyname(i8* %7)
store %struct.hostent* %8, %struct.hostent** %6
%9 = load %struct.hostent*, %struct.hostent** %6
%10 = icmp eq %struct.hostent* %9, null
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = call i32 @gettext(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%13 = load i8*, i8** %4
%14 = load i32, i32* @h_errno
%15 = call i32 @hstrerror(i32 %14)
%16 = call i32 @set_error(i32 %12, i8* %13, i32 %15)
store i32 -1, i32* %3
br label %33
17: ; preds = %2
%18 = load %struct.hostent*, %struct.hostent** %6
%19 = getelementptr inbounds %struct.hostent, %struct.hostent* %18, i32 0, i32 1
%20 = load i32, i32* %19
%21 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %21, i32 0, i32 1
store i32 %20, i32* %22
%23 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %23, i32 0, i32 0
%25 = load %struct.hostent*, %struct.hostent** %6
%26 = getelementptr inbounds %struct.hostent, %struct.hostent* %25, i32 0, i32 0
%27 = load i64*, i64** %26
%28 = getelementptr inbounds i64, i64* %27, i64 0
%29 = load i64, i64* %28
%30 = inttoptr i64 %29 to %struct.in_addr*
%31 = bitcast %struct.in_addr* %24 to i8*
%32 = bitcast %struct.in_addr* %30 to i8*
store i32 0, i32* %3
br label %33
33: ; preds = %17, %11
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.in_addr, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64*, i32 }
@.str = [32 x i8] c"Problem resolving %s (IPv4): %s\00"
@h_errno = global i32 0
define i32 @resolve_host_ipv4(i8* %0, %struct.sockaddr_in* nocapture writeonly %1) {
%3 = tail call %struct.hostent* @gethostbyname(i8* %0)
%4 = icmp eq %struct.hostent* %3, null
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = tail call i32 @gettext(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @h_errno
%8 = tail call i32 @hstrerror(i32 %7)
%9 = tail call i32 @set_error(i32 %6, i8* %0, i32 %8)
br label %20
10: ; preds = %2
%11 = getelementptr inbounds %struct.hostent, %struct.hostent* %3, i64 0, i32 1
%12 = load i32, i32* %11
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.hostent, %struct.hostent* %3, i64 0, i32 0
%15 = load i64*, i64** %14
%16 = load i64, i64* %15
%17 = inttoptr i64 %16 to i32*
%18 = getelementptr %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
%19 = load i32, i32* %17
store i32 %19, i32* %18
br label %20
20: ; preds = %10, %5
%21 = phi i32 [ -1, %5 ], [ 0, %10 ]
ret i32 %21
}