#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 EPERM;
int /*<<< orphan*/ PRIO_PROCESS;
int /*<<< orphan*/ __set_errno(int);
int errno;
int getpriority(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ int_add_no_wrap(int, int);
scalar_t__ setpriority(int /*<<< orphan*/, int /*<<< orphan*/,
                       int /*<<< orphan*/);
int __syscall_nice(int incr) {
  int old_priority;
  int old_errno;
  old_errno = errno;
  __set_errno(0);
  old_priority = getpriority(PRIO_PROCESS, 0);
  if ((old_priority == -1) && errno) {
    return -1;
  }
  __set_errno(old_errno);
  if (setpriority(PRIO_PROCESS, 0, int_add_no_wrap(old_priority, incr))) {
    __set_errno(EPERM);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<__syscall_nice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e86(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
xor    %edi,%edi
call   1030 <__set_errno@plt>
mov    0x2e7b(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1040 <getpriority@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1197 <__syscall_nice+0x57>
mov    0x2e56(%rip),%rax
cmpl   $0x0,(%rax)
je     1197 <__syscall_nice+0x57>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ed <__syscall_nice+0xad>
mov    -0x10(%rbp),%edi
call   1030 <__set_errno@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1050 <int_add_no_wrap@plt>
mov    -0x14(%rbp),%edi
mov    %eax,%edx
xor    %esi,%esi
call   1060 <setpriority@plt>
cmp    $0x0,%rax
je     11e6 <__syscall_nice+0xa6>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ed <__syscall_nice+0xad>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__syscall_nice>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r14d
mov    0x2e83(%rip),%r13
mov    0x0(%r13),%r15d
xor    %ebx,%ebx
xor    %edi,%edi
call   1030 <__set_errno@plt>
mov    0x2e77(%rip),%r12
mov    (%r12),%edi
xor    %esi,%esi
call   1040 <getpriority@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1182 <__syscall_nice+0x42>
cmpl   $0x0,0x0(%r13)
jne    11b7 <__syscall_nice+0x77>
mov    %r15d,%edi
call   1030 <__set_errno@plt>
mov    (%r12),%r15d
mov    %ebp,%edi
mov    %r14d,%esi
call   1050 <int_add_no_wrap@plt>
mov    %r15d,%edi
xor    %esi,%esi
mov    %eax,%edx
call   1060 <setpriority@plt>
test   %rax,%rax
je     11bc <__syscall_nice+0x7c>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@PRIO_PROCESS = global i32 0
@EPERM = global i32 0
define i32 @__syscall_nice(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* @errno
store i32 %6, i32* %5
%7 = call i32 @__set_errno(i32 0)
%8 = load i32, i32* @PRIO_PROCESS
%9 = call i32 @getpriority(i32 %8, i32 0)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %1
%13 = load i32, i32* @errno
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %12
store i32 -1, i32* %2
br label %29
16: ; preds = %12, %1
%17 = load i32, i32* %5
%18 = call i32 @__set_errno(i32 %17)
%19 = load i32, i32* @PRIO_PROCESS
%20 = load i32, i32* %4
%21 = load i32, i32* %3
%22 = call i32 @int_add_no_wrap(i32 %20, i32 %21)
%23 = call i64 @setpriority(i32 %19, i32 0, i32 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %16
%26 = load i32, i32* @EPERM
%27 = call i32 @__set_errno(i32 %26)
store i32 -1, i32* %2
br label %29
28: ; preds = %16
store i32 0, i32* %2
br label %29
29: ; preds = %28, %25, %15
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
@errno = global i32 0
@PRIO_PROCESS = global i32 0
@EPERM = global i32 0
define i32 @__syscall_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
%7 = load i32, i32* @errno
%8 = icmp ne i32 %7, 0
%9 = select i1 %6, i1 %8, i1 false
br i1 %9, label %19, label %10
10: ; preds = %1
%11 = tail call i32 @__set_errno(i32 %2)
%12 = load i32, i32* @PRIO_PROCESS
%13 = tail call i32 @int_add_no_wrap(i32 %5, i32 %0)
%14 = tail call i64 @setpriority(i32 %12, i32 0, i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %10
%17 = load i32, i32* @EPERM
%18 = tail call i32 @__set_errno(i32 %17)
br label %19
19: ; preds = %10, %1, %16
%20 = phi i32 [ -1, %16 ], [ -1, %1 ], [ 0, %10 ]
ret i32 %20
}