#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 /*<<< orphan*/ PR_SET_PDEATHSIG;
int /*<<< orphan*/ SIGKILL;
int WNOHANG;
int __WALL;
scalar_t__ chdir(char *);
int current_time_ms();
int /*<<< orphan*/ doexit(int /*<<< orphan*/);
int /*<<< orphan*/ fail(char *);
int fork();
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
scalar_t__ mkdir(char *, int);
int /*<<< orphan*/ prctl(int /*<<< orphan*/, int /*<<< orphan*/,
                         int /*<<< orphan*/, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ remove_dir(char *);
int /*<<< orphan*/ setpgrp();
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ test();
int /*<<< orphan*/ usleep(int);
int waitpid(int, int *, int);
void loop() {
  int iter;
  for (iter = 0;; iter++) {
    char cwdbuf[256];
    sprintf(cwdbuf, "./%d", iter);
    if (mkdir(cwdbuf, 0777))
      fail("failed to mkdir");
    int pid = fork();
    if (pid < 0)
      fail("clone failed");
    if (pid == 0) {
      prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
      setpgrp();
      if (chdir(cwdbuf))
        fail("failed to chdir");
      test();
      doexit(0);
    }
    int status = 0;
    uint64_t start = current_time_ms();
    for (;;) {
      int res = waitpid(-1, &status, __WALL | WNOHANG);
      if (res == pid)
        break;
      usleep(1000);
      if (current_time_ms() - start > 5 * 1000) {
        kill(-pid, SIGKILL);
        kill(pid, SIGKILL);
        while (waitpid(-1, &status, __WALL) != pid) {
        }
        break;
      }
    }
    remove_dir(cwdbuf);
  }
}


x86 assembly (clang, -O0):
<loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x110(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xdfd(%rip),%rsi
call   10c0 <sprintf@plt>
lea    -0x110(%rbp),%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
je     122f <loop+0x4f>
lea    0xddb(%rip),%rdi
call   1040 <fail@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0x0,-0x114(%rbp)
jge    1255 <loop+0x75>
lea    0xdc5(%rip),%rdi
call   1040 <fail@plt>
cmpl   $0x0,-0x114(%rbp)
jne    12b9 <loop+0xd9>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
mov    0x2d56(%rip),%rax
mov    (%rax),%esi
xor    %r8d,%r8d
mov    %r8d,%edx
mov    %r8d,%ecx
call   1080 <prctl@plt>
mov    $0x0,%al
call   10d0 <setpgrp@plt>
lea    -0x110(%rbp),%rdi
call   1060 <chdir@plt>
cmp    $0x0,%rax
je     12ab <loop+0xcb>
lea    0xd7c(%rip),%rdi
call   1040 <fail@plt>
mov    $0x0,%al
call   1100 <test@plt>
xor    %edi,%edi
call   1070 <doexit@plt>
movl   $0x0,-0x118(%rbp)
mov    $0x0,%al
call   1050 <current_time_ms@plt>
mov    %eax,-0x11c(%rbp)
mov    0x2cf9(%rip),%rax
mov    (%rax),%edx
mov    0x2d08(%rip),%rax
or     (%rax),%edx
mov    $0xffffffff,%edi
lea    -0x118(%rbp),%rsi
call   10b0 <waitpid@plt>
mov    %eax,-0x120(%rbp)
mov    -0x120(%rbp),%eax
cmp    -0x114(%rbp),%eax
jne    1310 <loop+0x130>
jmp    1391 <loop+0x1b1>
mov    $0x3e8,%edi
call   10f0 <usleep@plt>
mov    $0x0,%al
call   1050 <current_time_ms@plt>
sub    -0x11c(%rbp),%eax
cmp    $0x1388,%eax
jle    138c <loop+0x1ac>
xor    %edi,%edi
sub    -0x114(%rbp),%edi
mov    0x2c87(%rip),%rax
mov    (%rax),%esi
call   1090 <kill@plt>
mov    -0x114(%rbp),%edi
mov    0x2c73(%rip),%rax
mov    (%rax),%esi
call   1090 <kill@plt>
mov    0x2c6d(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
lea    -0x118(%rbp),%rsi
call   10b0 <waitpid@plt>
cmp    -0x114(%rbp),%eax
je     1387 <loop+0x1a7>
jmp    135c <loop+0x17c>
jmp    1391 <loop+0x1b1>
jmp    12d0 <loop+0xf0>
lea    -0x110(%rbp),%rdi
call   10a0 <remove_dir@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    11f2 <loop+0x12>

x86 assembly (clang, -O2):
<loop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
lea    0x10(%rsp),%r13
mov    0x2dd3(%rip),%r14
mov    0x2de4(%rip),%r12
lea    0xc(%rsp),%rbx
xor    %edx,%edx
jmp    121f <loop+0x3f>
nopl   (%rax)
mov    %r13,%rdi
call   10a0 <remove_dir@plt>
mov    0x8(%rsp),%edx
add    $0x1,%edx
mov    %r13,%rdi
lea    0xdd7(%rip),%rsi
mov    %edx,0x8(%rsp)
call   10c0 <sprintf@plt>
mov    %r13,%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
test   %rax,%rax
je     1250 <loop+0x70>
lea    0xdba(%rip),%rdi
call   1040 <fail@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
mov    %eax,%r15d
test   %eax,%eax
js     12b0 <loop+0xd0>
jne    12bc <loop+0xdc>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
mov    0x2d58(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <prctl@plt>
xor    %eax,%eax
call   10d0 <setpgrp@plt>
mov    %r13,%rdi
call   1060 <chdir@plt>
test   %rax,%rax
je     129e <loop+0xbe>
lea    0xd89(%rip),%rdi
call   1040 <fail@plt>
xor    %eax,%eax
call   1100 <test@plt>
xor    %edi,%edi
call   1070 <doexit@plt>
jmp    12bc <loop+0xdc>
xchg   %ax,%ax
lea    0xd5e(%rip),%rdi
call   1040 <fail@plt>
movl   $0x0,0xc(%rsp)
xor    %eax,%eax
call   1050 <current_time_ms@plt>
mov    %eax,%ebp
nopl   (%rax)
mov    (%r12),%edx
or     (%r14),%edx
mov    $0xffffffff,%edi
mov    %rbx,%rsi
call   10b0 <waitpid@plt>
cmp    %r15d,%eax
je     1210 <loop+0x30>
mov    $0x3e8,%edi
call   10f0 <usleep@plt>
xor    %eax,%eax
call   1050 <current_time_ms@plt>
sub    %ebp,%eax
cmp    $0x1389,%eax
jl     12d0 <loop+0xf0>
mov    %r15d,%edi
neg    %edi
mov    0x2cb5(%rip),%rbp
mov    0x0(%rbp),%esi
call   1090 <kill@plt>
mov    0x0(%rbp),%esi
mov    %r15d,%edi
call   1090 <kill@plt>
cs nopw 0x0(%rax,%rax,1)
mov    (%r14),%edx
mov    $0xffffffff,%edi
mov    %rbx,%rsi
call   10b0 <waitpid@plt>
cmp    %r15d,%eax
jne    1330 <loop+0x150>
jmp    1210 <loop+0x30>

LLVM IR (-O0):
@.str = [5 x i8] c"./%d\00"
@.str.1 = [16 x i8] c"failed to mkdir\00"
@.str.2 = [13 x i8] c"clone failed\00"
@PR_SET_PDEATHSIG = global i32 0
@SIGKILL = global i32 0
@.str.3 = [16 x i8] c"failed to chdir\00"
@__WALL = global i32 0
@WNOHANG = global i32 0
define void @loop() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
br label %7
7: ; preds = %74, %0
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%9 = load i32, i32* %1
%10 = call i32 @sprintf(i8* %8, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%12 = call i64 @mkdir(i8* %11, i32 511)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %7
%15 = call i32 @fail(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %7
%17 = call i32 (...) @fork()
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @fail(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %20, %16
%23 = load i32, i32* %3
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %38
25: ; preds = %22
%26 = load i32, i32* @PR_SET_PDEATHSIG
%27 = load i32, i32* @SIGKILL
%28 = call i32 @prctl(i32 %26, i32 %27, i32 0, i32 0, i32 0)
%29 = call i32 (...) @setpgrp()
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%31 = call i64 @chdir(i8* %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %25
%34 = call i32 @fail(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %35
35: ; preds = %33, %25
%36 = call i32 (...) @test()
%37 = call i32 @doexit(i32 0)
br label %38
38: ; preds = %35, %22
store i32 0, i32* %4
%39 = call i32 (...) @current_time_ms()
store i32 %39, i32* %5
br label %40
40: ; preds = %70, %38
%41 = load i32, i32* @__WALL
%42 = load i32, i32* @WNOHANG
%43 = or i32 %41, %42
%44 = call i32 @waitpid(i32 -1, i32* %4, i32 %43)
store i32 %44, i32* %6
%45 = load i32, i32* %6
%46 = load i32, i32* %3
%47 = icmp eq i32 %45, %46
br i1 %47, label %48, label %49
48: ; preds = %40
br label %71
49: ; preds = %40
%50 = call i32 @usleep(i32 1000)
%51 = call i32 (...) @current_time_ms()
%52 = load i32, i32* %5
%53 = sub nsw i32 %51, %52
%54 = icmp sgt i32 %53, 5000
br i1 %54, label %55, label %70
55: ; preds = %49
%56 = load i32, i32* %3
%57 = sub nsw i32 0, %56
%58 = load i32, i32* @SIGKILL
%59 = call i32 @kill(i32 %57, i32 %58)
%60 = load i32, i32* %3
%61 = load i32, i32* @SIGKILL
%62 = call i32 @kill(i32 %60, i32 %61)
br label %63
63: ; preds = %68, %55
%64 = load i32, i32* @__WALL
%65 = call i32 @waitpid(i32 -1, i32* %4, i32 %64)
%66 = load i32, i32* %3
%67 = icmp ne i32 %65, %66
br i1 %67, label %68, label %69
68: ; preds = %63
br label %63
69: ; preds = %63
br label %71
70: ; preds = %49
br label %40
71: ; preds = %69, %48
%72 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%73 = call i32 @remove_dir(i8* %72)
br label %74
74: ; preds = %71
%75 = load i32, i32* %1
%76 = add nsw i32 %75, 1
store i32 %76, i32* %1
br label %7
}

LLVM IR (-O2):
@.str = [5 x i8] c"./%d\00"
@.str.1 = [16 x i8] c"failed to mkdir\00"
@.str.2 = [13 x i8] c"clone failed\00"
@PR_SET_PDEATHSIG = global i32 0
@SIGKILL = global i32 0
@.str.3 = [16 x i8] c"failed to chdir\00"
@__WALL = global i32 0
@WNOHANG = global i32 0
define void @loop() {
%1 = alloca [256 x i8]
%2 = alloca i32
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%4 = bitcast i32* %2 to i8*
br label %5
5: ; preds = %54, %0
%6 = phi i32 [ 0, %0 ], [ %56, %54 ]
%7 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = call i64 @mkdir(i8* %3, i32 511)
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %5
%11 = call i32 @fail(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %5
%13 = call i32 (...) @fork()
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @fail(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %31
17: ; preds = %12
%18 = icmp eq i32 %13, 0
br i1 %18, label %19, label %31
19: ; preds = %17
%20 = load i32, i32* @PR_SET_PDEATHSIG
%21 = load i32, i32* @SIGKILL
%22 = call i32 @prctl(i32 %20, i32 %21, i32 0, i32 0, i32 0)
%23 = call i32 (...) @setpgrp()
%24 = call i64 @chdir(i8* %3)
%25 = icmp eq i64 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %19
%27 = call i32 @fail(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %28
28: ; preds = %26, %19
%29 = call i32 (...) @test()
%30 = call i32 @doexit(i32 0)
br label %31
31: ; preds = %15, %28, %17
store i32 0, i32* %2
%32 = call i32 (...) @current_time_ms()
br label %33
33: ; preds = %39, %31
%34 = load i32, i32* @__WALL
%35 = load i32, i32* @WNOHANG
%36 = or i32 %35, %34
%37 = call i32 @waitpid(i32 -1, i32* %2, i32 %36)
%38 = icmp eq i32 %37, %13
br i1 %38, label %54, label %39
39: ; preds = %33
%40 = call i32 @usleep(i32 1000)
%41 = call i32 (...) @current_time_ms()
%42 = sub nsw i32 %41, %32
%43 = icmp sgt i32 %42, 5000
br i1 %43, label %44, label %33
44: ; preds = %39
%45 = sub nsw i32 0, %13
%46 = load i32, i32* @SIGKILL
%47 = call i32 @kill(i32 %45, i32 %46)
%48 = load i32, i32* @SIGKILL
%49 = call i32 @kill(i32 %13, i32 %48)
br label %50
50: ; preds = %50, %44
%51 = load i32, i32* @__WALL
%52 = call i32 @waitpid(i32 -1, i32* %2, i32 %51)
%53 = icmp eq i32 %52, %13
br i1 %53, label %54, label %50
54: ; preds = %33, %50
%55 = call i32 @remove_dir(i8* %3)
%56 = add nuw nsw i32 %6, 1
br label %5
}