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

/* Variables and functions */
int /*<<< orphan*/ FUTEX_WAIT;
int /*<<< orphan*/ SYS_futex;
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pkey_write_deny(int);
int /*<<< orphan*/ sigsafe_printf(char *, int);
int syscall(int /*<<< orphan*/, int *, int /*<<< orphan*/, int,
            int /*<<< orphan*/ *, int *, int);
void test_kernel_gup_write_to_write_disabled_region(int *ptr, uint16_t pkey) {
  int ignored = 0xdada;
  int futex_ret;
  int some_int = 1218;
  do {
    if (1 <= 0)
      sigsafe_printf("disabling write to PKEY[%02d], "
                     "doing futex gunk in buffer\n",
                     pkey);
    fflush(NULL);
  } while (0);
  *ptr = some_int;
  pkey_write_deny(pkey);
  futex_ret = syscall(SYS_futex, ptr, FUTEX_WAIT, some_int - 1, NULL, &ignored,
                      ignored);
  if (0 > 0) {
    perror("futex");
  }
  do {
    if (1 <= 0)
      sigsafe_printf("futex() ret: %d\n", futex_ret);
    fflush(NULL);
  } while (0);
}


x86 assembly (clang, -O0):
<test_kernel_gup_write_to_write_disabled_region>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xdada,-0x10(%rbp)
movl   $0x4c2,-0x18(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
mov    -0x18(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0xc(%rbp),%edi
call   1030 <pkey_write_deny@plt>
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rsi
mov    0x2e6d(%rip),%rax
mov    (%rax),%edx
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
mov    -0x10(%rbp),%eax
xor    %r8d,%r8d
lea    -0x10(%rbp),%r9
mov    %eax,(%rsp)
call   1040 <syscall@plt>
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_kernel_gup_write_to_write_disabled_region>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
movl   $0xdada,0x14(%rsp)
xor    %edi,%edi
call   1050 <fflush@plt>
movl   $0x4c2,(%rbx)
mov    %ebp,%edi
call   1030 <pkey_write_deny@plt>
mov    0x2e7a(%rip),%rax
mov    (%rax),%edi
mov    0x2e81(%rip),%rax
mov    (%rax),%edx
movl   $0xdada,(%rsp)
lea    0x14(%rsp),%r9
mov    %rbx,%rsi
mov    $0x4c1,%ecx
xor    %r8d,%r8d
call   1040 <syscall@plt>
xor    %edi,%edi
call   1050 <fflush@plt>
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@SYS_futex = global i32 0
@FUTEX_WAIT = global i32 0
define void @test_kernel_gup_write_to_write_disabled_region(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
store i32 56026, i32* %5
store i32 1218, i32* %7
br label %8
8: ; preds = %2
%9 = call i32 @fflush(i32* null)
br label %10
10: ; preds = %8
%11 = load i32, i32* %7
%12 = load i32*, i32** %3
store i32 %11, i32* %12
%13 = load i32, i32* %4
%14 = call i32 @pkey_write_deny(i32 %13)
%15 = load i32, i32* @SYS_futex
%16 = load i32*, i32** %3
%17 = load i32, i32* @FUTEX_WAIT
%18 = load i32, i32* %7
%19 = sub nsw i32 %18, 1
%20 = load i32, i32* %5
%21 = call i32 @syscall(i32 %15, i32* %16, i32 %17, i32 %19, i32* null, i32* %5, i32 %20)
store i32 %21, i32* %6
br label %22
22: ; preds = %10
%23 = call i32 @fflush(i32* null)
br label %24
24: ; preds = %22
ret void
}

LLVM IR (-O2):
@SYS_futex = global i32 0
@FUTEX_WAIT = global i32 0
define void @test_kernel_gup_write_to_write_disabled_region(i32* %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 56026, i32* %3
%5 = tail call i32 @fflush(i32* null)
store i32 1218, i32* %0
%6 = tail call i32 @pkey_write_deny(i32 %1)
%7 = load i32, i32* @SYS_futex
%8 = load i32, i32* @FUTEX_WAIT
%9 = call i32 @syscall(i32 %7, i32* %0, i32 %8, i32 1217, i32* null, i32* %3, i32 56026)
%10 = call i32 @fflush(i32* null)
ret void
}