#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__ malloc(int);
void alloc_matrix(int ***matrix_ptr, int num_rows, int num_cols) {
  int i;
  *matrix_ptr = (int **)malloc(num_rows * sizeof(int *));
  for (i = 0; i < num_rows; i++)
    (*matrix_ptr)[i] = (int *)malloc(num_cols * sizeof(int));
}


x86 assembly (clang, -O0):
<alloc_matrix>:
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   1030 <malloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    117d <alloc_matrix+0x6d>
movslq -0x10(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,%rdx
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movslq -0x14(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1142 <alloc_matrix+0x32>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alloc_matrix>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r15d
mov    %esi,%r14d
mov    %rdi,%r12
lea    0x0(,%r14,8),%edi
call   1030 <malloc@plt>
mov    %rax,(%r12)
test   %r14d,%r14d
jle    1159 <alloc_matrix+0x49>
shl    $0x2,%r15d
mov    %r14d,%ebp
xor    %ebx,%ebx
mov    %r15d,%edi
call   1030 <malloc@plt>
mov    (%r12),%rcx
mov    %rax,(%rcx,%rbx,8)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1140 <alloc_matrix+0x30>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @alloc_matrix(i32*** nocapture %0, i32 %1, i32 %2) {
%4 = shl i32 %1, 3
%5 = tail call i64 @malloc(i32 %4)
%6 = inttoptr i64 %5 to i32**
store i32** %6, i32*** %0
%7 = icmp sgt i32 %1, 0
br i1 %7, label %8, label %19
8: ; preds = %3
%9 = shl i32 %2, 2
%10 = zext i32 %1 to i64
br label %11
11: ; preds = %8, %11
%12 = phi i64 [ 0, %8 ], [ %17, %11 ]
%13 = tail call i64 @malloc(i32 %9)
%14 = inttoptr i64 %13 to i32*
%15 = load i32**, i32*** %0
%16 = getelementptr inbounds i32*, i32** %15, i64 %12
store i32* %14, i32** %16
%17 = add nuw nsw i64 %12, 1
%18 = icmp eq i64 %17, %10
br i1 %18, label %19, label %11
19: ; preds = %11, %3
ret void
}