#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 getchar();
int readline(char *arr, int len) {
  int i = 0;
  int ch;
  while ((ch = getchar()) != '\n' && i < len - 1) {
    arr[i] = ch;
    i++;
  }
  arr[i] = '\0';
  return i;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x15(%rbp)
je     1151 <readline+0x41>
mov    -0x10(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
setl   %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    1161 <readline+0x51>
jmp    117f <readline+0x6f>
mov    -0x14(%rbp),%eax
mov    %al,%dl
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1126 <readline+0x16>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %r15
push   %r14
push   %rbx
mov    %esi,%r15d
mov    %rdi,%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <getchar@plt>
cmp    $0xa,%eax
je     1159 <readline+0x49>
cmp    $0x2,%r15d
jl     1159 <readline+0x49>
add    $0xffffffff,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
xor    %eax,%eax
call   1030 <getchar@plt>
cmp    $0xa,%eax
je     1159 <readline+0x49>
cmp    %r15,%rbx
jb     1140 <readline+0x30>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i32 @readline(i8* nocapture writeonly %0, i32 %1) {
%3 = tail call i32 (...) @getchar()
%4 = icmp ne i32 %3, 10
%5 = icmp sgt i32 %1, 1
%6 = and i1 %4, %5
br i1 %6, label %7, label %22
7: ; preds = %2
%8 = add nsw i32 %1, -1
%9 = zext i32 %8 to i64
br label %10
10: ; preds = %7, %10
%11 = phi i64 [ 0, %7 ], [ %15, %10 ]
%12 = phi i32 [ %3, %7 ], [ %16, %10 ]
%13 = trunc i32 %12 to i8
%14 = getelementptr inbounds i8, i8* %0, i64 %11
store i8 %13, i8* %14
%15 = add nuw nsw i64 %11, 1
%16 = tail call i32 (...) @getchar()
%17 = icmp ne i32 %16, 10
%18 = icmp ult i64 %15, %9
%19 = select i1 %17, i1 %18, i1 false
br i1 %19, label %10, label %20
20: ; preds = %10
%21 = trunc i64 %15 to i32
br label %22
22: ; preds = %20, %2
%23 = phi i32 [ 0, %2 ], [ %21, %20 ]
%24 = zext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %0, i64 %24
store i8 0, i8* %25
ret i32 %23
}