#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 uint64_t;

/* Variables and functions */
int WNOHANG;
int __WALL;
int current_time_ms();
int /*<<< orphan*/ execute_one();
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ kill_and_wait(int, int *);
int /*<<< orphan*/ setup_test();
int /*<<< orphan*/ sleep_ms(int);
int waitpid(int, int *, int);
void loop(void) {
  int iter = 0;
  for (;; iter++) {
    int pid = fork();
    if (pid < 0)
      exit(1);
    if (pid == 0) {
      setup_test();
      execute_one();
      exit(0);
    }
    int status = 0;
    uint64_t start = current_time_ms();
    for (;;) {
      if (waitpid(-1, &status, WNOHANG | __WALL) == pid)
        break;
      sleep_ms(1);
      if (current_time_ms() - start < 5000) {
        continue;
      }
      kill_and_wait(pid, &status);
      break;
    }
  }
}


x86 assembly (clang, -O0):
<loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11ad <loop+0x2d>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    11cc <loop+0x4c>
mov    $0x0,%al
call   1070 <setup_test@plt>
mov    $0x0,%al
call   1080 <execute_one@plt>
xor    %edi,%edi
call   1090 <exit@plt>
movl   $0x0,-0xc(%rbp)
mov    $0x0,%al
call   1030 <current_time_ms@plt>
mov    %eax,-0x10(%rbp)
mov    0x2e04(%rip),%rax
mov    (%rax),%edx
mov    0x2deb(%rip),%rax
or     (%rax),%edx
mov    $0xffffffff,%edi
lea    -0xc(%rbp),%rsi
call   1060 <waitpid@plt>
cmp    -0x8(%rbp),%eax
jne    120b <loop+0x8b>
jmp    123b <loop+0xbb>
mov    $0x1,%edi
call   1040 <sleep_ms@plt>
mov    $0x0,%al
call   1030 <current_time_ms@plt>
sub    -0x10(%rbp),%eax
cmp    $0x1388,%eax
jge    122f <loop+0xaf>
jmp    11dd <loop+0x5d>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1050 <kill_and_wait@plt>
jmp    1240 <loop+0xc0>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    118f <loop+0xf>

x86 assembly (clang, -O2):
<loop>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
jns    11a1 <loop+0x21>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    %eax,%ebp
mov    0x2e3e(%rip),%r15
mov    0x2e27(%rip),%r12
lea    0xc(%rsp),%r14
jmp    11cd <loop+0x4d>
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1197 <loop+0x17>
test   %ebp,%ebp
je     122e <loop+0xae>
movl   $0x0,0xc(%rsp)
xor    %eax,%eax
call   1030 <current_time_ms@plt>
mov    %eax,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r12),%edx
or     (%r15),%edx
mov    $0xffffffff,%edi
mov    %r14,%rsi
call   1060 <waitpid@plt>
cmp    %ebp,%eax
je     11c0 <loop+0x40>
mov    $0x1,%edi
call   1040 <sleep_ms@plt>
xor    %eax,%eax
call   1030 <current_time_ms@plt>
sub    %ebx,%eax
cmp    $0x1388,%eax
jl     11f0 <loop+0x70>
mov    %ebp,%edi
mov    %r14,%rsi
call   1050 <kill_and_wait@plt>
jmp    11c0 <loop+0x40>
xor    %eax,%eax
call   1070 <setup_test@plt>
xor    %eax,%eax
call   1080 <execute_one@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@WNOHANG = global i32 0
@__WALL = global i32 0
define void @loop() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
br label %5
5: ; preds = %39, %0
%6 = call i32 (...) @fork()
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %5
%12 = load i32, i32* %2
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = call i32 (...) @setup_test()
%16 = call i32 (...) @execute_one()
%17 = call i32 @exit(i32 0)
unreachable
18: ; preds = %11
store i32 0, i32* %3
%19 = call i32 (...) @current_time_ms()
store i32 %19, i32* %4
br label %20
20: ; preds = %34, %18
%21 = load i32, i32* @WNOHANG
%22 = load i32, i32* @__WALL
%23 = or i32 %21, %22
%24 = call i32 @waitpid(i32 -1, i32* %3, i32 %23)
%25 = load i32, i32* %2
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %20
br label %38
28: ; preds = %20
%29 = call i32 @sleep_ms(i32 1)
%30 = call i32 (...) @current_time_ms()
%31 = load i32, i32* %4
%32 = sub nsw i32 %30, %31
%33 = icmp slt i32 %32, 5000
br i1 %33, label %34, label %35
34: ; preds = %28
br label %20
35: ; preds = %28
%36 = load i32, i32* %2
%37 = call i32 @kill_and_wait(i32 %36, i32* %3)
br label %38
38: ; preds = %35, %27
br label %39
39: ; preds = %38
%40 = load i32, i32* %1
%41 = add nsw i32 %40, 1
store i32 %41, i32* %1
br label %5
}

LLVM IR (-O2):
@WNOHANG = global i32 0
@__WALL = global i32 0
define void @loop() {
%1 = alloca i32
%2 = call i32 (...) @fork()
%3 = icmp slt i32 %2, 0
br i1 %3, label %6, label %4
4: ; preds = %0
%5 = bitcast i32* %1 to i8*
br label %8
6: ; preds = %30, %0
%7 = call i32 @exit(i32 1)
unreachable
8: ; preds = %4, %30
%9 = phi i32 [ %2, %4 ], [ %31, %30 ]
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = call i32 (...) @setup_test()
%13 = call i32 (...) @execute_one()
%14 = call i32 @exit(i32 0)
unreachable
15: ; preds = %8
store i32 0, i32* %1
%16 = call i32 (...) @current_time_ms()
br label %17
17: ; preds = %23, %15
%18 = load i32, i32* @WNOHANG
%19 = load i32, i32* @__WALL
%20 = or i32 %19, %18
%21 = call i32 @waitpid(i32 -1, i32* %1, i32 %20)
%22 = icmp eq i32 %21, %9
br i1 %22, label %30, label %23
23: ; preds = %17
%24 = call i32 @sleep_ms(i32 1)
%25 = call i32 (...) @current_time_ms()
%26 = sub nsw i32 %25, %16
%27 = icmp slt i32 %26, 5000
br i1 %27, label %17, label %28
28: ; preds = %23
%29 = call i32 @kill_and_wait(i32 %9, i32* %1)
br label %30
30: ; preds = %17, %28
%31 = call i32 (...) @fork()
%32 = icmp slt i32 %31, 0
br i1 %32, label %6, label %8
}