#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*/ EXIT_FAILURE;
scalar_t__ calloc(int, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
double **matrix_multiplication(double **mat1, int mat1_row, int mat1_col,
                               double **mat2, int mat2_row, int mat2_col) {
  if (mat1_col != mat2_row) {
    fprintf(stderr, "ERROR: columns of mat1 and rows of mat2 are not matched!");
    exit(EXIT_FAILURE);
  }
  double **product = (double **)calloc(mat1_row, sizeof(double *));
  for (int i = 0; i < mat1_row; i++)
    product[i] = (double *)calloc(mat2_col, sizeof(double));
  for (int i = 0; i < mat1_row; i++)
    for (int j = 0; j < mat2_col; j++)
      for (int k = 0; k < mat1_col; k++)
        product[i][j] += mat1[i][k] * mat2[k][j];
  return product;
}


x86 assembly (clang, -O0):
<matrix_multiplication>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    %r9d,-0x20(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     117d <matrix_multiplication+0x4d>
mov    0x2e7f(%rip),%rax
mov    (%rax),%edi
lea    0xe96(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    -0xc(%rbp),%edi
mov    $0x8,%esi
call   1030 <calloc@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11cb <matrix_multiplication+0x9b>
mov    -0x20(%rbp),%edi
mov    $0x8,%esi
call   1030 <calloc@plt>
mov    %rax,%rdx
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1195 <matrix_multiplication+0x65>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1284 <matrix_multiplication+0x154>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    1271 <matrix_multiplication+0x141>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    125e <matrix_multiplication+0x12e>
mov    -0x8(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x38(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x18(%rbp),%rax
movslq -0x38(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
mov    -0x28(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    11f8 <matrix_multiplication+0xc8>
jmp    1263 <matrix_multiplication+0x133>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11e5 <matrix_multiplication+0xb5>
jmp    1276 <matrix_multiplication+0x146>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11d2 <matrix_multiplication+0xa2>
mov    -0x28(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<matrix_multiplication>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9d,(%rsp)
cmp    %r8d,%edx
jne    127d <matrix_multiplication+0x14d>
mov    %rcx,%rbx
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%r15
mov    %esi,%edi
mov    $0x8,%esi
call   1030 <calloc@plt>
mov    %rax,%r12
test   %ebp,%ebp
mov    (%rsp),%edi
jle    126b <matrix_multiplication+0x13b>
mov    %ebp,0x4(%rsp)
mov    %ebp,%r13d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
mov    $0x8,%esi
call   1030 <calloc@plt>
mov    (%rsp),%edi
mov    %rax,(%r12,%rbp,8)
add    $0x1,%rbp
cmp    %rbp,%r13
jne    1180 <matrix_multiplication+0x50>
cmpl   $0x0,0x4(%rsp)
jle    126b <matrix_multiplication+0x13b>
mov    %edi,%r9d
mov    %r14d,%r8d
mov    %r8d,%r11d
and    $0xfffffffe,%r11d
xor    %r10d,%r10d
jmp    11cd <matrix_multiplication+0x9d>
nopw   0x0(%rax,%rax,1)
add    $0x1,%r10
cmp    %r13,%r10
je     126b <matrix_multiplication+0x13b>
test   %edi,%edi
jle    11c0 <matrix_multiplication+0x90>
xor    %edx,%edx
jmp    11e9 <matrix_multiplication+0xb9>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%rdx
cmp    %r9,%rdx
je     11c0 <matrix_multiplication+0x90>
test   %r14d,%r14d
jle    11e0 <matrix_multiplication+0xb0>
mov    (%r15,%r10,8),%rax
mov    (%r12,%r10,8),%rcx
movsd  (%rcx,%rdx,8),%xmm0
xor    %ebp,%ebp
cmp    $0x1,%r14d
je     1249 <matrix_multiplication+0x119>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsd  (%rax,%rbp,8),%xmm1
mov    (%rbx,%rbp,8),%rsi
mulsd  (%rsi,%rdx,8),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rcx,%rdx,8)
movsd  0x8(%rax,%rbp,8),%xmm0
mov    0x8(%rbx,%rbp,8),%rsi
mulsd  (%rsi,%rdx,8),%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,(%rcx,%rdx,8)
add    $0x2,%rbp
cmp    %rbp,%r11
jne    1210 <matrix_multiplication+0xe0>
test   $0x1,%r8b
je     11e0 <matrix_multiplication+0xb0>
movsd  (%rax,%rbp,8),%xmm1
mov    (%rbx,%rbp,8),%rax
mulsd  (%rax,%rdx,8),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rcx,%rdx,8)
jmp    11e0 <matrix_multiplication+0xb0>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
lea    0xd73(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [57 x i8] c"ERROR: columns of mat1 and rows of mat2 are not matched!\00"
@EXIT_FAILURE = global i32 0
define double** @matrix_multiplication(double** %0, i32 %1, i32 %2, double** %3, i32 %4, i32 %5) {
%7 = alloca double**
%8 = alloca i32
%9 = alloca i32
%10 = alloca double**
%11 = alloca i32
%12 = alloca i32
%13 = alloca double**
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
store double** %0, double*** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store double** %3, double*** %10
store i32 %4, i32* %11
store i32 %5, i32* %12
%18 = load i32, i32* %9
%19 = load i32, i32* %11
%20 = icmp ne i32 %18, %19
br i1 %20, label %21, label %26
21: ; preds = %6
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %6
%27 = load i32, i32* %8
%28 = call i64 @calloc(i32 %27, i32 8)
%29 = inttoptr i64 %28 to double**
store double** %29, double*** %13
store i32 0, i32* %14
br label %30
30: ; preds = %42, %26
%31 = load i32, i32* %14
%32 = load i32, i32* %8
%33 = icmp slt i32 %31, %32
br i1 %33, label %34, label %45
34: ; preds = %30
%35 = load i32, i32* %12
%36 = call i64 @calloc(i32 %35, i32 8)
%37 = inttoptr i64 %36 to double*
%38 = load double**, double*** %13
%39 = load i32, i32* %14
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds double*, double** %38, i64 %40
store double* %37, double** %41
br label %42
42: ; preds = %34
%43 = load i32, i32* %14
%44 = add nsw i32 %43, 1
store i32 %44, i32* %14
br label %30
45: ; preds = %30
store i32 0, i32* %15
br label %46
46: ; preds = %97, %45
%47 = load i32, i32* %15
%48 = load i32, i32* %8
%49 = icmp slt i32 %47, %48
br i1 %49, label %50, label %100
50: ; preds = %46
store i32 0, i32* %16
br label %51
51: ; preds = %93, %50
%52 = load i32, i32* %16
%53 = load i32, i32* %12
%54 = icmp slt i32 %52, %53
br i1 %54, label %55, label %96
55: ; preds = %51
store i32 0, i32* %17
br label %56
56: ; preds = %89, %55
%57 = load i32, i32* %17
%58 = load i32, i32* %9
%59 = icmp slt i32 %57, %58
br i1 %59, label %60, label %92
60: ; preds = %56
%61 = load double**, double*** %7
%62 = load i32, i32* %15
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds double*, double** %61, i64 %63
%65 = load double*, double** %64
%66 = load i32, i32* %17
%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* %17
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds double*, double** %70, i64 %72
%74 = load double*, double** %73
%75 = load i32, i32* %16
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds double, double* %74, i64 %76
%78 = load double, double* %77
%79 = load double**, double*** %13
%80 = load i32, i32* %15
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds double*, double** %79, i64 %81
%83 = load double*, double** %82
%84 = load i32, i32* %16
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds double, double* %83, i64 %85
%87 = load double, double* %86
%88 = call double @llvm.fmuladd.f64(double %69, double %78, double %87)
store double %88, double* %86
br label %89
89: ; preds = %60
%90 = load i32, i32* %17
%91 = add nsw i32 %90, 1
store i32 %91, i32* %17
br label %56
92: ; preds = %56
br label %93
93: ; preds = %92
%94 = load i32, i32* %16
%95 = add nsw i32 %94, 1
store i32 %95, i32* %16
br label %51
96: ; preds = %51
br label %97
97: ; preds = %96
%98 = load i32, i32* %15
%99 = add nsw i32 %98, 1
store i32 %99, i32* %15
br label %46
100: ; preds = %46
%101 = load double**, double*** %13
ret double** %101
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [57 x i8] c"ERROR: columns of mat1 and rows of mat2 are not matched!\00"
@EXIT_FAILURE = global i32 0
define double** @matrix_multiplication(double** nocapture %0, i32 %1, i32 %2, double** nocapture %3, i32 %4, i32 %5) {
%7 = icmp eq i32 %2, %4
br i1 %7, label %13, label %8
8: ; preds = %6
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %6
%14 = tail call i64 @calloc(i32 %1, i32 8)
%15 = inttoptr i64 %14 to double**
%16 = icmp sgt i32 %1, 0
br i1 %16, label %17, label %42
17: ; preds = %13
%18 = zext i32 %1 to i64
br label %30
19: ; preds = %30
br i1 %16, label %20, label %42
20: ; preds = %19
%21 = icmp sgt i32 %5, 0
%22 = icmp sgt i32 %2, 0
%23 = zext i32 %1 to i64
%24 = zext i32 %5 to i64
%25 = zext i32 %2 to i64
%26 = and i64 %25, 1
%27 = icmp eq i32 %2, 1
%28 = and i64 %25, 4294967294
%29 = icmp eq i64 %26, 0
br label %37
30: ; preds = %17, %30
%31 = phi i64 [ 0, %17 ], [ %35, %30 ]
%32 = tail call i64 @calloc(i32 %5, i32 8)
%33 = inttoptr i64 %32 to double*
%34 = getelementptr inbounds double*, double** %15, i64 %31
store double* %33, double** %34
%35 = add nuw nsw i64 %31, 1
%36 = icmp eq i64 %35, %18
br i1 %36, label %19, label %30
37: ; preds = %20, %50
%38 = phi i64 [ 0, %20 ], [ %51, %50 ]
br i1 %21, label %39, label %50
39: ; preds = %37
%40 = getelementptr inbounds double*, double** %0, i64 %38
%41 = getelementptr inbounds double*, double** %15, i64 %38
br label %43
42: ; preds = %50, %13, %19
ret double** %15
43: ; preds = %39, %64
%44 = phi i64 [ 0, %39 ], [ %65, %64 ]
br i1 %22, label %45, label %64
45: ; preds = %43
%46 = load double*, double** %40
%47 = load double*, double** %41
%48 = getelementptr inbounds double, double* %47, i64 %44
%49 = load double, double* %48
br i1 %27, label %53, label %67
50: ; preds = %64, %37
%51 = add nuw nsw i64 %38, 1
%52 = icmp eq i64 %51, %23
br i1 %52, label %42, label %37
53: ; preds = %67, %45
%54 = phi double [ %49, %45 ], [ %85, %67 ]
%55 = phi i64 [ 0, %45 ], [ %86, %67 ]
br i1 %29, label %64, label %56
56: ; preds = %53
%57 = getelementptr inbounds double, double* %46, i64 %55
%58 = load double, double* %57
%59 = getelementptr inbounds double*, double** %3, i64 %55
%60 = load double*, double** %59
%61 = getelementptr inbounds double, double* %60, i64 %44
%62 = load double, double* %61
%63 = tail call double @llvm.fmuladd.f64(double %58, double %62, double %54)
store double %63, double* %48
br label %64
64: ; preds = %56, %53, %43
%65 = add nuw nsw i64 %44, 1
%66 = icmp eq i64 %65, %24
br i1 %66, label %50, label %43
67: ; preds = %45, %67
%68 = phi double [ %85, %67 ], [ %49, %45 ]
%69 = phi i64 [ %86, %67 ], [ 0, %45 ]
%70 = phi i64 [ %87, %67 ], [ 0, %45 ]
%71 = getelementptr inbounds double, double* %46, i64 %69
%72 = load double, double* %71
%73 = getelementptr inbounds double*, double** %3, i64 %69
%74 = load double*, double** %73
%75 = getelementptr inbounds double, double* %74, i64 %44
%76 = load double, double* %75
%77 = tail call double @llvm.fmuladd.f64(double %72, double %76, double %68)
store double %77, double* %48
%78 = or i64 %69, 1
%79 = getelementptr inbounds double, double* %46, i64 %78
%80 = load double, double* %79
%81 = getelementptr inbounds double*, double** %3, i64 %78
%82 = load double*, double** %81
%83 = getelementptr inbounds double, double* %82, i64 %44
%84 = load double, double* %83
%85 = tail call double @llvm.fmuladd.f64(double %80, double %84, double %77)
store double %85, double* %48
%86 = add nuw nsw i64 %69, 2
%87 = add i64 %70, 2
%88 = icmp eq i64 %87, %28
br i1 %88, label %53, label %67
}