#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 EOF;
int getchar();
int _getline(void) {
  int character, counter;
  extern char line[];
  counter = 0;
  while (counter < 1000 - 1 && (character = getchar()) != EOF &&
         character != '\n') {
    line[counter] = character;
    ++counter;
  }
  if (character == '\n') {
    line[counter] = character;
    ++counter;
  }
  line[counter] = '\0';
  return counter;
}


x86 assembly (clang, -O0):
<_getline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x8(%rbp)
xor    %eax,%eax
cmpl   $0x3e7,-0x8(%rbp)
mov    %al,-0x9(%rbp)
jge    115b <_getline+0x4b>
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %eax,%ecx
mov    %ecx,-0x4(%rbp)
xor    %eax,%eax
mov    0x2e9a(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x9(%rbp)
je     115b <_getline+0x4b>
cmpl   $0xa,-0x4(%rbp)
setne  %al
mov    %al,-0x9(%rbp)
mov    -0x9(%rbp),%al
test   $0x1,%al
jne    116b <_getline+0x5b>
jmp    118c <_getline+0x7c>
mov    -0x4(%rbp),%eax
mov    %al,%dl
movslq -0x8(%rbp),%rcx
mov    0x2e6d(%rip),%rax
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    111f <_getline+0xf>
cmpl   $0xa,-0x4(%rbp)
jne    11b2 <_getline+0xa2>
mov    -0x4(%rbp),%eax
mov    %al,%dl
movslq -0x8(%rbp),%rcx
mov    0x2e42(%rip),%rax
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
movslq -0x8(%rbp),%rcx
mov    0x2e2b(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_getline>:
push   %r15
push   %r14
push   %rbx
mov    0x2ec4(%rip),%r15
mov    0x2ec5(%rip),%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   1030 <getchar@plt>
cmp    $0xa,%eax
je     1159 <_getline+0x49>
cmp    (%r15),%eax
je     1159 <_getline+0x49>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    $0x3e7,%rbx
jne    1130 <_getline+0x20>
mov    $0x3e7,%ebx
jmp    1166 <_getline+0x56>
cmp    $0xa,%eax
jne    1166 <_getline+0x56>
movb   $0xa,(%r14,%rbx,1)
add    $0x1,%ebx
movslq %ebx,%rax
movb   $0x0,(%r14,%rax,1)
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@EOF = global i32 0
@line = external global [0 x i8]
define i32 @_getline() {
br label %1
1: ; preds = %0, %8
%2 = phi i64 [ 0, %0 ], [ %11, %8 ]
%3 = tail call i32 (...) @getchar()
%4 = load i32, i32* @EOF
%5 = icmp ne i32 %3, %4
%6 = icmp ne i32 %3, 10
%7 = and i1 %6, %5
br i1 %7, label %8, label %13
8: ; preds = %1
%9 = trunc i32 %3 to i8
%10 = getelementptr inbounds [0 x i8], [0 x i8]* @line, i64 0, i64 %2
store i8 %9, i8* %10
%11 = add nuw nsw i64 %2, 1
%12 = icmp eq i64 %11, 999
br i1 %12, label %20, label %1
13: ; preds = %1
%14 = trunc i64 %2 to i32
%15 = icmp eq i32 %3, 10
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = and i64 %2, 4294967295
%18 = getelementptr inbounds [0 x i8], [0 x i8]* @line, i64 0, i64 %17
store i8 10, i8* %18
%19 = add nuw nsw i32 %14, 1
br label %20
20: ; preds = %8, %16, %13
%21 = phi i32 [ %19, %16 ], [ %14, %13 ], [ 999, %8 ]
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [0 x i8], [0 x i8]* @line, i64 0, i64 %22
store i8 0, i8* %23
ret i32 %21
}