#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 {
  scalar_t__ st_size;
};
typedef int /*<<< orphan*/ reply_404;
typedef int /*<<< orphan*/ reply_400;
typedef int /*<<< orphan*/ buff;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
scalar_t__ recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, long);
int sscanf(char *, char *, char *);
scalar_t__ stat(char const *, struct stat *);
size_t strlen(char *);
void handle_client(int client_sock) {
  static char buff[1024];
  static char path_buff[1024];
  static char reply_400[] = "HTTP/1.1"
                            " 400 Bad Request\r\nConnection: close\r\n\r\n";
  static char reply_404[] =
      "HTTP/1.1"
      " 404 Not Found\r\nConnection: close\r\n\r\nThe requested document was "
      "not found on this server.\r\n";
  if (recv(client_sock, buff, sizeof(buff) - 1, 0) < 0) {
    perror("recv() failed");
    close(client_sock);
    return;
  }
  if (sscanf(buff, " GET %1024s %*s", path_buff) != 1) {
    send(client_sock, reply_400, sizeof(reply_400) - 1, 0);
    close(client_sock);
    return;
  }
  const char *path = path_buff;
  if (*path == '/') {
    path++;
  }
  struct stat statbuf;
  if (stat(path, &statbuf) < 0) {
    send(client_sock, reply_404, sizeof(reply_404) - 1, 0);
    close(client_sock);
    return;
  }
  FILE *to_send = fopen(path, "r");
  if (to_send == NULL) {
    send(client_sock, reply_404, sizeof(reply_404) - 1, 0);
    close(client_sock);
    return;
  }
  sprintf(buff,
          "HTTP/1.1"
          " 400 OK\r\nConnection: close\r\nContent-Length: "
          "%ld\r\nContent-Type: text/html\r\n\r\n",
          (long)statbuf.st_size);
  send(client_sock, buff, strlen(buff), 0);
  size_t n;
  while ((n = fread(buff, 1, sizeof(buff), to_send)) > 0) {
    send(client_sock, buff, n, 0);
  }
  fclose(to_send);
  close(client_sock);
}


