#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 /*<<< orphan*/ SCHED_RR;
void *atoi(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print_prio(void *);
int sched_setscheduler(void *, int /*<<< orphan*/, struct sched_param *);
int setpriority(int /*<<< orphan*/, void *, int);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ usage(char *);
int renice_main(int argc, char *argv[]) {
  int prio;
  int realtime = 0;
  char *cmd = argv[0];
  argc--;
  argv++;
  if (argc < 1)
    usage(cmd);
  if (strcmp("-r", argv[0]) == 0) {
    realtime = 1;
    argc--;
    argv++;
  }
  if (strcmp("-g", argv[0]) == 0) {
    if (argc < 2)
      usage(cmd);
    print_prio(atoi(argv[1]));
    return 0;
  }
  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_RR, &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    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%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    11c9 <renice_main+0x49>
mov    -0x20(%rbp),%rdi
call   1070 <usage@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe29(%rip),%rdi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    1202 <renice_main+0x82>
movl   $0x1,-0x18(%rbp)
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdf3(%rip),%rdi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    1253 <renice_main+0xd3>
cmpl   $0x2,-0x8(%rbp)
jge    1232 <renice_main+0xb2>
mov    -0x20(%rbp),%rdi
call   1070 <usage@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %rax,%rdi
call   1060 <print_prio@plt>
movl   $0x0,-0x4(%rbp)
jmp    1370 <renice_main+0x1f0>
cmpl   $0x1,-0x8(%rbp)
jge    1266 <renice_main+0xe6>
mov    -0x20(%rbp),%rdi
call   1070 <usage@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1090 <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    129d <renice_main+0x11d>
mov    -0x20(%rbp),%rdi
call   1070 <usage@plt>
jmp    12a2 <renice_main+0x122>
cmpl   $0x0,-0x8(%rbp)
je     1369 <renice_main+0x1e9>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1090 <atoi@plt>
mov    %rax,-0x28(%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     1323 <renice_main+0x1a3>
mov    -0x14(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2cdc(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1040 <sched_setscheduler@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     131e <renice_main+0x19e>
lea    0xcfb(%rip),%rdi
call   1080 <perror@plt>
mov    0x2cc1(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    1364 <renice_main+0x1e4>
mov    0x2cb6(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <setpriority@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
je     135f <renice_main+0x1df>
lea    0xcce(%rip),%rdi
call   1080 <perror@plt>
mov    0x2c80(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    1364 <renice_main+0x1e4>
jmp    12a2 <renice_main+0x122>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%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,%r12d
mov    (%rsi),%rbp
lea    0x8(%rsi),%r13
cmp    $0x1,%edi
jg     11a8 <renice_main+0x28>
mov    %rbp,%rdi
call   1070 <usage@plt>
mov    %rbp,0x8(%rsp)
mov    0x8(%rbx),%rsi
lea    0xe48(%rip),%rdi
call   1050 <strcmp@plt>
mov    %rax,%r15
lea    0x10(%rbx),%rax
xor    %r14d,%r14d
test   %r15,%r15
cmove  %rax,%r13
sete   %r14b
mov    %r14d,%ebp
not    %ebp
add    %r12d,%ebp
mov    0x8(%rbx,%r14,8),%rsi
lea    0xe1d(%rip),%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
je     1216 <renice_main+0x96>
lea    0x8(,%r14,8),%r14
test   %ebp,%ebp
jle    1247 <renice_main+0xc7>
mov    (%rbx,%r14,1),%rdi
call   1090 <atoi@plt>
cmp    $0x1,%ebp
jne    1269 <renice_main+0xe9>
mov    0x8(%rsp),%rdi
call   1070 <usage@plt>
jmp    1236 <renice_main+0xb6>
cmp    $0x1,%ebp
jg     1225 <renice_main+0xa5>
mov    0x8(%rsp),%rdi
call   1070 <usage@plt>
mov    0x8(%r13),%rdi
call   1090 <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    0x8(%rsp),%rdi
call   1070 <usage@plt>
mov    (%rbx,%r14,1),%rdi
call   1090 <atoi@plt>
mov    %rax,%r12
mov    0x8(%rsp),%rdi
call   1070 <usage@plt>
jmp    126c <renice_main+0xec>
mov    %rax,%r12
add    $0xffffffff,%ebp
xor    %ebx,%ebx
mov    0x2d68(%rip),%r14
jmp    12a7 <renice_main+0x127>
nopw   0x0(%rax,%rax,1)
mov    %r12d,0x10(%rsp)
mov    0x2d3c(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
lea    0x10(%rsp),%rdx
call   1040 <sched_setscheduler@plt>
test   %eax,%eax
jne    12d1 <renice_main+0x151>
add    $0x1,%rbx
cmp    %ebx,%ebp
je     1236 <renice_main+0xb6>
mov    0x8(%r13,%rbx,8),%rdi
call   1090 <atoi@plt>
test   %r15,%r15
je     1280 <renice_main+0x100>
mov    (%r14),%edi
mov    %rax,%rsi
mov    %r12d,%edx
call   1030 <setpriority@plt>
test   %eax,%eax
je     129f <renice_main+0x11f>
lea    0xd4b(%rip),%rdi
jmp    12d8 <renice_main+0x158>
lea    0xd2e(%rip),%rdi
call   1080 <perror@plt>
mov    0x2cf4(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

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

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