#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 */
void *malloc(int);
int **build_matrix(int N, int M) {
  int **c;
  int i = 0;
  if ((c = malloc(N * sizeof(int *))) == NULL) {
    return NULL;
  }
  for (i = 0; i < N; i++) {
    if ((c[i] = malloc(M * sizeof(int))) == NULL) {
      return NULL;
    }
  }
  return c;
}


x86 assembly (clang, -O0):
<build_matrix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    114f <build_matrix+0x3f>
movq   $0x0,-0x8(%rbp)
jmp    11af <build_matrix+0x9f>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11a7 <build_matrix+0x97>
movslq -0x10(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    -0x18(%rbp),%rcx
movslq -0x1c(%rbp),%rdx
mov    %rax,(%rcx,%rdx,8)
cmp    $0x0,%rax
jne    1194 <build_matrix+0x84>
movq   $0x0,-0x8(%rbp)
jmp    11af <build_matrix+0x9f>
jmp    1199 <build_matrix+0x89>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1156 <build_matrix+0x46>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_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     116c <build_matrix+0x5c>
mov    %rax,%r12
test   %r14d,%r14d
jle    116f <build_matrix+0x5f>
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     116c <build_matrix+0x5c>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1150 <build_matrix+0x40>
jmp    116f <build_matrix+0x5f>
xor    %r12d,%r12d
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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