#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*/ sp;

/* Variables and functions */
int EINVAL;
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ memset(struct sched_param *, int /*<<< orphan*/, int);
int sched_get_priority_max(int);
int sched_get_priority_min(int);
int sched_setparam(int /*<<< orphan*/, struct sched_param *);
int sched_setscheduler(int /*<<< orphan*/, int, struct sched_param *);
int /*<<< orphan*/ whine(char *);
int set_sched_status(int policy, int priority) {
  int ret, min_prio, max_prio;
  struct sched_param sp;
  max_prio = sched_get_priority_max(policy);
  min_prio = sched_get_priority_min(policy);
  if (max_prio == -1 || min_prio == -1)
    whine("Cannot determine scheduler prio limits!\n");
  else if (priority < min_prio)
    priority = min_prio;
  else if (priority > max_prio)
    priority = max_prio;
  memset(&sp, 0, sizeof(sp));
  sp.sched_priority = priority;
  ret = sched_setscheduler(getpid(), policy, &sp);
  if (ret) {
    whine("Cannot set scheduler policy!\n");
    return -EINVAL;
  }
  ret = sched_setparam(getpid(), &sp);
  if (ret) {
    whine("Cannot set scheduler prio!\n");
    return -EINVAL;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_sched_status>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   1090 <sched_get_priority_max@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1060 <sched_get_priority_min@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
je     11a8 <set_sched_status+0x38>
cmpl   $0xffffffff,-0x14(%rbp)
jne    11b9 <set_sched_status+0x49>
lea    0xe51(%rip),%rdi
call   1070 <whine@plt>
jmp    11ec <set_sched_status+0x7c>
mov    -0xc(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11d0 <set_sched_status+0x60>
mov    -0x14(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    11e7 <set_sched_status+0x77>
mov    -0xc(%rbp),%eax
cmp    -0x18(%rbp),%eax
jle    11e2 <set_sched_status+0x72>
mov    -0x18(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    11e7 <set_sched_status+0x77>
jmp    11ec <set_sched_status+0x7c>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
mov    -0x8(%rbp),%esi
lea    -0x20(%rbp),%rdx
call   1040 <sched_setscheduler@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     1243 <set_sched_status+0xd3>
lea    0xdfe(%rip),%rdi
call   1070 <whine@plt>
xor    %eax,%eax
mov    0x2da7(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1288 <set_sched_status+0x118>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
lea    -0x20(%rbp),%rsi
call   1080 <sched_setparam@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     1281 <set_sched_status+0x111>
lea    0xdde(%rip),%rdi
call   1070 <whine@plt>
xor    %eax,%eax
mov    0x2d69(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1288 <set_sched_status+0x118>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_sched_status>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%r15d
call   1090 <sched_get_priority_max@plt>
mov    %eax,%ebp
mov    %r15d,%edi
call   1060 <sched_get_priority_min@plt>
cmp    $0xffffffff,%ebp
je     11a6 <set_sched_status+0x36>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11a6 <set_sched_status+0x36>
cmp    %r14d,%ebx
jg     11b5 <set_sched_status+0x45>
cmp    %r14d,%ebp
cmovl  %ebp,%r14d
jmp    11b2 <set_sched_status+0x42>
lea    0xe53(%rip),%rdi
call   1070 <whine@plt>
mov    %r14d,%ebx
mov    %rsp,%rbp
xor    %r14d,%r14d
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    %ebx,(%rsp)
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%edi
mov    %r15d,%esi
mov    %rbp,%rdx
call   1040 <sched_setscheduler@plt>
test   %eax,%eax
je     11ee <set_sched_status+0x7e>
lea    0xe3d(%rip),%rdi
jmp    120a <set_sched_status+0x9a>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %rsp,%rsi
mov    %eax,%edi
call   1080 <sched_setparam@plt>
test   %eax,%eax
je     1219 <set_sched_status+0xa9>
lea    0xe3d(%rip),%rdi
call   1070 <whine@plt>
mov    0x2dca(%rip),%rax
sub    (%rax),%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sched_param = type { i32 }
@.str = [41 x i8] c"Cannot determine scheduler prio limits!\0A\00"
@.str.1 = [30 x i8] c"Cannot set scheduler policy!\0A\00"
@EINVAL = global i32 0
@.str.2 = [28 x i8] c"Cannot set scheduler prio!\0A\00"
define i32 @set_sched_status(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sched_param
store i32 %0, i32* %4
store i32 %1, i32* %5
%10 = load i32, i32* %4
%11 = call i32 @sched_get_priority_max(i32 %10)
store i32 %11, i32* %8
%12 = load i32, i32* %4
%13 = call i32 @sched_get_priority_min(i32 %12)
store i32 %13, i32* %7
%14 = load i32, i32* %8
%15 = icmp eq i32 %14, -1
br i1 %15, label %19, label %16
16: ; preds = %2
%17 = load i32, i32* %7
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %16, %2
%20 = call i32 @whine(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
br label %35
21: ; preds = %16
%22 = load i32, i32* %5
%23 = load i32, i32* %7
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = load i32, i32* %7
store i32 %26, i32* %5
br label %34
27: ; preds = %21
%28 = load i32, i32* %5
%29 = load i32, i32* %8
%30 = icmp sgt i32 %28, %29
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = load i32, i32* %8
store i32 %32, i32* %5
br label %33
33: ; preds = %31, %27
br label %34
34: ; preds = %33, %25
br label %35
35: ; preds = %34, %19
%36 = call i32 @memset(%struct.sched_param* %9, i32 0, i32 4)
%37 = load i32, i32* %5
%38 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %9, i32 0, i32 0
store i32 %37, i32* %38
%39 = call i32 (...) @getpid()
%40 = load i32, i32* %4
%41 = call i32 @sched_setscheduler(i32 %39, i32 %40, %struct.sched_param* %9)
store i32 %41, i32* %6
%42 = load i32, i32* %6
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %35
%45 = call i32 @whine(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%46 = load i32, i32* @EINVAL
%47 = sub nsw i32 0, %46
store i32 %47, i32* %3
br label %58
48: ; preds = %35
%49 = call i32 (...) @getpid()
%50 = call i32 @sched_setparam(i32 %49, %struct.sched_param* %9)
store i32 %50, i32* %6
%51 = load i32, i32* %6
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %48
%54 = call i32 @whine(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%55 = load i32, i32* @EINVAL
%56 = sub nsw i32 0, %55
store i32 %56, i32* %3
br label %58
57: ; preds = %48
store i32 0, i32* %3
br label %58
58: ; preds = %57, %53, %44
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
%struct.sched_param = type { i32 }
@.str = [41 x i8] c"Cannot determine scheduler prio limits!\0A\00"
@.str.1 = [30 x i8] c"Cannot set scheduler policy!\0A\00"
@EINVAL = global i32 0
@.str.2 = [28 x i8] c"Cannot set scheduler prio!\0A\00"
define i32 @set_sched_status(i32 %0, i32 %1) {
%3 = alloca %struct.sched_param
%4 = bitcast %struct.sched_param* %3 to i8*
%5 = tail call i32 @sched_get_priority_max(i32 %0)
%6 = tail call i32 @sched_get_priority_min(i32 %0)
%7 = icmp eq i32 %5, -1
%8 = icmp eq i32 %6, -1
%9 = select i1 %7, i1 true, i1 %8
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @whine(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
br label %17
12: ; preds = %2
%13 = icmp sgt i32 %6, %1
br i1 %13, label %17, label %14
14: ; preds = %12
%15 = icmp slt i32 %5, %1
%16 = select i1 %15, i32 %5, i32 %1
br label %17
17: ; preds = %14, %12, %10
%18 = phi i32 [ %1, %10 ], [ %6, %12 ], [ %16, %14 ]
%19 = call i32 @memset(%struct.sched_param* %3, i32 0, i32 4)
%20 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %3, i64 0, i32 0
store i32 %18, i32* %20
%21 = call i32 (...) @getpid()
%22 = call i32 @sched_setscheduler(i32 %21, i32 %0, %struct.sched_param* %3)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %17
%25 = call i32 (...) @getpid()
%26 = call i32 @sched_setparam(i32 %25, %struct.sched_param* %3)
%27 = icmp eq i32 %26, 0
br i1 %27, label %33, label %28
28: ; preds = %24, %17
%29 = phi i8* [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), %17 ], [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), %24 ]
%30 = call i32 @whine(i8* %29)
%31 = load i32, i32* @EINVAL
%32 = sub nsw i32 0, %31
br label %33
33: ; preds = %28, %24
%34 = phi i32 [ 0, %24 ], [ %32, %28 ]
ret i32 %34
}