#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;
  int tv_usec;
};

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
void setTimeIndex(int *index, struct timeval *position, double *result) {
  struct timeval tv2;
  gettimeofday(&tv2, NULL);
  int x = *index;
  if (x == 0) {
    x = 4;
  } else {
    x--;
  }
  struct timeval tv1 = position[x];
  double diff = (((tv2.tv_sec) * 1000 + (tv2.tv_usec) / 1000) -
                 ((tv1.tv_sec) * 1000 + (tv1.tv_usec) / 1000));
  if (*index < 5) {
    position[*index] = tv2;
    result[*index] = diff;
    (*index)++;
  } else {
    (*index) = 0;
    position[*index] = tv2;
    result[*index] = diff;
  }
}


x86 assembly (clang, -O0):
<setTimeIndex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    1150 <setTimeIndex+0x40>
movl   $0x4,-0x24(%rbp)
jmp    1159 <setTimeIndex+0x49>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x30(%rbp)
imul   $0x3e8,-0x20(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x40(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x3c(%rbp)
imul   $0x3e8,-0x30(%rbp),%ecx
mov    -0x2c(%rbp),%eax
mov    $0x3e8,%esi
cltd
idiv   %esi
mov    %eax,%edx
mov    -0x3c(%rbp),%eax
add    %edx,%ecx
sub    %ecx,%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x8(%rbp),%rax
cmpl   $0x5,(%rax)
jge    11f1 <setTimeIndex+0xe1>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
movslq (%rcx),%rcx
mov    -0x20(%rbp),%rdx
mov    %rdx,(%rax,%rcx,8)
movsd  -0x38(%rbp),%xmm0
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
movslq (%rcx),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
jmp    1223 <setTimeIndex+0x113>
mov    -0x8(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
movslq (%rcx),%rcx
mov    -0x20(%rbp),%rdx
mov    %rdx,(%rax,%rcx,8)
movsd  -0x38(%rbp),%xmm0
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
movslq (%rcx),%rcx
movsd  %xmm0,(%rax,%rcx,8)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setTimeIndex>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r15
mov    %rsp,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
movslq (%r15),%rax
mov    %eax,%ecx
sub    $0x1,%ecx
mov    $0x4,%edx
cmovae %ecx,%edx
movslq %edx,%rcx
movslq 0x4(%r12,%rcx,8),%rdx
mov    (%rsp),%esi
movslq 0x4(%rsp),%rdi
imul   $0x10624dd3,%rdi,%rdi
mov    %rdi,%rbx
shr    $0x3f,%rbx
sar    $0x26,%rdi
add    %ebx,%edi
imul   $0xffffffffef9db22d,%rdx,%rdx
mov    %rdx,%rbx
shr    $0x3f,%rbx
sar    $0x26,%rdx
add    %ebx,%edx
add    %edi,%edx
sub    (%r12,%rcx,8),%esi
imul   $0x3e8,%esi,%ecx
add    %edx,%ecx
cvtsi2sd %ecx,%xmm0
cmp    $0x4,%rax
jg     11a4 <setTimeIndex+0x94>
mov    (%rsp),%rcx
mov    %rcx,(%r12,%rax,8)
movslq (%r15),%rax
movsd  %xmm0,(%r14,%rax,8)
add    $0x1,%eax
mov    %eax,(%r15)
jmp    11bc <setTimeIndex+0xac>
movl   $0x0,(%r15)
mov    (%rsp),%rax
mov    %rax,(%r12)
movslq (%r15),%rax
movsd  %xmm0,(%r14,%rax,8)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
define void @setTimeIndex(i32* %0, %struct.timeval* %1, double* %2) {
%4 = alloca i32*
%5 = alloca %struct.timeval*
%6 = alloca double*
%7 = alloca %struct.timeval
%8 = alloca i32
%9 = alloca %struct.timeval
%10 = alloca double
store i32* %0, i32** %4
store %struct.timeval* %1, %struct.timeval** %5
store double* %2, double** %6
%11 = call i32 @gettimeofday(%struct.timeval* %7, i32* null)
%12 = load i32*, i32** %4
%13 = load i32, i32* %12
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %3
store i32 4, i32* %8
br label %20
17: ; preds = %3
%18 = load i32, i32* %8
%19 = add nsw i32 %18, -1
store i32 %19, i32* %8
br label %20
20: ; preds = %17, %16
%21 = load %struct.timeval*, %struct.timeval** %5
%22 = load i32, i32* %8
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %21, i64 %23
%25 = bitcast %struct.timeval* %9 to i8*
%26 = bitcast %struct.timeval* %24 to i8*
%27 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%28 = load i32, i32* %27
%29 = mul nsw i32 %28, 1000
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
%31 = load i32, i32* %30
%32 = sdiv i32 %31, 1000
%33 = add nsw i32 %29, %32
%34 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
%35 = load i32, i32* %34
%36 = mul nsw i32 %35, 1000
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
%38 = load i32, i32* %37
%39 = sdiv i32 %38, 1000
%40 = add nsw i32 %36, %39
%41 = sub nsw i32 %33, %40
%42 = sitofp i32 %41 to double
store double %42, double* %10
%43 = load i32*, i32** %4
%44 = load i32, i32* %43
%45 = icmp slt i32 %44, 5
br i1 %45, label %46, label %63
46: ; preds = %20
%47 = load %struct.timeval*, %struct.timeval** %5
%48 = load i32*, i32** %4
%49 = load i32, i32* %48
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds %struct.timeval, %struct.timeval* %47, i64 %50
%52 = bitcast %struct.timeval* %51 to i8*
%53 = bitcast %struct.timeval* %7 to i8*
%54 = load double, double* %10
%55 = load double*, double** %6
%56 = load i32*, i32** %4
%57 = load i32, i32* %56
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds double, double* %55, i64 %58
store double %54, double* %59
%60 = load i32*, i32** %4
%61 = load i32, i32* %60
%62 = add nsw i32 %61, 1
store i32 %62, i32* %60
br label %78
63: ; preds = %20
%64 = load i32*, i32** %4
store i32 0, i32* %64
%65 = load %struct.timeval*, %struct.timeval** %5
%66 = load i32*, i32** %4
%67 = load i32, i32* %66
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds %struct.timeval, %struct.timeval* %65, i64 %68
%70 = bitcast %struct.timeval* %69 to i8*
%71 = bitcast %struct.timeval* %7 to i8*
%72 = load double, double* %10
%73 = load double*, double** %6
%74 = load i32*, i32** %4
%75 = load i32, i32* %74
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds double, double* %73, i64 %76
store double %72, double* %77
br label %78
78: ; preds = %63, %46
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
define void @setTimeIndex(i32* nocapture %0, %struct.timeval* nocapture %1, double* nocapture writeonly %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to %struct.timeval*
%6 = bitcast i64* %4 to i8*
%7 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%8 = load i32, i32* %0
%9 = icmp eq i32 %8, 0
%10 = add nsw i32 %8, -1
%11 = select i1 %9, i32 4, i32 %10
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 %12, i32 0
%14 = load i32, i32* %13
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 %12, i32 1
%16 = load i32, i32* %15
%17 = bitcast i64* %4 to i32*
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
%20 = load i32, i32* %19
%21 = sdiv i32 %20, 1000
%22 = sdiv i32 %16, -1000
%23 = sub i32 %18, %14
%24 = mul i32 %23, 1000
%25 = add nsw i32 %21, %22
%26 = add i32 %25, %24
%27 = sitofp i32 %26 to double
%28 = icmp slt i32 %8, 5
br i1 %28, label %29, label %38
29: ; preds = %3
%30 = sext i32 %8 to i64
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 %30
%32 = bitcast %struct.timeval* %31 to i64*
%33 = load i64, i64* %4
store i64 %33, i64* %32
%34 = load i32, i32* %0
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds double, double* %2, i64 %35
store double %27, double* %36
%37 = add nsw i32 %34, 1
store i32 %37, i32* %0
br label %44
38: ; preds = %3
store i32 0, i32* %0
%39 = bitcast %struct.timeval* %1 to i64*
%40 = load i64, i64* %4
store i64 %40, i64* %39
%41 = load i32, i32* %0
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds double, double* %2, i64 %42
store double %27, double* %43
br label %44
44: ; preds = %38, %29
ret void
}