#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 */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int main(void) {
  pid_t pid = fork();
  if (pid < 0) {
    perror("fork");
    exit(EXIT_FAILURE);
  }
  if (pid > 0) {
    write(STDOUT_FILENO, "Hello ", strlen("Hello "));
  } else {
    while (getppid() != 1)
      ;
    pid_t pid2 = fork();
    if (pid2 < 0) {
      perror("fork");
      exit(EXIT_FAILURE);
    }
    if (pid2 > 0) {
      write(STDOUT_FILENO, "my ", strlen("my "));
    } else {
      while (getppid() != 1)
        ;
      write(STDOUT_FILENO, "friends!\n", strlen("friends!\n"));
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    119f <main+0x3f>
lea    0xe74(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jle    11d8 <main+0x78>
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
lea    0xe48(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x1c(%rbp),%edi
mov    %eax,%edx
lea    0xe37(%rip),%rsi
call   1030 <write@plt>
jmp    12a3 <main+0x143>
jmp    11dd <main+0x7d>
mov    $0x0,%al
call   1060 <getppid@plt>
cmp    $0x1,%eax
je     11f2 <main+0x92>
jmp    11dd <main+0x7d>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1222 <main+0xc2>
lea    0xdf1(%rip),%rdi
call   1050 <perror@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jle    125b <main+0xfb>
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
lea    0xdcc(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x20(%rbp),%edi
mov    %eax,%edx
lea    0xdbb(%rip),%rsi
call   1030 <write@plt>
jmp    129e <main+0x13e>
jmp    1260 <main+0x100>
mov    $0x0,%al
call   1060 <getppid@plt>
cmp    $0x1,%eax
je     1275 <main+0x115>
jmp    1260 <main+0x100>
mov    0x2d74(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
lea    0xd88(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x24(%rbp),%edi
mov    %eax,%edx
lea    0xd77(%rip),%rsi
call   1030 <write@plt>
jmp    12a3 <main+0x143>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     11e2 <main+0x82>
je     1180 <main+0x20>
lea    0xe8d(%rip),%rbx
jmp    11bc <main+0x5c>
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1060 <getppid@plt>
cmp    $0x1,%eax
jne    1180 <main+0x20>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     11e2 <main+0x82>
je     11a3 <main+0x43>
lea    0xe6b(%rip),%rbx
jmp    11bc <main+0x5c>
lea    0xe66(%rip),%rbx
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1060 <getppid@plt>
cmp    $0x1,%eax
jne    11b0 <main+0x50>
mov    0x2e2d(%rip),%rax
mov    (%rax),%ebp
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe17(%rip),%rdi
call   1050 <perror@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@STDOUT_FILENO = global i32 0
@.str.1 = [7 x i8] c"Hello \00"
@.str.2 = [4 x i8] c"my \00"
@.str.3 = [10 x i8] c"friends!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
store i32 0, i32* %1
%4 = call i64 (...) @fork()
store i64 %4, i64* %2
%5 = load i64, i64* %2
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = call i32 @exit(i32 %9)
unreachable
11: ; preds = %0
%12 = load i64, i64* %2
%13 = icmp sgt i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i32, i32* @STDOUT_FILENO
%16 = call i32 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @write(i32 %15, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %16)
br label %48
18: ; preds = %11
br label %19
19: ; preds = %22, %18
%20 = call i32 (...) @getppid()
%21 = icmp ne i32 %20, 1
br i1 %21, label %22, label %23
22: ; preds = %19
br label %19
23: ; preds = %19
%24 = call i64 (...) @fork()
store i64 %24, i64* %3
%25 = load i64, i64* %3
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %23
%32 = load i64, i64* %3
%33 = icmp sgt i64 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %31
%35 = load i32, i32* @STDOUT_FILENO
%36 = call i32 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @write(i32 %35, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %36)
br label %47
38: ; preds = %31
br label %39
39: ; preds = %42, %38
%40 = call i32 (...) @getppid()
%41 = icmp ne i32 %40, 1
br i1 %41, label %42, label %43
42: ; preds = %39
br label %39
43: ; preds = %39
%44 = load i32, i32* @STDOUT_FILENO
%45 = call i32 @strlen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%46 = call i32 @write(i32 %44, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %45)
br label %47
47: ; preds = %43, %34
br label %48
48: ; preds = %47, %14
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@STDOUT_FILENO = global i32 0
@.str.1 = [7 x i8] c"Hello \00"
@.str.2 = [4 x i8] c"my \00"
@.str.3 = [10 x i8] c"friends!\0A\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %7
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @EXIT_FAILURE
%6 = tail call i32 @exit(i32 %5)
unreachable
7: ; preds = %0
%8 = icmp eq i64 %1, 0
br i1 %8, label %9, label %24
9: ; preds = %7, %9
%10 = tail call i32 (...) @getppid()
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %9
12: ; preds = %9
%13 = tail call i64 (...) @fork()
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %12
%20 = icmp eq i64 %13, 0
br i1 %20, label %21, label %24
21: ; preds = %19, %21
%22 = tail call i32 (...) @getppid()
%23 = icmp eq i32 %22, 1
br i1 %23, label %24, label %21
24: ; preds = %21, %19, %7
%25 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %7 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), %19 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), %21 ]
%26 = load i32, i32* @STDOUT_FILENO
%27 = tail call i32 @strlen(i8* %25)
%28 = tail call i32 @write(i32 %26, i8* %25, i32 %27)
ret i32 0
}