#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 {
  size_t st_size;
};

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ _SC_PAGESIZE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fstat(int, struct stat *);
scalar_t__ mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int, int);
int /*<<< orphan*/ munmap(char *, int);
int open(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, size_t);
int sysconf(int /*<<< orphan*/);
int main(int argc, char **argv) {
  int fd, i;
  int pagesize, offset;
  char *p_map;
  struct stat sb;
  pagesize = sysconf(_SC_PAGESIZE);
  printf("pagesize is %d\n", pagesize);
  fd = open(argv[1], O_RDWR, 00777);
  fstat(fd, &sb);
  printf("file size is %zd\n", (size_t)sb.st_size);
  offset = 0;
  p_map = (char *)mmap(NULL, pagesize * 2, PROT_READ | PROT_WRITE, MAP_SHARED,
                       fd, offset);
  close(fd);
  p_map[sb.st_size] = '9';
  p_map[pagesize] = '9';
  munmap(p_map, pagesize * 2);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rsi
lea    0xe5e(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
mov    $0x1ff,%edx
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1090 <fstat@plt>
mov    -0x30(%rbp),%rsi
lea    0xe34(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x1c(%rbp),%esi
shl    $0x1,%esi
mov    0x2e03(%rip),%rax
mov    (%rax),%edx
mov    0x2dc2(%rip),%rax
or     (%rax),%edx
mov    0x2de1(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
mov    -0x20(%rbp),%r9d
xor    %eax,%eax
mov    %eax,%edi
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x39,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x39,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
shl    $0x1,%esi
call   1060 <munmap@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
movslq %eax,%r14
lea    0xe69(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    0x8(%rbx),%rdi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
mov    $0x1ff,%edx
call   1070 <open@plt>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
mov    0x8(%rsp),%r15
lea    0xe40(%rip),%rdi
mov    %r15,%rsi
call   1040 <printf@plt>
lea    (%r14,%r14,1),%r12d
mov    0x2e15(%rip),%rax
mov    0x2dd6(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2df3(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r12d,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1050 <close@plt>
movb   $0x39,(%rbx,%r15,1)
movb   $0x39,(%rbx,%r14,1)
mov    %rbx,%rdi
mov    %r12d,%esi
call   1060 <munmap@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@_SC_PAGESIZE = global i32 0
@.str = [16 x i8] c"pagesize is %d\0A\00"
@O_RDWR = global i32 0
@.str.1 = [18 x i8] c"file size is %zd\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* @_SC_PAGESIZE
%13 = call i32 @sysconf(i32 %12)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = sext i32 %14 to i64
%16 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i64 %15)
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = load i32, i32* @O_RDWR
%21 = call i32 @open(i8* %19, i32 %20, i32 511)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = call i32 @fstat(i32 %22, %struct.stat* %11)
%24 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%25 = load i64, i64* %24
%26 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i64 %25)
store i32 0, i32* %9
%27 = load i32, i32* %8
%28 = mul nsw i32 %27, 2
%29 = load i32, i32* @PROT_READ
%30 = load i32, i32* @PROT_WRITE
%31 = or i32 %29, %30
%32 = load i32, i32* @MAP_SHARED
%33 = load i32, i32* %6
%34 = load i32, i32* %9
%35 = call i64 @mmap(i32* null, i32 %28, i32 %31, i32 %32, i32 %33, i32 %34)
%36 = inttoptr i64 %35 to i8*
store i8* %36, i8** %10
%37 = load i32, i32* %6
%38 = call i32 @close(i32 %37)
%39 = load i8*, i8** %10
%40 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%41 = load i64, i64* %40
%42 = getelementptr inbounds i8, i8* %39, i64 %41
store i8 57, i8* %42
%43 = load i8*, i8** %10
%44 = load i32, i32* %8
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %43, i64 %45
store i8 57, i8* %46
%47 = load i8*, i8** %10
%48 = load i32, i32* %8
%49 = mul nsw i32 %48, 2
%50 = call i32 @munmap(i8* %47, i32 %49)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@_SC_PAGESIZE = global i32 0
@.str = [16 x i8] c"pagesize is %d\0A\00"
@O_RDWR = global i32 0
@.str.1 = [18 x i8] c"file size is %zd\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @_SC_PAGESIZE
%6 = tail call i32 @sysconf(i32 %5)
%7 = sext i32 %6 to i64
%8 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i64 %7)
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDWR
%12 = tail call i32 @open(i8* %10, i32 %11, i32 511)
%13 = call i32 @fstat(i32 %12, %struct.stat* %3)
%14 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%15 = load i64, i64* %14
%16 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i64 %15)
%17 = shl nsw i32 %6, 1
%18 = load i32, i32* @PROT_READ
%19 = load i32, i32* @PROT_WRITE
%20 = or i32 %19, %18
%21 = load i32, i32* @MAP_SHARED
%22 = tail call i64 @mmap(i32* null, i32 %17, i32 %20, i32 %21, i32 %12, i32 0)
%23 = inttoptr i64 %22 to i8*
%24 = tail call i32 @close(i32 %12)
%25 = getelementptr inbounds i8, i8* %23, i64 %15
store i8 57, i8* %25
%26 = getelementptr inbounds i8, i8* %23, i64 %7
store i8 57, i8* %26
%27 = tail call i32 @munmap(i8* %23, i32 %17)
ret i32 0
}