// File: /workspace/github_c_makefile_star2_O1/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 0x49cb
cmpl $0x3c, %edx
setae %al
cmpl $0x18, %ecx
setae %dil
orb %al, %dil
jne 0x49c6
movq 0x38(%rbx), %r8
movl %edx, %eax
movl %ecx, %edi
leaq (%rax,%rax,2), %r15
shlq $0x7, %r15
leaq (%r8,%r15), %rax
movq %rdi, %r12
shlq $0x4, %r12
cmpw $0x0, 0x2(%r12,%rax)
je 0x4a04
xorl %r14d, %r14d
jmp 0x49f8
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 0x4990
addq %rdi, %rax
addq $0x2, %rax
movw $0x0, (%rax)
movb $0x1, %r14b
movl %r14d, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
testl %edx, %edx
jle 0x4a23
leal -0x1(%rdx), %r9d
leaq (%r9,%r9,2), %r9
shlq $0x7, %r9
addq %r8, %r9
movb $0x1, %r14b
cmpw $0x0, 0x2(%r12,%r9)
jne 0x4a71
testl %ecx, %ecx
jle 0x4a3b
leal -0x1(%rcx), %r9d
shlq $0x4, %r9
movb $0x1, %r14b
cmpw $0x0, 0x2(%r9,%rax)
jne 0x4a71
cmpl $0x3a, %edx
jg 0x4a59
incl %edx
leaq (%rdx,%rdx,2), %rdx
shlq $0x7, %rdx
addq %rdx, %r8
movb $0x1, %r14b
cmpw $0x0, 0x2(%r12,%r8)
jne 0x4a71
cmpl $0x16, %ecx
jg 0x4a6e
shlq $0x4, %rdi
cmpw $0x0, 0x12(%rdi,%rax)
setne %r14b
jmp 0x4a71
xorl %r14d, %r14d
movzwl %si, %ebp
testb %r14b, %r14b
je 0x4b39
movzwl (%r12,%rax), %eax
xorl %r14d, %r14d
cmpl $0x47, %ebp
jg 0x4a9d
cmpl $0x42, %ebp
jg 0x4ab7
testl %ebp, %ebp
je 0x4ad5
cmpl $0x2b, %ebp
je 0x4ad5
jmp 0x4b39
cmpl $0x52, %ebp
jg 0x4ae9
cmpl $0x48, %ebp
je 0x4afe
cmpl $0x4d, %ebp
jne 0x4b39
cmpl $0x2229, %eax           # imm = 0x2229
jmp 0x4af8
cmpl $0x43, %ebp
je 0x4ad5
cmpl $0x46, %ebp
jne 0x4b39
cmpl $0x2e, %eax
sete %cl
cmpl $0x2663, %eax           # imm = 0x2663
sete %r14b
orb %cl, %r14b
jmp 0x4b39
cmpl $0x20, %eax
setne %cl
cmpl $0x2248, %eax           # imm = 0x2248
setne %r14b
andb %cl, %r14b
jmp 0x4b39
cmpl $0x53, %ebp
je 0x4b2b
cmpl $0x57, %ebp
jne 0x4b39
cmpl $0x2663, %eax           # imm = 0x2663
sete %r14b
jmp 0x4b39
movb $0x1, %r14b
cmpl $0x2e, %eax
je 0x4b17
cmpl $0x2229, %eax           # imm = 0x2229
je 0x4b17
cmpl $0x2663, %eax           # imm = 0x2663
je 0x4b17
xorl %r14d, %r14d
testb %r14b, %r14b
je 0x4b39
movl $0xc8, %esi
movq %rbx, %rdi
callq 0x4bb4
jmp 0x4b39
cmpl $0x2e, %eax
sete %r14b
addl $0x2, 0x3c8(%rbx)
testb %r14b, %r14b
je 0x49f8
movl %ebp, %edi
callq 0x4c2c
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
movsd 0x20(%rbx), %xmm2
subsd %xmm1, %xmm2
movsd 0x18(%rbx), %xmm1
movsd %xmm2, 0x20(%rbx)
subsd %xmm0, %xmm1
movsd %xmm1, 0x18(%rbx)
addq 0x38(%rbx), %r15
leaq (%r12,%r15), %rax
addq $0x8, %rax
movw %bp, -0x6(%rax)
cmpw $0x2b, %bp
jne 0x4ba8
movq $0x0, (%rax)
jmp 0x49f5
movq $-0x15180, (%rax)       # imm = 0xFFFEAE80
jmp 0x49f5