int main() {
  int **p, i, j, k = 1;
  p = (int **)malloc(sizeof(int *) * 4);
  for (i = 0; i < 4; i++) {
    p[i] = (int *)malloc(sizeof(int) * (i + 1));
    for (j = 0; j < i + 1; j++)
      p[i][j] = k++;
  }
  for (i = 0; i < 4; i++) {
    for (j = 0; j < i + 1; j++)
      printf("%3d", p[i][j]);
    putchar('\n');
  }
  for (i = 0; i < 4; i++)
    free(p[i]);
  free(p);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    $0x20,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x4,-0x14(%rbp)
jge    11ed <main+0xad>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
movslq %eax,%rdi
shl    $0x2,%rdi
call   1060 <malloc@plt>
mov    %rax,%rdx
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jge    11da <main+0x9a>
mov    -0x1c(%rbp),%edx
mov    %edx,%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x18(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    119d <main+0x5d>
jmp    11df <main+0x9f>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    116b <main+0x2b>
movl   $0x0,-0x14(%rbp)
cmpl   $0x4,-0x14(%rbp)
jge    125f <main+0x11f>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jge    1245 <main+0x105>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1205 <main+0xc5>
mov    $0xa,%edi
mov    $0x0,%al
call   1040 <putchar@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11f4 <main+0xb4>
movl   $0x0,-0x14(%rbp)
cmpl   $0x4,-0x14(%rbp)
jge    128f <main+0x14f>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1266 <main+0x126>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    $0x8,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
movabs $0x300000002,%rax
mov    %rax,(%r14)
mov    $0xc,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
movabs $0x500000004,%rax
mov    %rax,(%r15)
movl   $0x6,0x8(%r15)
mov    $0x10,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
movaps 0xe68(%rip),%xmm0
movups %xmm0,(%rax)
lea    0xe6e(%rip),%r12
mov    %r12,%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
xor    %eax,%eax
call   1040 <putchar@plt>
mov    (%r14),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x4(%r14),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
xor    %eax,%eax
call   1040 <putchar@plt>
mov    (%r15),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x4(%r15),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%r15),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
xor    %eax,%eax
call   1040 <putchar@plt>
mov    (%rbx),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x4(%rbx),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rbx),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0xc(%rbx),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
xor    %eax,%eax
call   1040 <putchar@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [4 x i8] c"%3d\00"
define i32 @main() {
%1 = tail call dereferenceable_or_null(8) i8* @malloc(i64 8)
%2 = bitcast i8* %1 to i32*
store i32 2, i32* %2
%3 = getelementptr inbounds i32, i32* %2, i64 1
store i32 3, i32* %3
%4 = tail call dereferenceable_or_null(12) i8* @malloc(i64 12)
%5 = bitcast i8* %4 to i32*
store i32 4, i32* %5
%6 = getelementptr inbounds i32, i32* %5, i64 1
store i32 5, i32* %6
%7 = getelementptr inbounds i32, i32* %5, i64 2
store i32 6, i32* %7
%8 = tail call dereferenceable_or_null(16) i8* @malloc(i64 16)
%9 = bitcast i8* %8 to i32*
%10 = getelementptr inbounds i32, i32* %9, i64 1
%11 = getelementptr inbounds i32, i32* %9, i64 2
%12 = getelementptr inbounds i32, i32* %9, i64 3
%13 = bitcast i8* %8 to <4 x i32>*
store <4 x i32> <i32 7, i32 8, i32 9, i32 10>, <4 x i32>* %13
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 1)
%15 = tail call i32 (i32, ...) bitcast (i32 (...)* @putchar to i32 (i32, ...)*)(i32 10)
%16 = load i32, i32* %2
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = load i32, i32* %3
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = tail call i32 (i32, ...) bitcast (i32 (...)* @putchar to i32 (i32, ...)*)(i32 10)
%21 = load i32, i32* %5
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = load i32, i32* %6
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = load i32, i32* %7
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %25)
%27 = tail call i32 (i32, ...) bitcast (i32 (...)* @putchar to i32 (i32, ...)*)(i32 10)
%28 = load i32, i32* %9
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %28)
%30 = load i32, i32* %10
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %30)
%32 = load i32, i32* %11
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %32)
%34 = load i32, i32* %12
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %34)
%36 = tail call i32 (i32, ...) bitcast (i32 (...)* @putchar to i32 (i32, ...)*)(i32 10)
tail call void @free(i8* %1)
tail call void @free(i8* %4)
tail call void @free(i8* %8)
ret i32 0
}