#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 */
scalar_t__ malloc(int);
int /*<<< orphan*/ multiplyMatrices(int **, int **, int, int, int, int);
int /*<<< orphan*/ printMatrix(int **, int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ readMatrix(int **, int, int);
int /*<<< orphan*/ scanf(char *, int *, int *);
int main(void) {
  int **mat1 = NULL, **mat2 = NULL, m1, n1, m2, n2, i;
  printf("\n m1*n1 = ?\n");
  scanf("%d %d", &m1, &n1);
  printf("\n m2*n2 = ?\n");
  scanf("%d %d", &m2, &n2);
  if (n1 != m2) {
    printf("\nERROR : incompatable matrices\n");
    return -1;
  }
  mat1 = (int **)malloc(m1 * sizeof(int *));
  if (NULL == mat1) {
    printf("\nFAILURE : memory allocation\n");
    return -1;
  }
  for (i = 0; i < m1; i++) {
    mat1[i] = (int *)malloc(n1 * sizeof(int));
  }
  mat2 = (int **)malloc(m2 * sizeof(int *));
  if (NULL == mat2) {
    printf("\nFAILURE : memory allocation\n");
    return -1;
  }
  for (i = 0; i < m2; i++) {
    mat2[i] = (int *)malloc(n2 * sizeof(int));
  }
  readMatrix(mat1, m1, n1);
  readMatrix(mat2, m2, n2);
  printMatrix(mat1, m1, n1);
  printMatrix(mat2, m2, n2);
  multiplyMatrices(mat1, mat2, m1, n1, m2, n2);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    0xe7a(%rip),%rdi
call   1040 <printf@plt>
lea    0xe7b(%rip),%rdi
lea    -0x1c(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1070 <scanf@plt>
lea    0xe6d(%rip),%rdi
call   1040 <printf@plt>
lea    0xe5b(%rip),%rdi
lea    -0x24(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1070 <scanf@plt>
mov    -0x20(%rbp),%eax
cmp    -0x24(%rbp),%eax
je     11e3 <main+0x83>
lea    0xe4e(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132d <main+0x1cd>
movslq -0x1c(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    121a <main+0xba>
lea    0xe37(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132d <main+0x1cd>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1259 <main+0xf9>
movslq -0x20(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,%rdx
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1221 <main+0xc1>
movslq -0x24(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    1290 <main+0x130>
lea    0xdc1(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132d <main+0x1cd>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    12cf <main+0x16f>
movslq -0x28(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1297 <main+0x137>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
call   1030 <readMatrix@plt>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
call   1030 <readMatrix@plt>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
call   1050 <printMatrix@plt>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
call   1050 <printMatrix@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
mov    -0x24(%rbp),%r8d
mov    -0x28(%rbp),%r9d
call   1080 <multiplyMatrices@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xe96(%rip),%rdi
call   1040 <puts@plt>
lea    0xe84(%rip),%rbx
lea    0x4(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    %rbx,%rdi
call   1070 <scanf@plt>
lea    0xe7d(%rip),%rdi
call   1040 <puts@plt>
mov    %rsp,%rsi
lea    0xc(%rsp),%rdx
mov    %rbx,%rdi
call   1070 <scanf@plt>
mov    0x8(%rsp),%eax
cmp    (%rsp),%eax
jne    129b <main+0x13b>
mov    0x4(%rsp),%edi
shl    $0x3,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     12a4 <main+0x144>
mov    %rax,%r15
cmpl   $0x0,0x4(%rsp)
jle    11fe <main+0x9e>
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    0x8(%rsp),%edi
shl    $0x2,%edi
call   1060 <malloc@plt>
mov    %rax,(%r15,%rbx,8)
add    $0x1,%rbx
movslq 0x4(%rsp),%rax
cmp    %rax,%rbx
jl     11e0 <main+0x80>
mov    (%rsp),%edi
shl    $0x3,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     12a4 <main+0x144>
mov    %rax,%r14
cmpl   $0x0,(%rsp)
jle    123d <main+0xdd>
xor    %ebx,%ebx
nopl   (%rax)
mov    0xc(%rsp),%edi
shl    $0x2,%edi
call   1060 <malloc@plt>
mov    %rax,(%r14,%rbx,8)
add    $0x1,%rbx
movslq (%rsp),%rax
cmp    %rax,%rbx
jl     1220 <main+0xc0>
mov    0x4(%rsp),%esi
mov    0x8(%rsp),%edx
mov    %r15,%rdi
call   1030 <readMatrix@plt>
mov    (%rsp),%esi
mov    0xc(%rsp),%edx
mov    %r14,%rdi
call   1030 <readMatrix@plt>
mov    0x4(%rsp),%esi
mov    0x8(%rsp),%edx
mov    %r15,%rdi
call   1050 <printMatrix@plt>
mov    (%rsp),%esi
mov    0xc(%rsp),%edx
mov    %r14,%rdi
call   1050 <printMatrix@plt>
mov    0x4(%rsp),%edx
mov    0x8(%rsp),%ecx
mov    (%rsp),%r8d
mov    0xc(%rsp),%r9d
mov    %r15,%rdi
mov    %r14,%rsi
call   1080 <multiplyMatrices@plt>
xor    %eax,%eax
jmp    12b5 <main+0x155>
lea    0xd99(%rip),%rdi
jmp    12ab <main+0x14b>
lea    0xd73(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [13 x i8] c"\0A m1*n1 = ?\0A\00"
@.str.1 = [6 x i8] c"%d %d\00"
@.str.2 = [13 x i8] c"\0A m2*n2 = ?\0A\00"
@.str.3 = [32 x i8] c"\0AERROR : incompatable matrices\0A\00"
@.str.4 = [30 x i8] c"\0AFAILURE : memory allocation\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32**
%3 = alloca i32**
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
store i32** null, i32*** %2
store i32** null, i32*** %3
%9 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32* %4, i32* %5)
%11 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%12 = call i32 @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32* %6, i32* %7)
%13 = load i32, i32* %5
%14 = load i32, i32* %6
%15 = icmp ne i32 %13, %14
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %1
br label %102
18: ; preds = %0
%19 = load i32, i32* %4
%20 = sext i32 %19 to i64
%21 = mul i64 %20, 8
%22 = trunc i64 %21 to i32
%23 = call i64 @malloc(i32 %22)
%24 = inttoptr i64 %23 to i32**
store i32** %24, i32*** %2
%25 = load i32**, i32*** %2
%26 = icmp eq i32** null, %25
br i1 %26, label %27, label %29
27: ; preds = %18
%28 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %1
br label %102
29: ; preds = %18
store i32 0, i32* %8
br label %30
30: ; preds = %45, %29
%31 = load i32, i32* %8
%32 = load i32, i32* %4
%33 = icmp slt i32 %31, %32
br i1 %33, label %34, label %48
34: ; preds = %30
%35 = load i32, i32* %5
%36 = sext i32 %35 to i64
%37 = mul i64 %36, 4
%38 = trunc i64 %37 to i32
%39 = call i64 @malloc(i32 %38)
%40 = inttoptr i64 %39 to i32*
%41 = load i32**, i32*** %2
%42 = load i32, i32* %8
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i32*, i32** %41, i64 %43
store i32* %40, i32** %44
br label %45
45: ; preds = %34
%46 = load i32, i32* %8
%47 = add nsw i32 %46, 1
store i32 %47, i32* %8
br label %30
48: ; preds = %30
%49 = load i32, i32* %6
%50 = sext i32 %49 to i64
%51 = mul i64 %50, 8
%52 = trunc i64 %51 to i32
%53 = call i64 @malloc(i32 %52)
%54 = inttoptr i64 %53 to i32**
store i32** %54, i32*** %3
%55 = load i32**, i32*** %3
%56 = icmp eq i32** null, %55
br i1 %56, label %57, label %59
57: ; preds = %48
%58 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %1
br label %102
59: ; preds = %48
store i32 0, i32* %8
br label %60
60: ; preds = %75, %59
%61 = load i32, i32* %8
%62 = load i32, i32* %6
%63 = icmp slt i32 %61, %62
br i1 %63, label %64, label %78
64: ; preds = %60
%65 = load i32, i32* %7
%66 = sext i32 %65 to i64
%67 = mul i64 %66, 4
%68 = trunc i64 %67 to i32
%69 = call i64 @malloc(i32 %68)
%70 = inttoptr i64 %69 to i32*
%71 = load i32**, i32*** %3
%72 = load i32, i32* %8
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i32*, i32** %71, i64 %73
store i32* %70, i32** %74
br label %75
75: ; preds = %64
%76 = load i32, i32* %8
%77 = add nsw i32 %76, 1
store i32 %77, i32* %8
br label %60
78: ; preds = %60
%79 = load i32**, i32*** %2
%80 = load i32, i32* %4
%81 = load i32, i32* %5
%82 = call i32 @readMatrix(i32** %79, i32 %80, i32 %81)
%83 = load i32**, i32*** %3
%84 = load i32, i32* %6
%85 = load i32, i32* %7
%86 = call i32 @readMatrix(i32** %83, i32 %84, i32 %85)
%87 = load i32**, i32*** %2
%88 = load i32, i32* %4
%89 = load i32, i32* %5
%90 = call i32 @printMatrix(i32** %87, i32 %88, i32 %89)
%91 = load i32**, i32*** %3
%92 = load i32, i32* %6
%93 = load i32, i32* %7
%94 = call i32 @printMatrix(i32** %91, i32 %92, i32 %93)
%95 = load i32**, i32*** %2
%96 = load i32**, i32*** %3
%97 = load i32, i32* %4
%98 = load i32, i32* %5
%99 = load i32, i32* %6
%100 = load i32, i32* %7
%101 = call i32 @multiplyMatrices(i32** %95, i32** %96, i32 %97, i32 %98, i32 %99, i32 %100)
store i32 0, i32* %1
br label %102
102: ; preds = %78, %57, %27, %16
%103 = load i32, i32* %1
ret i32 %103
}

LLVM IR (-O2):
@.str.1 = [6 x i8] c"%d %d\00"
@str = [12 x i8] c"\0A m1*n1 = ?\00"
@str.5 = [12 x i8] c"\0A m2*n2 = ?\00"
@str.7 = [29 x i8] c"\0AFAILURE : memory allocation\00"
@str.8 = [31 x i8] c"\0AERROR : incompatable matrices\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %1 to i8*
%6 = bitcast i32* %2 to i8*
%7 = bitcast i32* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%10 = call i32 @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32* %1, i32* %2)
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.5, i64 0, i64 0))
%12 = call i32 @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32* %3, i32* %4)
%13 = load i32, i32* %2
%14 = load i32, i32* %3
%15 = icmp eq i32 %13, %14
br i1 %15, label %18, label %16
16: ; preds = %0
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.8, i64 0, i64 0))
br label %80
18: ; preds = %0
%19 = load i32, i32* %1
%20 = shl i32 %19, 3
%21 = call i64 @malloc(i32 %20)
%22 = inttoptr i64 %21 to i32**
%23 = icmp eq i64 %21, 0
br i1 %23, label %27, label %24
24: ; preds = %18
%25 = load i32, i32* %1
%26 = icmp sgt i32 %25, 0
br i1 %26, label %29, label %40
27: ; preds = %18
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.7, i64 0, i64 0))
br label %80
29: ; preds = %24, %29
%30 = phi i64 [ %36, %29 ], [ 0, %24 ]
%31 = load i32, i32* %2
%32 = shl i32 %31, 2
%33 = call i64 @malloc(i32 %32)
%34 = inttoptr i64 %33 to i32*
%35 = getelementptr inbounds i32*, i32** %22, i64 %30
store i32* %34, i32** %35
%36 = add nuw nsw i64 %30, 1
%37 = load i32, i32* %1
%38 = sext i32 %37 to i64
%39 = icmp slt i64 %36, %38
br i1 %39, label %29, label %40
40: ; preds = %29, %24
%41 = load i32, i32* %3
%42 = shl i32 %41, 3
%43 = call i64 @malloc(i32 %42)
%44 = inttoptr i64 %43 to i32**
%45 = icmp eq i64 %43, 0
br i1 %45, label %49, label %46
46: ; preds = %40
%47 = load i32, i32* %3
%48 = icmp sgt i32 %47, 0
br i1 %48, label %51, label %62
49: ; preds = %40
%50 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.7, i64 0, i64 0))
br label %80
51: ; preds = %46, %51
%52 = phi i64 [ %58, %51 ], [ 0, %46 ]
%53 = load i32, i32* %4
%54 = shl i32 %53, 2
%55 = call i64 @malloc(i32 %54)
%56 = inttoptr i64 %55 to i32*
%57 = getelementptr inbounds i32*, i32** %44, i64 %52
store i32* %56, i32** %57
%58 = add nuw nsw i64 %52, 1
%59 = load i32, i32* %3
%60 = sext i32 %59 to i64
%61 = icmp slt i64 %58, %60
br i1 %61, label %51, label %62
62: ; preds = %51, %46
%63 = load i32, i32* %1
%64 = load i32, i32* %2
%65 = call i32 @readMatrix(i32** %22, i32 %63, i32 %64)
%66 = load i32, i32* %3
%67 = load i32, i32* %4
%68 = call i32 @readMatrix(i32** %44, i32 %66, i32 %67)
%69 = load i32, i32* %1
%70 = load i32, i32* %2
%71 = call i32 @printMatrix(i32** %22, i32 %69, i32 %70)
%72 = load i32, i32* %3
%73 = load i32, i32* %4
%74 = call i32 @printMatrix(i32** %44, i32 %72, i32 %73)
%75 = load i32, i32* %1
%76 = load i32, i32* %2
%77 = load i32, i32* %3
%78 = load i32, i32* %4
%79 = call i32 @multiplyMatrices(i32** %22, i32** %44, i32 %75, i32 %76, i32 %77, i32 %78)
br label %80
80: ; preds = %62, %49, %27, %16
%81 = phi i32 [ -1, %16 ], [ -1, %27 ], [ -1, %49 ], [ 0, %62 ]
ret i32 %81
}