#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 /*<<< orphan*/ rawoff();
int /*<<< orphan*/ rawon();
int read(int /*<<< orphan*/, char *, int);
int readln(int fd, char *buf, int len) {
  int n;
  char ch;
  rawoff();
  for (n = 0; n < len;) {
    if (read(0, &ch, 1) != 1)
      return n;
    buf[n++] = ch;
    if (ch == '\n')
      break;
  }
  rawon();
  return n;
}


x86 assembly (clang, -O0):
<readln>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    $0x0,%al
call   1030 <rawoff@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11af <readln+0x7f>
xor    %edi,%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
je     1180 <readln+0x50>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11bc <readln+0x8c>
mov    -0x19(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x18(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
movsbl -0x19(%rbp),%eax
cmp    $0xa,%eax
jne    11aa <readln+0x7a>
jmp    11af <readln+0x7f>
jmp    1150 <readln+0x20>
mov    $0x0,%al
call   1050 <rawon@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readln>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edx,%r12d
mov    %rsi,%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <rawoff@plt>
test   %r12d,%r12d
cmovle %ebx,%r12d
lea    0x7(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmp    %rbx,%r12
je     118d <readln+0x5d>
xor    %edi,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
jne    1196 <readln+0x66>
movzbl 0x7(%rsp),%eax
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    $0xa,%al
jne    1160 <readln+0x30>
mov    %ebx,%r12d
xor    %eax,%eax
call   1050 <rawon@plt>
jmp    1199 <readln+0x69>
mov    %ebx,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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