#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*/ getpid();
scalar_t__ getuid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int sched_get_priority_max(int);
int sched_get_priority_min(int);
int sched_getparam(int /*<<< orphan*/, struct sched_param *);
int sched_getscheduler(int /*<<< orphan*/);
int /*<<< orphan*/ sched_setscheduler(int, int, struct sched_param *);
scalar_t__ set_nonroot();
int main(void) {
  int max_priority, old_priority, old_policy, new_policy, policy;
  struct sched_param param;
  if (getuid() == 0) {
    if (set_nonroot() != 0) {
      printf("Cannot run this test as non-root user\n");
      return 5;
    }
  }
  if (sched_getparam(getpid(), &param) == -1) {
    perror("An error occurs when calling sched_getparam()");
    return 2;
  }
  old_priority = param.sched_priority;
  old_policy = sched_getscheduler(getpid());
  if (old_policy == -1) {
    perror("An error occurs when calling sched_getscheduler()");
    return 2;
  }
  policy = old_policy == SCHED_FIFO ? SCHED_RR : SCHED_FIFO;
  max_priority = sched_get_priority_max(policy);
  param.sched_priority = (old_priority == max_priority)
                             ? sched_get_priority_min(policy)
                             : max_priority;
  sched_setscheduler(1, policy, &param);
  if (sched_getparam(getpid(), &param) != 0) {
    perror("An error occurs when calling sched_getparam()");
    return 2;
  }
  new_policy = sched_getscheduler(getpid());
  if (new_policy == -1) {
    perror("An error occurs when calling sched_getscheduler()");
    return 2;
  }
  if (old_policy == new_policy && old_priority == param.sched_priority) {
    printf("Test PASSED\n");
    return 0;
  }
  if (param.sched_priority != old_priority) {
    printf("The param has changed\n");
  }
  if (new_policy != old_policy) {
    printf("The policy has changed\n");
  }
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1050 <getuid@plt>
cmp    $0x0,%rax
jne    11ee <main+0x4e>
mov    $0x0,%al
call   1090 <set_nonroot@plt>
cmp    $0x0,%rax
je     11e9 <main+0x49>
lea    0xe28(%rip),%rdi
call   1060 <printf@plt>
movl   $0x5,-0x4(%rbp)
jmp    13a0 <main+0x200>
jmp    11ee <main+0x4e>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%edi
lea    -0x20(%rbp),%rsi
call   1070 <sched_getparam@plt>
cmp    $0xffffffff,%eax
jne    1221 <main+0x81>
lea    0xe17(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    13a0 <main+0x200>
mov    -0x20(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%edi
call   1030 <sched_getscheduler@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    125a <main+0xba>
lea    0xe0c(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    13a0 <main+0x200>
mov    -0x10(%rbp),%eax
mov    0x2d84(%rip),%rcx
cmp    (%rcx),%eax
jne    127d <main+0xdd>
mov    0x2d5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    1289 <main+0xe9>
mov    0x2d64(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   10c0 <sched_get_priority_max@plt>
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    12b6 <main+0x116>
mov    -0x18(%rbp),%edi
call   10a0 <sched_get_priority_min@plt>
mov    %eax,-0x28(%rbp)
jmp    12bc <main+0x11c>
mov    -0x8(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%esi
mov    $0x1,%edi
lea    -0x20(%rbp),%rdx
call   1080 <sched_setscheduler@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%edi
lea    -0x20(%rbp),%rsi
call   1070 <sched_getparam@plt>
cmp    $0x0,%eax
je     1306 <main+0x166>
lea    0xd32(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    13a0 <main+0x200>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%edi
call   1030 <sched_getscheduler@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1339 <main+0x199>
lea    0xd2d(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    13a0 <main+0x200>
mov    -0x10(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    1369 <main+0x1c9>
mov    -0xc(%rbp),%eax
cmp    -0x20(%rbp),%eax
jne    1369 <main+0x1c9>
lea    0xd2f(%rip),%rdi
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13a0 <main+0x200>
mov    -0x20(%rbp),%eax
cmp    -0xc(%rbp),%eax
je     1381 <main+0x1e1>
lea    0xd18(%rip),%rdi
call   1060 <printf@plt>
mov    -0x14(%rbp),%eax
cmp    -0x10(%rbp),%eax
je     1399 <main+0x1f9>
lea    0xd17(%rip),%rdi
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1060 <getuid@plt>
test   %rax,%rax
je     1275 <main+0xd5>
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %rsp,%rsi
mov    %eax,%edi
call   1070 <sched_getparam@plt>
cmp    $0xffffffff,%eax
je     12c1 <main+0x121>
mov    (%rsp),%r15d
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%edi
call   1030 <sched_getscheduler@plt>
cmp    $0xffffffff,%eax
je     12ca <main+0x12a>
mov    %eax,%r14d
mov    0x2df2(%rip),%rax
mov    (%rax),%ebp
cmp    %ebp,%r14d
jne    1206 <main+0x66>
mov    0x2dcc(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   10c0 <sched_get_priority_max@plt>
cmp    %eax,%r15d
jne    1219 <main+0x79>
mov    %ebp,%edi
call   10a0 <sched_get_priority_min@plt>
mov    %eax,(%rsp)
mov    %rsp,%rbx
mov    $0x1,%edi
mov    %ebp,%esi
mov    %rbx,%rdx
call   1080 <sched_setscheduler@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%edi
mov    %rbx,%rsi
call   1070 <sched_getparam@plt>
test   %eax,%eax
jne    12c1 <main+0x121>
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%edi
call   1030 <sched_getscheduler@plt>
cmp    $0xffffffff,%eax
je     12ca <main+0x12a>
mov    %eax,%ebx
mov    (%rsp),%eax
cmp    %ebx,%r14d
jne    1298 <main+0xf8>
cmp    %eax,%r15d
jne    1298 <main+0xf8>
lea    0xe21(%rip),%rdi
call   1040 <puts@plt>
xor    %ebp,%ebp
jmp    12db <main+0x13b>
xor    %eax,%eax
call   1090 <set_nonroot@plt>
test   %rax,%rax
je     11b7 <main+0x17>
lea    0xe0d(%rip),%rdi
call   1040 <puts@plt>
mov    $0x5,%ebp
jmp    12db <main+0x13b>
cmp    %eax,%r15d
je     12a9 <main+0x109>
lea    0xdd3(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebp
cmp    %ebx,%r14d
je     12db <main+0x13b>
lea    0xda6(%rip),%rdi
call   1040 <puts@plt>
jmp    12db <main+0x13b>
lea    0xd38(%rip),%rdi
jmp    12d1 <main+0x131>
lea    0xd5d(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x2,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sched_param = type { i32 }
@.str = [39 x i8] c"Cannot run this test as non-root user\0A\00"
@.str.1 = [46 x i8] c"An error occurs when calling sched_getparam()\00"
@.str.2 = [50 x i8] c"An error occurs when calling sched_getscheduler()\00"
@SCHED_FIFO = global i32 0
@SCHED_RR = global i32 0
@.str.3 = [13 x i8] c"Test PASSED\0A\00"
@.str.4 = [23 x i8] c"The param has changed\0A\00"
@.str.5 = [24 x i8] c"The policy has changed\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sched_param
store i32 0, i32* %1
%8 = call i64 (...) @getuid()
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %16
10: ; preds = %0
%11 = call i64 (...) @set_nonroot()
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
store i32 5, i32* %1
br label %93
15: ; preds = %10
br label %16
16: ; preds = %15, %0
%17 = call i32 (...) @getpid()
%18 = call i32 @sched_getparam(i32 %17, %struct.sched_param* %7)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %1
br label %93
22: ; preds = %16
%23 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %7, i32 0, i32 0
%24 = load i32, i32* %23
store i32 %24, i32* %3
%25 = call i32 (...) @getpid()
%26 = call i32 @sched_getscheduler(i32 %25)
store i32 %26, i32* %4
%27 = load i32, i32* %4
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %22
%30 = call i32 @perror(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %1
br label %93
31: ; preds = %22
%32 = load i32, i32* %4
%33 = load i32, i32* @SCHED_FIFO
%34 = icmp eq i32 %32, %33
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = load i32, i32* @SCHED_RR
br label %39
37: ; preds = %31
%38 = load i32, i32* @SCHED_FIFO
br label %39
39: ; preds = %37, %35
%40 = phi i32 [ %36, %35 ], [ %38, %37 ]
store i32 %40, i32* %6
%41 = load i32, i32* %6
%42 = call i32 @sched_get_priority_max(i32 %41)
store i32 %42, i32* %2
%43 = load i32, i32* %3
%44 = load i32, i32* %2
%45 = icmp eq i32 %43, %44
br i1 %45, label %46, label %49
46: ; preds = %39
%47 = load i32, i32* %6
%48 = call i32 @sched_get_priority_min(i32 %47)
br label %51
49: ; preds = %39
%50 = load i32, i32* %2
br label %51
51: ; preds = %49, %46
%52 = phi i32 [ %48, %46 ], [ %50, %49 ]
%53 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %7, i32 0, i32 0
store i32 %52, i32* %53
%54 = load i32, i32* %6
%55 = call i32 @sched_setscheduler(i32 1, i32 %54, %struct.sched_param* %7)
%56 = call i32 (...) @getpid()
%57 = call i32 @sched_getparam(i32 %56, %struct.sched_param* %7)
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %51
%60 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %1
br label %93
61: ; preds = %51
%62 = call i32 (...) @getpid()
%63 = call i32 @sched_getscheduler(i32 %62)
store i32 %63, i32* %5
%64 = load i32, i32* %5
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %68
66: ; preds = %61
%67 = call i32 @perror(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %1
br label %93
68: ; preds = %61
%69 = load i32, i32* %4
%70 = load i32, i32* %5
%71 = icmp eq i32 %69, %70
br i1 %71, label %72, label %79
72: ; preds = %68
%73 = load i32, i32* %3
%74 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %7, i32 0, i32 0
%75 = load i32, i32* %74
%76 = icmp eq i32 %73, %75
br i1 %76, label %77, label %79
77: ; preds = %72
%78 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %1
br label %93
79: ; preds = %72, %68
%80 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %7, i32 0, i32 0
%81 = load i32, i32* %80
%82 = load i32, i32* %3
%83 = icmp ne i32 %81, %82
br i1 %83, label %84, label %86
84: ; preds = %79
%85 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
br label %86
86: ; preds = %84, %79
%87 = load i32, i32* %5
%88 = load i32, i32* %4
%89 = icmp ne i32 %87, %88
br i1 %89, label %90, label %92
90: ; preds = %86
%91 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
br label %92
92: ; preds = %90, %86
store i32 1, i32* %1
br label %93
93: ; preds = %92, %77, %66, %59, %29, %20, %13
%94 = load i32, i32* %1
ret i32 %94
}

LLVM IR (-O2):
%struct.sched_param = type { i32 }
@.str.1 = [46 x i8] c"An error occurs when calling sched_getparam()\00"
@.str.2 = [50 x i8] c"An error occurs when calling sched_getscheduler()\00"
@SCHED_FIFO = global i32 0
@SCHED_RR = global i32 0
@str = [23 x i8] c"The policy has changed\00"
@str.6 = [22 x i8] c"The param has changed\00"
@str.7 = [12 x i8] c"Test PASSED\00"
@str.8 = [38 x i8] c"Cannot run this test as non-root user\00"
define i32 @main() {
%1 = alloca %struct.sched_param
%2 = bitcast %struct.sched_param* %1 to i8*
%3 = tail call i64 (...) @getuid()
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %10
5: ; preds = %0
%6 = tail call i64 (...) @set_nonroot()
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %5
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.8, i64 0, i64 0))
br label %60
10: ; preds = %5, %0
%11 = tail call i32 (...) @getpid()
%12 = call i32 @sched_getparam(i32 %11, %struct.sched_param* %1)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
br label %60
16: ; preds = %10
%17 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %1, i64 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 (...) @getpid()
%20 = call i32 @sched_getscheduler(i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 @perror(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0))
br label %60
24: ; preds = %16
%25 = load i32, i32* @SCHED_FIFO
%26 = icmp eq i32 %20, %25
%27 = load i32, i32* @SCHED_RR
%28 = select i1 %26, i32 %27, i32 %25
%29 = call i32 @sched_get_priority_max(i32 %28)
%30 = icmp eq i32 %18, %29
br i1 %30, label %31, label %33
31: ; preds = %24
%32 = call i32 @sched_get_priority_min(i32 %28)
br label %33
33: ; preds = %24, %31
%34 = phi i32 [ %32, %31 ], [ %29, %24 ]
store i32 %34, i32* %17
%35 = call i32 @sched_setscheduler(i32 1, i32 %28, %struct.sched_param* %1)
%36 = call i32 (...) @getpid()
%37 = call i32 @sched_getparam(i32 %36, %struct.sched_param* %1)
%38 = icmp eq i32 %37, 0
br i1 %38, label %41, label %39
39: ; preds = %33
%40 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
br label %60
41: ; preds = %33
%42 = call i32 (...) @getpid()
%43 = call i32 @sched_getscheduler(i32 %42)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %47
45: ; preds = %41
%46 = call i32 @perror(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0))
br label %60
47: ; preds = %41
%48 = icmp eq i32 %20, %43
%49 = load i32, i32* %17
%50 = icmp eq i32 %18, %49
%51 = select i1 %48, i1 %50, i1 false
br i1 %51, label %52, label %54
52: ; preds = %47
%53 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.7, i64 0, i64 0))
br label %60
54: ; preds = %47
br i1 %50, label %57, label %55
55: ; preds = %54
%56 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.6, i64 0, i64 0))
br label %57
57: ; preds = %55, %54
br i1 %48, label %60, label %58
58: ; preds = %57
%59 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
br label %60
60: ; preds = %57, %58, %52, %45, %39, %22, %14, %8
%61 = phi i32 [ 5, %8 ], [ 2, %14 ], [ 2, %22 ], [ 2, %39 ], [ 2, %45 ], [ 0, %52 ], [ 1, %58 ], [ 1, %57 ]
ret i32 %61
}