#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 */
struct stat {
  int st_size;
};
typedef size_t ssize_t;
typedef int off_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_EXEC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ _SC_PAGE_SIZE;
void *atoi(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fstat(int, struct stat *);
int /*<<< orphan*/ getchar();
char *mmap(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/, int, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int sysconf(int /*<<< orphan*/);
size_t write(int /*<<< orphan*/, char *, size_t);
int main(int argc, char *argv[]) {
  char *addr;
  int fd, ret;
  struct stat sb;
  off_t offset, pa_offset;
  size_t length;
  ssize_t s;
  if (argc < 3 || argc > 4) {
    fprintf(stderr, "%s file length [offset]\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  fd = open(argv[1], O_RDWR);
  if (fd == -1)
    do {
      perror("open");
      exit(EXIT_FAILURE);
    } while (0);
  if (fstat(fd, &sb) == -1)
    do {
      perror("fstat");
      exit(EXIT_FAILURE);
    } while (0);
  printf("PAGE_SIZE:%lx\n", sysconf(_SC_PAGE_SIZE));
  offset = atoi(argv[3]);
  pa_offset = offset & ~(sysconf(_SC_PAGE_SIZE) - 1);
  printf("pa_offset:%lx\n", pa_offset);
  printf("pa_offset:%lx file size:%lx\n", pa_offset, sb.st_size);
  if (offset >= sb.st_size) {
    fprintf(stderr, "offset is past end of file\n");
    exit(EXIT_FAILURE);
  }
  if (argc == 4) {
    length = atoi(argv[2]);
    if (offset + length > sb.st_size)
      length = sb.st_size - offset;
  } else {
    length = sb.st_size - offset;
  }
  printf("length:%d\n", length);
  getchar();
  addr = mmap(NULL, length + offset - pa_offset,
              PROT_EXEC | PROT_READ | PROT_WRITE, MAP_SHARED, fd, pa_offset);
  if (addr == MAP_FAILED)
    do {
      perror("mmap");
      exit(EXIT_FAILURE);
    } while (0);
  printf("addr:%p %x\n", addr, *addr);
  s = write(STDOUT_FILENO, addr + offset - pa_offset, length);
  if (s != length) {
    if (s == -1)
      do {
        perror("write");
        exit(EXIT_FAILURE);
      } while (0);
    fprintf(stderr, "partial write");
    exit(EXIT_FAILURE);
  }
  getchar();
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jl     11da <main+0x2a>
cmpl   $0x4,-0x8(%rbp)
jle    1206 <main+0x56>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe0f(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1248 <main+0x98>
jmp    122e <main+0x7e>
lea    0xde4(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   10d0 <fstat@plt>
cmp    $0xffffffff,%eax
jne    127c <main+0xcc>
jmp    1262 <main+0xb2>
lea    0xdb5(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
call   10a0 <sysconf@plt>
mov    %eax,%esi
lea    0xd91(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    0x2ce9(%rip),%rax
mov    (%rax),%edi
call   10a0 <sysconf@plt>
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
sub    $0x1,%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%esi
lea    0xd5b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x30(%rbp),%esi
mov    -0x28(%rbp),%edx
lea    0xd56(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x2c(%rbp),%eax
cmp    -0x28(%rbp),%eax
jl     1324 <main+0x174>
mov    0x2cb2(%rip),%rax
mov    (%rax),%edi
lea    0xd50(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2c93(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
cmpl   $0x4,-0x8(%rbp)
jne    1365 <main+0x1b5>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10b0 <atoi@plt>
mov    %rax,-0x38(%rbp)
movslq -0x2c(%rbp),%rax
add    -0x38(%rbp),%rax
movslq -0x28(%rbp),%rcx
cmp    %rcx,%rax
jbe    1360 <main+0x1b0>
mov    -0x28(%rbp),%eax
sub    -0x2c(%rbp),%eax
cltq
mov    %rax,-0x38(%rbp)
jmp    1371 <main+0x1c1>
mov    -0x28(%rbp),%eax
sub    -0x2c(%rbp),%eax
cltq
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rsi
lea    0xcff(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1060 <getchar@plt>
mov    -0x38(%rbp),%rsi
movslq -0x2c(%rbp),%rax
add    %rax,%rsi
movslq -0x30(%rbp),%rax
sub    %rax,%rsi
mov    0x2c25(%rip),%rax
mov    (%rax),%edx
mov    0x2c4c(%rip),%rax
or     (%rax),%edx
mov    0x2bf3(%rip),%rax
or     (%rax),%edx
mov    0x2c22(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
mov    -0x30(%rbp),%r9d
xor    %eax,%eax
mov    %eax,%edi
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2bb0(%rip),%rcx
cmp    (%rcx),%rax
jne    1408 <main+0x258>
jmp    13ee <main+0x23e>
lea    0xc91(%rip),%rdi
call   1090 <perror@plt>
mov    0x2baf(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%rsi
mov    -0x18(%rbp),%rax
movsbl (%rax),%edx
lea    0xc71(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2bc0(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
movslq -0x2c(%rbp),%rax
add    %rax,%rsi
movslq -0x30(%rbp),%rcx
xor    %eax,%eax
sub    %rcx,%rax
add    %rax,%rsi
mov    -0x38(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x38(%rbp),%rax
je     14ab <main+0x2fb>
cmpq   $0xffffffffffffffff,-0x40(%rbp)
jne    1486 <main+0x2d6>
jmp    146c <main+0x2bc>
lea    0xc2c(%rip),%rdi
call   1090 <perror@plt>
mov    0x2b31(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x2b2b(%rip),%rax
mov    (%rax),%edi
lea    0xc01(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2b0c(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    $0x0,%al
call   1060 <getchar@plt>
mov    0x2adf(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
lea    -0x5(%rbp),%eax
cmp    $0xfffffffd,%eax
ja     11e7 <main+0x37>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe22(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1227 <main+0x77>
mov    0x8(%rbx),%rdi
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
jne    1207 <main+0x57>
lea    0xe14(%rip),%rdi
jmp    1222 <main+0x72>
mov    %eax,%r14d
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10d0 <fstat@plt>
cmp    $0xffffffff,%eax
jne    1235 <main+0x85>
lea    0xdfc(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x2d64(%rip),%r15
mov    (%r15),%edi
call   10a0 <sysconf@plt>
lea    0xdd9(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x18(%rbx),%rdi
call   10b0 <atoi@plt>
mov    %rax,%r12
mov    (%r15),%edi
call   10a0 <sysconf@plt>
mov    %eax,%r15d
neg    %r15d
and    %r12d,%r15d
lea    0xdbb(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rsp),%r13d
lea    0xdb4(%rip),%rdi
mov    %r15d,%esi
mov    %r13d,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12,%rax
mov    %r13d,%r12d
sub    %eax,%r12d
jle    12d4 <main+0x124>
cmp    $0x4,%ebp
jne    12f0 <main+0x140>
movslq %r13d,%rcx
mov    %rcx,0x10(%rsp)
mov    0x10(%rbx),%rdi
mov    %rax,%rbx
call   10b0 <atoi@plt>
mov    %rax,%r13
movslq %ebx,%rbp
add    %rbp,%rax
cmp    0x10(%rsp),%rax
jbe    12f6 <main+0x146>
movslq %r12d,%r13
jmp    12f6 <main+0x146>
mov    0x2cdd(%rip),%rax
mov    (%rax),%edi
lea    0xd7b(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1227 <main+0x77>
movslq %r12d,%r13
movslq %eax,%rbp
lea    0xd7e(%rip),%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1060 <getchar@plt>
movslq %r15d,%r12
mov    %rbp,%rsi
sub    %r12,%rsi
add    %r13,%rsi
mov    0x2ca7(%rip),%rax
mov    0x2cd0(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2c75(%rip),%rax
or     (%rax),%edx
mov    0x2ca4(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r14d,%r8d
mov    %r15d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    0x2c3b(%rip),%rax
cmp    (%rax),%rbx
jne    1366 <main+0x1b6>
lea    0xd25(%rip),%rdi
jmp    1222 <main+0x72>
movsbl (%rbx),%edx
lea    0xd1b(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2c67(%rip),%rax
mov    (%rax),%edi
add    %rbx,%rbp
sub    %r12,%rbp
mov    %rbp,%rsi
mov    %r13,%rdx
call   1030 <write@plt>
cmp    %r13,%rax
jne    13ae <main+0x1fe>
xor    %eax,%eax
call   1060 <getchar@plt>
mov    0x2bf1(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
cmp    $0xffffffffffffffff,%rax
jne    13c0 <main+0x210>
lea    0xce4(%rip),%rdi
jmp    1222 <main+0x72>
mov    0x2bf1(%rip),%rax
mov    (%rax),%edi
lea    0xcc7(%rip),%rsi
jmp    12e4 <main+0x134>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [25 x i8] c"%s file length [offset]\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [6 x i8] c"fstat\00"
@.str.3 = [15 x i8] c"PAGE_SIZE:%lx\0A\00"
@_SC_PAGE_SIZE = global i32 0
@.str.4 = [15 x i8] c"pa_offset:%lx\0A\00"
@.str.5 = [29 x i8] c"pa_offset:%lx file size:%lx\0A\00"
@.str.6 = [28 x i8] c"offset is past end of file\0A\00"
@.str.7 = [11 x i8] c"length:%d\0A\00"
@PROT_EXEC = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.8 = [5 x i8] c"mmap\00"
@.str.9 = [12 x i8] c"addr:%p %x\0A\00"
@STDOUT_FILENO = global i32 0
@.str.10 = [6 x i8] c"write\00"
@.str.11 = [14 x i8] c"partial write\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.stat
%10 = alloca i32
%11 = alloca i32
%12 = alloca i64
%13 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 3
br i1 %15, label %19, label %16
16: ; preds = %2
%17 = load i32, i32* %4
%18 = icmp sgt i32 %17, 4
br i1 %18, label %19, label %27
19: ; preds = %16, %2
%20 = load i32, i32* @stderr
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 0
%23 = load i8*, i8** %22
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %23)
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %16
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = load i32, i32* @O_RDWR
%32 = call i32 @open(i8* %30, i32 %31)
store i32 %32, i32* %7
%33 = load i32, i32* %7
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %41
35: ; preds = %27
br label %36
36: ; preds = %35
%37 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; No predecessors!
br label %41
41: ; preds = %40, %27
%42 = load i32, i32* %7
%43 = call i32 @fstat(i32 %42, %struct.stat* %9)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %51
45: ; preds = %41
br label %46
46: ; preds = %45
%47 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; No predecessors!
br label %51
51: ; preds = %50, %41
%52 = load i32, i32* @_SC_PAGE_SIZE
%53 = call i32 @sysconf(i32 %52)
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %53)
%55 = load i8**, i8*** %5
%56 = getelementptr inbounds i8*, i8** %55, i64 3
%57 = load i8*, i8** %56
%58 = call i8* @atoi(i8* %57)
%59 = ptrtoint i8* %58 to i32
store i32 %59, i32* %10
%60 = load i32, i32* %10
%61 = load i32, i32* @_SC_PAGE_SIZE
%62 = call i32 @sysconf(i32 %61)
%63 = sub nsw i32 %62, 1
%64 = xor i32 %63, -1
%65 = and i32 %60, %64
store i32 %65, i32* %11
%66 = load i32, i32* %11
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %66)
%68 = load i32, i32* %11
%69 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%70 = load i32, i32* %69
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %68, i32 %70)
%72 = load i32, i32* %10
%73 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%74 = load i32, i32* %73
%75 = icmp sge i32 %72, %74
br i1 %75, label %76, label %81
76: ; preds = %51
%77 = load i32, i32* @stderr
%78 = call i32 (i32, i8*, ...) @fprintf(i32 %77, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
%79 = load i32, i32* @EXIT_FAILURE
%80 = call i32 @exit(i32 %79)
unreachable
81: ; preds = %51
%82 = load i32, i32* %4
%83 = icmp eq i32 %82, 4
br i1 %83, label %84, label %105
84: ; preds = %81
%85 = load i8**, i8*** %5
%86 = getelementptr inbounds i8*, i8** %85, i64 2
%87 = load i8*, i8** %86
%88 = call i8* @atoi(i8* %87)
%89 = ptrtoint i8* %88 to i64
store i64 %89, i64* %12
%90 = load i32, i32* %10
%91 = sext i32 %90 to i64
%92 = load i64, i64* %12
%93 = add i64 %91, %92
%94 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%95 = load i32, i32* %94
%96 = sext i32 %95 to i64
%97 = icmp ugt i64 %93, %96
br i1 %97, label %98, label %104
98: ; preds = %84
%99 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%100 = load i32, i32* %99
%101 = load i32, i32* %10
%102 = sub nsw i32 %100, %101
%103 = sext i32 %102 to i64
store i64 %103, i64* %12
br label %104
104: ; preds = %98, %84
br label %111
105: ; preds = %81
%106 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%107 = load i32, i32* %106
%108 = load i32, i32* %10
%109 = sub nsw i32 %107, %108
%110 = sext i32 %109 to i64
store i64 %110, i64* %12
br label %111
111: ; preds = %105, %104
%112 = load i64, i64* %12
%113 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i64 %112)
%114 = call i32 (...) @getchar()
%115 = load i64, i64* %12
%116 = load i32, i32* %10
%117 = sext i32 %116 to i64
%118 = add i64 %115, %117
%119 = load i32, i32* %11
%120 = sext i32 %119 to i64
%121 = sub i64 %118, %120
%122 = load i32, i32* @PROT_EXEC
%123 = load i32, i32* @PROT_READ
%124 = or i32 %122, %123
%125 = load i32, i32* @PROT_WRITE
%126 = or i32 %124, %125
%127 = load i32, i32* @MAP_SHARED
%128 = load i32, i32* %7
%129 = load i32, i32* %11
%130 = call i8* @mmap(i32* null, i64 %121, i32 %126, i32 %127, i32 %128, i32 %129)
store i8* %130, i8** %6
%131 = load i8*, i8** %6
%132 = load i8*, i8** @MAP_FAILED
%133 = icmp eq i8* %131, %132
br i1 %133, label %134, label %140
134: ; preds = %111
br label %135
135: ; preds = %134
%136 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
%137 = load i32, i32* @EXIT_FAILURE
%138 = call i32 @exit(i32 %137)
unreachable
139: ; No predecessors!
br label %140
140: ; preds = %139, %111
%141 = load i8*, i8** %6
%142 = load i8*, i8** %6
%143 = load i8, i8* %142
%144 = sext i8 %143 to i32
%145 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i64 0, i64 0), i8* %141, i32 %144)
%146 = load i32, i32* @STDOUT_FILENO
%147 = load i8*, i8** %6
%148 = load i32, i32* %10
%149 = sext i32 %148 to i64
%150 = getelementptr inbounds i8, i8* %147, i64 %149
%151 = load i32, i32* %11
%152 = sext i32 %151 to i64
%153 = sub i64 0, %152
%154 = getelementptr inbounds i8, i8* %150, i64 %153
%155 = load i64, i64* %12
%156 = call i64 @write(i32 %146, i8* %154, i64 %155)
store i64 %156, i64* %13
%157 = load i64, i64* %13
%158 = load i64, i64* %12
%159 = icmp ne i64 %157, %158
br i1 %159, label %160, label %174
160: ; preds = %140
%161 = load i64, i64* %13
%162 = icmp eq i64 %161, -1
br i1 %162, label %163, label %169
163: ; preds = %160
br label %164
164: ; preds = %163
%165 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.10, i64 0, i64 0))
%166 = load i32, i32* @EXIT_FAILURE
%167 = call i32 @exit(i32 %166)
unreachable
168: ; No predecessors!
br label %169
169: ; preds = %168, %160
%170 = load i32, i32* @stderr
%171 = call i32 (i32, i8*, ...) @fprintf(i32 %170, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.11, i64 0, i64 0))
%172 = load i32, i32* @EXIT_FAILURE
%173 = call i32 @exit(i32 %172)
unreachable
174: ; preds = %140
%175 = call i32 (...) @getchar()
%176 = load i32, i32* @EXIT_SUCCESS
%177 = call i32 @exit(i32 %176)
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [25 x i8] c"%s file length [offset]\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [6 x i8] c"fstat\00"
@.str.3 = [15 x i8] c"PAGE_SIZE:%lx\0A\00"
@_SC_PAGE_SIZE = global i32 0
@.str.4 = [15 x i8] c"pa_offset:%lx\0A\00"
@.str.5 = [29 x i8] c"pa_offset:%lx file size:%lx\0A\00"
@.str.6 = [28 x i8] c"offset is past end of file\0A\00"
@.str.7 = [11 x i8] c"length:%d\0A\00"
@PROT_EXEC = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.8 = [5 x i8] c"mmap\00"
@.str.9 = [12 x i8] c"addr:%p %x\0A\00"
@STDOUT_FILENO = global i32 0
@.str.10 = [6 x i8] c"write\00"
@.str.11 = [14 x i8] c"partial write\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = add i32 %0, -5
%6 = icmp ult i32 %5, -2
br i1 %6, label %7, label %13
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i8*, i8** %1
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = load i32, i32* @O_RDWR
%17 = tail call i32 @open(i8* %15, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %23
19: ; preds = %13
%20 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %13
%24 = call i32 @fstat(i32 %17, %struct.stat* %3)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = tail call i32 @exit(i32 %28)
unreachable
30: ; preds = %23
%31 = load i32, i32* @_SC_PAGE_SIZE
%32 = tail call i32 @sysconf(i32 %31)
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %32)
%34 = getelementptr inbounds i8*, i8** %1, i64 3
%35 = load i8*, i8** %34
%36 = tail call i8* @atoi(i8* %35)
%37 = ptrtoint i8* %36 to i64
%38 = trunc i64 %37 to i32
%39 = load i32, i32* @_SC_PAGE_SIZE
%40 = tail call i32 @sysconf(i32 %39)
%41 = sub i32 0, %40
%42 = and i32 %38, %41
%43 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %42)
%44 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%45 = load i32, i32* %44
%46 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %42, i32 %45)
%47 = icmp sgt i32 %45, %38
br i1 %47, label %53, label %48
48: ; preds = %30
%49 = load i32, i32* @stderr
%50 = tail call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
%51 = load i32, i32* @EXIT_FAILURE
%52 = tail call i32 @exit(i32 %51)
unreachable
53: ; preds = %30
%54 = icmp eq i32 %0, 4
br i1 %54, label %55, label %68
55: ; preds = %53
%56 = getelementptr inbounds i8*, i8** %1, i64 2
%57 = load i8*, i8** %56
%58 = tail call i8* @atoi(i8* %57)
%59 = ptrtoint i8* %58 to i64
%60 = shl i64 %37, 32
%61 = ashr exact i64 %60, 32
%62 = add i64 %61, %59
%63 = sext i32 %45 to i64
%64 = icmp ugt i64 %62, %63
br i1 %64, label %65, label %73
65: ; preds = %55
%66 = sub nsw i32 %45, %38
%67 = sext i32 %66 to i64
br label %73
68: ; preds = %53
%69 = sub nsw i32 %45, %38
%70 = sext i32 %69 to i64
%71 = shl i64 %37, 32
%72 = ashr exact i64 %71, 32
br label %73
73: ; preds = %55, %65, %68
%74 = phi i64 [ %61, %55 ], [ %61, %65 ], [ %72, %68 ]
%75 = phi i64 [ %59, %55 ], [ %67, %65 ], [ %70, %68 ]
%76 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i64 %75)
%77 = tail call i32 (...) @getchar()
%78 = sext i32 %42 to i64
%79 = sub nsw i64 %74, %78
%80 = add i64 %79, %75
%81 = load i32, i32* @PROT_EXEC
%82 = load i32, i32* @PROT_READ
%83 = or i32 %82, %81
%84 = load i32, i32* @PROT_WRITE
%85 = or i32 %83, %84
%86 = load i32, i32* @MAP_SHARED
%87 = tail call i8* @mmap(i32* null, i64 %80, i32 %85, i32 %86, i32 %17, i32 %42)
%88 = load i8*, i8** @MAP_FAILED
%89 = icmp eq i8* %87, %88
br i1 %89, label %90, label %94
90: ; preds = %73
%91 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
%92 = load i32, i32* @EXIT_FAILURE
%93 = tail call i32 @exit(i32 %92)
unreachable
94: ; preds = %73
%95 = sub nsw i64 0, %78
%96 = load i8, i8* %87
%97 = sext i8 %96 to i32
%98 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i64 0, i64 0), i8* %87, i32 %97)
%99 = load i32, i32* @STDOUT_FILENO
%100 = getelementptr inbounds i8, i8* %87, i64 %74
%101 = getelementptr inbounds i8, i8* %100, i64 %95
%102 = tail call i64 @write(i32 %99, i8* %101, i64 %75)
%103 = icmp eq i64 %102, %75
br i1 %103, label %115, label %104
104: ; preds = %94
%105 = icmp eq i64 %102, -1
br i1 %105, label %106, label %110
106: ; preds = %104
%107 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.10, i64 0, i64 0))
%108 = load i32, i32* @EXIT_FAILURE
%109 = tail call i32 @exit(i32 %108)
unreachable
110: ; preds = %104
%111 = load i32, i32* @stderr
%112 = tail call i32 (i32, i8*, ...) @fprintf(i32 %111, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.11, i64 0, i64 0))
%113 = load i32, i32* @EXIT_FAILURE
%114 = tail call i32 @exit(i32 %113)
unreachable
115: ; preds = %94
%116 = tail call i32 (...) @getchar()
%117 = load i32, i32* @EXIT_SUCCESS
%118 = tail call i32 @exit(i32 %117)
unreachable
}