#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;
};
typedef void *pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ PRIO_PROCESS;
int SCHED_RR;
void *atoi(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int get_sched(char *);
int getopt(int, char **, char *);
char *optarg;
scalar_t__ optind;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print_prio(void *);
int sched_setscheduler(void *, int, struct sched_param *);
int setpriority(int /*<<< orphan*/, void *, int);
int /*<<< orphan*/ usage(char *);
int renice_main(int argc, char *argv[]) {
  int prio;
  int realtime = 0;
  int opt;
  int sched = SCHED_RR;
  char *cmd = argv[0];
  do {
    opt = getopt(argc, argv, "rt:g:");
    if (opt == -1)
      break;
    switch (opt) {
    case 'r':
      realtime = 1;
      break;
    case 't':
      sched = get_sched(optarg);
      break;
    case 'g':
      print_prio(atoi(optarg));
      return 0;
    default:
      usage(cmd);
    }
  } while (1);
  argc -= optind;
  argv += optind;
  if (argc < 1)
    usage(cmd);
  prio = atoi(argv[0]);
  argc--;
  argv++;
  if (argc < 1)
    usage(cmd);
  while (argc) {
    pid_t pid;
    pid = atoi(argv[0]);
    argc--;
    argv++;
    if (realtime) {
      struct sched_param sp = {.sched_priority = prio};
      int ret;
      ret = sched_setscheduler(pid, sched, &sp);
      if (ret) {
        perror("sched_set_scheduler");
        exit(EXIT_FAILURE);
      }
    } else {
      int ret;
      ret = setpriority(PRIO_PROCESS, pid, prio);
      if (ret) {
        perror("setpriority");
        exit(EXIT_FAILURE);
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<renice_main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe35(%rip),%rdx
call   1090 <getopt@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11e2 <renice_main+0x52>
jmp    127b <renice_main+0xeb>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x44(%rbp)
sub    $0x67,%eax
je     123b <renice_main+0xab>
jmp    11f6 <renice_main+0x66>
mov    -0x44(%rbp),%eax
sub    $0x72,%eax
je     1218 <renice_main+0x88>
jmp    1207 <renice_main+0x77>
mov    -0x44(%rbp),%eax
sub    $0x74,%eax
je     1224 <renice_main+0x94>
jmp    125e <renice_main+0xce>
movl   $0x1,-0x18(%rbp)
jmp    1267 <renice_main+0xd7>
mov    0x2dcd(%rip),%rax
mov    (%rax),%rdi
call   1050 <get_sched@plt>
mov    %eax,-0x20(%rbp)
jmp    1267 <renice_main+0xd7>
mov    0x2db6(%rip),%rax
mov    (%rax),%rdi
call   10a0 <atoi@plt>
mov    %rax,%rdi
call   1060 <print_prio@plt>
movl   $0x0,-0x4(%rbp)
jmp    13bc <renice_main+0x22c>
mov    -0x28(%rbp),%rdi
call   1070 <usage@plt>
jmp    126c <renice_main+0xdc>
mov    $0x1,%al
test   $0x1,%al
jne    11bd <renice_main+0x2d>
jmp    127b <renice_main+0xeb>
mov    0x2d5e(%rip),%rax
mov    (%rax),%rcx
movslq -0x8(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x8(%rbp)
mov    0x2d4a(%rip),%rax
mov    (%rax),%rax
shl    $0x3,%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jge    12b8 <renice_main+0x128>
mov    -0x28(%rbp),%rdi
call   1070 <usage@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jge    12ef <renice_main+0x15f>
mov    -0x28(%rbp),%rdi
call   1070 <usage@plt>
jmp    12f4 <renice_main+0x164>
cmpl   $0x0,-0x8(%rbp)
je     13b5 <renice_main+0x225>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   10a0 <atoi@plt>
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     136f <renice_main+0x1df>
mov    -0x14(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%esi
lea    -0x38(%rbp),%rdx
call   1040 <sched_setscheduler@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
je     136a <renice_main+0x1da>
lea    0xcaf(%rip),%rdi
call   1080 <perror@plt>
mov    0x2c65(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
jmp    13b0 <renice_main+0x220>
mov    0x2c5a(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <setpriority@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
je     13ab <renice_main+0x21b>
lea    0xc82(%rip),%rdi
call   1080 <perror@plt>
mov    0x2c24(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
jmp    13b0 <renice_main+0x220>
jmp    12f4 <renice_main+0x164>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<renice_main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %edi,%r14d
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    (%rsi),%rbp
xor    %r13d,%r13d
lea    0xe42(%rip),%r12
mov    0x2e33(%rip),%r15
jmp    11d8 <renice_main+0x48>
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1070 <usage@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1090 <getopt@plt>
cmp    $0x71,%eax
jg     1200 <renice_main+0x70>
cmp    $0xffffffff,%eax
je     1220 <renice_main+0x90>
cmp    $0x67,%eax
jne    11d0 <renice_main+0x40>
jmp    1252 <renice_main+0xc2>
nopw   0x0(%rax,%rax,1)
cmp    $0x72,%eax
je     1218 <renice_main+0x88>
cmp    $0x74,%eax
jne    11d0 <renice_main+0x40>
mov    (%r15),%rdi
call   1050 <get_sched@plt>
mov    %eax,0xc(%rsp)
jmp    11d8 <renice_main+0x48>
mov    $0x1,%r13d
jmp    11d8 <renice_main+0x48>
mov    0x2db9(%rip),%rax
mov    (%rax),%r15
mov    %r14d,%eax
sub    %r15d,%eax
test   %eax,%eax
jle    1273 <renice_main+0xe3>
mov    (%rbx,%r15,8),%rdi
mov    %eax,0x14(%rsp)
call   10a0 <atoi@plt>
cmpl   $0x1,0x14(%rsp)
jne    1291 <renice_main+0x101>
mov    %rbp,%rdi
call   1070 <usage@plt>
jmp    1262 <renice_main+0xd2>
mov    (%r15),%rdi
call   10a0 <atoi@plt>
mov    %rax,%rdi
call   1060 <print_prio@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbp,%rdi
call   1070 <usage@plt>
mov    (%rbx,%r15,8),%rdi
call   10a0 <atoi@plt>
mov    %rax,%r12
mov    %rbp,%rdi
call   1070 <usage@plt>
jmp    1294 <renice_main+0x104>
mov    %rax,%r12
add    $0xffffffff,%r14d
lea    0x10(%rsp),%rbp
jmp    12c1 <renice_main+0x131>
nop
mov    0x2d29(%rip),%rcx
mov    (%rcx),%edi
mov    %rax,%rsi
mov    %r12d,%edx
call   1030 <setpriority@plt>
test   %eax,%eax
jne    12f1 <renice_main+0x161>
add    $0x1,%r15
cmp    %r15d,%r14d
je     1262 <renice_main+0xd2>
mov    0x8(%rbx,%r15,8),%rdi
call   10a0 <atoi@plt>
test   %r13d,%r13d
je     12a0 <renice_main+0x110>
mov    %r12d,0x10(%rsp)
mov    %rax,%rdi
mov    0xc(%rsp),%esi
mov    %rbp,%rdx
call   1040 <sched_setscheduler@plt>
test   %eax,%eax
je     12b8 <renice_main+0x128>
lea    0xd17(%rip),%rdi
jmp    12f8 <renice_main+0x168>
lea    0xd22(%rip),%rdi
call   1080 <perror@plt>
mov    0x2cc4(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sched_param = type { i32 }
@SCHED_RR = global i32 0
@.str = [6 x i8] c"rt:g:\00"
@optarg = global i8* null
@optind = global i64 0
@.str.1 = [20 x i8] c"sched_set_scheduler\00"
@EXIT_FAILURE = global i32 0
@PRIO_PROCESS = global i32 0
@.str.2 = [12 x i8] c"setpriority\00"
define i32 @renice_main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca %struct.sched_param
%13 = alloca i32
%14 = alloca i32
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
%15 = load i32, i32* @SCHED_RR
store i32 %15, i32* %9
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
store i8* %18, i8** %10
br label %19
19: ; preds = %40, %2
%20 = load i32, i32* %4
%21 = load i8**, i8*** %5
%22 = call i32 @getopt(i32 %20, i8** %21, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %26
25: ; preds = %19
br label %41
26: ; preds = %19
%27 = load i32, i32* %8
switch i32 %27, label %36 [
i32 114, label %28
i32 116, label %29
i32 103, label %32
]
28: ; preds = %26
store i32 1, i32* %7
br label %39
29: ; preds = %26
%30 = load i8*, i8** @optarg
%31 = call i32 @get_sched(i8* %30)
store i32 %31, i32* %9
br label %39
32: ; preds = %26
%33 = load i8*, i8** @optarg
%34 = call i8* @atoi(i8* %33)
%35 = call i32 @print_prio(i8* %34)
store i32 0, i32* %3
br label %112
36: ; preds = %26
%37 = load i8*, i8** %10
%38 = call i32 @usage(i8* %37)
br label %39
39: ; preds = %36, %29, %28
br label %40
40: ; preds = %39
br i1 true, label %19, label %41
41: ; preds = %40, %25
%42 = load i64, i64* @optind
%43 = load i32, i32* %4
%44 = sext i32 %43 to i64
%45 = sub nsw i64 %44, %42
%46 = trunc i64 %45 to i32
store i32 %46, i32* %4
%47 = load i64, i64* @optind
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 %47
store i8** %49, i8*** %5
%50 = load i32, i32* %4
%51 = icmp slt i32 %50, 1
br i1 %51, label %52, label %55
52: ; preds = %41
%53 = load i8*, i8** %10
%54 = call i32 @usage(i8* %53)
br label %55
55: ; preds = %52, %41
%56 = load i8**, i8*** %5
%57 = getelementptr inbounds i8*, i8** %56, i64 0
%58 = load i8*, i8** %57
%59 = call i8* @atoi(i8* %58)
%60 = ptrtoint i8* %59 to i32
store i32 %60, i32* %6
%61 = load i32, i32* %4
%62 = add nsw i32 %61, -1
store i32 %62, i32* %4
%63 = load i8**, i8*** %5
%64 = getelementptr inbounds i8*, i8** %63, i32 1
store i8** %64, i8*** %5
%65 = load i32, i32* %4
%66 = icmp slt i32 %65, 1
br i1 %66, label %67, label %70
67: ; preds = %55
%68 = load i8*, i8** %10
%69 = call i32 @usage(i8* %68)
br label %70
70: ; preds = %67, %55
br label %71
71: ; preds = %110, %70
%72 = load i32, i32* %4
%73 = icmp ne i32 %72, 0
br i1 %73, label %74, label %111
74: ; preds = %71
%75 = load i8**, i8*** %5
%76 = getelementptr inbounds i8*, i8** %75, i64 0
%77 = load i8*, i8** %76
%78 = call i8* @atoi(i8* %77)
store i8* %78, i8** %11
%79 = load i32, i32* %4
%80 = add nsw i32 %79, -1
store i32 %80, i32* %4
%81 = load i8**, i8*** %5
%82 = getelementptr inbounds i8*, i8** %81, i32 1
store i8** %82, i8*** %5
%83 = load i32, i32* %7
%84 = icmp ne i32 %83, 0
br i1 %84, label %85, label %98
85: ; preds = %74
%86 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %12, i32 0, i32 0
%87 = load i32, i32* %6
store i32 %87, i32* %86
%88 = load i8*, i8** %11
%89 = load i32, i32* %9
%90 = call i32 @sched_setscheduler(i8* %88, i32 %89, %struct.sched_param* %12)
store i32 %90, i32* %13
%91 = load i32, i32* %13
%92 = icmp ne i32 %91, 0
br i1 %92, label %93, label %97
93: ; preds = %85
%94 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%95 = load i32, i32* @EXIT_FAILURE
%96 = call i32 @exit(i32 %95)
unreachable
97: ; preds = %85
br label %110
98: ; preds = %74
%99 = load i32, i32* @PRIO_PROCESS
%100 = load i8*, i8** %11
%101 = load i32, i32* %6
%102 = call i32 @setpriority(i32 %99, i8* %100, i32 %101)
store i32 %102, i32* %14
%103 = load i32, i32* %14
%104 = icmp ne i32 %103, 0
br i1 %104, label %105, label %109
105: ; preds = %98
%106 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%107 = load i32, i32* @EXIT_FAILURE
%108 = call i32 @exit(i32 %107)
unreachable
109: ; preds = %98
br label %110
110: ; preds = %109, %97
br label %71
111: ; preds = %71
store i32 0, i32* %3
br label %112
112: ; preds = %111, %32
%113 = load i32, i32* %3
ret i32 %113
}

LLVM IR (-O2):
%struct.sched_param = type { i32 }
@SCHED_RR = global i32 0
@.str = [6 x i8] c"rt:g:\00"
@optarg = global i8* null
@optind = global i64 0
@.str.1 = [20 x i8] c"sched_set_scheduler\00"
@EXIT_FAILURE = global i32 0
@PRIO_PROCESS = global i32 0
@.str.2 = [12 x i8] c"setpriority\00"
define i32 @renice_main(i32 %0, i8** %1) {
%3 = alloca %struct.sched_param
%4 = load i32, i32* @SCHED_RR
%5 = load i8*, i8** %1
br label %6
6: ; preds = %13, %2
%7 = phi i32 [ %15, %13 ], [ %4, %2 ]
%8 = phi i32 [ %10, %13 ], [ 0, %2 ]
br label %9
9: ; preds = %11, %6
%10 = phi i32 [ %8, %6 ], [ 1, %11 ]
br label %11
11: ; preds = %9, %20
%12 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
switch i32 %12, label %20 [
i32 -1, label %22
i32 114, label %9
i32 116, label %13
i32 103, label %16
]
13: ; preds = %11
%14 = load i8*, i8** @optarg
%15 = tail call i32 @get_sched(i8* %14)
br label %6
16: ; preds = %11
%17 = load i8*, i8** @optarg
%18 = tail call i8* @atoi(i8* %17)
%19 = tail call i32 @print_prio(i8* %18)
br label %72
20: ; preds = %11
%21 = tail call i32 @usage(i8* %5)
br label %11
22: ; preds = %11
%23 = load i64, i64* @optind
%24 = trunc i64 %23 to i32
%25 = sub i32 %0, %24
%26 = getelementptr inbounds i8*, i8** %1, i64 %23
%27 = icmp slt i32 %25, 1
br i1 %27, label %34, label %28
28: ; preds = %22
%29 = load i8*, i8** %26
%30 = tail call i8* @atoi(i8* %29)
%31 = icmp eq i32 %25, 1
br i1 %31, label %32, label %39
32: ; preds = %28
%33 = tail call i32 @usage(i8* %5)
br label %72
34: ; preds = %22
%35 = tail call i32 @usage(i8* %5)
%36 = load i8*, i8** %26
%37 = tail call i8* @atoi(i8* %36)
%38 = tail call i32 @usage(i8* %5)
br label %39
39: ; preds = %28, %34
%40 = phi i8* [ %37, %34 ], [ %30, %28 ]
%41 = ptrtoint i8* %40 to i64
%42 = trunc i64 %41 to i32
%43 = add nsw i32 %25, -1
%44 = icmp eq i32 %10, 0
%45 = bitcast %struct.sched_param* %3 to i8*
%46 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %3, i64 0, i32 0
br label %47
47: ; preds = %39, %69
%48 = phi i32 [ %43, %39 ], [ %70, %69 ]
%49 = phi i8** [ %26, %39 ], [ %50, %69 ]
%50 = getelementptr inbounds i8*, i8** %49, i64 1
%51 = load i8*, i8** %50
%52 = call i8* @atoi(i8* %51)
br i1 %44, label %61, label %53
53: ; preds = %47
store i32 %42, i32* %46
%54 = call i32 @sched_setscheduler(i8* %52, i32 %7, %struct.sched_param* %3)
%55 = icmp eq i32 %54, 0
br i1 %55, label %60, label %56
56: ; preds = %53
%57 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%58 = load i32, i32* @EXIT_FAILURE
%59 = call i32 @exit(i32 %58)
unreachable
60: ; preds = %53
br label %69
61: ; preds = %47
%62 = load i32, i32* @PRIO_PROCESS
%63 = call i32 @setpriority(i32 %62, i8* %52, i32 %42)
%64 = icmp eq i32 %63, 0
br i1 %64, label %69, label %65
65: ; preds = %61
%66 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%67 = load i32, i32* @EXIT_FAILURE
%68 = call i32 @exit(i32 %67)
unreachable
69: ; preds = %61, %60
%70 = add nsw i32 %48, -1
%71 = icmp eq i32 %70, 0
br i1 %71, label %72, label %47
72: ; preds = %69, %32, %16
ret i32 0
}