#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 *);
main() {
  int status;
  pid_t pc, pr;
  pc = fork();
  if (pc < 0)
    printf("error ocurred!\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);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11a8 <main+0x38>
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    123e <main+0xce>
cmpq   $0x0,-0x10(%rbp)
jne    11d4 <main+0x64>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x3,%edi
call   1070 <exit@plt>
lea    -0x8(%rbp),%rdi
call   1080 <wait@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1060 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1222 <main+0xb2>
mov    -0x18(%rbp),%rsi
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1234 <main+0xc4>
mov    -0x18(%rbp),%rsi
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1239 <main+0xc9>
jmp    123e <main+0xce>
mov    -0x4(%rbp),%eax
add    $0x20,%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     1201 <main+0x81>
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    0xe72(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0xc(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
lea    0xe7d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11f9 <main+0x79>
lea    0xeaa(%rip),%rdi
call   1030 <puts@plt>
jmp    11f9 <main+0x79>
lea    0xe75(%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    0xdf1(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x3,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"error ocurred!\0A\00"
@.str.1 = [39 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"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i64
store i32 0, i32* %1
%5 = call i64 (...) @fork()
store i64 %5, i64* %3
%6 = load i64, i64* %3
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %33
10: ; preds = %0
%11 = load i64, i64* %3
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = call i32 (...) @getpid()
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = call i32 @exit(i32 3)
unreachable
17: ; preds = %10
%18 = call i64 @wait(i32* %2)
store i64 %18, i64* %4
%19 = load i32, i32* %2
%20 = call i64 @WIFEXITED(i32 %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %17
%23 = load i64, i64* %4
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i64 %23)
%25 = load i32, i32* %2
%26 = call i32 @WEXITSTATUS(i32 %25)
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %26)
br label %31
28: ; preds = %17
%29 = load i64, i64* %4
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i64 %29)
br label %31
31: ; preds = %28, %22
br label %32
32: ; preds = %31
br label %33
33: ; preds = %32, %8
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
@.str.1 = [39 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 = [15 x i8] c"error ocurred!\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %25
7: ; preds = %0
%8 = icmp eq i64 %3, 0
br i1 %8, label %9, label %13
9: ; preds = %7
%10 = tail call i32 (...) @getpid()
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = tail call i32 @exit(i32 3)
unreachable
13: ; preds = %7
%14 = call i64 @wait(i32* %1)
%15 = load i32, i32* %1
%16 = call i64 @WIFEXITED(i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %23, label %18
18: ; preds = %13
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i64 %14)
%20 = load i32, i32* %1
%21 = call i32 @WEXITSTATUS(i32 %20)
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %21)
br label %25
23: ; preds = %13
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i64 %14)
br label %25
25: ; preds = %23, %18, %5
ret i32 0
}