#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*/ SEEK_SET;
scalar_t__ calloc(int, int);
void *fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
char *readLine(FILE *file) {
  int current = ftell(file);
  int n = 0;
  int c;
  do {
    c = fgetc(file);
    n++;
  } while (c != '\n' && c != '\r' && c != EOF);
  n--;
  if (c == EOF) {
    return NULL;
  }
  char *line = (char *)calloc(n + 1, sizeof(char));
  fseek(file, current, SEEK_SET);
  int i;
  for (i = 0; i < n; i++) {
    line[i] = fgetc(file);
  }
  fgetc(file);
  line[i] = '\0';
  return line;
}


x86 assembly (clang, -O0):
<readLine>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <ftell@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0xa,-0x1c(%rbp)
mov    %al,-0x2d(%rbp)
je     11a4 <readLine+0x64>
xor    %eax,%eax
cmpl   $0xd,-0x1c(%rbp)
mov    %al,-0x2d(%rbp)
je     11a4 <readLine+0x64>
mov    -0x1c(%rbp),%eax
mov    0x2e4c(%rip),%rcx
cmp    (%rcx),%eax
setne  %al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
test   $0x1,%al
jne    115f <readLine+0x1f>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2e26(%rip),%rcx
cmp    (%rcx),%eax
jne    11d7 <readLine+0x97>
movq   $0x0,-0x8(%rbp)
jmp    1254 <readLine+0x114>
mov    -0x18(%rbp),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    0x2ddf(%rip),%rax
mov    (%rax),%edx
call   1060 <fseek@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1237 <readLine+0xf7>
mov    -0x10(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %al,%dl
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1207 <readLine+0xc7>
mov    -0x10(%rbp),%rdi
call   1030 <fgetc@plt>
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readLine>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
call   1050 <ftell@plt>
mov    %eax,%r15d
xor    %ecx,%ecx
mov    0x2e89(%rip),%r14
nop
mov    %rcx,%r13
mov    %r12,%rdi
call   1030 <fgetc@plt>
cmp    $0xa,%eax
je     117e <readLine+0x3e>
cmp    $0xd,%eax
je     117e <readLine+0x3e>
lea    0x1(%r13),%rcx
cmp    %eax,(%r14)
jne    1160 <readLine+0x20>
cmp    %eax,(%r14)
jne    1188 <readLine+0x48>
xor    %r14d,%r14d
jmp    11e1 <readLine+0xa1>
lea    0x1(%r13),%rdi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,%r14
mov    0x2e38(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
mov    %r15d,%esi
call   1060 <fseek@plt>
test   %r13d,%r13d
je     11d4 <readLine+0x94>
mov    %r13,%rbp
mov    %r14,%rbx
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1030 <fgetc@plt>
mov    %al,(%rbx)
add    $0x1,%rbx
add    $0xffffffffffffffff,%rbp
jne    11c0 <readLine+0x80>
mov    %r12,%rdi
call   1030 <fgetc@plt>
movb   $0x0,(%r14,%r13,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EOF = global i32 0
@SEEK_SET = global i32 0
define i8* @readLine(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i32* %0, i32** %3
%9 = load i32*, i32** %3
%10 = call i32 @ftell(i32* %9)
store i32 %10, i32* %4
store i32 0, i32* %5
br label %11
11: ; preds = %27, %1
%12 = load i32*, i32** %3
%13 = call i8* @fgetc(i32* %12)
%14 = ptrtoint i8* %13 to i32
store i32 %14, i32* %6
%15 = load i32, i32* %5
%16 = add nsw i32 %15, 1
store i32 %16, i32* %5
br label %17
17: ; preds = %11
%18 = load i32, i32* %6
%19 = icmp ne i32 %18, 10
br i1 %19, label %20, label %27
20: ; preds = %17
%21 = load i32, i32* %6
%22 = icmp ne i32 %21, 13
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = load i32, i32* %6
%25 = load i32, i32* @EOF
%26 = icmp ne i32 %24, %25
br label %27
27: ; preds = %23, %20, %17
%28 = phi i1 [ false, %20 ], [ false, %17 ], [ %26, %23 ]
br i1 %28, label %11, label %29
29: ; preds = %27
%30 = load i32, i32* %5
%31 = add nsw i32 %30, -1
store i32 %31, i32* %5
%32 = load i32, i32* %6
%33 = load i32, i32* @EOF
%34 = icmp eq i32 %32, %33
br i1 %34, label %35, label %36
35: ; preds = %29
store i8* null, i8** %2
br label %68
36: ; preds = %29
%37 = load i32, i32* %5
%38 = add nsw i32 %37, 1
%39 = call i64 @calloc(i32 %38, i32 1)
%40 = inttoptr i64 %39 to i8*
store i8* %40, i8** %7
%41 = load i32*, i32** %3
%42 = load i32, i32* %4
%43 = load i32, i32* @SEEK_SET
%44 = call i32 @fseek(i32* %41, i32 %42, i32 %43)
store i32 0, i32* %8
br label %45
45: ; preds = %57, %36
%46 = load i32, i32* %8
%47 = load i32, i32* %5
%48 = icmp slt i32 %46, %47
br i1 %48, label %49, label %60
49: ; preds = %45
%50 = load i32*, i32** %3
%51 = call i8* @fgetc(i32* %50)
%52 = ptrtoint i8* %51 to i8
%53 = load i8*, i8** %7
%54 = load i32, i32* %8
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %53, i64 %55
store i8 %52, i8* %56
br label %57
57: ; preds = %49
%58 = load i32, i32* %8
%59 = add nsw i32 %58, 1
store i32 %59, i32* %8
br label %45
60: ; preds = %45
%61 = load i32*, i32** %3
%62 = call i8* @fgetc(i32* %61)
%63 = load i8*, i8** %7
%64 = load i32, i32* %8
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8, i8* %63, i64 %65
store i8 0, i8* %66
%67 = load i8*, i8** %7
store i8* %67, i8** %2
br label %68
68: ; preds = %60, %35
%69 = load i8*, i8** %2
ret i8* %69
}

LLVM IR (-O2):
@EOF = global i32 0
@SEEK_SET = global i32 0
define i8* @readLine(i32* nocapture %0) {
%2 = tail call i32 @ftell(i32* %0)
br label %3
3: ; preds = %9, %1
%4 = phi i32 [ 0, %1 ], [ %8, %9 ]
%5 = tail call i8* @fgetc(i32* %0)
%6 = ptrtoint i8* %5 to i64
%7 = trunc i64 %6 to i32
%8 = add nuw nsw i32 %4, 1
switch i32 %7, label %9 [
i32 13, label %12
i32 10, label %12
]
9: ; preds = %3
%10 = load i32, i32* @EOF
%11 = icmp eq i32 %10, %7
br i1 %11, label %12, label %3
12: ; preds = %3, %3, %9
%13 = ptrtoint i8* %5 to i64
%14 = trunc i64 %13 to i32
%15 = load i32, i32* @EOF
%16 = icmp eq i32 %15, %14
br i1 %16, label %37, label %17
17: ; preds = %12
%18 = tail call i64 @calloc(i32 %8, i32 1)
%19 = inttoptr i64 %18 to i8*
%20 = load i32, i32* @SEEK_SET
%21 = tail call i32 @fseek(i32* %0, i32 %2, i32 %20)
%22 = icmp eq i32 %4, 0
br i1 %22, label %33, label %23
23: ; preds = %17
%24 = zext i32 %4 to i64
br label %25
25: ; preds = %23, %25
%26 = phi i64 [ 0, %23 ], [ %31, %25 ]
%27 = tail call i8* @fgetc(i32* %0)
%28 = ptrtoint i8* %27 to i64
%29 = trunc i64 %28 to i8
%30 = getelementptr inbounds i8, i8* %19, i64 %26
store i8 %29, i8* %30
%31 = add nuw nsw i64 %26, 1
%32 = icmp eq i64 %31, %24
br i1 %32, label %33, label %25
33: ; preds = %25, %17
%34 = tail call i8* @fgetc(i32* %0)
%35 = zext i32 %4 to i64
%36 = getelementptr inbounds i8, i8* %19, i64 %35
store i8 0, i8* %36
br label %37
37: ; preds = %12, %33
%38 = phi i8* [ %19, %33 ], [ null, %12 ]
ret i8* %38
}