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

/* Variables and functions */
int nanosleep(struct timespec *, struct timespec *);
void _nxweb_sleep_us(int us) {
  struct timespec req;
  time_t sec = us / 1000000;
  us %= 1000000;
  req.tv_sec = sec;
  req.tv_nsec = us * 1000L;
  while (nanosleep(&req, &req) == -1)
    ;
}


x86 assembly (clang, -O0):
<_nxweb_sleep_us>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %edx,-0x4(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x10(%rbp)
movslq -0x4(%rbp),%rax
imul   $0x3e8,%rax,%rax
mov    %eax,-0xc(%rbp)
lea    -0x10(%rbp),%rsi
mov    %rsi,%rdi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
jne    1165 <_nxweb_sleep_us+0x55>
jmp    114b <_nxweb_sleep_us+0x3b>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_nxweb_sleep_us>:
push   %rbx
sub    $0x10,%rsp
movslq %edi,%rax
imul   $0x431bde83,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x32,%rcx
add    %edx,%ecx
imul   $0xf4240,%ecx,%edx
sub    %edx,%eax
mov    %ecx,0x8(%rsp)
imul   $0x3e8,%eax,%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
je     1150 <_nxweb_sleep_us+0x40>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
define void @_nxweb_sleep_us(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timespec
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = sdiv i32 %5, 1000000
store i32 %6, i32* %4
%7 = load i32, i32* %2
%8 = srem i32 %7, 1000000
store i32 %8, i32* %2
%9 = load i32, i32* %4
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i32 %9, i32* %10
%11 = load i32, i32* %2
%12 = sext i32 %11 to i64
%13 = mul nsw i64 %12, 1000
%14 = trunc i64 %13 to i32
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i32 %14, i32* %15
br label %16
16: ; preds = %19, %1
%17 = call i32 @nanosleep(%struct.timespec* %3, %struct.timespec* %3)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %16
br label %16
20: ; preds = %16
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
define void @_nxweb_sleep_us(i32 %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = sdiv i32 %0, 1000000
%5 = srem i32 %0, 1000000
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 %4, i32* %6
%7 = mul nsw i32 %5, 1000
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i32 %7, i32* %8
br label %9
9: ; preds = %9, %1
%10 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %2)
%11 = icmp eq i32 %10, -1
br i1 %11, label %9, label %12
12: ; preds = %9
ret void
}