#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 hostent {
  scalar_t__ *h_addr_list;
};
typedef unsigned long in_addr_t;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
struct hostent *gethostbyname(char const *);
unsigned long inet_addr(char const *);
unsigned long ntohl(unsigned long);
unsigned long _GeoIP_lookupaddress(const char *host) {
  unsigned long addr = inet_addr(host);
  struct hostent phe2;
  struct hostent *phe = &phe2;
  char *buf = NULL;
  int result = 0;
  if (addr == -1) {
    phe = gethostbyname(host);
    if (!phe || result != 0) {
      free(buf);
      return 0;
    }
    addr = *((in_addr_t *)phe->h_addr_list[0]);
  }
  return ntohl(addr);
}


x86 assembly (clang, -O0):
<_GeoIP_lookupaddress>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <inet_addr@plt>
mov    %rax,-0x18(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
cmpq   $0xffffffffffffffff,-0x18(%rbp)
jne    11c4 <_GeoIP_lookupaddress+0x84>
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     119d <_GeoIP_lookupaddress+0x5d>
cmpl   $0x0,-0x34(%rbp)
je     11b3 <_GeoIP_lookupaddress+0x73>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11d1 <_GeoIP_lookupaddress+0x91>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <ntohl@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_GeoIP_lookupaddress>:
push   %rbx
mov    %rdi,%rbx
call   1040 <inet_addr@plt>
cmp    $0xffffffffffffffff,%rax
jne    1165 <_GeoIP_lookupaddress+0x25>
mov    %rbx,%rdi
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     116e <_GeoIP_lookupaddress+0x2e>
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    %rax,%rdi
pop    %rbx
jmp    1060 <ntohl@plt>
xor    %edi,%edi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.hostent = type { i64* }
define i64 @_GeoIP_lookupaddress(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i64
%5 = alloca %struct.hostent
%6 = alloca %struct.hostent*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = call i64 @inet_addr(i8* %9)
store i64 %10, i64* %4
store %struct.hostent* %5, %struct.hostent** %6
store i8* null, i8** %7
store i32 0, i32* %8
%11 = load i64, i64* %4
%12 = icmp eq i64 %11, -1
br i1 %12, label %13, label %32
13: ; preds = %1
%14 = load i8*, i8** %3
%15 = call %struct.hostent* @gethostbyname(i8* %14)
store %struct.hostent* %15, %struct.hostent** %6
%16 = load %struct.hostent*, %struct.hostent** %6
%17 = icmp ne %struct.hostent* %16, null
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i32, i32* %8
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18, %13
%22 = load i8*, i8** %7
%23 = call i32 @free(i8* %22)
store i64 0, i64* %2
br label %35
24: ; preds = %18
%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
store i64 %31, i64* %4
br label %32
32: ; preds = %24, %1
%33 = load i64, i64* %4
%34 = call i64 @ntohl(i64 %33)
store i64 %34, i64* %2
br label %35
35: ; preds = %32, %21
%36 = load i64, i64* %2
ret i64 %36
}

LLVM IR (-O2):
%struct.hostent = type { i64* }
define i64 @_GeoIP_lookupaddress(i8* %0) {
%2 = tail call i64 @inet_addr(i8* %0)
%3 = icmp eq i64 %2, -1
br i1 %3, label %4, label %15
4: ; preds = %1
%5 = tail call %struct.hostent* @gethostbyname(i8* %0)
%6 = icmp eq %struct.hostent* %5, null
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i32 @free(i8* null)
br label %18
9: ; preds = %4
%10 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 0
%11 = load i64*, i64** %10
%12 = load i64, i64* %11
%13 = inttoptr i64 %12 to i64*
%14 = load i64, i64* %13
br label %15
15: ; preds = %9, %1
%16 = phi i64 [ %14, %9 ], [ %2, %1 ]
%17 = tail call i64 @ntohl(i64 %16)
br label %18
18: ; preds = %15, %7
%19 = phi i64 [ 0, %7 ], [ %17, %15 ]
ret i64 %19
}