#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
int get_content_length(char *);
char *malloc(int);
char *realloc(char *, int);
int recvfrom(int, char *, int, int /*<<< orphan*/, int /*<<< orphan*/ *,
             int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strstr(char *, char *);
int /*<<< orphan*/ usleep(int);
char *receive_http(int conn) {
  int nr_bytes_received = 0;
  char *headers = malloc(10000 * sizeof(char));
  while (1) {
    if (nr_bytes_received == 10000) {
      headers[10000 - 1] = '\0';
      fprintf(stderr, "No header: %s\n", headers);
      exit(1);
    }
    int nr_bytes = recvfrom(conn, headers + nr_bytes_received,
                            10000 - nr_bytes_received - 1, 0, NULL, NULL);
    if (nr_bytes < 0) {
      free(headers);
      return NULL;
    }
    nr_bytes_received += nr_bytes;
    headers[nr_bytes_received] = '\0';
    if (strstr(headers, "\r\n\r\n")) {
      break;
    }
    usleep(5E4);
  }
  int header_length = strstr(headers, "\r\n\r\n") - headers + 4;
  int body_length = get_content_length(headers);
  int alloc_size = header_length + body_length + 1;
  char *data = realloc(headers, alloc_size * sizeof(char));
  data[alloc_size - 1] = '\0';
  while (nr_bytes_received != header_length + body_length) {
    int nr_bytes = recvfrom(conn, data + nr_bytes_received,
                            alloc_size - nr_bytes_received, 0, NULL, NULL);
    usleep(5E4);
    if (nr_bytes < 0) {
      free(headers);
      return NULL;
    }
    nr_bytes_received += nr_bytes;
  }
  return data;
}


x86 assembly (clang, -O0):
<receive_http>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    $0x2710,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpl   $0x2710,-0x10(%rbp)
jne    11eb <receive_http+0x5b>
mov    -0x18(%rbp),%rax
movb   $0x0,0x270f(%rax)
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe24(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
movslq -0x10(%rbp),%rax
add    %rax,%rsi
mov    $0x2710,%edx
sub    -0x10(%rbp),%edx
sub    $0x1,%edx
xor    %ecx,%ecx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%r8
call   1060 <recvfrom@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1236 <receive_http+0xa6>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    134e <receive_http+0x1be>
mov    -0x1c(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
lea    0xdb9(%rip),%rsi
call   10a0 <strstr@plt>
cmp    $0x0,%rax
je     126a <receive_http+0xda>
jmp    1279 <receive_http+0xe9>
mov    $0xc350,%edi
call   10b0 <usleep@plt>
jmp    11b0 <receive_http+0x20>
mov    -0x18(%rbp),%rdi
lea    0xd8b(%rip),%rsi
call   10a0 <strstr@plt>
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
add    $0x4,%rax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1090 <get_content_length@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%eax
add    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
movslq -0x28(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%esi
call   1070 <realloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
mov    -0x20(%rbp),%ecx
add    -0x24(%rbp),%ecx
cmp    %ecx,%eax
je     1346 <receive_http+0x1b6>
mov    -0xc(%rbp),%edi
mov    -0x30(%rbp),%rsi
movslq -0x10(%rbp),%rax
add    %rax,%rsi
mov    -0x28(%rbp),%edx
sub    -0x10(%rbp),%edx
xor    %ecx,%ecx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%r8
call   1060 <recvfrom@plt>
mov    %eax,-0x34(%rbp)
mov    $0xc350,%edi
call   10b0 <usleep@plt>
cmpl   $0x0,-0x34(%rbp)
jge    1338 <receive_http+0x1a8>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    134e <receive_http+0x1be>
mov    -0x34(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    12d7 <receive_http+0x147>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receive_http>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r12d
mov    $0x2710,%edi
call   1050 <malloc@plt>
mov    %rax,%r13
xor    %ebx,%ebx
lea    0xe5b(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movslq %ebx,%rax
lea    (%rax,%r13,1),%rsi
mov    $0x270f,%edx
sub    %eax,%edx
mov    %r12d,%edi
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1060 <recvfrom@plt>
test   %eax,%eax
js     12b6 <receive_http+0x126>
add    %eax,%ebx
movslq %ebx,%rax
movb   $0x0,0x0(%r13,%rax,1)
mov    %r13,%rdi
mov    %r14,%rsi
call   10a0 <strstr@plt>
test   %rax,%rax
jne    123d <receive_http+0xad>
mov    $0xc350,%edi
call   10b0 <usleep@plt>
cmp    $0x2710,%ebx
jne    11c0 <receive_http+0x30>
movb   $0x0,0x270f(%r13)
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
lea    0xdd5(%rip),%rsi
mov    %r13,%rdx
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %rax,%rbp
sub    %r13d,%ebp
mov    %r13,%rdi
call   1090 <get_content_length@plt>
lea    (%rax,%rbp,1),%ecx
add    $0x4,%ecx
mov    %ecx,0x4(%rsp)
lea    (%rax,%rbp,1),%r15d
add    $0x5,%r15d
mov    %r13,%rdi
mov    %r15d,%esi
call   1070 <realloc@plt>
mov    %rax,%r14
movslq 0x4(%rsp),%rax
movb   $0x0,(%r14,%rax,1)
cmp    %eax,%ebx
je     12c1 <receive_http+0x131>
nopl   0x0(%rax)
movslq %ebx,%rax
lea    (%r14,%rax,1),%rsi
mov    %r15d,%edx
sub    %eax,%edx
mov    %r12d,%edi
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1060 <recvfrom@plt>
mov    %eax,%ebp
mov    $0xc350,%edi
call   10b0 <usleep@plt>
test   %ebp,%ebp
js     12b6 <receive_http+0x126>
add    %ebp,%ebx
cmp    0x4(%rsp),%ebx
jne    1280 <receive_http+0xf0>
jmp    12c1 <receive_http+0x131>
mov    %r13,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@stderr = global i32 0
@.str = [15 x i8] c"No header: %s\0A\00"
@.str.1 = [5 x i8] c"\0D\0A\0D\0A\00"
define i8* @receive_http(i32 %0) {
%2 = tail call dereferenceable_or_null(10000) i8* @malloc(i32 10000)
br label %8
3: ; preds = %21
%4 = getelementptr inbounds i8, i8* %2, i64 9999
store i8 0, i8* %4
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %2)
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %1, %21
%9 = phi i32 [ 0, %1 ], [ %16, %21 ]
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %2, i64 %10
%12 = sub i32 9999, %9
%13 = tail call i32 @recvfrom(i32 %0, i8* %11, i32 %12, i32 0, i32* null, i32* null)
%14 = icmp slt i32 %13, 0
br i1 %14, label %48, label %15
15: ; preds = %8
%16 = add nsw i32 %13, %9
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %2, i64 %17
store i8 0, i8* %18
%19 = tail call i8* @strstr(i8* dereferenceable(1) %2, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = tail call i32 @usleep(i32 50000)
%23 = icmp eq i32 %16, 10000
br i1 %23, label %3, label %8
24: ; preds = %15
%25 = ptrtoint i8* %19 to i64
%26 = ptrtoint i8* %2 to i64
%27 = sub i64 %25, %26
%28 = trunc i64 %27 to i32
%29 = tail call i32 @get_content_length(i8* %2)
%30 = add i32 %29, 4
%31 = add i32 %30, %28
%32 = add nsw i32 %31, 1
%33 = tail call i8* @realloc(i8* %2, i32 %32)
%34 = sext i32 %31 to i64
%35 = getelementptr inbounds i8, i8* %33, i64 %34
store i8 0, i8* %35
%36 = icmp eq i32 %16, %31
br i1 %36, label %50, label %39
37: ; preds = %39
%38 = icmp eq i32 %47, %31
br i1 %38, label %50, label %39
39: ; preds = %24, %37
%40 = phi i32 [ %47, %37 ], [ %16, %24 ]
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %33, i64 %41
%43 = sub nsw i32 %32, %40
%44 = tail call i32 @recvfrom(i32 %0, i8* %42, i32 %43, i32 0, i32* null, i32* null)
%45 = tail call i32 @usleep(i32 50000)
%46 = icmp slt i32 %44, 0
%47 = add nsw i32 %44, %40
br i1 %46, label %48, label %37
48: ; preds = %8, %39
%49 = tail call i32 @free(i8* %2)
br label %50
50: ; preds = %37, %48, %24
%51 = phi i8* [ %33, %24 ], [ null, %48 ], [ %33, %37 ]
ret i8* %51
}