#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 WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ exit(int);
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int vfork();
int /*<<< orphan*/ wait(int *);
int main() {
  pid_t p;
  int flag = 0;
  int status = 0;
  p = vfork();
  if (-1 == p) {
    printf("I am the parent. Fork was unsuccessful.\n");
  } else if (0 == p) {
    flag++;
    printf("I am the child. My pid is %d. My parent's pid is %d. flag is %d.\n",
           getpid(), getppid(), flag);
    sleep(1);
    exit(10);
  } else {
    flag++;
    printf("I am the parent. My pid is %d. Child pid is %d. flag is %d.\n",
           getpid(), p, flag);
    wait(&status);
    if (WIFEXITED(status) != 0)
      printf("Child process exited with exit code %d.\n", WEXITSTATUS(status));
    else
      printf("Child process exited normally.\n");
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
call   1060 <vfork@plt>
mov    %eax,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    11d6 <main+0x46>
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    129b <main+0x10b>
xor    %eax,%eax
cmp    -0x8(%rbp),%eax
jne    1225 <main+0x95>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1080 <getppid@plt>
mov    -0x14(%rbp),%esi
mov    %eax,%edx
mov    -0xc(%rbp),%ecx
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    $0xa,%edi
call   1090 <exit@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x8(%rbp),%edx
mov    -0xc(%rbp),%ecx
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x10(%rbp),%rdi
call   10b0 <wait@plt>
mov    -0x10(%rbp),%edi
call   1070 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1283 <main+0xf3>
mov    -0x10(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1291 <main+0x101>
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1296 <main+0x106>
jmp    129b <main+0x10b>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movl   $0x0,0xc(%rsp)
call   1070 <vfork@plt>
cmp    $0xffffffff,%eax
je     1215 <main+0x75>
test   %eax,%eax
je     1235 <main+0x95>
mov    %eax,%ebp
xor    %eax,%eax
mov    %eax,%ebx
call   1040 <getpid@plt>
lea    0xe72(%rip),%rdi
mov    $0x1,%ecx
mov    %eax,%esi
mov    %ebp,%edx
mov    %ebx,%eax
call   1050 <printf@plt>
lea    0xc(%rsp),%rdi
call   10c0 <wait@plt>
mov    0xc(%rsp),%edi
call   1080 <WIFEXITED@plt>
test   %rax,%rax
je     121e <main+0x7e>
mov    0xc(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
lea    0xe77(%rip),%rdi
xor    %ecx,%ecx
mov    %eax,%esi
mov    %ecx,%eax
call   1050 <printf@plt>
jmp    122a <main+0x8a>
lea    0xeab(%rip),%rdi
jmp    1225 <main+0x85>
lea    0xe83(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %eax,%eax
mov    %eax,%ebx
call   1040 <getpid@plt>
mov    %eax,%ebp
mov    %ebx,%eax
call   1090 <getppid@plt>
lea    0xdb2(%rip),%rdi
mov    $0x1,%r14d
mov    %ebp,%esi
mov    %eax,%edx
mov    %r14d,%ecx
mov    %ebx,%eax
call   1050 <printf@plt>
mov    %r14d,%edi
call   10b0 <sleep@plt>
mov    $0xa,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [41 x i8] c"I am the parent. Fork was unsuccessful.\0A\00"
@.str.1 = [66 x i8] c"I am the child. My pid is %d. My parent's pid is %d. flag is %d.\0A\00"
@.str.2 = [61 x i8] c"I am the parent. My pid is %d. Child pid is %d. flag is %d.\0A\00"
@.str.3 = [41 x i8] c"Child process exited with exit code %d.\0A\00"
@.str.4 = [32 x i8] c"Child process exited normally.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %3
store i32 0, i32* %4
%5 = call i32 @vfork()
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = icmp eq i32 -1, %6
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
br label %41
10: ; preds = %0
%11 = load i32, i32* %2
%12 = icmp eq i32 0, %11
br i1 %12, label %13, label %22
13: ; preds = %10
%14 = load i32, i32* %3
%15 = add nsw i32 %14, 1
store i32 %15, i32* %3
%16 = call i32 (...) @getpid()
%17 = call i32 (...) @getppid()
%18 = load i32, i32* %3
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.1, i64 0, i64 0), i32 %16, i32 %17, i32 %18)
%20 = call i32 @sleep(i32 1)
%21 = call i32 @exit(i32 10)
unreachable
22: ; preds = %10
%23 = load i32, i32* %3
%24 = add nsw i32 %23, 1
store i32 %24, i32* %3
%25 = call i32 (...) @getpid()
%26 = load i32, i32* %2
%27 = load i32, i32* %3
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.2, i64 0, i64 0), i32 %25, i32 %26, i32 %27)
%29 = call i32 @wait(i32* %4)
%30 = load i32, i32* %4
%31 = call i64 @WIFEXITED(i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %22
%34 = load i32, i32* %4
%35 = call i32 @WEXITSTATUS(i32 %34)
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i32 %35)
br label %39
37: ; preds = %22
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
br label %39
39: ; preds = %37, %33
br label %40
40: ; preds = %39
br label %41
41: ; preds = %40, %8
%42 = load i32, i32* %1
ret i32 %42
}

LLVM IR (-O2):
@.str.1 = [66 x i8] c"I am the child. My pid is %d. My parent's pid is %d. flag is %d.\0A\00"
@.str.2 = [61 x i8] c"I am the parent. My pid is %d. Child pid is %d. flag is %d.\0A\00"
@.str.3 = [41 x i8] c"Child process exited with exit code %d.\0A\00"
@str = [31 x i8] c"Child process exited normally.\00"
@str.5 = [40 x i8] c"I am the parent. Fork was unsuccessful.\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%3 = call i32 @vfork()
switch i32 %3, label %12 [
i32 -1, label %4
i32 0, label %6
]
4: ; preds = %0
%5 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str.5, i64 0, i64 0))
br label %25
6: ; preds = %0
%7 = call i32 (...) @getpid()
%8 = call i32 (...) @getppid()
%9 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([66 x i8], [66 x i8]* @.str.1, i64 0, i64 0), i32 %7, i32 %8, i32 1)
%10 = call i32 @sleep(i32 1)
%11 = call i32 @exit(i32 10)
unreachable
12: ; preds = %0
%13 = call i32 (...) @getpid()
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @.str.2, i64 0, i64 0), i32 %13, i32 %3, i32 1)
%15 = call i32 @wait(i32* %1)
%16 = load i32, i32* %1
%17 = call i64 @WIFEXITED(i32 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %12
%20 = load i32, i32* %1
%21 = call i32 @WEXITSTATUS(i32 %20)
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i32 %21)
br label %25
23: ; preds = %12
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %25
25: ; preds = %23, %19, %4
ret i32 0
}