#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 uint32_t;
struct timespec {
  int member_0;
  int member_1;
  int /*<<< orphan*/ tv_nsec;
};

/* Variables and functions */
int nanosleep(struct timespec *, struct timespec *);
void uswait(uint32_t us) {
  struct timespec req = {us / 1000, us % 1000 * 1000};
  struct timespec rem;
  while (nanosleep(&req, &rem) == -1)
    req.tv_nsec = rem.tv_nsec;
}


x86 assembly (clang, -O0):
<uswait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
imul   $0x3e8,%edx,%eax
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x8(%rbp)
lea    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
jne    1165 <uswait+0x55>
mov    -0x18(%rbp),%eax
mov    %eax,-0x8(%rbp)
jmp    1144 <uswait+0x34>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uswait>:
push   %r14
push   %rbx
sub    $0x28,%rsp
movslq %edi,%rax
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
mov    %ecx,0x8(%rsp)
imul   $0x3e8,%ecx,%ecx
sub    %ecx,%eax
imul   $0x3e8,%eax,%eax
mov    %eax,0xc(%rsp)
movl   $0x0,0x10(%rsp)
lea    0x8(%rsp),%rdi
lea    0x18(%rsp),%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
jne    1188 <uswait+0x78>
lea    0x8(%rsp),%r14
lea    0x18(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
mov    0x20(%rsp),%eax
mov    %eax,0x10(%rsp)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
je     1170 <uswait+0x60>
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

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

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