#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;
};

/* Variables and functions */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fstat(int, struct stat *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/);
int getpagesize();
char *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
scalar_t__ strcmp(char *, char *);
int main(int argc, char *argv[]) {
  char *filename;
  char *ptr, *buf;
  int ret, fd;
  struct stat stat_buf;
  int page_size = getpagesize();
  int offset, remain;
  if ((argc < 2) || (strcmp(argv[1], "-h") == 0)) {
    fprintf(stderr, "Usage: mmap_test <filename>\n");
    return 1;
  }
  filename = argv[1];
  fd = open(filename, O_RDONLY);
  if (fd < 0) {
    perror("Open");
    return 1;
  }
  ret = fstat(fd, &stat_buf);
  if (ret) {
    perror("Stat");
    return 1;
  }
  buf = mmap(NULL, stat_buf.st_size, PROT_READ, MAP_SHARED, fd, 0);
  if (buf == MAP_FAILED) {
    perror("MMap");
    return 1;
  }
  offset = stat_buf.st_size % page_size;
  ptr = buf + (stat_buf.st_size - offset);
  remain = page_size - offset;
  fprintf(stdout, "buf = %p, ptr = %p, size = %lu, offset = %d, remain = %d\n",
          buf, ptr, stat_buf.st_size, offset, remain);
  fprintf(stdout, "tail of ptr: \"");
  fwrite(ptr, sizeof(char), offset, stdout);
  fprintf(stdout, "\"\n");
  fprintf(stdout, "tail of page: \"");
  fwrite(ptr + offset, sizeof(char), remain, stdout);
  fprintf(stdout, "\"\n");
  close(fd);
  munmap(buf, stat_buf.st_size);
  return 0;
}


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)
mov    $0x0,%al
call   10a0 <getpagesize@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     11e8 <main+0x48>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe27(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    120b <main+0x6b>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
lea    0xe0b(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13da <main+0x23a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    124e <main+0xae>
lea    0xde3(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13da <main+0x23a>
mov    -0x30(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   10c0 <fstat@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     127f <main+0xdf>
lea    0xdb7(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13da <main+0x23a>
mov    -0x38(%rbp),%esi
mov    0x2d6f(%rip),%rax
mov    (%rax),%edx
mov    0x2d56(%rip),%rax
mov    (%rax),%ecx
mov    -0x30(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2d05(%rip),%rcx
cmp    (%rcx),%rax
jne    12d4 <main+0x134>
lea    0xd67(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13da <main+0x23a>
mov    -0x38(%rbp),%eax
cltd
idivl  -0x3c(%rbp)
mov    %edx,-0x40(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x38(%rbp),%ecx
sub    -0x40(%rbp),%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x3c(%rbp),%eax
sub    -0x40(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    0x2cbe(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x38(%rbp),%r8d
mov    -0x40(%rbp),%r9d
mov    -0x44(%rbp),%eax
lea    0xd11(%rip),%rsi
mov    %eax,(%rsp)
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2c91(%rip),%rax
mov    (%rax),%edi
lea    0xd31(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x40(%rbp),%edx
mov    0x2c73(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   10b0 <fwrite@plt>
mov    0x2c60(%rip),%rax
mov    (%rax),%edi
lea    0xd0f(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2c49(%rip),%rax
mov    (%rax),%edi
lea    0xcfb(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
movslq -0x40(%rbp),%rax
add    %rax,%rdi
mov    -0x44(%rbp),%edx
mov    0x2c24(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   10b0 <fwrite@plt>
mov    0x2c11(%rip),%rax
mov    (%rax),%edi
lea    0xcc0(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%esi
call   1070 <munmap@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbp
mov    %edi,%ebx
xor    %eax,%eax
call   10a0 <getpagesize@plt>
cmp    $0x2,%ebx
jl     1311 <main+0x171>
mov    %eax,%r12d
mov    0x8(%rbp),%rdi
lea    0xe2f(%rip),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     1311 <main+0x171>
mov    0x8(%rbp),%rdi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     1340 <main+0x1a0>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10c0 <fstat@plt>
test   %eax,%eax
jne    1349 <main+0x1a9>
mov    0x8(%rsp),%r15d
mov    0x2ddd(%rip),%rax
mov    (%rax),%edx
mov    0x2dc4(%rip),%rax
mov    (%rax),%ecx
xor    %r13d,%r13d
xor    %edi,%edi
mov    %r15d,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rcx
mov    0x2d75(%rip),%rax
cmp    (%rax),%rcx
je     1352 <main+0x1b2>
mov    %r15d,%eax
cltd
idiv   %r12d
mov    %edx,%ebp
mov    %r15d,%eax
sub    %edx,%eax
movslq %eax,%r14
add    %rcx,%r14
sub    %edx,%r12d
mov    %ebx,0xc(%rsp)
mov    0x2d52(%rip),%rbx
mov    %rcx,0x10(%rsp)
mov    (%rbx),%edi
mov    %r12d,(%rsp)
lea    0xdaf(%rip),%rsi
mov    %rcx,%rdx
mov    %r14,%rcx
mov    %r15d,%r8d
mov    %ebp,%r9d
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%rbx),%edi
lea    0xdcd(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%rbx),%ecx
mov    %r14,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
call   10b0 <fwrite@plt>
mov    (%rbx),%edi
lea    0xdbb(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%rbx),%edi
lea    0xdae(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
movslq %ebp,%rdi
add    %r14,%rdi
mov    (%rbx),%ecx
mov    $0x1,%esi
mov    %r12d,%edx
call   10b0 <fwrite@plt>
mov    (%rbx),%edi
lea    0xd86(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x10(%rsp),%rdi
mov    %r15d,%esi
call   1070 <munmap@plt>
jmp    132e <main+0x18e>
mov    0x2cb0(%rip),%rax
mov    (%rax),%edi
lea    0xce2(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%r13d
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcd9(%rip),%rdi
jmp    1359 <main+0x1b9>
lea    0xcd5(%rip),%rdi
jmp    1359 <main+0x1b9>
lea    0xcd1(%rip),%rdi
call   1090 <perror@plt>
jmp    1328 <main+0x188>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [3 x i8] c"-h\00"
@stderr = global i32 0
@.str.1 = [29 x i8] c"Usage: mmap_test <filename>\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [5 x i8] c"Open\00"
@.str.3 = [5 x i8] c"Stat\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [5 x i8] c"MMap\00"
@stdout = global i32 0
@.str.5 = [58 x i8] c"buf = %p, ptr = %p, size = %lu, offset = %d, remain = %d\0A\00"
@.str.6 = [15 x i8] c"tail of ptr: \22\00"
@.str.7 = [3 x i8] c"\22\0A\00"
@.str.8 = [16 x i8] c"tail of page: \22\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.stat
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = call i32 (...) @getpagesize()
store i32 %15, i32* %12
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 2
br i1 %17, label %24, label %18
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i64 @strcmp(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %18, %2
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %105
27: ; preds = %18
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
store i8* %30, i8** %6
%31 = load i8*, i8** %6
%32 = load i32, i32* @O_RDONLY
%33 = call i32 @open(i8* %31, i32 %32)
store i32 %33, i32* %10
%34 = load i32, i32* %10
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %27
%37 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %105
38: ; preds = %27
%39 = load i32, i32* %10
%40 = call i32 @fstat(i32 %39, %struct.stat* %11)
store i32 %40, i32* %9
%41 = load i32, i32* %9
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %38
%44 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %105
45: ; preds = %38
%46 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%47 = load i32, i32* %46
%48 = load i32, i32* @PROT_READ
%49 = load i32, i32* @MAP_SHARED
%50 = load i32, i32* %10
%51 = call i8* @mmap(i32* null, i32 %47, i32 %48, i32 %49, i32 %50, i32 0)
store i8* %51, i8** %8
%52 = load i8*, i8** %8
%53 = load i8*, i8** @MAP_FAILED
%54 = icmp eq i8* %52, %53
br i1 %54, label %55, label %57
55: ; preds = %45
%56 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %105
57: ; preds = %45
%58 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%59 = load i32, i32* %58
%60 = load i32, i32* %12
%61 = srem i32 %59, %60
store i32 %61, i32* %13
%62 = load i8*, i8** %8
%63 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%64 = load i32, i32* %63
%65 = load i32, i32* %13
%66 = sub nsw i32 %64, %65
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds i8, i8* %62, i64 %67
store i8* %68, i8** %7
%69 = load i32, i32* %12
%70 = load i32, i32* %13
%71 = sub nsw i32 %69, %70
store i32 %71, i32* %14
%72 = load i32, i32* @stdout
%73 = load i8*, i8** %8
%74 = load i8*, i8** %7
%75 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%76 = load i32, i32* %75
%77 = load i32, i32* %13
%78 = load i32, i32* %14
%79 = call i32 (i32, i8*, ...) @fprintf(i32 %72, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.5, i64 0, i64 0), i8* %73, i8* %74, i32 %76, i32 %77, i32 %78)
%80 = load i32, i32* @stdout
%81 = call i32 (i32, i8*, ...) @fprintf(i32 %80, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%82 = load i8*, i8** %7
%83 = load i32, i32* %13
%84 = load i32, i32* @stdout
%85 = call i32 @fwrite(i8* %82, i32 1, i32 %83, i32 %84)
%86 = load i32, i32* @stdout
%87 = call i32 (i32, i8*, ...) @fprintf(i32 %86, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
%88 = load i32, i32* @stdout
%89 = call i32 (i32, i8*, ...) @fprintf(i32 %88, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0))
%90 = load i8*, i8** %7
%91 = load i32, i32* %13
%92 = sext i32 %91 to i64
%93 = getelementptr inbounds i8, i8* %90, i64 %92
%94 = load i32, i32* %14
%95 = load i32, i32* @stdout
%96 = call i32 @fwrite(i8* %93, i32 1, i32 %94, i32 %95)
%97 = load i32, i32* @stdout
%98 = call i32 (i32, i8*, ...) @fprintf(i32 %97, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
%99 = load i32, i32* %10
%100 = call i32 @close(i32 %99)
%101 = load i8*, i8** %8
%102 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%103 = load i32, i32* %102
%104 = call i32 @munmap(i8* %101, i32 %103)
store i32 0, i32* %3
br label %105
105: ; preds = %57, %55, %43, %36, %24
%106 = load i32, i32* %3
ret i32 %106
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [3 x i8] c"-h\00"
@stderr = global i32 0
@.str.1 = [29 x i8] c"Usage: mmap_test <filename>\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [5 x i8] c"Open\00"
@.str.3 = [5 x i8] c"Stat\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [5 x i8] c"MMap\00"
@stdout = global i32 0
@.str.5 = [58 x i8] c"buf = %p, ptr = %p, size = %lu, offset = %d, remain = %d\0A\00"
@.str.6 = [15 x i8] c"tail of ptr: \22\00"
@.str.7 = [3 x i8] c"\22\0A\00"
@.str.8 = [16 x i8] c"tail of page: \22\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = tail call i32 (...) @getpagesize()
%6 = icmp slt i32 %0, 2
br i1 %6, label %12, label %7
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i64 @strcmp(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %7, %2
%13 = load i32, i32* @stderr
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %61
15: ; preds = %7
%16 = load i8*, i8** %8
%17 = load i32, i32* @O_RDONLY
%18 = tail call i32 @open(i8* %16, i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %61
22: ; preds = %15
%23 = call i32 @fstat(i32 %18, %struct.stat* %3)
%24 = icmp eq i32 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %22
%26 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %61
27: ; preds = %22
%28 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%29 = load i32, i32* %28
%30 = load i32, i32* @PROT_READ
%31 = load i32, i32* @MAP_SHARED
%32 = tail call i8* @mmap(i32* null, i32 %29, i32 %30, i32 %31, i32 %18, i32 0)
%33 = load i8*, i8** @MAP_FAILED
%34 = icmp eq i8* %32, %33
br i1 %34, label %35, label %37
35: ; preds = %27
%36 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %61
37: ; preds = %27
%38 = srem i32 %29, %5
%39 = sub nsw i32 %29, %38
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %32, i64 %40
%42 = sub nsw i32 %5, %38
%43 = load i32, i32* @stdout
%44 = tail call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.5, i64 0, i64 0), i8* %32, i8* %41, i32 %29, i32 %38, i32 %42)
%45 = load i32, i32* @stdout
%46 = tail call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%47 = load i32, i32* @stdout
%48 = tail call i32 @fwrite(i8* %41, i32 1, i32 %38, i32 %47)
%49 = load i32, i32* @stdout
%50 = tail call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
%51 = load i32, i32* @stdout
%52 = tail call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0))
%53 = sext i32 %38 to i64
%54 = getelementptr inbounds i8, i8* %41, i64 %53
%55 = load i32, i32* @stdout
%56 = tail call i32 @fwrite(i8* %54, i32 1, i32 %42, i32 %55)
%57 = load i32, i32* @stdout
%58 = tail call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
%59 = tail call i32 @close(i32 %18)
%60 = tail call i32 @munmap(i8* %32, i32 %29)
br label %61
61: ; preds = %37, %35, %25, %20, %12
%62 = phi i32 [ 1, %12 ], [ 1, %20 ], [ 1, %25 ], [ 1, %35 ], [ 0, %37 ]
ret i32 %62
}