#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 *, ...);
int /*<<< orphan*/ sleep(int);
scalar_t__ waitpid(int, int *, int /*<<< orphan*/);
int main() {
  pid_t pid;
  pid = fork();
  if (pid < 0) {
    printf("Create Child fail\n");
    return 1;
  }
  if (pid == 0) {
    printf("Child PID: %d ;Child runing...\n", getpid());
    sleep(5);
    exit(275);
  } else {
    int status = 0;
    pid_t ret = 0;
    ret = waitpid(-1, &status, 0);
    printf("Father PID : %d ,Father runing.....\n", getpid());
    if (WIFEXITED(status) && ret == pid) {
      printf("wait 5s child successed , chils return code is: %d \n",
             WEXITSTATUS(status));
    } else {
      printf("Wait Failed \n");
      return 1;
    }
    return 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11bf <main+0x3f>
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    128d <main+0x10d>
cmpq   $0x0,-0x10(%rbp)
jne    11f5 <main+0x75>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x5,%edi
call   1090 <sleep@plt>
mov    $0x113,%edi
call   1080 <exit@plt>
movl   $0x0,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    $0xffffffff,%edi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1070 <WIFEXITED@plt>
cmp    $0x0,%rax
je     126c <main+0xec>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    126c <main+0xec>
mov    -0x14(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1286 <main+0x106>
lea    0xe1a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    128d <main+0x10d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     120c <main+0x7c>
je     1230 <main+0xa0>
mov    %rax,%rbx
movl   $0x0,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    $0xffffffff,%edi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe4d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x4(%rsp),%edi
call   1080 <WIFEXITED@plt>
test   %rax,%rax
je     1215 <main+0x85>
cmp    %rbx,%r14
jne    1215 <main+0x85>
mov    0x4(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
lea    0xe46(%rip),%rdi
xor    %ebx,%ebx
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1226 <main+0x96>
lea    0xe74(%rip),%rdi
jmp    121c <main+0x8c>
lea    0xe5e(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdc2(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x5,%edi
call   10a0 <sleep@plt>
mov    $0x113,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"Create Child fail\0A\00"
@.str.1 = [32 x i8] c"Child PID: %d ;Child runing...\0A\00"
@.str.2 = [37 x i8] c"Father PID : %d ,Father runing.....\0A\00"
@.str.3 = [53 x i8] c"wait 5s child successed , chils return code is: %d \0A\00"
@.str.4 = [14 x i8] c"Wait Failed \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
%4 = alloca i64
store i32 0, i32* %1
%5 = call i64 (...) @fork()
store i64 %5, i64* %2
%6 = load i64, i64* %2
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %36
10: ; preds = %0
%11 = load i64, i64* %2
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %10
%14 = call i32 (...) @getpid()
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = call i32 @sleep(i32 5)
%17 = call i32 @exit(i32 275)
unreachable
18: ; preds = %10
store i32 0, i32* %3
store i64 0, i64* %4
%19 = call i64 @waitpid(i32 -1, i32* %3, i32 0)
store i64 %19, i64* %4
%20 = call i32 (...) @getpid()
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %20)
%22 = load i32, i32* %3
%23 = call i64 @WIFEXITED(i32 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %33
25: ; preds = %18
%26 = load i64, i64* %4
%27 = load i64, i64* %2
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = load i32, i32* %3
%31 = call i32 @WEXITSTATUS(i32 %30)
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0), i32 %31)
br label %35
33: ; preds = %25, %18
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %36
35: ; preds = %29
store i32 0, i32* %1
br label %36
36: ; preds = %35, %33, %8
%37 = load i32, i32* %1
ret i32 %37
}

LLVM IR (-O2):
@.str.1 = [32 x i8] c"Child PID: %d ;Child runing...\0A\00"
@.str.2 = [37 x i8] c"Father PID : %d ,Father runing.....\0A\00"
@.str.3 = [53 x i8] c"wait 5s child successed , chils return code is: %d \0A\00"
@str = [13 x i8] c"Wait Failed \00"
@str.5 = [18 x i8] c"Create Child fail\00"
define i32 @main() {
%1 = alloca i32
%2 = tail call i64 (...) @fork()
%3 = icmp slt i64 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.5, i64 0, i64 0))
br label %31
6: ; preds = %0
%7 = icmp eq i64 %2, 0
br i1 %7, label %8, label %13
8: ; preds = %6
%9 = tail call i32 (...) @getpid()
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = tail call i32 @sleep(i32 5)
%12 = tail call i32 @exit(i32 275)
unreachable
13: ; preds = %6
%14 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%15 = call i64 @waitpid(i32 -1, i32* %1, i32 0)
%16 = call i32 (...) @getpid()
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = load i32, i32* %1
%19 = call i64 @WIFEXITED(i32 %18)
%20 = icmp ne i64 %19, 0
%21 = icmp eq i64 %15, %2
%22 = select i1 %20, i1 %21, i1 false
br i1 %22, label %23, label %27
23: ; preds = %13
%24 = load i32, i32* %1
%25 = call i32 @WEXITSTATUS(i32 %24)
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0), i32 %25)
br label %29
27: ; preds = %13
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %29
29: ; preds = %23, %27
%30 = phi i32 [ 0, %23 ], [ 1, %27 ]
br label %31
31: ; preds = %29, %4
%32 = phi i32 [ 1, %4 ], [ %30, %29 ]
ret i32 %32
}