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

/* Variables and functions */
int EXIT_SUCCESS;
int /*<<< orphan*/ debug(char *, int);
int /*<<< orphan*/ nanosleep(struct timespec *, int /*<<< orphan*/ *);
int mafunc(int n) {
  debug("i sleep %d microsec\n", n);
  do {
    struct timespec tm;
    tm.tv_sec = (time_t)n / 1000;
    tm.tv_nsec = (long)(n % 1000) * 1000;
    nanosleep(&tm, NULL);
  } while (0);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<mafunc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%esi
lea    0xecb(%rip),%rdi
call   1030 <debug@plt>
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
movslq %edx,%rax
imul   $0x3e8,%rax,%rax
mov    %rax,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <nanosleep@plt>
mov    0x2e6b(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mafunc>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    0xed2(%rip),%rdi
mov    %ebx,%esi
call   1030 <debug@plt>
movslq %ebx,%rax
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
mov    %ecx,(%rsp)
imul   $0x3e8,%ecx,%ecx
sub    %ecx,%eax
imul   $0x3e8,%eax,%eax
cltq
mov    %rax,0x8(%rsp)
mov    %rsp,%rdi
xor    %esi,%esi
call   1040 <nanosleep@plt>
mov    0x2e6b(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
@.str = [21 x i8] c"i sleep %d microsec\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @mafunc(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timespec
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = call i32 @debug(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %4)
br label %6
6: ; preds = %1
%7 = load i32, i32* %2
%8 = sdiv i32 %7, 1000
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i32 %8, i32* %9
%10 = load i32, i32* %2
%11 = srem i32 %10, 1000
%12 = sext i32 %11 to i64
%13 = mul nsw i64 %12, 1000
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i64 %13, i64* %14
%15 = call i32 @nanosleep(%struct.timespec* %3, i32* null)
br label %16
16: ; preds = %6
%17 = load i32, i32* @EXIT_SUCCESS
ret i32 %17
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@.str = [21 x i8] c"i sleep %d microsec\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @mafunc(i32 %0) {
%2 = alloca %struct.timespec
%3 = tail call i32 @debug(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %0)
%4 = bitcast %struct.timespec* %2 to i8*
%5 = sdiv i32 %0, 1000
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 %5, i32* %6
%7 = srem i32 %0, 1000
%8 = mul nsw i32 %7, 1000
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i64 %9, i64* %10
%11 = call i32 @nanosleep(%struct.timespec* %2, i32* null)
%12 = load i32, i32* @EXIT_SUCCESS
ret i32 %12
}