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

/* Variables and functions */
int /*<<< orphan*/ assert(int);
scalar_t__ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int wait_for_timer(double *wait_seconds, uint64_t *tick) {
  const int ticks_per_second = 3;
  const uint64_t half_tick_usec = 1000000 / (ticks_per_second * 2);
  struct timeval tv;
  assert(gettimeofday(&tv, NULL) == 0);
  uint64_t current_tick =
      (tv.tv_sec * (ticks_per_second * 2) + (tv.tv_usec / half_tick_usec) + 1) /
      2;
  if (current_tick == *tick) {
    *wait_seconds =
        ((double)(half_tick_usec - (tv.tv_usec % half_tick_usec))) / 1e6;
    return 1;
  } else {
    *tick = current_tick;
    return 0;
  }
}


x86 assembly (clang, -O0):
<wait_for_timer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x3,-0x1c(%rbp)
movl   $0x28b0a,-0x20(%rbp)
lea    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
imul   $0x6,-0x28(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x24(%rbp),%eax
mov    $0x28b0a,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%eax
jne    11c8 <wait_for_timer+0xa8>
mov    -0x24(%rbp),%eax
mov    $0x28b0a,%ecx
cltd
idiv   %ecx
mov    $0x28b0a,%eax
sub    %edx,%eax
cvtsi2sd %eax,%xmm0
movsd  0xe50(%rip),%xmm1
divsd  %xmm1,%xmm0
mov    -0x10(%rbp),%rax
movsd  %xmm0,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    11d8 <wait_for_timer+0xb8>
mov    -0x2c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_timer>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
xor    %r15d,%r15d
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1040 <assert@plt>
mov    0x8(%rsp),%eax
lea    (%rax,%rax,2),%edx
movslq 0xc(%rsp),%rax
imul   $0xc953d05,%rax,%rcx
mov    %rcx,%rsi
shr    $0x3f,%rsi
sar    $0x2d,%rcx
add    %esi,%ecx
lea    (%rcx,%rdx,2),%esi
add    $0x1,%esi
lea    (%rcx,%rdx,2),%edx
shr    $0x1f,%esi
add    %esi,%edx
add    $0x1,%edx
sar    %edx
cmp    (%rbx),%edx
jne    11ab <wait_for_timer+0x8b>
imul   $0x28b0a,%ecx,%ecx
sub    %ecx,%eax
mov    $0x28b0a,%ecx
sub    %eax,%ecx
cvtsi2sd %ecx,%xmm0
divsd  0xe62(%rip),%xmm0
movsd  %xmm0,(%r14)
mov    $0x1,%r15d
jmp    11ad <wait_for_timer+0x8d>
mov    %edx,(%rbx)
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
define i32 @wait_for_timer(double* %0, i32* %1) {
%3 = alloca i32
%4 = alloca double*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.timeval
%9 = alloca i32
store double* %0, double** %4
store i32* %1, i32** %5
store i32 3, i32* %6
store i32 166666, i32* %7
%10 = call i64 @gettimeofday(%struct.timeval* %8, i32* null)
%11 = icmp eq i64 %10, 0
%12 = zext i1 %11 to i32
%13 = call i32 @assert(i32 %12)
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
%15 = load i32, i32* %14
%16 = mul nsw i32 %15, 6
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
%18 = load i32, i32* %17
%19 = sdiv i32 %18, 166666
%20 = add nsw i32 %16, %19
%21 = add nsw i32 %20, 1
%22 = sdiv i32 %21, 2
store i32 %22, i32* %9
%23 = load i32, i32* %9
%24 = load i32*, i32** %5
%25 = load i32, i32* %24
%26 = icmp eq i32 %23, %25
br i1 %26, label %27, label %35
27: ; preds = %2
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
%29 = load i32, i32* %28
%30 = srem i32 %29, 166666
%31 = sub nsw i32 166666, %30
%32 = sitofp i32 %31 to double
%33 = fdiv double %32, 1.000000e+06
%34 = load double*, double** %4
store double %33, double* %34
store i32 1, i32* %3
br label %38
35: ; preds = %2
%36 = load i32, i32* %9
%37 = load i32*, i32** %5
store i32 %36, i32* %37
store i32 0, i32* %3
br label %38
38: ; preds = %35, %27
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
define i32 @wait_for_timer(double* nocapture writeonly %0, i32* nocapture %1) {
%3 = alloca %struct.timeval
%4 = bitcast %struct.timeval* %3 to i8*
%5 = call i64 @gettimeofday(%struct.timeval* %3, i32* null)
%6 = icmp eq i64 %5, 0
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%10 = load i32, i32* %9
%11 = mul nsw i32 %10, 6
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%13 = load i32, i32* %12
%14 = sdiv i32 %13, 166666
%15 = srem i32 %13, 166666
%16 = or i32 %11, 1
%17 = add i32 %16, %14
%18 = sdiv i32 %17, 2
%19 = load i32, i32* %1
%20 = icmp eq i32 %18, %19
br i1 %20, label %21, label %25
21: ; preds = %2
%22 = sub nsw i32 166666, %15
%23 = sitofp i32 %22 to double
%24 = fdiv double %23, 1.000000e+06
store double %24, double* %0
br label %26
25: ; preds = %2
store i32 %18, i32* %1
br label %26
26: ; preds = %25, %21
%27 = phi i32 [ 1, %21 ], [ 0, %25 ]
ret i32 %27
}