// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movl $0x28, %edi
movl $0x1, %esi
callq 0x2110
movq %rax, 0x1927e(%rip)     # 0x26290
cmpq $0x0, %rax
jne 0xd02e
leaq 0x110d9(%rip), %rdi     # 0x1e0f8
callq 0x2190
movl $0x1, %edi
callq 0x2260
movq 0x1925b(%rip), %rax     # 0x26290
movl $0x400, 0x20(%rax)      # imm = 0x400
movq 0x1924d(%rip), %rax     # 0x26290
movl $0x0, 0x24(%rax)
movq -0x8(%rbp), %rdi
leaq 0x110f2(%rip), %rsi     # 0x1e147
callq 0x2230
movq %rax, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0xd086
leaq 0x110de(%rip), %rdi     # 0x1e14a
movb $0x0, %al
callq 0x2040
callq 0xd480
movq -0x8(%rbp), %rdi
callq 0xd7b0
jmp 0xd47a
movq 0x19203(%rip), %rdi     # 0x26290
addq $0x20, %rdi
movq -0x10(%rbp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x2320
cmpq $0x0, %rax
jne 0xd0c0
leaq 0x110c6(%rip), %rdi     # 0x1e177
callq 0x2190
movl $0x1, %edi
callq 0x2260
movq 0x191c9(%rip), %rdi     # 0x26290
addq $0x24, %rdi
movq -0x10(%rbp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x2320
cmpq $0x0, %rax
jne 0xd0fa
leaq 0x110e9(%rip), %rdi     # 0x1e1d4
callq 0x2190
movl $0x1, %edi
callq 0x2260
movq 0x1918f(%rip), %rdi     # 0x26290
addq $0x1c, %rdi
movq -0x10(%rbp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x2320
cmpq $0x0, %rax
jne 0xd134
leaq 0x11110(%rip), %rdi     # 0x1e235
callq 0x2190
movl $0x1, %edi
callq 0x2260
movq 0x19155(%rip), %rax     # 0x26290
movl 0x20(%rax), %eax
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %eax
imull -0x14(%rbp), %eax
cltq
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
shlq $0x2, %rdi
callq 0x22b0
movq 0x1912e(%rip), %rcx     # 0x26290
movq %rax, (%rcx)
cmpq $0x0, %rax
jne 0xd181
leaq 0x11130(%rip), %rdi     # 0x1e2a2
callq 0x2190
movl $0x1, %edi
callq 0x2260
movq 0x19108(%rip), %rax     # 0x26290
movq (%rax), %rdi
movq -0x20(%rbp), %rdx
movq -0x10(%rbp), %rcx
movl $0x4, %esi
callq 0x2320
cmpq -0x20(%rbp), %rax
je 0xd1b9
leaq 0x11162(%rip), %rdi     # 0x1e30c
callq 0x2190
movl $0x1, %edi
callq 0x2260
movl -0x14(%rbp), %eax
imull -0x14(%rbp), %eax
cltq
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
shlq $0x3, %rdi
callq 0x22b0
movq 0x190b6(%rip), %rcx     # 0x26290
movq %rax, 0x8(%rcx)
cmpq $0x0, %rax
jne 0xd1fa
leaq 0x11194(%rip), %rdi     # 0x1e37f
callq 0x2190
movl $0x1, %edi
callq 0x2260
movq 0x1908f(%rip), %rax     # 0x26290
movq 0x8(%rax), %rdi
movq -0x20(%rbp), %rdx
movq -0x10(%rbp), %rcx
movl $0x8, %esi
callq 0x2320
cmpq -0x20(%rbp), %rax
je 0xd233
leaq 0x111bd(%rip), %rdi     # 0x1e3e1
callq 0x2190
movl $0x1, %edi
callq 0x2260
movq 0x19056(%rip), %rdi     # 0x26290
addq $0x18, %rdi
movq -0x10(%rbp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x2320
cmpq $0x0, %rax
jne 0xd26d
leaq 0x111ee(%rip), %rdi     # 0x1e44c
callq 0x2190
movl $0x1, %edi
callq 0x2260
movq 0x1901c(%rip), %rax     # 0x26290
movslq 0x18(%rax), %rax
imulq $0x30, %rax, %rdi
callq 0x22b0
movq 0x19008(%rip), %rcx     # 0x26290
movq %rax, 0x10(%rcx)
cmpq $0x0, %rax
jne 0xd2a8
leaq 0x1121f(%rip), %rdi     # 0x1e4b8
callq 0x2190
movl $0x1, %edi
callq 0x2260
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq 0x18fd7(%rip), %rcx     # 0x26290
cmpl 0x18(%rcx), %eax
jge 0xd463
movq 0x18fc7(%rip), %rax     # 0x26290
movq 0x10(%rax), %rdi
movslq -0x2c(%rbp), %rax
imulq $0x30, %rax, %rax
addq %rax, %rdi
movq -0x10(%rbp), %rcx
movl $0x4, %esi
movl $0x8, %edx
callq 0x2320
cmpq $0x8, %rax
je 0xd307
leaq 0x1123f(%rip), %rdi     # 0x1e537
callq 0x2190
movl $0x1, %edi
callq 0x2260
movq 0x18f82(%rip), %rax     # 0x26290
movq 0x10(%rax), %rdi
movslq -0x2c(%rbp), %rax
imulq $0x30, %rax, %rax
addq %rax, %rdi
addq $0x28, %rdi
movq -0x10(%rbp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x2320
cmpq $0x0, %rax
jne 0xd350
leaq 0x11282(%rip), %rdi     # 0x1e5c3
callq 0x2190
movl $0x1, %edi
callq 0x2260
movq 0x18f39(%rip), %rax     # 0x26290
movq 0x10(%rax), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movslq 0x28(%rax), %rdi
shlq $0x3, %rdi
callq 0x22b0
movq 0x18f16(%rip), %rcx     # 0x26290
movq 0x10(%rcx), %rcx
movslq -0x2c(%rbp), %rdx
imulq $0x30, %rdx, %rdx
addq %rdx, %rcx
movq %rax, 0x20(%rcx)
cmpq $0x0, %rax
jne 0xd3a9
leaq 0x112af(%rip), %rdi     # 0x1e649
callq 0x2190
movl $0x1, %edi
callq 0x2260
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
movq 0x18ed6(%rip), %rcx     # 0x26290
movq 0x10(%rcx), %rcx
movslq -0x2c(%rbp), %rdx
imulq $0x30, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x28(%rcx), %eax
jge 0xd453
movq -0x10(%rbp), %rcx
leaq -0x28(%rbp), %rdi
movl $0x8, %esi
movl $0x1, %edx
callq 0x2320
cmpq $0x0, %rax
jne 0xd405
leaq 0x112f1(%rip), %rdi     # 0x1e6e7
callq 0x2190
movl $0x1, %edi
callq 0x2260
movq 0x18e84(%rip), %rax     # 0x26290
movq 0x8(%rax), %rdx
movl -0x24(%rbp), %eax
imull -0x14(%rbp), %eax
addl -0x28(%rbp), %eax
cltq
shlq $0x3, %rax
addq %rax, %rdx
movq 0x18e66(%rip), %rax     # 0x26290
movq 0x10(%rax), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq 0x20(%rax), %rax
movslq -0x30(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0xd3b0
jmp 0xd455
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0xd2af
movq -0x10(%rbp), %rdi
callq 0x21f0
leaq 0x112be(%rip), %rdi     # 0x1e731
movb $0x0, %al
callq 0x2040
addq $0x30, %rsp
popq %rbp
retq