#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 st_size;
  int /*<<< orphan*/ st_mtime;
  int /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ data;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/);
char *get_mime_type(char *);
int /*<<< orphan*/ send_error(int, char *, int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ send_headers(int, char *, int /*<<< orphan*/ *, char *, int,
                                int /*<<< orphan*/, int);
int /*<<< orphan*/ stdout;
void send_file(char *path, struct stat *statbuf, char *vpath, char *querystring,
               int ispost) {
  char data[4096];
  int n;
  char *mime_type = get_mime_type(path);
  FILE *file = fopen(path, "r");
  if (!file) {
    send_error(403, "Forbidden", NULL, "Access denied.");
    return;
  }
  int length = S_ISREG(statbuf->st_mode) ? statbuf->st_size : -1;
  send_headers(200, "OK", NULL, mime_type, length, statbuf->st_mtime, 1);
  while ((n = fread(data, 1, sizeof(data), file)) > 0)
    fwrite(data, 1, n, stdout);
  fclose(file);
}


x86 assembly (clang, -O0):
<send_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1060,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <get_mime_type@plt>
mov    %rax,-0x1040(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe46(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x1048(%rbp)
cmpq   $0x0,-0x1048(%rbp)
jne    11f5 <send_file+0x75>
mov    $0x193,%edi
lea    0xe22(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
lea    0xe21(%rip),%rcx
call   1080 <send_error@plt>
jmp    12c7 <send_file+0x147>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edi
call   1070 <S_ISREG@plt>
cmp    $0x0,%rax
je     121c <send_file+0x9c>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1050(%rbp)
jmp    122c <send_file+0xac>
mov    $0xffffffff,%eax
mov    %eax,-0x1050(%rbp)
jmp    122c <send_file+0xac>
mov    -0x1050(%rbp),%eax
mov    %eax,-0x104c(%rbp)
mov    -0x1040(%rbp),%rcx
mov    -0x104c(%rbp),%r8d
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%r9d
mov    $0xc8,%edi
lea    0xdc1(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
movl   $0x1,(%rsp)
call   1050 <send_headers@plt>
lea    -0x1030(%rbp),%rdi
mov    -0x1048(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1030 <fread@plt>
mov    %eax,-0x1034(%rbp)
cmp    $0x0,%eax
jle    12bb <send_file+0x13b>
lea    -0x1030(%rbp),%rdi
mov    -0x1034(%rbp),%edx
mov    0x2d36(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   10a0 <fwrite@plt>
jmp    126a <send_file+0xea>
mov    -0x1048(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x1060,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %rsi,%r15
mov    %rdi,%rbx
call   1060 <get_mime_type@plt>
mov    %rax,%r14
lea    0xe5d(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     11c6 <send_file+0x46>
mov    %rax,%r12
mov    0x8(%r15),%edi
call   1070 <S_ISREG@plt>
test   %rax,%rax
je     11ee <send_file+0x6e>
mov    (%r15),%r8d
jmp    11f4 <send_file+0x74>
lea    0xe35(%rip),%rsi
lea    0xe38(%rip),%rcx
mov    $0x193,%edi
xor    %edx,%edx
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1080 <send_error@plt>
mov    $0xffffffff,%r8d
mov    0x4(%r15),%r9d
movl   $0x1,(%rsp)
lea    0xe15(%rip),%rsi
mov    $0xc8,%edi
xor    %edx,%edx
mov    %r14,%rcx
call   1050 <send_headers@plt>
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jle    126b <send_file+0xeb>
mov    0x2da9(%rip),%r14
lea    0x10(%rsp),%rbx
nopl   0x0(%rax)
mov    (%r14),%ecx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %eax,%edx
call   10a0 <fwrite@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jg     1240 <send_file+0xc0>
mov    %r12,%rdi
call   1040 <fclose@plt>
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32, i32 }
@.str = [2 x i8] c"r\00"
@.str.1 = [10 x i8] c"Forbidden\00"
@.str.2 = [15 x i8] c"Access denied.\00"
@.str.3 = [3 x i8] c"OK\00"
@stdout = global i32 0
define void @send_file(i8* %0, %struct.stat* %1, i8* %2, i8* %3, i32 %4) {
%6 = alloca i8*
%7 = alloca %struct.stat*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca [4096 x i8]
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i32*
%15 = alloca i32
store i8* %0, i8** %6
store %struct.stat* %1, %struct.stat** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i32 %4, i32* %10
%16 = load i8*, i8** %6
%17 = call i8* @get_mime_type(i8* %16)
store i8* %17, i8** %13
%18 = load i8*, i8** %6
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %19, i32** %14
%20 = load i32*, i32** %14
%21 = icmp ne i32* %20, null
br i1 %21, label %24, label %22
22: ; preds = %5
%23 = call i32 @send_error(i32 403, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* null, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %56
24: ; preds = %5
%25 = load %struct.stat*, %struct.stat** %7
%26 = getelementptr inbounds %struct.stat, %struct.stat* %25, i32 0, i32 2
%27 = load i32, i32* %26
%28 = call i64 @S_ISREG(i32 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %24
%31 = load %struct.stat*, %struct.stat** %7
%32 = getelementptr inbounds %struct.stat, %struct.stat* %31, i32 0, i32 0
%33 = load i32, i32* %32
br label %35
34: ; preds = %24
br label %35
35: ; preds = %34, %30
%36 = phi i32 [ %33, %30 ], [ -1, %34 ]
store i32 %36, i32* %15
%37 = load i8*, i8** %13
%38 = load i32, i32* %15
%39 = load %struct.stat*, %struct.stat** %7
%40 = getelementptr inbounds %struct.stat, %struct.stat* %39, i32 0, i32 1
%41 = load i32, i32* %40
%42 = call i32 @send_headers(i32 200, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* null, i8* %37, i32 %38, i32 %41, i32 1)
br label %43
43: ; preds = %48, %35
%44 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%45 = load i32*, i32** %14
%46 = call i32 @fread(i8* %44, i32 1, i32 4096, i32* %45)
store i32 %46, i32* %12
%47 = icmp sgt i32 %46, 0
br i1 %47, label %48, label %53
48: ; preds = %43
%49 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%50 = load i32, i32* %12
%51 = load i32, i32* @stdout
%52 = call i32 @fwrite(i8* %49, i32 1, i32 %50, i32 %51)
br label %43
53: ; preds = %43
%54 = load i32*, i32** %14
%55 = call i32 @fclose(i32* %54)
br label %56
56: ; preds = %53, %22
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32, i32, i32 }
@.str = [2 x i8] c"r\00"
@.str.1 = [10 x i8] c"Forbidden\00"
@.str.2 = [15 x i8] c"Access denied.\00"
@.str.3 = [3 x i8] c"OK\00"
@stdout = global i32 0
define void @send_file(i8* %0, %struct.stat* nocapture %1, i8* nocapture readnone %2, i8* nocapture readnone %3, i32 %4) {
%6 = alloca [4096 x i8]
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%8 = tail call i8* @get_mime_type(i8* %0)
%9 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %5
%12 = tail call i32 @send_error(i32 403, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* null, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %36
13: ; preds = %5
%14 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 2
%15 = load i32, i32* %14
%16 = tail call i64 @S_ISREG(i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %13
%19 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%20 = load i32, i32* %19
br label %21
21: ; preds = %13, %18
%22 = phi i32 [ %20, %18 ], [ -1, %13 ]
%23 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 1
%24 = load i32, i32* %23
%25 = tail call i32 @send_headers(i32 200, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* null, i8* %8, i32 %22, i32 %24, i32 1)
%26 = call i32 @fread(i8* %7, i32 1, i32 4096, i32* %9)
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %21, %28
%29 = phi i32 [ %32, %28 ], [ %26, %21 ]
%30 = load i32, i32* @stdout
%31 = call i32 @fwrite(i8* %7, i32 1, i32 %29, i32 %30)
%32 = call i32 @fread(i8* %7, i32 1, i32 4096, i32* %9)
%33 = icmp sgt i32 %32, 0
br i1 %33, label %28, label %34
34: ; preds = %28, %21
%35 = call i32 @fclose(i32* %9)
br label %36
36: ; preds = %34, %11
ret void
}