#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 /*<<< orphan*/ SIGUSR1;
int WEXITSTATUS(int);
int /*<<< orphan*/ _SC_NPROCESSORS_CONF;
int *calloc(long, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int const);
int /*<<< orphan*/ stderr;
int sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ test_dirs(char const *, int const);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int test_exercise(const char *path, const int max_dirs, const int duration) {
  int i;
  long threads = sysconf(_SC_NPROCESSORS_CONF) * (8);
  pid_t *pids;
  int ret = (0);
  if ((pids = calloc(threads, sizeof(pid_t))) == NULL) {
    fprintf(stderr, "failed to calloc pids\n");
    return (2);
  }
  for (i = 0; i < threads; i++) {
    pid_t pid;
    pid = fork();
    switch (pid) {
    case -1:
      fprintf(stderr, "failed to fork child %d of %ld\n", i + 1, threads);
      break;
    case 0:
      exit(test_dirs(path, max_dirs));
    default:
      pids[i] = pid;
      break;
    }
  }
  sleep(duration);
  for (i = 0; i < threads; i++)
    kill(pids[i], SIGUSR1);
  for (i = 0; i < threads; i++) {
    int status;
    waitpid(pids[i], &status, 0);
    if (WEXITSTATUS(status) != (0))
      ret = WEXITSTATUS(status);
  }
  free(pids);
  return ret;
}


