#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*/ *);
int read_line(FILE *fp, char *line, int max_length) {
  int i;
  char ch;
  i = 0;
  for (;;) {
    ch = fgetc(fp);
    if (ch == EOF)
      return -1;
    if (ch == '\n') {
      line[i] = '\0';
      return 0;
    }
    if (i < max_length)
      line[i++] = ch;
  }
  return -1;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %al,-0x21(%rbp)
movsbl -0x21(%rbp),%eax
mov    0x2ea7(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    1158 <read_line+0x48>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a6 <read_line+0x96>
movsbl -0x21(%rbp),%eax
cmp    $0xa,%eax
jne    117d <read_line+0x6d>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
jmp    11a6 <read_line+0x96>
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11a1 <read_line+0x91>
mov    -0x21(%rbp),%dl
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x20(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    112a <read_line+0x1a>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r13d
mov    %rsi,%r15
mov    %rdi,%rbx
call   1030 <fgetc@plt>
mov    0x2eb8(%rip),%r12
mov    $0xffffffff,%r14d
cmp    (%r12),%al
je     116e <read_line+0x5e>
xor    %ebp,%ebp
jmp    114e <read_line+0x3e>
mov    %rbx,%rdi
call   1030 <fgetc@plt>
cmp    (%r12),%al
je     116e <read_line+0x5e>
cmp    $0xa,%al
je     1163 <read_line+0x53>
cmp    %r13d,%ebp
jge    1140 <read_line+0x30>
movslq %ebp,%rcx
add    $0x1,%ebp
mov    %al,(%r15,%rcx,1)
jmp    1140 <read_line+0x30>
movslq %ebp,%rax
movb   $0x0,(%r15,%rax,1)
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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