#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 */
char readb();
const char *readln(char *buffer, unsigned long size) {
  if (size == 0)
    return 0;
  for (int ix = 0; ix < size; ix++) {
    buffer[ix] = '\0';
    char c = readb();
    if (c == 0)
      return 0;
    if (c == '\n')
      return buffer;
    buffer[ix] = c;
  }
  buffer[size - 1] = '\0';
  return buffer;
}


x86 assembly (clang, -O0):
<readln>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1138 <readln+0x28>
movq   $0x0,-0x8(%rbp)
jmp    11cb <readln+0xbb>
movl   $0x0,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11b3 <readln+0xa3>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    $0x0,%al
call   1030 <readb@plt>
mov    %al,-0x1d(%rbp)
movsbl -0x1d(%rbp),%eax
cmp    $0x0,%eax
jne    117d <readln+0x6d>
movq   $0x0,-0x8(%rbp)
jmp    11cb <readln+0xbb>
movsbl -0x1d(%rbp),%eax
cmp    $0xa,%eax
jne    1197 <readln+0x87>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11cb <readln+0xbb>
mov    -0x1d(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    113f <readln+0x2f>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readln>:
push   %r15
push   %r14
push   %rbx
test   %rsi,%rsi
je     1159 <readln+0x49>
mov    %rsi,%r14
mov    %rdi,%r15
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movb   $0x0,(%r15,%rbx,1)
xor    %eax,%eax
call   1030 <readb@plt>
test   %al,%al
je     1159 <readln+0x49>
cmp    $0xa,%al
je     115c <readln+0x4c>
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1130 <readln+0x20>
movb   $0x0,-0x1(%r14,%r15,1)
jmp    115c <readln+0x4c>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @readln(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8
store i8* %0, i8** %4
store i64 %1, i64* %5
%8 = load i64, i64* %5
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %2
store i8* null, i8** %3
br label %48
11: ; preds = %2
store i32 0, i32* %6
br label %12
12: ; preds = %39, %11
%13 = load i32, i32* %6
%14 = sext i32 %13 to i64
%15 = load i64, i64* %5
%16 = icmp ult i64 %14, %15
br i1 %16, label %17, label %42
17: ; preds = %12
%18 = load i8*, i8** %4
%19 = load i32, i32* %6
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
store i8 0, i8* %21
%22 = call signext i8 (...) @readb()
store i8 %22, i8* %7
%23 = load i8, i8* %7
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %17
store i8* null, i8** %3
br label %48
27: ; preds = %17
%28 = load i8, i8* %7
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 10
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = load i8*, i8** %4
store i8* %32, i8** %3
br label %48
33: ; preds = %27
%34 = load i8, i8* %7
%35 = load i8*, i8** %4
%36 = load i32, i32* %6
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
store i8 %34, i8* %38
br label %39
39: ; preds = %33
%40 = load i32, i32* %6
%41 = add nsw i32 %40, 1
store i32 %41, i32* %6
br label %12
42: ; preds = %12
%43 = load i8*, i8** %4
%44 = load i64, i64* %5
%45 = sub i64 %44, 1
%46 = getelementptr inbounds i8, i8* %43, i64 %45
store i8 0, i8* %46
%47 = load i8*, i8** %4
store i8* %47, i8** %3
br label %48
48: ; preds = %42, %31, %26, %10
%49 = load i8*, i8** %3
ret i8* %49
}

LLVM IR (-O2):
define i8* @readln(i8* writeonly %0, i64 %1) {
%3 = icmp eq i64 %1, 0
br i1 %3, label %15, label %4
4: ; preds = %2, %8
%5 = phi i64 [ %9, %8 ], [ 0, %2 ]
%6 = getelementptr inbounds i8, i8* %0, i64 %5
store i8 0, i8* %6
%7 = tail call signext i8 (...) @readb()
switch i8 %7, label %8 [
i8 0, label %14
i8 10, label %15
]
8: ; preds = %4
store i8 %7, i8* %6
%9 = add nuw i64 %5, 1
%10 = icmp eq i64 %9, %1
br i1 %10, label %11, label %4
11: ; preds = %8
%12 = add i64 %1, -1
%13 = getelementptr inbounds i8, i8* %0, i64 %12
store i8 0, i8* %13
br label %15
14: ; preds = %4
br label %15
15: ; preds = %4, %14, %2, %11
%16 = phi i8* [ %0, %11 ], [ null, %2 ], [ null, %14 ], [ %0, %4 ]
ret i8* %16
}