#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 WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ wait(int *);
int main(int argc, char const *argv[]) {
  int status;
  pid_t pc, pr;
  pc = fork();
  if (pc < 0)
    printf("error occurred!\n");
  else if (pc == 0) {
    printf("This is child process with pid of %d\n", getpid());
    exit(3);
  } else {
    pr = wait(&status);
    if (WIFEXITED(status)) {
      printf("the child process %d exit normally.\n", pr);
      printf("the return code is %d.\n", WEXITSTATUS(status));
    } else {
      printf("the child process %d exit abnormally.\n", pr);
    }
    printf("I catched a child process with pid of %d\n", pr);
  }
  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   1090 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11af <main+0x3f>
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1252 <main+0xe2>
cmpq   $0x0,-0x20(%rbp)
jne    11db <main+0x6b>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x3,%edi
call   1070 <exit@plt>
lea    -0x14(%rbp),%rdi
call   1080 <wait@plt>
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1229 <main+0xb9>
mov    -0x28(%rbp),%rsi
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    123b <main+0xcb>
mov    -0x28(%rbp),%rsi
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rsi
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1252 <main+0xe2>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     11da <main+0x5a>
je     1212 <main+0x92>
lea    0xc(%rsp),%rdi
call   1090 <wait@plt>
mov    %rax,%rbx
mov    0xc(%rsp),%edi
call   1070 <WIFEXITED@plt>
test   %rax,%rax
je     11e8 <main+0x68>
lea    0xe71(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0xc(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
lea    0xe7c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11f9 <main+0x79>
lea    0xed3(%rip),%rdi
call   1030 <puts@plt>
jmp    120a <main+0x8a>
lea    0xe74(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe8a(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xde0(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x3,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"error occurred!\0A\00"
@.str.1 = [38 x i8] c"This is child process with pid of %d\0A\00"
@.str.2 = [37 x i8] c"the child process %d exit normally.\0A\00"
@.str.3 = [24 x i8] c"the return code is %d.\0A\00"
@.str.4 = [39 x i8] c"the child process %d exit abnormally.\0A\00"
@.str.5 = [42 x i8] c"I catched a child process with pid of %d\0A\00"
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 i64 (...) @fork()
store i64 %9, i64* %7
%10 = load i64, i64* %7
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %39
14: ; preds = %2
%15 = load i64, i64* %7
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = call i32 (...) @getpid()
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = call i32 @exit(i32 3)
unreachable
21: ; preds = %14
%22 = call i64 @wait(i32* %6)
store i64 %22, i64* %8
%23 = load i32, i32* %6
%24 = call i64 @WIFEXITED(i32 %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %21
%27 = load i64, i64* %8
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i64 %27)
%29 = load i32, i32* %6
%30 = call i32 @WEXITSTATUS(i32 %29)
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %30)
br label %35
32: ; preds = %21
%33 = load i64, i64* %8
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i64 %33)
br label %35
35: ; preds = %32, %26
%36 = load i64, i64* %8
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0), i64 %36)
br label %38
38: ; preds = %35
br label %39
39: ; preds = %38, %12
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [38 x i8] c"This is child process with pid of %d\0A\00"
@.str.2 = [37 x i8] c"the child process %d exit normally.\0A\00"
@.str.3 = [24 x i8] c"the return code is %d.\0A\00"
@.str.4 = [39 x i8] c"the child process %d exit abnormally.\0A\00"
@.str.5 = [42 x i8] c"I catched a child process with pid of %d\0A\00"
@str = [16 x i8] c"error occurred!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i64 (...) @fork()
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %29
9: ; preds = %2
%10 = icmp eq i64 %5, 0
br i1 %10, label %11, label %15
11: ; preds = %9
%12 = tail call i32 (...) @getpid()
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = tail call i32 @exit(i32 3)
unreachable
15: ; preds = %9
%16 = call i64 @wait(i32* %3)
%17 = load i32, i32* %3
%18 = call i64 @WIFEXITED(i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %25, label %20
20: ; preds = %15
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i64 %16)
%22 = load i32, i32* %3
%23 = call i32 @WEXITSTATUS(i32 %22)
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %23)
br label %27
25: ; preds = %15
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i64 %16)
br label %27
27: ; preds = %25, %20
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0), i64 %16)
br label %29
29: ; preds = %27, %7
ret i32 0
}