#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*/ HEAD;

/* Variables and functions */
size_t strcspn(char *, char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
char *strstr(char const *, char const *);
int get_request(const char *src, size_t srclen, char *request, size_t *rqlen) {
  const char HEAD[] = "GET /";
  char *p = NULL;
  const char *EOD = src + srclen - 1;
  size_t n = 0;
  const char SPACE[] = " ";
  p = strstr(src, HEAD);
  if (NULL == p)
    return 1;
  p += (sizeof(HEAD) - 1);
  if (p >= EOD)
    return 2;
  n = strcspn(p, " ");
  if ((SPACE[0] != p[n]) || ((p + n) > EOD) || (n >= *rqlen))
    return 3;
  (void)strncpy(request, p, n);
  request[n] = '\0';
  *rqlen = n;
  return 0;
}


x86 assembly (clang, -O0):
<get_request>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    0xeb2(%rip),%eax
mov    %eax,-0x2e(%rbp)
mov    0xeac(%rip),%ax
mov    %ax,-0x2a(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rax
add    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
mov    0xe83(%rip),%ax
mov    %ax,-0x4a(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x2e(%rbp),%rsi
call   1050 <strstr@plt>
mov    %rax,-0x38(%rbp)
xor    %eax,%eax
cmp    -0x38(%rbp),%rax
jne    11b0 <get_request+0x80>
movl   $0x1,-0x4(%rbp)
jmp    1260 <get_request+0x130>
mov    -0x38(%rbp),%rax
add    $0x5,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x40(%rbp),%rax
jb     11d6 <get_request+0xa6>
movl   $0x2,-0x4(%rbp)
jmp    1260 <get_request+0x130>
mov    -0x38(%rbp),%rdi
lea    0xe25(%rip),%rsi
call   1040 <strcspn@plt>
mov    %rax,-0x48(%rbp)
movsbl -0x4a(%rbp),%eax
mov    -0x38(%rbp),%rcx
mov    -0x48(%rbp),%rdx
movsbl (%rcx,%rdx,1),%ecx
cmp    %ecx,%eax
jne    1225 <get_request+0xf5>
mov    -0x38(%rbp),%rax
add    -0x48(%rbp),%rax
cmp    -0x40(%rbp),%rax
ja     1225 <get_request+0xf5>
mov    -0x48(%rbp),%rax
mov    -0x28(%rbp),%rcx
cmp    (%rcx),%rax
jb     1231 <get_request+0x101>
movl   $0x3,-0x4(%rbp)
jmp    1260 <get_request+0x130>
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x48(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x48(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_request>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r13
lea    0xeb4(%rip),%rsi
call   1050 <strstr@plt>
test   %rax,%rax
je     11b3 <get_request+0x83>
mov    %rax,%rbx
add    %r12,%r13
add    $0xffffffffffffffff,%r13
add    $0x5,%rbx
mov    $0x2,%eax
cmp    %r13,%rbx
jae    11b8 <get_request+0x88>
lea    0xe91(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcspn@plt>
mov    %rax,%r12
cmpb   $0x20,(%rbx,%rax,1)
mov    $0x3,%eax
jne    11b8 <get_request+0x88>
lea    (%rbx,%r12,1),%rcx
cmp    %r13,%rcx
ja     11b8 <get_request+0x88>
cmp    (%r15),%r12
jae    11b8 <get_request+0x88>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1030 <strncpy@plt>
movb   $0x0,(%r14,%r12,1)
mov    %r12,(%r15)
xor    %eax,%eax
jmp    11b8 <get_request+0x88>
mov    $0x1,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@__const.get_request.HEAD = [6 x i8] c"GET /\00"
@.str = [2 x i8] c" \00"
define i32 @get_request(i8* %0, i64 %1, i8* %2, i64* nocapture %3) {
%5 = getelementptr inbounds i8, i8* %0, i64 %1
%6 = getelementptr inbounds i8, i8* %5, i64 -1
%7 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @__const.get_request.HEAD, i64 0, i64 0))
%8 = icmp eq i8* %7, null
br i1 %8, label %25, label %9
9: ; preds = %4
%10 = getelementptr inbounds i8, i8* %7, i64 5
%11 = icmp ult i8* %10, %6
br i1 %11, label %12, label %25
12: ; preds = %9
%13 = tail call i64 @strcspn(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%14 = getelementptr inbounds i8, i8* %10, i64 %13
%15 = load i8, i8* %14
%16 = icmp ne i8 %15, 32
%17 = icmp ugt i8* %14, %6
%18 = select i1 %16, i1 true, i1 %17
br i1 %18, label %25, label %19
19: ; preds = %12
%20 = load i64, i64* %3
%21 = icmp ult i64 %13, %20
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = tail call i32 @strncpy(i8* %2, i8* %10, i64 %13)
%24 = getelementptr inbounds i8, i8* %2, i64 %13
store i8 0, i8* %24
store i64 %13, i64* %3
br label %25
25: ; preds = %12, %19, %9, %4, %22
%26 = phi i32 [ 0, %22 ], [ 1, %4 ], [ 2, %9 ], [ 3, %19 ], [ 3, %12 ]
ret i32 %26
}