#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 */
scalar_t__ EACCES;
scalar_t__ EPERM;
int PRIO_MAX;
int PRIO_MIN;
int /*<<< orphan*/ PRIO_PROCESS;
scalar_t__ errno;
int getpriority(int /*<<< orphan*/, int /*<<< orphan*/);
int setpriority(int /*<<< orphan*/, int /*<<< orphan*/, int);
int nice(int incr) {
  int prio;
  int res;
  errno = 0;
  prio = getpriority(PRIO_PROCESS, 0) + incr;
  if (prio < PRIO_MIN)
    prio = PRIO_MIN;
  if (prio >= PRIO_MAX)
    prio = PRIO_MAX - 1;
  if (setpriority(PRIO_PROCESS, 0, prio) == -1) {
    if (errno == EACCES)
      errno = EPERM;
    return -1;
  } else
    return getpriority(PRIO_PROCESS, 0);
}


x86 assembly (clang, -O0):
<nice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e8e(%rip),%rax
movq   $0x0,(%rax)
mov    0x2e88(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1030 <getpriority@plt>
add    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2e8f(%rip),%rcx
cmp    (%rcx),%eax
jge    116d <nice+0x4d>
mov    0x2e80(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2e69(%rip),%rcx
cmp    (%rcx),%eax
jl     118e <nice+0x6e>
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
sub    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
xor    %esi,%esi
call   1040 <setpriority@plt>
cmp    $0xffffffff,%eax
jne    11e4 <nice+0xc4>
mov    0x2e0f(%rip),%rax
mov    (%rax),%rax
mov    0x2e1d(%rip),%rcx
cmp    (%rcx),%rax
jne    11d8 <nice+0xb8>
mov    0x2de5(%rip),%rax
mov    (%rax),%rcx
mov    0x2deb(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11f7 <nice+0xd7>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1030 <getpriority@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nice>:
push   %r15
push   %r14
push   %rbx
mov    %edi,%ebx
mov    0x2e92(%rip),%r14
movq   $0x0,(%r14)
mov    0x2e8c(%rip),%r15
mov    (%r15),%edi
xor    %esi,%esi
call   1030 <getpriority@plt>
add    %eax,%ebx
mov    0x2e99(%rip),%rax
mov    (%rax),%eax
cmp    %eax,%ebx
cmovl  %eax,%ebx
mov    0x2e83(%rip),%rax
mov    (%rax),%eax
lea    -0x1(%rax),%edx
cmp    %eax,%ebx
cmovl  %ebx,%edx
mov    (%r15),%edi
xor    %esi,%esi
call   1040 <setpriority@plt>
cmp    $0xffffffff,%eax
je     1185 <nice+0x65>
mov    (%r15),%edi
xor    %esi,%esi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <getpriority@plt>
mov    (%r14),%rax
mov    0x2e49(%rip),%rcx
cmp    (%rcx),%rax
jne    11a1 <nice+0x81>
mov    0x2e15(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r14)
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@PRIO_PROCESS = global i32 0
@PRIO_MIN = global i32 0
@PRIO_MAX = global i32 0
@EACCES = global i64 0
@EPERM = global i64 0
define i32 @nice(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
store i64 0, i64* @errno
%6 = load i32, i32* @PRIO_PROCESS
%7 = call i32 @getpriority(i32 %6, i32 0)
%8 = load i32, i32* %3
%9 = add nsw i32 %7, %8
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = load i32, i32* @PRIO_MIN
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = load i32, i32* @PRIO_MIN
store i32 %14, i32* %4
br label %15
15: ; preds = %13, %1
%16 = load i32, i32* %4
%17 = load i32, i32* @PRIO_MAX
%18 = icmp sge i32 %16, %17
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* @PRIO_MAX
%21 = sub nsw i32 %20, 1
store i32 %21, i32* %4
br label %22
22: ; preds = %19, %15
%23 = load i32, i32* @PRIO_PROCESS
%24 = load i32, i32* %4
%25 = call i32 @setpriority(i32 %23, i32 0, i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %34
27: ; preds = %22
%28 = load i64, i64* @errno
%29 = load i64, i64* @EACCES
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = load i64, i64* @EPERM
store i64 %32, i64* @errno
br label %33
33: ; preds = %31, %27
store i32 -1, i32* %2
br label %37
34: ; preds = %22
%35 = load i32, i32* @PRIO_PROCESS
%36 = call i32 @getpriority(i32 %35, i32 0)
store i32 %36, i32* %2
br label %37
37: ; preds = %34, %33
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
@errno = global i64 0
@PRIO_PROCESS = global i32 0
@PRIO_MIN = global i32 0
@PRIO_MAX = global i32 0
@EACCES = global i64 0
@EPERM = global i64 0
define i32 @nice(i32 %0) {
store i64 0, i64* @errno
%2 = load i32, i32* @PRIO_PROCESS
%3 = tail call i32 @getpriority(i32 %2, i32 0)
%4 = add nsw i32 %3, %0
%5 = load i32, i32* @PRIO_MIN
%6 = icmp slt i32 %4, %5
%7 = select i1 %6, i32 %5, i32 %4
%8 = load i32, i32* @PRIO_MAX
%9 = icmp slt i32 %7, %8
%10 = add nsw i32 %8, -1
%11 = select i1 %9, i32 %7, i32 %10
%12 = load i32, i32* @PRIO_PROCESS
%13 = tail call i32 @setpriority(i32 %12, i32 0, i32 %11)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %21
15: ; preds = %1
%16 = load i64, i64* @errno
%17 = load i64, i64* @EACCES
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %24
19: ; preds = %15
%20 = load i64, i64* @EPERM
store i64 %20, i64* @errno
br label %24
21: ; preds = %1
%22 = load i32, i32* @PRIO_PROCESS
%23 = tail call i32 @getpriority(i32 %22, i32 0)
br label %24
24: ; preds = %15, %19, %21
%25 = phi i32 [ %23, %21 ], [ -1, %19 ], [ -1, %15 ]
ret i32 %25
}