// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x488, %rsp            # imm = 0x488
movq %rdi, %r14
xorl %eax, %eax
movq %rsi, 0x48(%rsp)
movl %eax, (%rsi)
movq %rdx, 0x50(%rsp)
movl %eax, (%rdx)
movl 0x7d42(%rip), %ebx      # 0xc18c
imull %ebx, %ebx
shlq $0x2, %rbx
movq %rbx, %rdi
callq 0x2030
movq %rax, %r12
movq %rbx, %rdi
callq 0x2030
movq %rax, %r13
movq %rbx, %rdi
callq 0x2030
movq %r14, %rdi
movq %rax, %rbp
movl $0x1, %r15d
movq %r14, 0x10(%rsp)
movq %r12, 0x60(%rsp)
movq %r13, 0x58(%rsp)
movl 0x7cfc(%rip), %edx      # 0xc18c
testl %edx, %edx
je 0x44b0
imull %edx, %edx
cmpl $0x1, %edx
adcl $0x0, %edx
shlq $0x2, %rdx
movq %r12, %rdi
xorl %esi, %esi
callq 0x20f0
movq 0x10(%rsp), %rdi
cmpl $0x0, 0x7cd1(%rip)      # 0xc188
jle 0x474b
movl $0x3, %eax
subl %r15d, %eax
movl %eax, 0x24(%rsp)
cmpl $0x1, %r15d
movq 0x48(%rsp), %rax
cmoveq 0x50(%rsp), %rax
movq %rax, 0x68(%rsp)
movl $0x1, %ecx
movl %r15d, 0xc(%rsp)
movl 0x7c9b(%rip), %ebx      # 0xc188
testl %ebx, %ebx
jle 0x473a
movl $0x1, %edx
movq %rcx, 0x30(%rsp)
movslq 0x7c86(%rip), %rsi      # 0xc18c
movq %rsi, %rax
movq %rsi, 0x18(%rsp)
imull %ecx, %eax
addl %edx, %eax
movslq %eax, %r14
cmpl $0x0, (%r12,%r14,4)
jne 0x4727
movq (%rdi), %rax
cmpl $0x0, (%rax,%r14,4)
jne 0x4727
movq %rdx, 0x38(%rsp)
movl %edx, %eax
shll $0x8, %eax
orl %ecx, %eax
movzwl %ax, %edx
movl %r15d, %esi
callq 0x4050
testb %al, %al
je 0x4566
movl $0x1, (%r12,%r14,4)
cmpl $0x1, %r15d
jne 0x4701
movq 0x50(%rsp), %rax
jmp 0x4706
movq 0x18(%rsp), %rax
movl %eax, %r15d
imull %r15d, %r15d
testl %eax, %eax
je 0x458e
cmpl $0x1, %r15d
movl %r15d, %edx
adcl $0x0, %edx
shlq $0x2, %rdx
movq %r13, %rdi
xorl %esi, %esi
callq 0x20f0
xorl %eax, %eax
movl %eax, 0x2c(%rsp)
movl %eax, 0x28(%rsp)
movq 0x10(%rsp), %r14
movq %r14, %rdi
movl 0xc(%rsp), %esi
movq 0x38(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq %r12, %r8
movq %r13, %r9
leaq 0x28(%rsp), %rax
pushq %rax
leaq 0x34(%rsp), %rax
pushq %rax
callq 0x4782
movq %r14, %rdi
addq $0x10, %rsp
movl 0x2c(%rsp), %esi
movl 0x28(%rsp), %eax
addl %esi, %eax
movl %ebx, %ecx
imull %ebx, %ecx
cmpl %ecx, %eax
je 0x4718
cmpl $0x0, 0x18(%rsp)
je 0x460a
cmpl $0x1, %r15d
movl %r15d, %eax
adcl $0x0, %eax
xorl %ecx, %ecx
movl (%r13,%rcx,4), %edx
xorl $0x1, %edx
movl %edx, (%rbp,%rcx,4)
incq %rcx
cmpq %rcx, %rax
jne 0x45f6
testl %ebx, %ebx
movl 0x24(%rsp), %r8d
jle 0x46cd
movl %esi, 0x44(%rsp)
movq %r15, 0x70(%rsp)
incl %ebx
movq 0x18(%rsp), %rax
leaq 0x1(%rax), %r15
xorl %r12d, %r12d
movl $0x1, %r13d
movq %r15, 0x78(%rsp)
movl $0x1, %r14d
cmpl $0x0, (%rbp,%r15,4)
jne 0x4691
movq (%rdi), %rax
cmpl $0x0, (%rax,%r15,4)
jne 0x4691
movl $0x0, 0x484(%rsp)
subq $0x8, %rsp
movq 0x18(%rsp), %rdi
xorl %esi, %esi
movl 0x14(%rsp), %edx
movl %r14d, %ecx
movl %r13d, %r8d
movq %rbp, %r9
leaq 0x8c(%rsp), %rax
pushq %rax
callq 0x428e
movq 0x20(%rsp), %rdi
addq $0x10, %rsp
movzbl %al, %eax
addl %eax, %r12d
incq %r14
incq %r15
cmpq %r14, %rbx
jne 0x463f
incq %r13
movq 0x78(%rsp), %r15
addq 0x18(%rsp), %r15
cmpq %rbx, %r13
jne 0x4634
xorl %eax, %eax
testl %r12d, %r12d
movq 0x60(%rsp), %r12
movq 0x58(%rsp), %r13
movl 0x24(%rsp), %r8d
movq 0x70(%rsp), %r15
movl 0x44(%rsp), %esi
jne 0x4711
cmpl $0x0, 0x18(%rsp)
je 0x470f
movq (%rdi), %rcx
cmpl $0x1, %r15d
adcl $0x0, %r15d
xorl %edx, %edx
movl %esi, %eax
cmpl $0x0, (%r13,%rdx,4)
je 0x46f7
xorl %esi, %esi
cmpl %r8d, (%rcx,%rdx,4)
sete %sil
addl %esi, %eax
incq %rdx
cmpq %rdx, %r15
jne 0x46e3
jmp 0x4711
movq 0x48(%rsp), %rax
incl (%rax)
movq 0x10(%rsp), %rdi
jmp 0x471d
movl %esi, %eax
movq 0x68(%rsp), %rcx
addl %eax, (%rcx)
movl 0xc(%rsp), %r15d
movq 0x30(%rsp), %rcx
movq 0x38(%rsp), %rdx
leal 0x1(%rdx), %eax
movl 0x7a58(%rip), %ebx      # 0xc188
cmpl %ebx, %edx
movl %eax, %edx
jl 0x44ff
leal 0x1(%rcx), %eax
cmpl 0x7a45(%rip), %ecx      # 0xc188
movl %eax, %ecx
jl 0x44e7
incl %r15d
cmpl $0x3, %r15d
jne 0x448a
movq %r12, %rdi
callq 0x2200
movq %r13, %rdi
callq 0x2200
movq %rbp, %rdi
callq 0x2200
addq $0x488, %rsp            # imm = 0x488
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq