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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memset(char *, char, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
size_t recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ sscanf(char *, char *, char *);
int strlen(char *);
void *client_fun(void *arg) {
  int connfd = (int)arg;
  size_t recv_len = 0;
  char recv_buf[2048] = "";
  char send_buf[512] = "";
  char head[] = "HTTP/1.1 200 OK\r\n"
                "Content-Type: text/html\r\n"
                "\r\n";
  char err[] = "HTTP/1.1 404 Not Found\r\n"
               "Content-Type: text/html\r\n"
               "\r\n"
               "<HTML><BODY>File not found</BODY></HTML>";
  if ((recv_len = recv(connfd, recv_buf, sizeof(recv_buf), 0)) > 0) {
    char file_name[512] = "";
    char file_path[50] = "";
    int open_fd = 0;
    sscanf(recv_buf, "GET /%[^ ]", file_name);
    printf("\e[34m%s\e[0m\n", recv_buf);
    sprintf(file_path, "./html/%s", file_name);
    open_fd = open(file_path, O_RDONLY);
    if (open_fd < 0) {
      perror("open");
      send(connfd, err, strlen(err), 0);
    } else {
      int length = 0;
      send(connfd, head, strlen(head), 0);
      while ((length = read(open_fd, send_buf, sizeof(send_buf))) != 0) {
        send(connfd, send_buf, length, 0);
        memset(send_buf, '\0', 512);
      }
    }
  }
  close(connfd);
  printf("\e[31mclient closed!\e[0m\n");
  return NULL;
}


x86 assembly (clang, -O0):
<client_fun>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %eax,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x820(%rbp),%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1070 <memset@plt>
lea    -0xa20(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1070 <memset@plt>
lea    -0xa50(%rbp),%rdi
lea    0xdee(%rip),%rsi
mov    $0x2d,%edx
call   10a0 <memcpy@plt>
lea    -0xab0(%rbp),%rdi
lea    0xe06(%rip),%rsi
mov    $0x5c,%edx
call   10a0 <memcpy@plt>
mov    -0xc(%rbp),%edi
lea    -0x820(%rbp),%rsi
mov    $0x800,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jbe    13e5 <client_fun+0x225>
lea    -0xcb0(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1070 <memset@plt>
lea    -0xcf0(%rbp),%rdi
xor    %esi,%esi
mov    $0x32,%edx
call   1070 <memset@plt>
movl   $0x0,-0xcf4(%rbp)
lea    -0x820(%rbp),%rdi
lea    -0xcb0(%rbp),%rdx
lea    0xdef(%rip),%rsi
call   10b0 <sscanf@plt>
lea    -0x820(%rbp),%rsi
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0xcf0(%rbp),%rdi
lea    -0xcb0(%rbp),%rdx
lea    0xdd8(%rip),%rsi
call   10e0 <sprintf@plt>
lea    -0xcf0(%rbp),%rdi
mov    0x2d09(%rip),%rax
mov    (%rax),%esi
call   10c0 <open@plt>
mov    %eax,-0xcf4(%rbp)
cmpl   $0x0,-0xcf4(%rbp)
jge    1343 <client_fun+0x183>
lea    0xdae(%rip),%rdi
call   10d0 <perror@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0xd04(%rbp)
lea    -0xab0(%rbp),%rax
mov    %rax,-0xd00(%rbp)
lea    -0xab0(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xd04(%rbp),%edi
mov    -0xd00(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
jmp    13e0 <client_fun+0x220>
movl   $0x0,-0xcf8(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0xd14(%rbp)
lea    -0xa50(%rbp),%rax
mov    %rax,-0xd10(%rbp)
lea    -0xa50(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xd14(%rbp),%edi
mov    -0xd10(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    -0xcf4(%rbp),%edi
lea    -0xa20(%rbp),%rsi
mov    $0x200,%edx
call   1090 <read@plt>
mov    %eax,-0xcf8(%rbp)
cmp    $0x0,%eax
je     13db <client_fun+0x21b>
mov    -0xc(%rbp),%edi
lea    -0xa20(%rbp),%rsi
mov    -0xcf8(%rbp),%edx
xor    %ecx,%ecx
call   1050 <send@plt>
lea    -0xa20(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1070 <memset@plt>
jmp    1386 <client_fun+0x1c6>
jmp    13e0 <client_fun+0x220>
jmp    13e5 <client_fun+0x225>
mov    -0xc(%rbp),%edi
call   1080 <close@plt>
lea    0xcbf(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %eax,%eax
add    $0xd20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<client_fun>:
push   %rbp
push   %r14
push   %rbx
sub    $0xcd0,%rsp
mov    %rdi,%r14
lea    0x4d0(%rsp),%rbp
mov    $0x800,%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1080 <memset@plt>
lea    0xd0(%rsp),%rdi
mov    $0x200,%edx
xor    %esi,%esi
call   1080 <memset@plt>
movups 0xe1d(%rip),%xmm0
movups %xmm0,0x5d(%rsp)
movaps 0xe04(%rip),%xmm0
movaps %xmm0,0x50(%rsp)
movaps 0xde8(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movups 0xe58(%rip),%xmm0
movups %xmm0,0xbc(%rsp)
movaps 0xe3d(%rip),%xmm0
movaps %xmm0,0xb0(%rsp)
movaps 0xe1e(%rip),%xmm0
movaps %xmm0,0xa0(%rsp)
movaps 0xdff(%rip),%xmm0
movaps %xmm0,0x90(%rsp)
movaps 0xde0(%rip),%xmm0
movaps %xmm0,0x80(%rsp)
movaps 0xdc1(%rip),%xmm0
movaps %xmm0,0x70(%rsp)
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x800,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
je     1391 <client_fun+0x1d1>
lea    0x2d0(%rsp),%rbp
mov    $0x200,%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1080 <memset@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
movw   $0x0,0x30(%rsp)
lea    0xdc7(%rip),%rsi
lea    0x4d0(%rsp),%rbx
mov    %rbx,%rdi
mov    %rbp,%rdx
call   10b0 <sscanf@plt>
lea    0xdb8(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xdb4(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %rbp,%rdx
call   10e0 <sprintf@plt>
mov    0x2ce3(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10c0 <open@plt>
test   %eax,%eax
js     13b3 <client_fun+0x1f3>
mov    %eax,%ebp
lea    0x40(%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>
lea    0xd0(%rsp),%rsi
mov    %ebp,%edi
mov    $0x200,%edx
call   10a0 <read@plt>
test   %eax,%eax
je     1391 <client_fun+0x1d1>
lea    0xd0(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1080 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   10a0 <read@plt>
test   %eax,%eax
jne    1360 <client_fun+0x1a0>
mov    %r14d,%edi
call   1090 <close@plt>
lea    0xd13(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0xcd0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xcf4(%rip),%rdi
call   10d0 <perror@plt>
lea    0x70(%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>
jmp    1391 <client_fun+0x1d1>

LLVM IR (-O0):
@__const.client_fun.head = [45 x i8] c"HTTP/1.1 200 OK\0D\0AContent-Type: text/html\0D\0A\0D\0A\00"
@__const.client_fun.err = [92 x i8] c"HTTP/1.1 404 Not Found\0D\0AContent-Type: text/html\0D\0A\0D\0A<HTML><BODY>File not found</BODY></HTML>\00"
@.str = [11 x i8] c"GET /%[^ ]\00"
@.str.1 = [13 x i8] c"\1B[34m%s\1B[0m\0A\00"
@.str.2 = [10 x i8] c"./html/%s\00"
@O_RDONLY = global i32 0
@.str.3 = [5 x i8] c"open\00"
@.str.4 = [25 x i8] c"\1B[31mclient closed!\1B[0m\0A\00"
define i8* @client_fun(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i64
%5 = alloca [2048 x i8]
%6 = alloca [512 x i8]
%7 = alloca [45 x i8]
%8 = alloca [92 x i8]
%9 = alloca [512 x i8]
%10 = alloca [50 x i8]
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %2
%13 = load i8*, i8** %2
%14 = ptrtoint i8* %13 to i32
store i32 %14, i32* %3
store i64 0, i64* %4
%15 = bitcast [2048 x i8]* %5 to i8*
%16 = bitcast [512 x i8]* %6 to i8*
%17 = bitcast [45 x i8]* %7 to i8*
%18 = bitcast [92 x i8]* %8 to i8*
%19 = load i32, i32* %3
%20 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%21 = call i64 @recv(i32 %19, i8* %20, i32 2048, i32 0)
store i64 %21, i64* %4
%22 = icmp ugt i64 %21, 0
br i1 %22, label %23, label %66
23: ; preds = %1
%24 = bitcast [512 x i8]* %9 to i8*
%25 = bitcast [50 x i8]* %10 to i8*
store i32 0, i32* %11
%26 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%27 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%28 = call i32 @sscanf(i8* %26, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %27)
%29 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %29)
%31 = getelementptr inbounds [50 x i8], [50 x i8]* %10, i64 0, i64 0
%32 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%33 = call i32 @sprintf(i8* %31, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %32)
%34 = getelementptr inbounds [50 x i8], [50 x i8]* %10, i64 0, i64 0
%35 = load i32, i32* @O_RDONLY
%36 = call i32 @open(i8* %34, i32 %35)
store i32 %36, i32* %11
%37 = load i32, i32* %11
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %46
39: ; preds = %23
%40 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%41 = load i32, i32* %3
%42 = getelementptr inbounds [92 x i8], [92 x i8]* %8, i64 0, i64 0
%43 = getelementptr inbounds [92 x i8], [92 x i8]* %8, i64 0, i64 0
%44 = call i32 @strlen(i8* %43)
%45 = call i32 @send(i32 %41, i8* %42, i32 %44, i32 0)
br label %65
46: ; preds = %23
store i32 0, i32* %12
%47 = load i32, i32* %3
%48 = getelementptr inbounds [45 x i8], [45 x i8]* %7, i64 0, i64 0
%49 = getelementptr inbounds [45 x i8], [45 x i8]* %7, i64 0, i64 0
%50 = call i32 @strlen(i8* %49)
%51 = call i32 @send(i32 %47, i8* %48, i32 %50, i32 0)
br label %52
52: ; preds = %57, %46
%53 = load i32, i32* %11
%54 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%55 = call i32 @read(i32 %53, i8* %54, i32 512)
store i32 %55, i32* %12
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %64
57: ; preds = %52
%58 = load i32, i32* %3
%59 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%60 = load i32, i32* %12
%61 = call i32 @send(i32 %58, i8* %59, i32 %60, i32 0)
%62 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%63 = call i32 @memset(i8* %62, i8 signext 0, i32 512)
br label %52
64: ; preds = %52
br label %65
65: ; preds = %64, %39
br label %66
66: ; preds = %65, %1
%67 = load i32, i32* %3
%68 = call i32 @close(i32 %67)
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
ret i8* null
}

LLVM IR (-O2):
@__const.client_fun.head = [45 x i8] c"HTTP/1.1 200 OK\0D\0AContent-Type: text/html\0D\0A\0D\0A\00"
@__const.client_fun.err = [92 x i8] c"HTTP/1.1 404 Not Found\0D\0AContent-Type: text/html\0D\0A\0D\0A<HTML><BODY>File not found</BODY></HTML>\00"
@.str = [11 x i8] c"GET /%[^ ]\00"
@.str.1 = [13 x i8] c"\1B[34m%s\1B[0m\0A\00"
@.str.2 = [10 x i8] c"./html/%s\00"
@O_RDONLY = global i32 0
@.str.3 = [5 x i8] c"open\00"
@str = [24 x i8] c"\1B[31mclient closed!\1B[0m\00"
define noalias i8* @client_fun(i8* %0) {
%2 = alloca [2048 x i8]
%3 = alloca [512 x i8]
%4 = alloca [45 x i8]
%5 = alloca [92 x i8]
%6 = alloca [512 x i8]
%7 = alloca [50 x i8]
%8 = ptrtoint i8* %0 to i64
%9 = trunc i64 %8 to i32
%10 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
%11 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%12 = getelementptr inbounds [45 x i8], [45 x i8]* %4, i64 0, i64 0
%13 = getelementptr inbounds [92 x i8], [92 x i8]* %5, i64 0, i64 0
%14 = call i64 @recv(i32 %9, i8* %10, i32 2048, i32 0)
%15 = icmp eq i64 %14, 0
br i1 %15, label %41, label %16
16: ; preds = %1
%17 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%18 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 0
%19 = call i32 @sscanf(i8* %10, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %17)
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%21 = call i32 @sprintf(i8* %18, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %17)
%22 = load i32, i32* @O_RDONLY
%23 = call i32 @open(i8* %18, i32 %22)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %16
%26 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%27 = call i32 @strlen(i8* %13)
%28 = call i32 @send(i32 %9, i8* %13, i32 %27, i32 0)
br label %40
29: ; preds = %16
%30 = call i32 @strlen(i8* %12)
%31 = call i32 @send(i32 %9, i8* %12, i32 %30, i32 0)
%32 = call i32 @read(i32 %23, i8* %11, i32 512)
%33 = icmp eq i32 %32, 0
br i1 %33, label %40, label %34
34: ; preds = %29, %34
%35 = phi i32 [ %38, %34 ], [ %32, %29 ]
%36 = call i32 @send(i32 %9, i8* %11, i32 %35, i32 0)
%37 = call i32 @memset(i8* %11, i8 signext 0, i32 512)
%38 = call i32 @read(i32 %23, i8* %11, i32 512)
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %34
40: ; preds = %34, %29, %25
br label %41
41: ; preds = %40, %1
%42 = call i32 @close(i32 %9)
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
ret i8* null
}