#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();
scalar_t__ is_vmflag_set(unsigned long, char *);
int /*<<< orphan*/ printf(char *, ...);
int unlock_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);
  page1_flags = get_kpageflags(page1_flags & 0x007FFFFFFFFFFFFFULL);
  page2_flags = get_kpageflags(page2_flags & 0x007FFFFFFFFFFFFFULL);
  if ((page1_flags & (1UL << 18)) || (page2_flags & (1UL << 18))) {
    printf("A page is still marked unevictable after unlock\n");
    return 1;
  }
  if (is_vmflag_set((unsigned long)map, "lo")) {
    printf("VMA flag %s is present on page 1 after unlock\n", "lo");
    return 1;
  }
  if (is_vmflag_set((unsigned long)map + page_size, "lo")) {
    printf("VMA flag %s is present on page 2 after unlock\n", "lo");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<unlock_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 $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
jne    11e1 <unlock_lock_check+0x91>
movslq -0x20(%rbp),%rax
and    $0x40000,%rax
cmp    $0x0,%rax
je     11fb <unlock_lock_check+0xab>
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    127c <unlock_lock_check+0x12c>
mov    -0x10(%rbp),%rdi
lea    0xe2b(%rip),%rsi
call   1030 <is_vmflag_set@plt>
cmp    $0x0,%rax
je     1236 <unlock_lock_check+0xe6>
lea    0xe18(%rip),%rdi
lea    0xe0e(%rip),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    127c <unlock_lock_check+0x12c>
mov    -0x10(%rbp),%rdi
add    -0x18(%rbp),%rdi
lea    0xdec(%rip),%rsi
call   1030 <is_vmflag_set@plt>
cmp    $0x0,%rax
je     1275 <unlock_lock_check+0x125>
lea    0xe08(%rip),%rdi
lea    0xdcf(%rip),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    127c <unlock_lock_check+0x12c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unlock_lock_check>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
xor    %eax,%eax
call   1080 <getpagesize@plt>
mov    %rax,%r12
mov    %r14,%rdi
call   1060 <get_pageflags@plt>
mov    %eax,%r15d
add    %r14,%r12
mov    %r12,%rdi
call   1060 <get_pageflags@plt>
mov    %eax,%ebp
mov    %r15d,%edi
call   1070 <get_kpageflags@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1070 <get_kpageflags@plt>
or     %ebx,%eax
test   $0x40000,%eax
jne    11c4 <unlock_lock_check+0x64>
lea    0xe52(%rip),%rsi
mov    %r14,%rdi
call   1040 <is_vmflag_set@plt>
test   %rax,%rax
je     11d2 <unlock_lock_check+0x72>
lea    0xe41(%rip),%rdi
jmp    11ed <unlock_lock_check+0x8d>
lea    0xe96(%rip),%rdi
call   1030 <puts@plt>
jmp    11fb <unlock_lock_check+0x9b>
lea    0xe27(%rip),%rsi
mov    %r12,%rdi
call   1040 <is_vmflag_set@plt>
test   %rax,%rax
je     1209 <unlock_lock_check+0xa9>
lea    0xe45(%rip),%rdi
lea    0xe0c(%rip),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
jmp    1200 <unlock_lock_check+0xa0>

LLVM IR (-O0):
@.str = [49 x i8] c"A page is still marked unevictable after unlock\0A\00"
@.str.1 = [3 x i8] c"lo\00"
@.str.2 = [47 x i8] c"VMA flag %s is present on page 1 after unlock\0A\00"
@.str.3 = [47 x i8] c"VMA flag %s is present on page 2 after unlock\0A\00"
define i32 @unlock_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, 36028797018963967
%19 = trunc i64 %18 to i32
%20 = call i32 @get_kpageflags(i32 %19)
store i32 %20, i32* %5
%21 = load i32, i32* %6
%22 = sext i32 %21 to i64
%23 = and i64 %22, 36028797018963967
%24 = trunc i64 %23 to i32
%25 = call i32 @get_kpageflags(i32 %24)
store i32 %25, i32* %6
%26 = load i32, i32* %5
%27 = sext i32 %26 to i64
%28 = and i64 %27, 262144
%29 = icmp ne i64 %28, 0
br i1 %29, label %35, label %30
30: ; preds = %1
%31 = load i32, i32* %6
%32 = sext i32 %31 to i64
%33 = and i64 %32, 262144
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %30, %1
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %2
br label %54
37: ; preds = %30
%38 = load i8*, i8** %3
%39 = ptrtoint i8* %38 to i64
%40 = call i64 @is_vmflag_set(i64 %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %37
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %2
br label %54
44: ; preds = %37
%45 = load i8*, i8** %3
%46 = ptrtoint i8* %45 to i64
%47 = load i64, i64* %4
%48 = add i64 %46, %47
%49 = call i64 @is_vmflag_set(i64 %48, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %44
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %2
br label %54
53: ; preds = %44
store i32 0, i32* %2
br label %54
54: ; preds = %53, %51, %42, %35
%55 = load i32, i32* %2
ret i32 %55
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"lo\00"
@.str.2 = [47 x i8] c"VMA flag %s is present on page 1 after unlock\0A\00"
@.str.3 = [47 x i8] c"VMA flag %s is present on page 2 after unlock\0A\00"
@str = [48 x i8] c"A page is still marked unevictable after unlock\00"
define i32 @unlock_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 = tail call i32 @get_kpageflags(i32 %4)
%8 = tail call i32 @get_kpageflags(i32 %6)
%9 = and i32 %7, 262144
%10 = icmp eq i32 %9, 0
%11 = and i32 %8, 262144
%12 = icmp eq i32 %11, 0
%13 = select i1 %10, i1 %12, i1 false
br i1 %13, label %16, label %14
14: ; preds = %1
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str, i64 0, i64 0))
br label %26
16: ; preds = %1
%17 = tail call i64 @is_vmflag_set(i64 %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %26
21: ; preds = %16
%22 = tail call i64 @is_vmflag_set(i64 %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %21
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %21, %24, %19, %14
%27 = phi i32 [ 1, %14 ], [ 1, %19 ], [ 1, %24 ], [ 0, %21 ]
ret i32 %27
}