// File: /workspace/github_cpp_makefile_star2_O3/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 0x8fcd(%rip), %al       # 0x112e8
testb %al, %al
je 0x86be
movb 0x8fdf(%rip), %al       # 0x11308
testb %al, %al
je 0x86c8
movl 0xac(%rbx), %ecx
movl 0xb0(%rbx), %eax
cmpl %ecx, %eax
jle 0x83d0
movq 0x8f84(%rip), %rdx      # 0x112d0
movq 0x8f85(%rip), %rsi      # 0x112d8
xorl %eax, %eax
cmpq %rsi, %rdx
je 0x8451
leaq 0x453b(%rip), %rdi      # 0xc8a0
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 0x83b5
movl $0xa, %r11d
cmpl $0xa, %r8d
ja 0x83b5
movl %r8d, %r11d
addl $-0x4, %r11d
movl (%rdi,%r11,4), %r11d
jmp 0x83b5
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 0x839d
addl %r8d, %eax
addl %r11d, %ecx
addq $0x18, %rdx
cmpq %rsi, %rdx
jne 0x8367
incl %ecx
jmp 0x8456
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 0x83fa
incl %eax
movl %eax, 0xb0(%rbx)
jmp 0x8509
testl %eax, %eax
jle 0x854b
movl %eax, %edx
andl $0x4, %edx
movl $0x6, %ecx
cmovel %edx, %ecx
movq 0x8eba(%rip), %rdx      # 0x112d0
movq 0x8ebb(%rip), %rsi      # 0x112d8
cmpq %rsi, %rdx
je 0x851b
movq (%rdx), %rdi
movq 0x8(%rdx), %r8
cmpq %r8, %rdi
je 0x844b
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 0x842d
addq $0x18, %rdx
jmp 0x841d
movl $0x1, %ecx
movslq 0x70(%rbx), %rdx
leaq 0x43ef(%rip), %rsi      # 0xc850
addl -0x4(%rsi,%rdx,4), %ecx
imull %eax, %ecx
addl %ecx, 0x6c(%rbx)
cvtsi2sd %ecx, %xmm0
divsd 0x3cd1(%rip), %xmm0     # 0xc148
callq 0x3080
cvtsi2sdl 0x78(%rbx), %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
movl 0x74(%rbx), %ecx
testl %ecx, %ecx
jle 0x84a6
testl %eax, %eax
jle 0x84a6
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 0x8e40(%rip), %rax      # 0x112f0
movq 0x8e41(%rip), %rcx      # 0x112f8
cmpq %rcx, %rax
je 0x84d5
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 0x84b7
leaq 0x8df4(%rip), %rdi      # 0x112d0
callq 0x89be
movq 0x8e08(%rip), %rax      # 0x112f0
cmpq %rax, 0x8e09(%rip)      # 0x112f8
je 0x84f8
movq %rax, 0x8e00(%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 0x8dce(%rip), %rdx      # 0x112f0
movq 0x8dcf(%rip), %rsi      # 0x112f8
cmpq %rsi, %rdx
je 0x83ed
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 0x8529
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 %r12d, %r12d
xorl %r13d, %r13d
movq $0x0, -0x48(%rbp)
leaq (,%r13,2), %rax
addq %r13, %rax
leaq (%rbx,%rax,2), %r15
xorl %r14d, %r14d
movzbl (%r14,%r15), %eax
testl %eax, %eax
je 0x8667
cmpl $0x5, %eax
je 0x8667
xorpd %xmm0, %xmm0
movapd %xmm0, -0x60(%rbp)
movq %r12, -0x50(%rbp)
movq %r12, -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 0x8a00
movq -0x58(%rbp), %rax
subq -0x60(%rbp), %rax
cmpq $0x19, %rax
jb 0x861c
leaq 0x8ce4(%rip), %rdi      # 0x112d0
leaq -0x60(%rbp), %rsi
callq 0x8bc4
movq 0x8cfc(%rip), %rsi      # 0x112f8
movq -0x40(%rbp), %rdx
movq -0x38(%rbp), %rcx
leaq 0x8ce5(%rip), %rdi      # 0x112f0
callq 0x8fa6
movb $0x1, %al
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rdi
jmp 0x864f
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x864f
movq %rdi, %rcx
movslq 0x4(%rcx), %rdx
movslq (%rcx), %rsi
leaq (%rdx,%rdx,2), %rdx
leaq -0xb0(,%rdx,2), %rdx
addq %rbp, %rdx
movb $0x0, (%rsi,%rdx)
addq $0x8, %rcx
cmpq %rax, %rcx
jne 0x862c
testq %rdi, %rdi
je 0x8659
callq 0x3260
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x8667
callq 0x3260
incq %r14
cmpq $0x6, %r14
jne 0x858e
incq %r13
cmpq $0xd, %r13
jne 0x857c
testb $0x1, -0x48(%rbp)
je 0x8699
movl $0x1, 0xb0(%rbx)
incl 0x70(%rbx)
jmp 0x8509
movl $0x0, 0x70(%rbx)
cmpb $0x0, 0x8(%rbx)
je 0x86b2
movl $0x6, 0x68(%rbx)
jmp 0x8509
movl $0x3, 0x68(%rbx)
jmp 0x8509
callq 0x34d0
jmp 0x8323
callq 0x351f
jmp 0x8331
jmp 0x86d4
movq %rax, %rbx
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x86e5
callq 0x3260
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x86f3
callq 0x3260
movq %rbx, %rdi
callq 0x3480
nop