#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 /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ getuid();
int nice(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int ret, nic;
  nic = nice(0);
  printf("current nice value: %d\n", nic);
  ret = fork();
  if (ret == -1) {
    perror("fork");
    exit(1);
  } else if (ret == 0) {
    setuid(getuid());
    nic = nice(-10);
    if (nic == -1) {
      perror("nice");
      fprintf(stderr, "non-root can't decrease the nice value!\n");
      exit(1);
    }
    printf("Child process's nice value: %d\n", nic);
    return 0;
  }
  nic = nice(-10);
  if (nic == -1) {
    perror("nice");
    exit(1);
  }
  printf("Parent's process nice value: %d\n", nic);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %edi,%edi
call   1060 <nice@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xe56(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11d9 <main+0x59>
lea    0xe4e(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    124e <main+0xce>
mov    $0x0,%al
call   1030 <getuid@plt>
mov    %eax,%edi
call   1090 <setuid@plt>
mov    $0xfffffff6,%edi
call   1060 <nice@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1233 <main+0xb3>
lea    0xe0e(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
lea    0xdfe(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%esi
lea    0xe0e(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1296 <main+0x116>
jmp    1253 <main+0xd3>
mov    $0xfffffff6,%edi
call   1060 <nice@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1280 <main+0x100>
lea    0xdac(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%esi
lea    0xde1(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %edi,%edi
call   1060 <nice@plt>
lea    0xe71(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     11be <main+0x3e>
cmp    $0xffffffff,%eax
je     11ed <main+0x6d>
mov    $0xfffffff6,%edi
call   1060 <nice@plt>
cmp    $0xffffffff,%eax
je     11f6 <main+0x76>
lea    0xeaf(%rip),%rdi
jmp    11e2 <main+0x62>
xor    %eax,%eax
call   1030 <getuid@plt>
mov    %eax,%edi
call   1090 <setuid@plt>
mov    $0xfffffff6,%edi
call   1060 <nice@plt>
cmp    $0xffffffff,%eax
je     120c <main+0x8c>
lea    0xe69(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe24(%rip),%rdi
jmp    11fd <main+0x7d>
lea    0xe20(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe0a(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
lea    0xdfa(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"current nice value: %d\0A\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [5 x i8] c"nice\00"
@stderr = global i32 0
@.str.3 = [41 x i8] c"non-root can't decrease the nice value!\0A\00"
@.str.4 = [32 x i8] c"Child process's nice value: %d\0A\00"
@.str.5 = [33 x i8] c"Parent's process nice value: %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 @nice(i32 0)
store i32 %8, i32* %7
%9 = load i32, i32* %7
%10 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = call i32 (...) @fork()
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i32, i32* %6
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %34
20: ; preds = %17
%21 = call i32 (...) @getuid()
%22 = call i32 @setuid(i32 %21)
%23 = call i32 @nice(i32 -10)
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %31
26: ; preds = %20
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %20
%32 = load i32, i32* %7
%33 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %32)
store i32 0, i32* %3
br label %45
34: ; preds = %17
br label %35
35: ; preds = %34
%36 = call i32 @nice(i32 -10)
store i32 %36, i32* %7
%37 = load i32, i32* %7
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %35
%43 = load i32, i32* %7
%44 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %43)
store i32 0, i32* %3
br label %45
45: ; preds = %42, %31
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@.str = [24 x i8] c"current nice value: %d\0A\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [5 x i8] c"nice\00"
@stderr = global i32 0
@.str.3 = [41 x i8] c"non-root can't decrease the nice value!\0A\00"
@.str.4 = [32 x i8] c"Child process's nice value: %d\0A\00"
@.str.5 = [33 x i8] c"Parent's process nice value: %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @nice(i32 0)
%4 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = tail call i32 (...) @fork()
switch i32 %5, label %21 [
i32 -1, label %6
i32 0, label %9
]
6: ; preds = %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = tail call i32 (...) @getuid()
%11 = tail call i32 @setuid(i32 %10)
%12 = tail call i32 @nice(i32 -10)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %19
14: ; preds = %9
%15 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %9
%20 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %12)
br label %29
21: ; preds = %2
%22 = tail call i32 @nice(i32 -10)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %21
%28 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %22)
br label %29
29: ; preds = %27, %19
ret i32 0
}