#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 /*<<< orphan*/ clobber;

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int pwrite(int, unsigned char *, int, unsigned long);
void clobber_insn_unless_known_safe(unsigned char *p) {
  extern unsigned char safe_wrpkru_trusted[];
  extern unsigned char safe_wrpkru_normal[];
  if (p == safe_wrpkru_trusted || p == safe_wrpkru_normal) {
    printf("  known safe\n");
    return;
  }
  printf("  clobbering\n");
  int fd = open("/proc/self/mem", O_WRONLY);
  if (fd == -1)
    err(1, "open mem");
  unsigned char clobber[] = {0xcc, 0xcc, 0xcc};
  if (pwrite(fd, clobber, sizeof(clobber), (unsigned long)p) != sizeof(clobber))
    err(1, "clobbering instruction failed");
  close(fd);
}


x86 assembly (clang, -O0):
<clobber_insn_unless_known_safe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e75(%rip),%rax
cmp    %rax,-0x8(%rbp)
je     117e <clobber_insn_unless_known_safe+0x2e>
mov    0x2e7c(%rip),%rax
cmp    %rax,-0x8(%rbp)
jne    118f <clobber_insn_unless_known_safe+0x3f>
lea    0xe7b(%rip),%rdi
call   1030 <printf@plt>
jmp    121c <clobber_insn_unless_known_safe+0xcc>
lea    0xe78(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11ce <clobber_insn_unless_known_safe+0x7e>
mov    $0x1,%edi
lea    0xe62(%rip),%rsi
call   1050 <err@plt>
mov    0xe7d(%rip),%ax
mov    %ax,-0xf(%rbp)
mov    0xe75(%rip),%al
mov    %al,-0xd(%rbp)
mov    -0xc(%rbp),%edi
lea    -0xf(%rbp),%rsi
mov    -0x8(%rbp),%rcx
mov    $0x3,%edx
call   1060 <pwrite@plt>
cltq
cmp    $0x3,%rax
je     1214 <clobber_insn_unless_known_safe+0xc4>
mov    $0x1,%edi
lea    0xe25(%rip),%rsi
call   1050 <err@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clobber_insn_unless_known_safe>:
push   %rbp
push   %rbx
push   %rax
cmp    0x2e7e(%rip),%rdi
je     11e8 <clobber_insn_unless_known_safe+0x98>
mov    %rdi,%rbx
cmp    0x2e86(%rip),%rdi
je     11e8 <clobber_insn_unless_known_safe+0x98>
lea    0xec3(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
lea    0xe78(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11a5 <clobber_insn_unless_known_safe+0x55>
lea    0xe74(%rip),%rsi
mov    $0x1,%edi
call   1050 <err@plt>
movb   $0xcc,0x6(%rsp)
movw   $0xcccc,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    %ebp,%edi
mov    $0x3,%edx
mov    %rbx,%rcx
call   1060 <pwrite@plt>
cmp    $0x3,%eax
je     11db <clobber_insn_unless_known_safe+0x8b>
lea    0xe47(%rip),%rsi
mov    $0x1,%edi
call   1050 <err@plt>
mov    %ebp,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <close@plt>
lea    0xe54(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <puts@plt>

LLVM IR (-O0):
@safe_wrpkru_trusted = external global [0 x i8]
@safe_wrpkru_normal = external global [0 x i8]
@.str = [14 x i8] c" known safe\0A\00"
@.str.1 = [14 x i8] c" clobbering\0A\00"
@.str.2 = [15 x i8] c"/proc/self/mem\00"
@O_WRONLY = global i32 0
@.str.3 = [9 x i8] c"open mem\00"
@__const.clobber_insn_unless_known_safe.clobber = [3 x i8] c"\CC\CC\CC"
@.str.4 = [30 x i8] c"clobbering instruction failed\00"
define void @clobber_insn_unless_known_safe(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [3 x i8]
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = icmp eq i8* %5, getelementptr inbounds ([0 x i8], [0 x i8]* @safe_wrpkru_trusted, i64 0, i64 0)
br i1 %6, label %10, label %7
7: ; preds = %1
%8 = load i8*, i8** %2
%9 = icmp eq i8* %8, getelementptr inbounds ([0 x i8], [0 x i8]* @safe_wrpkru_normal, i64 0, i64 0)
br i1 %9, label %10, label %12
10: ; preds = %7, %1
%11 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %34
12: ; preds = %7
%13 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* @O_WRONLY
%15 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %14)
store i32 %15, i32* %3
%16 = load i32, i32* %3
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = call i32 @err(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %20
20: ; preds = %18, %12
%21 = bitcast [3 x i8]* %4 to i8*
%22 = load i32, i32* %3
%23 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
%24 = load i8*, i8** %2
%25 = ptrtoint i8* %24 to i64
%26 = call i32 @pwrite(i32 %22, i8* %23, i32 3, i64 %25)
%27 = sext i32 %26 to i64
%28 = icmp ne i64 %27, 3
br i1 %28, label %29, label %31
29: ; preds = %20
%30 = call i32 @err(i32 1, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
br label %31
31: ; preds = %29, %20
%32 = load i32, i32* %3
%33 = call i32 @close(i32 %32)
br label %34
34: ; preds = %31, %10
ret void
}

LLVM IR (-O2):
@safe_wrpkru_trusted = external global [0 x i8]
@safe_wrpkru_normal = external global [0 x i8]
@.str.2 = [15 x i8] c"/proc/self/mem\00"
@O_WRONLY = global i32 0
@.str.3 = [9 x i8] c"open mem\00"
@.str.4 = [30 x i8] c"clobbering instruction failed\00"
@str = [13 x i8] c" clobbering\00"
@str.5 = [13 x i8] c" known safe\00"
define void @clobber_insn_unless_known_safe(i8* %0) {
%2 = alloca [3 x i8]
%3 = icmp eq i8* %0, getelementptr inbounds ([0 x i8], [0 x i8]* @safe_wrpkru_trusted, i64 0, i64 0)
%4 = icmp eq i8* %0, getelementptr inbounds ([0 x i8], [0 x i8]* @safe_wrpkru_normal, i64 0, i64 0)
%5 = or i1 %3, %4
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.5, i64 0, i64 0))
br label %24
8: ; preds = %1
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%10 = load i32, i32* @O_WRONLY
%11 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %15
15: ; preds = %13, %8
%16 = getelementptr inbounds [3 x i8], [3 x i8]* %2, i64 0, i64 0
%17 = ptrtoint i8* %0 to i64
%18 = call i32 @pwrite(i32 %11, i8* %16, i32 3, i64 %17)
%19 = icmp eq i32 %18, 3
br i1 %19, label %22, label %20
20: ; preds = %15
%21 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
br label %22
22: ; preds = %20, %15
%23 = tail call i32 @close(i32 %11)
br label %24
24: ; preds = %22, %6
ret void
}