#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;
};
struct passwd {
  int /*<<< orphan*/ pw_uid;
};

/* Variables and functions */
int SCHED_FIFO;
int SCHED_OTHER;
int SCHED_RR;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
struct passwd *getpwnam(char *);
scalar_t__ getuid();
int /*<<< orphan*/ sched_get_priority_max(int);
int /*<<< orphan*/ sched_setscheduler(int /*<<< orphan*/, int,
                                      struct sched_param *);
int /*<<< orphan*/ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int set_scheduler(int scheduler, char *username) {
  int policy;
  switch (scheduler) {
  case 0:
    policy = SCHED_OTHER;
    break;
  case 1:
    policy = SCHED_FIFO;
    break;
  case 2:
    policy = SCHED_RR;
    break;
  default:
    fprintf(stderr, "Unknown scheduler %d\n", scheduler);
    return -1;
    break;
  }
  struct sched_param param;
  param.sched_priority = sched_get_priority_max(policy);
  sched_setscheduler(0, policy, &param);
  if (getuid() == 0) {
    struct passwd *user = getpwnam(username);
    if (user == NULL) {
      return -1;
    }
    setuid(user->pw_uid);
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_scheduler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x24(%rbp)
test   %eax,%eax
je     11a4 <set_scheduler+0x44>
jmp    1182 <set_scheduler+0x22>
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
je     11b5 <set_scheduler+0x55>
jmp    1193 <set_scheduler+0x33>
mov    -0x24(%rbp),%eax
sub    $0x2,%eax
je     11c6 <set_scheduler+0x66>
jmp    11d7 <set_scheduler+0x77>
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    11fb <set_scheduler+0x9b>
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    11fb <set_scheduler+0x9b>
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    11fb <set_scheduler+0x9b>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe16(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125b <set_scheduler+0xfb>
mov    -0x14(%rbp),%edi
call   1070 <sched_get_priority_max@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%esi
xor    %edi,%edi
lea    -0x18(%rbp),%rdx
call   1040 <sched_setscheduler@plt>
mov    $0x0,%al
call   1030 <getuid@plt>
cmp    $0x0,%rax
jne    1254 <set_scheduler+0xf4>
mov    -0x10(%rbp),%rdi
call   1050 <getpwnam@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1249 <set_scheduler+0xe9>
movl   $0xffffffff,-0x4(%rbp)
jmp    125b <set_scheduler+0xfb>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1080 <setuid@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_scheduler>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%edx
cmp    $0x3,%edi
jae    11bf <set_scheduler+0x5f>
mov    %rsi,%r14
movslq %edx,%rax
lea    0x2c84(%rip),%rcx
mov    (%rcx,%rax,8),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   1070 <sched_get_priority_max@plt>
mov    %eax,0x8(%rsp)
xor    %ebx,%ebx
lea    0x8(%rsp),%rdx
xor    %edi,%edi
mov    %ebp,%esi
call   1040 <sched_setscheduler@plt>
xor    %eax,%eax
call   1030 <getuid@plt>
test   %rax,%rax
jne    11d9 <set_scheduler+0x79>
mov    %r14,%rdi
call   1050 <getpwnam@plt>
test   %rax,%rax
je     11d4 <set_scheduler+0x74>
mov    (%rax),%edi
call   1080 <setuid@plt>
jmp    11d9 <set_scheduler+0x79>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
lea    0xe31(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sched_param = type { i32 }
%struct.passwd = type { i32 }
@SCHED_OTHER = global i32 0
@SCHED_FIFO = global i32 0
@SCHED_RR = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"Unknown scheduler %d\0A\00"
define i32 @set_scheduler(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.sched_param
%8 = alloca %struct.passwd*
store i32 %0, i32* %4
store i8* %1, i8** %5
%9 = load i32, i32* %4
switch i32 %9, label %16 [
i32 0, label %10
i32 1, label %12
i32 2, label %14
]
10: ; preds = %2
%11 = load i32, i32* @SCHED_OTHER
store i32 %11, i32* %6
br label %20
12: ; preds = %2
%13 = load i32, i32* @SCHED_FIFO
store i32 %13, i32* %6
br label %20
14: ; preds = %2
%15 = load i32, i32* @SCHED_RR
store i32 %15, i32* %6
br label %20
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = load i32, i32* %4
%19 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %18)
store i32 -1, i32* %3
br label %40
20: ; preds = %14, %12, %10
%21 = load i32, i32* %6
%22 = call i32 @sched_get_priority_max(i32 %21)
%23 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %7, i32 0, i32 0
store i32 %22, i32* %23
%24 = load i32, i32* %6
%25 = call i32 @sched_setscheduler(i32 0, i32 %24, %struct.sched_param* %7)
%26 = call i64 (...) @getuid()
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %39
28: ; preds = %20
%29 = load i8*, i8** %5
%30 = call %struct.passwd* @getpwnam(i8* %29)
store %struct.passwd* %30, %struct.passwd** %8
%31 = load %struct.passwd*, %struct.passwd** %8
%32 = icmp eq %struct.passwd* %31, null
br i1 %32, label %33, label %34
33: ; preds = %28
store i32 -1, i32* %3
br label %40
34: ; preds = %28
%35 = load %struct.passwd*, %struct.passwd** %8
%36 = getelementptr inbounds %struct.passwd, %struct.passwd* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = call i32 @setuid(i32 %37)
br label %39
39: ; preds = %34, %20
store i32 0, i32* %3
br label %40
40: ; preds = %39, %33, %16
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
%struct.sched_param = type { i32 }
%struct.passwd = type { i32 }
@SCHED_OTHER = global i32 0
@SCHED_FIFO = global i32 0
@SCHED_RR = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"Unknown scheduler %d\0A\00"
@switch.table.set_scheduler = [3 x i32*] [i32* @SCHED_OTHER, i32* @SCHED_FIFO, i32* @SCHED_RR]
define i32 @set_scheduler(i32 %0, i8* nocapture %1) {
%3 = alloca %struct.sched_param
%4 = icmp ult i32 %0, 3
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %28
8: ; preds = %2
%9 = sext i32 %0 to i64
%10 = getelementptr inbounds [3 x i32*], [3 x i32*]* @switch.table.set_scheduler, i64 0, i64 %9
%11 = load i32*, i32** %10
%12 = load i32, i32* %11
%13 = bitcast %struct.sched_param* %3 to i8*
%14 = tail call i32 @sched_get_priority_max(i32 %12)
%15 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %3, i64 0, i32 0
store i32 %14, i32* %15
%16 = call i32 @sched_setscheduler(i32 0, i32 %12, %struct.sched_param* %3)
%17 = call i64 (...) @getuid()
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %26
19: ; preds = %8
%20 = call %struct.passwd* @getpwnam(i8* %1)
%21 = icmp eq %struct.passwd* %20, null
br i1 %21, label %26, label %22
22: ; preds = %19
%23 = getelementptr inbounds %struct.passwd, %struct.passwd* %20, i64 0, i32 0
%24 = load i32, i32* %23
%25 = call i32 @setuid(i32 %24)
br label %26
26: ; preds = %8, %22, %19
%27 = phi i32 [ -1, %19 ], [ 0, %22 ], [ 0, %8 ]
br label %28
28: ; preds = %26, %5
%29 = phi i32 [ -1, %5 ], [ %27, %26 ]
ret i32 %29
}