#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 /*<<< orphan*/ WNOHANG;
int /*<<< orphan*/ add_instance(int);
int /*<<< orphan*/ close_instance(int);
int find_instance(scalar_t__);
int /*<<< orphan*/ printf(char *, scalar_t__);
scalar_t__ waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
void sig_child(int sig) {
  pid_t pid = 0;
  do {
    pid = waitpid(-1, NULL, WNOHANG);
    if (pid == 0)
      break;
    printf("%d崩溃...\n", pid);
    int index = find_instance(pid);
    if (index == -1)
      continue;
    close_instance(index);
    add_instance(index);
  } while (1);
}


x86 assembly (clang, -O0):
<sig_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <waitpid@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    118e <sig_child+0x3e>
jmp    11d8 <sig_child+0x88>
mov    -0x10(%rbp),%rsi
lea    0xe67(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1060 <find_instance@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11b9 <sig_child+0x69>
jmp    11c9 <sig_child+0x79>
mov    -0x14(%rbp),%edi
call   1070 <close_instance@plt>
mov    -0x14(%rbp),%edi
call   1030 <add_instance@plt>
mov    $0x1,%al
test   $0x1,%al
jne    1163 <sig_child+0x13>
jmp    11d8 <sig_child+0x88>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sig_child>:
push   %r15
push   %r14
push   %rbx
mov    0x2e8c(%rip),%r15
mov    (%r15),%edx
mov    $0xffffffff,%edi
xor    %esi,%esi
call   1050 <waitpid@plt>
test   %rax,%rax
je     11c1 <sig_child+0x71>
mov    %rax,%rbx
lea    0xe86(%rip),%r14
jmp    1197 <sig_child+0x47>
nopl   0x0(%rax)
mov    (%r15),%edx
mov    $0xffffffff,%edi
xor    %esi,%esi
call   1050 <waitpid@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11c1 <sig_child+0x71>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1060 <find_instance@plt>
cmp    $0xffffffff,%eax
je     1180 <sig_child+0x30>
mov    %eax,%ebx
mov    %eax,%edi
call   1070 <close_instance@plt>
mov    %ebx,%edi
call   1030 <add_instance@plt>
jmp    1180 <sig_child+0x30>
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@WNOHANG = global i32 0
@.str = [13 x i8] c"%d\E5\B4\A9\E6\BA\83...\0A\00"
define void @sig_child(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32
store i32 %0, i32* %2
store i64 0, i64* %3
br label %5
5: ; preds = %24, %1
%6 = load i32, i32* @WNOHANG
%7 = call i64 @waitpid(i32 -1, i32* null, i32 %6)
store i64 %7, i64* %3
%8 = load i64, i64* %3
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %5
br label %25
11: ; preds = %5
%12 = load i64, i64* %3
%13 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i64 %12)
%14 = load i64, i64* %3
%15 = call i32 @find_instance(i64 %14)
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %11
br label %24
19: ; preds = %11
%20 = load i32, i32* %4
%21 = call i32 @close_instance(i32 %20)
%22 = load i32, i32* %4
%23 = call i32 @add_instance(i32 %22)
br label %24
24: ; preds = %19, %18
br i1 true, label %5, label %25
25: ; preds = %24, %10
ret void
}

LLVM IR (-O2):
@WNOHANG = global i32 0
@.str = [13 x i8] c"%d\E5\B4\A9\E6\BA\83...\0A\00"
define void @sig_child(i32 %0) {
%2 = load i32, i32* @WNOHANG
%3 = tail call i64 @waitpid(i32 -1, i32* null, i32 %2)
%4 = icmp eq i64 %3, 0
br i1 %4, label %17, label %5
5: ; preds = %1, %13
%6 = phi i64 [ %15, %13 ], [ %3, %1 ]
%7 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i64 %6)
%8 = tail call i32 @find_instance(i64 %6)
%9 = icmp eq i32 %8, -1
br i1 %9, label %13, label %10
10: ; preds = %5
%11 = tail call i32 @close_instance(i32 %8)
%12 = tail call i32 @add_instance(i32 %8)
br label %13
13: ; preds = %5, %10
%14 = load i32, i32* @WNOHANG
%15 = tail call i64 @waitpid(i32 -1, i32* null, i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %5
17: ; preds = %13, %1
ret void
}