// File: /workspace/github_c_makefile_star2_O2/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 0x4234
cmpl $0x3c, %edx
setae %al
cmpl $0x18, %ecx
setae %dil
orb %al, %dil
jne 0x4230
movq 0x38(%rbx), %r12
movl %edx, %eax
movl %ecx, %edi
imulq $0x180, %rax, %r14      # imm = 0x180
leaq (%r12,%r14), %rax
movq %rdi, %r15
shlq $0x4, %r15
cmpw $0x0, 0x2(%r15,%rax)
je 0x4267
xorl %eax, %eax
jmp 0x425e
movslq %edx, %rax
imulq $0x180, %rax, %rax      # imm = 0x180
addq 0x38(%rbx), %rax
movslq %ecx, %rdi
shlq $0x4, %rdi
cmpw $0x0, 0x2(%rdi,%rax)
je 0x41fb
addq %rdi, %rax
addq $0x2, %rax
andw $0x0, (%rax)
movb $0x1, %al
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
testl %edx, %edx
je 0x4282
leal -0x1(%rdx), %r8d
imulq $0x180, %r8, %r8        # imm = 0x180
addq %r12, %r8
cmpw $0x0, 0x2(%r15,%r8)
jne 0x42ca
testl %ecx, %ecx
je 0x4297
leal -0x1(%rcx), %r8d
shlq $0x4, %r8
cmpw $0x0, 0x2(%r8,%rax)
jne 0x42ca
cmpl $0x3a, %edx
ja 0x42b1
incl %edx
imulq $0x180, %rdx, %rdx      # imm = 0x180
addq %r12, %rdx
cmpw $0x0, 0x2(%r15,%rdx)
jne 0x42ca
cmpl $0x16, %ecx
ja 0x4230
shlq $0x4, %rdi
cmpw $0x0, 0x12(%rdi,%rax)
je 0x4230
movzwl (%r15,%rax), %ecx
movzwl %si, %ebp
testw %bp, %bp
je 0x4300
cmpl $0x2b, %ebp
je 0x4300
cmpl $0x43, %ebp
je 0x4300
cmpl $0x46, %ebp
je 0x4365
cmpl $0x48, %ebp
je 0x4319
cmpl $0x4d, %ebp
je 0x4357
cmpl $0x53, %ebp
je 0x4345
cmpl $0x57, %ebp
jne 0x4230
jmp 0x436a
xorl %eax, %eax
cmpl $0x20, %ecx
je 0x425e
cmpl $0x2248, %ecx           # imm = 0x2248
je 0x425e
jmp 0x4376
cmpl $0x2e, %ecx
je 0x4332
cmpl $0x2663, %ecx           # imm = 0x2663
je 0x4332
cmpl $0x2229, %ecx           # imm = 0x2229
jne 0x4230
movl $0xc8, %esi
movq %rbx, %rdi
callq 0x43d7
movq 0x38(%rbx), %r12
jmp 0x4376
addl $0x2, 0x3c8(%rbx)
cmpl $0x2e, %ecx
jne 0x4230
jmp 0x4376
cmpl $0x2229, %ecx           # imm = 0x2229
jne 0x4230
jmp 0x4376
cmpl $0x2e, %ecx
je 0x4376
cmpl $0x2663, %ecx           # imm = 0x2663
jne 0x4230
movl %ebp, %edi
callq 0x442d
cvtsi2sd %eax, %xmm0
shrq $0x20, %rax
cvtsi2sd %eax, %xmm1
movsd 0x28(%rbx), %xmm2
subsd %xmm1, %xmm2
movsd %xmm2, 0x28(%rbx)
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 0x43cb
andq $0x0, (%rax)
jmp 0x425c
movq $-0x15180, (%rax)       # imm = 0xFFFEAE80
jmp 0x425c