#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 */
typedef scalar_t__ uint32_t;
struct hostent {
  scalar_t__ *h_addr_list;
};

/* Variables and functions */
scalar_t__ INADDR_NONE;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
struct hostent *gethostbyname(char const *);
scalar_t__ inet_addr(char const *);
scalar_t__ ntohl(scalar_t__);
int /*<<< orphan*/ stderr;
int resolve(const char *hostname, uint32_t *address) {
  *address = ntohl(inet_addr(hostname));
  if (*address == INADDR_NONE) {
    struct hostent *h = gethostbyname(hostname);
    if (!h) {
      fprintf(stderr, "unable to resolve: %s\n", hostname);
      return 1;
    }
    *address = ntohl(*(uint32_t *)h->h_addr_list[0]);
  }
  return 0;
}


x86 assembly (clang, -O0):
<resolve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <inet_addr@plt>
mov    %rax,%rdi
call   1060 <ntohl@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2e6f(%rip),%rcx
cmp    (%rcx),%rax
jne    11db <resolve+0x9b>
mov    -0x10(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11bf <resolve+0x7f>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe52(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11e2 <resolve+0xa2>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%rdi
call   1060 <ntohl@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <inet_addr@plt>
mov    %rax,%rdi
call   1060 <ntohl@plt>
mov    %rax,(%r14)
mov    0x2e87(%rip),%rcx
xor    %ebp,%ebp
cmp    (%rcx),%rax
jne    11a5 <resolve+0x65>
mov    %rbx,%rdi
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     1188 <resolve+0x48>
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%rdi
call   1060 <ntohl@plt>
mov    %rax,(%r14)
jmp    11a5 <resolve+0x65>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
lea    0xe68(%rip),%rsi
mov    %rbx,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i64* }
@INADDR_NONE = global i64 0
@stderr = global i32 0
@.str = [23 x i8] c"unable to resolve: %s\0A\00"
define i32 @resolve(i8* %0, i64* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca %struct.hostent*
store i8* %0, i8** %4
store i64* %1, i64** %5
%7 = load i8*, i8** %4
%8 = call i64 @inet_addr(i8* %7)
%9 = call i64 @ntohl(i64 %8)
%10 = load i64*, i64** %5
store i64 %9, i64* %10
%11 = load i64*, i64** %5
%12 = load i64, i64* %11
%13 = load i64, i64* @INADDR_NONE
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %34
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = call %struct.hostent* @gethostbyname(i8* %16)
store %struct.hostent* %17, %struct.hostent** %6
%18 = load %struct.hostent*, %struct.hostent** %6
%19 = icmp ne %struct.hostent* %18, null
br i1 %19, label %24, label %20
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = load i8*, i8** %4
%23 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %22)
store i32 1, i32* %3
br label %35
24: ; preds = %15
%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 i64*
%31 = load i64, i64* %30
%32 = call i64 @ntohl(i64 %31)
%33 = load i64*, i64** %5
store i64 %32, i64* %33
br label %34
34: ; preds = %24, %2
store i32 0, i32* %3
br label %35
35: ; preds = %34, %20
%36 = load i32, i32* %3
ret i32 %36
}

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