#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 /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ fread(char *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
size_t grepline(char **lineptr, size_t *n, FILE *stream) {
  size_t len = 0;
  size_t offset = ftell(stream);
  char trash = 0;
  char *buf = NULL;
  char sizeofnewline = 0;
  if (lineptr != NULL) {
    free(*lineptr);
  }
  do {
    trash = fgetc(stream);
    ++len;
  } while (!feof(stream) && trash != '\n' && trash != '\r');
  if (trash == '\r') {
    trash = fgetc(stream);
    if (trash == '\n') {
      ++sizeofnewline;
    }
  }
  fseek(stream, offset, SEEK_SET);
  buf = (char *)malloc(len + 1);
  fread(buf, len, 1, stream);
  if (sizeofnewline) {
    trash = fgetc(stream);
  }
  buf[len - 1] = '\n';
  buf[len] = '\0';
  *n = len;
  *lineptr = buf;
  return len;
}


x86 assembly (clang, -O0):
<grepline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x28(%rbp)
movb   $0x0,-0x29(%rbp)
movq   $0x0,-0x38(%rbp)
movb   $0x0,-0x39(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11c0 <grepline+0x50>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
jmp    11c5 <grepline+0x55>
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %al,-0x29(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <feof@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x3a(%rbp)
jne    1215 <grepline+0xa5>
movsbl -0x29(%rbp),%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x3a(%rbp)
je     1215 <grepline+0xa5>
movsbl -0x29(%rbp),%eax
cmp    $0xd,%eax
setne  %al
mov    %al,-0x3a(%rbp)
mov    -0x3a(%rbp),%al
test   $0x1,%al
jne    11c5 <grepline+0x55>
movsbl -0x29(%rbp),%eax
cmp    $0xd,%eax
jne    1253 <grepline+0xe3>
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %al,-0x29(%rbp)
movsbl -0x29(%rbp),%eax
cmp    $0xa,%eax
jne    124e <grepline+0xde>
mov    -0x39(%rbp),%al
add    $0x1,%al
mov    %al,-0x39(%rbp)
jmp    1253 <grepline+0xe3>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    0x2d7e(%rip),%rax
mov    (%rax),%edx
call   1090 <fseek@plt>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
cmpb   $0x0,-0x39(%rbp)
je     12a6 <grepline+0x136>
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %al,-0x29(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movb   $0xa,(%rax,%rcx,1)
mov    -0x38(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<grepline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,(%rsp)
mov    %rdi,%r15
mov    %rdx,%rdi
call   1060 <ftell@plt>
mov    %rax,%r13
test   %r15,%r15
je     119d <grepline+0x2d>
mov    (%r15),%rdi
call   1030 <free@plt>
mov    $0x1,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rax,%rbp
mov    %r12,%rdi
call   1050 <fgetc@plt>
mov    %eax,%ebx
mov    %r12,%rdi
call   1070 <feof@plt>
test   %eax,%eax
jne    11d9 <grepline+0x69>
cmp    $0xa,%bl
je     11f1 <grepline+0x81>
lea    0x1(%rbp),%rax
cmp    $0xd,%bl
jne    11b0 <grepline+0x40>
jmp    11e1 <grepline+0x71>
mov    $0x1,%r14b
cmp    $0xd,%bl
jne    11f4 <grepline+0x84>
mov    %r12,%rdi
call   1050 <fgetc@plt>
cmp    $0xa,%al
setne  %r14b
jmp    11f4 <grepline+0x84>
mov    $0x1,%r14b
mov    0x2de5(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
mov    %r13,%rsi
call   1090 <fseek@plt>
lea    0x1(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %rbp,%rsi
mov    $0x1,%edx
mov    %r12,%rcx
call   1040 <fread@plt>
test   %r14b,%r14b
jne    1234 <grepline+0xc4>
mov    %r12,%rdi
call   1050 <fgetc@plt>
movw   $0xa,-0x1(%r13,%rbp,1)
mov    (%rsp),%rax
mov    %rbp,(%rax)
mov    %r13,(%r15)
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@SEEK_SET = global i32 0
define i64 @grepline(i8** %0, i64* nocapture writeonly %1, i32* nocapture %2) {
%4 = tail call i64 @ftell(i32* %2)
%5 = icmp eq i8** %0, null
br i1 %5, label %9, label %6
6: ; preds = %3
%7 = load i8*, i8** %0
%8 = tail call i32 @free(i8* %7)
br label %9
9: ; preds = %6, %3
br label %10
10: ; preds = %9, %16
%11 = phi i64 [ %13, %16 ], [ 0, %9 ]
%12 = tail call signext i8 @fgetc(i32* %2)
%13 = add i64 %11, 1
%14 = tail call i32 @feof(i32* %2)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %10
switch i8 %12, label %10 [
i8 13, label %19
i8 10, label %22
]
17: ; preds = %10
%18 = icmp eq i8 %12, 13
br i1 %18, label %19, label %22
19: ; preds = %16, %17
%20 = tail call signext i8 @fgetc(i32* %2)
%21 = icmp ne i8 %20, 10
br label %22
22: ; preds = %16, %19, %17
%23 = phi i1 [ true, %17 ], [ %21, %19 ], [ true, %16 ]
%24 = load i32, i32* @SEEK_SET
%25 = tail call i32 @fseek(i32* %2, i64 %4, i32 %24)
%26 = add i64 %11, 2
%27 = tail call i64 @malloc(i64 %26)
%28 = inttoptr i64 %27 to i8*
%29 = tail call i32 @fread(i8* %28, i64 %13, i32 1, i32* %2)
br i1 %23, label %32, label %30
30: ; preds = %22
%31 = tail call signext i8 @fgetc(i32* %2)
br label %32
32: ; preds = %30, %22
%33 = getelementptr inbounds i8, i8* %28, i64 %11
store i8 10, i8* %33
%34 = getelementptr inbounds i8, i8* %28, i64 %13
store i8 0, i8* %34
store i64 %13, i64* %1
store i8* %28, i8** %0
ret i64 %13
}