#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 */
typedef int /*<<< orphan*/ uint8_t;

/* Variables and functions */
int /*<<< orphan*/ abort();
void *malloc(int);
int /*<<< orphan*/ perror(char *);
void malloc2D(uint8_t ***dest, int d1, int d2) {
  uint8_t **T;
  *dest = malloc(d1 * sizeof(uint8_t *));
  T = *dest;
  if (T == NULL) {
    perror("malloc");
    abort();
  }
  for (int i = 0; i < d1; i++) {
    T[i] = malloc(d2);
    if (T[i] == NULL) {
      perror("malloc");
      abort();
    }
  }
}


x86 assembly (clang, -O0):
<malloc2D>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1184 <malloc2D+0x54>
lea    0xe88(%rip),%rdi
call   1050 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11e7 <malloc2D+0xb7>
mov    -0x10(%rbp),%edi
call   1040 <malloc@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    11d4 <malloc2D+0xa4>
lea    0xe38(%rip),%rdi
call   1050 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    11d9 <malloc2D+0xa9>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    118b <malloc2D+0x5b>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<malloc2D>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r12d
mov    %esi,%r14d
mov    %rdi,%r15
lea    0x0(,%r14,8),%edi
call   1040 <malloc@plt>
mov    %rax,(%r15)
test   %rax,%rax
je     119a <malloc2D+0x6a>
test   %r14d,%r14d
jle    118b <malloc2D+0x5b>
mov    %rax,%r13
mov    %r14d,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    %r12d,%edi
call   1040 <malloc@plt>
mov    %rax,0x0(%r13,%rbx,8)
test   %rax,%rax
je     119a <malloc2D+0x6a>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1170 <malloc2D+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe5f(%rip),%rdi
call   1050 <perror@plt>
xor    %eax,%eax
call   1030 <abort@plt>

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

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