void testHeap2D() {
  int lenX = 10;
  int lenY = 5;
  int *ar = (int *)malloc(lenX * lenY * 4);
  int **ar2D = (int **)malloc(lenX * 4);
  int i;
  for (i = 0; i < lenX; i++) {
    ar2D[i] = ar + lenY * i;
  }
  testBrackets2D(ar2D, lenX, lenY);
  free(ar);
  free(ar2D);
}


x86 assembly (clang, -O0):
<testHeap2D>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0xa,-0x4(%rbp)
movl   $0x5,-0x8(%rbp)
mov    -0x4(%rbp),%eax
imul   -0x8(%rbp),%eax
shl    $0x2,%eax
movslq %eax,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
shl    $0x2,%eax
movslq %eax,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11af <testHeap2D+0x7f>
mov    -0x10(%rbp),%rdx
mov    -0x8(%rbp),%eax
imul   -0x1c(%rbp),%eax
cltq
shl    $0x2,%rax
add    %rax,%rdx
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1175 <testHeap2D+0x45>
mov    -0x18(%rbp),%rdi
mov    -0x4(%rbp),%esi
mov    -0x8(%rbp),%edx
mov    $0x0,%al
call   1040 <testBrackets2D@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<testHeap2D>:
push   %r14
push   %rbx
push   %rax
mov    $0xc8,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    $0x28,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %r14,(%rax)
movq   %r14,%xmm0
pshufd $0x44,%xmm0,%xmm0
movdqa 0xe9d(%rip),%xmm1
paddq  %xmm0,%xmm1
movdqu %xmm1,0x8(%rax)
movdqa 0xe9c(%rip),%xmm1
paddq  %xmm0,%xmm1
movdqu %xmm1,0x18(%rax)
movdqa 0xe9b(%rip),%xmm1
paddq  %xmm0,%xmm1
paddq  0xe9f(%rip),%xmm0
movdqu %xmm1,0x28(%rax)
movdqu %xmm0,0x38(%rax)
lea    0xb4(%r14),%rax
mov    %rax,0x48(%rbx)
mov    %rbx,%rdi
mov    $0xa,%esi
mov    $0x5,%edx
xor    %eax,%eax
call   1040 <testBrackets2D@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <free@plt>

LLVM IR (-O0):
define void @testHeap2D() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32**
%5 = alloca i32
store i32 10, i32* %1
store i32 5, i32* %2
%6 = load i32, i32* %1
%7 = load i32, i32* %2
%8 = mul nsw i32 %6, %7
%9 = mul nsw i32 %8, 4
%10 = sext i32 %9 to i64
%11 = call i8* @malloc(i64 %10)
%12 = bitcast i8* %11 to i32*
store i32* %12, i32** %3
%13 = load i32, i32* %1
%14 = mul nsw i32 %13, 4
%15 = sext i32 %14 to i64
%16 = call i8* @malloc(i64 %15)
%17 = bitcast i8* %16 to i32**
store i32** %17, i32*** %4
store i32 0, i32* %5
br label %18
18: ; preds = %33, %0
%19 = load i32, i32* %5
%20 = load i32, i32* %1
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %36
22: ; preds = %18
%23 = load i32*, i32** %3
%24 = load i32, i32* %2
%25 = load i32, i32* %5
%26 = mul nsw i32 %24, %25
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i32, i32* %23, i64 %27
%29 = load i32**, i32*** %4
%30 = load i32, i32* %5
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i32*, i32** %29, i64 %31
store i32* %28, i32** %32
br label %33
33: ; preds = %22
%34 = load i32, i32* %5
%35 = add nsw i32 %34, 1
store i32 %35, i32* %5
br label %18
36: ; preds = %18
%37 = load i32**, i32*** %4
%38 = load i32, i32* %1
%39 = load i32, i32* %2
%40 = call i32 (i32**, i32, i32, ...) bitcast (i32 (...)* @testBrackets2D to i32 (i32**, i32, i32, ...)*)(i32** %37, i32 %38, i32 %39)
%41 = load i32*, i32** %3
%42 = bitcast i32* %41 to i8*
%43 = load i32**, i32*** %4
%44 = bitcast i32** %43 to i8*
ret void
}

LLVM IR (-O2):
define void @testHeap2D() {
%1 = tail call dereferenceable_or_null(200) i8* @malloc(i64 200)
%2 = bitcast i8* %1 to i32*
%3 = tail call dereferenceable_or_null(40) i8* @malloc(i64 40)
%4 = bitcast i8* %3 to i32**
%5 = bitcast i8* %3 to i8**
store i8* %1, i8** %5
%6 = getelementptr inbounds i32*, i32** %4, i64 1
%7 = insertelement <2 x i32*> poison, i32* %2, i64 0
%8 = shufflevector <2 x i32*> %7, <2 x i32*> poison, <2 x i32> zeroinitializer
%9 = getelementptr i32, <2 x i32*> %8, <2 x i64> <i64 5, i64 10>
%10 = bitcast i32** %6 to <2 x i32*>*
store <2 x i32*> %9, <2 x i32*>* %10
%11 = getelementptr inbounds i32*, i32** %4, i64 3
%12 = getelementptr i32, <2 x i32*> %8, <2 x i64> <i64 15, i64 20>
%13 = bitcast i32** %11 to <2 x i32*>*
store <2 x i32*> %12, <2 x i32*>* %13
%14 = getelementptr inbounds i32*, i32** %4, i64 5
%15 = getelementptr i32, <2 x i32*> %8, <2 x i64> <i64 25, i64 30>
%16 = bitcast i32** %14 to <2 x i32*>*
store <2 x i32*> %15, <2 x i32*>* %16
%17 = getelementptr inbounds i32*, i32** %4, i64 7
%18 = getelementptr i32, <2 x i32*> %8, <2 x i64> <i64 35, i64 40>
%19 = bitcast i32** %17 to <2 x i32*>*
store <2 x i32*> %18, <2 x i32*>* %19
%20 = getelementptr inbounds i32, i32* %2, i64 45
%21 = getelementptr inbounds i32*, i32** %4, i64 9
store i32* %20, i32** %21
%22 = tail call i32 (i32**, i32, i32, ...) bitcast (i32 (...)* @testBrackets2D to i32 (i32**, i32, i32, ...)*)(i32** %4, i32 10, i32 5)
tail call void @free(i8* %1)
tail call void @free(i8* %3)
ret void
}