#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 */
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
scalar_t__ memcmp(char *, char *, int);
int read_to(int, char *, int, double);
scalar_t__ realloc(char *, int);
int dumb_get_HTTP_headers(int socket_h, char **headers, double timeout) {
  int len_in = 0, len = 4096;
  char *buffer = (char *)malloc(len);
  int rc = 0;
  *headers = NULL;
  for (;;) {
    int rrc = read_to(socket_h, &buffer[len_in], 1, timeout);
    if (rrc == 0 || rrc == -1) {
      rc = -1;
      break;
    } else if (rrc == -2) {
      free(buffer);
      return -2;
    }
    len_in += rrc;
    buffer[len_in] = 0x00;
    if (memcmp(&buffer[len_in - 4], "\r\n\r\n", 4) == 0)
      break;
    if (memcmp(&buffer[len_in - 2], "\n\n", 2) == 0)
      break;
    if (len_in == (len - 1)) {
      len <<= 1;
      buffer = (char *)realloc(buffer, len);
    }
  }
  *headers = buffer;
  return rc;
}


x86 assembly (clang, -O0):
<dumb_get_HTTP_headers>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movsd  %xmm0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x1000,-0x20(%rbp)
mov    -0x20(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
movsd  -0x18(%rbp),%xmm0
mov    $0x1,%edx
call   1040 <read_to@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
je     11c4 <dumb_get_HTTP_headers+0x74>
cmpl   $0xffffffff,-0x30(%rbp)
jne    11d0 <dumb_get_HTTP_headers+0x80>
movl   $0xffffffff,-0x2c(%rbp)
jmp    1296 <dumb_get_HTTP_headers+0x146>
cmpl   $0xfffffffe,-0x30(%rbp)
jne    11ef <dumb_get_HTTP_headers+0x9f>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12a7 <dumb_get_HTTP_headers+0x157>
jmp    11f4 <dumb_get_HTTP_headers+0xa4>
mov    -0x30(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%eax
sub    $0x4,%eax
cltq
add    %rax,%rdi
lea    0xde1(%rip),%rsi
mov    $0x4,%edx
call   1050 <memcmp@plt>
cmp    $0x0,%rax
jne    1238 <dumb_get_HTTP_headers+0xe8>
jmp    1296 <dumb_get_HTTP_headers+0x146>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%eax
sub    $0x2,%eax
cltq
add    %rax,%rdi
lea    0xdb7(%rip),%rsi
mov    $0x2,%edx
call   1050 <memcmp@plt>
cmp    $0x0,%rax
jne    1267 <dumb_get_HTTP_headers+0x117>
jmp    1296 <dumb_get_HTTP_headers+0x146>
mov    -0x1c(%rbp),%eax
mov    -0x20(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    1291 <dumb_get_HTTP_headers+0x141>
mov    -0x20(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1070 <realloc@plt>
mov    %rax,-0x28(%rbp)
jmp    1190 <dumb_get_HTTP_headers+0x40>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dumb_get_HTTP_headers>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movsd  %xmm0,0x10(%rsp)
mov    %rsi,%r14
mov    %edi,%r15d
mov    $0x1000,%ebp
mov    $0x1000,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
movq   $0x0,(%r14)
mov    %r15d,%edi
mov    %rax,%rsi
mov    $0x1,%edx
movsd  0x10(%rsp),%xmm0
call   1040 <read_to@plt>
lea    0x1(%rax),%ecx
cmp    $0x2,%ecx
jae    11ac <dumb_get_HTTP_headers+0x5c>
mov    $0xffffffff,%r13d
jmp    125a <dumb_get_HTTP_headers+0x10a>
mov    %r14,0x8(%rsp)
xor    %r12d,%r12d
xor    %r13d,%r13d
jmp    11e3 <dumb_get_HTTP_headers+0x93>
nopl   0x0(%rax)
movsd  0x10(%rsp),%xmm0
add    %rbx,%r14
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <read_to@plt>
mov    %eax,%ecx
add    $0x1,%ecx
cmp    $0x1,%ecx
jbe    124f <dumb_get_HTTP_headers+0xff>
cmp    $0xfffffffe,%eax
je     126f <dumb_get_HTTP_headers+0x11f>
add    %eax,%r12d
movslq %r12d,%r14
movb   $0x0,(%rbx,%r14,1)
lea    (%rbx,%r14,1),%rdi
add    $0xfffffffffffffffc,%rdi
lea    0xdfa(%rip),%rsi
mov    $0x4,%edx
call   1050 <memcmp@plt>
test   %rax,%rax
je     1255 <dumb_get_HTTP_headers+0x105>
lea    (%r14,%rbx,1),%rdi
add    $0xfffffffffffffffe,%rdi
lea    0xde1(%rip),%rsi
mov    $0x2,%edx
call   1050 <memcmp@plt>
test   %rax,%rax
je     1255 <dumb_get_HTTP_headers+0x105>
lea    -0x1(%rbp),%eax
cmp    %eax,%r12d
jne    11c0 <dumb_get_HTTP_headers+0x70>
add    %ebp,%ebp
mov    %rbx,%rdi
mov    %ebp,%esi
call   1070 <realloc@plt>
mov    %rax,%rbx
jmp    11c0 <dumb_get_HTTP_headers+0x70>
mov    $0xffffffff,%r13d
mov    0x8(%rsp),%r14
mov    %rbx,(%r14)
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0xfffffffe,%r13d
jmp    125d <dumb_get_HTTP_headers+0x10d>

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

LLVM IR (-O2):
@.str = [5 x i8] c"\0D\0A\0D\0A\00"
@.str.1 = [3 x i8] c"\0A\0A\00"
define i32 @dumb_get_HTTP_headers(i32 %0, i8** nocapture writeonly %1, double %2) {
%4 = tail call i64 @malloc(i32 4096)
%5 = inttoptr i64 %4 to i8*
store i8* null, i8** %1
%6 = tail call i32 @read_to(i32 %0, i8* %5, i32 1, double %2)
%7 = add i32 %6, 1
%8 = icmp ult i32 %7, 2
br i1 %8, label %46, label %9
9: ; preds = %3, %39
%10 = phi i32 [ %43, %39 ], [ %6, %3 ]
%11 = phi i8* [ %41, %39 ], [ %5, %3 ]
%12 = phi i32 [ %40, %39 ], [ 4096, %3 ]
%13 = phi i32 [ %18, %39 ], [ 0, %3 ]
%14 = icmp eq i32 %10, -2
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = tail call i32 @free(i8* %11)
br label %49
17: ; preds = %9
%18 = add nsw i32 %10, %13
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %11, i64 %19
store i8 0, i8* %20
%21 = add nsw i32 %18, -4
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %11, i64 %22
%24 = tail call i64 @memcmp(i8* %23, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%25 = icmp eq i64 %24, 0
br i1 %25, label %46, label %26
26: ; preds = %17
%27 = add nsw i32 %18, -2
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %11, i64 %28
%30 = tail call i64 @memcmp(i8* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%31 = icmp eq i64 %30, 0
br i1 %31, label %46, label %32
32: ; preds = %26
%33 = add nsw i32 %12, -1
%34 = icmp eq i32 %18, %33
br i1 %34, label %35, label %39
35: ; preds = %32
%36 = shl i32 %12, 1
%37 = tail call i64 @realloc(i8* %11, i32 %36)
%38 = inttoptr i64 %37 to i8*
br label %39
39: ; preds = %32, %35
%40 = phi i32 [ %36, %35 ], [ %12, %32 ]
%41 = phi i8* [ %38, %35 ], [ %11, %32 ]
%42 = getelementptr inbounds i8, i8* %41, i64 %19
%43 = tail call i32 @read_to(i32 %0, i8* %42, i32 1, double %2)
%44 = add i32 %43, 1
%45 = icmp ult i32 %44, 2
br i1 %45, label %46, label %9
46: ; preds = %26, %17, %39, %3
%47 = phi i8* [ %5, %3 ], [ %41, %39 ], [ %11, %17 ], [ %11, %26 ]
%48 = phi i32 [ -1, %3 ], [ -1, %39 ], [ 0, %17 ], [ 0, %26 ]
store i8* %47, i8** %1
br label %49
49: ; preds = %15, %46
%50 = phi i32 [ %48, %46 ], [ -2, %15 ]
ret i32 %50
}