#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 */

/* Variables and functions */
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
int lseek(int, size_t, int /*<<< orphan*/);
char *mmap(int /*<<< orphan*/, int, int /*<<< orphan*/, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ sleep(int);
int main() {
  int fd = open("/tmp/file", O_RDONLY);
  lseek(fd, 0, SEEK_END);
  int len = lseek(fd, (size_t)0, SEEK_CUR);
  lseek(fd, 0, SEEK_SET);
  char *addr = mmap(0, len, PROT_READ, MAP_PRIVATE, fd, 0);
  if (!addr) {
    perror("mmap\n");
    exit(1);
  }
  char *origAddr = addr;
  for (;;) {
    addr = origAddr;
    while (*addr != '\n') {
      putchar(*addr);
      addr++;
    }
    sleep(1);
    printf("\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
lea    0xe61(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e2f(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1060 <lseek@plt>
mov    -0x8(%rbp),%edi
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1060 <lseek@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2de2(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1060 <lseek@plt>
mov    -0xc(%rbp),%esi
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
mov    0x2dbc(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1232 <main+0xb2>
lea    0xde7(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
je     126f <main+0xef>
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1030 <putchar@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1242 <main+0xc2>
mov    $0x1,%edi
call   10a0 <sleep@plt>
lea    0xd8e(%rip),%rdi
call   1050 <printf@plt>
jmp    123a <main+0xba>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
lea    0xe7c(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
mov    0x2e4e(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %eax,%ebp
mov    0x2e08(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edx
mov    0x2de5(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
test   %rax,%rax
je     1236 <main+0xc6>
mov    %rax,%rbx
mov    %rax,%r14
add    $0x1,%r14
jmp    1214 <main+0xa4>
cs nopw 0x0(%rax,%rax,1)
nop
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    (%rbx),%al
cmp    $0xa,%al
je     1200 <main+0x90>
mov    %r14,%rbp
nopl   (%rax)
movsbl %al,%edi
call   1030 <putchar@plt>
movzbl 0x0(%rbp),%eax
add    $0x1,%rbp
cmp    $0xa,%al
jne    1220 <main+0xb0>
jmp    1200 <main+0x90>
lea    0xdcd(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"/tmp/file\00"
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@SEEK_CUR = global i32 0
@SEEK_SET = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@.str.1 = [6 x i8] c"mmap\0A\00"
@.str.2 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
store i32 0, i32* %1
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = load i32, i32* @SEEK_END
%10 = call i32 @lseek(i32 %8, i64 0, i32 %9)
%11 = load i32, i32* %2
%12 = load i32, i32* @SEEK_CUR
%13 = call i32 @lseek(i32 %11, i64 0, i32 %12)
store i32 %13, i32* %3
%14 = load i32, i32* %2
%15 = load i32, i32* @SEEK_SET
%16 = call i32 @lseek(i32 %14, i64 0, i32 %15)
%17 = load i32, i32* %3
%18 = load i32, i32* @PROT_READ
%19 = load i32, i32* @MAP_PRIVATE
%20 = load i32, i32* %2
%21 = call i8* @mmap(i32 0, i32 %17, i32 %18, i32 %19, i32 %20, i32 0)
store i8* %21, i8** %4
%22 = load i8*, i8** %4
%23 = icmp ne i8* %22, null
br i1 %23, label %27, label %24
24: ; preds = %0
%25 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %0
%28 = load i8*, i8** %4
store i8* %28, i8** %5
br label %29
29: ; preds = %42, %27
%30 = load i8*, i8** %5
store i8* %30, i8** %4
br label %31
31: ; preds = %36, %29
%32 = load i8*, i8** %4
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp ne i32 %34, 10
br i1 %35, label %36, label %42
36: ; preds = %31
%37 = load i8*, i8** %4
%38 = load i8, i8* %37
%39 = call i32 @putchar(i8 signext %38)
%40 = load i8*, i8** %4
%41 = getelementptr inbounds i8, i8* %40, i32 1
store i8* %41, i8** %4
br label %31
42: ; preds = %31
%43 = call i32 @sleep(i32 1)
%44 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %29
}

LLVM IR (-O2):
@.str = [10 x i8] c"/tmp/file\00"
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@SEEK_CUR = global i32 0
@SEEK_SET = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@.str.1 = [6 x i8] c"mmap\0A\00"
define i32 @main() {
%1 = load i32, i32* @O_RDONLY
%2 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = load i32, i32* @SEEK_END
%4 = tail call i32 @lseek(i32 %2, i64 0, i32 %3)
%5 = load i32, i32* @SEEK_CUR
%6 = tail call i32 @lseek(i32 %2, i64 0, i32 %5)
%7 = load i32, i32* @SEEK_SET
%8 = tail call i32 @lseek(i32 %2, i64 0, i32 %7)
%9 = load i32, i32* @PROT_READ
%10 = load i32, i32* @MAP_PRIVATE
%11 = tail call i8* @mmap(i32 0, i32 %6, i32 %9, i32 %10, i32 %2, i32 0)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %0, %26
%17 = load i8, i8* %11
%18 = icmp eq i8 %17, 10
br i1 %18, label %26, label %19
19: ; preds = %16, %19
%20 = phi i8 [ %24, %19 ], [ %17, %16 ]
%21 = phi i8* [ %23, %19 ], [ %11, %16 ]
%22 = tail call i32 @putchar(i8 signext %20)
%23 = getelementptr inbounds i8, i8* %21, i64 1
%24 = load i8, i8* %23
%25 = icmp eq i8 %24, 10
br i1 %25, label %26, label %19
26: ; preds = %19, %16
%27 = tail call i32 @sleep(i32 1)
%28 = tail call i32 bitcast (i32 (i8)* @putchar to i32 (i32)*)(i32 10)
br label %16
}