#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 *memstr(char const *, char const *, int);
int strlen(char const *);
int find_http_header(const char *buf, int buflen) {
  const char *magic = "\r\n\r\n";
  char *ptr = memstr(buf, magic, buflen);
  return ptr ? ptr - buf + strlen(magic) : -1;
}


x86 assembly (clang, -O0):
<find_http_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    0xeca(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1040 <memstr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1186 <find_http_header+0x66>
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    1196 <find_http_header+0x76>
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
jmp    1196 <find_http_header+0x76>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_http_header>:
push   %r14
push   %rbx
push   %rax
mov    %esi,%edx
mov    %rdi,%r14
lea    0xed0(%rip),%rsi
call   1040 <memstr@plt>
test   %rax,%rax
je     1150 <find_http_header+0x30>
mov    %rax,%rbx
sub    %r14d,%ebx
lea    0xeb9(%rip),%rdi
call   1030 <strlen@plt>
add    %ebx,%eax
jmp    1155 <find_http_header+0x35>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [5 x i8] c"\0D\0A\0D\0A\00"
define i32 @find_http_header(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %5
%7 = load i8*, i8** %3
%8 = load i8*, i8** %5
%9 = load i32, i32* %4
%10 = call i8* @memstr(i8* %7, i8* %8, i32 %9)
store i8* %10, i8** %6
%11 = load i8*, i8** %6
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %23
13: ; preds = %2
%14 = load i8*, i8** %6
%15 = load i8*, i8** %3
%16 = ptrtoint i8* %14 to i64
%17 = ptrtoint i8* %15 to i64
%18 = sub i64 %16, %17
%19 = load i8*, i8** %5
%20 = call i32 @strlen(i8* %19)
%21 = sext i32 %20 to i64
%22 = add nsw i64 %18, %21
br label %24
23: ; preds = %2
br label %24
24: ; preds = %23, %13
%25 = phi i64 [ %22, %13 ], [ -1, %23 ]
%26 = trunc i64 %25 to i32
ret i32 %26
}

LLVM IR (-O2):
@.str = [5 x i8] c"\0D\0A\0D\0A\00"
define i32 @find_http_header(i8* %0, i32 %1) {
%3 = tail call i8* @memstr(i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %1)
%4 = icmp eq i8* %3, null
br i1 %4, label %12, label %5
5: ; preds = %2
%6 = ptrtoint i8* %3 to i64
%7 = ptrtoint i8* %0 to i64
%8 = sub i64 %6, %7
%9 = tail call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%10 = trunc i64 %8 to i32
%11 = add i32 %9, %10
br label %12
12: ; preds = %2, %5
%13 = phi i32 [ %11, %5 ], [ -1, %2 ]
ret i32 %13
}