#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 *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
char *strchr(char *, char);
size_t strlen(char *);
char *strstr(char *, char *);
bool HTTPFindHeader(char *In, char *Buff, char **Out, char **StartIndex,
                    char **EndIndex) {
  char *valIndex = Buff;
  size_t searchIndex = 0, inLen = strlen(In);
  do {
    valIndex = strstr(Buff + searchIndex, In);
    if (valIndex == NULL)
      return false;
    if (valIndex == Buff || *(valIndex - 1) != '\n')
      searchIndex = (size_t)valIndex + inLen;
    else
      break;
  } while (1);
  char *valEnd = strstr(valIndex + inLen, "\r\n");
  if (valEnd == NULL) {
    valEnd = strchr(valIndex + inLen, '\n');
    if (valEnd == NULL)
      return false;
  }
  char *valIndexEnd = valIndex + inLen;
  size_t valLen = valEnd - valIndexEnd;
  *Out = malloc(valLen + 1);
  memcpy(*Out, valIndexEnd, valLen);
  (*Out)[valLen] = 0x00;
  if (StartIndex != NULL)
    *StartIndex = valIndex;
  if (EndIndex != NULL)
    *EndIndex = valEnd;
  return true;
}


x86 assembly (clang, -O0):
<HTTPFindHeader>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
add    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11b5 <HTTPFindHeader+0x65>
movl   $0x0,-0x4(%rbp)
jmp    12db <HTTPFindHeader+0x18b>
mov    -0x38(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     11d4 <HTTPFindHeader+0x84>
mov    -0x38(%rbp),%rax
movsbl -0x1(%rax),%eax
cmp    $0xa,%eax
je     11e5 <HTTPFindHeader+0x95>
mov    -0x38(%rbp),%rax
add    -0x48(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    11ea <HTTPFindHeader+0x9a>
jmp    11fe <HTTPFindHeader+0xae>
jmp    11ef <HTTPFindHeader+0x9f>
mov    $0x1,%al
test   $0x1,%al
jne    1189 <HTTPFindHeader+0x39>
jmp    11fe <HTTPFindHeader+0xae>
mov    -0x38(%rbp),%rdi
add    -0x48(%rbp),%rdi
lea    0xdf3(%rip),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    1253 <HTTPFindHeader+0x103>
mov    -0x38(%rbp),%rdi
add    -0x48(%rbp),%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    124e <HTTPFindHeader+0xfe>
movl   $0x0,-0x4(%rbp)
jmp    12db <HTTPFindHeader+0x18b>
jmp    1253 <HTTPFindHeader+0x103>
mov    -0x38(%rbp),%rax
add    -0x48(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x50(%rbp),%rax
mov    -0x58(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x58(%rbp),%rsi
mov    -0x60(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    -0x60(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpq   $0x0,-0x28(%rbp)
je     12be <HTTPFindHeader+0x16e>
mov    -0x38(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x30(%rbp)
je     12d4 <HTTPFindHeader+0x184>
mov    -0x50(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<HTTPFindHeader>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,0x10(%rsp)
mov    %rcx,0x8(%rsp)
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <strlen@plt>
mov    %rax,%r13
mov    %r14,%rdi
mov    %r15,%rsi
call   1070 <strstr@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     123f <HTTPFindHeader+0xef>
mov    %rax,%rbx
jmp    11bd <HTTPFindHeader+0x6d>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    %r13,%rbx
add    %r14,%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1070 <strstr@plt>
mov    %rax,%rbx
test   %rax,%rax
je     123f <HTTPFindHeader+0xef>
cmp    %r14,%rbx
je     11a0 <HTTPFindHeader+0x50>
cmpb   $0xa,-0x1(%rbx)
jne    11a0 <HTTPFindHeader+0x50>
add    %rbx,%r13
lea    0xe2e(%rip),%rsi
mov    %r13,%rdi
call   1070 <strstr@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11f7 <HTTPFindHeader+0xa7>
mov    %r13,%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
mov    %rax,%r14
test   %rax,%rax
je     123f <HTTPFindHeader+0xef>
mov    %r14,%rbp
sub    %r13,%rbp
lea    0x1(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,(%r12)
mov    %rax,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1050 <memcpy@plt>
mov    (%r12),%rax
movb   $0x0,(%rax,%rbp,1)
mov    0x8(%rsp),%rax
test   %rax,%rax
je     122d <HTTPFindHeader+0xdd>
mov    %rbx,(%rax)
mov    $0x1,%ebp
mov    0x10(%rsp),%rax
test   %rax,%rax
je     123f <HTTPFindHeader+0xef>
mov    %r14,(%rax)
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"\0D\0A\00"
define i32 @HTTPFindHeader(i8* %0, i8* %1, i8** %2, i8** %3, i8** %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8**
%10 = alloca i8**
%11 = alloca i8**
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i64
%15 = alloca i8*
%16 = alloca i8*
%17 = alloca i64
store i8* %0, i8** %7
store i8* %1, i8** %8
store i8** %2, i8*** %9
store i8** %3, i8*** %10
store i8** %4, i8*** %11
%18 = load i8*, i8** %8
store i8* %18, i8** %12
store i64 0, i64* %13
%19 = load i8*, i8** %7
%20 = call i64 @strlen(i8* %19)
store i64 %20, i64* %14
br label %21
21: ; preds = %47, %5
%22 = load i8*, i8** %8
%23 = load i64, i64* %13
%24 = getelementptr inbounds i8, i8* %22, i64 %23
%25 = load i8*, i8** %7
%26 = call i8* @strstr(i8* %24, i8* %25)
store i8* %26, i8** %12
%27 = load i8*, i8** %12
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %30
29: ; preds = %21
store i32 0, i32* %6
br label %98
30: ; preds = %21
%31 = load i8*, i8** %12
%32 = load i8*, i8** %8
%33 = icmp eq i8* %31, %32
br i1 %33, label %40, label %34
34: ; preds = %30
%35 = load i8*, i8** %12
%36 = getelementptr inbounds i8, i8* %35, i64 -1
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp ne i32 %38, 10
br i1 %39, label %40, label %45
40: ; preds = %34, %30
%41 = load i8*, i8** %12
%42 = ptrtoint i8* %41 to i64
%43 = load i64, i64* %14
%44 = add i64 %42, %43
store i64 %44, i64* %13
br label %46
45: ; preds = %34
br label %48
46: ; preds = %40
br label %47
47: ; preds = %46
br i1 true, label %21, label %48
48: ; preds = %47, %45
%49 = load i8*, i8** %12
%50 = load i64, i64* %14
%51 = getelementptr inbounds i8, i8* %49, i64 %50
%52 = call i8* @strstr(i8* %51, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %52, i8** %15
%53 = load i8*, i8** %15
%54 = icmp eq i8* %53, null
br i1 %54, label %55, label %64
55: ; preds = %48
%56 = load i8*, i8** %12
%57 = load i64, i64* %14
%58 = getelementptr inbounds i8, i8* %56, i64 %57
%59 = call i8* @strchr(i8* %58, i8 signext 10)
store i8* %59, i8** %15
%60 = load i8*, i8** %15
%61 = icmp eq i8* %60, null
br i1 %61, label %62, label %63
62: ; preds = %55
store i32 0, i32* %6
br label %98
63: ; preds = %55
br label %64
64: ; preds = %63, %48
%65 = load i8*, i8** %12
%66 = load i64, i64* %14
%67 = getelementptr inbounds i8, i8* %65, i64 %66
store i8* %67, i8** %16
%68 = load i8*, i8** %15
%69 = load i8*, i8** %16
%70 = ptrtoint i8* %68 to i64
%71 = ptrtoint i8* %69 to i64
%72 = sub i64 %70, %71
store i64 %72, i64* %17
%73 = load i64, i64* %17
%74 = add i64 %73, 1
%75 = call i8* @malloc(i64 %74)
%76 = load i8**, i8*** %9
store i8* %75, i8** %76
%77 = load i8**, i8*** %9
%78 = load i8*, i8** %77
%79 = load i8*, i8** %16
%80 = load i64, i64* %17
%81 = call i32 @memcpy(i8* %78, i8* %79, i64 %80)
%82 = load i8**, i8*** %9
%83 = load i8*, i8** %82
%84 = load i64, i64* %17
%85 = getelementptr inbounds i8, i8* %83, i64 %84
store i8 0, i8* %85
%86 = load i8**, i8*** %10
%87 = icmp ne i8** %86, null
br i1 %87, label %88, label %91
88: ; preds = %64
%89 = load i8*, i8** %12
%90 = load i8**, i8*** %10
store i8* %89, i8** %90
br label %91
91: ; preds = %88, %64
%92 = load i8**, i8*** %11
%93 = icmp ne i8** %92, null
br i1 %93, label %94, label %97
94: ; preds = %91
%95 = load i8*, i8** %15
%96 = load i8**, i8*** %11
store i8* %95, i8** %96
br label %97
97: ; preds = %94, %91
store i32 1, i32* %6
br label %98
98: ; preds = %97, %62, %29
%99 = load i32, i32* %6
ret i32 %99
}

LLVM IR (-O2):
@.str = [3 x i8] c"\0D\0A\00"
define i32 @HTTPFindHeader(i8* nocapture %0, i8* %1, i8** nocapture %2, i8** writeonly %3, i8** writeonly %4) {
%6 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%7 = tail call i8* @strstr(i8* dereferenceable(1) %1, i8* dereferenceable(1) %0)
%8 = icmp eq i8* %7, null
br i1 %8, label %44, label %9
9: ; preds = %5, %16
%10 = phi i8* [ %20, %16 ], [ %7, %5 ]
%11 = icmp eq i8* %10, %1
br i1 %11, label %16, label %12
12: ; preds = %9
%13 = getelementptr inbounds i8, i8* %10, i64 -1
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 10
br i1 %15, label %22, label %16
16: ; preds = %12, %9
%17 = ptrtoint i8* %10 to i64
%18 = add i64 %6, %17
%19 = getelementptr inbounds i8, i8* %1, i64 %18
%20 = tail call i8* @strstr(i8* dereferenceable(1) %19, i8* dereferenceable(1) %0)
%21 = icmp eq i8* %20, null
br i1 %21, label %44, label %9
22: ; preds = %12
%23 = getelementptr inbounds i8, i8* %10, i64 %6
%24 = tail call i8* @strstr(i8* %23, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = tail call i8* @strchr(i8* %23, i8 signext 10)
%28 = icmp eq i8* %27, null
br i1 %28, label %44, label %29
29: ; preds = %26, %22
%30 = phi i8* [ %27, %26 ], [ %24, %22 ]
%31 = ptrtoint i8* %30 to i64
%32 = ptrtoint i8* %23 to i64
%33 = sub i64 %31, %32
%34 = add i64 %33, 1
%35 = tail call i8* @malloc(i64 %34)
store i8* %35, i8** %2
%36 = tail call i32 @memcpy(i8* %35, i8* %23, i64 %33)
%37 = load i8*, i8** %2
%38 = getelementptr inbounds i8, i8* %37, i64 %33
store i8 0, i8* %38
%39 = icmp eq i8** %3, null
br i1 %39, label %41, label %40
40: ; preds = %29
store i8* %10, i8** %3
br label %41
41: ; preds = %40, %29
%42 = icmp eq i8** %4, null
br i1 %42, label %44, label %43
43: ; preds = %41
store i8* %30, i8** %4
br label %44
44: ; preds = %16, %5, %26, %43, %41
%45 = phi i32 [ 0, %26 ], [ 1, %43 ], [ 1, %41 ], [ 0, %5 ], [ 0, %16 ]
ret i32 %45
}