#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 /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int, ...);
int /*<<< orphan*/ pthread_getschedparam(int /*<<< orphan*/, int *,
                                         struct sched_param *);
int /*<<< orphan*/ pthread_self();
scalar_t__ pthread_setschedparam(int /*<<< orphan*/, int, struct sched_param *);
int sched_get_priority_max(int);
int sched_get_priority_min(int);
int get_rt_prio(int prio) {
  struct sched_param param;
  int policy;
  pthread_getschedparam(pthread_self(), &policy, &param);
  printf("Current schedparam: policy %d, prio %d\n", policy,
         param.sched_priority);
  int sched = SCHED_FIFO;
  int min = sched_get_priority_min(sched);
  int max = sched_get_priority_max(sched);
  param.sched_priority = prio;
  if (prio > max || prio < min) {
    printf("Requested prio %d outside current min/max prios: %d/%d\n", prio,
           min, max);
    if (prio > max) {
      param.sched_priority = max;
    } else {
      param.sched_priority = min;
    }
  }
  if (pthread_setschedparam(pthread_self(), sched, &param)) {
    perror("setschedparam failed!");
    return -1;
  } else {
    pthread_getschedparam(pthread_self(), &policy, &param);
    printf("New schedparam: policy %d, prio %d\n", policy,
           param.sched_priority);
  }
  return 0;
}


