#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 *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ ftruncate(int, size_t);
scalar_t__ mmap(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/, int,
                int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, size_t);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  int fd;
  size_t length;
  int i;
  int *ptr;
  fd = open("mapped.dat", O_RDWR | O_CREAT, 0666);
  if (fd == -1) {
    printf("File open failed!\n");
    exit(1);
  }
  length = 100000 * sizeof(int);
  ftruncate(fd, length);
  ptr = (int *)mmap(NULL, length, PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0);
  close(fd);
  if (ptr == MAP_FAILED) {
    printf("Mapping failed!\n");
    exit(2);
  }
  (*ptr)++;
  printf("%d", *ptr);
  munmap((void *)ptr, length);
  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
mov    0x2e69(%rip),%rax
or     (%rax),%esi
lea    0xe68(%rip),%rdi
mov    $0x1b6,%edx
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11c7 <main+0x57>
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
movq   $0x61a80,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1050 <ftruncate@plt>
mov    -0x10(%rbp),%rsi
mov    0x2dda(%rip),%rax
mov    (%rax),%edx
mov    0x2e01(%rip),%rax
or     (%rax),%edx
mov    0x2de8(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%rax
mov    0x2d97(%rip),%rcx
cmp    (%rcx),%rax
jne    1242 <main+0xd2>
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
lea    0xdd5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <munmap@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e46(%rip),%rax
mov    0x2e67(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe64(%rip),%rdi
mov    $0x1b6,%edx
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1227 <main+0xa7>
mov    %eax,%ebp
mov    $0x61a80,%esi
mov    %eax,%edi
call   1060 <ftruncate@plt>
mov    0x2e00(%rip),%rax
mov    0x2e29(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e0e(%rip),%rax
mov    (%rax),%ecx
mov    $0x61a80,%esi
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1070 <close@plt>
mov    0x2dc1(%rip),%rax
cmp    %rbx,(%rax)
je     123d <main+0xbd>
mov    (%rbx),%esi
add    $0x1,%esi
mov    %esi,(%rbx)
lea    0xe01(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x61a80,%esi
mov    %rbx,%rdi
call   1080 <munmap@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdf0(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xdca(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"mapped.dat\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [19 x i8] c"File open failed!\0A\00"
@PROT_WRITE = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str.2 = [17 x i8] c"Mapping failed!\0A\00"
@.str.3 = [3 x i8] c"%d\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32*
store i32 0, i32* %1
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_CREAT
%8 = or i32 %6, %7
%9 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %8, i32 438)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %0
store i64 400000, i64* %3
%16 = load i32, i32* %2
%17 = load i64, i64* %3
%18 = call i32 @ftruncate(i32 %16, i64 %17)
%19 = load i64, i64* %3
%20 = load i32, i32* @PROT_WRITE
%21 = load i32, i32* @PROT_READ
%22 = or i32 %20, %21
%23 = load i32, i32* @MAP_SHARED
%24 = load i32, i32* %2
%25 = call i64 @mmap(i32* null, i64 %19, i32 %22, i32 %23, i32 %24, i32 0)
%26 = inttoptr i64 %25 to i32*
store i32* %26, i32** %5
%27 = load i32, i32* %2
%28 = call i32 @close(i32 %27)
%29 = load i32*, i32** %5
%30 = load i32*, i32** @MAP_FAILED
%31 = icmp eq i32* %29, %30
br i1 %31, label %32, label %35
32: ; preds = %15
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%34 = call i32 @exit(i32 2)
unreachable
35: ; preds = %15
%36 = load i32*, i32** %5
%37 = load i32, i32* %36
%38 = add nsw i32 %37, 1
store i32 %38, i32* %36
%39 = load i32*, i32** %5
%40 = load i32, i32* %39
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %40)
%42 = load i32*, i32** %5
%43 = bitcast i32* %42 to i8*
%44 = load i64, i64* %3
%45 = call i32 @munmap(i8* %43, i64 %44)
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"mapped.dat\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@PROT_WRITE = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str.3 = [3 x i8] c"%d\00"
@str = [16 x i8] c"Mapping failed!\00"
@str.4 = [18 x i8] c"File open failed!\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = load i32, i32* @O_CREAT
%3 = or i32 %2, %1
%4 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %3, i32 438)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.4, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = tail call i32 @ftruncate(i32 %4, i64 400000)
%11 = load i32, i32* @PROT_WRITE
%12 = load i32, i32* @PROT_READ
%13 = or i32 %12, %11
%14 = load i32, i32* @MAP_SHARED
%15 = tail call i64 @mmap(i32* null, i64 400000, i32 %13, i32 %14, i32 %4, i32 0)
%16 = inttoptr i64 %15 to i32*
%17 = tail call i32 @close(i32 %4)
%18 = load i32*, i32** @MAP_FAILED
%19 = icmp eq i32* %18, %16
br i1 %19, label %20, label %23
20: ; preds = %9
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%22 = tail call i32 @exit(i32 2)
unreachable
23: ; preds = %9
%24 = load i32, i32* %16
%25 = add nsw i32 %24, 1
store i32 %25, i32* %16
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%27 = inttoptr i64 %15 to i8*
%28 = tail call i32 @munmap(i8* %27, i64 400000)
ret i32 0
}