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

/* Variables and functions */
double difftime(int /*<<< orphan*/, int /*<<< orphan*/);
int calc_speed(const struct timeval *start, const struct timeval *stop,
               double count) {
  if (0 == count) {
    return 0;
  }
  double secs = difftime(stop->tv_sec, start->tv_sec);
  return count * 1000000 /
         (secs * 1000000 + (double)(stop->tv_usec - start->tv_usec));
}


x86 assembly (clang, -O0):
<calc_speed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movsd  %xmm0,-0x20(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x20(%rbp),%xmm0
jne    1145 <calc_speed+0x35>
jp     1145 <calc_speed+0x35>
movl   $0x0,-0x4(%rbp)
jmp    119d <calc_speed+0x8d>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%esi
call   1030 <difftime@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  0xe9b(%rip),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm1
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    -0x10(%rbp),%rcx
sub    (%rcx),%rax
cvtsi2sd %rax,%xmm2
movsd  0xe76(%rip),%xmm3
mulsd  %xmm3,%xmm1
addsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calc_speed>:
xor    %eax,%eax
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    111f <calc_speed+0xf>
jp     111f <calc_speed+0xf>
ret
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x8(%rsi),%edi
mov    0x8(%r14),%esi
movsd  %xmm0,(%rsp)
call   1030 <difftime@plt>
movsd  0xebe(%rip),%xmm1
mov    (%rbx),%rax
sub    (%r14),%rax
movsd  (%rsp),%xmm3
mulsd  %xmm1,%xmm3
cvtsi2sd %rax,%xmm2
mulsd  %xmm1,%xmm0
addsd  %xmm2,%xmm0
divsd  %xmm0,%xmm3
cvttsd2si %xmm3,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i32 }
define i32 @calc_speed(%struct.timeval* %0, %struct.timeval* %1, double %2) {
%4 = alloca i32
%5 = alloca %struct.timeval*
%6 = alloca %struct.timeval*
%7 = alloca double
%8 = alloca double
store %struct.timeval* %0, %struct.timeval** %5
store %struct.timeval* %1, %struct.timeval** %6
store double %2, double* %7
%9 = load double, double* %7
%10 = fcmp oeq double 0.000000e+00, %9
br i1 %10, label %11, label %12
11: ; preds = %3
store i32 0, i32* %4
br label %34
12: ; preds = %3
%13 = load %struct.timeval*, %struct.timeval** %6
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 1
%15 = load i32, i32* %14
%16 = load %struct.timeval*, %struct.timeval** %5
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %16, i32 0, i32 1
%18 = load i32, i32* %17
%19 = call double @difftime(i32 %15, i32 %18)
store double %19, double* %8
%20 = load double, double* %7
%21 = fmul double %20, 1.000000e+06
%22 = load double, double* %8
%23 = load %struct.timeval*, %struct.timeval** %6
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %23, i32 0, i32 0
%25 = load i64, i64* %24
%26 = load %struct.timeval*, %struct.timeval** %5
%27 = getelementptr inbounds %struct.timeval, %struct.timeval* %26, i32 0, i32 0
%28 = load i64, i64* %27
%29 = sub nsw i64 %25, %28
%30 = sitofp i64 %29 to double
%31 = call double @llvm.fmuladd.f64(double %22, double 1.000000e+06, double %30)
%32 = fdiv double %21, %31
%33 = fptosi double %32 to i32
store i32 %33, i32* %4
br label %34
34: ; preds = %12, %11
%35 = load i32, i32* %4
ret i32 %35
}

LLVM IR (-O2):
%struct.timeval = type { i64, i32 }
define i32 @calc_speed(%struct.timeval* nocapture %0, %struct.timeval* nocapture %1, double %2) {
%4 = fcmp oeq double %2, 0.000000e+00
br i1 %4, label %21, label %5
5: ; preds = %3
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%7 = load i32, i32* %6
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%9 = load i32, i32* %8
%10 = tail call double @difftime(i32 %7, i32 %9)
%11 = fmul double %2, 1.000000e+06
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%13 = load i64, i64* %12
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
%15 = load i64, i64* %14
%16 = sub nsw i64 %13, %15
%17 = sitofp i64 %16 to double
%18 = tail call double @llvm.fmuladd.f64(double %10, double 1.000000e+06, double %17)
%19 = fdiv double %11, %18
%20 = fptosi double %19 to i32
br label %21
21: ; preds = %3, %5
%22 = phi i32 [ %20, %5 ], [ 0, %3 ]
ret i32 %22
}