#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 */
typedef int /*<<< orphan*/ c;

/* Variables and functions */
int read(int, char *, int);
int httpreadline(int fd, char *buf, int len) {
  char c;
  int size;
  for (len--, size = 0; (size < len); size++) {
    if (read(fd, &c, sizeof(c)) != sizeof(c))
      break;
    if (c == '\n')
      break;
    *buf++ = c;
  }
  *buf = 0;
  return (size);
}


x86 assembly (clang, -O0):
<httpreadline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1194 <httpreadline+0x84>
mov    -0x4(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cltq
cmp    $0x1,%rax
je     1160 <httpreadline+0x50>
jmp    1194 <httpreadline+0x84>
movsbl -0x15(%rbp),%eax
cmp    $0xa,%eax
jne    1172 <httpreadline+0x62>
jmp    1194 <httpreadline+0x84>
mov    -0x15(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1132 <httpreadline+0x22>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<httpreadline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
cmp    $0x2,%edx
jl     116f <httpreadline+0x5f>
mov    %edx,%r14d
mov    %edi,%r13d
add    $0xffffffff,%r14d
mov    %r14d,%ebp
xor    %ebx,%ebx
lea    0x7(%rsp),%r15
nopw   0x0(%rax,%rax,1)
mov    %r13d,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    1174 <httpreadline+0x64>
movzbl 0x7(%rsp),%eax
cmp    $0xa,%al
je     1174 <httpreadline+0x64>
mov    %al,(%r12,%rbx,1)
add    $0x1,%rbx
cmp    %ebx,%ebp
jne    1140 <httpreadline+0x30>
add    %rbx,%r12
jmp    117a <httpreadline+0x6a>
xor    %r14d,%r14d
jmp    117a <httpreadline+0x6a>
add    %rbx,%r12
mov    %ebx,%r14d
movb   $0x0,(%r12)
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @httpreadline(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i32, i32* %6
%10 = add nsw i32 %9, -1
store i32 %10, i32* %6
store i32 0, i32* %8
br label %11
11: ; preds = %30, %3
%12 = load i32, i32* %8
%13 = load i32, i32* %6
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %33
15: ; preds = %11
%16 = load i32, i32* %4
%17 = call i32 @read(i32 %16, i8* %7, i32 1)
%18 = sext i32 %17 to i64
%19 = icmp ne i64 %18, 1
br i1 %19, label %20, label %21
20: ; preds = %15
br label %33
21: ; preds = %15
%22 = load i8, i8* %7
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 10
br i1 %24, label %25, label %26
25: ; preds = %21
br label %33
26: ; preds = %21
%27 = load i8, i8* %7
%28 = load i8*, i8** %5
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %5
store i8 %27, i8* %28
br label %30
30: ; preds = %26
%31 = load i32, i32* %8
%32 = add nsw i32 %31, 1
store i32 %32, i32* %8
br label %11
33: ; preds = %25, %20, %11
%34 = load i8*, i8** %5
store i8 0, i8* %34
%35 = load i32, i32* %8
ret i32 %35
}

LLVM IR (-O2):
define i32 @httpreadline(i32 %0, i8* nocapture writeonly %1, i32 %2) {
%4 = alloca i8
%5 = add i32 %2, -1
%6 = icmp sgt i32 %2, 1
br i1 %6, label %7, label %19
7: ; preds = %3, %15
%8 = phi i32 [ %17, %15 ], [ 0, %3 ]
%9 = phi i8* [ %16, %15 ], [ %1, %3 ]
%10 = call i32 @read(i32 %0, i8* %4, i32 1)
%11 = icmp ne i32 %10, 1
%12 = load i8, i8* %4
%13 = icmp eq i8 %12, 10
%14 = select i1 %11, i1 true, i1 %13
br i1 %14, label %19, label %15
15: ; preds = %7
%16 = getelementptr inbounds i8, i8* %9, i64 1
store i8 %12, i8* %9
%17 = add nuw nsw i32 %8, 1
%18 = icmp eq i32 %17, %5
br i1 %18, label %19, label %7
19: ; preds = %15, %7, %3
%20 = phi i8* [ %1, %3 ], [ %9, %7 ], [ %16, %15 ]
%21 = phi i32 [ 0, %3 ], [ %8, %7 ], [ %5, %15 ]
store i8 0, i8* %20
ret i32 %21
}