// File: /workspace/github_cpp_makefile_star2_O1/jeffreyan11[P]go-engine/search.cpp
// Function: generateMove(int, unsigned short)
Move generateMove(Player p, Move lastMove) {
    MoveList legalMoves = game.getLegalMoves(p);
    MoveList localMoves = game.getLocalMoves(lastMove);

    // Pass if every move is either into your own eye, a suicide, or places
    // a chain into atari
    bool playPass = true;
    for (unsigned int n = 0; n < legalMoves.size(); n++) {
        Board copy = Board(game);
        Move m = legalMoves.get(n);

        if (!copy.isMoveValid(otherPlayer(p), m) && copy.isEye(p, m))
            continue;

        if (!copy.isMoveValid(p, m))
            continue;

        copy.doMove(p, m);
        if (copy.isInAtari(m))
            continue;

        playPass = false;
        break;
    }

    if (playPass)
        return MOVE_PASS;


    MCTree searchTree;
    float komiAdjustment = 0.0;
    Move captureLastStone = game.getPotentialCapture(lastMove);
    Move potentialEscape = game.getPotentialEscape(p, lastMove);

    // Add all first-level moves
    for (unsigned int n = 0; n < legalMoves.size(); n++) {
        Board copy = Board(game);
        Player genPlayer = p;

        Move next = legalMoves.get(n);
        // Check legality of moves (suicide)
        if (!copy.isMoveValid(genPlayer, next))
            continue;

        copy.doMove(genPlayer, next);
        // Never place own chain in atari
        if (copy.isInAtari(next))
            continue;

        // Check for ko rule violation
        bool koViolation = false;
        if (next != MOVE_PASS) {
            uint64_t newKey = copy.getZobristKey();
            for (int i = keyStackSize-1; i >= 0; i--) {
                if (newKey == keyStack[i]) {
                    koViolation = true;
                    break;
                }
            }
        }
        if (koViolation)
            continue;

        // First level moves are added to the root
        MCNode *leaf = searchTree.root;
        MCNode *addition = new MCNode();
        addition->parent = leaf;
        addition->m = next;

        // Play out a random game. The final board state will be stored in copy.
        playRandomGame(otherPlayer(genPlayer), copy);

        // Score the game
        float myScore = 0.0, oppScore = 0.0;
        scoreGame(genPlayer, copy, myScore, oppScore);
        if (myScore > oppScore)
            addition->numerator++;
        addition->scoreDiff = ((int) myScore) - ((int) oppScore);
        komiAdjustment += myScore - oppScore;

        // Add the new node to the tree
        leaf->children[leaf->size] = addition;
        leaf->size++;

        // Backpropagate the results
        searchTree.backPropagate(addition);

        // Do priors, if any
        // Own eye and opening priors inspired by Pachi,
        // written by Petr Baudis and Jean-loup Gailly
        int basePrior = boardSize * boardSize / 8;
        // Discourage playing into own eyes
        if (game.isEye(genPlayer, next)) {
            addition->denominator += basePrior;
            // If this eye is not ko-related we almost certainly should not play
            // in it
            if (!game.isMoveValid(otherPlayer(genPlayer), next)) {
                addition->denominator += 10 * basePrior;
                addition->scoreDiff -= 10 * 360;
            }
        }

        // Discourage playing onto edges and encourage playing onto the 4th line
        // in 13x13 and 19x19 openings
        unsigned int openingMoves = boardSize * boardSize - boardSize;
        if ((boardSize == 13 || boardSize == 19) && legalMoves.size() > openingMoves) {
            int x = getX(next);
            int y = getY(next);
            if (x == 1 || x == 19 || y == 1 || y == 19) {
                addition->denominator += 2 * basePrior;
            }
            else {
                int taperedPrior = basePrior * (legalMoves.size() - openingMoves) / boardSize;
                if (x == 4 || x == boardSize-3) {
                    addition->numerator += 2 * taperedPrior;
                    addition->denominator += 2 * taperedPrior;
                }
                if (y == 4 || y == boardSize-3) {
                    addition->numerator += 2 * taperedPrior;
                    addition->denominator += 2 * taperedPrior;
                }
                if (x == 3 || x == boardSize-2 || y == 3 || y == boardSize-2) {
                    addition->numerator += taperedPrior;
                    addition->denominator += taperedPrior;
                }
            }
        }
        // And the same for 9x9
        else if (boardSize == 9 && legalMoves.size() > openingMoves) {
            int x = getX(next);
            int y = getY(next);
            if (x == 1 || x == boardSize || y == 1 || y == boardSize) {
                addition->denominator += 2 * basePrior;
            }
            else {
                int taperedPrior = basePrior * (legalMoves.size() - openingMoves) / boardSize;
                if (x == 3 || x == boardSize-2) {
                    addition->numerator += 2 * taperedPrior;
                    addition->denominator += 2 * taperedPrior;
                }
                if (y == 3 || y == boardSize-2) {
                    addition->numerator += 2 * taperedPrior;
                    addition->denominator += 2 * taperedPrior;
                }
            }
        }

        // Add a bonus for capturing a chain that the opponent placed
        // into atari on the previous move
        if (next == captureLastStone) {
            addition->numerator += 5 * basePrior;
            addition->denominator += 5 * basePrior;
        }

        // Add a bonus for escaping when the opponent's last move
        // placed our chain into atari
        if (next == potentialEscape) {
            addition->numerator += 5 * basePrior;
            addition->denominator += 5 * basePrior;
        }

        // Add a bonus to local moves
        if (legalMoves.size() < openingMoves) {
            int li = localMoves.find(next);
            if (li != -1) {
                localMoves.removeFast(li);
            }
            else {
                addition->numerator += basePrior;
                addition->denominator += 2 * basePrior;
            }
        }
    }

    // If we have no moves that are ko-legal, pass.
    if (searchTree.root->size == 0)
        return MOVE_PASS;

    // Calculate an estimate of a komi adjustment
    komiAdjustment /= legalMoves.size();


    // Expand the MC tree iteratively
    for (int n = 0; n < playouts; n++) {
        Board copy = Board(game);
        Player genPlayer = p;

        // Find a node in the tree to add a child to
        int depth = -1;
        MCNode *leaf = searchTree.findLeaf(genPlayer, copy, depth);

        MCNode *addition = new MCNode();
        addition->parent = leaf;
        MoveList candidates = copy.getLegalMoves(genPlayer);
        candidates.add(MOVE_PASS);

        // Set up a permutation matrix
        int *permutation = new int[candidates.size()];
        // Fisher-Yates shuffle
        for (unsigned int i = 0; i < candidates.size(); i++) {
            std::uniform_int_distribution<int> distribution(0, i);
            int j = distribution(rng);
            permutation[i] = permutation[j];
            permutation[j] = i;
        }

        // Find a random move that has not been explored yet
        Move next = 0;
        for (unsigned int i = 0; i < candidates.size(); i++) {
            next = candidates.get(permutation[i]);

            bool used = false;
            for (int j = 0; j < leaf->size; j++) {
                if (next == leaf->children[j]->m) {
                    used = true;
                    break;
                }
            }

            if (!used && copy.isMoveValid(genPlayer, next))
                break;
        }

        delete[] permutation;

        addition->m = next;
        copy.doMove(genPlayer, next);

        // Play out a random game. The final board state will be stored in copy.
        playRandomGame(otherPlayer(genPlayer), copy);

        // Score the game... somehow...
        float myScore = 0.0, oppScore = 0.0;
        scoreGame(genPlayer, copy, myScore, oppScore);
        myScore += (genPlayer == p) ? -komiAdjustment : komiAdjustment;

        if (myScore > oppScore) {
            addition->numerator++;
            // If the node is not a child of root
            if (depth)
                raveTable.inc(next, depth);
        }
        else {
            if (depth)
                raveTable.dec(next, depth);
        }
        addition->scoreDiff = ((int) myScore) - ((int) oppScore);

        // Add the new node to the tree
        leaf->children[leaf->size] = addition;
        leaf->size++;

        // Backpropagate the results
        searchTree.backPropagate(addition);
    }


    // Find the highest scoring move
    Move bestMove = searchTree.root->children[0]->m;
    double bestScore = 0.0;
    int64_t diff = -(1 << 30);
    int maxRAVE = raveTable.max();
    for (int i = 0; i < searchTree.root->size; i++) {
        double candidateScore = (double) searchTree.root->children[i]->numerator
                              / (double) searchTree.root->children[i]->denominator;
                            // +   (double) raveTable.score(searchTree.root->children[i]->m)
                            //   / ((double) maxRAVE)
                            //   / (16 + std::sqrt(searchTree.root->children[i]->denominator))
                            // +   (double) searchTree.root->children[i]->scoreDiff
                            //   / (double) (360 * 32);

        // if (candidateScore > bestScore) {
        //     bestScore = candidateScore;
        //     bestMove = searchTree.root->children[i]->m;
        // }

        if (debugOutput) {
            std::cerr << "(" << getX(searchTree.root->children[i]->m) << ", "
                      << getY(searchTree.root->children[i]->m) << "): "
                      << searchTree.root->children[i]->numerator << " / "
                      << searchTree.root->children[i]->denominator << std::endl;
        }

        if (candidateScore > bestScore
         || (candidateScore == bestScore && searchTree.root->children[i]->scoreDiff > diff)) {
            bestScore = candidateScore;
            bestMove = searchTree.root->children[i]->m;
            diff = searchTree.root->children[i]->scoreDiff;
        }
    }

    raveTable.age();

    return bestMove;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1ca8, %rsp           # imm = 0x1CA8
movl %esi, %r12d
movl %edi, %r14d
leaq 0x8cc6(%rip), %rbx      # 0xe8f8
leaq 0x470(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
movl %r14d, %edx
callq 0x40f8
leaq 0x874(%rsp), %rdi
movq %rbx, %rsi
movl %r12d, %edx
callq 0x5070
cmpl $0x0, 0x400(%r15)
movl %r14d, 0x10(%rsp)
je 0x5d2c
movl %r12d, 0x18(%rsp)
movl $0x3, %ebp
subl %r14d, %ebp
movb $0x1, %r14b
leaq 0xc78(%rsp), %r12
xorl %r15d, %r15d
movq %r12, %rdi
leaq 0x8c65(%rip), %rsi      # 0xe8f8
callq 0x2eba
movslq %r15d, %rax
movzwl 0x470(%rsp,%rax,2), %ebx
movq %r12, %rdi
movl %ebp, %esi
movl %ebx, %edx
callq 0x3e1e
testb %al, %al
jne 0x5cc8
movq %r12, %rdi
movl 0x10(%rsp), %esi
movl %ebx, %edx
callq 0x4050
movb $0x1, %r13b
testb %al, %al
jne 0x5d07
movq %r12, %rdi
movl 0x10(%rsp), %esi
movl %ebx, %edx
callq 0x3e1e
movb $0x1, %r13b
testb %al, %al
je 0x5d07
movq %r12, %rdi
movl 0x10(%rsp), %esi
movl %ebx, %edx
callq 0x3046
movq %r12, %rdi
movl %ebx, %esi
callq 0x4920
movl %eax, %r13d
movzbl %r14b, %r14d
testb %al, %al
movl $0x0, %eax
cmovel %eax, %r14d
movq %r12, %rdi
callq 0x2fd8
testb %r13b, %r13b
je 0x5d25
incl %r15d
cmpl 0x870(%rsp), %r15d
jb 0x5c89
movl 0x18(%rsp), %r12d
jmp 0x5d2f
movb $0x1, %r14b
movw $0x8000, %bp            # imm = 0x8000
testb $0x1, %r14b
movl 0x10(%rsp), %r15d
jne 0x6690
movq %rsp, %rdi
callq 0x69be
movzwl %r12w, %ebx
leaq 0x8ba3(%rip), %rdi      # 0xe8f8
movl %ebx, %esi
callq 0x496c
movw %ax, 0x2e(%rsp)
leaq 0x8b90(%rip), %rdi      # 0xe8f8
movl %r15d, %esi
movl %ebx, %edx
callq 0x49c8
movw %ax, 0x2c(%rsp)
movl 0x870(%rsp), %eax
testl %eax, %eax
je 0x617f
movl $0x3, %eax
subl %r15d, %eax
movl %eax, 0x50(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
leaq 0x8b57(%rip), %rbp      # 0xe8f8
leaq 0xc78(%rsp), %r13
xorl %ebx, %ebx
movq %r13, %rdi
movq %rbp, %rsi
callq 0x2eba
movslq %ebx, %rax
movzwl 0x470(%rsp,%rax,2), %r12d
movzwl %r12w, %r14d
movq %r13, %rdi
movl %r15d, %esi
movl %r14d, %edx
callq 0x3e1e
testb %al, %al
je 0x6164
movq %r13, %rdi
movl %r15d, %esi
movl %r14d, %edx
callq 0x3046
movq %r13, %rdi
movl %r14d, %esi
callq 0x4920
testb %al, %al
jne 0x6164
movl %r12d, %eax
negw %ax
jno 0x600f
movq (%rsp), %r15
movl $0x28, %edi
callq 0x21b0
movq %rax, %rbp
movabsq $0x100000000, %rax      # imm = 0x100000000
movq %rax, (%rbp)
xorl %eax, %eax
movq %rax, 0x8(%rbp)
movl $0x0, 0x10(%rbp)
movq %rax, 0x18(%rbp)
movl $0x1000, %edi           # imm = 0x1000
callq 0x2030
movq %rax, 0x20(%rbp)
movq %r15, 0x18(%rbp)
movw %r12w, 0x10(%rbp)
movl 0x50(%rsp), %edi
movq %r13, %rsi
callq 0x6716
xorl %eax, %eax
movl %eax, 0x6c(%rsp)
movl %eax, 0x20(%rsp)
movl 0x10(%rsp), %edi
movq %r13, %rsi
leaq 0x6c(%rsp), %rdx
leaq 0x20(%rsp), %rcx
callq 0x690c
movss 0x6c(%rsp), %xmm0
movss 0x20(%rsp), %xmm1
ucomiss %xmm1, %xmm0
jbe 0x5e90
incl (%rbp)
movss %xmm0, 0x18(%rsp)
cvttss2si %xmm0, %eax
movss %xmm1, 0x30(%rsp)
cvttss2si %xmm1, %ecx
subl %ecx, %eax
cltq
movq %rax, 0x8(%rbp)
movq 0x20(%r15), %rax
movswq 0x12(%r15), %rcx
movq %rbp, (%rax,%rcx,8)
leal 0x1(%rcx), %eax
movw %ax, 0x12(%r15)
movq %rsp, %rdi
movq %rbp, %rsi
callq 0x5a5e
leaq 0x62b5(%rip), %rax      # 0xc188
movl (%rax), %r15d
leaq 0x8a1b(%rip), %rdi      # 0xe8f8
movl 0x10(%rsp), %esi
movl %r14d, %edx
callq 0x4050
imull %r15d, %r15d
shrl $0x3, %r15d
testb %al, %al
movss 0x18(%rsp), %xmm1
je 0x5f2d
addl %r15d, 0x4(%rbp)
leaq 0x89f2(%rip), %rdi      # 0xe8f8
movl 0x50(%rsp), %esi
movl %r14d, %edx
callq 0x3e1e
testb %al, %al
movss 0x18(%rsp), %xmm1
jne 0x5f2d
leal (%r15,%r15,4), %eax
addl %eax, %eax
addl %eax, 0x4(%rbp)
addq $-0xe10, 0x8(%rbp)      # imm = 0xF1F0
leaq 0x6254(%rip), %rax      # 0xc188
movl (%rax), %esi
leal -0x1(%rsi), %ecx
imull %esi, %ecx
cmpl $0x9, %esi
je 0x6044
cmpl $0x13, %esi
je 0x5f53
cmpl $0xd, %esi
jne 0x60bf
movl 0x870(%rsp), %eax
subl %ecx, %eax
jbe 0x603f
movzbl %r14b, %edi
cmpl $0x13, %edi
je 0x605c
cmpw $0x1, %di
je 0x605c
shrl $0x8, %r14d
cmpw $0x1, %r14w
je 0x605c
cmpw $0x13, %r14w
je 0x605c
imull %r15d, %eax
xorl %edx, %edx
divl %esi
cmpw $0x4, %r14w
sete %r8b
leal -0x3(%rsi), %edx
cmpl %r14d, %edx
sete %r9b
orb %r8b, %r9b
cmpb $0x1, %r9b
jne 0x5fc3
leal (%rax,%rax), %r8d
addl %r8d, (%rbp)
addl %r8d, 0x4(%rbp)
cmpw $0x4, %di
sete %r8b
cmpl %edi, %edx
sete %dl
orb %r8b, %dl
cmpb $0x1, %dl
jne 0x5fe1
leal (%rax,%rax), %edx
addl %edx, (%rbp)
addl %edx, 0x4(%rbp)
cmpw $0x3, %r14w
je 0x60b9
addl $-0x2, %esi
cmpl %edi, %esi
je 0x60b9
cmpw $0x3, %di
je 0x60b9
cmpl %r14d, %esi
jne 0x60bf
jmp 0x60b9
movq %r13, %rdi
callq 0x5324
movl 0x11913(%rip), %ecx     # 0x17930
testl %ecx, %ecx
jle 0x5e09
leaq -0x1(%rcx), %rdx
leaq 0x9900(%rip), %rsi      # 0xf930
cmpq -0x8(%rsi,%rcx,8), %rax
movq %rdx, %rcx
jne 0x601d
jmp 0x6164
cmpl $0x9, %esi
jne 0x60bf
movl 0x870(%rsp), %eax
subl %ecx, %eax
jbe 0x60bf
movl %r14d, %r8d
shrl $0x8, %r8d
cmpl $0x1, %r8d
jne 0x6062
leal (%r15,%r15), %eax
jmp 0x60bc
movzbl %r14b, %edi
cmpl %edi, %esi
je 0x605c
cmpw $0x1, %di
je 0x605c
cmpl %r8d, %esi
je 0x605c
imull %r15d, %eax
xorl %edx, %edx
divl %esi
cmpw $0x3, %r8w
sete %dl
addl $-0x2, %esi
cmpl %r8d, %esi
sete %r8b
orb %dl, %r8b
cmpb $0x1, %r8b
jne 0x60a1
leal (%rax,%rax), %edx
addl %edx, (%rbp)
addl %edx, 0x4(%rbp)
cmpw $0x3, %di
sete %dl
cmpl %edi, %esi
sete %sil
orb %dl, %sil
cmpb $0x1, %sil
jne 0x60bf
addl %eax, %eax
addl %eax, (%rbp)
addl %eax, 0x4(%rbp)
cmpw 0x2e(%rsp), %r12w
jne 0x60d1
leal (%r15,%r15,4), %eax
addl %eax, (%rbp)
addl %eax, 0x4(%rbp)
cmpw 0x2c(%rsp), %r12w
jne 0x60e3
leal (%r15,%r15,4), %eax
addl %eax, (%rbp)
addl %eax, 0x4(%rbp)
cmpl %ecx, 0x870(%rsp)
jae 0x6144
movl 0xc74(%rsp), %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x6116
xorl %edx, %edx
cmpw %r12w, 0x874(%rsp,%rdx,2)
je 0x6114
incq %rdx
cmpq %rdx, %rax
jne 0x60ff
jmp 0x6116
movl %edx, %ecx
cmpl $-0x1, %ecx
je 0x6139
decl %eax
movzwl 0x874(%rsp,%rax,2), %edx
movslq %ecx, %rcx
movw %dx, 0x874(%rsp,%rcx,2)
movl %eax, 0xc74(%rsp)
jmp 0x6144
addl %r15d, (%rbp)
addl %r15d, %r15d
addl %r15d, 0x4(%rbp)
subss 0x30(%rsp), %xmm1
movaps 0x40(%rsp), %xmm0
addss %xmm1, %xmm0
movaps %xmm0, 0x40(%rsp)
movl 0x10(%rsp), %r15d
leaq 0x8794(%rip), %rbp      # 0xe8f8
movq %r13, %rdi
callq 0x2fd8
incl %ebx
movl 0x870(%rsp), %eax
cmpl %eax, %ebx
jb 0x5dab
jmp 0x6187
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movq (%rsp), %rcx
cmpw $0x0, 0x12(%rcx)
je 0x6684
cmpl $0x0, 0x5fff(%rip)      # 0xc19c
jle 0x64b0
movl %eax, %eax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
movaps 0x40(%rsp), %xmm1
divss %xmm0, %xmm1
movaps 0x2e63(%rip), %xmm0     # 0x9020
movaps %xmm1, 0x40(%rsp)
xorps %xmm1, %xmm0
movaps %xmm0, 0x50(%rsp)
leaq 0xc78(%rsp), %rbx
leaq 0x20(%rsp), %r13
xorl %r14d, %r14d
movq %rbx, %rdi
leaq 0x8714(%rip), %rsi      # 0xe8f8
callq 0x2eba
movl %r15d, 0xc(%rsp)
movl $0xffffffff, 0x28(%rsp) # imm = 0xFFFFFFFF
movq %rsp, %rdi
leaq 0xc(%rsp), %rsi
movq %rbx, %rdx
leaq 0x28(%rsp), %rcx
callq 0x58c4
movq %rax, %r12
movl $0x28, %edi
callq 0x21b0
leaq 0x11f59(%rip), %r15     # 0x18178
movq %rax, %rbp
movabsq $0x100000000, %rax      # imm = 0x100000000
movq %rax, (%rbp)
xorl %eax, %eax
movq %rax, 0x8(%rbp)
movl $0x0, 0x10(%rbp)
movq %rax, 0x18(%rbp)
movl $0x1000, %edi           # imm = 0x1000
callq 0x2030
movq %rax, 0x20(%rbp)
movq %rbp, 0x18(%rsp)
movq %r12, 0x18(%rbp)
movl 0xc(%rsp), %edx
leaq 0x6c(%rsp), %rdi
movq %rbx, %rsi
callq 0x40f8
movl %r14d, 0x30(%rsp)
movl 0x46c(%rsp), %eax
movw $0x8000, 0x6c(%rsp,%rax,2) # imm = 0x8000
leal 0x1(%rax), %edi
movl %edi, 0x46c(%rsp)
shlq $0x2, %rdi
callq 0x2030
movq %rax, %rbx
cmpl $0x0, 0x46c(%rsp)
je 0x62d6
xorl %r14d, %r14d
movl $0x0, 0x20(%rsp)
movl %r14d, 0x24(%rsp)
movq %r13, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x5a8a
cltq
movl (%rbx,%rax,4), %ecx
movl %ecx, (%rbx,%r14,4)
movl %r14d, (%rbx,%rax,4)
incq %r14
movl 0x46c(%rsp), %eax
cmpq %rax, %r14
jb 0x629f
cmpl $0x0, 0x46c(%rsp)
je 0x633c
xorl %r14d, %r14d
movslq (%rbx,%r14,4), %rax
movzwl 0x6c(%rsp,%rax,2), %r15d
movswq 0x12(%r12), %rax
testq %rax, %rax
jle 0x6312
movq 0x20(%r12), %rcx
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
cmpw 0x10(%rsi), %r15w
je 0x632b
incq %rdx
cmpq %rdx, %rax
jne 0x62ff
movl 0xc(%rsp), %esi
movzwl %r15w, %edx
leaq 0xc78(%rsp), %rdi
callq 0x3e1e
testb %al, %al
jne 0x633f
incq %r14
movl 0x46c(%rsp), %eax
cmpq %rax, %r14
jb 0x62e3
jmp 0x633f
xorl %r15d, %r15d
movq %rbx, %rdi
callq 0x2200
movq 0x18(%rsp), %rax
movw %r15w, 0x10(%rax)
movl 0xc(%rsp), %esi
movzwl %r15w, %ebx
leaq 0xc78(%rsp), %rbp
movq %rbp, %rdi
movl %ebx, %edx
callq 0x3046
movl $0x3, %edi
subl 0xc(%rsp), %edi
movq %rbp, %rsi
callq 0x6716
xorl %eax, %eax
movl %eax, 0x20(%rsp)
movl %eax, 0x3c(%rsp)
movl 0xc(%rsp), %edi
movq %rbp, %rsi
movq %r13, %rdx
leaq 0x3c(%rsp), %rcx
callq 0x690c
movl 0x30(%rsp), %r14d
movaps 0x50(%rsp), %xmm0
movl 0x10(%rsp), %eax
cmpl %eax, 0xc(%rsp)
movq 0x18(%rsp), %r8
je 0x63b8
movaps 0x40(%rsp), %xmm0
addss 0x20(%rsp), %xmm0
movss %xmm0, 0x20(%rsp)
movss 0x3c(%rsp), %xmm1
ucomiss %xmm1, %xmm0
jbe 0x6416
incl (%r8)
movl 0x28(%rsp), %eax
testl %eax, %eax
je 0x6458
movl %r15d, %ecx
negw %cx
jo 0x6458
movl $0xfffffffb, %ecx       # imm = 0xFFFFFFFB
subl %eax, %ecx
imull %eax, %ecx
addl $0x96, %ecx
testl %ecx, %ecx
movl $0x0, %eax
cmovlel %eax, %ecx
shrl $0x8, %ebx
movzbl %r15b, %eax
imulq $0x5c, %rbx, %rdx
leaq 0x11526(%rip), %rsi     # 0x17934
addq %rsi, %rdx
addl %ecx, (%rdx,%rax,4)
jmp 0x6458
movl 0x28(%rsp), %eax
testl %eax, %eax
je 0x6458
movl %r15d, %ecx
negw %cx
jo 0x6458
movl $0xfffffffb, %ecx       # imm = 0xFFFFFFFB
subl %eax, %ecx
imull %eax, %ecx
addl $0x96, %ecx
testl %ecx, %ecx
movl $0x0, %eax
cmovlel %eax, %ecx
shrl $0x8, %ebx
movzbl %r15b, %eax
imulq $0x5c, %rbx, %rdx
leaq 0x114e2(%rip), %rsi     # 0x17934
addq %rsi, %rdx
subl %ecx, (%rdx,%rax,4)
cvttss2si %xmm0, %eax
cvttss2si %xmm1, %ecx
subl %ecx, %eax
cltq
movq %rax, 0x8(%r8)
movq 0x20(%r12), %rax
movswq 0x12(%r12), %rcx
movq %r8, (%rax,%rcx,8)
leal 0x1(%rcx), %eax
movw %ax, 0x12(%r12)
movq %rsp, %rdi
movq %r8, %rsi
callq 0x5a5e
leaq 0xc78(%rsp), %rbx
movq %rbx, %rdi
callq 0x2fd8
incl %r14d
cmpl 0x5cf7(%rip), %r14d     # 0xc19c
movl 0x10(%rsp), %r15d
jl 0x61da
movq (%rsp), %rax
movq 0x20(%rax), %rcx
movq (%rcx), %rcx
movzwl 0x10(%rcx), %ebp
cmpw $0x0, 0x12(%rax)
jle 0x6652
xorps %xmm0, %xmm0
movsd %xmm0, 0x30(%rsp)
movq $-0x40000000, %r13      # imm = 0xC0000000
xorl %r15d, %r15d
leaq 0x121ec(%rip), %r12     # 0x186d0
movq 0x5b05(%rip), %rbx      # 0xbff0
movq 0x20(%rax), %rax
movq (%rax,%r15,8), %rax
xorps %xmm0, %xmm0
cvtsi2sdl (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdl 0x4(%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
cmpb $0x1, (%r12)
jne 0x65f9
movl $0x1, %edx
movq %rbx, %rdi
leaq 0x2b08(%rip), %rsi      # 0x9030
callq 0x21f0
movq (%rsp), %rax
movq 0x20(%rax), %rax
movq (%rax,%r15,8), %rax
movzbl 0x11(%rax), %esi
movq %rbx, %rdi
callq 0x22b0
movq %rax, %r14
movl $0x2, %edx
movq %rax, %rdi
leaq 0x2adb(%rip), %rsi      # 0x9032
callq 0x21f0
movq (%rsp), %rax
movq 0x20(%rax), %rax
movq (%rax,%r15,8), %rax
movzbl 0x10(%rax), %esi
movq %r14, %rdi
callq 0x22b0
movq %rax, %r14
movl $0x3, %edx
movq %rax, %rdi
leaq 0x2aaf(%rip), %rsi      # 0x9035
callq 0x21f0
movq (%rsp), %rax
movq 0x20(%rax), %rax
movq (%rax,%r15,8), %rax
movl (%rax), %esi
movq %r14, %rdi
callq 0x22b0
movq %rax, %r14
movl $0x3, %edx
movq %rax, %rdi
leaq 0x2a86(%rip), %rsi      # 0x9039
callq 0x21f0
movq (%rsp), %rax
movq 0x20(%rax), %rax
movq (%rax,%r15,8), %rax
movl 0x4(%rax), %esi
movq %r14, %rdi
callq 0x22b0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2110
movsd 0x10(%rsp), %xmm0
divsd 0x18(%rsp), %xmm0
ucomisd 0x30(%rsp), %xmm0
ja 0x6623
jne 0x663d
jp 0x663d
movq (%rsp), %rax
movq 0x20(%rax), %rax
movq (%rax,%r15,8), %rax
cmpq %r13, 0x8(%rax)
jle 0x663d
movq (%rsp), %rax
movq 0x20(%rax), %rax
movq (%rax,%r15,8), %rax
movzwl 0x10(%rax), %ebp
movq 0x8(%rax), %r13
movsd %xmm0, 0x30(%rsp)
incq %r15
movq (%rsp), %rax
movswq 0x12(%rax), %rcx
cmpq %rcx, %r15
jl 0x64eb
leaq 0x112db(%rip), %rax     # 0x17934
xorl %ecx, %ecx
xorl %edx, %edx
movl (%rax,%rdx,4), %esi
movl %esi, %edi
shrl $0x1f, %edi
addl %esi, %edi
sarl %edi
movl %edi, (%rax,%rdx,4)
incq %rdx
cmpq $0x17, %rdx
jne 0x665d
incq %rcx
addq $0x5c, %rax
cmpq $0x17, %rcx
jne 0x665b
jmp 0x6688
movw $0x8000, %bp            # imm = 0x8000
movq %rsp, %rdi
callq 0x6a22
movl %ebp, %eax
addq $0x1ca8, %rsp           # imm = 0x1CA8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x66ef
jmp 0x66ef
jmp 0x66d2
jmp 0x66f6
jmp 0x66ba
jmp 0x66f6
jmp 0x66f6
jmp 0x66f6
jmp 0x66ef
jmp 0x66f6
jmp 0x66f6
movq %rax, %rbx
movl $0x28, %esi
movq %rbp, %rdi
callq 0x21c0
jmp 0x66f9
jmp 0x66f6
jmp 0x66f6
jmp 0x66ef
movq %rax, %rbx
jmp 0x670e
jmp 0x66f6
jmp 0x66f6
jmp 0x66f6
movq %rax, %rbx
leaq 0xc78(%rsp), %rdi
callq 0x2fd8
jmp 0x670e
movq %rax, %rbx
jmp 0x6706
jmp 0x66f6
movq %rax, %rbx
leaq 0xc78(%rsp), %rdi
callq 0x2fd8
movq %rsp, %rdi
callq 0x6a22
movq %rbx, %rdi
callq 0x22c0