#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 {
  unsigned long tv_sec;
  unsigned long tv_usec;
};

/* Variables and functions */
int /*<<< orphan*/ select(int /*<<< orphan*/, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          struct timeval *);
void delay_ms(unsigned long int time_ms) {
  struct timeval tv;
  if (!time_ms)
    return;
  tv.tv_sec = time_ms / 1000;
  tv.tv_usec = (time_ms - (tv.tv_sec * 1000)) * 1000;
  select(0, NULL, NULL, NULL, &tv);
}


x86 assembly (clang, -O0):
<delay_ms>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    112c <delay_ms+0x1c>
jmp    116d <delay_ms+0x5d>
mov    -0x8(%rbp),%rax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
imul   $0x3e8,-0x18(%rbp),%rcx
sub    %rcx,%rax
imul   $0x3e8,%rax,%rax
mov    %rax,-0x10(%rbp)
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rsi
mov    %rcx,%rdx
call   1030 <select@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<delay_ms>:
test   %rdi,%rdi
je     1162 <delay_ms+0x52>
sub    $0x18,%rsp
mov    %rdi,%rax
shr    $0x3,%rax
movabs $0x20c49ba5e353f7cf,%rcx
9b c4 20
mul    %rcx
shr    $0x4,%rdx
mov    %rdx,0x8(%rsp)
imul   $0xfffffffffffffc18,%rdx,%rax
add    %rdi,%rax
imul   $0x3e8,%rax,%rax
mov    %rax,0x10(%rsp)
lea    0x8(%rsp),%r8
xor    %edi,%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1030 <select@plt>
add    $0x18,%rsp
ret

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

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