#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 EOF;
char getchar();
int ReadLine(char s[], int maxlen) {
  char buffer;
  int i = 0;
  bool chars_remain = true;
  while (chars_remain) {
    buffer = getchar();
    if ((buffer == '\n') || (buffer == EOF))
      chars_remain = false;
    else if (i < maxlen - 1) {
      s[i] = buffer;
      i++;
    }
  }
  s[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,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     11a2 <ReadLine+0x92>
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %al,-0xd(%rbp)
movsbl -0xd(%rbp),%eax
cmp    $0xa,%eax
je     1164 <ReadLine+0x54>
movsbl -0xd(%rbp),%eax
mov    0x2e8f(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    1170 <ReadLine+0x60>
movl   $0x0,-0x18(%rbp)
jmp    119d <ReadLine+0x8d>
mov    -0x14(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1198 <ReadLine+0x88>
mov    -0xd(%rbp),%dl
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    119d <ReadLine+0x8d>
jmp    112d <ReadLine+0x1d>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReadLine>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <getchar@plt>
cmp    $0xa,%al
je     115e <ReadLine+0x4e>
mov    0x2eb8(%rip),%r15
cmp    (%r15),%al
je     115e <ReadLine+0x4e>
add    $0xffffffff,%ebp
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
cmp    %ebp,%ebx
jge    114e <ReadLine+0x3e>
movslq %ebx,%rcx
mov    %al,(%r14,%rcx,1)
add    $0x1,%ebx
xor    %eax,%eax
call   1030 <getchar@plt>
cmp    $0xa,%al
je     115e <ReadLine+0x4e>
cmp    (%r15),%al
jne    1140 <ReadLine+0x30>
movslq %ebx,%rax
movb   $0x0,(%r14,%rax,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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