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

/* Variables and functions */
int nanosleep(struct timespec *, struct timespec *);
void nswait(uint64_t ns) {
  struct timespec req = {ns / 1000000, ns % 1000000};
  struct timespec rem;
  while (nanosleep(&req, &rem) == -1)
    req.tv_nsec = rem.tv_nsec;
}


x86 assembly (clang, -O0):
<nswait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %edx,-0xc(%rbp)
movl   $0x0,-0x8(%rbp)
lea    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
jne    115f <nswait+0x4f>
mov    -0x18(%rbp),%eax
mov    %eax,-0x8(%rbp)
jmp    113e <nswait+0x2e>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nswait>:
push   %r14
push   %rbx
sub    $0x28,%rsp
movslq %edi,%rax
imul   $0x431bde83,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x32,%rcx
add    %edx,%ecx
mov    %ecx,0x8(%rsp)
imul   $0xf4240,%ecx,%ecx
sub    %ecx,%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 <nswait+0x78>
lea    0x8(%rsp),%r14
lea    0x18(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x20(%rsp),%eax
mov    %eax,0x10(%rsp)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
je     1170 <nswait+0x60>
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

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

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