#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 */
char *ag_malloc(int);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strstr(char *, char *);
char *get_prot(char *raw) {
  char *start_prot;
  char *end_prot;
  char *rval;
  int plength;
  start_prot = strstr(raw, " ");
  if (!start_prot)
    return NULL;
  start_prot++;
  start_prot = strstr(start_prot, " ");
  if (!start_prot)
    return NULL;
  start_prot++;
  end_prot = strstr(start_prot, "\r\n");
  if (!end_prot)
    return NULL;
  end_prot--;
  plength = end_prot - start_prot + 1;
  rval = ag_malloc(plength + 1);
  strncpy(rval, start_prot, plength);
  return rval;
}


x86 assembly (clang, -O0):
<get_prot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeb9(%rip),%rsi
call   1050 <strstr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1168 <get_prot+0x38>
movq   $0x0,-0x8(%rbp)
jmp    121d <get_prot+0xed>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe81(%rip),%rsi
call   1050 <strstr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a0 <get_prot+0x70>
movq   $0x0,-0x8(%rbp)
jmp    121d <get_prot+0xed>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe4b(%rip),%rsi
call   1050 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d8 <get_prot+0xa8>
movq   $0x0,-0x8(%rbp)
jmp    121d <get_prot+0xed>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
add    $0x1,%edi
call   1040 <ag_malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_prot>:
push   %rbp
push   %r14
push   %rbx
mov    $0x20,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11a9 <get_prot+0x69>
add    $0x1,%rax
mov    %rax,%rdi
mov    $0x20,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11a9 <get_prot+0x69>
mov    %rax,%rbx
add    $0x1,%rbx
lea    0xe89(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strstr@plt>
test   %rax,%rax
je     11a9 <get_prot+0x69>
mov    %eax,%ebp
sub    %ebx,%ebp
mov    %ebx,%ecx
not    %ecx
lea    (%rcx,%rax,1),%edi
add    $0x2,%edi
call   1050 <ag_malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
jmp    11ac <get_prot+0x6c>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c" \00"
@.str.1 = [3 x i8] c"\0D\0A\00"
define i8* @get_prot(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i8* @strstr(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %9, i8** %4
%10 = load i8*, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %1
store i8* null, i8** %2
br label %47
13: ; preds = %1
%14 = load i8*, i8** %4
%15 = getelementptr inbounds i8, i8* %14, i32 1
store i8* %15, i8** %4
%16 = load i8*, i8** %4
%17 = call i8* @strstr(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %17, i8** %4
%18 = load i8*, i8** %4
%19 = icmp ne i8* %18, null
br i1 %19, label %21, label %20
20: ; preds = %13
store i8* null, i8** %2
br label %47
21: ; preds = %13
%22 = load i8*, i8** %4
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %4
%24 = load i8*, i8** %4
%25 = call i8* @strstr(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i8* %25, i8** %5
%26 = load i8*, i8** %5
%27 = icmp ne i8* %26, null
br i1 %27, label %29, label %28
28: ; preds = %21
store i8* null, i8** %2
br label %47
29: ; preds = %21
%30 = load i8*, i8** %5
%31 = getelementptr inbounds i8, i8* %30, i32 -1
store i8* %31, i8** %5
%32 = load i8*, i8** %5
%33 = load i8*, i8** %4
%34 = ptrtoint i8* %32 to i64
%35 = ptrtoint i8* %33 to i64
%36 = sub i64 %34, %35
%37 = add nsw i64 %36, 1
%38 = trunc i64 %37 to i32
store i32 %38, i32* %7
%39 = load i32, i32* %7
%40 = add nsw i32 %39, 1
%41 = call i8* @ag_malloc(i32 %40)
store i8* %41, i8** %6
%42 = load i8*, i8** %6
%43 = load i8*, i8** %4
%44 = load i32, i32* %7
%45 = call i32 @strncpy(i8* %42, i8* %43, i32 %44)
%46 = load i8*, i8** %6
store i8* %46, i8** %2
br label %47
47: ; preds = %29, %28, %20, %12
%48 = load i8*, i8** %2
ret i8* %48
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"\0D\0A\00"
define i8* @get_prot(i8* %0) {
%2 = tail call i8* @strchr(i8* dereferenceable(1) %0, i32 32)
%3 = icmp eq i8* %2, null
br i1 %3, label %22, label %4
4: ; preds = %1
%5 = getelementptr inbounds i8, i8* %2, i64 1
%6 = tail call i8* @strchr(i8* %5, i32 32)
%7 = icmp eq i8* %6, null
br i1 %7, label %22, label %8
8: ; preds = %4
%9 = getelementptr inbounds i8, i8* %6, i64 1
%10 = tail call i8* @strstr(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i8* %10, null
br i1 %11, label %22, label %12
12: ; preds = %8
%13 = getelementptr inbounds i8, i8* %10, i64 -1
%14 = ptrtoint i8* %13 to i64
%15 = ptrtoint i8* %9 to i64
%16 = sub i64 %14, %15
%17 = trunc i64 %16 to i32
%18 = add i32 %17, 1
%19 = add i32 %17, 2
%20 = tail call i8* @ag_malloc(i32 %19)
%21 = tail call i32 @strncpy(i8* %20, i8* %9, i32 %18)
br label %22
22: ; preds = %8, %4, %1, %12
%23 = phi i8* [ %20, %12 ], [ null, %1 ], [ null, %4 ], [ null, %8 ]
ret i8* %23
}