#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);
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 *);
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]);
    if (stat(name, &st) == 0 && S_ISREG(st.st_mode)) {
      dir_join(name, getenv("SCRIPT_NAME"), indices[i]);
      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    $0x430,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x41c(%rbp)
movslq -0x41c(%rbp),%rcx
lea    0x2c76(%rip),%rax
cmpq   $0x0,(%rax,%rcx,8)
je     1242 <http_serve_directory+0xe2>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x41c(%rbp),%rcx
lea    0x2c52(%rip),%rax
mov    (%rax,%rcx,8),%rdx
call   1070 <dir_join@plt>
lea    -0x410(%rbp),%rdi
lea    -0x418(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jne    1229 <http_serve_directory+0xc9>
mov    -0x418(%rbp),%edi
call   1050 <S_ISREG@plt>
cmp    $0x0,%rax
je     1229 <http_serve_directory+0xc9>
lea    -0x410(%rbp),%rax
mov    %rax,-0x428(%rbp)
lea    0xe21(%rip),%rdi
call   1030 <getenv@plt>
mov    -0x428(%rbp),%rdi
mov    %rax,%rsi
movslq -0x41c(%rbp),%rcx
lea    0x2be5(%rip),%rax
mov    (%rax,%rcx,8),%rdx
call   1070 <dir_join@plt>
jmp    1242 <http_serve_directory+0xe2>
jmp    122e <http_serve_directory+0xce>
mov    -0x41c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x41c(%rbp)
jmp    117c <http_serve_directory+0x1c>
movslq -0x41c(%rbp),%rcx
lea    0x2bb0(%rip),%rax
cmpq   $0x0,(%rax,%rcx,8)
jne    1278 <http_serve_directory+0x118>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rcx
mov    $0x193,%esi
lea    0xdbd(%rip),%rdx
call   1080 <http_err@plt>
jmp    1287 <http_serve_directory+0x127>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
call   1060 <http_serve@plt>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<http_serve_directory>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x418,%rsp
mov    %rsi,%r15
mov    %edi,%r14d
lea    0xe86(%rip),%rbp
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %rbp,%rdx
call   1070 <dir_join@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
test   %rax,%rax
je     121b <http_serve_directory+0xbb>
lea    0xe68(%rip),%rbp
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %rbp,%rdx
call   1070 <dir_join@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
test   %rax,%rax
je     122e <http_serve_directory+0xce>
lea    0xe46(%rip),%rbp
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %rbp,%rdx
call   1070 <dir_join@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
test   %rax,%rax
jne    1202 <http_serve_directory+0xa2>
mov    0x8(%rsp),%edi
call   1050 <S_ISREG@plt>
test   %rax,%rax
jne    123c <http_serve_directory+0xdc>
lea    0xe22(%rip),%rdx
mov    %r14d,%edi
mov    $0x193,%esi
mov    %r15,%rcx
call   1080 <http_err@plt>
jmp    1266 <http_serve_directory+0x106>
mov    0x8(%rsp),%edi
call   1050 <S_ISREG@plt>
test   %rax,%rax
jne    123c <http_serve_directory+0xdc>
jmp    119c <http_serve_directory+0x3c>
mov    0x8(%rsp),%edi
call   1050 <S_ISREG@plt>
test   %rax,%rax
je     11c8 <http_serve_directory+0x68>
lea    0xddc(%rip),%rdi
call   1030 <getenv@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %rax,%rsi
mov    %rbp,%rdx
call   1070 <dir_join@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1060 <http_serve@plt>
add    $0x418,%rsp
pop    %rbx
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 = %39, %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 %42
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 = call i32 @dir_join(i8* %15, i8* %16, i8* %20)
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%23 = call i64 @stat(i8* %22, %struct.stat* %6)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %38
25: ; preds = %14
%26 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i64 @S_ISREG(i32 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %38
30: ; preds = %25
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%32 = call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%33 = load i32, i32* %7
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [4 x i8*], [4 x i8*]* @http_serve_directory.indices, i64 0, i64 %34
%36 = load i8*, i8** %35
%37 = call i32 @dir_join(i8* %31, i8* %32, i8* %36)
br label %42
38: ; preds = %25, %14
br label %39
39: ; preds = %38
%40 = load i32, i32* %7
%41 = add nsw i32 %40, 1
store i32 %41, i32* %7
br label %8
42: ; preds = %30, %8
%43 = load i32, i32* %7
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [4 x i8*], [4 x i8*]* @http_serve_directory.indices, i64 0, i64 %44
%46 = load i8*, i8** %45
%47 = icmp eq i8* %46, null
br i1 %47, label %48, label %52
48: ; preds = %42
%49 = load i32, i32* %3
%50 = load i8*, i8** %4
%51 = call i32 @http_err(i32 %49, i32 403, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %50)
br label %56
52: ; preds = %42
%53 = load i32, i32* %3
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%55 = call i32 @http_serve(i32 %53, i8* %54)
br label %56
56: ; preds = %52, %48
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 i32 @dir_join(i8* %5, i8* %1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%9 = call i64 @stat(i8* %5, %struct.stat* %4)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i32, i32* %7
%13 = call i64 @S_ISREG(i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %33
15: ; preds = %2, %11
%16 = call i32 @dir_join(i8* %5, i8* %1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%17 = call i64 @stat(i8* %5, %struct.stat* %4)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = load i32, i32* %7
%21 = call i64 @S_ISREG(i32 %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %33
23: ; preds = %19, %15
%24 = call i32 @dir_join(i8* %5, i8* %1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%25 = call i64 @stat(i8* %5, %struct.stat* %4)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = load i32, i32* %7
%29 = call i64 @S_ISREG(i32 %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %27, %23
%32 = 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 %38
33: ; preds = %27, %19, %11
%34 = phi i8* [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), %11 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %19 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), %27 ]
%35 = call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%36 = call i32 @dir_join(i8* %5, i8* %35, i8* %34)
%37 = call i32 @http_serve(i32 %0, i8* %5)
br label %38
38: ; preds = %33, %31
ret void
}