#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 */
scalar_t__ read(int, char *, int);
size_t readline(int fd, char *buf, size_t len) {
  size_t pos = 0;
  while (pos < len && read(fd, &buf[pos], 1) && buf[pos] != '\n')
    pos++;
  if (pos < len)
    buf[pos] = '\0';
  return pos;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rcx
xor    %eax,%eax
cmp    -0x18(%rbp),%rcx
mov    %al,-0x21(%rbp)
jae    117a <readline+0x6a>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x21(%rbp)
je     117a <readline+0x6a>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    118a <readline+0x7a>
jmp    119b <readline+0x8b>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    112b <readline+0x1b>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11b5 <readline+0xa5>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdx,%rdx
je     1164 <readline+0x54>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%r12d
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
lea    (%r15,%rbx,1),%rbp
mov    %r12d,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %rax,%rax
je     115d <readline+0x4d>
cmpb   $0xa,0x0(%rbp)
je     115d <readline+0x4d>
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1130 <readline+0x20>
mov    %r14,%rbx
jmp    1166 <readline+0x56>
movb   $0x0,(%r15,%rbx,1)
jmp    1166 <readline+0x56>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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