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

/* Variables and functions */
struct hostent *gethostbyname(char const *);
unsigned int inet_addr(char const *);
int /*<<< orphan*/ log_message(char *, char const *, ...);
int /*<<< orphan*/ memcpy(unsigned int *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ my_ntoa(unsigned int);
unsigned int lookup_ip(const char *host) {
  struct hostent *he;
  unsigned int ip;
  log_message("lookup_ip: resolving %s", host);
  ip = inet_addr(host);
  if (ip == 0xffffffff) {
    he = gethostbyname(host);
    if (!he) {
      log_message("lookup_ip: can't find ip for host %s", host);
      return 0;
    }
    memcpy(&ip, he->h_addr_list[0], he->h_length);
  }
  log_message("lookup_ip: %s is %s", host, my_ntoa(ip));
  return ip;
}


x86 assembly (clang, -O0):
<lookup_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe99(%rip),%rdi
mov    $0x0,%al
call   1060 <log_message@plt>
mov    -0x10(%rbp),%rdi
call   1030 <inet_addr@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11d3 <lookup_ip+0x83>
mov    -0x10(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ba <lookup_ip+0x6a>
mov    -0x10(%rbp),%rsi
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1060 <log_message@plt>
movl   $0x0,-0x4(%rbp)
jmp    11fd <lookup_ip+0xad>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rdi
call   1050 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
call   1070 <my_ntoa@plt>
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1060 <log_message@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lookup_ip>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0xea3(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <log_message@plt>
mov    %rbx,%rdi
call   1030 <inet_addr@plt>
mov    %eax,0x4(%rsp)
cmp    $0xffffffff,%eax
jne    119b <lookup_ip+0x4b>
mov    %rbx,%rdi
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     11c2 <lookup_ip+0x72>
mov    0x8(%rax),%rcx
mov    (%rcx),%esi
mov    (%rax),%edx
lea    0x4(%rsp),%rdi
call   1050 <memcpy@plt>
mov    0x4(%rsp),%eax
mov    %eax,%edi
call   1070 <my_ntoa@plt>
lea    0xe94(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <log_message@plt>
mov    0x4(%rsp),%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe4f(%rip),%rdi
xor    %ebp,%ebp
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <log_message@plt>
jmp    11b9 <lookup_ip+0x69>

LLVM IR (-O0):
%struct.hostent = type { i32, i32* }
@.str = [24 x i8] c"lookup_ip: resolving %s\00"
@.str.1 = [37 x i8] c"lookup_ip: can't find ip for host %s\00"
@.str.2 = [20 x i8] c"lookup_ip: %s is %s\00"
define i32 @lookup_ip(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.hostent*
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 (i8*, i8*, ...) @log_message(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = load i8*, i8** %3
%9 = call i32 @inet_addr(i8* %8)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %30
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = call %struct.hostent* @gethostbyname(i8* %13)
store %struct.hostent* %14, %struct.hostent** %4
%15 = load %struct.hostent*, %struct.hostent** %4
%16 = icmp ne %struct.hostent* %15, null
br i1 %16, label %20, label %17
17: ; preds = %12
%18 = load i8*, i8** %3
%19 = call i32 (i8*, i8*, ...) @log_message(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %18)
store i32 0, i32* %2
br label %36
20: ; preds = %12
%21 = load %struct.hostent*, %struct.hostent** %4
%22 = getelementptr inbounds %struct.hostent, %struct.hostent* %21, i32 0, i32 1
%23 = load i32*, i32** %22
%24 = getelementptr inbounds i32, i32* %23, i64 0
%25 = load i32, i32* %24
%26 = load %struct.hostent*, %struct.hostent** %4
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %26, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @memcpy(i32* %5, i32 %25, i32 %28)
br label %30
30: ; preds = %20, %1
%31 = load i8*, i8** %3
%32 = load i32, i32* %5
%33 = call i32 @my_ntoa(i32 %32)
%34 = call i32 (i8*, i8*, ...) @log_message(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %31, i32 %33)
%35 = load i32, i32* %5
store i32 %35, i32* %2
br label %36
36: ; preds = %30, %17
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
%struct.hostent = type { i32, i32* }
@.str = [24 x i8] c"lookup_ip: resolving %s\00"
@.str.1 = [37 x i8] c"lookup_ip: can't find ip for host %s\00"
@.str.2 = [20 x i8] c"lookup_ip: %s is %s\00"
define i32 @lookup_ip(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i32 (i8*, i8*, ...) @log_message(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = tail call i32 @inet_addr(i8* %0)
store i32 %5, i32* %2
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %20
7: ; preds = %1
%8 = tail call %struct.hostent* @gethostbyname(i8* %0)
%9 = icmp eq %struct.hostent* %8, null
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 (i8*, i8*, ...) @log_message(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %25
12: ; preds = %7
%13 = getelementptr inbounds %struct.hostent, %struct.hostent* %8, i64 0, i32 1
%14 = load i32*, i32** %13
%15 = load i32, i32* %14
%16 = getelementptr inbounds %struct.hostent, %struct.hostent* %8, i64 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @memcpy(i32* %2, i32 %15, i32 %17)
%19 = load i32, i32* %2
br label %20
20: ; preds = %12, %1
%21 = phi i32 [ %19, %12 ], [ %5, %1 ]
%22 = call i32 @my_ntoa(i32 %21)
%23 = call i32 (i8*, i8*, ...) @log_message(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %22)
%24 = load i32, i32* %2
br label %25
25: ; preds = %20, %10
%26 = phi i32 [ %24, %20 ], [ 0, %10 ]
ret i32 %26
}