#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, size_t length) {
  for (size_t i = 0; i < (length - 1); i++) {
    char ch;
    if (read(fd, &ch, 1) < 1)
      return (0);
    if (ch == '\n') {
      buf[i] = '\0';
      break;
    }
    buf[i] = ch;
  }
  return (1);
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jae    11a3 <readline+0x93>
mov    -0x8(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jge    1166 <readline+0x56>
movl   $0x0,-0x4(%rbp)
jmp    11aa <readline+0x9a>
movsbl -0x21(%rbp),%eax
cmp    $0xa,%eax
jne    1184 <readline+0x74>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11a3 <readline+0x93>
mov    -0x21(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    112b <readline+0x1b>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    $0x2,%r12d
add    $0xffffffffffffffff,%rbx
je     1177 <readline+0x67>
mov    %rsi,%r14
mov    %edi,%r13d
xor    %ebp,%ebp
lea    0x7(%rsp),%r15
mov    $0x1,%r12d
nopl   (%rax)
mov    %r13d,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %eax,%eax
jle    1177 <readline+0x67>
movzbl 0x7(%rsp),%eax
cmp    $0xa,%al
je     116c <readline+0x5c>
mov    %al,(%r14,%rbp,1)
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1140 <readline+0x30>
jmp    1171 <readline+0x61>
movb   $0x0,(%r14,%rbp,1)
mov    $0x2,%r12d
xor    %eax,%eax
cmp    $0x2,%r12d
sete   %al
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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