#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 */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ memset(char *, char, int);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
int /*<<< orphan*/ *strstr(char const *, char *);
char *REQUEST_get_message_header(const char *content_data,
                                 long content_length) {
  char *hdr_line;
  char *temp_hdr_result;
  static char hdr_result[2048];
  int i = 0;
  int j = 0;
  if (strstr(content_data, "\r\n\r\n") == NULL) {
    return "";
  }
  hdr_line = (char *)calloc(256, sizeof(char));
  temp_hdr_result = (char *)calloc(2048 + 1, sizeof(char));
  for (i = 0; i < content_length; i++) {
    if (content_data[i] != '\n') {
      hdr_line[j] = content_data[i];
      j++;
    } else {
      if (strlen(hdr_line) == 1) {
        break;
      }
      if (strlen(temp_hdr_result) == 0) {
        strncpy(temp_hdr_result, hdr_line, 2048);
      } else {
        strncat(temp_hdr_result, hdr_line, 2048);
      }
      strncat(temp_hdr_result, "\n", 2048);
      memset(hdr_line, '\0', 256);
      j = 0;
    }
  }
  free(hdr_line);
  hdr_line = NULL;
  memset(hdr_result, '\0', 2048);
  strncpy(hdr_result, temp_hdr_result, 2048);
  free(temp_hdr_result);
  temp_hdr_result = NULL;
  return ((char *)&hdr_result);
}


