#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
struct hostent *gethostbyaddr(char *, int /*<<< orphan*/, int /*<<< orphan*/);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ strlen(char *);
unsigned long net_lookup(char *host) {
  struct hostent *he;
  if ((he = gethostbyname(host)) == NULL) {
    if ((he = gethostbyaddr(host, strlen(host), AF_INET)) == NULL)
      return 0;
  }
  return *(long *)(he->h_addr_list[0]);
}


x86 assembly (clang, -O0):
<net_lookup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1198 <net_lookup+0x68>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%esi
mov    0x2e77(%rip),%rax
mov    (%rax),%edx
call   1050 <gethostbyaddr@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1193 <net_lookup+0x63>
movq   $0x0,-0x8(%rbp)
jmp    11a9 <net_lookup+0x79>
jmp    1198 <net_lookup+0x68>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<net_lookup>:
push   %rbx
mov    %rdi,%rbx
call   1040 <gethostbyname@plt>
test   %rax,%rax
jne    115e <net_lookup+0x2e>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    0x2e9b(%rip),%rcx
mov    (%rcx),%edx
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <gethostbyaddr@plt>
test   %rax,%rax
je     1169 <net_lookup+0x39>
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%rax
pop    %rbx
ret
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.hostent = type { i64* }
@AF_INET = global i32 0
define i64 @net_lookup(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca %struct.hostent*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call %struct.hostent* @gethostbyname(i8* %5)
store %struct.hostent* %6, %struct.hostent** %4
%7 = icmp eq %struct.hostent* %6, null
br i1 %7, label %8, label %17
8: ; preds = %1
%9 = load i8*, i8** %3
%10 = load i8*, i8** %3
%11 = call i32 @strlen(i8* %10)
%12 = load i32, i32* @AF_INET
%13 = call %struct.hostent* @gethostbyaddr(i8* %9, i32 %11, i32 %12)
store %struct.hostent* %13, %struct.hostent** %4
%14 = icmp eq %struct.hostent* %13, null
br i1 %14, label %15, label %16
15: ; preds = %8
store i64 0, i64* %2
br label %25
16: ; preds = %8
br label %17
17: ; preds = %16, %1
%18 = load %struct.hostent*, %struct.hostent** %4
%19 = getelementptr inbounds %struct.hostent, %struct.hostent* %18, i32 0, i32 0
%20 = load i64*, i64** %19
%21 = getelementptr inbounds i64, i64* %20, i64 0
%22 = load i64, i64* %21
%23 = inttoptr i64 %22 to i64*
%24 = load i64, i64* %23
store i64 %24, i64* %2
br label %25
25: ; preds = %17, %15
%26 = load i64, i64* %2
ret i64 %26
}

LLVM IR (-O2):
%struct.hostent = type { i64* }
@AF_INET = global i32 0
define i64 @net_lookup(i8* %0) {
%2 = tail call %struct.hostent* @gethostbyname(i8* %0)
%3 = icmp eq %struct.hostent* %2, null
br i1 %3, label %4, label %9
4: ; preds = %1
%5 = tail call i32 @strlen(i8* %0)
%6 = load i32, i32* @AF_INET
%7 = tail call %struct.hostent* @gethostbyaddr(i8* %0, i32 %5, i32 %6)
%8 = icmp eq %struct.hostent* %7, null
br i1 %8, label %16, label %9
9: ; preds = %4, %1
%10 = phi %struct.hostent* [ %7, %4 ], [ %2, %1 ]
%11 = getelementptr inbounds %struct.hostent, %struct.hostent* %10, 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
br label %16
16: ; preds = %4, %9
%17 = phi i64 [ %15, %9 ], [ 0, %4 ]
ret i64 %17
}