#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 /*<<< orphan*/ SIGKILL;
int /*<<< orphan*/ WNOHANG;
int fork();
int getpid();
int kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
scalar_t__ waitpid(int, int *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  pid_t child_pid;
  int status;
  int retval;
  child_pid = fork();
  if (-1 == child_pid) {
    perror("fork()");
    printf("创建子进程失败！\n");
  } else if (0 == child_pid) {
    printf("In child process.\n");
    printf("子进程号 = %d, 子进程睡眠100秒\n", getpid());
    sleep(100);
  } else {
    printf("父进程号 = %d.\n父进程等待子进程结束，"
           "若未结束则立即返回.\n",
           getpid());
    if (0 == (waitpid(child_pid, &status, WNOHANG))) {
      printf("杀死子进程前当前的活跃进程\n");
      retval = kill(child_pid, SIGKILL);
      printf("父进程杀死子进程\n");
      if (retval != 0) {
        printf("kill failed\n");
        perror("kill()");
        waitpid(child_pid, &status, 0);
      } else {
        printf("%d killed\n", child_pid);
      }
    }
  }
  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   1090 <fork@plt>
mov    %eax,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    11bd <main+0x4d>
lea    0xe5b(%rip),%rdi
call   1070 <perror@plt>
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12bd <main+0x14d>
xor    %eax,%eax
cmp    -0x14(%rbp),%eax
jne    11fc <main+0x8c>
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x64,%edi
call   1080 <sleep@plt>
jmp    12b8 <main+0x148>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2dcb(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rsi
call   1060 <waitpid@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    12b3 <main+0x143>
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
call   1050 <kill@plt>
mov    %eax,-0x1c(%rbp)
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x1c(%rbp)
je     129d <main+0x12d>
lea    0xe7c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe7b(%rip),%rdi
call   1070 <perror@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
jmp    12ae <main+0x13e>
mov    -0x14(%rbp),%esi
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12b3 <main+0x143>
jmp    12b8 <main+0x148>
jmp    12bd <main+0x14d>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     120a <main+0x8a>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1240 <main+0xc0>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe8a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
call   1070 <waitpid@plt>
test   %rax,%rax
jne    1237 <main+0xb7>
lea    0xec4(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1060 <kill@plt>
mov    %eax,%ebp
lea    0xece(%rip),%rdi
call   1030 <puts@plt>
test   %ebp,%ebp
jne    125a <main+0xda>
lea    0xe8b(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1237 <main+0xb7>
lea    0xed1(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xde3(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x64,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdb9(%rip),%rdi
call   1080 <perror@plt>
lea    0xea1(%rip),%rdi
call   1030 <puts@plt>
jmp    1237 <main+0xb7>
lea    0xe75(%rip),%rdi
call   1030 <puts@plt>
lea    0xe16(%rip),%rdi
call   1080 <perror@plt>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1070 <waitpid@plt>
jmp    1237 <main+0xb7>

LLVM IR (-O0):
@.str = [7 x i8] c"fork()\00"
@.str.1 = [26 x i8] c"\E5\88\9B\E5\BB\BA\E5\AD\90\E8\BF\9B\E7\A8\8B\E5\A4\B1\E8\B4\A5\EF\BC\81\0A\00"
@.str.2 = [19 x i8] c"In child process.\0A\00"
@.str.3 = [42 x i8] c"\E5\AD\90\E8\BF\9B\E7\A8\8B\E5\8F\B7 = %d, \E5\AD\90\E8\BF\9B\E7\A8\8B\E7\9D\A1\E7\9C\A0100\E7\A7\92\0A\00"
@.str.4 = [82 x i8] c"\E7\88\B6\E8\BF\9B\E7\A8\8B\E5\8F\B7 = %d.\0A\E7\88\B6\E8\BF\9B\E7\A8\8B\E7\AD\89\E5\BE\85\E5\AD\90\E8\BF\9B\E7\A8\8B\E7\BB\93\E6\9D\9F\EF\BC\8C\E8\8B\A5\E6\9C\AA\E7\BB\93\E6\9D\9F\E5\88\99\E7\AB\8B\E5\8D\B3\E8\BF\94\E5\9B\9E.\0A\00"
@WNOHANG = global i32 0
@.str.5 = [41 x i8] c"\E6\9D\80\E6\AD\BB\E5\AD\90\E8\BF\9B\E7\A8\8B\E5\89\8D\E5\BD\93\E5\89\8D\E7\9A\84\E6\B4\BB\E8\B7\83\E8\BF\9B\E7\A8\8B\0A\00"
@SIGKILL = global i32 0
@.str.6 = [26 x i8] c"\E7\88\B6\E8\BF\9B\E7\A8\8B\E6\9D\80\E6\AD\BB\E5\AD\90\E8\BF\9B\E7\A8\8B\0A\00"
@.str.7 = [13 x i8] c"kill failed\0A\00"
@.str.8 = [7 x i8] c"kill()\00"
@.str.9 = [11 x i8] c"%d killed\0A\00"
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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 (...) @fork()
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp eq i32 -1, %10
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %49
15: ; preds = %2
%16 = load i32, i32* %6
%17 = icmp eq i32 0, %16
br i1 %17, label %18, label %23
18: ; preds = %15
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%20 = call i32 (...) @getpid()
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i32 %20)
%22 = call i32 @sleep(i32 100)
br label %48
23: ; preds = %15
%24 = call i32 (...) @getpid()
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([82 x i8], [82 x i8]* @.str.4, i64 0, i64 0), i32 %24)
%26 = load i32, i32* %6
%27 = load i32, i32* @WNOHANG
%28 = call i64 @waitpid(i32 %26, i32* %7, i32 %27)
%29 = icmp eq i64 0, %28
br i1 %29, label %30, label %47
30: ; preds = %23
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0))
%32 = load i32, i32* %6
%33 = load i32, i32* @SIGKILL
%34 = call i32 @kill(i32 %32, i32 %33)
store i32 %34, i32* %8
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
%36 = load i32, i32* %8
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %30
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%40 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
%41 = load i32, i32* %6
%42 = call i64 @waitpid(i32 %41, i32* %7, i32 0)
br label %46
43: ; preds = %30
%44 = load i32, i32* %6
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.9, i64 0, i64 0), i32 %44)
br label %46
46: ; preds = %43, %38
br label %47
47: ; preds = %46, %23
br label %48
48: ; preds = %47, %18
br label %49
49: ; preds = %48, %12
ret i32 0
}

