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

/* Variables and functions */
int nanosleep(struct timespec *, int /*<<< orphan*/ *);
int p_mdelay(unsigned int milliseconds) {
  struct timespec t;
  int result;
  if (milliseconds < 1000) {
    t.tv_sec = 0;
    t.tv_nsec = (long)milliseconds * 1000000;
  } else {
    t.tv_sec = (time_t)(milliseconds / 1000);
    t.tv_nsec = (long)(milliseconds % 1000) * 1000000;
  }
  result = nanosleep(&t, NULL);
  return (result);
}


x86 assembly (clang, -O0):
<p_mdelay>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x3e8,-0x4(%rbp)
jae    1143 <p_mdelay+0x33>
movq   $0x0,-0x10(%rbp)
mov    -0x4(%rbp),%eax
imul   $0xf4240,%rax,%rax
mov    %rax,-0x18(%rbp)
jmp    116e <p_mdelay+0x5e>
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,%eax
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
mov    %edx,%eax
imul   $0xf4240,%rax,%rax
mov    %rax,-0x18(%rbp)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <nanosleep@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<p_mdelay>:
cmp    $0x3e7,%edi
ja     1125 <p_mdelay+0x15>
mov    %edi,%eax
imul   $0xf4240,%rax,%rcx
xor    %eax,%eax
jmp    1140 <p_mdelay+0x30>
mov    %edi,%eax
imul   $0x10624dd3,%rax,%rax
shr    $0x26,%rax
imul   $0x3e8,%eax,%ecx
sub    %ecx,%edi
imul   $0xf4240,%edi,%ecx
sub    $0x18,%rsp
mov    %rax,0x10(%rsp)
mov    %rcx,0x8(%rsp)
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1030 <nanosleep@plt>
add    $0x18,%rsp
ret

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

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