#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 /*<<< orphan*/ 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 /*<<< orphan*/ sched_get_priority_max(int /*<<< orphan*/);
int sched_setscheduler(int, int /*<<< orphan*/, struct sched_param *);
scalar_t__ set_nonroot();
int main(void) {
  int result;
  struct sched_param param;
  if (getuid() == 0) {
    if (set_nonroot() != 0) {
      printf("Cannot run this test as non-root user\n");
      return 5;
    }
  }
  param.sched_priority = sched_get_priority_max(SCHED_FIFO);
  result = sched_setscheduler(1, SCHED_FIFO, &param);
  if (result == -1 && errno == EPERM) {
    printf("Test PASSED\n");
    return 0;
  } else if (errno != EPERM) {
    perror("errno is not EPERM");
    return 1;
  } else {
    printf("The returned code is not -1.\n");
    return 1;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1030 <getuid@plt>
cmp    $0x0,%rax
jne    11ae <main+0x4e>
mov    $0x0,%al
call   1060 <set_nonroot@plt>
cmp    $0x0,%rax
je     11a9 <main+0x49>
lea    0xe68(%rip),%rdi
call   1040 <printf@plt>
movl   $0x5,-0x4(%rbp)
jmp    125a <main+0xfa>
jmp    11ae <main+0x4e>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
call   1080 <sched_get_priority_max@plt>
mov    %eax,-0x10(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    -0x10(%rbp),%rdx
call   1050 <sched_setscheduler@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1215 <main+0xb5>
mov    0x2dee(%rip),%rax
mov    (%rax),%rax
mov    0x2dd4(%rip),%rcx
cmp    (%rcx),%rax
jne    1215 <main+0xb5>
lea    0xe23(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    125a <main+0xfa>
mov    0x2dbc(%rip),%rax
mov    (%rax),%rax
mov    0x2da2(%rip),%rcx
cmp    (%rcx),%rax
je     1247 <main+0xe7>
lea    0xdfe(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    125a <main+0xfa>
lea    0xdf9(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1040 <getuid@plt>
test   %rax,%rax
je     11da <main+0x7a>
mov    0x2e70(%rip),%rbx
mov    (%rbx),%edi
call   1080 <sched_get_priority_max@plt>
mov    %eax,0x8(%rsp)
mov    (%rbx),%esi
lea    0x8(%rsp),%rdx
mov    $0x1,%edi
call   1050 <sched_setscheduler@plt>
mov    0x2e3d(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2e23(%rip),%rdx
mov    (%rdx),%rdx
cmp    $0xffffffff,%eax
jne    11c2 <main+0x62>
cmp    %rdx,%rcx
jne    11c2 <main+0x62>
lea    0xe77(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    11f7 <main+0x97>
cmp    %rdx,%rcx
jne    11fd <main+0x9d>
lea    0xe45(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    11f7 <main+0x97>
xor    %eax,%eax
call   1060 <set_nonroot@plt>
test   %rax,%rax
je     1171 <main+0x11>
lea    0xe4f(%rip),%rdi
call   1030 <puts@plt>
mov    $0x5,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xdfc(%rip),%rdi
call   1070 <perror@plt>
jmp    11d3 <main+0x73>

LLVM IR (-O0):
%struct.sched_param = type { i32 }
@.str = [39 x i8] c"Cannot run this test as non-root user\0A\00"
@SCHED_FIFO = global i32 0
@errno = global i64 0
@EPERM = global i64 0
@.str.1 = [13 x i8] c"Test PASSED\0A\00"
@.str.2 = [19 x i8] c"errno is not EPERM\00"
@.str.3 = [30 x i8] c"The returned code is not -1.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sched_param
store i32 0, i32* %1
%4 = call i64 (...) @getuid()
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %12
6: ; preds = %0
%7 = call i64 (...) @set_nonroot()
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
store i32 5, i32* %1
br label %34
11: ; preds = %6
br label %12
12: ; preds = %11, %0
%13 = load i32, i32* @SCHED_FIFO
%14 = call i32 @sched_get_priority_max(i32 %13)
%15 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %3, i32 0, i32 0
store i32 %14, i32* %15
%16 = load i32, i32* @SCHED_FIFO
%17 = call i32 @sched_setscheduler(i32 1, i32 %16, %struct.sched_param* %3)
store i32 %17, i32* %2
%18 = load i32, i32* %2
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %26
20: ; preds = %12
%21 = load i64, i64* @errno
%22 = load i64, i64* @EPERM
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %34
26: ; preds = %20, %12
%27 = load i64, i64* @errno
%28 = load i64, i64* @EPERM
%29 = icmp ne i64 %27, %28
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %34
32: ; preds = %26
%33 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %34
34: ; preds = %32, %30, %24, %9
%35 = load i32, i32* %1
ret i32 %35
}

LLVM IR (-O2):
%struct.sched_param = type { i32 }
@SCHED_FIFO = global i32 0
@errno = global i64 0
@EPERM = global i64 0
@.str.2 = [19 x i8] c"errno is not EPERM\00"
@str = [29 x i8] c"The returned code is not -1.\00"
@str.4 = [12 x i8] c"Test PASSED\00"
@str.5 = [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.5, i64 0, i64 0))
br label %29
10: ; preds = %5, %0
%11 = load i32, i32* @SCHED_FIFO
%12 = tail call i32 @sched_get_priority_max(i32 %11)
%13 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %1, i64 0, i32 0
store i32 %12, i32* %13
%14 = load i32, i32* @SCHED_FIFO
%15 = call i32 @sched_setscheduler(i32 1, i32 %14, %struct.sched_param* %1)
%16 = icmp eq i32 %15, -1
%17 = load i64, i64* @errno
%18 = load i64, i64* @EPERM
%19 = icmp eq i64 %17, %18
%20 = select i1 %16, i1 %19, i1 false
br i1 %20, label %21, label %23
21: ; preds = %10
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.4, i64 0, i64 0))
br label %29
23: ; preds = %10
%24 = icmp eq i64 %17, %18
br i1 %24, label %27, label %25
25: ; preds = %23
%26 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %29
27: ; preds = %23
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
br label %29
29: ; preds = %27, %25, %21, %8
%30 = phi i32 [ 5, %8 ], [ 0, %21 ], [ 1, %25 ], [ 1, %27 ]
ret i32 %30
}