#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 */
scalar_t__ EPERM;
int /*<<< orphan*/ SCHED_FIFO;
scalar_t__ errno;
scalar_t__ getuid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int sched_get_priority_min(int /*<<< orphan*/);
int sched_getparam(int /*<<< orphan*/, struct sched_param *);
int sched_setparam(int /*<<< orphan*/, struct sched_param *);
int /*<<< orphan*/ sched_setscheduler(int /*<<< orphan*/, int /*<<< orphan*/,
                                      struct sched_param *);
scalar_t__ set_nonroot();
int main(void) {
  int old_priority;
  struct sched_param param;
  int rc;
  param.sched_priority = sched_get_priority_min(SCHED_FIFO);
  sched_setscheduler(0, SCHED_FIFO, &param);
  if (getuid() == 0) {
    if (set_nonroot() != 0) {
      printf("Cannot run this test as non-root user\n");
      return 5;
    }
  }
  if (sched_getparam(0, &param) == -1) {
    perror("An error occurs when calling sched_getparam()");
    return 2;
  }
  old_priority = param.sched_priority;
  param.sched_priority++;
  rc = sched_setparam(0, &param);
  if (rc != -1 || (rc == -1 && errno != EPERM)) {
    perror("sched_setparam() does not return EPERM\n");
    return 2;
  }
  if (sched_getparam(0, &param) != 0) {
    perror("An error occurs when calling sched_getparam()");
    return 2;
  }
  if (param.sched_priority == old_priority) {
    printf("Test PASSED\n");
    return 0;
  } else {
    printf("The priority have changed.\n");
    return 1;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
call   1080 <sched_get_priority_min@plt>
mov    %eax,-0x10(%rbp)
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x10(%rbp),%rdx
call   1060 <sched_setscheduler@plt>
mov    $0x0,%al
call   1030 <getuid@plt>
cmp    $0x0,%rax
jne    11f3 <main+0x73>
mov    $0x0,%al
call   1070 <set_nonroot@plt>
cmp    $0x0,%rax
je     11ee <main+0x6e>
lea    0xe23(%rip),%rdi
call   1040 <printf@plt>
movl   $0x5,-0x4(%rbp)
jmp    12e5 <main+0x165>
jmp    11f3 <main+0x73>
xor    %edi,%edi
lea    -0x10(%rbp),%rsi
call   1050 <sched_getparam@plt>
cmp    $0xffffffff,%eax
jne    121f <main+0x9f>
lea    0xe19(%rip),%rdi
call   1090 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12e5 <main+0x165>
mov    -0x10(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
xor    %edi,%edi
lea    -0x10(%rbp),%rsi
call   10a0 <sched_setparam@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    126a <main+0xea>
cmpl   $0xffffffff,-0x14(%rbp)
jne    1282 <main+0x102>
mov    0x2d81(%rip),%rax
mov    (%rax),%rax
mov    0x2d67(%rip),%rcx
cmp    (%rcx),%rax
je     1282 <main+0x102>
lea    0xde4(%rip),%rdi
call   1090 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12e5 <main+0x165>
xor    %edi,%edi
lea    -0x10(%rbp),%rsi
call   1050 <sched_getparam@plt>
cmp    $0x0,%eax
je     12ae <main+0x12e>
lea    0xd8a(%rip),%rdi
call   1090 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12e5 <main+0x165>
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    12d2 <main+0x152>
lea    0xdbc(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e5 <main+0x165>
lea    0xdb1(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e5c(%rip),%rbx
mov    (%rbx),%edi
call   1080 <sched_get_priority_min@plt>
mov    %eax,0x8(%rsp)
mov    (%rbx),%esi
lea    0x8(%rsp),%rdx
xor    %edi,%edi
call   1060 <sched_setscheduler@plt>
xor    %eax,%eax
call   1040 <getuid@plt>
test   %rax,%rax
je     121e <main+0x9e>
lea    0x8(%rsp),%rsi
xor    %edi,%edi
call   1050 <sched_getparam@plt>
cmp    $0xffffffff,%eax
je     1259 <main+0xd9>
mov    0x8(%rsp),%ebx
lea    0x1(%rbx),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
xor    %edi,%edi
call   10a0 <sched_setparam@plt>
cmp    $0xffffffff,%eax
jne    1250 <main+0xd0>
mov    0x2def(%rip),%rax
mov    (%rax),%rax
mov    0x2dd5(%rip),%rcx
cmp    (%rcx),%rax
jne    1250 <main+0xd0>
lea    0x8(%rsp),%rsi
xor    %edi,%edi
call   1050 <sched_getparam@plt>
test   %eax,%eax
jne    1259 <main+0xd9>
cmp    %ebx,0x8(%rsp)
jne    123d <main+0xbd>
lea    0xe5c(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    126a <main+0xea>
xor    %eax,%eax
call   1070 <set_nonroot@plt>
test   %rax,%rax
je     11b1 <main+0x31>
lea    0xe4c(%rip),%rdi
call   1030 <puts@plt>
mov    $0x5,%eax
jmp    126a <main+0xea>
lea    0xe12(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    126a <main+0xea>
lea    0xdd7(%rip),%rdi
jmp    1260 <main+0xe0>
lea    0xda0(%rip),%rdi
call   1090 <perror@plt>
mov    $0x2,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sched_param = type { i32 }
@SCHED_FIFO = global i32 0
@.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"
@errno = global i64 0
@EPERM = global i64 0
@.str.2 = [40 x i8] c"sched_setparam() does not return EPERM\0A\00"
@.str.3 = [13 x i8] c"Test PASSED\0A\00"
@.str.4 = [28 x i8] c"The priority have changed.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sched_param
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @SCHED_FIFO
%6 = call i32 @sched_get_priority_min(i32 %5)
%7 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %3, i32 0, i32 0
store i32 %6, i32* %7
%8 = load i32, i32* @SCHED_FIFO
%9 = call i32 @sched_setscheduler(i32 0, i32 %8, %struct.sched_param* %3)
%10 = call i64 (...) @getuid()
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %0
%13 = call i64 (...) @set_nonroot()
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
store i32 5, i32* %1
br label %55
17: ; preds = %12
br label %18
18: ; preds = %17, %0
%19 = call i32 @sched_getparam(i32 0, %struct.sched_param* %3)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = 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 %55
23: ; preds = %18
%24 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %3, i32 0, i32 0
%25 = load i32, i32* %24
store i32 %25, i32* %2
%26 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %3, i32 0, i32 0
%27 = load i32, i32* %26
%28 = add nsw i32 %27, 1
store i32 %28, i32* %26
%29 = call i32 @sched_setparam(i32 0, %struct.sched_param* %3)
store i32 %29, i32* %4
%30 = load i32, i32* %4
%31 = icmp ne i32 %30, -1
br i1 %31, label %39, label %32
32: ; preds = %23
%33 = load i32, i32* %4
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %41
35: ; preds = %32
%36 = load i64, i64* @errno
%37 = load i64, i64* @EPERM
%38 = icmp ne i64 %36, %37
br i1 %38, label %39, label %41
39: ; preds = %35, %23
%40 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %1
br label %55
41: ; preds = %35, %32
%42 = call i32 @sched_getparam(i32 0, %struct.sched_param* %3)
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %41
%45 = 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 %55
46: ; preds = %41
%47 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %3, i32 0, i32 0
%48 = load i32, i32* %47
%49 = load i32, i32* %2
%50 = icmp eq i32 %48, %49
br i1 %50, label %51, label %53
51: ; preds = %46
%52 = 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 %55
53: ; preds = %46
%54 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %55
55: ; preds = %53, %51, %44, %39, %21, %15
%56 = load i32, i32* %1
ret i32 %56
}

LLVM IR (-O2):
%struct.sched_param = type { i32 }
@SCHED_FIFO = global i32 0
@.str.1 = [46 x i8] c"An error occurs when calling sched_getparam()\00"
@errno = global i64 0
@EPERM = global i64 0
@.str.2 = [40 x i8] c"sched_setparam() does not return EPERM\0A\00"
@str = [27 x i8] c"The priority have changed.\00"
@str.5 = [12 x i8] c"Test PASSED\00"
@str.6 = [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 = load i32, i32* @SCHED_FIFO
%4 = tail call i32 @sched_get_priority_min(i32 %3)
%5 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %1, i64 0, i32 0
store i32 %4, i32* %5
%6 = load i32, i32* @SCHED_FIFO
%7 = call i32 @sched_setscheduler(i32 0, i32 %6, %struct.sched_param* %1)
%8 = call i64 (...) @getuid()
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %0
%11 = call i64 (...) @set_nonroot()
%12 = icmp eq i64 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %10
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.6, i64 0, i64 0))
br label %43
15: ; preds = %10, %0
%16 = call i32 @sched_getparam(i32 0, %struct.sched_param* %1)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
br label %43
20: ; preds = %15
%21 = load i32, i32* %5
%22 = add nsw i32 %21, 1
store i32 %22, i32* %5
%23 = call i32 @sched_setparam(i32 0, %struct.sched_param* %1)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %20
%26 = load i64, i64* @errno
%27 = load i64, i64* @EPERM
%28 = icmp eq i64 %26, %27
br i1 %28, label %31, label %29
29: ; preds = %25, %20
%30 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
br label %43
31: ; preds = %25
%32 = call i32 @sched_getparam(i32 0, %struct.sched_param* %1)
%33 = icmp eq i32 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %31
%35 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
br label %43
36: ; preds = %31
%37 = load i32, i32* %5
%38 = icmp eq i32 %37, %21
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.5, i64 0, i64 0))
br label %43
41: ; preds = %36
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
br label %43
43: ; preds = %41, %39, %34, %29, %18, %13
%44 = phi i32 [ 5, %13 ], [ 2, %18 ], [ 2, %29 ], [ 2, %34 ], [ 0, %39 ], [ 1, %41 ]
ret i32 %44
}