// File: /workspace/github_c_makefile_star2_O3/Ziddykins[P]Dawn/src/map.c
// Function: init_map
void init_map(char const * const fn) {
    CALLEXIT(!(global_map = calloc(sizeof *global_map, 1)))
    global_map->dim = 1<<10;
    global_map->flags = 0;

    FILE *file = fopen(fn, "rb");
    if (!file) {
        printf(INFO "Generating new heightmap\n");
        generate_map();
        save_map(fn);
    } else {
        CALLEXIT(!(fread(&global_map->dim, sizeof global_map->dim, 1, file)))
        CALLEXIT(!(fread(&global_map->flags, sizeof global_map->flags, 1, file)))
        CALLEXIT(!(fread(&global_map->water_level, sizeof global_map->water_level, 1, file)))

        int dim = global_map->dim;

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

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

        CALLEXIT(!(fread(&global_map->town_count, sizeof global_map->town_count, 1, file)))
        CALLEXIT(!(global_map->towns = malloc((size_t) (global_map->town_count) * sizeof *global_map->towns)))

        struct Location e_pos;

        for (int i = 0; i < global_map->town_count; i++) {
            CALLEXIT(fread(global_map->towns[i].matdistr, sizeof *global_map->towns[i].matdistr, MAT_COUNT, file) !=
                     MAT_COUNT)
            CALLEXIT(
                    !(fread(&(global_map->towns[i].entitiy_count), sizeof global_map->towns[i].entitiy_count, 1, file)))
            CALLEXIT(!(global_map->towns[i].t_ent = malloc(
                    (size_t) (global_map->towns[i].entitiy_count) * sizeof *global_map->towns[i].t_ent)))
            for (int j = 0; j < global_map->towns[i].entitiy_count; j++) {
                CALLEXIT(!(fread(&e_pos, sizeof e_pos, 1, file)))
                global_map->towns[i].t_ent[j] = &global_map->m_ent[e_pos.y * dim + e_pos.x];
            }
        }

        fclose(file);
        printf(INFO "Map loaded\n");
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %r14
movl $0x28, %edi
movl $0x1, %esi
callq 0x2110
movq %rax, 0x159a1(%rip)     # 0x1f2a0
testq %rax, %rax
je 0x9c04
movl $0x400, 0x20(%rax)      # imm = 0x400
leaq 0xb7d9(%rip), %rsi      # 0x150ef
movq %r14, %rdi
callq 0x2230
testq %rax, %rax
je 0x9bc9
movq %rax, %rbx
movq 0x1596f(%rip), %rdi     # 0x1f2a0
addq $0x20, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x2320
testq %rax, %rax
je 0x9c09
movq 0x15949(%rip), %rdi     # 0x1f2a0
addq $0x24, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x2320
testq %rax, %rax
je 0x9c0e
movq 0x15923(%rip), %rdi     # 0x1f2a0
addq $0x1c, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x2320
testq %rax, %rax
je 0x9c13
movq 0x158fd(%rip), %r12     # 0x1f2a0
movslq 0x20(%r12), %r15
movl %r15d, %r14d
imull %r14d, %r14d
leaq (,%r14,4), %rdi
callq 0x22b0
movq %rax, (%r12)
testq %rax, %rax
je 0x9c18
movq 0x158d0(%rip), %rax     # 0x1f2a0
movq (%rax), %rdi
movl $0x4, %esi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x2320
cmpq %r14, %rax
jne 0x9c1d
leaq (,%r14,8), %rdi
callq 0x22b0
movq 0x158a0(%rip), %rcx     # 0x1f2a0
movq %rax, 0x8(%rcx)
testq %rax, %rax
je 0x9c22
movl $0x8, %esi
movq %rax, %rdi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x2320
cmpq %r14, %rax
jne 0x9c27
movq 0x15870(%rip), %rdi     # 0x1f2a0
addq $0x18, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x2320
testq %rax, %rax
je 0x9c2c
movq 0x1584a(%rip), %r13     # 0x1f2a0
movslq 0x18(%r13), %r14
movq %r14, %rax
shlq $0x4, %rax
leaq (%rax,%rax,2), %rdi
callq 0x22b0
movq %rax, 0x10(%r13)
testq %rax, %rax
je 0x9c31
testl %r14d, %r14d
jle 0x9ba7
xorl %r12d, %r12d
leaq 0x10(%rsp), %r14
leaq (%r12,%r12,2), %rbp
shlq $0x4, %rbp
movq 0x10(%r13), %rdi
addq %rbp, %rdi
movl $0x4, %esi
movl $0x8, %edx
movq %rbx, %rcx
callq 0x2320
cmpq $0x8, %rax
jne 0x9bff
movq %r12, 0x8(%rsp)
movq 0x157e1(%rip), %rax     # 0x1f2a0
movq 0x10(%rax), %rax
leaq (%rax,%rbp), %rdi
addq $0x28, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x2320
testq %rax, %rax
je 0x9bf5
movq 0x157b3(%rip), %rax     # 0x1f2a0
movq 0x10(%rax), %r12
movslq 0x28(%r12,%rbp), %rdi
shlq $0x3, %rdi
callq 0x22b0
movq %rax, 0x20(%r12,%rbp)
testq %rax, %rax
je 0x9bfa
movq 0x1578c(%rip), %r13     # 0x1f2a0
movq 0x10(%r13), %rax
cmpl $0x0, 0x28(%rax,%rbp)
jle 0x9b92
movq 0x8(%rsp), %rax
shlq $0x4, %rax
leaq (%rax,%rax,2), %rbp
xorl %r12d, %r12d
movl $0x8, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x2320
testq %rax, %rax
je 0x9bf0
movq 0x1574c(%rip), %rax     # 0x1f2a0
movslq 0x14(%rsp), %rcx
imulq %r15, %rcx
movslq 0x10(%rsp), %rdx
addq %rcx, %rdx
shlq $0x3, %rdx
addq 0x8(%rax), %rdx
movq 0x10(%rax), %rax
movq 0x20(%rax,%rbp), %rax
movq %rdx, (%rax,%r12,8)
incq %r12
movq 0x1571c(%rip), %r13     # 0x1f2a0
movq 0x10(%r13), %rax
movslq 0x28(%rax,%rbp), %rax
cmpq %rax, %r12
jl 0x9b2f
movq 0x8(%rsp), %r12
incq %r12
movslq 0x18(%r13), %rax
cmpq %rax, %r12
jl 0x9a88
movq %rbx, %rdi
callq 0x21f0
leaq 0xc7de(%rip), %rdi      # 0x16394
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2330
leaq 0xc798(%rip), %rdi      # 0x16368
callq 0x2330
callq 0x9c36
movq %r14, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa51b
callq 0x2a26
callq 0x2a50
callq 0x2a3b
callq 0x2a11
callq 0x2af8
callq 0x2ae3
callq 0x2ace
callq 0x2ab9
callq 0x2aa4
callq 0x29e7
callq 0x2a8f
callq 0x29fc
callq 0x2a7a
callq 0x2a65