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

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int nanosleep(struct timespec *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int iolib_delay_ms(unsigned int msec) {
  int ret;
  struct timespec a;
  if (msec > 999) {
    fprintf(stderr, "delay_ms error: delay value needs to be less than 999\n");
    msec = 999;
  }
  a.tv_nsec = ((long)(msec)) * 1000000;
  a.tv_sec = 0;
  if ((ret = nanosleep(&a, NULL)) != 0) {
    fprintf(stderr, "delay_ms error: %s\n", strerror(errno));
  }
  return 0;
}


x86 assembly (clang, -O0):
<iolib_delay_ms>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x3e7,-0x4(%rbp)
jbe    1166 <iolib_delay_ms+0x36>
mov    0x2e91(%rip),%rax
mov    (%rax),%edi
lea    0xea8(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x3e7,-0x4(%rbp)
mov    -0x4(%rbp),%eax
imul   $0xf4240,%rax,%rax
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <nanosleep@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
je     11c3 <iolib_delay_ms+0x93>
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe7b(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iolib_delay_ms>:
push   %rbx
sub    $0x10,%rsp
cmp    $0x3e8,%edi
jb     1159 <iolib_delay_ms+0x29>
mov    0x2e9c(%rip),%rax
mov    (%rax),%edi
lea    0xeb3(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x3e7,%edi
mov    %edi,%eax
imul   $0xf4240,%rax,%rax
mov    %rax,(%rsp)
movq   $0x0,0x8(%rsp)
mov    %rsp,%rdi
xor    %esi,%esi
call   1030 <nanosleep@plt>
test   %eax,%eax
je     11a7 <iolib_delay_ms+0x77>
mov    0x2e5c(%rip),%rax
mov    (%rax),%ebx
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe9c(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@stderr = global i32 0
@.str = [55 x i8] c"delay_ms error: delay value needs to be less than 999\0A\00"
@.str.1 = [20 x i8] c"delay_ms error: %s\0A\00"
@errno = global i32 0
define i32 @iolib_delay_ms(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timespec
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = icmp ugt i32 %5, 999
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0))
store i32 999, i32* %2
br label %10
10: ; preds = %7, %1
%11 = load i32, i32* %2
%12 = zext i32 %11 to i64
%13 = mul nsw i64 %12, 1000000
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store i64 %13, i64* %14
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i64 0, i64* %15
%16 = call i32 @nanosleep(%struct.timespec* %4, i32* null)
store i32 %16, i32* %3
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %10
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %21)
br label %23
23: ; preds = %18, %10
ret i32 0
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@stderr = global i32 0
@.str = [55 x i8] c"delay_ms error: delay value needs to be less than 999\0A\00"
@.str.1 = [20 x i8] c"delay_ms error: %s\0A\00"
@errno = global i32 0
define i32 @iolib_delay_ms(i32 %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = icmp ugt i32 %0, 999
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = load i32, i32* @stderr
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %5, %1
%9 = phi i32 [ 999, %5 ], [ %0, %1 ]
%10 = zext i32 %9 to i64
%11 = mul nuw nsw i64 %10, 1000000
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i64 %11, i64* %12
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i64 0, i64* %13
%14 = call i32 @nanosleep(%struct.timespec* %2, i32* null)
%15 = icmp eq i32 %14, 0
br i1 %15, label %21, label %16
16: ; preds = %8
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %19)
br label %21
21: ; preds = %16, %8
ret i32 0
}