#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 {
  unsigned long tv_sec;
  unsigned long tv_nsec;
};
struct sched_param {
  int sched_priority;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ SCHED_FIFO;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ sched_setscheduler(int /*<<< orphan*/, int /*<<< orphan*/,
                              struct sched_param *);
int /*<<< orphan*/ sscanf(char *, char *, ...);
int /*<<< orphan*/ usleep(unsigned long);
int main(int argc, char *argv[]) {
  unsigned long sleep_timeout, *ts_delta;
  int iters, i;
  struct timespec ts1, ts2, ts_timeout, ts_rem;
  struct sched_param param = {.sched_priority = 1};
  if (argc < 3) {
    printf("test <sleep_timeout in ns> <iters>\n");
    return -2;
  }
  sscanf(argv[1], "%lu", &sleep_timeout);
  sscanf(argv[2], "%d", &iters);
  if (sched_setscheduler(0, SCHED_FIFO, &param) < 0) {
    perror("sched_setscheduler:");
  }
  ts_delta = (unsigned long *)malloc(sizeof(unsigned long) * iters);
  i = 0;
  ts_timeout.tv_sec = 0;
  ts_timeout.tv_nsec = sleep_timeout;
  while (i++ < iters) {
    ts_rem.tv_sec = ts_rem.tv_nsec = 0;
    clock_gettime(CLOCK_MONOTONIC, &ts1);
    usleep(sleep_timeout / 1000);
    clock_gettime(CLOCK_MONOTONIC, &ts2);
    ts_delta[i] = ts2.tv_sec * 1000000000 + ts2.tv_nsec -
                  ts1.tv_sec * 1000000000 - ts1.tv_nsec;
    printf("iter = %d ts_delta = %lu\n", i, ts_delta[i]);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe74(%rip),%eax
mov    %eax,-0x70(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11b3 <main+0x43>
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12e9 <main+0x179>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe66(%rip),%rsi
lea    -0x18(%rbp),%rdx
mov    $0x0,%al
call   1070 <sscanf@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe50(%rip),%rsi
lea    -0x24(%rbp),%rdx
mov    $0x0,%al
call   1070 <sscanf@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x70(%rbp),%rdx
call   1050 <sched_setscheduler@plt>
cmp    $0x0,%rax
jge    1211 <main+0xa1>
lea    0xe23(%rip),%rdi
call   1080 <perror@plt>
movslq -0x24(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
movq   $0x0,-0x58(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x28(%rbp)
cmp    -0x24(%rbp),%eax
jge    12e2 <main+0x172>
movq   $0x0,-0x60(%rbp)
movq   $0x0,-0x68(%rbp)
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x18(%rbp),%rax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,%rdi
call   1090 <usleep@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
lea    -0x48(%rbp),%rsi
call   1030 <clock_gettime@plt>
imul   $0x3b9aca00,-0x48(%rbp),%rdx
3b
add    -0x40(%rbp),%rdx
imul   $0x3b9aca00,-0x38(%rbp),%rax
3b
sub    %rax,%rdx
sub    -0x30(%rbp),%rdx
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x28(%rbp),%esi
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xd6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    123b <main+0xcb>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
movl   $0x1,0x8(%rsp)
cmp    $0x2,%edi
jg     11b1 <main+0x31>
lea    0xe93(%rip),%rdi
call   1030 <puts@plt>
mov    $0xfffffffe,%ebp
jmp    12b9 <main+0x139>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
lea    0xe41(%rip),%rsi
xor    %ebp,%ebp
lea    0x10(%rsp),%rdx
xor    %eax,%eax
call   1080 <sscanf@plt>
mov    0x10(%rbx),%rdi
lea    0xe2c(%rip),%rsi
lea    0x4(%rsp),%rdx
xor    %eax,%eax
call   1080 <sscanf@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
xor    %edi,%edi
call   1060 <sched_setscheduler@plt>
test   %rax,%rax
js     12ca <main+0x14a>
mov    0x4(%rsp),%edi
shl    $0x3,%edi
call   1070 <malloc@plt>
cmpl   $0x0,0x4(%rsp)
jle    12b9 <main+0x139>
mov    %rax,%r14
mov    $0x1,%ebx
mov    0x2dc0(%rip),%r15
lea    0x18(%rsp),%r12
lea    0xde7(%rip),%r13
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
mov    (%r15),%edi
lea    0x28(%rsp),%rsi
call   1040 <clock_gettime@plt>
mov    0x10(%rsp),%rax
shr    $0x3,%rax
movabs $0x20c49ba5e353f7cf,%rcx
9b c4 20
mul    %rcx
mov    %rdx,%rdi
shr    $0x4,%rdi
call   10a0 <usleep@plt>
mov    (%r15),%edi
mov    %r12,%rsi
call   1040 <clock_gettime@plt>
mov    0x18(%rsp),%rax
mov    0x20(%rsp),%rdx
sub    0x28(%rsp),%rax
imul   $0x3b9aca00,%rax,%rax
sub    0x30(%rsp),%rdx
add    %rax,%rdx
mov    %rdx,(%r14,%rbx,8)
mov    %r13,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x1(%rbx),%rax
movslq 0x4(%rsp),%rcx
cmp    %rcx,%rbx
mov    %rax,%rbx
jl     1240 <main+0xc0>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd36(%rip),%rdi
call   1090 <perror@plt>
jmp    1202 <main+0x82>

LLVM IR (-O0):
%struct.sched_param = type { i32 }
%struct.timespec = type { i64, i64 }
@__const.main.param = %struct.sched_param { i32 1 }
@.str = [36 x i8] c"test <sleep_timeout in ns> <iters>\0A\00"
@.str.1 = [4 x i8] c"%lu\00"
@.str.2 = [3 x i8] c"%d\00"
@SCHED_FIFO = global i32 0
@.str.3 = [20 x i8] c"sched_setscheduler:\00"
@CLOCK_MONOTONIC = global i32 0
@.str.4 = [26 x i8] c"iter = %d ts_delta = %lu\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.timespec
%11 = alloca %struct.timespec
%12 = alloca %struct.timespec
%13 = alloca %struct.timespec
%14 = alloca %struct.sched_param
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = bitcast %struct.sched_param* %14 to i8*
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 3
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
store i32 -2, i32* %3
br label %84
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32 (i8*, i8*, ...) @sscanf(i8* %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64* %6)
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 2
%27 = load i8*, i8** %26
%28 = call i32 (i8*, i8*, ...) @sscanf(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %8)
%29 = load i32, i32* @SCHED_FIFO
%30 = call i64 @sched_setscheduler(i32 0, i32 %29, %struct.sched_param* %14)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %20
%33 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %34
34: ; preds = %32, %20
%35 = load i32, i32* %8
%36 = sext i32 %35 to i64
%37 = mul i64 8, %36
%38 = trunc i64 %37 to i32
%39 = call i64 @malloc(i32 %38)
%40 = inttoptr i64 %39 to i64*
store i64* %40, i64** %7
store i32 0, i32* %9
%41 = getelementptr inbounds %struct.timespec, %struct.timespec* %12, i32 0, i32 0
store i64 0, i64* %41
%42 = load i64, i64* %6
%43 = getelementptr inbounds %struct.timespec, %struct.timespec* %12, i32 0, i32 1
store i64 %42, i64* %43
br label %44
44: ; preds = %49, %34
%45 = load i32, i32* %9
%46 = add nsw i32 %45, 1
store i32 %46, i32* %9
%47 = load i32, i32* %8
%48 = icmp slt i32 %45, %47
br i1 %48, label %49, label %83
49: ; preds = %44
%50 = getelementptr inbounds %struct.timespec, %struct.timespec* %13, i32 0, i32 1
store i64 0, i64* %50
%51 = getelementptr inbounds %struct.timespec, %struct.timespec* %13, i32 0, i32 0
store i64 0, i64* %51
%52 = load i32, i32* @CLOCK_MONOTONIC
%53 = call i32 @clock_gettime(i32 %52, %struct.timespec* %10)
%54 = load i64, i64* %6
%55 = udiv i64 %54, 1000
%56 = call i32 @usleep(i64 %55)
%57 = load i32, i32* @CLOCK_MONOTONIC
%58 = call i32 @clock_gettime(i32 %57, %struct.timespec* %11)
%59 = getelementptr inbounds %struct.timespec, %struct.timespec* %11, i32 0, i32 0
%60 = load i64, i64* %59
%61 = mul i64 %60, 1000000000
%62 = getelementptr inbounds %struct.timespec, %struct.timespec* %11, i32 0, i32 1
%63 = load i64, i64* %62
%64 = add i64 %61, %63
%65 = getelementptr inbounds %struct.timespec, %struct.timespec* %10, i32 0, i32 0
%66 = load i64, i64* %65
%67 = mul i64 %66, 1000000000
%68 = sub i64 %64, %67
%69 = getelementptr inbounds %struct.timespec, %struct.timespec* %10, i32 0, i32 1
%70 = load i64, i64* %69
%71 = sub i64 %68, %70
%72 = load i64*, i64** %7
%73 = load i32, i32* %9
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds i64, i64* %72, i64 %74
store i64 %71, i64* %75
%76 = load i32, i32* %9
%77 = load i64*, i64** %7
%78 = load i32, i32* %9
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds i64, i64* %77, i64 %79
%81 = load i64, i64* %80
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %76, i64 %81)
br label %44
83: ; preds = %44
store i32 0, i32* %3
br label %84
84: ; preds = %83, %18
%85 = load i32, i32* %3
ret i32 %85
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
%struct.sched_param = type { i32 }
@.str.1 = [4 x i8] c"%lu\00"
@.str.2 = [3 x i8] c"%d\00"
@SCHED_FIFO = global i32 0
@.str.3 = [20 x i8] c"sched_setscheduler:\00"
@CLOCK_MONOTONIC = global i32 0
@.str.4 = [26 x i8] c"iter = %d ts_delta = %lu\0A\00"
@str = [35 x i8] c"test <sleep_timeout in ns> <iters>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i64
%4 = alloca i32
%5 = alloca %struct.timespec
%6 = alloca %struct.timespec
%7 = alloca %struct.sched_param
%8 = bitcast i64* %3 to i8*
%9 = bitcast i32* %4 to i8*
%10 = bitcast %struct.timespec* %5 to i8*
%11 = bitcast %struct.timespec* %6 to i8*
%12 = bitcast %struct.sched_param* %7 to i8*
%13 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %7, i64 0, i32 0
store i32 1, i32* %13
%14 = icmp slt i32 %0, 3
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
br label %65
17: ; preds = %2
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = call i32 (i8*, i8*, ...) @sscanf(i8* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64* %3)
%21 = getelementptr inbounds i8*, i8** %1, i64 2
%22 = load i8*, i8** %21
%23 = call i32 (i8*, i8*, ...) @sscanf(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%24 = load i32, i32* @SCHED_FIFO
%25 = call i64 @sched_setscheduler(i32 0, i32 %24, %struct.sched_param* %7)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %17
%28 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %29
29: ; preds = %27, %17
%30 = load i32, i32* %4
%31 = shl i32 %30, 3
%32 = call i64 @malloc(i32 %31)
%33 = inttoptr i64 %32 to i64*
%34 = load i32, i32* %4
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %65
36: ; preds = %29
%37 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i64 0, i32 0
%38 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i64 0, i32 1
%39 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 0
%40 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 1
br label %41
41: ; preds = %36, %41
%42 = phi i64 [ 1, %36 ], [ %61, %41 ]
%43 = load i32, i32* @CLOCK_MONOTONIC
%44 = call i32 @clock_gettime(i32 %43, %struct.timespec* %5)
%45 = load i64, i64* %3
%46 = udiv i64 %45, 1000
%47 = call i32 @usleep(i64 %46)
%48 = load i32, i32* @CLOCK_MONOTONIC
%49 = call i32 @clock_gettime(i32 %48, %struct.timespec* %6)
%50 = load i64, i64* %37
%51 = load i64, i64* %38
%52 = load i64, i64* %39
%53 = load i64, i64* %40
%54 = sub i64 %50, %52
%55 = mul i64 %54, 1000000000
%56 = sub i64 %51, %53
%57 = add i64 %56, %55
%58 = getelementptr inbounds i64, i64* %33, i64 %42
store i64 %57, i64* %58
%59 = trunc i64 %42 to i32
%60 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %59, i64 %57)
%61 = add nuw nsw i64 %42, 1
%62 = load i32, i32* %4
%63 = sext i32 %62 to i64
%64 = icmp slt i64 %42, %63
br i1 %64, label %41, label %65
65: ; preds = %41, %29, %15
%66 = phi i32 [ -2, %15 ], [ 0, %29 ], [ 0, %41 ]
ret i32 %66
}