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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ response_header(int, char *);
int /*<<< orphan*/ send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strlen(char *);
void serve_file(int clientfd, const char *file_path) {
  printf("Serve file...\n");
  FILE *resource;
  char buf[1024];
  char filename[1024];
  int numchars = 1;
  int file_len = 0;
  filename[0] = '.';
  filename[1] = '\0';
  strcat(filename, file_path);
  printf("respond file: %s\n", filename);
  resource = fopen(filename, "r");
  fseek(resource, 0, 2);
  file_len = ftell(resource);
  fseek(resource, 0, 0);
  printf("====file_len:%d=======\n", file_len);
  if (resource == NULL) {
    printf("File not found!\n");
  } else {
    response_header(clientfd, filename);
    fgets(buf, sizeof(buf), resource);
    while (!feof(resource)) {
      send(clientfd, buf, strlen(buf), 0);
      fgets(buf, sizeof(buf), resource);
    }
  }
  fclose(resource);
}


x86 assembly (clang, -O0):
<serve_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe37(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x824(%rbp)
movl   $0x0,-0x828(%rbp)
movb   $0x2e,-0x820(%rbp)
movb   $0x0,-0x81f(%rbp)
lea    -0x820(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10d0 <strcat@plt>
lea    -0x820(%rbp),%rsi
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x820(%rbp),%rdi
lea    0xdfc(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x2,%edx
call   10b0 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1080 <ftell@plt>
mov    %eax,-0x828(%rbp)
mov    -0x18(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   10b0 <fseek@plt>
mov    -0x828(%rbp),%esi
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
cmpq   $0x0,-0x18(%rbp)
jne    128c <serve_file+0xdc>
lea    0xdbb(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1326 <serve_file+0x176>
mov    -0x4(%rbp),%edi
lea    -0x820(%rbp),%rsi
call   10a0 <response_header@plt>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x400,%esi
call   1070 <fgets@plt>
mov    -0x18(%rbp),%rdi
call   1090 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    12ce <serve_file+0x11e>
jmp    1321 <serve_file+0x171>
mov    -0x4(%rbp),%eax
mov    %eax,-0x834(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x830(%rbp)
lea    -0x420(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x834(%rbp),%edi
mov    -0x830(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x400,%esi
call   1070 <fgets@plt>
jmp    12b0 <serve_file+0x100>
jmp    1326 <serve_file+0x176>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serve_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x800,%rsp
mov    %rsi,%rbx
mov    %edi,%r14d
lea    0xe54(%rip),%rdi
call   1030 <puts@plt>
movw   $0x2e,(%rsp)
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %rbx,%rsi
call   10e0 <strcat@plt>
lea    0xe08(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xe09(%rip),%rsi
mov    %rbp,%rdi
call   10d0 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x2,%edx
call   10c0 <fseek@plt>
mov    %rbx,%rdi
call   1090 <ftell@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10c0 <fseek@plt>
lea    0xdd4(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1070 <printf@plt>
test   %rbx,%rbx
je     12c5 <serve_file+0x105>
mov    %rsp,%rsi
mov    %r14d,%edi
call   10b0 <response_header@plt>
lea    0x400(%rsp),%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
call   1080 <fgets@plt>
mov    %rbx,%rdi
call   10a0 <feof@plt>
test   %eax,%eax
jne    12d1 <serve_file+0x111>
lea    0x400(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %rbp,%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
call   1080 <fgets@plt>
mov    %rbx,%rdi
call   10a0 <feof@plt>
test   %eax,%eax
je     1290 <serve_file+0xd0>
jmp    12d1 <serve_file+0x111>
lea    0xd6e(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
add    $0x800,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"Serve file...\0A\00"
@.str.1 = [18 x i8] c"respond file: %s\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [24 x i8] c"====file_len:%d=======\0A\00"
@.str.4 = [17 x i8] c"File not found!\0A\00"
define void @serve_file(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca [1024 x i8]
%7 = alloca [1024 x i8]
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %8
store i32 0, i32* %9
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
store i8 46, i8* %11
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 1
store i8 0, i8* %12
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%14 = load i8*, i8** %4
%15 = call i32 @strcat(i8* %13, i8* %14)
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %19, i32** %5
%20 = load i32*, i32** %5
%21 = call i32 @fseek(i32* %20, i32 0, i32 2)
%22 = load i32*, i32** %5
%23 = call i32 @ftell(i32* %22)
store i32 %23, i32* %9
%24 = load i32*, i32** %5
%25 = call i32 @fseek(i32* %24, i32 0, i32 0)
%26 = load i32, i32* %9
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %26)
%28 = load i32*, i32** %5
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %32
30: ; preds = %2
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %54
32: ; preds = %2
%33 = load i32, i32* %3
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%35 = call i32 @response_header(i32 %33, i8* %34)
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%37 = load i32*, i32** %5
%38 = call i32 @fgets(i8* %36, i32 1024, i32* %37)
br label %39
39: ; preds = %44, %32
%40 = load i32*, i32** %5
%41 = call i32 @feof(i32* %40)
%42 = icmp ne i32 %41, 0
%43 = xor i1 %42, true
br i1 %43, label %44, label %53
44: ; preds = %39
%45 = load i32, i32* %3
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%48 = call i32 @strlen(i8* %47)
%49 = call i32 @send(i32 %45, i8* %46, i32 %48, i32 0)
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%51 = load i32*, i32** %5
%52 = call i32 @fgets(i8* %50, i32 1024, i32* %51)
br label %39
53: ; preds = %39
br label %54
54: ; preds = %53, %30
%55 = load i32*, i32** %5
%56 = call i32 @fclose(i32* %55)
ret void
}

LLVM IR (-O2):
@.str.1 = [18 x i8] c"respond file: %s\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [24 x i8] c"====file_len:%d=======\0A\00"
@str = [14 x i8] c"Serve file...\00"
@str.5 = [16 x i8] c"File not found!\00"
define void @serve_file(i32 %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = alloca [1024 x i8]
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
store i8 46, i8* %7
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 1
store i8 0, i8* %8
%9 = call i32 @strcat(i8* %7, i8* %1)
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %7)
%11 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%12 = call i32 @fseek(i32* %11, i32 0, i32 2)
%13 = call i32 @ftell(i32* %11)
%14 = call i32 @fseek(i32* %11, i32 0, i32 0)
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%16 = icmp eq i32* %11, null
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.5, i64 0, i64 0))
br label %30
19: ; preds = %2
%20 = call i32 @response_header(i32 %0, i8* %7)
%21 = call i32 @fgets(i8* %6, i32 1024, i32* %11)
%22 = call i32 @feof(i32* %11)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %19, %24
%25 = call i32 @strlen(i8* %6)
%26 = call i32 @send(i32 %0, i8* %6, i32 %25, i32 0)
%27 = call i32 @fgets(i8* %6, i32 1024, i32* %11)
%28 = call i32 @feof(i32* %11)
%29 = icmp eq i32 %28, 0
br i1 %29, label %24, label %30
30: ; preds = %24, %19, %17
%31 = call i32 @fclose(i32* %11)
ret void
}