#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 SIGTERM;
int /*<<< orphan*/ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ alarm(int);
int /*<<< orphan*/ do_child();
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ errx(int, char *, scalar_t__, int);
scalar_t__ fork();
scalar_t__ kill(int, int);
int /*<<< orphan*/ puts(char *);
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main(void) {
  pid_t children[3] = {0};
  for (size_t i = 0; i < 3; i++) {
    children[i] = fork();
    if (children[i] < 0) {
      err(1, "fork");
    } else if (children[i] == 0) {
      alarm(60 * 3);
      do_child();
    }
  }
  if (kill(-1, SIGTERM) < 0) {
    err(1, "kill");
  }
  for (size_t i = 0; i < 3; i++) {
    int status = 0;
    if (waitpid(children[i], &status, 0) < 0) {
      err(1, "waitpid");
    }
    if (!WIFSIGNALED(status)) {
      errx(1, "child %d not killed (%d)", children[i], status);
    }
    if (WTERMSIG(status) != SIGTERM) {
      errx(1, "child %d killed by a wrong signal (%d)", children[i],
           WTERMSIG(status));
    }
  }
  puts("TEST OK");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
movq   $0x0,-0x28(%rbp)
cmpq   $0x3,-0x28(%rbp)
jae    1257 <main+0xa7>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,-0x20(%rbp,%rax,8)
mov    -0x28(%rbp),%rax
cmpq   $0x0,-0x20(%rbp,%rax,8)
jge    121b <main+0x6b>
mov    $0x1,%edi
lea    0xdef(%rip),%rsi
call   10a0 <err@plt>
jmp    1241 <main+0x91>
mov    -0x28(%rbp),%rax
cmpq   $0x0,-0x20(%rbp,%rax,8)
jne    123c <main+0x8c>
mov    $0xb4,%edi
call   1070 <alarm@plt>
mov    $0x0,%al
call   1030 <do_child@plt>
jmp    1241 <main+0x91>
jmp    1246 <main+0x96>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11d7 <main+0x27>
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
mov    $0xffffffff,%edi
call   1090 <kill@plt>
cmp    $0x0,%rax
jge    1285 <main+0xd5>
mov    $0x1,%edi
lea    0xd85(%rip),%rsi
call   10a0 <err@plt>
movq   $0x0,-0x30(%rbp)
cmpq   $0x3,-0x30(%rbp)
jae    1355 <main+0x1a5>
movl   $0x0,-0x34(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp,%rax,8),%rdi
lea    -0x34(%rbp),%rsi
xor    %edx,%edx
call   10c0 <waitpid@plt>
cmp    $0x0,%rax
jge    12ce <main+0x11e>
mov    $0x1,%edi
lea    0xd41(%rip),%rsi
call   10a0 <err@plt>
mov    -0x34(%rbp),%edi
call   1080 <WIFSIGNALED@plt>
cmp    $0x0,%eax
jne    12fc <main+0x14c>
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp,%rax,8),%rdx
mov    -0x34(%rbp),%ecx
mov    $0x1,%edi
lea    0xd1b(%rip),%rsi
call   1050 <errx@plt>
mov    -0x34(%rbp),%edi
call   10b0 <WTERMSIG@plt>
mov    0x2cdd(%rip),%rcx
cmp    (%rcx),%eax
je     133f <main+0x18f>
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp,%rax,8),%rax
mov    %rax,-0x40(%rbp)
mov    -0x34(%rbp),%edi
call   10b0 <WTERMSIG@plt>
mov    -0x40(%rbp),%rdx
mov    %eax,%ecx
mov    $0x1,%edi
lea    0xcf1(%rip),%rsi
call   1050 <errx@plt>
jmp    1344 <main+0x194>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    128d <main+0xdd>
lea    0xcf6(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
js     11cc <main+0x2c>
jne    11dd <main+0x3d>
mov    $0xb4,%edi
call   1060 <alarm@plt>
xor    %eax,%eax
call   1030 <do_child@plt>
jmp    11dd <main+0x3d>
lea    0xe2d(%rip),%rsi
mov    $0x1,%edi
call   1090 <err@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %rax,%r15
test   %rax,%rax
js     1201 <main+0x61>
jne    1212 <main+0x72>
mov    $0xb4,%edi
call   1060 <alarm@plt>
xor    %eax,%eax
call   1030 <do_child@plt>
jmp    1212 <main+0x72>
lea    0xdf8(%rip),%rsi
mov    $0x1,%edi
call   1090 <err@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %rax,%r14
test   %rax,%rax
js     1236 <main+0x96>
jne    1247 <main+0xa7>
mov    $0xb4,%edi
call   1060 <alarm@plt>
xor    %eax,%eax
call   1030 <do_child@plt>
jmp    1247 <main+0xa7>
lea    0xdc3(%rip),%rsi
mov    $0x1,%edi
call   1090 <err@plt>
mov    0x2d9a(%rip),%r12
mov    (%r12),%esi
mov    $0xffffffff,%edi
call   1080 <kill@plt>
test   %rax,%rax
jns    1272 <main+0xd2>
lea    0xd9d(%rip),%rsi
mov    $0x1,%edi
call   1090 <err@plt>
movl   $0x0,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   10b0 <waitpid@plt>
test   %rax,%rax
jns    129f <main+0xff>
lea    0xd75(%rip),%rsi
mov    $0x1,%edi
call   1090 <err@plt>
mov    0x4(%rsp),%edi
call   1070 <WIFSIGNALED@plt>
test   %eax,%eax
jne    12c4 <main+0x124>
mov    0x4(%rsp),%ecx
lea    0xd5b(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
call   1050 <errx@plt>
mov    0x4(%rsp),%edi
call   10a0 <WTERMSIG@plt>
cmp    (%r12),%eax
je     12f2 <main+0x152>
mov    0x4(%rsp),%edi
call   10a0 <WTERMSIG@plt>
lea    0xd48(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
mov    %eax,%ecx
call   1050 <errx@plt>
movl   $0x0,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    %r15,%rdi
xor    %edx,%edx
call   10b0 <waitpid@plt>
test   %rax,%rax
jns    131f <main+0x17f>
lea    0xcf5(%rip),%rsi
mov    $0x1,%edi
call   1090 <err@plt>
mov    0x4(%rsp),%edi
call   1070 <WIFSIGNALED@plt>
test   %eax,%eax
jne    1344 <main+0x1a4>
mov    0x4(%rsp),%ecx
lea    0xcdb(%rip),%rsi
mov    $0x1,%edi
mov    %r15,%rdx
call   1050 <errx@plt>
mov    0x4(%rsp),%edi
call   10a0 <WTERMSIG@plt>
cmp    (%r12),%eax
je     1372 <main+0x1d2>
mov    0x4(%rsp),%edi
call   10a0 <WTERMSIG@plt>
lea    0xcc8(%rip),%rsi
mov    $0x1,%edi
mov    %r15,%rdx
mov    %eax,%ecx
call   1050 <errx@plt>
movl   $0x0,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    %r14,%rdi
xor    %edx,%edx
call   10b0 <waitpid@plt>
test   %rax,%rax
jns    139f <main+0x1ff>
lea    0xc75(%rip),%rsi
mov    $0x1,%edi
call   1090 <err@plt>
mov    0x4(%rsp),%edi
call   1070 <WIFSIGNALED@plt>
test   %eax,%eax
jne    13c4 <main+0x224>
mov    0x4(%rsp),%ecx
lea    0xc5b(%rip),%rsi
mov    $0x1,%edi
mov    %r14,%rdx
call   1050 <errx@plt>
mov    0x4(%rsp),%edi
call   10a0 <WTERMSIG@plt>
cmp    (%r12),%eax
je     13f2 <main+0x252>
mov    0x4(%rsp),%edi
call   10a0 <WTERMSIG@plt>
lea    0xc48(%rip),%rsi
mov    $0x1,%edi
mov    %r14,%rdx
mov    %eax,%ecx
call   1050 <errx@plt>
lea    0xc59(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@SIGTERM = global i32 0
@.str.1 = [5 x i8] c"kill\00"
@.str.2 = [8 x i8] c"waitpid\00"
@.str.3 = [25 x i8] c"child %d not killed (%d)\00"
@.str.4 = [39 x i8] c"child %d killed by a wrong signal (%d)\00"
@.str.5 = [8 x i8] c"TEST OK\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [3 x i64]
%3 = alloca i64
%4 = alloca i64
%5 = alloca i32
store i32 0, i32* %1
%6 = bitcast [3 x i64]* %2 to i8*
store i64 0, i64* %3
br label %7
7: ; preds = %30, %0
%8 = load i64, i64* %3
%9 = icmp ult i64 %8, 3
br i1 %9, label %10, label %33
10: ; preds = %7
%11 = call i64 (...) @fork()
%12 = load i64, i64* %3
%13 = getelementptr inbounds [3 x i64], [3 x i64]* %2, i64 0, i64 %12
store i64 %11, i64* %13
%14 = load i64, i64* %3
%15 = getelementptr inbounds [3 x i64], [3 x i64]* %2, i64 0, i64 %14
%16 = load i64, i64* %15
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %10
%19 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %29
20: ; preds = %10
%21 = load i64, i64* %3
%22 = getelementptr inbounds [3 x i64], [3 x i64]* %2, i64 0, i64 %21
%23 = load i64, i64* %22
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = call i32 @alarm(i32 180)
%27 = call i32 (...) @do_child()
br label %28
28: ; preds = %25, %20
br label %29
29: ; preds = %28, %18
br label %30
30: ; preds = %29
%31 = load i64, i64* %3
%32 = add i64 %31, 1
store i64 %32, i64* %3
br label %7
33: ; preds = %7
%34 = load i32, i32* @SIGTERM
%35 = call i64 @kill(i32 -1, i32 %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %39
39: ; preds = %37, %33
store i64 0, i64* %4
br label %40
40: ; preds = %74, %39
%41 = load i64, i64* %4
%42 = icmp ult i64 %41, 3
br i1 %42, label %43, label %77
43: ; preds = %40
store i32 0, i32* %5
%44 = load i64, i64* %4
%45 = getelementptr inbounds [3 x i64], [3 x i64]* %2, i64 0, i64 %44
%46 = load i64, i64* %45
%47 = call i64 @waitpid(i64 %46, i32* %5, i32 0)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %43
%50 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %51
51: ; preds = %49, %43
%52 = load i32, i32* %5
%53 = call i32 @WIFSIGNALED(i32 %52)
%54 = icmp ne i32 %53, 0
br i1 %54, label %61, label %55
55: ; preds = %51
%56 = load i64, i64* %4
%57 = getelementptr inbounds [3 x i64], [3 x i64]* %2, i64 0, i64 %56
%58 = load i64, i64* %57
%59 = load i32, i32* %5
%60 = call i32 @errx(i32 1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i64 %58, i32 %59)
br label %61
61: ; preds = %55, %51
%62 = load i32, i32* %5
%63 = call i32 @WTERMSIG(i32 %62)
%64 = load i32, i32* @SIGTERM
%65 = icmp ne i32 %63, %64
br i1 %65, label %66, label %73
66: ; preds = %61
%67 = load i64, i64* %4
%68 = getelementptr inbounds [3 x i64], [3 x i64]* %2, i64 0, i64 %67
%69 = load i64, i64* %68
%70 = load i32, i32* %5
%71 = call i32 @WTERMSIG(i32 %70)
%72 = call i32 @errx(i32 1, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i64 %69, i32 %71)
br label %73
73: ; preds = %66, %61
br label %74
74: ; preds = %73
%75 = load i64, i64* %4
%76 = add i64 %75, 1
store i64 %76, i64* %4
br label %40
77: ; preds = %40
%78 = call i32 @puts(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@SIGTERM = global i32 0
@.str.1 = [5 x i8] c"kill\00"
@.str.2 = [8 x i8] c"waitpid\00"
@.str.3 = [25 x i8] c"child %d not killed (%d)\00"
@.str.4 = [39 x i8] c"child %d killed by a wrong signal (%d)\00"
@.str.5 = [8 x i8] c"TEST OK\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 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %11
6: ; preds = %0
%7 = icmp eq i64 %2, 0
br i1 %7, label %8, label %11
8: ; preds = %6
%9 = tail call i32 @alarm(i32 180)
%10 = tail call i32 (...) @do_child()
br label %11
11: ; preds = %4, %8, %6
%12 = tail call i64 (...) @fork()
%13 = icmp slt i64 %12, 0
br i1 %13, label %19, label %14
14: ; preds = %11
%15 = icmp eq i64 %12, 0
br i1 %15, label %16, label %21
16: ; preds = %14
%17 = tail call i32 @alarm(i32 180)
%18 = tail call i32 (...) @do_child()
br label %21
19: ; preds = %11
%20 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %16, %14
%22 = tail call i64 (...) @fork()
%23 = icmp slt i64 %22, 0
br i1 %23, label %29, label %24
24: ; preds = %21
%25 = icmp eq i64 %22, 0
br i1 %25, label %26, label %31
26: ; preds = %24
%27 = tail call i32 @alarm(i32 180)
%28 = tail call i32 (...) @do_child()
br label %31
29: ; preds = %21
%30 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %31
31: ; preds = %29, %26, %24
%32 = load i32, i32* @SIGTERM
%33 = tail call i64 @kill(i32 -1, i32 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %37
37: ; preds = %35, %31
%38 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%39 = call i64 @waitpid(i64 %2, i32* %1, i32 0)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %43
43: ; preds = %41, %37
%44 = load i32, i32* %1
%45 = call i32 @WIFSIGNALED(i32 %44)
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %43
%48 = load i32, i32* %1
%49 = call i32 @errx(i32 1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i64 %2, i32 %48)
br label %50
50: ; preds = %47, %43
%51 = load i32, i32* %1
%52 = call i32 @WTERMSIG(i32 %51)
%53 = load i32, i32* @SIGTERM
%54 = icmp eq i32 %52, %53
br i1 %54, label %59, label %55
55: ; preds = %50
%56 = load i32, i32* %1
%57 = call i32 @WTERMSIG(i32 %56)
%58 = call i32 @errx(i32 1, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i64 %2, i32 %57)
br label %59
59: ; preds = %55, %50
store i32 0, i32* %1
%60 = call i64 @waitpid(i64 %12, i32* %1, i32 0)
%61 = icmp slt i64 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %59
%63 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %64
64: ; preds = %62, %59
%65 = load i32, i32* %1
%66 = call i32 @WIFSIGNALED(i32 %65)
%67 = icmp eq i32 %66, 0
br i1 %67, label %68, label %71
68: ; preds = %64
%69 = load i32, i32* %1
%70 = call i32 @errx(i32 1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i64 %12, i32 %69)
br label %71
71: ; preds = %68, %64
%72 = load i32, i32* %1
%73 = call i32 @WTERMSIG(i32 %72)
%74 = load i32, i32* @SIGTERM
%75 = icmp eq i32 %73, %74
br i1 %75, label %80, label %76
76: ; preds = %71
%77 = load i32, i32* %1
%78 = call i32 @WTERMSIG(i32 %77)
%79 = call i32 @errx(i32 1, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i64 %12, i32 %78)
br label %80
80: ; preds = %76, %71
store i32 0, i32* %1
%81 = call i64 @waitpid(i64 %22, i32* %1, i32 0)
%82 = icmp slt i64 %81, 0
br i1 %82, label %83, label %85
83: ; preds = %80
%84 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %85
85: ; preds = %83, %80
%86 = load i32, i32* %1
%87 = call i32 @WIFSIGNALED(i32 %86)
%88 = icmp eq i32 %87, 0
br i1 %88, label %89, label %92
89: ; preds = %85
%90 = load i32, i32* %1
%91 = call i32 @errx(i32 1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i64 %22, i32 %90)
br label %92
92: ; preds = %89, %85
%93 = load i32, i32* %1
%94 = call i32 @WTERMSIG(i32 %93)
%95 = load i32, i32* @SIGTERM
%96 = icmp eq i32 %94, %95
br i1 %96, label %101, label %97
97: ; preds = %92
%98 = load i32, i32* %1
%99 = call i32 @WTERMSIG(i32 %98)
%100 = call i32 @errx(i32 1, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i64 %22, i32 %99)
br label %101
101: ; preds = %97, %92
%102 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
ret i32 0
}