#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 {
  char *h_name;
  char **h_aliases;
};
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__ strcasecmp(char *, char const *);
int file_lookup(const char *name, struct hostent **host) {
  FILE *fp;
  char **alias;
  int status;
  fp = fopen("/etc/hosts", "r");
  if (!fp)
    return 4;
  while ((status = ares__get_hostent(fp, host)) == 0) {
    if (strcasecmp((*host)->h_name, name) == 0)
      break;
    for (alias = (*host)->h_aliases; *alias; alias++) {
      if (strcasecmp(*alias, name) == 0)
        break;
    }
    if (*alias)
      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    1279 <file_lookup+0x129>
jmp    1193 <file_lookup+0x43>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <ares__get_hostent@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jne    1244 <file_lookup+0xf4>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcasecmp@plt>
cmp    $0x0,%rax
jne    11ce <file_lookup+0x7e>
jmp    1244 <file_lookup+0xf4>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmpq   $0x0,(%rax)
je     1220 <file_lookup+0xd0>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcasecmp@plt>
cmp    $0x0,%rax
jne    120a <file_lookup+0xba>
jmp    1220 <file_lookup+0xd0>
jmp    120f <file_lookup+0xbf>
mov    -0x28(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x28(%rbp)
jmp    11dd <file_lookup+0x8d>
mov    -0x28(%rbp),%rax
cmpq   $0x0,(%rax)
je     1233 <file_lookup+0xe3>
jmp    1244 <file_lookup+0xf4>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <ares_free_hostent@plt>
jmp    1193 <file_lookup+0x43>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
cmpl   $0xd,-0x2c(%rbp)
jne    125e <file_lookup+0x10e>
movl   $0x4,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     1273 <file_lookup+0x123>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x2c(%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   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe9b(%rip),%rdi
lea    0xe9f(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     120c <file_lookup+0xbc>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
call   1060 <ares__get_hostent@plt>
mov    %eax,%ebp
test   %eax,%eax
je     11c7 <file_lookup+0x77>
mov    %r15,%rdi
call   1050 <fclose@plt>
cmp    $0xd,%ebp
mov    $0x4,%eax
cmovne %ebp,%eax
movq   $0x0,(%r14)
jmp    121d <file_lookup+0xcd>
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1030 <ares_free_hostent@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1060 <ares__get_hostent@plt>
test   %eax,%eax
jne    1226 <file_lookup+0xd6>
mov    (%r14),%r12
mov    (%r12),%rdi
mov    %rbx,%rsi
call   1040 <strcasecmp@plt>
test   %rax,%rax
je     1213 <file_lookup+0xc3>
mov    0x8(%r12),%rbp
mov    0x0(%rbp),%rdi
test   %rdi,%rdi
je     11b0 <file_lookup+0x60>
add    $0x8,%rbp
nopl   (%rax)
mov    %rbx,%rsi
call   1040 <strcasecmp@plt>
test   %rax,%rax
je     1213 <file_lookup+0xc3>
mov    0x0(%rbp),%rdi
add    $0x8,%rbp
test   %rdi,%rdi
jne    11f0 <file_lookup+0xa0>
jmp    11b0 <file_lookup+0x60>
mov    $0x4,%eax
jmp    121d <file_lookup+0xcd>
mov    %r15,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebp
jmp    118e <file_lookup+0x3e>

LLVM IR (-O0):
%struct.hostent = type { i8*, i8** }
@.str = [11 x i8] c"/etc/hosts\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @file_lookup(i8* %0, %struct.hostent** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.hostent**
%6 = alloca i32*
%7 = alloca i8**
%8 = alloca i32
store i8* %0, i8** %4
store %struct.hostent** %1, %struct.hostent*** %5
%9 = 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* %9, i32** %6
%10 = load i32*, i32** %6
%11 = icmp ne i32* %10, null
br i1 %11, label %13, label %12
12: ; preds = %2
store i32 4, i32* %3
br label %70
13: ; preds = %2
br label %14
14: ; preds = %53, %13
%15 = load i32*, i32** %6
%16 = load %struct.hostent**, %struct.hostent*** %5
%17 = call i32 @ares__get_hostent(i32* %15, %struct.hostent** %16)
store i32 %17, i32* %8
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %57
19: ; preds = %14
%20 = load %struct.hostent**, %struct.hostent*** %5
%21 = load %struct.hostent*, %struct.hostent** %20
%22 = getelementptr inbounds %struct.hostent, %struct.hostent* %21, i32 0, i32 0
%23 = load i8*, i8** %22
%24 = load i8*, i8** %4
%25 = call i64 @strcasecmp(i8* %23, i8* %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %19
br label %57
28: ; preds = %19
%29 = load %struct.hostent**, %struct.hostent*** %5
%30 = load %struct.hostent*, %struct.hostent** %29
%31 = getelementptr inbounds %struct.hostent, %struct.hostent* %30, i32 0, i32 1
%32 = load i8**, i8*** %31
store i8** %32, i8*** %7
br label %33
33: ; preds = %45, %28
%34 = load i8**, i8*** %7
%35 = load i8*, i8** %34
%36 = icmp ne i8* %35, null
br i1 %36, label %37, label %48
37: ; preds = %33
%38 = load i8**, i8*** %7
%39 = load i8*, i8** %38
%40 = load i8*, i8** %4
%41 = call i64 @strcasecmp(i8* %39, i8* %40)
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %37
br label %48
44: ; preds = %37
br label %45
45: ; preds = %44
%46 = load i8**, i8*** %7
%47 = getelementptr inbounds i8*, i8** %46, i32 1
store i8** %47, i8*** %7
br label %33
48: ; preds = %43, %33
%49 = load i8**, i8*** %7
%50 = load i8*, i8** %49
%51 = icmp ne i8* %50, null
br i1 %51, label %52, label %53
52: ; preds = %48
br label %57
53: ; preds = %48
%54 = load %struct.hostent**, %struct.hostent*** %5
%55 = load %struct.hostent*, %struct.hostent** %54
%56 = call i32 @ares_free_hostent(%struct.hostent* %55)
br label %14
57: ; preds = %52, %27, %14
%58 = load i32*, i32** %6
%59 = call i32 @fclose(i32* %58)
%60 = load i32, i32* %8
%61 = icmp eq i32 %60, 13
br i1 %61, label %62, label %63
62: ; preds = %57
store i32 4, i32* %8
br label %63
63: ; preds = %62, %57
%64 = load i32, i32* %8
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %63
%67 = load %struct.hostent**, %struct.hostent*** %5
store %struct.hostent* null, %struct.hostent** %67
br label %68
68: ; preds = %66, %63
%69 = load i32, i32* %8
store i32 %69, i32* %3
br label %70
70: ; preds = %68, %12
%71 = load i32, i32* %3
ret i32 %71
}

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