#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 */
scalar_t__ calloc2d(int, int, int);
int /*<<< orphan*/ memcpy(char *, char *, unsigned int);
char **copy2d(char **orig, int ndim1, int ndim2, int size) {
  char **copy = (char **)calloc2d(ndim1, ndim2, size);
  if (copy == NULL)
    return NULL;
  unsigned matrix_size = (unsigned)(ndim1 * ndim2 * size);
  memcpy(*copy, *orig, matrix_size);
  return ((char **)copy);
}


x86 assembly (clang, -O0):
<copy2d>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
call   1040 <calloc2d@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    115f <copy2d+0x3f>
movq   $0x0,-0x8(%rbp)
jmp    118b <copy2d+0x6b>
mov    -0x14(%rbp),%eax
imul   -0x18(%rbp),%eax
imul   -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x2c(%rbp),%edx
call   1030 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy2d>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r14d
mov    %edx,%ebp
mov    %esi,%r12d
mov    %rdi,%r15
mov    %esi,%edi
mov    %edx,%esi
mov    %ecx,%edx
call   1040 <calloc2d@plt>
test   %rax,%rax
je     115d <copy2d+0x3d>
mov    %rax,%rbx
imul   %r12d,%ebp
imul   %r14d,%ebp
mov    (%rax),%rdi
mov    (%r15),%rsi
mov    %ebp,%edx
call   1030 <memcpy@plt>
jmp    115f <copy2d+0x3f>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8** @copy2d(i8** %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i8**
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8**
%11 = alloca i32
store i8** %0, i8*** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%12 = load i32, i32* %7
%13 = load i32, i32* %8
%14 = load i32, i32* %9
%15 = call i64 @calloc2d(i32 %12, i32 %13, i32 %14)
%16 = inttoptr i64 %15 to i8**
store i8** %16, i8*** %10
%17 = load i8**, i8*** %10
%18 = icmp eq i8** %17, null
br i1 %18, label %19, label %20
19: ; preds = %4
store i8** null, i8*** %5
br label %33
20: ; preds = %4
%21 = load i32, i32* %7
%22 = load i32, i32* %8
%23 = mul nsw i32 %21, %22
%24 = load i32, i32* %9
%25 = mul nsw i32 %23, %24
store i32 %25, i32* %11
%26 = load i8**, i8*** %10
%27 = load i8*, i8** %26
%28 = load i8**, i8*** %6
%29 = load i8*, i8** %28
%30 = load i32, i32* %11
%31 = call i32 @memcpy(i8* %27, i8* %29, i32 %30)
%32 = load i8**, i8*** %10
store i8** %32, i8*** %5
br label %33
33: ; preds = %20, %19
%34 = load i8**, i8*** %5
ret i8** %34
}

LLVM IR (-O2):
define i8** @copy2d(i8** nocapture %0, i32 %1, i32 %2, i32 %3) {
%5 = tail call i64 @calloc2d(i32 %1, i32 %2, i32 %3)
%6 = inttoptr i64 %5 to i8**
%7 = icmp eq i64 %5, 0
br i1 %7, label %14, label %8
8: ; preds = %4
%9 = mul nsw i32 %2, %1
%10 = mul nsw i32 %9, %3
%11 = load i8*, i8** %6
%12 = load i8*, i8** %0
%13 = tail call i32 @memcpy(i8* %11, i8* %12, i32 %10)
br label %14
14: ; preds = %4, %8
%15 = phi i8** [ %6, %8 ], [ null, %4 ]
ret i8** %15
}