#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 EXIT_SUCCESS;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ printf(char *);
int vfork();
int wait(int *);
int main(void) {
  pid_t pid;
  int status, wpid;
  char *argv[] = {
      "/bin/ls",
      NULL,
  };
  if ((pid = vfork()) == 0) {
    printf("Hi.  I'm the child process...\n");
    execvp(argv[0], argv);
    _exit(0);
  }
  printf("Hello.  I'm the parent process.\n");
  while (1) {
    wpid = wait(&status);
    if (wpid > 0 && wpid != pid) {
      continue;
    }
    if (wpid == pid)
      break;
  }
  printf("Child process exited.\nGoodbye.\n");
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2c9a(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2c97(%rip),%rax
mov    %rax,-0x18(%rbp)
call   1050 <vfork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jne    11a6 <main+0x56>
lea    0xe7b(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <execvp@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
lea    0xe7a(%rip),%rdi
call   1040 <printf@plt>
lea    -0xc(%rbp),%rdi
call   1070 <wait@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    11d9 <main+0x89>
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
je     11d9 <main+0x89>
jmp    11b2 <main+0x62>
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    11ea <main+0x9a>
jmp    11ef <main+0x9f>
jmp    11b2 <main+0x62>
lea    0xe52(%rip),%rdi
call   1040 <printf@plt>
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x28,%rsp
movaps 0x2ca2(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
call   1050 <vfork@plt>
test   %eax,%eax
je     11ba <main+0x6a>
mov    %eax,%ebx
lea    0xe93(%rip),%rdi
call   1040 <puts@plt>
lea    0xc(%rsp),%r14
nop
mov    %r14,%rdi
call   1070 <wait@plt>
test   %eax,%eax
setle  %cl
cmp    %ebx,%eax
sete   %dl
or     %cl,%dl
cmp    $0x1,%dl
jne    1180 <main+0x30>
cmp    %ebx,%eax
jne    1180 <main+0x30>
lea    0xe84(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe86(%rip),%rdi
call   1040 <puts@plt>
lea    0xe33(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1060 <execvp@plt>
xor    %edi,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"/bin/ls\00"
@__const.main.argv = [2 x i8*] [i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0), i8* null]
@.str.1 = [31 x i8] c"Hi. I'm the child process...\0A\00"
@.str.2 = [33 x i8] c"Hello. I'm the parent process.\0A\00"
@.str.3 = [32 x i8] c"Child process exited.\0AGoodbye.\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [2 x i8*]
store i32 0, i32* %1
%6 = bitcast [2 x i8*]* %5 to i8*
%7 = call i32 @vfork()
store i32 %7, i32* %2
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %16
9: ; preds = %0
%10 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%11 = getelementptr inbounds [2 x i8*], [2 x i8*]* %5, i64 0, i64 0
%12 = load i8*, i8** %11
%13 = getelementptr inbounds [2 x i8*], [2 x i8*]* %5, i64 0, i64 0
%14 = call i32 @execvp(i8* %12, i8** %13)
%15 = call i32 @_exit(i32 0)
unreachable
16: ; preds = %0
%17 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %16, %26, %32
%19 = call i32 @wait(i32* %3)
store i32 %19, i32* %4
%20 = load i32, i32* %4
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %18
%23 = load i32, i32* %4
%24 = load i32, i32* %2
%25 = icmp ne i32 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %22
br label %18
27: ; preds = %22, %18
%28 = load i32, i32* %4
%29 = load i32, i32* %2
%30 = icmp eq i32 %28, %29
br i1 %30, label %31, label %32
31: ; preds = %27
br label %33
32: ; preds = %27
br label %18
33: ; preds = %31
%34 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%35 = load i32, i32* @EXIT_SUCCESS
ret i32 %35
}

LLVM IR (-O2):
@.str = [8 x i8] c"/bin/ls\00"
@__const.main.argv = [2 x i8*] [i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0), i8* null]
@EXIT_SUCCESS = global i32 0
@str = [32 x i8] c"Hello. I'm the parent process.\00"
@str.4 = [31 x i8] c"Child process exited.\0AGoodbye.\00"
@str.5 = [30 x i8] c"Hi. I'm the child process...\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i8*]
%3 = bitcast i32* %1 to i8*
%4 = bitcast [2 x i8*]* %2 to i8*
%5 = call i32 @vfork()
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %0
%8 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.5, i64 0, i64 0))
%9 = getelementptr inbounds [2 x i8*], [2 x i8*]* %2, i64 0, i64 0
%10 = call i32 @execvp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %9)
%11 = call i32 @_exit(i32 0)
unreachable
12: ; preds = %0
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
br label %14
14: ; preds = %14, %12
%15 = call i32 @wait(i32* %1)
%16 = icmp slt i32 %15, 1
%17 = icmp eq i32 %15, %5
%18 = select i1 %16, i1 true, i1 %17
%19 = select i1 %18, i1 %17, i1 false
br i1 %19, label %20, label %14
20: ; preds = %14
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.4, i64 0, i64 0))
%22 = load i32, i32* @EXIT_SUCCESS
ret i32 %22
}