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


x86 assembly (clang, -O0):
<__pkey_write_allow>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    $0x7d,%edi
call   1050 <_rdpkru@plt>
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
shl    $0x1,%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     117b <__pkey_write_allow+0x4b>
mov    -0x14(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
cltq
and    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    118f <__pkey_write_allow+0x5f>
mov    -0x14(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
cltq
or     -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <wrpkru@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1030 <fflush@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__pkey_write_allow>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
mov    $0x7d,%edi
call   1050 <_rdpkru@plt>
add    %ebp,%ebp
or     $0x1,%bpl
mov    $0x1,%edx
mov    %ebp,%ecx
shl    %cl,%edx
movslq %edx,%rdi
mov    %rax,%rcx
and    %rdi,%rcx
or     %rax,%rdi
test   %ebx,%ebx
cmovne %rcx,%rdi
call   1040 <wrpkru@plt>
xor    %edi,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <fflush@plt>

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

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