#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;
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int read_line(FILE *fd, char *line) {
  int n_bytes = 0;
  while (!feof(fd)) {
    char chr = fgetc(fd);
    if (chr == EOF) {
      break;
    }
    if (chr == '\n') {
      n_bytes++;
      line[n_bytes - 1] = '\0';
      break;
    }
    n_bytes++;
    line[n_bytes - 1] = chr;
  }
  return n_bytes == 0 ? 0 : 1;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1155 <read_line+0x35>
jmp    11c9 <read_line+0xa9>
mov    -0x8(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %al,-0x15(%rbp)
movsbl -0x15(%rbp),%eax
mov    0x2e7c(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    117c <read_line+0x5c>
jmp    11c9 <read_line+0xa9>
movsbl -0x15(%rbp),%eax
cmp    $0xa,%eax
jne    11a8 <read_line+0x88>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11c9 <read_line+0xa9>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x15(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1137 <read_line+0x17>
mov    -0x14(%rbp),%edx
mov    $0x1,%eax
xor    %ecx,%ecx
cmp    $0x0,%edx
cmove  %ecx,%eax
add    $0x20,%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   1040 <feof@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
je     1147 <read_line+0x27>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
xor    %ebx,%ebx
mov    0x2e98(%rip),%r12
mov    %r15,%rdi
call   1030 <fgetc@plt>
cmp    (%r12),%al
je     117d <read_line+0x5d>
cmp    $0xa,%al
je     1186 <read_line+0x66>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
mov    %r15,%rdi
call   1040 <feof@plt>
test   %eax,%eax
je     1150 <read_line+0x30>
mov    $0x1,%eax
jmp    113b <read_line+0x1b>
xor    %eax,%eax
test   %ebx,%ebx
setne  %al
jmp    113b <read_line+0x1b>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
mov    $0x1,%eax
jmp    113b <read_line+0x1b>

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

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