#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 readLine(int fd, char *buf, int max) {
  int n = 0;
  while (n < max) {
    if (read(fd, buf, 1) != 1) {
      n = -n;
      break;
    }
    n++;
    if (*buf == '\015')
      continue;
    if (*buf == '\012')
      break;
    buf++;
  }
  *buf = 0;
  return n;
}


x86 assembly (clang, -O0):
<readLine>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11a0 <readLine+0x90>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
je     115c <readLine+0x4c>
xor    %eax,%eax
sub    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11a0 <readLine+0x90>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xd,%eax
jne    117a <readLine+0x6a>
jmp    1129 <readLine+0x19>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    118f <readLine+0x7f>
jmp    11a0 <readLine+0x90>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1129 <readLine+0x19>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readLine>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %rsi,%r12
mov    %edi,%r15d
xor    %ebp,%ebp
cmp    %r14d,%ebp
mov    %r14d,%ebx
cmovg  %ebp,%ebx
nopl   0x0(%rax)
cmp    %ebp,%ebx
je     115f <readLine+0x4f>
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    1163 <readLine+0x53>
add    $0x1,%ebp
movzbl (%r12),%eax
cmp    $0xd,%al
je     1130 <readLine+0x20>
cmp    $0xa,%al
je     1165 <readLine+0x55>
add    $0x1,%r12
jmp    1123 <readLine+0x13>
mov    %ebx,%ebp
jmp    1165 <readLine+0x55>
neg    %ebp
movb   $0x0,(%r12)
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @readLine(i32 %0, i8* nocapture %1, i32 %2) {
br label %4
4: ; preds = %19, %3
%5 = phi i8* [ %20, %19 ], [ %1, %3 ]
%6 = phi i32 [ %17, %19 ], [ 0, %3 ]
%7 = call i32 @llvm.smax.i32(i32 %6, i32 %2)
br label %8
8: ; preds = %4, %16
%9 = phi i32 [ %17, %16 ], [ %6, %4 ]
%10 = icmp eq i32 %9, %7
br i1 %10, label %21, label %11
11: ; preds = %8
%12 = tail call i32 @read(i32 %0, i8* %5, i32 1)
%13 = icmp eq i32 %12, 1
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = sub nsw i32 0, %9
br label %21
16: ; preds = %11
%17 = add i32 %9, 1
%18 = load i8, i8* %5
switch i8 %18, label %19 [
i8 13, label %8
i8 10, label %21
]
19: ; preds = %16
%20 = getelementptr inbounds i8, i8* %5, i64 1
br label %4
21: ; preds = %16, %8, %14
%22 = phi i32 [ %15, %14 ], [ %7, %8 ], [ %17, %16 ]
store i8 0, i8* %5
ret i32 %22
}