#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 */
int /*<<< orphan*/ __cpuid(unsigned int *, unsigned int *, unsigned int *,
                           unsigned int *);
int /*<<< orphan*/ printf(char *);
int pkru_xstate_offset(void) {
  unsigned int eax;
  unsigned int ebx;
  unsigned int ecx;
  unsigned int edx;
  int xstate_offset;
  int xstate_size;
  unsigned long XSTATE_CPUID = 0xd;
  int leaf;
  leaf = (9);
  {
    eax = XSTATE_CPUID;
    ecx = leaf;
    __cpuid(&eax, &ebx, &ecx, &edx);
    if (leaf == (9)) {
      xstate_offset = ebx;
      xstate_size = eax;
    }
  }
  if (xstate_size == 0) {
    printf("could not find size/offset of PKRU in xsave state\n");
    return 0;
  }
  return xstate_offset;
}


x86 assembly (clang, -O0):
<pkru_xstate_offset>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movq   $0xd,-0x28(%rbp)
movl   $0x9,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
mov    %eax,-0x8(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x10(%rbp)
lea    -0x8(%rbp),%rdi
lea    -0xc(%rbp),%rsi
lea    -0x10(%rbp),%rdx
lea    -0x14(%rbp),%rcx
call   1040 <__cpuid@plt>
cmpl   $0x9,-0x2c(%rbp)
jne    116f <pkru_xstate_offset+0x4f>
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1191 <pkru_xstate_offset+0x71>
lea    0xe80(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1197 <pkru_xstate_offset+0x77>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pkru_xstate_offset>:
sub    $0x18,%rsp
movl   $0xd,0x8(%rsp)
movl   $0x9,0xc(%rsp)
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
lea    0x14(%rsp),%rcx
call   1040 <__cpuid@plt>
cmpl   $0x0,0x8(%rsp)
je     115d <pkru_xstate_offset+0x3d>
mov    0x10(%rsp),%eax
add    $0x18,%rsp
ret
lea    0xe9c(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
@.str = [51 x i8] c"could not find size/offset of PKRU in xsave state\0A\00"
define i32 @pkru_xstate_offset() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca i32
store i64 13, i64* %8
store i32 9, i32* %9
%10 = load i64, i64* %8
%11 = trunc i64 %10 to i32
store i32 %11, i32* %2
%12 = load i32, i32* %9
store i32 %12, i32* %4
%13 = call i32 @__cpuid(i32* %2, i32* %3, i32* %4, i32* %5)
%14 = load i32, i32* %9
%15 = icmp eq i32 %14, 9
br i1 %15, label %16, label %19
16: ; preds = %0
%17 = load i32, i32* %3
store i32 %17, i32* %6
%18 = load i32, i32* %2
store i32 %18, i32* %7
br label %19
19: ; preds = %16, %0
%20 = load i32, i32* %7
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %26
24: ; preds = %19
%25 = load i32, i32* %6
store i32 %25, i32* %1
br label %26
26: ; preds = %24, %22
%27 = load i32, i32* %1
ret i32 %27
}

LLVM IR (-O2):
@str = [50 x i8] c"could not find size/offset of PKRU in xsave state\00"
define i32 @pkru_xstate_offset() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %1 to i8*
%6 = bitcast i32* %2 to i8*
%7 = bitcast i32* %3 to i8*
%8 = bitcast i32* %4 to i8*
store i32 13, i32* %1
store i32 9, i32* %3
%9 = call i32 @__cpuid(i32* %1, i32* %2, i32* %3, i32* %4)
%10 = load i32, i32* %2
%11 = load i32, i32* %1
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str, i64 0, i64 0))
br label %15
15: ; preds = %0, %13
%16 = phi i32 [ 0, %13 ], [ %10, %0 ]
ret i32 %16
}