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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC_RAW;
scalar_t__ clock_gettime(int /*<<< orphan*/, struct timespec *);
void cmn_delay_us(int us) {
  struct timespec tstart, tstop;
  time_t sec;
  long nsec;
  if (clock_gettime(CLOCK_MONOTONIC_RAW, &tstart) != 0)
    return;
  sec = tstart.tv_sec;
  nsec = tstart.tv_nsec + us * 1000;
  if (nsec >= 1000000000) {
    sec++;
    nsec -= 1000000000;
  }
  while (clock_gettime(CLOCK_MONOTONIC_RAW, &tstop) == 0) {
    if (tstop.tv_sec > sec)
      break;
    else if (tstop.tv_sec == sec) {
      if (tstop.tv_nsec >= nsec)
        break;
    }
  }
}


x86 assembly (clang, -O0):
<cmn_delay_us>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2ec6(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0x0,%rax
je     113c <cmn_delay_us+0x2c>
jmp    11e2 <cmn_delay_us+0xd2>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%eax
imul   $0x3e8,-0x4(%rbp),%ecx
add    %ecx,%eax
cltq
mov    %rax,-0x38(%rbp)
cmpq   $0x3b9aca00,-0x38(%rbp)
3b
jl     117e <cmn_delay_us+0x6e>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%rax
sub    $0x3b9aca00,%rax
mov    %rax,-0x38(%rbp)
jmp    1183 <cmn_delay_us+0x73>
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0x0,%rax
jne    11e2 <cmn_delay_us+0xd2>
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jle    11b2 <cmn_delay_us+0xa2>
jmp    11e2 <cmn_delay_us+0xd2>
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jne    11d8 <cmn_delay_us+0xc8>
movslq -0x20(%rbp),%rax
cmp    -0x38(%rbp),%rax
jl     11d3 <cmn_delay_us+0xc3>
jmp    11e2 <cmn_delay_us+0xd2>
jmp    11d8 <cmn_delay_us+0xc8>
jmp    11dd <cmn_delay_us+0xcd>
jmp    1183 <cmn_delay_us+0x73>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmn_delay_us>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebx
mov    0x2ec4(%rip),%r14
mov    (%r14),%edi
lea    0x18(%rsp),%rsi
call   1030 <clock_gettime@plt>
test   %rax,%rax
je     1142 <cmn_delay_us+0x32>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
movslq 0x20(%rsp),%rax
movslq %ebx,%rcx
imul   $0x3e8,%rcx,%rcx
lea    (%rax,%rcx,1),%rdx
xor    %ebx,%ebx
cmp    $0x3b9aca00,%edx
lea    -0x3b9aca00(%rax,%rcx,1),%r12
c4
cmovl  %rdx,%r12
setge  %bl
add    0x18(%rsp),%rbx
lea    0x8(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
mov    (%r14),%edi
mov    %r15,%rsi
call   1030 <clock_gettime@plt>
test   %rax,%rax
jne    1136 <cmn_delay_us+0x26>
cmp    %rbx,0x8(%rsp)
jg     1136 <cmn_delay_us+0x26>
jne    1180 <cmn_delay_us+0x70>
movslq 0x10(%rsp),%rax
cmp    %rax,%r12
jg     1180 <cmn_delay_us+0x70>
jmp    1136 <cmn_delay_us+0x26>

LLVM IR (-O0):
%struct.timespec = type { i64, i32 }
@CLOCK_MONOTONIC_RAW = global i32 0
define void @cmn_delay_us(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = alloca i64
%6 = alloca i64
store i32 %0, i32* %2
%7 = load i32, i32* @CLOCK_MONOTONIC_RAW
%8 = call i64 @clock_gettime(i32 %7, %struct.timespec* %3)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %1
br label %53
11: ; preds = %1
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%13 = load i64, i64* %12
store i64 %13, i64* %5
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%15 = load i32, i32* %14
%16 = load i32, i32* %2
%17 = mul nsw i32 %16, 1000
%18 = add nsw i32 %15, %17
%19 = sext i32 %18 to i64
store i64 %19, i64* %6
%20 = load i64, i64* %6
%21 = icmp sge i64 %20, 1000000000
br i1 %21, label %22, label %27
22: ; preds = %11
%23 = load i64, i64* %5
%24 = add nsw i64 %23, 1
store i64 %24, i64* %5
%25 = load i64, i64* %6
%26 = sub nsw i64 %25, 1000000000
store i64 %26, i64* %6
br label %27
27: ; preds = %22, %11
br label %28
28: ; preds = %52, %27
%29 = load i32, i32* @CLOCK_MONOTONIC_RAW
%30 = call i64 @clock_gettime(i32 %29, %struct.timespec* %4)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %53
32: ; preds = %28
%33 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%34 = load i64, i64* %33
%35 = load i64, i64* %5
%36 = icmp sgt i64 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %32
br label %53
38: ; preds = %32
%39 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%40 = load i64, i64* %39
%41 = load i64, i64* %5
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %51
43: ; preds = %38
%44 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%45 = load i32, i32* %44
%46 = sext i32 %45 to i64
%47 = load i64, i64* %6
%48 = icmp sge i64 %46, %47
br i1 %48, label %49, label %50
49: ; preds = %43
br label %53
50: ; preds = %43
br label %51
51: ; preds = %50, %38
br label %52
52: ; preds = %51
br label %28
53: ; preds = %10, %49, %37, %28
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i64, i32 }
@CLOCK_MONOTONIC_RAW = global i32 0
define void @cmn_delay_us(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_RAW
%7 = call i64 @clock_gettime(i32 %6, %struct.timespec* %2)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %37
9: ; preds = %1
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%11 = load i64, i64* %10
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%13 = load i32, i32* %12
%14 = mul nsw i32 %0, 1000
%15 = add nsw i32 %13, %14
%16 = sext i32 %15 to i64
%17 = icmp sgt i32 %15, 999999999
%18 = add nsw i64 %16, -1000000000
%19 = zext i1 %17 to i64
%20 = add nsw i64 %11, %19
%21 = select i1 %17, i64 %18, i64 %16
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
br label %24
24: ; preds = %31, %9
%25 = load i32, i32* @CLOCK_MONOTONIC_RAW
%26 = call i64 @clock_gettime(i32 %25, %struct.timespec* %3)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %37
28: ; preds = %24
%29 = load i64, i64* %22
%30 = icmp sgt i64 %29, %20
br i1 %30, label %37, label %31
31: ; preds = %28
%32 = icmp ne i64 %29, %20
%33 = load i32, i32* %23
%34 = sext i32 %33 to i64
%35 = icmp sgt i64 %21, %34
%36 = select i1 %32, i1 true, i1 %35
br i1 %36, label %24, label %37
37: ; preds = %24, %28, %31, %1
ret void
}