#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 *calloc(int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ stderr;
int **alloc_matrix(int size) {
  int **matrix = (int **)malloc(size * sizeof(int));
  if (!matrix) {
    fprintf(stderr, "Out of memory!\n");
    return NULL;
  };
  for (int i = 0; i < size; ++i) {
    matrix[i] = calloc(size, sizeof(int));
    if (!matrix[i]) {
      fprintf(stderr, "Out of memory!\n");
      return NULL;
    };
  }
  return matrix;
}


x86 assembly (clang, -O0):
<alloc_matrix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    117b <alloc_matrix+0x4b>
mov    0x2e80(%rip),%rax
mov    (%rax),%edi
lea    0xe97(%rip),%rsi
call   1040 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11fa <alloc_matrix+0xca>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11f2 <alloc_matrix+0xc2>
mov    -0xc(%rbp),%edi
mov    $0x4,%esi
call   1030 <calloc@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
jne    11df <alloc_matrix+0xaf>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
lea    0xe33(%rip),%rsi
call   1040 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11fa <alloc_matrix+0xca>
jmp    11e4 <alloc_matrix+0xb4>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1182 <alloc_matrix+0x52>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alloc_matrix>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edi,%r12d
lea    0x0(,%r12,4),%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1181 <alloc_matrix+0x51>
mov    %rax,%r14
test   %r12d,%r12d
jle    1199 <alloc_matrix+0x69>
mov    %r12d,%r15d
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    $0x4,%esi
call   1030 <calloc@plt>
mov    %rax,(%r14,%rbx,8)
test   %rax,%rax
je     1181 <alloc_matrix+0x51>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1160 <alloc_matrix+0x30>
jmp    1199 <alloc_matrix+0x69>
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
lea    0xe6f(%rip),%rsi
call   1040 <fprintf@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [16 x i8] c"Out of memory!\0A\00"
define i32** @alloc_matrix(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, 4
%9 = trunc i64 %8 to i32
%10 = call i64 @malloc(i32 %9)
%11 = inttoptr i64 %10 to i32**
store i32** %11, i32*** %4
%12 = load i32**, i32*** %4
%13 = icmp ne i32** %12, null
br i1 %13, label %17, label %14
14: ; preds = %1
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32** null, i32*** %2
br label %44
17: ; preds = %1
store i32 0, i32* %5
br label %18
18: ; preds = %39, %17
%19 = load i32, i32* %5
%20 = load i32, i32* %3
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %42
22: ; preds = %18
%23 = load i32, i32* %3
%24 = call i32* @calloc(i32 %23, i32 4)
%25 = load i32**, i32*** %4
%26 = load i32, i32* %5
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i32*, i32** %25, i64 %27
store i32* %24, i32** %28
%29 = load i32**, i32*** %4
%30 = load i32, i32* %5
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i32*, i32** %29, i64 %31
%33 = load i32*, i32** %32
%34 = icmp ne i32* %33, null
br i1 %34, label %38, label %35
35: ; preds = %22
%36 = load i32, i32* @stderr
%37 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32** null, i32*** %2
br label %44
38: ; preds = %22
br label %39
39: ; preds = %38
%40 = load i32, i32* %5
%41 = add nsw i32 %40, 1
store i32 %41, i32* %5
br label %18
42: ; preds = %18
%43 = load i32**, i32*** %4
store i32** %43, i32*** %2
br label %44
44: ; preds = %42, %35, %14
%45 = load i32**, i32*** %2
ret i32** %45
}

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