#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_list_dir() {
  printf("Test process request: List directory\n");
  char *ROOT = "test_root_dir";
  const char *FILENAME = "test_server_list_dir";
  int root_created = 0;
  struct stat *st = {0};
  if (stat(ROOT, st) == -1) {
    mkdir(ROOT, 0700);
    root_created = 1;
  }
  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, "Index of /") == 1))
      return "Error, should've printed directory listing";
  } while (0);
  if (root_created == 1)
    rmdir(ROOT);
  remove(FILENAME);
  printf("\n");
  return 0;
}


x86 assembly (clang, -O0):
<test_process_request_list_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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)
movl   $0x0,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   10b0 <stat@plt>
cmp    $0xffffffff,%eax
jne    1204 <test_process_request_list_dir+0x64>
mov    -0x10(%rbp),%rdi
mov    $0x1c0,%esi
call   1040 <mkdir@plt>
movl   $0x1,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe3a(%rip),%rsi
call   1090 <write_to_file@plt>
mov    -0x18(%rbp),%rdi
lea    0xe39(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1243 <test_process_request_list_dir+0xa3>
lea    0xe21(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    12bc <test_process_request_list_dir+0x11c>
jmp    1248 <test_process_request_list_dir+0xa8>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <process_request@plt>
mov    -0x30(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x18(%rbp),%rdi
lea    0xe03(%rip),%rsi
call   10a0 <is_in_file@plt>
cmp    $0x1,%eax
je     1287 <test_process_request_list_dir+0xe7>
lea    0xdf9(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    12bc <test_process_request_list_dir+0x11c>
jmp    128c <test_process_request_list_dir+0xec>
cmpl   $0x1,-0x1c(%rbp)
jne    129f <test_process_request_list_dir+0xff>
mov    -0x10(%rbp),%rdi
call   1070 <rmdir@plt>
mov    -0x18(%rbp),%rdi
call   1030 <remove@plt>
lea    0xd75(%rip),%rdi
call   1080 <printf@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_process_request_list_dir>:
push   %rbp
push   %rbx
push   %rax
lea    0xec2(%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_list_dir+0x35>
lea    0xe25(%rip),%rdi
mov    $0x1c0,%esi
call   1050 <mkdir@plt>
lea    0xe22(%rip),%rbx
lea    0xe30(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <write_to_file@plt>
lea    0xe30(%rip),%rsi
mov    %rbx,%rdi
call   10d0 <fopen@plt>
test   %rax,%rax
je     126c <test_process_request_list_dir+0xbc>
mov    %rax,%rbx
lea    0xde7(%rip),%rsi
mov    %rax,%rdi
call   1070 <process_request@plt>
mov    %rbx,%rdi
call   1080 <fclose@plt>
lea    0xdde(%rip),%rdi
lea    0xe0f(%rip),%rsi
call   10b0 <is_in_file@plt>
cmp    $0x1,%eax
jne    1275 <test_process_request_list_dir+0xc5>
cmp    $0xffffffff,%ebp
jne    1252 <test_process_request_list_dir+0xa2>
lea    0xdb3(%rip),%rdi
call   1090 <rmdir@plt>
lea    0xdb5(%rip),%rdi
call   1040 <remove@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
jmp    127c <test_process_request_list_dir+0xcc>
lea    0xdc2(%rip),%rax
jmp    127c <test_process_request_list_dir+0xcc>
lea    0xdd5(%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 = [21 x i8] c"test_server_list_dir\00"
@.str.3 = [15 x i8] c"GET / HTTP/1.1\00"
@.str.4 = [3 x i8] c"a+\00"
@.str.5 = [17 x i8] c"Error, f == NULL\00"
@.str.6 = [11 x i8] c"Index of /\00"
@.str.7 = [43 x i8] c"Error, should've printed directory listing\00"
@.str.8 = [2 x i8] c"\0A\00"
define i8* @test_process_request_list_dir() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.stat*
%6 = alloca i32*
%7 = 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 ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8** %3
store i32 0, i32* %4
store %struct.stat* null, %struct.stat** %5
%8 = load i8*, i8** %2
%9 = load %struct.stat*, %struct.stat** %5
%10 = call i32 @stat(i8* %8, %struct.stat* %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = load i8*, i8** %2
%14 = call i32 @mkdir(i8* %13, i32 448)
store i32 1, i32* %4
br label %15
15: ; preds = %12, %0
%16 = load i8*, i8** %3
%17 = call i32 @write_to_file(i8* %16, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%18 = load i8*, i8** %3
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %19, i32** %6
br label %20
20: ; preds = %15
%21 = load i32*, i32** %6
%22 = icmp ne i32* %21, null
br i1 %22, label %24, label %23
23: ; preds = %20
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i8** %1
br label %47
24: ; preds = %20
br label %25
25: ; preds = %24
%26 = load i32*, i32** %6
%27 = load i8*, i8** %2
%28 = call i32 @process_request(i32* %26, i8* %27)
%29 = load i32*, i32** %6
%30 = call i32 @fclose(i32* %29)
br label %31
31: ; preds = %25
%32 = load i8*, i8** %3
%33 = call i32 @is_in_file(i8* %32, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
%34 = icmp eq i32 %33, 1
br i1 %34, label %36, label %35
35: ; preds = %31
store i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0), i8** %1
br label %47
36: ; preds = %31
br label %37
37: ; preds = %36
%38 = load i32, i32* %4
%39 = icmp eq i32 %38, 1
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = load i8*, i8** %2
%42 = call i32 @rmdir(i8* %41)
br label %43
43: ; preds = %40, %37
%44 = load i8*, i8** %3
%45 = call i32 @remove(i8* %44)
%46 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0))
store i8* null, i8** %1
br label %47
47: ; preds = %43, %35, %23
%48 = load i8*, i8** %1
ret i8* %48
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str.1 = [14 x i8] c"test_root_dir\00"
@.str.2 = [21 x i8] c"test_server_list_dir\00"
@.str.3 = [15 x i8] c"GET / HTTP/1.1\00"
@.str.4 = [3 x i8] c"a+\00"
@.str.5 = [17 x i8] c"Error, f == NULL\00"
@.str.6 = [11 x i8] c"Index of /\00"
@.str.7 = [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_list_dir() {
%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 ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%8 = tail call i32* @fopen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %21, label %10
10: ; preds = %6
%11 = tail call i32 @process_request(i32* %8, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @fclose(i32* %8)
%13 = tail call i32 @is_in_file(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %21
15: ; preds = %10
br i1 %3, label %16, label %18
16: ; preds = %15
%17 = tail call i32 @rmdir(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %15
%19 = tail call i32 @remove(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%20 = tail call i32 @putchar(i32 10)
br label %21
21: ; preds = %10, %6, %18
%22 = phi i8* [ null, %18 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), %6 ], [ getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0), %10 ]
ret i8* %22
}