#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 {
  int st_mode;
};
typedef int /*<<< orphan*/ req_line;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int S_IFDIR;
int S_IFMT;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ error_handler(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ http_get(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ process_404(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int stat(char *, struct stat *);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int strlen(char *);
int /*<<< orphan*/ *strstr(char *, char *);
int /*<<< orphan*/ strtok(char *, char *);
int /*<<< orphan*/ unknown(int /*<<< orphan*/ *);
void *request_handler(void *arg) {
  int client_sock = *((int *)arg);
  char req_line[100];
  char method[255];
  char path[255];
  char url[255];
  FILE *client_read;
  FILE *client_write;
  client_read = fdopen(client_sock, "r");
  client_write = fdopen(client_sock, "w");
  fgets(req_line, sizeof(req_line), client_read);
  if (strstr(req_line, "HTTP/") == NULL) {
    error_handler(client_write);
    fclose(client_read);
    fclose(client_write);
    return NULL;
  }
  strcpy(method, strtok(req_line, " /"));
  strcpy(url, strtok(NULL, " /"));
  struct stat st;
  sprintf(path, "webapps/%s", url);
  if (path[strlen(path) - 1] == '/')
    strcat(path, "/index.html");
  if (stat(path, &st) == -1) {
    process_404(client_write);
    return NULL;
  }
  if ((st.st_mode & S_IFMT) == S_IFDIR)
    strcat(path, "/index.html");
  if (stat(path, &st) == -1) {
    process_404(client_write);
    return NULL;
  }
  if (strcmp(method, "GET") == 0) {
    http_get(client_write, url);
  } else if (strcmp(method, "POST") == 0) {
  } else {
    unknown(client_write);
  }
  fclose(client_read);
  close(client_sock);
  return NULL;
}


x86 assembly (clang, -O0):
<request_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x3b0,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    0xdde(%rip),%rsi
call   10c0 <fdopen@plt>
mov    %rax,-0x388(%rbp)
mov    -0x14(%rbp),%edi
lea    0xdca(%rip),%rsi
call   10c0 <fdopen@plt>
mov    %rax,-0x390(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x388(%rbp),%rdx
mov    $0x64,%esi
call   1080 <fgets@plt>
lea    -0x80(%rbp),%rdi
lea    0xda0(%rip),%rsi
call   1120 <strstr@plt>
cmp    $0x0,%rax
jne    12a4 <request_handler+0xa4>
mov    -0x390(%rbp),%rdi
call   1060 <error_handler@plt>
mov    -0x388(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x390(%rbp),%rdi
call   1040 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    146a <request_handler+0x26a>
lea    -0x180(%rbp),%rax
mov    %rax,-0x3a8(%rbp)
lea    -0x80(%rbp),%rdi
lea    0xd4d(%rip),%rsi
call   10e0 <strtok@plt>
mov    -0x3a8(%rbp),%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
lea    -0x380(%rbp),%rax
mov    %rax,-0x3a0(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xd21(%rip),%rsi
call   10e0 <strtok@plt>
mov    -0x3a0(%rbp),%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
lea    -0x280(%rbp),%rdi
lea    -0x380(%rbp),%rdx
lea    0xcfc(%rip),%rsi
call   1100 <sprintf@plt>
lea    -0x280(%rbp),%rdi
call   1050 <strlen@plt>
sub    $0x1,%eax
cltq
movsbl -0x280(%rbp,%rax,1),%eax
ff
cmp    $0x2f,%eax
jne    134b <request_handler+0x14b>
lea    -0x280(%rbp),%rdi
lea    0xcd2(%rip),%rsi
call   10f0 <strcat@plt>
lea    -0x280(%rbp),%rdi
lea    -0x398(%rbp),%rsi
call   10b0 <stat@plt>
cmp    $0xffffffff,%eax
jne    1380 <request_handler+0x180>
mov    -0x390(%rbp),%rdi
call   1110 <process_404@plt>
movq   $0x0,-0x8(%rbp)
jmp    146a <request_handler+0x26a>
mov    -0x398(%rbp),%eax
mov    0x2c53(%rip),%rcx
and    (%rcx),%eax
mov    0x2c3a(%rip),%rcx
cmp    (%rcx),%eax
jne    13b1 <request_handler+0x1b1>
lea    -0x280(%rbp),%rdi
lea    0xc6c(%rip),%rsi
call   10f0 <strcat@plt>
lea    -0x280(%rbp),%rdi
lea    -0x398(%rbp),%rsi
call   10b0 <stat@plt>
cmp    $0xffffffff,%eax
jne    13e6 <request_handler+0x1e6>
mov    -0x390(%rbp),%rdi
call   1110 <process_404@plt>
movq   $0x0,-0x8(%rbp)
jmp    146a <request_handler+0x26a>
lea    -0x180(%rbp),%rdi
lea    0xc30(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    141b <request_handler+0x21b>
mov    -0x390(%rbp),%rdi
lea    -0x380(%rbp),%rsi
call   10d0 <http_get@plt>
jmp    144e <request_handler+0x24e>
lea    -0x180(%rbp),%rdi
lea    0xbff(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    143d <request_handler+0x23d>
jmp    1449 <request_handler+0x249>
mov    -0x390(%rbp),%rdi
call   10a0 <unknown@plt>
jmp    144e <request_handler+0x24e>
mov    -0x388(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x3b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<request_handler>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x380,%rsp
mov    (%rdi),%r12d
lea    0xde7(%rip),%rsi
mov    %r12d,%edi
call   10c0 <fdopen@plt>
mov    %rax,%r14
lea    0xdd7(%rip),%rsi
mov    %r12d,%edi
call   10c0 <fdopen@plt>
mov    %rax,%r15
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
mov    %r14,%rdx
call   1080 <fgets@plt>
lea    0xdb2(%rip),%rsi
mov    %rbx,%rdi
call   1120 <strstr@plt>
test   %rax,%rax
je     1381 <request_handler+0x181>
lea    0xda0(%rip),%rbx
lea    0x10(%rsp),%rdi
mov    %rbx,%rsi
call   10e0 <strtok@plt>
lea    0x180(%rsp),%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   10e0 <strtok@plt>
lea    0x280(%rsp),%rbx
mov    %rbx,%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
lea    0xd64(%rip),%rsi
lea    0x80(%rsp),%rbp
mov    %rbp,%rdi
mov    %rbx,%rdx
call   1100 <sprintf@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%rbp,1)
jne    12e1 <request_handler+0xe1>
lea    0xd44(%rip),%rsi
lea    0x80(%rsp),%rdi
call   10f0 <strcat@plt>
lea    0x80(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   10b0 <stat@plt>
cmp    $0xffffffff,%eax
je     1377 <request_handler+0x177>
mov    0x2ce1(%rip),%rax
mov    (%rax),%eax
and    0x8(%rsp),%eax
mov    0x2cc4(%rip),%rcx
cmp    (%rcx),%eax
jne    1324 <request_handler+0x124>
lea    0xd01(%rip),%rsi
lea    0x80(%rsp),%rdi
call   10f0 <strcat@plt>
lea    0x80(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   10b0 <stat@plt>
cmp    $0xffffffff,%eax
je     1377 <request_handler+0x177>
lea    0xce2(%rip),%rsi
lea    0x180(%rsp),%rdi
call   1090 <strcmp@plt>
test   %rax,%rax
je     139b <request_handler+0x19b>
lea    0xccd(%rip),%rsi
lea    0x180(%rsp),%rdi
call   1090 <strcmp@plt>
test   %rax,%rax
je     13ab <request_handler+0x1ab>
mov    %r15,%rdi
call   10a0 <unknown@plt>
jmp    13ab <request_handler+0x1ab>
mov    %r15,%rdi
call   1110 <process_404@plt>
jmp    13bb <request_handler+0x1bb>
mov    %r15,%rdi
call   1060 <error_handler@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
jmp    13bb <request_handler+0x1bb>
lea    0x280(%rsp),%rsi
mov    %r15,%rdi
call   10d0 <http_get@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %r12d,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x380,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [6 x i8] c"HTTP/\00"
@.str.3 = [3 x i8] c" /\00"
@.str.4 = [11 x i8] c"webapps/%s\00"
@.str.5 = [12 x i8] c"/index.html\00"
@S_IFMT = global i32 0
@S_IFDIR = global i32 0
@.str.6 = [4 x i8] c"GET\00"
@.str.7 = [5 x i8] c"POST\00"
define i8* @request_handler(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [100 x i8]
%6 = alloca [255 x i8]
%7 = alloca [255 x i8]
%8 = alloca [255 x i8]
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca %struct.stat
store i8* %0, i8** %3
%12 = load i8*, i8** %3
%13 = bitcast i8* %12 to i32*
%14 = load i32, i32* %13
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = call i32* @fdopen(i32 %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %9
%17 = load i32, i32* %4
%18 = call i32* @fdopen(i32 %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %10
%19 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%20 = load i32*, i32** %9
%21 = call i32 @fgets(i8* %19, i32 100, i32* %20)
%22 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%23 = call i32* @strstr(i8* %22, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %32
25: ; preds = %1
%26 = load i32*, i32** %10
%27 = call i32 @error_handler(i32* %26)
%28 = load i32*, i32** %9
%29 = call i32 @fclose(i32* %28)
%30 = load i32*, i32** %10
%31 = call i32 @fclose(i32* %30)
store i8* null, i8** %2
br label %100
32: ; preds = %1
%33 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%34 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%35 = call i32 @strtok(i8* %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%36 = call i32 @strcpy(i8* %33, i32 %35)
%37 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%38 = call i32 @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%39 = call i32 @strcpy(i8* %37, i32 %38)
%40 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%41 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%42 = call i32 @sprintf(i8* %40, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* %41)
%43 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%44 = call i32 @strlen(i8* %43)
%45 = sub nsw i32 %44, 1
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 %46
%48 = load i8, i8* %47
%49 = sext i8 %48 to i32
%50 = icmp eq i32 %49, 47
br i1 %50, label %51, label %54
51: ; preds = %32
%52 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%53 = call i32 @strcat(i8* %52, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
br label %54
54: ; preds = %51, %32
%55 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%56 = call i32 @stat(i8* %55, %struct.stat* %11)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %61
58: ; preds = %54
%59 = load i32*, i32** %10
%60 = call i32 @process_404(i32* %59)
store i8* null, i8** %2
br label %100
61: ; preds = %54
%62 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%63 = load i32, i32* %62
%64 = load i32, i32* @S_IFMT
%65 = and i32 %63, %64
%66 = load i32, i32* @S_IFDIR
%67 = icmp eq i32 %65, %66
br i1 %67, label %68, label %71
68: ; preds = %61
%69 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%70 = call i32 @strcat(i8* %69, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
br label %71
71: ; preds = %68, %61
%72 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%73 = call i32 @stat(i8* %72, %struct.stat* %11)
%74 = icmp eq i32 %73, -1
br i1 %74, label %75, label %78
75: ; preds = %71
%76 = load i32*, i32** %10
%77 = call i32 @process_404(i32* %76)
store i8* null, i8** %2
br label %100
78: ; preds = %71
%79 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%80 = call i64 @strcmp(i8* %79, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
%81 = icmp eq i64 %80, 0
br i1 %81, label %82, label %86
82: ; preds = %78
%83 = load i32*, i32** %10
%84 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%85 = call i32 @http_get(i32* %83, i8* %84)
br label %95
86: ; preds = %78
%87 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%88 = call i64 @strcmp(i8* %87, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%89 = icmp eq i64 %88, 0
br i1 %89, label %90, label %91
90: ; preds = %86
br label %94
91: ; preds = %86
%92 = load i32*, i32** %10
%93 = call i32 @unknown(i32* %92)
br label %94
94: ; preds = %91, %90
br label %95
95: ; preds = %94, %82
%96 = load i32*, i32** %9
%97 = call i32 @fclose(i32* %96)
%98 = load i32, i32* %4
%99 = call i32 @close(i32 %98)
store i8* null, i8** %2
br label %100
100: ; preds = %95, %75, %58, %25
%101 = load i8*, i8** %2
ret i8* %101
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [6 x i8] c"HTTP/\00"
@.str.3 = [3 x i8] c" /\00"
@.str.4 = [11 x i8] c"webapps/%s\00"
@.str.5 = [12 x i8] c"/index.html\00"
@S_IFMT = global i32 0
@S_IFDIR = global i32 0
@.str.6 = [4 x i8] c"GET\00"
@.str.7 = [5 x i8] c"POST\00"
define noalias i8* @request_handler(i8* nocapture %0) {
%2 = alloca [100 x i8]
%3 = alloca [255 x i8]
%4 = alloca [255 x i8]
%5 = alloca [255 x i8]
%6 = alloca %struct.stat
%7 = bitcast i8* %0 to i32*
%8 = load i32, i32* %7
%9 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%10 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%11 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%12 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 0
%13 = tail call i32* @fdopen(i32 %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32* @fdopen(i32 %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @fgets(i8* %9, i32 100, i32* %13)
%16 = call i32* @strstr(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %22
18: ; preds = %1
%19 = call i32 @error_handler(i32* %14)
%20 = call i32 @fclose(i32* %13)
%21 = call i32 @fclose(i32* %14)
br label %70
22: ; preds = %1
%23 = call i32 @strtok(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%24 = call i32 @strcpy(i8* %10, i32 %23)
%25 = call i32 @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%26 = call i32 @strcpy(i8* %12, i32 %25)
%27 = bitcast %struct.stat* %6 to i8*
%28 = call i32 @sprintf(i8* %11, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* %12)
%29 = call i32 @strlen(i8* %11)
%30 = add nsw i32 %29, -1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 %31
%33 = load i8, i8* %32
%34 = icmp eq i8 %33, 47
br i1 %34, label %35, label %37
35: ; preds = %22
%36 = call i32 @strcat(i8* %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
br label %37
37: ; preds = %35, %22
%38 = call i32 @stat(i8* %11, %struct.stat* %6)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @process_404(i32* %14)
br label %69
42: ; preds = %37
%43 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 0
%44 = load i32, i32* %43
%45 = load i32, i32* @S_IFMT
%46 = and i32 %45, %44
%47 = load i32, i32* @S_IFDIR
%48 = icmp eq i32 %46, %47
br i1 %48, label %49, label %51
49: ; preds = %42
%50 = call i32 @strcat(i8* %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
br label %51
51: ; preds = %49, %42
%52 = call i32 @stat(i8* %11, %struct.stat* %6)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %56
54: ; preds = %51
%55 = call i32 @process_404(i32* %14)
br label %69
56: ; preds = %51
%57 = call i64 @strcmp(i8* %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
%58 = icmp eq i64 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %56
%60 = call i32 @http_get(i32* %14, i8* %12)
br label %66
61: ; preds = %56
%62 = call i64 @strcmp(i8* %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%63 = icmp eq i64 %62, 0
br i1 %63, label %66, label %64
64: ; preds = %61
%65 = call i32 @unknown(i32* %14)
br label %66
66: ; preds = %64, %61, %59
%67 = call i32 @fclose(i32* %13)
%68 = call i32 @close(i32 %8)
br label %69
69: ; preds = %66, %54, %40
br label %70
70: ; preds = %69, %18
ret i8* null
}