#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
scalar_t__ malloc(unsigned int);
double rand();
double **AllocateRandomMatrix(unsigned int N) {
  double **M;
  M = (double **)malloc(N * sizeof(double *));
  for (int i = 0; i < N; ++i) {
    M[i] = (double *)malloc(N * sizeof(double));
    for (int j = 0; j < N; ++j) {
      M[i][j] = rand();
    }
  }
  return M;
}


x86 assembly (clang, -O0):
<AllocateRandomMatrix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
shl    $0x3,%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jae    11bd <AllocateRandomMatrix+0x9d>
mov    -0x4(%rbp),%eax
shl    $0x3,%rax
mov    %eax,%edi
call   1030 <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
cmp    -0x4(%rbp),%eax
jae    11aa <AllocateRandomMatrix+0x8a>
mov    $0x0,%al
call   1040 <rand@plt>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x18(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1174 <AllocateRandomMatrix+0x54>
jmp    11af <AllocateRandomMatrix+0x8f>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1144 <AllocateRandomMatrix+0x24>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<AllocateRandomMatrix>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%r14d
lea    0x0(,%r14,8),%r15d
mov    %r15d,%edi
call   1030 <malloc@plt>
mov    %rax,%r12
test   %r14d,%r14d
je     1182 <AllocateRandomMatrix+0x62>
mov    %r14d,%r14d
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
call   1030 <malloc@plt>
mov    %rax,(%r12,%rbx,8)
xor    %ebp,%ebp
xchg   %ax,%ax
xor    %eax,%eax
call   1040 <rand@plt>
mov    (%r12,%rbx,8),%rax
movsd  %xmm0,(%rax,%rbp,8)
add    $0x1,%rbp
cmp    %rbp,%r14
jne    1160 <AllocateRandomMatrix+0x40>
add    $0x1,%rbx
cmp    %r14,%rbx
jne    1150 <AllocateRandomMatrix+0x30>
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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