#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 */

/* Variables and functions */
int /*<<< orphan*/ BAD_REQUEST(int);
int ENOENT;
int /*<<< orphan*/ INTERNAL_SERVER_ERROR(int);
int /*<<< orphan*/ NOT_FOUND(int);
int /*<<< orphan*/ OK(int);
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ conn_delete(int);
int errno;
int /*<<< orphan*/ free(int *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char *, int, int /*<<< orphan*/);
char *sep_path(char *, char *);
char *str_comp(char *, char *);
int /*<<< orphan*/ thread_release();
void *web_service(void *arg) {
  printf("new Thread Started\n");
  char req_buf[1024];
  int sock = *((int *)arg);
  recv(sock, req_buf, 1024, 0);
  char *next = str_comp(req_buf, "GET");
  if (next == NULL) {
    BAD_REQUEST(sock);
    goto fin;
  }
  char path_buf[256];
  next = sep_path(next, path_buf);
  if (*path_buf != '/') {
    BAD_REQUEST(sock);
    goto fin;
  }
  char *path = path_buf;
  path++;
  if (*path == '\0') {
    path = "index.html";
  }
  int res_fd = open(path, O_RDONLY);
  if (res_fd == -1) {
    int error = errno;
    if (error == ENOENT) {
      NOT_FOUND(sock);
      goto fin;
    }
    INTERNAL_SERVER_ERROR(sock);
    goto fin;
  }
  OK(sock);
  int nsize;
  char file_buf[1024];
  while ((nsize = read(res_fd, file_buf, 1024)) > 0) {
    send(sock, file_buf, nsize, 0);
  }
fin:
  close(sock);
  thread_release();
  conn_delete(sock);
  free((int *)arg);
  return NULL;
}


