// File: /workspace/github_c_makefile_star2_O3/skeeto[P]goblin-com/src/game.c
// Function: game_build
bool
game_build(game_t *game, uint16_t building, int x, int y)
{
    if (building == C_NONE) {
        /* Erase */
        if (game->map->high[x][y].building != C_NONE) {
            game->map->high[x][y].building = C_NONE;
            return true;
        }
    }
    if (x < 0 || x >= MAP_WIDTH || y < 0 || y >= MAP_HEIGHT ||
        game->map->high[x][y].building != C_NONE) {
        return false;
    }

    bool valid = false;
    if (x > 0 && game->map->high[x - 1][y].building != C_NONE)
        valid = true;
    else if (y > 0 && game->map->high[x][y - 1].building != C_NONE)
        valid = true;
    else if (x + 1 < MAP_WIDTH && game->map->high[x + 1][y].building != C_NONE)
        valid = true;
    else if (y + 1 < MAP_HEIGHT && game->map->high[x][y + 1].building != C_NONE)
        valid = true;
    if (valid) {
        valid = false;
        enum map_base base = game->map->high[x][y].base;
        switch (building) {
        case C_NONE:
        case C_CASTLE:
        case C_ROAD:
            valid = base != BASE_OCEAN && base != BASE_COAST;
            break;
        case C_LUMBERYARD:
            valid = base == BASE_FOREST;
            break;
        case C_STABLE:
            valid = base == BASE_GRASSLAND;
            game->max_hero += STABLE_INC;
            break;
        case C_HAMLET:
            valid =
                base == BASE_GRASSLAND ||
                base == BASE_FOREST ||
                base == BASE_HILL;
            if (valid)
                add_population(game, HAMLET_INC);
            break;
        case C_MINE:
            valid = base == BASE_HILL;
            break;
            break;
        case C_FARM:
            valid = base == BASE_GRASSLAND || base == BASE_FOREST;
            break;
        }
    }
    if (valid) {
        yield_t cost = building_cost(building);
        game->food -= cost.food;
        game->wood -= cost.wood;
        game->gold -= cost.gold;
        game->map->high[x][y].building = building;
        if (building == C_ROAD)
            game->map->high[x][y].building_age = 0;
        else
            game->map->high[x][y].building_age = INIT_BUILDING_AGE;
    }
    return valid;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq %rdi, %rbx
testl %esi, %esi
je 0x4fba
cmpl $0x3c, %edx
setae %al
cmpl $0x18, %ecx
setae %dil
orb %al, %dil
jne 0x4fb6
movq 0x38(%rbx), %r12
movl %edx, %edi
movl %ecx, %eax
leaq (%rdi,%rdi,2), %r14
shlq $0x7, %r14
leaq (%r12,%r14), %rdi
movq %rax, %r15
shlq $0x4, %r15
cmpw $0x0, 0x2(%r15,%rdi)
je 0x4fef
xorl %eax, %eax
jmp 0x4fe6
movslq %edx, %rax
leaq (%rax,%rax,2), %rax
shlq $0x7, %rax
addq 0x38(%rbx), %rax
movslq %ecx, %rdi
shlq $0x4, %rdi
cmpw $0x0, 0x2(%rdi,%rax)
je 0x4f80
addq %rdi, %rax
addq $0x2, %rax
movw $0x0, (%rax)
movb $0x1, %al
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
testl %edx, %edx
je 0x500b
leal -0x1(%rdx), %r8d
leaq (%r8,%r8,2), %r8
shlq $0x7, %r8
addq %r12, %r8
cmpw $0x0, 0x2(%r15,%r8)
jne 0x5054
testl %ecx, %ecx
je 0x5020
leal -0x1(%rcx), %r8d
shlq $0x4, %r8
cmpw $0x0, 0x2(%r8,%rdi)
jne 0x5054
cmpl $0x3a, %edx
ja 0x503b
incl %edx
leaq (%rdx,%rdx,2), %rdx
shlq $0x7, %rdx
addq %r12, %rdx
cmpw $0x0, 0x2(%r15,%rdx)
jne 0x5054
cmpl $0x16, %ecx
ja 0x4fb6
shlq $0x4, %rax
cmpw $0x0, 0x12(%rax,%rdi)
je 0x4fb6
movzwl (%r15,%rdi), %ecx
movzwl %si, %ebp
xorl %eax, %eax
cmpl $0x47, %ebp
jg 0x5076
cmpl $0x42, %ebp
jg 0x509a
testl %ebp, %ebp
je 0x50af
cmpl $0x2b, %ebp
je 0x50af
jmp 0x4fe6
cmpl $0x52, %ebp
jg 0x50c8
cmpl $0x48, %ebp
je 0x50e4
cmpl $0x4d, %ebp
jne 0x4fe6
cmpl $0x2229, %ecx           # imm = 0x2229
jne 0x4fb6
jmp 0x5120
cmpl $0x43, %ebp
je 0x50af
cmpl $0x46, %ebp
jne 0x4fe6
cmpl $0x2e, %ecx
jne 0x50d6
jmp 0x5120
xorl %eax, %eax
cmpl $0x20, %ecx
je 0x4fe6
cmpl $0x2248, %ecx           # imm = 0x2248
je 0x4fe6
jmp 0x5120
cmpl $0x53, %ebp
je 0x5110
cmpl $0x57, %ebp
jne 0x4fe6
cmpl $0x2663, %ecx           # imm = 0x2663
jne 0x4fb6
jmp 0x5120
cmpl $0x2e, %ecx
je 0x50fd
cmpl $0x2663, %ecx           # imm = 0x2663
je 0x50fd
cmpl $0x2229, %ecx           # imm = 0x2229
jne 0x4fb6
movl $0xc8, %esi
movq %rbx, %rdi
callq 0x5187
movq 0x38(%rbx), %r12
jmp 0x5120
addl $0x2, 0x3c8(%rbx)
cmpl $0x2e, %ecx
jne 0x4fb6
movl %ebp, %edi
callq 0x51ff
cvtsi2sd %eax, %xmm0
shrq $0x20, %rax
cvtsi2sd %eax, %xmm1
movsd 0x28(%rbx), %xmm2
subsd %xmm1, %xmm2
movsd %xmm2, 0x28(%rbx)
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
unpcklpd %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movupd 0x18(%rbx), %xmm1
subpd %xmm0, %xmm1
movupd %xmm1, 0x18(%rbx)
addq %r14, %r12
leaq (%r15,%r12), %rax
addq $0x8, %rax
movw %bp, -0x6(%rax)
cmpw $0x2b, %bp
jne 0x517b
movq $0x0, (%rax)
jmp 0x4fe4
movq $-0x15180, (%rax)       # imm = 0xFFFEAE80
jmp 0x4fe4