#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 timezone {
  int dummy;
};
struct timeval {
  scalar_t__ tv_usec;
  scalar_t__ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ exit(int);
scalar_t__ gettimeofday(struct timeval *, struct timezone *);
int /*<<< orphan*/ perror(char *);
void my_sleep(double sleeptime) {
  struct timeval tv;
  struct timezone tzp;
  double start;
  double real;
  if (gettimeofday(&tv, &tzp) != 0) {
    perror("get_time: ");
    exit(-1);
  }
  start = (double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0);
  real = start;
  while ((real - start) < sleeptime) {
    if (gettimeofday(&tv, &tzp) != 0) {
      perror("get_time: ");
      exit(-1);
    }
    real = (double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0);
  }
}


x86 assembly (clang, -O0):
<my_sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x8(%rbp)
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1030 <gettimeofday@plt>
cmp    $0x0,%rax
je     116a <my_sleep+0x3a>
lea    0xead(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>
cvtsi2sdq -0x10(%rbp),%xmm0
cvtsi2sdq -0x18(%rbp),%xmm1
movsd  0xe82(%rip),%xmm2
divsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm1
subsd  -0x28(%rbp),%xmm1
movsd  -0x8(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    1201 <my_sleep+0xd1>
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1030 <gettimeofday@plt>
cmp    $0x0,%rax
je     11db <my_sleep+0xab>
lea    0xe3c(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>
cvtsi2sdq -0x10(%rbp),%xmm0
cvtsi2sdq -0x18(%rbp),%xmm1
movsd  0xe11(%rip),%xmm2
divsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    1195 <my_sleep+0x65>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_sleep>:
push   %r14
push   %rbx
sub    $0x28,%rsp
movsd  %xmm0,0x8(%rsp)
lea    0x10(%rsp),%rdi
mov    %rsp,%rsi
call   1030 <gettimeofday@plt>
test   %rax,%rax
jne    11ea <my_sleep+0xba>
xorps  %xmm0,%xmm0
cvtsi2sdq 0x18(%rsp),%xmm0
cvtsi2sdq 0x10(%rsp),%xmm1
divsd  0xe94(%rip),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,0x20(%rsp)
movapd %xmm1,%xmm0
subsd  %xmm1,%xmm0
movsd  0x8(%rsp),%xmm1
ucomisd %xmm0,%xmm1
jbe    11e2 <my_sleep+0xb2>
lea    0x10(%rsp),%r14
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <gettimeofday@plt>
test   %rax,%rax
jne    11ea <my_sleep+0xba>
xorps  %xmm0,%xmm0
cvtsi2sdq 0x18(%rsp),%xmm0
xorps  %xmm1,%xmm1
cvtsi2sdq 0x10(%rsp),%xmm1
divsd  0xe34(%rip),%xmm1
addsd  %xmm0,%xmm1
subsd  0x20(%rsp),%xmm1
movsd  0x8(%rsp),%xmm0
ucomisd %xmm1,%xmm0
ja     11a0 <my_sleep+0x70>
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe17(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
%struct.timezone = type { i32 }
@.str = [11 x i8] c"get_time: \00"
define void @my_sleep(double %0) {
%2 = alloca double
%3 = alloca %struct.timeval
%4 = alloca %struct.timezone
%5 = alloca double
%6 = alloca double
store double %0, double* %2
%7 = call i64 @gettimeofday(%struct.timeval* %3, %struct.timezone* %4)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 -1)
unreachable
12: ; preds = %1
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%14 = load i64, i64* %13
%15 = sitofp i64 %14 to double
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%17 = load i64, i64* %16
%18 = sitofp i64 %17 to double
%19 = fdiv double %18, 1.000000e+06
%20 = fadd double %15, %19
store double %20, double* %5
%21 = load double, double* %5
store double %21, double* %6
br label %22
22: ; preds = %34, %12
%23 = load double, double* %6
%24 = load double, double* %5
%25 = fsub double %23, %24
%26 = load double, double* %2
%27 = fcmp olt double %25, %26
br i1 %27, label %28, label %43
28: ; preds = %22
%29 = call i64 @gettimeofday(%struct.timeval* %3, %struct.timezone* %4)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%33 = call i32 @exit(i32 -1)
unreachable
34: ; preds = %28
%35 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%36 = load i64, i64* %35
%37 = sitofp i64 %36 to double
%38 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%39 = load i64, i64* %38
%40 = sitofp i64 %39 to double
%41 = fdiv double %40, 1.000000e+06
%42 = fadd double %37, %41
store double %42, double* %6
br label %22
43: ; preds = %22
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
%struct.timezone = type { i32 }
@.str = [11 x i8] c"get_time: \00"
define void @my_sleep(double %0) {
%2 = alloca %struct.timeval
%3 = alloca %struct.timezone
%4 = bitcast %struct.timeval* %2 to i8*
%5 = bitcast %struct.timezone* %3 to i8*
%6 = call i64 @gettimeofday(%struct.timeval* %2, %struct.timezone* %3)
%7 = icmp eq i64 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %1
%9 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 -1)
unreachable
11: ; preds = %1
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%13 = load i64, i64* %12
%14 = sitofp i64 %13 to double
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%16 = load i64, i64* %15
%17 = sitofp i64 %16 to double
%18 = fdiv double %17, 1.000000e+06
%19 = fadd double %18, %14
%20 = fsub double %19, %19
%21 = fcmp olt double %20, %0
br i1 %21, label %22, label %37
22: ; preds = %11, %28
%23 = call i64 @gettimeofday(%struct.timeval* %2, %struct.timezone* %3)
%24 = icmp eq i64 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %22
%26 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%27 = tail call i32 @exit(i32 -1)
unreachable
28: ; preds = %22
%29 = load i64, i64* %12
%30 = sitofp i64 %29 to double
%31 = load i64, i64* %15
%32 = sitofp i64 %31 to double
%33 = fdiv double %32, 1.000000e+06
%34 = fadd double %33, %30
%35 = fsub double %34, %19
%36 = fcmp olt double %35, %0
br i1 %36, label %22, label %37
37: ; preds = %28, %11
ret void
}