#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;
typedef scalar_t__ clock_t;

/* Variables and functions */
double CLOCKS_PER_SEC;
int /*<<< orphan*/ WIFEXITED(int);
int /*<<< orphan*/ WIFSIGNALED(int);
scalar_t__ clock();
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
scalar_t__ *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ run_for_duration(int, int);
int /*<<< orphan*/ sleep(int);
int wait(int *);
run_dhrystone(int duration, int num_threads, long num_loops, int delay) {
  printf("duration: %d seconds\n", duration);
  printf("number of threads: %d\n", num_threads);
  printf("number of loops: %ld\n", num_loops);
  printf("delay between starting threads: %d seconds\n", delay);
  printf("\n");
  pid_t *children = malloc(num_threads * sizeof(pid_t));
  int loops_per_thread = num_loops / num_threads;
  clock_t run_start = clock();
  long i;
  int actual_duration;
  for (i = 0; i < (num_threads - 1); i++) {
    pid_t c = fork();
    if (c == 0) {
      actual_duration = duration - i * delay;
      if (actual_duration < 0)
        actual_duration = 0;
      run_for_duration(actual_duration, loops_per_thread);
      exit(0);
    }
    children[i] = c;
    sleep(delay);
  }
  actual_duration = duration - delay * (num_threads - 1);
  if (actual_duration < 0)
    actual_duration = 0;
  run_for_duration(actual_duration, loops_per_thread);
  for (i = 0; i < num_threads; i++) {
    int status, w;
    do {
      w = wait(&status);
    } while (w != -1 && (!WIFEXITED(status) && !WIFSIGNALED(status)));
  }
  clock_t run_end = clock();
  printf("\nTotal dhrystone run time: %f seconds.\n",
         (double)(run_end - run_start) / CLOCKS_PER_SEC);
  exit(0);
}


