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

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
scalar_t__ nanosleep(struct timespec *, int /*<<< orphan*/ *);
int do_sleep(int mili) {
  struct timespec t;
  struct timeval t1, t2;
  t.tv_sec = mili / 1000;
  t.tv_nsec = (mili % 1000) * 1000000;
  if (mili == 0)
    return 0;
  gettimeofday(&t1, NULL);
  if (nanosleep(&t, NULL) < 0) {
    return -1;
  }
  gettimeofday(&t2, NULL);
  return 0;
}


x86 assembly (clang, -O0):
<do_sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
imul   $0xf4240,%edx,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1163 <do_sleep+0x43>
movl   $0x0,-0x4(%rbp)
jmp    11a7 <do_sleep+0x87>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <nanosleep@plt>
cmp    $0x0,%rax
jge    1193 <do_sleep+0x73>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a7 <do_sleep+0x87>
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_sleep>:
push   %rbx
sub    $0x20,%rsp
movslq %edi,%rax
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
mov    %ecx,0x8(%rsp)
imul   $0x3e8,%ecx,%ecx
mov    %eax,%edx
sub    %ecx,%edx
imul   $0xf4240,%edx,%ecx
mov    %ecx,0xc(%rsp)
xor    %ebx,%ebx
test   %eax,%eax
je     118a <do_sleep+0x6a>
lea    0x18(%rsp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1030 <nanosleep@plt>
test   %rax,%rax
js     1185 <do_sleep+0x65>
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
jmp    118a <do_sleep+0x6a>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
%struct.timeval = type { i32 }
define i32 @do_sleep(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timespec
%5 = alloca %struct.timeval
%6 = alloca %struct.timeval
store i32 %0, i32* %3
%7 = load i32, i32* %3
%8 = sdiv i32 %7, 1000
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store i32 %8, i32* %9
%10 = load i32, i32* %3
%11 = srem i32 %10, 1000
%12 = mul nsw i32 %11, 1000000
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i32 %12, i32* %13
%14 = load i32, i32* %3
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %1
store i32 0, i32* %2
br label %24
17: ; preds = %1
%18 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%19 = call i64 @nanosleep(%struct.timespec* %4, i32* null)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %17
store i32 -1, i32* %2
br label %24
22: ; preds = %17
%23 = call i32 @gettimeofday(%struct.timeval* %6, i32* null)
store i32 0, i32* %2
br label %24
24: ; preds = %22, %21, %16
%25 = load i32, i32* %2
ret i32 %25
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
%struct.timeval = type { i32 }
define i32 @do_sleep(i32 %0) {
%2 = alloca %struct.timespec
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
%5 = bitcast %struct.timespec* %2 to i8*
%6 = bitcast %struct.timeval* %3 to i8*
%7 = bitcast %struct.timeval* %4 to i8*
%8 = sdiv i32 %0, 1000
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 %8, i32* %9
%10 = srem i32 %0, 1000
%11 = mul nsw i32 %10, 1000000
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i32 %11, i32* %12
%13 = icmp eq i32 %0, 0
br i1 %13, label %20, label %14
14: ; preds = %1
%15 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%16 = call i64 @nanosleep(%struct.timespec* %2, i32* null)
%17 = icmp slt i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %14
%19 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
br label %20
20: ; preds = %14, %1, %18
%21 = phi i32 [ 0, %18 ], [ 0, %1 ], [ -1, %14 ]
ret i32 %21
}