#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 uint64_t;
struct timeval {
  int tv_sec;
  int tv_usec;
};

/* Variables and functions */
int get_C_state_time(struct timeval time_diff, uint64_t mperf_diff,
                     unsigned long max_freq, struct timeval *C0_time,
                     struct timeval *CX_time, unsigned int *percent) {
  unsigned long long overall_msecs, expected_ticks, c0_time, cx_time;
  overall_msecs = (time_diff.tv_sec * 1000 * 1000 + time_diff.tv_usec) / 1000;
  expected_ticks = max_freq * overall_msecs;
  *percent = (mperf_diff * 100) / expected_ticks;
  cx_time = (expected_ticks - mperf_diff) / max_freq;
  c0_time = mperf_diff / max_freq;
  CX_time->tv_sec = cx_time / 1000;
  CX_time->tv_usec = cx_time % 1000;
  C0_time->tv_sec = c0_time / 1000;
  C0_time->tv_usec = c0_time % 1000;
  return 0;
}


x86 assembly (clang, -O0):
<get_C_state_time>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
imul   $0x3e8,-0x8(%rbp),%eax
imul   $0x3e8,%eax,%eax
add    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
cltq
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
imul   -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
imul   $0x64,-0xc(%rbp),%eax
cltq
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x40(%rbp)
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x40(%rbp),%rax
movslq -0xc(%rbp),%rcx
sub    %rcx,%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x18(%rbp)
mov    %rax,-0x50(%rbp)
movslq -0xc(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x18(%rbp)
mov    %rax,-0x48(%rbp)
mov    -0x50(%rbp),%rax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %rcx
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x50(%rbp),%rax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %rcx
mov    %edx,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x48(%rbp),%rax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %rcx
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x48(%rbp),%rax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %rcx
mov    %edx,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x4(%rax)
xor    %eax,%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_C_state_time>:
mov    %rdx,%r10
imul   $0xf4240,%edi,%eax
shr    $0x20,%rdi
add    %edi,%eax
cltq
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rax
add    %edx,%eax
movslq %eax,%rdi
imul   %r10,%rdi
imul   $0x64,%esi,%eax
movslq %eax,%r11
mov    %r11,%rdx
or     %rdi,%rdx
shr    $0x20,%rdx
je     1148 <get_C_state_time+0x48>
mov    %r11,%rax
xor    %edx,%edx
div    %rdi
jmp    114c <get_C_state_time+0x4c>
xor    %edx,%edx
div    %edi
mov    %eax,(%r9)
movslq %esi,%r9
sub    %r9,%rdi
mov    %rdi,%rax
or     %r10,%rax
shr    $0x20,%rax
je     116e <get_C_state_time+0x6e>
mov    %rdi,%rax
xor    %edx,%edx
div    %r10
mov    %rax,%rdi
jmp    1177 <get_C_state_time+0x77>
mov    %edi,%eax
xor    %edx,%edx
div    %r10d
mov    %eax,%edi
mov    %r9,%rax
or     %r10,%rax
shr    $0x20,%rax
je     1190 <get_C_state_time+0x90>
mov    %r9,%rax
xor    %edx,%edx
div    %r10
mov    %rax,%rsi
jmp    1199 <get_C_state_time+0x99>
mov    %esi,%eax
xor    %edx,%edx
div    %r10d
mov    %eax,%esi
mov    %rdi,%rax
shr    $0x3,%rax
movabs $0x20c49ba5e353f7cf,%r9
9b c4 20
mul    %r9
shr    $0x4,%rdx
mov    %edx,(%r8)
imul   $0x3e8,%edx,%eax
sub    %eax,%edi
mov    %edi,0x4(%r8)
mov    %rsi,%rax
shr    $0x3,%rax
mul    %r9
shr    $0x4,%rdx
mov    %edx,(%rcx)
imul   $0x3e8,%edx,%eax
sub    %eax,%esi
mov    %esi,0x4(%rcx)
xor    %eax,%eax
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
define i32 @get_C_state_time(i64 %0, i32 %1, i64 %2, %struct.timeval* %3, %struct.timeval* %4, i32* %5) {
%7 = alloca %struct.timeval
%8 = alloca i32
%9 = alloca i64
%10 = alloca %struct.timeval*
%11 = alloca %struct.timeval*
%12 = alloca i32*
%13 = alloca i64
%14 = alloca i64
%15 = alloca i64
%16 = alloca i64
%17 = bitcast %struct.timeval* %7 to i64*
store i64 %0, i64* %17
store i32 %1, i32* %8
store i64 %2, i64* %9
store %struct.timeval* %3, %struct.timeval** %10
store %struct.timeval* %4, %struct.timeval** %11
store i32* %5, i32** %12
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%19 = load i32, i32* %18
%20 = mul nsw i32 %19, 1000
%21 = mul nsw i32 %20, 1000
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
%23 = load i32, i32* %22
%24 = add nsw i32 %21, %23
%25 = sdiv i32 %24, 1000
%26 = sext i32 %25 to i64
store i64 %26, i64* %13
%27 = load i64, i64* %9
%28 = load i64, i64* %13
%29 = mul i64 %27, %28
store i64 %29, i64* %14
%30 = load i32, i32* %8
%31 = mul nsw i32 %30, 100
%32 = sext i32 %31 to i64
%33 = load i64, i64* %14
%34 = udiv i64 %32, %33
%35 = trunc i64 %34 to i32
%36 = load i32*, i32** %12
store i32 %35, i32* %36
%37 = load i64, i64* %14
%38 = load i32, i32* %8
%39 = sext i32 %38 to i64
%40 = sub i64 %37, %39
%41 = load i64, i64* %9
%42 = udiv i64 %40, %41
store i64 %42, i64* %16
%43 = load i32, i32* %8
%44 = sext i32 %43 to i64
%45 = load i64, i64* %9
%46 = udiv i64 %44, %45
store i64 %46, i64* %15
%47 = load i64, i64* %16
%48 = udiv i64 %47, 1000
%49 = trunc i64 %48 to i32
%50 = load %struct.timeval*, %struct.timeval** %11
%51 = getelementptr inbounds %struct.timeval, %struct.timeval* %50, i32 0, i32 0
store i32 %49, i32* %51
%52 = load i64, i64* %16
%53 = urem i64 %52, 1000
%54 = trunc i64 %53 to i32
%55 = load %struct.timeval*, %struct.timeval** %11
%56 = getelementptr inbounds %struct.timeval, %struct.timeval* %55, i32 0, i32 1
store i32 %54, i32* %56
%57 = load i64, i64* %15
%58 = udiv i64 %57, 1000
%59 = trunc i64 %58 to i32
%60 = load %struct.timeval*, %struct.timeval** %10
%61 = getelementptr inbounds %struct.timeval, %struct.timeval* %60, i32 0, i32 0
store i32 %59, i32* %61
%62 = load i64, i64* %15
%63 = urem i64 %62, 1000
%64 = trunc i64 %63 to i32
%65 = load %struct.timeval*, %struct.timeval** %10
%66 = getelementptr inbounds %struct.timeval, %struct.timeval* %65, i32 0, i32 1
store i32 %64, i32* %66
ret i32 0
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
define i32 @get_C_state_time(i64 %0, i32 %1, i64 %2, %struct.timeval* nocapture writeonly %3, %struct.timeval* nocapture writeonly %4, i32* nocapture writeonly %5) {
%7 = trunc i64 %0 to i32
%8 = lshr i64 %0, 32
%9 = trunc i64 %8 to i32
%10 = mul i32 %7, 1000000
%11 = add nsw i32 %10, %9
%12 = sdiv i32 %11, 1000
%13 = sext i32 %12 to i64
%14 = mul i64 %13, %2
%15 = mul nsw i32 %1, 100
%16 = sext i32 %15 to i64
%17 = udiv i64 %16, %14
%18 = trunc i64 %17 to i32
store i32 %18, i32* %5
%19 = sext i32 %1 to i64
%20 = sub i64 %14, %19
%21 = udiv i64 %20, %2
%22 = udiv i64 %19, %2
%23 = udiv i64 %21, 1000
%24 = trunc i64 %23 to i32
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 %24, i32* %25
%26 = urem i64 %21, 1000
%27 = trunc i64 %26 to i32
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i32 %27, i32* %28
%29 = udiv i64 %22, 1000
%30 = trunc i64 %29 to i32
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
store i32 %30, i32* %31
%32 = urem i64 %22, 1000
%33 = trunc i64 %32 to i32
%34 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
store i32 %33, i32* %34
ret i32 0
}