#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 timezone {
  int dummy;
};
struct timeval {
  int tv_sec;
  int tv_usec;
};
struct timespec {
  int tv_sec;
  int tv_nsec;
};

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ gettimeofday(struct timeval *, struct timezone *);
void timespec_delay(struct timespec *t, double incr) {
  struct timeval tv;
  gettimeofday(&tv, (struct timezone *)NULL);
  t->tv_sec = tv.tv_sec;
  t->tv_nsec = tv.tv_usec * 1000 + 999;
  long nsec = t->tv_nsec + (long)(incr * 1000000000.0);
  if (nsec >= 1000000000) {
    t->tv_sec += 1;
    nsec -= 1000000000;
    assert(nsec < 1000000000);
  }
  t->tv_nsec = nsec;
}


x86 assembly (clang, -O0):
<timespec_delay>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x18(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
imul   $0x3e8,-0x14(%rbp),%ecx
add    $0x3e7,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x8(%rbp),%rax
movslq 0x4(%rax),%rax
movsd  0xe95(%rip),%xmm0
mulsd  -0x10(%rbp),%xmm0
cvttsd2si %xmm0,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x3b9aca00,-0x20(%rbp)
3b
jl     11b8 <timespec_delay+0x98>
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
sub    $0x3b9aca00,%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x3b9aca00,-0x20(%rbp)
3b
setl   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x20(%rbp),%rax
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x4(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timespec_delay>:
push   %r14
push   %rbx
sub    $0x18,%rsp
movsd  %xmm0,0x10(%rsp)
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    0x8(%rsp),%eax
mov    %eax,(%rbx)
movslq 0xc(%rsp),%rcx
imul   $0x3e8,%rcx,%rcx
lea    0x3e7(%rcx),%rdx
mov    %edx,0x4(%rbx)
movsd  0x10(%rsp),%xmm0
mulsd  0xe9a(%rip),%xmm0
cvttsd2si %xmm0,%rdx
add    %rdx,%rcx
add    $0x3e7,%rcx
cmp    $0x3b9aca00,%rcx
jl     119f <timespec_delay+0x7f>
add    $0x1,%eax
mov    %eax,(%rbx)
lea    -0x3b9aca00(%rcx),%r14
xor    %edi,%edi
cmp    $0x77359400,%rcx
setb   %dil
call   1040 <assert@plt>
mov    %r14,%rcx
mov    %ecx,0x4(%rbx)
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
%struct.timeval = type { i32, i32 }
%struct.timezone = type { i32 }
define void @timespec_delay(%struct.timespec* %0, double %1) {
%3 = alloca %struct.timespec*
%4 = alloca double
%5 = alloca %struct.timeval
%6 = alloca i64
store %struct.timespec* %0, %struct.timespec** %3
store double %1, double* %4
%7 = call i32 @gettimeofday(%struct.timeval* %5, %struct.timezone* null)
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%9 = load i32, i32* %8
%10 = load %struct.timespec*, %struct.timespec** %3
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %10, i32 0, i32 0
store i32 %9, i32* %11
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%13 = load i32, i32* %12
%14 = mul nsw i32 %13, 1000
%15 = add nsw i32 %14, 999
%16 = load %struct.timespec*, %struct.timespec** %3
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %16, i32 0, i32 1
store i32 %15, i32* %17
%18 = load %struct.timespec*, %struct.timespec** %3
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %18, i32 0, i32 1
%20 = load i32, i32* %19
%21 = sext i32 %20 to i64
%22 = load double, double* %4
%23 = fmul double %22, 1.000000e+09
%24 = fptosi double %23 to i64
%25 = add nsw i64 %21, %24
store i64 %25, i64* %6
%26 = load i64, i64* %6
%27 = icmp sge i64 %26, 1000000000
br i1 %27, label %28, label %39
28: ; preds = %2
%29 = load %struct.timespec*, %struct.timespec** %3
%30 = getelementptr inbounds %struct.timespec, %struct.timespec* %29, i32 0, i32 0
%31 = load i32, i32* %30
%32 = add nsw i32 %31, 1
store i32 %32, i32* %30
%33 = load i64, i64* %6
%34 = sub nsw i64 %33, 1000000000
store i64 %34, i64* %6
%35 = load i64, i64* %6
%36 = icmp slt i64 %35, 1000000000
%37 = zext i1 %36 to i32
%38 = call i32 @assert(i32 %37)
br label %39
39: ; preds = %28, %2
%40 = load i64, i64* %6
%41 = trunc i64 %40 to i32
%42 = load %struct.timespec*, %struct.timespec** %3
%43 = getelementptr inbounds %struct.timespec, %struct.timespec* %42, i32 0, i32 1
store i32 %41, i32* %43
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
%struct.timeval = type { i32, i32 }
%struct.timezone = type { i32 }
define void @timespec_delay(%struct.timespec* nocapture %0, double %1) {
%3 = alloca %struct.timeval
%4 = bitcast %struct.timeval* %3 to i8*
%5 = call i32 @gettimeofday(%struct.timeval* %3, %struct.timezone* null)
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%7 = load i32, i32* %6
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 0
store i32 %7, i32* %8
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%10 = load i32, i32* %9
%11 = mul nsw i32 %10, 1000
%12 = add nsw i32 %11, 999
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 1
store i32 %12, i32* %13
%14 = sext i32 %12 to i64
%15 = fmul double %1, 1.000000e+09
%16 = fptosi double %15 to i64
%17 = add nsw i64 %14, %16
%18 = icmp sgt i64 %17, 999999999
br i1 %18, label %19, label %25
19: ; preds = %2
%20 = add nsw i32 %7, 1
store i32 %20, i32* %8
%21 = add nsw i64 %17, -1000000000
%22 = icmp ult i64 %17, 2000000000
%23 = zext i1 %22 to i32
%24 = tail call i32 @assert(i32 %23)
br label %25
25: ; preds = %19, %2
%26 = phi i64 [ %21, %19 ], [ %17, %2 ]
%27 = trunc i64 %26 to i32
store i32 %27, i32* %13
ret void
}