#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;
};

/* Variables and functions */
int select(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int microsleep(int i) {
  struct timeval tv;
  if (i > 1000000) {
    tv.tv_sec = (int)i / 1000000;
    tv.tv_usec = i - tv.tv_sec * 1000000;
  } else {
    tv.tv_sec = 0;
    tv.tv_usec = i;
  }
  return select(0, NULL, NULL, NULL, &tv);
}


x86 assembly (clang, -O0):
<microsleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0xf4240,-0x4(%rbp)
jle    114a <microsleep+0x3a>
mov    -0x4(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
imul   $0xf4240,-0x10(%rbp),%ecx
sub    %ecx,%eax
mov    %eax,-0xc(%rbp)
jmp    1157 <microsleep+0x47>
movl   $0x0,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0xc(%rbp)
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x10(%rbp),%r8
mov    %rcx,%rsi
mov    %rcx,%rdx
call   1030 <select@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<microsleep>:
xor    %eax,%eax
cmp    $0xf4241,%edi
jl     112f <microsleep+0x1f>
mov    %edi,%eax
imul   $0x431bde83,%rax,%rax
shr    $0x32,%rax
imul   $0xfff0bdc0,%eax,%ecx
add    %ecx,%edi
push   %rax
mov    %eax,(%rsp)
mov    %edi,0x4(%rsp)
mov    %rsp,%r8
xor    %edi,%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1030 <select@plt>
pop    %rcx
ret

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

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