#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(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *, char *);
char *getpid();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stdout;
scalar_t__ wait(int *);
void pLeft() {
  pid_t pid;
  fprintf(stdout, "%s%d%s", "P2 (pid=", getpid(), ")\n");
  pid = fork();
  if (pid == 0) {
    sleep(1);
    fprintf(stdout, "%s%d%s", "P5 (pid=", getpid(), ")\n");
    exit(0);
  } else {
    sleep(1);
    fprintf(stdout, "%s%d%s", "P4 (pid=", getpid(), ")\n");
    while (wait((int *)0) != pid)
      ;
    sleep(1);
    fprintf(stdout, "%s%d%s", "P7 (pid=", getpid(), ")\n");
    return;
  }
}


x86 assembly (clang, -O0):
<pLeft>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0xc(%rbp),%edi
mov    %rax,%rcx
lea    0xe78(%rip),%rsi
lea    0xe78(%rip),%rdx
lea    0xe7a(%rip),%r8
call   1040 <fprintf@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11f5 <pLeft+0x95>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x10(%rbp),%edi
mov    %rax,%rcx
lea    0xe25(%rip),%rsi
lea    0xe31(%rip),%rdx
lea    0xe27(%rip),%r8
call   1040 <fprintf@plt>
xor    %edi,%edi
call   1050 <exit@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x14(%rbp),%edi
mov    %rax,%rcx
lea    0xde1(%rip),%rsi
lea    0xdf6(%rip),%rdx
lea    0xde3(%rip),%r8
call   1040 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <wait@plt>
cmp    -0x8(%rbp),%rax
je     124a <pLeft+0xea>
jmp    1232 <pLeft+0xd2>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%rcx
lea    0xd8c(%rip),%rsi
lea    0xdaa(%rip),%rdx
lea    0xd8e(%rip),%r8
call   1040 <fprintf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pLeft>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e75(%rip),%r14
mov    (%r14),%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe84(%rip),%rsi
lea    0xe84(%rip),%rdx
lea    0xe86(%rip),%r8
mov    %ebx,%edi
mov    %rax,%rcx
call   1040 <fprintf@plt>
xor    %eax,%eax
call   1080 <fork@plt>
mov    %rax,%rbx
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    (%r14),%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe47(%rip),%rsi
test   %rbx,%rbx
je     1223 <pLeft+0xc3>
lea    0xe57(%rip),%rdx
lea    0xe44(%rip),%r8
mov    %ebp,%edi
mov    %rax,%rcx
call   1040 <fprintf@plt>
cs nopw 0x0(%rax,%rax,1)
xor    %edi,%edi
call   1070 <wait@plt>
cmp    %rbx,%rax
jne    11e0 <pLeft+0x80>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    (%r14),%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xdf9(%rip),%rsi
lea    0xe17(%rip),%rdx
lea    0xdfb(%rip),%r8
mov    %ebx,%edi
mov    %rax,%rcx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <fprintf@plt>
lea    0xde9(%rip),%rdx
lea    0xddf(%rip),%r8
mov    %ebp,%edi
mov    %rax,%rcx
call   1040 <fprintf@plt>
xor    %edi,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@stdout = global i32 0
@.str = [7 x i8] c"%s%d%s\00"
@.str.1 = [9 x i8] c"P2 (pid=\00"
@.str.2 = [3 x i8] c")\0A\00"
@.str.3 = [9 x i8] c"P5 (pid=\00"
@.str.4 = [9 x i8] c"P4 (pid=\00"
@.str.5 = [9 x i8] c"P7 (pid=\00"
define void @pLeft() {
%1 = alloca i64
%2 = load i32, i32* @stdout
%3 = call i8* (...) @getpid()
%4 = call i32 @fprintf(i32 %2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%5 = call i64 (...) @fork()
store i64 %5, i64* %1
%6 = load i64, i64* %1
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %14
8: ; preds = %0
%9 = call i32 @sleep(i32 1)
%10 = load i32, i32* @stdout
%11 = call i8* (...) @getpid()
%12 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%13 = call i32 @exit(i32 0)
unreachable
14: ; preds = %0
%15 = call i32 @sleep(i32 1)
%16 = load i32, i32* @stdout
%17 = call i8* (...) @getpid()
%18 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %19
19: ; preds = %23, %14
%20 = call i64 @wait(i32* null)
%21 = load i64, i64* %1
%22 = icmp ne i64 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %19
br label %19
24: ; preds = %19
%25 = call i32 @sleep(i32 1)
%26 = load i32, i32* @stdout
%27 = call i8* (...) @getpid()
%28 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [7 x i8] c"%s%d%s\00"
@.str.1 = [9 x i8] c"P2 (pid=\00"
@.str.2 = [3 x i8] c")\0A\00"
@.str.3 = [9 x i8] c"P5 (pid=\00"
@.str.4 = [9 x i8] c"P4 (pid=\00"
@.str.5 = [9 x i8] c"P7 (pid=\00"
define void @pLeft() {
%1 = load i32, i32* @stdout
%2 = tail call i8* (...) @getpid()
%3 = tail call i32 @fprintf(i32 %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%4 = tail call i64 (...) @fork()
%5 = icmp eq i64 %4, 0
%6 = tail call i32 @sleep(i32 1)
%7 = load i32, i32* @stdout
%8 = tail call i8* (...) @getpid()
br i1 %5, label %9, label %12
9: ; preds = %0
%10 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%11 = tail call i32 @exit(i32 0)
unreachable
12: ; preds = %0
%13 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %14
14: ; preds = %14, %12
%15 = tail call i64 @wait(i32* null)
%16 = icmp eq i64 %15, %4
br i1 %16, label %17, label %14
17: ; preds = %14
%18 = tail call i32 @sleep(i32 1)
%19 = load i32, i32* @stdout
%20 = tail call i8* (...) @getpid()
%21 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
ret void
}