#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_FAILURE;
int EXIT_SUCCESS;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ exit(int);
int fork();
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  pid_t parent, child;
  int status;
  int i;
  parent = getpid();
  if ((child = fork()) == -1) {
    do {
      perror("fork");
      exit(EXIT_FAILURE);
    } while (0);
  }
  if (child) {
    printf("Parent with pid = %d\nJust sapwned Child with pid = %d\n", parent,
           child);
    waitpid(child, &status, 0);
    printf("child exited with status = 0x%08x\n", status);
    if (WIFEXITED(status)) {
      printf("Child exited with status = %d\n", WEXITSTATUS(status));
    }
    if (WIFSIGNALED(status)) {
      printf("Child exited due to signal = %d\n", WTERMSIG(status));
    }
  } else {
    child = getpid();
    printf("Running child with pid = %d\n", child);
    i = 10;
    while (i) {
      i--;
      printf("sleeping for a second\n");
      sleep(1);
      if (i == 5) {
        exit(3);
        *(int *)0 = 10;
      }
    }
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1202 <main+0x52>
jmp    11e8 <main+0x38>
lea    0xe11(%rip),%rdi
call   1090 <perror@plt>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
cmpl   $0x0,-0x18(%rbp)
je     1298 <main+0xe8>
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    -0x1c(%rbp),%esi
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
call   10a0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1269 <main+0xb9>
mov    -0x1c(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1050 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1293 <main+0xe3>
mov    -0x1c(%rbp),%edi
call   1060 <WTERMSIG@plt>
mov    %eax,%esi
lea    0xdf2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1303 <main+0x153>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xa,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     12fe <main+0x14e>
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
cmpl   $0x5,-0x20(%rbp)
jne    12f9 <main+0x149>
mov    $0x3,%edi
call   10b0 <exit@plt>
jmp    12ba <main+0x10a>
jmp    1303 <main+0x153>
mov    0x2cce(%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   1040 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
je     1276 <main+0xb6>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     12f8 <main+0x138>
lea    0xe18(%rip),%rdi
mov    %ebp,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1090 <waitpid@plt>
mov    0x4(%rsp),%esi
lea    0xe2b(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x4(%rsp),%edi
call   10b0 <WIFEXITED@plt>
test   %rax,%rax
je     123f <main+0x7f>
mov    0x4(%rsp),%edi
call   1080 <WEXITSTATUS@plt>
lea    0xe29(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x4(%rsp),%edi
call   1060 <WIFSIGNALED@plt>
test   %rax,%rax
je     1266 <main+0xa6>
mov    0x4(%rsp),%edi
call   1070 <WTERMSIG@plt>
lea    0xe21(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d6b(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe1b(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe28(%rip),%rbx
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
mov    $0x3,%edi
call   10c0 <exit@plt>
lea    0xd01(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cd5(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [55 x i8] c"Parent with pid = %d\0AJust sapwned Child with pid = %d\0A\00"
@.str.2 = [35 x i8] c"child exited with status = 0x%08x\0A\00"
@.str.3 = [31 x i8] c"Child exited with status = %d\0A\00"
@.str.4 = [33 x i8] c"Child exited due to signal = %d\0A\00"
@.str.5 = [29 x i8] c"Running child with pid = %d\0A\00"
@.str.6 = [23 x i8] c"sleeping for a second\0A\00"
@EXIT_SUCCESS = global i32 0
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
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32 (...) @getpid()
store i32 %10, i32* %6
%11 = call i32 (...) @fork()
store i32 %11, i32* %7
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %19
13: ; preds = %2
br label %14
14: ; preds = %13
%15 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; No predecessors!
br label %19
19: ; preds = %18, %2
%20 = load i32, i32* %7
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %46
22: ; preds = %19
%23 = load i32, i32* %6
%24 = load i32, i32* %7
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0), i32 %23, i32 %24)
%26 = load i32, i32* %7
%27 = call i32 @waitpid(i32 %26, i32* %8, i32 0)
%28 = load i32, i32* %8
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %28)
%30 = load i32, i32* %8
%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* %8
%35 = call i32 @WEXITSTATUS(i32 %34)
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %33, %22
%38 = load i32, i32* %8
%39 = call i64 @WIFSIGNALED(i32 %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %37
%42 = load i32, i32* %8
%43 = call i32 @WTERMSIG(i32 %42)
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %43)
br label %45
45: ; preds = %41, %37
br label %64
46: ; preds = %19
%47 = call i32 (...) @getpid()
store i32 %47, i32* %7
%48 = load i32, i32* %7
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %48)
store i32 10, i32* %9
br label %50
50: ; preds = %62, %46
%51 = load i32, i32* %9
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %63
53: ; preds = %50
%54 = load i32, i32* %9
%55 = add nsw i32 %54, -1
store i32 %55, i32* %9
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
%57 = call i32 @sleep(i32 1)
%58 = load i32, i32* %9
%59 = icmp eq i32 %58, 5
br i1 %59, label %60, label %62
60: ; preds = %53
%61 = call i32 @exit(i32 3)
unreachable
62: ; preds = %53
br label %50
63: ; preds = %50
br label %64
64: ; preds = %63, %45
%65 = load i32, i32* @EXIT_SUCCESS
ret i32 %65
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [55 x i8] c"Parent with pid = %d\0AJust sapwned Child with pid = %d\0A\00"
@.str.2 = [35 x i8] c"child exited with status = 0x%08x\0A\00"
@.str.3 = [31 x i8] c"Child exited with status = %d\0A\00"
@.str.4 = [33 x i8] c"Child exited due to signal = %d\0A\00"
@.str.5 = [29 x i8] c"Running child with pid = %d\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [22 x i8] c"sleeping for a second\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 (...) @fork()
switch i32 %6, label %11 [
i32 -1, label %7
i32 0, label %31
]
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %2
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0), i32 %5, i32 %6)
%13 = call i32 @waitpid(i32 %6, i32* %3, i32 0)
%14 = load i32, i32* %3
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%16 = load i32, i32* %3
%17 = call i64 @WIFEXITED(i32 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %11
%20 = load i32, i32* %3
%21 = call i32 @WEXITSTATUS(i32 %20)
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %19, %11
%24 = load i32, i32* %3
%25 = call i64 @WIFSIGNALED(i32 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %45, label %27
27: ; preds = %23
%28 = load i32, i32* %3
%29 = call i32 @WTERMSIG(i32 %28)
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %29)
br label %45
31: ; preds = %2
%32 = tail call i32 (...) @getpid()
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %32)
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%35 = tail call i32 @sleep(i32 1)
%36 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%37 = tail call i32 @sleep(i32 1)
%38 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%39 = tail call i32 @sleep(i32 1)
%40 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%41 = tail call i32 @sleep(i32 1)
%42 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%43 = tail call i32 @sleep(i32 1)
%44 = tail call i32 @exit(i32 3)
unreachable
45: ; preds = %23, %27
%46 = load i32, i32* @EXIT_SUCCESS
ret i32 %46
}