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


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xa,%eax
je     116a <read_line+0x5a>
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1165 <read_line+0x55>
mov    -0x10(%rbp),%eax
mov    %al,%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    112d <read_line+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):
<read_line>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <getchar@plt>
cmp    $0xa,%eax
jne    1134 <read_line+0x24>
movslq %ebx,%rax
movb   $0x0,(%r14,%rax,1)
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %ebx,%ebx
jmp    114c <read_line+0x3c>
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <getchar@plt>
cmp    $0xa,%eax
je     1127 <read_line+0x17>
cmp    %ebp,%ebx
jge    1140 <read_line+0x30>
movslq %ebx,%rcx
mov    %al,(%r14,%rcx,1)
add    $0x1,%ebx
jmp    1140 <read_line+0x30>

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

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