#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);
  }
  run_for_duration(duration - delay * (num_threads - 1), 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),%edi
mov    -0x14(%rbp),%eax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
imul   %ecx,%eax
sub    %eax,%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    1370 <run_dhrystone+0x1d0>
jmp    1303 <run_dhrystone+0x163>
lea    -0x4c(%rbp),%rdi
call   10b0 <wait@plt>
mov    %eax,-0x50(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x50(%rbp)
mov    %al,-0x59(%rbp)
je     134f <run_dhrystone+0x1af>
mov    -0x4c(%rbp),%edi
call   1080 <WIFEXITED@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x5a(%rbp)
jne    1349 <run_dhrystone+0x1a9>
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    1303 <run_dhrystone+0x163>
jmp    135f <run_dhrystone+0x1bf>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    12ed <run_dhrystone+0x14d>
mov    $0x0,%al
call   1030 <clock@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
sub    -0x30(%rbp),%rax
cvtsi2sd %rax,%xmm0
mov    0x2c59(%rip),%rax
divsd  (%rax),%xmm0
lea    0xcd5(%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),%r12d
mov    0x8(%rsp),%r14d
xor    %r15d,%r15d
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
je     133a <run_dhrystone+0x18a>
mov    %rax,(%rbx,%r15,8)
mov    %ebp,%edi
call   10b0 <sleep@plt>
add    $0x1,%r15
sub    %ebp,%r14d
cmp    %r15,%r12
jne    1280 <run_dhrystone+0xd0>
mov    0x4(%rsp),%eax
imul   %ebp,%eax
mov    0x8(%rsp),%edi
sub    %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>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   10c0 <wait@plt>
cmp    $0xffffffff,%eax
je     12ce <run_dhrystone+0x11e>
mov    0xc(%rsp),%edi
call   1090 <WIFEXITED@plt>
test   %eax,%eax
jne    12ce <run_dhrystone+0x11e>
mov    0xc(%rsp),%edi
call   1070 <WIFSIGNALED@plt>
test   %eax,%eax
je     12e0 <run_dhrystone+0x130>
jmp    12ce <run_dhrystone+0x11e>
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
%82 = load i32, i32* %10
%83 = call i32 @run_for_duration(i32 %81, i32 %82)
store i64 0, i64* %12
br label %84
84: ; preds = %109, %75
%85 = load i64, i64* %12
%86 = load i32, i32* %6
%87 = sext i32 %86 to i64
%88 = icmp slt i64 %85, %87
br i1 %88, label %89, label %112
89: ; preds = %84
br label %90
90: ; preds = %106, %89
%91 = call i32 @wait(i32* %15)
store i32 %91, i32* %16
br label %92
92: ; preds = %90
%93 = load i32, i32* %16
%94 = icmp ne i32 %93, -1
br i1 %94, label %95, label %106
95: ; preds = %92
%96 = load i32, i32* %15
%97 = call i32 @WIFEXITED(i32 %96)
%98 = icmp ne i32 %97, 0
br i1 %98, label %104, label %99
99: ; preds = %95
%100 = load i32, i32* %15
%101 = call i32 @WIFSIGNALED(i32 %100)
%102 = icmp ne i32 %101, 0
%103 = xor i1 %102, true
br label %104
104: ; preds = %99, %95
%105 = phi i1 [ false, %95 ], [ %103, %99 ]
br label %106
106: ; preds = %104, %92
%107 = phi i1 [ false, %92 ], [ %105, %104 ]
br i1 %107, label %90, label %108
108: ; preds = %106
br label %109
109: ; preds = %108
%110 = load i64, i64* %12
%111 = add nsw i64 %110, 1
store i64 %111, i64* %12
br label %84
112: ; preds = %84
%113 = call i64 (...) @clock()
store i64 %113, i64* %17
%114 = load i64, i64* %17
%115 = load i64, i64* %11
%116 = sub nsw i64 %114, %115
%117 = sitofp i64 %116 to double
%118 = load double, double* @CLOCKS_PER_SEC
%119 = fdiv double %117, %118
%120 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), double %119)
%121 = 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 = tail call i32 @run_for_duration(i32 %40, i32 %15)
%42 = icmp sgt i32 %1, 0
br i1 %42, label %43, label %61
43: ; preds = %38
%44 = bitcast i32* %5 to i8*
br label %45
45: ; preds = %43, %58
%46 = phi i64 [ 0, %43 ], [ %59, %58 ]
br label %47
47: ; preds = %54, %45
%48 = call i32 @wait(i32* %5)
%49 = icmp eq i32 %48, -1
br i1 %49, label %58, label %50
50: ; preds = %47
%51 = load i32, i32* %5
%52 = call i32 @WIFEXITED(i32 %51)
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %58
54: ; preds = %50
%55 = load i32, i32* %5
%56 = call i32 @WIFSIGNALED(i32 %55)
%57 = icmp eq i32 %56, 0
br i1 %57, label %47, label %58
58: ; preds = %50, %47, %54
%59 = add nuw nsw i64 %46, 1
%60 = icmp eq i64 %59, %11
br i1 %60, label %61, label %45
61: ; preds = %58, %38
%62 = call i64 (...) @clock()
%63 = sub nsw i64 %62, %16
%64 = sitofp i64 %63 to double
%65 = load double, double* @CLOCKS_PER_SEC
%66 = fdiv double %64, %65
%67 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), double %66)
%68 = call i32 @exit(i32 0)
unreachable
}