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


x86 assembly (clang, -O0):
<nice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
xor    %edi,%edi
call   1030 <__set_errno@plt>
mov    0x2e73(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1040 <getpriority@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1194 <nice+0x64>
mov    0x2e4e(%rip),%rax
cmpl   $0x0,(%rax)
je     1187 <nice+0x57>
movl   $0xffffffff,-0x4(%rbp)
jmp    124f <nice+0x11f>
mov    -0xc(%rbp),%edi
call   1030 <__set_errno@plt>
jmp    1194 <nice+0x64>
mov    -0x8(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    0x2e41(%rip),%rcx
cmp    (%rcx),%eax
jge    11c0 <nice+0x90>
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
jmp    11e6 <nice+0xb6>
mov    -0x10(%rbp),%eax
mov    0x2e16(%rip),%rcx
cmp    (%rcx),%eax
jl     11e1 <nice+0xb1>
mov    0x2e07(%rip),%rax
mov    (%rax),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11e6 <nice+0xb6>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%edx
xor    %esi,%esi
call   1050 <setpriority@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    123c <nice+0x10c>
mov    0x2db3(%rip),%rax
mov    (%rax),%eax
mov    0x2dc2(%rip),%rcx
cmp    (%rcx),%eax
jne    1230 <nice+0x100>
mov    0x2d8b(%rip),%rax
mov    (%rax),%ecx
mov    0x2d92(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    124f <nice+0x11f>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1040 <getpriority@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nice>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%ebx
mov    0x2e7f(%rip),%r15
mov    (%r15),%r14d
xor    %edi,%edi
call   1030 <__set_errno@plt>
mov    0x2e76(%rip),%r12
mov    (%r12),%edi
xor    %esi,%esi
call   1040 <getpriority@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     11a8 <nice+0x78>
add    %ebx,%ebp
mov    0x2e7b(%rip),%rax
mov    (%rax),%eax
mov    0x2e6a(%rip),%rcx
mov    (%rcx),%ecx
lea    -0x1(%rcx),%edx
cmp    %ecx,%ebp
cmovl  %ebp,%edx
cmp    %eax,%ebp
cmovl  %eax,%edx
mov    (%r12),%edi
xor    %esi,%esi
call   1050 <setpriority@plt>
cmp    $0xffffffff,%eax
je     11b8 <nice+0x88>
mov    (%r12),%edi
xor    %esi,%esi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <getpriority@plt>
cmpl   $0x0,(%r15)
jne    11d2 <nice+0xa2>
mov    %r14d,%edi
call   1030 <__set_errno@plt>
jmp    1164 <nice+0x34>
mov    (%r15),%eax
mov    0x2e16(%rip),%rcx
cmp    (%rcx),%eax
jne    11d2 <nice+0xa2>
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%r15)
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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