#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, int);
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 *);
int 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);
    if (stat(name, &st) == 0 && S_ISREG(st.st_mode)) {
      dir_join(name, getenv("SCRIPT_NAME"), indices[i], 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    $0x440,%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     127e <http_serve_directory+0x10e>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x41c(%rbp),%rcx
lea    0x2c42(%rip),%rax
mov    (%rax,%rcx,8),%rdx
mov    $0x400,%ecx
call   1080 <dir_join@plt>
lea    -0x410(%rbp),%rdi
lea    -0x418(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jne    1265 <http_serve_directory+0xf5>
mov    -0x418(%rbp),%edi
call   1060 <S_ISREG@plt>
cmp    $0x0,%rax
je     1265 <http_serve_directory+0xf5>
lea    -0x410(%rbp),%rax
mov    %rax,-0x438(%rbp)
lea    0xe0c(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x430(%rbp)
movslq -0x41c(%rbp),%rcx
lea    0x2bd3(%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    %eax,%ecx
call   1080 <dir_join@plt>
jmp    127e <http_serve_directory+0x10e>
jmp    126a <http_serve_directory+0xfa>
mov    -0x41c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x41c(%rbp)
jmp    118c <http_serve_directory+0x1c>
movslq -0x41c(%rbp),%rcx
lea    0x2b74(%rip),%rax
cmpq   $0x0,(%rax,%rcx,8)
jne    12b4 <http_serve_directory+0x144>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rcx
mov    $0x193,%esi
lea    0xd81(%rip),%rdx
call   1090 <http_err@plt>
jmp    12c3 <http_serve_directory+0x153>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
call   1070 <http_serve@plt>
add    $0x440,%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    0xe76(%rip),%rbx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    %rbx,%rdx
mov    $0x400,%ecx
call   1080 <dir_join@plt>
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   1050 <stat@plt>
test   %rax,%rax
je     123e <http_serve_directory+0xce>
lea    0xe4f(%rip),%rbx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
mov    $0x400,%ecx
call   1080 <dir_join@plt>
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   1050 <stat@plt>
test   %rax,%rax
je     1251 <http_serve_directory+0xe1>
lea    0xe28(%rip),%rbx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
mov    $0x400,%ecx
call   1080 <dir_join@plt>
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   1050 <stat@plt>
test   %rax,%rax
jne    1225 <http_serve_directory+0xb5>
mov    0x8(%rsp),%edi
call   1060 <S_ISREG@plt>
test   %rax,%rax
jne    125f <http_serve_directory+0xef>
lea    0xdff(%rip),%rdx
mov    %r14d,%edi
mov    $0x193,%esi
mov    %r15,%rcx
call   1090 <http_err@plt>
jmp    1296 <http_serve_directory+0x126>
mov    0x8(%rsp),%edi
call   1060 <S_ISREG@plt>
test   %rax,%rax
jne    125f <http_serve_directory+0xef>
jmp    11b5 <http_serve_directory+0x45>
mov    0x8(%rsp),%edi
call   1060 <S_ISREG@plt>
test   %rax,%rax
je     11e6 <http_serve_directory+0x76>
lea    0xdb9(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbp
lea    0x10(%rsp),%r15
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %r15,%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
mov    %eax,%ecx
call   1080 <dir_join@plt>
mov    %r14d,%edi
mov    %r15,%rsi
call   1070 <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 = %41, %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 %44
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, i32 1024)
%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 %40
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 %40
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 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%38 = call i32 @strlen(i8* %37)
%39 = call i32 @dir_join(i8* %31, i8* %32, i8* %36, i32 %38)
br label %44
40: ; preds = %25, %14
br label %41
41: ; preds = %40
%42 = load i32, i32* %7
%43 = add nsw i32 %42, 1
store i32 %43, i32* %7
br label %8
44: ; preds = %30, %8
%45 = load i32, i32* %7
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [4 x i8*], [4 x i8*]* @http_serve_directory.indices, i64 0, i64 %46
%48 = load i8*, i8** %47
%49 = icmp eq i8* %48, null
br i1 %49, label %50, label %54
50: ; preds = %44
%51 = load i32, i32* %3
%52 = load i8*, i8** %4
%53 = call i32 @http_err(i32 %51, i32 403, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %52)
br label %58
54: ; preds = %44
%55 = load i32, i32* %3
%56 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%57 = call i32 @http_serve(i32 %55, i8* %56)
br label %58
58: ; preds = %54, %50
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), i32 1024)
%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), i32 1024)
%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), i32 1024)
%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 %39
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 @strlen(i8* %5)
%37 = call i32 @dir_join(i8* %5, i8* %35, i8* %34, i32 %36)
%38 = call i32 @http_serve(i32 %0, i8* %5)
br label %39
39: ; preds = %33, %31
ret void
}