#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 uint64_t;

/* Variables and functions */
int get_kpageflags(int);
int get_pageflags(unsigned long);
unsigned long getpagesize();
int /*<<< orphan*/ is_vmflag_set(unsigned long, char *);
int /*<<< orphan*/ printf(char *, ...);
int lock_check(char *map) {
  unsigned long page_size = getpagesize();
  uint64_t page1_flags, page2_flags;
  page1_flags = get_pageflags((unsigned long)map);
  page2_flags = get_pageflags((unsigned long)map + page_size);
  if (((page1_flags & 0x8000000000000000ULL) == 0) ||
      ((page2_flags & 0x8000000000000000ULL) == 0)) {
    printf("Failed to make both pages present\n");
    return 1;
  }
  page1_flags = get_kpageflags(page1_flags & 0x007FFFFFFFFFFFFFULL);
  page2_flags = get_kpageflags(page2_flags & 0x007FFFFFFFFFFFFFULL);
  if (((page1_flags & (1UL << 18)) == 0) ||
      ((page2_flags & (1UL << 18)) == 0)) {
    printf("Failed to make both pages unevictable\n");
    return 1;
  }
  if (!is_vmflag_set((unsigned long)map, "lo")) {
    printf("VMA flag %s is missing on page 1\n", "lo");
    return 1;
  }
  if (!is_vmflag_set((unsigned long)map + page_size, "lo")) {
    printf("VMA flag %s is missing on page 2\n", "lo");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<lock_check>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1070 <getpagesize@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <get_pageflags@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
add    -0x18(%rbp),%rdi
call   1050 <get_pageflags@plt>
mov    %eax,-0x20(%rbp)
movslq -0x1c(%rbp),%rax
movabs $0x8000000000000000,%rcx
and    %rcx,%rax
cmp    $0x0,%rax
je     11b9 <lock_check+0x69>
movslq -0x20(%rbp),%rax
movabs $0x8000000000000000,%rcx
and    %rcx,%rax
cmp    $0x0,%rax
jne    11d3 <lock_check+0x83>
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ca <lock_check+0x17a>
movslq -0x1c(%rbp),%rax
movabs $0x7fffffffffffff,%rcx
and    %rcx,%rax
mov    %eax,%edi
call   1060 <get_kpageflags@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x20(%rbp),%rax
movabs $0x7fffffffffffff,%rcx
and    %rcx,%rax
mov    %eax,%edi
call   1060 <get_kpageflags@plt>
mov    %eax,-0x20(%rbp)
movslq -0x1c(%rbp),%rax
and    $0x40000,%rax
cmp    $0x0,%rax
je     1231 <lock_check+0xe1>
movslq -0x20(%rbp),%rax
and    $0x40000,%rax
cmp    $0x0,%rax
jne    124b <lock_check+0xfb>
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ca <lock_check+0x17a>
mov    -0x10(%rbp),%rdi
lea    0xdf4(%rip),%rsi
call   1030 <is_vmflag_set@plt>
cmp    $0x0,%eax
jne    1285 <lock_check+0x135>
lea    0xde2(%rip),%rdi
lea    0xdd8(%rip),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ca <lock_check+0x17a>
mov    -0x10(%rbp),%rdi
add    -0x18(%rbp),%rdi
lea    0xdb6(%rip),%rsi
call   1030 <is_vmflag_set@plt>
cmp    $0x0,%eax
jne    12c3 <lock_check+0x173>
lea    0xdc6(%rip),%rdi
lea    0xd9a(%rip),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ca <lock_check+0x17a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_check>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %eax,%eax
call   1080 <getpagesize@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1060 <get_pageflags@plt>
mov    %eax,%ebp
add    %r14,%r15
mov    %r15,%rdi
call   1060 <get_pageflags@plt>
test   %ebp,%ebp
jns    11e5 <lock_check+0x85>
mov    %eax,%ebx
test   %eax,%eax
jns    11e5 <lock_check+0x85>
mov    %ebp,%edi
call   1070 <get_kpageflags@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1070 <get_kpageflags@plt>
test   $0x40000,%ebp
je     11f3 <lock_check+0x93>
and    $0x40000,%eax
je     11f3 <lock_check+0x93>
lea    0xe47(%rip),%rsi
mov    %r14,%rdi
call   1040 <is_vmflag_set@plt>
test   %eax,%eax
je     1201 <lock_check+0xa1>
lea    0xe34(%rip),%rsi
mov    %r15,%rdi
call   1040 <is_vmflag_set@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jne    121b <lock_check+0xbb>
lea    0xe42(%rip),%rdi
jmp    1208 <lock_check+0xa8>
lea    0xe81(%rip),%rdi
call   1030 <puts@plt>
jmp    1216 <lock_check+0xb6>
lea    0xe4d(%rip),%rdi
call   1030 <puts@plt>
jmp    1216 <lock_check+0xb6>
lea    0xdfb(%rip),%rdi
lea    0xdf1(%rip),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [35 x i8] c"Failed to make both pages present\0A\00"
@.str.1 = [39 x i8] c"Failed to make both pages unevictable\0A\00"
@.str.2 = [3 x i8] c"lo\00"
@.str.3 = [34 x i8] c"VMA flag %s is missing on page 1\0A\00"
@.str.4 = [34 x i8] c"VMA flag %s is missing on page 2\0A\00"
define i32 @lock_check(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = call i64 (...) @getpagesize()
store i64 %7, i64* %4
%8 = load i8*, i8** %3
%9 = ptrtoint i8* %8 to i64
%10 = call i32 @get_pageflags(i64 %9)
store i32 %10, i32* %5
%11 = load i8*, i8** %3
%12 = ptrtoint i8* %11 to i64
%13 = load i64, i64* %4
%14 = add i64 %12, %13
%15 = call i32 @get_pageflags(i64 %14)
store i32 %15, i32* %6
%16 = load i32, i32* %5
%17 = sext i32 %16 to i64
%18 = and i64 %17, -9223372036854775808
%19 = icmp eq i64 %18, 0
br i1 %19, label %25, label %20
20: ; preds = %1
%21 = load i32, i32* %6
%22 = sext i32 %21 to i64
%23 = and i64 %22, -9223372036854775808
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %20, %1
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %2
br label %66
27: ; preds = %20
%28 = load i32, i32* %5
%29 = sext i32 %28 to i64
%30 = and i64 %29, 36028797018963967
%31 = trunc i64 %30 to i32
%32 = call i32 @get_kpageflags(i32 %31)
store i32 %32, i32* %5
%33 = load i32, i32* %6
%34 = sext i32 %33 to i64
%35 = and i64 %34, 36028797018963967
%36 = trunc i64 %35 to i32
%37 = call i32 @get_kpageflags(i32 %36)
store i32 %37, i32* %6
%38 = load i32, i32* %5
%39 = sext i32 %38 to i64
%40 = and i64 %39, 262144
%41 = icmp eq i64 %40, 0
br i1 %41, label %47, label %42
42: ; preds = %27
%43 = load i32, i32* %6
%44 = sext i32 %43 to i64
%45 = and i64 %44, 262144
%46 = icmp eq i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %42, %27
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %2
br label %66
49: ; preds = %42
%50 = load i8*, i8** %3
%51 = ptrtoint i8* %50 to i64
%52 = call i32 @is_vmflag_set(i64 %51, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%53 = icmp ne i32 %52, 0
br i1 %53, label %56, label %54
54: ; preds = %49
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %2
br label %66
56: ; preds = %49
%57 = load i8*, i8** %3
%58 = ptrtoint i8* %57 to i64
%59 = load i64, i64* %4
%60 = add i64 %58, %59
%61 = call i32 @is_vmflag_set(i64 %60, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%62 = icmp ne i32 %61, 0
br i1 %62, label %65, label %63
63: ; preds = %56
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %2
br label %66
65: ; preds = %56
store i32 0, i32* %2
br label %66
66: ; preds = %65, %63, %54, %47, %25
%67 = load i32, i32* %2
ret i32 %67
}

LLVM IR (-O2):
@.str.2 = [3 x i8] c"lo\00"
@.str.3 = [34 x i8] c"VMA flag %s is missing on page 1\0A\00"
@.str.4 = [34 x i8] c"VMA flag %s is missing on page 2\0A\00"
@str = [38 x i8] c"Failed to make both pages unevictable\00"
@str.5 = [34 x i8] c"Failed to make both pages present\00"
define i32 @lock_check(i8* %0) {
%2 = tail call i64 (...) @getpagesize()
%3 = ptrtoint i8* %0 to i64
%4 = tail call i32 @get_pageflags(i64 %3)
%5 = add i64 %2, %3
%6 = tail call i32 @get_pageflags(i64 %5)
%7 = icmp sgt i32 %4, -1
%8 = icmp sgt i32 %6, -1
%9 = select i1 %7, i1 true, i1 %8
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.5, i64 0, i64 0))
br label %32
12: ; preds = %1
%13 = tail call i32 @get_kpageflags(i32 %4)
%14 = tail call i32 @get_kpageflags(i32 %6)
%15 = and i32 %13, 262144
%16 = icmp eq i32 %15, 0
%17 = and i32 %14, 262144
%18 = icmp eq i32 %17, 0
%19 = select i1 %16, i1 true, i1 %18
br i1 %19, label %20, label %22
20: ; preds = %12
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
br label %32
22: ; preds = %12
%23 = tail call i32 @is_vmflag_set(i64 %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %32
27: ; preds = %22
%28 = tail call i32 @is_vmflag_set(i64 %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %27, %30, %25, %20, %10
%33 = phi i32 [ 1, %10 ], [ 1, %20 ], [ 1, %30 ], [ 1, %25 ], [ 0, %27 ]
ret i32 %33
}