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

/* Variables and functions */
unsigned long long arch_get_ticks();
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
unsigned long calibrating_loop() {
  struct timeval a;
  unsigned long long start, end;
  unsigned long mhz;
  gettimeofday(&a, NULL);
  start = arch_get_ticks();
  for (;;) {
    unsigned long usec;
    struct timeval b;
    gettimeofday(&b, NULL);
    usec = (b.tv_sec - a.tv_sec) * 1000000;
    usec += b.tv_usec - a.tv_usec;
    if (usec >= 1000000)
      break;
  }
  end = arch_get_ticks();
  mhz = end - start;
  return mhz;
}


x86 assembly (clang, -O0):
<calibrating_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    $0x0,%al
call   1040 <arch_get_ticks@plt>
mov    %rax,-0x18(%rbp)
lea    -0x40(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x40(%rbp),%eax
sub    -0x10(%rbp),%eax
imul   $0xf4240,%eax,%eax
cltq
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%rax
sub    -0x8(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0xf4240,-0x30(%rbp)
jb     1182 <calibrating_loop+0x62>
jmp    1187 <calibrating_loop+0x67>
jmp    1140 <calibrating_loop+0x20>
mov    $0x0,%al
call   1040 <arch_get_ticks@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
sub    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calibrating_loop>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0x18(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
xor    %eax,%eax
call   1040 <arch_get_ticks@plt>
mov    %rax,%r14
movslq 0x18(%rsp),%r12
mov    0x20(%rsp),%rbx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
movslq 0x8(%rsp),%rax
sub    %r12,%rax
imul   $0xf4240,%rax,%rax
mov    0x10(%rsp),%rcx
sub    %rbx,%rcx
add    %rax,%rcx
cmp    $0xf4240,%rcx
jb     1150 <calibrating_loop+0x30>
xor    %eax,%eax
call   1040 <arch_get_ticks@plt>
sub    %r14,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
define i64 @calibrating_loop() {
%1 = alloca %struct.timeval
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca %struct.timeval
%7 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
%8 = call i64 (...) @arch_get_ticks()
store i64 %8, i64* %2
br label %9
9: ; preds = %28, %0
%10 = call i32 @gettimeofday(%struct.timeval* %6, i32* null)
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%12 = load i32, i32* %11
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i32 0, i32 0
%14 = load i32, i32* %13
%15 = sub nsw i32 %12, %14
%16 = mul nsw i32 %15, 1000000
%17 = sext i32 %16 to i64
store i64 %17, i64* %5
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
%19 = load i64, i64* %18
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i32 0, i32 1
%21 = load i64, i64* %20
%22 = sub nsw i64 %19, %21
%23 = load i64, i64* %5
%24 = add i64 %23, %22
store i64 %24, i64* %5
%25 = load i64, i64* %5
%26 = icmp uge i64 %25, 1000000
br i1 %26, label %27, label %28
27: ; preds = %9
br label %29
28: ; preds = %9
br label %9
29: ; preds = %27
%30 = call i64 (...) @arch_get_ticks()
store i64 %30, i64* %3
%31 = load i64, i64* %3
%32 = load i64, i64* %2
%33 = sub i64 %31, %32
store i64 %33, i64* %4
%34 = load i64, i64* %4
ret i64 %34
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
define i64 @calibrating_loop() {
%1 = alloca %struct.timeval
%2 = alloca %struct.timeval
%3 = bitcast %struct.timeval* %1 to i8*
%4 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
%5 = tail call i64 (...) @arch_get_ticks()
%6 = bitcast %struct.timeval* %2 to i8*
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%9 = load i32, i32* %8
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%12 = load i64, i64* %11
br label %13
13: ; preds = %13, %0
%14 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%15 = load i32, i32* %7
%16 = sub nsw i32 %15, %9
%17 = mul nsw i32 %16, 1000000
%18 = sext i32 %17 to i64
%19 = load i64, i64* %10
%20 = sub i64 %19, %12
%21 = add i64 %20, %18
%22 = icmp ugt i64 %21, 999999
br i1 %22, label %23, label %13
23: ; preds = %13
%24 = tail call i64 (...) @arch_get_ticks()
%25 = sub i64 %24, %5
ret i64 %25
}