#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 fork();
scalar_t__ getpid();
scalar_t__ getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, long, int);
int wait(int *);
int main(int argc, char *argv[]) {
  pid_t pid;
  pid_t wpid;
  int status;
  pid = fork();
  if (pid == -1) {
    perror("fork()");
    return 0;
  } else if (pid == 0) {
    printf("PID %ld: child started, parent is %ld\n", (long)getpid(),
           (long)getppid());
  } else {
    printf("PID %ld: started child PID %ld\n", (long)getpid(), (long)pid);
    wpid = wait(&status);
    if (wpid == -1) {
      perror("wait()");
      return 0;
    } else if (wpid != pid) {
      return 0;
    } else {
      printf("child PID %ld exited status 0x%04X\n", (long)pid, status);
    }
  }
  return 0;
}


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   1080 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11a2 <main+0x42>
lea    0xe6f(%rip),%rdi
call   1050 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1260 <main+0x100>
cmpl   $0x0,-0x14(%rbp)
jne    11d5 <main+0x75>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
lea    0xe3c(%rip),%rdi
call   1040 <printf@plt>
jmp    1254 <main+0xf4>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,%rsi
movslq -0x14(%rbp),%rax
mov    %eax,%edx
lea    0xe42(%rip),%rdi
call   1040 <printf@plt>
lea    -0x1c(%rbp),%rdi
call   1070 <wait@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    121f <main+0xbf>
lea    0xe40(%rip),%rdi
call   1050 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1260 <main+0x100>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     1237 <main+0xd7>
movl   $0x0,-0x4(%rbp)
jmp    1260 <main+0x100>
movslq -0x14(%rbp),%rsi
mov    -0x1c(%rbp),%edx
lea    0xe10(%rip),%rdi
call   1040 <printf@plt>
jmp    124f <main+0xef>
jmp    1254 <main+0xf4>
jmp    1259 <main+0xf9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11b2 <main+0x52>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11dc <main+0x7c>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xea9(%rip),%rdi
mov    %rax,%rsi
mov    %ebx,%edx
call   1040 <printf@plt>
lea    0xc(%rsp),%rdi
call   1070 <wait@plt>
cmp    $0xffffffff,%eax
je     11ea <main+0x8a>
cmp    %ebx,%eax
jne    11d4 <main+0x74>
movslq %ebx,%rsi
mov    0xc(%rsp),%edx
lea    0xea5(%rip),%rdi
jmp    11cf <main+0x6f>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe3d(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe1d(%rip),%rdi
call   1050 <perror@plt>
jmp    11d4 <main+0x74>
lea    0xe5d(%rip),%rdi
call   1050 <perror@plt>
jmp    11d4 <main+0x74>

LLVM IR (-O0):
@.str = [7 x i8] c"fork()\00"
@.str.1 = [39 x i8] c"PID %ld: child started, parent is %ld\0A\00"
@.str.2 = [32 x i8] c"PID %ld: started child PID %ld\0A\00"
@.str.3 = [7 x i8] c"wait()\00"
@.str.4 = [36 x i8] c"child PID %ld exited status 0x%04X\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 (...) @fork()
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %47
14: ; preds = %2
%15 = load i32, i32* %6
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %14
%18 = call i64 (...) @getpid()
%19 = call i64 (...) @getppid()
%20 = trunc i64 %19 to i32
%21 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i64 %18, i32 %20)
br label %45
22: ; preds = %14
%23 = call i64 (...) @getpid()
%24 = load i32, i32* %6
%25 = sext i32 %24 to i64
%26 = trunc i64 %25 to i32
%27 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i64 %23, i32 %26)
%28 = call i32 @wait(i32* %8)
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %22
%32 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
br label %47
33: ; preds = %22
%34 = load i32, i32* %7
%35 = load i32, i32* %6
%36 = icmp ne i32 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %33
store i32 0, i32* %3
br label %47
38: ; preds = %33
%39 = load i32, i32* %6
%40 = sext i32 %39 to i64
%41 = load i32, i32* %8
%42 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i64 %40, i32 %41)
br label %43
43: ; preds = %38
br label %44
44: ; preds = %43
br label %45
45: ; preds = %44, %17
br label %46
46: ; preds = %45
store i32 0, i32* %3
br label %47
47: ; preds = %46, %37, %31, %12
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
@.str = [7 x i8] c"fork()\00"
@.str.1 = [39 x i8] c"PID %ld: child started, parent is %ld\0A\00"
@.str.2 = [32 x i8] c"PID %ld: started child PID %ld\0A\00"
@.str.3 = [7 x i8] c"wait()\00"
@.str.4 = [36 x i8] c"child PID %ld exited status 0x%04X\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 (...) @fork()
switch i32 %5, label %13 [
i32 -1, label %6
i32 0, label %8
]
6: ; preds = %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %26
8: ; preds = %2
%9 = tail call i64 (...) @getpid()
%10 = tail call i64 (...) @getppid()
%11 = trunc i64 %10 to i32
%12 = tail call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i64 %9, i32 %11)
br label %26
13: ; preds = %2
%14 = tail call i64 (...) @getpid()
%15 = sext i32 %5 to i64
%16 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i64 %14, i32 %5)
%17 = call i32 @wait(i32* %3)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %26
21: ; preds = %13
%22 = icmp eq i32 %17, %5
br i1 %22, label %23, label %26
23: ; preds = %21
%24 = load i32, i32* %3
%25 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i64 %15, i32 %24)
br label %26
26: ; preds = %23, %8, %21, %19, %6
ret i32 0
}