double **alloc_square_array(int nArr) {
  int i;
  double **vec;
  assert(nArr >= 1);
  vec = malloc(nArr * sizeof(double *));
  assert(vec);
  for (i = 0; i < nArr; i++) {
    vec[i] = malloc(nArr * sizeof(double));
    assert(vec);
  }
  return vec;
}


x86 assembly (clang, -O0):
<alloc_square_array>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x1,-0x4(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
mov    $0x0,%al
call   1030 <assert@plt>
movslq -0x4(%rbp),%rdi
shl    $0x3,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x0,%al
call   1030 <assert@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11a2 <alloc_square_array+0x82>
movslq -0x4(%rbp),%rdi
shl    $0x3,%rdi
call   1040 <malloc@plt>
mov    %rax,%rdx
mov    -0x10(%rbp),%rax
movslq -0x8(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x10(%rbp),%rdi
mov    $0x0,%al
call   1030 <assert@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1161 <alloc_square_array+0x41>
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alloc_square_array>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r15d
xor    %edi,%edi
test   %r15d,%r15d
setg   %dil
xor    %eax,%eax
call   1030 <assert@plt>
movslq %r15d,%rbp
lea    0x0(,%rbp,8),%r14
mov    %r14,%rdi
call   1040 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %eax,%eax
call   1030 <assert@plt>
test   %ebp,%ebp
jle    118f <alloc_square_array+0x6f>
mov    %r15d,%r15d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%rdi
call   1040 <malloc@plt>
mov    %rax,(%rbx,%rbp,8)
mov    %rbx,%rdi
xor    %eax,%eax
call   1030 <assert@plt>
add    $0x1,%rbp
cmp    %rbp,%r15
jne    1170 <alloc_square_array+0x50>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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