#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;
int EPERM;
int /*<<< orphan*/ PRIO_PROCESS;
int __getpriority(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ __set_errno(int);
int __setpriority(int /*<<< orphan*/, int /*<<< orphan*/, int);
scalar_t__ 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;
  }
  result = __setpriority(PRIO_PROCESS, 0, prio + incr);
  if (result == -1) {
    if (errno == EACCES)
      __set_errno(EPERM);
    return -1;
  }
  __set_errno(save);
  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),%rax
mov    %eax,-0xc(%rbp)
xor    %edi,%edi
call   1030 <__set_errno@plt>
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1040 <__getpriority@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    118e <nice+0x5e>
mov    0x2e5d(%rip),%rax
cmpq   $0x0,(%rax)
je     1189 <nice+0x59>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <nice+0xd0>
jmp    118e <nice+0x5e>
mov    0x2e43(%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    11e5 <nice+0xb5>
mov    0x2e18(%rip),%rax
mov    (%rax),%rax
mov    0x2e26(%rip),%rcx
cmp    (%rcx),%rax
jne    11d9 <nice+0xa9>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <nice+0xd0>
mov    -0xc(%rbp),%edi
call   1030 <__set_errno@plt>
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   %rbx
push   %rax
mov    %edi,%ebp
mov    0x2e90(%rip),%r15
mov    (%r15),%r14d
xor    %edi,%edi
call   1030 <__set_errno@plt>
mov    0x2e87(%rip),%rbx
mov    (%rbx),%edi
xor    %esi,%esi
call   1040 <__getpriority@plt>
cmp    $0xffffffff,%eax
jne    1175 <nice+0x45>
cmpq   $0x0,(%r15)
je     1175 <nice+0x45>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rbx),%edi
add    %ebp,%eax
xor    %esi,%esi
mov    %eax,%edx
call   1050 <__setpriority@plt>
cmp    $0xffffffff,%eax
je     11a2 <nice+0x72>
mov    %r14d,%edi
call   1030 <__set_errno@plt>
mov    (%rbx),%edi
xor    %esi,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <__getpriority@plt>
mov    (%r15),%rax
mov    0x2e3c(%rip),%rcx
cmp    (%rcx),%rax
jne    1165 <nice+0x35>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
jmp    1165 <nice+0x35>

LLVM IR (-O0):
@errno = global i64 0
@PRIO_PROCESS = global i32 0
@EACCES = global i64 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 i64, i64* @errno
%8 = trunc i64 %7 to i32
store i32 %8, i32* %4
%9 = call i32 @__set_errno(i32 0)
%10 = load i32, i32* @PRIO_PROCESS
%11 = call i32 @__getpriority(i32 %10, i32 0)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %19
14: ; preds = %1
%15 = load i64, i64* @errno
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %14
store i32 -1, i32* %2
br label %40
18: ; preds = %14
br label %19
19: ; preds = %18, %1
%20 = load i32, i32* @PRIO_PROCESS
%21 = load i32, i32* %5
%22 = load i32, i32* %3
%23 = add nsw i32 %21, %22
%24 = call i32 @__setpriority(i32 %20, i32 0, i32 %23)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %35
27: ; preds = %19
%28 = load i64, i64* @errno
%29 = load i64, i64* @EACCES
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = load i32, i32* @EPERM
%33 = call i32 @__set_errno(i32 %32)
br label %34
34: ; preds = %31, %27
store i32 -1, i32* %2
br label %40
35: ; preds = %19
%36 = load i32, i32* %4
%37 = call i32 @__set_errno(i32 %36)
%38 = load i32, i32* @PRIO_PROCESS
%39 = call i32 @__getpriority(i32 %38, i32 0)
store i32 %39, i32* %2
br label %40
40: ; preds = %35, %34, %17
%41 = load i32, i32* %2
ret i32 %41
}

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