#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__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ readBuffer(int, char *, int);
char *readChunks(int, size_t *);
int readLine(int, char *, int);
int /*<<< orphan*/ sscanf(char *, char *, ...);
scalar_t__ strcmp(char *, char *);
char tolower(char);
int getResponse(int fd, char **response) {
  char respHeader[512];
  int contentLen = 0;
  int retCode;
  char contentType[32] = {0};
  char transferEnc[32] = {0};
  int n;
  char *ptr;
  while (1) {
    n = readLine(fd, respHeader, 512 - 1);
    if (n <= 0) {
      return -1;
    }
    if (n > 0 && (*respHeader) == '\0')
      break;
    for (ptr = respHeader; (*ptr != ':' && *ptr); ptr++)
      *ptr = tolower(*ptr);
    sscanf(respHeader, "http/1.%*d %03d", (int *)&retCode);
    sscanf(respHeader, "content-type: %s", contentType);
    sscanf(respHeader, "content-length: %d", &contentLen);
    sscanf(respHeader, "transfer-encoding: %s", transferEnc);
    printf("header: %s\n", respHeader);
  }
  if (contentLen != 0) {
    *response = (char *)malloc(contentLen);
    if (*response == NULL)
      return -1;
    if (readBuffer(fd, *response, contentLen) <= 0) {
      return -1;
    }
  } else if (strcmp(transferEnc, "chunked") == 0) {
    printf("chunked transfer\n");
    size_t len;
    *response = readChunks(fd, &len);
    if (*response == NULL) {
      return -1;
    }
    printf("content len: %d\n", len);
  }
  return retCode;
}


