#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*/ SEEK_CUR;
char *calloc(size_t, int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ log_info(char *, char *);
int /*<<< orphan*/ memmove(char *, char *, size_t);
int /*<<< orphan*/ process_get_request(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ send_response(int /*<<< orphan*/ *, int, char *,
                                 int /*<<< orphan*/ *, char *);
scalar_t__ strcasecmp(char *, char *);
int /*<<< orphan*/ strcat(char *, char *);
size_t strlen(char *);
char *strtok(char *, char *);
int process_request(FILE *f, char *root) {
  char buf[1024];
  char *method;
  char *relative_path;
  char *protocol;
  if (!fgets(buf, sizeof(buf), f))
    return -1;
  log_info("%s", buf);
  method = strtok(buf, " ");
  relative_path = strtok(NULL, " ");
  protocol = strtok(NULL, "\r");
  if (!method || !relative_path || !protocol)
    return -1;
  char *path = calloc(strlen(root) + strlen(relative_path) + 1, sizeof(char));
  memmove(path, root, strlen(root));
  path[strlen(root) + 1] = '\0';
  strcat(path, relative_path);
  fseek(f, 0, SEEK_CUR);
  if (strcasecmp(method, "GET") == 0) {
    process_get_request(f, path, relative_path);
  } else {
    send_response(f, 501, "Not supported", NULL, "Method is not supported");
  }
  free(path);
  return 0;
}


x86 assembly (clang, -O0):
<process_request>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x400,%esi
call   1090 <fgets@plt>
cmp    $0x0,%eax
jne    11fd <process_request+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ba <process_request+0x1fa>
lea    -0x420(%rbp),%rsi
lea    0xdf5(%rip),%rdi
call   1060 <log_info@plt>
lea    -0x420(%rbp),%rdi
lea    0xde5(%rip),%rsi
call   10d0 <strtok@plt>
mov    %rax,-0x428(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xdce(%rip),%rsi
call   10d0 <strtok@plt>
mov    %rax,-0x430(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xdb9(%rip),%rsi
call   10d0 <strtok@plt>
mov    %rax,-0x438(%rbp)
cmpq   $0x0,-0x428(%rbp)
je     1282 <process_request+0xc2>
cmpq   $0x0,-0x430(%rbp)
je     1282 <process_request+0xc2>
cmpq   $0x0,-0x438(%rbp)
jne    128e <process_request+0xce>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ba <process_request+0x1fa>
mov    -0x18(%rbp),%rdi
call   1080 <strlen@plt>
mov    %rax,-0x460(%rbp)
mov    -0x430(%rbp),%rdi
call   1080 <strlen@plt>
mov    -0x460(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   10a0 <calloc@plt>
mov    %rax,-0x440(%rbp)
mov    -0x440(%rbp),%rax
mov    %rax,-0x458(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x450(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <strlen@plt>
mov    -0x458(%rbp),%rdi
mov    -0x450(%rbp),%rsi
mov    %rax,%rdx
call   10c0 <memmove@plt>
mov    -0x440(%rbp),%rax
mov    %rax,-0x448(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <strlen@plt>
mov    %rax,%rcx
mov    -0x448(%rbp),%rax
movb   $0x0,0x1(%rax,%rcx,1)
mov    -0x440(%rbp),%rdi
mov    -0x430(%rbp),%rsi
call   10e0 <strcat@plt>
mov    -0x10(%rbp),%rdi
mov    0x2ca3(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    -0x428(%rbp),%rdi
lea    0xcab(%rip),%rsi
call   1050 <strcasecmp@plt>
cmp    $0x0,%rax
jne    1387 <process_request+0x1c7>
mov    -0x10(%rbp),%rdi
mov    -0x440(%rbp),%rsi
mov    -0x430(%rbp),%rdx
call   1030 <process_get_request@plt>
jmp    13a7 <process_request+0x1e7>
mov    -0x10(%rbp),%rdi
mov    $0x1f5,%esi
lea    0xc74(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
lea    0xc77(%rip),%r8
call   1070 <send_response@plt>
mov    -0x440(%rbp),%rdi
call   1040 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x460,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<process_request>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%r12
mov    %rdi,%r14
mov    %rsp,%rdi
mov    $0x400,%esi
mov    %r14,%rdx
call   1090 <fgets@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
je     1306 <process_request+0x146>
lea    0xe05(%rip),%rdi
mov    %rsp,%rbx
mov    %rbx,%rsi
call   1060 <log_info@plt>
lea    0xdf6(%rip),%r15
mov    %rbx,%rdi
mov    %r15,%rsi
call   10d0 <strtok@plt>
mov    %rax,%rbx
xor    %edi,%edi
mov    %r15,%rsi
call   10d0 <strtok@plt>
mov    %rax,%r15
lea    0xdd6(%rip),%rsi
xor    %edi,%edi
call   10d0 <strtok@plt>
test   %rbx,%rbx
je     1306 <process_request+0x146>
test   %r15,%r15
je     1306 <process_request+0x146>
test   %rax,%rax
je     1306 <process_request+0x146>
mov    %r12,%rdi
call   1080 <strlen@plt>
mov    %rax,%rbp
mov    %r15,%rdi
call   1080 <strlen@plt>
lea    (%rax,%rbp,1),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   10a0 <calloc@plt>
mov    %rax,%r13
mov    %r12,%rdi
call   1080 <strlen@plt>
mov    %r13,%rdi
mov    %r12,%rsi
mov    %rax,%rdx
call   10c0 <memmove@plt>
mov    %r12,%rdi
call   1080 <strlen@plt>
movb   $0x0,0x1(%rax,%r13,1)
mov    %r13,%rdi
mov    %r15,%rsi
call   10e0 <strcat@plt>
mov    0x2d39(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   10b0 <fseek@plt>
lea    0xd45(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcasecmp@plt>
test   %rax,%rax
je     12ee <process_request+0x12e>
lea    0xd35(%rip),%rdx
lea    0xd3c(%rip),%r8
mov    %r14,%rdi
mov    $0x1f5,%esi
xor    %ecx,%ecx
call   1070 <send_response@plt>
jmp    12fc <process_request+0x13c>
mov    %r14,%rdi
mov    %r13,%rsi
mov    %r15,%rdx
call   1030 <process_get_request@plt>
mov    %r13,%rdi
call   1040 <free@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@.str.1 = [2 x i8] c" \00"
@.str.2 = [2 x i8] c"\0D\00"
@SEEK_CUR = global i32 0
@.str.3 = [4 x i8] c"GET\00"
@.str.4 = [14 x i8] c"Not supported\00"
@.str.5 = [24 x i8] c"Method is not supported\00"
define i32 @process_request(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca [1024 x i8]
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
store i32* %0, i32** %4
store i8* %1, i8** %5
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%12 = load i32*, i32** %4
%13 = call i32 @fgets(i8* %11, i32 1024, i32* %12)
%14 = icmp ne i32 %13, 0
br i1 %14, label %16, label %15
15: ; preds = %2
store i32 -1, i32* %3
br label %70
16: ; preds = %2
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%18 = call i32 @log_info(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%20 = call i8* @strtok(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %20, i8** %7
%21 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %21, i8** %8
%22 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %22, i8** %9
%23 = load i8*, i8** %7
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %31
25: ; preds = %16
%26 = load i8*, i8** %8
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i8*, i8** %9
%30 = icmp ne i8* %29, null
br i1 %30, label %32, label %31
31: ; preds = %28, %25, %16
store i32 -1, i32* %3
br label %70
32: ; preds = %28
%33 = load i8*, i8** %5
%34 = call i64 @strlen(i8* %33)
%35 = load i8*, i8** %8
%36 = call i64 @strlen(i8* %35)
%37 = add i64 %34, %36
%38 = add i64 %37, 1
%39 = call i8* @calloc(i64 %38, i32 1)
store i8* %39, i8** %10
%40 = load i8*, i8** %10
%41 = load i8*, i8** %5
%42 = load i8*, i8** %5
%43 = call i64 @strlen(i8* %42)
%44 = call i32 @memmove(i8* %40, i8* %41, i64 %43)
%45 = load i8*, i8** %10
%46 = load i8*, i8** %5
%47 = call i64 @strlen(i8* %46)
%48 = add i64 %47, 1
%49 = getelementptr inbounds i8, i8* %45, i64 %48
store i8 0, i8* %49
%50 = load i8*, i8** %10
%51 = load i8*, i8** %8
%52 = call i32 @strcat(i8* %50, i8* %51)
%53 = load i32*, i32** %4
%54 = load i32, i32* @SEEK_CUR
%55 = call i32 @fseek(i32* %53, i32 0, i32 %54)
%56 = load i8*, i8** %7
%57 = call i64 @strcasecmp(i8* %56, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%58 = icmp eq i64 %57, 0
br i1 %58, label %59, label %64
59: ; preds = %32
%60 = load i32*, i32** %4
%61 = load i8*, i8** %10
%62 = load i8*, i8** %8
%63 = call i32 @process_get_request(i32* %60, i8* %61, i8* %62)
br label %67
64: ; preds = %32
%65 = load i32*, i32** %4
%66 = call i32 @send_response(i32* %65, i32 501, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32* null, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
br label %67
67: ; preds = %64, %59
%68 = load i8*, i8** %10
%69 = call i32 @free(i8* %68)
store i32 0, i32* %3
br label %70
70: ; preds = %67, %31, %15
%71 = load i32, i32* %3
ret i32 %71
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@.str.1 = [2 x i8] c" \00"
@.str.2 = [2 x i8] c"\0D\00"
@SEEK_CUR = global i32 0
@.str.3 = [4 x i8] c"GET\00"
@.str.4 = [14 x i8] c"Not supported\00"
@.str.5 = [24 x i8] c"Method is not supported\00"
define i32 @process_request(i32* %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = call i32 @fgets(i8* %4, i32 1024, i32* %0)
%6 = icmp eq i32 %5, 0
br i1 %6, label %39, label %7
7: ; preds = %2
%8 = call i32 @log_info(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %4)
%9 = call i8* @strtok(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%12 = icmp ne i8* %9, null
%13 = icmp ne i8* %10, null
%14 = select i1 %12, i1 %13, i1 false
%15 = icmp ne i8* %11, null
%16 = select i1 %14, i1 %15, i1 false
br i1 %16, label %17, label %39
17: ; preds = %7
%18 = call i64 @strlen(i8* dereferenceable(1) %1)
%19 = call i64 @strlen(i8* %10)
%20 = add i64 %18, 1
%21 = add i64 %20, %19
%22 = call i8* @calloc(i64 %21, i32 1)
%23 = call i64 @strlen(i8* dereferenceable(1) %1)
%24 = call i32 @memmove(i8* %22, i8* %1, i64 %23)
%25 = call i64 @strlen(i8* dereferenceable(1) %1)
%26 = add i64 %25, 1
%27 = getelementptr inbounds i8, i8* %22, i64 %26
store i8 0, i8* %27
%28 = call i32 @strcat(i8* %22, i8* %10)
%29 = load i32, i32* @SEEK_CUR
%30 = call i32 @fseek(i32* %0, i32 0, i32 %29)
%31 = call i64 @strcasecmp(i8* %9, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %17
%34 = call i32 @process_get_request(i32* %0, i8* %22, i8* %10)
br label %37
35: ; preds = %17
%36 = call i32 @send_response(i32* %0, i32 501, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32* null, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
br label %37
37: ; preds = %35, %33
%38 = call i32 @free(i8* %22)
br label %39
39: ; preds = %7, %2, %37
%40 = phi i32 [ 0, %37 ], [ -1, %2 ], [ -1, %7 ]
ret i32 %40
}