#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_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int **allocate_dynamic_matrix(int row, int col) {
  int **ret_val;
  int i;
  ret_val = (int **)malloc(sizeof(int *) * row);
  if (ret_val == NULL) {
    perror("memory allocation failure");
    exit(EXIT_FAILURE);
  }
  for (i = 0; i < row; ++i) {
    ret_val[i] = (int *)malloc(sizeof(int) * col);
    if (ret_val[i] == NULL) {
      perror("memory allocation failure");
      exit(EXIT_FAILURE);
    }
  }
  return ret_val;
}


x86 assembly (clang, -O0):
<allocate_dynamic_matrix>:
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   1030 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1176 <allocate_dynamic_matrix+0x46>
lea    0xe9d(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11e7 <allocate_dynamic_matrix+0xb7>
movslq -0x8(%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    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
jne    11d4 <allocate_dynamic_matrix+0xa4>
lea    0xe3f(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
jmp    11d9 <allocate_dynamic_matrix+0xa9>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    117d <allocate_dynamic_matrix+0x4d>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<allocate_dynamic_matrix>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r15d
mov    %edi,%r14d
lea    0x0(,%r14,8),%edi
call   1030 <malloc@plt>
test   %rax,%rax
je     1196 <allocate_dynamic_matrix+0x66>
mov    %rax,%r12
test   %r14d,%r14d
jle    118a <allocate_dynamic_matrix+0x5a>
shl    $0x2,%r15d
mov    %r14d,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
call   1030 <malloc@plt>
mov    %rax,(%r12,%rbx,8)
test   %rax,%rax
je     1196 <allocate_dynamic_matrix+0x66>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1170 <allocate_dynamic_matrix+0x40>
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe63(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

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

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