#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 */
double *calloc(int, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
double **malloc(int);
int /*<<< orphan*/ stderr;
double **malloc2D(int X, int Y) {
  int i;
  double **a;
  a = malloc(X * sizeof(double *));
  if (a == NULL) {
    fprintf(stderr, "Malloc failed!\n");
    exit(-1);
  }
  a[0] = calloc(X * Y, sizeof(double));
  if (a[0] == NULL) {
    fprintf(stderr, "Malloc failed!\n");
    exit(-1);
  }
  for (i = 1; i < X; i++)
    a[i] = a[i - 1] + Y;
  return a;
}


x86 assembly (clang, -O0):
<malloc2D>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movslq -0x4(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    118b <malloc2D+0x4b>
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
lea    0xe84(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    -0x4(%rbp),%edi
imul   -0x8(%rbp),%edi
mov    $0x8,%esi
call   1030 <calloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11d3 <malloc2D+0x93>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
lea    0xe3c(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
movl   $0x1,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    121c <malloc2D+0xdc>
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdx
movslq -0x8(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdx
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11da <malloc2D+0x9a>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<malloc2D>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %edi,%ebp
lea    0x0(,%rbp,8),%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1248 <malloc2D+0x108>
mov    %rax,%rbx
mov    %r14d,%edi
imul   %ebp,%edi
mov    $0x8,%esi
call   1030 <calloc@plt>
mov    %rax,(%rbx)
test   %rax,%rax
je     1248 <malloc2D+0x108>
cmp    $0x2,%ebp
jl     1240 <malloc2D+0x100>
movslq %r14d,%r9
mov    %ebp,%ecx
lea    -0x1(%rcx),%rdi
add    $0xfffffffffffffffe,%rcx
mov    %edi,%edx
and    $0x7,%edx
mov    $0x1,%esi
cmp    $0x7,%rcx
jb     1216 <malloc2D+0xd6>
and    $0xfffffffffffffff8,%rdi
mov    %r9,%r8
shl    $0x6,%r8
lea    0x0(,%r9,8),%rbp
xor    %esi,%esi
nopl   0x0(%rax,%rax,1)
mov    %rax,%rcx
add    %rbp,%rax
mov    %rax,0x8(%rbx,%rsi,8)
add    %rbp,%rax
mov    %rax,0x10(%rbx,%rsi,8)
add    %rbp,%rax
mov    %rax,0x18(%rbx,%rsi,8)
add    %rbp,%rax
mov    %rax,0x20(%rbx,%rsi,8)
add    %rbp,%rax
mov    %rax,0x28(%rbx,%rsi,8)
add    %rbp,%rax
mov    %rax,0x30(%rbx,%rsi,8)
add    %rbp,%rax
mov    %rax,0x38(%rbx,%rsi,8)
add    %rbp,%rax
mov    %rax,0x40(%rbx,%rsi,8)
add    $0x8,%rsi
cmp    %rsi,%rdi
jne    11c0 <malloc2D+0x80>
add    %r8,%rcx
add    $0x1,%rsi
mov    %rcx,%rax
test   %rdx,%rdx
je     1240 <malloc2D+0x100>
lea    (%rax,%r9,8),%rax
shl    $0x3,%r9
lea    (%rbx,%rsi,8),%rcx
xor    %esi,%esi
nopl   0x0(%rax)
mov    %rax,(%rcx,%rsi,8)
add    $0x1,%rsi
add    %r9,%rax
cmp    %rsi,%rdx
jne    1230 <malloc2D+0xf0>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
lea    0xda8(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [16 x i8] c"Malloc failed!\0A\00"
define double** @malloc2D(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca double**
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = load i32, i32* %3
%8 = sext i32 %7 to i64
%9 = mul i64 %8, 8
%10 = trunc i64 %9 to i32
%11 = call double** @malloc(i32 %10)
store double** %11, double*** %6
%12 = load double**, double*** %6
%13 = icmp eq double** %12, null
br i1 %13, label %14, label %18
14: ; preds = %2
%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))
%17 = call i32 @exit(i32 -1)
unreachable
18: ; preds = %2
%19 = load i32, i32* %3
%20 = load i32, i32* %4
%21 = mul nsw i32 %19, %20
%22 = call double* @calloc(i32 %21, i32 8)
%23 = load double**, double*** %6
%24 = getelementptr inbounds double*, double** %23, i64 0
store double* %22, double** %24
%25 = load double**, double*** %6
%26 = getelementptr inbounds double*, double** %25, i64 0
%27 = load double*, double** %26
%28 = icmp eq double* %27, null
br i1 %28, label %29, label %33
29: ; preds = %18
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%32 = call i32 @exit(i32 -1)
unreachable
33: ; preds = %18
store i32 1, i32* %5
br label %34
34: ; preds = %52, %33
%35 = load i32, i32* %5
%36 = load i32, i32* %3
%37 = icmp slt i32 %35, %36
br i1 %37, label %38, label %55
38: ; preds = %34
%39 = load double**, double*** %6
%40 = load i32, i32* %5
%41 = sub nsw i32 %40, 1
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds double*, double** %39, i64 %42
%44 = load double*, double** %43
%45 = load i32, i32* %4
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds double, double* %44, i64 %46
%48 = load double**, double*** %6
%49 = load i32, i32* %5
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds double*, double** %48, i64 %50
store double* %47, double** %51
br label %52
52: ; preds = %38
%53 = load i32, i32* %5
%54 = add nsw i32 %53, 1
store i32 %54, i32* %5
br label %34
55: ; preds = %34
%56 = load double**, double*** %6
ret double** %56
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [16 x i8] c"Malloc failed!\0A\00"
define noalias double** @malloc2D(i32 %0, i32 %1) {
%3 = shl i32 %0, 3
%4 = tail call double** @malloc(i32 %3)
%5 = icmp eq double** %4, null
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 -1)
unreachable
10: ; preds = %2
%11 = mul nsw i32 %1, %0
%12 = tail call double* @calloc(i32 %11, i32 8)
store double* %12, double** %4
%13 = icmp eq double* %12, null
br i1 %13, label %25, label %14
14: ; preds = %10
%15 = icmp sgt i32 %0, 1
br i1 %15, label %16, label %72
16: ; preds = %14
%17 = sext i32 %1 to i64
%18 = zext i32 %0 to i64
%19 = add nsw i64 %18, -1
%20 = add nsw i64 %18, -2
%21 = and i64 %19, 7
%22 = icmp ult i64 %20, 7
br i1 %22, label %59, label %23
23: ; preds = %16
%24 = and i64 %19, -8
br label %29
25: ; preds = %10
%26 = load i32, i32* @stderr
%27 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%28 = tail call i32 @exit(i32 -1)
unreachable
29: ; preds = %29, %23
%30 = phi double* [ %12, %23 ], [ %54, %29 ]
%31 = phi i64 [ 1, %23 ], [ %56, %29 ]
%32 = phi i64 [ 0, %23 ], [ %57, %29 ]
%33 = getelementptr inbounds double, double* %30, i64 %17
%34 = getelementptr inbounds double*, double** %4, i64 %31
store double* %33, double** %34
%35 = add nuw nsw i64 %31, 1
%36 = getelementptr inbounds double, double* %33, i64 %17
%37 = getelementptr inbounds double*, double** %4, i64 %35
store double* %36, double** %37
%38 = add nuw nsw i64 %31, 2
%39 = getelementptr inbounds double, double* %36, i64 %17
%40 = getelementptr inbounds double*, double** %4, i64 %38
store double* %39, double** %40
%41 = add nuw nsw i64 %31, 3
%42 = getelementptr inbounds double, double* %39, i64 %17
%43 = getelementptr inbounds double*, double** %4, i64 %41
store double* %42, double** %43
%44 = add nuw nsw i64 %31, 4
%45 = getelementptr inbounds double, double* %42, i64 %17
%46 = getelementptr inbounds double*, double** %4, i64 %44
store double* %45, double** %46
%47 = add nuw nsw i64 %31, 5
%48 = getelementptr inbounds double, double* %45, i64 %17
%49 = getelementptr inbounds double*, double** %4, i64 %47
store double* %48, double** %49
%50 = add nuw nsw i64 %31, 6
%51 = getelementptr inbounds double, double* %48, i64 %17
%52 = getelementptr inbounds double*, double** %4, i64 %50
store double* %51, double** %52
%53 = add nuw nsw i64 %31, 7
%54 = getelementptr inbounds double, double* %51, i64 %17
%55 = getelementptr inbounds double*, double** %4, i64 %53
store double* %54, double** %55
%56 = add nuw nsw i64 %31, 8
%57 = add i64 %32, 8
%58 = icmp eq i64 %57, %24
br i1 %58, label %59, label %29
59: ; preds = %29, %16
%60 = phi double* [ %12, %16 ], [ %54, %29 ]
%61 = phi i64 [ 1, %16 ], [ %56, %29 ]
%62 = icmp eq i64 %21, 0
br i1 %62, label %72, label %63
63: ; preds = %59, %63
%64 = phi double* [ %67, %63 ], [ %60, %59 ]
%65 = phi i64 [ %69, %63 ], [ %61, %59 ]
%66 = phi i64 [ %70, %63 ], [ 0, %59 ]
%67 = getelementptr inbounds double, double* %64, i64 %17
%68 = getelementptr inbounds double*, double** %4, i64 %65
store double* %67, double** %68
%69 = add nuw nsw i64 %65, 1
%70 = add i64 %66, 1
%71 = icmp eq i64 %70, %21
br i1 %71, label %72, label %63
72: ; preds = %59, %63, %14
ret double** %4
}