x86 assembly (clang, -O0):
<test_exercise>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
call   10a0 <sysconf@plt>
shl    $0x3,%eax
cltq
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x4,%esi
call   1040 <calloc@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    121f <test_exercise+0x6f>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
lea    0xdf4(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1369 <test_exercise+0x1b9>
movl   $0x0,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    -0x28(%rbp),%rax
jge    12bc <test_exercise+0x10c>
xor    %eax,%eax
call   10d0 <fork@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x40(%rbp)
sub    $0xffffffff,%eax
je     1262 <test_exercise+0xb2>
jmp    1252 <test_exercise+0xa2>
mov    -0x40(%rbp),%eax
test   %eax,%eax
je     1288 <test_exercise+0xd8>
jmp    129b <test_exercise+0xeb>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
add    $0x1,%edx
mov    -0x28(%rbp),%rcx
lea    0xd9b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    12a9 <test_exercise+0xf9>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <test_dirs@plt>
mov    %eax,%edi
call   10b0 <exit@plt>
mov    -0x38(%rbp),%edx
mov    -0x30(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
jmp    12ae <test_exercise+0xfe>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1226 <test_exercise+0x76>
mov    -0x18(%rbp),%edi
call   10c0 <sleep@plt>
movl   $0x0,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    -0x28(%rbp),%rax
jge    1300 <test_exercise+0x150>
mov    -0x30(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    0x2cfd(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    12cb <test_exercise+0x11b>
movl   $0x0,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    -0x28(%rbp),%rax
jge    135a <test_exercise+0x1aa>
mov    -0x30(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
lea    -0x3c(%rbp),%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
mov    -0x3c(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
cmp    $0x0,%eax
je     1347 <test_exercise+0x197>
mov    -0x3c(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
mov    %eax,-0x34(%rbp)
jmp    134c <test_exercise+0x19c>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1307 <test_exercise+0x157>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_exercise>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,%ebp
mov    %esi,%r12d
mov    %rdi,%r13
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   10a0 <sysconf@plt>
mov    %eax,%ebx
lea    0x0(,%rbx,8),%eax
movslq %eax,%r14
mov    %r14,%rdi
mov    $0x4,%esi
call   1040 <calloc@plt>
test   %rax,%rax
je     1311 <test_exercise+0x161>
mov    %rax,%r15
test   %ebx,%ebx
jle    132f <test_exercise+0x17f>
mov    %r13,0x18(%rsp)
mov    %r12d,0x10(%rsp)
mov    %ebp,0x14(%rsp)
mov    %rbx,0x20(%rsp)
cmp    $0x2,%r14
mov    $0x1,%ebp
cmovge %r14,%rbp
mov    0x2db0(%rip),%rbx
lea    0xde8(%rip),%r12
xor    %r13d,%r13d
jmp    125b <test_exercise+0xab>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%rbx),%edi
add    $0x1,%r13
mov    %r12,%rsi
mov    %r13d,%edx
mov    %r14,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
cmp    %rbp,%r13
je     127c <test_exercise+0xcc>
xor    %eax,%eax
call   10d0 <fork@plt>
cmp    $0xffffffff,%eax
je     1240 <test_exercise+0x90>
test   %eax,%eax
je     1351 <test_exercise+0x1a1>
mov    %eax,(%r15,%r13,4)
add    $0x1,%r13
cmp    %rbp,%r13
jne    125b <test_exercise+0xab>
mov    0x14(%rsp),%edi
call   10c0 <sleep@plt>
mov    0x20(%rsp),%r13
test   %r13d,%r13d
jle    1336 <test_exercise+0x186>
xor    %ebx,%ebx
mov    0x2d4c(%rip),%r12
nopl   0x0(%rax)
mov    (%r15,%rbx,4),%edi
mov    (%r12),%esi
call   1060 <kill@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    12a0 <test_exercise+0xf0>
test   %r13d,%r13d
jle    1336 <test_exercise+0x186>
cmp    $0x2,%r14
mov    $0x1,%r12d
cmovge %r14,%r12
xor    %ebp,%ebp
lea    0xc(%rsp),%r14
xor    %ebx,%ebx
jmp    12e9 <test_exercise+0x139>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%rbp
cmp    %rbp,%r12
je     1338 <test_exercise+0x188>
mov    (%r15,%rbp,4),%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
mov    0xc(%rsp),%edi
call   1080 <WEXITSTATUS@plt>
test   %eax,%eax
je     12e0 <test_exercise+0x130>
mov    0xc(%rsp),%edi
call   1080 <WEXITSTATUS@plt>
mov    %eax,%ebx
jmp    12e0 <test_exercise+0x130>
mov    0x2cc0(%rip),%rax
mov    (%rax),%edi
lea    0xcdf(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x2,%ebx
jmp    1340 <test_exercise+0x190>
mov    %ebp,%edi
call   10c0 <sleep@plt>
xor    %ebx,%ebx
mov    %r15,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x18(%rsp),%rdi
mov    0x10(%rsp),%esi
call   1070 <test_dirs@plt>
mov    %eax,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@_SC_NPROCESSORS_CONF = global i32 0
@stderr = global i32 0
@.str = [23 x i8] c"failed to calloc pids\0A\00"
@.str.1 = [32 x i8] c"failed to fork child %d of %ld\0A\00"
@SIGUSR1 = global i32 0
define i32 @test_exercise(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%14 = load i32, i32* @_SC_NPROCESSORS_CONF
%15 = call i32 @sysconf(i32 %14)
%16 = mul nsw i32 %15, 8
%17 = sext i32 %16 to i64
store i64 %17, i64* %9
store i32 0, i32* %11
%18 = load i64, i64* %9
%19 = call i32* @calloc(i64 %18, i32 4)
store i32* %19, i32** %10
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %24
21: ; preds = %3
%22 = load i32, i32* @stderr
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %4
br label %100
24: ; preds = %3
store i32 0, i32* %8
br label %25
25: ; preds = %51, %24
%26 = load i32, i32* %8
%27 = sext i32 %26 to i64
%28 = load i64, i64* %9
%29 = icmp slt i64 %27, %28
br i1 %29, label %30, label %54
30: ; preds = %25
%31 = call i32 (...) @fork()
store i32 %31, i32* %12
%32 = load i32, i32* %12
switch i32 %32, label %44 [
i32 -1, label %33
i32 0, label %39
]
33: ; preds = %30
%34 = load i32, i32* @stderr
%35 = load i32, i32* %8
%36 = add nsw i32 %35, 1
%37 = load i64, i64* %9
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %36, i64 %37)
br label %50
39: ; preds = %30
%40 = load i8*, i8** %5
%41 = load i32, i32* %6
%42 = call i32 @test_dirs(i8* %40, i32 %41)
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %30
%45 = load i32, i32* %12
%46 = load i32*, i32** %10
%47 = load i32, i32* %8
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i32, i32* %46, i64 %48
store i32 %45, i32* %49
br label %50
50: ; preds = %44, %33
br label %51
51: ; preds = %50
%52 = load i32, i32* %8
%53 = add nsw i32 %52, 1
store i32 %53, i32* %8
br label %25
54: ; preds = %25
%55 = load i32, i32* %7
%56 = call i32 @sleep(i32 %55)
store i32 0, i32* %8
br label %57
57: ; preds = %70, %54
%58 = load i32, i32* %8
%59 = sext i32 %58 to i64
%60 = load i64, i64* %9
%61 = icmp slt i64 %59, %60
br i1 %61, label %62, label %73
62: ; preds = %57
%63 = load i32*, i32** %10
%64 = load i32, i32* %8
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i32, i32* %63, i64 %65
%67 = load i32, i32* %66
%68 = load i32, i32* @SIGUSR1
%69 = call i32 @kill(i32 %67, i32 %68)
br label %70
70: ; preds = %62
%71 = load i32, i32* %8
%72 = add nsw i32 %71, 1
store i32 %72, i32* %8
br label %57
73: ; preds = %57
store i32 0, i32* %8
br label %74
74: ; preds = %93, %73
%75 = load i32, i32* %8
%76 = sext i32 %75 to i64
%77 = load i64, i64* %9
%78 = icmp slt i64 %76, %77
br i1 %78, label %79, label %96
79: ; preds = %74
%80 = load i32*, i32** %10
%81 = load i32, i32* %8
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds i32, i32* %80, i64 %82
%84 = load i32, i32* %83
%85 = call i32 @waitpid(i32 %84, i32* %13, i32 0)
%86 = load i32, i32* %13
%87 = call i32 @WEXITSTATUS(i32 %86)
%88 = icmp ne i32 %87, 0
br i1 %88, label %89, label %92
89: ; preds = %79
%90 = load i32, i32* %13
%91 = call i32 @WEXITSTATUS(i32 %90)
store i32 %91, i32* %11
br label %92
92: ; preds = %89, %79
br label %93
93: ; preds = %92
%94 = load i32, i32* %8
%95 = add nsw i32 %94, 1
store i32 %95, i32* %8
br label %74
96: ; preds = %74
%97 = load i32*, i32** %10
%98 = call i32 @free(i32* %97)
%99 = load i32, i32* %11
store i32 %99, i32* %4
br label %100
100: ; preds = %96, %21
%101 = load i32, i32* %4
ret i32 %101
}

LLVM IR (-O2):
@_SC_NPROCESSORS_CONF = global i32 0
@stderr = global i32 0
@.str = [23 x i8] c"failed to calloc pids\0A\00"
@.str.1 = [32 x i8] c"failed to fork child %d of %ld\0A\00"
@SIGUSR1 = global i32 0
define i32 @test_exercise(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = load i32, i32* @_SC_NPROCESSORS_CONF
%6 = tail call i32 @sysconf(i32 %5)
%7 = shl nsw i32 %6, 3
%8 = sext i32 %7 to i64
%9 = tail call i32* @calloc(i64 %8, i32 4)
%10 = icmp eq i32* %9, null
br i1 %10, label %17, label %11
11: ; preds = %3
%12 = icmp sgt i32 %6, 0
br i1 %12, label %15, label %13
13: ; preds = %11
%14 = tail call i32 @sleep(i32 %2)
br label %69
15: ; preds = %11
%16 = call i64 @llvm.smax.i64(i64 %8, i64 1)
br label %20
17: ; preds = %3
%18 = load i32, i32* @stderr
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %72
20: ; preds = %15, %34
%21 = phi i64 [ 0, %15 ], [ %35, %34 ]
%22 = tail call i32 (...) @fork()
switch i32 %22, label %31 [
i32 -1, label %23
i32 0, label %28
]
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = add nuw nsw i64 %21, 1
%26 = trunc i64 %25 to i32
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %26, i64 %8)
br label %34
28: ; preds = %20
%29 = tail call i32 @test_dirs(i8* %0, i32 %1)
%30 = tail call i32 @exit(i32 %29)
unreachable
31: ; preds = %20
%32 = getelementptr inbounds i32, i32* %9, i64 %21
store i32 %22, i32* %32
%33 = add nuw nsw i64 %21, 1
br label %34
34: ; preds = %31, %23
%35 = phi i64 [ %33, %31 ], [ %25, %23 ]
%36 = icmp eq i64 %35, %16
br i1 %36, label %37, label %20
37: ; preds = %34
%38 = tail call i32 @sleep(i32 %2)
br i1 %12, label %39, label %69
39: ; preds = %37
%40 = call i64 @llvm.smax.i64(i64 %8, i64 1)
br label %45
41: ; preds = %45
br i1 %12, label %42, label %69
42: ; preds = %41
%43 = bitcast i32* %4 to i8*
%44 = call i64 @llvm.smax.i64(i64 %8, i64 1)
br label %53
45: ; preds = %39, %45
%46 = phi i64 [ 0, %39 ], [ %51, %45 ]
%47 = getelementptr inbounds i32, i32* %9, i64 %46
%48 = load i32, i32* %47
%49 = load i32, i32* @SIGUSR1
%50 = tail call i32 @kill(i32 %48, i32 %49)
%51 = add nuw nsw i64 %46, 1
%52 = icmp eq i64 %51, %40
br i1 %52, label %41, label %45
53: ; preds = %42, %65
%54 = phi i64 [ 0, %42 ], [ %67, %65 ]
%55 = phi i32 [ 0, %42 ], [ %66, %65 ]
%56 = getelementptr inbounds i32, i32* %9, i64 %54
%57 = load i32, i32* %56
%58 = call i32 @waitpid(i32 %57, i32* %4, i32 0)
%59 = load i32, i32* %4
%60 = call i32 @WEXITSTATUS(i32 %59)
%61 = icmp eq i32 %60, 0
br i1 %61, label %65, label %62
62: ; preds = %53
%63 = load i32, i32* %4
%64 = call i32 @WEXITSTATUS(i32 %63)
br label %65
65: ; preds = %62, %53
%66 = phi i32 [ %64, %62 ], [ %55, %53 ]
%67 = add nuw nsw i64 %54, 1
%68 = icmp eq i64 %67, %44
br i1 %68, label %69, label %53
69: ; preds = %65, %37, %13, %41
%70 = phi i32 [ 0, %41 ], [ 0, %13 ], [ 0, %37 ], [ %66, %65 ]
%71 = call i32 @free(i32* %9)
br label %72
72: ; preds = %69, %17
%73 = phi i32 [ 2, %17 ], [ %70, %69 ]
ret i32 %73
}