#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*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ sched_getparam(int /*<<< orphan*/, struct sched_param *);
int sched_getscheduler(int /*<<< orphan*/);
int sched_setscheduler(int /*<<< orphan*/, int, struct sched_param *);
int /*<<< orphan*/ stderr;
int check_privs(void) {
  int policy = sched_getscheduler(0);
  struct sched_param param, old_param;
  if (policy == SCHED_FIFO || policy == SCHED_RR)
    return 0;
  if (sched_getparam(0, &old_param)) {
    fprintf(stderr, "unable to get scheduler parameters\n");
    return 1;
  }
  param = old_param;
  param.sched_priority = 1;
  if (sched_setscheduler(0, SCHED_FIFO, &param)) {
    fprintf(stderr, "Unable to change scheduling policy!\n");
    fprintf(stderr, "either run as root or join realtime group\n");
    return 1;
  }
  return sched_setscheduler(0, policy, &old_param);
}


x86 assembly (clang, -O0):
<check_privs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
xor    %edi,%edi
call   1030 <sched_getscheduler@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    0x2e8c(%rip),%rcx
cmp    (%rcx),%eax
je     1176 <check_privs+0x36>
mov    -0x8(%rbp),%eax
mov    0x2e5a(%rip),%rcx
cmp    (%rcx),%eax
jne    1182 <check_privs+0x42>
movl   $0x0,-0x4(%rbp)
jmp    1229 <check_privs+0xe9>
xor    %edi,%edi
lea    -0x18(%rbp),%rsi
call   1040 <sched_getparam@plt>
cmp    $0x0,%rax
je     11b8 <check_privs+0x78>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
lea    0xe59(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1229 <check_privs+0xe9>
mov    -0x18(%rbp),%eax
mov    %eax,-0x10(%rbp)
movl   $0x1,-0x10(%rbp)
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x10(%rbp),%rdx
call   1050 <sched_setscheduler@plt>
cmp    $0x0,%eax
je     1218 <check_privs+0xd8>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
lea    0xe32(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
lea    0xe42(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1229 <check_privs+0xe9>
mov    -0x8(%rbp),%esi
xor    %edi,%edi
lea    -0x18(%rbp),%rdx
call   1050 <sched_setscheduler@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_privs>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %ebp,%ebp
xor    %edi,%edi
call   1030 <sched_getscheduler@plt>
mov    0x2e90(%rip),%r14
cmp    (%r14),%eax
je     11ce <check_privs+0x8e>
mov    %eax,%ebx
mov    0x2e62(%rip),%rax
cmp    (%rax),%ebx
je     11ce <check_privs+0x8e>
mov    %rsp,%rsi
xor    %edi,%edi
call   1040 <sched_getparam@plt>
test   %rax,%rax
je     118b <check_privs+0x4b>
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
lea    0xe77(%rip),%rsi
jmp    11c4 <check_privs+0x84>
movl   $0x1,0x8(%rsp)
mov    (%r14),%esi
lea    0x8(%rsp),%rdx
xor    %edi,%edi
call   1050 <sched_setscheduler@plt>
test   %eax,%eax
je     11d9 <check_privs+0x99>
mov    0x2e2b(%rip),%rbx
mov    (%rbx),%edi
lea    0xe6e(%rip),%rsi
call   1060 <fprintf@plt>
mov    (%rbx),%edi
lea    0xe85(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %rsp,%rdx
xor    %edi,%edi
mov    %ebx,%esi
call   1050 <sched_setscheduler@plt>
mov    %eax,%ebp
jmp    11ce <check_privs+0x8e>

LLVM IR (-O0):
%struct.sched_param = type { i32 }
@SCHED_FIFO = global i32 0
@SCHED_RR = global i32 0
@stderr = global i32 0
@.str = [36 x i8] c"unable to get scheduler parameters\0A\00"
@.str.1 = [37 x i8] c"Unable to change scheduling policy!\0A\00"
@.str.2 = [43 x i8] c"either run as root or join realtime group\0A\00"
define i32 @check_privs() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sched_param
%4 = alloca %struct.sched_param
%5 = call i32 @sched_getscheduler(i32 0)
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = load i32, i32* @SCHED_FIFO
%8 = icmp eq i32 %6, %7
br i1 %8, label %13, label %9
9: ; preds = %0
%10 = load i32, i32* %2
%11 = load i32, i32* @SCHED_RR
%12 = icmp eq i32 %10, %11
br i1 %12, label %13, label %14
13: ; preds = %9, %0
store i32 0, i32* %1
br label %35
14: ; preds = %9
%15 = call i64 @sched_getparam(i32 0, %struct.sched_param* %4)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %35
20: ; preds = %14
%21 = bitcast %struct.sched_param* %3 to i8*
%22 = bitcast %struct.sched_param* %4 to i8*
%23 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %3, i32 0, i32 0
store i32 1, i32* %23
%24 = load i32, i32* @SCHED_FIFO
%25 = call i32 @sched_setscheduler(i32 0, i32 %24, %struct.sched_param* %3)
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %20
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %35
32: ; preds = %20
%33 = load i32, i32* %2
%34 = call i32 @sched_setscheduler(i32 0, i32 %33, %struct.sched_param* %4)
store i32 %34, i32* %1
br label %35
35: ; preds = %32, %27, %17, %13
%36 = load i32, i32* %1
ret i32 %36
}

LLVM IR (-O2):
%struct.sched_param = type { i32 }
@SCHED_FIFO = global i32 0
@SCHED_RR = global i32 0
@stderr = global i32 0
@.str = [36 x i8] c"unable to get scheduler parameters\0A\00"
@.str.1 = [37 x i8] c"Unable to change scheduling policy!\0A\00"
@.str.2 = [43 x i8] c"either run as root or join realtime group\0A\00"
define i32 @check_privs() {
%1 = alloca %struct.sched_param
%2 = alloca %struct.sched_param
%3 = tail call i32 @sched_getscheduler(i32 0)
%4 = bitcast %struct.sched_param* %1 to i8*
%5 = bitcast %struct.sched_param* %2 to i8*
%6 = load i32, i32* @SCHED_FIFO
%7 = icmp eq i32 %3, %6
%8 = load i32, i32* @SCHED_RR
%9 = icmp eq i32 %3, %8
%10 = select i1 %7, i1 true, i1 %9
br i1 %10, label %29, label %11
11: ; preds = %0
%12 = call i64 @sched_getparam(i32 0, %struct.sched_param* %2)
%13 = icmp eq i64 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %29
17: ; preds = %11
%18 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %1, i64 0, i32 0
store i32 1, i32* %18
%19 = load i32, i32* @SCHED_FIFO
%20 = call i32 @sched_setscheduler(i32 0, i32 %19, %struct.sched_param* %1)
%21 = icmp eq i32 %20, 0
br i1 %21, label %27, label %22
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
br label %29
27: ; preds = %17
%28 = call i32 @sched_setscheduler(i32 0, i32 %3, %struct.sched_param* %2)
br label %29
29: ; preds = %0, %27, %22, %14
%30 = phi i32 [ 1, %14 ], [ 1, %22 ], [ %28, %27 ], [ 0, %0 ]
ret i32 %30
}