// File: /workspace/github_c_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq 0x13c6a(%rip), %rax     # 0x1d2a0
testq %rax, %rax
je 0x9872
movl 0x24(%rax), %eax
andl $0x3, %eax
cmpl $0x1, %eax
jne 0x9801
leaq 0xb035(%rip), %rsi      # 0x1468a
callq 0x2240
testq %rax, %rax
je 0x9832
movq %rax, %rbx
movq 0x13c33(%rip), %rdi     # 0x1d2a0
addq $0x20, %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
callq 0x2480
testq %rax, %rax
je 0x9891
movq 0x13c11(%rip), %rdi     # 0x1d2a0
addq $0x24, %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x2480
testq %rax, %rax
je 0x989a
movq 0x13bef(%rip), %rdi     # 0x1d2a0
addq $0x1c, %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x2480
testq %rax, %rax
je 0x98a3
movq 0x13bcd(%rip), %rax     # 0x1d2a0
movl 0x20(%rax), %r14d
imull %r14d, %r14d
movq (%rax), %rdi
pushq $0x4
popq %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x2480
cmpq %r14, %rax
jne 0x98ac
movq 0x13ba4(%rip), %rax     # 0x1d2a0
movq 0x8(%rax), %rdi
pushq $0x8
popq %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x2480
cmpq %r14, %rax
jne 0x98b5
movq 0x13b82(%rip), %rdi     # 0x1d2a0
addq $0x18, %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x2480
testq %rax, %rax
je 0x98be
movq 0x13b60(%rip), %rax     # 0x1d2a0
xorl %edx, %edx
pushq $0x4
popq %rbp
pushq $0x8
popq %r15
pushq $0x1
popq %r12
leaq 0x10(%rsp), %r13
movslq 0x18(%rax), %rcx
cmpq %rcx, %rdx
jge 0x9810
movq %rdx, 0x8(%rsp)
imulq $0x30, %rdx, %r14
movq 0x10(%rax), %rdi
addq %r14, %rdi
movq %rbp, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x2480
cmpq $0x8, %rax
jne 0x9860
movq 0x13b12(%rip), %rax     # 0x1d2a0
movq 0x10(%rax), %rax
leaq (%rax,%r14), %rdi
addq $0x28, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x2480
testq %rax, %rax
je 0x9869
xorl %ebp, %ebp
movq 0x13ae6(%rip), %rax     # 0x1d2a0
movq 0x10(%rax), %rcx
cmpl 0x28(%rcx,%r14), %ebp
jge 0x97f1
movq 0x20(%rcx,%r14), %rax
movq (%rax), %rdi
callq 0x98c7
movq %rax, 0x10(%rsp)
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x2480
testq %rax, %rax
je 0x984c
incl %ebp
jmp 0x97b3
movq 0x8(%rsp), %rdx
incq %rdx
pushq $0x4
popq %rbp
jmp 0x9752
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xbb43(%rip), %rdi      # 0x1535a
callq 0x2340
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21f0
leaq 0xae54(%rip), %rdi      # 0x1468d
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2190
leaq 0xb209(%rip), %rdi      # 0x14a5c
callq 0x2190
pushq $0x1
popq %rdi
callq 0x2270
leaq 0xb0e1(%rip), %rdi      # 0x14948
jmp 0x9853
leaq 0xb165(%rip), %rdi      # 0x149d5
jmp 0x9853
leaq 0xaddb(%rip), %rdi      # 0x14654
leaq 0xaddf(%rip), %rsi      # 0x1465f
leaq 0xade2(%rip), %rcx      # 0x14669
movl $0x84, %edx
callq 0x2100
leaq 0xae30(%rip), %rdi      # 0x146c8
jmp 0x9853
leaq 0xae86(%rip), %rdi      # 0x14727
jmp 0x9853
leaq 0xaee0(%rip), %rdi      # 0x1478a
jmp 0x9853
leaq 0xaf46(%rip), %rdi      # 0x147f9
jmp 0x9853
leaq 0xafb2(%rip), %rdi      # 0x1486e
jmp 0x9853
leaq 0xb016(%rip), %rdi      # 0x148db
jmp 0x9853