#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 */
scalar_t__ drand48();
int init_apm_pwr_reporting(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ start_apm_pwr_reporting();
int /*<<< orphan*/ stop_apm_pwr_reporting(int);
int /*<<< orphan*/ usleep(int);
int test_apm_pwr_reporting(int family) {
  int result;
  printf("\n###############################\n");
  printf("Testing APM Accumulerated Power\n");
  printf("###############################\n\n");
  result = init_apm_pwr_reporting(family);
  if (result < 0)
    return result;
  printf("\n\nMeasuring power while sleeping 5ms\n");
  start_apm_pwr_reporting();
  usleep(5000);
  stop_apm_pwr_reporting(family);
  printf("\n\nMeasuring power while calculating:\n");
  start_apm_pwr_reporting();
  {
    int i;
    double a = 0.0;
    for (i = 0; i < 20000000; i++) {
      a += drand48();
      a -= drand48();
      a = a * a;
    }
    printf("%lf\n", a);
  }
  stop_apm_pwr_reporting(family);
  return 0;
}


x86 assembly (clang, -O0):
<test_apm_pwr_reporting>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
lea    0xe8e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xea2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xeb5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <init_apm_pwr_reporting@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11b5 <test_apm_pwr_reporting+0x55>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    127c <test_apm_pwr_reporting+0x11c>
lea    0xea9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1050 <start_apm_pwr_reporting@plt>
mov    $0x1388,%edi
call   1080 <usleep@plt>
mov    -0x8(%rbp),%edi
call   1040 <stop_apm_pwr_reporting@plt>
lea    0xea8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1050 <start_apm_pwr_reporting@plt>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0x1312d00,-0x10(%rbp)
jge    125a <test_apm_pwr_reporting+0xfa>
mov    $0x0,%al
call   1070 <drand48@plt>
cvtsi2sd %rax,%xmm0
addsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    $0x0,%al
call   1070 <drand48@plt>
cvtsi2sd %rax,%xmm1
movsd  -0x18(%rbp),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
mulsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1200 <test_apm_pwr_reporting+0xa0>
movsd  -0x18(%rbp),%xmm0
lea    0xe4b(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1040 <stop_apm_pwr_reporting@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_apm_pwr_reporting>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0xe89(%rip),%rdi
call   1030 <puts@plt>
lea    0xe9e(%rip),%rdi
call   1030 <puts@plt>
lea    0xeb2(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1070 <init_apm_pwr_reporting@plt>
test   %eax,%eax
js     124a <test_apm_pwr_reporting+0xda>
lea    0xeb8(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1060 <start_apm_pwr_reporting@plt>
mov    $0x1388,%edi
call   1090 <usleep@plt>
mov    %ebx,%edi
call   1050 <stop_apm_pwr_reporting@plt>
lea    0xeb9(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1060 <start_apm_pwr_reporting@plt>
xorpd  %xmm0,%xmm0
mov    $0x1312d00,%ebp
nopl   0x0(%rax,%rax,1)
movsd  %xmm0,(%rsp)
xor    %eax,%eax
call   1080 <drand48@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
addsd  (%rsp),%xmm0
movsd  %xmm0,(%rsp)
xor    %eax,%eax
call   1080 <drand48@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
movsd  (%rsp),%xmm1
subsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
mulsd  %xmm1,%xmm0
add    $0xffffffff,%ebp
jne    11f0 <test_apm_pwr_reporting+0x80>
lea    0xdc6(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
mov    %ebx,%edi
call   1050 <stop_apm_pwr_reporting@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [34 x i8] c"\0A###############################\0A\00"
@.str.1 = [33 x i8] c"Testing APM Accumulerated Power\0A\00"
@.str.2 = [34 x i8] c"###############################\0A\0A\00"
@.str.3 = [38 x i8] c"\0A\0AMeasuring power while sleeping 5ms\0A\00"
@.str.4 = [38 x i8] c"\0A\0AMeasuring power while calculating:\0A\00"
@.str.5 = [5 x i8] c"%lf\0A\00"
define i32 @test_apm_pwr_reporting(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca double
store i32 %0, i32* %3
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
%10 = load i32, i32* %3
%11 = call i32 @init_apm_pwr_reporting(i32 %10)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = load i32, i32* %4
store i32 %15, i32* %2
br label %47
16: ; preds = %1
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
%18 = call i32 (...) @start_apm_pwr_reporting()
%19 = call i32 @usleep(i32 5000)
%20 = load i32, i32* %3
%21 = call i32 @stop_apm_pwr_reporting(i32 %20)
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0))
%23 = call i32 (...) @start_apm_pwr_reporting()
store double 0.000000e+00, double* %6
store i32 0, i32* %5
br label %24
24: ; preds = %39, %16
%25 = load i32, i32* %5
%26 = icmp slt i32 %25, 20000000
br i1 %26, label %27, label %42
27: ; preds = %24
%28 = call i64 (...) @drand48()
%29 = sitofp i64 %28 to double
%30 = load double, double* %6
%31 = fadd double %30, %29
store double %31, double* %6
%32 = call i64 (...) @drand48()
%33 = sitofp i64 %32 to double
%34 = load double, double* %6
%35 = fsub double %34, %33
store double %35, double* %6
%36 = load double, double* %6
%37 = load double, double* %6
%38 = fmul double %36, %37
store double %38, double* %6
br label %39
39: ; preds = %27
%40 = load i32, i32* %5
%41 = add nsw i32 %40, 1
store i32 %41, i32* %5
br label %24
42: ; preds = %24
%43 = load double, double* %6
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), double %43)
%45 = load i32, i32* %3
%46 = call i32 @stop_apm_pwr_reporting(i32 %45)
store i32 0, i32* %2
br label %47
47: ; preds = %42, %14
%48 = load i32, i32* %2
ret i32 %48
}

LLVM IR (-O2):
@.str.5 = [5 x i8] c"%lf\0A\00"
@str = [33 x i8] c"\0A###############################\00"
@str.6 = [32 x i8] c"Testing APM Accumulerated Power\00"
@str.7 = [33 x i8] c"###############################\0A\00"
@str.8 = [37 x i8] c"\0A\0AMeasuring power while sleeping 5ms\00"
@str.9 = [37 x i8] c"\0A\0AMeasuring power while calculating:\00"
define i32 @test_apm_pwr_reporting(i32 %0) {
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.6, i64 0, i64 0))
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.7, i64 0, i64 0))
%5 = tail call i32 @init_apm_pwr_reporting(i32 %0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %29, label %7
7: ; preds = %1
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.8, i64 0, i64 0))
%9 = tail call i32 (...) @start_apm_pwr_reporting()
%10 = tail call i32 @usleep(i32 5000)
%11 = tail call i32 @stop_apm_pwr_reporting(i32 %0)
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.9, i64 0, i64 0))
%13 = tail call i32 (...) @start_apm_pwr_reporting()
br label %14
14: ; preds = %7, %14
%15 = phi double [ 0.000000e+00, %7 ], [ %23, %14 ]
%16 = phi i32 [ 0, %7 ], [ %24, %14 ]
%17 = tail call i64 (...) @drand48()
%18 = sitofp i64 %17 to double
%19 = fadd double %15, %18
%20 = tail call i64 (...) @drand48()
%21 = sitofp i64 %20 to double
%22 = fsub double %19, %21
%23 = fmul double %22, %22
%24 = add nuw nsw i32 %16, 1
%25 = icmp eq i32 %24, 20000000
br i1 %25, label %26, label %14
26: ; preds = %14
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), double %23)
%28 = tail call i32 @stop_apm_pwr_reporting(i32 %0)
br label %29
29: ; preds = %1, %26
%30 = phi i32 [ 0, %26 ], [ %5, %1 ]
ret i32 %30
}