#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 my_getline(char line[], int max_len) {
  int i = 0;
  char c;
  while ((c = getchar()) != EOF && c != '\n' && i < max_len - 1)
    line[i++] = c;
  line[i] = '\0';
  return i;
}


x86 assembly (clang, -O0):
<my_getline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %al,-0x11(%rbp)
movsbl %al,%ecx
mov    0x2eae(%rip),%rax
movsbl (%rax),%edx
xor    %eax,%eax
cmp    %edx,%ecx
mov    %al,-0x12(%rbp)
je     116d <my_getline+0x5d>
movsbl -0x11(%rbp),%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x12(%rbp)
je     116d <my_getline+0x5d>
mov    -0x10(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
setl   %al
mov    %al,-0x12(%rbp)
mov    -0x12(%rbp),%al
test   $0x1,%al
jne    117d <my_getline+0x6d>
jmp    119a <my_getline+0x8a>
mov    -0x11(%rbp),%dl
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x10(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1126 <my_getline+0x16>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_getline>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <getchar@plt>
cmp    $0xa,%al
je     116e <my_getline+0x5e>
mov    0x2eb6(%rip),%r12
cmp    (%r12),%al
je     116e <my_getline+0x5e>
cmp    $0x2,%r15d
jl     116e <my_getline+0x5e>
add    $0xffffffff,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
xor    %eax,%eax
call   1030 <getchar@plt>
cmp    $0xa,%al
je     116e <my_getline+0x5e>
cmp    (%r12),%al
je     116e <my_getline+0x5e>
cmp    %r15,%rbx
jb     1150 <my_getline+0x40>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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