// File: /workspace/github_c_makefile_star2_O1/Ziddykins[P]Dawn/src/map.c
// Function: generate_map
void generate_map() {
    int dim = global_map->dim;
    unsigned int seed;
    FILE *urandom;
    float *copy;


    CALLEXIT(!(urandom = fopen("/dev/urandom", "r")))
    CALLEXIT(!(fread(&seed, sizeof(seed), 1, urandom)))
    srand(seed);
    fclose(urandom);

    global_map->dim = 1 << 10;
    CALLEXIT(!(global_map->heightmap = calloc(sizeof *global_map->heightmap, (size_t) (global_map->dim * global_map->dim))))
    diamond_square(global_map->heightmap, global_map->dim, 4000.0, global_map->dim);
    global_map->flags |= MAP_PRESENT;
    CALLEXIT(!(copy = malloc((size_t)(dim*dim) * sizeof *copy)))

    for(int i = 0; i < dim*dim; i++) {
        copy[i] = global_map->heightmap[i];
    }

    qsort(copy, (size_t) (dim*dim), sizeof *copy, &compare_float_asc);
    global_map->water_level = copy[(int)(1.0/6.0*dim*dim)];
    free(copy);

    int town_count = (int) (10 + gaussrand() * 3);
    global_map->town_count = 0;
    CALLEXIT(!(global_map->towns = calloc((size_t) (town_count), sizeof *global_map->towns)))
    CALLEXIT(!(global_map->m_ent = calloc((size_t) (dim * dim), sizeof *global_map->m_ent)))
    perlin_init();

    for (int i = 0; i < town_count; i++) {
        place_town(i);
        global_map->town_count++;
    }

    assert(town_count == global_map->town_count);

    for (int i = 0; i < town_count; i++) {
        grow_town(i);
    }

    perlin_cleanup();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq 0x156ea(%rip), %rax     # 0x1f2a0
movl 0x20(%rax), %ebp
leaq 0xad12(%rip), %rdi      # 0x148d2
leaq 0xa7fc(%rip), %rsi      # 0x143c3
callq 0x2230
testq %rax, %rax
je 0xa4ba
movq %rax, %rbx
leaq 0x44(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x2320
testq %rax, %rax
je 0xa4bf
movl 0x44(%rsp), %edi
callq 0x21d0
movq %rbx, %rdi
callq 0x21f0
movq 0x15690(%rip), %rbx     # 0x1f2a0
movl $0x400, 0x20(%rbx)      # imm = 0x400
movl $0x4, %edi
movl $0x100000, %esi         # imm = 0x100000
callq 0x2110
movq %rax, (%rbx)
testq %rax, %rax
je 0xa4c4
movq 0x15667(%rip), %rax     # 0x1f2a0
movq (%rax), %rdi
movl 0x20(%rax), %edx
movss 0xb3bd(%rip), %xmm0     # 0x15004
movl %edx, %esi
callq 0xa7a5
movq 0x1564b(%rip), %r15     # 0x1f2a0
orl $0x1, 0x24(%r15)
movl %ebp, %ebx
imull %ebx, %ebx
leaq (,%rbx,4), %rdi
callq 0x22b0
testq %rax, %rax
je 0xa4c9
movq %rax, %r14
testl %ebp, %ebp
je 0x9c9f
movq (%r15), %rax
cmpl $0x2, %ebx
movl $0x1, %ecx
cmovael %ebx, %ecx
xorl %edx, %edx
movss (%rax,%rdx,4), %xmm0
movss %xmm0, (%r14,%rdx,4)
incq %rdx
cmpq %rdx, %rcx
jne 0x9c8c
leaq 0x61eb(%rip), %rcx      # 0xfe91
movl $0x4, %edx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2450
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movsd 0xb35b(%rip), %xmm1     # 0x15020
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
cltq
movss (%r14,%rax,4), %xmm0
movq 0x155c0(%rip), %rax     # 0x1f2a0
movss %xmm0, 0x1c(%rax)
movq %r14, %rdi
callq 0x2240
callq 0xfd3c
mulsd 0xb32e(%rip), %xmm0     # 0x15028
addsd 0xb32e(%rip), %xmm0     # 0x15030
cvttsd2si %xmm0, %eax
movq 0x15593(%rip), %r14     # 0x1f2a0
movl $0x0, 0x18(%r14)
movl %eax, 0x24(%rsp)
movslq %eax, %rdi
movl $0x30, %esi
callq 0x2110
movq %rax, 0x10(%r14)
testq %rax, %rax
je 0xa4ce
movl $0x8, %esi
movq %rbx, %rdi
callq 0x2110
movq %rax, 0x8(%r14)
testq %rax, %rax
je 0xa4d3
callq 0xfeaf
movl 0x24(%rsp), %ecx
movl %ecx, %eax
movq %rax, 0x48(%rsp)
testl %ecx, %ecx
jle 0x9fc7
xorl %r14d, %r14d
xorl %ecx, %ecx
movq %r14, 0x28(%rsp)
movq 0x1552a(%rip), %rax     # 0x1f2a0
movl 0x20(%rax), %r15d
movq 0x10(%rax), %rbx
movq %rcx, 0x10(%rsp)
leaq (%rcx,%rcx,2), %r14
shlq $0x4, %r14
callq 0xfd3c
mulsd 0xb2a0(%rip), %xmm0     # 0x15038
addsd 0xb290(%rip), %xmm0     # 0x15030
cvttsd2si %xmm0, %eax
movl %eax, 0x28(%rbx,%r14)
movslq %eax, %rdi
movl $0x8, %esi
callq 0x2110
movq %rax, 0x20(%rbx,%r14)
testq %rax, %rax
je 0xa4ab
movq %rbx, 0x38(%rsp)
addq %rbx, %r14
movq %r14, 0x50(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %esi, %esi
movl %r15d, 0x30(%rsp)
movl %esi, %r14d
callq 0xfd28
mulsd 0x8(%rsp), %xmm0
cvttsd2si %xmm0, %ebp
callq 0xfd28
mulsd 0x8(%rsp), %xmm0
cvttsd2si %xmm0, %ebx
leal 0x1(%r14), %esi
testl %ebp, %ebp
js 0x9ee9
cmpl %ebx, %r15d
jle 0x9ee9
cmpl %ebp, %r15d
jle 0x9ee9
testl %ebx, %ebx
js 0x9ee9
movq 0x1546c(%rip), %rcx     # 0x1f2a0
movq (%rcx), %rax
movl 0x20(%rcx), %edx
imull %ebx, %edx
addl %ebp, %edx
movslq %edx, %rdx
movss 0x1c(%rcx), %xmm0
ucomiss (%rax,%rdx,4), %xmm0
ja 0x9ee9
movl %esi, 0x18(%rsp)
cmpl $0x0, 0x18(%rcx)
setg %r13b
jle 0x9eda
movl $0x20, %r15d
xorl %r12d, %r12d
movq 0x10(%rcx), %rax
movq (%rax,%r15), %rax
movq (%rax), %rax
testq %rax, %rax
je 0x9ebf
subq 0x8(%rcx), %rax
movslq 0x20(%rcx), %rcx
sarq $0x3, %rax
cqto
idivq %rcx
subl %ebp, %edx
imull %edx, %edx
subl %ebx, %eax
imull %eax, %eax
addl %edx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
ucomisd 0xb19c(%rip), %xmm0     # 0x15040
jb 0x9eac
sqrtsd %xmm0, %xmm0
jmp 0x9eb1
callq 0x2150
movsd 0xb177(%rip), %xmm1     # 0x15030
ucomisd %xmm0, %xmm1
jae 0x9eda
incq %r12
movq 0x153d7(%rip), %rcx     # 0x1f2a0
movslq 0x18(%rcx), %rax
addq $0x30, %r15
cmpq %rax, %r12
setl %r13b
jl 0x9e68
testb $0x1, %r13b
movl 0x30(%rsp), %r15d
movl 0x18(%rsp), %esi
je 0x9ef6
cmpl $0x270f, %r14d          # imm = 0x270F
jb 0x9de6
cmpl $0x270f, %r14d          # imm = 0x270F
jae 0xa4b0
movq 0x15396(%rip), %rax     # 0x1f2a0
imull %ebx, %r15d
addl %ebp, %r15d
movslq %r15d, %rcx
shlq $0x3, %rcx
addq 0x8(%rax), %rcx
movq 0x50(%rsp), %rdx
movq 0x20(%rdx), %rax
movq %rcx, (%rax)
movq 0x20(%rdx), %rax
movq (%rax), %rax
xorps %xmm1, %xmm1
cvtsi2ss %ebp, %xmm1
movl $0x1, (%rax)
cvtsi2ss %ebx, %xmm2
movss 0xb0c0(%rip), %xmm0     # 0x15008
mulss %xmm0, %xmm1
movss %xmm1, 0x8(%rsp)
mulss %xmm0, %xmm2
movss %xmm2, 0x18(%rsp)
movq 0x28(%rsp), %r14
movq 0x38(%rsp), %r15
addq %r14, %r15
xorl %ebx, %ebx
movss 0xb099(%rip), %xmm1     # 0x1500c
xorps %xmm2, %xmm2
cvtsi2ss %ebx, %xmm2
mulss %xmm1, %xmm2
movss 0x8(%rsp), %xmm0
movss 0x18(%rsp), %xmm1
callq 0xffae
movss 0xb075(%rip), %xmm1     # 0x1500c
movss %xmm0, (%r15,%rbx,4)
incq %rbx
cmpq $0x8, %rbx
jne 0x9f73
movq 0x152f3(%rip), %rax     # 0x1f2a0
incl 0x18(%rax)
movq 0x10(%rsp), %rcx
incq %rcx
addq $0x30, %r14
cmpq 0x48(%rsp), %rcx
jne 0x9d6a
movq 0x152d2(%rip), %rax     # 0x1f2a0
movl 0x24(%rsp), %ecx
cmpl %ecx, 0x18(%rax)
jne 0xa4d8
testl %ecx, %ecx
jle 0xa44a
xorl %r13d, %r13d
movq 0x152b3(%rip), %r15     # 0x1f2a0
movq 0x10(%r15), %rbp
leaq (,%r13,2), %r14
addq %r13, %r14
shlq $0x4, %r14
movslq 0x20(%r15), %r12
movl 0x28(%rbp,%r14), %eax
movq %rax, 0x8(%rsp)
leal (%rax,%rax,4), %ebx
addl $0x5, %ebx
movslq %ebx, %rdi
movl $0x8, %esi
movq %rdi, 0x30(%rsp)
callq 0x2110
testq %rax, %rax
je 0xa4b5
movq %rax, %rdi
addq %r14, %rbp
movq 0x20(%rbp), %rax
movq (%rax), %rax
subq 0x8(%r15), %rax
sarq $0x3, %rax
cqto
idivq %r12
movq %rax, %rcx
leal 0x1(%rdx), %eax
movl %eax, (%rdi)
movl %ecx, 0x4(%rdi)
cmpl $0x1, %ebx
je 0xa487
movq %rdx, %rsi
leal -0x1(%rdx), %eax
movl %eax, 0x8(%rdi)
movl %ecx, 0xc(%rdi)
movl $0x2, %eax
xorl %edx, %edx
movq 0x30(%rsp), %r8
divq %r8
testq %rdx, %rdx
je 0xa487
movq %r13, 0x38(%rsp)
shlq $0x20, %rcx
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rcx, %rax
shrq $0x20, %rax
movl %esi, (%rdi,%rdx,8)
movl %eax, 0x4(%rdi,%rdx,8)
leaq 0x1(%rdx), %rax
cmpq %r8, %rax
je 0xa487
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
addq %rax, %rcx
shrq $0x20, %rcx
movl %esi, 0x8(%rdi,%rdx,8)
movl %ecx, 0xc(%rdi,%rdx,8)
addq $0x2, %rdx
movq %rdx, %rax
xorl %edx, %edx
divq %r8
testq %rdx, %rdx
je 0xa487
cmpl $0x0, 0x8(%rsp)
je 0xa432
movq %rbp, %r9
movq %r12, %r10
movq %rdx, %r14
xorl %edx, %edx
xorl %ecx, %ecx
movq %rbp, 0x10(%rsp)
movq %rdi, 0x28(%rsp)
cmpq %r14, %rcx
je 0xa45d
movq %r14, 0x8(%rsp)
leaq 0x1(%rcx), %rbx
movl (%rdi,%rcx,8), %esi
movl 0x4(%rdi,%rcx,8), %ecx
movq 0x15181(%rip), %rax     # 0x1f2a0
movq %rcx, %r12
movq %r10, %rbp
imull %r10d, %ecx
movq %rsi, %r13
addl %esi, %ecx
movslq %ecx, %rcx
shlq $0x3, %rcx
addq 0x8(%rax), %rcx
movq 0x20(%r9), %rax
movq %rcx, (%rax,%rdx,8)
movq 0x20(%r9), %rax
movq %rdx, 0x18(%rsp)
movq (%rax,%rdx,8), %r14
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
callq 0xfd28
leaq 0xc220(%rip), %rax      # 0x16380
xorps %xmm1, %xmm1
cvtss2sd (%rax), %xmm1
incl %r15d
addq $0x4, %rax
ucomisd %xmm1, %xmm0
ja 0xa160
movq 0x30(%rsp), %r11
cmpq %r11, %rbx
movl $0x0, %ecx
cmovneq %rbx, %rcx
movl %r15d, (%r14)
leal -0x2(%r15), %eax
cmpl $0x4, %eax
jae 0xa47c
movq 0x15104(%rip), %rsi     # 0x1f2a0
movl 0x20(%rsi), %ebx
movq %r13, %r15
cmpl $-0x1, %r15d
jl 0xa24b
cmpl %ebx, %r12d
movq %rbp, %r10
movq 0x28(%rsp), %rdi
movq 0x8(%rsp), %r14
jge 0xa258
testl %r12d, %r12d
js 0xa258
leal 0x1(%r15), %r8d
cmpl %ebx, %r8d
jge 0xa258
movq (%rsi), %rdx
movl %ebx, %eax
imull %r12d, %eax
addl %r8d, %eax
cltq
movss 0x1c(%rsi), %xmm0
ucomiss (%rdx,%rax,4), %xmm0
ja 0xa258
movq 0x8(%rsi), %rdx
cmpl $0x0, (%rdx,%rax,8)
jne 0xa258
cmpq %r14, %rcx
setne %al
je 0xa226
movq %rcx, %rdx
cmpl %r8d, (%rdi,%rdx,8)
jne 0xa213
cmpl %r12d, 0x4(%rdi,%rdx,8)
je 0xa226
incq %rdx
movq %rdx, %rax
xorl %edx, %edx
divq %r11
cmpq %r14, %rdx
setne %al
jne 0xa206
testb $0x1, %al
jne 0xa258
movl %r8d, (%rdi,%r14,8)
movl %r12d, 0x4(%rdi,%r14,8)
incq %r14
movq %r14, %rax
xorl %edx, %edx
divq %r11
movq %rdx, %r14
cmpq %rcx, %rdx
jne 0xa258
jmp 0xa487
movq %rbp, %r10
movq 0x28(%rsp), %rdi
movq 0x8(%rsp), %r14
testl %r15d, %r15d
jle 0xa2ea
cmpl %ebx, %r12d
jge 0xa2ea
testl %r12d, %r12d
js 0xa2ea
cmpl %ebx, %r15d
jg 0xa2ea
leal -0x1(%r15), %r8d
movq (%rsi), %rdx
movl %ebx, %eax
imull %r12d, %eax
addl %r8d, %eax
cltq
movss 0x1c(%rsi), %xmm0
ucomiss (%rdx,%rax,4), %xmm0
ja 0xa2ea
movq 0x8(%rsi), %rdx
cmpl $0x0, (%rdx,%rax,8)
jne 0xa2ea
cmpq %r14, %rcx
setne %al
je 0xa2c6
movq %rcx, %rdx
cmpl %r8d, (%rdi,%rdx,8)
jne 0xa2b3
cmpl %r12d, 0x4(%rdi,%rdx,8)
je 0xa2c6
incq %rdx
movq %rdx, %rax
xorl %edx, %edx
divq %r11
cmpq %r14, %rdx
setne %al
jne 0xa2a6
testb $0x1, %al
jne 0xa2ea
movl %r8d, (%rdi,%r14,8)
movl %r12d, 0x4(%rdi,%r14,8)
incq %r14
movq %r14, %rax
xorl %edx, %edx
divq %r11
movq %rdx, %r14
cmpq %rcx, %rdx
je 0xa487
testl %r15d, %r15d
js 0xa385
leal 0x1(%r12), %r8d
cmpl %ebx, %r15d
setge %al
cmpl $-0x1, %r12d
setl %dl
cmpl %ebx, %r8d
setge %r9b
orb %al, %dl
orb %r9b, %dl
jne 0xa385
movq (%rsi), %rdx
movl %ebx, %eax
imull %r8d, %eax
addl %r15d, %eax
cltq
movss 0x1c(%rsi), %xmm0
ucomiss (%rdx,%rax,4), %xmm0
ja 0xa385
movq 0x8(%rsi), %rdx
cmpl $0x0, (%rdx,%rax,8)
jne 0xa385
cmpq %r14, %rcx
setne %al
je 0xa361
movq %rcx, %rdx
cmpl %r15d, (%rdi,%rdx,8)
jne 0xa34e
cmpl %r8d, 0x4(%rdi,%rdx,8)
je 0xa361
incq %rdx
movq %rdx, %rax
xorl %edx, %edx
divq %r11
cmpq %r14, %rdx
setne %al
jne 0xa341
testb $0x1, %al
jne 0xa385
movl %r15d, (%rdi,%r14,8)
movl %r8d, 0x4(%rdi,%r14,8)
incq %r14
movq %r14, %rax
xorl %edx, %edx
divq %r11
movq %rdx, %r14
cmpq %rcx, %rdx
je 0xa487
testl %r15d, %r15d
js 0xa418
cmpl %ebx, %r12d
jg 0xa418
testl %r12d, %r12d
jle 0xa418
cmpl %ebx, %r15d
jge 0xa418
decl %r12d
movq (%rsi), %rdx
imull %r12d, %ebx
addl %r15d, %ebx
movslq %ebx, %rax
movss 0x1c(%rsi), %xmm0
ucomiss (%rdx,%rax,4), %xmm0
ja 0xa418
movq 0x8(%rsi), %rdx
cmpl $0x0, (%rdx,%rax,8)
movq 0x10(%rsp), %r9
jne 0xa41d
cmpq %r14, %rcx
setne %al
je 0xa3f6
movq %rcx, %rdx
cmpl %r15d, (%rdi,%rdx,8)
jne 0xa3e3
cmpl %r12d, 0x4(%rdi,%rdx,8)
je 0xa3f6
incq %rdx
movq %rdx, %rax
xorl %edx, %edx
divq %r11
cmpq %r14, %rdx
setne %al
jne 0xa3d6
testb $0x1, %al
jne 0xa41d
movl %r15d, (%rdi,%r14,8)
movl %r12d, 0x4(%rdi,%r14,8)
incq %r14
movq %r14, %rax
xorl %edx, %edx
divq %r11
movq %rdx, %r14
cmpq %rcx, %rdx
jne 0xa41d
jmp 0xa487
movq 0x10(%rsp), %r9
movq 0x18(%rsp), %rdx
incq %rdx
movslq 0x28(%r9), %rax
cmpq %rax, %rdx
jb 0xa0ff
callq 0x2240
movq 0x38(%rsp), %r13
incq %r13
cmpq 0x48(%rsp), %r13
jne 0x9fe6
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xff8c
leaq 0xbd0f(%rip), %rdi      # 0x16173
leaq 0xb1fc(%rip), %rsi      # 0x15667
leaq 0xbd0f(%rip), %rcx      # 0x16181
movl $0xfd, %edx
callq 0x2100
cmpl $0x1, %r15d
jne 0xa4a6
callq 0x2b0d
leaq 0xbc8b(%rip), %rdi      # 0x16119
leaq 0xb1d2(%rip), %rsi      # 0x15667
leaq 0xbc8b(%rip), %rcx      # 0x16127
movl $0x107, %edx            # imm = 0x107
callq 0x2100
callq 0x2b22
callq 0x2b61
callq 0x2b4c
callq 0x2b37
callq 0x2bdf
callq 0x2bca
callq 0x2bb5
callq 0x2ba0
callq 0x2b8b
callq 0x2b76
leaq 0xb82f(%rip), %rdi      # 0x15d0e
leaq 0xb181(%rip), %rsi      # 0x15667
leaq 0xb846(%rip), %rcx      # 0x15d33
movl $0x16e, %edx            # imm = 0x16E
callq 0x2100