#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(int);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, int, int);
int **build_matrix(int n, int p) {
  int i;
  int **m;
  m = (int **)malloc(sizeof(int *) * n);
  if (m == NULL) {
    printf("Could not allocate a matrix with dimension %dx%d", n, p);
    exit(1);
  }
  for (i = 0; i < n; i++) {
    m[i] = (int *)malloc(sizeof(int) * p);
    if (m[i] == NULL) {
      printf("Could not allocate a matrix with dimension %dx%d", n, p);
      exit(1);
    }
  }
  return m;
}


x86 assembly (clang, -O0):
<build_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   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1178 <build_matrix+0x48>
mov    -0x4(%rbp),%esi
mov    -0x8(%rbp),%edx
lea    0xe97(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11eb <build_matrix+0xbb>
movslq -0x8(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
jne    11d8 <build_matrix+0xa8>
mov    -0x4(%rbp),%esi
mov    -0x8(%rbp),%edx
lea    0xe37(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
jmp    11dd <build_matrix+0xad>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    117f <build_matrix+0x4f>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_matrix>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%r15d
lea    0x0(,%r15,8),%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     119c <build_matrix+0x6c>
mov    %rax,%r12
test   %r15d,%r15d
jle    118a <build_matrix+0x5a>
lea    0x0(,%r14,4),%r13d
mov    %r15d,%ebx
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    %r13d,%edi
call   1040 <malloc@plt>
mov    %rax,(%r12,%rbp,8)
test   %rax,%rax
je     119c <build_matrix+0x6c>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1170 <build_matrix+0x40>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe5d(%rip),%rdi
mov    %r15d,%esi
mov    %r14d,%edx
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

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

LLVM IR (-O2):
@.str = [49 x i8] c"Could not allocate a matrix with dimension %dx%d\00"
define i32** @build_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 %27
9: ; preds = %7
%10 = shl i32 %1, 2
%11 = zext i32 %0 to i64
br label %17
12: ; preds = %2
%13 = tail call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %1)
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %17
%16 = icmp eq i64 %23, %11
br i1 %16, label %27, label %17
17: ; preds = %9, %15
%18 = phi i64 [ 0, %9 ], [ %23, %15 ]
%19 = tail call i64 @malloc(i32 %10)
%20 = inttoptr i64 %19 to i32*
%21 = getelementptr inbounds i32*, i32** %5, i64 %18
store i32* %20, i32** %21
%22 = icmp eq i64 %19, 0
%23 = add nuw nsw i64 %18, 1
br i1 %22, label %24, label %15
24: ; preds = %17
%25 = tail call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %1)
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %15, %7
ret i32** %5
}