// File: /workspace/github_cpp_makefile_star2_O2/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 0x9517(%rip), %al       # 0x102e0
testb %al, %al
je 0x7112
movb 0x9529(%rip), %al       # 0x10300
testb %al, %al
je 0x7165
movl 0xac(%rbx), %eax
movl 0xb0(%rbx), %ecx
cmpl %eax, %ecx
jle 0x6e68
movq 0x94d2(%rip), %rdx      # 0x102c8
xorl %eax, %eax
movq 0x94d1(%rip), %rsi      # 0x102d0
pushq $0xa
popq %rdi
leaq 0x3a17(%rip), %r8       # 0xa820
xorl %ecx, %ecx
cmpq %rsi, %rdx
je 0x6e85
movq (%rdx), %r9
movq 0x8(%rdx), %r10
movq %r10, %r14
subq %r9, %r14
sarq $0x3, %r14
xorl %r11d, %r11d
cmpl $0x4, %r14d
jl 0x6e3e
movl %edi, %r11d
cmpl $0xa, %r14d
ja 0x6e3e
movl %r14d, %r11d
addl $-0x4, %r11d
movl (%r8,%r11,4), %r11d
addl %r14d, %ecx
cmpq %r10, %r9
je 0x6e5f
movslq 0x4(%r9), %r14
movslq (%r9), %r15
imulq $0x6, %r14, %r14
addq %rbx, %r14
movb $0x0, (%r15,%r14)
addq $0x8, %r9
jmp 0x6e41
addl %r11d, %eax
addq $0x18, %rdx
jmp 0x6e0b
addl %eax, %eax
pushq $0x3
popq %rsi
cltd
idivl %esi
cmpl %eax, %ecx
jle 0x6f41
incl %ecx
movl %ecx, 0xb0(%rbx)
jmp 0x6f2f
movslq 0x70(%rbx), %rdx
leaq 0x3940(%rip), %rsi      # 0xa7d0
movl -0x4(%rsi,%rdx,4), %edx
addl %edx, %eax
incl %eax
imull %ecx, %eax
addl %eax, 0x6c(%rbx)
cvtsi2sd %eax, %xmm0
divsd 0x3216(%rip), %xmm0     # 0xa0c0
callq 0x3070
cvtsi2sdl 0x78(%rbx), %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
movl 0x74(%rbx), %ecx
testl %ecx, %ecx
jle 0x6ed0
testl %eax, %eax
jle 0x6ed0
decl %ecx
movl %ecx, 0x74(%rbx)
decl %eax
jmp 0x6ebf
movl %eax, 0x78(%rbx)
movq 0x940e(%rip), %rax      # 0x102e8
movq 0x940f(%rip), %rcx      # 0x102f0
cmpq %rcx, %rax
je 0x6efe
movslq 0x4(%rax), %rdx
movslq (%rax), %rsi
imulq $0x6, %rdx, %rdx
addq %rbx, %rdx
movb $0x0, (%rsi,%rdx)
addq $0x8, %rax
jmp 0x6ee1
leaq 0x93c3(%rip), %rdi      # 0x102c8
callq 0x7410
movq 0x93d7(%rip), %rax      # 0x102e8
cmpq %rax, 0x93d8(%rip)      # 0x102f0
je 0x6f21
movq %rax, 0x93cf(%rip)      # 0x102f0
andl $0x0, 0xb0(%rbx)
movl $0x1, 0x68(%rbx)
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %ecx, %ecx
jle 0x6fbc
movl %ecx, %edx
andl $0x4, %edx
pushq $0x6
popq %rax
cmovel %edx, %eax
movq 0x9371(%rip), %rdx      # 0x102c8
movq 0x9372(%rip), %rsi      # 0x102d0
cmpq %rsi, %rdx
je 0x6f8d
movq (%rdx), %rdi
movq 0x8(%rdx), %r8
cmpq %r8, %rdi
je 0x6f87
movslq 0x4(%rdi), %r9
movslq (%rdi), %r10
imulq $0x6, %r9, %r9
addq %rbx, %r9
movb %al, (%r10,%r9)
addq $0x8, %rdi
jmp 0x6f6a
addq $0x18, %rdx
jmp 0x6f5e
movq 0x9354(%rip), %rdx      # 0x102e8
movq 0x9355(%rip), %rsi      # 0x102f0
cmpq %rsi, %rdx
je 0x6e78
movslq 0x4(%rdx), %rdi
movslq (%rdx), %r8
imulq $0x6, %rdi, %rdi
addq %rbx, %rdi
movb %al, (%r8,%rdi)
addq $0x8, %rdx
jmp 0x6f9b
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 %r15d, %r15d
xorl %r13d, %r13d
cmpq $0xd, %r15
je 0x70d8
imulq $0x6, %r15, %r12
addq %rbx, %r12
xorl %r14d, %r14d
cmpq $0x6, %r14
je 0x70d0
movzbl (%r14,%r12), %eax
testl %eax, %eax
je 0x709c
cmpl $0x5, %eax
je 0x709c
xorpd %xmm0, %xmm0
movapd %xmm0, -0x60(%rbp)
andq $0x0, -0x50(%rbp)
movapd %xmm0, -0x40(%rbp)
andq $0x0, -0x30(%rbp)
movq %rbx, %rdi
leaq -0xb0(%rbp), %rsi
leaq -0x60(%rbp), %rdx
leaq -0x40(%rbp), %rcx
movl %r14d, %r8d
movl %r15d, %r9d
callq 0x741e
movq -0x58(%rbp), %rax
subq -0x60(%rbp), %rax
cmpq $0x19, %rax
jb 0x70a4
leaq 0x9265(%rip), %rdi      # 0x102c8
leaq -0x60(%rbp), %rsi
callq 0x7598
movq 0x927d(%rip), %rsi      # 0x102f0
movb $0x1, %r13b
movq -0x40(%rbp), %rdx
movq -0x38(%rbp), %rcx
leaq 0x9263(%rip), %rdi      # 0x102e8
callq 0x75d4
leaq -0x40(%rbp), %rdi
callq 0x762e
leaq -0x60(%rbp), %rdi
callq 0x762e
incq %r14
jmp 0x6ff9
movq -0x40(%rbp), %rax
movq -0x38(%rbp), %rcx
cmpq %rcx, %rax
je 0x708a
movslq 0x4(%rax), %rdx
movslq (%rax), %rsi
imulq $0x6, %rdx, %rdx
addq %rbp, %rdx
addq $-0xb0, %rdx
movb $0x0, (%rsi,%rdx)
addq $0x8, %rax
jmp 0x70ac
incq %r15
jmp 0x6fe5
testb $0x1, %r13b
je 0x70f0
movl $0x1, 0xb0(%rbx)
incl 0x70(%rbx)
jmp 0x6f2f
andl $0x0, 0x70(%rbx)
cmpb $0x0, 0x8(%rbx)
je 0x7106
movl $0x6, 0x68(%rbx)
jmp 0x6f2f
movl $0x3, 0x68(%rbx)
jmp 0x6f2f
leaq 0x91c7(%rip), %rdi      # 0x102e0
callq 0x3480
testl %eax, %eax
je 0x6dd1
xorpd %xmm0, %xmm0
movupd %xmm0, 0x9196(%rip)     # 0x102c8
andq $0x0, 0x919e(%rip)      # 0x102d8
leaq 0x9187(%rip), %rsi      # 0x102c8
leaq 0x292(%rip), %rdi       # 0x73da
leaq 0x9111(%rip), %rdx      # 0x10260
callq 0x3210
leaq 0x9185(%rip), %rdi      # 0x102e0
callq 0x31c0
jmp 0x6dd1
leaq 0x9194(%rip), %rdi      # 0x10300
callq 0x3480
testl %eax, %eax
je 0x6ddf
xorpd %xmm0, %xmm0
movupd %xmm0, 0x9163(%rip)     # 0x102e8
andq $0x0, 0x916b(%rip)      # 0x102f8
leaq 0x9154(%rip), %rsi      # 0x102e8
leaq 0x26b(%rip), %rdi       # 0x7406
leaq 0x90be(%rip), %rdx      # 0x10260
callq 0x3210
leaq 0x9152(%rip), %rdi      # 0x10300
callq 0x31c0
jmp 0x6ddf
jmp 0x71ba
movq %rax, %rbx
leaq -0x40(%rbp), %rdi
callq 0x762e
leaq -0x60(%rbp), %rdi
callq 0x762e
movq %rbx, %rdi
callq 0x3470
nop