#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 timespec {
  int tv_sec;
  int tv_nsec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ nanosleep(struct timespec *, int /*<<< orphan*/ *);
void bcm2835_delayMicroseconds(unsigned int micros) {
  struct timespec t0, t1;
  double t_us;
  clock_gettime(CLOCK_MONOTONIC, &t0);
  if (micros > 450) {
    t1.tv_sec = 0;
    t1.tv_nsec = 1000 * (long)(micros - 200);
    nanosleep(&t1, NULL);
  }
  while (1) {
    clock_gettime(CLOCK_MONOTONIC, &t1);
    t_us = (t1.tv_sec - t0.tv_sec) * 1e6 + (t1.tv_nsec - t0.tv_nsec) * 1e-3;
    if (t_us >= micros)
      break;
  }
}


x86 assembly (clang, -O0):
<bcm2835_delayMicroseconds>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2eb6(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmpl   $0x1c2,-0x4(%rbp)
jbe    1172 <bcm2835_delayMicroseconds+0x52>
movl   $0x0,-0x18(%rbp)
mov    -0x4(%rbp),%eax
sub    $0xc8,%eax
mov    %eax,%eax
imul   $0x3e8,%rax,%rax
mov    %eax,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <nanosleep@plt>
jmp    1177 <bcm2835_delayMicroseconds+0x57>
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%eax
mov    -0x10(%rbp),%esi
mov    -0xc(%rbp),%ecx
sub    %esi,%edx
cvtsi2sd %edx,%xmm0
sub    %ecx,%eax
cvtsi2sd %eax,%xmm1
movsd  0xe57(%rip),%xmm2
mulsd  %xmm2,%xmm1
movsd  0xe53(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
mov    -0x4(%rbp),%eax
cvtsi2sd %rax,%xmm1
ucomisd %xmm1,%xmm0
jb     11de <bcm2835_delayMicroseconds+0xbe>
jmp    11e3 <bcm2835_delayMicroseconds+0xc3>
jmp    1177 <bcm2835_delayMicroseconds+0x57>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bcm2835_delayMicroseconds>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
mov    0x2eb8(%rip),%r14
mov    (%r14),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0x1c3,%ebx
jb     1165 <bcm2835_delayMicroseconds+0x45>
movl   $0x0,(%rsp)
imul   $0x3e8,%ebx,%eax
add    $0xfffcf2c0,%eax
mov    %eax,0x4(%rsp)
mov    %rsp,%rdi
xor    %esi,%esi
call   1040 <nanosleep@plt>
mov    %ebx,%eax
cvtsi2sd %rax,%xmm0
movsd  %xmm0,0x10(%rsp)
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r14),%edi
mov    %rbx,%rsi
call   1030 <clock_gettime@plt>
mov    (%rsp),%eax
mov    0x4(%rsp),%ecx
sub    0x8(%rsp),%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
sub    0xc(%rsp),%ecx
xorps  %xmm1,%xmm1
cvtsi2sd %ecx,%xmm1
mulsd  0xe50(%rip),%xmm1
mulsd  0xe50(%rip),%xmm0
addsd  %xmm1,%xmm0
ucomisd 0x10(%rsp),%xmm0
jb     1180 <bcm2835_delayMicroseconds+0x60>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@CLOCK_MONOTONIC = global i32 0
define void @bcm2835_delayMicroseconds(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = alloca double
store i32 %0, i32* %2
%6 = load i32, i32* @CLOCK_MONOTONIC
%7 = call i32 @clock_gettime(i32 %6, %struct.timespec* %3)
%8 = load i32, i32* %2
%9 = icmp ugt i32 %8, 450
br i1 %9, label %10, label %19
10: ; preds = %1
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store i32 0, i32* %11
%12 = load i32, i32* %2
%13 = sub i32 %12, 200
%14 = zext i32 %13 to i64
%15 = mul nsw i64 1000, %14
%16 = trunc i64 %15 to i32
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i32 %16, i32* %17
%18 = call i32 @nanosleep(%struct.timespec* %4, i32* null)
br label %19
19: ; preds = %10, %1
br label %20
20: ; preds = %19, %42
%21 = load i32, i32* @CLOCK_MONOTONIC
%22 = call i32 @clock_gettime(i32 %21, %struct.timespec* %4)
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%26 = load i32, i32* %25
%27 = sub nsw i32 %24, %26
%28 = sitofp i32 %27 to double
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%32 = load i32, i32* %31
%33 = sub nsw i32 %30, %32
%34 = sitofp i32 %33 to double
%35 = fmul double %34, 1.000000e-03
%36 = call double @llvm.fmuladd.f64(double %28, double 1.000000e+06, double %35)
store double %36, double* %5
%37 = load double, double* %5
%38 = load i32, i32* %2
%39 = uitofp i32 %38 to double
%40 = fcmp oge double %37, %39
br i1 %40, label %41, label %42
41: ; preds = %20
br label %43
42: ; preds = %20
br label %20
43: ; preds = %41
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@CLOCK_MONOTONIC = global i32 0
define void @bcm2835_delayMicroseconds(i32 %0) {
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = bitcast %struct.timespec* %2 to i8*
%5 = bitcast %struct.timespec* %3 to i8*
%6 = load i32, i32* @CLOCK_MONOTONIC
%7 = call i32 @clock_gettime(i32 %6, %struct.timespec* %2)
%8 = icmp ugt i32 %0, 450
br i1 %8, label %9, label %15
9: ; preds = %1
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
store i32 0, i32* %10
%11 = mul i32 %0, 1000
%12 = add i32 %11, -200000
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
store i32 %12, i32* %13
%14 = call i32 @nanosleep(%struct.timespec* %3, i32* null)
br label %15
15: ; preds = %9, %1
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%20 = uitofp i32 %0 to double
br label %21
21: ; preds = %21, %15
%22 = load i32, i32* @CLOCK_MONOTONIC
%23 = call i32 @clock_gettime(i32 %22, %struct.timespec* %3)
%24 = load i32, i32* %16
%25 = load i32, i32* %17
%26 = sub nsw i32 %24, %25
%27 = sitofp i32 %26 to double
%28 = load i32, i32* %18
%29 = load i32, i32* %19
%30 = sub nsw i32 %28, %29
%31 = sitofp i32 %30 to double
%32 = fmul double %31, 1.000000e-03
%33 = call double @llvm.fmuladd.f64(double %27, double 1.000000e+06, double %32)
%34 = fcmp ult double %33, %20
br i1 %34, label %21, label %35
35: ; preds = %21
ret void
}