#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 periodic_task {
  int dummy;
};

/* Variables and functions */
int calibrate(int);
int cpu_speed();
int gettime();
int /*<<< orphan*/ job_body(int);
int /*<<< orphan*/ make_rt();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
struct periodic_task *start_periodic_timer(int /*<<< orphan*/, int);
int /*<<< orphan*/ wait_next_activation(struct periodic_task *);
int main(int argc, char *argv[]) {
  uint64_t cnt, t1, t2, hz;
  struct periodic_task *h;
  make_rt();
  hz = cpu_speed();
  cnt = calibrate(100 * hz);
  printf("CPU Speed: %Lu\n", hz);
  printf("Cycles: %Lu\n", cnt);
  h = start_periodic_timer(0, 100000);
  if (h == NULL) {
    perror("Start Periodic Timer");
    return -1;
  }
  while (1) {
    wait_next_activation(h);
    t1 = gettime();
    job_body(cnt / 100);
    t2 = gettime();
    printf("%Lu\n", t2 - t1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1030 <make_rt@plt>
mov    $0x0,%al
call   1090 <cpu_speed@plt>
mov    %eax,-0x20(%rbp)
imul   $0x64,-0x20(%rbp),%edi
call   1060 <calibrate@plt>
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%esi
lea    0xe33(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%esi
lea    0xe34(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
mov    $0x186a0,%esi
call   1070 <start_periodic_timer@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1213 <main+0x83>
lea    0xe1a(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%eax
add    $0x30,%rsp
pop    %rbp
ret
jmp    1218 <main+0x88>
mov    -0x28(%rbp),%rdi
call   1080 <wait_next_activation@plt>
mov    $0x0,%al
call   10b0 <gettime@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x64,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
call   1040 <job_body@plt>
mov    $0x0,%al
call   10b0 <gettime@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
sub    -0x18(%rbp),%esi
mov    %esi,-0x2c(%rbp)
lea    0xdb4(%rip),%rdi
call   1050 <printf@plt>
jmp    1218 <main+0x88>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1030 <make_rt@plt>
xor    %eax,%eax
call   1090 <cpu_speed@plt>
mov    %eax,%ebp
imul   $0x64,%eax,%edi
call   1060 <calibrate@plt>
mov    %eax,%ebx
lea    0xe48(%rip),%rdi
mov    %ebp,%esi
call   1050 <printf@plt>
lea    0xe4a(%rip),%rdi
mov    %ebx,%esi
call   1050 <printf@plt>
xor    %edi,%edi
mov    $0x186a0,%esi
call   1070 <start_periodic_timer@plt>
test   %rax,%rax
je     122d <main+0x9d>
mov    %rax,%r14
movslq %ebx,%rax
imul   $0x51eb851f,%rax,%rbx
mov    %rbx,%rax
shr    $0x3f,%rax
sar    $0x25,%rbx
add    %eax,%ebx
lea    0xe0c(%rip),%r15
nop
mov    %r14,%rdi
call   1080 <wait_next_activation@plt>
xor    %eax,%eax
call   10b0 <gettime@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <job_body@plt>
xor    %eax,%eax
call   10b0 <gettime@plt>
sub    %ebp,%eax
mov    %r15,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
jmp    1200 <main+0x70>
lea    0xde9(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.periodic_task = type { i32 }
@.str = [16 x i8] c"CPU Speed: %Lu\0A\00"
@.str.1 = [13 x i8] c"Cycles: %Lu\0A\00"
@.str.2 = [21 x i8] c"Start Periodic Timer\00"
@.str.3 = [5 x i8] c"%Lu\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.periodic_task*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32 (...) @make_rt()
%12 = call i32 (...) @cpu_speed()
store i32 %12, i32* %9
%13 = load i32, i32* %9
%14 = mul nsw i32 100, %13
%15 = call i32 @calibrate(i32 %14)
store i32 %15, i32* %6
%16 = load i32, i32* %9
%17 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = load i32, i32* %6
%19 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = call %struct.periodic_task* @start_periodic_timer(i32 0, i32 100000)
store %struct.periodic_task* %20, %struct.periodic_task** %10
%21 = load %struct.periodic_task*, %struct.periodic_task** %10
%22 = icmp eq %struct.periodic_task* %21, null
br i1 %22, label %23, label %25
23: ; preds = %2
%24 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
ret i32 -1
25: ; preds = %2
br label %26
26: ; preds = %25, %26
%27 = load %struct.periodic_task*, %struct.periodic_task** %10
%28 = call i32 @wait_next_activation(%struct.periodic_task* %27)
%29 = call i32 (...) @gettime()
store i32 %29, i32* %7
%30 = load i32, i32* %6
%31 = sdiv i32 %30, 100
%32 = call i32 @job_body(i32 %31)
%33 = call i32 (...) @gettime()
store i32 %33, i32* %8
%34 = load i32, i32* %8
%35 = load i32, i32* %7
%36 = sub nsw i32 %34, %35
%37 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %36)
br label %26
}

LLVM IR (-O2):
%struct.periodic_task = type { i32 }
@.str = [16 x i8] c"CPU Speed: %Lu\0A\00"
@.str.1 = [13 x i8] c"Cycles: %Lu\0A\00"
@.str.2 = [21 x i8] c"Start Periodic Timer\00"
@.str.3 = [5 x i8] c"%Lu\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @make_rt()
%4 = tail call i32 (...) @cpu_speed()
%5 = mul nsw i32 %4, 100
%6 = tail call i32 @calibrate(i32 %5)
%7 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %4)
%8 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%9 = tail call %struct.periodic_task* @start_periodic_timer(i32 0, i32 100000)
%10 = icmp eq %struct.periodic_task* %9, null
br i1 %10, label %13, label %11
11: ; preds = %2
%12 = sdiv i32 %6, 100
br label %15
13: ; preds = %2
%14 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
ret i32 -1
15: ; preds = %11, %15
%16 = tail call i32 @wait_next_activation(%struct.periodic_task* %9)
%17 = tail call i32 (...) @gettime()
%18 = tail call i32 @job_body(i32 %12)
%19 = tail call i32 (...) @gettime()
%20 = sub nsw i32 %19, %17
%21 = tail call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %20)
br label %15
}