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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
scalar_t__ nanosleep(struct timespec *, struct timespec *);
void upm_delay_us(unsigned int time) {
  if (time <= 0)
    time = 1;
  struct timespec delay_time;
  delay_time.tv_sec = time / 1000000;
  delay_time.tv_nsec = (time % 1000000) * 1000;
  while (nanosleep(&delay_time, &delay_time) && errno == EINTR)
    ;
}


x86 assembly (clang, -O0):
<upm_delay_us>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
ja     112c <upm_delay_us+0x1c>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    $0xf4240,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    $0xf4240,%ecx
xor    %edx,%edx
div    %ecx
imul   $0x3e8,%edx,%eax
mov    %eax,-0xc(%rbp)
lea    -0x10(%rbp),%rsi
mov    %rsi,%rdi
call   1030 <nanosleep@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x11(%rbp)
je     1188 <upm_delay_us+0x78>
mov    0x2e6b(%rip),%rax
mov    (%rax),%rax
mov    0x2e59(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    1198 <upm_delay_us+0x88>
jmp    119d <upm_delay_us+0x8d>
jmp    1150 <upm_delay_us+0x40>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<upm_delay_us>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
test   %edi,%edi
mov    $0x1,%eax
cmovne %edi,%eax
imul   $0x431bde83,%rax,%rcx
shr    $0x32,%rcx
mov    %ecx,0x8(%rsp)
imul   $0xf4240,%ecx,%ecx
sub    %ecx,%eax
imul   $0x3e8,%eax,%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rbx
mov    0x2e90(%rip),%r14
mov    0x2e81(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
test   %rax,%rax
je     1178 <upm_delay_us+0x68>
mov    (%r15),%rax
cmp    %rax,(%r14)
je     1160 <upm_delay_us+0x50>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@errno = global i64 0
@EINTR = global i64 0
define void @upm_delay_us(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timespec
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = icmp ule i32 %4, 0
br i1 %5, label %6, label %7
6: ; preds = %1
store i32 1, i32* %2
br label %7
7: ; preds = %6, %1
%8 = load i32, i32* %2
%9 = udiv i32 %8, 1000000
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i32 %9, i32* %10
%11 = load i32, i32* %2
%12 = urem i32 %11, 1000000
%13 = mul i32 %12, 1000
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i32 %13, i32* %14
br label %15
15: ; preds = %24, %7
%16 = call i64 @nanosleep(%struct.timespec* %3, %struct.timespec* %3)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i64, i64* @errno
%20 = load i64, i64* @EINTR
%21 = icmp eq i64 %19, %20
br label %22
22: ; preds = %18, %15
%23 = phi i1 [ false, %15 ], [ %21, %18 ]
br i1 %23, label %24, label %25
24: ; preds = %22
br label %15
25: ; preds = %22
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@errno = global i64 0
@EINTR = global i64 0
define void @upm_delay_us(i32 %0) {
%2 = alloca %struct.timespec
%3 = icmp eq i32 %0, 0
%4 = select i1 %3, i32 1, i32 %0
%5 = bitcast %struct.timespec* %2 to i8*
%6 = udiv i32 %4, 1000000
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 %6, i32* %7
%8 = urem i32 %4, 1000000
%9 = mul nuw nsw i32 %8, 1000
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i32 %9, i32* %10
br label %11
11: ; preds = %11, %1
%12 = call i64 @nanosleep(%struct.timespec* %2, %struct.timespec* %2)
%13 = icmp ne i64 %12, 0
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
%17 = select i1 %13, i1 %16, i1 false
br i1 %17, label %11, label %18
18: ; preds = %11
ret void
}