#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__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getpid();
int /*<<< orphan*/ mibb();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ wait(int *);
int main(int argc, const char *argv[]) {
  pid_t child;
  int status;
  child = fork();
  if (child < 0) {
    fprintf(stderr, "fork failed!\n");
    return -1;
  }
  if (child == 0)
    printf("Parent %d\n", getpid());
  else {
    pid_t grand_child = fork();
    if (grand_child < 0)
      fprintf(stderr, "fork gc failed\n");
    else if (grand_child == 0) {
      printf("Grand child %d\n", getpid());
      mibb();
    } else
      printf("Child %d\n", getpid());
    wait(&status);
  }
  mibb();
  printf("Waiting in %d\n", getpid());
  wait(&status);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11ad <main+0x4d>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
lea    0xe64(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127d <main+0x11d>
cmpq   $0x0,-0x18(%rbp)
jne    11d2 <main+0x72>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe46(%rip),%rdi
call   1040 <printf@plt>
jmp    1251 <main+0xf1>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1202 <main+0xa2>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
lea    0xe21(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1248 <main+0xe8>
cmpq   $0x0,-0x28(%rbp)
jne    122e <main+0xce>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe0c(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <mibb@plt>
jmp    1243 <main+0xe3>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xdfb(%rip),%rdi
call   1040 <printf@plt>
jmp    1248 <main+0xe8>
lea    -0x1c(%rbp),%rdi
call   1070 <wait@plt>
mov    $0x0,%al
call   1050 <mibb@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xddb(%rip),%rdi
call   1040 <printf@plt>
lea    -0x1c(%rbp),%rdi
call   1070 <wait@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     119e <main+0x3e>
je     11ba <main+0x5a>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     11d1 <main+0x71>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
test   %rbx,%rbx
je     11e8 <main+0x88>
lea    0xea4(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
jmp    11fd <main+0x9d>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
lea    0xe52(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    122f <main+0xcf>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe46(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
jmp    1207 <main+0xa7>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
lea    0xe38(%rip),%rsi
call   1060 <fprintf@plt>
jmp    11fd <main+0x9d>
lea    0xe3a(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <mibb@plt>
lea    0xc(%rsp),%rdi
call   1070 <wait@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <mibb@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe25(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
lea    0xc(%rsp),%rdi
call   1070 <wait@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [14 x i8] c"fork failed!\0A\00"
@.str.1 = [11 x i8] c"Parent %d\0A\00"
@.str.2 = [16 x i8] c"fork gc failed\0A\00"
@.str.3 = [16 x i8] c"Grand child %d\0A\00"
@.str.4 = [10 x i8] c"Child %d\0A\00"
@.str.5 = [15 x i8] c"Waiting in %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i64 (...) @fork()
store i64 %9, i64* %6
%10 = load i64, i64* %6
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %46
15: ; preds = %2
%16 = load i64, i64* %6
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = call i32 (...) @getpid()
%20 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %19)
br label %41
21: ; preds = %15
%22 = call i64 (...) @fork()
store i64 %22, i64* %8
%23 = load i64, i64* %8
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %39
28: ; preds = %21
%29 = load i64, i64* %8
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %28
%32 = call i32 (...) @getpid()
%33 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %32)
%34 = call i32 (...) @mibb()
br label %38
35: ; preds = %28
%36 = call i32 (...) @getpid()
%37 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %36)
br label %38
38: ; preds = %35, %31
br label %39
39: ; preds = %38, %25
%40 = call i32 @wait(i32* %7)
br label %41
41: ; preds = %39, %18
%42 = call i32 (...) @mibb()
%43 = call i32 (...) @getpid()
%44 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %43)
%45 = call i32 @wait(i32* %7)
store i32 0, i32* %3
br label %46
46: ; preds = %41, %12
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [14 x i8] c"fork failed!\0A\00"
@.str.1 = [11 x i8] c"Parent %d\0A\00"
@.str.2 = [16 x i8] c"fork gc failed\0A\00"
@.str.3 = [16 x i8] c"Grand child %d\0A\00"
@.str.4 = [10 x i8] c"Child %d\0A\00"
@.str.5 = [15 x i8] c"Waiting in %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i64 (...) @fork()
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %36
10: ; preds = %2
%11 = icmp eq i64 %5, 0
br i1 %11, label %12, label %15
12: ; preds = %10
%13 = tail call i32 (...) @getpid()
%14 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %13)
br label %31
15: ; preds = %10
%16 = tail call i64 (...) @fork()
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %29
21: ; preds = %15
%22 = icmp eq i64 %16, 0
%23 = tail call i32 (...) @getpid()
br i1 %22, label %24, label %27
24: ; preds = %21
%25 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %23)
%26 = tail call i32 (...) @mibb()
br label %29
27: ; preds = %21
%28 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %23)
br label %29
29: ; preds = %24, %27, %18
%30 = call i32 @wait(i32* %3)
br label %31
31: ; preds = %29, %12
%32 = call i32 (...) @mibb()
%33 = call i32 (...) @getpid()
%34 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %33)
%35 = call i32 @wait(i32* %3)
br label %36
36: ; preds = %31, %7
%37 = phi i32 [ -1, %7 ], [ 0, %31 ]
ret i32 %37
}