#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 */

/* Variables and functions */
int PRIO_PGRP;
int PRIO_USER;
scalar_t__ errno;
int getpriority(int, int);
int /*<<< orphan*/ printf(char *, int, char const *, int, int);
scalar_t__ setpriority(int, int, int);
int /*<<< orphan*/ warn(char *, int, char const *);
int donice(int which, int who, int prio) {
  int oldprio, newprio;
  const char *idtype = ("process ID");
  if (which == PRIO_USER)
    idtype = ("user ID");
  else if (which == PRIO_PGRP)
    idtype = ("process group ID");
  errno = 0;
  oldprio = getpriority(which, who);
  if (oldprio == -1 && errno) {
    warn(("failed to get priority for %d (%s)"), who, idtype);
    return 1;
  }
  if (setpriority(which, who, prio) < 0) {
    warn(("failed to set priority for %d (%s)"), who, idtype);
    return 1;
  }
  errno = 0;
  newprio = getpriority(which, who);
  if (newprio == -1 && errno) {
    warn(("failed to get priority for %d (%s)"), who, idtype);
    return 1;
  }
  printf(("%d (%s) old priority %d, new priority %d\n"), who, idtype, oldprio,
         newprio);
  return 0;
}


x86 assembly (clang, -O0):
<donice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
lea    0xea8(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%eax
mov    0x2e82(%rip),%rcx
cmp    (%rcx),%eax
jne    117e <donice+0x3e>
lea    0xe96(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    11a0 <donice+0x60>
mov    -0x8(%rbp),%eax
mov    0x2e48(%rip),%rcx
cmp    (%rcx),%eax
jne    119b <donice+0x5b>
lea    0xe7c(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    11a0 <donice+0x60>
mov    0x2e31(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1030 <getpriority@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11f6 <donice+0xb6>
mov    0x2e0b(%rip),%rax
cmpq   $0x0,(%rax)
je     11f6 <donice+0xb6>
mov    -0xc(%rbp),%esi
mov    -0x20(%rbp),%rdx
lea    0xe3f(%rip),%rdi
call   1040 <warn@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a4 <donice+0x164>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1060 <setpriority@plt>
cmp    $0x0,%rax
jge    122d <donice+0xed>
mov    -0xc(%rbp),%esi
mov    -0x20(%rbp),%rdx
lea    0xe2b(%rip),%rdi
call   1040 <warn@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a4 <donice+0x164>
mov    0x2da4(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1030 <getpriority@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1283 <donice+0x143>
mov    0x2d7e(%rip),%rax
cmpq   $0x0,(%rax)
je     1283 <donice+0x143>
mov    -0xc(%rbp),%esi
mov    -0x20(%rbp),%rdx
lea    0xdb2(%rip),%rdi
call   1040 <warn@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a4 <donice+0x164>
mov    -0xc(%rbp),%esi
mov    -0x20(%rbp),%rdx
mov    -0x14(%rbp),%ecx
mov    -0x18(%rbp),%r8d
lea    0xdd2(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<donice>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r12d
mov    %esi,%r15d
mov    %edi,%ebp
mov    0x2e76(%rip),%rax
cmp    %edi,(%rax)
lea    0xeb0(%rip),%rax
lea    0xe96(%rip),%rcx
cmove  %rax,%rcx
mov    0x2e73(%rip),%rax
cmp    %edi,(%rax)
lea    0xe8d(%rip),%rbx
cmovne %rcx,%rbx
mov    0x2e4f(%rip),%r13
movq   $0x0,0x0(%r13)
call   1030 <getpriority@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
jne    11a5 <donice+0x65>
cmpq   $0x0,0x0(%r13)
jne    11d5 <donice+0x95>
mov    %ebp,%edi
mov    %r15d,%esi
mov    %r12d,%edx
call   1060 <setpriority@plt>
test   %rax,%rax
js     11de <donice+0x9e>
movq   $0x0,0x0(%r13)
mov    %ebp,%edi
mov    %r15d,%esi
call   1030 <getpriority@plt>
cmp    $0xffffffff,%eax
jne    1204 <donice+0xc4>
cmpq   $0x0,0x0(%r13)
je     1204 <donice+0xc4>
lea    0xe48(%rip),%rdi
jmp    11e5 <donice+0xa5>
lea    0xe62(%rip),%rdi
mov    %r15d,%esi
mov    %rbx,%rdx
call   1040 <warn@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe5f(%rip),%rdi
mov    %r15d,%esi
mov    %rbx,%rdx
mov    %r14d,%ecx
mov    %eax,%r8d
call   1050 <printf@plt>
xor    %eax,%eax
jmp    11f5 <donice+0xb5>

LLVM IR (-O0):
@.str = [11 x i8] c"process ID\00"
@PRIO_USER = global i32 0
@.str.1 = [8 x i8] c"user ID\00"
@PRIO_PGRP = global i32 0
@.str.2 = [17 x i8] c"process group ID\00"
@errno = global i64 0
@.str.3 = [35 x i8] c"failed to get priority for %d (%s)\00"
@.str.4 = [35 x i8] c"failed to set priority for %d (%s)\00"
@.str.5 = [42 x i8] c"%d (%s) old priority %d, new priority %d\0A\00"
define i32 @donice(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8** %10
%11 = load i32, i32* %5
%12 = load i32, i32* @PRIO_USER
%13 = icmp eq i32 %11, %12
br i1 %13, label %14, label %15
14: ; preds = %3
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8** %10
br label %21
15: ; preds = %3
%16 = load i32, i32* %5
%17 = load i32, i32* @PRIO_PGRP
%18 = icmp eq i32 %16, %17
br i1 %18, label %19, label %20
19: ; preds = %15
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8** %10
br label %20
20: ; preds = %19, %15
br label %21
21: ; preds = %20, %14
store i64 0, i64* @errno
%22 = load i32, i32* %5
%23 = load i32, i32* %6
%24 = call i32 @getpriority(i32 %22, i32 %23)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %34
27: ; preds = %21
%28 = load i64, i64* @errno
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = load i32, i32* %6
%32 = load i8*, i8** %10
%33 = call i32 @warn(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %31, i8* %32)
store i32 1, i32* %4
br label %63
34: ; preds = %27, %21
%35 = load i32, i32* %5
%36 = load i32, i32* %6
%37 = load i32, i32* %7
%38 = call i64 @setpriority(i32 %35, i32 %36, i32 %37)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %34
%41 = load i32, i32* %6
%42 = load i8*, i8** %10
%43 = call i32 @warn(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i32 %41, i8* %42)
store i32 1, i32* %4
br label %63
44: ; preds = %34
store i64 0, i64* @errno
%45 = load i32, i32* %5
%46 = load i32, i32* %6
%47 = call i32 @getpriority(i32 %45, i32 %46)
store i32 %47, i32* %9
%48 = load i32, i32* %9
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %57
50: ; preds = %44
%51 = load i64, i64* @errno
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %50
%54 = load i32, i32* %6
%55 = load i8*, i8** %10
%56 = call i32 @warn(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %54, i8* %55)
store i32 1, i32* %4
br label %63
57: ; preds = %50, %44
%58 = load i32, i32* %6
%59 = load i8*, i8** %10
%60 = load i32, i32* %8
%61 = load i32, i32* %9
%62 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0), i32 %58, i8* %59, i32 %60, i32 %61)
store i32 0, i32* %4
br label %63
63: ; preds = %57, %53, %40, %30
%64 = load i32, i32* %4
ret i32 %64
}

LLVM IR (-O2):
@.str = [11 x i8] c"process ID\00"
@PRIO_USER = global i32 0
@.str.1 = [8 x i8] c"user ID\00"
@PRIO_PGRP = global i32 0
@.str.2 = [17 x i8] c"process group ID\00"
@errno = global i64 0
@.str.3 = [35 x i8] c"failed to get priority for %d (%s)\00"
@.str.4 = [35 x i8] c"failed to set priority for %d (%s)\00"
@.str.5 = [42 x i8] c"%d (%s) old priority %d, new priority %d\0A\00"
define i32 @donice(i32 %0, i32 %1, i32 %2) {
%4 = load i32, i32* @PRIO_USER
%5 = icmp eq i32 %4, %0
%6 = load i32, i32* @PRIO_PGRP
%7 = icmp eq i32 %6, %0
%8 = select i1 %7, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0)
%9 = select i1 %5, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %8
store i64 0, i64* @errno
%10 = tail call i32 @getpriority(i32 %0, i32 %1)
%11 = icmp eq i32 %10, -1
%12 = load i64, i64* @errno
%13 = icmp ne i64 %12, 0
%14 = select i1 %11, i1 %13, i1 false
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = tail call i32 @warn(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %1, i8* %9)
br label %32
17: ; preds = %3
%18 = tail call i64 @setpriority(i32 %0, i32 %1, i32 %2)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = tail call i32 @warn(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i32 %1, i8* %9)
br label %32
22: ; preds = %17
store i64 0, i64* @errno
%23 = tail call i32 @getpriority(i32 %0, i32 %1)
%24 = icmp eq i32 %23, -1
%25 = load i64, i64* @errno
%26 = icmp ne i64 %25, 0
%27 = select i1 %24, i1 %26, i1 false
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = tail call i32 @warn(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %1, i8* %9)
br label %32
30: ; preds = %22
%31 = tail call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0), i32 %1, i8* %9, i32 %10, i32 %23)
br label %32
32: ; preds = %30, %28, %20, %15
%33 = phi i32 [ 1, %15 ], [ 1, %20 ], [ 1, %28 ], [ 0, %30 ]
ret i32 %33
}