#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 udelay(unsigned us) {
  struct timespec rem, req = {
                           .tv_sec = us / 1000000,
                           .tv_nsec = (us % 1000000) * 1000,
                       };
  while (nanosleep(&req, &rem)) {
    if (errno != EINTR)
      break;
    req = rem;
  }
}


x86 assembly (clang, -O0):
<udelay>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    $0xf4240,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%eax
mov    $0xf4240,%ecx
xor    %edx,%edx
div    %ecx
imul   $0x3e8,%edx,%eax
mov    %eax,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1030 <nanosleep@plt>
cmp    $0x0,%rax
je     1182 <udelay+0x72>
mov    0x2e83(%rip),%rax
mov    (%rax),%rax
mov    0x2e71(%rip),%rcx
cmp    (%rcx),%rax
je     1175 <udelay+0x65>
jmp    1182 <udelay+0x72>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    113f <udelay+0x2f>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udelay>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%eax
imul   $0x431bde83,%rax,%rax
shr    $0x32,%rax
mov    %eax,0x8(%rsp)
imul   $0xf4240,%eax,%eax
sub    %eax,%edi
imul   $0x3e8,%edi,%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1030 <nanosleep@plt>
test   %rax,%rax
je     1193 <udelay+0x83>
mov    0x2e87(%rip),%r15
mov    0x2e78(%rip),%r12
lea    0x8(%rsp),%r14
lea    0x10(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
mov    (%r15),%rax
cmp    (%r12),%rax
jne    1193 <udelay+0x83>
mov    0x10(%rsp),%rax
mov    %rax,0x8(%rsp)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
test   %rax,%rax
jne    1170 <udelay+0x60>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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