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

/* Variables and functions */
int read_msr(unsigned int, int, unsigned long long *);
int get_aperf_mperf(unsigned int cpu, uint64_t *aperf, uint64_t *mperf) {
  int retval;
  retval = read_msr(cpu, 0x000000E8, (unsigned long long *)aperf);
  if (retval < 0)
    return retval;
  retval = read_msr(cpu, 0x000000E7, (unsigned long long *)mperf);
  if (retval < 0)
    return retval;
  return 0;
}


x86 assembly (clang, -O0):
<get_aperf_mperf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rdx
mov    $0xe8,%esi
call   1030 <read_msr@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    114c <get_aperf_mperf+0x3c>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    117c <get_aperf_mperf+0x6c>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rdx
mov    $0xe7,%esi
call   1030 <read_msr@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1175 <get_aperf_mperf+0x65>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    117c <get_aperf_mperf+0x6c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_aperf_mperf>:
push   %rbp
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%rdx
mov    %edi,%ebp
mov    $0xe8,%esi
call   1030 <read_msr@plt>
test   %eax,%eax
js     113f <get_aperf_mperf+0x2f>
mov    %ebp,%edi
mov    $0xe7,%esi
mov    %rbx,%rdx
call   1030 <read_msr@plt>
mov    %eax,%ecx
sar    $0x1f,%eax
and    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @get_aperf_mperf(i32 %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32* %2, i32** %7
%9 = load i32, i32* %5
%10 = load i32*, i32** %6
%11 = bitcast i32* %10 to i64*
%12 = call i32 @read_msr(i32 %9, i32 232, i64* %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = load i32, i32* %8
store i32 %16, i32* %4
br label %27
17: ; preds = %3
%18 = load i32, i32* %5
%19 = load i32*, i32** %7
%20 = bitcast i32* %19 to i64*
%21 = call i32 @read_msr(i32 %18, i32 231, i64* %20)
store i32 %21, i32* %8
%22 = load i32, i32* %8
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %17
%25 = load i32, i32* %8
store i32 %25, i32* %4
br label %27
26: ; preds = %17
store i32 0, i32* %4
br label %27
27: ; preds = %26, %24, %15
%28 = load i32, i32* %4
ret i32 %28
}

LLVM IR (-O2):
define i32 @get_aperf_mperf(i32 %0, i32* %1, i32* %2) {
%4 = bitcast i32* %1 to i64*
%5 = tail call i32 @read_msr(i32 %0, i32 232, i64* %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %12, label %7
7: ; preds = %3
%8 = bitcast i32* %2 to i64*
%9 = tail call i32 @read_msr(i32 %0, i32 231, i64* %8)
%10 = icmp slt i32 %9, 0
%11 = select i1 %10, i32 %9, i32 0
br label %12
12: ; preds = %7, %3
%13 = phi i32 [ %5, %3 ], [ %11, %7 ]
ret i32 %13
}