#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 getline_my(char *line) {
  int i = 0;
  char c = 0;
  while ((c = getchar()) != '\0' && c != '\n' && c != EOF) {
    *line++ = c;
    i++;
  }
  if (c == '\n') {
    *++line = '\n';
    *++line = '\0';
    i += 2;
    ;
  }
  if (c == EOF) {
    return 0;
  }
  return i;
}


x86 assembly (clang, -O0):
<getline_my>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movb   $0x0,-0x15(%rbp)
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %al,-0x15(%rbp)
movsbl %al,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x16(%rbp)
je     116a <getline_my+0x5a>
movsbl -0x15(%rbp),%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x16(%rbp)
je     116a <getline_my+0x5a>
movsbl -0x15(%rbp),%eax
mov    0x2e89(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
setne  %al
mov    %al,-0x16(%rbp)
mov    -0x16(%rbp),%al
test   $0x1,%al
jne    117a <getline_my+0x6a>
jmp    119c <getline_my+0x8c>
mov    -0x15(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1127 <getline_my+0x17>
movsbl -0x15(%rbp),%eax
cmp    $0xa,%eax
jne    11d8 <getline_my+0xc8>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0xa,0x1(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x0,0x1(%rax)
mov    -0x14(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x14(%rbp)
movsbl -0x15(%rbp),%eax
mov    0x2e05(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    11fa <getline_my+0xea>
movl   $0x0,-0x4(%rbp)
jmp    1200 <getline_my+0xf0>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getline_my>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    0x2ec9(%rip),%r15
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <getchar@plt>
test   %al,%al
je     115d <getline_my+0x4d>
cmp    $0xa,%al
je     1152 <getline_my+0x42>
cmp    (%r15),%al
je     114e <getline_my+0x3e>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
jmp    1130 <getline_my+0x20>
cmp    $0xa,%al
jne    115d <getline_my+0x4d>
movw   $0xa,0x1(%r14,%rbx,1)
add    $0x2,%ebx
xor    %ecx,%ecx
cmp    (%r15),%al
cmovne %ebx,%ecx
mov    %ecx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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