#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_FIFO;
int SCHED_RR;
int /*<<< orphan*/ __NR_gettid;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(int /*<<< orphan*/ *);
int sched_getscheduler(int /*<<< orphan*/);
int sched_setscheduler(int /*<<< orphan*/, int, struct sched_param *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ syscall(int /*<<< orphan*/);
void _set_priority(int prio, int policy) {
  int err;
  if (policy != SCHED_RR && policy != SCHED_FIFO)
    return;
  struct sched_param param;
  param.sched_priority = prio;
  err = sched_setscheduler(0, policy, &param);
  if (err) {
    fprintf(stderr, "Could not set priority for %ld\n", syscall(__NR_gettid));
    perror(NULL);
  }
  if (sched_getscheduler(0) != policy) {
    fprintf(stderr, "Error setting policy for %ld\n", syscall(__NR_gettid));
  }
}


x86 assembly (clang, -O0):
<_set_priority>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    0x2e58(%rip),%rcx
cmp    (%rcx),%eax
je     1187 <_set_priority+0x37>
mov    -0x8(%rbp),%eax
mov    0x2e66(%rip),%rcx
cmp    (%rcx),%eax
je     1187 <_set_priority+0x37>
jmp    1217 <_set_priority+0xc7>
mov    -0x4(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%esi
xor    %edi,%edi
lea    -0x10(%rbp),%rdx
call   1040 <sched_setscheduler@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11dc <_set_priority+0x8c>
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
call   1060 <syscall@plt>
mov    -0x14(%rbp),%edi
mov    %eax,%edx
lea    0xe32(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <perror@plt>
xor    %edi,%edi
call   1030 <sched_getscheduler@plt>
cmp    -0x8(%rbp),%eax
je     1217 <_set_priority+0xc7>
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
call   1060 <syscall@plt>
mov    -0x18(%rbp),%edi
mov    %eax,%edx
lea    0xe0e(%rip),%rsi
call   1050 <fprintf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_set_priority>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    0x2e64(%rip),%rax
cmp    %esi,(%rax)
je     116b <_set_priority+0x1b>
mov    0x2e79(%rip),%rax
cmp    %ebx,(%rax)
jne    11b0 <_set_priority+0x60>
mov    %edi,(%rsp)
mov    %rsp,%rdx
xor    %edi,%edi
mov    %ebx,%esi
call   1040 <sched_setscheduler@plt>
test   %eax,%eax
jne    11b7 <_set_priority+0x67>
xor    %edi,%edi
call   1030 <sched_getscheduler@plt>
cmp    %ebx,%eax
je     11b0 <_set_priority+0x60>
mov    0x2e40(%rip),%rax
mov    (%rax),%ebx
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
call   1060 <syscall@plt>
lea    0xe79(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   1050 <fprintf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e12(%rip),%rax
mov    (%rax),%ebp
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
call   1060 <syscall@plt>
lea    0xe2b(%rip),%rsi
mov    %ebp,%edi
mov    %eax,%edx
call   1050 <fprintf@plt>
xor    %edi,%edi
call   1070 <perror@plt>
jmp    117e <_set_priority+0x2e>

LLVM IR (-O0):
%struct.sched_param = type { i32 }
@SCHED_RR = global i32 0
@SCHED_FIFO = global i32 0
@stderr = global i32 0
@.str = [32 x i8] c"Could not set priority for %ld\0A\00"
@__NR_gettid = global i32 0
@.str.1 = [30 x i8] c"Error setting policy for %ld\0A\00"
define void @_set_priority(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sched_param
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = load i32, i32* @SCHED_RR
%9 = icmp ne i32 %7, %8
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = load i32, i32* %4
%12 = load i32, i32* @SCHED_FIFO
%13 = icmp ne i32 %11, %12
br i1 %13, label %14, label %15
14: ; preds = %10
br label %37
15: ; preds = %10, %2
%16 = load i32, i32* %3
%17 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %6, i32 0, i32 0
store i32 %16, i32* %17
%18 = load i32, i32* %4
%19 = call i32 @sched_setscheduler(i32 0, i32 %18, %struct.sched_param* %6)
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %15
%23 = load i32, i32* @stderr
%24 = load i32, i32* @__NR_gettid
%25 = call i32 @syscall(i32 %24)
%26 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %25)
%27 = call i32 @perror(i32* null)
br label %28
28: ; preds = %22, %15
%29 = call i32 @sched_getscheduler(i32 0)
%30 = load i32, i32* %4
%31 = icmp ne i32 %29, %30
br i1 %31, label %32, label %37
32: ; preds = %28
%33 = load i32, i32* @stderr
%34 = load i32, i32* @__NR_gettid
%35 = call i32 @syscall(i32 %34)
%36 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %14, %32, %28
ret void
}

LLVM IR (-O2):
%struct.sched_param = type { i32 }
@SCHED_RR = global i32 0
@SCHED_FIFO = global i32 0
@stderr = global i32 0
@.str = [32 x i8] c"Could not set priority for %ld\0A\00"
@__NR_gettid = global i32 0
@.str.1 = [30 x i8] c"Error setting policy for %ld\0A\00"
define void @_set_priority(i32 %0, i32 %1) {
%3 = alloca %struct.sched_param
%4 = load i32, i32* @SCHED_RR
%5 = icmp eq i32 %4, %1
%6 = load i32, i32* @SCHED_FIFO
%7 = icmp eq i32 %6, %1
%8 = select i1 %5, i1 true, i1 %7
br i1 %8, label %9, label %29
9: ; preds = %2
%10 = bitcast %struct.sched_param* %3 to i8*
%11 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %3, i64 0, i32 0
store i32 %0, i32* %11
%12 = call i32 @sched_setscheduler(i32 0, i32 %1, %struct.sched_param* %3)
%13 = icmp eq i32 %12, 0
br i1 %13, label %20, label %14
14: ; preds = %9
%15 = load i32, i32* @stderr
%16 = load i32, i32* @__NR_gettid
%17 = call i32 @syscall(i32 %16)
%18 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = call i32 @perror(i32* null)
br label %20
20: ; preds = %14, %9
%21 = call i32 @sched_getscheduler(i32 0)
%22 = icmp eq i32 %21, %1
br i1 %22, label %28, label %23
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = load i32, i32* @__NR_gettid
%26 = call i32 @syscall(i32 %25)
%27 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %26)
br label %28
28: ; preds = %23, %20
br label %29
29: ; preds = %2, %28
ret void
}