#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 *calloc(int, int);
int /*<<< orphan*/ fail(char *);
void *malloc(int);
void *allocmat(int rows, int columns, int size) {
  int i;
  void **p, *rp;
  rp = malloc(rows * sizeof(void *) + sizeof(int));
  if (rp == NULL)
    fail("allocmat: malloc [] failed!");
  *((int *)rp) = rows;
  p = rp + sizeof(int);
  for (i = 0; i < rows; i++)
    if ((p[i] = calloc(columns, size)) == NULL)
      fail("allocmat: malloc [][] failed!");
  return p;
}


x86 assembly (clang, -O0):
<allocmat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
movslq -0x4(%rbp),%rax
shl    $0x3,%rax
add    $0x4,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    116f <allocmat+0x3f>
lea    0xe96(%rip),%rdi
call   1030 <fail@plt>
mov    -0x4(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11d7 <allocmat+0xa7>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1040 <calloc@plt>
mov    -0x18(%rbp),%rcx
movslq -0x10(%rbp),%rdx
mov    %rax,(%rcx,%rdx,8)
cmp    $0x0,%rax
jne    11c4 <allocmat+0x94>
lea    0xe5d(%rip),%rdi
call   1030 <fail@plt>
jmp    11c9 <allocmat+0x99>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    118b <allocmat+0x5b>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<allocmat>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%r12d
mov    %edi,%r15d
lea    0x4(,%r15,8),%edi
call   1050 <malloc@plt>
mov    %rax,%r13
test   %rax,%rax
jne    1165 <allocmat+0x35>
lea    0xea0(%rip),%rdi
call   1030 <fail@plt>
mov    %r15d,0x0(%r13)
add    $0x4,%r13
test   %r15d,%r15d
jle    11a8 <allocmat+0x78>
mov    %r15d,%ebp
xor    %ebx,%ebx
lea    0xe9e(%rip),%r15
jmp    1189 <allocmat+0x59>
add    $0x1,%rbx
cmp    %rbx,%rbp
je     11a8 <allocmat+0x78>
mov    %r12d,%edi
mov    %r14d,%esi
call   1040 <calloc@plt>
mov    %rax,0x0(%r13,%rbx,8)
test   %rax,%rax
jne    1180 <allocmat+0x50>
mov    %r15,%rdi
call   1030 <fail@plt>
jmp    1180 <allocmat+0x50>
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [28 x i8] c"allocmat: malloc [] failed!\00"
@.str.1 = [30 x i8] c"allocmat: malloc [][] failed!\00"
define noalias i8* @allocmat(i32 %0, i32 %1, i32 %2) {
%4 = shl i32 %0, 3
%5 = or i32 %4, 4
%6 = tail call i8* @malloc(i32 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 @fail(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %3
%11 = bitcast i8* %6 to i32*
store i32 %0, i32* %11
%12 = getelementptr i8, i8* %6, i64 4
%13 = bitcast i8* %12 to i8**
%14 = icmp sgt i32 %0, 0
br i1 %14, label %15, label %27
15: ; preds = %10
%16 = zext i32 %0 to i64
br label %17
17: ; preds = %15, %24
%18 = phi i64 [ 0, %15 ], [ %25, %24 ]
%19 = tail call i8* @calloc(i32 %1, i32 %2)
%20 = getelementptr inbounds i8*, i8** %13, i64 %18
store i8* %19, i8** %20
%21 = icmp eq i8* %19, null
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = tail call i32 @fail(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %17, %22
%25 = add nuw nsw i64 %18, 1
%26 = icmp eq i64 %25, %16
br i1 %26, label %27, label %17
27: ; preds = %24, %10
ret i8* %12
}