x86 assembly (clang, -O0):
<web_service>:
push   %rbp
mov    %rsp,%rbp
sub    $0x940,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xdfa(%rip),%rdi
call   1090 <printf@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x414(%rbp)
mov    -0x414(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1050 <recv@plt>
lea    -0x410(%rbp),%rdi
lea    0xdd6(%rip),%rsi
call   10e0 <str_comp@plt>
mov    %rax,-0x420(%rbp)
cmpq   $0x0,-0x420(%rbp)
jne    1268 <web_service+0x78>
mov    -0x414(%rbp),%edi
call   1110 <BAD_REQUEST@plt>
jmp    13a4 <web_service+0x1b4>
mov    -0x420(%rbp),%rdi
lea    -0x520(%rbp),%rsi
call   1060 <sep_path@plt>
mov    %rax,-0x420(%rbp)
movsbl -0x520(%rbp),%eax
cmp    $0x2f,%eax
je     12a2 <web_service+0xb2>
mov    -0x414(%rbp),%edi
call   1110 <BAD_REQUEST@plt>
jmp    13a4 <web_service+0x1b4>
lea    -0x520(%rbp),%rax
mov    %rax,-0x528(%rbp)
mov    -0x528(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x528(%rbp)
mov    -0x528(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    12e3 <web_service+0xf3>
lea    0xd3c(%rip),%rax
mov    %rax,-0x528(%rbp)
mov    -0x528(%rbp),%rdi
mov    0x2cf7(%rip),%rax
mov    (%rax),%esi
call   1100 <open@plt>
mov    %eax,-0x52c(%rbp)
cmpl   $0xffffffff,-0x52c(%rbp)
jne    134f <web_service+0x15f>
mov    0x2cc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x530(%rbp)
mov    -0x530(%rbp),%eax
mov    0x2ca1(%rip),%rcx
cmp    (%rcx),%eax
jne    133f <web_service+0x14f>
mov    -0x414(%rbp),%edi
call   10c0 <NOT_FOUND@plt>
jmp    13a4 <web_service+0x1b4>
mov    -0x414(%rbp),%edi
call   10f0 <INTERNAL_SERVER_ERROR@plt>
jmp    13a4 <web_service+0x1b4>
mov    -0x414(%rbp),%edi
call   1070 <OK@plt>
mov    -0x52c(%rbp),%edi
lea    -0x940(%rbp),%rsi
mov    $0x400,%edx
call   10b0 <read@plt>
mov    %eax,-0x534(%rbp)
cmp    $0x0,%eax
jle    139f <web_service+0x1af>
mov    -0x414(%rbp),%edi
lea    -0x940(%rbp),%rsi
mov    -0x534(%rbp),%edx
xor    %ecx,%ecx
call   1080 <send@plt>
jmp    135a <web_service+0x16a>
jmp    13a4 <web_service+0x1b4>
mov    -0x414(%rbp),%edi
call   10a0 <close@plt>
mov    $0x0,%al
call   1030 <thread_release@plt>
mov    -0x414(%rbp),%edi
call   10d0 <conn_delete@plt>
mov    -0x8(%rbp),%rdi
call   1040 <free@plt>
xor    %eax,%eax
add    $0x940,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<web_service>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x908,%rsp
mov    %rdi,%r14
lea    0xe08(%rip),%rdi
call   1060 <puts@plt>
mov    (%r14),%r15d
lea    0x500(%rsp),%rbx
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1050 <recv@plt>
lea    0xdd0(%rip),%rsi
mov    %rbx,%rdi
call   10e0 <str_comp@plt>
test   %rax,%rax
je     12d4 <web_service+0xe4>
mov    %rsp,%rsi
mov    %rax,%rdi
call   1070 <sep_path@plt>
cmpb   $0x2f,(%rsp)
jne    12d4 <web_service+0xe4>
lea    0x1(%rsp),%rax
cmpb   $0x0,0x1(%rsp)
lea    0xd9d(%rip),%rdi
cmovne %rax,%rdi
mov    0x2d76(%rip),%rax
mov    (%rax),%esi
call   1100 <open@plt>
cmp    $0xffffffff,%eax
je     130b <web_service+0x11b>
mov    %eax,%ebx
mov    %r15d,%edi
call   1080 <OK@plt>
lea    0x100(%rsp),%rsi
mov    %ebx,%edi
mov    $0x400,%edx
call   10b0 <read@plt>
test   %eax,%eax
jle    12dc <web_service+0xec>
lea    0x100(%rsp),%rbp
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1090 <send@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   10b0 <read@plt>
test   %eax,%eax
jg     12b0 <web_service+0xc0>
jmp    12dc <web_service+0xec>
mov    %r15d,%edi
call   1110 <BAD_REQUEST@plt>
mov    %r15d,%edi
call   10a0 <close@plt>
xor    %eax,%eax
call   1030 <thread_release@plt>
mov    %r15d,%edi
call   10d0 <conn_delete@plt>
mov    %r14,%rdi
call   1040 <free@plt>
xor    %eax,%eax
add    $0x908,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cc6(%rip),%rax
mov    (%rax),%eax
mov    0x2cad(%rip),%rcx
cmp    (%rcx),%eax
jne    1329 <web_service+0x139>
mov    %r15d,%edi
call   10c0 <NOT_FOUND@plt>
jmp    12dc <web_service+0xec>
mov    %r15d,%edi
call   10f0 <INTERNAL_SERVER_ERROR@plt>
jmp    12dc <web_service+0xec>

LLVM IR (-O0):
@.str = [20 x i8] c"new Thread Started\0A\00"
@.str.1 = [4 x i8] c"GET\00"
@.str.2 = [11 x i8] c"index.html\00"
@O_RDONLY = global i32 0
@errno = global i32 0
@ENOENT = global i32 0
define i8* @web_service(i8* %0) {
%2 = alloca i8*
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [256 x i8]
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [1024 x i8]
store i8* %0, i8** %2
%12 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%13 = load i8*, i8** %2
%14 = bitcast i8* %13 to i32*
%15 = load i32, i32* %14
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%18 = call i32 @recv(i32 %16, i8* %17, i32 1024, i32 0)
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%20 = call i8* @str_comp(i8* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i8* %20, i8** %5
%21 = load i8*, i8** %5
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %26
23: ; preds = %1
%24 = load i32, i32* %4
%25 = call i32 @BAD_REQUEST(i32 %24)
br label %77
26: ; preds = %1
%27 = load i8*, i8** %5
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%29 = call i8* @sep_path(i8* %27, i8* %28)
store i8* %29, i8** %5
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp ne i32 %32, 47
br i1 %33, label %34, label %37
34: ; preds = %26
%35 = load i32, i32* %4
%36 = call i32 @BAD_REQUEST(i32 %35)
br label %77
37: ; preds = %26
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
store i8* %38, i8** %7
%39 = load i8*, i8** %7
%40 = getelementptr inbounds i8, i8* %39, i32 1
store i8* %40, i8** %7
%41 = load i8*, i8** %7
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %37
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8** %7
br label %46
46: ; preds = %45, %37
%47 = load i8*, i8** %7
%48 = load i32, i32* @O_RDONLY
%49 = call i32 @open(i8* %47, i32 %48)
store i32 %49, i32* %8
%50 = load i32, i32* %8
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %63
52: ; preds = %46
%53 = load i32, i32* @errno
store i32 %53, i32* %9
%54 = load i32, i32* %9
%55 = load i32, i32* @ENOENT
%56 = icmp eq i32 %54, %55
br i1 %56, label %57, label %60
57: ; preds = %52
%58 = load i32, i32* %4
%59 = call i32 @NOT_FOUND(i32 %58)
br label %77
60: ; preds = %52
%61 = load i32, i32* %4
%62 = call i32 @INTERNAL_SERVER_ERROR(i32 %61)
br label %77
63: ; preds = %46
%64 = load i32, i32* %4
%65 = call i32 @OK(i32 %64)
br label %66
66: ; preds = %71, %63
%67 = load i32, i32* %8
%68 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%69 = call i32 @read(i32 %67, i8* %68, i32 1024)
store i32 %69, i32* %10
%70 = icmp sgt i32 %69, 0
br i1 %70, label %71, label %76
71: ; preds = %66
%72 = load i32, i32* %4
%73 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%74 = load i32, i32* %10
%75 = call i32 @send(i32 %72, i8* %73, i32 %74, i32 0)
br label %66
76: ; preds = %66
br label %77
77: ; preds = %76, %60, %57, %34, %23
%78 = load i32, i32* %4
%79 = call i32 @close(i32 %78)
%80 = call i32 (...) @thread_release()
%81 = load i32, i32* %4
%82 = call i32 @conn_delete(i32 %81)
%83 = load i8*, i8** %2
%84 = bitcast i8* %83 to i32*
%85 = call i32 @free(i32* %84)
ret i8* null
}

LLVM IR (-O2):
@.str.1 = [4 x i8] c"GET\00"
@.str.2 = [11 x i8] c"index.html\00"
@O_RDONLY = global i32 0
@errno = global i32 0
@ENOENT = global i32 0
@str = [19 x i8] c"new Thread Started\00"
define noalias i8* @web_service(i8* nocapture %0) {
%2 = alloca [1024 x i8]
%3 = alloca [256 x i8]
%4 = alloca [1024 x i8]
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%7 = bitcast i8* %0 to i32*
%8 = load i32, i32* %7
%9 = call i32 @recv(i32 %8, i8* %6, i32 1024, i32 0)
%10 = call i8* @str_comp(i8* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 @BAD_REQUEST(i32 %8)
br label %47
14: ; preds = %1
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%16 = call i8* @sep_path(i8* %10, i8* %15)
%17 = load i8, i8* %15
%18 = icmp eq i8 %17, 47
br i1 %18, label %21, label %19
19: ; preds = %14
%20 = call i32 @BAD_REQUEST(i32 %8)
br label %47
21: ; preds = %14
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 1
%23 = load i8, i8* %22
%24 = icmp eq i8 %23, 0
%25 = select i1 %24, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %22
%26 = load i32, i32* @O_RDONLY
%27 = call i32 @open(i8* %25, i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %37
29: ; preds = %21
%30 = load i32, i32* @errno
%31 = load i32, i32* @ENOENT
%32 = icmp eq i32 %30, %31
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @NOT_FOUND(i32 %8)
br label %47
35: ; preds = %29
%36 = call i32 @INTERNAL_SERVER_ERROR(i32 %8)
br label %47
37: ; preds = %21
%38 = call i32 @OK(i32 %8)
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%40 = call i32 @read(i32 %27, i8* %39, i32 1024)
%41 = icmp sgt i32 %40, 0
br i1 %41, label %42, label %47
42: ; preds = %37, %42
%43 = phi i32 [ %45, %42 ], [ %40, %37 ]
%44 = call i32 @send(i32 %8, i8* %39, i32 %43, i32 0)
%45 = call i32 @read(i32 %27, i8* %39, i32 1024)
%46 = icmp sgt i32 %45, 0
br i1 %46, label %42, label %47
47: ; preds = %42, %37, %33, %35, %19, %12
%48 = call i32 @close(i32 %8)
%49 = call i32 (...) @thread_release()
%50 = call i32 @conn_delete(i32 %8)
%51 = call i32 @free(i32* %7)
ret i8* null
}