#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 calculateMinimumHP(int **, int, int);
void *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  int row_size = 3, col_size = 3;
  int i, j, **dungeon = malloc(row_size * sizeof(int *));
  for (i = 0; i < row_size; i++) {
    dungeon[i] = malloc(col_size * sizeof(int));
  }
  dungeon[0][0] = 1;
  dungeon[0][1] = -3;
  dungeon[0][2] = 3;
  dungeon[1][0] = 0;
  dungeon[1][1] = -2;
  dungeon[1][2] = 0;
  dungeon[2][0] = -3;
  dungeon[2][1] = -3;
  dungeon[2][2] = -3;
  printf("dungeon:\n");
  for (i = 0; i < row_size; i++) {
    for (j = 0; j < col_size; j++) {
      printf("%d ", dungeon[i][j]);
    }
    printf("\n");
  }
  printf("%d\n", calculateMinimumHP(dungeon, row_size, col_size));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x3,-0x8(%rbp)
movl   $0x3,-0xc(%rbp)
movslq -0x8(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    119f <main+0x6f>
movslq -0xc(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,%rdx
mov    -0x20(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1167 <main+0x37>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
movl   $0x1,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
movl   $0xfffffffd,0x4(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
movl   $0x3,0x8(%rax)
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
movl   $0x0,(%rax)
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
movl   $0xfffffffe,0x4(%rax)
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
movl   $0x0,0x8(%rax)
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rax
movl   $0xfffffffd,(%rax)
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rax
movl   $0xfffffffd,0x4(%rax)
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rax
movl   $0xfffffffd,0x8(%rax)
lea    0xdd9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    129f <main+0x16f>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1283 <main+0x153>
mov    -0x20(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xd9c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1248 <main+0x118>
lea    0xd7e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1235 <main+0x105>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
call   1050 <calculateMinimumHP@plt>
mov    %eax,%esi
lea    0xd57(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    $0x18,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    $0xc,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rax,(%r14)
mov    $0xc,%edi
call   1060 <malloc@plt>
mov    %rax,%r13
mov    %rax,0x8(%r14)
mov    $0xc,%edi
call   1060 <malloc@plt>
mov    %rax,%r12
mov    %rax,0x10(%r14)
movabs $0xfffffffd00000001,%rax
ff ff ff
mov    %rax,(%rbx)
movl   $0x3,0x8(%rbx)
movabs $0xfffffffe00000000,%rax
ff ff ff
mov    %rax,0x0(%r13)
movl   $0x0,0x8(%r13)
movabs $0xfffffffdfffffffd,%rax
ff ff ff
mov    %rax,(%r12)
movl   $0xfffffffd,0x8(%r12)
ff ff
lea    0xe28(%rip),%rdi
call   1040 <puts@plt>
mov    (%rbx),%esi
lea    0xe12(%rip),%r15
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x4(%rbx),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rbx),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x0(%r13),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x4(%r13),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%r13),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    (%r12),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x4(%r12),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%r12),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r14,%rdi
mov    $0x3,%esi
mov    $0x3,%edx
call   1070 <calculateMinimumHP@plt>
lea    0xd65(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [10 x i8] c"dungeon:\0A\00"
@.str.1 = [4 x i8] c"%d \00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32**
store i32 0, i32* %1
store i32 3, i32* %2
store i32 3, i32* %3
%7 = load i32, i32* %2
%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
store i32 0, i32* %4
br label %13
13: ; preds = %28, %0
%14 = load i32, i32* %4
%15 = load i32, i32* %2
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %31
17: ; preds = %13
%18 = load i32, i32* %3
%19 = sext i32 %18 to i64
%20 = mul i64 %19, 4
%21 = trunc i64 %20 to i32
%22 = call i8* @malloc(i32 %21)
%23 = bitcast i8* %22 to i32*
%24 = load i32**, i32*** %6
%25 = load i32, i32* %4
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i32*, i32** %24, i64 %26
store i32* %23, i32** %27
br label %28
28: ; preds = %17
%29 = load i32, i32* %4
%30 = add nsw i32 %29, 1
store i32 %30, i32* %4
br label %13
31: ; preds = %13
%32 = load i32**, i32*** %6
%33 = getelementptr inbounds i32*, i32** %32, i64 0
%34 = load i32*, i32** %33
%35 = getelementptr inbounds i32, i32* %34, i64 0
store i32 1, i32* %35
%36 = load i32**, i32*** %6
%37 = getelementptr inbounds i32*, i32** %36, i64 0
%38 = load i32*, i32** %37
%39 = getelementptr inbounds i32, i32* %38, i64 1
store i32 -3, i32* %39
%40 = load i32**, i32*** %6
%41 = getelementptr inbounds i32*, i32** %40, i64 0
%42 = load i32*, i32** %41
%43 = getelementptr inbounds i32, i32* %42, i64 2
store i32 3, i32* %43
%44 = load i32**, i32*** %6
%45 = getelementptr inbounds i32*, i32** %44, i64 1
%46 = load i32*, i32** %45
%47 = getelementptr inbounds i32, i32* %46, i64 0
store i32 0, i32* %47
%48 = load i32**, i32*** %6
%49 = getelementptr inbounds i32*, i32** %48, i64 1
%50 = load i32*, i32** %49
%51 = getelementptr inbounds i32, i32* %50, i64 1
store i32 -2, i32* %51
%52 = load i32**, i32*** %6
%53 = getelementptr inbounds i32*, i32** %52, i64 1
%54 = load i32*, i32** %53
%55 = getelementptr inbounds i32, i32* %54, i64 2
store i32 0, i32* %55
%56 = load i32**, i32*** %6
%57 = getelementptr inbounds i32*, i32** %56, i64 2
%58 = load i32*, i32** %57
%59 = getelementptr inbounds i32, i32* %58, i64 0
store i32 -3, i32* %59
%60 = load i32**, i32*** %6
%61 = getelementptr inbounds i32*, i32** %60, i64 2
%62 = load i32*, i32** %61
%63 = getelementptr inbounds i32, i32* %62, i64 1
store i32 -3, i32* %63
%64 = load i32**, i32*** %6
%65 = getelementptr inbounds i32*, i32** %64, i64 2
%66 = load i32*, i32** %65
%67 = getelementptr inbounds i32, i32* %66, i64 2
store i32 -3, i32* %67
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %4
br label %69
69: ; preds = %94, %31
%70 = load i32, i32* %4
%71 = load i32, i32* %2
%72 = icmp slt i32 %70, %71
br i1 %72, label %73, label %97
73: ; preds = %69
store i32 0, i32* %5
br label %74
74: ; preds = %89, %73
%75 = load i32, i32* %5
%76 = load i32, i32* %3
%77 = icmp slt i32 %75, %76
br i1 %77, label %78, label %92
78: ; preds = %74
%79 = load i32**, i32*** %6
%80 = load i32, i32* %4
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds i32*, i32** %79, i64 %81
%83 = load i32*, i32** %82
%84 = load i32, i32* %5
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds i32, i32* %83, i64 %85
%87 = load i32, i32* %86
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %87)
br label %89
89: ; preds = %78
%90 = load i32, i32* %5
%91 = add nsw i32 %90, 1
store i32 %91, i32* %5
br label %74
92: ; preds = %74
%93 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %94
94: ; preds = %92
%95 = load i32, i32* %4
%96 = add nsw i32 %95, 1
store i32 %96, i32* %4
br label %69
97: ; preds = %69
%98 = load i32**, i32*** %6
%99 = load i32, i32* %2
%100 = load i32, i32* %3
%101 = call i32 @calculateMinimumHP(i32** %98, i32 %99, i32 %100)
%102 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %101)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [4 x i8] c"%d \00"
@.str.3 = [4 x i8] c"%d\0A\00"
@str = [9 x i8] c"dungeon:\00"
define i32 @main() {
%1 = tail call dereferenceable_or_null(24) i8* @malloc(i32 24)
%2 = bitcast i8* %1 to i32**
%3 = tail call dereferenceable_or_null(12) i8* @malloc(i32 12)
%4 = bitcast i8* %1 to i8**
store i8* %3, i8** %4
%5 = tail call dereferenceable_or_null(12) i8* @malloc(i32 12)
%6 = getelementptr inbounds i32*, i32** %2, i64 1
%7 = bitcast i32** %6 to i8**
store i8* %5, i8** %7
%8 = tail call dereferenceable_or_null(12) i8* @malloc(i32 12)
%9 = getelementptr inbounds i32*, i32** %2, i64 2
%10 = bitcast i32** %9 to i8**
store i8* %8, i8** %10
%11 = bitcast i8* %3 to i32*
store i32 1, i32* %11
%12 = getelementptr inbounds i32, i32* %11, i64 1
store i32 -3, i32* %12
%13 = getelementptr inbounds i32, i32* %11, i64 2
store i32 3, i32* %13
%14 = bitcast i8* %5 to i32*
store i32 0, i32* %14
%15 = getelementptr inbounds i32, i32* %14, i64 1
store i32 -2, i32* %15
%16 = getelementptr inbounds i32, i32* %14, i64 2
store i32 0, i32* %16
%17 = bitcast i8* %8 to i32*
store i32 -3, i32* %17
%18 = getelementptr inbounds i32, i32* %17, i64 1
store i32 -3, i32* %18
%19 = getelementptr inbounds i32, i32* %17, i64 2
store i32 -3, i32* %19
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
%21 = load i32, i32* %11
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = load i32, i32* %12
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = load i32, i32* %13
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %25)
%27 = tail call i32 @putchar(i32 10)
%28 = load i32, i32* %14
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %28)
%30 = load i32, i32* %15
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %30)
%32 = load i32, i32* %16
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %32)
%34 = tail call i32 @putchar(i32 10)
%35 = load i32, i32* %17
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %35)
%37 = load i32, i32* %18
%38 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %37)
%39 = load i32, i32* %19
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %39)
%41 = tail call i32 @putchar(i32 10)
%42 = tail call i32 @calculateMinimumHP(i32** %2, i32 3, i32 3)
%43 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %42)
ret i32 0
}