LLVM IR (-O2):
@.str = [7 x i8] c"fork()\00"
@.str.3 = [42 x i8] c"\E5\AD\90\E8\BF\9B\E7\A8\8B\E5\8F\B7 = %d, \E5\AD\90\E8\BF\9B\E7\A8\8B\E7\9D\A1\E7\9C\A0100\E7\A7\92\0A\00"
@.str.4 = [82 x i8] c"\E7\88\B6\E8\BF\9B\E7\A8\8B\E5\8F\B7 = %d.\0A\E7\88\B6\E8\BF\9B\E7\A8\8B\E7\AD\89\E5\BE\85\E5\AD\90\E8\BF\9B\E7\A8\8B\E7\BB\93\E6\9D\9F\EF\BC\8C\E8\8B\A5\E6\9C\AA\E7\BB\93\E6\9D\9F\E5\88\99\E7\AB\8B\E5\8D\B3\E8\BF\94\E5\9B\9E.\0A\00"
@WNOHANG = global i32 0
@SIGKILL = global i32 0
@.str.8 = [7 x i8] c"kill()\00"
@.str.9 = [11 x i8] c"%d killed\0A\00"
@str = [40 x i8] c"\E6\9D\80\E6\AD\BB\E5\AD\90\E8\BF\9B\E7\A8\8B\E5\89\8D\E5\BD\93\E5\89\8D\E7\9A\84\E6\B4\BB\E8\B7\83\E8\BF\9B\E7\A8\8B\00"
@str.10 = [25 x i8] c"\E7\88\B6\E8\BF\9B\E7\A8\8B\E6\9D\80\E6\AD\BB\E5\AD\90\E8\BF\9B\E7\A8\8B\00"
@str.11 = [12 x i8] c"kill failed\00"
@str.12 = [18 x i8] c"In child process.\00"
@str.13 = [25 x i8] c"\E5\88\9B\E5\BB\BA\E5\AD\90\E8\BF\9B\E7\A8\8B\E5\A4\B1\E8\B4\A5\EF\BC\81\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 (...) @fork()
switch i32 %5, label %14 [
i32 -1, label %6
i32 0, label %9
]
6: ; preds = %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.13, i64 0, i64 0))
br label %32
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.12, i64 0, i64 0))
%11 = tail call i32 (...) @getpid()
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i32 %11)
%13 = tail call i32 @sleep(i32 100)
br label %32
14: ; preds = %2
%15 = tail call i32 (...) @getpid()
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([82 x i8], [82 x i8]* @.str.4, i64 0, i64 0), i32 %15)
%17 = load i32, i32* @WNOHANG
%18 = call i64 @waitpid(i32 %5, i32* %3, i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %32
20: ; preds = %14
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str, i64 0, i64 0))
%22 = load i32, i32* @SIGKILL
%23 = call i32 @kill(i32 %5, i32 %22)
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.10, i64 0, i64 0))
%25 = icmp eq i32 %23, 0
br i1 %25, label %30, label %26
26: ; preds = %20
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.11, i64 0, i64 0))
%28 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
%29 = call i64 @waitpid(i32 %5, i32* %3, i32 0)
br label %32
30: ; preds = %20
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.9, i64 0, i64 0), i32 %5)
br label %32
32: ; preds = %9, %26, %30, %14, %6
ret i32 0
}