#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_MAX;
int PRIO_MIN;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int, int);
int getpriority(int, int);
scalar_t__ setpriority(int, int, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ warn(char *, int);
int donice(int which, int who, int prio, int incr) {
  int oldprio;
  errno = 0;
  oldprio = getpriority(which, who);
  if (oldprio == -1 && errno) {
    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) < 0) {
    warn("%d: setpriority", who);
    return (1);
  }
  fprintf(stderr, "%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    0x2e6d(%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    11a6 <donice+0x66>
mov    0x2e47(%rip),%rax
cmpq   $0x0,(%rax)
je     11a6 <donice+0x66>
mov    -0xc(%rbp),%esi
lea    0xe6b(%rip),%rdi
call   1040 <warn@plt>
movl   $0x1,-0x4(%rbp)
jmp    124e <donice+0x10e>
cmpl   $0x0,-0x14(%rbp)
je     11b9 <donice+0x79>
mov    -0x18(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    0x2e1d(%rip),%rcx
cmp    (%rcx),%eax
jle    11d7 <donice+0x97>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    0x2e07(%rip),%rcx
cmp    (%rcx),%eax
jge    11f5 <donice+0xb5>
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1050 <setpriority@plt>
cmp    $0x0,%rax
jge    1228 <donice+0xe8>
mov    -0xc(%rbp),%esi
lea    0xdf9(%rip),%rdi
call   1040 <warn@plt>
movl   $0x1,-0x4(%rbp)
jmp    124e <donice+0x10e>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%r8d
lea    0xdde(%rip),%rsi
call   1060 <fprintf@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,%r15d
mov    %edi,%ebx
mov    0x2e6c(%rip),%r13
movq   $0x0,0x0(%r13)
call   1030 <getpriority@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
jne    1181 <donice+0x41>
cmpq   $0x0,0x0(%r13)
je     1181 <donice+0x41>
lea    0xe81(%rip),%rdi
jmp    11e0 <donice+0xa0>
test   %ebp,%ebp
cmovne %r14d,%ebp
add    %r12d,%ebp
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
cmp    %eax,%ebp
cmovg  %eax,%ebp
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
cmp    %eax,%ebp
cmovl  %eax,%ebp
mov    %ebx,%edi
mov    %r15d,%esi
mov    %ebp,%edx
call   1050 <setpriority@plt>
test   %rax,%rax
js     11d9 <donice+0x99>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
lea    0xe59(%rip),%rsi
mov    %r15d,%edx
mov    %r14d,%ecx
mov    %ebp,%r8d
call   1060 <fprintf@plt>
xor    %eax,%eax
jmp    11ed <donice+0xad>
lea    0xe30(%rip),%rdi
mov    %r15d,%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"
@stderr = global i32 0
@.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 = load i32, i32* %10
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %22
16: ; preds = %4
%17 = load i64, i64* @errno
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i32, i32* %7
%21 = call i32 @warn(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %20)
store i32 1, i32* %5
br label %56
22: ; preds = %16, %4
%23 = load i32, i32* %9
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i32, i32* %10
%27 = load i32, i32* %8
%28 = add nsw i32 %26, %27
store i32 %28, i32* %8
br label %29
29: ; preds = %25, %22
%30 = load i32, i32* %8
%31 = load i32, i32* @PRIO_MAX
%32 = icmp sgt i32 %30, %31
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = load i32, i32* @PRIO_MAX
store i32 %34, i32* %8
br label %35
35: ; preds = %33, %29
%36 = load i32, i32* %8
%37 = load i32, i32* @PRIO_MIN
%38 = icmp slt i32 %36, %37
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = load i32, i32* @PRIO_MIN
store i32 %40, i32* %8
br label %41
41: ; preds = %39, %35
%42 = load i32, i32* %6
%43 = load i32, i32* %7
%44 = load i32, i32* %8
%45 = call i64 @setpriority(i32 %42, i32 %43, i32 %44)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %41
%48 = load i32, i32* %7
%49 = call i32 @warn(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %48)
store i32 1, i32* %5
br label %56
50: ; preds = %41
%51 = load i32, i32* @stderr
%52 = load i32, i32* %7
%53 = load i32, i32* %10
%54 = load i32, i32* %8
%55 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32 %52, i32 %53, i32 %54)
store i32 0, i32* %5
br label %56
56: ; preds = %50, %47, %19
%57 = load i32, i32* %5
ret i32 %57
}

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"
@stderr = global i32 0
@.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 %29
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 i64 @setpriority(i32 %0, i32 %1, i32 %21)
%23 = icmp slt i64 %22, 0
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 %29
26: ; preds = %12
%27 = load i32, i32* @stderr
%28 = tail call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32 %1, i32 %5, i32 %21)
br label %29
29: ; preds = %26, %24, %10
%30 = phi i32 [ 1, %10 ], [ 1, %24 ], [ 0, %26 ]
ret i32 %30
}