#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 timeval {
  int tv_sec;
  int tv_usec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int select(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
void my_sleep(double dt) {
  int nfd;
  struct timeval tv;
  tv.tv_sec = (int)dt;
  tv.tv_usec = (int)((dt - tv.tv_sec) * 1000000.);
slipping:
  nfd = select(0, (fd_set *)NULL, (fd_set *)NULL, (fd_set *)NULL, &tv);
  if (nfd < 0) {
    if (errno == EINTR)
      goto slipping;
    fprintf(stderr, "Error in mydelay(){ select() }. %s\n", strerror(errno));
  }
}


x86 assembly (clang, -O0):
<my_sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x8(%rbp)
cvttsd2si -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
movsd  -0x8(%rbp),%xmm0
cvtsi2sdl -0x18(%rbp),%xmm1
subsd  %xmm1,%xmm0
movsd  0xea5(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x14(%rbp)
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rsi
mov    %rcx,%rdx
call   1040 <select@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11d4 <my_sleep+0xa4>
mov    0x2e49(%rip),%rax
mov    (%rax),%rax
mov    0x2e37(%rip),%rcx
cmp    (%rcx),%rax
jne    11a7 <my_sleep+0x77>
jmp    1166 <my_sleep+0x36>
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe39(%rip),%rsi
call   1030 <fprintf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_sleep>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
cvttsd2si %xmm0,%eax
cvttpd2dq %xmm0,%xmm1
mov    %eax,0x8(%rsp)
cvtdq2pd %xmm1,%xmm1
subsd  %xmm1,%xmm0
mulsd  0xeab(%rip),%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%r15
mov    0x2e6f(%rip),%r14
mov    0x2e60(%rip),%rbx
xor    %edi,%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1040 <select@plt>
test   %eax,%eax
jns    11ab <my_sleep+0x7b>
mov    (%r14),%rdi
cmp    (%rbx),%rdi
je     1170 <my_sleep+0x40>
mov    0x2e4d(%rip),%rax
mov    (%rax),%ebx
call   1050 <strerror@plt>
lea    0xe67(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1030 <fprintf@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [36 x i8] c"Error in mydelay(){ select() }. %s\0A\00"
define void @my_sleep(double %0) {
%2 = alloca double
%3 = alloca i32
%4 = alloca %struct.timeval
store double %0, double* %2
%5 = load double, double* %2
%6 = fptosi double %5 to i32
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
store i32 %6, i32* %7
%8 = load double, double* %2
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%10 = load i32, i32* %9
%11 = sitofp i32 %10 to double
%12 = fsub double %8, %11
%13 = fmul double %12, 1.000000e+06
%14 = fptosi double %13 to i32
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
store i32 %14, i32* %15
br label %16
16: ; preds = %24, %1
%17 = call i32 @select(i32 0, i32* null, i32* null, i32* null, %struct.timeval* %4)
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %30
20: ; preds = %16
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %25
24: ; preds = %20
br label %16
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = load i64, i64* @errno
%28 = call i8* @strerror(i64 %27)
%29 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %28)
br label %30
30: ; preds = %25, %16
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [36 x i8] c"Error in mydelay(){ select() }. %s\0A\00"
define void @my_sleep(double %0) {
%2 = alloca %struct.timeval
%3 = bitcast %struct.timeval* %2 to i8*
%4 = fptosi double %0 to i32
%5 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
store i32 %4, i32* %5
%6 = sitofp i32 %4 to double
%7 = fsub double %0, %6
%8 = fmul double %7, 1.000000e+06
%9 = fptosi double %8 to i32
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
store i32 %9, i32* %10
br label %11
11: ; preds = %14, %1
%12 = call i32 @select(i32 0, i32* null, i32* null, i32* null, %struct.timeval* %2)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %22
14: ; preds = %11
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
br i1 %17, label %11, label %18
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = call i8* @strerror(i64 %15)
%21 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %20)
br label %22
22: ; preds = %11, %18
ret void
}