#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 read(int, char *, int);
int reads(int fd, char *buf, int count) {
  int i, j;
  char c;
  for (i = 0; i < (count - 1); i++) {
    j = read(fd, &c, 1);
    if ((j == 0) || (c == '\r') || (c == '\n')) {
      *buf = 0;
      return 1;
    }
    *buf++ = c;
  }
  *buf = 0;
  return 0;
}


x86 assembly (clang, -O0):
<reads>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11a7 <reads+0x97>
mov    -0x8(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1172 <reads+0x62>
movsbl -0x1d(%rbp),%eax
cmp    $0xd,%eax
je     1172 <reads+0x62>
movsbl -0x1d(%rbp),%eax
cmp    $0xa,%eax
jne    1185 <reads+0x75>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    11b5 <reads+0xa5>
mov    -0x1d(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1129 <reads+0x19>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reads>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
xor    %r14d,%r14d
cmp    $0x2,%edx
jl     1174 <reads+0x64>
mov    %edx,%ebp
mov    %edi,%r15d
add    $0xffffffff,%ebp
lea    0xf(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %eax,%eax
je     116e <reads+0x5e>
movzbl 0xf(%rsp),%eax
cmp    $0xd,%al
je     116e <reads+0x5e>
cmp    $0xa,%al
je     116e <reads+0x5e>
mov    %al,(%rbx)
add    $0x1,%rbx
add    $0xffffffff,%ebp
jne    1140 <reads+0x30>
jmp    1174 <reads+0x64>
mov    $0x1,%r14d
movb   $0x0,(%rbx)
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @reads(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 %21
7: ; preds = %3, %17
%8 = phi i32 [ %19, %17 ], [ 0, %3 ]
%9 = phi i8* [ %18, %17 ], [ %1, %3 ]
%10 = call i32 @read(i32 %0, i8* %4, i32 1)
%11 = icmp eq i32 %10, 0
%12 = load i8, i8* %4
%13 = icmp eq i8 %12, 13
%14 = select i1 %11, i1 true, i1 %13
%15 = icmp eq i8 %12, 10
%16 = select i1 %14, i1 true, i1 %15
br i1 %16, label %21, label %17
17: ; preds = %7
%18 = getelementptr inbounds i8, i8* %9, i64 1
store i8 %12, i8* %9
%19 = add nuw nsw i32 %8, 1
%20 = icmp eq i32 %19, %5
br i1 %20, label %21, label %7
21: ; preds = %7, %17, %3
%22 = phi i8* [ %1, %3 ], [ %18, %17 ], [ %9, %7 ]
%23 = phi i32 [ 0, %3 ], [ 0, %17 ], [ 1, %7 ]
store i8 0, i8* %22
ret i32 %23
}