#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 /*<<< orphan*/ PRIO_PROCESS;
int __getpriority(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ __set_errno(int);
int __setpriority(int /*<<< orphan*/, int /*<<< orphan*/, int);
int errno;
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);
  }
  result = __setpriority(PRIO_PROCESS, 0, prio + incr);
  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    0x2e8e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
xor    %edi,%edi
call   1030 <__set_errno@plt>
mov    0x2e83(%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    0x2e5e(%rip),%rax
cmpl   $0x0,(%rax)
je     1187 <nice+0x57>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <nice+0xd0>
mov    -0xc(%rbp),%edi
call   1030 <__set_errno@plt>
jmp    1194 <nice+0x64>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%edx
add    -0x8(%rbp),%edx
xor    %esi,%esi
call   1050 <__setpriority@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11ed <nice+0xbd>
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    0x2e21(%rip),%rcx
cmp    (%rcx),%eax
jne    11e1 <nice+0xb1>
mov    0x2dea(%rip),%rax
mov    (%rax),%ecx
mov    0x2df1(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <nice+0xd0>
mov    0x2de4(%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    0x2e8f(%rip),%r15
mov    (%r15),%r14d
xor    %edi,%edi
call   1030 <__set_errno@plt>
mov    0x2e86(%rip),%r12
mov    (%r12),%edi
xor    %esi,%esi
call   1040 <__getpriority@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     118b <nice+0x5b>
mov    (%r12),%edi
add    %ebx,%ebp
xor    %esi,%esi
mov    %ebp,%edx
call   1050 <__setpriority@plt>
cmp    $0xffffffff,%eax
je     119b <nice+0x6b>
mov    (%r12),%edi
xor    %esi,%esi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <__getpriority@plt>
cmpl   $0x0,(%r15)
jne    11b5 <nice+0x85>
mov    %r14d,%edi
call   1030 <__set_errno@plt>
jmp    1164 <nice+0x34>
mov    (%r15),%eax
mov    0x2e43(%rip),%rcx
cmp    (%rcx),%eax
jne    11b5 <nice+0x85>
mov    0x2e10(%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
@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 %39
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* @PRIO_PROCESS
%23 = load i32, i32* %5
%24 = load i32, i32* %3
%25 = add nsw i32 %23, %24
%26 = call i32 @__setpriority(i32 %22, i32 0, i32 %25)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %36
29: ; preds = %21
%30 = load i32, i32* @errno
%31 = load i32, i32* @EACCES
%32 = icmp eq i32 %30, %31
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = load i32, i32* @EPERM
store i32 %34, i32* @errno
br label %35
35: ; preds = %33, %29
store i32 -1, i32* %2
br label %39
36: ; preds = %21
%37 = load i32, i32* @PRIO_PROCESS
%38 = call i32 @__getpriority(i32 %37, i32 0)
store i32 %38, i32* %2
br label %39
39: ; preds = %36, %35, %16
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
@errno = global i32 0
@PRIO_PROCESS = 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 %26
10: ; preds = %7
%11 = tail call i32 @__set_errno(i32 %2)
br label %12
12: ; preds = %10, %1
%13 = load i32, i32* @PRIO_PROCESS
%14 = add nsw i32 %5, %0
%15 = tail call i32 @__setpriority(i32 %13, i32 0, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %23
17: ; preds = %12
%18 = load i32, i32* @errno
%19 = load i32, i32* @EACCES
%20 = icmp eq i32 %18, %19
br i1 %20, label %21, label %26
21: ; preds = %17
%22 = load i32, i32* @EPERM
store i32 %22, i32* @errno
br label %26
23: ; preds = %12
%24 = load i32, i32* @PRIO_PROCESS
%25 = tail call i32 @__getpriority(i32 %24, i32 0)
br label %26
26: ; preds = %17, %21, %7, %23
%27 = phi i32 [ %25, %23 ], [ -1, %7 ], [ -1, %21 ], [ -1, %17 ]
ret i32 %27
}