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

/* Variables and functions */
int TEMP_FAILURE_RETRY(int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct timespec *, int /*<<< orphan*/, int);
int /*<<< orphan*/ nanosleep(struct timespec *, struct timespec *);
int usleep_signal_safe(useconds_t usec) {
  struct timespec ts;
  memset(&ts, 0, sizeof(ts));
  ts.tv_sec = usec / 1000 / 1000;
  ts.tv_nsec = (usec % (1000 * 1000)) * 1000;
  return TEMP_FAILURE_RETRY(nanosleep(&ts, &ts));
}


x86 assembly (clang, -O0):
<usleep_signal_safe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
imul   $0x3e8,%edx,%eax
mov    %eax,-0xc(%rbp)
lea    -0x10(%rbp),%rsi
mov    %rsi,%rdi
call   1030 <nanosleep@plt>
mov    %eax,%edi
call   1050 <TEMP_FAILURE_RETRY@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<usleep_signal_safe>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
movslq %ebx,%rax
imul   $0x431bde83,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x32,%rcx
add    %edx,%ecx
mov    %ecx,(%rsp)
imul   $0xf4240,%ecx,%ecx
sub    %ecx,%eax
imul   $0x3e8,%eax,%eax
mov    %eax,0x4(%rsp)
mov    %r14,%rdi
mov    %r14,%rsi
call   1030 <nanosleep@plt>
mov    %eax,%edi
call   1050 <TEMP_FAILURE_RETRY@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
define i32 @usleep_signal_safe(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timespec
store i32 %0, i32* %2
%4 = call i32 @memset(%struct.timespec* %3, i32 0, i32 8)
%5 = load i32, i32* %2
%6 = sdiv i32 %5, 1000
%7 = sdiv i32 %6, 1000
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i32 %7, i32* %8
%9 = load i32, i32* %2
%10 = srem i32 %9, 1000000
%11 = mul nsw i32 %10, 1000
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i32 %11, i32* %12
%13 = call i32 @nanosleep(%struct.timespec* %3, %struct.timespec* %3)
%14 = call i32 @TEMP_FAILURE_RETRY(i32 %13)
ret i32 %14
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
define i32 @usleep_signal_safe(i32 %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = call i32 @memset(%struct.timespec* %2, i32 0, i32 8)
%5 = sdiv i32 %0, 1000000
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 %5, i32* %6
%7 = srem i32 %0, 1000000
%8 = mul nsw i32 %7, 1000
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i32 %8, i32* %9
%10 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %2)
%11 = call i32 @TEMP_FAILURE_RETRY(i32 %10)
ret i32 %11
}