#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 scalar_t__ pid_t;

/* Variables and functions */
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
scalar_t__ wait(int *);
int main(void) {
  pid_t pid;
  int status;
  printf("Programm gestartet\n");
  pid = fork();
  if (pid < 0) {
    perror("fork error");
    exit(1);
  } else if (pid == 0) {
    sleep(1);
    printf("Kindprozess mit PID %d\n", getpid());
    exit(100);
  }
  sleep(2);
  printf("Elternprozess mit PID %d\n", getpid());
  printf("Kindprozess hat PID %d \n", pid);
  pid = wait(&status);
  if (pid > 0) {
    printf("Kindprozess PID %d ", pid);
    if (WIFEXITED(status)) {
      printf("mit Exit-Status %d ", status >> 8);
    }
    printf("terminiert\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11c9 <main+0x49>
lea    0xe5a(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    11ff <main+0x7f>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x64,%edi
call   1070 <exit@plt>
jmp    1204 <main+0x84>
mov    $0x2,%edi
call   1080 <sleep@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rsi
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x14(%rbp),%rdi
call   1090 <wait@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jle    1295 <main+0x115>
mov    -0x10(%rbp),%rsi
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1287 <main+0x107>
mov    -0x14(%rbp),%esi
sar    $0x8,%esi
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0xee2(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     1243 <main+0xb3>
je     1259 <main+0xc9>
mov    %rax,%rbx
mov    $0x2,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe51(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe5b(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xc(%rsp),%rdi
call   10a0 <wait@plt>
test   %rax,%rax
jle    123b <main+0xab>
lea    0xe54(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0xc(%rsp),%edi
call   1070 <WIFEXITED@plt>
test   %rax,%rax
je     122f <main+0x9f>
mov    0xc(%rsp),%esi
sar    $0x8,%esi
lea    0xe42(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe5b(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xdb6(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xd9a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x64,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"Programm gestartet\0A\00"
@.str.1 = [11 x i8] c"fork error\00"
@.str.2 = [24 x i8] c"Kindprozess mit PID %d\0A\00"
@.str.3 = [26 x i8] c"Elternprozess mit PID %d\0A\00"
@.str.4 = [25 x i8] c"Kindprozess hat PID %d \0A\00"
@.str.5 = [20 x i8] c"Kindprozess PID %d \00"
@.str.6 = [20 x i8] c"mit Exit-Status %d \00"
@.str.7 = [12 x i8] c"terminiert\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
store i32 0, i32* %1
%4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%5 = call i64 (...) @fork()
store i64 %5, i64* %2
%6 = load i64, i64* %2
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = load i64, i64* %2
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %11
%15 = call i32 @sleep(i32 1)
%16 = call i32 (...) @getpid()
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = call i32 @exit(i32 100)
unreachable
19: ; preds = %11
br label %20
20: ; preds = %19
%21 = call i32 @sleep(i32 2)
%22 = call i32 (...) @getpid()
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = load i64, i64* %2
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i64 %24)
%26 = call i64 @wait(i32* %3)
store i64 %26, i64* %2
%27 = load i64, i64* %2
%28 = icmp sgt i64 %27, 0
br i1 %28, label %29, label %41
29: ; preds = %20
%30 = load i64, i64* %2
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i64 %30)
%32 = load i32, i32* %3
%33 = call i64 @WIFEXITED(i32 %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %29
%36 = load i32, i32* %3
%37 = ashr i32 %36, 8
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i32 %37)
br label %39
39: ; preds = %35, %29
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0))
br label %41
41: ; preds = %39, %20
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [11 x i8] c"fork error\00"
@.str.2 = [24 x i8] c"Kindprozess mit PID %d\0A\00"
@.str.3 = [26 x i8] c"Elternprozess mit PID %d\0A\00"
@.str.4 = [25 x i8] c"Kindprozess hat PID %d \0A\00"
@.str.5 = [20 x i8] c"Kindprozess PID %d \00"
@.str.6 = [20 x i8] c"mit Exit-Status %d \00"
@str = [19 x i8] c"Programm gestartet\00"
@str.8 = [11 x i8] c"terminiert\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%4 = tail call i64 (...) @fork()
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = icmp eq i64 %4, 0
br i1 %10, label %11, label %16
11: ; preds = %9
%12 = tail call i32 @sleep(i32 1)
%13 = tail call i32 (...) @getpid()
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%15 = tail call i32 @exit(i32 100)
unreachable
16: ; preds = %9
%17 = tail call i32 @sleep(i32 2)
%18 = tail call i32 (...) @getpid()
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %18)
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i64 %4)
%21 = call i64 @wait(i32* %1)
%22 = icmp sgt i64 %21, 0
br i1 %22, label %23, label %34
23: ; preds = %16
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i64 %21)
%25 = load i32, i32* %1
%26 = call i64 @WIFEXITED(i32 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %32, label %28
28: ; preds = %23
%29 = load i32, i32* %1
%30 = ashr i32 %29, 8
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i32 %30)
br label %32
32: ; preds = %28, %23
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.8, i64 0, i64 0))
br label %34
34: ; preds = %32, %16
ret i32 0
}