#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*/ getpid();
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ sched_exit(int);
int sched_getpid();
int sched_getppid();
int /*<<< orphan*/ sched_nice(int);
void child() {
  switch (sched_getpid()) {
  case 2:
    sched_nice(10);
    break;
  case 3:
    sched_nice(8);
    break;
  case 4:
    sched_nice(4);
    break;
  case 5:
    sched_nice(6);
    break;
  case 6:
    sched_nice(2);
    break;
  }
  printf("In child with pid %d, ppid %d\n", sched_getpid(), sched_getppid());
  unsigned long max = 1e8;
  for (unsigned long i = 0; i < max; i++) {
    getpid();
  }
  sched_exit(sched_getpid());
}


x86 assembly (clang, -O0):
<child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
xor    %eax,%eax
call   1080 <sched_getpid@plt>
add    $0xfffffffe,%eax
mov    %eax,%ecx
mov    %rcx,-0x18(%rbp)
sub    $0x4,%eax
ja     11db <child+0x7b>
mov    -0x18(%rbp),%rax
lea    0xe74(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    $0xa,%edi
call   1030 <sched_nice@plt>
jmp    11db <child+0x7b>
mov    $0x8,%edi
call   1030 <sched_nice@plt>
jmp    11db <child+0x7b>
mov    $0x4,%edi
call   1030 <sched_nice@plt>
jmp    11db <child+0x7b>
mov    $0x6,%edi
call   1030 <sched_nice@plt>
jmp    11db <child+0x7b>
mov    $0x2,%edi
call   1030 <sched_nice@plt>
mov    $0x0,%al
call   1080 <sched_getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1070 <sched_getppid@plt>
mov    -0x1c(%rbp),%esi
mov    %eax,%edx
lea    0xe1c(%rip),%rdi
call   1050 <printf@plt>
movq   $0x5f5e100,-0x8(%rbp)
05
movq   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x8(%rbp),%rax
jae    1233 <child+0xd3>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    120d <child+0xad>
mov    $0x0,%al
call   1080 <sched_getpid@plt>
mov    %eax,%edi
call   1060 <sched_exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<child>:
push   %rbx
xor    %eax,%eax
call   1080 <sched_getpid@plt>
add    $0xfffffffe,%eax
cmp    $0x4,%eax
ja     1181 <child+0x21>
cltq
lea    0xea7(%rip),%rcx
mov    (%rcx,%rax,4),%edi
call   1030 <sched_nice@plt>
xor    %eax,%eax
call   1080 <sched_getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1070 <sched_getppid@plt>
lea    0xe68(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1050 <printf@plt>
mov    $0x5f5e100,%ebx
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <getpid@plt>
add    $0xffffffffffffffff,%rbx
jne    11b0 <child+0x50>
xor    %eax,%eax
call   1080 <sched_getpid@plt>
mov    %eax,%edi
pop    %rbx
jmp    1060 <sched_exit@plt>

LLVM IR (-O0):
@.str = [31 x i8] c"In child with pid %d, ppid %d\0A\00"
define void @child() {
%1 = alloca i64
%2 = alloca i64
%3 = call i32 (...) @sched_getpid()
switch i32 %3, label %14 [
i32 2, label %4
i32 3, label %6
i32 4, label %8
i32 5, label %10
i32 6, label %12
]
4: ; preds = %0
%5 = call i32 @sched_nice(i32 10)
br label %14
6: ; preds = %0
%7 = call i32 @sched_nice(i32 8)
br label %14
8: ; preds = %0
%9 = call i32 @sched_nice(i32 4)
br label %14
10: ; preds = %0
%11 = call i32 @sched_nice(i32 6)
br label %14
12: ; preds = %0
%13 = call i32 @sched_nice(i32 2)
br label %14
14: ; preds = %0, %12, %10, %8, %6, %4
%15 = call i32 (...) @sched_getpid()
%16 = call i32 (...) @sched_getppid()
%17 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %15, i32 %16)
store i64 100000000, i64* %1
store i64 0, i64* %2
br label %18
18: ; preds = %24, %14
%19 = load i64, i64* %2
%20 = load i64, i64* %1
%21 = icmp ult i64 %19, %20
br i1 %21, label %22, label %27
22: ; preds = %18
%23 = call i32 (...) @getpid()
br label %24
24: ; preds = %22
%25 = load i64, i64* %2
%26 = add i64 %25, 1
store i64 %26, i64* %2
br label %18
27: ; preds = %18
%28 = call i32 (...) @sched_getpid()
%29 = call i32 @sched_exit(i32 %28)
ret void
}

LLVM IR (-O2):
@.str = [31 x i8] c"In child with pid %d, ppid %d\0A\00"
@switch.table.child = [5 x i32] [i32 10, i32 8, i32 4, i32 6, i32 2]
define void @child() {
%1 = tail call i32 (...) @sched_getpid()
%2 = add i32 %1, -2
%3 = icmp ult i32 %2, 5
br i1 %3, label %4, label %9
4: ; preds = %0
%5 = sext i32 %2 to i64
%6 = getelementptr inbounds [5 x i32], [5 x i32]* @switch.table.child, i64 0, i64 %5
%7 = load i32, i32* %6
%8 = tail call i32 @sched_nice(i32 %7)
br label %9
9: ; preds = %0, %4
%10 = tail call i32 (...) @sched_getpid()
%11 = tail call i32 (...) @sched_getppid()
%12 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %11)
br label %16
13: ; preds = %16
%14 = tail call i32 (...) @sched_getpid()
%15 = tail call i32 @sched_exit(i32 %14)
ret void
16: ; preds = %9, %16
%17 = phi i64 [ 0, %9 ], [ %19, %16 ]
%18 = tail call i32 (...) @getpid()
%19 = add nuw nsw i64 %17, 1
%20 = icmp eq i64 %19, 100000000
br i1 %20, label %13, label %16
}