#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*/ error(int);
char *strchr(char const *, char);
scalar_t__ strncmp(char const *, char *, int);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
bool parse(const char *line, char *abs_path, char *query) {
  if (strncmp(line, "GET ", 4) != 0) {
    error(405);
    return false;
  }
  if (strncmp(line + 4, "/", 1) != 0) {
    error(501);
    return false;
  }
  if (strchr(line, '"') != NULL) {
    error(400);
    return false;
  }
  char *next_space = strchr(line + 4, ' ');
  if (strncmp("HTTP/1.1", next_space + 1, 8) != 0) {
    error(505);
    return false;
  }
  size_t abs_path_len;
  char *question_mark;
  if ((question_mark = strchr(line + 4, '?')) != NULL) {
    abs_path_len = question_mark - (line + 4);
    size_t query_len = next_space - question_mark - 1;
    strncpy(query, question_mark + 1, query_len);
  } else {
    abs_path_len = next_space - (line + 4);
    query[0] = '\0';
  }
  strncpy(abs_path, line + 4, abs_path_len);
  abs_path[abs_path_len] = '\0';
  return true;
}


x86 assembly (clang, -O0):
<parse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea1(%rip),%rsi
mov    $0x4,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
je     1189 <parse+0x49>
mov    $0x195,%edi
call   1060 <error@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e1 <parse+0x1a1>
mov    -0x10(%rbp),%rdi
add    $0x4,%rdi
lea    0xe6d(%rip),%rsi
mov    $0x1,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
je     11c2 <parse+0x82>
mov    $0x1f5,%edi
call   1060 <error@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e1 <parse+0x1a1>
mov    -0x10(%rbp),%rdi
mov    $0x22,%esi
call   1050 <strchr@plt>
cmp    $0x0,%rax
je     11f0 <parse+0xb0>
mov    $0x190,%edi
call   1060 <error@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e1 <parse+0x1a1>
mov    -0x10(%rbp),%rdi
add    $0x4,%rdi
mov    $0x20,%esi
call   1050 <strchr@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
add    $0x1,%rsi
lea    0xdf2(%rip),%rdi
mov    $0x8,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
je     123f <parse+0xff>
mov    $0x1f9,%edi
call   1060 <error@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e1 <parse+0x1a1>
mov    -0x10(%rbp),%rdi
add    $0x4,%rdi
mov    $0x3f,%esi
call   1050 <strchr@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     129f <parse+0x15f>
mov    -0x38(%rbp),%rax
mov    -0x10(%rbp),%rcx
add    $0x4,%rcx
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x38(%rbp),%rcx
sub    %rcx,%rax
sub    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rsi
add    $0x1,%rsi
mov    -0x40(%rbp),%rdx
call   1030 <strncpy@plt>
jmp    12b9 <parse+0x179>
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
add    $0x4,%rcx
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x4,%rsi
mov    -0x30(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r13
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xea7(%rip),%rsi
mov    $0x4,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     116f <parse+0x2f>
mov    $0x195,%edi
jmp    11db <parse+0x9b>
lea    0x4(%rbx),%r15
lea    0xe8b(%rip),%rsi
mov    %r15,%rdi
mov    $0x1,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     1193 <parse+0x53>
mov    $0x1f5,%edi
jmp    11db <parse+0x9b>
mov    %rbx,%rdi
mov    $0x22,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11ac <parse+0x6c>
mov    $0x190,%edi
jmp    11db <parse+0x9b>
mov    %r15,%rdi
mov    $0x20,%esi
call   1050 <strchr@plt>
mov    %rax,%r12
lea    0x1(%rax),%rsi
lea    0xe40(%rip),%rdi
mov    $0x8,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     11ec <parse+0xac>
mov    $0x1f9,%edi
call   1060 <error@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    %r15,%rdi
mov    $0x3f,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     121e <parse+0xde>
mov    %rax,%rbx
sub    %r15,%rbx
mov    %rax,%rdx
not    %rdx
add    %r12,%rdx
add    $0x1,%rax
mov    %r13,%rdi
mov    %rax,%rsi
call   1030 <strncpy@plt>
jmp    1229 <parse+0xe9>
sub    %r15,%r12
movb   $0x0,0x0(%r13)
mov    %r12,%rbx
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1030 <strncpy@plt>
movb   $0x0,(%r14,%rbx,1)
mov    $0x1,%eax
jmp    11e2 <parse+0xa2>

LLVM IR (-O0):
@.str = [5 x i8] c"GET \00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [9 x i8] c"HTTP/1.1\00"
define i32 @parse(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%12 = load i8*, i8** %5
%13 = call i64 @strncmp(i8* %12, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = call i32 @error(i32 405)
store i32 0, i32* %4
br label %81
17: ; preds = %3
%18 = load i8*, i8** %5
%19 = getelementptr inbounds i8, i8* %18, i64 4
%20 = call i64 @strncmp(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = call i32 @error(i32 501)
store i32 0, i32* %4
br label %81
24: ; preds = %17
%25 = load i8*, i8** %5
%26 = call i8* @strchr(i8* %25, i8 signext 34)
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 @error(i32 400)
store i32 0, i32* %4
br label %81
30: ; preds = %24
%31 = load i8*, i8** %5
%32 = getelementptr inbounds i8, i8* %31, i64 4
%33 = call i8* @strchr(i8* %32, i8 signext 32)
store i8* %33, i8** %8
%34 = load i8*, i8** %8
%35 = getelementptr inbounds i8, i8* %34, i64 1
%36 = call i64 @strncmp(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %35, i32 8)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %30
%39 = call i32 @error(i32 505)
store i32 0, i32* %4
br label %81
40: ; preds = %30
%41 = load i8*, i8** %5
%42 = getelementptr inbounds i8, i8* %41, i64 4
%43 = call i8* @strchr(i8* %42, i8 signext 63)
store i8* %43, i8** %10
%44 = icmp ne i8* %43, null
br i1 %44, label %45, label %63
45: ; preds = %40
%46 = load i8*, i8** %10
%47 = load i8*, i8** %5
%48 = getelementptr inbounds i8, i8* %47, i64 4
%49 = ptrtoint i8* %46 to i64
%50 = ptrtoint i8* %48 to i64
%51 = sub i64 %49, %50
store i64 %51, i64* %9
%52 = load i8*, i8** %8
%53 = load i8*, i8** %10
%54 = ptrtoint i8* %52 to i64
%55 = ptrtoint i8* %53 to i64
%56 = sub i64 %54, %55
%57 = sub nsw i64 %56, 1
store i64 %57, i64* %11
%58 = load i8*, i8** %7
%59 = load i8*, i8** %10
%60 = getelementptr inbounds i8, i8* %59, i64 1
%61 = load i64, i64* %11
%62 = call i32 @strncpy(i8* %58, i8* %60, i64 %61)
br label %72
63: ; preds = %40
%64 = load i8*, i8** %8
%65 = load i8*, i8** %5
%66 = getelementptr inbounds i8, i8* %65, i64 4
%67 = ptrtoint i8* %64 to i64
%68 = ptrtoint i8* %66 to i64
%69 = sub i64 %67, %68
store i64 %69, i64* %9
%70 = load i8*, i8** %7
%71 = getelementptr inbounds i8, i8* %70, i64 0
store i8 0, i8* %71
br label %72
72: ; preds = %63, %45
%73 = load i8*, i8** %6
%74 = load i8*, i8** %5
%75 = getelementptr inbounds i8, i8* %74, i64 4
%76 = load i64, i64* %9
%77 = call i32 @strncpy(i8* %73, i8* %75, i64 %76)
%78 = load i8*, i8** %6
%79 = load i64, i64* %9
%80 = getelementptr inbounds i8, i8* %78, i64 %79
store i8 0, i8* %80
store i32 1, i32* %4
br label %81
81: ; preds = %72, %38, %28, %22, %15
%82 = load i32, i32* %4
ret i32 %82
}

LLVM IR (-O2):
@.str = [5 x i8] c"GET \00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [9 x i8] c"HTTP/1.1\00"
define i32 @parse(i8* %0, i8* %1, i8* %2) {
%4 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = tail call i32 @error(i32 405)
br label %46
8: ; preds = %3
%9 = getelementptr inbounds i8, i8* %0, i64 4
%10 = tail call i64 @strncmp(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%11 = icmp eq i64 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %8
%13 = tail call i32 @error(i32 501)
br label %46
14: ; preds = %8
%15 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 34)
%16 = icmp eq i8* %15, null
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = tail call i32 @error(i32 400)
br label %46
19: ; preds = %14
%20 = tail call i8* @strchr(i8* %9, i8 signext 32)
%21 = getelementptr inbounds i8, i8* %20, i64 1
%22 = tail call i64 @strncmp(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %21, i32 8)
%23 = icmp eq i64 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %19
%25 = tail call i32 @error(i32 505)
br label %46
26: ; preds = %19
%27 = tail call i8* @strchr(i8* %9, i8 signext 63)
%28 = icmp eq i8* %27, null
br i1 %28, label %38, label %29
29: ; preds = %26
%30 = ptrtoint i8* %27 to i64
%31 = ptrtoint i8* %9 to i64
%32 = sub i64 %30, %31
%33 = ptrtoint i8* %20 to i64
%34 = xor i64 %30, -1
%35 = add i64 %34, %33
%36 = getelementptr inbounds i8, i8* %27, i64 1
%37 = tail call i32 @strncpy(i8* %2, i8* %36, i64 %35)
br label %42
38: ; preds = %26
%39 = ptrtoint i8* %20 to i64
%40 = ptrtoint i8* %9 to i64
%41 = sub i64 %39, %40
store i8 0, i8* %2
br label %42
42: ; preds = %38, %29
%43 = phi i64 [ %32, %29 ], [ %41, %38 ]
%44 = tail call i32 @strncpy(i8* %1, i8* %9, i64 %43)
%45 = getelementptr inbounds i8, i8* %1, i64 %43
store i8 0, i8* %45
br label %46
46: ; preds = %24, %42, %17, %12, %6
%47 = phi i32 [ 0, %6 ], [ 0, %12 ], [ 0, %17 ], [ 0, %24 ], [ 1, %42 ]
ret i32 %47
}