#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;
typedef int /*<<< orphan*/ intmax_t;

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int fork();
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print_pid(char *);
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ wait(int *);
int main(void) {
  int status;
  int i;
  pid_t pid;
  pid_t ppid;
  i = 0;
  ppid = getpid();
  if (ppid == -1) {
    perror("getpid");
    exit(EXIT_FAILURE);
  }
  print_pid("before fork");
  fflush(stdout);
  fflush(stderr);
  pid = fork();
  if (pid == -1) {
    perror("fork");
    assert(false);
  }
  print_pid("after fork");
  if (pid == 0) {
    print_pid("inside (pid == 0)");
    pid = getpid();
    if (pid == -1) {
      perror("getpid");
      exit(EXIT_FAILURE);
    }
    assert(pid != ppid);
    i++;
    exit(EXIT_SUCCESS);
  }
  print_pid("after (pid == 0)");
  wait(&status);
  if (WIFEXITED(status)) {
    assert(status == WEXITSTATUS(EXIT_SUCCESS));
  } else {
    perror("execl abnormal exit");
    assert(false);
  }
  print_pid("after wait");
  printf("fork() return = %jd\n", (intmax_t)pid);
  assert(i == 0);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11f4 <main+0x44>
lea    0xe1f(%rip),%rdi
call   1080 <perror@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xe0c(%rip),%rdi
call   10b0 <print_pid@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1243 <main+0x93>
lea    0xdd7(%rip),%rdi
call   1080 <perror@plt>
xor    %edi,%edi
call   1050 <assert@plt>
lea    0xdc9(%rip),%rdi
call   10b0 <print_pid@plt>
cmpl   $0x0,-0x10(%rbp)
jne    12bd <main+0x10d>
lea    0xdbe(%rip),%rdi
call   10b0 <print_pid@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1293 <main+0xe3>
lea    0xd80(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%eax
cmp    -0x14(%rbp),%eax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    0x2d12(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xd6c(%rip),%rdi
call   10b0 <print_pid@plt>
lea    -0x8(%rbp),%rdi
call   10c0 <wait@plt>
mov    -0x8(%rbp),%edi
call   1090 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1311 <main+0x161>
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2cd7(%rip),%rax
mov    (%rax),%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
cmp    %ecx,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
jmp    1324 <main+0x174>
lea    0xd29(%rip),%rdi
call   1080 <perror@plt>
xor    %edi,%edi
call   1050 <assert@plt>
lea    0xd2a(%rip),%rdi
call   10b0 <print_pid@plt>
mov    -0x10(%rbp),%esi
lea    0xd26(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    0x2c71(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1030 <getpid@plt>
cmp    $0xffffffff,%eax
je     12d6 <main+0x126>
mov    %eax,%ebp
lea    0xe3b(%rip),%rdi
call   10b0 <print_pid@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     128a <main+0xda>
lea    0xe0d(%rip),%rdi
call   10b0 <print_pid@plt>
test   %ebx,%ebx
je     12be <main+0x10e>
lea    0xe16(%rip),%rdi
call   10b0 <print_pid@plt>
lea    0x4(%rsp),%rdi
call   10c0 <wait@plt>
mov    0x4(%rsp),%edi
call   1090 <WIFEXITED@plt>
test   %rax,%rax
je     12ae <main+0xfe>
mov    0x4(%rsp),%ebp
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
call   1070 <WEXITSTATUS@plt>
xor    %edi,%edi
cmp    %eax,%ebp
sete   %dil
call   1050 <assert@plt>
lea    0xdf8(%rip),%rdi
call   10b0 <print_pid@plt>
lea    0xdf7(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1050 <assert@plt>
mov    0x2d47(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd7d(%rip),%rdi
call   1080 <perror@plt>
xor    %edi,%edi
call   1050 <assert@plt>
lea    0xd6f(%rip),%rdi
call   10b0 <print_pid@plt>
jmp    1213 <main+0x63>
lea    0xd8c(%rip),%rdi
call   1080 <perror@plt>
xor    %edi,%edi
jmp    1251 <main+0xa1>
lea    0xd59(%rip),%rdi
call   10b0 <print_pid@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
cmp    $0xffffffff,%eax
jne    12f0 <main+0x140>
lea    0xd23(%rip),%rdi
call   1080 <perror@plt>
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
xor    %edi,%edi
cmp    %ebp,%eax
setne  %dil
call   1050 <assert@plt>
mov    0x2cc4(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"getpid\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [12 x i8] c"before fork\00"
@stdout = global i32 0
@stderr = global i32 0
@.str.2 = [5 x i8] c"fork\00"
@.str.3 = [11 x i8] c"after fork\00"
@.str.4 = [18 x i8] c"inside (pid == 0)\00"
@EXIT_SUCCESS = global i32 0
@.str.5 = [17 x i8] c"after (pid == 0)\00"
@.str.6 = [20 x i8] c"execl abnormal exit\00"
@.str.7 = [11 x i8] c"after wait\00"
@.str.8 = [21 x i8] c"fork() return = %jd\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %3
%6 = call i32 (...) @getpid()
store i32 %6, i32* %5
%7 = load i32, i32* %5
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %13
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @exit(i32 %11)
unreachable
13: ; preds = %0
%14 = call i32 @print_pid(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* @stdout
%16 = call i32 @fflush(i32 %15)
%17 = load i32, i32* @stderr
%18 = call i32 @fflush(i32 %17)
%19 = call i32 (...) @fork()
store i32 %19, i32* %4
%20 = load i32, i32* %4
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %13
%23 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @assert(i32 0)
br label %25
25: ; preds = %22, %13
%26 = call i32 @print_pid(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%27 = load i32, i32* %4
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %48
29: ; preds = %25
%30 = call i32 @print_pid(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%31 = call i32 (...) @getpid()
store i32 %31, i32* %4
%32 = load i32, i32* %4
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %38
34: ; preds = %29
%35 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %29
%39 = load i32, i32* %4
%40 = load i32, i32* %5
%41 = icmp ne i32 %39, %40
%42 = zext i1 %41 to i32
%43 = call i32 @assert(i32 %42)
%44 = load i32, i32* %3
%45 = add nsw i32 %44, 1
store i32 %45, i32* %3
%46 = load i32, i32* @EXIT_SUCCESS
%47 = call i32 @exit(i32 %46)
unreachable
48: ; preds = %25
%49 = call i32 @print_pid(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%50 = call i32 @wait(i32* %2)
%51 = load i32, i32* %2
%52 = call i64 @WIFEXITED(i32 %51)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %61
54: ; preds = %48
%55 = load i32, i32* %2
%56 = load i32, i32* @EXIT_SUCCESS
%57 = call i32 @WEXITSTATUS(i32 %56)
%58 = icmp eq i32 %55, %57
%59 = zext i1 %58 to i32
%60 = call i32 @assert(i32 %59)
br label %64
61: ; preds = %48
%62 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
%63 = call i32 @assert(i32 0)
br label %64
64: ; preds = %61, %54
%65 = call i32 @print_pid(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0))
%66 = load i32, i32* %4
%67 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.8, i64 0, i64 0), i32 %66)
%68 = load i32, i32* %3
%69 = icmp eq i32 %68, 0
%70 = zext i1 %69 to i32
%71 = call i32 @assert(i32 %70)
%72 = load i32, i32* @EXIT_SUCCESS
ret i32 %72
}

LLVM IR (-O2):
@.str = [7 x i8] c"getpid\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [12 x i8] c"before fork\00"
@stdout = global i32 0
@stderr = global i32 0
@.str.2 = [5 x i8] c"fork\00"
@.str.3 = [11 x i8] c"after fork\00"
@.str.4 = [18 x i8] c"inside (pid == 0)\00"
@EXIT_SUCCESS = global i32 0
@.str.5 = [17 x i8] c"after (pid == 0)\00"
@.str.6 = [20 x i8] c"execl abnormal exit\00"
@.str.7 = [11 x i8] c"after wait\00"
@.str.8 = [21 x i8] c"fork() return = %jd\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 (...) @getpid()
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %0
%10 = tail call i32 @print_pid(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%11 = load i32, i32* @stdout
%12 = tail call i32 @fflush(i32 %11)
%13 = load i32, i32* @stderr
%14 = tail call i32 @fflush(i32 %13)
%15 = tail call i32 (...) @fork()
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %9
%18 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%19 = tail call i32 @assert(i32 0)
%20 = tail call i32 @print_pid(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %38
21: ; preds = %9
%22 = tail call i32 @print_pid(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%23 = icmp eq i32 %15, 0
br i1 %23, label %24, label %38
24: ; preds = %21
%25 = tail call i32 @print_pid(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%26 = tail call i32 (...) @getpid()
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = tail call i32 @exit(i32 %30)
unreachable
32: ; preds = %24
%33 = icmp ne i32 %26, %3
%34 = zext i1 %33 to i32
%35 = tail call i32 @assert(i32 %34)
%36 = load i32, i32* @EXIT_SUCCESS
%37 = tail call i32 @exit(i32 %36)
unreachable
38: ; preds = %17, %21
%39 = tail call i32 @print_pid(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%40 = call i32 @wait(i32* %1)
%41 = load i32, i32* %1
%42 = call i64 @WIFEXITED(i32 %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %50, label %44
44: ; preds = %38
%45 = load i32, i32* %1
%46 = load i32, i32* @EXIT_SUCCESS
%47 = call i32 @WEXITSTATUS(i32 %46)
%48 = icmp eq i32 %45, %47
%49 = zext i1 %48 to i32
br label %52
50: ; preds = %38
%51 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
br label %52
52: ; preds = %50, %44
%53 = phi i32 [ 0, %50 ], [ %49, %44 ]
%54 = call i32 @assert(i32 %53)
%55 = call i32 @print_pid(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0))
%56 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.8, i64 0, i64 0), i32 %15)
%57 = call i32 @assert(i32 1)
%58 = load i32, i32* @EXIT_SUCCESS
ret i32 %58
}