#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);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int **matrixalloc(int n) {
  int **a, i;
  a = (int **)malloc(n * sizeof(int *));
  for (i = 0; i < n; i++)
    a[i] = (int *)malloc(n * sizeof(int));
  if (a == NULL) {
    perror("Memory allocation error!");
    exit(1);
  }
  return a;
}


x86 assembly (clang, -O0):
<matrixalloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movslq -0x4(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    118d <matrixalloc+0x5d>
movslq -0x4(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,%rdx
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1155 <matrixalloc+0x25>
cmpq   $0x0,-0x10(%rbp)
jne    11ae <matrixalloc+0x7e>
lea    0xe61(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<matrixalloc>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edi,%r15d
lea    0x0(,%r15,8),%edi
call   1030 <malloc@plt>
mov    %rax,%r14
test   %r15d,%r15d
jle    1175 <matrixalloc+0x45>
mov    %r15d,%r12d
shl    $0x2,%r15d
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    %r15d,%edi
call   1030 <malloc@plt>
mov    %rax,(%r14,%rbx,8)
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1160 <matrixalloc+0x30>
test   %r14,%r14
je     1189 <matrixalloc+0x59>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe70(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"Memory allocation error!\00"
define i32** @matrixalloc(i32 %0) {
%2 = alloca i32
%3 = alloca i32**
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = sext i32 %5 to i64
%7 = mul i64 %6, 8
%8 = trunc i64 %7 to i32
%9 = call i64 @malloc(i32 %8)
%10 = inttoptr i64 %9 to i32**
store i32** %10, i32*** %3
store i32 0, i32* %4
br label %11
11: ; preds = %26, %1
%12 = load i32, i32* %4
%13 = load i32, i32* %2
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %29
15: ; preds = %11
%16 = load i32, i32* %2
%17 = sext i32 %16 to i64
%18 = mul i64 %17, 4
%19 = trunc i64 %18 to i32
%20 = call i64 @malloc(i32 %19)
%21 = inttoptr i64 %20 to i32*
%22 = load i32**, i32*** %3
%23 = load i32, i32* %4
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i32*, i32** %22, i64 %24
store i32* %21, i32** %25
br label %26
26: ; preds = %15
%27 = load i32, i32* %4
%28 = add nsw i32 %27, 1
store i32 %28, i32* %4
br label %11
29: ; preds = %11
%30 = load i32**, i32*** %3
%31 = icmp eq i32** %30, null
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %29
%36 = load i32**, i32*** %3
ret i32** %36
}

LLVM IR (-O2):
@.str = [25 x i8] c"Memory allocation error!\00"
define i32** @matrixalloc(i32 %0) {
%2 = shl i32 %0, 3
%3 = tail call i64 @malloc(i32 %2)
%4 = inttoptr i64 %3 to i32**
%5 = icmp sgt i32 %0, 0
br i1 %5, label %6, label %16
6: ; preds = %1
%7 = shl i32 %0, 2
%8 = zext i32 %0 to i64
br label %9
9: ; preds = %6, %9
%10 = phi i64 [ 0, %6 ], [ %14, %9 ]
%11 = tail call i64 @malloc(i32 %7)
%12 = inttoptr i64 %11 to i32*
%13 = getelementptr inbounds i32*, i32** %4, i64 %10
store i32* %12, i32** %13
%14 = add nuw nsw i64 %10, 1
%15 = icmp eq i64 %14, %8
br i1 %15, label %16, label %9
16: ; preds = %9, %1
%17 = icmp eq i64 %3, 0
br i1 %17, label %18, label %21
18: ; preds = %16
%19 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %16
ret i32** %4
}