#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);
void *malloc(int);
int /*<<< orphan*/ printf(char *);
int **__new_2d_int_array(int width, int height) {
  int x;
  int **result;
  result = malloc(sizeof(int *) * width);
  if (result == NULL) {
    printf("Malloc 1 failed in __new_2d_int_array()\n");
    exit(1);
  }
  for (x = 0; x < width; x++) {
    result[x] = malloc(sizeof(int) * height);
    if (result[x] == NULL) {
      printf("Malloc 2 failed in __new_2d_int_array()\n");
      exit(1);
    }
  }
  return result;
}


x86 assembly (clang, -O0):
<__new_2d_int_array>:
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    1172 <__new_2d_int_array+0x42>
lea    0xe9d(%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    11df <__new_2d_int_array+0xaf>
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    11cc <__new_2d_int_array+0x9c>
lea    0xe6c(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
jmp    11d1 <__new_2d_int_array+0xa1>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1179 <__new_2d_int_array+0x49>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__new_2d_int_array>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r15d
mov    %edi,%r14d
lea    0x0(,%r14,8),%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     11ac <__new_2d_int_array+0x7c>
mov    %rax,%r12
test   %r14d,%r14d
jle    118a <__new_2d_int_array+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   1040 <malloc@plt>
mov    %rax,(%r12,%rbx,8)
test   %rax,%rax
je     1196 <__new_2d_int_array+0x66>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1170 <__new_2d_int_array+0x40>
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe63(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
lea    0xe75(%rip),%rdi
jmp    119d <__new_2d_int_array+0x6d>

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

LLVM IR (-O2):
@str = [40 x i8] c"Malloc 2 failed in __new_2d_int_array()\00"
@str.2 = [40 x i8] c"Malloc 1 failed in __new_2d_int_array()\00"
define noalias i32** @__new_2d_int_array(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 %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 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str.2, i64 0, i64 0))
%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 i8* @malloc(i32 %10)
%20 = getelementptr inbounds i32*, i32** %5, i64 %18
%21 = bitcast i32** %20 to i8**
store i8* %19, i8** %21
%22 = icmp eq i8* %19, null
%23 = add nuw nsw i64 %18, 1
br i1 %22, label %24, label %15
24: ; preds = %17
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str, i64 0, i64 0))
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %15, %7
ret i32** %5
}