#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 {
  double tv_nsec;
  int /*<<< orphan*/ tv_sec;
  int /*<<< orphan*/ member_1;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
scalar_t__ my_arc4random_r();
int /*<<< orphan*/ nanosleep(struct timespec *, int /*<<< orphan*/ *);
void randomsleep(double max_sleep_sec) {
  struct timespec slptime = {0, 0};
  double rndsleep;
  rndsleep = (double)my_arc4random_r() / 4294967296.0 * max_sleep_sec;
  while (rndsleep >= 1.0) {
    slptime.tv_sec++;
    rndsleep -= 1.0;
  }
  slptime.tv_nsec = rndsleep * 1e9;
  (void)nanosleep(&slptime, NULL);
}


x86 assembly (clang, -O0):
<randomsleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    $0x0,%al
call   1050 <my_arc4random_r@plt>
cvtsi2sd %rax,%xmm0
movsd  0xe9f(%rip),%xmm1
divsd  %xmm1,%xmm0
mulsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  0xe8c(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     11aa <randomsleep+0x7a>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  0xe6c(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    116f <randomsleep+0x3f>
movsd  0xe5e(%rip),%xmm0
mulsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <nanosleep@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<randomsleep>:
push   %rbx
sub    $0x20,%rsp
movsd  %xmm0,(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <my_arc4random_r@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
mulsd  0xeb5(%rip),%xmm0
mulsd  (%rsp),%xmm0
ucomisd 0xeb0(%rip),%xmm0
jb     1181 <randomsleep+0x61>
movsd  0xeae(%rip),%xmm1
movsd  0xe9e(%rip),%xmm2
nopw   0x0(%rax,%rax,1)
add    $0x1,%ebx
addsd  %xmm1,%xmm0
ucomisd %xmm2,%xmm0
jae    1170 <randomsleep+0x50>
mov    %ebx,0x10(%rsp)
mulsd  0xe8f(%rip),%xmm0
movsd  %xmm0,0x8(%rsp)
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1030 <nanosleep@plt>
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { double, i32, i32, i32 }
define void @randomsleep(double %0) {
%2 = alloca double
%3 = alloca %struct.timespec
%4 = alloca double
store double %0, double* %2
%5 = bitcast %struct.timespec* %3 to i8*
%6 = call i64 (...) @my_arc4random_r()
%7 = sitofp i64 %6 to double
%8 = fdiv double %7, 0x41F0000000000000
%9 = load double, double* %2
%10 = fmul double %8, %9
store double %10, double* %4
br label %11
11: ; preds = %14, %1
%12 = load double, double* %4
%13 = fcmp oge double %12, 1.000000e+00
br i1 %13, label %14, label %20
14: ; preds = %11
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%16 = load i32, i32* %15
%17 = add nsw i32 %16, 1
store i32 %17, i32* %15
%18 = load double, double* %4
%19 = fsub double %18, 1.000000e+00
store double %19, double* %4
br label %11
20: ; preds = %11
%21 = load double, double* %4
%22 = fmul double %21, 1.000000e+09
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store double %22, double* %23
%24 = call i32 @nanosleep(%struct.timespec* %3, i32* null)
ret void
}

LLVM IR (-O2):
%struct.timespec = type { double, i32, i32, i32 }
define void @randomsleep(double %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%5 = bitcast i32* %4 to i8*
%6 = tail call i64 (...) @my_arc4random_r()
%7 = sitofp i64 %6 to double
%8 = fmul double %7, 0x3DF0000000000000
%9 = fmul double %8, %0
%10 = fcmp ult double %9, 1.000000e+00
br i1 %10, label %20, label %11
11: ; preds = %1
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
br label %13
13: ; preds = %11, %13
%14 = phi i32 [ 0, %11 ], [ %16, %13 ]
%15 = phi double [ %9, %11 ], [ %17, %13 ]
%16 = add nuw nsw i32 %14, 1
%17 = fadd double %15, -1.000000e+00
%18 = fcmp ult double %17, 1.000000e+00
br i1 %18, label %19, label %13
19: ; preds = %13
store i32 %16, i32* %12
br label %20
20: ; preds = %19, %1
%21 = phi double [ %17, %19 ], [ %9, %1 ]
%22 = fmul double %21, 1.000000e+09
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store double %22, double* %23
%24 = call i32 @nanosleep(%struct.timespec* %2, i32* null)
ret void
}