// File: miklhh[P]tetris/src/game.cpp
// Function: current_block_rotate(bool)
void current_block_rotate(rotation_t counter_clockwise)
{
    /* Test if the current block can rotate. */
    if (test_rotation(current_block, counter_clockwise) == NO_COLLISION)
    {
        block_rotate(current_block, counter_clockwise);
        return;
    }

    /* Test if a left wall kick can be preformed. */
    block_t temp_block = *current_block;
    block_move(&temp_block, DIRECTION_RIGHT);
    if (test_rotation(&temp_block, counter_clockwise) == NO_COLLISION)
    {
        block_move(current_block, DIRECTION_RIGHT);
        block_rotate(current_block, counter_clockwise);
        return;
    }

    /* Test if a right side wall kick can be preformed. */
    temp_block = *current_block;
    block_move(&temp_block, DIRECTION_LEFT);
    if (test_rotation(&temp_block, counter_clockwise) == NO_COLLISION)
    {
        block_move(current_block, DIRECTION_LEFT);
        block_rotate(current_block, counter_clockwise);
        return;
    }

    //   ------------------------------------------------------------
    //   -------------        SPECTIAL BLOCKS       -----------------
    //   ------------------------------------------------------------

    /* 1x4 block, also known as I-block. */
    if (current_block->block_type == BLOCK_I)
    {
        /* Test a left side double wall kick. */
        temp_block = *current_block;
        for (int i = 0; i < 2; i++)
            block_move(&temp_block, DIRECTION_RIGHT);

        if (test_rotation(&temp_block, counter_clockwise)  == NO_COLLISION)
        {
            for (int i = 0; i < 2; i++)
                block_move(current_block, DIRECTION_RIGHT);

            block_rotate(current_block, counter_clockwise);
            return;
        }



        /* Test a right side double wall kick. */
        temp_block = *current_block;
        for (int i = 0; i < 2; i++)
            block_move(&temp_block, DIRECTION_LEFT);

        if (test_rotation(&temp_block, counter_clockwise) == NO_COLLISION)
        {
            for (int i = 0; i < 2; i++)
                block_move(current_block, DIRECTION_LEFT);

            block_rotate(current_block, counter_clockwise);
            return;
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movb %dil, %al
andb $0x1, %al
movb %al, -0x1(%rbp)
movq 0x5101(%rip), %rdi      # 0x8ad8
movb -0x1(%rbp), %al
andb $0x1, %al
movzbl %al, %esi
callq 0x2e70
cmpl $0x0, %eax
jne 0x3a02
movq 0x50e8(%rip), %rdi      # 0x8ad8
movb -0x1(%rbp), %al
andb $0x1, %al
movzbl %al, %esi
callq 0x2c20
jmp 0x3bf2
movq 0x50cf(%rip), %rsi      # 0x8ad8
leaq -0x54(%rbp), %rdi
movl $0x50, %edx
callq 0x2130
leaq -0x54(%rbp), %rdi
movl $0x1, %esi
callq 0x2b80
movb -0x1(%rbp), %al
leaq -0x54(%rbp), %rdi
andb $0x1, %al
movzbl %al, %esi
callq 0x2e70
cmpl $0x0, %eax
jne 0x3a65
movq 0x5096(%rip), %rdi      # 0x8ad8
movl $0x1, %esi
callq 0x2b80
movq 0x5085(%rip), %rdi      # 0x8ad8
movb -0x1(%rbp), %al
andb $0x1, %al
movzbl %al, %esi
callq 0x2c20
jmp 0x3bf2
movq 0x506c(%rip), %rsi      # 0x8ad8
leaq -0x54(%rbp), %rdi
movl $0x50, %edx
callq 0x2130
leaq -0x54(%rbp), %rdi
xorl %esi, %esi
callq 0x2b80
movb -0x1(%rbp), %al
leaq -0x54(%rbp), %rdi
andb $0x1, %al
movzbl %al, %esi
callq 0x2e70
cmpl $0x0, %eax
jne 0x3ac2
movq 0x5036(%rip), %rdi      # 0x8ad8
xorl %esi, %esi
callq 0x2b80
movq 0x5028(%rip), %rdi      # 0x8ad8
movb -0x1(%rbp), %al
andb $0x1, %al
movzbl %al, %esi
callq 0x2c20
jmp 0x3bf2
movq 0x500f(%rip), %rax      # 0x8ad8
cmpl $0x0, 0x4(%rax)
jne 0x3bf2
movq 0x4ffe(%rip), %rsi      # 0x8ad8
leaq -0x54(%rbp), %rdi
movl $0x50, %edx
callq 0x2130
movl $0x0, -0x58(%rbp)
cmpl $0x2, -0x58(%rbp)
jge 0x3b0e
leaq -0x54(%rbp), %rdi
movl $0x1, %esi
callq 0x2b80
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x3aef
movb -0x1(%rbp), %al
leaq -0x54(%rbp), %rdi
andb $0x1, %al
movzbl %al, %esi
callq 0x2e70
cmpl $0x0, %eax
jne 0x3b66
movl $0x0, -0x5c(%rbp)
cmpl $0x2, -0x5c(%rbp)
jge 0x3b4d
movq 0x4fa0(%rip), %rdi      # 0x8ad8
movl $0x1, %esi
callq 0x2b80
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x3b2b
movq 0x4f84(%rip), %rdi      # 0x8ad8
movb -0x1(%rbp), %al
andb $0x1, %al
movzbl %al, %esi
callq 0x2c20
jmp 0x3bf2
movq 0x4f6b(%rip), %rsi      # 0x8ad8
leaq -0x54(%rbp), %rdi
movl $0x50, %edx
callq 0x2130
movl $0x0, -0x60(%rbp)
cmpl $0x2, -0x60(%rbp)
jge 0x3b9e
leaq -0x54(%rbp), %rdi
xorl %esi, %esi
callq 0x2b80
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x3b82
movb -0x1(%rbp), %al
leaq -0x54(%rbp), %rdi
andb $0x1, %al
movzbl %al, %esi
callq 0x2e70
cmpl $0x0, %eax
jne 0x3bf0
movl $0x0, -0x64(%rbp)
cmpl $0x2, -0x64(%rbp)
jge 0x3bda
movq 0x4f10(%rip), %rdi      # 0x8ad8
xorl %esi, %esi
callq 0x2b80
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x3bbb
movq 0x4ef7(%rip), %rdi      # 0x8ad8
movb -0x1(%rbp), %al
andb $0x1, %al
movzbl %al, %esi
callq 0x2c20
jmp 0x3bf2
jmp 0x3bf2
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax,%rax)