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

/* Variables and functions */
int /*<<< orphan*/ MSG_NOSIGNAL;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
unsigned long ftell(int /*<<< orphan*/ *);
char *get_file_name(int);
scalar_t__ malloc(int);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ send(int, char *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ send_header(int, int, unsigned long);
int /*<<< orphan*/ stderr;
void work(int client_socket) {
  char *req_file_name = get_file_name(client_socket);
  if (req_file_name == NULL) {
    fprintf(stderr, "Requested file not found! (404)\n");
    send_header(client_socket, 404, 0);
    return;
  }
  FILE *req_file = fopen(req_file_name, "r");
  if (req_file != NULL) {
    fseek(req_file, 0L, SEEK_END);
    unsigned long fsize = ftell(req_file);
    rewind(req_file);
    fprintf(stderr, "Sending the file (200)\n");
    send_header(client_socket, 200, fsize);
    char *buf = (char *)malloc(1024);
    size_t num_read;
    while (0 != (num_read = fread(buf, 1, 1024, req_file))) {
      send(client_socket, buf, num_read, MSG_NOSIGNAL);
    }
    fclose(req_file);
    free(buf);
  } else {
    fprintf(stderr, "Requested file not found! (404)\n");
    send_header(client_socket, 404, 0);
  }
  free(req_file_name);
}


x86 assembly (clang, -O0):
<work>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   10d0 <get_file_name@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    120d <work+0x4d>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
lea    0xe0e(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x4(%rbp),%edi
mov    $0x194,%esi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <send_header@plt>
jmp    1319 <work+0x159>
mov    -0x10(%rbp),%rdi
lea    0xe09(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12ea <work+0x12a>
mov    -0x18(%rbp),%rdi
mov    0x2db1(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10c0 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   10a0 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <rewind@plt>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
lea    0xdbb(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    $0xc8,%esi
call   1090 <send_header@plt>
mov    $0x400,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1040 <fread@plt>
mov    %rax,%rcx
mov    %rcx,-0x30(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
je     12d3 <work+0x113>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    0x2d09(%rip),%rax
mov    (%rax),%ecx
call   1060 <send@plt>
jmp    128c <work+0xcc>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    1310 <work+0x150>
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
lea    0xd06(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x4(%rbp),%edi
mov    $0x194,%esi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <send_header@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<work>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%r15d
call   10d0 <get_file_name@plt>
test   %rax,%rax
je     12ae <work+0xee>
mov    %rax,%r14
lea    0xe3e(%rip),%rsi
mov    %rax,%rdi
call   10e0 <fopen@plt>
test   %rax,%rax
je     12da <work+0x11a>
mov    %rax,%rbx
mov    0x2dea(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    %rbx,%rdi
call   10a0 <ftell@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1070 <rewind@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
lea    0xdf6(%rip),%rsi
call   1080 <fprintf@plt>
mov    %r15d,%edi
mov    $0xc8,%esi
mov    %rbp,%rdx
call   1090 <send_header@plt>
mov    $0x400,%edi
call   10b0 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     129c <work+0xdc>
mov    0x2d60(%rip),%r12
mov    (%r12),%ecx
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rax,%rdx
call   1060 <send@plt>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
test   %rax,%rax
jne    1270 <work+0xb0>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
jmp    12fe <work+0x13e>
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
lea    0xd42(%rip),%rsi
call   1080 <fprintf@plt>
mov    %r15d,%edi
mov    $0x194,%esi
xor    %edx,%edx
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <send_header@plt>
mov    0x2cf7(%rip),%rax
mov    (%rax),%edi
lea    0xd16(%rip),%rsi
call   1080 <fprintf@plt>
mov    %r15d,%edi
mov    $0x194,%esi
xor    %edx,%edx
call   1090 <send_header@plt>
mov    %r14,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [33 x i8] c"Requested file not found! (404)\0A\00"
@.str.1 = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@.str.2 = [24 x i8] c"Sending the file (200)\0A\00"
@MSG_NOSIGNAL = global i32 0
define void @work(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
store i32 %0, i32* %2
%8 = load i32, i32* %2
%9 = call i8* @get_file_name(i32 %8)
store i8* %9, i8** %3
%10 = load i8*, i8** %3
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %17
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* %2
%16 = call i32 @send_header(i32 %15, i32 404, i64 0)
br label %61
17: ; preds = %1
%18 = load i8*, i8** %3
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %4
%20 = load i32*, i32** %4
%21 = icmp ne i32* %20, null
br i1 %21, label %22, label %53
22: ; preds = %17
%23 = load i32*, i32** %4
%24 = load i32, i32* @SEEK_END
%25 = call i32 @fseek(i32* %23, i64 0, i32 %24)
%26 = load i32*, i32** %4
%27 = call i64 @ftell(i32* %26)
store i64 %27, i64* %5
%28 = load i32*, i32** %4
%29 = call i32 @rewind(i32* %28)
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%32 = load i32, i32* %2
%33 = load i64, i64* %5
%34 = call i32 @send_header(i32 %32, i32 200, i64 %33)
%35 = call i64 @malloc(i32 1024)
%36 = inttoptr i64 %35 to i8*
store i8* %36, i8** %6
br label %37
37: ; preds = %42, %22
%38 = load i8*, i8** %6
%39 = load i32*, i32** %4
%40 = call i64 @fread(i8* %38, i32 1, i32 1024, i32* %39)
store i64 %40, i64* %7
%41 = icmp ne i64 0, %40
br i1 %41, label %42, label %48
42: ; preds = %37
%43 = load i32, i32* %2
%44 = load i8*, i8** %6
%45 = load i64, i64* %7
%46 = load i32, i32* @MSG_NOSIGNAL
%47 = call i32 @send(i32 %43, i8* %44, i64 %45, i32 %46)
br label %37
48: ; preds = %37
%49 = load i32*, i32** %4
%50 = call i32 @fclose(i32* %49)
%51 = load i8*, i8** %6
%52 = call i32 @free(i8* %51)
br label %58
53: ; preds = %17
%54 = load i32, i32* @stderr
%55 = call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%56 = load i32, i32* %2
%57 = call i32 @send_header(i32 %56, i32 404, i64 0)
br label %58
58: ; preds = %53, %48
%59 = load i8*, i8** %3
%60 = call i32 @free(i8* %59)
br label %61
61: ; preds = %58, %12
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [33 x i8] c"Requested file not found! (404)\0A\00"
@.str.1 = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@.str.2 = [24 x i8] c"Sending the file (200)\0A\00"
@MSG_NOSIGNAL = global i32 0
define void @work(i32 %0) {
%2 = tail call i8* @get_file_name(i32 %0)
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %8
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @send_header(i32 %0, i32 404, i64 0)
br label %38
8: ; preds = %1
%9 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %32, label %11
11: ; preds = %8
%12 = load i32, i32* @SEEK_END
%13 = tail call i32 @fseek(i32* %9, i64 0, i32 %12)
%14 = tail call i64 @ftell(i32* %9)
%15 = tail call i32 @rewind(i32* %9)
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%18 = tail call i32 @send_header(i32 %0, i32 200, i64 %14)
%19 = tail call i64 @malloc(i32 1024)
%20 = inttoptr i64 %19 to i8*
%21 = tail call i64 @fread(i8* %20, i32 1, i32 1024, i32* %9)
%22 = icmp eq i64 %21, 0
br i1 %22, label %29, label %23
23: ; preds = %11, %23
%24 = phi i64 [ %27, %23 ], [ %21, %11 ]
%25 = load i32, i32* @MSG_NOSIGNAL
%26 = tail call i32 @send(i32 %0, i8* %20, i64 %24, i32 %25)
%27 = tail call i64 @fread(i8* %20, i32 1, i32 1024, i32* %9)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %23
29: ; preds = %23, %11
%30 = tail call i32 @fclose(i32* %9)
%31 = tail call i32 @free(i8* %20)
br label %36
32: ; preds = %8
%33 = load i32, i32* @stderr
%34 = tail call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%35 = tail call i32 @send_header(i32 %0, i32 404, i64 0)
br label %36
36: ; preds = %32, %29
%37 = tail call i32 @free(i8* %2)
br label %38
38: ; preds = %36, %4
ret void
}