x86 assembly (clang, -O0):
<handle_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    0x2f6b(%rip),%rsi
mov    $0x3ff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0x0,%rax
jge    11f4 <handle_client+0x44>
lea    0xe1e(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x4(%rbp),%edi
call   1080 <close@plt>
jmp    136d <handle_client+0x1bd>
lea    0x2f35(%rip),%rdi
lea    0xe0c(%rip),%rsi
lea    0x3327(%rip),%rdx
call   10a0 <sscanf@plt>
cmp    $0x1,%eax
je     123a <handle_client+0x8a>
mov    -0x4(%rbp),%edi
lea    0x2e5f(%rip),%rsi
mov    $0x2f,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    -0x4(%rbp),%edi
call   1080 <close@plt>
jmp    136d <handle_client+0x1bd>
lea    0x32ef(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1261 <handle_client+0xb1>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%rax
jge    129b <handle_client+0xeb>
mov    -0x4(%rbp),%edi
lea    0x2e2e(%rip),%rsi
mov    $0x63,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    -0x4(%rbp),%edi
call   1080 <close@plt>
jmp    136d <handle_client+0x1bd>
mov    -0x10(%rbp),%rdi
lea    0xd78(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12dd <handle_client+0x12d>
mov    -0x4(%rbp),%edi
lea    0x2dec(%rip),%rsi
mov    $0x63,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    -0x4(%rbp),%edi
call   1080 <close@plt>
jmp    136d <handle_client+0x1bd>
mov    -0x18(%rbp),%rdx
lea    0x2e48(%rip),%rdi
lea    0xd31(%rip),%rsi
call   10d0 <sprintf@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x2c(%rbp)
lea    0x2e2f(%rip),%rdi
call   1060 <strlen@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0x2e1d(%rip),%rsi
xor    %ecx,%ecx
call   1070 <send@plt>
mov    -0x20(%rbp),%rcx
lea    0x2e0b(%rip),%rdi
mov    $0x1,%esi
mov    $0x400,%edx
call   1040 <fread@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jbe    135c <handle_client+0x1ac>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rdx
lea    0x2de0(%rip),%rsi
xor    %ecx,%ecx
call   1070 <send@plt>
jmp    131a <handle_client+0x16a>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x4(%rbp),%edi
call   1080 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_client>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
lea    0x2f6e(%rip),%rsi
mov    $0x3ff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
js     1321 <handle_client+0x171>
lea    0x2f52(%rip),%rdi
lea    0xe29(%rip),%rsi
lea    0x3344(%rip),%rbx
mov    %rbx,%rdx
call   10a0 <sscanf@plt>
cmp    $0x1,%eax
jne    12d4 <handle_client+0x124>
cmpb   $0x2f,0x332c(%rip)
lea    0x3326(%rip),%rax
cmove  %rax,%rbx
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1090 <stat@plt>
test   %rax,%rax
js     12fa <handle_client+0x14a>
lea    0xdf2(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12fa <handle_client+0x14a>
mov    %rax,%rbx
mov    0x8(%rsp),%rdx
lea    0x2ee4(%rip),%rbp
lea    0xdcd(%rip),%rsi
mov    %rbp,%rdi
call   10d0 <sprintf@plt>
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %rax,%rdx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     12ca <handle_client+0x11a>
lea    0x2e9c(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %rax,%rdx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
test   %rax,%rax
jne    12a0 <handle_client+0xf0>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    1310 <handle_client+0x160>
lea    0x2da5(%rip),%rsi
mov    $0x2f,%edx
mov    %r14d,%edi
xor    %ecx,%ecx
call   1070 <send@plt>
mov    %r14d,%edi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1080 <close@plt>
lea    0x2daf(%rip),%rsi
mov    $0x63,%edx
mov    %r14d,%edi
xor    %ecx,%ecx
call   1070 <send@plt>
mov    %r14d,%edi
call   1080 <close@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xcd8(%rip),%rdi
call   10c0 <perror@plt>
jmp    12ea <handle_client+0x13a>

LLVM IR (-O0):
%struct.stat = type { i64 }
@handle_client.buff = internal global [1024 x i8] zeroinitializer
@handle_client.path_buff = internal global [1024 x i8] zeroinitializer
@handle_client.reply_400 = internal global [48 x i8] c"HTTP/1.1 400 Bad Request\0D\0AConnection: close\0D\0A\0D\0A\00"
@handle_client.reply_404 = internal global [100 x i8] c"HTTP/1.1 404 Not Found\0D\0AConnection: close\0D\0A\0D\0AThe requested document was not found on this server.\0D\0A\00"
@.str = [14 x i8] c"recv() failed\00"
@.str.1 = [16 x i8] c" GET %1024s %*s\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [85 x i8] c"HTTP/1.1 400 OK\0D\0AConnection: close\0D\0AContent-Length: %ld\0D\0AContent-Type: text/html\0D\0A\0D\0A\00"
define void @handle_client(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i32*
%6 = alloca i64
store i32 %0, i32* %2
%7 = load i32, i32* %2
%8 = call i64 @recv(i32 %7, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_client.buff, i64 0, i64 0), i32 1023, i32 0)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* %2
%13 = call i32 @close(i32 %12)
br label %69
14: ; preds = %1
%15 = call i32 @sscanf(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_client.buff, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_client.path_buff, i64 0, i64 0))
%16 = icmp ne i32 %15, 1
br i1 %16, label %17, label %22
17: ; preds = %14
%18 = load i32, i32* %2
%19 = call i32 @send(i32 %18, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @handle_client.reply_400, i64 0, i64 0), i64 47, i32 0)
%20 = load i32, i32* %2
%21 = call i32 @close(i32 %20)
br label %69
22: ; preds = %14
store i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_client.path_buff, i64 0, i64 0), i8** %3
%23 = load i8*, i8** %3
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 47
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = load i8*, i8** %3
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %3
br label %30
30: ; preds = %27, %22
%31 = load i8*, i8** %3
%32 = call i64 @stat(i8* %31, %struct.stat* %4)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %30
%35 = load i32, i32* %2
%36 = call i32 @send(i32 %35, i8* getelementptr inbounds ([100 x i8], [100 x i8]* @handle_client.reply_404, i64 0, i64 0), i64 99, i32 0)
%37 = load i32, i32* %2
%38 = call i32 @close(i32 %37)
br label %69
39: ; preds = %30
%40 = load i8*, i8** %3
%41 = call i32* @fopen(i8* %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %41, i32** %5
%42 = load i32*, i32** %5
%43 = icmp eq i32* %42, null
br i1 %43, label %44, label %49
44: ; preds = %39
%45 = load i32, i32* %2
%46 = call i32 @send(i32 %45, i8* getelementptr inbounds ([100 x i8], [100 x i8]* @handle_client.reply_404, i64 0, i64 0), i64 99, i32 0)
%47 = load i32, i32* %2
%48 = call i32 @close(i32 %47)
br label %69
49: ; preds = %39
%50 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%51 = load i64, i64* %50
%52 = call i32 @sprintf(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_client.buff, i64 0, i64 0), i8* getelementptr inbounds ([85 x i8], [85 x i8]* @.str.3, i64 0, i64 0), i64 %51)
%53 = load i32, i32* %2
%54 = call i64 @strlen(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_client.buff, i64 0, i64 0))
%55 = call i32 @send(i32 %53, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_client.buff, i64 0, i64 0), i64 %54, i32 0)
br label %56
56: ; preds = %60, %49
%57 = load i32*, i32** %5
%58 = call i64 @fread(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_client.buff, i64 0, i64 0), i32 1, i32 1024, i32* %57)
store i64 %58, i64* %6
%59 = icmp ugt i64 %58, 0
br i1 %59, label %60, label %64
60: ; preds = %56
%61 = load i32, i32* %2
%62 = load i64, i64* %6
%63 = call i32 @send(i32 %61, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_client.buff, i64 0, i64 0), i64 %62, i32 0)
br label %56
64: ; preds = %56
%65 = load i32*, i32** %5
%66 = call i32 @fclose(i32* %65)
%67 = load i32, i32* %2
%68 = call i32 @close(i32 %67)
br label %69
69: ; preds = %64, %44, %34, %17, %10
ret void
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@handle_client.buff = internal global [1024 x i8] zeroinitializer
@handle_client.path_buff = internal global [1024 x i8] zeroinitializer
@handle_client.reply_400 = internal global [48 x i8] c"HTTP/1.1 400 Bad Request\0D\0AConnection: close\0D\0A\0D\0A\00"
@handle_client.reply_404 = internal global [100 x i8] c"HTTP/1.1 404 Not Found\0D\0AConnection: close\0D\0A\0D\0AThe requested document was not found on this server.\0D\0A\00"
@.str = [14 x i8] c"recv() failed\00"
@.str.1 = [16 x i8] c" GET %1024s %*s\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [85 x i8] c"HTTP/1.1 400 OK\0D\0AConnection: close\0D\0AContent-Length: %ld\0D\0AContent-Type: text/html\0D\0A\0D\0A\00"
define void @handle_client(i32 %0) {
%2 = alloca %struct.stat
%3 = tail call i64 @recv(i32 %0, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_client.buff, i64 0, i64 0), i32 1023, i32 0)
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @close(i32 %0)
br label %45
8: ; preds = %1
%9 = tail call i32 @sscanf(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_client.buff, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_client.path_buff, i64 0, i64 0))
%10 = icmp eq i32 %9, 1
br i1 %10, label %14, label %11
11: ; preds = %8
%12 = tail call i32 @send(i32 %0, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @handle_client.reply_400, i64 0, i64 0), i64 47, i32 0)
%13 = tail call i32 @close(i32 %0)
br label %45
14: ; preds = %8
%15 = load i8, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_client.path_buff, i64 0, i64 0)
%16 = icmp eq i8 %15, 47
%17 = select i1 %16, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_client.path_buff, i64 0, i64 1), i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_client.path_buff, i64 0, i64 0)
%18 = bitcast %struct.stat* %2 to i8*
%19 = call i64 @stat(i8* %17, %struct.stat* %2)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = call i32 @send(i32 %0, i8* getelementptr inbounds ([100 x i8], [100 x i8]* @handle_client.reply_404, i64 0, i64 0), i64 99, i32 0)
br label %43
23: ; preds = %14
%24 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32 @send(i32 %0, i8* getelementptr inbounds ([100 x i8], [100 x i8]* @handle_client.reply_404, i64 0, i64 0), i64 99, i32 0)
br label %43
28: ; preds = %23
%29 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%30 = load i64, i64* %29
%31 = call i32 @sprintf(i8* dereferenceable(1) getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_client.buff, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([85 x i8], [85 x i8]* @.str.3, i64 0, i64 0), i64 %30)
%32 = call i64 @strlen(i8* dereferenceable(1) getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_client.buff, i64 0, i64 0))
%33 = call i32 @send(i32 %0, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_client.buff, i64 0, i64 0), i64 %32, i32 0)
%34 = call i64 @fread(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_client.buff, i64 0, i64 0), i32 1, i32 1024, i32* %24)
%35 = icmp eq i64 %34, 0
br i1 %35, label %41, label %36
36: ; preds = %28, %36
%37 = phi i64 [ %39, %36 ], [ %34, %28 ]
%38 = call i32 @send(i32 %0, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_client.buff, i64 0, i64 0), i64 %37, i32 0)
%39 = call i64 @fread(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_client.buff, i64 0, i64 0), i32 1, i32 1024, i32* %24)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %36
41: ; preds = %36, %28
%42 = call i32 @fclose(i32* %24)
br label %43
43: ; preds = %26, %41, %21
%44 = call i32 @close(i32 %0)
br label %45
45: ; preds = %43, %5, %11
ret void
}