#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*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
scalar_t__ strcmp(char *, char *);
int strlen(char *);
void do_get(int fd, char *filename) {
  if (strcmp(filename, "/") == 0)
    filename = "/index.html";
  char path[128] = {'\0'};
  sprintf(path, "%s%s", "/WEB", filename);
  FILE *fp = fopen(path, "r");
  if (NULL == fp) {
    printf("The file %s is not found!\n", path);
    char errmsg[32] = "<h1>404 file not found!</h1>";
    send(fd, errmsg, strlen(errmsg), 0);
    return;
  }
  while (1) {
    char buf[1024] = {'\0'};
    int read_size = fread(buf, 1, sizeof(buf), fp);
    if (read_size <= 0)
      break;
    send(fd, buf, read_size, 0);
  }
  fclose(fp);
  return;
}


x86 assembly (clang, -O0):
<do_get>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4e0,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe53(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    11c7 <do_get+0x37>
lea    0xe3f(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    -0x90(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1080 <memset@plt>
lea    -0x90(%rbp),%rdi
mov    -0x10(%rbp),%rcx
lea    0xe22(%rip),%rsi
lea    0xe20(%rip),%rdx
call   10b0 <sprintf@plt>
lea    -0x90(%rbp),%rdi
lea    0xe12(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x98(%rbp)
xor    %eax,%eax
cmp    -0x98(%rbp),%rax
jne    12aa <do_get+0x11a>
lea    -0x90(%rbp),%rsi
lea    0xdeb(%rip),%rdi
call   1070 <printf@plt>
mov    0xe05(%rip),%rax
mov    %rax,-0xc0(%rbp)
mov    0xdff(%rip),%rax
mov    %rax,-0xb8(%rbp)
mov    0xdf9(%rip),%rax
mov    %rax,-0xb0(%rbp)
mov    0xdf3(%rip),%rax
mov    %rax,-0xa8(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x4d4(%rbp)
lea    -0xc0(%rbp),%rax
mov    %rax,-0x4d0(%rbp)
lea    -0xc0(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x4d4(%rbp),%edi
mov    -0x4d0(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
jmp    131f <do_get+0x18f>
jmp    12af <do_get+0x11f>
lea    -0x4c0(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
lea    -0x4c0(%rbp),%rdi
mov    -0x98(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1030 <fread@plt>
mov    %eax,-0x4c4(%rbp)
cmpl   $0x0,-0x4c4(%rbp)
jg     12f7 <do_get+0x167>
jmp    1313 <do_get+0x183>
mov    -0x4(%rbp),%edi
lea    -0x4c0(%rbp),%rsi
mov    -0x4c4(%rbp),%edx
xor    %ecx,%ecx
call   1060 <send@plt>
jmp    12af <do_get+0x11f>
mov    -0x98(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x4e0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_get>:
push   %rbp
push   %r14
push   %rbx
sub    $0x480,%rsp
mov    %rsi,%rbx
mov    %edi,%r14d
lea    0xe58(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcmp@plt>
test   %rax,%rax
lea    0xe48(%rip),%rcx
cmovne %rbx,%rcx
xorps  %xmm0,%xmm0
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
lea    0xe1f(%rip),%rsi
lea    0xe1d(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
call   10b0 <sprintf@plt>
lea    0xe10(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1291 <do_get+0x101>
mov    %rax,%rbx
lea    0x80(%rsp),%rbp
mov    $0x400,%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1080 <memset@plt>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jle    1287 <do_get+0xf7>
lea    0x80(%rsp),%rbp
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    $0x400,%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1080 <memset@plt>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jg     1250 <do_get+0xc0>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    12dd <do_get+0x14d>
lea    0xd82(%rip),%rdi
mov    %rsp,%rsi
call   1070 <printf@plt>
movaps 0xda9(%rip),%xmm0
movaps %xmm0,0x90(%rsp)
movaps 0xd8a(%rip),%xmm0
movaps %xmm0,0x80(%rsp)
lea    0x80(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
add    $0x480,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@.str.1 = [12 x i8] c"/index.html\00"
@.str.2 = [5 x i8] c"%s%s\00"
@.str.3 = [5 x i8] c"/WEB\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [27 x i8] c"The file %s is not found!\0A\00"
@__const.do_get.errmsg = [32 x i8] c"<h1>404 file not found!</h1>\00\00\00\00"
define void @do_get(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca [128 x i8]
%6 = alloca i32*
%7 = alloca [32 x i8]
%8 = alloca [1024 x i8]
%9 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%10 = load i8*, i8** %4
%11 = call i64 @strcmp(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8** %4
br label %14
14: ; preds = %13, %2
%15 = bitcast [128 x i8]* %5 to i8*
%16 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%17 = load i8*, i8** %4
%18 = call i32 @sprintf(i8* %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %17)
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %20, i32** %6
%21 = load i32*, i32** %6
%22 = icmp eq i32* null, %21
br i1 %22, label %23, label %32
23: ; preds = %14
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%25 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i8* %24)
%26 = bitcast [32 x i8]* %7 to i8*
%27 = load i32, i32* %3
%28 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%29 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%30 = call i32 @strlen(i8* %29)
%31 = call i32 @send(i32 %27, i8* %28, i32 %30, i32 0)
br label %49
32: ; preds = %14
br label %33
33: ; preds = %32, %41
%34 = bitcast [1024 x i8]* %8 to i8*
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%36 = load i32*, i32** %6
%37 = call i32 @fread(i8* %35, i32 1, i32 1024, i32* %36)
store i32 %37, i32* %9
%38 = load i32, i32* %9
%39 = icmp sle i32 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %33
br label %46
41: ; preds = %33
%42 = load i32, i32* %3
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%44 = load i32, i32* %9
%45 = call i32 @send(i32 %42, i8* %43, i32 %44, i32 0)
br label %33
46: ; preds = %40
%47 = load i32*, i32** %6
%48 = call i32 @fclose(i32* %47)
br label %49
49: ; preds = %46, %23
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.1 = [12 x i8] c"/index.html\00"
@.str.2 = [5 x i8] c"%s%s\00"
@.str.3 = [5 x i8] c"/WEB\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [27 x i8] c"The file %s is not found!\0A\00"
@__const.do_get.errmsg = [32 x i8] c"<h1>404 file not found!</h1>\00\00\00\00"
define void @do_get(i32 %0, i8* %1) {
%3 = alloca [128 x i8]
%4 = alloca [32 x i8]
%5 = alloca [1024 x i8]
%6 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i64 %6, 0
%8 = select i1 %7, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %1
%9 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%10 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %8)
%11 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %17, label %13
13: ; preds = %2
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%15 = call i32 @fread(i8* %14, i32 1, i32 1024, i32* %11)
%16 = icmp slt i32 %15, 1
br i1 %16, label %27, label %22
17: ; preds = %2
%18 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i8* %9)
%19 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%20 = call i32 @strlen(i8* %19)
%21 = call i32 @send(i32 %0, i8* %19, i32 %20, i32 0)
br label %29
22: ; preds = %13, %22
%23 = phi i32 [ %25, %22 ], [ %15, %13 ]
%24 = call i32 @send(i32 %0, i8* %14, i32 %23, i32 0)
%25 = call i32 @fread(i8* %14, i32 1, i32 1024, i32* %11)
%26 = icmp slt i32 %25, 1
br i1 %26, label %27, label %22
27: ; preds = %22, %13
%28 = call i32 @fclose(i32* %11)
br label %29
29: ; preds = %27, %17
ret void
}