x86 assembly (clang, -O0):
<getResponse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x280,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x214(%rbp)
lea    -0x240(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
lea    -0x260(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x1ff,%edx
call   1070 <readLine@plt>
mov    %eax,-0x264(%rbp)
cmpl   $0x0,-0x264(%rbp)
jg     1205 <getResponse+0x75>
movl   $0xffffffff,-0x4(%rbp)
jmp    1421 <getResponse+0x291>
cmpl   $0x0,-0x264(%rbp)
jle    1227 <getResponse+0x97>
movsbl -0x210(%rbp),%eax
cmp    $0x0,%eax
jne    1227 <getResponse+0x97>
jmp    1334 <getResponse+0x1a4>
lea    -0x210(%rbp),%rax
mov    %rax,-0x270(%rbp)
mov    -0x270(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x3a,%ecx
mov    %al,-0x279(%rbp)
je     1266 <getResponse+0xd6>
mov    -0x270(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x279(%rbp)
mov    -0x279(%rbp),%al
test   $0x1,%al
jne    1279 <getResponse+0xe9>
jmp    12aa <getResponse+0x11a>
mov    -0x270(%rbp),%rax
movsbl (%rax),%edi
call   1080 <tolower@plt>
mov    %al,%cl
mov    -0x270(%rbp),%rax
mov    %cl,(%rax)
mov    -0x270(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x270(%rbp)
jmp    1235 <getResponse+0xa5>
lea    -0x210(%rbp),%rdi
lea    0xd48(%rip),%rsi
lea    -0x218(%rbp),%rdx
mov    $0x0,%al
call   10a0 <sscanf@plt>
lea    -0x210(%rbp),%rdi
lea    -0x240(%rbp),%rdx
lea    0xd35(%rip),%rsi
mov    $0x0,%al
call   10a0 <sscanf@plt>
lea    -0x210(%rbp),%rdi
lea    0xd31(%rip),%rsi
lea    -0x214(%rbp),%rdx
mov    $0x0,%al
call   10a0 <sscanf@plt>
lea    -0x210(%rbp),%rdi
lea    -0x260(%rbp),%rdx
lea    0xd21(%rip),%rsi
mov    $0x0,%al
call   10a0 <sscanf@plt>
lea    -0x210(%rbp),%rsi
lea    0xd22(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11d2 <getResponse+0x42>
cmpl   $0x0,-0x214(%rbp)
je     13a0 <getResponse+0x210>
mov    -0x214(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1370 <getResponse+0x1e0>
movl   $0xffffffff,-0x4(%rbp)
jmp    1421 <getResponse+0x291>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x214(%rbp),%edx
call   1040 <readBuffer@plt>
cmp    $0x0,%rax
jg     139b <getResponse+0x20b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1421 <getResponse+0x291>
jmp    1418 <getResponse+0x288>
lea    -0x260(%rbp),%rdi
lea    0xca8(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1413 <getResponse+0x283>
lea    0xc9a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x278(%rbp),%rsi
call   10b0 <readChunks@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    13fe <getResponse+0x26e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1421 <getResponse+0x291>
mov    -0x278(%rbp),%rsi
lea    0xc64(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1418 <getResponse+0x288>
mov    -0x218(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x280,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getResponse>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x268,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
movl   $0x0,0x8(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
lea    0x60(%rsp),%rsi
mov    $0x1ff,%edx
call   1070 <readLine@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
jle    1348 <getResponse+0x1b8>
mov    %r14,0x10(%rsp)
lea    0x60(%rsp),%rbx
lea    0xe42(%rip),%r14
lea    0x20(%rsp),%r15
lea    0xe4c(%rip),%r12
jmp    1272 <getResponse+0xe2>
mov    %rbx,%rdi
lea    0xdf6(%rip),%rsi
lea    0xc(%rsp),%rdx
xor    %eax,%eax
call   10a0 <sscanf@plt>
mov    %rbx,%rdi
lea    0xdf0(%rip),%rsi
lea    0x40(%rsp),%rdx
xor    %eax,%eax
call   10a0 <sscanf@plt>
mov    %rbx,%rdi
lea    0xdeb(%rip),%rsi
lea    0x8(%rsp),%rdx
xor    %eax,%eax
call   10a0 <sscanf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   10a0 <sscanf@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1ff,%edx
call   1070 <readLine@plt>
test   %eax,%eax
jle    12ea <getResponse+0x15a>
mov    0x60(%rsp),%al
test   %al,%al
je     12b6 <getResponse+0x126>
lea    0x61(%rsp),%r13
test   %al,%al
je     1200 <getResponse+0x70>
nopw   0x0(%rax,%rax,1)
cmp    $0x3a,%al
je     1200 <getResponse+0x70>
movsbl %al,%edi
call   1080 <tolower@plt>
mov    %al,-0x1(%r13)
movzbl 0x0(%r13),%eax
add    $0x1,%r13
test   %al,%al
jne    1290 <getResponse+0x100>
jmp    1200 <getResponse+0x70>
mov    0x8(%rsp),%edi
test   %edi,%edi
je     12f1 <getResponse+0x161>
call   1090 <malloc@plt>
mov    0x10(%rsp),%rcx
mov    %rax,(%rcx)
test   %rax,%rax
je     12ea <getResponse+0x15a>
mov    0x8(%rsp),%edx
mov    %ebp,%edi
mov    %rax,%rsi
call   1050 <readBuffer@plt>
test   %rax,%rax
mov    $0xffffffff,%ebx
jg     1344 <getResponse+0x1b4>
jmp    1348 <getResponse+0x1b8>
mov    $0xffffffff,%ebx
jmp    1348 <getResponse+0x1b8>
lea    0xd5e(%rip),%rsi
lea    0x20(%rsp),%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
mov    $0xffffffff,%ebx
jne    1344 <getResponse+0x1b4>
lea    0xd5c(%rip),%rdi
call   1030 <puts@plt>
lea    0x18(%rsp),%rsi
mov    %ebp,%edi
call   10b0 <readChunks@plt>
mov    0x10(%rsp),%rcx
mov    %rax,(%rcx)
test   %rax,%rax
je     1348 <getResponse+0x1b8>
mov    0x18(%rsp),%rsi
lea    0xd21(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xc(%rsp),%ebx
mov    %ebx,%eax
add    $0x268,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"http/1.%*d %03d\00"
@.str.1 = [17 x i8] c"content-type: %s\00"
@.str.2 = [19 x i8] c"content-length: %d\00"
@.str.3 = [22 x i8] c"transfer-encoding: %s\00"
@.str.4 = [12 x i8] c"header: %s\0A\00"
@.str.5 = [8 x i8] c"chunked\00"
@.str.6 = [18 x i8] c"chunked transfer\0A\00"
@.str.7 = [17 x i8] c"content len: %d\0A\00"
define i32 @getResponse(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [512 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca [32 x i8]
%10 = alloca [32 x i8]
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i64
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
%14 = bitcast [32 x i8]* %9 to i8*
%15 = bitcast [32 x i8]* %10 to i8*
br label %16
16: ; preds = %2, %54
%17 = load i32, i32* %4
%18 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%19 = call i32 @readLine(i32 %17, i8* %18, i32 511)
store i32 %19, i32* %11
%20 = load i32, i32* %11
%21 = icmp sle i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %16
store i32 -1, i32* %3
br label %107
23: ; preds = %16
%24 = load i32, i32* %11
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %23
%27 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %26
br label %67
32: ; preds = %26, %23
%33 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
store i8* %33, i8** %12
br label %34
34: ; preds = %51, %32
%35 = load i8*, i8** %12
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp ne i32 %37, 58
br i1 %38, label %39, label %44
39: ; preds = %34
%40 = load i8*, i8** %12
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = icmp ne i32 %42, 0
br label %44
44: ; preds = %39, %34
%45 = phi i1 [ false, %34 ], [ %43, %39 ]
br i1 %45, label %46, label %54
46: ; preds = %44
%47 = load i8*, i8** %12
%48 = load i8, i8* %47
%49 = call signext i8 @tolower(i8 signext %48)
%50 = load i8*, i8** %12
store i8 %49, i8* %50
br label %51
51: ; preds = %46
%52 = load i8*, i8** %12
%53 = getelementptr inbounds i8, i8* %52, i32 1
store i8* %53, i8** %12
br label %34
54: ; preds = %44
%55 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%56 = call i32 (i8*, i8*, ...) @sscanf(i8* %55, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32* %8)
%57 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%58 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%59 = call i32 (i8*, i8*, ...) @sscanf(i8* %57, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %58)
%60 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%61 = call i32 (i8*, i8*, ...) @sscanf(i8* %60, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32* %7)
%62 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%63 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%64 = call i32 (i8*, i8*, ...) @sscanf(i8* %62, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %63)
%65 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %65)
br label %16
67: ; preds = %31
%68 = load i32, i32* %7
%69 = icmp ne i32 %68, 0
br i1 %69, label %70, label %88
70: ; preds = %67
%71 = load i32, i32* %7
%72 = call i64 @malloc(i32 %71)
%73 = inttoptr i64 %72 to i8*
%74 = load i8**, i8*** %5
store i8* %73, i8** %74
%75 = load i8**, i8*** %5
%76 = load i8*, i8** %75
%77 = icmp eq i8* %76, null
br i1 %77, label %78, label %79
78: ; preds = %70
store i32 -1, i32* %3
br label %107
79: ; preds = %70
%80 = load i32, i32* %4
%81 = load i8**, i8*** %5
%82 = load i8*, i8** %81
%83 = load i32, i32* %7
%84 = call i64 @readBuffer(i32 %80, i8* %82, i32 %83)
%85 = icmp sle i64 %84, 0
br i1 %85, label %86, label %87
86: ; preds = %79
store i32 -1, i32* %3
br label %107
87: ; preds = %79
br label %105
88: ; preds = %67
%89 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%90 = call i64 @strcmp(i8* %89, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%91 = icmp eq i64 %90, 0
br i1 %91, label %92, label %104
92: ; preds = %88
%93 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
%94 = load i32, i32* %4
%95 = call i8* @readChunks(i32 %94, i64* %13)
%96 = load i8**, i8*** %5
store i8* %95, i8** %96
%97 = load i8**, i8*** %5
%98 = load i8*, i8** %97
%99 = icmp eq i8* %98, null
br i1 %99, label %100, label %101
100: ; preds = %92
store i32 -1, i32* %3
br label %107
101: ; preds = %92
%102 = load i64, i64* %13
%103 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i64 %102)
br label %104
104: ; preds = %101, %88
br label %105
105: ; preds = %104, %87
%106 = load i32, i32* %8
store i32 %106, i32* %3
br label %107
107: ; preds = %105, %100, %86, %78, %22
%108 = load i32, i32* %3
ret i32 %108
}

LLVM IR (-O2):
@.str = [16 x i8] c"http/1.%*d %03d\00"
@.str.1 = [17 x i8] c"content-type: %s\00"
@.str.2 = [19 x i8] c"content-length: %d\00"
@.str.3 = [22 x i8] c"transfer-encoding: %s\00"
@.str.4 = [12 x i8] c"header: %s\0A\00"
@.str.5 = [8 x i8] c"chunked\00"
@.str.7 = [17 x i8] c"content len: %d\0A\00"
@str = [17 x i8] c"chunked transfer\00"
define i32 @getResponse(i32 %0, i8** nocapture writeonly %1) {
%3 = alloca [512 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca [32 x i8]
%7 = alloca [32 x i8]
%8 = alloca i64
%9 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%10 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%11 = bitcast i32* %5 to i8*
%12 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%13 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%14 = call i32 @readLine(i32 %0, i8* %9, i32 511)
%15 = icmp slt i32 %14, 1
br i1 %15, label %59, label %16
16: ; preds = %2, %26
%17 = load i8, i8* %9
%18 = icmp eq i8 %17, 0
br i1 %18, label %34, label %19
19: ; preds = %16, %22
%20 = phi i8 [ %25, %22 ], [ %17, %16 ]
%21 = phi i8* [ %24, %22 ], [ %9, %16 ]
switch i8 %20, label %22 [
i8 58, label %26
i8 0, label %26
]
22: ; preds = %19
%23 = call signext i8 @tolower(i8 signext %20)
store i8 %23, i8* %21
%24 = getelementptr inbounds i8, i8* %21, i64 1
%25 = load i8, i8* %24
br label %19
26: ; preds = %19, %19
%27 = call i32 (i8*, i8*, ...) @sscanf(i8* %9, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32* %5)
%28 = call i32 (i8*, i8*, ...) @sscanf(i8* %9, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %12)
%29 = call i32 (i8*, i8*, ...) @sscanf(i8* %9, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%30 = call i32 (i8*, i8*, ...) @sscanf(i8* %9, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %13)
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %9)
%32 = call i32 @readLine(i32 %0, i8* %9, i32 511)
%33 = icmp slt i32 %32, 1
br i1 %33, label %59, label %16
34: ; preds = %16
%35 = load i32, i32* %4
%36 = icmp eq i32 %35, 0
br i1 %36, label %45, label %37
37: ; preds = %34
%38 = call i64 @malloc(i32 %35)
%39 = inttoptr i64 %38 to i8*
store i8* %39, i8** %1
%40 = icmp eq i64 %38, 0
br i1 %40, label %59, label %41
41: ; preds = %37
%42 = load i32, i32* %4
%43 = call i64 @readBuffer(i32 %0, i8* %39, i32 %42)
%44 = icmp slt i64 %43, 1
br i1 %44, label %59, label %56
45: ; preds = %34
%46 = call i64 @strcmp(i8* %13, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %56
48: ; preds = %45
%49 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%50 = bitcast i64* %8 to i8*
%51 = call i8* @readChunks(i32 %0, i64* %8)
store i8* %51, i8** %1
%52 = icmp eq i8* %51, null
br i1 %52, label %58, label %53
53: ; preds = %48
%54 = load i64, i64* %8
%55 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i64 %54)
br label %56
56: ; preds = %53, %45, %41
%57 = load i32, i32* %5
br label %59
58: ; preds = %48
br label %59
59: ; preds = %26, %2, %58, %41, %37, %56
%60 = phi i32 [ %57, %56 ], [ -1, %37 ], [ -1, %41 ], [ -1, %58 ], [ -1, %2 ], [ -1, %26 ]
ret i32 %60
}