#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 _rdpkru(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ sigsafe_printf(char *, unsigned int);
int /*<<< orphan*/ wrpkru(unsigned int);
void __pkey_access_allow(int pkey, int do_allow) {
  unsigned int pkru = _rdpkru(120);
  int bit = pkey * 2;
  if (do_allow) {
    pkru &= (1 << bit);
  } else {
    pkru |= (1 << bit);
  }
  do {
    if (4 <= 0)
      sigsafe_printf("pkru now: %08x\n", _rdpkru(132));
    fflush(NULL);
  } while (0);
  wrpkru(pkru);
}


x86 assembly (clang, -O0):
<__pkey_access_allow>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    $0x78,%edi
call   1050 <_rdpkru@plt>
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1173 <__pkey_access_allow+0x43>
mov    -0x10(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
and    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    1183 <__pkey_access_allow+0x53>
mov    -0x10(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    1188 <__pkey_access_allow+0x58>
xor    %eax,%eax
mov    %eax,%edi
call   1030 <fflush@plt>
mov    -0xc(%rbp),%edi
call   1040 <wrpkru@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__pkey_access_allow>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %edi,%ebp
mov    $0x78,%edi
call   1050 <_rdpkru@plt>
add    %ebp,%ebp
mov    $0x1,%ebx
mov    %ebp,%ecx
shl    %cl,%ebx
and    %eax,%ebx
bts    %ebp,%eax
test   %r14d,%r14d
cmove  %eax,%ebx
xor    %edi,%edi
call   1030 <fflush@plt>
mov    %ebx,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <wrpkru@plt>

LLVM IR (-O0):
define void @__pkey_access_allow(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = call i32 @_rdpkru(i32 120)
store i32 %7, i32* %5
%8 = load i32, i32* %3
%9 = mul nsw i32 %8, 2
store i32 %9, i32* %6
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i32, i32* %6
%14 = shl i32 1, %13
%15 = load i32, i32* %5
%16 = and i32 %15, %14
store i32 %16, i32* %5
br label %22
17: ; preds = %2
%18 = load i32, i32* %6
%19 = shl i32 1, %18
%20 = load i32, i32* %5
%21 = or i32 %20, %19
store i32 %21, i32* %5
br label %22
22: ; preds = %17, %12
br label %23
23: ; preds = %22
%24 = call i32 @fflush(i32* null)
br label %25
25: ; preds = %23
%26 = load i32, i32* %5
%27 = call i32 @wrpkru(i32 %26)
ret void
}

LLVM IR (-O2):
define void @__pkey_access_allow(i32 %0, i32 %1) {
%3 = tail call i32 @_rdpkru(i32 120)
%4 = shl nsw i32 %0, 1
%5 = icmp eq i32 %1, 0
%6 = shl nuw i32 1, %4
%7 = and i32 %3, %6
%8 = or i32 %3, %6
%9 = select i1 %5, i32 %8, i32 %7
%10 = tail call i32 @fflush(i32* null)
%11 = tail call i32 @wrpkru(i32 %9)
ret void
}