// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x28
popq %rdi
pushq $0x1
popq %rsi
callq 0x2110
movq %rax, 0x148dd(%rip)     # 0x1d2a0
testq %rax, %rax
je 0x8caa
movl $0x400, 0x20(%rax)      # imm = 0x400
leaq 0xb6bd(%rip), %rsi      # 0x14097
movq %r14, %rdi
callq 0x2240
testq %rax, %rax
je 0x8c54
movq %rax, %rbx
movq 0x148ab(%rip), %rdi     # 0x1d2a0
addq $0x20, %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
callq 0x2330
testq %rax, %rax
je 0x8cb3
movq 0x14889(%rip), %rdi     # 0x1d2a0
addq $0x24, %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x2330
testq %rax, %rax
je 0x8cbc
movq 0x14867(%rip), %rdi     # 0x1d2a0
addq $0x1c, %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %r13
movq %rbx, %rcx
callq 0x2330
testq %rax, %rax
je 0x8cc5
movq 0x14842(%rip), %rbx     # 0x1d2a0
movslq 0x20(%rbx), %rbp
movl %ebp, %r14d
imull %r14d, %r14d
leaq (,%r14,4), %rdi
callq 0x22c0
movq %rax, (%rbx)
testq %rax, %rax
je 0x8cce
movq 0x14817(%rip), %rax     # 0x1d2a0
movq (%rax), %rdi
pushq $0x4
popq %rsi
movq %r14, %rdx
movq %r13, %rbx
movq %r13, %rcx
callq 0x2330
cmpq %r14, %rax
jne 0x8cd7
leaq (,%r14,8), %rdi
callq 0x22c0
movq 0x147e6(%rip), %rcx     # 0x1d2a0
movq %rax, 0x8(%rcx)
testq %rax, %rax
je 0x8ce0
pushq $0x8
popq %rsi
movq %rax, %rdi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x2330
cmpq %r14, %rax
jne 0x8ce9
movq 0x147b8(%rip), %rdi     # 0x1d2a0
addq $0x18, %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x2330
testq %rax, %rax
je 0x8cf2
movq 0x14796(%rip), %r14     # 0x1d2a0
movslq 0x18(%r14), %rbx
imulq $0x30, %rbx, %rdi
callq 0x22c0
movq %rax, 0x10(%r14)
testq %rax, %rax
je 0x8cfb
movq %rax, %rdi
xorl %ecx, %ecx
pushq $0x4
popq %r15
pushq $0x1
popq %r12
movslq %ebx, %rax
cmpq %rax, %rcx
jge 0x8c32
movq %rcx, 0x10(%rsp)
imulq $0x30, %rcx, %r14
addq %r14, %rdi
movq %r15, %rsi
pushq $0x8
popq %rdx
movq %r13, %rbx
movq %r13, %rcx
callq 0x2330
cmpq $0x8, %rax
jne 0x8c8f
movq 0x14735(%rip), %rax     # 0x1d2a0
movq 0x10(%rax), %rax
leaq (%rax,%r14), %rdi
addq $0x28, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x2330
testq %rax, %rax
je 0x8c98
movq 0x1470b(%rip), %rax     # 0x1d2a0
movq 0x10(%rax), %rbx
movslq 0x28(%rbx,%r14), %rdi
shlq $0x3, %rdi
callq 0x22c0
movq %rax, 0x20(%rbx,%r14)
testq %rax, %rax
pushq $0x8
popq %r15
je 0x8ca1
xorl %ebx, %ebx
movq 0x146de(%rip), %rax     # 0x1d2a0
movq 0x10(%rax), %rdi
movslq 0x28(%rdi,%r14), %rcx
cmpq %rcx, %rbx
jge 0x8c1e
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0x2330
testq %rax, %rax
je 0x8c7b
movq 0x146ad(%rip), %rax     # 0x1d2a0
movslq 0xc(%rsp), %rcx
imulq %rbp, %rcx
movslq 0x8(%rsp), %rdx
addq %rcx, %rdx
shlq $0x3, %rdx
addq 0x8(%rax), %rdx
movq 0x10(%rax), %rax
movq 0x20(%rax,%r14), %rax
movq %rdx, (%rax,%rbx,8)
incq %rbx
jmp 0x8bbb
movq 0x10(%rsp), %rcx
incq %rcx
movl 0x18(%rax), %ebx
pushq $0x4
popq %r15
jmp 0x8b31
movq %r13, %rdi
callq 0x21f0
leaq 0xc6fb(%rip), %rdi      # 0x1533c
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2340
leaq 0xc6b5(%rip), %rdi      # 0x15310
callq 0x2340
callq 0x8d07
movq %r14, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x9621
leaq 0xb988(%rip), %rdi      # 0x1460a
callq 0x2190
pushq $0x1
popq %rdi
callq 0x2270
leaq 0xb7c4(%rip), %rdi      # 0x1445a
jmp 0x8c82
leaq 0xb847(%rip), %rdi      # 0x144e6
jmp 0x8c82
leaq 0xb8c4(%rip), %rdi      # 0x1456c
jmp 0x8c82
leaq 0xb397(%rip), %rdi      # 0x14048
jmp 0x8c82
leaq 0xb3e0(%rip), %rdi      # 0x1409a
jmp 0x8c82
leaq 0xb434(%rip), %rdi      # 0x140f7
jmp 0x8c82
leaq 0xb48c(%rip), %rdi      # 0x14158
jmp 0x8c82
leaq 0xb4f0(%rip), %rdi      # 0x141c5
jmp 0x8c82
leaq 0xb551(%rip), %rdi      # 0x1422f
jmp 0x8c82
leaq 0xb5bb(%rip), %rdi      # 0x142a2
jmp 0x8c82
leaq 0xb614(%rip), %rdi      # 0x14304
jmp 0x8c82
leaq 0xb676(%rip), %rdi      # 0x1436f
jmp 0x8c82
leaq 0xb6d9(%rip), %rdi      # 0x143db
jmp 0x8c82