#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*/ *);
int /*<<< orphan*/ get_mimetype_by_name(char *);
int /*<<< orphan*/ send_header(int /*<<< orphan*/ *, int, char *,
                               int /*<<< orphan*/ *, int /*<<< orphan*/, int,
                               int /*<<< orphan*/);
int /*<<< orphan*/ send_response(int /*<<< orphan*/ *, int, char *,
                                 int /*<<< orphan*/ *, char *);
void send_file(FILE *f, char *path, struct stat *statbuf) {
  char data[1024];
  int n;
  FILE *file = fopen(path, "r");
  if (!file)
    send_response(f, 403, "Forbidden", NULL, "Access denied.");
  else {
    int length = S_ISREG(statbuf->st_mode) ? statbuf->st_size : -1;
    send_header(f, 200, "OK", NULL, get_mimetype_by_name(path), length,
                statbuf->st_mtime);
    while ((n = fread(data, 1, sizeof(data), file)) > 0)
      fwrite(data, 1, n, f);
    fclose(file);
  }
}


x86 assembly (clang, -O0):
<send_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe5e(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x430(%rbp)
cmpq   $0x0,-0x430(%rbp)
jne    11e1 <send_file+0x61>
mov    -0x8(%rbp),%rdi
mov    $0x193,%esi
lea    0xe36(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
lea    0xe35(%rip),%r8
call   1040 <send_response@plt>
jmp    12be <send_file+0x13e>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%edi
call   1070 <S_ISREG@plt>
cmp    $0x0,%rax
je     1208 <send_file+0x88>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x438(%rbp)
jmp    1218 <send_file+0x98>
mov    $0xffffffff,%eax
mov    %eax,-0x438(%rbp)
jmp    1218 <send_file+0x98>
mov    -0x438(%rbp),%eax
mov    %eax,-0x434(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x440(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <get_mimetype_by_name@plt>
mov    -0x440(%rbp),%rdi
mov    %eax,%r8d
mov    -0x434(%rbp),%r9d
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%eax
mov    $0xc8,%esi
lea    0xdbf(%rip),%rdx
xor    %ecx,%ecx
mov    %eax,(%rsp)
call   1060 <send_header@plt>
lea    -0x420(%rbp),%rdi
mov    -0x430(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1030 <fread@plt>
mov    %eax,-0x424(%rbp)
cmp    $0x0,%eax
jle    12b2 <send_file+0x132>
lea    -0x420(%rbp),%rdi
mov    -0x424(%rbp),%edx
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
call   10a0 <fwrite@plt>
jmp    1266 <send_file+0xe6>
mov    -0x430(%rbp),%rdi
call   1050 <fclose@plt>
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_file>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x410,%rsp
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
lea    0xe60(%rip),%rsi
mov    %r12,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     11c3 <send_file+0x43>
mov    %rax,%r13
mov    0x8(%r15),%edi
call   1070 <S_ISREG@plt>
test   %rax,%rax
je     11f0 <send_file+0x70>
mov    (%r15),%ebx
jmp    11f5 <send_file+0x75>
lea    0xe38(%rip),%rdx
lea    0xe3b(%rip),%r8
mov    %r14,%rdi
mov    $0x193,%esi
xor    %ecx,%ecx
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1040 <send_response@plt>
mov    $0xffffffff,%ebx
mov    %r12,%rdi
call   1080 <get_mimetype_by_name@plt>
mov    0x4(%r15),%ecx
mov    %ecx,(%rsp)
lea    0xe10(%rip),%rdx
mov    %r14,%rdi
mov    $0xc8,%esi
xor    %ecx,%ecx
mov    %eax,%r8d
mov    %ebx,%r9d
call   1060 <send_header@plt>
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r13,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jle    126b <send_file+0xeb>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %r14,%rcx
call   10a0 <fwrite@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r13,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jg     1240 <send_file+0xc0>
mov    %r13,%rdi
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1050 <fclose@plt>

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"
define void @send_file(i32* %0, i8* %1, %struct.stat* %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca %struct.stat*
%7 = alloca [1024 x i8]
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
store i32* %0, i32** %4
store i8* %1, i8** %5
store %struct.stat* %2, %struct.stat** %6
%11 = load i8*, i8** %5
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %9
%13 = load i32*, i32** %9
%14 = icmp ne i32* %13, null
br i1 %14, label %18, label %15
15: ; preds = %3
%16 = load i32*, i32** %4
%17 = call i32 @send_response(i32* %16, 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 %52
18: ; preds = %3
%19 = load %struct.stat*, %struct.stat** %6
%20 = getelementptr inbounds %struct.stat, %struct.stat* %19, i32 0, i32 2
%21 = load i32, i32* %20
%22 = call i64 @S_ISREG(i32 %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %18
%25 = load %struct.stat*, %struct.stat** %6
%26 = getelementptr inbounds %struct.stat, %struct.stat* %25, i32 0, i32 0
%27 = load i32, i32* %26
br label %29
28: ; preds = %18
br label %29
29: ; preds = %28, %24
%30 = phi i32 [ %27, %24 ], [ -1, %28 ]
store i32 %30, i32* %10
%31 = load i32*, i32** %4
%32 = load i8*, i8** %5
%33 = call i32 @get_mimetype_by_name(i8* %32)
%34 = load i32, i32* %10
%35 = load %struct.stat*, %struct.stat** %6
%36 = getelementptr inbounds %struct.stat, %struct.stat* %35, i32 0, i32 1
%37 = load i32, i32* %36
%38 = call i32 @send_header(i32* %31, i32 200, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* null, i32 %33, i32 %34, i32 %37)
br label %39
39: ; preds = %44, %29
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%41 = load i32*, i32** %9
%42 = call i32 @fread(i8* %40, i32 1, i32 1024, i32* %41)
store i32 %42, i32* %8
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %39
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%46 = load i32, i32* %8
%47 = load i32*, i32** %4
%48 = call i32 @fwrite(i8* %45, i32 1, i32 %46, i32* %47)
br label %39
49: ; preds = %39
%50 = load i32*, i32** %9
%51 = call i32 @fclose(i32* %50)
br label %52
52: ; preds = %49, %15
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"
define void @send_file(i32* %0, i8* %1, %struct.stat* nocapture %2) {
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%6 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 @send_response(i32* %0, 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 %33
10: ; preds = %3
%11 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 2
%12 = load i32, i32* %11
%13 = tail call i64 @S_ISREG(i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %10
%16 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%17 = load i32, i32* %16
br label %18
18: ; preds = %10, %15
%19 = phi i32 [ %17, %15 ], [ -1, %10 ]
%20 = tail call i32 @get_mimetype_by_name(i8* %1)
%21 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%22 = load i32, i32* %21
%23 = tail call i32 @send_header(i32* %0, i32 200, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* null, i32 %20, i32 %19, i32 %22)
%24 = call i32 @fread(i8* %5, i32 1, i32 1024, i32* %6)
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %18, %26
%27 = phi i32 [ %29, %26 ], [ %24, %18 ]
%28 = call i32 @fwrite(i8* %5, i32 1, i32 %27, i32* %0)
%29 = call i32 @fread(i8* %5, i32 1, i32 1024, i32* %6)
%30 = icmp sgt i32 %29, 0
br i1 %30, label %26, label %31
31: ; preds = %26, %18
%32 = tail call i32 @fclose(i32* %6)
br label %33
33: ; preds = %31, %8
ret void
}