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


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %al,-0x19(%rbp)
mov    -0x18(%rbp),%ecx
xor    %eax,%eax
cmp    -0x14(%rbp),%ecx
mov    %al,-0x31(%rbp)
jge    115e <readline+0x3e>
movsbl -0x19(%rbp),%eax
cmp    $0xa,%eax
setne  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    116e <readline+0x4e>
jmp    1194 <readline+0x74>
mov    -0x19(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %al,-0x19(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1140 <readline+0x20>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    11ba <readline+0x9a>
lea    -0x30(%rbp),%rdi
mov    $0x10,%esi
call   1030 <readline@plt>
movl   $0x0,-0x4(%rbp)
jmp    11cd <readline+0xad>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
xor    %ebp,%ebp
xor    %eax,%eax
call   1040 <getchar@plt>
test   %r14d,%r14d
jle    1168 <readline+0x48>
cmp    $0xa,%al
je     1168 <readline+0x48>
mov    %r14d,%ebx
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
mov    %al,(%r15,%rbp,1)
xor    %eax,%eax
call   1040 <getchar@plt>
add    $0x1,%rbp
cmp    %rbx,%rbp
jae    1168 <readline+0x48>
cmp    $0xa,%al
jne    1150 <readline+0x30>
cmp    %r14d,%ebp
jne    117e <readline+0x5e>
mov    %rsp,%rdi
mov    $0x10,%esi
call   1030 <readline@plt>
xor    %eax,%eax
jmp    118a <readline+0x6a>
mov    %ebp,%eax
movb   $0x0,(%r15,%rax,1)
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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