x86 assembly (clang, -O0):
<get_rt_prio>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    $0x0,%al
call   1070 <pthread_self@plt>
mov    %eax,%edi
lea    -0x14(%rbp),%rsi
lea    -0x10(%rbp),%rdx
call   1050 <pthread_getschedparam@plt>
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%edx
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   1060 <sched_get_priority_min@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
call   1090 <sched_get_priority_max@plt>
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x20(%rbp),%eax
jg     11e5 <get_rt_prio+0x75>
mov    -0x8(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    121e <get_rt_prio+0xae>
mov    -0x8(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%eax
cmp    -0x20(%rbp),%eax
jle    1213 <get_rt_prio+0xa3>
mov    -0x20(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1219 <get_rt_prio+0xa9>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    121e <get_rt_prio+0xae>
mov    $0x0,%al
call   1070 <pthread_self@plt>
mov    %eax,%edi
mov    -0x18(%rbp),%esi
lea    -0x10(%rbp),%rdx
call   1030 <pthread_setschedparam@plt>
cmp    $0x0,%rax
je     1255 <get_rt_prio+0xe5>
lea    0xe1c(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1286 <get_rt_prio+0x116>
mov    $0x0,%al
call   1070 <pthread_self@plt>
mov    %eax,%edi
lea    -0x14(%rbp),%rsi
lea    -0x10(%rbp),%rdx
call   1050 <pthread_getschedparam@plt>
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%edx
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_rt_prio>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
xor    %eax,%eax
call   1070 <pthread_self@plt>
lea    0x4(%rsp),%rsi
mov    %rsp,%rdx
mov    %eax,%edi
call   1050 <pthread_getschedparam@plt>
mov    0x4(%rsp),%esi
mov    (%rsp),%edx
lea    0xe63(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%r15d
mov    %r15d,%edi
call   1060 <sched_get_priority_min@plt>
mov    %eax,%r14d
mov    %r15d,%edi
call   1090 <sched_get_priority_max@plt>
mov    %eax,%ebp
mov    %ebx,(%rsp)
cmp    %ebx,%eax
jl     11cf <get_rt_prio+0x5f>
cmp    %ebx,%r14d
jle    11f1 <get_rt_prio+0x81>
lea    0xe52(%rip),%rdi
mov    %ebx,%esi
mov    %r14d,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
cmp    %ebx,%ebp
jge    11ed <get_rt_prio+0x7d>
mov    %ebp,(%rsp)
jmp    11f1 <get_rt_prio+0x81>
mov    %r14d,(%rsp)
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <pthread_self@plt>
mov    %rsp,%rdx
mov    %eax,%edi
mov    %r15d,%esi
call   1030 <pthread_setschedparam@plt>
test   %rax,%rax
jne    1244 <get_rt_prio+0xd4>
xor    %eax,%eax
call   1070 <pthread_self@plt>
lea    0x4(%rsp),%rsi
mov    %rsp,%rdx
mov    %eax,%edi
call   1050 <pthread_getschedparam@plt>
mov    0x4(%rsp),%esi
mov    (%rsp),%edx
lea    0xe46(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe15(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1237 <get_rt_prio+0xc7>

LLVM IR (-O0):
%struct.sched_param = type { i32 }
@.str = [40 x i8] c"Current schedparam: policy %d, prio %d\0A\00"
@SCHED_FIFO = global i32 0
@.str.1 = [56 x i8] c"Requested prio %d outside current min/max prios: %d/%d\0A\00"
@.str.2 = [22 x i8] c"setschedparam failed!\00"
@.str.3 = [36 x i8] c"New schedparam: policy %d, prio %d\0A\00"
define i32 @get_rt_prio(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sched_param
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
%9 = call i32 (...) @pthread_self()
%10 = call i32 @pthread_getschedparam(i32 %9, i32* %5, %struct.sched_param* %4)
%11 = load i32, i32* %5
%12 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %4, i32 0, i32 0
%13 = load i32, i32* %12
%14 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %11, i32 %13)
%15 = load i32, i32* @SCHED_FIFO
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = call i32 @sched_get_priority_min(i32 %16)
store i32 %17, i32* %7
%18 = load i32, i32* %6
%19 = call i32 @sched_get_priority_max(i32 %18)
store i32 %19, i32* %8
%20 = load i32, i32* %3
%21 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %4, i32 0, i32 0
store i32 %20, i32* %21
%22 = load i32, i32* %3
%23 = load i32, i32* %8
%24 = icmp sgt i32 %22, %23
br i1 %24, label %29, label %25
25: ; preds = %1
%26 = load i32, i32* %3
%27 = load i32, i32* %7
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %44
29: ; preds = %25, %1
%30 = load i32, i32* %3
%31 = load i32, i32* %7
%32 = load i32, i32* %8
%33 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0), i32 %30, i32 %31, i32 %32)
%34 = load i32, i32* %3
%35 = load i32, i32* %8
%36 = icmp sgt i32 %34, %35
br i1 %36, label %37, label %40
37: ; preds = %29
%38 = load i32, i32* %8
%39 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %4, i32 0, i32 0
store i32 %38, i32* %39
br label %43
40: ; preds = %29
%41 = load i32, i32* %7
%42 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %4, i32 0, i32 0
store i32 %41, i32* %42
br label %43
43: ; preds = %40, %37
br label %44
44: ; preds = %43, %25
%45 = call i32 (...) @pthread_self()
%46 = load i32, i32* %6
%47 = call i64 @pthread_setschedparam(i32 %45, i32 %46, %struct.sched_param* %4)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %44
%50 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %59
51: ; preds = %44
%52 = call i32 (...) @pthread_self()
%53 = call i32 @pthread_getschedparam(i32 %52, i32* %5, %struct.sched_param* %4)
%54 = load i32, i32* %5
%55 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %4, i32 0, i32 0
%56 = load i32, i32* %55
%57 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %54, i32 %56)
br label %58
58: ; preds = %51
store i32 0, i32* %2
br label %59
59: ; preds = %58, %49
%60 = load i32, i32* %2
ret i32 %60
}

LLVM IR (-O2):
%struct.sched_param = type { i32 }
@.str = [40 x i8] c"Current schedparam: policy %d, prio %d\0A\00"
@SCHED_FIFO = global i32 0
@.str.1 = [56 x i8] c"Requested prio %d outside current min/max prios: %d/%d\0A\00"
@.str.2 = [22 x i8] c"setschedparam failed!\00"
@.str.3 = [36 x i8] c"New schedparam: policy %d, prio %d\0A\00"
define i32 @get_rt_prio(i32 %0) {
%2 = alloca %struct.sched_param
%3 = alloca i32
%4 = bitcast %struct.sched_param* %2 to i8*
%5 = bitcast i32* %3 to i8*
%6 = tail call i32 (...) @pthread_self()
%7 = call i32 @pthread_getschedparam(i32 %6, i32* %3, %struct.sched_param* %2)
%8 = load i32, i32* %3
%9 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %2, i64 0, i32 0
%10 = load i32, i32* %9
%11 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %8, i32 %10)
%12 = load i32, i32* @SCHED_FIFO
%13 = call i32 @sched_get_priority_min(i32 %12)
%14 = call i32 @sched_get_priority_max(i32 %12)
store i32 %0, i32* %9
%15 = icmp slt i32 %14, %0
%16 = icmp sgt i32 %13, %0
%17 = select i1 %15, i1 true, i1 %16
br i1 %17, label %18, label %22
18: ; preds = %1
%19 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0), i32 %0, i32 %13, i32 %14)
br i1 %15, label %20, label %21
20: ; preds = %18
store i32 %14, i32* %9
br label %22
21: ; preds = %18
store i32 %13, i32* %9
br label %22
22: ; preds = %1, %20, %21
%23 = call i32 (...) @pthread_self()
%24 = call i64 @pthread_setschedparam(i32 %23, i32 %12, %struct.sched_param* %2)
%25 = icmp eq i64 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %22
%27 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %34
28: ; preds = %22
%29 = call i32 (...) @pthread_self()
%30 = call i32 @pthread_getschedparam(i32 %29, i32* %3, %struct.sched_param* %2)
%31 = load i32, i32* %3
%32 = load i32, i32* %9
%33 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %31, i32 %32)
br label %34
34: ; preds = %28, %26
%35 = phi i32 [ -1, %26 ], [ 0, %28 ]
ret i32 %35
}