#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 */
struct gbuf {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ gbuf_add_ch(struct gbuf *, char);
int read(int, char *, int);
scalar_t__ read_timeout(int, int);
int http_read_response(int fd, struct gbuf *buf, int timeout_ms) {
  char prev = 0;
  if (read_timeout(fd, timeout_ms))
    return -1;
  while (1) {
    int rc;
    char ch;
    rc = read(fd, &ch, 1);
    if (rc == -1) {
      return -1;
    }
    if (rc == 0) {
      return -2;
    }
    if (ch == '\r')
      continue;
    if (ch == '\n' && prev == '\n')
      return 0;
    gbuf_add_ch(buf, ch);
    prev = ch;
  }
}


x86 assembly (clang, -O0):
<http_read_response>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movb   $0x0,-0x15(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1050 <read_timeout@plt>
cmp    $0x0,%rax
je     1167 <http_read_response+0x37>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fc <http_read_response+0xcc>
jmp    116c <http_read_response+0x3c>
mov    -0x8(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1196 <http_read_response+0x66>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fc <http_read_response+0xcc>
cmpl   $0x0,-0x1c(%rbp)
jne    11ac <http_read_response+0x7c>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11fc <http_read_response+0xcc>
movsbl -0x1d(%rbp),%eax
cmp    $0xd,%eax
jne    11be <http_read_response+0x8e>
jmp    116c <http_read_response+0x3c>
movsbl -0x1d(%rbp),%eax
cmp    $0xa,%eax
jne    11e4 <http_read_response+0xb4>
movsbl -0x15(%rbp),%eax
cmp    $0xa,%eax
jne    11e4 <http_read_response+0xb4>
movl   $0x0,-0x4(%rbp)
jmp    11fc <http_read_response+0xcc>
mov    -0x10(%rbp),%rdi
movsbl -0x1d(%rbp),%esi
call   1040 <gbuf_add_ch@plt>
mov    -0x1d(%rbp),%al
mov    %al,-0x15(%rbp)
jmp    116c <http_read_response+0x3c>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<http_read_response>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
mov    %edx,%esi
call   1050 <read_timeout@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
je     1162 <http_read_response+0x32>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r12d,%r12d
lea    0xf(%rsp),%r15
jmp    117e <http_read_response+0x4e>
nopl   0x0(%rax)
movsbl %bl,%esi
mov    %r14,%rdi
call   1040 <gbuf_add_ch@plt>
mov    %ebx,%r12d
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
je     1155 <http_read_response+0x25>
test   %eax,%eax
je     11af <http_read_response+0x7f>
movzbl 0xf(%rsp),%ebx
cmp    $0xd,%bl
je     117e <http_read_response+0x4e>
cmp    $0xa,%bl
jne    1170 <http_read_response+0x40>
cmp    $0xa,%r12b
jne    1170 <http_read_response+0x40>
xor    %eax,%eax
jmp    1155 <http_read_response+0x25>
mov    $0xfffffffe,%eax
jmp    1155 <http_read_response+0x25>

LLVM IR (-O0):
%struct.gbuf = type { i32 }
define i32 @http_read_response(i32 %0, %struct.gbuf* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.gbuf*
%7 = alloca i32
%8 = alloca i8
%9 = alloca i32
%10 = alloca i8
store i32 %0, i32* %5
store %struct.gbuf* %1, %struct.gbuf** %6
store i32 %2, i32* %7
store i8 0, i8* %8
%11 = load i32, i32* %5
%12 = load i32, i32* %7
%13 = call i64 @read_timeout(i32 %11, i32 %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 -1, i32* %4
br label %46
16: ; preds = %3
br label %17
17: ; preds = %16, %31, %41
%18 = load i32, i32* %5
%19 = call i32 @read(i32 %18, i8* %10, i32 1)
store i32 %19, i32* %9
%20 = load i32, i32* %9
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %17
store i32 -1, i32* %4
br label %46
23: ; preds = %17
%24 = load i32, i32* %9
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %23
store i32 -2, i32* %4
br label %46
27: ; preds = %23
%28 = load i8, i8* %10
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 13
br i1 %30, label %31, label %32
31: ; preds = %27
br label %17
32: ; preds = %27
%33 = load i8, i8* %10
%34 = sext i8 %33 to i32
%35 = icmp eq i32 %34, 10
br i1 %35, label %36, label %41
36: ; preds = %32
%37 = load i8, i8* %8
%38 = sext i8 %37 to i32
%39 = icmp eq i32 %38, 10
br i1 %39, label %40, label %41
40: ; preds = %36
store i32 0, i32* %4
br label %46
41: ; preds = %36, %32
%42 = load %struct.gbuf*, %struct.gbuf** %6
%43 = load i8, i8* %10
%44 = call i32 @gbuf_add_ch(%struct.gbuf* %42, i8 signext %43)
%45 = load i8, i8* %10
store i8 %45, i8* %8
br label %17
46: ; preds = %40, %26, %22, %15
%47 = load i32, i32* %4
ret i32 %47
}

LLVM IR (-O2):
%struct.gbuf = type { i32 }
define i32 @http_read_response(i32 %0, %struct.gbuf* %1, i32 %2) {
%4 = alloca i8
%5 = tail call i64 @read_timeout(i32 %0, i32 %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %25
7: ; preds = %3, %23
%8 = phi i8 [ %24, %23 ], [ 0, %3 ]
%9 = call i32 @read(i32 %0, i8* %4, i32 1)
switch i32 %9, label %10 [
i32 -1, label %19
i32 0, label %21
]
10: ; preds = %7
%11 = load i8, i8* %4
%12 = icmp eq i8 %11, 13
br i1 %12, label %23, label %13
13: ; preds = %10
%14 = icmp eq i8 %11, 10
%15 = icmp eq i8 %8, 10
%16 = select i1 %14, i1 %15, i1 false
br i1 %16, label %19, label %17
17: ; preds = %13
%18 = tail call i32 @gbuf_add_ch(%struct.gbuf* %1, i8 signext %11)
br label %23
19: ; preds = %13, %7
%20 = phi i32 [ %9, %7 ], [ 0, %13 ]
br label %21
21: ; preds = %7, %19
%22 = phi i32 [ %20, %19 ], [ -2, %7 ]
br label %25
23: ; preds = %10, %17
%24 = phi i8 [ %11, %17 ], [ %8, %10 ]
br label %7
25: ; preds = %21, %3
%26 = phi i32 [ -1, %3 ], [ %22, %21 ]
ret i32 %26
}