#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);
void allocate_grid(int nrows, int ncols, double ***grid) {
  int *a, i, j;
  (*grid) = (double **)malloc(sizeof(double *) * (nrows + 2));
  for (i = 0; i < nrows + 2; i++) {
    (*grid)[i] = (double *)malloc(sizeof(double) * (ncols + 2));
    for (j = 0; j < ncols + 2; j++) {
      (*grid)[i][j] = 0;
    }
  }
}


x86 assembly (clang, -O0):
<allocate_grid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2,%eax
cltq
shl    $0x3,%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x4(%rbp),%ecx
add    $0x2,%ecx
cmp    %ecx,%eax
jge    11d0 <allocate_grid+0xc0>
mov    -0x8(%rbp),%eax
add    $0x2,%eax
cltq
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)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    -0x8(%rbp),%ecx
add    $0x2,%ecx
cmp    %ecx,%eax
jge    11bd <allocate_grid+0xad>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x20(%rbp),%rcx
xorps  %xmm0,%xmm0
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1183 <allocate_grid+0x73>
jmp    11c2 <allocate_grid+0xb2>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1146 <allocate_grid+0x36>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<allocate_grid>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %edi,%r12d
lea    0x10(,%r12,8),%edi
call   1040 <malloc@plt>
mov    %rax,(%rbx)
cmp    $0xffffffff,%r12d
jl     11b1 <allocate_grid+0x91>
add    $0x2,%r12d
lea    0x2(%r14),%eax
cmp    $0x2,%eax
mov    $0x1,%r15d
cmovge %eax,%r15d
lea    0x10(,%r14,8),%r13d
shl    $0x3,%r15
mov    %r12d,%r12d
xor    %ebp,%ebp
jmp    1189 <allocate_grid+0x69>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x1,%rbp
cmp    %rbp,%r12
je     11b1 <allocate_grid+0x91>
mov    %r13d,%edi
call   1040 <malloc@plt>
mov    (%rbx),%rcx
mov    %rax,(%rcx,%rbp,8)
cmp    $0xffffffff,%r14d
jl     1180 <allocate_grid+0x60>
mov    (%rbx),%rax
mov    (%rax,%rbp,8),%rdi
xor    %esi,%esi
mov    %r15,%rdx
call   1030 <memset@plt>
jmp    1180 <allocate_grid+0x60>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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