#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 sched_param {
  int sched_priority;
};

/* Variables and functions */
int SCHED_OTHER;
int SCHED_RR;
int /*<<< orphan*/ pthread_self();
int pthread_setschedparam(int /*<<< orphan*/, int const, struct sched_param *);
int sched_get_priority_max(int const);
int sched_get_priority_min(int const);
int numanuma__set_thread_priority(const double prio) {
  const int policy = (prio <= 0.0) ? SCHED_OTHER : SCHED_RR;
  const int min_pri = sched_get_priority_min(policy);
  const int max_pri = sched_get_priority_max(policy);
  if (min_pri == -1 || max_pri == -1)
    return -1;
  struct sched_param sp;
  sp.sched_priority = (int)(prio * (max_pri - min_pri)) + min_pri;
  return pthread_setschedparam(pthread_self(), policy, &sp);
}


x86 assembly (clang, -O0):
<numanuma__set_thread_priority>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x10(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x10(%rbp),%xmm0
jb     116c <numanuma__set_thread_priority+0x2c>
mov    0x2e7e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    1178 <numanuma__set_thread_priority+0x38>
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <sched_get_priority_min@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <sched_get_priority_max@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
je     11a8 <numanuma__set_thread_priority+0x68>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11b4 <numanuma__set_thread_priority+0x74>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e9 <numanuma__set_thread_priority+0xa9>
movsd  -0x10(%rbp),%xmm0
mov    -0x1c(%rbp),%eax
sub    -0x18(%rbp),%eax
cvtsi2sd %eax,%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1050 <pthread_self@plt>
mov    %eax,%edi
mov    -0x14(%rbp),%esi
lea    -0x20(%rbp),%rdx
call   1030 <pthread_setschedparam@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<numanuma__set_thread_priority>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
xorpd  %xmm1,%xmm1
ucomisd %xmm0,%xmm1
movsd  %xmm0,0x10(%rsp)
jb     115f <numanuma__set_thread_priority+0x1f>
mov    0x2e83(%rip),%rax
jmp    1166 <numanuma__set_thread_priority+0x26>
mov    0x2e6a(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1040 <sched_get_priority_min@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1060 <sched_get_priority_max@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0xffffffff,%ebp
je     11b7 <numanuma__set_thread_priority+0x77>
cmp    $0xffffffff,%ecx
je     11b7 <numanuma__set_thread_priority+0x77>
sub    %ebp,%ecx
xorps  %xmm0,%xmm0
cvtsi2sd %ecx,%xmm0
mulsd  0x10(%rsp),%xmm0
cvttsd2si %xmm0,%eax
add    %ebp,%eax
mov    %eax,0x8(%rsp)
xor    %eax,%eax
call   1050 <pthread_self@plt>
lea    0x8(%rsp),%rdx
mov    %eax,%edi
mov    %ebx,%esi
call   1030 <pthread_setschedparam@plt>
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sched_param = type { i32 }
@SCHED_OTHER = global i32 0
@SCHED_RR = global i32 0
define i32 @numanuma__set_thread_priority(double %0) {
%2 = alloca i32
%3 = alloca double
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sched_param
store double %0, double* %3
%8 = load double, double* %3
%9 = fcmp ole double %8, 0.000000e+00
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = load i32, i32* @SCHED_OTHER
br label %14
12: ; preds = %1
%13 = load i32, i32* @SCHED_RR
br label %14
14: ; preds = %12, %10
%15 = phi i32 [ %11, %10 ], [ %13, %12 ]
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = call i32 @sched_get_priority_min(i32 %16)
store i32 %17, i32* %5
%18 = load i32, i32* %4
%19 = call i32 @sched_get_priority_max(i32 %18)
store i32 %19, i32* %6
%20 = load i32, i32* %5
%21 = icmp eq i32 %20, -1
br i1 %21, label %25, label %22
22: ; preds = %14
%23 = load i32, i32* %6
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %26
25: ; preds = %22, %14
store i32 -1, i32* %2
br label %40
26: ; preds = %22
%27 = load double, double* %3
%28 = load i32, i32* %6
%29 = load i32, i32* %5
%30 = sub nsw i32 %28, %29
%31 = sitofp i32 %30 to double
%32 = fmul double %27, %31
%33 = fptosi double %32 to i32
%34 = load i32, i32* %5
%35 = add nsw i32 %33, %34
%36 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %7, i32 0, i32 0
store i32 %35, i32* %36
%37 = call i32 (...) @pthread_self()
%38 = load i32, i32* %4
%39 = call i32 @pthread_setschedparam(i32 %37, i32 %38, %struct.sched_param* %7)
store i32 %39, i32* %2
br label %40
40: ; preds = %26, %25
%41 = load i32, i32* %2
ret i32 %41
}

LLVM IR (-O2):
%struct.sched_param = type { i32 }
@SCHED_OTHER = global i32 0
@SCHED_RR = global i32 0
define i32 @numanuma__set_thread_priority(double %0) {
%2 = alloca %struct.sched_param
%3 = fcmp ole double %0, 0.000000e+00
%4 = load i32, i32* @SCHED_OTHER
%5 = load i32, i32* @SCHED_RR
%6 = select i1 %3, i32 %4, i32 %5
%7 = tail call i32 @sched_get_priority_min(i32 %6)
%8 = tail call i32 @sched_get_priority_max(i32 %6)
%9 = icmp eq i32 %7, -1
%10 = icmp eq i32 %8, -1
%11 = select i1 %9, i1 true, i1 %10
br i1 %11, label %22, label %12
12: ; preds = %1
%13 = bitcast %struct.sched_param* %2 to i8*
%14 = sub nsw i32 %8, %7
%15 = sitofp i32 %14 to double
%16 = fmul double %15, %0
%17 = fptosi double %16 to i32
%18 = add nsw i32 %7, %17
%19 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %2, i64 0, i32 0
store i32 %18, i32* %19
%20 = tail call i32 (...) @pthread_self()
%21 = call i32 @pthread_setschedparam(i32 %20, i32 %6, %struct.sched_param* %2)
br label %22
22: ; preds = %1, %12
%23 = phi i32 [ %21, %12 ], [ -1, %1 ]
ret i32 %23
}