#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
char EOF;
char fgetc(int /*<<< orphan*/ *);
size_t read_line(FILE *stream, char line[]) {
  char ch;
  size_t size = 0;
  while ((ch = fgetc(stream)) != EOF && ch != '\n')
    line[size++] = ch;
  line[size] = '\0';
  return size;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %al,-0x11(%rbp)
movsbl %al,%ecx
mov    0x2eaa(%rip),%rax
movsbl (%rax),%edx
xor    %eax,%eax
cmp    %edx,%ecx
mov    %al,-0x21(%rbp)
je     115b <read_line+0x4b>
movsbl -0x11(%rbp),%eax
cmp    $0xa,%eax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    116b <read_line+0x5b>
jmp    1189 <read_line+0x79>
mov    -0x11(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x20(%rbp)
mov    %dl,(%rax,%rcx,1)
jmp    1128 <read_line+0x18>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <fgetc@plt>
xor    %ebx,%ebx
cmp    $0xa,%al
je     115a <read_line+0x4a>
mov    0x2eb8(%rip),%r12
cmp    (%r12),%al
je     115a <read_line+0x4a>
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
mov    %r15,%rdi
call   1030 <fgetc@plt>
cmp    $0xa,%al
je     115a <read_line+0x4a>
cmp    (%r12),%al
jne    1140 <read_line+0x30>
movb   $0x0,(%r14,%rbx,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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