#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;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ exit(int);
int getpagesize();
int mincore(void *, int, char *);
int mlock(void *, int);
void *mmap(int /*<<< orphan*/, int, int, int, int, int /*<<< orphan*/);
int munlock(void *, int);
int /*<<< orphan*/ munmap(void *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char);
int main(void) {
  char vec = 0;
  void *a;
  a = mmap(0, getpagesize() * 8, PROT_READ | PROT_WRITE,
           MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  if (a < 0) {
    perror("mmap failed ");
    exit(-1);
  }
  int ret = mlock(a, getpagesize() * 2);
  if (ret) {
    perror("lock failed ");
    munmap(a, getpagesize() * 2);
    exit(-1);
  }
  printf("vec0 %x\n", vec);
  ret = mincore(a, getpagesize() * 2, &vec);
  if (ret) {
    perror("test failed ");
    munmap(a, getpagesize() * 2);
    exit(-2);
  }
  printf("vec1 %x\n", vec);
  ret = munlock(a, getpagesize() * 2);
  if (ret) {
    perror("unlock failed ");
    munmap(a, getpagesize() * 2);
    exit(-3);
  }
  ret = mincore(a, getpagesize() * 2, &vec);
  if (ret) {
    perror("test2 failed ");
    munmap(a, getpagesize() * 2);
    exit(-2);
  }
  printf("vec2 %x\n", vec);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
movb   $0x0,-0x5(%rbp)
mov    $0x0,%al
call   10a0 <getpagesize@plt>
mov    %eax,%esi
shl    $0x3,%esi
mov    0x2e42(%rip),%rax
mov    (%rax),%edx
mov    0x2e11(%rip),%rax
or     (%rax),%edx
mov    0x2e18(%rip),%rax
mov    (%rax),%ecx
mov    0x2df7(%rip),%rax
or     (%rax),%ecx
xor    %r9d,%r9d
mov    $0xffffffff,%r8d
mov    %r9d,%edi
call   1050 <mmap@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jae    1209 <main+0x79>
lea    0xe06(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   10a0 <getpagesize@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%esi
shl    $0x1,%esi
call   1040 <mlock@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1266 <main+0xd6>
lea    0xdf4(%rip),%rdi
call   1090 <perror@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   10a0 <getpagesize@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%esi
shl    $0x1,%esi
call   1080 <munmap@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
lea    0xda0(%rip),%rdi
movsbl -0x5(%rbp),%esi
call   1060 <printf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   10a0 <getpagesize@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%esi
shl    $0x1,%esi
lea    -0x5(%rbp),%rdx
call   1070 <mincore@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     12d7 <main+0x147>
lea    0xd6b(%rip),%rdi
call   1090 <perror@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   10a0 <getpagesize@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%esi
shl    $0x1,%esi
call   1080 <munmap@plt>
mov    $0xfffffffe,%edi
call   10b0 <exit@plt>
lea    0xd45(%rip),%rdi
movsbl -0x5(%rbp),%esi
call   1060 <printf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   10a0 <getpagesize@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%esi
shl    $0x1,%esi
call   1030 <munlock@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1344 <main+0x1b4>
lea    0xd14(%rip),%rdi
call   1090 <perror@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   10a0 <getpagesize@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%esi
shl    $0x1,%esi
call   1080 <munmap@plt>
mov    $0xfffffffd,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    $0x0,%al
call   10a0 <getpagesize@plt>
mov    -0x50(%rbp),%rdi
mov    %eax,%esi
shl    $0x1,%esi
lea    -0x5(%rbp),%rdx
call   1070 <mincore@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     13a5 <main+0x215>
lea    0xcc2(%rip),%rdi
call   1090 <perror@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    $0x0,%al
call   10a0 <getpagesize@plt>
mov    -0x58(%rbp),%rdi
mov    %eax,%esi
shl    $0x1,%esi
call   1080 <munmap@plt>
mov    $0xfffffffe,%edi
call   10b0 <exit@plt>
lea    0xc9d(%rip),%rdi
movsbl -0x5(%rbp),%esi
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
movb   $0x0,0xf(%rsp)
xor    %eax,%eax
call   10a0 <getpagesize@plt>
lea    0x0(,%rax,8),%esi
mov    0x2e49(%rip),%rax
mov    0x2e1a(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e1f(%rip),%rax
mov    0x2e00(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   10a0 <getpagesize@plt>
lea    (%rax,%rax,1),%esi
mov    %rbx,%rdi
call   1040 <mlock@plt>
test   %eax,%eax
jne    1281 <main+0xf1>
lea    0xe00(%rip),%rdi
xor    %esi,%esi
call   1060 <printf@plt>
xor    %eax,%eax
call   10a0 <getpagesize@plt>
lea    (%rax,%rax,1),%esi
lea    0xf(%rsp),%rdx
mov    %rbx,%rdi
call   1070 <mincore@plt>
test   %eax,%eax
jne    12a9 <main+0x119>
movsbl 0xf(%rsp),%esi
lea    0xde4(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
call   10a0 <getpagesize@plt>
lea    (%rax,%rax,1),%esi
mov    %rbx,%rdi
call   1030 <munlock@plt>
test   %eax,%eax
jne    12b2 <main+0x122>
xor    %eax,%eax
call   10a0 <getpagesize@plt>
lea    (%rax,%rax,1),%esi
lea    0xf(%rsp),%rdx
mov    %rbx,%rdi
call   1070 <mincore@plt>
test   %eax,%eax
jne    12da <main+0x14a>
movsbl 0xf(%rsp),%esi
lea    0xdc8(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xd99(%rip),%rdi
call   1090 <perror@plt>
xor    %eax,%eax
call   10a0 <getpagesize@plt>
lea    (%rax,%rax,1),%esi
mov    %rbx,%rdi
call   1080 <munmap@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
lea    0xd59(%rip),%rdi
jmp    12e1 <main+0x151>
lea    0xd66(%rip),%rdi
call   1090 <perror@plt>
xor    %eax,%eax
call   10a0 <getpagesize@plt>
lea    (%rax,%rax,1),%esi
mov    %rbx,%rdi
call   1080 <munmap@plt>
mov    $0xfffffffd,%edi
call   10b0 <exit@plt>
lea    0xd4d(%rip),%rdi
call   1090 <perror@plt>
xor    %eax,%eax
call   10a0 <getpagesize@plt>
lea    (%rax,%rax,1),%esi
mov    %rbx,%rdi
call   1080 <munmap@plt>
mov    $0xfffffffe,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@.str = [13 x i8] c"mmap failed \00"
@.str.1 = [13 x i8] c"lock failed \00"
@.str.2 = [9 x i8] c"vec0 %x\0A\00"
@.str.3 = [13 x i8] c"test failed \00"
@.str.4 = [9 x i8] c"vec1 %x\0A\00"
@.str.5 = [15 x i8] c"unlock failed \00"
@.str.6 = [14 x i8] c"test2 failed \00"
@.str.7 = [9 x i8] c"vec2 %x\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8
%3 = alloca i8*
%4 = alloca i32
store i32 0, i32* %1
store i8 0, i8* %2
%5 = call i32 (...) @getpagesize()
%6 = mul nsw i32 %5, 8
%7 = load i32, i32* @PROT_READ
%8 = load i32, i32* @PROT_WRITE
%9 = or i32 %7, %8
%10 = load i32, i32* @MAP_ANONYMOUS
%11 = load i32, i32* @MAP_PRIVATE
%12 = or i32 %10, %11
%13 = call i8* @mmap(i32 0, i32 %6, i32 %9, i32 %12, i32 -1, i32 0)
store i8* %13, i8** %3
%14 = load i8*, i8** %3
%15 = icmp ult i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %0
%17 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 -1)
unreachable
19: ; preds = %0
%20 = load i8*, i8** %3
%21 = call i32 (...) @getpagesize()
%22 = mul nsw i32 %21, 2
%23 = call i32 @mlock(i8* %20, i32 %22)
store i32 %23, i32* %4
%24 = load i32, i32* %4
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %19
%27 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%28 = load i8*, i8** %3
%29 = call i32 (...) @getpagesize()
%30 = mul nsw i32 %29, 2
%31 = call i32 @munmap(i8* %28, i32 %30)
%32 = call i32 @exit(i32 -1)
unreachable
33: ; preds = %19
%34 = load i8, i8* %2
%35 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8 signext %34)
%36 = load i8*, i8** %3
%37 = call i32 (...) @getpagesize()
%38 = mul nsw i32 %37, 2
%39 = call i32 @mincore(i8* %36, i32 %38, i8* %2)
store i32 %39, i32* %4
%40 = load i32, i32* %4
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %49
42: ; preds = %33
%43 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%44 = load i8*, i8** %3
%45 = call i32 (...) @getpagesize()
%46 = mul nsw i32 %45, 2
%47 = call i32 @munmap(i8* %44, i32 %46)
%48 = call i32 @exit(i32 -2)
unreachable
49: ; preds = %33
%50 = load i8, i8* %2
%51 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8 signext %50)
%52 = load i8*, i8** %3
%53 = call i32 (...) @getpagesize()
%54 = mul nsw i32 %53, 2
%55 = call i32 @munlock(i8* %52, i32 %54)
store i32 %55, i32* %4
%56 = load i32, i32* %4
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %65
58: ; preds = %49
%59 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%60 = load i8*, i8** %3
%61 = call i32 (...) @getpagesize()
%62 = mul nsw i32 %61, 2
%63 = call i32 @munmap(i8* %60, i32 %62)
%64 = call i32 @exit(i32 -3)
unreachable
65: ; preds = %49
%66 = load i8*, i8** %3
%67 = call i32 (...) @getpagesize()
%68 = mul nsw i32 %67, 2
%69 = call i32 @mincore(i8* %66, i32 %68, i8* %2)
store i32 %69, i32* %4
%70 = load i32, i32* %4
%71 = icmp ne i32 %70, 0
br i1 %71, label %72, label %79
72: ; preds = %65
%73 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
%74 = load i8*, i8** %3
%75 = call i32 (...) @getpagesize()
%76 = mul nsw i32 %75, 2
%77 = call i32 @munmap(i8* %74, i32 %76)
%78 = call i32 @exit(i32 -2)
unreachable
79: ; preds = %65
%80 = load i8, i8* %2
%81 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i8 signext %80)
ret i32 0
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@.str.1 = [13 x i8] c"lock failed \00"
@.str.2 = [9 x i8] c"vec0 %x\0A\00"
@.str.3 = [13 x i8] c"test failed \00"
@.str.4 = [9 x i8] c"vec1 %x\0A\00"
@.str.5 = [15 x i8] c"unlock failed \00"
@.str.6 = [14 x i8] c"test2 failed \00"
@.str.7 = [9 x i8] c"vec2 %x\0A\00"
define i32 @main() {
%1 = alloca i8
store i8 0, i8* %1
%2 = tail call i32 (...) @getpagesize()
%3 = shl nsw i32 %2, 3
%4 = load i32, i32* @PROT_READ
%5 = load i32, i32* @PROT_WRITE
%6 = or i32 %5, %4
%7 = load i32, i32* @MAP_ANONYMOUS
%8 = load i32, i32* @MAP_PRIVATE
%9 = or i32 %8, %7
%10 = tail call i8* @mmap(i32 0, i32 %3, i32 %6, i32 %9, i32 -1, i32 0)
%11 = tail call i32 (...) @getpagesize()
%12 = shl nsw i32 %11, 1
%13 = tail call i32 @mlock(i8* %10, i32 %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %21, label %15
15: ; preds = %0
%16 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i32 (...) @getpagesize()
%18 = shl nsw i32 %17, 1
%19 = tail call i32 @munmap(i8* %10, i32 %18)
%20 = tail call i32 @exit(i32 -1)
unreachable
21: ; preds = %0
%22 = tail call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8 signext 0)
%23 = tail call i32 (...) @getpagesize()
%24 = shl nsw i32 %23, 1
%25 = call i32 @mincore(i8* %10, i32 %24, i8* %1)
%26 = icmp eq i32 %25, 0
br i1 %26, label %33, label %27
27: ; preds = %21
%28 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%29 = call i32 (...) @getpagesize()
%30 = shl nsw i32 %29, 1
%31 = call i32 @munmap(i8* %10, i32 %30)
%32 = call i32 @exit(i32 -2)
unreachable
33: ; preds = %21
%34 = load i8, i8* %1
%35 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8 signext %34)
%36 = call i32 (...) @getpagesize()
%37 = shl nsw i32 %36, 1
%38 = call i32 @munlock(i8* %10, i32 %37)
%39 = icmp eq i32 %38, 0
br i1 %39, label %46, label %40
40: ; preds = %33
%41 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%42 = call i32 (...) @getpagesize()
%43 = shl nsw i32 %42, 1
%44 = call i32 @munmap(i8* %10, i32 %43)
%45 = call i32 @exit(i32 -3)
unreachable
46: ; preds = %33
%47 = call i32 (...) @getpagesize()
%48 = shl nsw i32 %47, 1
%49 = call i32 @mincore(i8* %10, i32 %48, i8* %1)
%50 = icmp eq i32 %49, 0
br i1 %50, label %57, label %51
51: ; preds = %46
%52 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
%53 = call i32 (...) @getpagesize()
%54 = shl nsw i32 %53, 1
%55 = call i32 @munmap(i8* %10, i32 %54)
%56 = call i32 @exit(i32 -2)
unreachable
57: ; preds = %46
%58 = load i8, i8* %1
%59 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i8 signext %58)
ret i32 0
}