#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_SUCCESS;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdout;
scalar_t__ wait(int *);
int main(int argc, char *argv[]) {
  int wait_stat;
  pid_t pid, ch_pid;
  printf("\nThis is the parent, about to fork. pid=%d\n", getpid());
  fflush(stdout);
  pid = fork();
  if (pid > 0) {
    printf("\nThis is the parent, after the fork. Child pid=%d\n", pid);
    ch_pid = wait(&wait_stat);
    if (WIFEXITED(wait_stat))
      printf("\nChild exited with code %d\n", WEXITSTATUS(wait_stat));
    else
      printf("\nChild terminated abnormally\n");
    exit(EXIT_SUCCESS);
  }
  if (pid == 0) {
    printf("\nThis is the child, after the fork. pid=%d\n", getpid());
    exit(EXIT_SUCCESS);
  }
  {
    perror("fork");
    exit(errno);
  };
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jle    124b <main+0xbb>
mov    -0x20(%rbp),%rsi
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x14(%rbp),%rdi
call   10a0 <wait@plt>
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1080 <WIFEXITED@plt>
cmp    $0x0,%rax
je     122f <main+0x9f>
mov    -0x14(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    123d <main+0xad>
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jne    127b <main+0xeb>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    0xe43(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe51(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
jle    1209 <main+0x69>
lea    0xe53(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x4(%rsp),%rdi
call   10b0 <wait@plt>
mov    0x4(%rsp),%edi
call   1090 <WIFEXITED@plt>
test   %rax,%rax
jne    121b <main+0x7b>
lea    0xeaa(%rip),%rdi
call   1030 <puts@plt>
jmp    1234 <main+0x94>
jne    1242 <main+0xa2>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe62(%rip),%rdi
jmp    122b <main+0x8b>
mov    0x4(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
lea    0xe34(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xe5e(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [44 x i8] c"\0AThis is the parent, about to fork. pid=%d\0A\00"
@stdout = global i32 0
@.str.1 = [51 x i8] c"\0AThis is the parent, after the fork. Child pid=%d\0A\00"
@.str.2 = [28 x i8] c"\0AChild exited with code %d\0A\00"
@.str.3 = [30 x i8] c"\0AChild terminated abnormally\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.4 = [44 x i8] c"\0AThis is the child, after the fork. pid=%d\0A\00"
@.str.5 = [5 x i8] c"fork\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 (...) @getpid()
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = load i32, i32* @stdout
%12 = call i32 @fflush(i32 %11)
%13 = call i64 (...) @fork()
store i64 %13, i64* %7
%14 = load i64, i64* %7
%15 = icmp sgt i64 %14, 0
br i1 %15, label %16, label %32
16: ; preds = %2
%17 = load i64, i64* %7
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i64 %17)
%19 = call i64 @wait(i32* %6)
store i64 %19, i64* %8
%20 = load i32, i32* %6
%21 = call i64 @WIFEXITED(i32 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %16
%24 = load i32, i32* %6
%25 = call i32 @WEXITSTATUS(i32 %24)
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %25)
br label %29
27: ; preds = %16
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
br label %29
29: ; preds = %27, %23
%30 = load i32, i32* @EXIT_SUCCESS
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %2
%33 = load i64, i64* %7
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %32
%36 = call i32 (...) @getpid()
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i32 %36)
%38 = load i32, i32* @EXIT_SUCCESS
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %32
%41 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%42 = load i32, i32* @errno
%43 = call i32 @exit(i32 %42)
unreachable
}

LLVM IR (-O2):
@.str = [44 x i8] c"\0AThis is the parent, about to fork. pid=%d\0A\00"
@stdout = global i32 0
@.str.1 = [51 x i8] c"\0AThis is the parent, after the fork. Child pid=%d\0A\00"
@.str.2 = [28 x i8] c"\0AChild exited with code %d\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.4 = [44 x i8] c"\0AThis is the child, after the fork. pid=%d\0A\00"
@.str.5 = [5 x i8] c"fork\00"
@errno = global i32 0
@str = [29 x i8] c"\0AChild terminated abnormally\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 (...) @getpid()
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = load i32, i32* @stdout
%8 = tail call i32 @fflush(i32 %7)
%9 = tail call i64 (...) @fork()
%10 = icmp sgt i64 %9, 0
br i1 %10, label %11, label %26
11: ; preds = %2
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i64 %9)
%13 = call i64 @wait(i32* %3)
%14 = load i32, i32* %3
%15 = call i64 @WIFEXITED(i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %11
%18 = load i32, i32* %3
%19 = call i32 @WEXITSTATUS(i32 %18)
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %19)
br label %23
21: ; preds = %11
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
br label %23
23: ; preds = %21, %17
%24 = load i32, i32* @EXIT_SUCCESS
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %2
%27 = icmp eq i64 %9, 0
br i1 %27, label %28, label %33
28: ; preds = %26
%29 = tail call i32 (...) @getpid()
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i32 %29)
%31 = load i32, i32* @EXIT_SUCCESS
%32 = tail call i32 @exit(i32 %31)
unreachable
33: ; preds = %26
%34 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%35 = load i32, i32* @errno
%36 = tail call i32 @exit(i32 %35)
unreachable
}