#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 execl(char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int getpid();
int getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ wait(int *);
int main(void) {
  pid_t pid;
  int status;
  int i, retval;
  char str[8];
  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++;
    sprintf(str, "%d", i);
    retval = execl("./ChildProgA3.e", "ChildProgA3.e", str, NULL);
    if (retval < 0)
      perror("\nexecl not successful");
    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    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x5,-0x10(%rbp)
mov    -0x10(%rbp),%esi
lea    0xe50(%rip),%rdi
xor    %eax,%eax
mov    %al,-0x21(%rbp)
call   1040 <printf@plt>
mov    -0x21(%rbp),%al
call   10b0 <fork@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
sub    $0xffffffff,%eax
je     11e9 <main+0x59>
jmp    11d9 <main+0x49>
mov    -0x20(%rbp),%eax
test   %eax,%eax
je     11fa <main+0x6a>
jmp    124f <main+0xbf>
lea    0xe23(%rip),%rdi
call   1050 <perror@plt>
jmp    129b <main+0x10b>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
lea    -0x1c(%rbp),%rdi
mov    -0x10(%rbp),%edx
lea    0xe11(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x1c(%rbp),%rdx
lea    0xe04(%rip),%rdi
lea    0xdff(%rip),%rsi
xor    %eax,%eax
mov    %eax,%ecx
call   1090 <execl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    124a <main+0xba>
lea    0xdf0(%rip),%rdi
call   1050 <perror@plt>
jmp    129b <main+0x10b>
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    0xde0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1070 <getppid@plt>
mov    -0x28(%rbp),%esi
mov    %eax,%edx
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0xc(%rbp),%rdi
call   10a0 <wait@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0xe64(%rip),%rdi
mov    $0x5,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     11c3 <main+0x33>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1222 <main+0x92>
lea    0xe52(%rip),%rdi
jmp    11ff <main+0x6f>
lea    0xe58(%rip),%rsi
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x6,%edx
call   1060 <sprintf@plt>
lea    0xe42(%rip),%rdi
lea    0xe3d(%rip),%rsi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   1090 <execl@plt>
test   %eax,%eax
jns    1204 <main+0x74>
lea    0xe36(%rip),%rdi
call   1050 <perror@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe88(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe1b(%rip),%rdi
mov    %eax,%esi
mov    $0x4,%edx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1070 <getppid@plt>
lea    0xe26(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x4(%rsp),%rdi
call   10a0 <wait@plt>
jmp    1204 <main+0x74>

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 = [3 x i8] c"%d\00"
@.str.3 = [16 x i8] c"./ChildProgA3.e\00"
@.str.4 = [14 x i8] c"ChildProgA3.e\00"
@.str.5 = [22 x i8] c"\0Aexecl not successful\00"
@.str.6 = [39 x i8] c"\0A... wird sind die Eltern %d mit i %d \00"
@.str.7 = [40 x i8] c"und Kind %d,\0A unsere Eltern sind %d\0A\00"
@.str.8 = [40 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
%5 = alloca i32
%6 = alloca [8 x i8]
store i32 0, i32* %1
store i32 5, i32* %4
%7 = load i32, i32* %4
%8 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = call i32 (...) @fork()
store i32 %9, i32* %2
%10 = load i32, i32* %2
switch i32 %10, label %26 [
i32 -1, label %11
i32 0, label %13
]
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %36
13: ; preds = %0
%14 = load i32, i32* %4
%15 = add nsw i32 %14, 1
store i32 %15, i32* %4
%16 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 0
%17 = load i32, i32* %4
%18 = call i32 @sprintf(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%19 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 0
%20 = call i32 @execl(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* %19, i32* null)
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %13
%24 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
br label %25
25: ; preds = %23, %13
br label %36
26: ; preds = %0
%27 = load i32, i32* %4
%28 = add nsw i32 %27, -1
store i32 %28, i32* %4
%29 = call i32 (...) @getpid()
%30 = load i32, i32* %4
%31 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0), i32 %29, i32 %30)
%32 = load i32, i32* %2
%33 = call i32 (...) @getppid()
%34 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.7, i64 0, i64 0), i32 %32, i32 %33)
%35 = call i32 @wait(i32* %3)
br label %36
36: ; preds = %26, %25, %11
%37 = call i32 (...) @getpid()
%38 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.8, i64 0, i64 0), i32 %37)
%39 = 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 = [3 x i8] c"%d\00"
@.str.3 = [16 x i8] c"./ChildProgA3.e\00"
@.str.4 = [14 x i8] c"ChildProgA3.e\00"
@.str.5 = [22 x i8] c"\0Aexecl not successful\00"
@.str.6 = [39 x i8] c"\0A... wird sind die Eltern %d mit i %d \00"
@.str.7 = [40 x i8] c"und Kind %d,\0A unsere Eltern sind %d\0A\00"
@.str.8 = [40 x i8] c"\0A. . . . . und wer bin ich ?(pid: %d)\0A\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [8 x i8]
%3 = bitcast i32* %1 to i8*
%4 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 0
%5 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 5)
%6 = tail call i32 (...) @fork()
switch i32 %6, label %15 [
i32 -1, label %7
i32 0, label %9
]
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %21
9: ; preds = %0
%10 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 6)
%11 = call i32 @execl(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* %4, i32* null)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %21
13: ; preds = %9
%14 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
br label %21
15: ; preds = %0
%16 = tail call i32 (...) @getpid()
%17 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0), i32 %16, i32 4)
%18 = tail call i32 (...) @getppid()
%19 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.7, i64 0, i64 0), i32 %6, i32 %18)
%20 = call i32 @wait(i32* %1)
br label %21
21: ; preds = %9, %13, %15, %7
%22 = call i32 (...) @getpid()
%23 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.8, i64 0, i64 0), i32 %22)
%24 = call i32 @exit(i32 0)
unreachable
}