#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 await_children(int *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open_pipe(char *);
int /*<<< orphan*/ perror(char *);
int read_and_print_results(int, int *, int);
int /*<<< orphan*/ run_child();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ unlink(char *);
int main(int argc, char **argv) {
  int fd;
  int i;
  int num_reported;
  pid_t pids[10];
  if ((fd = open_pipe("/tmp/challenge_fifo")) < 0) {
    exit(-1);
  }
  for (i = 0; i < 10; ++i) {
    pid_t pid = fork();
    pids[i] = pid;
    if (pid == -1) {
      perror("Error forking child process");
      exit(-1);
    } else if (pids[i] == 0) {
      run_child();
    }
  }
  if (await_children(pids, 10) != 10) {
    fprintf(stderr, "Child count doesn't match number forked\n");
  }
  num_reported = read_and_print_results(fd, pids, 10);
  if (num_reported != 10) {
    fprintf(stderr, "Not all children reported results\n");
  }
  close(fd);
  unlink("/tmp/challenge_fifo");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe43(%rip),%rdi
call   1080 <open_pipe@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11d8 <main+0x38>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0xa,-0x18(%rbp)
jge    124c <main+0xac>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x54(%rbp)
mov    -0x54(%rbp),%ecx
movslq -0x18(%rbp),%rax
mov    %ecx,-0x50(%rbp,%rax,4)
cmpl   $0xffffffff,-0x54(%rbp)
jne    121e <main+0x7e>
lea    0xe05(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
movslq -0x18(%rbp),%rax
cmpl   $0x0,-0x50(%rbp,%rax,4)
jne    1234 <main+0x94>
mov    $0x0,%al
call   10a0 <run_child@plt>
jmp    1239 <main+0x99>
jmp    123e <main+0x9e>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11df <main+0x3f>
lea    -0x50(%rbp),%rdi
mov    $0xa,%esi
call   1040 <await_children@plt>
cmp    $0xa,%eax
je     1278 <main+0xd8>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
lea    0xdbd(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x14(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0xa,%edx
call   1060 <read_and_print_results@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xa,-0x1c(%rbp)
je     12ab <main+0x10b>
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
lea    0xdb3(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
lea    0xd46(%rip),%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x30,%rsp
lea    0xe54(%rip),%rdi
call   1080 <open_pipe@plt>
test   %eax,%eax
js     136a <main+0x1ca>
mov    %eax,%ebx
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,(%rsp)
test   %eax,%eax
je     11d3 <main+0x33>
cmp    $0xffffffff,%eax
jne    11da <main+0x3a>
jmp    12e2 <main+0x142>
xor    %eax,%eax
call   10a0 <run_child@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,0x4(%rsp)
test   %eax,%eax
je     11f3 <main+0x53>
cmp    $0xffffffff,%eax
jne    11fa <main+0x5a>
jmp    12e2 <main+0x142>
xor    %eax,%eax
call   10a0 <run_child@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,0x8(%rsp)
test   %eax,%eax
je     1213 <main+0x73>
cmp    $0xffffffff,%eax
jne    121a <main+0x7a>
jmp    12e2 <main+0x142>
xor    %eax,%eax
call   10a0 <run_child@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,0xc(%rsp)
test   %eax,%eax
je     1233 <main+0x93>
cmp    $0xffffffff,%eax
jne    123a <main+0x9a>
jmp    12e2 <main+0x142>
xor    %eax,%eax
call   10a0 <run_child@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,0x10(%rsp)
test   %eax,%eax
je     1253 <main+0xb3>
cmp    $0xffffffff,%eax
jne    125a <main+0xba>
jmp    12e2 <main+0x142>
xor    %eax,%eax
call   10a0 <run_child@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,0x14(%rsp)
test   %eax,%eax
je     1270 <main+0xd0>
cmp    $0xffffffff,%eax
jne    1277 <main+0xd7>
jmp    12e2 <main+0x142>
xor    %eax,%eax
call   10a0 <run_child@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,0x18(%rsp)
test   %eax,%eax
je     128d <main+0xed>
cmp    $0xffffffff,%eax
jne    1294 <main+0xf4>
jmp    12e2 <main+0x142>
xor    %eax,%eax
call   10a0 <run_child@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,0x1c(%rsp)
test   %eax,%eax
je     12aa <main+0x10a>
cmp    $0xffffffff,%eax
jne    12b1 <main+0x111>
jmp    12e2 <main+0x142>
xor    %eax,%eax
call   10a0 <run_child@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,0x20(%rsp)
test   %eax,%eax
je     12c7 <main+0x127>
cmp    $0xffffffff,%eax
jne    12ce <main+0x12e>
jmp    12e2 <main+0x142>
xor    %eax,%eax
call   10a0 <run_child@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,0x24(%rsp)
test   %eax,%eax
je     12f8 <main+0x158>
cmp    $0xffffffff,%eax
jne    12ff <main+0x15f>
lea    0xd2b(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
xor    %eax,%eax
call   10a0 <run_child@plt>
mov    %rsp,%rdi
mov    $0xa,%esi
call   1040 <await_children@plt>
cmp    $0xa,%eax
je     1326 <main+0x186>
mov    0x2cc8(%rip),%rax
mov    (%rax),%edi
lea    0xd0f(%rip),%rsi
call   1070 <fprintf@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xa,%edx
call   1060 <read_and_print_results@plt>
cmp    $0xa,%eax
je     134f <main+0x1af>
mov    0x2c9f(%rip),%rax
mov    (%rax),%edi
lea    0xd0f(%rip),%rsi
call   1070 <fprintf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
lea    0xca3(%rip),%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
ret
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"/tmp/challenge_fifo\00"
@.str.1 = [28 x i8] c"Error forking child process\00"
@stderr = global i32 0
@.str.2 = [41 x i8] c"Child count doesn't match number forked\0A\00"
@.str.3 = [35 x i8] c"Not all children reported results\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
%9 = alloca [10 x i32]
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32 @open_pipe(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 %11, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @exit(i32 -1)
unreachable
15: ; preds = %2
store i32 0, i32* %7
br label %16
16: ; preds = %40, %15
%17 = load i32, i32* %7
%18 = icmp slt i32 %17, 10
br i1 %18, label %19, label %43
19: ; preds = %16
%20 = call i32 (...) @fork()
store i32 %20, i32* %10
%21 = load i32, i32* %10
%22 = load i32, i32* %7
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [10 x i32], [10 x i32]* %9, i64 0, i64 %23
store i32 %21, i32* %24
%25 = load i32, i32* %10
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %19
%28 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 -1)
unreachable
30: ; preds = %19
%31 = load i32, i32* %7
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [10 x i32], [10 x i32]* %9, i64 0, i64 %32
%34 = load i32, i32* %33
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %30
%37 = call i32 (...) @run_child()
br label %38
38: ; preds = %36, %30
br label %39
39: ; preds = %38
br label %40
40: ; preds = %39
%41 = load i32, i32* %7
%42 = add nsw i32 %41, 1
store i32 %42, i32* %7
br label %16
43: ; preds = %16
%44 = getelementptr inbounds [10 x i32], [10 x i32]* %9, i64 0, i64 0
%45 = call i32 @await_children(i32* %44, i32 10)
%46 = icmp ne i32 %45, 10
br i1 %46, label %47, label %50
47: ; preds = %43
%48 = load i32, i32* @stderr
%49 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
br label %50
50: ; preds = %47, %43
%51 = load i32, i32* %6
%52 = getelementptr inbounds [10 x i32], [10 x i32]* %9, i64 0, i64 0
%53 = call i32 @read_and_print_results(i32 %51, i32* %52, i32 10)
store i32 %53, i32* %8
%54 = load i32, i32* %8
%55 = icmp ne i32 %54, 10
br i1 %55, label %56, label %59
56: ; preds = %50
%57 = load i32, i32* @stderr
%58 = call i32 @fprintf(i32 %57, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
br label %59
59: ; preds = %56, %50
%60 = load i32, i32* %6
%61 = call i32 @close(i32 %60)
%62 = call i32 @unlink(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [20 x i8] c"/tmp/challenge_fifo\00"
@.str.1 = [28 x i8] c"Error forking child process\00"
@stderr = global i32 0
@.str.2 = [41 x i8] c"Child count doesn't match number forked\0A\00"
@.str.3 = [35 x i8] c"Not all children reported results\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [10 x i32]
%4 = bitcast [10 x i32]* %3 to i8*
%5 = tail call i32 @open_pipe(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%6 = icmp slt i32 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = tail call i32 (...) @fork()
%9 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i64 0, i64 0
store i32 %8, i32* %9
switch i32 %8, label %17 [
i32 -1, label %12
i32 0, label %15
]
10: ; preds = %2
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %57, %52, %47, %42, %37, %32, %27, %22, %17, %7
%13 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 @exit(i32 -1)
unreachable
15: ; preds = %7
%16 = tail call i32 (...) @run_child()
br label %17
17: ; preds = %7, %15
%18 = tail call i32 (...) @fork()
%19 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i64 0, i64 1
store i32 %18, i32* %19
switch i32 %18, label %22 [
i32 -1, label %12
i32 0, label %20
]
20: ; preds = %17
%21 = tail call i32 (...) @run_child()
br label %22
22: ; preds = %20, %17
%23 = tail call i32 (...) @fork()
%24 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i64 0, i64 2
store i32 %23, i32* %24
switch i32 %23, label %27 [
i32 -1, label %12
i32 0, label %25
]
25: ; preds = %22
%26 = tail call i32 (...) @run_child()
br label %27
27: ; preds = %25, %22
%28 = tail call i32 (...) @fork()
%29 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i64 0, i64 3
store i32 %28, i32* %29
switch i32 %28, label %32 [
i32 -1, label %12
i32 0, label %30
]
30: ; preds = %27
%31 = tail call i32 (...) @run_child()
br label %32
32: ; preds = %30, %27
%33 = tail call i32 (...) @fork()
%34 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i64 0, i64 4
store i32 %33, i32* %34
switch i32 %33, label %37 [
i32 -1, label %12
i32 0, label %35
]
35: ; preds = %32
%36 = tail call i32 (...) @run_child()
br label %37
37: ; preds = %35, %32
%38 = tail call i32 (...) @fork()
%39 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i64 0, i64 5
store i32 %38, i32* %39
switch i32 %38, label %42 [
i32 -1, label %12
i32 0, label %40
]
40: ; preds = %37
%41 = tail call i32 (...) @run_child()
br label %42
42: ; preds = %40, %37
%43 = tail call i32 (...) @fork()
%44 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i64 0, i64 6
store i32 %43, i32* %44
switch i32 %43, label %47 [
i32 -1, label %12
i32 0, label %45
]
45: ; preds = %42
%46 = tail call i32 (...) @run_child()
br label %47
47: ; preds = %45, %42
%48 = tail call i32 (...) @fork()
%49 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i64 0, i64 7
store i32 %48, i32* %49
switch i32 %48, label %52 [
i32 -1, label %12
i32 0, label %50
]
50: ; preds = %47
%51 = tail call i32 (...) @run_child()
br label %52
52: ; preds = %50, %47
%53 = tail call i32 (...) @fork()
%54 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i64 0, i64 8
store i32 %53, i32* %54
switch i32 %53, label %57 [
i32 -1, label %12
i32 0, label %55
]
55: ; preds = %52
%56 = tail call i32 (...) @run_child()
br label %57
57: ; preds = %55, %52
%58 = tail call i32 (...) @fork()
%59 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i64 0, i64 9
store i32 %58, i32* %59
switch i32 %58, label %62 [
i32 -1, label %12
i32 0, label %60
]
60: ; preds = %57
%61 = tail call i32 (...) @run_child()
br label %62
62: ; preds = %60, %57
%63 = call i32 @await_children(i32* %9, i32 10)
%64 = icmp eq i32 %63, 10
br i1 %64, label %68, label %65
65: ; preds = %62
%66 = load i32, i32* @stderr
%67 = call i32 @fprintf(i32 %66, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
br label %68
68: ; preds = %65, %62
%69 = call i32 @read_and_print_results(i32 %5, i32* %9, i32 10)
%70 = icmp eq i32 %69, 10
br i1 %70, label %74, label %71
71: ; preds = %68
%72 = load i32, i32* @stderr
%73 = call i32 @fprintf(i32 %72, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
br label %74
74: ; preds = %71, %68
%75 = call i32 @close(i32 %5)
%76 = call i32 @unlink(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
ret i32 0
}