#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 */
typedef int id_t;

/* Variables and functions */
int PRIO_MAX;
int PRIO_MIN;
scalar_t__ errno;
int getpriority(int, int);
int /*<<< orphan*/ printf(char *, int, int, int);
int setpriority(int, int, int);
int /*<<< orphan*/ warn(char *, int);
int donice(int which, id_t who, int prio, int incr) {
  int oldprio;
  errno = 0;
  if ((oldprio = getpriority(which, who)) == -1 && errno != 0) {
    warn("%d: getpriority", who);
    return 1;
  }
  if (incr)
    prio = oldprio + prio;
  if (prio > PRIO_MAX)
    prio = PRIO_MAX;
  if (prio < PRIO_MIN)
    prio = PRIO_MIN;
  if (setpriority(which, who, prio) == -1) {
    warn("%d: setpriority", who);
    return 1;
  }
  (void)printf("%d: old priority %d, new priority %d\n", who, oldprio, prio);
  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)
mov    %ecx,-0x14(%rbp)
mov    0x2e75(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1030 <getpriority@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    11a5 <donice+0x65>
mov    0x2e50(%rip),%rax
cmpq   $0x0,(%rax)
je     11a5 <donice+0x65>
mov    -0xc(%rbp),%esi
lea    0xe6c(%rip),%rdi
call   1040 <warn@plt>
movl   $0x1,-0x4(%rbp)
jmp    1242 <donice+0x102>
cmpl   $0x0,-0x14(%rbp)
je     11b8 <donice+0x78>
mov    -0x18(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    0x2e1e(%rip),%rcx
cmp    (%rcx),%eax
jle    11d6 <donice+0x96>
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    0x2e08(%rip),%rcx
cmp    (%rcx),%eax
jge    11f4 <donice+0xb4>
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1060 <setpriority@plt>
cmp    $0xffffffff,%eax
jne    1226 <donice+0xe6>
mov    -0xc(%rbp),%esi
lea    0xdfb(%rip),%rdi
call   1040 <warn@plt>
movl   $0x1,-0x4(%rbp)
jmp    1242 <donice+0x102>
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x10(%rbp),%ecx
lea    0xdea(%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    %ecx,%ebp
mov    %edx,%r12d
mov    %esi,%r14d
mov    %edi,%r15d
mov    0x2e73(%rip),%r13
movq   $0x0,0x0(%r13)
call   1030 <getpriority@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1181 <donice+0x41>
cmpq   $0x0,0x0(%r13)
je     1181 <donice+0x41>
lea    0xe81(%rip),%rdi
jmp    11d5 <donice+0x95>
test   %ebp,%ebp
cmovne %ebx,%ebp
add    %r12d,%ebp
mov    0x2e50(%rip),%rax
mov    (%rax),%eax
cmp    %eax,%ebp
cmovg  %eax,%ebp
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
cmp    %eax,%ebp
cmovl  %eax,%ebp
mov    %r15d,%edi
mov    %r14d,%esi
mov    %ebp,%edx
call   1060 <setpriority@plt>
cmp    $0xffffffff,%eax
je     11ce <donice+0x8e>
lea    0xe62(%rip),%rdi
mov    %r14d,%esi
mov    %ebx,%edx
mov    %ebp,%ecx
call   1050 <printf@plt>
xor    %eax,%eax
jmp    11e2 <donice+0xa2>
lea    0xe3b(%rip),%rdi
mov    %r14d,%esi
call   1040 <warn@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i64 0
@.str = [16 x i8] c"%d: getpriority\00"
@PRIO_MAX = global i32 0
@PRIO_MIN = global i32 0
@.str.1 = [16 x i8] c"%d: setpriority\00"
@.str.2 = [38 x i8] c"%d: old priority %d, new priority %d\0A\00"
define i32 @donice(i32 %0, i32 %1, i32 %2, i32 %3) {
store i64 0, i64* @errno
%5 = tail call i32 @getpriority(i32 %0, i32 %1)
%6 = icmp eq i32 %5, -1
%7 = load i64, i64* @errno
%8 = icmp ne i64 %7, 0
%9 = select i1 %6, i1 %8, i1 false
br i1 %9, label %10, label %12
10: ; preds = %4
%11 = tail call i32 @warn(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %1)
br label %28
12: ; preds = %4
%13 = icmp eq i32 %3, 0
%14 = select i1 %13, i32 0, i32 %5
%15 = add nsw i32 %14, %2
%16 = load i32, i32* @PRIO_MAX
%17 = icmp sgt i32 %15, %16
%18 = select i1 %17, i32 %16, i32 %15
%19 = load i32, i32* @PRIO_MIN
%20 = icmp slt i32 %18, %19
%21 = select i1 %20, i32 %19, i32 %18
%22 = tail call i32 @setpriority(i32 %0, i32 %1, i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %12
%25 = tail call i32 @warn(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %1)
br label %28
26: ; preds = %12
%27 = tail call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32 %1, i32 %5, i32 %21)
br label %28
28: ; preds = %26, %24, %10
%29 = phi i32 [ 1, %10 ], [ 1, %24 ], [ 0, %26 ]
ret i32 %29
}