#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 *malloc(int);
int /*<<< orphan*/ printf(char *);
int **create_array(int max) {
  int **array;
  array = malloc(max * sizeof(int *));
  if (array == NULL) {
    printf("Out of memory\n");
    return NULL;
  }
  for (int i = 0; i < max; i++) {
    array[i] = malloc(max * sizeof(int *));
    if (array == NULL) {
      printf("Out of memory\n");
      return NULL;
    }
  }
  return array;
}


x86 assembly (clang, -O0):
<create_array>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-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    1162 <create_array+0x42>
lea    0xeb0(%rip),%rdi
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11d2 <create_array+0xb2>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11ca <create_array+0xaa>
movslq -0xc(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
cmpq   $0x0,-0x18(%rbp)
jne    11b7 <create_array+0x97>
lea    0xe5b(%rip),%rdi
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11d2 <create_array+0xb2>
jmp    11bc <create_array+0x9c>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1169 <create_array+0x49>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_array>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%r14d
lea    0x0(,%r14,8),%r12d
mov    %r12d,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     1167 <create_array+0x47>
mov    %rax,%r15
test   %r14d,%r14d
jle    1176 <create_array+0x56>
mov    %r14d,%ebx
xor    %ebp,%ebp
nopl   (%rax)
mov    %r12d,%edi
call   1040 <malloc@plt>
mov    %rax,(%r15,%rbp,8)
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1150 <create_array+0x30>
jmp    1176 <create_array+0x56>
lea    0xe92(%rip),%rdi
call   1030 <puts@plt>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"Out of memory\0A\00"
define i32** @create_array(i32 %0) {
%2 = alloca i32**
%3 = alloca i32
%4 = alloca i32**
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = sext i32 %6 to i64
%8 = mul i64 %7, 8
%9 = trunc i64 %8 to i32
%10 = call i8* @malloc(i32 %9)
%11 = bitcast i8* %10 to i32**
store i32** %11, i32*** %4
%12 = load i32**, i32*** %4
%13 = icmp eq i32** %12, null
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32** null, i32*** %2
br label %42
16: ; preds = %1
store i32 0, i32* %5
br label %17
17: ; preds = %37, %16
%18 = load i32, i32* %5
%19 = load i32, i32* %3
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %40
21: ; preds = %17
%22 = load i32, i32* %3
%23 = sext i32 %22 to i64
%24 = mul i64 %23, 8
%25 = trunc i64 %24 to i32
%26 = call i8* @malloc(i32 %25)
%27 = bitcast i8* %26 to i32*
%28 = load i32**, i32*** %4
%29 = load i32, i32* %5
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i32*, i32** %28, i64 %30
store i32* %27, i32** %31
%32 = load i32**, i32*** %4
%33 = icmp eq i32** %32, null
br i1 %33, label %34, label %36
34: ; preds = %21
%35 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32** null, i32*** %2
br label %42
36: ; preds = %21
br label %37
37: ; preds = %36
%38 = load i32, i32* %5
%39 = add nsw i32 %38, 1
store i32 %39, i32* %5
br label %17
40: ; preds = %17
%41 = load i32**, i32*** %4
store i32** %41, i32*** %2
br label %42
42: ; preds = %40, %34, %14
%43 = load i32**, i32*** %2
ret i32** %43
}

LLVM IR (-O2):
@str = [14 x i8] c"Out of memory\00"
define noalias i32** @create_array(i32 %0) {
%2 = shl i32 %0, 3
%3 = tail call i8* @malloc(i32 %2)
%4 = bitcast i8* %3 to i32**
%5 = icmp eq i8* %3, null
br i1 %5, label %10, label %6
6: ; preds = %1
%7 = icmp sgt i32 %0, 0
br i1 %7, label %8, label %19
8: ; preds = %6
%9 = zext i32 %0 to i64
br label %12
10: ; preds = %1
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %19
12: ; preds = %8, %12
%13 = phi i64 [ 0, %8 ], [ %17, %12 ]
%14 = tail call i8* @malloc(i32 %2)
%15 = getelementptr inbounds i32*, i32** %4, i64 %13
%16 = bitcast i32** %15 to i8**
store i8* %14, i8** %16
%17 = add nuw nsw i64 %13, 1
%18 = icmp eq i64 %17, %9
br i1 %18, label %19, label %12
19: ; preds = %12, %6, %10
%20 = phi i32** [ null, %10 ], [ %4, %6 ], [ %4, %12 ]
ret i32** %20
}