#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, size_t buf_len, char *buf) {
  size_t i = 0;
  char c = 0;
  do {
    if (read(fd, &c, sizeof(char)) != sizeof(char))
      return -1;
    buf[i++] = c;
  } while (c != '\n' && i < buf_len);
  return i;
}


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)
movb   $0x0,-0x21(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cltq
cmp    $0x1,%rax
je     1158 <readline+0x48>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a3 <readline+0x93>
mov    -0x21(%rbp),%dl
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x20(%rbp)
mov    %dl,(%rax,%rcx,1)
movsbl -0x21(%rbp),%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x22(%rbp)
je     1191 <readline+0x81>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
setb   %al
mov    %al,-0x22(%rbp)
mov    -0x22(%rbp),%al
test   $0x1,%al
jne    112f <readline+0x1f>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
movb   $0x0,0xf(%rsp)
xor    %ebx,%ebx
lea    0xf(%rsp),%r12
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    115c <readline+0x4c>
movzbl 0xf(%rsp),%eax
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    $0xa,%al
je     1161 <readline+0x51>
cmp    %r15,%rbx
jb     1130 <readline+0x20>
jmp    1161 <readline+0x51>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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