#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 */
scalar_t__ fread(char *, int, int, int /*<<< orphan*/ *);
int readline(FILE *fd, char *line) {
  while (fread(line, 1, 1, fd)) {
    if (*line == '\n') {
      *line = '\0';
      return 1;
    }
    line++;
  }
  *line = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
cmp    $0x0,%rax
je     1172 <readline+0x62>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    1161 <readline+0x51>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1180 <readline+0x70>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1120 <readline+0x10>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%rbp
mov    %rsi,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1030 <fread@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     116b <readline+0x5b>
nopw   0x0(%rax,%rax,1)
cmpb   $0xa,(%rbx)
je     1165 <readline+0x55>
add    $0x1,%rbx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1030 <fread@plt>
test   %rax,%rax
jne    1140 <readline+0x30>
jmp    116b <readline+0x5b>
mov    $0x1,%r14d
movb   $0x0,(%rbx)
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @readline(i32* nocapture %0, i8* nocapture %1) {
%3 = tail call i64 @fread(i8* %1, i32 1, i32 1, i32* %0)
%4 = icmp eq i64 %3, 0
br i1 %4, label %13, label %5
5: ; preds = %2, %9
%6 = phi i8* [ %10, %9 ], [ %1, %2 ]
%7 = load i8, i8* %6
%8 = icmp eq i8 %7, 10
br i1 %8, label %13, label %9
9: ; preds = %5
%10 = getelementptr inbounds i8, i8* %6, i64 1
%11 = tail call i64 @fread(i8* %10, i32 1, i32 1, i32* %0)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %5
13: ; preds = %5, %9, %2
%14 = phi i8* [ %1, %2 ], [ %10, %9 ], [ %6, %5 ]
%15 = phi i32 [ 0, %2 ], [ 0, %9 ], [ 1, %5 ]
store i8 0, i8* %14
ret i32 %15
}