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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ doit(int, int, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ handle_scheduler(int /*<<< orphan*/, int, int);
int read(int, int *, int);
int /*<<< orphan*/ stderr;
int write(int, int *, int);
int create_daemons(int **p, pid_t *pids, int procs, int process_size) {
  int i, j;
  int msg;
  handle_scheduler(getpid(), 0, procs - 1);
  for (i = 1; i < procs; ++i) {
    switch (pids[i] = fork()) {
    case -1:
      return i;
    case 0:
      handle_scheduler(getpid(), i, procs - 1);
      for (j = 0; j < procs; ++j) {
        if (j != i - 1)
          close(p[j][0]);
        if (j != i)
          close(p[j][1]);
      }
      doit(p[i - 1][0], p[i][1], process_size);
    default:;
    }
  }
  if (write(p[0][1], &msg, sizeof(msg)) != sizeof(msg) ||
      read(p[procs - 1][0], &msg, sizeof(msg)) != sizeof(msg)) {
    (void)fprintf(stderr, "write/read/write on pipe"
                          "\n");
    exit(1);
  }
  return procs;
}


x86 assembly (clang, -O0):
<create_daemons>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%edi
mov    -0x1c(%rbp),%edx
sub    $0x1,%edx
xor    %esi,%esi
call   1080 <handle_scheduler@plt>
movl   $0x1,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    12d2 <create_daemons+0x142>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    -0x18(%rbp),%rcx
movslq -0x24(%rbp),%rdx
mov    %eax,(%rcx,%rdx,4)
mov    %eax,%ecx
mov    %ecx,-0x30(%rbp)
sub    $0xffffffff,%eax
je     1204 <create_daemons+0x74>
jmp    11f4 <create_daemons+0x64>
mov    -0x30(%rbp),%eax
test   %eax,%eax
je     120f <create_daemons+0x7f>
jmp    12ba <create_daemons+0x12a>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1348 <create_daemons+0x1b8>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%edi
mov    -0x24(%rbp),%esi
mov    -0x1c(%rbp),%edx
sub    $0x1,%edx
call   1080 <handle_scheduler@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1290 <create_daemons+0x100>
mov    -0x28(%rbp),%eax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
je     125d <create_daemons+0xcd>
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
je     127d <create_daemons+0xed>
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    0x4(%rax),%edi
call   1050 <close@plt>
jmp    1282 <create_daemons+0xf2>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    122d <create_daemons+0x9d>
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    0x4(%rax),%esi
mov    -0x20(%rbp),%edx
call   1090 <doit@plt>
jmp    12bf <create_daemons+0x12f>
jmp    12c4 <create_daemons+0x134>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11c3 <create_daemons+0x33>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x4(%rax),%edi
lea    -0x2c(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cltq
cmp    $0x4,%rax
jne    1323 <create_daemons+0x193>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%edi
lea    -0x2c(%rbp),%rsi
mov    $0x4,%edx
call   1060 <read@plt>
cltq
cmp    $0x4,%rax
je     1342 <create_daemons+0x1b2>
mov    0x2cb6(%rip),%rax
mov    (%rax),%edi
lea    0xccd(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_daemons>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,0x10(%rsp)
mov    %edx,%ebp
mov    %rsi,%r13
mov    %rdi,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    -0x1(%rbp),%edx
mov    %eax,%edi
xor    %esi,%esi
mov    %edx,0x4(%rsp)
call   1080 <handle_scheduler@plt>
mov    %rbp,0x8(%rsp)
cmp    $0x2,%ebp
jl     126c <create_daemons+0xdc>
mov    0x8(%rsp),%r14d
mov    $0x1,%ebp
xor    %r15d,%r15d
jmp    1204 <create_daemons+0x74>
xchg   %ax,%ax
mov    -0x8(%rbx,%rbp,8),%rax
mov    (%rbx,%rbp,8),%rcx
mov    (%rax),%edi
mov    0x4(%rcx),%esi
mov    0x10(%rsp),%edx
call   1090 <doit@plt>
add    $0x1,%rbp
add    $0x1,%r15
cmp    %r14,%rbp
je     126c <create_daemons+0xdc>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,0x0(%r13,%rbp,4)
test   %eax,%eax
je     1220 <create_daemons+0x90>
cmp    $0xffffffff,%eax
jne    11f7 <create_daemons+0x67>
jmp    12b9 <create_daemons+0x129>
xchg   %ax,%ax
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%edi
mov    %ebp,%esi
mov    0x4(%rsp),%edx
call   1080 <handle_scheduler@plt>
xor    %r12d,%r12d
jmp    1249 <create_daemons+0xb9>
nopl   0x0(%rax)
add    $0x1,%r12
cmp    %r12,%r14
je     11e0 <create_daemons+0x50>
cmp    %r12,%r15
je     1259 <create_daemons+0xc9>
mov    (%rbx,%r12,8),%rax
mov    (%rax),%edi
call   1050 <close@plt>
cmp    %r12,%rbp
je     1240 <create_daemons+0xb0>
mov    (%rbx,%r12,8),%rax
mov    0x4(%rax),%edi
call   1050 <close@plt>
jmp    1240 <create_daemons+0xb0>
mov    (%rbx),%rax
mov    0x4(%rax),%edi
lea    0x14(%rsp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
jne    12c0 <create_daemons+0x130>
movslq 0x4(%rsp),%rax
mov    (%rbx,%rax,8),%rax
mov    (%rax),%edi
lea    0x14(%rsp),%rsi
mov    $0x4,%edx
call   1060 <read@plt>
cmp    $0x4,%eax
jne    12c0 <create_daemons+0x130>
mov    0x8(%rsp),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbp,0x8(%rsp)
jmp    12a5 <create_daemons+0x115>
mov    0x2d19(%rip),%rax
mov    (%rax),%edi
lea    0xd30(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [26 x i8] c"write/read/write on pipe\0A\00"
define i32 @create_daemons(i32** %0, i32* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32**
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32** %0, i32*** %6
store i32* %1, i32** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%13 = call i32 (...) @getpid()
%14 = load i32, i32* %8
%15 = sub nsw i32 %14, 1
%16 = call i32 @handle_scheduler(i32 %13, i32 0, i32 %15)
store i32 1, i32* %10
br label %17
17: ; preds = %90, %4
%18 = load i32, i32* %10
%19 = load i32, i32* %8
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %93
21: ; preds = %17
%22 = call i32 (...) @fork()
%23 = load i32*, i32** %7
%24 = load i32, i32* %10
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i32, i32* %23, i64 %25
store i32 %22, i32* %26
switch i32 %22, label %88 [
i32 -1, label %27
i32 0, label %29
]
27: ; preds = %21
%28 = load i32, i32* %10
store i32 %28, i32* %5
br label %120
29: ; preds = %21
%30 = call i32 (...) @getpid()
%31 = load i32, i32* %10
%32 = load i32, i32* %8
%33 = sub nsw i32 %32, 1
%34 = call i32 @handle_scheduler(i32 %30, i32 %31, i32 %33)
store i32 0, i32* %11
br label %35
35: ; preds = %67, %29
%36 = load i32, i32* %11
%37 = load i32, i32* %8
%38 = icmp slt i32 %36, %37
br i1 %38, label %39, label %70
39: ; preds = %35
%40 = load i32, i32* %11
%41 = load i32, i32* %10
%42 = sub nsw i32 %41, 1
%43 = icmp ne i32 %40, %42
br i1 %43, label %44, label %53
44: ; preds = %39
%45 = load i32**, i32*** %6
%46 = load i32, i32* %11
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i32*, i32** %45, i64 %47
%49 = load i32*, i32** %48
%50 = getelementptr inbounds i32, i32* %49, i64 0
%51 = load i32, i32* %50
%52 = call i32 @close(i32 %51)
br label %53
53: ; preds = %44, %39
%54 = load i32, i32* %11
%55 = load i32, i32* %10
%56 = icmp ne i32 %54, %55
br i1 %56, label %57, label %66
57: ; preds = %53
%58 = load i32**, i32*** %6
%59 = load i32, i32* %11
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i32*, i32** %58, i64 %60
%62 = load i32*, i32** %61
%63 = getelementptr inbounds i32, i32* %62, i64 1
%64 = load i32, i32* %63
%65 = call i32 @close(i32 %64)
br label %66
66: ; preds = %57, %53
br label %67
67: ; preds = %66
%68 = load i32, i32* %11
%69 = add nsw i32 %68, 1
store i32 %69, i32* %11
br label %35
70: ; preds = %35
%71 = load i32**, i32*** %6
%72 = load i32, i32* %10
%73 = sub nsw i32 %72, 1
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds i32*, i32** %71, i64 %74
%76 = load i32*, i32** %75
%77 = getelementptr inbounds i32, i32* %76, i64 0
%78 = load i32, i32* %77
%79 = load i32**, i32*** %6
%80 = load i32, i32* %10
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds i32*, i32** %79, i64 %81
%83 = load i32*, i32** %82
%84 = getelementptr inbounds i32, i32* %83, i64 1
%85 = load i32, i32* %84
%86 = load i32, i32* %9
%87 = call i32 @doit(i32 %78, i32 %85, i32 %86)
br label %88
88: ; preds = %21, %70
br label %89
89: ; preds = %88
br label %90
90: ; preds = %89
%91 = load i32, i32* %10
%92 = add nsw i32 %91, 1
store i32 %92, i32* %10
br label %17
93: ; preds = %17
%94 = load i32**, i32*** %6
%95 = getelementptr inbounds i32*, i32** %94, i64 0
%96 = load i32*, i32** %95
%97 = getelementptr inbounds i32, i32* %96, i64 1
%98 = load i32, i32* %97
%99 = call i32 @write(i32 %98, i32* %12, i32 4)
%100 = sext i32 %99 to i64
%101 = icmp ne i64 %100, 4
br i1 %101, label %114, label %102
102: ; preds = %93
%103 = load i32**, i32*** %6
%104 = load i32, i32* %8
%105 = sub nsw i32 %104, 1
%106 = sext i32 %105 to i64
%107 = getelementptr inbounds i32*, i32** %103, i64 %106
%108 = load i32*, i32** %107
%109 = getelementptr inbounds i32, i32* %108, i64 0
%110 = load i32, i32* %109
%111 = call i32 @read(i32 %110, i32* %12, i32 4)
%112 = sext i32 %111 to i64
%113 = icmp ne i64 %112, 4
br i1 %113, label %114, label %118
114: ; preds = %102, %93
%115 = load i32, i32* @stderr
%116 = call i32 @fprintf(i32 %115, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%117 = call i32 @exit(i32 1)
unreachable
118: ; preds = %102
%119 = load i32, i32* %8
store i32 %119, i32* %5
br label %120
120: ; preds = %118, %27
%121 = load i32, i32* %5
ret i32 %121
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [26 x i8] c"write/read/write on pipe\0A\00"
define i32 @create_daemons(i32** nocapture %0, i32* nocapture writeonly %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = tail call i32 (...) @getpid()
%8 = add nsw i32 %2, -1
%9 = tail call i32 @handle_scheduler(i32 %7, i32 0, i32 %8)
%10 = icmp sgt i32 %2, 1
br i1 %10, label %11, label %53
11: ; preds = %4
%12 = zext i32 %2 to i64
br label %13
13: ; preds = %11, %50
%14 = phi i64 [ 1, %11 ], [ %51, %50 ]
%15 = tail call i32 (...) @fork()
%16 = getelementptr inbounds i32, i32* %1, i64 %14
store i32 %15, i32* %16
switch i32 %15, label %50 [
i32 -1, label %70
i32 0, label %17
]
17: ; preds = %13
%18 = tail call i32 (...) @getpid()
%19 = trunc i64 %14 to i32
%20 = tail call i32 @handle_scheduler(i32 %18, i32 %19, i32 %8)
%21 = add nsw i64 %14, -1
br label %22
22: ; preds = %17, %38
%23 = phi i64 [ 0, %17 ], [ %39, %38 ]
%24 = icmp eq i64 %23, %21
br i1 %24, label %30, label %25
25: ; preds = %22
%26 = getelementptr inbounds i32*, i32** %0, i64 %23
%27 = load i32*, i32** %26
%28 = load i32, i32* %27
%29 = tail call i32 @close(i32 %28)
br label %30
30: ; preds = %25, %22
%31 = icmp eq i64 %23, %14
br i1 %31, label %38, label %32
32: ; preds = %30
%33 = getelementptr inbounds i32*, i32** %0, i64 %23
%34 = load i32*, i32** %33
%35 = getelementptr inbounds i32, i32* %34, i64 1
%36 = load i32, i32* %35
%37 = tail call i32 @close(i32 %36)
br label %38
38: ; preds = %30, %32
%39 = add nuw nsw i64 %23, 1
%40 = icmp eq i64 %39, %12
br i1 %40, label %41, label %22
41: ; preds = %38
%42 = getelementptr inbounds i32*, i32** %0, i64 %21
%43 = load i32*, i32** %42
%44 = load i32, i32* %43
%45 = getelementptr inbounds i32*, i32** %0, i64 %14
%46 = load i32*, i32** %45
%47 = getelementptr inbounds i32, i32* %46, i64 1
%48 = load i32, i32* %47
%49 = tail call i32 @doit(i32 %44, i32 %48, i32 %3)
br label %50
50: ; preds = %13, %41
%51 = add nuw nsw i64 %14, 1
%52 = icmp eq i64 %51, %12
br i1 %52, label %53, label %13
53: ; preds = %50, %4
%54 = load i32*, i32** %0
%55 = getelementptr inbounds i32, i32* %54, i64 1
%56 = load i32, i32* %55
%57 = call i32 @write(i32 %56, i32* %5, i32 4)
%58 = icmp eq i32 %57, 4
br i1 %58, label %59, label %66
59: ; preds = %53
%60 = sext i32 %8 to i64
%61 = getelementptr inbounds i32*, i32** %0, i64 %60
%62 = load i32*, i32** %61
%63 = load i32, i32* %62
%64 = call i32 @read(i32 %63, i32* %5, i32 4)
%65 = icmp eq i32 %64, 4
br i1 %65, label %72, label %66
66: ; preds = %59, %53
%67 = load i32, i32* @stderr
%68 = tail call i32 @fprintf(i32 %67, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%69 = tail call i32 @exit(i32 1)
unreachable
70: ; preds = %13
%71 = trunc i64 %14 to i32
br label %72
72: ; preds = %70, %59
%73 = phi i32 [ %2, %59 ], [ %71, %70 ]
ret i32 %73
}