#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 in_addr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int ares__get_hostent(int /*<<< orphan*/ *, struct hostent **);
int /*<<< orphan*/ ares_free_hostent(struct hostent *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ memcmp(int /*<<< orphan*/, struct in_addr *, int);
int file_lookup(struct in_addr *addr, struct hostent **host) {
  FILE *fp;
  int status;
  fp = fopen("/etc/hosts", "r");
  if (!fp)
    return 4;
  while ((status = ares__get_hostent(fp, host)) == 0) {
    if (memcmp((*host)->h_addr, addr, sizeof(struct in_addr)) == 0)
      break;
    ares_free_hostent(*host);
  }
  fclose(fp);
  if (status == 13)
    status = 4;
  if (status != 0)
    *host = NULL;
  return status;
}


x86 assembly (clang, -O0):
<file_lookup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    0xe99(%rip),%rdi
lea    0xe9d(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    118e <file_lookup+0x3e>
movl   $0x4,-0x4(%rbp)
jmp    1218 <file_lookup+0xc8>
jmp    1193 <file_lookup+0x43>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <ares__get_hostent@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jne    11e3 <file_lookup+0x93>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1050 <memcmp@plt>
cmp    $0x0,%rax
jne    11d2 <file_lookup+0x82>
jmp    11e3 <file_lookup+0x93>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <ares_free_hostent@plt>
jmp    1193 <file_lookup+0x43>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0xd,-0x24(%rbp)
jne    11fd <file_lookup+0xad>
movl   $0x4,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1212 <file_lookup+0xc2>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_lookup>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r15
lea    0xe9c(%rip),%rdi
lea    0xea0(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11ca <file_lookup+0x7a>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1060 <ares__get_hostent@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    11d3 <file_lookup+0x83>
jmp    11a7 <file_lookup+0x57>
nopl   0x0(%rax,%rax,1)
mov    (%rbx),%rdi
call   1030 <ares_free_hostent@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <ares__get_hostent@plt>
test   %eax,%eax
jne    11d1 <file_lookup+0x81>
mov    (%rbx),%rax
mov    (%rax),%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1050 <memcmp@plt>
test   %rax,%rax
jne    1190 <file_lookup+0x40>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    11ed <file_lookup+0x9d>
mov    $0x4,%eax
jmp    11ed <file_lookup+0x9d>
mov    %eax,%ebp
mov    %r14,%rdi
call   1040 <fclose@plt>
cmp    $0xd,%ebp
mov    $0x4,%eax
cmovne %ebp,%eax
movq   $0x0,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.in_addr = type { i32 }
%struct.hostent = type { i32 }
@.str = [11 x i8] c"/etc/hosts\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @file_lookup(%struct.in_addr* %0, %struct.hostent** %1) {
%3 = alloca i32
%4 = alloca %struct.in_addr*
%5 = alloca %struct.hostent**
%6 = alloca i32*
%7 = alloca i32
store %struct.in_addr* %0, %struct.in_addr** %4
store %struct.hostent** %1, %struct.hostent*** %5
%8 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %6
%9 = load i32*, i32** %6
%10 = icmp ne i32* %9, null
br i1 %10, label %12, label %11
11: ; preds = %2
store i32 4, i32* %3
br label %44
12: ; preds = %2
br label %13
13: ; preds = %27, %12
%14 = load i32*, i32** %6
%15 = load %struct.hostent**, %struct.hostent*** %5
%16 = call i32 @ares__get_hostent(i32* %14, %struct.hostent** %15)
store i32 %16, i32* %7
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %31
18: ; preds = %13
%19 = load %struct.hostent**, %struct.hostent*** %5
%20 = load %struct.hostent*, %struct.hostent** %19
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i32 0, i32 0
%22 = load i32, i32* %21
%23 = load %struct.in_addr*, %struct.in_addr** %4
%24 = call i64 @memcmp(i32 %22, %struct.in_addr* %23, i32 4)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %18
br label %31
27: ; preds = %18
%28 = load %struct.hostent**, %struct.hostent*** %5
%29 = load %struct.hostent*, %struct.hostent** %28
%30 = call i32 @ares_free_hostent(%struct.hostent* %29)
br label %13
31: ; preds = %26, %13
%32 = load i32*, i32** %6
%33 = call i32 @fclose(i32* %32)
%34 = load i32, i32* %7
%35 = icmp eq i32 %34, 13
br i1 %35, label %36, label %37
36: ; preds = %31
store i32 4, i32* %7
br label %37
37: ; preds = %36, %31
%38 = load i32, i32* %7
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = load %struct.hostent**, %struct.hostent*** %5
store %struct.hostent* null, %struct.hostent** %41
br label %42
42: ; preds = %40, %37
%43 = load i32, i32* %7
store i32 %43, i32* %3
br label %44
44: ; preds = %42, %11
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
%struct.hostent = type { i32 }
@.str = [11 x i8] c"/etc/hosts\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @file_lookup(%struct.in_addr* %0, %struct.hostent** %1) {
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %26, label %5
5: ; preds = %2
%6 = tail call i32 @ares__get_hostent(i32* %3, %struct.hostent** %1)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %19
8: ; preds = %5, %14
%9 = load %struct.hostent*, %struct.hostent** %1
%10 = getelementptr inbounds %struct.hostent, %struct.hostent* %9, i64 0, i32 0
%11 = load i32, i32* %10
%12 = tail call i64 @memcmp(i32 %11, %struct.in_addr* %0, i32 4)
%13 = icmp eq i64 %12, 0
br i1 %13, label %24, label %14
14: ; preds = %8
%15 = load %struct.hostent*, %struct.hostent** %1
%16 = tail call i32 @ares_free_hostent(%struct.hostent* %15)
%17 = tail call i32 @ares__get_hostent(i32* %3, %struct.hostent** %1)
%18 = icmp eq i32 %17, 0
br i1 %18, label %8, label %19
19: ; preds = %14, %5
%20 = phi i32 [ %6, %5 ], [ %17, %14 ]
%21 = tail call i32 @fclose(i32* %3)
%22 = icmp eq i32 %20, 13
%23 = select i1 %22, i32 4, i32 %20
store %struct.hostent* null, %struct.hostent** %1
br label %26
24: ; preds = %8
%25 = tail call i32 @fclose(i32* %3)
br label %26
26: ; preds = %24, %19, %2
%27 = phi i32 [ 4, %2 ], [ %23, %19 ], [ 0, %24 ]
ret i32 %27
}