#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct hostent {
  scalar_t__ *h_addr_list;
};
typedef int /*<<< orphan*/ in_port_t;
typedef int /*<<< orphan*/ in_addr_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ h_errno;
char *hstrerror(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int resolve(const char *host, in_port_t port, struct sockaddr_in *address) {
  struct hostent *host_p;
  host_p = gethostbyname(host);
  if (!host_p) {
    fprintf(stderr, "Resolution of %s failed: %s\n", host, hstrerror(h_errno));
    return -1;
  }
  address->sin_family = AF_INET;
  address->sin_port = htons(port);
  address->sin_addr.s_addr = *((in_addr_t *)host_p->h_addr_list[0]);
  return 0;
}


x86 assembly (clang, -O0):
<resolve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11af <resolve+0x6f>
mov    0x2e5e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
call   1060 <hstrerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xe62(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e9 <resolve+0xa9>
mov    0x2e32(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %esi,%r15d
mov    %rdi,%r14
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     1183 <resolve+0x43>
mov    %rax,%rbp
mov    0x2e84(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rbx)
mov    %r15d,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rbx)
mov    0x0(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,(%rbx)
xor    %eax,%eax
jmp    11b3 <resolve+0x73>
mov    0x2e46(%rip),%rax
mov    (%rax),%ebx
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
call   1060 <hstrerror@plt>
lea    0xe5f(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i64* }
@stderr = global i32 0
@.str = [29 x i8] c"Resolution of %s failed: %s\0A\00"
@h_errno = global i32 0
@AF_INET = global i32 0
define i32 @resolve(i8* %0, i32 %1, %struct.sockaddr_in* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.sockaddr_in*
%8 = alloca %struct.hostent*
store i8* %0, i8** %5
store i32 %1, i32* %6
store %struct.sockaddr_in* %2, %struct.sockaddr_in** %7
%9 = load i8*, i8** %5
%10 = call %struct.hostent* @gethostbyname(i8* %9)
store %struct.hostent* %10, %struct.hostent** %8
%11 = load %struct.hostent*, %struct.hostent** %8
%12 = icmp ne %struct.hostent* %11, null
br i1 %12, label %19, label %13
13: ; preds = %3
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %5
%16 = load i32, i32* @h_errno
%17 = call i8* @hstrerror(i32 %16)
%18 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %17)
store i32 -1, i32* %4
br label %37
19: ; preds = %3
%20 = load i32, i32* @AF_INET
%21 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %21, i32 0, i32 2
store i32 %20, i32* %22
%23 = load i32, i32* %6
%24 = call i32 @htons(i32 %23)
%25 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %25, i32 0, i32 1
store i32 %24, i32* %26
%27 = load %struct.hostent*, %struct.hostent** %8
%28 = getelementptr inbounds %struct.hostent, %struct.hostent* %27, i32 0, i32 0
%29 = load i64*, i64** %28
%30 = getelementptr inbounds i64, i64* %29, i64 0
%31 = load i64, i64* %30
%32 = inttoptr i64 %31 to i32*
%33 = load i32, i32* %32
%34 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %34, i32 0, i32 0
%36 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %35, i32 0, i32 0
store i32 %33, i32* %36
store i32 0, i32* %4
br label %37
37: ; preds = %19, %13
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i64* }
@stderr = global i32 0
@.str = [29 x i8] c"Resolution of %s failed: %s\0A\00"
@h_errno = global i32 0
@AF_INET = global i32 0
define i32 @resolve(i8* %0, i32 %1, %struct.sockaddr_in* nocapture writeonly %2) {
%4 = tail call %struct.hostent* @gethostbyname(i8* %0)
%5 = icmp eq %struct.hostent* %4, null
br i1 %5, label %6, label %11
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = load i32, i32* @h_errno
%9 = tail call i8* @hstrerror(i32 %8)
%10 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %9)
br label %22
11: ; preds = %3
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %12, i32* %13
%14 = tail call i32 @htons(i32 %1)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.hostent, %struct.hostent* %4, i64 0, i32 0
%17 = load i64*, i64** %16
%18 = load i64, i64* %17
%19 = inttoptr i64 %18 to i32*
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %20, i32* %21
br label %22
22: ; preds = %11, %6
%23 = phi i32 [ 0, %11 ], [ -1, %6 ]
ret i32 %23
}