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

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int);
int nanosleep(struct timespec *, struct timespec *);
int /*<<< orphan*/ stderr;
void msp_udelay(unsigned long microseconds) {
  struct timespec start;
  struct timespec end;
  int rc;
  start.tv_sec = (microseconds / 1000000);
  microseconds -= (start.tv_sec * 1000000);
  start.tv_nsec = 1000 * microseconds;
  rc = nanosleep(&start, &end);
  if (0 > rc)
    fprintf(stderr, "nanosleep() returned %lu.%lu!\n", end.tv_sec, end.tv_nsec);
  return;
}


x86 assembly (clang, -O0):
<msp_udelay>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    $0xf4240,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,-0x18(%rbp)
imul   $0xf4240,-0x18(%rbp),%rcx
mov    -0x8(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x8(%rbp)
imul   $0x3e8,-0x8(%rbp),%rax
mov    %eax,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1030 <nanosleep@plt>
mov    %eax,-0x2c(%rbp)
xor    %eax,%eax
cmp    -0x2c(%rbp),%eax
jle    1195 <msp_udelay+0x75>
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rax
mov    %eax,%edx
mov    -0x20(%rbp),%ecx
lea    0xe70(%rip),%rsi
call   1040 <fprintf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<msp_udelay>:
sub    $0x28,%rsp
movabs $0x431bde82d7b634db,%rcx
de 1b 43
mov    %rdi,%rax
mul    %rcx
shr    $0x12,%rdx
mov    %rdx,0x18(%rsp)
imul   $0xfff0bdc0,%edx,%eax
add    %eax,%edi
imul   $0x3e8,%edi,%eax
mov    %eax,0x20(%rsp)
lea    0x18(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1030 <nanosleep@plt>
test   %eax,%eax
js     1167 <msp_udelay+0x47>
add    $0x28,%rsp
ret
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rsp),%edx
mov    0x10(%rsp),%ecx
lea    0xe81(%rip),%rsi
call   1040 <fprintf@plt>
add    $0x28,%rsp
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i32 }
@stderr = global i32 0
@.str = [31 x i8] c"nanosleep() returned %lu.%lu!\0A\00"
define void @msp_udelay(i64 %0) {
%2 = alloca i64
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = alloca i32
store i64 %0, i64* %2
%6 = load i64, i64* %2
%7 = udiv i64 %6, 1000000
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i64 %7, i64* %8
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%10 = load i64, i64* %9
%11 = mul i64 %10, 1000000
%12 = load i64, i64* %2
%13 = sub i64 %12, %11
store i64 %13, i64* %2
%14 = load i64, i64* %2
%15 = mul i64 1000, %14
%16 = trunc i64 %15 to i32
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i32 %16, i32* %17
%18 = call i32 @nanosleep(%struct.timespec* %3, %struct.timespec* %4)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp sgt i32 0, %19
br i1 %20, label %21, label %29
21: ; preds = %1
%22 = load i32, i32* @stderr
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%24 = load i64, i64* %23
%25 = trunc i64 %24 to i32
%26 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%27 = load i32, i32* %26
%28 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %25, i32 %27)
br label %29
29: ; preds = %21, %1
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i64, i32 }
@stderr = global i32 0
@.str = [31 x i8] c"nanosleep() returned %lu.%lu!\0A\00"
define void @msp_udelay(i64 %0) {
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = bitcast %struct.timespec* %2 to i8*
%5 = bitcast %struct.timespec* %3 to i8*
%6 = udiv i64 %0, 1000000
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i64 %6, i64* %7
%8 = mul i64 %6, -1000000
%9 = add i64 %8, %0
%10 = trunc i64 %9 to i32
%11 = mul i32 %10, 1000
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i32 %11, i32* %12
%13 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %23
15: ; preds = %1
%16 = load i32, i32* @stderr
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%18 = load i64, i64* %17
%19 = trunc i64 %18 to i32
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%21 = load i32, i32* %20
%22 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %19, i32 %21)
br label %23
23: ; preds = %15, %1
ret void
}