#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);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ wait(int *);
int main(int argc, char *argv[]) {
  pid_t pid;
  pid = fork();
  if (pid < 0) {
    perror("fork");
    exit(0);
  }
  if (pid == 0) {
    int i = 0;
    for (i = 0; i < 5; i++) {
      printf("this is son process\n");
      sleep(1);
    }
    _exit(2);
  } else if (pid > 0) {
    int status = 0;
    wait(&status);
    if (WIFEXITED(status) != 0) {
      printf("son process return %d\n", WEXITSTATUS(status));
    }
    printf("this is father process\n");
  }
  return 0;
}


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   10b0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11cf <main+0x3f>
lea    0xe3d(%rip),%rdi
call   1060 <perror@plt>
xor    %edi,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1222 <main+0x92>
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x5,-0x1c(%rbp)
jge    1218 <main+0x88>
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11e8 <main+0x58>
mov    $0x2,%edi
call   1030 <_exit@plt>
cmpq   $0x0,-0x18(%rbp)
jle    1275 <main+0xe5>
movl   $0x0,-0x20(%rbp)
lea    -0x20(%rbp),%rdi
call   10a0 <wait@plt>
mov    -0x20(%rbp),%edi
call   1070 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1267 <main+0xd7>
mov    -0x20(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdc3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    127a <main+0xea>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     1200 <main+0x60>
je     1213 <main+0x73>
movl   $0x0,0xc(%rsp)
lea    0xc(%rsp),%rdi
call   10b0 <wait@plt>
mov    0xc(%rsp),%edi
call   1080 <WIFEXITED@plt>
test   %rax,%rax
je     11ec <main+0x4c>
mov    0xc(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
lea    0xe22(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe29(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xdf9(%rip),%rdi
call   1070 <perror@plt>
xor    %edi,%edi
call   1090 <exit@plt>
lea    0xe19(%rip),%rbx
mov    %rbx,%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    $0x2,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@.str.1 = [21 x i8] c"this is son process\0A\00"
@.str.2 = [23 x i8] c"son process return %d\0A\00"
@.str.3 = [24 x i8] c"this is father process\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i64 (...) @fork()
store i64 %9, i64* %6
%10 = load i64, i64* %6
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 0)
unreachable
15: ; preds = %2
%16 = load i64, i64* %6
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %30
18: ; preds = %15
store i32 0, i32* %7
store i32 0, i32* %7
br label %19
19: ; preds = %25, %18
%20 = load i32, i32* %7
%21 = icmp slt i32 %20, 5
br i1 %21, label %22, label %28
22: ; preds = %19
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @sleep(i32 1)
br label %25
25: ; preds = %22
%26 = load i32, i32* %7
%27 = add nsw i32 %26, 1
store i32 %27, i32* %7
br label %19
28: ; preds = %19
%29 = call i32 @_exit(i32 2)
unreachable
30: ; preds = %15
%31 = load i64, i64* %6
%32 = icmp sgt i64 %31, 0
br i1 %32, label %33, label %44
33: ; preds = %30
store i32 0, i32* %8
%34 = call i32 @wait(i32* %8)
%35 = load i32, i32* %8
%36 = call i64 @WIFEXITED(i32 %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %33
%39 = load i32, i32* %8
%40 = call i32 @WEXITSTATUS(i32 %39)
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %40)
br label %42
42: ; preds = %38, %33
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %44
44: ; preds = %42, %30
br label %45
45: ; preds = %44
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@.str.2 = [23 x i8] c"son process return %d\0A\00"
@str = [23 x i8] c"this is father process\00"
@str.4 = [20 x i8] c"this is son process\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = tail call i64 (...) @fork()
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 0)
unreachable
9: ; preds = %2
%10 = icmp eq i64 %4, 0
br i1 %10, label %11, label %23
11: ; preds = %9
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.4, i64 0, i64 0))
%13 = tail call i32 @sleep(i32 1)
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.4, i64 0, i64 0))
%15 = tail call i32 @sleep(i32 1)
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.4, i64 0, i64 0))
%17 = tail call i32 @sleep(i32 1)
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.4, i64 0, i64 0))
%19 = tail call i32 @sleep(i32 1)
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.4, i64 0, i64 0))
%21 = tail call i32 @sleep(i32 1)
%22 = tail call i32 @_exit(i32 2)
unreachable
23: ; preds = %9
%24 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%25 = call i32 @wait(i32* %3)
%26 = load i32, i32* %3
%27 = call i64 @WIFEXITED(i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %23
%30 = load i32, i32* %3
%31 = call i32 @WEXITSTATUS(i32 %30)
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %31)
br label %33
33: ; preds = %29, %23
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
ret i32 0
}