// File: /workspace/github_c_makefile_star2_O1/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
pushq %rax
movq %rdi, %r14
movl $0x28, %edi
movl $0x1, %esi
callq 0x2110
movq %rax, 0x15a34(%rip)     # 0x1f2a0
testq %rax, %rax
je 0x9b6f
movq $0x400, 0x20(%rax)      # imm = 0x400
leaq 0xb81b(%rip), %rsi      # 0x1509f
movq %r14, %rdi
callq 0x2230
testq %rax, %rax
je 0x9b34
movq %rax, %rbx
movq 0x15a01(%rip), %rdi     # 0x1f2a0
addq $0x20, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x2320
testq %rax, %rax
je 0x9b74
movq 0x159db(%rip), %rdi     # 0x1f2a0
addq $0x24, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x2320
testq %rax, %rax
je 0x9b79
movq 0x159b5(%rip), %rdi     # 0x1f2a0
addq $0x1c, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x2320
testq %rax, %rax
je 0x9b7e
movq 0x1598f(%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 0x9b83
movq 0x15962(%rip), %rax     # 0x1f2a0
movq (%rax), %rdi
movl $0x4, %esi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x2320
cmpq %r14, %rax
jne 0x9b88
leaq (,%r14,8), %rdi
callq 0x22b0
movq 0x15932(%rip), %rcx     # 0x1f2a0
movq %rax, 0x8(%rcx)
testq %rax, %rax
je 0x9b8d
movl $0x8, %esi
movq %rax, %rdi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x2320
cmpq %r14, %rax
jne 0x9b92
movq 0x15902(%rip), %rdi     # 0x1f2a0
addq $0x18, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x2320
testq %rax, %rax
je 0x9b97
movq 0x158dc(%rip), %r14     # 0x1f2a0
movslq 0x18(%r14), %rax
shlq $0x4, %rax
leaq (%rax,%rax,2), %rdi
callq 0x22b0
movq %rax, 0x10(%r14)
testq %rax, %rax
je 0x9b9c
movq 0x158b7(%rip), %rax     # 0x1f2a0
cmpl $0x0, 0x18(%rax)
jle 0x9b12
xorl %r12d, %r12d
movq %rsp, %r14
leaq (%r12,%r12,2), %r13
shlq $0x4, %r13
movq 0x10(%rax), %rdi
addq %r13, %rdi
movl $0x4, %esi
movl $0x8, %edx
movq %rbx, %rcx
callq 0x2320
cmpq $0x8, %rax
jne 0x9b6a
movq 0x15875(%rip), %rax     # 0x1f2a0
movq 0x10(%rax), %rax
leaq (%rax,%r13), %rdi
addq $0x28, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x2320
testq %rax, %rax
je 0x9b60
movq 0x15847(%rip), %rax     # 0x1f2a0
movq 0x10(%rax), %rbp
movslq 0x28(%rbp,%r13), %rdi
shlq $0x3, %rdi
callq 0x22b0
movq %rax, 0x20(%rbp,%r13)
testq %rax, %rax
je 0x9b65
movq 0x15820(%rip), %rax     # 0x1f2a0
movq 0x10(%rax), %rax
cmpl $0x0, 0x28(%rax,%r13)
jle 0x9afb
movq %r12, %rax
shlq $0x4, %rax
leaq (%rax,%rax,2), %r13
xorl %ebp, %ebp
movl $0x8, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x2320
testq %rax, %rax
je 0x9b5b
movq 0x157e2(%rip), %rax     # 0x1f2a0
movslq 0x4(%rsp), %rcx
imulq %r15, %rcx
movslq (%rsp), %rdx
addq %rcx, %rdx
shlq $0x3, %rdx
addq 0x8(%rax), %rdx
movq 0x10(%rax), %rax
movq 0x20(%rax,%r13), %rax
movq %rdx, (%rax,%rbp,8)
incq %rbp
movq 0x157b3(%rip), %rax     # 0x1f2a0
movq 0x10(%rax), %rax
movslq 0x28(%rax,%r13), %rax
cmpq %rax, %rbp
jl 0x9a99
incq %r12
movq 0x1579b(%rip), %rax     # 0x1f2a0
movslq 0x18(%rax), %rcx
cmpq %rcx, %r12
jl 0x99f9
movq %rbx, %rdi
callq 0x21f0
leaq 0xc823(%rip), %rdi      # 0x16344
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2330
leaq 0xc7dd(%rip), %rdi      # 0x16318
callq 0x2330
callq 0x9ba1
movq %r14, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa4f7
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