#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 dummy;
};

/* Variables and functions */
scalar_t__ EPERM;
scalar_t__ errno;
scalar_t__ getuid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int sched_getparam(int /*<<< orphan*/, struct sched_param *);
int sched_setparam(int, 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;
    }
  }
  if (sched_getparam(0, &param) == -1) {
    perror("An error occurs when calling sched_getparam()");
    return 2;
  }
  result = sched_setparam(1, &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    126c <main+0x10c>
jmp    11ae <main+0x4e>
xor    %edi,%edi
lea    -0x10(%rbp),%rsi
call   1050 <sched_getparam@plt>
cmp    $0xffffffff,%eax
jne    11da <main+0x7a>
lea    0xe5e(%rip),%rdi
call   1070 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    126c <main+0x10c>
mov    $0x1,%edi
lea    -0x10(%rbp),%rsi
call   1080 <sched_setparam@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1227 <main+0xc7>
mov    0x2de4(%rip),%rax
mov    (%rax),%rax
mov    0x2dca(%rip),%rcx
cmp    (%rcx),%rax
jne    1227 <main+0xc7>
lea    0xe3f(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    126c <main+0x10c>
mov    0x2db2(%rip),%rax
mov    (%rax),%rax
mov    0x2d98(%rip),%rcx
cmp    (%rcx),%rax
je     1259 <main+0xf9>
lea    0xe1a(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    126c <main+0x10c>
lea    0xe15(%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   %rax
xor    %eax,%eax
call   1040 <getuid@plt>
test   %rax,%rax
je     11b7 <main+0x57>
mov    %rsp,%rsi
xor    %edi,%edi
call   1050 <sched_getparam@plt>
cmp    $0xffffffff,%eax
je     11ee <main+0x8e>
mov    %rsp,%rsi
mov    $0x1,%edi
call   1080 <sched_setparam@plt>
mov    0x2e50(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2e36(%rip),%rdx
mov    (%rdx),%rdx
cmp    $0xffffffff,%eax
jne    11d6 <main+0x76>
cmp    %rdx,%rcx
jne    11d6 <main+0x76>
lea    0xeb0(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rcx
ret
xor    %eax,%eax
call   1060 <set_nonroot@plt>
test   %rax,%rax
je     116d <main+0xd>
lea    0xea0(%rip),%rdi
call   1030 <puts@plt>
mov    $0x5,%eax
pop    %rcx
ret
cmp    %rdx,%rcx
jne    1201 <main+0xa1>
lea    0xe5f(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
pop    %rcx
ret
lea    0xe0b(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%eax
pop    %rcx
ret
lea    0xe26(%rip),%rdi
call   1070 <perror@plt>
jmp    11e7 <main+0x87>

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"
@errno = global i64 0
@EPERM = global i64 0
@.str.2 = [13 x i8] c"Test PASSED\0A\00"
@.str.3 = [19 x i8] c"errno is not EPERM\00"
@.str.4 = [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 %35
11: ; preds = %6
br label %12
12: ; preds = %11, %0
%13 = call i32 @sched_getparam(i32 0, %struct.sched_param* %3)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = 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 %35
17: ; preds = %12
%18 = call i32 @sched_setparam(i32 1, %struct.sched_param* %3)
store i32 %18, i32* %2
%19 = load i32, i32* %2
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %27
21: ; preds = %17
%22 = load i64, i64* @errno
%23 = load i64, i64* @EPERM
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %35
27: ; preds = %21, %17
%28 = load i64, i64* @errno
%29 = load i64, i64* @EPERM
%30 = icmp ne i64 %28, %29
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %35
33: ; preds = %27
%34 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %35
35: ; preds = %33, %31, %25, %15, %9
%36 = load i32, i32* %1
ret i32 %36
}

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