#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_ANONYMOUS;
void *MAP_FAILED;
int MAP_PRIVATE;
int MAP_SHARED;
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ make_test_file(int);
void *mmap(int /*<<< orphan*/ *, int, int, int, int, int);
int munmap(void *, int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ unlink(char *);
int main(int argc, char *argv[]) {
  void *foo;
  char *test_file = (argc > 1) ? argv[1] : "/tmp/mmaptest.tmp";
  int rv, fd;
  errno = 0;
  foo = mmap(NULL, 65536, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS,
             0, 0);
  printf("mmap() returned %p, errno = %d\n", foo, errno);
  if (foo == MAP_FAILED)
    return 1;
  rv = munmap(foo, 65536);
  printf("munmap() returned %d, errno = %d\n", rv, errno);
  if (rv)
    return 1;
  fd = open(test_file, O_RDWR | O_CREAT | O_TRUNC, 0666);
  if (fd < 0) {
    perror(test_file);
    return 1;
  }
  make_test_file(fd);
  foo = mmap(NULL, 65536, PROT_READ, MAP_SHARED, fd, 131072);
  printf("mmap() returned %p, errno = %d\n", foo, errno);
  if (foo == MAP_FAILED)
    return 1;
  if (*(unsigned long *)foo != 131072) {
    printf("mmap() with offset returned the wrong offset %ld!\n",
           *(unsigned long *)foo);
    return 1;
  }
  if (munmap(foo, 65536)) {
    printf("munmap() returned nonzero, errno = %d\n", errno);
    return 1;
  }
  close(fd);
  unlink(test_file);
  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)
cmpl   $0x1,-0x8(%rbp)
jle    11b1 <main+0x31>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    11c1 <main+0x41>
lea    0xe48(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    11c1 <main+0x41>
mov    -0x30(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2de0(%rip),%rax
movl   $0x0,(%rax)
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
mov    0x2dc2(%rip),%rax
or     (%rax),%edx
mov    0x2db1(%rip),%rax
mov    (%rax),%ecx
mov    0x2dc8(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x10000,%esi
xor    %r9d,%r9d
mov    %r9d,%r8d
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,%esi
mov    0x2d91(%rip),%rax
mov    (%rax),%edx
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rax
mov    0x2d5e(%rip),%rcx
cmp    (%rcx),%rax
jne    124f <main+0xcf>
movl   $0x1,-0x4(%rbp)
jmp    13d4 <main+0x254>
mov    -0x18(%rbp),%rdi
mov    $0x10000,%esi
call   1080 <munmap@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
mov    0x2d46(%rip),%rax
mov    (%rax),%edx
lea    0xdbf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x0,-0x24(%rbp)
je     1290 <main+0x110>
movl   $0x1,-0x4(%rbp)
jmp    13d4 <main+0x254>
mov    -0x20(%rbp),%rdi
mov    0x2d2d(%rip),%rax
mov    (%rax),%esi
mov    0x2d54(%rip),%rax
or     (%rax),%esi
mov    0x2d43(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1090 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    12db <main+0x15b>
mov    -0x20(%rbp),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d4 <main+0x254>
mov    -0x28(%rbp),%edi
call   1070 <make_test_file@plt>
mov    0x2cfe(%rip),%rax
mov    (%rax),%edx
mov    0x2ce5(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x10000,%esi
mov    $0x20000,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,%esi
mov    0x2c92(%rip),%rax
mov    (%rax),%edx
lea    0xceb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rax
mov    0x2c5f(%rip),%rcx
cmp    (%rcx),%rax
jne    134e <main+0x1ce>
movl   $0x1,-0x4(%rbp)
jmp    13d4 <main+0x254>
mov    -0x18(%rbp),%rax
cmpq   $0x20000,(%rax)
je     1382 <main+0x202>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %eax,%esi
lea    0xce5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d4 <main+0x254>
mov    -0x18(%rbp),%rdi
mov    $0x10000,%esi
call   1080 <munmap@plt>
cmp    $0x0,%eax
je     13bc <main+0x23c>
mov    0x2c10(%rip),%rax
mov    (%rax),%esi
lea    0xcde(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d4 <main+0x254>
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%rdi
call   1030 <unlink@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
cmp    $0x2,%edi
jl     1196 <main+0x16>
mov    0x8(%rsi),%rbp
jmp    119d <main+0x1d>
lea    0xe63(%rip),%rbp
mov    0x2e0c(%rip),%r12
movl   $0x0,(%r12)
mov    0x2e35(%rip),%r15
mov    0x2dee(%rip),%rax
mov    (%rax),%edx
or     (%r15),%edx
mov    0x2dda(%rip),%rax
mov    0x2df3(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    $0x10000,%esi
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    (%r12),%edx
lea    0xe21(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d97(%rip),%r14
mov    $0x1,%r13d
cmp    (%r14),%rbx
je     12f0 <main+0x170>
mov    %rbx,%rdi
mov    $0x10000,%esi
call   1080 <munmap@plt>
mov    %eax,%ebx
mov    (%r12),%edx
lea    0xe08(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
test   %ebx,%ebx
jne    12f0 <main+0x170>
mov    %r15,%rbx
mov    0x2d83(%rip),%rax
mov    0x2dac(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d99(%rip),%rax
or     (%rax),%esi
mov    %rbp,(%rsp)
mov    %rbp,%rdi
mov    $0x1b6,%edx
call   1090 <open@plt>
test   %eax,%eax
js     1318 <main+0x198>
mov    %eax,%r15d
mov    %eax,%edi
call   1070 <make_test_file@plt>
mov    (%rbx),%edx
mov    0x2d53(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x10000,%esi
mov    %r15d,%r8d
mov    $0x20000,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    (%r12),%edx
lea    0xd68(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
cmp    (%r14),%rbx
je     12f0 <main+0x170>
mov    (%rbx),%rsi
cmp    $0x20000,%rsi
jne    12e2 <main+0x162>
mov    %rbx,%rdi
mov    $0x10000,%esi
call   1080 <munmap@plt>
test   %eax,%eax
je     1302 <main+0x182>
mov    (%r12),%esi
lea    0xda7(%rip),%rdi
jmp    12e9 <main+0x169>
lea    0xd6b(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15d,%edi
call   1060 <close@plt>
mov    (%rsp),%rdi
call   1030 <unlink@plt>
xor    %r13d,%r13d
jmp    12f0 <main+0x170>
mov    (%rsp),%rdi
call   10a0 <perror@plt>
jmp    12f0 <main+0x170>

LLVM IR (-O0):
@.str = [18 x i8] c"/tmp/mmaptest.tmp\00"
@errno = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@.str.1 = [32 x i8] c"mmap() returned %p, errno = %d\0A\00"
@MAP_FAILED = global i8* null
@.str.2 = [34 x i8] c"munmap() returned %d, errno = %d\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@MAP_SHARED = global i32 0
@.str.3 = [51 x i8] c"mmap() with offset returned the wrong offset %ld!\0A\00"
@.str.4 = [39 x i8] c"munmap() returned nonzero, errno = %d\0A\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 i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp sgt i32 %10, 1
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
br label %17
16: ; preds = %2
br label %17
17: ; preds = %16, %12
%18 = phi i8* [ %15, %12 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %16 ]
store i8* %18, i8** %7
store i32 0, i32* @errno
%19 = load i32, i32* @PROT_READ
%20 = load i32, i32* @PROT_WRITE
%21 = or i32 %19, %20
%22 = load i32, i32* @MAP_PRIVATE
%23 = load i32, i32* @MAP_ANONYMOUS
%24 = or i32 %22, %23
%25 = call i8* @mmap(i32* null, i32 65536, i32 %21, i32 %24, i32 0, i32 0)
store i8* %25, i8** %6
%26 = load i8*, i8** %6
%27 = ptrtoint i8* %26 to i32
%28 = load i32, i32* @errno
%29 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %27, i32 %28)
%30 = load i8*, i8** %6
%31 = load i8*, i8** @MAP_FAILED
%32 = icmp eq i8* %30, %31
br i1 %32, label %33, label %34
33: ; preds = %17
store i32 1, i32* %3
br label %94
34: ; preds = %17
%35 = load i8*, i8** %6
%36 = call i32 @munmap(i8* %35, i32 65536)
store i32 %36, i32* %8
%37 = load i32, i32* %8
%38 = load i32, i32* @errno
%39 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %37, i32 %38)
%40 = load i32, i32* %8
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %34
store i32 1, i32* %3
br label %94
43: ; preds = %34
%44 = load i8*, i8** %7
%45 = load i32, i32* @O_RDWR
%46 = load i32, i32* @O_CREAT
%47 = or i32 %45, %46
%48 = load i32, i32* @O_TRUNC
%49 = or i32 %47, %48
%50 = call i32 @open(i8* %44, i32 %49, i32 438)
store i32 %50, i32* %9
%51 = load i32, i32* %9
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %43
%54 = load i8*, i8** %7
%55 = call i32 @perror(i8* %54)
store i32 1, i32* %3
br label %94
56: ; preds = %43
%57 = load i32, i32* %9
%58 = call i32 @make_test_file(i32 %57)
%59 = load i32, i32* @PROT_READ
%60 = load i32, i32* @MAP_SHARED
%61 = load i32, i32* %9
%62 = call i8* @mmap(i32* null, i32 65536, i32 %59, i32 %60, i32 %61, i32 131072)
store i8* %62, i8** %6
%63 = load i8*, i8** %6
%64 = ptrtoint i8* %63 to i32
%65 = load i32, i32* @errno
%66 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %64, i32 %65)
%67 = load i8*, i8** %6
%68 = load i8*, i8** @MAP_FAILED
%69 = icmp eq i8* %67, %68
br i1 %69, label %70, label %71
70: ; preds = %56
store i32 1, i32* %3
br label %94
71: ; preds = %56
%72 = load i8*, i8** %6
%73 = bitcast i8* %72 to i64*
%74 = load i64, i64* %73
%75 = icmp ne i64 %74, 131072
br i1 %75, label %76, label %82
76: ; preds = %71
%77 = load i8*, i8** %6
%78 = bitcast i8* %77 to i64*
%79 = load i64, i64* %78
%80 = trunc i64 %79 to i32
%81 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0), i32 %80)
store i32 1, i32* %3
br label %94
82: ; preds = %71
%83 = load i8*, i8** %6
%84 = call i32 @munmap(i8* %83, i32 65536)
%85 = icmp ne i32 %84, 0
br i1 %85, label %86, label %89
86: ; preds = %82
%87 = load i32, i32* @errno
%88 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i32 %87)
store i32 1, i32* %3
br label %94
89: ; preds = %82
%90 = load i32, i32* %9
%91 = call i32 @close(i32 %90)
%92 = load i8*, i8** %7
%93 = call i32 @unlink(i8* %92)
store i32 0, i32* %3
br label %94
94: ; preds = %89, %86, %76, %70, %53, %42, %33
%95 = load i32, i32* %3
ret i32 %95
}

LLVM IR (-O2):
@.str = [18 x i8] c"/tmp/mmaptest.tmp\00"
@errno = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@.str.1 = [32 x i8] c"mmap() returned %p, errno = %d\0A\00"
@MAP_FAILED = global i8* null
@.str.2 = [34 x i8] c"munmap() returned %d, errno = %d\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@MAP_SHARED = global i32 0
@.str.3 = [51 x i8] c"mmap() with offset returned the wrong offset %ld!\0A\00"
@.str.4 = [39 x i8] c"munmap() returned nonzero, errno = %d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
br label %7
7: ; preds = %2, %4
%8 = phi i8* [ %6, %4 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %2 ]
store i32 0, i32* @errno
%9 = load i32, i32* @PROT_READ
%10 = load i32, i32* @PROT_WRITE
%11 = or i32 %10, %9
%12 = load i32, i32* @MAP_PRIVATE
%13 = load i32, i32* @MAP_ANONYMOUS
%14 = or i32 %13, %12
%15 = tail call i8* @mmap(i32* null, i32 65536, i32 %11, i32 %14, i32 0, i32 0)
%16 = ptrtoint i8* %15 to i64
%17 = trunc i64 %16 to i32
%18 = load i32, i32* @errno
%19 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %17, i32 %18)
%20 = load i8*, i8** @MAP_FAILED
%21 = icmp eq i8* %15, %20
br i1 %21, label %64, label %22
22: ; preds = %7
%23 = tail call i32 @munmap(i8* %15, i32 65536)
%24 = load i32, i32* @errno
%25 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %23, i32 %24)
%26 = icmp eq i32 %23, 0
br i1 %26, label %27, label %64
27: ; preds = %22
%28 = load i32, i32* @O_RDWR
%29 = load i32, i32* @O_CREAT
%30 = or i32 %29, %28
%31 = load i32, i32* @O_TRUNC
%32 = or i32 %30, %31
%33 = tail call i32 @open(i8* %8, i32 %32, i32 438)
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %27
%36 = tail call i32 @perror(i8* %8)
br label %64
37: ; preds = %27
%38 = tail call i32 @make_test_file(i32 %33)
%39 = load i32, i32* @PROT_READ
%40 = load i32, i32* @MAP_SHARED
%41 = tail call i8* @mmap(i32* null, i32 65536, i32 %39, i32 %40, i32 %33, i32 131072)
%42 = ptrtoint i8* %41 to i64
%43 = trunc i64 %42 to i32
%44 = load i32, i32* @errno
%45 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %43, i32 %44)
%46 = load i8*, i8** @MAP_FAILED
%47 = icmp eq i8* %41, %46
br i1 %47, label %64, label %48
48: ; preds = %37
%49 = bitcast i8* %41 to i64*
%50 = load i64, i64* %49
%51 = icmp eq i64 %50, 131072
br i1 %51, label %55, label %52
52: ; preds = %48
%53 = trunc i64 %50 to i32
%54 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0), i32 %53)
br label %64
55: ; preds = %48
%56 = tail call i32 @munmap(i8* %41, i32 65536)
%57 = icmp eq i32 %56, 0
br i1 %57, label %61, label %58
58: ; preds = %55
%59 = load i32, i32* @errno
%60 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i32 %59)
br label %64
61: ; preds = %55
%62 = tail call i32 @close(i32 %33)
%63 = tail call i32 @unlink(i8* %8)
br label %64
64: ; preds = %37, %22, %7, %61, %58, %52, %35
%65 = phi i32 [ 1, %35 ], [ 1, %52 ], [ 1, %58 ], [ 0, %61 ], [ 1, %7 ], [ 1, %22 ], [ 1, %37 ]
ret i32 %65
}