#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 my_getline(char *line) {
  int c, length;
  for (length = 0; (c = getchar()) != EOF; ++length) {
    line[length] = c;
    if (c == '\n') {
      line[++length] = '\0';
      break;
    }
  }
  return length;
}


x86 assembly (clang, -O0):
<my_getline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %eax,-0xc(%rbp)
mov    0x2eb4(%rip),%rcx
cmp    (%rcx),%eax
je     1182 <my_getline+0x72>
mov    -0xc(%rbp),%eax
mov    %al,%dl
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
cmpl   $0xa,-0xc(%rbp)
jne    116f <my_getline+0x5f>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,-0x10(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1182 <my_getline+0x72>
jmp    1174 <my_getline+0x64>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1123 <my_getline+0x13>
mov    -0x10(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_getline>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <getchar@plt>
mov    0x2ec0(%rip),%r15
cmp    (%r15),%eax
je     1152 <my_getline+0x42>
xor    %ebx,%ebx
nop
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    $0xa,%eax
je     114b <my_getline+0x3b>
xor    %eax,%eax
call   1030 <getchar@plt>
cmp    (%r15),%eax
jne    1130 <my_getline+0x20>
jmp    1152 <my_getline+0x42>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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