#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 {
  long sched_priority;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
long LONG_MAX;
long LONG_MIN;
int SCHED_FIFO;
int SCHED_RR;
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fatal(char *);
scalar_t__ geteuid();
int /*<<< orphan*/ getuid();
int /*<<< orphan*/ helpAndLeave(char *, int /*<<< orphan*/);
int /*<<< orphan*/ pexit(char *);
int sched_get_priority_max(int);
int sched_get_priority_min(int);
int sched_setscheduler(int /*<<< orphan*/, int, struct sched_param *);
int seteuid(int /*<<< orphan*/);
int /*<<< orphan*/ strncmp(char *, char *, int);
long strtol(char *, char **, int);
int main(int argc, char *argv[]) {
  if (argc < 4)
    helpAndLeave(argv[0], EXIT_FAILURE);
  if (geteuid() != 0)
    fatal("This program must be run as root (or setuid-root)\n");
  int policy;
  long priority;
  char *endptr;
  struct sched_param schedp;
  int priority_min, priority_max;
  if (!strncmp(argv[1], "r", 2))
    policy = SCHED_RR;
  else if (!strncmp(argv[1], "f", 2))
    policy = SCHED_FIFO;
  else
    fatal("Unknown scheduling policy");
  priority = strtol(argv[2], &endptr, 10);
  if (priority == LONG_MIN || priority == LONG_MAX || *endptr != '\0')
    fatal("Invalid priority\n");
  if ((priority_min = sched_get_priority_min(policy)) == -1)
    pexit("sched_get_priority_min");
  if ((priority_max = sched_get_priority_max(policy)) == -1)
    pexit("sched_get_priority_max");
  if (priority < priority_min || priority > priority_max)
    fatal("priority out of bounds\n");
  schedp.sched_priority = priority;
  if (sched_setscheduler(0, policy, &schedp) == -1)
    pexit("sched_setscheduler");
  if (seteuid(getuid()) == -1)
    pexit("seteuid");
  execvp(argv[3], &argv[3]);
  pexit("exec");
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    1205 <main+0x35>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
call   1090 <helpAndLeave@plt>
mov    $0x0,%al
call   1060 <geteuid@plt>
cmp    $0x0,%rax
je     1222 <main+0x52>
lea    0xde3(%rip),%rdi
call   10e0 <fatal@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe87(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    1255 <main+0x85>
mov    0x2d65(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    1299 <main+0xc9>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdcf(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    1288 <main+0xb8>
mov    0x2d62(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    1294 <main+0xc4>
lea    0xda6(%rip),%rdi
call   10e0 <fatal@plt>
jmp    1299 <main+0xc9>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d2a(%rip),%rcx
cmp    (%rcx),%rax
je     12eb <main+0x11b>
mov    -0x20(%rbp),%rax
mov    0x2cfe(%rip),%rcx
cmp    (%rcx),%rax
je     12eb <main+0x11b>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12f7 <main+0x127>
lea    0xd5d(%rip),%rdi
call   10e0 <fatal@plt>
mov    -0x14(%rbp),%edi
call   1080 <sched_get_priority_min@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
jne    1317 <main+0x147>
lea    0xd4f(%rip),%rdi
call   10d0 <pexit@plt>
mov    -0x14(%rbp),%edi
call   10c0 <sched_get_priority_max@plt>
mov    %eax,-0x38(%rbp)
cmp    $0xffffffff,%eax
jne    1337 <main+0x167>
lea    0xd46(%rip),%rdi
call   10d0 <pexit@plt>
mov    -0x20(%rbp),%rax
movslq -0x34(%rbp),%rcx
cmp    %rcx,%rax
jl     1359 <main+0x189>
mov    -0x20(%rbp),%rax
movslq -0x38(%rbp),%rcx
cmp    %rcx,%rax
jle    1365 <main+0x195>
lea    0xd2f(%rip),%rdi
call   10e0 <fatal@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%esi
xor    %edi,%edi
lea    -0x30(%rbp),%rdx
call   1050 <sched_setscheduler@plt>
cmp    $0xffffffff,%eax
jne    1390 <main+0x1c0>
lea    0xd1c(%rip),%rdi
call   10d0 <pexit@plt>
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %eax,%edi
call   10f0 <seteuid@plt>
cmp    $0xffffffff,%eax
jne    13b3 <main+0x1e3>
lea    0xd0c(%rip),%rdi
call   10d0 <pexit@plt>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x18,%rsi
call   10a0 <execvp@plt>
lea    0xcf3(%rip),%rdi
call   10d0 <pexit@plt>
mov    0x2be5(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
cmp    $0x3,%edi
jg     11f5 <main+0x25>
mov    (%r15),%rdi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
call   1090 <helpAndLeave@plt>
xor    %eax,%eax
call   1060 <geteuid@plt>
test   %rax,%rax
je     120d <main+0x3d>
lea    0xdf8(%rip),%rdi
call   10e0 <fatal@plt>
mov    0x8(%r15),%rdi
lea    0xea0(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    1232 <main+0x62>
mov    0x2d83(%rip),%rax
mov    (%rax),%r14d
jmp    1263 <main+0x93>
mov    0x8(%r15),%rdi
lea    0xdf6(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    1257 <main+0x87>
mov    0x2d8e(%rip),%rax
mov    (%rax),%r14d
jmp    1263 <main+0x93>
lea    0xdd7(%rip),%rdi
call   10e0 <fatal@plt>
mov    0x10(%r15),%rdi
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    %rax,%rbp
mov    0x2d68(%rip),%rax
cmp    (%rax),%rbp
je     129b <main+0xcb>
mov    0x2d44(%rip),%rax
cmp    (%rax),%rbp
je     129b <main+0xcb>
mov    0x8(%rsp),%rax
cmpb   $0x0,(%rax)
je     12a7 <main+0xd7>
lea    0xdad(%rip),%rdi
call   10e0 <fatal@plt>
mov    %r14d,%edi
call   1080 <sched_get_priority_min@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
jne    12c3 <main+0xf3>
lea    0xda3(%rip),%rdi
call   10d0 <pexit@plt>
mov    %r14d,%edi
call   10c0 <sched_get_priority_max@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    12de <main+0x10e>
lea    0xd9f(%rip),%rdi
call   10d0 <pexit@plt>
movslq %r12d,%rax
cmp    %rax,%rbp
jl     12ee <main+0x11e>
movslq %ebx,%rax
cmp    %rax,%rbp
jle    12fa <main+0x12a>
lea    0xd9a(%rip),%rdi
call   10e0 <fatal@plt>
mov    %rbp,(%rsp)
mov    %rsp,%rdx
xor    %edi,%edi
mov    %r14d,%esi
call   1050 <sched_setscheduler@plt>
cmp    $0xffffffff,%eax
jne    131c <main+0x14c>
lea    0xd90(%rip),%rdi
call   10d0 <pexit@plt>
xor    %eax,%eax
call   1040 <getuid@plt>
mov    %eax,%edi
call   10f0 <seteuid@plt>
cmp    $0xffffffff,%eax
jne    133b <main+0x16b>
lea    0xd84(%rip),%rdi
call   10d0 <pexit@plt>
mov    0x18(%r15),%rdi
add    $0x18,%r15
mov    %r15,%rsi
call   10a0 <execvp@plt>
lea    0xd70(%rip),%rdi
call   10d0 <pexit@plt>
mov    0x2c62(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sched_param = type { i64 }
@EXIT_FAILURE = global i32 0
@.str = [51 x i8] c"This program must be run as root (or setuid-root)\0A\00"
@.str.1 = [2 x i8] c"r\00"
@SCHED_RR = global i32 0
@.str.2 = [2 x i8] c"f\00"
@SCHED_FIFO = global i32 0
@.str.3 = [26 x i8] c"Unknown scheduling policy\00"
@LONG_MIN = global i64 0
@LONG_MAX = global i64 0
@.str.4 = [18 x i8] c"Invalid priority\0A\00"
@.str.5 = [23 x i8] c"sched_get_priority_min\00"
@.str.6 = [23 x i8] c"sched_get_priority_max\00"
@.str.7 = [24 x i8] c"priority out of bounds\0A\00"
@.str.8 = [19 x i8] c"sched_setscheduler\00"
@.str.9 = [8 x i8] c"seteuid\00"
@.str.10 = [5 x i8] c"exec\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca i8*
%9 = alloca %struct.sched_param
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 4
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @helpAndLeave(i8* %17, i32 %18)
br label %20
20: ; preds = %14, %2
%21 = call i64 (...) @geteuid()
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 @fatal(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %23, %20
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 @strncmp(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 2)
%30 = icmp ne i32 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %25
%32 = load i32, i32* @SCHED_RR
store i32 %32, i32* %6
br label %44
33: ; preds = %25
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 1
%36 = load i8*, i8** %35
%37 = call i32 @strncmp(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 2)
%38 = icmp ne i32 %37, 0
br i1 %38, label %41, label %39
39: ; preds = %33
%40 = load i32, i32* @SCHED_FIFO
store i32 %40, i32* %6
br label %43
41: ; preds = %33
%42 = call i32 @fatal(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %43
43: ; preds = %41, %39
br label %44
44: ; preds = %43, %31
%45 = load i8**, i8*** %5
%46 = getelementptr inbounds i8*, i8** %45, i64 2
%47 = load i8*, i8** %46
%48 = call i64 @strtol(i8* %47, i8** %8, i32 10)
store i64 %48, i64* %7
%49 = load i64, i64* %7
%50 = load i64, i64* @LONG_MIN
%51 = icmp eq i64 %49, %50
br i1 %51, label %61, label %52
52: ; preds = %44
%53 = load i64, i64* %7
%54 = load i64, i64* @LONG_MAX
%55 = icmp eq i64 %53, %54
br i1 %55, label %61, label %56
56: ; preds = %52
%57 = load i8*, i8** %8
%58 = load i8, i8* %57
%59 = sext i8 %58 to i32
%60 = icmp ne i32 %59, 0
br i1 %60, label %61, label %63
61: ; preds = %56, %52, %44
%62 = call i32 @fatal(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %63
63: ; preds = %61, %56
%64 = load i32, i32* %6
%65 = call i32 @sched_get_priority_min(i32 %64)
store i32 %65, i32* %10
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %69
67: ; preds = %63
%68 = call i32 @pexit(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
br label %69
69: ; preds = %67, %63
%70 = load i32, i32* %6
%71 = call i32 @sched_get_priority_max(i32 %70)
store i32 %71, i32* %11
%72 = icmp eq i32 %71, -1
br i1 %72, label %73, label %75
73: ; preds = %69
%74 = call i32 @pexit(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
br label %75
75: ; preds = %73, %69
%76 = load i64, i64* %7
%77 = load i32, i32* %10
%78 = sext i32 %77 to i64
%79 = icmp slt i64 %76, %78
br i1 %79, label %85, label %80
80: ; preds = %75
%81 = load i64, i64* %7
%82 = load i32, i32* %11
%83 = sext i32 %82 to i64
%84 = icmp sgt i64 %81, %83
br i1 %84, label %85, label %87
85: ; preds = %80, %75
%86 = call i32 @fatal(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
br label %87
87: ; preds = %85, %80
%88 = load i64, i64* %7
%89 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %9, i32 0, i32 0
store i64 %88, i64* %89
%90 = load i32, i32* %6
%91 = call i32 @sched_setscheduler(i32 0, i32 %90, %struct.sched_param* %9)
%92 = icmp eq i32 %91, -1
br i1 %92, label %93, label %95
93: ; preds = %87
%94 = call i32 @pexit(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0))
br label %95
95: ; preds = %93, %87
%96 = call i32 (...) @getuid()
%97 = call i32 @seteuid(i32 %96)
%98 = icmp eq i32 %97, -1
br i1 %98, label %99, label %101
99: ; preds = %95
%100 = call i32 @pexit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0))
br label %101
101: ; preds = %99, %95
%102 = load i8**, i8*** %5
%103 = getelementptr inbounds i8*, i8** %102, i64 3
%104 = load i8*, i8** %103
%105 = load i8**, i8*** %5
%106 = getelementptr inbounds i8*, i8** %105, i64 3
%107 = call i32 @execvp(i8* %104, i8** %106)
%108 = call i32 @pexit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.10, i64 0, i64 0))
%109 = load i32, i32* @EXIT_SUCCESS
%110 = call i32 @exit(i32 %109)
unreachable
}

LLVM IR (-O2):
%struct.sched_param = type { i64 }
@EXIT_FAILURE = global i32 0
@.str = [51 x i8] c"This program must be run as root (or setuid-root)\0A\00"
@.str.1 = [2 x i8] c"r\00"
@SCHED_RR = global i32 0
@.str.2 = [2 x i8] c"f\00"
@SCHED_FIFO = global i32 0
@.str.3 = [26 x i8] c"Unknown scheduling policy\00"
@LONG_MIN = global i64 0
@LONG_MAX = global i64 0
@.str.4 = [18 x i8] c"Invalid priority\0A\00"
@.str.5 = [23 x i8] c"sched_get_priority_min\00"
@.str.6 = [23 x i8] c"sched_get_priority_max\00"
@.str.7 = [24 x i8] c"priority out of bounds\0A\00"
@.str.8 = [19 x i8] c"sched_setscheduler\00"
@.str.9 = [8 x i8] c"seteuid\00"
@.str.10 = [5 x i8] c"exec\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i8*
%4 = alloca %struct.sched_param
%5 = icmp slt i32 %0, 4
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @helpAndLeave(i8* %7, i32 %8)
br label %10
10: ; preds = %6, %2
%11 = tail call i64 (...) @geteuid()
%12 = icmp eq i64 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %10
%14 = tail call i32 @fatal(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
%16 = bitcast i8** %3 to i8*
%17 = bitcast %struct.sched_param* %4 to i8*
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = tail call i32 @strncmp(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 2)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %15
%23 = load i32, i32* @SCHED_RR
br label %32
24: ; preds = %15
%25 = load i8*, i8** %18
%26 = tail call i32 @strncmp(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 2)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = load i32, i32* @SCHED_FIFO
br label %32
30: ; preds = %24
%31 = tail call i32 @fatal(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %32
32: ; preds = %28, %30, %22
%33 = phi i32 [ undef, %30 ], [ %29, %28 ], [ %23, %22 ]
%34 = getelementptr inbounds i8*, i8** %1, i64 2
%35 = load i8*, i8** %34
%36 = call i64 @strtol(i8* %35, i8** %3, i32 10)
%37 = load i64, i64* @LONG_MIN
%38 = icmp eq i64 %36, %37
%39 = load i64, i64* @LONG_MAX
%40 = icmp eq i64 %36, %39
%41 = select i1 %38, i1 true, i1 %40
br i1 %41, label %46, label %42
42: ; preds = %32
%43 = load i8*, i8** %3
%44 = load i8, i8* %43
%45 = icmp eq i8 %44, 0
br i1 %45, label %48, label %46
46: ; preds = %42, %32
%47 = tail call i32 @fatal(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %48
48: ; preds = %46, %42
%49 = tail call i32 @sched_get_priority_min(i32 %33)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %53
51: ; preds = %48
%52 = tail call i32 @pexit(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
br label %53
53: ; preds = %51, %48
%54 = tail call i32 @sched_get_priority_max(i32 %33)
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %58
56: ; preds = %53
%57 = tail call i32 @pexit(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
br label %58
58: ; preds = %56, %53
%59 = sext i32 %49 to i64
%60 = icmp slt i64 %36, %59
%61 = sext i32 %54 to i64
%62 = icmp sgt i64 %36, %61
%63 = select i1 %60, i1 true, i1 %62
br i1 %63, label %64, label %66
64: ; preds = %58
%65 = tail call i32 @fatal(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
br label %66
66: ; preds = %58, %64
%67 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %4, i64 0, i32 0
store i64 %36, i64* %67
%68 = call i32 @sched_setscheduler(i32 0, i32 %33, %struct.sched_param* %4)
%69 = icmp eq i32 %68, -1
br i1 %69, label %70, label %72
70: ; preds = %66
%71 = call i32 @pexit(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0))
br label %72
72: ; preds = %70, %66
%73 = call i32 (...) @getuid()
%74 = call i32 @seteuid(i32 %73)
%75 = icmp eq i32 %74, -1
br i1 %75, label %76, label %78
76: ; preds = %72
%77 = call i32 @pexit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0))
br label %78
78: ; preds = %76, %72
%79 = getelementptr inbounds i8*, i8** %1, i64 3
%80 = load i8*, i8** %79
%81 = call i32 @execvp(i8* %80, i8** %79)
%82 = call i32 @pexit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.10, i64 0, i64 0))
%83 = load i32, i32* @EXIT_SUCCESS
%84 = call i32 @exit(i32 %83)
unreachable
}