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

/* Variables and functions */
scalar_t__ INADDR_NONE;
struct hostent *gethostbyname(char *);
scalar_t__ inet_addr(char *);
int /*<<< orphan*/ memcpy(scalar_t__ *, int /*<<< orphan*/, int);
uint32_t resolvehost4(char *host) {
  uint32_t ip;
  struct hostent *hp;
  ip = inet_addr(host);
  if (ip == INADDR_NONE) {
    hp = gethostbyname(host);
    memcpy(&ip, hp->h_addr, 4);
  }
  return (ip);
}


x86 assembly (clang, -O0):
<resolvehost4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <inet_addr@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e94(%rip),%rcx
cmp    (%rcx),%rax
jne    117e <resolvehost4+0x4e>
mov    -0x8(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdi
mov    $0x4,%edx
call   1050 <memcpy@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolvehost4>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
call   1030 <inet_addr@plt>
mov    %rax,0x8(%rsp)
mov    0x2e9f(%rip),%rcx
cmp    (%rcx),%rax
jne    116c <resolvehost4+0x3c>
mov    %rbx,%rdi
call   1040 <gethostbyname@plt>
mov    (%rax),%esi
lea    0x8(%rsp),%rdi
mov    $0x4,%edx
call   1050 <memcpy@plt>
mov    0x8(%rsp),%rax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.hostent = type { i32 }
@INADDR_NONE = global i64 0
define i64 @resolvehost4(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca %struct.hostent*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i64 @inet_addr(i8* %5)
store i64 %6, i64* %3
%7 = load i64, i64* %3
%8 = load i64, i64* @INADDR_NONE
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %17
10: ; preds = %1
%11 = load i8*, i8** %2
%12 = call %struct.hostent* @gethostbyname(i8* %11)
store %struct.hostent* %12, %struct.hostent** %4
%13 = load %struct.hostent*, %struct.hostent** %4
%14 = getelementptr inbounds %struct.hostent, %struct.hostent* %13, i32 0, i32 0
%15 = load i32, i32* %14
%16 = call i32 @memcpy(i64* %3, i32 %15, i32 4)
br label %17
17: ; preds = %10, %1
%18 = load i64, i64* %3
ret i64 %18
}

LLVM IR (-O2):
%struct.hostent = type { i32 }
@INADDR_NONE = global i64 0
define i64 @resolvehost4(i8* %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to i8*
%4 = tail call i64 @inet_addr(i8* %0)
store i64 %4, i64* %2
%5 = load i64, i64* @INADDR_NONE
%6 = icmp eq i64 %4, %5
br i1 %6, label %7, label %13
7: ; preds = %1
%8 = tail call %struct.hostent* @gethostbyname(i8* %0)
%9 = getelementptr inbounds %struct.hostent, %struct.hostent* %8, i64 0, i32 0
%10 = load i32, i32* %9
%11 = call i32 @memcpy(i64* %2, i32 %10, i32 4)
%12 = load i64, i64* %2
br label %13
13: ; preds = %7, %1
%14 = phi i64 [ %12, %7 ], [ %4, %1 ]
ret i64 %14
}