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

/* Variables and functions */
int /*<<< orphan*/ EINTR;
int /*<<< orphan*/ errno;
int msleep(int);
int nanosleep(const struct timespec *req, struct timespec *rem) {
  int rc;
  rc = msleep(req->tv_sec * 1000 + req->tv_nsec / 1000000);
  if (rc > 0) {
    if (rem) {
      rem->tv_sec = rc / 1000;
      rem->tv_nsec = (rc % 1000) * 1000000;
    }
    errno = EINTR;
    return -1;
  }
  return rc;
}


x86 assembly (clang, -O0):
<nanosleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
imul   $0x3e8,(%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
add    %eax,%edi
call   1030 <msleep@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    11a1 <nanosleep+0x91>
cmpq   $0x0,-0x18(%rbp)
je     1183 <nanosleep+0x73>
mov    -0x1c(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
imul   $0xf4240,%edx,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    0x2e4e(%rip),%rax
mov    (%rax),%ecx
mov    0x2e4d(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11a7 <nanosleep+0x97>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nanosleep>:
push   %rbx
mov    %rsi,%rbx
imul   $0x3e8,(%rdi),%eax
movslq 0x4(%rdi),%rcx
imul   $0x431bde83,%rcx,%rdi
mov    %rdi,%rcx
shr    $0x3f,%rcx
sar    $0x32,%rdi
add    %ecx,%edi
add    %eax,%edi
call   1030 <msleep@plt>
test   %eax,%eax
jle    1179 <nanosleep+0x69>
test   %rbx,%rbx
je     1162 <nanosleep+0x52>
mov    %eax,%ecx
imul   $0x10624dd3,%rcx,%rcx
shr    $0x26,%rcx
mov    %ecx,(%rbx)
imul   $0x3e8,%ecx,%ecx
sub    %ecx,%eax
imul   $0xf4240,%eax,%eax
mov    %eax,0x4(%rbx)
mov    0x2e6f(%rip),%rax
mov    (%rax),%eax
mov    0x2e6e(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@EINTR = global i32 0
@errno = global i32 0
define i32 @nanosleep(%struct.timespec* %0, %struct.timespec* %1) {
%3 = alloca i32
%4 = alloca %struct.timespec*
%5 = alloca %struct.timespec*
%6 = alloca i32
store %struct.timespec* %0, %struct.timespec** %4
store %struct.timespec* %1, %struct.timespec** %5
%7 = load %struct.timespec*, %struct.timespec** %4
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 0
%9 = load i32, i32* %8
%10 = mul nsw i32 %9, 1000
%11 = load %struct.timespec*, %struct.timespec** %4
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %11, i32 0, i32 1
%13 = load i32, i32* %12
%14 = sdiv i32 %13, 1000000
%15 = add nsw i32 %10, %14
%16 = call i32 @msleep(i32 %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %34
19: ; preds = %2
%20 = load %struct.timespec*, %struct.timespec** %5
%21 = icmp ne %struct.timespec* %20, null
br i1 %21, label %22, label %32
22: ; preds = %19
%23 = load i32, i32* %6
%24 = sdiv i32 %23, 1000
%25 = load %struct.timespec*, %struct.timespec** %5
%26 = getelementptr inbounds %struct.timespec, %struct.timespec* %25, i32 0, i32 0
store i32 %24, i32* %26
%27 = load i32, i32* %6
%28 = srem i32 %27, 1000
%29 = mul nsw i32 %28, 1000000
%30 = load %struct.timespec*, %struct.timespec** %5
%31 = getelementptr inbounds %struct.timespec, %struct.timespec* %30, i32 0, i32 1
store i32 %29, i32* %31
br label %32
32: ; preds = %22, %19
%33 = load i32, i32* @EINTR
store i32 %33, i32* @errno
store i32 -1, i32* %3
br label %36
34: ; preds = %2
%35 = load i32, i32* %6
store i32 %35, i32* %3
br label %36
36: ; preds = %34, %32
%37 = load i32, i32* %3
ret i32 %37
}

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