#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct stat {
  int dummy;
};
struct TYPE_2__ {
  int /*<<< orphan*/ member_0;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int is_in_file(char const *, char *);
int /*<<< orphan*/ mkdir(char *, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ process_request(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ remove(char const *);
int /*<<< orphan*/ rmdir(char *);
int stat(char *, struct stat *);
int /*<<< orphan*/ write_to_file(char const *, char *);
char *test_process_request_index_found() {
  printf("Test process request: List directory\n");
  char *ROOT = "test_root_dir";
  const char *FILENAME = "test_server_index_found";
  const char *INDEXFILE = "test_root_dir/index.html";
  int root_created = 0;
  struct stat *st = {0};
  if (stat(ROOT, st) == -1) {
    mkdir(ROOT, 0700);
    root_created = 1;
  }
  write_to_file(INDEXFILE, "test index file");
  write_to_file(FILENAME, "GET / HTTP/1.1");
  FILE *f = fopen(FILENAME, "a+");
  do {
    if (!(f != NULL))
      return "Error, f == NULL";
  } while (0);
  process_request(f, ROOT);
  fclose(f);
  do {
    if (!(is_in_file(FILENAME, "test index file") == 1))
      return "Error, should've printed directory listing";
  } while (0);
  remove(INDEXFILE);
  if (root_created == 1)
    rmdir(ROOT);
  remove(FILENAME);
  printf("\n");
  return 0;
}


x86 assembly (clang, -O0):
<test_process_request_index_found>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    0xe51(%rip),%rdi
call   1080 <printf@plt>
lea    0xe6b(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    0xe6e(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0xe7b(%rip),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   10b0 <stat@plt>
cmp    $0xffffffff,%eax
jne    120f <test_process_request_index_found+0x6f>
mov    -0x10(%rbp),%rdi
mov    $0x1c0,%esi
call   1040 <mkdir@plt>
movl   $0x1,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe4b(%rip),%rsi
call   1090 <write_to_file@plt>
mov    -0x18(%rbp),%rdi
lea    0xe4b(%rip),%rsi
call   1090 <write_to_file@plt>
mov    -0x18(%rbp),%rdi
lea    0xe4a(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    125e <test_process_request_index_found+0xbe>
lea    0xe32(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    12e0 <test_process_request_index_found+0x140>
jmp    1263 <test_process_request_index_found+0xc3>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <process_request@plt>
mov    -0x38(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x18(%rbp),%rdi
lea    0xde1(%rip),%rsi
call   10a0 <is_in_file@plt>
cmp    $0x1,%eax
je     12a2 <test_process_request_index_found+0x102>
lea    0xdff(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    12e0 <test_process_request_index_found+0x140>
jmp    12a7 <test_process_request_index_found+0x107>
mov    -0x20(%rbp),%rdi
call   1030 <remove@plt>
cmpl   $0x1,-0x24(%rbp)
jne    12c3 <test_process_request_index_found+0x123>
mov    -0x10(%rbp),%rdi
call   1070 <rmdir@plt>
mov    -0x18(%rbp),%rdi
call   1030 <remove@plt>
lea    0xd51(%rip),%rdi
call   1080 <printf@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_process_request_index_found>:
push   %rbp
push   %rbx
push   %rax
lea    0xee3(%rip),%rdi
call   1060 <puts@plt>
lea    0xe3a(%rip),%rdi
xor    %esi,%esi
call   10c0 <stat@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11e5 <test_process_request_index_found+0x35>
lea    0xe25(%rip),%rdi
mov    $0x1c0,%esi
call   1050 <mkdir@plt>
lea    0xe3a(%rip),%rdi
lea    0xe4c(%rip),%rsi
call   10a0 <write_to_file@plt>
lea    0xe0f(%rip),%rbx
lea    0xe49(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <write_to_file@plt>
lea    0xe49(%rip),%rsi
mov    %rbx,%rdi
call   10d0 <fopen@plt>
test   %rax,%rax
je     128b <test_process_request_index_found+0xdb>
mov    %rax,%rbx
lea    0xdd4(%rip),%rsi
mov    %rax,%rdi
call   1070 <process_request@plt>
mov    %rbx,%rdi
call   1080 <fclose@plt>
lea    0xdcb(%rip),%rdi
lea    0xdf5(%rip),%rsi
call   10b0 <is_in_file@plt>
cmp    $0x1,%eax
jne    1294 <test_process_request_index_found+0xe4>
lea    0xdcb(%rip),%rdi
call   1040 <remove@plt>
cmp    $0xffffffff,%ebp
jne    1271 <test_process_request_index_found+0xc1>
lea    0xd94(%rip),%rdi
call   1090 <rmdir@plt>
lea    0xd96(%rip),%rdi
call   1040 <remove@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
jmp    129b <test_process_request_index_found+0xeb>
lea    0xdcf(%rip),%rax
jmp    129b <test_process_request_index_found+0xeb>
lea    0xdd7(%rip),%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [38 x i8] c"Test process request: List directory\0A\00"
@.str.1 = [14 x i8] c"test_root_dir\00"
@.str.2 = [24 x i8] c"test_server_index_found\00"
@.str.3 = [25 x i8] c"test_root_dir/index.html\00"
@.str.4 = [16 x i8] c"test index file\00"
@.str.5 = [15 x i8] c"GET / HTTP/1.1\00"
@.str.6 = [3 x i8] c"a+\00"
@.str.7 = [17 x i8] c"Error, f == NULL\00"
@.str.8 = [43 x i8] c"Error, should've printed directory listing\00"
@.str.9 = [2 x i8] c"\0A\00"
define i8* @test_process_request_index_found() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.stat*
%7 = alloca i32*
%8 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8** %2
store i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8** %3
store i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8** %4
store i32 0, i32* %5
store %struct.stat* null, %struct.stat** %6
%9 = load i8*, i8** %2
%10 = load %struct.stat*, %struct.stat** %6
%11 = call i32 @stat(i8* %9, %struct.stat* %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = load i8*, i8** %2
%15 = call i32 @mkdir(i8* %14, i32 448)
store i32 1, i32* %5
br label %16
16: ; preds = %13, %0
%17 = load i8*, i8** %4
%18 = call i32 @write_to_file(i8* %17, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%19 = load i8*, i8** %3
%20 = call i32 @write_to_file(i8* %19, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%21 = load i8*, i8** %3
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
store i32* %22, i32** %7
br label %23
23: ; preds = %16
%24 = load i32*, i32** %7
%25 = icmp ne i32* %24, null
br i1 %25, label %27, label %26
26: ; preds = %23
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i8** %1
br label %52
27: ; preds = %23
br label %28
28: ; preds = %27
%29 = load i32*, i32** %7
%30 = load i8*, i8** %2
%31 = call i32 @process_request(i32* %29, i8* %30)
%32 = load i32*, i32** %7
%33 = call i32 @fclose(i32* %32)
br label %34
34: ; preds = %28
%35 = load i8*, i8** %3
%36 = call i32 @is_in_file(i8* %35, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%37 = icmp eq i32 %36, 1
br i1 %37, label %39, label %38
38: ; preds = %34
store i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.8, i64 0, i64 0), i8** %1
br label %52
39: ; preds = %34
br label %40
40: ; preds = %39
%41 = load i8*, i8** %4
%42 = call i32 @remove(i8* %41)
%43 = load i32, i32* %5
%44 = icmp eq i32 %43, 1
br i1 %44, label %45, label %48
45: ; preds = %40
%46 = load i8*, i8** %2
%47 = call i32 @rmdir(i8* %46)
br label %48
48: ; preds = %45, %40
%49 = load i8*, i8** %3
%50 = call i32 @remove(i8* %49)
%51 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
store i8* null, i8** %1
br label %52
52: ; preds = %48, %38, %26
%53 = load i8*, i8** %1
ret i8* %53
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str.1 = [14 x i8] c"test_root_dir\00"
@.str.2 = [24 x i8] c"test_server_index_found\00"
@.str.3 = [25 x i8] c"test_root_dir/index.html\00"
@.str.4 = [16 x i8] c"test index file\00"
@.str.5 = [15 x i8] c"GET / HTTP/1.1\00"
@.str.6 = [3 x i8] c"a+\00"
@.str.7 = [17 x i8] c"Error, f == NULL\00"
@.str.8 = [43 x i8] c"Error, should've printed directory listing\00"
@str = [37 x i8] c"Test process request: List directory\00"
define i8* @test_process_request_index_found() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str, i64 0, i64 0))
%2 = tail call i32 @stat(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), %struct.stat* null)
%3 = icmp eq i32 %2, -1
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @mkdir(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 448)
br label %6
6: ; preds = %4, %0
%7 = tail call i32 @write_to_file(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%8 = tail call i32 @write_to_file(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%9 = tail call i32* @fopen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %23, label %11
11: ; preds = %6
%12 = tail call i32 @process_request(i32* %9, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i32 @fclose(i32* %9)
%14 = tail call i32 @is_in_file(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %23
16: ; preds = %11
%17 = tail call i32 @remove(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br i1 %3, label %18, label %20
18: ; preds = %16
%19 = tail call i32 @rmdir(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %16
%21 = tail call i32 @remove(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%22 = tail call i32 @putchar(i32 10)
br label %23
23: ; preds = %11, %6, %20
%24 = phi i8* [ null, %20 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), %6 ], [ getelementptr inbounds ([43 x i8], [43 x i8]* @.str.8, i64 0, i64 0), %11 ]
ret i8* %24
}