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

/* Variables and functions */
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ dir_join(char *, char const *, char const *const,
                            scalar_t__);
char const *getenv(char *);
int /*<<< orphan*/ http_err(int, int, char *, char const *);
int /*<<< orphan*/ http_serve(int, char *);
scalar_t__ stat(char *, struct stat *);
scalar_t__ strlen(char *);
void http_serve_directory(int fd, const char *pn) {
  static const char *const indices[] = {"index.html", "index.php", "index.cgi",
                                        NULL};
  char name[1024];
  struct stat st;
  int i;
  for (i = 0; indices[i]; i++) {
    dir_join(name, pn, indices[i], 1024 - strlen(name));
    if (stat(name, &st) == 0 && S_ISREG(st.st_mode)) {
      dir_join(name, getenv("SCRIPT_NAME"), indices[i], 1024 - strlen(name));
      break;
    }
  }
  if (indices[i] == NULL) {
    http_err(fd, 403, "No index file in %s", pn);
    return;
  }
  http_serve(fd, name);
}


x86 assembly (clang, -O0):
<http_serve_directory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x41c(%rbp)
movslq -0x41c(%rbp),%rcx
lea    0x2c66(%rip),%rax
cmpq   $0x0,(%rax,%rcx,8)
je     12bd <http_serve_directory+0x14d>
lea    -0x410(%rbp),%rax
mov    %rax,-0x438(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x430(%rbp)
movslq -0x41c(%rbp),%rcx
lea    0x2c34(%rip),%rax
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x428(%rbp)
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x438(%rbp),%rdi
mov    -0x430(%rbp),%rsi
mov    -0x428(%rbp),%rdx
mov    $0x400,%ecx
sub    %rax,%rcx
call   1080 <dir_join@plt>
lea    -0x410(%rbp),%rdi
lea    -0x418(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jne    12a4 <http_serve_directory+0x134>
mov    -0x418(%rbp),%edi
call   1060 <S_ISREG@plt>
cmp    $0x0,%rax
je     12a4 <http_serve_directory+0x134>
lea    -0x410(%rbp),%rax
mov    %rax,-0x450(%rbp)
lea    0xdd3(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x448(%rbp)
movslq -0x41c(%rbp),%rcx
lea    0x2b9a(%rip),%rax
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x440(%rbp)
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x450(%rbp),%rdi
mov    -0x448(%rbp),%rsi
mov    -0x440(%rbp),%rdx
mov    $0x400,%ecx
sub    %rax,%rcx
call   1080 <dir_join@plt>
jmp    12bd <http_serve_directory+0x14d>
jmp    12a9 <http_serve_directory+0x139>
mov    -0x41c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x41c(%rbp)
jmp    118c <http_serve_directory+0x1c>
movslq -0x41c(%rbp),%rcx
lea    0x2b35(%rip),%rax
cmpq   $0x0,(%rax,%rcx,8)
jne    12f3 <http_serve_directory+0x183>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rcx
mov    $0x193,%esi
lea    0xd42(%rip),%rdx
call   1090 <http_err@plt>
jmp    1302 <http_serve_directory+0x192>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
call   1070 <http_serve@plt>
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<http_serve_directory>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x410,%rsp
mov    %rsi,%r15
mov    %edi,%r14d
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    $0x400,%ebx
mov    $0x400,%ecx
sub    %rax,%rcx
lea    0xe5a(%rip),%r12
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1080 <dir_join@plt>
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   1050 <stat@plt>
test   %rax,%rax
je     1267 <http_serve_directory+0xf7>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
sub    %rax,%rbx
lea    0xe2a(%rip),%r12
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %rbx,%rcx
call   1080 <dir_join@plt>
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   1050 <stat@plt>
test   %rax,%rax
je     127a <http_serve_directory+0x10a>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    $0x400,%ecx
sub    %rax,%rcx
lea    0xdf5(%rip),%r12
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1080 <dir_join@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <stat@plt>
test   %rax,%rax
jne    124e <http_serve_directory+0xde>
mov    0x8(%rsp),%edi
call   1060 <S_ISREG@plt>
test   %rax,%rax
jne    128c <http_serve_directory+0x11c>
lea    0xdd6(%rip),%rdx
mov    %r14d,%edi
mov    $0x193,%esi
mov    %r15,%rcx
call   1090 <http_err@plt>
jmp    12c9 <http_serve_directory+0x159>
mov    0x8(%rsp),%edi
call   1060 <S_ISREG@plt>
test   %rax,%rax
jne    128c <http_serve_directory+0x11c>
jmp    11ca <http_serve_directory+0x5a>
mov    0x8(%rsp),%edi
call   1060 <S_ISREG@plt>
test   %rax,%rax
je     1204 <http_serve_directory+0x94>
lea    0xd8c(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    $0x400,%ecx
sub    %rax,%rcx
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1080 <dir_join@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
call   1070 <http_serve@plt>
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@http_serve_directory.indices = internal constant [4 x i8*] [i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i32 0, i32 0), i8* null]
@.str = [11 x i8] c"index.html\00"
@.str.1 = [10 x i8] c"index.php\00"
@.str.2 = [10 x i8] c"index.cgi\00"
@.str.3 = [12 x i8] c"SCRIPT_NAME\00"
@.str.4 = [20 x i8] c"No index file in %s\00"
define void @http_serve_directory(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca [1024 x i8]
%6 = alloca %struct.stat
%7 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
store i32 0, i32* %7
br label %8
8: ; preds = %45, %2
%9 = load i32, i32* %7
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds [4 x i8*], [4 x i8*]* @http_serve_directory.indices, i64 0, i64 %10
%12 = load i8*, i8** %11
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %48
14: ; preds = %8
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%16 = load i8*, i8** %4
%17 = load i32, i32* %7
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [4 x i8*], [4 x i8*]* @http_serve_directory.indices, i64 0, i64 %18
%20 = load i8*, i8** %19
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%22 = call i64 @strlen(i8* %21)
%23 = sub nsw i64 1024, %22
%24 = call i32 @dir_join(i8* %15, i8* %16, i8* %20, i64 %23)
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%26 = call i64 @stat(i8* %25, %struct.stat* %6)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %44
28: ; preds = %14
%29 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i64 @S_ISREG(i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %44
33: ; preds = %28
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%35 = call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%36 = load i32, i32* %7
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [4 x i8*], [4 x i8*]* @http_serve_directory.indices, i64 0, i64 %37
%39 = load i8*, i8** %38
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%41 = call i64 @strlen(i8* %40)
%42 = sub nsw i64 1024, %41
%43 = call i32 @dir_join(i8* %34, i8* %35, i8* %39, i64 %42)
br label %48
44: ; preds = %28, %14
br label %45
45: ; preds = %44
%46 = load i32, i32* %7
%47 = add nsw i32 %46, 1
store i32 %47, i32* %7
br label %8
48: ; preds = %33, %8
%49 = load i32, i32* %7
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [4 x i8*], [4 x i8*]* @http_serve_directory.indices, i64 0, i64 %50
%52 = load i8*, i8** %51
%53 = icmp eq i8* %52, null
br i1 %53, label %54, label %58
54: ; preds = %48
%55 = load i32, i32* %3
%56 = load i8*, i8** %4
%57 = call i32 @http_err(i32 %55, i32 403, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %56)
br label %62
58: ; preds = %48
%59 = load i32, i32* %3
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%61 = call i32 @http_serve(i32 %59, i8* %60)
br label %62
62: ; preds = %58, %54
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [11 x i8] c"index.html\00"
@.str.1 = [10 x i8] c"index.php\00"
@.str.2 = [10 x i8] c"index.cgi\00"
@.str.3 = [12 x i8] c"SCRIPT_NAME\00"
@.str.4 = [20 x i8] c"No index file in %s\00"
define void @http_serve_directory(i32 %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.stat* %4 to i8*
%7 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%8 = call i64 @strlen(i8* %5)
%9 = sub nsw i64 1024, %8
%10 = call i32 @dir_join(i8* %5, i8* %1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i64 %9)
%11 = call i64 @stat(i8* %5, %struct.stat* %4)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i32, i32* %7
%15 = call i64 @S_ISREG(i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %39
17: ; preds = %2, %13
%18 = call i64 @strlen(i8* %5)
%19 = sub nsw i64 1024, %18
%20 = call i32 @dir_join(i8* %5, i8* %1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i64 %19)
%21 = call i64 @stat(i8* %5, %struct.stat* %4)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %17
%24 = load i32, i32* %7
%25 = call i64 @S_ISREG(i32 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %39
27: ; preds = %23, %17
%28 = call i64 @strlen(i8* %5)
%29 = sub nsw i64 1024, %28
%30 = call i32 @dir_join(i8* %5, i8* %1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i64 %29)
%31 = call i64 @stat(i8* %5, %struct.stat* %4)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %27
%34 = load i32, i32* %7
%35 = call i64 @S_ISREG(i32 %34)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %33, %27
%38 = call i32 @http_err(i32 %0, i32 403, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %1)
br label %46
39: ; preds = %33, %23, %13
%40 = phi i8* [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), %13 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %23 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), %33 ]
%41 = call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%42 = call i64 @strlen(i8* %5)
%43 = sub nsw i64 1024, %42
%44 = call i32 @dir_join(i8* %5, i8* %41, i8* %40, i64 %43)
%45 = call i32 @http_serve(i32 %0, i8* %5)
br label %46
46: ; preds = %39, %37
ret void
}