#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 */
int EOF;
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
size_t freadline(FILE *fp, char *buf, size_t n) {
  size_t len = 0;
  while (!feof(fp)) {
    int c = fgetc(fp);
    if (c == EOF || len >= n)
      break;
    buf[len++] = (char)c;
    if (c == '\n')
      break;
  }
  buf[len] = 0;
  return len;
}


x86 assembly (clang, -O0):
<freadline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    115a <freadline+0x3a>
jmp    11ba <freadline+0x9a>
mov    -0x8(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2e78(%rip),%rcx
cmp    (%rcx),%eax
je     1186 <freadline+0x66>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     118b <freadline+0x6b>
jmp    11ba <freadline+0x9a>
mov    -0x24(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x20(%rbp)
mov    %dl,(%rax,%rcx,1)
cmpl   $0xa,-0x24(%rbp)
jne    11b5 <freadline+0x95>
jmp    11ba <freadline+0x9a>
jmp    113c <freadline+0x1c>
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):
<freadline>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
xor    %ebx,%ebx
mov    0x2ead(%rip),%r13
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1040 <feof@plt>
test   %eax,%eax
jne    116c <freadline+0x4c>
mov    %r12,%rdi
call   1030 <fgetc@plt>
cmp    0x0(%r13),%eax
je     116c <freadline+0x4c>
cmp    %r15,%rbx
jae    116c <freadline+0x4c>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    $0xa,%eax
jne    1140 <freadline+0x20>
movb   $0x0,(%r14,%rbx,1)
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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