#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(int);
int allocate_matrix(double ***M, int nx, int ny) {
  int i;
  (*M) = (double **)malloc(sizeof(double *) * nx);
  if (*M == NULL)
    return 1;
  for (i = 0; i < nx; i++) {
    (*M)[i] = (double *)malloc(sizeof(double) * ny);
    if ((*M)[i] == NULL)
      return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<allocate_matrix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1155 <allocate_matrix+0x45>
movl   $0x1,-0x4(%rbp)
jmp    11c5 <allocate_matrix+0xb5>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11be <allocate_matrix+0xae>
movslq -0x18(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,%rdx
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x1c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x1c(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
jne    11ab <allocate_matrix+0x9b>
movl   $0x1,-0x4(%rbp)
jmp    11c5 <allocate_matrix+0xb5>
jmp    11b0 <allocate_matrix+0xa0>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    115c <allocate_matrix+0x4c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<allocate_matrix>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r12d
mov    %esi,%r15d
mov    %rdi,%rbx
lea    0x0(,%r15,8),%edi
call   1030 <malloc@plt>
mov    %rax,(%rbx)
mov    $0x1,%r14d
test   %rax,%rax
je     1175 <allocate_matrix+0x65>
test   %r15d,%r15d
jle    1172 <allocate_matrix+0x62>
shl    $0x3,%r12d
mov    %r15d,%r15d
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
mov    %r12d,%edi
call   1030 <malloc@plt>
mov    (%rbx),%rcx
mov    %rax,(%rcx,%rbp,8)
mov    (%rbx),%rax
cmpq   $0x0,(%rax,%rbp,8)
je     1175 <allocate_matrix+0x65>
add    $0x1,%rbp
cmp    %rbp,%r15
jne    1150 <allocate_matrix+0x40>
xor    %r14d,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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