#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 */
typedef int useconds_t;
typedef int time_t;
struct timespec {
  int tv_sec;
  int tv_nsec;
};

/* Variables and functions */
int nanosleep(struct timespec *, int /*<<< orphan*/ *);
int usleep(useconds_t usec) {
  struct timespec rqtp;
  time_t sec;
  int ret = 0;
  if (usec) {
    sec = usec / 1000000;
    rqtp.tv_sec = sec;
    rqtp.tv_nsec = (usec - (sec * 1000000)) * 1000;
    ret = nanosleep(&rqtp, NULL);
  }
  return ret;
}


x86 assembly (clang, -O0):
<usleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     1165 <usleep+0x55>
mov    -0x4(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
imul   $0xf4240,-0x14(%rbp),%ecx
sub    %ecx,%eax
imul   $0x3e8,%eax,%eax
mov    %eax,-0xc(%rbp)
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <nanosleep@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<usleep>:
test   %edi,%edi
je     1150 <usleep+0x40>
push   %rax
movslq %edi,%rax
imul   $0x431bde83,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x32,%rax
add    %ecx,%eax
mov    %eax,(%rsp)
imul   $0xfff0bdc0,%eax,%eax
add    %edi,%eax
imul   $0x3e8,%eax,%eax
mov    %eax,0x4(%rsp)
mov    %rsp,%rdi
xor    %esi,%esi
call   1030 <nanosleep@plt>
add    $0x8,%rsp
ret
xor    %eax,%eax
ret

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

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