#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*/ printf(char *);
int /*<<< orphan*/ sscanf(char const *, char *, char *, char *, char *);
scalar_t__ strcasecmp(char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *strtok(char *, char const *);
bool parse(const char *line, char *abs_path, char *query) {
  char method[20], request_target[100], HTTP_version[20];
  char *absolute_path, *query_string;
  const char question_mark = '?';
  if (line == NULL) {
    return false;
  }
  sscanf(line, "%s %s %s", method, request_target, HTTP_version);
  if (request_target[0] != '/') {
    printf("501 Not Implemented\n");
    return false;
  }
  if (strcasecmp(method, "get") != 0) {
    printf("405 Method Not Allowed\n");
    return false;
  }
  if (strcasecmp(HTTP_version, "HTTP/1.1") != 0) {
    printf("505 HTTP Version Not Supported\n");
    return false;
  }
  if (strlen(request_target) > 1) {
    absolute_path = strtok(request_target, &question_mark);
    query_string = strtok(NULL, &question_mark);
    if (query_string == NULL) {
      query_string = "";
    } else if (strcmp(query_string, "q") == 0) {
      printf("400 Bad Request\n");
      return false;
    }
    strcpy(abs_path, absolute_path);
    strcpy(query, query_string);
    return true;
  } else {
    strcpy(abs_path, "/");
    return true;
  }
}


x86 assembly (clang, -O0):
<parse>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movb   $0x3f,-0xe1(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a5 <parse+0x35>
movl   $0x0,-0x4(%rbp)
jmp    133a <parse+0x1ca>
mov    -0x10(%rbp),%rdi
lea    -0x40(%rbp),%rdx
lea    -0xb0(%rbp),%rcx
lea    -0xd0(%rbp),%r8
lea    0xe3e(%rip),%rsi
call   1080 <sscanf@plt>
movsbl -0xb0(%rbp),%eax
cmp    $0x2f,%eax
je     11ef <parse+0x7f>
lea    0xe2b(%rip),%rdi
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    133a <parse+0x1ca>
lea    -0x40(%rbp),%rdi
lea    0xe24(%rip),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%rax
je     1221 <parse+0xb1>
lea    0xe12(%rip),%rdi
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    133a <parse+0x1ca>
lea    -0xd0(%rbp),%rdi
lea    0xe0b(%rip),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%rax
je     1256 <parse+0xe6>
lea    0xdfe(%rip),%rdi
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    133a <parse+0x1ca>
lea    -0xb0(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x1,%eax
jle    1323 <parse+0x1b3>
lea    -0xb0(%rbp),%rdi
lea    -0xe1(%rbp),%rsi
call   1090 <strtok@plt>
mov    %rax,-0xd8(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    -0xe1(%rbp),%rsi
call   1090 <strtok@plt>
mov    %rax,-0xe0(%rbp)
cmpq   $0x0,-0xe0(%rbp)
jne    12bd <parse+0x14d>
lea    0xd6c(%rip),%rax
mov    %rax,-0xe0(%rbp)
jmp    12f7 <parse+0x187>
mov    -0xe0(%rbp),%rdi
lea    0xd98(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    12f2 <parse+0x182>
lea    0xd84(%rip),%rdi
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    133a <parse+0x1ca>
jmp    12f7 <parse+0x187>
mov    -0x18(%rbp),%rdi
mov    -0xd8(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0xe0(%rbp),%rsi
call   1040 <strcpy@plt>
movl   $0x1,-0x4(%rbp)
jmp    133a <parse+0x1ca>
mov    -0x18(%rbp),%rdi
lea    0xd48(%rip),%rsi
call   1040 <strcpy@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0xb8,%rsp
movb   $0x3f,0xf(%rsp)
xor    %ebp,%ebp
test   %rdi,%rdi
je     11dc <parse+0x6c>
mov    %rdx,%r14
mov    %rsi,%rbx
lea    0xe6a(%rip),%rsi
lea    0x30(%rsp),%rdx
lea    0x50(%rsp),%rcx
lea    0x10(%rsp),%r8
call   1080 <sscanf@plt>
cmpb   $0x2f,0x50(%rsp)
jne    11d0 <parse+0x60>
lea    0xe51(%rip),%rsi
lea    0x30(%rsp),%rdi
call   1030 <strcasecmp@plt>
test   %rax,%rax
je     11ec <parse+0x7c>
lea    0xe7b(%rip),%rdi
jmp    11d7 <parse+0x67>
lea    0xe89(%rip),%rdi
call   1050 <puts@plt>
mov    %ebp,%eax
add    $0xb8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe1a(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1030 <strcasecmp@plt>
test   %rax,%rax
je     120b <parse+0x9b>
lea    0xe21(%rip),%rdi
jmp    11d7 <parse+0x67>
lea    0x50(%rsp),%rdi
call   1060 <strlen@plt>
cmp    $0x2,%eax
jl     1268 <parse+0xf8>
lea    0x50(%rsp),%rdi
lea    0xf(%rsp),%rbp
mov    %rbp,%rsi
call   1090 <strtok@plt>
mov    %rax,%r15
xor    %edi,%edi
mov    %rbp,%rsi
call   1090 <strtok@plt>
test   %rax,%rax
je     1274 <parse+0x104>
mov    %rax,%rbp
lea    0xdce(%rip),%rsi
mov    %rax,%rdi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    127b <parse+0x10b>
lea    0xdbe(%rip),%rdi
call   1050 <puts@plt>
xor    %ebp,%ebp
jmp    11dc <parse+0x6c>
lea    0xda9(%rip),%rsi
mov    %rbx,%rdi
jmp    128c <parse+0x11c>
lea    0xd9e(%rip),%rbp
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
mov    %r14,%rdi
mov    %rbp,%rsi
call   1040 <strcpy@plt>
mov    $0x1,%ebp
jmp    11dc <parse+0x6c>

LLVM IR (-O0):
@.str = [9 x i8] c"%s %s %s\00"
@.str.1 = [21 x i8] c"501 Not Implemented\0A\00"
@.str.2 = [4 x i8] c"get\00"
@.str.3 = [24 x i8] c"405 Method Not Allowed\0A\00"
@.str.4 = [9 x i8] c"HTTP/1.1\00"
@.str.5 = [32 x i8] c"505 HTTP Version Not Supported\0A\00"
@.str.6 = [1 x i8] zeroinitializer
@.str.7 = [2 x i8] c"q\00"
@.str.8 = [17 x i8] c"400 Bad Request\0A\00"
@.str.9 = [2 x i8] c"/\00"
define i32 @parse(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca [20 x i8]
%9 = alloca [100 x i8]
%10 = alloca [20 x i8]
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i8 63, i8* %13
%14 = load i8*, i8** %5
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %17
16: ; preds = %3
store i32 0, i32* %4
br label %69
17: ; preds = %3
%18 = load i8*, i8** %5
%19 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%20 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%21 = getelementptr inbounds [20 x i8], [20 x i8]* %10, i64 0, i64 0
%22 = call i32 @sscanf(i8* %18, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %19, i8* %20, i8* %21)
%23 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp ne i32 %25, 47
br i1 %26, label %27, label %29
27: ; preds = %17
%28 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %4
br label %69
29: ; preds = %17
%30 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%31 = call i64 @strcasecmp(i8* %30, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %4
br label %69
35: ; preds = %29
%36 = getelementptr inbounds [20 x i8], [20 x i8]* %10, i64 0, i64 0
%37 = call i64 @strcasecmp(i8* %36, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %4
br label %69
41: ; preds = %35
%42 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%43 = call i32 @strlen(i8* %42)
%44 = icmp sgt i32 %43, 1
br i1 %44, label %45, label %66
45: ; preds = %41
%46 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%47 = call i8* @strtok(i8* %46, i8* %13)
store i8* %47, i8** %11
%48 = call i8* @strtok(i8* null, i8* %13)
store i8* %48, i8** %12
%49 = load i8*, i8** %12
%50 = icmp eq i8* %49, null
br i1 %50, label %51, label %52
51: ; preds = %45
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.6, i64 0, i64 0), i8** %12
br label %59
52: ; preds = %45
%53 = load i8*, i8** %12
%54 = call i64 @strcmp(i8* %53, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %52
%57 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0))
store i32 0, i32* %4
br label %69
58: ; preds = %52
br label %59
59: ; preds = %58, %51
%60 = load i8*, i8** %6
%61 = load i8*, i8** %11
%62 = call i32 @strcpy(i8* %60, i8* %61)
%63 = load i8*, i8** %7
%64 = load i8*, i8** %12
%65 = call i32 @strcpy(i8* %63, i8* %64)
store i32 1, i32* %4
br label %69
66: ; preds = %41
%67 = load i8*, i8** %6
%68 = call i32 @strcpy(i8* %67, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
store i32 1, i32* %4
br label %69
69: ; preds = %66, %59, %56, %39, %33, %27, %16
%70 = load i32, i32* %4
ret i32 %70
}

LLVM IR (-O2):
@.str = [9 x i8] c"%s %s %s\00"
@.str.2 = [4 x i8] c"get\00"
@.str.4 = [9 x i8] c"HTTP/1.1\00"
@.str.6 = [1 x i8] zeroinitializer
@.str.7 = [2 x i8] c"q\00"
@.str.9 = [2 x i8] c"/\00"
@str = [16 x i8] c"400 Bad Request\00"
@str.10 = [31 x i8] c"505 HTTP Version Not Supported\00"
@str.11 = [23 x i8] c"405 Method Not Allowed\00"
@str.12 = [20 x i8] c"501 Not Implemented\00"
define i32 @parse(i8* %0, i8* %1, i8* %2) {
%4 = alloca [20 x i8]
%5 = alloca [100 x i8]
%6 = alloca [20 x i8]
%7 = alloca i8
%8 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%10 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
store i8 63, i8* %7
%11 = icmp eq i8* %0, null
br i1 %11, label %46, label %12
12: ; preds = %3
%13 = call i32 @sscanf(i8* %0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %8, i8* %9, i8* %10)
%14 = load i8, i8* %9
%15 = icmp eq i8 %14, 47
br i1 %15, label %18, label %16
16: ; preds = %12
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.12, i64 0, i64 0))
br label %46
18: ; preds = %12
%19 = call i64 @strcasecmp(i8* %8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.11, i64 0, i64 0))
br label %46
23: ; preds = %18
%24 = call i64 @strcasecmp(i8* %10, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.10, i64 0, i64 0))
br label %46
28: ; preds = %23
%29 = call i32 @strlen(i8* %9)
%30 = icmp sgt i32 %29, 1
br i1 %30, label %31, label %44
31: ; preds = %28
%32 = call i8* @strtok(i8* %9, i8* %7)
%33 = call i8* @strtok(i8* null, i8* %7)
%34 = icmp eq i8* %33, null
br i1 %34, label %40, label %35
35: ; preds = %31
%36 = call i64 @strcmp(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %46
40: ; preds = %31, %35
%41 = phi i8* [ %33, %35 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.6, i64 0, i64 0), %31 ]
%42 = call i32 @strcpy(i8* %1, i8* %32)
%43 = call i32 @strcpy(i8* %2, i8* %41)
br label %46
44: ; preds = %28
%45 = call i32 @strcpy(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
br label %46
46: ; preds = %3, %44, %40, %38, %26, %21, %16
%47 = phi i32 [ 0, %16 ], [ 0, %21 ], [ 0, %26 ], [ 1, %40 ], [ 0, %38 ], [ 1, %44 ], [ 0, %3 ]
ret i32 %47
}