#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int ftruncate(int /*<<< orphan*/, int);
void *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/,
           int /*<<< orphan*/, int /*<<< orphan*/);
int munmap(void *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int main(void) {
  FILE *fp = fopen("testfile", "a+");
  if (!fp) {
    perror("fopen");
    return 1;
  }
  int rv = ftruncate(fileno(fp), 0x10000f000);
  if (rv) {
    perror("ftruncate");
    return 1;
  } else {
    printf("large_mmap: ftruncate OK\n");
  }
  void *a = mmap(NULL, 0x8000f000, PROT_READ | PROT_WRITE, MAP_PRIVATE,
                 fileno(fp), 0);
  if (a == MAP_FAILED) {
    perror("mmap 1");
    return 1;
  }
  ((char *)a)[0x80000000] = 0xff;
  printf("large_mmap: mmap 1 completed OK\n");
  rv = munmap(a, 0x8000f000);
  if (rv) {
    perror("mumap");
    return 1;
  }
  a = mmap(NULL, 0x10000f000, PROT_READ | PROT_WRITE, MAP_PRIVATE, fileno(fp),
           0);
  if (a == MAP_FAILED) {
    perror("mmap 2");
    return 1;
  }
  ((char *)a)[0x100000000] = 0xff;
  printf("large_mmap: mmap 2 completed OK\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rdi
lea    0xe7c(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b9 <main+0x49>
lea    0xe64(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    135b <main+0x1eb>
mov    -0x10(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
mov    $0xf000,%esi
call   1050 <ftruncate@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11f3 <main+0x83>
lea    0xe30(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    135b <main+0x1eb>
lea    0xe22(%rip),%rdi
call   1040 <printf@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
mov    0x2dc9(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x28(%rbp)
mov    0x2db5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <fileno@plt>
mov    -0x28(%rbp),%edx
mov    -0x24(%rbp),%ecx
mov    %eax,%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x8000f000,%esi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d76(%rip),%rcx
cmp    (%rcx),%rax
jne    1273 <main+0x103>
lea    0xdd4(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    135b <main+0x1eb>
movabs $0x80000000,%rax
add    -0x20(%rbp),%rax
movb   $0xff,(%rax)
lea    0xdb2(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    $0x8000f000,%esi
call   1070 <munmap@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     12c3 <main+0x153>
lea    0xdac(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    135b <main+0x1eb>
mov    0x2d2e(%rip),%rax
mov    (%rax),%eax
mov    0x2d05(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x30(%rbp)
mov    0x2cf1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <fileno@plt>
mov    -0x30(%rbp),%edx
mov    -0x2c(%rbp),%ecx
mov    %eax,%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0xf000,%esi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2cb2(%rip),%rcx
cmp    (%rcx),%rax
jne    1337 <main+0x1c7>
lea    0xd3e(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    135b <main+0x1eb>
movabs $0x100000000,%rax
add    -0x20(%rbp),%rax
movb   $0xff,(%rax)
lea    0xd1c(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe7e(%rip),%rdi
lea    0xe80(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     128b <main+0x11b>
mov    %rax,%r14
mov    %rax,%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
mov    $0xf000,%esi
call   1050 <ftruncate@plt>
test   %eax,%eax
jne    1294 <main+0x124>
lea    0xe73(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e2f(%rip),%rax
mov    0x2e08(%rip),%r12
mov    (%r12),%ebx
or     (%rax),%ebx
mov    0x2df3(%rip),%r13
mov    0x0(%r13),%ebp
mov    %r14,%rdi
call   1060 <fileno@plt>
xor    %edi,%edi
mov    $0x8000f000,%esi
mov    %ebx,%edx
mov    %ebp,%ecx
mov    %eax,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    0x2dc2(%rip),%r15
cmp    (%r15),%rax
je     129d <main+0x12d>
mov    %rax,%rbx
mov    $0x80000000,%eax
movb   $0xff,(%rbx,%rax,1)
lea    0xe27(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    $0x8000f000,%esi
call   1070 <munmap@plt>
test   %eax,%eax
jne    12a6 <main+0x136>
mov    (%r12),%ebp
mov    0x2db5(%rip),%rax
or     (%rax),%ebp
mov    0x0(%r13),%ebx
mov    %r14,%rdi
call   1060 <fileno@plt>
xor    %r14d,%r14d
xor    %edi,%edi
mov    $0xf000,%esi
mov    %ebp,%edx
mov    %ebx,%ecx
mov    %eax,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
cmp    (%r15),%rax
je     12af <main+0x13f>
movabs $0x100000000,%rcx
movb   $0xff,(%rax,%rcx,1)
lea    0xde5(%rip),%rdi
call   1030 <puts@plt>
jmp    12c1 <main+0x151>
lea    0xd7a(%rip),%rdi
jmp    12b6 <main+0x146>
lea    0xd77(%rip),%rdi
jmp    12b6 <main+0x146>
lea    0xd78(%rip),%rdi
jmp    12b6 <main+0x146>
lea    0xd76(%rip),%rdi
jmp    12b6 <main+0x146>
lea    0xd73(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"testfile\00"
@.str.1 = [3 x i8] c"a+\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [10 x i8] c"ftruncate\00"
@.str.4 = [26 x i8] c"large_mmap: ftruncate OK\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.5 = [7 x i8] c"mmap 1\00"
@.str.6 = [33 x i8] c"large_mmap: mmap 1 completed OK\0A\00"
@.str.7 = [6 x i8] c"mumap\00"
@.str.8 = [7 x i8] c"mmap 2\00"
@.str.9 = [33 x i8] c"large_mmap: mmap 2 completed OK\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i8*
store i32 0, i32* %1
%5 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %2
%6 = load i32*, i32** %2
%7 = icmp ne i32* %6, null
br i1 %7, label %10, label %8
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %60
10: ; preds = %0
%11 = load i32*, i32** %2
%12 = call i32 @fileno(i32* %11)
%13 = call i32 @ftruncate(i32 %12, i32 61440)
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %10
%17 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %60
18: ; preds = %10
%19 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %20
20: ; preds = %18
%21 = load i32, i32* @PROT_READ
%22 = load i32, i32* @PROT_WRITE
%23 = or i32 %21, %22
%24 = load i32, i32* @MAP_PRIVATE
%25 = load i32*, i32** %2
%26 = call i32 @fileno(i32* %25)
%27 = call i8* @mmap(i32* null, i32 -2147422208, i32 %23, i32 %24, i32 %26, i32 0)
store i8* %27, i8** %4
%28 = load i8*, i8** %4
%29 = load i8*, i8** @MAP_FAILED
%30 = icmp eq i8* %28, %29
br i1 %30, label %31, label %33
31: ; preds = %20
%32 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %60
33: ; preds = %20
%34 = load i8*, i8** %4
%35 = getelementptr inbounds i8, i8* %34, i64 2147483648
store i8 -1, i8* %35
%36 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0))
%37 = load i8*, i8** %4
%38 = call i32 @munmap(i8* %37, i32 -2147422208)
store i32 %38, i32* %3
%39 = load i32, i32* %3
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %33
%42 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %1
br label %60
43: ; preds = %33
%44 = load i32, i32* @PROT_READ
%45 = load i32, i32* @PROT_WRITE
%46 = or i32 %44, %45
%47 = load i32, i32* @MAP_PRIVATE
%48 = load i32*, i32** %2
%49 = call i32 @fileno(i32* %48)
%50 = call i8* @mmap(i32* null, i32 61440, i32 %46, i32 %47, i32 %49, i32 0)
store i8* %50, i8** %4
%51 = load i8*, i8** %4
%52 = load i8*, i8** @MAP_FAILED
%53 = icmp eq i8* %51, %52
br i1 %53, label %54, label %56
54: ; preds = %43
%55 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
store i32 1, i32* %1
br label %60
56: ; preds = %43
%57 = load i8*, i8** %4
%58 = getelementptr inbounds i8, i8* %57, i64 4294967296
store i8 -1, i8* %58
%59 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.9, i64 0, i64 0))
store i32 0, i32* %1
br label %60
60: ; preds = %56, %54, %41, %31, %16, %8
%61 = load i32, i32* %1
ret i32 %61
}

LLVM IR (-O2):
@.str = [9 x i8] c"testfile\00"
@.str.1 = [3 x i8] c"a+\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [10 x i8] c"ftruncate\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.5 = [7 x i8] c"mmap 1\00"
@.str.7 = [6 x i8] c"mumap\00"
@.str.8 = [7 x i8] c"mmap 2\00"
@str = [25 x i8] c"large_mmap: ftruncate OK\00"
@str.10 = [32 x i8] c"large_mmap: mmap 1 completed OK\00"
@str.11 = [32 x i8] c"large_mmap: mmap 2 completed OK\00"
define i32 @main() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %44
5: ; preds = %0
%6 = tail call i32 @fileno(i32* %1)
%7 = tail call i32 @ftruncate(i32 %6, i32 61440)
%8 = icmp eq i32 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %5
%10 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %44
11: ; preds = %5
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%13 = load i32, i32* @PROT_READ
%14 = load i32, i32* @PROT_WRITE
%15 = or i32 %14, %13
%16 = load i32, i32* @MAP_PRIVATE
%17 = tail call i32 @fileno(i32* %1)
%18 = tail call i8* @mmap(i32* null, i32 -2147422208, i32 %15, i32 %16, i32 %17, i32 0)
%19 = load i8*, i8** @MAP_FAILED
%20 = icmp eq i8* %18, %19
br i1 %20, label %21, label %23
21: ; preds = %11
%22 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %44
23: ; preds = %11
%24 = getelementptr inbounds i8, i8* %18, i64 2147483648
store i8 -1, i8* %24
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.10, i64 0, i64 0))
%26 = tail call i32 @munmap(i8* %18, i32 -2147422208)
%27 = icmp eq i32 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %23
%29 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
br label %44
30: ; preds = %23
%31 = load i32, i32* @PROT_READ
%32 = load i32, i32* @PROT_WRITE
%33 = or i32 %32, %31
%34 = load i32, i32* @MAP_PRIVATE
%35 = tail call i32 @fileno(i32* %1)
%36 = tail call i8* @mmap(i32* null, i32 61440, i32 %33, i32 %34, i32 %35, i32 0)
%37 = load i8*, i8** @MAP_FAILED
%38 = icmp eq i8* %36, %37
br i1 %38, label %39, label %41
39: ; preds = %30
%40 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
br label %44
41: ; preds = %30
%42 = getelementptr inbounds i8, i8* %36, i64 4294967296
store i8 -1, i8* %42
%43 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.11, i64 0, i64 0))
br label %44
44: ; preds = %9, %41, %39, %28, %21, %3
%45 = phi i32 [ 1, %3 ], [ 1, %9 ], [ 1, %21 ], [ 1, %28 ], [ 1, %39 ], [ 0, %41 ]
ret i32 %45
}