#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 fread(char *, int, int, int /*<<< orphan*/ *);
int freadline(FILE *stream, char *buf, int size) {
  char c;
  int n;
  for (n = 0; n < size; n++) {
    if (fread(&c, 1, 1, stream) == 1) {
      if (c == '\n') {
        buf[n] = 0;
        break;
      } else {
        buf[n] = c;
      }
    } else {
      buf[n] = 0;
      break;
    }
  }
  return n;
}


x86 assembly (clang, -O0):
<freadline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11a8 <freadline+0x98>
mov    -0x8(%rbp),%rcx
lea    -0x15(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    1184 <freadline+0x74>
movsbl -0x15(%rbp),%eax
cmp    $0xa,%eax
jne    1171 <freadline+0x61>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11a8 <freadline+0x98>
mov    -0x15(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1195 <freadline+0x85>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11a8 <freadline+0x98>
jmp    119a <freadline+0x8a>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    112a <freadline+0x1a>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<freadline>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %edx,%edx
jle    117c <freadline+0x6c>
mov    %rsi,%r14
mov    %rdi,%r15
mov    %edx,%r13d
xor    %ebx,%ebx
lea    0xf(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    1175 <freadline+0x65>
movzbl 0xf(%rsp),%eax
cmp    $0xa,%al
je     1175 <freadline+0x65>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1140 <freadline+0x30>
mov    %r13d,%ebx
jmp    117e <freadline+0x6e>
movb   $0x0,(%r14,%rbx,1)
jmp    117e <freadline+0x6e>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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