#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 */
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int getpid();
int getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ wait(int *);
int main(void) {
  pid_t pid;
  int status;
  int i;
  i = 5;
  printf("\n\ni vor fork: %d\n\n", i);
  pid = fork();
  switch (pid) {
  case -1:
    perror("Could not fork");
    break;
  case 0:
    i++;
    printf("\n... ich bin das Kind %d mit i %d, ", getpid(), i);
    printf("meine Eltern sind %d \n", getppid());
    break;
  default:
    i--;
    printf("\n... wird sind die Eltern %d mit i %d ", getpid(), i);
    printf("und Kind %d,\n    unsere Eltern sind %d\n", pid, getppid());
    wait(&status);
    break;
  }
  printf("\n. . . . . und wer bin ich ?(pid:%d)\n\n", getpid());
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x5,-0x10(%rbp)
mov    -0x10(%rbp),%esi
lea    0xe70(%rip),%rdi
xor    %eax,%eax
mov    %al,-0x15(%rbp)
call   1040 <printf@plt>
mov    -0x15(%rbp),%al
call   1090 <fork@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
sub    $0xffffffff,%eax
je     11c9 <main+0x59>
jmp    11b9 <main+0x49>
mov    -0x14(%rbp),%eax
test   %eax,%eax
je     11da <main+0x6a>
jmp    1219 <main+0xa9>
lea    0xe43(%rip),%rdi
call   1050 <perror@plt>
jmp    1265 <main+0xf5>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x10(%rbp),%edx
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <getppid@plt>
mov    %eax,%esi
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1265 <main+0xf5>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x10(%rbp),%edx
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x1c(%rbp),%esi
mov    %eax,%edx
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0xc(%rbp),%rdi
call   1080 <wait@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe37(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0xe84(%rip),%rdi
mov    $0x5,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1090 <fork@plt>
cmp    $0xffffffff,%eax
jne    11be <main+0x4e>
lea    0xe78(%rip),%rdi
call   1050 <perror@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xefe(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    %eax,%ebx
xor    %eax,%eax
test   %ebx,%ebx
jne    11f9 <main+0x89>
call   1030 <getpid@plt>
lea    0xe50(%rip),%rdi
mov    %eax,%esi
mov    $0x6,%edx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe58(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11a0 <main+0x30>
call   1030 <getpid@plt>
lea    0xe58(%rip),%rdi
mov    %eax,%esi
mov    $0x4,%edx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe63(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xc(%rsp),%rdi
call   1080 <wait@plt>
jmp    11a0 <main+0x30>

LLVM IR (-O0):
@.str = [19 x i8] c"\0A\0Ai vor fork: %d\0A\0A\00"
@.str.1 = [15 x i8] c"Could not fork\00"
@.str.2 = [36 x i8] c"\0A... ich bin das Kind %d mit i %d, \00"
@.str.3 = [23 x i8] c"meine Eltern sind %d \0A\00"
@.str.4 = [39 x i8] c"\0A... wird sind die Eltern %d mit i %d \00"
@.str.5 = [40 x i8] c"und Kind %d,\0A unsere Eltern sind %d\0A\00"
@.str.6 = [39 x i8] c"\0A. . . . . und wer bin ich ?(pid:%d)\0A\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
store i32 5, i32* %4
%5 = load i32, i32* %4
%6 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = call i32 (...) @fork()
store i32 %7, i32* %2
%8 = load i32, i32* %2
switch i32 %8, label %19 [
i32 -1, label %9
i32 0, label %11
]
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %29
11: ; preds = %0
%12 = load i32, i32* %4
%13 = add nsw i32 %12, 1
store i32 %13, i32* %4
%14 = call i32 (...) @getpid()
%15 = load i32, i32* %4
%16 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %14, i32 %15)
%17 = call i32 (...) @getppid()
%18 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %17)
br label %29
19: ; preds = %0
%20 = load i32, i32* %4
%21 = add nsw i32 %20, -1
store i32 %21, i32* %4
%22 = call i32 (...) @getpid()
%23 = load i32, i32* %4
%24 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i32 %22, i32 %23)
%25 = load i32, i32* %2
%26 = call i32 (...) @getppid()
%27 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i32 %25, i32 %26)
%28 = call i32 @wait(i32* %3)
br label %29
29: ; preds = %19, %11, %9
%30 = call i32 (...) @getpid()
%31 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0), i32 %30)
%32 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [19 x i8] c"\0A\0Ai vor fork: %d\0A\0A\00"
@.str.1 = [15 x i8] c"Could not fork\00"
@.str.2 = [36 x i8] c"\0A... ich bin das Kind %d mit i %d, \00"
@.str.3 = [23 x i8] c"meine Eltern sind %d \0A\00"
@.str.4 = [39 x i8] c"\0A... wird sind die Eltern %d mit i %d \00"
@.str.5 = [40 x i8] c"und Kind %d,\0A unsere Eltern sind %d\0A\00"
@.str.6 = [39 x i8] c"\0A. . . . . und wer bin ich ?(pid:%d)\0A\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 5)
%4 = tail call i32 (...) @fork()
switch i32 %4, label %12 [
i32 -1, label %5
i32 0, label %7
]
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %18
7: ; preds = %0
%8 = tail call i32 (...) @getpid()
%9 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %8, i32 6)
%10 = tail call i32 (...) @getppid()
%11 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %10)
br label %18
12: ; preds = %0
%13 = tail call i32 (...) @getpid()
%14 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i32 %13, i32 4)
%15 = tail call i32 (...) @getppid()
%16 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i32 %4, i32 %15)
%17 = call i32 @wait(i32* %1)
br label %18
18: ; preds = %12, %7, %5
%19 = call i32 (...) @getpid()
%20 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0), i32 %19)
%21 = call i32 @exit(i32 0)
unreachable
}