#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;
};
typedef int /*<<< orphan*/ sched;

/* Variables and functions */
int /*<<< orphan*/ SCHED_RR;
int /*<<< orphan*/ memset(struct sched_param *, int /*<<< orphan*/, int);
int sched_get_priority_max(int /*<<< orphan*/);
int sched_setscheduler(int /*<<< orphan*/, int /*<<< orphan*/,
                       struct sched_param *);
int thread_setPriority(const int priority) {
  struct sched_param sched;
  memset(&sched, 0, sizeof(sched));
  int maxPriority = sched_get_priority_max(SCHED_RR);
  if (priority > maxPriority)
    sched.sched_priority = maxPriority;
  else
    sched.sched_priority = priority;
  return sched_setscheduler(0, SCHED_RR, &sched);
}


x86 assembly (clang, -O0):
<thread_setPriority>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    0x2e86(%rip),%rax
mov    (%rax),%edi
call   1050 <sched_get_priority_max@plt>
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%eax
cmp    -0xc(%rbp),%eax
jle    1173 <thread_setPriority+0x43>
mov    -0xc(%rbp),%eax
mov    %eax,-0x8(%rbp)
jmp    1179 <thread_setPriority+0x49>
mov    -0x4(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x8(%rbp),%rdx
call   1030 <sched_setscheduler@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thread_setPriority>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    0x2e82(%rip),%r15
mov    (%r15),%edi
call   1050 <sched_get_priority_max@plt>
cmp    %ebx,%eax
cmovl  %eax,%ebx
mov    %ebx,0x8(%rsp)
mov    (%r15),%esi
xor    %edi,%edi
mov    %r14,%rdx
call   1030 <sched_setscheduler@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sched_param = type { i32 }
@SCHED_RR = global i32 0
define i32 @thread_setPriority(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sched_param
%4 = alloca i32
store i32 %0, i32* %2
%5 = call i32 @memset(%struct.sched_param* %3, i32 0, i32 4)
%6 = load i32, i32* @SCHED_RR
%7 = call i32 @sched_get_priority_max(i32 %6)
store i32 %7, i32* %4
%8 = load i32, i32* %2
%9 = load i32, i32* %4
%10 = icmp sgt i32 %8, %9
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32, i32* %4
%13 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %3, i32 0, i32 0
store i32 %12, i32* %13
br label %17
14: ; preds = %1
%15 = load i32, i32* %2
%16 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %3, i32 0, i32 0
store i32 %15, i32* %16
br label %17
17: ; preds = %14, %11
%18 = load i32, i32* @SCHED_RR
%19 = call i32 @sched_setscheduler(i32 0, i32 %18, %struct.sched_param* %3)
ret i32 %19
}

LLVM IR (-O2):
%struct.sched_param = type { i32 }
@SCHED_RR = global i32 0
define i32 @thread_setPriority(i32 %0) {
%2 = alloca %struct.sched_param
%3 = bitcast %struct.sched_param* %2 to i8*
%4 = call i32 @memset(%struct.sched_param* %2, i32 0, i32 4)
%5 = load i32, i32* @SCHED_RR
%6 = call i32 @sched_get_priority_max(i32 %5)
%7 = icmp slt i32 %6, %0
%8 = select i1 %7, i32 %6, i32 %0
%9 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %2, i64 0, i32 0
store i32 %8, i32* %9
%10 = load i32, i32* @SCHED_RR
%11 = call i32 @sched_setscheduler(i32 0, i32 %10, %struct.sched_param* %2)
ret i32 %11
}