#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;
};
typedef int /*<<< orphan*/ pathbuf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ log_debug(char *, char *);
int /*<<< orphan*/ send_directory_listing(int /*<<< orphan*/ *, struct stat,
                                          char *, char *);
int /*<<< orphan*/ send_file(int /*<<< orphan*/ *, char *, struct stat *);
int /*<<< orphan*/ send_response(int /*<<< orphan*/ *, int, char *, char *,
                                 char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
scalar_t__ stat(char *, struct stat *);
int strlen(char *);
int process_get_request(FILE *f, char *path, char *relative_path) {
  struct stat statbuf;
  char pathbuf[4096];
  int len;
  if (stat(path, &statbuf) >= 0) {
    if (S_ISDIR(statbuf.st_mode)) {
      len = strlen(path);
      if (len == 0 || path[len - 1] != '/') {
        snprintf(pathbuf, sizeof(pathbuf), "Location: %s/", relative_path);
        log_debug("HTTP 302, %s", pathbuf);
        send_response(f, 302, "Found", pathbuf,
                      "Directories must end with a slash.");
      } else {
        snprintf(pathbuf, sizeof(pathbuf), "%sindex.html", path);
        if (stat(pathbuf, &statbuf) >= 0) {
          send_file(f, pathbuf, &statbuf);
        } else {
          send_directory_listing(f, statbuf, relative_path, path);
        }
      }
    } else {
      send_file(f, path, &statbuf);
    }
  } else {
    send_response(f, 404, "Not found", NULL, "File not found");
  }
  return 0;
}


x86 assembly (clang, -O0):
<process_get_request>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jl     12d3 <process_get_request+0x153>
mov    -0x20(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%rax
je     12bd <process_get_request+0x13d>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1024(%rbp)
cmpl   $0x0,-0x1024(%rbp)
je     11f9 <process_get_request+0x79>
mov    -0x10(%rbp),%rax
mov    -0x1024(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     1250 <process_get_request+0xd0>
lea    -0x1020(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1000,%esi
lea    0xdf0(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x1020(%rbp),%rsi
lea    0xdeb(%rip),%rdi
call   1070 <log_debug@plt>
mov    -0x8(%rbp),%rdi
lea    -0x1020(%rbp),%rcx
mov    $0x12e,%esi
lea    0xddc(%rip),%rdx
lea    0xddb(%rip),%r8
call   1030 <send_response@plt>
jmp    12b8 <process_get_request+0x138>
lea    -0x1020(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1000,%esi
lea    0xddd(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x1020(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jl     129f <process_get_request+0x11f>
mov    -0x8(%rbp),%rdi
lea    -0x1020(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1090 <send_file@plt>
jmp    12b3 <process_get_request+0x133>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x20(%rbp),%esi
call   10a0 <send_directory_listing@plt>
jmp    12b8 <process_get_request+0x138>
jmp    12ce <process_get_request+0x14e>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1090 <send_file@plt>
jmp    12f3 <process_get_request+0x173>
mov    -0x8(%rbp),%rdi
mov    $0x194,%esi
lea    0xd6e(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
lea    0xd6d(%rip),%r8
call   1030 <send_response@plt>
xor    %eax,%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<process_get_request>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1080 <stat@plt>
test   %rax,%rax
js     1211 <process_get_request+0x91>
mov    0x8(%rsp),%edi
call   1050 <S_ISDIR@plt>
test   %rax,%rax
je     1276 <process_get_request+0xf6>
mov    %rbx,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
je     122b <process_get_request+0xab>
cltq
cmpb   $0x2f,-0x1(%rax,%rbx,1)
jne    122b <process_get_request+0xab>
lea    0xe6d(%rip),%rdx
lea    0x10(%rsp),%r12
mov    %r12,%rdi
mov    $0x1000,%esi
mov    %rbx,%rcx
call   1060 <snprintf@plt>
lea    0x8(%rsp),%rsi
mov    %r12,%rdi
call   1080 <stat@plt>
test   %rax,%rax
js     1297 <process_get_request+0x117>
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    %r15,%rdi
jmp    1281 <process_get_request+0x101>
lea    0xe39(%rip),%rdx
lea    0xe3c(%rip),%r8
mov    %r15,%rdi
mov    $0x194,%esi
xor    %ecx,%ecx
jmp    126f <process_get_request+0xef>
lea    0xdce(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x1000,%esi
mov    %r14,%rcx
call   1060 <snprintf@plt>
lea    0xdc0(%rip),%rdi
mov    %rbx,%rsi
call   1070 <log_debug@plt>
lea    0xdbe(%rip),%rdx
lea    0xdbd(%rip),%r8
mov    %r15,%rdi
mov    $0x12e,%esi
mov    %rbx,%rcx
call   1030 <send_response@plt>
jmp    1286 <process_get_request+0x106>
lea    0x8(%rsp),%rdx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1090 <send_file@plt>
xor    %eax,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x8(%rsp),%esi
mov    %r15,%rdi
mov    %r14,%rdx
mov    %rbx,%rcx
call   10a0 <send_directory_listing@plt>
jmp    1286 <process_get_request+0x106>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [14 x i8] c"Location: %s/\00"
@.str.1 = [13 x i8] c"HTTP 302, %s\00"
@.str.2 = [6 x i8] c"Found\00"
@.str.3 = [35 x i8] c"Directories must end with a slash.\00"
@.str.4 = [13 x i8] c"%sindex.html\00"
@.str.5 = [10 x i8] c"Not found\00"
@.str.6 = [15 x i8] c"File not found\00"
define i32 @process_get_request(i32* %0, i8* %1, i8* %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca %struct.stat
%8 = alloca [4096 x i8]
%9 = alloca i32
store i32* %0, i32** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%10 = load i8*, i8** %5
%11 = call i64 @stat(i8* %10, %struct.stat* %7)
%12 = icmp sge i64 %11, 0
br i1 %12, label %13, label %66
13: ; preds = %3
%14 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%15 = load i32, i32* %14
%16 = call i64 @S_ISDIR(i32 %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %61
18: ; preds = %13
%19 = load i8*, i8** %5
%20 = call i32 @strlen(i8* %19)
store i32 %20, i32* %9
%21 = load i32, i32* %9
%22 = icmp eq i32 %21, 0
br i1 %22, label %32, label %23
23: ; preds = %18
%24 = load i8*, i8** %5
%25 = load i32, i32* %9
%26 = sub nsw i32 %25, 1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %24, i64 %27
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp ne i32 %30, 47
br i1 %31, label %32, label %41
32: ; preds = %23, %18
%33 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%34 = load i8*, i8** %6
%35 = call i32 @snprintf(i8* %33, i32 4096, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %34)
%36 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%37 = call i32 @log_debug(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %36)
%38 = load i32*, i32** %4
%39 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%40 = call i32 @send_response(i32* %38, i32 302, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %39, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
br label %60
41: ; preds = %23
%42 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%43 = load i8*, i8** %5
%44 = call i32 @snprintf(i8* %42, i32 4096, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %43)
%45 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%46 = call i64 @stat(i8* %45, %struct.stat* %7)
%47 = icmp sge i64 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %41
%49 = load i32*, i32** %4
%50 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%51 = call i32 @send_file(i32* %49, i8* %50, %struct.stat* %7)
br label %59
52: ; preds = %41
%53 = load i32*, i32** %4
%54 = load i8*, i8** %6
%55 = load i8*, i8** %5
%56 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%57 = load i32, i32* %56
%58 = call i32 @send_directory_listing(i32* %53, i32 %57, i8* %54, i8* %55)
br label %59
59: ; preds = %52, %48
br label %60
60: ; preds = %59, %32
br label %65
61: ; preds = %13
%62 = load i32*, i32** %4
%63 = load i8*, i8** %5
%64 = call i32 @send_file(i32* %62, i8* %63, %struct.stat* %7)
br label %65
65: ; preds = %61, %60
br label %69
66: ; preds = %3
%67 = load i32*, i32** %4
%68 = call i32 @send_response(i32* %67, i32 404, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* null, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
br label %69
69: ; preds = %66, %65
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [14 x i8] c"Location: %s/\00"
@.str.1 = [13 x i8] c"HTTP 302, %s\00"
@.str.2 = [6 x i8] c"Found\00"
@.str.3 = [35 x i8] c"Directories must end with a slash.\00"
@.str.4 = [13 x i8] c"%sindex.html\00"
@.str.5 = [10 x i8] c"Not found\00"
@.str.6 = [15 x i8] c"File not found\00"
define i32 @process_get_request(i32* %0, i8* %1, i8* %2) {
%4 = alloca %struct.stat
%5 = alloca [4096 x i8]
%6 = bitcast %struct.stat* %4 to i8*
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%8 = call i64 @stat(i8* %1, %struct.stat* %4)
%9 = icmp sgt i64 %8, -1
br i1 %9, label %10, label %39
10: ; preds = %3
%11 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%12 = load i32, i32* %11
%13 = call i64 @S_ISDIR(i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %37, label %15
15: ; preds = %10
%16 = call i32 @strlen(i8* %1)
%17 = icmp eq i32 %16, 0
br i1 %17, label %24, label %18
18: ; preds = %15
%19 = add nsw i32 %16, -1
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %1, i64 %20
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 47
br i1 %23, label %28, label %24
24: ; preds = %18, %15
%25 = call i32 @snprintf(i8* %7, i32 4096, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %2)
%26 = call i32 @log_debug(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %7)
%27 = call i32 @send_response(i32* %0, i32 302, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %7, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
br label %41
28: ; preds = %18
%29 = call i32 @snprintf(i8* %7, i32 4096, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %1)
%30 = call i64 @stat(i8* %7, %struct.stat* %4)
%31 = icmp sgt i64 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @send_file(i32* %0, i8* %7, %struct.stat* %4)
br label %41
34: ; preds = %28
%35 = load i32, i32* %11
%36 = call i32 @send_directory_listing(i32* %0, i32 %35, i8* %2, i8* %1)
br label %41
37: ; preds = %10
%38 = call i32 @send_file(i32* %0, i8* %1, %struct.stat* %4)
br label %41
39: ; preds = %3
%40 = call i32 @send_response(i32* %0, i32 404, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* null, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
br label %41
41: ; preds = %37, %32, %34, %24, %39
ret i32 0
}