// File: /workspace/github_c_makefile_star2_O3/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 $0x48, %rsp
movq 0x15655(%rip), %rax     # 0x1f2a0
movl 0x20(%rax), %ebp
leaq 0xacad(%rip), %rdi      # 0x14902
leaq 0xa797(%rip), %rsi      # 0x143f3
callq 0x2230
testq %rax, %rax
je 0xa4de
movq %rax, %rbx
leaq 0x34(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x2320
testq %rax, %rax
je 0xa4e3
movl 0x34(%rsp), %edi
callq 0x21d0
movq %rbx, %rdi
callq 0x21f0
movq 0x155fb(%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 0xa4e8
movq 0x155d2(%rip), %rax     # 0x1f2a0
movq (%rax), %rdi
movl 0x20(%rax), %edx
movss 0xb358(%rip), %xmm0     # 0x15034
movl %edx, %esi
callq 0xa7c2
movq 0x155b6(%rip), %r15     # 0x1f2a0
orl $0x1, 0x24(%r15)
movl %ebp, %ebx
imull %ebx, %ebx
leaq (,%rbx,4), %rdi
callq 0x22b0
testq %rax, %rax
je 0xa4ed
movq %rax, %r14
testl %ebp, %ebp
je 0x9d34
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 0x9d21
leaq 0x6416(%rip), %rcx      # 0x10151
movl $0x4, %edx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2450
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movsd 0xb2f6(%rip), %xmm1     # 0x15050
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
cltq
movss (%r14,%rax,4), %xmm0
movq 0x1552b(%rip), %rax     # 0x1f2a0
movss %xmm0, 0x1c(%rax)
movq %r14, %rdi
callq 0x2240
callq 0xfffc
mulsd 0xb2c9(%rip), %xmm0     # 0x15058
addsd 0xb2c9(%rip), %xmm0     # 0x15060
cvttsd2si %xmm0, %ebp
movq 0x154fe(%rip), %r14     # 0x1f2a0
movl $0x0, 0x18(%r14)
movslq %ebp, %rdi
movl $0x30, %esi
callq 0x2110
movq %rax, 0x10(%r14)
testq %rax, %rax
je 0xa4f2
movl $0x8, %esi
movq %rbx, %rdi
callq 0x2110
movq %rax, 0x8(%r14)
testq %rax, %rax
je 0xa4f7
callq 0x1016f
movq 0x154b6(%rip), %r15     # 0x1f2a0
testl %ebp, %ebp
jle 0xa464
movl %ebp, 0x20(%rsp)
movl %ebp, %eax
movq %rax, 0x38(%rsp)
xorl %r14d, %r14d
xorl %ecx, %ecx
movq %r14, 0x28(%rsp)
movl 0x20(%r15), %ebp
movq 0x10(%r15), %r15
movq %rcx, 0x18(%rsp)
leaq (%rcx,%rcx,2), %rbx
shlq $0x4, %rbx
callq 0xfffc
mulsd 0xb23f(%rip), %xmm0     # 0x15068
addsd 0xb22f(%rip), %xmm0     # 0x15060
cvttsd2si %xmm0, %eax
movl %eax, 0x28(%r15,%rbx)
movslq %eax, %rdi
movl $0x8, %esi
callq 0x2110
movq %rax, 0x20(%r15,%rbx)
testq %rax, %rax
je 0xa4d4
addq %r15, %rbx
movq %rbx, 0x10(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %esi, %esi
movl %esi, %ebx
callq 0xffe8
mulsd 0x8(%rsp), %xmm0
cvttsd2si %xmm0, %r13d
callq 0xffe8
leal 0x1(%rbx), %esi
testl %r13d, %r13d
js 0x9f6c
mulsd 0x8(%rsp), %xmm0
cvttsd2si %xmm0, %ecx
cmpl %ecx, %ebp
jbe 0x9f6c
cmpl %r13d, %ebp
jle 0x9f6c
testl %ecx, %ecx
js 0x9f6c
movq 0x153e7(%rip), %rdi     # 0x1f2a0
movq (%rdi), %rax
movslq 0x20(%rdi), %r8
movl %r8d, %edx
imull %ecx, %edx
addl %r13d, %edx
movslq %edx, %rdx
movss 0x1c(%rdi), %xmm0
ucomiss (%rax,%rdx,4), %xmm0
ja 0x9f6c
movslq 0x18(%rdi), %r9
testq %r9, %r9
jle 0x9f7d
movq 0x10(%rdi), %r10
movq %r9, %r11
negq %r11
addq $0x20, %r10
movb $0x1, %r14b
movl $0x1, %r12d
movsd 0xb159(%rip), %xmm1     # 0x15060
movq (%r10), %rax
movq (%rax), %rax
testq %rax, %rax
je 0x9f3d
subq 0x8(%rdi), %rax
sarq $0x3, %rax
cqto
idivq %r8
subl %r13d, %edx
imull %edx, %edx
subl %ecx, %eax
imull %eax, %eax
addl %edx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
sqrtsd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jae 0x9f58
cmpq %r9, %r12
setb %r14b
leaq (%r11,%r12), %rax
incq %rax
incq %r12
addq $0x30, %r10
cmpq $0x1, %rax
jne 0x9f07
testb $0x1, %r14b
je 0x9f7d
cmpl $0x270f, %ebx           # imm = 0x270F
jb 0x9e6c
jmp 0x9f7d
cmpl $0x270f, %ebx           # imm = 0x270F
jb 0x9e6c
jmp 0xa481
cmpl $0x270f, %ebx           # imm = 0x270F
jae 0xa481
imull %ecx, %ebp
addl %r13d, %ebp
movslq %ebp, %rax
shlq $0x3, %rax
addq 0x8(%rdi), %rax
movq 0x10(%rsp), %rsi
movq 0x20(%rsi), %rdx
movq %rax, (%rdx)
movq 0x20(%rsi), %rax
movq (%rax), %rax
xorps %xmm1, %xmm1
cvtsi2ss %r13d, %xmm1
movl $0x1, (%rax)
cvtsi2ss %ecx, %xmm2
movss 0xb071(%rip), %xmm0     # 0x15038
mulss %xmm0, %xmm1
movss %xmm1, 0x8(%rsp)
mulss %xmm0, %xmm2
movss %xmm2, 0x10(%rsp)
movq 0x28(%rsp), %r14
addq %r14, %r15
xorl %ebx, %ebx
xorps %xmm2, %xmm2
cvtsi2ss %ebx, %xmm2
mulss 0xb048(%rip), %xmm2     # 0x1503c
movss 0x8(%rsp), %xmm0
movss 0x10(%rsp), %xmm1
callq 0x10285
movss %xmm0, (%r15,%rbx,4)
incq %rbx
cmpq $0x8, %rbx
jne 0x9fe5
movq 0x15285(%rip), %r15     # 0x1f2a0
movl 0x18(%r15), %eax
incl %eax
movl %eax, 0x18(%r15)
movq 0x18(%rsp), %rcx
incq %rcx
addq $0x30, %r14
cmpq 0x38(%rsp), %rcx
jne 0x9e02
movl 0x20(%rsp), %ecx
cmpl %ecx, %eax
jne 0xa4fc
testl %ecx, %ecx
jle 0xa46e
xorl %r12d, %r12d
movq 0x10(%r15), %r13
leaq (%r12,%r12,2), %r14
shlq $0x4, %r14
movslq 0x20(%r15), %rbx
movl 0x28(%r13,%r14), %eax
movq %rax, 0x8(%rsp)
leal (%rax,%rax,4), %ebp
addl $0x5, %ebp
movslq %ebp, %rdi
movl $0x8, %esi
movq %rdi, 0x18(%rsp)
callq 0x2110
testq %rax, %rax
je 0xa4d9
movq %rax, %r11
addq %r14, %r13
movq 0x20(%r13), %rax
movq (%rax), %rax
subq 0x8(%r15), %rax
sarq $0x3, %rax
cqto
idivq %rbx
movq %rax, %rcx
leal 0x1(%rdx), %eax
movl %eax, (%r11)
movl %ecx, 0x4(%r11)
cmpl $0x1, %ebp
je 0xa4b0
movq %rdx, %rsi
leal -0x1(%rdx), %eax
movl %eax, 0x8(%r11)
movl %ecx, 0xc(%r11)
movl $0x2, %eax
xorl %edx, %edx
movq 0x18(%rsp), %rdi
divq %rdi
testq %rdx, %rdx
je 0xa4b0
shlq $0x20, %rcx
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rcx, %rax
shrq $0x20, %rax
movl %esi, (%r11,%rdx,8)
movl %eax, 0x4(%r11,%rdx,8)
leaq 0x1(%rdx), %rax
cmpq %rdi, %rax
je 0xa4b0
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
addq %rax, %rcx
shrq $0x20, %rcx
movl %esi, 0x8(%r11,%rdx,8)
movl %ecx, 0xc(%r11,%rdx,8)
addq $0x2, %rdx
movq %rdx, %rax
xorl %edx, %edx
divq %rdi
testq %rdx, %rdx
je 0xa4b0
movq %r12, 0x40(%rsp)
cmpl $0x0, 0x8(%rsp)
je 0xa447
movq %r13, %r8
movq %rbx, %r9
movq %rdx, %rbx
xorl %edx, %edx
xorl %ecx, %ecx
movq %r11, 0x20(%rsp)
cmpq %rbx, %rcx
je 0xa486
movq %rbx, 0x8(%rsp)
leaq 0x1(%rcx), %r13
movl (%r11,%rcx,8), %esi
movl 0x4(%r11,%rcx,8), %eax
movq %rax, 0x28(%rsp)
movq %r9, %rbp
imull %r9d, %eax
movq %rsi, %r12
addl %esi, %eax
cltq
shlq $0x3, %rax
addq 0x8(%r15), %rax
movq 0x20(%r8), %rcx
movq %rax, (%rcx,%rdx,8)
movq %r8, %rbx
movq 0x20(%r8), %rax
movq %rdx, 0x10(%rsp)
movq (%rax,%rdx,8), %r15
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
callq 0xffe8
leaq 0xc20f(%rip), %rax      # 0x163d0
xorps %xmm1, %xmm1
cvtss2sd (%rax), %xmm1
incl %r14d
addq $0x4, %rax
ucomisd %xmm1, %xmm0
ja 0xa1c1
movq 0x18(%rsp), %r10
cmpq %r10, %r13
movl $0x0, %ecx
cmovneq %r13, %rcx
movl %r14d, (%r15)
leal -0x2(%r14), %eax
cmpl $0x4, %eax
jae 0xa4a5
movq 0x150a3(%rip), %r15     # 0x1f2a0
movq %r12, %r14
cmpl $-0x1, %r14d
movq %rbx, %r8
movq %rbp, %r9
movq 0x20(%rsp), %r11
jl 0xa42d
movl 0x20(%r15), %esi
movq 0x28(%rsp), %r12
cmpl %esi, %r12d
movq 0x8(%rsp), %rbx
jae 0xa29b
testl %r12d, %r12d
js 0xa29b
leal 0x1(%r14), %edi
cmpl %esi, %edi
jge 0xa29b
movq (%r15), %rdx
movl %esi, %eax
imull %r12d, %eax
addl %edi, %eax
movss 0x1c(%r15), %xmm0
ucomiss (%rdx,%rax,4), %xmm0
ja 0xa29b
movq 0x8(%r15), %rdx
cmpl $0x0, (%rdx,%rax,8)
jne 0xa29b
cmpq %rbx, %rcx
je 0xa27b
movq %rcx, %rdx
cmpl %edi, (%r11,%rdx,8)
jne 0xa26b
cmpl %r12d, 0x4(%r11,%rdx,8)
je 0xa29b
incq %rdx
movq %rdx, %rax
xorl %edx, %edx
divq %r10
cmpq %rbx, %rdx
jne 0xa25e
movl %edi, (%r11,%rbx,8)
movl %r12d, 0x4(%r11,%rbx,8)
incq %rbx
movq %rbx, %rax
xorl %edx, %edx
divq %r10
movq %rdx, %rbx
cmpq %rcx, %rdx
je 0xa4b0
testl %r14d, %r14d
jle 0xa319
cmpl %esi, %r12d
jae 0xa322
testl %r12d, %r12d
js 0xa322
cmpl %esi, %r14d
jg 0xa322
leal -0x1(%r14), %edi
movq (%r15), %rdx
movl %esi, %eax
imull %r12d, %eax
addl %edi, %eax
movss 0x1c(%r15), %xmm0
ucomiss (%rdx,%rax,4), %xmm0
ja 0xa322
movq 0x8(%r15), %rdx
cmpl $0x0, (%rdx,%rax,8)
jne 0xa322
cmpq %rbx, %rcx
je 0xa2f9
movq %rcx, %rdx
cmpl %edi, (%r11,%rdx,8)
jne 0xa2e9
cmpl %r12d, 0x4(%r11,%rdx,8)
je 0xa319
incq %rdx
movq %rdx, %rax
xorl %edx, %edx
divq %r10
cmpq %rbx, %rdx
jne 0xa2dc
movl %edi, (%r11,%rbx,8)
movl %r12d, 0x4(%r11,%rbx,8)
incq %rbx
movq %rbx, %rax
xorl %edx, %edx
divq %r10
movq %rdx, %rbx
cmpq %rcx, %rdx
je 0xa4b0
testl %r14d, %r14d
js 0xa432
leal 0x1(%r12), %edi
cmpl %esi, %r14d
setl %al
cmpl $-0x1, %r12d
setge %dl
andb %al, %dl
cmpl %esi, %edi
setb %al
andb %dl, %al
cmpb $0x1, %al
jne 0xa3a7
movq (%r15), %rdx
movl %esi, %eax
imull %edi, %eax
addl %r14d, %eax
movss 0x1c(%r15), %xmm0
ucomiss (%rdx,%rax,4), %xmm0
ja 0xa3a7
movq 0x8(%r15), %rdx
cmpl $0x0, (%rdx,%rax,8)
jne 0xa3a7
cmpq %rbx, %rcx
je 0xa387
movq %rcx, %rdx
cmpl %r14d, (%r11,%rdx,8)
jne 0xa377
cmpl %edi, 0x4(%r11,%rdx,8)
je 0xa3a7
incq %rdx
movq %rdx, %rax
xorl %edx, %edx
divq %r10
cmpq %rbx, %rdx
jne 0xa36a
movl %r14d, (%r11,%rbx,8)
movl %edi, 0x4(%r11,%rbx,8)
incq %rbx
movq %rbx, %rax
xorl %edx, %edx
divq %r10
movq %rdx, %rbx
cmpq %rcx, %rdx
je 0xa4b0
cmpl %esi, %r14d
setl %al
leal -0x1(%r12), %edi
testl %r12d, %r12d
setg %dl
andb %al, %dl
cmpl %esi, %edi
setb %al
andb %dl, %al
cmpb $0x1, %al
jne 0xa432
movq (%r15), %rdx
imull %edi, %esi
addl %r14d, %esi
movslq %esi, %rax
movss 0x1c(%r15), %xmm0
ucomiss (%rdx,%rax,4), %xmm0
ja 0xa432
movq 0x8(%r15), %rdx
cmpl $0x0, (%rdx,%rax,8)
jne 0xa432
cmpq %rbx, %rcx
je 0xa40c
movq %rcx, %rdx
cmpl %r14d, (%r11,%rdx,8)
jne 0xa3fc
cmpl %edi, 0x4(%r11,%rdx,8)
je 0xa432
incq %rdx
movq %rdx, %rax
xorl %edx, %edx
divq %r10
cmpq %rbx, %rdx
jne 0xa3ef
movl %r14d, (%r11,%rbx,8)
movl %edi, 0x4(%r11,%rbx,8)
incq %rbx
movq %rbx, %rax
xorl %edx, %edx
divq %r10
movq %rdx, %rbx
cmpq %rcx, %rdx
jne 0xa432
jmp 0xa4b0
movq 0x8(%rsp), %rbx
movq 0x10(%rsp), %rdx
incq %rdx
movslq 0x28(%r8), %rax
cmpq %rax, %rdx
jb 0xa161
movq %r11, %rdi
callq 0x2240
movq 0x40(%rsp), %r12
incq %r12
cmpq 0x38(%rsp), %r12
jne 0xa053
jmp 0xa46e
cmpl %ebp, 0x18(%r15)
jne 0xa4fc
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10263
callq 0x2b4c
leaq 0xbd36(%rip), %rdi      # 0x161c3
leaq 0xb223(%rip), %rsi      # 0x156b7
leaq 0xbd36(%rip), %rcx      # 0x161d1
movl $0xfd, %edx
callq 0x2100
cmpl $0x1, %r14d
jne 0xa4cf
callq 0x2b0d
leaq 0xbcb2(%rip), %rdi      # 0x16169
leaq 0xb1f9(%rip), %rsi      # 0x156b7
leaq 0xbcb2(%rip), %rcx      # 0x16177
movl $0x107, %edx            # imm = 0x107
callq 0x2100
callq 0x2b22
callq 0x2b61
callq 0x2b37
callq 0x2bdf
callq 0x2bca
callq 0x2bb5
callq 0x2ba0
callq 0x2b8b
callq 0x2b76
leaq 0xb85b(%rip), %rdi      # 0x15d5e
leaq 0xb1ad(%rip), %rsi      # 0x156b7
leaq 0xb872(%rip), %rcx      # 0x15d83
movl $0x16e, %edx            # imm = 0x16E
callq 0x2100