#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 */
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ alarm(int);
scalar_t__ fork();
int /*<<< orphan*/ fork_child();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, scalar_t__);
scalar_t__ wait(int *);
int main(void) {
  pid_t childs[10];
  int i;
  int status;
  int num_exited = 0;
  alarm(180);
  for (i = 0; i < 10; i++) {
    pid_t pid;
    pid = fork();
    if (pid > 0) {
      childs[i] = pid;
    } else if (pid == 0) {
      fork_child();
      return 0;
    } else {
      perror("fork");
      return 1;
    }
  }
  while (num_exited != 10) {
    pid_t pid = wait(&status);
    if (pid == -1) {
      perror("wait");
      return 1;
    }
    if (WIFEXITED(status)) {
      num_exited++;
    } else {
      printf("Hmm, unexpected wait status 0x%x from child %d\n", status, pid);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x6c(%rbp)
mov    $0xb4,%edi
call   1040 <alarm@plt>
movl   $0x0,-0x64(%rbp)
cmpl   $0xa,-0x64(%rbp)
jge    1215 <main+0xa5>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x78(%rbp)
cmpq   $0x0,-0x78(%rbp)
jle    11cc <main+0x5c>
mov    -0x78(%rbp),%rcx
movslq -0x64(%rbp),%rax
mov    %rcx,-0x60(%rbp,%rax,8)
jmp    1202 <main+0x92>
cmpq   $0x0,-0x78(%rbp)
jne    11ea <main+0x7a>
mov    $0x0,%al
call   1050 <fork_child@plt>
movl   $0x0,-0x4(%rbp)
jmp    1293 <main+0x123>
lea    0xe0f(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1293 <main+0x123>
jmp    1207 <main+0x97>
mov    -0x64(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x64(%rbp)
jmp    119a <main+0x2a>
jmp    121a <main+0xaa>
cmpl   $0xa,-0x6c(%rbp)
je     128c <main+0x11c>
lea    -0x68(%rbp),%rdi
call   1080 <wait@plt>
mov    %rax,-0x80(%rbp)
cmpq   $0xffffffffffffffff,-0x80(%rbp)
jne    1254 <main+0xe4>
lea    0xdc2(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1293 <main+0x123>
mov    -0x68(%rbp),%edi
call   1070 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1274 <main+0x104>
mov    -0x6c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x6c(%rbp)
jmp    1287 <main+0x117>
mov    -0x68(%rbp),%esi
mov    -0x80(%rbp),%rdx
lea    0xd88(%rip),%rdi
call   1030 <printf@plt>
jmp    121a <main+0xaa>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0xb4,%edi
call   1040 <alarm@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jle    1209 <main+0x99>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jle    1209 <main+0x99>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jle    1209 <main+0x99>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jle    1209 <main+0x99>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jle    1209 <main+0x99>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jle    1209 <main+0x99>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jle    1209 <main+0x99>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jle    1209 <main+0x99>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jle    1209 <main+0x99>
xor    %ebx,%ebx
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jle    1209 <main+0x99>
lea    0x4(%rsp),%r15
lea    0xe03(%rip),%r14
jmp    123a <main+0xca>
test   %rax,%rax
jne    125e <main+0xee>
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <fork_child@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x4(%rsp),%esi
mov    %r14,%rdi
mov    %rbp,%rdx
call   1030 <printf@plt>
xor    %ebp,%ebp
cmp    $0xa,%ebx
je     1217 <main+0xa7>
mov    %r15,%rdi
call   1080 <wait@plt>
cmp    $0xffffffffffffffff,%rax
je     1271 <main+0x101>
mov    %rax,%rbp
mov    0x4(%rsp),%edi
call   1070 <WIFEXITED@plt>
test   %rax,%rax
je     1224 <main+0xb4>
add    $0x1,%ebx
jmp    1233 <main+0xc3>
lea    0xd9b(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%ebp
jmp    1217 <main+0xa7>
lea    0xd8d(%rip),%rdi
jmp    1265 <main+0xf5>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@.str.1 = [5 x i8] c"wait\00"
@.str.2 = [48 x i8] c"Hmm, unexpected wait status 0x%x from child %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [10 x i64]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
store i32 0, i32* %1
store i32 0, i32* %5
%8 = call i32 @alarm(i32 180)
store i32 0, i32* %3
br label %9
9: ; preds = %29, %0
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 10
br i1 %11, label %12, label %32
12: ; preds = %9
%13 = call i64 (...) @fork()
store i64 %13, i64* %6
%14 = load i64, i64* %6
%15 = icmp sgt i64 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %12
%17 = load i64, i64* %6
%18 = load i32, i32* %3
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [10 x i64], [10 x i64]* %2, i64 0, i64 %19
store i64 %17, i64* %20
br label %28
21: ; preds = %12
%22 = load i64, i64* %6
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 (...) @fork_child()
store i32 0, i32* %1
br label %55
26: ; preds = %21
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %55
28: ; preds = %16
br label %29
29: ; preds = %28
%30 = load i32, i32* %3
%31 = add nsw i32 %30, 1
store i32 %31, i32* %3
br label %9
32: ; preds = %9
br label %33
33: ; preds = %53, %32
%34 = load i32, i32* %5
%35 = icmp ne i32 %34, 10
br i1 %35, label %36, label %54
36: ; preds = %33
%37 = call i64 @wait(i32* %4)
store i64 %37, i64* %7
%38 = load i64, i64* %7
%39 = icmp eq i64 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %55
42: ; preds = %36
%43 = load i32, i32* %4
%44 = call i64 @WIFEXITED(i32 %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = load i32, i32* %5
%48 = add nsw i32 %47, 1
store i32 %48, i32* %5
br label %53
49: ; preds = %42
%50 = load i32, i32* %4
%51 = load i64, i64* %7
%52 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i32 %50, i64 %51)
br label %53
53: ; preds = %49, %46
br label %33
54: ; preds = %33
store i32 0, i32* %1
br label %55
55: ; preds = %54, %40, %26, %24
%56 = load i32, i32* %1
ret i32 %56
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@.str.1 = [5 x i8] c"wait\00"
@.str.2 = [48 x i8] c"Hmm, unexpected wait status 0x%x from child %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 @alarm(i32 180)
%4 = tail call i64 (...) @fork()
%5 = icmp sgt i64 %4, 0
br i1 %5, label %6, label %33
6: ; preds = %0
%7 = tail call i64 (...) @fork()
%8 = icmp sgt i64 %7, 0
br i1 %8, label %9, label %33
9: ; preds = %6
%10 = tail call i64 (...) @fork()
%11 = icmp sgt i64 %10, 0
br i1 %11, label %12, label %33
12: ; preds = %9
%13 = tail call i64 (...) @fork()
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %33
15: ; preds = %12
%16 = tail call i64 (...) @fork()
%17 = icmp sgt i64 %16, 0
br i1 %17, label %18, label %33
18: ; preds = %15
%19 = tail call i64 (...) @fork()
%20 = icmp sgt i64 %19, 0
br i1 %20, label %21, label %33
21: ; preds = %18
%22 = tail call i64 (...) @fork()
%23 = icmp sgt i64 %22, 0
br i1 %23, label %24, label %33
24: ; preds = %21
%25 = tail call i64 (...) @fork()
%26 = icmp sgt i64 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %24
%28 = tail call i64 (...) @fork()
%29 = icmp sgt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = tail call i64 (...) @fork()
%32 = icmp sgt i64 %31, 0
br i1 %32, label %40, label %33
33: ; preds = %30, %27, %24, %21, %18, %15, %12, %9, %6, %0
%34 = phi i64 [ %4, %0 ], [ %7, %6 ], [ %10, %9 ], [ %13, %12 ], [ %16, %15 ], [ %19, %18 ], [ %22, %21 ], [ %25, %24 ], [ %28, %27 ], [ %31, %30 ]
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = tail call i32 (...) @fork_child()
br label %58
38: ; preds = %33
%39 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %58
40: ; preds = %30, %55
%41 = phi i32 [ %56, %55 ], [ 0, %30 ]
%42 = call i64 @wait(i32* %1)
%43 = icmp eq i64 %42, -1
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %58
46: ; preds = %40
%47 = load i32, i32* %1
%48 = call i64 @WIFEXITED(i32 %47)
%49 = icmp eq i64 %48, 0
br i1 %49, label %52, label %50
50: ; preds = %46
%51 = add nsw i32 %41, 1
br label %55
52: ; preds = %46
%53 = load i32, i32* %1
%54 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i32 %53, i64 %42)
br label %55
55: ; preds = %50, %52
%56 = phi i32 [ %51, %50 ], [ %41, %52 ]
%57 = icmp eq i32 %56, 10
br i1 %57, label %58, label %40
58: ; preds = %55, %36, %38, %44
%59 = phi i32 [ 1, %44 ], [ 0, %36 ], [ 1, %38 ], [ 0, %55 ]
ret i32 %59
}