#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 */

/* Variables and functions */
int /*<<< orphan*/ assert(int /*<<< orphan*/);
scalar_t__ checkResult(double **, double *, double *, long, long);
scalar_t__ malloc(int);
double omp_get_wtime();
int /*<<< orphan*/ omp_set_num_threads(int);
int /*<<< orphan*/ printf(char *, ...);
int loopParCollapse(int threads, double *matrix[], double *vector,
                    long int rows, long int cols, long int vectorSize) {
  int i, j;
  double start_time, end_time;
  double *result;
  assert((result = (double *)malloc(sizeof(double) * rows)) != NULL);
  for (i = 0; i < rows; i = i + 1)
    result[i] = 0.0;
  omp_set_num_threads(threads);
  start_time = omp_get_wtime();
#pragma omp parallel for collapse(2)
  for (i = 0; i < rows; i++) {
    for (j = 0; j < cols; j++) {
      result[i] = result[i] + matrix[i][j] * vector[j];
    }
  }
  end_time = omp_get_wtime();
  if (checkResult(matrix, vector, result, rows, cols) != 0) {
    printf(
        "\n\t\t There is a difference from Serial and Parallel Computation \n");
    return 1;
  }
  printf("\n\t\t Time Taken (Collapse Clause : OpenMP-3.0)   : %lf sec ",
         (end_time - start_time));
  return 0;
}


