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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ TIMER_ABSTIME;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ clock_nanosleep(int /*<<< orphan*/, int /*<<< orphan*/,
                                   struct timespec *, int /*<<< orphan*/ *);
int usleep(int asdf) {
  struct timespec t;
  clock_gettime(CLOCK_MONOTONIC, &t);
  t.tv_nsec = 5000000 * (t.tv_nsec / 5000000 + 1);
  if (t.tv_nsec >= 1000000000) {
    t.tv_nsec = 0;
    t.tv_sec++;
  }
  clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
  return 0;
}


x86 assembly (clang, -O0):
<usleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2eae(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x10(%rbp),%eax
mov    $0x4c4b40,%ecx
cltd
idiv   %ecx
add    $0x1,%eax
imul   $0x4c4b40,%eax,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x3b9aca00,-0x10(%rbp)
jl     1171 <usleep+0x51>
movl   $0x0,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    0x2e68(%rip),%rax
mov    (%rax),%edi
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1040 <clock_nanosleep@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<usleep>:
push   %rbx
sub    $0x10,%rsp
mov    0x2eb4(%rip),%rbx
mov    (%rbx),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
movslq 0x8(%rsp),%rax
imul   $0x6b5fca6b,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x35,%rax
add    %ecx,%eax
imul   $0x4c4b40,%eax,%eax
add    $0x4c4b40,%eax
mov    %eax,0x8(%rsp)
cmp    $0x3b9aca00,%eax
jl     1174 <usleep+0x54>
movl   $0x0,0x8(%rsp)
addl   $0x1,0xc(%rsp)
mov    (%rbx),%edi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
xor    %ecx,%ecx
call   1040 <clock_nanosleep@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@CLOCK_MONOTONIC = global i32 0
@TIMER_ABSTIME = global i32 0
define i32 @usleep(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timespec
store i32 %0, i32* %2
%4 = load i32, i32* @CLOCK_MONOTONIC
%5 = call i32 @clock_gettime(i32 %4, %struct.timespec* %3)
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%7 = load i32, i32* %6
%8 = sdiv i32 %7, 5000000
%9 = add nsw i32 %8, 1
%10 = mul nsw i32 5000000, %9
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%13 = load i32, i32* %12
%14 = icmp sge i32 %13, 1000000000
br i1 %14, label %15, label %20
15: ; preds = %1
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i32 0, i32* %16
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%18 = load i32, i32* %17
%19 = add nsw i32 %18, 1
store i32 %19, i32* %17
br label %20
20: ; preds = %15, %1
%21 = load i32, i32* @CLOCK_MONOTONIC
%22 = load i32, i32* @TIMER_ABSTIME
%23 = call i32 @clock_nanosleep(i32 %21, i32 %22, %struct.timespec* %3, i32* null)
ret i32 0
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@CLOCK_MONOTONIC = global i32 0
@TIMER_ABSTIME = global i32 0
define i32 @usleep(i32 %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = load i32, i32* @CLOCK_MONOTONIC
%5 = call i32 @clock_gettime(i32 %4, %struct.timespec* %2)
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%7 = load i32, i32* %6
%8 = srem i32 %7, 5000000
%9 = sub i32 %7, %8
%10 = add i32 %9, 5000000
store i32 %10, i32* %6
%11 = icmp sgt i32 %10, 999999999
br i1 %11, label %12, label %16
12: ; preds = %1
store i32 0, i32* %6
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%14 = load i32, i32* %13
%15 = add nsw i32 %14, 1
store i32 %15, i32* %13
br label %16
16: ; preds = %12, %1
%17 = load i32, i32* @CLOCK_MONOTONIC
%18 = load i32, i32* @TIMER_ABSTIME
%19 = call i32 @clock_nanosleep(i32 %17, i32 %18, %struct.timespec* %2, i32* null)
ret i32 0
}