#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 atoi(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ getHeaderValue(char *);
int /*<<< orphan*/ printf(char *, ...);
char *readLine(int);
scalar_t__ strlen(char *);
scalar_t__ strstr(char *, char *);
int readHeaders(int socket, int debug) {
  char *line;
  int contentLength = -1;
  if (debug) {
    printf("Response:\n");
  }
  line = readLine(socket);
  while (strlen(line) != 0) {
    if (strstr(line, "Content-Length")) {
      contentLength = atoi(getHeaderValue(line));
    }
    if (debug) {
      printf("%s\n", line);
    }
    free(line);
    line = readLine(socket);
  }
  free(line);
  if (debug) {
    printf("\n");
  }
  return contentLength;
}


x86 assembly (clang, -O0):
<readHeaders>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0xffffffff,-0x14(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     119d <readHeaders+0x2d>
lea    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x4(%rbp),%edi
call   1070 <readLine@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%rax
je     121f <readHeaders+0xaf>
mov    -0x10(%rbp),%rdi
lea    0xe44(%rip),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
je     11e9 <readHeaders+0x79>
mov    -0x10(%rbp),%rdi
call   1060 <getHeaderValue@plt>
mov    %eax,%edi
call   1080 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1205 <readHeaders+0x95>
mov    -0x10(%rbp),%rsi
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%edi
call   1070 <readLine@plt>
mov    %rax,-0x10(%rbp)
jmp    11a9 <readHeaders+0x39>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x8(%rbp)
je     1240 <readHeaders+0xd0>
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readHeaders>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r15d
mov    %edi,%ebp
test   %esi,%esi
je     118d <readHeaders+0x1d>
lea    0xe87(%rip),%rdi
call   1050 <puts@plt>
mov    %ebp,%edi
call   1070 <readLine@plt>
mov    %rax,%rbx
cmpb   $0x0,(%rax)
je     11f8 <readHeaders+0x88>
mov    $0xffffffff,%r14d
lea    0xe57(%rip),%r12
jmp    11c7 <readHeaders+0x57>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%edi
call   1070 <readLine@plt>
mov    %rax,%rbx
cmpb   $0x0,(%rax)
je     11fe <readHeaders+0x8e>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1090 <strstr@plt>
test   %rax,%rax
je     11e9 <readHeaders+0x79>
mov    %rbx,%rdi
call   1060 <getHeaderValue@plt>
mov    %eax,%edi
call   1080 <atoi@plt>
mov    %eax,%r14d
test   %r15d,%r15d
je     11b0 <readHeaders+0x40>
mov    %rbx,%rdi
call   1050 <puts@plt>
jmp    11b0 <readHeaders+0x40>
mov    $0xffffffff,%r14d
mov    %rbx,%rdi
call   1030 <free@plt>
test   %r15d,%r15d
je     1215 <readHeaders+0xa5>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"Response:\0A\00"
@.str.1 = [15 x i8] c"Content-Length\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@.str.3 = [2 x i8] c"\0A\00"
define i32 @readHeaders(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 -1, i32* %6
%7 = load i32, i32* %4
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = load i32, i32* %3
%13 = call i8* @readLine(i32 %12)
store i8* %13, i8** %5
br label %14
14: ; preds = %32, %11
%15 = load i8*, i8** %5
%16 = call i64 @strlen(i8* %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %37
18: ; preds = %14
%19 = load i8*, i8** %5
%20 = call i64 @strstr(i8* %19, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %18
%23 = load i8*, i8** %5
%24 = call i32 @getHeaderValue(i8* %23)
%25 = call i32 @atoi(i32 %24)
store i32 %25, i32* %6
br label %26
26: ; preds = %22, %18
%27 = load i32, i32* %4
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load i8*, i8** %5
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %30)
br label %32
32: ; preds = %29, %26
%33 = load i8*, i8** %5
%34 = call i32 @free(i8* %33)
%35 = load i32, i32* %3
%36 = call i8* @readLine(i32 %35)
store i8* %36, i8** %5
br label %14
37: ; preds = %14
%38 = load i8*, i8** %5
%39 = call i32 @free(i8* %38)
%40 = load i32, i32* %4
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %37
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %44
44: ; preds = %42, %37
%45 = load i32, i32* %6
ret i32 %45
}

LLVM IR (-O2):
@.str.1 = [15 x i8] c"Content-Length\00"
@str = [10 x i8] c"Response:\00"
define i32 @readHeaders(i32 %0, i32 %1) {
%3 = icmp eq i32 %1, 0
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
br label %6
6: ; preds = %4, %2
%7 = tail call i8* @readLine(i32 %0)
%8 = load i8, i8* %7
%9 = icmp eq i8 %8, 0
br i1 %9, label %27, label %10
10: ; preds = %6, %22
%11 = phi i32 [ %19, %22 ], [ -1, %6 ]
%12 = phi i8* [ %24, %22 ], [ %7, %6 ]
%13 = tail call i64 @strstr(i8* %12, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i64 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %10
%16 = tail call i32 @getHeaderValue(i8* %12)
%17 = tail call i32 @atoi(i32 %16)
br label %18
18: ; preds = %15, %10
%19 = phi i32 [ %17, %15 ], [ %11, %10 ]
br i1 %3, label %22, label %20
20: ; preds = %18
%21 = tail call i32 @puts(i8* %12)
br label %22
22: ; preds = %20, %18
%23 = tail call i32 @free(i8* %12)
%24 = tail call i8* @readLine(i32 %0)
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 0
br i1 %26, label %27, label %10
27: ; preds = %22, %6
%28 = phi i8* [ %7, %6 ], [ %24, %22 ]
%29 = phi i32 [ -1, %6 ], [ %19, %22 ]
%30 = tail call i32 @free(i8* %28)
br i1 %3, label %33, label %31
31: ; preds = %27
%32 = tail call i32 @putchar(i32 10)
br label %33
33: ; preds = %31, %27
ret i32 %29
}