// File: /workspace/github_cpp_makefile_star2_O1/Leeingnyo[P]newPuyoC/./board.h
// Function: Board::ChainingUpdate()
void ChainingUpdate() {
        bool chaining = false;
        static std::vector<std::vector<Pair>> chain_list;
        static std::vector<Pair> removed_obstacles;
        static const int CHAIN_BONUS[] = {0, 8, 16, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 480, 512};
        static const int MAX_REMOVE_BONUS[] = {0, 2, 3, 4, 5, 6, 7};
        // FIXME: move this to static member variables (without cpp file, I want)
        // but it caused link error
        
        if (chaining_t > CHAINING_FRAME) {
            int removed_puyo_number = 0;
            int max_removed_bonus = 0;
            for (auto &chain : chain_list) {
                int chain_size = chain.size();
                removed_puyo_number += chain_size;
                max_removed_bonus += chain_size < 4 ? 0 : chain_size > 10 ? 10 : MAX_REMOVE_BONUS[chain_size - 4];
                // FIXME: I want to this logic to function 
                for (auto &pair : chain) {
                    map[pair.y][pair.x] = Puyo();
                }
            }
            // http://kayezero.blogspot.kr/2009/08/blog-post_8347.html 점수 계산법
            int gained_score = removed_puyo_number * (CHAIN_BONUS[chain_number - 1] + max_removed_bonus/* + 12*/ + 1);
            gain_score += gained_score;
            obstacle_number_to_send += std::ceil(gained_score / 7.0);
            while (obstacle_number_taken > 0 && obstacle_number_to_send > 0) {
                obstacle_number_taken -= 1;
                obstacle_number_to_send -= 1;
            }
            // TODO: 에잇 받아라 아이스스톰 등 effect 발행
            for (auto &pair : removed_obstacles) {
                map[pair.y][pair.x] = Puyo();
            }
            chain_list.clear();
            removed_obstacles.clear();
            chaining_t = 0;
            state = State::DROP;
            return;
        }
        else if (chaining_t > CHAINING_FRAME * 2 / 3) {
            chaining_t++;
            return;
        }
        else if (chaining_t > 0) {
            Puyo effect = Puyo(chaining_t / 4 % 2 ? PuyoKind::EXPLOSION : PuyoKind::BLANK);
            for (auto &chain : chain_list) {
                for (auto &pair : chain) {
                    map[pair.y][pair.x] = effect;
                }
            }
            for (auto &pair : removed_obstacles) {
                map[pair.y][pair.x] = effect;
            }
            chaining_t++;
            return;
        }
        
        // 맵 끝에서 끝까지 
        bool check[MAP_HEIGHT][MAP_WIDTH] = { false, };
        for (int y = 0; y < MAP_HEIGHT; y++) {
            for (int x = 0; x < MAP_WIDTH; x++) {
                if (map[y][x].IsBlank()) continue;
                if (map[y][x].IsObstacle()) continue;
                
                std::vector<Pair> chain;
                std::vector<Pair> nearby_obstacles;
                Chaining(check, chain, nearby_obstacles, x, y);
                if (chain.size() >= 4) { // FIXME make 4 constant. extract it
                    chaining = true;
                    chain_list.push_back(chain);
                    removed_obstacles.insert(removed_obstacles.end(), nearby_obstacles.begin(), nearby_obstacles.end());
                }
                else {
                    for (auto& obstacles : nearby_obstacles) {
                        check[obstacles.y][obstacles.x] = false;
                    }
                }
            }
        }
        
        if (chaining) {
            chaining_t = 1;
            chain_number++;
        }
        else {
            chain_number = 0;
            if (map[1][2].IsBlank()) state = State::DROP_OBSTACLE;
            else state = State::GAMEOVER;
        }
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdi, %rbx
movb 0x8f5d(%rip), %al       # 0x112e8
testb %al, %al
je 0x871f
movb 0x8f6f(%rip), %al       # 0x11308
testb %al, %al
je 0x8729
movl 0xac(%rbx), %ecx
movl 0xb0(%rbx), %eax
cmpl %ecx, %eax
jle 0x8440
movq 0x8f14(%rip), %rdx      # 0x112d0
movq 0x8f15(%rip), %rsi      # 0x112d8
xorl %eax, %eax
cmpq %rsi, %rdx
je 0x84c1
leaq 0x44eb(%rip), %rdi      # 0xc8c0
xorl %ecx, %ecx
movq (%rdx), %r9
movq 0x8(%rdx), %r10
movq %r10, %r8
subq %r9, %r8
sarq $0x3, %r8
movl $0x0, %r11d
cmpl $0x4, %r8d
jl 0x8425
movl $0xa, %r11d
cmpl $0xa, %r8d
jg 0x8425
movl %r8d, %r11d
addl $-0x4, %r11d
movl (%rdi,%r11,4), %r11d
jmp 0x8425
movslq 0x4(%r9), %r14
movslq (%r9), %r15
leaq (%r14,%r14,2), %r14
leaq (%rbx,%r14,2), %r14
movb $0x0, (%r15,%r14)
addq $0x8, %r9
cmpq %r10, %r9
jne 0x840d
addl %r8d, %eax
addl %r11d, %ecx
addq $0x18, %rdx
cmpq %rsi, %rdx
jne 0x83d7
incl %ecx
jmp 0x84c6
addl %ecx, %ecx
movslq %ecx, %rcx
imulq $0x55555556, %rcx, %rcx # imm = 0x55555556
movq %rcx, %rdx
shrq $0x3f, %rdx
shrq $0x20, %rcx
addl %edx, %ecx
cmpl %ecx, %eax
jle 0x846a
incl %eax
movl %eax, 0xb0(%rbx)
jmp 0x8579
testl %eax, %eax
jle 0x85bb
movl %eax, %edx
andl $0x4, %edx
movl $0x6, %ecx
cmovel %edx, %ecx
movq 0x8e4a(%rip), %rdx      # 0x112d0
movq 0x8e4b(%rip), %rsi      # 0x112d8
cmpq %rsi, %rdx
je 0x858b
movq (%rdx), %rdi
movq 0x8(%rdx), %r8
cmpq %r8, %rdi
je 0x84bb
movslq 0x4(%rdi), %r9
movslq (%rdi), %r10
leaq (%r9,%r9,2), %r9
leaq (%rbx,%r9,2), %r9
movb %cl, (%r10,%r9)
addq $0x8, %rdi
jmp 0x849d
addq $0x18, %rdx
jmp 0x848d
movl $0x1, %ecx
movslq 0x70(%rbx), %rdx
leaq 0x439f(%rip), %rsi      # 0xc870
addl -0x4(%rsi,%rdx,4), %ecx
imull %eax, %ecx
addl %ecx, 0x6c(%rbx)
cvtsi2sd %ecx, %xmm0
divsd 0x3c81(%rip), %xmm0     # 0xc168
callq 0x3080
cvtsi2sdl 0x78(%rbx), %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
movl 0x74(%rbx), %ecx
testl %ecx, %ecx
jle 0x8516
testl %eax, %eax
jle 0x8516
decl %ecx
decl %eax
cmpl %ecx, %eax
movl %ecx, %edx
cmovbl %eax, %edx
subl %edx, %ecx
subl %edx, %eax
movl %ecx, 0x74(%rbx)
movl %eax, 0x78(%rbx)
movq 0x8dd0(%rip), %rax      # 0x112f0
movq 0x8dd1(%rip), %rcx      # 0x112f8
cmpq %rcx, %rax
je 0x8545
movslq 0x4(%rax), %rdx
movslq (%rax), %rsi
leaq (%rdx,%rdx,2), %rdx
leaq (%rbx,%rdx,2), %rdx
movb $0x0, (%rsi,%rdx)
addq $0x8, %rax
jmp 0x8527
leaq 0x8d84(%rip), %rdi      # 0x112d0
callq 0x8a0a
movq 0x8d98(%rip), %rax      # 0x112f0
cmpq %rax, 0x8d99(%rip)      # 0x112f8
je 0x8568
movq %rax, 0x8d90(%rip)      # 0x112f8
movl $0x0, 0xb0(%rbx)
movl $0x1, 0x68(%rbx)
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8d5e(%rip), %rdx      # 0x112f0
movq 0x8d5f(%rip), %rsi      # 0x112f8
cmpq %rsi, %rdx
je 0x845d
movslq 0x4(%rdx), %rdi
movslq (%rdx), %r8
leaq (%rdi,%rdi,2), %rdi
leaq (%rbx,%rdi,2), %rdi
movb %cl, (%r8,%rdi)
addq $0x8, %rdx
jmp 0x8599
xorpd %xmm0, %xmm0
leaq -0xb0(%rbp), %rax
movupd %xmm0, 0x3e(%rax)
movapd %xmm0, 0x30(%rax)
movapd %xmm0, 0x20(%rax)
movapd %xmm0, 0x10(%rax)
movapd %xmm0, (%rax)
xorl %r13d, %r13d
xorl %r15d, %r15d
leaq (,%r13,2), %rax
addq %r13, %rax
leaq (%rbx,%rax,2), %r12
xorl %r14d, %r14d
movzbl (%r14,%r12), %eax
testl %eax, %eax
je 0x8699
cmpl $0x5, %eax
je 0x8699
xorpd %xmm0, %xmm0
movapd %xmm0, -0x60(%rbp)
xorl %eax, %eax
movq %rax, -0x50(%rbp)
movq %rax, -0x30(%rbp)
movapd %xmm0, -0x40(%rbp)
movq %rbx, %rdi
leaq -0xb0(%rbp), %rsi
leaq -0x60(%rbp), %rdx
leaq -0x40(%rbp), %rcx
movl %r14d, %r8d
movl %r13d, %r9d
callq 0x8a4c
movq -0x58(%rbp), %rax
subq -0x60(%rbp), %rax
cmpq $0x19, %rax
jb 0x86a8
leaq 0x8c7a(%rip), %rdi      # 0x112d0
leaq -0x60(%rbp), %rsi
callq 0x8c10
movq 0x8c92(%rip), %rsi      # 0x112f8
movq -0x40(%rbp), %rdx
movq -0x38(%rbp), %rcx
movb $0x1, %r15b
leaq 0x8c78(%rip), %rdi      # 0x112f0
callq 0x9000
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x868b
callq 0x3260
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x8699
callq 0x3260
incq %r14
cmpq $0x6, %r14
jne 0x85f6
jmp 0x86d5
movq -0x40(%rbp), %rax
movq -0x38(%rbp), %rcx
cmpq %rcx, %rax
je 0x867d
movslq 0x4(%rax), %rdx
movslq (%rax), %rsi
leaq (%rdx,%rdx,2), %rdx
leaq -0xb0(,%rdx,2), %rdx
addq %rbp, %rdx
movb $0x0, (%rsi,%rdx)
addq $0x8, %rax
jmp 0x86b0
incq %r13
cmpq $0xd, %r13
jne 0x85e4
testb $0x1, %r15b
je 0x86fa
movl $0x1, 0xb0(%rbx)
incl 0x70(%rbx)
jmp 0x8579
movl $0x0, 0x70(%rbx)
cmpb $0x0, 0x8(%rbx)
je 0x8713
movl $0x6, 0x68(%rbx)
jmp 0x8579
movl $0x3, 0x68(%rbx)
jmp 0x8579
callq 0x34d6
jmp 0x8393
callq 0x3525
jmp 0x83a1
jmp 0x8735
movq %rax, %rbx
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x8746
callq 0x3260
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x8754
callq 0x3260
movq %rbx, %rdi
callq 0x3480