#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*/ free_matrix(int **, int);
void *malloc(int);
int **allocate_matrix(int width, int height) {
  int **matrix = NULL;
  int i;
  if ((matrix = malloc(height * sizeof(int *))) == NULL)
    return NULL;
  for (i = 0; i < height; i++) {
    if ((matrix[i] = malloc(width * sizeof(int))) == NULL) {
      free_matrix(matrix, i);
      return NULL;
    }
  }
  return matrix;
}


x86 assembly (clang, -O0):
<allocate_matrix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movslq -0x10(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1160 <allocate_matrix+0x40>
movq   $0x0,-0x8(%rbp)
jmp    11cc <allocate_matrix+0xac>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11c4 <allocate_matrix+0xa4>
movslq -0xc(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    -0x18(%rbp),%rcx
movslq -0x1c(%rbp),%rdx
mov    %rax,(%rcx,%rdx,8)
cmp    $0x0,%rax
jne    11b1 <allocate_matrix+0x91>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1030 <free_matrix@plt>
movq   $0x0,-0x8(%rbp)
jmp    11cc <allocate_matrix+0xac>
jmp    11b6 <allocate_matrix+0x96>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1167 <allocate_matrix+0x47>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<allocate_matrix>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%r14d
lea    0x0(,%rbp,8),%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     1176 <allocate_matrix+0x56>
mov    %rax,%r15
test   %ebp,%ebp
jle    1179 <allocate_matrix+0x59>
shl    $0x2,%r14d
mov    %ebp,%ebx
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    %r14d,%edi
call   1040 <malloc@plt>
mov    %rax,(%r15,%rbp,8)
test   %rax,%rax
je     116c <allocate_matrix+0x4c>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1150 <allocate_matrix+0x30>
jmp    1179 <allocate_matrix+0x59>
mov    %r15,%rdi
mov    %ebp,%esi
call   1030 <free_matrix@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32** @allocate_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** null, 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 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 %43
16: ; preds = %2
store i32 0, i32* %7
br label %17
17: ; preds = %38, %16
%18 = load i32, i32* %7
%19 = load i32, i32* %5
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %41
21: ; preds = %17
%22 = load i32, i32* %4
%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 %37
33: ; preds = %21
%34 = load i32**, i32*** %6
%35 = load i32, i32* %7
%36 = call i32 @free_matrix(i32** %34, i32 %35)
store i32** null, i32*** %3
br label %43
37: ; preds = %21
br label %38
38: ; preds = %37
%39 = load i32, i32* %7
%40 = add nsw i32 %39, 1
store i32 %40, i32* %7
br label %17
41: ; preds = %17
%42 = load i32**, i32*** %6
store i32** %42, i32*** %3
br label %43
43: ; preds = %41, %33, %15
%44 = load i32**, i32*** %3
ret i32** %44
}

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