// File: /workspace/github_c_makefile_star2_O3/Ziddykins[P]Dawn/src/map.c
// Function: save_map
void save_map(char const * const fn) {
    assert(global_map);
    if(global_map->flags & MAP_PRESENT && !(global_map->flags & MAP_SAVED)) {
        FILE *file = fopen(fn, "wb");
        if(!file) {
            PRINTWARN("Could not save heightmap")
            return;
        }
        CALLEXIT(!(fwrite(&global_map->dim, sizeof global_map->dim, 1, file)))
        CALLEXIT(!(fwrite(&global_map->flags, sizeof global_map->flags, 1, file)))
        CALLEXIT(!(fwrite(&global_map->water_level, sizeof global_map->water_level, 1, file)))

        int dim = global_map->dim;

        size_t nmemb = (size_t) (dim * dim);
        CALLEXIT(fwrite(global_map->heightmap, sizeof *global_map->heightmap, nmemb, file) != nmemb)

        nmemb = (size_t) (dim * dim);
        CALLEXIT(fwrite(global_map->m_ent, sizeof *global_map->m_ent, nmemb, file) != nmemb)

        CALLEXIT(!(fwrite(&global_map->town_count, sizeof global_map->town_count, 1, file)))
        for (int i = 0; i < global_map->town_count; i++) {
            CALLEXIT(fwrite(global_map->towns[i].matdistr, sizeof *global_map->towns[i].matdistr, MAT_COUNT, file) !=
                     MAT_COUNT)
            CALLEXIT(!(fwrite(&(global_map->towns[i].entitiy_count), sizeof global_map->towns[i].entitiy_count, 1,
                              file)))
            for (int j = 0; j < global_map->towns[i].entitiy_count; j++) {
                struct Location e_pos = entity_location(global_map->towns[i].t_ent[0]);
                CALLEXIT(!(fwrite(&e_pos, sizeof e_pos, 1, file)))
            }
        }

        printf(INFO "Saved map\n");
        fclose(file);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x10, %rsp
movq 0x14d72(%rip), %rax     # 0x1f2a0
testq %rax, %rax
je 0xa785
movl 0x24(%rax), %eax
andl $0x3, %eax
cmpl $0x1, %eax
jne 0xa751
leaq 0xb195(%rip), %rsi      # 0x156e2
callq 0x2230
testq %rax, %rax
je 0xa75e
movq %rax, %rbx
movq 0x14d3b(%rip), %rdi     # 0x1f2a0
addq $0x20, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x2480
testq %rax, %rax
je 0xa7a4
movq 0x14d15(%rip), %rdi     # 0x1f2a0
addq $0x24, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x2480
testq %rax, %rax
je 0xa7a9
movq 0x14cef(%rip), %rdi     # 0x1f2a0
addq $0x1c, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x2480
testq %rax, %rax
je 0xa7ae
movq 0x14cc9(%rip), %rax     # 0x1f2a0
movl 0x20(%rax), %r14d
imull %r14d, %r14d
movq (%rax), %rdi
movl $0x4, %esi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x2480
cmpq %r14, %rax
jne 0xa7b3
movq 0x14c9e(%rip), %rax     # 0x1f2a0
movq 0x8(%rax), %rdi
movl $0x8, %esi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x2480
cmpq %r14, %rax
jne 0xa7b8
movq 0x14c7a(%rip), %rdi     # 0x1f2a0
addq $0x18, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x2480
testq %rax, %rax
je 0xa7bd
movq 0x14c54(%rip), %rcx     # 0x1f2a0
cmpl $0x0, 0x18(%rcx)
jle 0xa731
xorl %r15d, %r15d
leaq 0x8(%rsp), %r14
leaq (%r15,%r15,2), %r12
shlq $0x4, %r12
movq 0x10(%rcx), %rdi
addq %r12, %rdi
movl $0x4, %esi
movl $0x8, %edx
movq %rbx, %rcx
callq 0x2480
cmpq $0x8, %rax
jne 0xa780
movq 0x14c10(%rip), %rax     # 0x1f2a0
movq 0x10(%rax), %rax
leaq (%rax,%r12), %rdi
addq $0x28, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x2480
testq %rax, %rax
je 0xa77b
movq 0x14be2(%rip), %rcx     # 0x1f2a0
movq 0x10(%rcx), %rax
cmpl $0x0, 0x28(%rax,%r12)
jle 0xa721
xorl %ebp, %ebp
movq 0x20(%rax,%r12), %rax
movq (%rax), %rax
movslq 0x20(%rcx), %rsi
subq 0x8(%rcx), %rax
sarq $0x3, %rax
cqto
idivq %rsi
shlq $0x20, %rax
movl %edx, %ecx
orq %rax, %rcx
movq %rcx, 0x8(%rsp)
movl $0x8, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x2480
testq %rax, %rax
je 0xa776
incl %ebp
movq 0x14b8a(%rip), %rcx     # 0x1f2a0
movq 0x10(%rcx), %rax
cmpl 0x28(%rax,%r12), %ebp
jl 0xa6cc
incq %r15
movslq 0x18(%rcx), %rax
cmpq %rax, %r15
jl 0xa65e
leaq 0xbc7a(%rip), %rdi      # 0x163b2
callq 0x2330
movq %rbx, %rdi
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x21f0
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0xaf80(%rip), %rdi      # 0x156e5
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x2190
callq 0x2c33
callq 0x2c48
callq 0x2c1e
leaq 0xaf20(%rip), %rdi      # 0x156ac
leaq 0xaf24(%rip), %rsi      # 0x156b7
leaq 0xaf27(%rip), %rcx      # 0x156c1
movl $0x84, %edx
callq 0x2100
callq 0x2c9c
callq 0x2c87
callq 0x2c72
callq 0x2bf4
callq 0x2c09
callq 0x2c5d