int main(void) {
  int **mat1 = (int **)malloc(4 * sizeof(int *));
  for (int i = 0; i < 4; i++)
    mat1[i] = (int *)malloc(3 * sizeof(int));
  int **mat2 = (int **)malloc(4 * sizeof(int *));
  for (int j = 0; j < 3; j++)
    mat2[j] = (int *)malloc(4 * sizeof(int));
  genMat(mat1, 4, 3);
  genMat(mat2, 3, 4);
  printMat(mat1, 4, 3);
  printMat(mat2, 3, 4);
  for (int i = 0; i < 4; i++)
    free(mat1[i]);
  free(mat1);
  for (int j = 0; j < 3; j++)
    free(mat2[j]);
  free(mat2);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x20,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x4,-0x14(%rbp)
jge    1195 <main+0x55>
mov    $0xc,%edi
call   1050 <malloc@plt>
mov    %rax,%rdx
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1164 <main+0x24>
mov    $0x20,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x3,-0x24(%rbp)
jge    11db <main+0x9b>
mov    $0x10,%edi
call   1050 <malloc@plt>
mov    %rax,%rdx
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11aa <main+0x6a>
mov    -0x10(%rbp),%rdi
mov    $0x4,%esi
mov    $0x3,%edx
mov    $0x0,%al
call   1060 <genMat@plt>
mov    -0x20(%rbp),%rdi
mov    $0x3,%esi
mov    $0x4,%edx
mov    $0x0,%al
call   1060 <genMat@plt>
mov    -0x10(%rbp),%rdi
mov    $0x4,%esi
mov    $0x3,%edx
mov    $0x0,%al
call   1040 <printMat@plt>
mov    -0x20(%rbp),%rdi
mov    $0x3,%esi
mov    $0x4,%edx
mov    $0x0,%al
call   1040 <printMat@plt>
movl   $0x0,-0x28(%rbp)
cmpl   $0x4,-0x28(%rbp)
jge    125f <main+0x11f>
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1236 <main+0xf6>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x2c(%rbp)
cmpl   $0x3,-0x2c(%rbp)
jge    1298 <main+0x158>
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    126f <main+0x12f>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    $0x20,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    $0xc,%edi
call   1050 <malloc@plt>
mov    %rax,(%rbx)
mov    $0xc,%edi
call   1050 <malloc@plt>
mov    %rax,0x8(%rbx)
mov    $0xc,%edi
call   1050 <malloc@plt>
mov    %rax,0x10(%rbx)
mov    $0xc,%edi
call   1050 <malloc@plt>
mov    %rax,0x18(%rbx)
mov    $0x20,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    $0x10,%edi
call   1050 <malloc@plt>
mov    %rax,(%r14)
mov    $0x10,%edi
call   1050 <malloc@plt>
mov    %rax,0x8(%r14)
mov    $0x10,%edi
call   1050 <malloc@plt>
mov    %rax,0x10(%r14)
mov    %rbx,%rdi
mov    $0x4,%esi
mov    $0x3,%edx
xor    %eax,%eax
call   1060 <genMat@plt>
mov    %r14,%rdi
mov    $0x3,%esi
mov    $0x4,%edx
xor    %eax,%eax
call   1060 <genMat@plt>
mov    %rbx,%rdi
mov    $0x4,%esi
mov    $0x3,%edx
xor    %eax,%eax
call   1040 <printMat@plt>
mov    %r14,%rdi
mov    $0x3,%esi
mov    $0x4,%edx
xor    %eax,%eax
call   1040 <printMat@plt>
mov    (%rbx),%rdi
call   1030 <free@plt>
mov    0x8(%rbx),%rdi
call   1030 <free@plt>
mov    0x10(%rbx),%rdi
call   1030 <free@plt>
mov    0x18(%rbx),%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    (%r14),%rdi
call   1030 <free@plt>
mov    0x8(%r14),%rdi
call   1030 <free@plt>
mov    0x10(%r14),%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @main() {
%1 = alloca i32
%2 = alloca i32**
%3 = alloca i32
%4 = alloca i32**
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = call i8* @malloc(i64 32)
%9 = bitcast i8* %8 to i32**
store i32** %9, i32*** %2
store i32 0, i32* %3
br label %10
10: ; preds = %20, %0
%11 = load i32, i32* %3
%12 = icmp slt i32 %11, 4
br i1 %12, label %13, label %23
13: ; preds = %10
%14 = call i8* @malloc(i64 12)
%15 = bitcast i8* %14 to i32*
%16 = load i32**, i32*** %2
%17 = load i32, i32* %3
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i32*, i32** %16, i64 %18
store i32* %15, i32** %19
br label %20
20: ; preds = %13
%21 = load i32, i32* %3
%22 = add nsw i32 %21, 1
store i32 %22, i32* %3
br label %10
23: ; preds = %10
%24 = call i8* @malloc(i64 32)
%25 = bitcast i8* %24 to i32**
store i32** %25, i32*** %4
store i32 0, i32* %5
br label %26
26: ; preds = %36, %23
%27 = load i32, i32* %5
%28 = icmp slt i32 %27, 3
br i1 %28, label %29, label %39
29: ; preds = %26
%30 = call i8* @malloc(i64 16)
%31 = bitcast i8* %30 to i32*
%32 = load i32**, i32*** %4
%33 = load i32, i32* %5
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i32*, i32** %32, i64 %34
store i32* %31, i32** %35
br label %36
36: ; preds = %29
%37 = load i32, i32* %5
%38 = add nsw i32 %37, 1
store i32 %38, i32* %5
br label %26
39: ; preds = %26
%40 = load i32**, i32*** %2
%41 = call i32 (i32**, i32, i32, ...) bitcast (i32 (...)* @genMat to i32 (i32**, i32, i32, ...)*)(i32** %40, i32 4, i32 3)
%42 = load i32**, i32*** %4
%43 = call i32 (i32**, i32, i32, ...) bitcast (i32 (...)* @genMat to i32 (i32**, i32, i32, ...)*)(i32** %42, i32 3, i32 4)
%44 = load i32**, i32*** %2
%45 = call i32 (i32**, i32, i32, ...) bitcast (i32 (...)* @printMat to i32 (i32**, i32, i32, ...)*)(i32** %44, i32 4, i32 3)
%46 = load i32**, i32*** %4
%47 = call i32 (i32**, i32, i32, ...) bitcast (i32 (...)* @printMat to i32 (i32**, i32, i32, ...)*)(i32** %46, i32 3, i32 4)
store i32 0, i32* %6
br label %48
48: ; preds = %58, %39
%49 = load i32, i32* %6
%50 = icmp slt i32 %49, 4
br i1 %50, label %51, label %61
51: ; preds = %48
%52 = load i32**, i32*** %2
%53 = load i32, i32* %6
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i32*, i32** %52, i64 %54
%56 = load i32*, i32** %55
%57 = bitcast i32* %56 to i8*
br label %58
58: ; preds = %51
%59 = load i32, i32* %6
%60 = add nsw i32 %59, 1
store i32 %60, i32* %6
br label %48
61: ; preds = %48
%62 = load i32**, i32*** %2
%63 = bitcast i32** %62 to i8*
store i32 0, i32* %7
br label %64
64: ; preds = %74, %61
%65 = load i32, i32* %7
%66 = icmp slt i32 %65, 3
br i1 %66, label %67, label %77
67: ; preds = %64
%68 = load i32**, i32*** %4
%69 = load i32, i32* %7
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i32*, i32** %68, i64 %70
%72 = load i32*, i32** %71
%73 = bitcast i32* %72 to i8*
br label %74
74: ; preds = %67
%75 = load i32, i32* %7
%76 = add nsw i32 %75, 1
store i32 %76, i32* %7
br label %64
77: ; preds = %64
%78 = load i32**, i32*** %4
%79 = bitcast i32** %78 to i8*
ret i32 0
}

LLVM IR (-O2):
define i32 @main() {
%1 = tail call dereferenceable_or_null(32) i8* @malloc(i64 32)
%2 = bitcast i8* %1 to i32**
%3 = tail call dereferenceable_or_null(12) i8* @malloc(i64 12)
%4 = bitcast i8* %1 to i8**
store i8* %3, i8** %4
%5 = tail call dereferenceable_or_null(12) i8* @malloc(i64 12)
%6 = getelementptr inbounds i32*, i32** %2, i64 1
%7 = bitcast i32** %6 to i8**
store i8* %5, i8** %7
%8 = tail call dereferenceable_or_null(12) i8* @malloc(i64 12)
%9 = getelementptr inbounds i32*, i32** %2, i64 2
%10 = bitcast i32** %9 to i8**
store i8* %8, i8** %10
%11 = tail call dereferenceable_or_null(12) i8* @malloc(i64 12)
%12 = getelementptr inbounds i32*, i32** %2, i64 3
%13 = bitcast i32** %12 to i8**
store i8* %11, i8** %13
%14 = tail call dereferenceable_or_null(32) i8* @malloc(i64 32)
%15 = bitcast i8* %14 to i32**
%16 = tail call dereferenceable_or_null(16) i8* @malloc(i64 16)
%17 = bitcast i8* %14 to i8**
store i8* %16, i8** %17
%18 = tail call dereferenceable_or_null(16) i8* @malloc(i64 16)
%19 = getelementptr inbounds i32*, i32** %15, i64 1
%20 = bitcast i32** %19 to i8**
store i8* %18, i8** %20
%21 = tail call dereferenceable_or_null(16) i8* @malloc(i64 16)
%22 = getelementptr inbounds i32*, i32** %15, i64 2
%23 = bitcast i32** %22 to i8**
store i8* %21, i8** %23
%24 = tail call i32 (i32**, i32, i32, ...) bitcast (i32 (...)* @genMat to i32 (i32**, i32, i32, ...)*)(i32** %2, i32 4, i32 3)
%25 = tail call i32 (i32**, i32, i32, ...) bitcast (i32 (...)* @genMat to i32 (i32**, i32, i32, ...)*)(i32** %15, i32 3, i32 4)
%26 = tail call i32 (i32**, i32, i32, ...) bitcast (i32 (...)* @printMat to i32 (i32**, i32, i32, ...)*)(i32** %2, i32 4, i32 3)
%27 = tail call i32 (i32**, i32, i32, ...) bitcast (i32 (...)* @printMat to i32 (i32**, i32, i32, ...)*)(i32** %15, i32 3, i32 4)
%28 = load i8*, i8** %4
tail call void @free(i8* %28)
%29 = load i8*, i8** %7
tail call void @free(i8* %29)
%30 = load i8*, i8** %10
tail call void @free(i8* %30)
%31 = load i8*, i8** %13
tail call void @free(i8* %31)
tail call void @free(i8* %1)
%32 = load i8*, i8** %17
tail call void @free(i8* %32)
%33 = load i8*, i8** %20
tail call void @free(i8* %33)
%34 = load i8*, i8** %23
tail call void @free(i8* %34)
tail call void @free(i8* %14)
ret i32 0
}