#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 /*<<< orphan*/ pthread_t;
typedef scalar_t__ clock_t;

/* Variables and functions */
double CLOCKS_PER_SEC;
scalar_t__ clock();
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *);
int pthread_create(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                   void *(*)(void *), void *);
int /*<<< orphan*/ pthread_join(int /*<<< orphan*/, int /*<<< orphan*/);
float benchmark_start(int t, void *(*producer)(void *),
                      void *(*consumer)(void *), void *arg) {
  clock_t start_t, end_t;
  float total;
  pthread_t producer_threads[t];
  pthread_t consumer_threads[t];
  int s, s0;
  start_t = clock();
  for (int i = 0; i < t; ++i) {
    s = pthread_create(&producer_threads[i], NULL, producer, arg);
    s0 = pthread_create(&consumer_threads[i], NULL, consumer, arg);
    if (s != 0 || s0 != 0) {
      printf("Error creating thread.");
      exit(-1);
    }
  }
  for (int i = 0; i < t; i++) {
    pthread_join(producer_threads[i], 0);
    pthread_join(consumer_threads[i], 0);
  }
  end_t = clock();
  total = (double)(end_t - start_t) / CLOCKS_PER_SEC;
  return total;
}


x86 assembly (clang, -O0):
<benchmark_start>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x4(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x40(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x70(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x48(%rbp)
mov    -0x4(%rbp),%eax
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x68(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x50(%rbp)
mov    $0x0,%al
call   1030 <clock@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x5c(%rbp)
mov    -0x5c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    124f <benchmark_start+0xff>
mov    -0x70(%rbp),%rdi
movslq -0x5c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%esi
call   1050 <pthread_create@plt>
mov    -0x68(%rbp),%rdi
mov    %eax,-0x54(%rbp)
movslq -0x5c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%esi
call   1050 <pthread_create@plt>
mov    %eax,-0x58(%rbp)
cmpl   $0x0,-0x54(%rbp)
jne    1226 <benchmark_start+0xd6>
cmpl   $0x0,-0x58(%rbp)
je     123c <benchmark_start+0xec>
lea    0xdd3(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
jmp    1241 <benchmark_start+0xf1>
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    11c0 <benchmark_start+0x70>
movl   $0x0,-0x60(%rbp)
mov    -0x60(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1294 <benchmark_start+0x144>
mov    -0x70(%rbp),%rax
movslq -0x60(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
xor    %esi,%esi
call   1070 <pthread_join@plt>
mov    -0x68(%rbp),%rax
movslq -0x60(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
xor    %esi,%esi
call   1070 <pthread_join@plt>
mov    -0x60(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x60(%rbp)
jmp    1256 <benchmark_start+0x106>
mov    $0x0,%al
call   1030 <clock@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
sub    -0x28(%rbp),%rax
cvtsi2sd %rax,%xmm0
mov    0x2d35(%rip),%rax
divsd  (%rax),%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x34(%rbp)
movss  -0x34(%rbp),%xmm0
mov    -0x40(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<benchmark_start>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rcx,%rbx
mov    %rdx,-0x60(%rbp)
mov    %rsi,-0x58(%rbp)
mov    %edi,%r15d
mov    %edi,%r13d
mov    %rsp,%r14
lea    0xf(,%r13,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    %rsp,%r12
sub    %rax,%r12
mov    %r12,%rsp
xor    %eax,%eax
call   1030 <clock@plt>
mov    %rax,-0x50(%rbp)
test   %r15d,%r15d
jle    121f <benchmark_start+0xcf>
mov    %r15d,-0x2c(%rbp)
mov    %r13,-0x48(%rbp)
mov    %r14,-0x40(%rbp)
mov    %r12,-0x38(%rbp)
mov    %r14,%rdi
xor    %esi,%esi
mov    -0x58(%rbp),%rdx
mov    %rbx,%rcx
call   1050 <pthread_create@plt>
mov    %eax,%r15d
mov    %r12,%rdi
xor    %esi,%esi
mov    -0x60(%rbp),%rdx
mov    %rbx,%rcx
call   1050 <pthread_create@plt>
or     %r15d,%eax
jne    124d <benchmark_start+0xfd>
add    $0x4,%r12
add    $0x4,%r14
add    $0xffffffffffffffff,%r13
jne    11b0 <benchmark_start+0x60>
cmpl   $0x0,-0x2c(%rbp)
mov    -0x48(%rbp),%r14
mov    -0x40(%rbp),%r15
mov    -0x38(%rbp),%r12
jle    121f <benchmark_start+0xcf>
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    (%r15,%rbx,4),%edi
xor    %esi,%esi
call   1070 <pthread_join@plt>
mov    (%r12,%rbx,4),%edi
xor    %esi,%esi
call   1070 <pthread_join@plt>
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1200 <benchmark_start+0xb0>
xor    %eax,%eax
call   1030 <clock@plt>
sub    -0x50(%rbp),%rax
cvtsi2sd %rax,%xmm0
mov    0x2db2(%rip),%rax
divsd  (%rax),%xmm0
cvtsd2ss %xmm0,%xmm0
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdac(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [23 x i8] c"Error creating thread.\00"
@CLOCKS_PER_SEC = global double 0.000000e+00
define float @benchmark_start(i32 %0, i8* (i8*)* %1, i8* (i8*)* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8* (i8*)*
%7 = alloca i8* (i8*)*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca float
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i64
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
store i32 %0, i32* %5
store i8* (i8*)* %1, i8* (i8*)** %6
store i8* (i8*)* %2, i8* (i8*)** %7
store i8* %3, i8** %8
%19 = load i32, i32* %5
%20 = zext i32 %19 to i64
%21 = call i8* @llvm.stacksave()
store i8* %21, i8** %12
%22 = alloca i32, i64 %20
store i64 %20, i64* %13
%23 = load i32, i32* %5
%24 = zext i32 %23 to i64
%25 = alloca i32, i64 %24
store i64 %24, i64* %14
%26 = call i64 (...) @clock()
store i64 %26, i64* %9
store i32 0, i32* %17
br label %27
27: ; preds = %53, %4
%28 = load i32, i32* %17
%29 = load i32, i32* %5
%30 = icmp slt i32 %28, %29
br i1 %30, label %31, label %56
31: ; preds = %27
%32 = load i32, i32* %17
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i32, i32* %22, i64 %33
%35 = load i8* (i8*)*, i8* (i8*)** %6
%36 = load i8*, i8** %8
%37 = call i32 @pthread_create(i32* %34, i32* null, i8* (i8*)* %35, i8* %36)
store i32 %37, i32* %15
%38 = load i32, i32* %17
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i32, i32* %25, i64 %39
%41 = load i8* (i8*)*, i8* (i8*)** %7
%42 = load i8*, i8** %8
%43 = call i32 @pthread_create(i32* %40, i32* null, i8* (i8*)* %41, i8* %42)
store i32 %43, i32* %16
%44 = load i32, i32* %15
%45 = icmp ne i32 %44, 0
br i1 %45, label %49, label %46
46: ; preds = %31
%47 = load i32, i32* %16
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %46, %31
%50 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%51 = call i32 @exit(i32 -1)
unreachable
52: ; preds = %46
br label %53
53: ; preds = %52
%54 = load i32, i32* %17
%55 = add nsw i32 %54, 1
store i32 %55, i32* %17
br label %27
56: ; preds = %27
store i32 0, i32* %18
br label %57
57: ; preds = %72, %56
%58 = load i32, i32* %18
%59 = load i32, i32* %5
%60 = icmp slt i32 %58, %59
br i1 %60, label %61, label %75
61: ; preds = %57
%62 = load i32, i32* %18
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i32, i32* %22, i64 %63
%65 = load i32, i32* %64
%66 = call i32 @pthread_join(i32 %65, i32 0)
%67 = load i32, i32* %18
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i32, i32* %25, i64 %68
%70 = load i32, i32* %69
%71 = call i32 @pthread_join(i32 %70, i32 0)
br label %72
72: ; preds = %61
%73 = load i32, i32* %18
%74 = add nsw i32 %73, 1
store i32 %74, i32* %18
br label %57
75: ; preds = %57
%76 = call i64 (...) @clock()
store i64 %76, i64* %10
%77 = load i64, i64* %10
%78 = load i64, i64* %9
%79 = sub nsw i64 %77, %78
%80 = sitofp i64 %79 to double
%81 = load double, double* @CLOCKS_PER_SEC
%82 = fdiv double %80, %81
%83 = fptrunc double %82 to float
store float %83, float* %11
%84 = load float, float* %11
%85 = load i8*, i8** %12
ret float %84
}

LLVM IR (-O2):
@.str = [23 x i8] c"Error creating thread.\00"
@CLOCKS_PER_SEC = global double 0.000000e+00
define float @benchmark_start(i32 %0, i8* (i8*)* %1, i8* (i8*)* %2, i8* %3) {
%5 = zext i32 %0 to i64
%6 = alloca i32, i64 %5
%7 = alloca i32, i64 %5
%8 = tail call i64 (...) @clock()
%9 = icmp sgt i32 %0, 0
br i1 %9, label %13, label %26
10: ; preds = %13
%11 = icmp eq i64 %22, %5
br i1 %11, label %12, label %13
12: ; preds = %10
br i1 %9, label %33, label %26
13: ; preds = %4, %10
%14 = phi i64 [ %22, %10 ], [ 0, %4 ]
%15 = getelementptr inbounds i32, i32* %6, i64 %14
%16 = call i32 @pthread_create(i32* %15, i32* null, i8* (i8*)* %1, i8* %3)
%17 = getelementptr inbounds i32, i32* %7, i64 %14
%18 = call i32 @pthread_create(i32* %17, i32* null, i8* (i8*)* %2, i8* %3)
%19 = icmp ne i32 %16, 0
%20 = icmp ne i32 %18, 0
%21 = select i1 %19, i1 true, i1 %20
%22 = add nuw nsw i64 %14, 1
br i1 %21, label %23, label %10
23: ; preds = %13
%24 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @exit(i32 -1)
unreachable
26: ; preds = %33, %4, %12
%27 = call i64 (...) @clock()
%28 = sub nsw i64 %27, %8
%29 = sitofp i64 %28 to double
%30 = load double, double* @CLOCKS_PER_SEC
%31 = fdiv double %29, %30
%32 = fptrunc double %31 to float
ret float %32
33: ; preds = %12, %33
%34 = phi i64 [ %41, %33 ], [ 0, %12 ]
%35 = getelementptr inbounds i32, i32* %6, i64 %34
%36 = load i32, i32* %35
%37 = call i32 @pthread_join(i32 %36, i32 0)
%38 = getelementptr inbounds i32, i32* %7, i64 %34
%39 = load i32, i32* %38
%40 = call i32 @pthread_join(i32 %39, i32 0)
%41 = add nuw nsw i64 %34, 1
%42 = icmp eq i64 %41, %5
br i1 %42, label %26, label %33
}