#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 {
  unsigned long long tv_sec;
  scalar_t__ tv_nsec;
};
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ __NR_getpid;
int /*<<< orphan*/ assert(int);
scalar_t__ clock_gettime(int /*<<< orphan*/, struct timespec *);
scalar_t__ getpid();
int /*<<< orphan*/ printf(char *, int);
scalar_t__ syscall(int /*<<< orphan*/);
unsigned long long calibrate(void) {
  struct timespec start, finish;
  unsigned long long i, samples, step = 9973;
  pid_t pid, ret;
  int seconds = 15;
  printf("Calibrating sample size for %d seconds worth of syscalls ...\n",
         seconds);
  samples = 0;
  pid = getpid();
  assert(clock_gettime(CLOCK_MONOTONIC, &start) == 0);
  do {
    for (i = 0; i < step; i++) {
      ret = syscall(__NR_getpid);
      assert(pid == ret);
    }
    assert(clock_gettime(CLOCK_MONOTONIC, &finish) == 0);
    samples += step;
    i = finish.tv_sec - start.tv_sec;
    i *= 1000000000ULL;
    i += finish.tv_nsec - start.tv_nsec;
  } while (i < 1000000000ULL);
  return samples * seconds;
}


x86 assembly (clang, -O0):
<calibrate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movq   $0x26f5,-0x38(%rbp)
movl   $0xf,-0x4c(%rbp)
mov    -0x4c(%rbp),%esi
lea    0xe8f(%rip),%rdi
call   1050 <printf@plt>
movq   $0x0,-0x30(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,-0x40(%rbp)
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x38(%rbp),%rax
jae    11fa <calibrate+0xaa>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
call   1070 <syscall@plt>
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x48(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11b4 <calibrate+0x64>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x38(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
sub    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
imul   $0x3b9aca00,-0x28(%rbp),%rax
3b
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
sub    -0x8(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x3b9aca00,-0x28(%rbp)
3b
jb     11ac <calibrate+0x5c>
mov    -0x30(%rbp),%rax
movslq -0x4c(%rbp),%rcx
imul   %rcx,%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calibrate>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0xe9b(%rip),%rdi
mov    $0xf,%esi
call   1050 <printf@plt>
xor    %r15d,%r15d
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %rax,%rbx
mov    0x2e65(%rip),%r12
mov    (%r12),%edi
lea    0x18(%rsp),%rsi
call   1030 <clock_gettime@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1060 <assert@plt>
mov    0x2e3a(%rip),%r13
lea    0x8(%rsp),%r14
nopl   0x0(%rax,%rax,1)
mov    $0x26f5,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x0(%r13),%edi
call   1070 <syscall@plt>
xor    %edi,%edi
cmp    %rax,%rbx
sete   %dil
call   1060 <assert@plt>
add    $0xffffffffffffffff,%rbp
jne    11c0 <calibrate+0x70>
mov    (%r12),%edi
mov    %r14,%rsi
call   1030 <clock_gettime@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1060 <assert@plt>
add    $0x26f5,%r15
mov    0x8(%rsp),%rax
mov    0x10(%rsp),%rcx
sub    0x18(%rsp),%rax
imul   $0x3b9aca00,%rax,%rax
sub    0x20(%rsp),%rcx
add    %rax,%rcx
cmp    $0x3b9aca00,%rcx
jb     11b0 <calibrate+0x60>
lea    (%r15,%r15,4),%rax
lea    (%rax,%rax,2),%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@.str = [62 x i8] c"Calibrating sample size for %d seconds worth of syscalls ...\0A\00"
@CLOCK_MONOTONIC = global i32 0
@__NR_getpid = global i32 0
define i64 @calibrate() {
%1 = alloca %struct.timespec
%2 = alloca %struct.timespec
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
store i64 9973, i64* %5
store i32 15, i32* %8
%9 = load i32, i32* %8
%10 = call i32 @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str, i64 0, i64 0), i32 %9)
store i64 0, i64* %4
%11 = call i64 (...) @getpid()
store i64 %11, i64* %6
%12 = load i32, i32* @CLOCK_MONOTONIC
%13 = call i64 @clock_gettime(i32 %12, %struct.timespec* %1)
%14 = icmp eq i64 %13, 0
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
br label %17
17: ; preds = %56, %0
store i64 0, i64* %3
br label %18
18: ; preds = %30, %17
%19 = load i64, i64* %3
%20 = load i64, i64* %5
%21 = icmp ult i64 %19, %20
br i1 %21, label %22, label %33
22: ; preds = %18
%23 = load i32, i32* @__NR_getpid
%24 = call i64 @syscall(i32 %23)
store i64 %24, i64* %7
%25 = load i64, i64* %6
%26 = load i64, i64* %7
%27 = icmp eq i64 %25, %26
%28 = zext i1 %27 to i32
%29 = call i32 @assert(i32 %28)
br label %30
30: ; preds = %22
%31 = load i64, i64* %3
%32 = add i64 %31, 1
store i64 %32, i64* %3
br label %18
33: ; preds = %18
%34 = load i32, i32* @CLOCK_MONOTONIC
%35 = call i64 @clock_gettime(i32 %34, %struct.timespec* %2)
%36 = icmp eq i64 %35, 0
%37 = zext i1 %36 to i32
%38 = call i32 @assert(i32 %37)
%39 = load i64, i64* %5
%40 = load i64, i64* %4
%41 = add i64 %40, %39
store i64 %41, i64* %4
%42 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 0
%43 = load i64, i64* %42
%44 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i32 0, i32 0
%45 = load i64, i64* %44
%46 = sub i64 %43, %45
store i64 %46, i64* %3
%47 = load i64, i64* %3
%48 = mul i64 %47, 1000000000
store i64 %48, i64* %3
%49 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 1
%50 = load i64, i64* %49
%51 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i32 0, i32 1
%52 = load i64, i64* %51
%53 = sub nsw i64 %50, %52
%54 = load i64, i64* %3
%55 = add i64 %54, %53
store i64 %55, i64* %3
br label %56
56: ; preds = %33
%57 = load i64, i64* %3
%58 = icmp ult i64 %57, 1000000000
br i1 %58, label %17, label %59
59: ; preds = %56
%60 = load i64, i64* %4
%61 = load i32, i32* %8
%62 = sext i32 %61 to i64
%63 = mul i64 %60, %62
ret i64 %63
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@.str = [62 x i8] c"Calibrating sample size for %d seconds worth of syscalls ...\0A\00"
@CLOCK_MONOTONIC = global i32 0
@__NR_getpid = global i32 0
define i64 @calibrate() {
%1 = alloca %struct.timespec
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %1 to i8*
%4 = bitcast %struct.timespec* %2 to i8*
%5 = tail call i32 @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str, i64 0, i64 0), i32 15)
%6 = tail call i64 (...) @getpid()
%7 = load i32, i32* @CLOCK_MONOTONIC
%8 = call i64 @clock_gettime(i32 %7, %struct.timespec* %1)
%9 = icmp eq i64 %8, 0
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
br label %16
16: ; preds = %27, %0
%17 = phi i64 [ 0, %0 ], [ %33, %27 ]
br label %18
18: ; preds = %16, %18
%19 = phi i64 [ 0, %16 ], [ %25, %18 ]
%20 = load i32, i32* @__NR_getpid
%21 = call i64 @syscall(i32 %20)
%22 = icmp eq i64 %6, %21
%23 = zext i1 %22 to i32
%24 = call i32 @assert(i32 %23)
%25 = add nuw nsw i64 %19, 1
%26 = icmp eq i64 %25, 9973
br i1 %26, label %27, label %18
27: ; preds = %18
%28 = load i32, i32* @CLOCK_MONOTONIC
%29 = call i64 @clock_gettime(i32 %28, %struct.timespec* %2)
%30 = icmp eq i64 %29, 0
%31 = zext i1 %30 to i32
%32 = call i32 @assert(i32 %31)
%33 = add i64 %17, 9973
%34 = load i64, i64* %12
%35 = load i64, i64* %13
%36 = sub i64 %34, %35
%37 = mul i64 %36, 1000000000
%38 = load i64, i64* %14
%39 = load i64, i64* %15
%40 = sub i64 %38, %39
%41 = add i64 %40, %37
%42 = icmp ult i64 %41, 1000000000
br i1 %42, label %16, label %43
43: ; preds = %27
%44 = mul i64 %33, 15
ret i64 %44
}