x86 assembly (clang, -O0):
<loopParCollapse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    -0x20(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x50(%rbp)
cmp    $0x0,%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
movl   $0x0,-0x34(%rbp)
movslq -0x34(%rbp),%rax
cmp    -0x20(%rbp),%rax
jge    11d6 <loopParCollapse+0x76>
mov    -0x50(%rbp),%rax
movslq -0x34(%rbp),%rcx
xorps  %xmm0,%xmm0
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11aa <loopParCollapse+0x4a>
mov    -0x8(%rbp),%edi
call   1080 <omp_set_num_threads@plt>
mov    $0x0,%al
call   1030 <omp_get_wtime@plt>
movsd  %xmm0,-0x40(%rbp)
movl   $0x0,-0x34(%rbp)
movslq -0x34(%rbp),%rax
cmp    -0x20(%rbp),%rax
jge    1279 <loopParCollapse+0x119>
movl   $0x0,-0x38(%rbp)
movslq -0x38(%rbp),%rax
cmp    -0x28(%rbp),%rax
jge    1266 <loopParCollapse+0x106>
mov    -0x50(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm1
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x38(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x18(%rbp),%rax
movslq -0x38(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
mov    -0x50(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1206 <loopParCollapse+0xa6>
jmp    126b <loopParCollapse+0x10b>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11f1 <loopParCollapse+0x91>
mov    $0x0,%al
call   1030 <omp_get_wtime@plt>
movsd  %xmm0,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x50(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
call   1050 <checkResult@plt>
cmp    $0x0,%rax
je     12c2 <loopParCollapse+0x162>
lea    0xd51(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e1 <loopParCollapse+0x181>
movsd  -0x48(%rbp),%xmm0
subsd  -0x40(%rbp),%xmm0
lea    0xd6e(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loopParCollapse>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,%r15
mov    %rcx,%r14
mov    %rdx,%r13
mov    %rsi,%r12
mov    %edi,%ebp
lea    0x0(,%rcx,8),%edi
call   1090 <malloc@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1080 <assert@plt>
test   %r14,%r14
jle    11d0 <loopParCollapse+0x50>
lea    0x0(,%r14,8),%rdx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <memset@plt>
mov    %ebp,%edi
call   10a0 <omp_set_num_threads@plt>
xor    %eax,%eax
call   1040 <omp_get_wtime@plt>
movsd  %xmm0,0x10(%rsp)
test   %r14,%r14
jle    1271 <loopParCollapse+0xf1>
mov    %r15,%rax
and    $0xfffffffffffffffe,%rax
xor    %ecx,%ecx
jmp    1209 <loopParCollapse+0x89>
nopl   0x0(%rax,%rax,1)
add    $0x1,%rcx
cmp    %r14,%rcx
je     1271 <loopParCollapse+0xf1>
test   %r15,%r15
jle    1200 <loopParCollapse+0x80>
mov    (%r12,%rcx,8),%rdx
movsd  (%rbx,%rcx,8),%xmm0
xor    %esi,%esi
cmp    $0x1,%r15
je     1254 <loopParCollapse+0xd4>
nop
movsd  (%rdx,%rsi,8),%xmm1
mulsd  0x0(%r13,%rsi,8),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rbx,%rcx,8)
movsd  0x8(%rdx,%rsi,8),%xmm0
mulsd  0x8(%r13,%rsi,8),%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,(%rbx,%rcx,8)
add    $0x2,%rsi
cmp    %rsi,%rax
jne    1220 <loopParCollapse+0xa0>
test   $0x1,%r15b
je     1200 <loopParCollapse+0x80>
movsd  (%rdx,%rsi,8),%xmm1
mulsd  0x0(%r13,%rsi,8),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rbx,%rcx,8)
jmp    1200 <loopParCollapse+0x80>
xor    %ebp,%ebp
xor    %eax,%eax
call   1040 <omp_get_wtime@plt>
movsd  %xmm0,0x8(%rsp)
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
mov    %r14,%rcx
mov    %r15,%r8
call   1060 <checkResult@plt>
test   %rax,%rax
je     12ac <loopParCollapse+0x12c>
lea    0xd9b(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebp
jmp    12c6 <loopParCollapse+0x146>
movsd  0x8(%rsp),%xmm0
subsd  0x10(%rsp),%xmm0
lea    0xd41(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [65 x i8] c"\0A\09\09 There is a difference from Serial and Parallel Computation \0A\00"
@.str.1 = [59 x i8] c"\0A\09\09 Time Taken (Collapse Clause : OpenMP-3.0) : %lf sec \00"
define i32 @loopParCollapse(i32 %0, double** %1, double* %2, i64 %3, i64 %4, i64 %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca double**
%10 = alloca double*
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
%14 = alloca i32
%15 = alloca i32
%16 = alloca double
%17 = alloca double
%18 = alloca double*
store i32 %0, i32* %8
store double** %1, double*** %9
store double* %2, double** %10
store i64 %3, i64* %11
store i64 %4, i64* %12
store i64 %5, i64* %13
%19 = load i64, i64* %11
%20 = mul i64 8, %19
%21 = trunc i64 %20 to i32
%22 = call i64 @malloc(i32 %21)
%23 = inttoptr i64 %22 to double*
store double* %23, double** %18
%24 = icmp ne double* %23, null
%25 = zext i1 %24 to i32
%26 = call i32 @assert(i32 %25)
store i32 0, i32* %14
br label %27
27: ; preds = %37, %6
%28 = load i32, i32* %14
%29 = sext i32 %28 to i64
%30 = load i64, i64* %11
%31 = icmp slt i64 %29, %30
br i1 %31, label %32, label %40
32: ; preds = %27
%33 = load double*, double** %18
%34 = load i32, i32* %14
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds double, double* %33, i64 %35
store double 0.000000e+00, double* %36
br label %37
37: ; preds = %32
%38 = load i32, i32* %14
%39 = add nsw i32 %38, 1
store i32 %39, i32* %14
br label %27
40: ; preds = %27
%41 = load i32, i32* %8
%42 = call i32 @omp_set_num_threads(i32 %41)
%43 = call double (...) @omp_get_wtime()
store double %43, double* %16
store i32 0, i32* %14
br label %44
44: ; preds = %84, %40
%45 = load i32, i32* %14
%46 = sext i32 %45 to i64
%47 = load i64, i64* %11
%48 = icmp slt i64 %46, %47
br i1 %48, label %49, label %87
49: ; preds = %44
store i32 0, i32* %15
br label %50
50: ; preds = %80, %49
%51 = load i32, i32* %15
%52 = sext i32 %51 to i64
%53 = load i64, i64* %12
%54 = icmp slt i64 %52, %53
br i1 %54, label %55, label %83
55: ; preds = %50
%56 = load double*, double** %18
%57 = load i32, i32* %14
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds double, double* %56, i64 %58
%60 = load double, double* %59
%61 = load double**, double*** %9
%62 = load i32, i32* %14
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds double*, double** %61, i64 %63
%65 = load double*, double** %64
%66 = load i32, i32* %15
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds double, double* %65, i64 %67
%69 = load double, double* %68
%70 = load double*, double** %10
%71 = load i32, i32* %15
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds double, double* %70, i64 %72
%74 = load double, double* %73
%75 = call double @llvm.fmuladd.f64(double %69, double %74, double %60)
%76 = load double*, double** %18
%77 = load i32, i32* %14
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds double, double* %76, i64 %78
store double %75, double* %79
br label %80
80: ; preds = %55
%81 = load i32, i32* %15
%82 = add nsw i32 %81, 1
store i32 %82, i32* %15
br label %50
83: ; preds = %50
br label %84
84: ; preds = %83
%85 = load i32, i32* %14
%86 = add nsw i32 %85, 1
store i32 %86, i32* %14
br label %44
87: ; preds = %44
%88 = call double (...) @omp_get_wtime()
store double %88, double* %17
%89 = load double**, double*** %9
%90 = load double*, double** %10
%91 = load double*, double** %18
%92 = load i64, i64* %11
%93 = load i64, i64* %12
%94 = call i64 @checkResult(double** %89, double* %90, double* %91, i64 %92, i64 %93)
%95 = icmp ne i64 %94, 0
br i1 %95, label %96, label %98
96: ; preds = %87
%97 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %7
br label %103
98: ; preds = %87
%99 = load double, double* %17
%100 = load double, double* %16
%101 = fsub double %99, %100
%102 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.1, i64 0, i64 0), double %101)
store i32 0, i32* %7
br label %103
103: ; preds = %98, %96
%104 = load i32, i32* %7
ret i32 %104
}

LLVM IR (-O2):
@.str.1 = [59 x i8] c"\0A\09\09 Time Taken (Collapse Clause : OpenMP-3.0) : %lf sec \00"
@str = [64 x i8] c"\0A\09\09 There is a difference from Serial and Parallel Computation \00"
define i32 @loopParCollapse(i32 %0, double** %1, double* %2, i64 %3, i64 %4, i64 %5) {
%7 = trunc i64 %3 to i32
%8 = shl i32 %7, 3
%9 = tail call i64 @malloc(i32 %8)
%10 = inttoptr i64 %9 to double*
%11 = icmp ne i64 %9, 0
%12 = zext i1 %11 to i32
%13 = tail call i32 @assert(i32 %12)
%14 = icmp sgt i64 %3, 0
br i1 %14, label %15, label %18
15: ; preds = %6
%16 = inttoptr i64 %9 to i8*
%17 = shl nuw i64 %3, 3
br label %18
18: ; preds = %15, %6
%19 = tail call i32 @omp_set_num_threads(i32 %0)
%20 = tail call double (...) @omp_get_wtime()
br i1 %14, label %21, label %64
21: ; preds = %18
%22 = icmp sgt i64 %4, 0
%23 = and i64 %4, 1
%24 = icmp eq i64 %4, 1
%25 = and i64 %4, -2
%26 = icmp eq i64 %23, 0
br label %27
27: ; preds = %21, %61
%28 = phi i64 [ 0, %21 ], [ %62, %61 ]
br i1 %22, label %29, label %61
29: ; preds = %27
%30 = getelementptr inbounds double, double* %10, i64 %28
%31 = getelementptr inbounds double*, double** %1, i64 %28
%32 = load double*, double** %31
%33 = load double, double* %30
br i1 %24, label %52, label %34
34: ; preds = %29, %34
%35 = phi double [ %48, %34 ], [ %33, %29 ]
%36 = phi i64 [ %49, %34 ], [ 0, %29 ]
%37 = phi i64 [ %50, %34 ], [ 0, %29 ]
%38 = getelementptr inbounds double, double* %32, i64 %36
%39 = load double, double* %38
%40 = getelementptr inbounds double, double* %2, i64 %36
%41 = load double, double* %40
%42 = tail call double @llvm.fmuladd.f64(double %39, double %41, double %35)
store double %42, double* %30
%43 = or i64 %36, 1
%44 = getelementptr inbounds double, double* %32, i64 %43
%45 = load double, double* %44
%46 = getelementptr inbounds double, double* %2, i64 %43
%47 = load double, double* %46
%48 = tail call double @llvm.fmuladd.f64(double %45, double %47, double %42)
store double %48, double* %30
%49 = add nuw nsw i64 %36, 2
%50 = add i64 %37, 2
%51 = icmp eq i64 %50, %25
br i1 %51, label %52, label %34
52: ; preds = %34, %29
%53 = phi double [ %33, %29 ], [ %48, %34 ]
%54 = phi i64 [ 0, %29 ], [ %49, %34 ]
br i1 %26, label %61, label %55
55: ; preds = %52
%56 = getelementptr inbounds double, double* %32, i64 %54
%57 = load double, double* %56
%58 = getelementptr inbounds double, double* %2, i64 %54
%59 = load double, double* %58
%60 = tail call double @llvm.fmuladd.f64(double %57, double %59, double %53)
store double %60, double* %30
br label %61
61: ; preds = %55, %52, %27
%62 = add nuw nsw i64 %28, 1
%63 = icmp eq i64 %62, %3
br i1 %63, label %64, label %27
64: ; preds = %61, %18
%65 = tail call double (...) @omp_get_wtime()
%66 = tail call i64 @checkResult(double** %1, double* %2, double* %10, i64 %3, i64 %4)
%67 = icmp eq i64 %66, 0
br i1 %67, label %70, label %68
68: ; preds = %64
%69 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([64 x i8], [64 x i8]* @str, i64 0, i64 0))
br label %73
70: ; preds = %64
%71 = fsub double %65, %20
%72 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([59 x i8], [59 x i8]* @.str.1, i64 0, i64 0), double %71)
br label %73
73: ; preds = %70, %68
%74 = phi i32 [ 1, %68 ], [ 0, %70 ]
ret i32 %74
}