x86 assembly (clang, -O0):
<run_dhrystone>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    -0x4(%rbp),%esi
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%esi
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rsi
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%esi
lea    0xe50(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe94(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movslq -0x8(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x8(%rbp),%rcx
cqto
idiv   %rcx
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1030 <clock@plt>
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
jge    12cc <run_dhrystone+0x12c>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    12a3 <run_dhrystone+0x103>
movslq -0x4(%rbp),%rax
mov    -0x38(%rbp),%rcx
movslq -0x14(%rbp),%rdx
imul   %rdx,%rcx
sub    %rcx,%rax
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    1291 <run_dhrystone+0xf1>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%edi
mov    -0x24(%rbp),%esi
call   1040 <run_for_duration@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    -0x48(%rbp),%rdx
mov    -0x20(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x14(%rbp),%edi
call   10a0 <sleep@plt>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    123e <run_dhrystone+0x9e>
mov    -0x4(%rbp),%eax
mov    -0x14(%rbp),%ecx
mov    -0x8(%rbp),%edx
sub    $0x1,%edx
imul   %edx,%ecx
sub    %ecx,%eax
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    12f1 <run_dhrystone+0x151>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%edi
mov    -0x24(%rbp),%esi
call   1040 <run_for_duration@plt>
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
movslq -0x8(%rbp),%rcx
cmp    %rcx,%rax
jge    1387 <run_dhrystone+0x1e7>
jmp    131a <run_dhrystone+0x17a>
lea    -0x4c(%rbp),%rdi
call   10b0 <wait@plt>
mov    %eax,-0x50(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x50(%rbp)
mov    %al,-0x59(%rbp)
je     1366 <run_dhrystone+0x1c6>
mov    -0x4c(%rbp),%edi
call   1080 <WIFEXITED@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x5a(%rbp)
jne    1360 <run_dhrystone+0x1c0>
mov    -0x4c(%rbp),%edi
call   1060 <WIFSIGNALED@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x5a(%rbp)
mov    -0x5a(%rbp),%al
mov    %al,-0x59(%rbp)
mov    -0x59(%rbp),%al
test   $0x1,%al
jne    131a <run_dhrystone+0x17a>
jmp    1376 <run_dhrystone+0x1d6>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    1304 <run_dhrystone+0x164>
mov    $0x0,%al
call   1030 <clock@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
sub    -0x30(%rbp),%rax
cvtsi2sd %rax,%xmm0
mov    0x2c42(%rip),%rax
divsd  (%rax),%xmm0
lea    0xcbe(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<run_dhrystone>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %ecx,%ebp
mov    %rdx,%r15
mov    %esi,%r14d
mov    %edi,%esi
lea    0xe31(%rip),%rdi
mov    %esi,0x8(%rsp)
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe35(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe3b(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe40(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
movslq %r14d,%r13
lea    0x0(,%r13,8),%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %r15,%rax
or     %r13,%rax
shr    $0x20,%rax
jne    123f <run_dhrystone+0x8f>
mov    %r15d,%eax
xor    %edx,%edx
div    %r14d
jmp    1247 <run_dhrystone+0x97>
mov    %r15,%rax
cqto
idiv   %r13
mov    %rax,0x10(%rsp)
xor    %eax,%eax
call   1040 <clock@plt>
mov    %rax,0x18(%rsp)
lea    -0x1(%r14),%eax
mov    %eax,0x4(%rsp)
mov    %r14,0x20(%rsp)
cmp    $0x2,%r14d
jl     12a7 <run_dhrystone+0xf7>
mov    0x4(%rsp),%r15d
mov    0x8(%rsp),%r14d
xor    %r12d,%r12d
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
je     133a <run_dhrystone+0x18a>
mov    %rax,(%rbx,%r12,8)
mov    %ebp,%edi
call   10b0 <sleep@plt>
add    $0x1,%r12
sub    %ebp,%r14d
cmp    %r12,%r15
jne    1280 <run_dhrystone+0xd0>
mov    0x4(%rsp),%ecx
imul   %ebp,%ecx
mov    0x8(%rsp),%eax
sub    %ecx,%eax
mov    $0x0,%edi
cmovns %eax,%edi
mov    0x10(%rsp),%rsi
call   1050 <run_for_duration@plt>
cmpl   $0x0,0x20(%rsp)
jle    1309 <run_dhrystone+0x159>
xor    %ebp,%ebp
lea    0xc(%rsp),%rbx
jmp    12e0 <run_dhrystone+0x130>
add    $0x1,%rbp
cmp    %r13,%rbp
je     1309 <run_dhrystone+0x159>
nop
mov    %rbx,%rdi
call   10c0 <wait@plt>
cmp    $0xffffffff,%eax
je     12d6 <run_dhrystone+0x126>
mov    0xc(%rsp),%edi
call   1090 <WIFEXITED@plt>
test   %eax,%eax
jne    12d6 <run_dhrystone+0x126>
mov    0xc(%rsp),%edi
call   1070 <WIFSIGNALED@plt>
test   %eax,%eax
je     12e0 <run_dhrystone+0x130>
jmp    12d6 <run_dhrystone+0x126>
xor    %eax,%eax
call   1040 <clock@plt>
sub    0x18(%rsp),%rax
cvtsi2sd %rax,%xmm0
mov    0x2cc7(%rip),%rax
divsd  (%rax),%xmm0
lea    0xd43(%rip),%rdi
mov    $0x1,%al
call   1060 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
xor    %eax,%eax
test   %r14d,%r14d
cmovs  %eax,%r14d
mov    %r14d,%edi
mov    0x10(%rsp),%rsi
call   1050 <run_for_duration@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [22 x i8] c"duration: %d seconds\0A\00"
@.str.1 = [23 x i8] c"number of threads: %d\0A\00"
@.str.2 = [22 x i8] c"number of loops: %ld\0A\00"
@.str.3 = [44 x i8] c"delay between starting threads: %d seconds\0A\00"
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [40 x i8] c"\0ATotal dhrystone run time: %f seconds.\0A\00"
@CLOCKS_PER_SEC = global double 0.000000e+00
define i32 @run_dhrystone(i32 %0, i32 %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64*
%10 = alloca i32
%11 = alloca i64
%12 = alloca i64
%13 = alloca i32
%14 = alloca i64
%15 = alloca i32
%16 = alloca i32
%17 = alloca i64
store i32 %0, i32* %5
store i32 %1, i32* %6
store i64 %2, i64* %7
store i32 %3, i32* %8
%18 = load i32, i32* %5
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = load i32, i32* %6
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = load i64, i64* %7
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i64 %22)
%24 = load i32, i32* %8
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i32 %24)
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%27 = load i32, i32* %6
%28 = sext i32 %27 to i64
%29 = mul i64 %28, 8
%30 = trunc i64 %29 to i32
%31 = call i64* @malloc(i32 %30)
store i64* %31, i64** %9
%32 = load i64, i64* %7
%33 = load i32, i32* %6
%34 = sext i32 %33 to i64
%35 = sdiv i64 %32, %34
%36 = trunc i64 %35 to i32
store i32 %36, i32* %10
%37 = call i64 (...) @clock()
store i64 %37, i64* %11
store i64 0, i64* %12
br label %38
38: ; preds = %72, %4
%39 = load i64, i64* %12
%40 = load i32, i32* %6
%41 = sub nsw i32 %40, 1
%42 = sext i32 %41 to i64
%43 = icmp slt i64 %39, %42
br i1 %43, label %44, label %75
44: ; preds = %38
%45 = call i64 (...) @fork()
store i64 %45, i64* %14
%46 = load i64, i64* %14
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %65
48: ; preds = %44
%49 = load i32, i32* %5
%50 = sext i32 %49 to i64
%51 = load i64, i64* %12
%52 = load i32, i32* %8
%53 = sext i32 %52 to i64
%54 = mul nsw i64 %51, %53
%55 = sub nsw i64 %50, %54
%56 = trunc i64 %55 to i32
store i32 %56, i32* %13
%57 = load i32, i32* %13
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %48
store i32 0, i32* %13
br label %60
60: ; preds = %59, %48
%61 = load i32, i32* %13
%62 = load i32, i32* %10
%63 = call i32 @run_for_duration(i32 %61, i32 %62)
%64 = call i32 @exit(i32 0)
unreachable
65: ; preds = %44
%66 = load i64, i64* %14
%67 = load i64*, i64** %9
%68 = load i64, i64* %12
%69 = getelementptr inbounds i64, i64* %67, i64 %68
store i64 %66, i64* %69
%70 = load i32, i32* %8
%71 = call i32 @sleep(i32 %70)
br label %72
72: ; preds = %65
%73 = load i64, i64* %12
%74 = add nsw i64 %73, 1
store i64 %74, i64* %12
br label %38
75: ; preds = %38
%76 = load i32, i32* %5
%77 = load i32, i32* %8
%78 = load i32, i32* %6
%79 = sub nsw i32 %78, 1
%80 = mul nsw i32 %77, %79
%81 = sub nsw i32 %76, %80
store i32 %81, i32* %13
%82 = load i32, i32* %13
%83 = icmp slt i32 %82, 0
br i1 %83, label %84, label %85
84: ; preds = %75
store i32 0, i32* %13
br label %85
85: ; preds = %84, %75
%86 = load i32, i32* %13
%87 = load i32, i32* %10
%88 = call i32 @run_for_duration(i32 %86, i32 %87)
store i64 0, i64* %12
br label %89
89: ; preds = %114, %85
%90 = load i64, i64* %12
%91 = load i32, i32* %6
%92 = sext i32 %91 to i64
%93 = icmp slt i64 %90, %92
br i1 %93, label %94, label %117
94: ; preds = %89
br label %95
95: ; preds = %111, %94
%96 = call i32 @wait(i32* %15)
store i32 %96, i32* %16
br label %97
97: ; preds = %95
%98 = load i32, i32* %16
%99 = icmp ne i32 %98, -1
br i1 %99, label %100, label %111
100: ; preds = %97
%101 = load i32, i32* %15
%102 = call i32 @WIFEXITED(i32 %101)
%103 = icmp ne i32 %102, 0
br i1 %103, label %109, label %104
104: ; preds = %100
%105 = load i32, i32* %15
%106 = call i32 @WIFSIGNALED(i32 %105)
%107 = icmp ne i32 %106, 0
%108 = xor i1 %107, true
br label %109
109: ; preds = %104, %100
%110 = phi i1 [ false, %100 ], [ %108, %104 ]
br label %111
111: ; preds = %109, %97
%112 = phi i1 [ false, %97 ], [ %110, %109 ]
br i1 %112, label %95, label %113
113: ; preds = %111
br label %114
114: ; preds = %113
%115 = load i64, i64* %12
%116 = add nsw i64 %115, 1
store i64 %116, i64* %12
br label %89
117: ; preds = %89
%118 = call i64 (...) @clock()
store i64 %118, i64* %17
%119 = load i64, i64* %17
%120 = load i64, i64* %11
%121 = sub nsw i64 %119, %120
%122 = sitofp i64 %121 to double
%123 = load double, double* @CLOCKS_PER_SEC
%124 = fdiv double %122, %123
%125 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), double %124)
%126 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [22 x i8] c"duration: %d seconds\0A\00"
@.str.1 = [23 x i8] c"number of threads: %d\0A\00"
@.str.2 = [22 x i8] c"number of loops: %ld\0A\00"
@.str.3 = [44 x i8] c"delay between starting threads: %d seconds\0A\00"
@.str.5 = [40 x i8] c"\0ATotal dhrystone run time: %f seconds.\0A\00"
@CLOCKS_PER_SEC = global double 0.000000e+00
define i32 @run_dhrystone(i32 %0, i32 %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %1)
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i64 %2)
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i32 %3)
%10 = tail call i32 @putchar(i32 10)
%11 = sext i32 %1 to i64
%12 = shl i32 %1, 3
%13 = tail call i64* @malloc(i32 %12)
%14 = sdiv i64 %2, %11
%15 = trunc i64 %14 to i32
%16 = tail call i64 (...) @clock()
%17 = add nsw i32 %1, -1
%18 = icmp sgt i32 %1, 1
br i1 %18, label %19, label %38
19: ; preds = %4
%20 = zext i32 %17 to i64
br label %21
21: ; preds = %19, %33
%22 = phi i64 [ %36, %33 ], [ 0, %19 ]
%23 = tail call i64 (...) @fork()
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %33
25: ; preds = %21
%26 = trunc i64 %22 to i32
%27 = mul i32 %26, %3
%28 = sub i32 %0, %27
%29 = icmp sgt i32 %28, 0
%30 = select i1 %29, i32 %28, i32 0
%31 = tail call i32 @run_for_duration(i32 %30, i32 %15)
%32 = tail call i32 @exit(i32 0)
unreachable
33: ; preds = %21
%34 = getelementptr inbounds i64, i64* %13, i64 %22
store i64 %23, i64* %34
%35 = tail call i32 @sleep(i32 %3)
%36 = add nuw nsw i64 %22, 1
%37 = icmp eq i64 %36, %20
br i1 %37, label %38, label %21
38: ; preds = %33, %4
%39 = mul nsw i32 %17, %3
%40 = sub nsw i32 %0, %39
%41 = icmp sgt i32 %40, 0
%42 = select i1 %41, i32 %40, i32 0
%43 = tail call i32 @run_for_duration(i32 %42, i32 %15)
%44 = icmp sgt i32 %1, 0
br i1 %44, label %45, label %63
45: ; preds = %38
%46 = bitcast i32* %5 to i8*
br label %47
47: ; preds = %45, %60
%48 = phi i64 [ 0, %45 ], [ %61, %60 ]
br label %49
49: ; preds = %56, %47
%50 = call i32 @wait(i32* %5)
%51 = icmp eq i32 %50, -1
br i1 %51, label %60, label %52
52: ; preds = %49
%53 = load i32, i32* %5
%54 = call i32 @WIFEXITED(i32 %53)
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %60
56: ; preds = %52
%57 = load i32, i32* %5
%58 = call i32 @WIFSIGNALED(i32 %57)
%59 = icmp eq i32 %58, 0
br i1 %59, label %49, label %60
60: ; preds = %52, %49, %56
%61 = add nuw nsw i64 %48, 1
%62 = icmp eq i64 %61, %11
br i1 %62, label %63, label %47
63: ; preds = %60, %38
%64 = call i64 (...) @clock()
%65 = sub nsw i64 %64, %16
%66 = sitofp i64 %65 to double
%67 = load double, double* @CLOCKS_PER_SEC
%68 = fdiv double %66, %67
%69 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), double %68)
%70 = call i32 @exit(i32 0)
unreachable
}