#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 */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ stderr;
void alloc_grid(double ***Gptr, int N) {
  int i;
  double **G = (double **)malloc(N * sizeof(double *));
  if (G == 0) {
    fprintf(stderr, "malloc failed\n");
    exit(42);
  }
  for (i = 0; i < N; i++) {
    G[i] = (double *)malloc(N * sizeof(double));
    if (G[i] == 0) {
      fprintf(stderr, "malloc failed\n");
      exit(42);
    }
  }
  *Gptr = G;
}


x86 assembly (clang, -O0):
<alloc_grid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    117c <alloc_grid+0x4c>
mov    0x2e7c(%rip),%rax
mov    (%rax),%edi
lea    0xe93(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x2a,%edi
call   1050 <exit@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11f2 <alloc_grid+0xc2>
movslq -0xc(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
movslq -0x10(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
jne    11df <alloc_grid+0xaf>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x2a,%edi
call   1050 <exit@plt>
jmp    11e4 <alloc_grid+0xb4>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1183 <alloc_grid+0x53>
mov    -0x18(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alloc_grid>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%r14
lea    0x0(,%r15,8),%r12d
mov    %r12d,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     119d <alloc_grid+0x6d>
mov    %rax,%r13
test   %r15d,%r15d
jle    118b <alloc_grid+0x5b>
mov    %r15d,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r12d,%edi
call   1040 <malloc@plt>
mov    %rax,0x0(%r13,%rbx,8)
test   %rax,%rax
je     119d <alloc_grid+0x6d>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1170 <alloc_grid+0x40>
mov    %r13,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
lea    0xe53(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x2a,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [15 x i8] c"malloc failed\0A\00"
define void @alloc_grid(double*** %0, i32 %1) {
%3 = alloca double***
%4 = alloca i32
%5 = alloca i32
%6 = alloca double**
store double*** %0, double**** %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = sext i32 %7 to i64
%9 = mul i64 %8, 8
%10 = trunc i64 %9 to i32
%11 = call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to double**
store double** %12, double*** %6
%13 = load double**, double*** %6
%14 = icmp eq double** %13, null
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 42)
unreachable
19: ; preds = %2
store i32 0, i32* %5
br label %20
20: ; preds = %46, %19
%21 = load i32, i32* %5
%22 = load i32, i32* %4
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %49
24: ; preds = %20
%25 = load i32, i32* %4
%26 = sext i32 %25 to i64
%27 = mul i64 %26, 8
%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 i32, i32* %5
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds double*, double** %31, i64 %33
store double* %30, double** %34
%35 = load double**, double*** %6
%36 = load i32, i32* %5
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds double*, double** %35, i64 %37
%39 = load double*, double** %38
%40 = icmp eq double* %39, null
br i1 %40, label %41, label %45
41: ; preds = %24
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%44 = call i32 @exit(i32 42)
unreachable
45: ; preds = %24
br label %46
46: ; preds = %45
%47 = load i32, i32* %5
%48 = add nsw i32 %47, 1
store i32 %48, i32* %5
br label %20
49: ; preds = %20
%50 = load double**, double*** %6
%51 = load double***, double**** %3
store double** %50, double*** %51
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [15 x i8] c"malloc failed\0A\00"
define void @alloc_grid(double*** nocapture writeonly %0, i32 %1) {
%3 = shl i32 %1, 3
%4 = tail call i64 @malloc(i32 %3)
%5 = inttoptr i64 %4 to double**
%6 = icmp eq i64 %4, 0
br i1 %6, label %11, label %7
7: ; preds = %2
%8 = icmp sgt i32 %1, 0
br i1 %8, label %9, label %28
9: ; preds = %7
%10 = zext i32 %1 to i64
br label %17
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 42)
unreachable
15: ; preds = %17
%16 = icmp eq i64 %23, %10
br i1 %16, label %28, label %17
17: ; preds = %9, %15
%18 = phi i64 [ 0, %9 ], [ %23, %15 ]
%19 = tail call i64 @malloc(i32 %3)
%20 = inttoptr i64 %19 to double*
%21 = getelementptr inbounds double*, double** %5, i64 %18
store double* %20, double** %21
%22 = icmp eq i64 %19, 0
%23 = add nuw nsw i64 %18, 1
br i1 %22, label %24, label %15
24: ; preds = %17
%25 = load i32, i32* @stderr
%26 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%27 = tail call i32 @exit(i32 42)
unreachable
28: ; preds = %15, %7
store double** %5, double*** %0
ret void
}