// File: jeffreyan11[P]go-engine/board.cpp
// Function: Board::countTerritory(int&, int&)
void Board::countTerritory(int &whiteTerritory, int &blackTerritory) {
    whiteTerritory = 0;
    blackTerritory = 0;
    Stone *visited = new Stone[arraySize*arraySize];
    Stone *territory = new Stone[arraySize*arraySize];
    Stone *region = new Stone[arraySize*arraySize]; 

    // Count territory for both sides
    for (Player p = BLACK; p <= WHITE; p++) {
        // Reset the visited array
        for (int i = 0; i < arraySize*arraySize; i++)
            visited[i] = 0;

        // Main loop
        for (int j = 1; j <= boardSize; j++) {
            for (int i = 1; i <= boardSize; i++) {
                // Don't recount territory
                if (visited[index(i, j)])
                    continue;
                // Only use empty squares as seeds
                if (pieces[index(i, j)])
                    continue;

                if (isEye(p, coordToMove(i, j))) {
                    visited[index(i, j)] = 1;
                    if (p == BLACK)
                        blackTerritory++;
                    else
                        whiteTerritory++;
                    continue;
                }
                
                for (int k = 0; k < arraySize*arraySize; k++)
                    territory[k] = 0;
                int territorySize = 0;
                int boundarySize = 0;

                getTerritory(p, i, j, visited, territory, territorySize, boundarySize);

                // Check if territory was actually sectioned off
                if (territorySize + boundarySize == boardSize*boardSize)
                    continue;

                // Detect life/death of internal stones
                // Initialize region to 0 if territory is 1, and vice versa
                // This acts as our "visited" array, so that we only explore areas
                // inside the territory
                // bool isContested = false;
                // for (int n = 1; n <= boardSize; n++) {
                //     for (int m = 1; m <= boardSize; m++) {
                //         if (!territory[index(m, n)])
                //             continue;
                //         if (pieces[index(m, n)] == otherPlayer(p)) {
                //             isContested = true;
                //             break;
                //         }
                //     }
                // }

                // if (!isContested) {
                //     if (p == BLACK)
                //         blackTerritory += territorySize;
                //     else
                //         whiteTerritory += territorySize;
                // }
                
                for (int k = 0; k < arraySize*arraySize; k++)
                    region[k] = territory[k] ^ 1;
                int internalRegions = 0;

                for (int n = 1; n <= boardSize; n++) {
                    for (int m = 1; m <= boardSize; m++) {
                        if (region[index(m, n)])
                            continue;
                        if (pieces[index(m, n)])
                            continue;

                        MoveList eye;
                        if (isSurrounded(EMPTY, p, m, n, region, eye))
                            internalRegions++;
                    }
                }

                int territoryCount = 0;
                if (internalRegions == 0) {
                    territoryCount += territorySize;
                    // Score dead stones
                    for (int k = 0; k < arraySize*arraySize; k++)
                        if (territory[k] && pieces[k] == otherPlayer(p))
                            territoryCount++;
                }

                if (p == BLACK)
                    blackTerritory += territoryCount;
                else
                    whiteTerritory += territoryCount;
            }
        }
    }

    delete[] visited;
    delete[] territory;
    delete[] region;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x4f0, %rsp            # imm = 0x4F0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x480(%rbp)
movq -0x10(%rbp), %rax
movl $0x0, (%rax)
movq -0x18(%rbp), %rax
movl $0x0, (%rax)
movl 0xe098(%rip), %eax      # 0x141f4
imull 0xe091(%rip), %eax      # 0x141f4
cltq
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2030
movq %rax, -0x20(%rbp)
movl 0xe064(%rip), %eax      # 0x141f4
imull 0xe05d(%rip), %eax      # 0x141f4
cltq
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2030
movq %rax, -0x28(%rbp)
movl 0xe030(%rip), %eax      # 0x141f4
imull 0xe029(%rip), %eax      # 0x141f4
cltq
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2030
movq %rax, -0x30(%rbp)
movl $0x1, -0x34(%rbp)
cmpl $0x2, -0x34(%rbp)
jg 0x6641
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
movl 0xdfe1(%rip), %ecx      # 0x141f4
imull 0xdfda(%rip), %ecx      # 0x141f4
cmpl %ecx, %eax
jge 0x6238
movq -0x20(%rbp), %rax
movslq -0x38(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x620a
movl $0x1, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl 0xdfa8(%rip), %eax      # 0x141f0
jg 0x6631
movl $0x1, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl 0xdf92(%rip), %eax      # 0x141f0
jg 0x6621
movq -0x20(%rbp), %rax
movq %rax, -0x488(%rbp)
movl -0x40(%rbp), %edi
movl -0x3c(%rbp), %esi
callq 0x8bc0
movl %eax, %ecx
movq -0x488(%rbp), %rax
movslq %ecx, %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x6291
jmp 0x6613
movq -0x480(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x490(%rbp)
movl -0x40(%rbp), %edi
movl -0x3c(%rbp), %esi
callq 0x8bc0
movl %eax, %ecx
movq -0x490(%rbp), %rax
movslq %ecx, %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x62c4
jmp 0x6613
movl -0x34(%rbp), %eax
movl %eax, -0x494(%rbp)
movl -0x40(%rbp), %edi
movl -0x3c(%rbp), %esi
callq 0x8c30
movq -0x480(%rbp), %rdi
movl -0x494(%rbp), %esi
movzwl %ax, %edx
callq 0x5890
testb $0x1, %al
jne 0x62f3
jmp 0x633f
movq -0x20(%rbp), %rax
movq %rax, -0x4a0(%rbp)
movl -0x40(%rbp), %edi
movl -0x3c(%rbp), %esi
callq 0x8bc0
movl %eax, %ecx
movq -0x4a0(%rbp), %rax
movslq %ecx, %rcx
movl $0x1, (%rax,%rcx,4)
cmpl $0x1, -0x34(%rbp)
jne 0x632f
movq -0x18(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x633a
movq -0x10(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x6613
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movl 0xdea5(%rip), %ecx      # 0x141f4
imull 0xde9e(%rip), %ecx      # 0x141f4
cmpl %ecx, %eax
jge 0x6374
movq -0x28(%rbp), %rax
movslq -0x44(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x6346
movq -0x480(%rbp), %rdi
movl $0x0, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x34(%rbp), %esi
movl -0x40(%rbp), %edx
movl -0x3c(%rbp), %ecx
movq -0x20(%rbp), %r8
movq -0x28(%rbp), %r9
leaq -0x48(%rbp), %r10
leaq -0x4c(%rbp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x66b0
movl -0x48(%rbp), %eax
addl -0x4c(%rbp), %eax
movl 0xde34(%rip), %ecx      # 0x141f0
imull 0xde2d(%rip), %ecx      # 0x141f0
cmpl %ecx, %eax
jne 0x63cc
jmp 0x6613
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
movl 0xde18(%rip), %ecx      # 0x141f4
imull 0xde11(%rip), %ecx      # 0x141f4
cmpl %ecx, %eax
jge 0x640b
movq -0x28(%rbp), %rax
movslq -0x50(%rbp), %rcx
movl (%rax,%rcx,4), %edx
xorl $0x1, %edx
movq -0x30(%rbp), %rax
movslq -0x50(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x63d3
movl $0x0, -0x54(%rbp)
movl $0x1, -0x58(%rbp)
movl -0x58(%rbp), %eax
cmpl 0xddce(%rip), %eax      # 0x141f0
jg 0x6546
movl $0x1, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl 0xddb8(%rip), %eax      # 0x141f0
jg 0x6536
movq -0x30(%rbp), %rax
movq %rax, -0x4a8(%rbp)
movl -0x5c(%rbp), %edi
movl -0x58(%rbp), %esi
callq 0x8bc0
movl %eax, %ecx
movq -0x4a8(%rbp), %rax
movslq %ecx, %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x646b
jmp 0x6528
movq -0x480(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x4b0(%rbp)
movl -0x5c(%rbp), %edi
movl -0x58(%rbp), %esi
callq 0x8bc0
movl %eax, %ecx
movq -0x4b0(%rbp), %rax
movslq %ecx, %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x649e
jmp 0x6528
leaq -0x460(%rbp), %rdi
movq %rdi, -0x4c0(%rbp)
callq 0x8ed0
movq -0x4c0(%rbp), %rsi
movq -0x480(%rbp), %rdi
movl -0x34(%rbp), %edx
movl -0x5c(%rbp), %ecx
movl -0x58(%rbp), %r8d
movq -0x30(%rbp), %r9
movq %rsp, %rax
movq %rsi, (%rax)
xorl %esi, %esi
callq 0x5e20
movb %al, -0x4b1(%rbp)
jmp 0x64e2
movb -0x4b1(%rbp), %al
testb $0x1, %al
jne 0x64ee
jmp 0x651c
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x651c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x468(%rbp)
movl %eax, -0x46c(%rbp)
leaq -0x460(%rbp), %rdi
callq 0x8f30
jmp 0x66a1
leaq -0x460(%rbp), %rdi
callq 0x8f30
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x642f
jmp 0x6538
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x6419
movl $0x0, -0x470(%rbp)
cmpl $0x0, -0x54(%rbp)
jne 0x65ed
movl -0x48(%rbp), %eax
addl -0x470(%rbp), %eax
movl %eax, -0x470(%rbp)
movl $0x0, -0x474(%rbp)
movl -0x474(%rbp), %eax
movl 0xdc75(%rip), %ecx      # 0x141f4
imull 0xdc6e(%rip), %ecx      # 0x141f4
cmpl %ecx, %eax
jge 0x65eb
movq -0x28(%rbp), %rax
movslq -0x474(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x65d8
movq -0x480(%rbp), %rax
movq (%rax), %rax
movslq -0x474(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x4c4(%rbp)
movl -0x34(%rbp), %edi
callq 0x8c10
movl %eax, %ecx
movl -0x4c4(%rbp), %eax
cmpl %ecx, %eax
jne 0x65d8
movl -0x470(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x470(%rbp)
jmp 0x65da
movl -0x474(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x474(%rbp)
jmp 0x6573
jmp 0x65ed
cmpl $0x1, -0x34(%rbp)
jne 0x6603
movl -0x470(%rbp), %ecx
movq -0x18(%rbp), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
jmp 0x6611
movl -0x470(%rbp), %ecx
movq -0x10(%rbp), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
jmp 0x6613
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x6255
jmp 0x6623
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x623f
jmp 0x6633
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x61f9
movq -0x20(%rbp), %rax
movq %rax, -0x4d0(%rbp)
cmpq $0x0, %rax
je 0x665e
movq -0x4d0(%rbp), %rdi
callq 0x22a0
movq -0x28(%rbp), %rax
movq %rax, -0x4d8(%rbp)
cmpq $0x0, %rax
je 0x667b
movq -0x4d8(%rbp), %rdi
callq 0x22a0
movq -0x30(%rbp), %rax
movq %rax, -0x4e0(%rbp)
cmpq $0x0, %rax
je 0x6698
movq -0x4e0(%rbp), %rdi
callq 0x22a0
addq $0x4f0, %rsp            # imm = 0x4F0
popq %rbp
retq
movq -0x468(%rbp), %rdi
callq 0x2350
nopl (%rax)