const int **alloc2(const int N) {
  printf("* alloc2():\n");
  const int **a = malloc(N * sizeof(int *));
  null_check(a);
  for (int i = 0; i < N; i++) {
    a[i] = malloc(N * sizeof(int));
    null_check(a[i]);
  }
  return a;
}


x86 assembly (clang, -O0):
<alloc2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
lea    0xebe(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movslq -0x4(%rbp),%rdi
shl    $0x3,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x0,%al
call   1040 <null_check@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11b5 <alloc2+0x85>
movslq -0x4(%rbp),%rdi
shl    $0x2,%rdi
call   1050 <malloc@plt>
mov    %rax,%rdx
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    $0x0,%al
call   1040 <null_check@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    116c <alloc2+0x3c>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alloc2>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0xebf(%rip),%rdi
call   1030 <puts@plt>
movslq %ebx,%rbx
lea    0x0(,%rbx,8),%rdi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
xor    %eax,%eax
call   1040 <null_check@plt>
test   %ebx,%ebx
jle    119f <alloc2+0x6f>
lea    0x0(,%rbx,4),%r15
mov    %ebx,%r12d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15,%rdi
call   1050 <malloc@plt>
mov    %rax,(%r14,%rbx,8)
mov    %rax,%rdi
xor    %eax,%eax
call   1040 <null_check@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1180 <alloc2+0x50>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [13 x i8] c"* alloc2():\0A\00"
define i32** @alloc2(i32 %0) {
%2 = alloca i32
%3 = alloca i32**
%4 = alloca i32
store i32 %0, i32* %2
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* %2
%7 = sext i32 %6 to i64
%8 = mul i64 %7, 8
%9 = call i8* @malloc(i64 %8)
%10 = bitcast i8* %9 to i32**
store i32** %10, i32*** %3
%11 = load i32**, i32*** %3
%12 = call i32 (i32**, ...) bitcast (i32 (...)* @null_check to i32 (i32**, ...)*)(i32** %11)
store i32 0, i32* %4
br label %13
13: ; preds = %33, %1
%14 = load i32, i32* %4
%15 = load i32, i32* %2
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %36
17: ; preds = %13
%18 = load i32, i32* %2
%19 = sext i32 %18 to i64
%20 = mul i64 %19, 4
%21 = call i8* @malloc(i64 %20)
%22 = bitcast i8* %21 to i32*
%23 = load i32**, i32*** %3
%24 = load i32, i32* %4
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i32*, i32** %23, i64 %25
store i32* %22, i32** %26
%27 = load i32**, i32*** %3
%28 = load i32, i32* %4
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i32*, i32** %27, i64 %29
%31 = load i32*, i32** %30
%32 = call i32 (i32*, ...) bitcast (i32 (...)* @null_check to i32 (i32*, ...)*)(i32* %31)
br label %33
33: ; preds = %17
%34 = load i32, i32* %4
%35 = add nsw i32 %34, 1
store i32 %35, i32* %4
br label %13
36: ; preds = %13
%37 = load i32**, i32*** %3
ret i32** %37
}

LLVM IR (-O2):
@str = [12 x i8] c"* alloc2():\00"
define i32** @alloc2(i32 %0) {
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%3 = sext i32 %0 to i64
%4 = shl nsw i64 %3, 3
%5 = tail call i8* @malloc(i64 %4)
%6 = bitcast i8* %5 to i32**
%7 = tail call i32 (i32**, ...) bitcast (i32 (...)* @null_check to i32 (i32**, ...)*)(i32** %6)
%8 = icmp sgt i32 %0, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = shl nsw i64 %3, 2
%11 = zext i32 %0 to i64
br label %13
12: ; preds = %13, %1
ret i32** %6
13: ; preds = %9, %13
%14 = phi i64 [ 0, %9 ], [ %20, %13 ]
%15 = tail call i8* @malloc(i64 %10)
%16 = bitcast i8* %15 to i32*
%17 = getelementptr inbounds i32*, i32** %6, i64 %14
%18 = bitcast i32** %17 to i8**
store i8* %15, i8** %18
%19 = tail call i32 (i32*, ...) bitcast (i32 (...)* @null_check to i32 (i32*, ...)*)(i32* %16)
%20 = add nuw nsw i64 %14, 1
%21 = icmp eq i64 %20, %11
br i1 %21, label %12, label %13
}