// File: miklhh[P]tetris/src/game.cpp
// Function: block_spawn(int, int, int, int)
block_t* block_spawn(block_type_t block_type, int start_x, int start_y, color_t color)
{
    /* Decide what to spawn. */
    block_type_t block_to_spawn;
    if (block_type == BLOCK_RANDOM)
        block_to_spawn = (block_type_t)random_int(BLOCK_RANDOM);
    else
        block_to_spawn = block_type;


    /* Spawn the block. */
    block_t* block      = new block_t;
    block->block_type   = block_to_spawn;
    switch (block_to_spawn)
    {
    case BLOCK_I:
        block->structure = block_ref_i.structure;
        break;

    case BLOCK_J:
        block->structure = block_ref_j.structure;
        break;

    case BLOCK_L:
        block->structure = block_ref_l.structure;
        break;

    case BLOCK_O:
        block->structure = block_ref_o.structure;
        break;

    case BLOCK_Z:
        block->structure = block_ref_z.structure;
        break;

    case BLOCK_T:
        block->structure = block_ref_t.structure;
        break;

    case BLOCK_S:
        block->structure = block_ref_s.structure;
        break;
    }

    /* Set the block position, color and return the block-pointer. */
    if (color == COLOR_RANDOM)
    {
        block->color = random_int(COLOR_RANDOM);
    }
    else
    {
        block->color = color;
    }
    block->position.x = start_x;
    block->position.y = start_y;
   
    return block;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movl %edi, -0x4(%rbp)
movl %esi, -0x8(%rbp)
movl %edx, -0xc(%rbp)
movl %ecx, -0x10(%rbp)
cmpl $0x7, -0x4(%rbp)
jne 0x29e9
movl $0x7, %edi
callq 0x2860
movl %eax, -0x14(%rbp)
jmp 0x29ef
movl -0x4(%rbp), %eax
movl %eax, -0x14(%rbp)
movl $0x50, %edi
callq 0x21b0
movq %rax, -0x20(%rbp)
movl -0x14(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x4(%rax)
movl -0x14(%rbp), %eax
movq %rax, -0x28(%rbp)
subq $0x6, %rax
ja 0x2b09
movq -0x28(%rbp), %rax
leaq 0x2629(%rip), %rcx      # 0x504c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x20(%rbp), %rdi
addq $0x10, %rdi
leaq 0x2669(%rip), %rsi      # 0x50a4
addq $0x10, %rsi
movl $0x40, %edx
callq 0x2130
jmp 0x2b09
movq -0x20(%rbp), %rdi
addq $0x10, %rdi
leaq 0x2697(%rip), %rsi      # 0x50f4
addq $0x10, %rsi
movl $0x40, %edx
callq 0x2130
jmp 0x2b09
movq -0x20(%rbp), %rdi
addq $0x10, %rdi
leaq 0x26c5(%rip), %rsi      # 0x5144
addq $0x10, %rsi
movl $0x40, %edx
callq 0x2130
jmp 0x2b09
movq -0x20(%rbp), %rdi
addq $0x10, %rdi
leaq 0x26f6(%rip), %rsi      # 0x5194
addq $0x10, %rsi
movl $0x40, %edx
callq 0x2130
jmp 0x2b09
movq -0x20(%rbp), %rdi
addq $0x10, %rdi
leaq 0x2727(%rip), %rsi      # 0x51e4
addq $0x10, %rsi
movl $0x40, %edx
callq 0x2130
jmp 0x2b09
movq -0x20(%rbp), %rdi
addq $0x10, %rdi
leaq 0x2758(%rip), %rsi      # 0x5234
addq $0x10, %rsi
movl $0x40, %edx
callq 0x2130
jmp 0x2b09
movq -0x20(%rbp), %rdi
addq $0x10, %rdi
leaq 0x2789(%rip), %rsi      # 0x5284
addq $0x10, %rsi
movl $0x40, %edx
callq 0x2130
cmpl $0x3, -0x10(%rbp)
jne 0x2b23
movl $0x3, %edi
callq 0x2860
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
jmp 0x2b2c
movl -0x10(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl -0x8(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x8(%rax)
movl -0xc(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x20(%rbp), %rax
addq $0x30, %rsp
popq %rbp
retq
nopw (%rax,%rax)