// File: disjukr[P]Tetris/Tetris.cc
// Function: Tetris::Loop()
void Tetris::Loop() {
    if (Keyboard::hit()) {
        switch(Keyboard::code()) {
        case UP: case W:
            this -> ControlPiece([this]() {
                currentPiece -> RotateCW();
            });
            break;
        case X: case Q:
            this -> ControlPiece([this]() {
                currentPiece -> RotateCCW();
            });
            break;
        case LEFT: case A:
            this -> ControlPiece([this]() {
                --(currentPiece -> x);
            });
            break;
        case RIGHT: case D:
            this -> ControlPiece([this]() {
                ++(currentPiece -> x);
            });
            break;
        case DOWN: case S:
            this -> SoftDrop();
            ++statistics.score;
            break;
        case SPACE: case F:
            this -> HardDrop();
            break;
        case C: case R:
            this -> HardDrop(false);
            break;
        case Z: case E:
            this -> HoldPiece();
            break;
        case ESC:
            this -> Exit();
            return;
        default:
            break;
        }
    }
    int frameDiff = (int) (frame - lastDrop);
    if (dropFrameInterval < frameDiff) {
        if (this -> SoftDrop(attachFrameInterval < frameDiff))
            lastDrop = frame;
    }
    if (this -> CheckGameOver()) {
        this -> Exit();
        return;
    }
    int lineCount = 0;
    for (int i = 0; i < TetrisStage::height; ++i) {
        if (this -> CheckLine(i)) {
            this -> stage.EraseLine(i);
            ++lineCount;
        }
    }
    switch (lineCount) {
    case 1: ++statistics._single; statistics.score += 100; break;
    case 2: ++statistics._double; statistics.score += 300; break;
    case 3: ++statistics._triple; statistics.score += 500; break;
    case 4: ++statistics._tetris; statistics.score += 800; break;
    }
    if (statistics.score > statistics.level * 1000)
        LevelUp();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xd0(%rbp)
callq 0xb7f0
testb $0x1, %al
jne 0x43e8
jmp 0x460e
callq 0xb810
movl %eax, %ecx
movq %rcx, -0xd8(%rbp)
subl $0x10, %eax
ja 0x460a
movq -0xd8(%rbp), %rax
leaq 0x7c2f(%rip), %rcx      # 0xc03c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0xd0(%rbp), %rax
movq %rax, -0x30(%rbp)
leaq -0x28(%rbp), %rdi
movq %rdi, -0xe0(%rbp)
leaq -0x30(%rbp), %rsi
callq 0x4870
movq -0xd0(%rbp), %rdi
movq -0xe0(%rbp), %rsi
callq 0x47d0
jmp 0x444a
leaq -0x28(%rbp), %rdi
callq 0x72d0
jmp 0x460c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x72d0
jmp 0x47be
movq -0xd0(%rbp), %rax
movq %rax, -0x68(%rbp)
leaq -0x60(%rbp), %rdi
movq %rdi, -0xe8(%rbp)
leaq -0x68(%rbp), %rsi
callq 0x4900
movq -0xd0(%rbp), %rdi
movq -0xe8(%rbp), %rsi
callq 0x47d0
jmp 0x44a6
leaq -0x60(%rbp), %rdi
callq 0x72d0
jmp 0x460c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x72d0
jmp 0x47be
movq -0xd0(%rbp), %rax
movq %rax, -0x90(%rbp)
leaq -0x88(%rbp), %rdi
movq %rdi, -0xf0(%rbp)
leaq -0x90(%rbp), %rsi
callq 0x4990
movq -0xd0(%rbp), %rdi
movq -0xf0(%rbp), %rsi
callq 0x47d0
jmp 0x450b
leaq -0x88(%rbp), %rdi
callq 0x72d0
jmp 0x460c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x72d0
jmp 0x47be
movq -0xd0(%rbp), %rax
movq %rax, -0xb8(%rbp)
leaq -0xb0(%rbp), %rdi
movq %rdi, -0xf8(%rbp)
leaq -0xb8(%rbp), %rsi
callq 0x4a20
movq -0xd0(%rbp), %rdi
movq -0xf8(%rbp), %rsi
callq 0x47d0
jmp 0x4576
leaq -0xb0(%rbp), %rdi
callq 0x72d0
jmp 0x460c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x72d0
jmp 0x47be
movq -0xd0(%rbp), %rdi
movl $0x1, %esi
callq 0x4ab0
movq -0xd0(%rbp), %rax
movl 0x20(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x20(%rax)
jmp 0x460c
movq -0xd0(%rbp), %rdi
movl $0x1, %esi
callq 0x4b30
jmp 0x460c
movq -0xd0(%rbp), %rdi
xorl %esi, %esi
callq 0x4b30
jmp 0x460c
movq -0xd0(%rbp), %rdi
callq 0x4ba0
jmp 0x460c
movq -0xd0(%rbp), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
jmp 0x47b5
jmp 0x460c
jmp 0x460e
movq -0xd0(%rbp), %rax
movl 0xc(%rax), %ecx
subl 0x1c(%rax), %ecx
movl %ecx, -0xbc(%rbp)
movl 0x14(%rax), %eax
cmpl -0xbc(%rbp), %eax
jge 0x465f
movq -0xd0(%rbp), %rdi
movl 0x18(%rdi), %eax
cmpl -0xbc(%rbp), %eax
setl %al
movzbl %al, %esi
andl $0x1, %esi
callq 0x4ab0
testb $0x1, %al
jne 0x4650
jmp 0x465d
movq -0xd0(%rbp), %rax
movl 0xc(%rax), %ecx
movl %ecx, 0x1c(%rax)
jmp 0x465f
movq -0xd0(%rbp), %rdi
callq 0x4c30
testb $0x1, %al
jne 0x4671
jmp 0x4683
movq -0xd0(%rbp), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
jmp 0x47b5
movl $0x0, -0xc0(%rbp)
movl $0x0, -0xc4(%rbp)
cmpl $0x16, -0xc4(%rbp)
jge 0x46f0
movq -0xd0(%rbp), %rdi
movl -0xc4(%rbp), %esi
callq 0x4c90
testb $0x1, %al
jne 0x46b8
jmp 0x46dd
movq -0xd0(%rbp), %rdi
addq $0x38, %rdi
movl -0xc4(%rbp), %esi
callq 0x4cf0
movl -0xc0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc0(%rbp)
jmp 0x46df
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
jmp 0x4697
movl -0xc0(%rbp), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x100(%rbp)
subl $0x3, %eax
ja 0x4794
movq -0x100(%rbp), %rax
leaq 0x7968(%rip), %rcx      # 0xc080
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0xd0(%rbp), %rax
movl 0x28(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x28(%rax)
movl 0x20(%rax), %ecx
addl $0x64, %ecx
movl %ecx, 0x20(%rax)
jmp 0x4794
movq -0xd0(%rbp), %rax
movl 0x2c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x2c(%rax)
movl 0x20(%rax), %ecx
addl $0x12c, %ecx            # imm = 0x12C
movl %ecx, 0x20(%rax)
jmp 0x4794
movq -0xd0(%rbp), %rax
movl 0x30(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x30(%rax)
movl 0x20(%rax), %ecx
addl $0x1f4, %ecx            # imm = 0x1F4
movl %ecx, 0x20(%rax)
jmp 0x4794
movq -0xd0(%rbp), %rax
movl 0x34(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x34(%rax)
movl 0x20(%rax), %ecx
addl $0x320, %ecx            # imm = 0x320
movl %ecx, 0x20(%rax)
movq -0xd0(%rbp), %rcx
movl 0x20(%rcx), %eax
imull $0x3e8, 0x24(%rcx), %ecx # imm = 0x3E8
cmpl %ecx, %eax
jle 0x47b5
movq -0xd0(%rbp), %rdi
callq 0x4d70
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
movq -0x38(%rbp), %rdi
callq 0x3270
nopw (%rax,%rax)