#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 *strstr(char *, char *);
int calculateHeaderSize(char *data) {
  char *pch = NULL, *pHeader;
  int headerSize = 0;
  pch = strstr(data, "\r\n");
  while (1) {
    if (pch == NULL)
      break;
    else
      pHeader = pch;
    pch++;
    pch = strstr(pch, "\r\n");
  }
  headerSize = pHeader - data + 2;
  return headerSize;
}


x86 assembly (clang, -O0):
<calculateHeaderSize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xeca(%rip),%rsi
call   1030 <strstr@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    114f <calculateHeaderSize+0x3f>
jmp    117c <calculateHeaderSize+0x6c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1030 <strstr@plt>
mov    %rax,-0x10(%rbp)
jmp    113f <calculateHeaderSize+0x2f>
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
add    $0x2,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calculateHeaderSize>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
lea    0xee1(%rip),%rsi
call   1030 <strstr@plt>
test   %rax,%rax
je     1146 <calculateHeaderSize+0x36>
lea    0xed0(%rip),%r15
mov    %rax,%rbx
lea    0x1(%rax),%rdi
mov    %r15,%rsi
call   1030 <strstr@plt>
test   %rax,%rax
jne    1130 <calculateHeaderSize+0x20>
jmp    1146 <calculateHeaderSize+0x36>
sub    %r14d,%ebx
add    $0x2,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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