#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_END;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
char *strtok(char *, char *);
int main(int argc, char *argv[]) {
  if (argc == 1) {
    printf("usage: ./a.out <name of file>\n");
    return 1;
  }
  FILE *f = fopen(argv[1], "r");
  fseek(f, 0, SEEK_END);
  int size = ftell(f);
  rewind(f);
  printf("Size is: %d\n", size);
  printf("buffer is: %d\n", size / 500);
  char buff[size / 500];
  int real_size = size / 500;
  if (f) {
    int lastlen = 0;
    int len;
    while ((len = fread(buff + lastlen, 1, size - lastlen - 1, f)) > 0) {
      lastlen += len;
      buff[len] = '\0';
      char *token = strtok(buff, "\n");
      for (; token; token = strtok(NULL, "\n"))
        printf("%s\n", token);
    }
    fclose(f);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    11ba <main+0x3a>
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    133c <main+0x1bc>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe56(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
mov    %esi,-0x58(%rbp)
call   1080 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1070 <ftell@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x58(%rbp),%eax
mov    -0x1c(%rbp),%esi
lea    0xe16(%rip),%rdi
mov    %al,-0x51(%rbp)
call   1050 <printf@plt>
mov    -0x51(%rbp),%al
movslq -0x1c(%rbp),%rcx
imul   $0x10624dd3,%rcx,%rdx
mov    %rdx,%rcx
shr    $0x3f,%rcx
sar    $0x25,%rdx
mov    %edx,%esi
add    %ecx,%esi
lea    0xdf7(%rip),%rdi
call   1050 <printf@plt>
movslq -0x1c(%rbp),%rax
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x25,%rax
add    %ecx,%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x1f4,%ecx
cltd
idiv   %ecx
mov    %eax,-0x34(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     132e <main+0x1ae>
movl   $0x0,-0x38(%rbp)
mov    -0x50(%rbp),%rdi
movslq -0x38(%rbp),%rax
add    %rax,%rdi
mov    -0x1c(%rbp),%edx
sub    -0x38(%rbp),%edx
sub    $0x1,%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
jle    1325 <main+0x1a5>
mov    -0x50(%rbp),%rdi
mov    -0x3c(%rbp),%eax
add    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
movslq -0x3c(%rbp),%rax
movb   $0x0,(%rdi,%rax,1)
lea    0xd3c(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     1320 <main+0x1a0>
mov    -0x48(%rbp),%rsi
lea    0xd3d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd0b(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x48(%rbp)
jmp    12ea <main+0x16a>
jmp    1297 <main+0x117>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x1,%edi
jne    11ba <main+0x2a>
lea    0xe74(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%r14d
jmp    12db <main+0x14b>
mov    0x8(%rsi),%rdi
lea    0xe3b(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%r12
mov    0x2e14(%rip),%rax
mov    (%rax),%edx
xor    %r14d,%r14d
mov    %r12,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %r12,%rdi
call   1080 <ftell@plt>
mov    %eax,%r15d
mov    %r12,%rdi
call   1070 <rewind@plt>
lea    0xe05(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
movslq %r15d,%rax
imul   $0x10624dd3,%rax,%rbx
mov    %rbx,%rax
shr    $0x3f,%rax
sar    $0x25,%rbx
add    %eax,%ebx
lea    0xdea(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rsp,%rax
add    $0xf,%rbx
and    $0xfffffffffffffff0,%rbx
mov    %rsp,%r13
sub    %rbx,%r13
mov    %r13,%rsp
test   %r12,%r12
je     12d8 <main+0x148>
mov    %rax,-0x30(%rbp)
lea    -0x1(%r15),%edx
mov    %r13,%rdi
mov    $0x1,%esi
mov    %r12,%rcx
call   1040 <fread@plt>
test   %eax,%eax
jle    12c9 <main+0x139>
xor    %r14d,%r14d
lea    0xd9c(%rip),%rbx
jmp    129d <main+0x10d>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movslq %r14d,%rdx
lea    (%rdx,%r13,1),%rdi
not    %edx
add    %r15d,%edx
mov    $0x1,%esi
mov    %r12,%rcx
call   1040 <fread@plt>
test   %eax,%eax
jle    12c9 <main+0x139>
add    %eax,%r14d
mov    %eax,%eax
movb   $0x0,0x0(%r13,%rax,1)
mov    %r13,%rdi
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rsi
call   10b0 <strtok@plt>
test   %rax,%rax
je     1280 <main+0xf0>
mov    %rax,%rdi
call   1030 <puts@plt>
xor    %edi,%edi
jmp    12b0 <main+0x120>
mov    %r12,%rdi
call   1050 <fclose@plt>
xor    %r14d,%r14d
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [31 x i8] c"usage: ./a.out <name of file>\0A\00"
@.str.1 = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@.str.2 = [13 x i8] c"Size is: %d\0A\00"
@.str.3 = [15 x i8] c"buffer is: %d\0A\00"
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %78
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %22, i32** %6
%23 = load i32*, i32** %6
%24 = load i32, i32* @SEEK_END
%25 = call i32 @fseek(i32* %23, i32 0, i32 %24)
%26 = load i32*, i32** %6
%27 = call i32 @ftell(i32* %26)
store i32 %27, i32* %7
%28 = load i32*, i32** %6
%29 = call i32 @rewind(i32* %28)
%30 = load i32, i32* %7
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %30)
%32 = load i32, i32* %7
%33 = sdiv i32 %32, 500
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %33)
%35 = load i32, i32* %7
%36 = sdiv i32 %35, 500
%37 = zext i32 %36 to i64
%38 = call i8* @llvm.stacksave()
store i8* %38, i8** %8
%39 = alloca i8, i64 %37
store i64 %37, i64* %9
%40 = load i32, i32* %7
%41 = sdiv i32 %40, 500
store i32 %41, i32* %10
%42 = load i32*, i32** %6
%43 = icmp ne i32* %42, null
br i1 %43, label %44, label %76
44: ; preds = %18
store i32 0, i32* %11
br label %45
45: ; preds = %72, %44
%46 = load i32, i32* %11
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %39, i64 %47
%49 = load i32, i32* %7
%50 = load i32, i32* %11
%51 = sub nsw i32 %49, %50
%52 = sub nsw i32 %51, 1
%53 = load i32*, i32** %6
%54 = call i32 @fread(i8* %48, i32 1, i32 %52, i32* %53)
store i32 %54, i32* %12
%55 = icmp sgt i32 %54, 0
br i1 %55, label %56, label %73
56: ; preds = %45
%57 = load i32, i32* %12
%58 = load i32, i32* %11
%59 = add nsw i32 %58, %57
store i32 %59, i32* %11
%60 = load i32, i32* %12
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i8, i8* %39, i64 %61
store i8 0, i8* %62
%63 = call i8* @strtok(i8* %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i8* %63, i8** %13
br label %64
64: ; preds = %70, %56
%65 = load i8*, i8** %13
%66 = icmp ne i8* %65, null
br i1 %66, label %67, label %72
67: ; preds = %64
%68 = load i8*, i8** %13
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %68)
br label %70
70: ; preds = %67
%71 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i8* %71, i8** %13
br label %64
72: ; preds = %64
br label %45
73: ; preds = %45
%74 = load i32*, i32** %6
%75 = call i32 @fclose(i32* %74)
br label %76
76: ; preds = %73, %18
store i32 0, i32* %3
%77 = load i8*, i8** %8
br label %78
78: ; preds = %76, %16
%79 = load i32, i32* %3
ret i32 %79
}

LLVM IR (-O2):
@.str.1 = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@.str.2 = [13 x i8] c"Size is: %d\0A\00"
@.str.3 = [15 x i8] c"buffer is: %d\0A\00"
@.str.4 = [2 x i8] c"\0A\00"
@str = [30 x i8] c"usage: ./a.out <name of file>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 1
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
br label %48
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = load i32, i32* @SEEK_END
%11 = tail call i32 @fseek(i32* %9, i32 0, i32 %10)
%12 = tail call i32 @ftell(i32* %9)
%13 = tail call i32 @rewind(i32* %9)
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%15 = sdiv i32 %12, 500
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %15)
%17 = zext i32 %15 to i64
%18 = tail call i8* @llvm.stacksave()
%19 = alloca i8, i64 %17
%20 = icmp eq i32* %9, null
br i1 %20, label %47, label %21
21: ; preds = %6
%22 = add i32 %12, -1
%23 = call i32 @fread(i8* %19, i32 1, i32 %22, i32* %9)
%24 = icmp sgt i32 %23, 0
br i1 %24, label %32, label %45
25: ; preds = %40, %32
%26 = sext i32 %35 to i64
%27 = getelementptr inbounds i8, i8* %19, i64 %26
%28 = xor i32 %35, -1
%29 = add i32 %12, %28
%30 = call i32 @fread(i8* %27, i32 1, i32 %29, i32* %9)
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %45
32: ; preds = %21, %25
%33 = phi i32 [ %30, %25 ], [ %23, %21 ]
%34 = phi i32 [ %35, %25 ], [ 0, %21 ]
%35 = add nsw i32 %33, %34
%36 = zext i32 %33 to i64
%37 = getelementptr inbounds i8, i8* %19, i64 %36
store i8 0, i8* %37
%38 = call i8* @strtok(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%39 = icmp eq i8* %38, null
br i1 %39, label %25, label %40
40: ; preds = %32, %40
%41 = phi i8* [ %43, %40 ], [ %38, %32 ]
%42 = call i32 @puts(i8* %41)
%43 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%44 = icmp eq i8* %43, null
br i1 %44, label %25, label %40
45: ; preds = %25, %21
%46 = call i32 @fclose(i32* %9)
br label %47
47: ; preds = %45, %6
br label %48
48: ; preds = %47, %4
%49 = phi i32 [ 1, %4 ], [ 0, %47 ]
ret i32 %49
}