#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*/ memcpy(char *, char *, int);
int sscanf(char *, char *, char *, char *, char *);
char *strchr(char *, char);
int strlen(char *);
int parse_request_head(char *buffer, char *method, char *url, char *protocol) {
  char tmp_buf[8192] = {0};
  char *end_of_line;
  memcpy(tmp_buf, buffer, 8192 - 1);
  end_of_line = strchr(tmp_buf, '\n');
  if (end_of_line == NULL)
    return -1;
  *end_of_line = '\0';
  if (sscanf(tmp_buf, "%[^ ] %[^ ] %[^ ]", method, url, protocol) != 3)
    return -1;
  int pro_len = strlen(protocol);
  for (int i = 0; i < pro_len; i++) {
    if (protocol[i] == '\r' || protocol[i] == '\n') {
      protocol[i] = '\0';
      break;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<parse_request_head>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
lea    -0x2030(%rbp),%rdi
xor    %esi,%esi
mov    $0x2000,%edx
call   1050 <memset@plt>
lea    -0x2030(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x1fff,%edx
call   1060 <memcpy@plt>
lea    -0x2030(%rbp),%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
mov    %rax,-0x2038(%rbp)
cmpq   $0x0,-0x2038(%rbp)
jne    11c5 <parse_request_head+0x75>
movl   $0xffffffff,-0x4(%rbp)
jmp    1292 <parse_request_head+0x142>
mov    -0x2038(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x2030(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
lea    0xe17(%rip),%rsi
call   1070 <sscanf@plt>
cmp    $0x3,%eax
je     1203 <parse_request_head+0xb3>
movl   $0xffffffff,-0x4(%rbp)
jmp    1292 <parse_request_head+0x142>
mov    -0x28(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x203c(%rbp)
movl   $0x0,-0x2040(%rbp)
mov    -0x2040(%rbp),%eax
cmp    -0x203c(%rbp),%eax
jge    128b <parse_request_head+0x13b>
mov    -0x28(%rbp),%rax
movslq -0x2040(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
je     125e <parse_request_head+0x10e>
mov    -0x28(%rbp),%rax
movslq -0x2040(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1272 <parse_request_head+0x122>
mov    -0x28(%rbp),%rax
movslq -0x2040(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    128b <parse_request_head+0x13b>
jmp    1277 <parse_request_head+0x127>
mov    -0x2040(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2040(%rbp)
jmp    121c <parse_request_head+0xcc>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_request_head>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x2000,%rsp
mov    %rcx,%r12
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbp
mov    %rsp,%rbx
mov    $0x2000,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x1fff,%edx
call   1060 <memcpy@plt>
mov    %rbx,%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     1201 <parse_request_head+0xb1>
movb   $0x0,(%rax)
lea    0xe52(%rip),%rsi
mov    %rsp,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
mov    %r12,%r8
call   1070 <sscanf@plt>
cmp    $0x3,%eax
jne    1201 <parse_request_head+0xb1>
mov    %r12,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
jle    11ff <parse_request_head+0xaf>
mov    %eax,%eax
xor    %ecx,%ecx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movzbl (%r12,%rcx,1),%edx
cmp    $0xd,%dl
je     11fa <parse_request_head+0xaa>
cmp    $0xa,%dl
je     11fa <parse_request_head+0xaa>
add    $0x1,%rcx
cmp    %rcx,%rax
jne    11e0 <parse_request_head+0x90>
jmp    11ff <parse_request_head+0xaf>
movb   $0x0,(%r12,%rcx,1)
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x2000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"%[^ ] %[^ ] %[^ ]\00"
define i32 @parse_request_head(i8* %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca [8192 x i8]
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
%14 = bitcast [8192 x i8]* %10 to i8*
%15 = getelementptr inbounds [8192 x i8], [8192 x i8]* %10, i64 0, i64 0
%16 = load i8*, i8** %6
%17 = call i32 @memcpy(i8* %15, i8* %16, i32 8191)
%18 = getelementptr inbounds [8192 x i8], [8192 x i8]* %10, i64 0, i64 0
%19 = call i8* @strchr(i8* %18, i8 signext 10)
store i8* %19, i8** %11
%20 = load i8*, i8** %11
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %23
22: ; preds = %4
store i32 -1, i32* %5
br label %65
23: ; preds = %4
%24 = load i8*, i8** %11
store i8 0, i8* %24
%25 = getelementptr inbounds [8192 x i8], [8192 x i8]* %10, i64 0, i64 0
%26 = load i8*, i8** %7
%27 = load i8*, i8** %8
%28 = load i8*, i8** %9
%29 = call i32 @sscanf(i8* %25, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %26, i8* %27, i8* %28)
%30 = icmp ne i32 %29, 3
br i1 %30, label %31, label %32
31: ; preds = %23
store i32 -1, i32* %5
br label %65
32: ; preds = %23
%33 = load i8*, i8** %9
%34 = call i32 @strlen(i8* %33)
store i32 %34, i32* %12
store i32 0, i32* %13
br label %35
35: ; preds = %61, %32
%36 = load i32, i32* %13
%37 = load i32, i32* %12
%38 = icmp slt i32 %36, %37
br i1 %38, label %39, label %64
39: ; preds = %35
%40 = load i8*, i8** %9
%41 = load i32, i32* %13
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = icmp eq i32 %45, 13
br i1 %46, label %55, label %47
47: ; preds = %39
%48 = load i8*, i8** %9
%49 = load i32, i32* %13
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
%52 = load i8, i8* %51
%53 = sext i8 %52 to i32
%54 = icmp eq i32 %53, 10
br i1 %54, label %55, label %60
55: ; preds = %47, %39
%56 = load i8*, i8** %9
%57 = load i32, i32* %13
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %56, i64 %58
store i8 0, i8* %59
br label %64
60: ; preds = %47
br label %61
61: ; preds = %60
%62 = load i32, i32* %13
%63 = add nsw i32 %62, 1
store i32 %63, i32* %13
br label %35
64: ; preds = %55, %35
store i32 0, i32* %5
br label %65
65: ; preds = %64, %31, %22
%66 = load i32, i32* %5
ret i32 %66
}

LLVM IR (-O2):
@.str = [18 x i8] c"%[^ ] %[^ ] %[^ ]\00"
define i32 @parse_request_head(i8* %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca [8192 x i8]
%6 = getelementptr inbounds [8192 x i8], [8192 x i8]* %5, i64 0, i64 0
%7 = call i32 @memcpy(i8* %6, i8* %0, i32 8191)
%8 = call i8* @strchr(i8* %6, i8 signext 10)
%9 = icmp eq i8* %8, null
br i1 %9, label %28, label %10
10: ; preds = %4
store i8 0, i8* %8
%11 = call i32 @sscanf(i8* %6, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %2, i8* %3)
%12 = icmp eq i32 %11, 3
br i1 %12, label %13, label %28
13: ; preds = %10
%14 = call i32 @strlen(i8* %3)
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %28
16: ; preds = %13
%17 = zext i32 %14 to i64
br label %18
18: ; preds = %16, %25
%19 = phi i64 [ 0, %16 ], [ %26, %25 ]
%20 = getelementptr inbounds i8, i8* %3, i64 %19
%21 = load i8, i8* %20
switch i8 %21, label %25 [
i8 13, label %22
i8 10, label %22
]
22: ; preds = %18, %18
%23 = and i64 %19, 4294967295
%24 = getelementptr inbounds i8, i8* %3, i64 %23
store i8 0, i8* %24
br label %28
25: ; preds = %18
%26 = add nuw nsw i64 %19, 1
%27 = icmp eq i64 %26, %17
br i1 %27, label %28, label %18
28: ; preds = %25, %13, %22, %10, %4
%29 = phi i32 [ -1, %4 ], [ -1, %10 ], [ 0, %22 ], [ 0, %13 ], [ 0, %25 ]
ret i32 %29
}