int pfm_mont_reserved(unsigned int cnum, unsigned long *val,
                      struct pt_regs *regs) {
  unsigned long tmp1, tmp2, ival = *val;
  tmp1 = ival & PMC_RSVD_MASK(cnum);
  tmp2 = PMC_DFL_VAL(cnum) & ~PMC_RSVD_MASK(cnum);
  *val = tmp1 | tmp2;
  DPRINT(("pmc[%d]=0x%lx, mask=0x%lx, reset=0x%lx, val=0x%lx\n", cnum, ival,
          PMC_RSVD_MASK(cnum), PMC_DFL_VAL(cnum), *val));
  return 0;
}


x86 assembly (clang, -O0):
<pfm_mont_reserved>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x4(%rbp),%edi
mov    $0x0,%al
call   1040 <PMC_RSVD_MASK@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
and    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    $0x0,%al
call   1050 <PMC_DFL_VAL@plt>
mov    %eax,-0x34(%rbp)
mov    -0x4(%rbp),%edi
mov    $0x0,%al
call   1040 <PMC_RSVD_MASK@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
xor    $0xffffffff,%ecx
and    %ecx,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rcx
or     -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x4(%rbp),%edi
mov    $0x0,%al
call   1040 <PMC_RSVD_MASK@plt>
mov    -0x4(%rbp),%edi
mov    $0x0,%al
call   1050 <PMC_DFL_VAL@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    $0x0,%al
call   1030 <DPRINT@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pfm_mont_reserved>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%ebp
mov    (%rsi),%r14
xor    %eax,%eax
call   1040 <PMC_RSVD_MASK@plt>
movslq %eax,%rbx
and    %r14,%rbx
mov    %ebp,%edi
xor    %eax,%eax
call   1050 <PMC_DFL_VAL@plt>
mov    %eax,%r14d
mov    %ebp,%edi
xor    %eax,%eax
call   1040 <PMC_RSVD_MASK@plt>
not    %eax
and    %r14d,%eax
cltq
or     %rbx,%rax
mov    %rax,(%r15)
mov    %ebp,%edi
xor    %eax,%eax
call   1040 <PMC_RSVD_MASK@plt>
mov    %ebp,%edi
xor    %eax,%eax
call   1050 <PMC_DFL_VAL@plt>
mov    (%r15),%rdi
xor    %eax,%eax
call   1030 <DPRINT@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pt_regs = type opaque
@.str = [51 x i8] c"pmc[%d]=0x%lx, mask=0x%lx, reset=0x%lx, val=0x%lx\0A\00"
define i32 @pfm_mont_reserved(i32 %0, i64* %1, %struct.pt_regs* %2) {
%4 = alloca i32
%5 = alloca i64*
%6 = alloca %struct.pt_regs*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i32 %0, i32* %4
store i64* %1, i64** %5
store %struct.pt_regs* %2, %struct.pt_regs** %6
%10 = load i64*, i64** %5
%11 = load i64, i64* %10
store i64 %11, i64* %9
%12 = load i64, i64* %9
%13 = load i32, i32* %4
%14 = call i32 (i32, ...) bitcast (i32 (...)* @PMC_RSVD_MASK to i32 (i32, ...)*)(i32 %13)
%15 = sext i32 %14 to i64
%16 = and i64 %12, %15
store i64 %16, i64* %7
%17 = load i32, i32* %4
%18 = call i32 (i32, ...) bitcast (i32 (...)* @PMC_DFL_VAL to i32 (i32, ...)*)(i32 %17)
%19 = load i32, i32* %4
%20 = call i32 (i32, ...) bitcast (i32 (...)* @PMC_RSVD_MASK to i32 (i32, ...)*)(i32 %19)
%21 = xor i32 %20, -1
%22 = and i32 %18, %21
%23 = sext i32 %22 to i64
store i64 %23, i64* %8
%24 = load i64, i64* %7
%25 = load i64, i64* %8
%26 = or i64 %24, %25
%27 = load i64*, i64** %5
store i64 %26, i64* %27
%28 = load i32, i32* %4
%29 = load i64, i64* %9
%30 = load i32, i32* %4
%31 = call i32 (i32, ...) bitcast (i32 (...)* @PMC_RSVD_MASK to i32 (i32, ...)*)(i32 %30)
%32 = load i32, i32* %4
%33 = call i32 (i32, ...) bitcast (i32 (...)* @PMC_DFL_VAL to i32 (i32, ...)*)(i32 %32)
%34 = load i64*, i64** %5
%35 = load i64, i64* %34
%36 = call i32 (i64, ...) bitcast (i32 (...)* @DPRINT to i32 (i64, ...)*)(i64 %35)
ret i32 0
}

LLVM IR (-O2):
%struct.pt_regs = type opaque
define i32 @pfm_mont_reserved(i32 %0, i64* nocapture %1, %struct.pt_regs* nocapture readnone %2) {
%4 = load i64, i64* %1
%5 = tail call i32 (i32, ...) bitcast (i32 (...)* @PMC_RSVD_MASK to i32 (i32, ...)*)(i32 %0)
%6 = sext i32 %5 to i64
%7 = and i64 %4, %6
%8 = tail call i32 (i32, ...) bitcast (i32 (...)* @PMC_DFL_VAL to i32 (i32, ...)*)(i32 %0)
%9 = tail call i32 (i32, ...) bitcast (i32 (...)* @PMC_RSVD_MASK to i32 (i32, ...)*)(i32 %0)
%10 = xor i32 %9, -1
%11 = and i32 %8, %10
%12 = sext i32 %11 to i64
%13 = or i64 %7, %12
store i64 %13, i64* %1
%14 = tail call i32 (i32, ...) bitcast (i32 (...)* @PMC_RSVD_MASK to i32 (i32, ...)*)(i32 %0)
%15 = tail call i32 (i32, ...) bitcast (i32 (...)* @PMC_DFL_VAL to i32 (i32, ...)*)(i32 %0)
%16 = load i64, i64* %1
%17 = tail call i32 (i64, ...) bitcast (i32 (...)* @DPRINT to i32 (i64, ...)*)(i64 %16)
ret i32 0
}