#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 */
struct timespec {
  int tv_sec;
  scalar_t__ tv_nsec;
};
typedef double int64_t;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int atoi(char *);
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ hb_energy_finish();
char *hb_energy_get_source();
scalar_t__ hb_energy_init();
double hb_energy_read_total(double, double);
scalar_t__ nanosleep(struct timespec *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, double);
int /*<<< orphan*/ stderr;
double to_nanosec(struct timespec *);
int main(int argc, char **argv) {
  struct timespec ts_sleep;
  struct timespec time_info;
  int64_t time_start;
  int64_t time_end;
  double joules_start;
  double joules_end;
  double watts;
  if (argc > 1) {
    ts_sleep.tv_sec = atoi(argv[1]);
    if (ts_sleep.tv_sec <= 0) {
      fprintf(stderr,
              "Sleep time must be > 0 seconds. Defaulting to %d seconds.\n",
              10);
      ts_sleep.tv_sec = 10;
    }
  } else {
    ts_sleep.tv_sec = 10;
  }
  ts_sleep.tv_nsec = 0;
  if (hb_energy_init()) {
    fprintf(stderr, "Could not initialize energy reading from: %s\n",
            hb_energy_get_source());
    return 1;
  }
  clock_gettime(CLOCK_REALTIME, &time_info);
  time_start = to_nanosec(&time_info);
  clock_gettime(CLOCK_REALTIME, &time_info);
  time_end = to_nanosec(&time_info);
  joules_start = hb_energy_read_total(time_start, time_end);
  clock_gettime(CLOCK_REALTIME, &time_info);
  time_start = to_nanosec(&time_info);
  if (nanosleep(&ts_sleep, NULL)) {
    fprintf(stderr, "Sleep failed - aborting.\n");
    hb_energy_finish();
    return 1;
  }
  clock_gettime(CLOCK_REALTIME, &time_info);
  time_end = to_nanosec(&time_info);
  joules_end = hb_energy_read_total(time_start, time_end);
  watts = (joules_end - joules_start) / (time_end - time_start) * 1000000000.0;
  printf("%f\n", watts);
  hb_energy_finish();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1202 <main+0x62>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jg     11fd <main+0x5d>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
lea    0xe1e(%rip),%rsi
mov    $0xa,%edx
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xa,-0x20(%rbp)
jmp    1209 <main+0x69>
movl   $0xa,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   1090 <hb_energy_init@plt>
cmp    $0x0,%rax
je     1255 <main+0xb5>
mov    0x2db7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    $0x0,%al
call   10b0 <hb_energy_get_source@plt>
mov    -0x5c(%rbp),%edi
mov    %rax,%rdx
lea    0xe01(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1386 <main+0x1e6>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1030 <clock_gettime@plt>
lea    -0x30(%rbp),%rdi
call   1080 <to_nanosec@plt>
movsd  %xmm0,-0x38(%rbp)
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1030 <clock_gettime@plt>
lea    -0x30(%rbp),%rdi
call   1080 <to_nanosec@plt>
movsd  %xmm0,-0x40(%rbp)
movsd  -0x38(%rbp),%xmm0
movsd  -0x40(%rbp),%xmm1
call   1040 <hb_energy_read_total@plt>
movsd  %xmm0,-0x48(%rbp)
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1030 <clock_gettime@plt>
lea    -0x30(%rbp),%rdi
call   1080 <to_nanosec@plt>
movsd  %xmm0,-0x38(%rbp)
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <nanosleep@plt>
cmp    $0x0,%rax
je     130a <main+0x16a>
mov    0x2cf9(%rip),%rax
mov    (%rax),%edi
lea    0xd81(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x0,%al
call   10a0 <hb_energy_finish@plt>
movl   $0x1,-0x4(%rbp)
jmp    1386 <main+0x1e6>
mov    0x2cc7(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1030 <clock_gettime@plt>
lea    -0x30(%rbp),%rdi
call   1080 <to_nanosec@plt>
movsd  %xmm0,-0x40(%rbp)
movsd  -0x38(%rbp),%xmm0
movsd  -0x40(%rbp),%xmm1
call   1040 <hb_energy_read_total@plt>
movsd  %xmm0,-0x50(%rbp)
movsd  -0x50(%rbp),%xmm0
subsd  -0x48(%rbp),%xmm0
movsd  -0x40(%rbp),%xmm1
subsd  -0x38(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  0xca2(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x58(%rbp),%xmm0
lea    0xd18(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   10a0 <hb_energy_finish@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x38,%rsp
cmp    $0x2,%edi
jl     11d9 <main+0x39>
mov    0x8(%rsi),%rdi
call   10c0 <atoi@plt>
mov    %eax,0x8(%rsp)
test   %eax,%eax
jg     11e1 <main+0x41>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
lea    0xe3b(%rip),%rsi
mov    $0xa,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
movl   $0xa,0x8(%rsp)
movq   $0x0,0x10(%rsp)
xor    %eax,%eax
call   1090 <hb_energy_init@plt>
test   %rax,%rax
je     1223 <main+0x83>
mov    0x2de3(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   10b0 <hb_energy_get_source@plt>
lea    0xe36(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%ebx
jmp    131a <main+0x17a>
mov    0x2dae(%rip),%r14
mov    (%r14),%edi
lea    0x28(%rsp),%rbx
mov    %rbx,%rsi
call   1030 <clock_gettime@plt>
mov    %rbx,%rdi
call   1080 <to_nanosec@plt>
movsd  %xmm0,(%rsp)
mov    (%r14),%edi
mov    %rbx,%rsi
call   1030 <clock_gettime@plt>
mov    %rbx,%rdi
call   1080 <to_nanosec@plt>
movapd %xmm0,%xmm1
movsd  (%rsp),%xmm0
call   1040 <hb_energy_read_total@plt>
movsd  %xmm0,0x20(%rsp)
mov    (%r14),%edi
mov    %rbx,%rsi
call   1030 <clock_gettime@plt>
mov    %rbx,%rdi
call   1080 <to_nanosec@plt>
movsd  %xmm0,(%rsp)
xor    %ebx,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1060 <nanosleep@plt>
test   %rax,%rax
je     12be <main+0x11e>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
lea    0xdc8(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
xor    %eax,%eax
call   10a0 <hb_energy_finish@plt>
mov    $0x1,%ebx
jmp    131a <main+0x17a>
mov    (%r14),%edi
lea    0x28(%rsp),%r14
mov    %r14,%rsi
call   1030 <clock_gettime@plt>
mov    %r14,%rdi
call   1080 <to_nanosec@plt>
movapd %xmm0,%xmm1
movsd  %xmm0,0x18(%rsp)
movsd  (%rsp),%xmm0
call   1040 <hb_energy_read_total@plt>
subsd  0x20(%rsp),%xmm0
movsd  0x18(%rsp),%xmm1
subsd  (%rsp),%xmm1
divsd  %xmm1,%xmm0
mulsd  0xcf9(%rip),%xmm0
lea    0xd7d(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
call   10a0 <hb_energy_finish@plt>
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
@stderr = global i32 0
@.str = [59 x i8] c"Sleep time must be > 0 seconds. Defaulting to %d seconds.\0A\00"
@.str.1 = [46 x i8] c"Could not initialize energy reading from: %s\0A\00"
@CLOCK_REALTIME = global i32 0
@.str.2 = [26 x i8] c"Sleep failed - aborting.\0A\00"
@.str.3 = [4 x i8] c"%f\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.timespec
%7 = alloca %struct.timespec
%8 = alloca double
%9 = alloca double
%10 = alloca double
%11 = alloca double
%12 = alloca double
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp sgt i32 %13, 1
br i1 %14, label %15, label %29
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @atoi(i8* %18)
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%22 = load i32, i32* %21
%23 = icmp sle i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %15
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0), i32 10)
%27 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
store i32 10, i32* %27
br label %28
28: ; preds = %24, %15
br label %31
29: ; preds = %2
%30 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
store i32 10, i32* %30
br label %31
31: ; preds = %29, %28
%32 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
store i64 0, i64* %32
%33 = call i64 (...) @hb_energy_init()
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %31
%36 = load i32, i32* @stderr
%37 = call i8* (...) @hb_energy_get_source()
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i8* %37)
store i32 1, i32* %3
br label %76
39: ; preds = %31
%40 = load i32, i32* @CLOCK_REALTIME
%41 = call i32 @clock_gettime(i32 %40, %struct.timespec* %7)
%42 = call double @to_nanosec(%struct.timespec* %7)
store double %42, double* %8
%43 = load i32, i32* @CLOCK_REALTIME
%44 = call i32 @clock_gettime(i32 %43, %struct.timespec* %7)
%45 = call double @to_nanosec(%struct.timespec* %7)
store double %45, double* %9
%46 = load double, double* %8
%47 = load double, double* %9
%48 = call double @hb_energy_read_total(double %46, double %47)
store double %48, double* %10
%49 = load i32, i32* @CLOCK_REALTIME
%50 = call i32 @clock_gettime(i32 %49, %struct.timespec* %7)
%51 = call double @to_nanosec(%struct.timespec* %7)
store double %51, double* %8
%52 = call i64 @nanosleep(%struct.timespec* %6, i32* null)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %58
54: ; preds = %39
%55 = load i32, i32* @stderr
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%57 = call i32 (...) @hb_energy_finish()
store i32 1, i32* %3
br label %76
58: ; preds = %39
%59 = load i32, i32* @CLOCK_REALTIME
%60 = call i32 @clock_gettime(i32 %59, %struct.timespec* %7)
%61 = call double @to_nanosec(%struct.timespec* %7)
store double %61, double* %9
%62 = load double, double* %8
%63 = load double, double* %9
%64 = call double @hb_energy_read_total(double %62, double %63)
store double %64, double* %11
%65 = load double, double* %11
%66 = load double, double* %10
%67 = fsub double %65, %66
%68 = load double, double* %9
%69 = load double, double* %8
%70 = fsub double %68, %69
%71 = fdiv double %67, %70
%72 = fmul double %71, 1.000000e+09
store double %72, double* %12
%73 = load double, double* %12
%74 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), double %73)
%75 = call i32 (...) @hb_energy_finish()
store i32 0, i32* %3
br label %76
76: ; preds = %58, %54, %35
%77 = load i32, i32* %3
ret i32 %77
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@stderr = global i32 0
@.str = [59 x i8] c"Sleep time must be > 0 seconds. Defaulting to %d seconds.\0A\00"
@.str.1 = [46 x i8] c"Could not initialize energy reading from: %s\0A\00"
@CLOCK_REALTIME = global i32 0
@.str.2 = [26 x i8] c"Sleep failed - aborting.\0A\00"
@.str.3 = [4 x i8] c"%f\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = bitcast %struct.timespec* %3 to i8*
%6 = bitcast %struct.timespec* %4 to i8*
%7 = icmp sgt i32 %0, 1
br i1 %7, label %8, label %17
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 @atoi(i8* %10)
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
store i32 %11, i32* %12
%13 = icmp slt i32 %11, 1
br i1 %13, label %14, label %19
14: ; preds = %8
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0), i32 10)
store i32 10, i32* %12
br label %19
17: ; preds = %2
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
store i32 10, i32* %18
br label %19
19: ; preds = %8, %14, %17
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
store i64 0, i64* %20
%21 = tail call i64 (...) @hb_energy_init()
%22 = icmp eq i64 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = tail call i8* (...) @hb_energy_get_source()
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i8* %25)
br label %55
27: ; preds = %19
%28 = load i32, i32* @CLOCK_REALTIME
%29 = call i32 @clock_gettime(i32 %28, %struct.timespec* %4)
%30 = call double @to_nanosec(%struct.timespec* %4)
%31 = load i32, i32* @CLOCK_REALTIME
%32 = call i32 @clock_gettime(i32 %31, %struct.timespec* %4)
%33 = call double @to_nanosec(%struct.timespec* %4)
%34 = call double @hb_energy_read_total(double %30, double %33)
%35 = load i32, i32* @CLOCK_REALTIME
%36 = call i32 @clock_gettime(i32 %35, %struct.timespec* %4)
%37 = call double @to_nanosec(%struct.timespec* %4)
%38 = call i64 @nanosleep(%struct.timespec* %3, i32* null)
%39 = icmp eq i64 %38, 0
br i1 %39, label %44, label %40
40: ; preds = %27
%41 = load i32, i32* @stderr
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%43 = call i32 (...) @hb_energy_finish()
br label %55
44: ; preds = %27
%45 = load i32, i32* @CLOCK_REALTIME
%46 = call i32 @clock_gettime(i32 %45, %struct.timespec* %4)
%47 = call double @to_nanosec(%struct.timespec* %4)
%48 = call double @hb_energy_read_total(double %37, double %47)
%49 = fsub double %48, %34
%50 = fsub double %47, %37
%51 = fdiv double %49, %50
%52 = fmul double %51, 1.000000e+09
%53 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), double %52)
%54 = call i32 (...) @hb_energy_finish()
br label %55
55: ; preds = %44, %40, %23
%56 = phi i32 [ 1, %23 ], [ 1, %40 ], [ 0, %44 ]
ret i32 %56
}