// File: /workspace/github_c_makefile_star2_O1/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 0x14d96(%rip), %rax     # 0x1f2a0
testq %rax, %rax
je 0xa768
movl 0x24(%rax), %eax
andl $0x3, %eax
cmpl $0x1, %eax
jne 0xa734
leaq 0xb169(%rip), %rsi      # 0x15692
callq 0x2230
testq %rax, %rax
je 0xa741
movq %rax, %rbx
movq 0x14d5f(%rip), %rdi     # 0x1f2a0
addq $0x20, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x2480
testq %rax, %rax
je 0xa787
movq 0x14d39(%rip), %rdi     # 0x1f2a0
addq $0x24, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x2480
testq %rax, %rax
je 0xa78c
movq 0x14d13(%rip), %rdi     # 0x1f2a0
addq $0x1c, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x2480
testq %rax, %rax
je 0xa791
movq 0x14ced(%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 0xa796
movq 0x14cc2(%rip), %rax     # 0x1f2a0
movq 0x8(%rax), %rdi
movl $0x8, %esi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x2480
cmpq %r14, %rax
jne 0xa79b
movq 0x14c9e(%rip), %rdi     # 0x1f2a0
addq $0x18, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x2480
testq %rax, %rax
je 0xa7a0
movq 0x14c78(%rip), %rax     # 0x1f2a0
cmpl $0x0, 0x18(%rax)
jle 0xa714
xorl %r15d, %r15d
leaq 0x8(%rsp), %r14
leaq (%r15,%r15,2), %r12
shlq $0x4, %r12
movq 0x10(%rax), %rdi
addq %r12, %rdi
movl $0x4, %esi
movl $0x8, %edx
movq %rbx, %rcx
callq 0x2480
cmpq $0x8, %rax
jne 0xa763
movq 0x14c34(%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 0xa75e
movq 0x14c06(%rip), %rcx     # 0x1f2a0
movq 0x10(%rcx), %rax
cmpl $0x0, 0x28(%rax,%r12)
jle 0xa6fd
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 0xa759
incl %ebp
movq 0x14bae(%rip), %rcx     # 0x1f2a0
movq 0x10(%rcx), %rax
cmpl 0x28(%rax,%r12), %ebp
jl 0xa6a8
incq %r15
movq 0x14b99(%rip), %rax     # 0x1f2a0
movslq 0x18(%rax), %rcx
cmpq %rcx, %r15
jl 0xa63a
leaq 0xbc47(%rip), %rdi      # 0x16362
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 0xaf4d(%rip), %rdi      # 0x15695
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x2190
callq 0x2c33
callq 0x2c48
callq 0x2c1e
leaq 0xaeed(%rip), %rdi      # 0x1565c
leaq 0xaef1(%rip), %rsi      # 0x15667
leaq 0xaef4(%rip), %rcx      # 0x15671
movl $0x84, %edx
callq 0x2100
callq 0x2c9c
callq 0x2c87
callq 0x2c72
callq 0x2bf4
callq 0x2c09
callq 0x2c5d