float CalculateCPULoad(unsigned long long idleTicks,
                       unsigned long long totalTicks) {
  static unsigned long long _previousTotalTicks = 0;
  static unsigned long long _previousIdleTicks = 0;
  unsigned long long totalTicksSinceLastTime = totalTicks - _previousTotalTicks;
  unsigned long long idleTicksSinceLastTime = idleTicks - _previousIdleTicks;
  float ret =
      1.0f - ((totalTicksSinceLastTime > 0)
                  ? ((float)idleTicksSinceLastTime) / totalTicksSinceLastTime
                  : 0);
  _previousTotalTicks = totalTicks;
  _previousIdleTicks = idleTicks;
  return ret;
}


x86 assembly (clang, -O0):
<CalculateCPULoad>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
sub    0x2f11(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
sub    0x2f0a(%rip),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    11d1 <CalculateCPULoad+0xd1>
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
shr    %rdx
mov    %eax,%ecx
and    $0x1,%ecx
or     %rdx,%rcx
cvtsi2ss %rcx,%xmm0
addss  %xmm0,%xmm0
cvtsi2ss %rax,%xmm1
movss  %xmm1,-0x2c(%rbp)
test   %rax,%rax
movss  %xmm0,-0x28(%rbp)
js     1172 <CalculateCPULoad+0x72>
movss  -0x2c(%rbp),%xmm0
movss  %xmm0,-0x28(%rbp)
movss  -0x28(%rbp),%xmm0
movss  %xmm0,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
shr    %rdx
mov    %eax,%ecx
and    $0x1,%ecx
or     %rdx,%rcx
cvtsi2ss %rcx,%xmm0
addss  %xmm0,%xmm0
cvtsi2ss %rax,%xmm1
movss  %xmm1,-0x34(%rbp)
test   %rax,%rax
movss  %xmm0,-0x30(%rbp)
js     11b9 <CalculateCPULoad+0xb9>
movss  -0x34(%rbp),%xmm0
movss  %xmm0,-0x30(%rbp)
movss  -0x38(%rbp),%xmm0
movss  -0x30(%rbp),%xmm1
divss  %xmm1,%xmm0
movss  %xmm0,-0x3c(%rbp)
jmp    11de <CalculateCPULoad+0xde>
xorps  %xmm0,%xmm0
movss  %xmm0,-0x3c(%rbp)
jmp    11de <CalculateCPULoad+0xde>
movss  -0x3c(%rbp),%xmm1
movss  0xe15(%rip),%xmm0
subss  %xmm1,%xmm0
movss  %xmm0,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,0x2e29(%rip)
mov    -0x8(%rbp),%rax
mov    %rax,0x2e26(%rip)
movss  -0x24(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<CalculateCPULoad>:
mov    %rsi,%rax
sub    0x2f1e(%rip),%rax
je     1139 <CalculateCPULoad+0x39>
mov    %rdi,%rcx
sub    0x2f1a(%rip),%rcx
js     113e <CalculateCPULoad+0x3e>
cvtsi2ss %rcx,%xmm1
test   %rax,%rax
jns    1158 <CalculateCPULoad+0x58>
mov    %rax,%rcx
shr    %rcx
and    $0x1,%eax
or     %rcx,%rax
cvtsi2ss %rax,%xmm0
addss  %xmm0,%xmm0
jmp    115d <CalculateCPULoad+0x5d>
xorps  %xmm1,%xmm1
jmp    1161 <CalculateCPULoad+0x61>
mov    %rcx,%rdx
shr    %rdx
and    $0x1,%ecx
or     %rdx,%rcx
cvtsi2ss %rcx,%xmm1
addss  %xmm1,%xmm1
test   %rax,%rax
js     1122 <CalculateCPULoad+0x22>
cvtsi2ss %rax,%xmm0
divss  %xmm0,%xmm1
movss  0xe97(%rip),%xmm0
subss  %xmm1,%xmm0
mov    %rsi,0x2eb4(%rip)
mov    %rdi,0x2eb5(%rip)
ret

LLVM IR (-O0):
@CalculateCPULoad._previousTotalTicks = internal global i64 0
@CalculateCPULoad._previousIdleTicks = internal global i64 0
define float @CalculateCPULoad(i64 %0, i64 %1) {
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca float
store i64 %0, i64* %3
store i64 %1, i64* %4
%8 = load i64, i64* %4
%9 = load i64, i64* @CalculateCPULoad._previousTotalTicks
%10 = sub i64 %8, %9
store i64 %10, i64* %5
%11 = load i64, i64* %3
%12 = load i64, i64* @CalculateCPULoad._previousIdleTicks
%13 = sub i64 %11, %12
store i64 %13, i64* %6
%14 = load i64, i64* %5
%15 = icmp ugt i64 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %2
%17 = load i64, i64* %6
%18 = uitofp i64 %17 to float
%19 = load i64, i64* %5
%20 = uitofp i64 %19 to float
%21 = fdiv float %18, %20
br label %23
22: ; preds = %2
br label %23
23: ; preds = %22, %16
%24 = phi float [ %21, %16 ], [ 0.000000e+00, %22 ]
%25 = fsub float 1.000000e+00, %24
store float %25, float* %7
%26 = load i64, i64* %4
store i64 %26, i64* @CalculateCPULoad._previousTotalTicks
%27 = load i64, i64* %3
store i64 %27, i64* @CalculateCPULoad._previousIdleTicks
%28 = load float, float* %7
ret float %28
}

LLVM IR (-O2):
@CalculateCPULoad._previousTotalTicks = internal unnamed_addr global i64 0
@CalculateCPULoad._previousIdleTicks = internal unnamed_addr global i64 0
define float @CalculateCPULoad(i64 %0, i64 %1) {
%3 = load i64, i64* @CalculateCPULoad._previousTotalTicks
%4 = sub i64 %1, %3
%5 = icmp eq i64 %4, 0
br i1 %5, label %12, label %6
6: ; preds = %2
%7 = load i64, i64* @CalculateCPULoad._previousIdleTicks
%8 = sub i64 %0, %7
%9 = uitofp i64 %8 to float
%10 = uitofp i64 %4 to float
%11 = fdiv float %9, %10
br label %12
12: ; preds = %2, %6
%13 = phi float [ %11, %6 ], [ 0.000000e+00, %2 ]
%14 = fsub float 1.000000e+00, %13
store i64 %1, i64* @CalculateCPULoad._previousTotalTicks
store i64 %0, i64* @CalculateCPULoad._previousIdleTicks
ret float %14
}