#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 /*<<< orphan*/ sigset_t;
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ SIGUSR1;
int /*<<< orphan*/ SIG_BLOCK;
int /*<<< orphan*/ SIG_SETMASK;
int do_wait(int *, int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ free(int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sigaddset(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int /*<<< orphan*/ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                               int /*<<< orphan*/ *);
int /*<<< orphan*/ sigwait(int /*<<< orphan*/ *, int *);
int spawn_processes(int num) {
  int i;
  int chld_ret = 0;
  pid_t *pids;
  pid_t pid;
  sigset_t set, oldset;
  int signal;
  sigemptyset(&set);
  sigaddset(&set, SIGUSR1);
  if (num <= 1) {
    return 0;
  }
  pids = (pid_t *)malloc(num * sizeof(pid_t));
  if (pids == NULL) {
    exit(12);
  }
  for (i = 0; i < (num - 1); i++) {
    pid = fork();
    switch (pid) {
    case -1:
      perror("fork");
      do_wait(pids, i);
      free(pids);
      exit(5);
    case 0:
      printf("Remove or add a device to try to cause an error\n");
      printf("Waiting for signal SIGUSR1\n");
      sigprocmask(SIG_BLOCK, &set, &oldset);
      sigwait(&set, &signal);
      sigprocmask(SIG_SETMASK, &oldset, NULL);
      free(pids);
      return 0;
    default:
      pids[i] = pid;
      printf("spawned %d\n", pid);
    }
  }
  chld_ret = do_wait(pids, (num - 1));
  free(pids);
  return chld_ret;
}


x86 assembly (clang, -O0):
<spawn_processes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
call   1060 <sigemptyset@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdi
call   10c0 <sigaddset@plt>
cmpl   $0x1,-0x8(%rbp)
jg     11f3 <spawn_processes+0x43>
movl   $0x0,-0x4(%rbp)
jmp    1349 <spawn_processes+0x199>
movslq -0x8(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    121b <spawn_processes+0x6b>
mov    $0xc,%edi
call   10b0 <exit@plt>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1328 <spawn_processes+0x178>
xor    %eax,%eax
call   10d0 <fork@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
sub    $0xffffffff,%eax
je     1261 <spawn_processes+0xb1>
jmp    1251 <spawn_processes+0xa1>
mov    -0x2c(%rbp),%eax
test   %eax,%eax
je     128c <spawn_processes+0xdc>
jmp    12f6 <spawn_processes+0x146>
lea    0xd98(%rip),%rdi
call   1090 <perror@plt>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1080 <do_wait@plt>
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
mov    $0x5,%edi
call   10b0 <exit@plt>
lea    0xd72(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xd95(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d21(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1030 <sigprocmask@plt>
lea    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   10a0 <sigwait@plt>
mov    0x2d0e(%rip),%rax
mov    (%rax),%edi
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    1349 <spawn_processes+0x199>
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x1c(%rbp),%esi
lea    0xd44(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    131a <spawn_processes+0x16a>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1222 <spawn_processes+0x72>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%esi
sub    $0x1,%esi
call   1080 <do_wait@plt>
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawn_processes>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r15d
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <sigemptyset@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10d0 <sigaddset@plt>
xor    %ebx,%ebx
cmp    $0x2,%r15d
jl     12bc <spawn_processes+0xfc>
lea    0x0(,%r15,4),%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     12f2 <spawn_processes+0x132>
mov    %rax,%r14
add    $0xffffffff,%r15d
lea    0xde9(%rip),%r12
xor    %ebx,%ebx
xchg   %ax,%ax
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
je     125c <spawn_processes+0x9c>
cmp    $0xffffffff,%eax
je     12ca <spawn_processes+0x10a>
mov    %eax,(%r14,%rbx,4)
mov    %r12,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1220 <spawn_processes+0x60>
mov    %r14,%rdi
mov    %r15d,%esi
call   1090 <do_wait@plt>
mov    %eax,%ebx
jmp    12b4 <spawn_processes+0xf4>
lea    0xdae(%rip),%rdi
call   1050 <puts@plt>
lea    0xdd2(%rip),%rdi
call   1050 <puts@plt>
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
lea    0xc(%rsp),%rbx
lea    0x14(%rsp),%r15
mov    %rbx,%rsi
mov    %r15,%rdx
call   1030 <sigprocmask@plt>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   10b0 <sigwait@plt>
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
xor    %ebx,%ebx
mov    %r15,%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
mov    %r14,%rdi
call   1040 <free@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xd2f(%rip),%rdi
call   10a0 <perror@plt>
mov    %r14,%rdi
mov    %ebx,%esi
call   1090 <do_wait@plt>
mov    %r14,%rdi
call   1040 <free@plt>
mov    $0x5,%edi
call   10c0 <exit@plt>
mov    $0xc,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@SIGUSR1 = global i32 0
@.str = [5 x i8] c"fork\00"
@.str.1 = [49 x i8] c"Remove or add a device to try to cause an error\0A\00"
@.str.2 = [28 x i8] c"Waiting for signal SIGUSR1\0A\00"
@SIG_BLOCK = global i32 0
@SIG_SETMASK = global i32 0
@.str.3 = [12 x i8] c"spawned %d\0A\00"
define i32 @spawn_processes(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %3
store i32 0, i32* %5
%11 = call i32 @sigemptyset(i32* %8)
%12 = load i32, i32* @SIGUSR1
%13 = call i32 @sigaddset(i32* %8, i32 %12)
%14 = load i32, i32* %3
%15 = icmp sle i32 %14, 1
br i1 %15, label %16, label %17
16: ; preds = %1
store i32 0, i32* %2
br label %75
17: ; preds = %1
%18 = load i32, i32* %3
%19 = sext i32 %18 to i64
%20 = mul i64 %19, 4
%21 = trunc i64 %20 to i32
%22 = call i64 @malloc(i32 %21)
%23 = inttoptr i64 %22 to i32*
store i32* %23, i32** %6
%24 = load i32*, i32** %6
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %28
26: ; preds = %17
%27 = call i32 @exit(i32 12)
unreachable
28: ; preds = %17
store i32 0, i32* %4
br label %29
29: ; preds = %64, %28
%30 = load i32, i32* %4
%31 = load i32, i32* %3
%32 = sub nsw i32 %31, 1
%33 = icmp slt i32 %30, %32
br i1 %33, label %34, label %67
34: ; preds = %29
%35 = call i32 (...) @fork()
store i32 %35, i32* %7
%36 = load i32, i32* %7
switch i32 %36, label %55 [
i32 -1, label %37
i32 0, label %45
]
37: ; preds = %34
%38 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%39 = load i32*, i32** %6
%40 = load i32, i32* %4
%41 = call i32 @do_wait(i32* %39, i32 %40)
%42 = load i32*, i32** %6
%43 = call i32 @free(i32* %42)
%44 = call i32 @exit(i32 5)
unreachable
45: ; preds = %34
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%48 = load i32, i32* @SIG_BLOCK
%49 = call i32 @sigprocmask(i32 %48, i32* %8, i32* %9)
%50 = call i32 @sigwait(i32* %8, i32* %10)
%51 = load i32, i32* @SIG_SETMASK
%52 = call i32 @sigprocmask(i32 %51, i32* %9, i32* null)
%53 = load i32*, i32** %6
%54 = call i32 @free(i32* %53)
store i32 0, i32* %2
br label %75
55: ; preds = %34
%56 = load i32, i32* %7
%57 = load i32*, i32** %6
%58 = load i32, i32* %4
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i32, i32* %57, i64 %59
store i32 %56, i32* %60
%61 = load i32, i32* %7
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %61)
br label %63
63: ; preds = %55
br label %64
64: ; preds = %63
%65 = load i32, i32* %4
%66 = add nsw i32 %65, 1
store i32 %66, i32* %4
br label %29
67: ; preds = %29
%68 = load i32*, i32** %6
%69 = load i32, i32* %3
%70 = sub nsw i32 %69, 1
%71 = call i32 @do_wait(i32* %68, i32 %70)
store i32 %71, i32* %5
%72 = load i32*, i32** %6
%73 = call i32 @free(i32* %72)
%74 = load i32, i32* %5
store i32 %74, i32* %2
br label %75
75: ; preds = %67, %45, %16
%76 = load i32, i32* %2
ret i32 %76
}

LLVM IR (-O2):
@SIGUSR1 = global i32 0
@.str = [5 x i8] c"fork\00"
@SIG_BLOCK = global i32 0
@SIG_SETMASK = global i32 0
@.str.3 = [12 x i8] c"spawned %d\0A\00"
@str = [48 x i8] c"Remove or add a device to try to cause an error\00"
@str.4 = [27 x i8] c"Waiting for signal SIGUSR1\00"
define i32 @spawn_processes(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = call i32 @sigemptyset(i32* %2)
%9 = load i32, i32* @SIGUSR1
%10 = call i32 @sigaddset(i32* %2, i32 %9)
%11 = icmp slt i32 %0, 2
br i1 %11, label %49, label %12
12: ; preds = %1
%13 = shl i32 %0, 2
%14 = call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to i32*
%16 = icmp eq i64 %14, 0
br i1 %16, label %20, label %17
17: ; preds = %12
%18 = add nsw i32 %0, -1
%19 = zext i32 %18 to i64
br label %22
20: ; preds = %12
%21 = call i32 @exit(i32 12)
unreachable
22: ; preds = %17, %39
%23 = phi i64 [ 0, %17 ], [ %42, %39 ]
%24 = call i32 (...) @fork()
switch i32 %24, label %39 [
i32 -1, label %25
i32 0, label %31
]
25: ; preds = %22
%26 = trunc i64 %23 to i32
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%28 = call i32 @do_wait(i32* %15, i32 %26)
%29 = call i32 @free(i32* %15)
%30 = call i32 @exit(i32 5)
unreachable
31: ; preds = %22
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str, i64 0, i64 0))
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.4, i64 0, i64 0))
%34 = load i32, i32* @SIG_BLOCK
%35 = call i32 @sigprocmask(i32 %34, i32* %2, i32* %3)
%36 = call i32 @sigwait(i32* %2, i32* %4)
%37 = load i32, i32* @SIG_SETMASK
%38 = call i32 @sigprocmask(i32 %37, i32* %3, i32* null)
br label %46
39: ; preds = %22
%40 = getelementptr inbounds i32, i32* %15, i64 %23
store i32 %24, i32* %40
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %24)
%42 = add nuw nsw i64 %23, 1
%43 = icmp eq i64 %42, %19
br i1 %43, label %44, label %22
44: ; preds = %39
%45 = call i32 @do_wait(i32* %15, i32 %18)
br label %46
46: ; preds = %31, %44
%47 = phi i32 [ %45, %44 ], [ 0, %31 ]
%48 = call i32 @free(i32* %15)
br label %49
49: ; preds = %46, %1
%50 = phi i32 [ 0, %1 ], [ %47, %46 ]
ret i32 %50
}