#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 */
unsigned int *MAP_FAILED;
int MAP_LOCKED;
int MAP_SHARED;
int O_RDWR;
int O_SYNC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int getpagesize();
unsigned int *mmap(int /*<<< orphan*/, int, int, int, int, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int main(void) {
  int fd;
  unsigned int *kadr;
  int len = 16 * getpagesize();
  if ((fd = open("/dev/mmap_alloc", O_RDWR | O_SYNC)) < 0) {
    perror("open");
    exit(-1);
  }
  fprintf(stderr, "mmap_alloc: open OK\n");
  kadr = mmap(0, len, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED, fd, 0);
  if (kadr == MAP_FAILED) {
    perror("mmap");
    exit(-1);
  }
  fprintf(stderr, "mmap_alloc: mmap OK\n");
  if ((kadr[0] != 0xdead0000) || (kadr[1] != 0xbeef0000) ||
      (kadr[len / sizeof(int) - 2] != (0xdead0000 + len / sizeof(int) - 2)) ||
      (kadr[len / sizeof(int) - 1] != (0xbeef0000 + len / sizeof(int) - 2))) {
    fprintf(stderr, "mmap_alloc: check ERROR\n");
    fprintf(stderr, "0x%x 0x%x\n", kadr[0], kadr[1]);
    fprintf(stderr, "0x%x 0x%x\n", kadr[len / sizeof(int) - 2],
            kadr[len / sizeof(int) - 1]);
  } else {
    fprintf(stderr, "mmap_alloc: check OK\n");
  }
  close(fd);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1080 <getpagesize@plt>
shl    $0x4,%eax
mov    %eax,-0x14(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
mov    0x2e34(%rip),%rax
or     (%rax),%esi
lea    0xe5b(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11cc <main+0x5c>
lea    0xe53(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
lea    0xe39(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x14(%rbp),%esi
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
mov    0x2dba(%rip),%rax
or     (%rax),%edx
mov    0x2de9(%rip),%rax
mov    (%rax),%ecx
mov    0x2db8(%rip),%rax
or     (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2d80(%rip),%rcx
cmp    (%rcx),%rax
jne    1247 <main+0xd7>
lea    0xdf2(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
lea    0xdd8(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rax
cmpl   $0xdead0000,(%rax)
jne    12e9 <main+0x179>
mov    -0x10(%rbp),%rax
cmpl   $0xbeef0000,0x4(%rax)
jne    12e9 <main+0x179>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
shr    $0x2,%rcx
sub    $0x2,%rcx
mov    (%rax,%rcx,4),%eax
movslq -0x14(%rbp),%rcx
shr    $0x2,%rcx
movabs $0xdead0000,%rdx
add    %rdx,%rcx
sub    $0x2,%rcx
cmp    %rcx,%rax
jne    12e9 <main+0x179>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
shr    $0x2,%rcx
sub    $0x1,%rcx
mov    (%rax,%rcx,4),%eax
movslq -0x14(%rbp),%rcx
shr    $0x2,%rcx
movabs $0xbeef0000,%rdx
add    %rdx,%rcx
sub    $0x2,%rcx
cmp    %rcx,%rax
je     1366 <main+0x1f6>
mov    0x2cc8(%rip),%rax
mov    (%rax),%edi
lea    0xd4b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2cb1(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%ecx
lea    0xd40(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2c8d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
shr    $0x2,%rcx
sub    $0x2,%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
shr    $0x2,%rcx
sub    $0x1,%rcx
mov    (%rax,%rcx,4),%ecx
lea    0xd03(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    137d <main+0x20d>
mov    0x2c4b(%rip),%rax
mov    (%rax),%edi
lea    0xcf2(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
xor    %eax,%eax
call   1080 <getpagesize@plt>
mov    %eax,%ebp
mov    0x2e50(%rip),%rax
mov    0x2e41(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe66(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     12c1 <main+0x151>
mov    %eax,%r14d
shl    $0x4,%ebp
mov    0x2e04(%rip),%r15
mov    (%r15),%edi
lea    0xe57(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2e2c(%rip),%rax
mov    0x2ddd(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e0a(%rip),%rax
mov    0x2ddb(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%esi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2da6(%rip),%rax
cmp    (%rax),%rbx
je     12ca <main+0x15a>
mov    (%r15),%edi
lea    0xe1a(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
cmpl   $0xdead0000,(%rbx)
movslq %ebp,%rbp
jne    126f <main+0xff>
cmpl   $0xbeef0000,0x4(%rbx)
jne    126f <main+0xff>
mov    $0xbeeefffe,%eax
mov    %rbp,%rcx
shr    $0x2,%rcx
mov    -0x8(%rbp,%rbx,1),%edx
lea    (%rcx,%rax,1),%rsi
add    $0x1fbe0000,%rsi
cmp    %rdx,%rsi
jne    126f <main+0xff>
mov    -0x4(%rbx,%rcx,4),%edx
add    %rcx,%rax
cmp    %rdx,%rax
jne    126f <main+0xff>
mov    (%r15),%edi
lea    0xe02(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    12ae <main+0x13e>
mov    (%r15),%edi
lea    0xdcb(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%r15),%edi
mov    (%rbx),%edx
mov    0x4(%rbx),%ecx
lea    0xdce(%rip),%r12
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%r15),%edi
mov    -0x8(%rbp,%rbx,1),%edx
mov    -0x4(%rbp,%rbx,1),%ecx
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r14d,%edi
call   1040 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd48(%rip),%rdi
jmp    12d1 <main+0x161>
lea    0xd59(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"/dev/mmap_alloc\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [5 x i8] c"open\00"
@stderr = global i32 0
@.str.2 = [21 x i8] c"mmap_alloc: open OK\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_LOCKED = global i32 0
@MAP_FAILED = global i32* null
@.str.3 = [5 x i8] c"mmap\00"
@.str.4 = [21 x i8] c"mmap_alloc: mmap OK\0A\00"
@.str.5 = [25 x i8] c"mmap_alloc: check ERROR\0A\00"
@.str.6 = [11 x i8] c"0x%x 0x%x\0A\00"
@.str.7 = [22 x i8] c"mmap_alloc: check OK\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 (...) @getpagesize()
%6 = mul nsw i32 16, %5
store i32 %6, i32* %4
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_SYNC
%9 = or i32 %7, %8
%10 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %2
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @exit(i32 -1)
unreachable
15: ; preds = %0
%16 = load i32, i32* @stderr
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%18 = load i32, i32* %4
%19 = load i32, i32* @PROT_READ
%20 = load i32, i32* @PROT_WRITE
%21 = or i32 %19, %20
%22 = load i32, i32* @MAP_SHARED
%23 = load i32, i32* @MAP_LOCKED
%24 = or i32 %22, %23
%25 = load i32, i32* %2
%26 = call i32* @mmap(i32 0, i32 %18, i32 %21, i32 %24, i32 %25, i32 0)
store i32* %26, i32** %3
%27 = load i32*, i32** %3
%28 = load i32*, i32** @MAP_FAILED
%29 = icmp eq i32* %27, %28
br i1 %29, label %30, label %33
30: ; preds = %15
%31 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 @exit(i32 -1)
unreachable
33: ; preds = %15
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%36 = load i32*, i32** %3
%37 = getelementptr inbounds i32, i32* %36, i64 0
%38 = load i32, i32* %37
%39 = icmp ne i32 %38, -559087616
br i1 %39, label %75, label %40
40: ; preds = %33
%41 = load i32*, i32** %3
%42 = getelementptr inbounds i32, i32* %41, i64 1
%43 = load i32, i32* %42
%44 = icmp ne i32 %43, -1091633152
br i1 %44, label %75, label %45
45: ; preds = %40
%46 = load i32*, i32** %3
%47 = load i32, i32* %4
%48 = sext i32 %47 to i64
%49 = udiv i64 %48, 4
%50 = sub i64 %49, 2
%51 = getelementptr inbounds i32, i32* %46, i64 %50
%52 = load i32, i32* %51
%53 = zext i32 %52 to i64
%54 = load i32, i32* %4
%55 = sext i32 %54 to i64
%56 = udiv i64 %55, 4
%57 = add i64 3735879680, %56
%58 = sub i64 %57, 2
%59 = icmp ne i64 %53, %58
br i1 %59, label %75, label %60
60: ; preds = %45
%61 = load i32*, i32** %3
%62 = load i32, i32* %4
%63 = sext i32 %62 to i64
%64 = udiv i64 %63, 4
%65 = sub i64 %64, 1
%66 = getelementptr inbounds i32, i32* %61, i64 %65
%67 = load i32, i32* %66
%68 = zext i32 %67 to i64
%69 = load i32, i32* %4
%70 = sext i32 %69 to i64
%71 = udiv i64 %70, 4
%72 = add i64 3203334144, %71
%73 = sub i64 %72, 2
%74 = icmp ne i64 %68, %73
br i1 %74, label %75, label %102
75: ; preds = %60, %45, %40, %33
%76 = load i32, i32* @stderr
%77 = call i32 (i32, i8*, ...) @fprintf(i32 %76, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
%78 = load i32, i32* @stderr
%79 = load i32*, i32** %3
%80 = getelementptr inbounds i32, i32* %79, i64 0
%81 = load i32, i32* %80
%82 = load i32*, i32** %3
%83 = getelementptr inbounds i32, i32* %82, i64 1
%84 = load i32, i32* %83
%85 = call i32 (i32, i8*, ...) @fprintf(i32 %78, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i32 %81, i32 %84)
%86 = load i32, i32* @stderr
%87 = load i32*, i32** %3
%88 = load i32, i32* %4
%89 = sext i32 %88 to i64
%90 = udiv i64 %89, 4
%91 = sub i64 %90, 2
%92 = getelementptr inbounds i32, i32* %87, i64 %91
%93 = load i32, i32* %92
%94 = load i32*, i32** %3
%95 = load i32, i32* %4
%96 = sext i32 %95 to i64
%97 = udiv i64 %96, 4
%98 = sub i64 %97, 1
%99 = getelementptr inbounds i32, i32* %94, i64 %98
%100 = load i32, i32* %99
%101 = call i32 (i32, i8*, ...) @fprintf(i32 %86, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i32 %93, i32 %100)
br label %105
102: ; preds = %60
%103 = load i32, i32* @stderr
%104 = call i32 (i32, i8*, ...) @fprintf(i32 %103, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0))
br label %105
105: ; preds = %102, %75
%106 = load i32, i32* %2
%107 = call i32 @close(i32 %106)
ret i32 0
}

LLVM IR (-O2):
@.str = [16 x i8] c"/dev/mmap_alloc\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [5 x i8] c"open\00"
@stderr = global i32 0
@.str.2 = [21 x i8] c"mmap_alloc: open OK\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_LOCKED = global i32 0
@MAP_FAILED = global i32* null
@.str.3 = [5 x i8] c"mmap\00"
@.str.4 = [21 x i8] c"mmap_alloc: mmap OK\0A\00"
@.str.5 = [25 x i8] c"mmap_alloc: check ERROR\0A\00"
@.str.6 = [11 x i8] c"0x%x 0x%x\0A\00"
@.str.7 = [22 x i8] c"mmap_alloc: check OK\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @getpagesize()
%2 = shl nsw i32 %1, 4
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_SYNC
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i32 @exit(i32 -1)
unreachable
11: ; preds = %0
%12 = load i32, i32* @stderr
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @PROT_WRITE
%16 = or i32 %15, %14
%17 = load i32, i32* @MAP_SHARED
%18 = load i32, i32* @MAP_LOCKED
%19 = or i32 %18, %17
%20 = tail call i32* @mmap(i32 0, i32 %2, i32 %16, i32 %19, i32 %6, i32 0)
%21 = load i32*, i32** @MAP_FAILED
%22 = icmp eq i32* %20, %21
br i1 %22, label %23, label %26
23: ; preds = %11
%24 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%25 = tail call i32 @exit(i32 -1)
unreachable
26: ; preds = %11
%27 = load i32, i32* @stderr
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%29 = load i32, i32* %20
%30 = icmp eq i32 %29, -559087616
br i1 %30, label %31, label %51
31: ; preds = %26
%32 = getelementptr inbounds i32, i32* %20, i64 1
%33 = load i32, i32* %32
%34 = icmp eq i32 %33, -1091633152
br i1 %34, label %35, label %51
35: ; preds = %31
%36 = sext i32 %2 to i64
%37 = lshr exact i64 %36, 2
%38 = add nsw i64 %37, -2
%39 = getelementptr inbounds i32, i32* %20, i64 %38
%40 = load i32, i32* %39
%41 = zext i32 %40 to i64
%42 = add nuw nsw i64 %37, 3735879678
%43 = icmp eq i64 %42, %41
br i1 %43, label %44, label %51
44: ; preds = %35
%45 = add nsw i64 %37, -1
%46 = getelementptr inbounds i32, i32* %20, i64 %45
%47 = load i32, i32* %46
%48 = zext i32 %47 to i64
%49 = add nuw nsw i64 %37, 3203334142
%50 = icmp eq i64 %49, %48
br i1 %50, label %69, label %51
51: ; preds = %44, %35, %31, %26
%52 = load i32, i32* @stderr
%53 = tail call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
%54 = load i32, i32* @stderr
%55 = load i32, i32* %20
%56 = getelementptr inbounds i32, i32* %20, i64 1
%57 = load i32, i32* %56
%58 = tail call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i32 %55, i32 %57)
%59 = load i32, i32* @stderr
%60 = sext i32 %2 to i64
%61 = lshr exact i64 %60, 2
%62 = add nsw i64 %61, -2
%63 = getelementptr inbounds i32, i32* %20, i64 %62
%64 = load i32, i32* %63
%65 = add nsw i64 %61, -1
%66 = getelementptr inbounds i32, i32* %20, i64 %65
%67 = load i32, i32* %66
%68 = tail call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i32 %64, i32 %67)
br label %72
69: ; preds = %44
%70 = load i32, i32* @stderr
%71 = tail call i32 (i32, i8*, ...) @fprintf(i32 %70, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0))
br label %72
72: ; preds = %69, %51
%73 = tail call i32 @close(i32 %6)
ret i32 0
}