x86 assembly (clang, -O0):
<REQUEST_get_message_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe67(%rip),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
jne    11b8 <REQUEST_get_message_header+0x48>
lea    0xe55(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1312 <REQUEST_get_message_header+0x1a2>
mov    $0x100,%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    $0x801,%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movslq -0x2c(%rbp),%rax
cmp    -0x18(%rbp),%rax
jge    12bd <REQUEST_get_message_header+0x14d>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     122c <REQUEST_get_message_header+0xbc>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x20(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    12aa <REQUEST_get_message_header+0x13a>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x1,%eax
jne    1243 <REQUEST_get_message_header+0xd3>
jmp    12bd <REQUEST_get_message_header+0x14d>
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x0,%eax
jne    126c <REQUEST_get_message_header+0xfc>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x800,%edx
call   1040 <strncpy@plt>
jmp    127e <REQUEST_get_message_header+0x10e>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x800,%edx
call   1070 <strncat@plt>
mov    -0x28(%rbp),%rdi
lea    0xd7a(%rip),%rsi
mov    $0x800,%edx
call   1070 <strncat@plt>
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
movl   $0x0,-0x30(%rbp)
jmp    12af <REQUEST_get_message_header+0x13f>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11e5 <REQUEST_get_message_header+0x75>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x20(%rbp)
lea    0x2d9b(%rip),%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1060 <memset@plt>
mov    -0x28(%rbp),%rsi
lea    0x2d84(%rip),%rdi
mov    $0x800,%edx
call   1040 <strncpy@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x28(%rbp)
lea    0x2d62(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<REQUEST_get_message_header>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe78(%rip),%rsi
call   1090 <strstr@plt>
test   %rax,%rax
je     127d <REQUEST_get_message_header+0x10d>
mov    $0x100,%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,%rbx
mov    $0x801,%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,%r12
test   %r14,%r14
jle    1245 <REQUEST_get_message_header+0xd5>
xor    %ebp,%ebp
xor    %r13d,%r13d
jmp    11e4 <REQUEST_get_message_header+0x74>
nopw   0x0(%rax,%rax,1)
movslq %r13d,%r13
mov    %al,(%rbx,%r13,1)
add    $0x1,%r13d
add    $0x1,%rbp
cmp    %rbp,%r14
je     1245 <REQUEST_get_message_header+0xd5>
movzbl (%r15,%rbp,1),%eax
cmp    $0xa,%al
jne    11d0 <REQUEST_get_message_header+0x60>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cmp    $0x1,%eax
je     1245 <REQUEST_get_message_header+0xd5>
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    $0x800,%edx
test   %eax,%eax
je     1218 <REQUEST_get_message_header+0xa8>
call   1070 <strncat@plt>
jmp    121d <REQUEST_get_message_header+0xad>
call   1040 <strncpy@plt>
mov    %r12,%rdi
lea    0xddc(%rip),%rsi
mov    $0x800,%edx
call   1070 <strncat@plt>
xor    %r13d,%r13d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
jmp    11db <REQUEST_get_message_header+0x6b>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0x2e1c(%rip),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1060 <memset@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x800,%edx
call   1040 <strncpy@plt>
mov    %r12,%rdi
call   1030 <free@plt>
jmp    1284 <REQUEST_get_message_header+0x114>
lea    0xd80(%rip),%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@REQUEST_get_message_header.hdr_result = internal global [2048 x i8] zeroinitializer
@.str = [5 x i8] c"\0D\0A\0D\0A\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [2 x i8] c"\0A\00"
define i8* @REQUEST_get_message_header(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32 0, i32* %8
store i32 0, i32* %9
%10 = load i8*, i8** %4
%11 = call i32* @strstr(i8* %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %14
13: ; preds = %2
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8** %3
br label %78
14: ; preds = %2
%15 = call i64 @calloc(i32 256, i32 1)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %6
%17 = call i64 @calloc(i32 2049, i32 1)
%18 = inttoptr i64 %17 to i8*
store i8* %18, i8** %7
store i32 0, i32* %8
br label %19
19: ; preds = %67, %14
%20 = load i32, i32* %8
%21 = sext i32 %20 to i64
%22 = load i64, i64* %5
%23 = icmp slt i64 %21, %22
br i1 %23, label %24, label %70
24: ; preds = %19
%25 = load i8*, i8** %4
%26 = load i32, i32* %8
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp ne i32 %30, 10
br i1 %31, label %32, label %44
32: ; preds = %24
%33 = load i8*, i8** %4
%34 = load i32, i32* %8
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = load i8, i8* %36
%38 = load i8*, i8** %6
%39 = load i32, i32* %9
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
store i8 %37, i8* %41
%42 = load i32, i32* %9
%43 = add nsw i32 %42, 1
store i32 %43, i32* %9
br label %66
44: ; preds = %24
%45 = load i8*, i8** %6
%46 = call i32 @strlen(i8* %45)
%47 = icmp eq i32 %46, 1
br i1 %47, label %48, label %49
48: ; preds = %44
br label %70
49: ; preds = %44
%50 = load i8*, i8** %7
%51 = call i32 @strlen(i8* %50)
%52 = icmp eq i32 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %49
%54 = load i8*, i8** %7
%55 = load i8*, i8** %6
%56 = call i32 @strncpy(i8* %54, i8* %55, i32 2048)
br label %61
57: ; preds = %49
%58 = load i8*, i8** %7
%59 = load i8*, i8** %6
%60 = call i32 @strncat(i8* %58, i8* %59, i32 2048)
br label %61
61: ; preds = %57, %53
%62 = load i8*, i8** %7
%63 = call i32 @strncat(i8* %62, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 2048)
%64 = load i8*, i8** %6
%65 = call i32 @memset(i8* %64, i8 signext 0, i32 256)
store i32 0, i32* %9
br label %66
66: ; preds = %61, %32
br label %67
67: ; preds = %66
%68 = load i32, i32* %8
%69 = add nsw i32 %68, 1
store i32 %69, i32* %8
br label %19
70: ; preds = %48, %19
%71 = load i8*, i8** %6
%72 = call i32 @free(i8* %71)
store i8* null, i8** %6
%73 = call i32 @memset(i8* getelementptr inbounds ([2048 x i8], [2048 x i8]* @REQUEST_get_message_header.hdr_result, i64 0, i64 0), i8 signext 0, i32 2048)
%74 = load i8*, i8** %7
%75 = call i32 @strncpy(i8* getelementptr inbounds ([2048 x i8], [2048 x i8]* @REQUEST_get_message_header.hdr_result, i64 0, i64 0), i8* %74, i32 2048)
%76 = load i8*, i8** %7
%77 = call i32 @free(i8* %76)
store i8* null, i8** %7
store i8* getelementptr inbounds ([2048 x i8], [2048 x i8]* @REQUEST_get_message_header.hdr_result, i32 0, i32 0), i8** %3
br label %78
78: ; preds = %70, %13
%79 = load i8*, i8** %3
ret i8* %79
}

LLVM IR (-O2):
@REQUEST_get_message_header.hdr_result = internal global [2048 x i8] zeroinitializer
@.str = [5 x i8] c"\0D\0A\0D\0A\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [2 x i8] c"\0A\00"
define i8* @REQUEST_get_message_header(i8* %0, i64 %1) {
%3 = tail call i32* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %43, label %5
5: ; preds = %2
%6 = tail call i64 @calloc(i32 256, i32 1)
%7 = inttoptr i64 %6 to i8*
%8 = tail call i64 @calloc(i32 2049, i32 1)
%9 = inttoptr i64 %8 to i8*
%10 = icmp sgt i64 %1, 0
br i1 %10, label %11, label %38
11: ; preds = %5, %34
%12 = phi i64 [ %36, %34 ], [ 0, %5 ]
%13 = phi i32 [ %35, %34 ], [ 0, %5 ]
%14 = getelementptr inbounds i8, i8* %0, i64 %12
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 10
br i1 %16, label %21, label %17
17: ; preds = %11
%18 = sext i32 %13 to i64
%19 = getelementptr inbounds i8, i8* %7, i64 %18
store i8 %15, i8* %19
%20 = add nsw i32 %13, 1
br label %34
21: ; preds = %11
%22 = tail call i32 @strlen(i8* %7)
%23 = icmp eq i32 %22, 1
br i1 %23, label %38, label %24
24: ; preds = %21
%25 = tail call i32 @strlen(i8* %9)
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = tail call i32 @strncpy(i8* %9, i8* %7, i32 2048)
br label %31
29: ; preds = %24
%30 = tail call i32 @strncat(i8* %9, i8* %7, i32 2048)
br label %31
31: ; preds = %29, %27
%32 = tail call i32 @strncat(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 2048)
%33 = tail call i32 @memset(i8* %7, i8 signext 0, i32 256)
br label %34
34: ; preds = %17, %31
%35 = phi i32 [ %20, %17 ], [ 0, %31 ]
%36 = add nuw nsw i64 %12, 1
%37 = icmp eq i64 %36, %1
br i1 %37, label %38, label %11
38: ; preds = %34, %21, %5
%39 = tail call i32 @free(i8* %7)
%40 = tail call i32 @memset(i8* getelementptr inbounds ([2048 x i8], [2048 x i8]* @REQUEST_get_message_header.hdr_result, i64 0, i64 0), i8 signext 0, i32 2048)
%41 = tail call i32 @strncpy(i8* getelementptr inbounds ([2048 x i8], [2048 x i8]* @REQUEST_get_message_header.hdr_result, i64 0, i64 0), i8* %9, i32 2048)
%42 = tail call i32 @free(i8* %9)
br label %43
43: ; preds = %2, %38
%44 = phi i8* [ getelementptr inbounds ([2048 x i8], [2048 x i8]* @REQUEST_get_message_header.hdr_result, i64 0, i64 0), %38 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), %2 ]
ret i8* %44
}