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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ CLOCK_REALTIME;
int EINTR;
int EINVAL;
int clock_nanosleep(int /*<<< orphan*/, int /*<<< orphan*/, struct timespec *,
                    struct timespec *);
void _wall_Sleep(uint64_t i_delay, bool b_realtime) {
  struct timespec ts;
  ts.tv_sec = i_delay / 27000000;
  ts.tv_nsec = (i_delay % 27000000) * 1000 / 27;
  int val = EINVAL;
  if (!b_realtime)
    while ((val = clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, &ts)) == EINTR)
      ;
  if (val == EINVAL) {
    ts.tv_sec = i_delay / 27000000;
    ts.tv_nsec = (i_delay % 27000000) * 1000 / 27;
    while (clock_nanosleep(CLOCK_REALTIME, 0, &ts, &ts) == EINTR)
      ;
  }
}


x86 assembly (clang, -O0):
<_wall_Sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x19bfcc0,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x19bfcc0,%ecx
cltd
idiv   %ecx
imul   $0x3e8,%edx,%eax
mov    $0x1b,%ecx
cltd
idiv   %ecx
mov    %eax,-0xc(%rbp)
mov    0x2e81(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1196 <_wall_Sleep+0x86>
jmp    1163 <_wall_Sleep+0x53>
mov    0x2e7e(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
lea    -0x10(%rbp),%rcx
mov    %rcx,%rdx
call   1030 <clock_nanosleep@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e54(%rip),%rcx
cmp    (%rcx),%eax
jne    1191 <_wall_Sleep+0x81>
jmp    1163 <_wall_Sleep+0x53>
jmp    1196 <_wall_Sleep+0x86>
mov    -0x14(%rbp),%eax
mov    0x2e30(%rip),%rcx
cmp    (%rcx),%eax
jne    1202 <_wall_Sleep+0xf2>
mov    -0x4(%rbp),%eax
mov    $0x19bfcc0,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x19bfcc0,%ecx
cltd
idiv   %ecx
imul   $0x3e8,%edx,%eax
mov    $0x1b,%ecx
cltd
idiv   %ecx
mov    %eax,-0xc(%rbp)
mov    0x2def(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
lea    -0x10(%rbp),%rcx
mov    %rcx,%rdx
call   1030 <clock_nanosleep@plt>
mov    0x2de8(%rip),%rcx
cmp    (%rcx),%eax
jne    11fd <_wall_Sleep+0xed>
jmp    11d2 <_wall_Sleep+0xc2>
jmp    1202 <_wall_Sleep+0xf2>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_wall_Sleep>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
movslq %edi,%rax
imul   $0x27c4a9c9,%rax,%r14
mov    %r14,%rcx
shr    $0x3f,%rcx
sar    $0x36,%r14
add    %ecx,%r14d
mov    %r14d,0x8(%rsp)
imul   $0x19bfcc0,%r14d,%ecx
sub    %ecx,%eax
imul   $0x3e8,%eax,%eax
cltq
imul   $0x4bda12f7,%rax,%r15
mov    %r15,%rax
shr    $0x3f,%rax
sar    $0x23,%r15
add    %eax,%r15d
mov    %r15d,0xc(%rsp)
test   %esi,%esi
jne    11a2 <_wall_Sleep+0x92>
mov    0x2e78(%rip),%r12
lea    0x8(%rsp),%rbx
mov    0x2e5c(%rip),%r13
nopl   0x0(%rax)
mov    (%r12),%edi
xor    %esi,%esi
mov    %rbx,%rdx
mov    %rbx,%rcx
call   1030 <clock_nanosleep@plt>
cmp    0x0(%r13),%eax
je     1180 <_wall_Sleep+0x70>
mov    0x2e32(%rip),%rcx
cmp    (%rcx),%eax
jne    11d5 <_wall_Sleep+0xc5>
mov    %r14d,0x8(%rsp)
mov    %r15d,0xc(%rsp)
mov    0x2e15(%rip),%r14
lea    0x8(%rsp),%rbx
mov    0x2e19(%rip),%r15
nop
mov    (%r14),%edi
xor    %esi,%esi
mov    %rbx,%rdx
mov    %rbx,%rcx
call   1030 <clock_nanosleep@plt>
cmp    (%r15),%eax
je     11c0 <_wall_Sleep+0xb0>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@EINVAL = global i32 0
@CLOCK_MONOTONIC = global i32 0
@EINTR = global i32 0
@CLOCK_REALTIME = global i32 0
define void @_wall_Sleep(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timespec
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = load i32, i32* %3
%8 = sdiv i32 %7, 27000000
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
store i32 %8, i32* %9
%10 = load i32, i32* %3
%11 = srem i32 %10, 27000000
%12 = mul nsw i32 %11, 1000
%13 = sdiv i32 %12, 27
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
store i32 %13, i32* %14
%15 = load i32, i32* @EINVAL
store i32 %15, i32* %6
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 0
br i1 %17, label %26, label %18
18: ; preds = %2
br label %19
19: ; preds = %24, %18
%20 = load i32, i32* @CLOCK_MONOTONIC
%21 = call i32 @clock_nanosleep(i32 %20, i32 0, %struct.timespec* %5, %struct.timespec* %5)
store i32 %21, i32* %6
%22 = load i32, i32* @EINTR
%23 = icmp eq i32 %21, %22
br i1 %23, label %24, label %25
24: ; preds = %19
br label %19
25: ; preds = %19
br label %26
26: ; preds = %25, %2
%27 = load i32, i32* %6
%28 = load i32, i32* @EINVAL
%29 = icmp eq i32 %27, %28
br i1 %29, label %30, label %46
30: ; preds = %26
%31 = load i32, i32* %3
%32 = sdiv i32 %31, 27000000
%33 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
store i32 %32, i32* %33
%34 = load i32, i32* %3
%35 = srem i32 %34, 27000000
%36 = mul nsw i32 %35, 1000
%37 = sdiv i32 %36, 27
%38 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
store i32 %37, i32* %38
br label %39
39: ; preds = %44, %30
%40 = load i32, i32* @CLOCK_REALTIME
%41 = call i32 @clock_nanosleep(i32 %40, i32 0, %struct.timespec* %5, %struct.timespec* %5)
%42 = load i32, i32* @EINTR
%43 = icmp eq i32 %41, %42
br i1 %43, label %44, label %45
44: ; preds = %39
br label %39
45: ; preds = %39
br label %46
46: ; preds = %45, %26
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@EINVAL = global i32 0
@CLOCK_MONOTONIC = global i32 0
@EINTR = global i32 0
@CLOCK_REALTIME = global i32 0
define void @_wall_Sleep(i32 %0, i32 %1) {
%3 = alloca %struct.timespec
%4 = bitcast %struct.timespec* %3 to i8*
%5 = sdiv i32 %0, 27000000
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
store i32 %5, i32* %6
%7 = srem i32 %0, 27000000
%8 = mul nsw i32 %7, 1000
%9 = sdiv i32 %8, 27
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
store i32 %9, i32* %10
%11 = icmp eq i32 %1, 0
br i1 %11, label %12, label %20
12: ; preds = %2, %12
%13 = load i32, i32* @CLOCK_MONOTONIC
%14 = call i32 @clock_nanosleep(i32 %13, i32 0, %struct.timespec* %3, %struct.timespec* %3)
%15 = load i32, i32* @EINTR
%16 = icmp eq i32 %14, %15
br i1 %16, label %12, label %17
17: ; preds = %12
%18 = load i32, i32* @EINVAL
%19 = icmp eq i32 %14, %18
br i1 %19, label %20, label %26
20: ; preds = %2, %17
store i32 %5, i32* %6
store i32 %9, i32* %10
br label %21
21: ; preds = %21, %20
%22 = load i32, i32* @CLOCK_REALTIME
%23 = call i32 @clock_nanosleep(i32 %22, i32 0, %struct.timespec* %3, %struct.timespec* %3)
%24 = load i32, i32* @EINTR
%25 = icmp eq i32 %23, %24
br i1 %25, label %21, label %26
26: ; preds = %21, %17
ret void
}