#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 */
void *calloc(int, int);
double ***allocate3D(int dim1, int dim2, int dim3) {
  double ***arr;
  arr = calloc(dim1, sizeof(double **));
  for (int i = 0; i < dim1; ++i) {
    arr[i] = calloc(dim2, sizeof(double *));
    for (int j = 0; j < dim2; ++j) {
      arr[i][j] = calloc(dim3, sizeof(double));
    }
  }
  return arr;
}


x86 assembly (clang, -O0):
<allocate3D>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    -0x4(%rbp),%edi
mov    $0x8,%esi
call   1030 <calloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11b9 <allocate3D+0xa9>
mov    -0x8(%rbp),%edi
mov    $0x8,%esi
call   1030 <calloc@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11a6 <allocate3D+0x96>
mov    -0xc(%rbp),%edi
mov    $0x8,%esi
call   1030 <calloc@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x20(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1168 <allocate3D+0x58>
jmp    11ab <allocate3D+0x9b>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1139 <allocate3D+0x29>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<allocate3D>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r13d
mov    %esi,%r15d
mov    %edi,%ebp
mov    $0x8,%esi
call   1030 <calloc@plt>
mov    %rax,0x8(%rsp)
test   %ebp,%ebp
jle    119d <allocate3D+0x8d>
mov    %ebp,%eax
mov    %rax,0x10(%rsp)
mov    %r15d,%ebx
xor    %r14d,%r14d
jmp    115b <allocate3D+0x4b>
nopl   0x0(%rax,%rax,1)
add    $0x1,%r14
cmp    0x10(%rsp),%r14
je     119d <allocate3D+0x8d>
mov    %r15d,%edi
mov    $0x8,%esi
call   1030 <calloc@plt>
mov    %rax,%rbp
mov    0x8(%rsp),%rax
mov    %rbp,(%rax,%r14,8)
test   %r15d,%r15d
jle    1150 <allocate3D+0x40>
xor    %r12d,%r12d
nopl   0x0(%rax)
mov    %r13d,%edi
mov    $0x8,%esi
call   1030 <calloc@plt>
mov    %rax,0x0(%rbp,%r12,8)
add    $0x1,%r12
cmp    %r12,%rbx
jne    1180 <allocate3D+0x70>
jmp    1150 <allocate3D+0x40>
mov    0x8(%rsp),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define noalias double*** @allocate3D(i32 %0, i32 %1, i32 %2) {
%4 = tail call i8* @calloc(i32 %0, i32 8)
%5 = bitcast i8* %4 to double***
%6 = icmp sgt i32 %0, 0
br i1 %6, label %7, label %11
7: ; preds = %3
%8 = icmp sgt i32 %1, 0
%9 = zext i32 %0 to i64
%10 = zext i32 %1 to i64
br label %12
11: ; preds = %18, %3
ret double*** %5
12: ; preds = %7, %18
%13 = phi i64 [ 0, %7 ], [ %19, %18 ]
%14 = tail call i8* @calloc(i32 %1, i32 8)
%15 = getelementptr inbounds double**, double*** %5, i64 %13
%16 = bitcast double*** %15 to i8**
store i8* %14, i8** %16
%17 = bitcast i8* %14 to double**
br i1 %8, label %21, label %18
18: ; preds = %21, %12
%19 = add nuw nsw i64 %13, 1
%20 = icmp eq i64 %19, %9
br i1 %20, label %11, label %12
21: ; preds = %12, %21
%22 = phi i64 [ %26, %21 ], [ 0, %12 ]
%23 = tail call i8* @calloc(i32 %2, i32 8)
%24 = getelementptr inbounds double*, double** %17, i64 %22
%25 = bitcast double** %24 to i8**
store i8* %23, i8** %25
%26 = add nuw nsw i64 %22, 1
%27 = icmp eq i64 %26, %10
br i1 %27, label %18, label %21
}