#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);
int /*<<< orphan*/ putchar(char);
int main() {
  int fd = open("/etc/services", O_RDONLY);
  lseek(fd, 0, SEEK_END);
  int len = lseek(fd, (size_t)0, SEEK_CUR);
  lseek(fd, 0, SEEK_SET);
  printf("%d\n", len);
  char *addr = mmap(0, len, PROT_READ, MAP_PRIVATE, fd, 0);
  if (!addr) {
    perror("mmap\n");
    exit(1);
  }
  while (*addr != '\n') {
    putchar(*addr);
    addr++;
  }
  addr += 10240;
  while (*addr != '\n') {
    putchar(*addr);
    addr++;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1060 <lseek@plt>
mov    -0x8(%rbp),%edi
mov    0x2e22(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1060 <lseek@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2df2(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1060 <lseek@plt>
mov    -0xc(%rbp),%esi
lea    0xe2b(%rip),%rdi
call   1050 <printf@plt>
mov    -0xc(%rbp),%esi
mov    0x2e06(%rip),%rax
mov    (%rax),%edx
mov    0x2dbd(%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    1231 <main+0xc1>
lea    0xdf0(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1236 <main+0xc6>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
je     1263 <main+0xf3>
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1030 <putchar@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1236 <main+0xc6>
mov    -0x18(%rbp),%rax
add    $0x2800,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
je     129e <main+0x12e>
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1030 <putchar@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1271 <main+0x101>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
lea    0xe7d(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
mov    0x2e4f(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
mov    %eax,%ebp
mov    0x2e09(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
lea    0xe45(%rip),%rdi
mov    %ebp,%esi
call   1050 <printf@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
mov    0x2dd8(%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     124c <main+0xdc>
mov    %rax,%rbx
mov    (%rax),%al
cmp    $0xa,%al
je     1214 <main+0xa4>
nop
movsbl %al,%edi
call   1030 <putchar@plt>
movzbl 0x1(%rbx),%eax
add    $0x1,%rbx
cmp    $0xa,%al
jne    1200 <main+0x90>
mov    0x2800(%rbx),%al
cmp    $0xa,%al
je     1243 <main+0xd3>
add    $0x2801,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
movsbl %al,%edi
call   1030 <putchar@plt>
movzbl (%rbx),%eax
add    $0x1,%rbx
cmp    $0xa,%al
jne    1230 <main+0xc0>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdbf(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

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

LLVM IR (-O2):
@.str = [14 x i8] c"/etc/services\00"
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@SEEK_CUR = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [4 x i8] c"%d\0A\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@.str.2 = [6 x i8] c"mmap\0A\00"
define i32 @main() {
%1 = load i32, i32* @O_RDONLY
%2 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 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 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @MAP_PRIVATE
%12 = tail call i8* @mmap(i32 0, i32 %6, i32 %10, i32 %11, i32 %2, i32 0)
%13 = icmp eq i8* %12, null
br i1 %13, label %17, label %14
14: ; preds = %0
%15 = load i8, i8* %12
%16 = icmp eq i8 %15, 10
br i1 %16, label %27, label %20
17: ; preds = %0
%18 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %14, %20
%21 = phi i8 [ %25, %20 ], [ %15, %14 ]
%22 = phi i8* [ %24, %20 ], [ %12, %14 ]
%23 = tail call i32 @putchar(i8 signext %21)
%24 = getelementptr inbounds i8, i8* %22, i64 1
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 10
br i1 %26, label %27, label %20
27: ; preds = %20, %14
%28 = phi i8* [ %12, %14 ], [ %24, %20 ]
%29 = getelementptr inbounds i8, i8* %28, i64 10240
%30 = load i8, i8* %29
%31 = icmp eq i8 %30, 10
br i1 %31, label %39, label %32
32: ; preds = %27, %32
%33 = phi i8 [ %37, %32 ], [ %30, %27 ]
%34 = phi i8* [ %36, %32 ], [ %29, %27 ]
%35 = tail call i32 @putchar(i8 signext %33)
%36 = getelementptr inbounds i8, i8* %34, i64 1
%37 = load i8, i8* %36
%38 = icmp eq i8 %37, 10
br i1 %38, label %39, label %32
39: ; preds = %32, %27
ret i32 0
}