#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 readln(int fd, char *buf, int cnt) {
  int i, rcnt = 0;
  if (cnt < 1)
    return -1;
  while (cnt >= 2) {
    i = read(fd, buf, 1);
    if (i != 1)
      return i;
    if (*buf == '\n')
      break;
    rcnt++;
    buf++;
    cnt--;
  }
  *++buf = '\0';
  rcnt++;
  return rcnt;
}


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)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x1,-0x14(%rbp)
jge    113f <readln+0x2f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d1 <readln+0xc1>
jmp    1144 <readln+0x34>
cmpl   $0x2,-0x14(%rbp)
jl     11af <readln+0x9f>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x1,-0x18(%rbp)
je     1177 <readln+0x67>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d1 <readln+0xc1>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    118c <readln+0x7c>
jmp    11af <readln+0x9f>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1144 <readln+0x34>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x0,0x1(%rax)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readln>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %edx,%edx
jle    118c <readln+0x7c>
mov    %rsi,%rbx
mov    $0x1,%r14d
cmp    $0x1,%edx
je     1186 <readln+0x76>
mov    %edi,%r15d
mov    $0x2,%ebp
cmovle %edx,%ebp
mov    %edx,%r12d
sub    %ebp,%r12d
add    $0x2,%r12d
sub    %edx,%ebp
mov    $0x1,%r14d
nopw   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    1194 <readln+0x84>
cmpb   $0xa,(%rbx)
je     1186 <readln+0x76>
add    $0x1,%rbx
lea    (%r14,%rbp,1),%eax
add    $0x1,%eax
mov    %r14d,%ecx
add    $0x1,%ecx
mov    %ecx,%r14d
cmp    $0x2,%eax
jne    1150 <readln+0x40>
mov    %r12d,%r14d
movb   $0x0,0x1(%rbx)
jmp    1197 <readln+0x87>
mov    $0xffffffff,%r14d
jmp    1197 <readln+0x87>
mov    %eax,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
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 i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %9
%10 = load i32, i32* %7
%11 = icmp slt i32 %10, 1
br i1 %11, label %12, label %13
12: ; preds = %3
store i32 -1, i32* %4
br label %44
13: ; preds = %3
br label %14
14: ; preds = %31, %13
%15 = load i32, i32* %7
%16 = icmp sge i32 %15, 2
br i1 %16, label %17, label %38
17: ; preds = %14
%18 = load i32, i32* %5
%19 = load i8*, i8** %6
%20 = call i32 @read(i32 %18, i8* %19, i32 1)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp ne i32 %21, 1
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = load i32, i32* %8
store i32 %24, i32* %4
br label %44
25: ; preds = %17
%26 = load i8*, i8** %6
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 10
br i1 %29, label %30, label %31
30: ; preds = %25
br label %38
31: ; preds = %25
%32 = load i32, i32* %9
%33 = add nsw i32 %32, 1
store i32 %33, i32* %9
%34 = load i8*, i8** %6
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %6
%36 = load i32, i32* %7
%37 = add nsw i32 %36, -1
store i32 %37, i32* %7
br label %14
38: ; preds = %30, %14
%39 = load i8*, i8** %6
%40 = getelementptr inbounds i8, i8* %39, i32 1
store i8* %40, i8** %6
store i8 0, i8* %40
%41 = load i32, i32* %9
%42 = add nsw i32 %41, 1
store i32 %42, i32* %9
%43 = load i32, i32* %9
store i32 %43, i32* %4
br label %44
44: ; preds = %38, %23, %12
%45 = load i32, i32* %4
ret i32 %45
}

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