#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 WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ test();
int /*<<< orphan*/ wait(int *);
int main(void) {
  for (int i = 0; i < 300; i++) {
    pid_t p = fork();
    if (p == -1) {
      printf("fork failed: %m\n");
      return 1;
    }
    if (p == 0)
      _exit(test());
    int status;
    wait(&status);
    if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
      printf("child exited with %d\n", WEXITSTATUS(status));
      return 1;
    } else if (WIFSIGNALED(status)) {
      printf("child killed by signal %d\n", WTERMSIG(status));
      return 1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x12c,-0x8(%rbp)
jge    1297 <main+0x107>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11e1 <main+0x51>
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    129e <main+0x10e>
cmpl   $0x0,-0xc(%rbp)
jne    11f9 <main+0x69>
mov    $0x0,%al
call   10b0 <test@plt>
mov    %eax,%edi
call   1030 <_exit@plt>
lea    -0x10(%rbp),%rdi
call   1090 <wait@plt>
mov    -0x10(%rbp),%edi
call   1080 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1249 <main+0xb9>
mov    -0x10(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
cmp    $0x0,%eax
je     1249 <main+0xb9>
mov    -0x10(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    129e <main+0x10e>
mov    -0x10(%rbp),%edi
call   1050 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     127f <main+0xef>
mov    -0x10(%rbp),%edi
call   1060 <WTERMSIG@plt>
mov    %eax,%esi
lea    0xdbb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    129e <main+0x10e>
jmp    1284 <main+0xf4>
jmp    1289 <main+0xf9>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11a6 <main+0x16>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    $0x12c,%ebp
lea    0x4(%rsp),%rbx
nopl   (%rax)
xor    %eax,%eax
call   10a0 <fork@plt>
cmp    $0xffffffff,%eax
je     11ee <main+0x5e>
test   %eax,%eax
je     1235 <main+0xa5>
mov    %rbx,%rdi
call   1090 <wait@plt>
mov    0x4(%rsp),%edi
call   1080 <WIFEXITED@plt>
test   %rax,%rax
je     11d7 <main+0x47>
mov    0x4(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
test   %eax,%eax
jne    1210 <main+0x80>
mov    0x4(%rsp),%edi
call   1050 <WIFSIGNALED@plt>
test   %rax,%rax
jne    11fe <main+0x6e>
add    $0xffffffff,%ebp
jne    11a0 <main+0x10>
xor    %eax,%eax
jmp    122e <main+0x9e>
lea    0xe0b(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1229 <main+0x99>
mov    0x4(%rsp),%edi
call   1060 <WTERMSIG@plt>
lea    0xe19(%rip),%rdi
jmp    1220 <main+0x90>
mov    0x4(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
lea    0xdf1(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
xor    %eax,%eax
call   10b0 <test@plt>
mov    %eax,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"fork failed: %m\0A\00"
@.str.1 = [22 x i8] c"child exited with %d\0A\00"
@.str.2 = [27 x i8] c"child killed by signal %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
br label %5
5: ; preds = %43, %0
%6 = load i32, i32* %2
%7 = icmp slt i32 %6, 300
br i1 %7, label %8, label %46
8: ; preds = %5
%9 = call i32 (...) @fork()
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %47
14: ; preds = %8
%15 = load i32, i32* %3
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = call i32 (...) @test()
%19 = call i32 @_exit(i32 %18)
unreachable
20: ; preds = %14
%21 = call i32 @wait(i32* %4)
%22 = load i32, i32* %4
%23 = call i64 @WIFEXITED(i32 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %33
25: ; preds = %20
%26 = load i32, i32* %4
%27 = call i32 @WEXITSTATUS(i32 %26)
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = load i32, i32* %4
%31 = call i32 @WEXITSTATUS(i32 %30)
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %31)
store i32 1, i32* %1
br label %47
33: ; preds = %25, %20
%34 = load i32, i32* %4
%35 = call i64 @WIFSIGNALED(i32 %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %33
%38 = load i32, i32* %4
%39 = call i32 @WTERMSIG(i32 %38)
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %39)
store i32 1, i32* %1
br label %47
41: ; preds = %33
br label %42
42: ; preds = %41
br label %43
43: ; preds = %42
%44 = load i32, i32* %2
%45 = add nsw i32 %44, 1
store i32 %45, i32* %2
br label %5
46: ; preds = %5
store i32 0, i32* %1
br label %47
47: ; preds = %46, %37, %29, %12
%48 = load i32, i32* %1
ret i32 %48
}

LLVM IR (-O2):
@.str = [17 x i8] c"fork failed: %m\0A\00"
@.str.1 = [22 x i8] c"child exited with %d\0A\00"
@.str.2 = [27 x i8] c"child killed by signal %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
br label %3
3: ; preds = %0, %33
%4 = phi i32 [ 0, %0 ], [ %34, %33 ]
%5 = call i32 (...) @fork()
switch i32 %5, label %11 [
i32 -1, label %6
i32 0, label %8
]
6: ; preds = %3
%7 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %36
8: ; preds = %3
%9 = call i32 (...) @test()
%10 = call i32 @_exit(i32 %9)
unreachable
11: ; preds = %3
%12 = call i32 @wait(i32* %1)
%13 = load i32, i32* %1
%14 = call i64 @WIFEXITED(i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %24, label %16
16: ; preds = %11
%17 = load i32, i32* %1
%18 = call i32 @WEXITSTATUS(i32 %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %16
%21 = load i32, i32* %1
%22 = call i32 @WEXITSTATUS(i32 %21)
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %22)
br label %32
24: ; preds = %16, %11
%25 = load i32, i32* %1
%26 = call i64 @WIFSIGNALED(i32 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %33, label %28
28: ; preds = %24
%29 = load i32, i32* %1
%30 = call i32 @WTERMSIG(i32 %29)
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %30)
br label %32
32: ; preds = %20, %28
br label %36
33: ; preds = %24
%34 = add nuw nsw i32 %4, 1
%35 = icmp eq i32 %34, 300
br i1 %35, label %36, label %3
36: ; preds = %33, %32, %6
%37 = phi i32 [ 1, %32 ], [ 1, %6 ], [ 0, %33 ]
ret i32 %37
}