// File: /workspace/github_c_makefile_star2_O2/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 $0x68, %rsp
movq 0x14584(%rip), %rax     # 0x1d2a0
movl 0x20(%rax), %ebp
leaq 0xabac(%rip), %rdi      # 0x138d2
leaq 0xa696(%rip), %rsi      # 0x133c3
callq 0x2240
testq %rax, %rax
je 0x95c6
movq %rax, %rbx
leaq 0x4c(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
callq 0x2330
testq %rax, %rax
je 0x95cf
movl 0x4c(%rsp), %edi
callq 0x21d0
movq %rbx, %rdi
callq 0x21f0
movq 0x1452e(%rip), %rbx     # 0x1d2a0
movl $0x400, 0x20(%rbx)      # imm = 0x400
pushq $0x4
popq %rdi
movl $0x100000, %esi         # imm = 0x100000
callq 0x2110
movq %rax, (%rbx)
testq %rax, %rax
je 0x95d8
movq 0x14507(%rip), %rax     # 0x1d2a0
movq (%rax), %rdi
movl 0x20(%rax), %edx
movss 0xb25d(%rip), %xmm0     # 0x14004
movl %edx, %esi
callq 0x98ec
movq 0x144eb(%rip), %r15     # 0x1d2a0
orl $0x1, 0x24(%r15)
movl %ebp, %ebx
imull %ebx, %ebx
leaq (,%rbx,4), %rdi
callq 0x22c0
testq %rax, %rax
je 0x95e1
movq %rax, %r14
xorl %eax, %eax
cmpq %rax, %rbx
je 0x8df2
movq (%r15), %rcx
movss (%rcx,%rax,4), %xmm0
movss %xmm0, (%r14,%rax,4)
incq %rax
jmp 0x8dda
leaq 0x6073(%rip), %rcx      # 0xee6c
pushq $0x4
popq %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2450
cvtsi2sd %ebp, %xmm0
movsd 0xb20d(%rip), %xmm1     # 0x14020
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
cltq
movss (%r14,%rax,4), %xmm0
movq 0x14472(%rip), %rax     # 0x1d2a0
movss %xmm0, 0x1c(%rax)
movq %r14, %rdi
callq 0x2250
callq 0xed40
mulsd 0xb1e0(%rip), %xmm0     # 0x14028
addsd 0xb1e0(%rip), %xmm0     # 0x14030
cvttsd2si %xmm0, %eax
movq 0x14445(%rip), %r14     # 0x1d2a0
andl $0x0, 0x18(%r14)
movl %eax, 0x34(%rsp)
movslq %eax, %rdi
pushq $0x30
popq %rsi
callq 0x2110
movq %rax, 0x10(%r14)
testq %rax, %rax
je 0x95ea
pushq $0x8
popq %rsi
movq %rbx, %rdi
callq 0x2110
movq %rax, 0x8(%r14)
testq %rax, %rax
je 0x95f6
callq 0xee88
xorl %r14d, %r14d
movl 0x34(%rsp), %ecx
testl %ecx, %ecx
movl $0x0, %eax
cmovgl %ecx, %eax
movq %rax, 0x50(%rsp)
xorl %ecx, %ecx
movq 0x143e8(%rip), %rax     # 0x1d2a0
cmpq 0x50(%rsp), %rcx
je 0x90d5
movq %r14, 0x20(%rsp)
movl 0x20(%rax), %r15d
movq 0x10(%rax), %rbx
movq %rcx, 0x38(%rsp)
imulq $0x30, %rcx, %r14
callq 0xed40
mulsd 0xb152(%rip), %xmm0     # 0x14038
addsd 0xb142(%rip), %xmm0     # 0x14030
cvttsd2si %xmm0, %eax
movl %eax, 0x28(%rbx,%r14)
movslq %eax, %rdi
pushq $0x8
popq %rsi
callq 0x2110
movq %rax, 0x20(%rbx,%r14)
testq %rax, %rax
je 0x95b4
movq %rbx, 0x60(%rsp)
addq %rbx, %r14
movq %r14, 0x58(%rsp)
cvtsi2sd %r15d, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %r12d, %r12d
movl %r15d, 0x30(%rsp)
movl %r12d, %ebx
callq 0xed2c
mulsd 0x8(%rsp), %xmm0
cvttsd2si %xmm0, %ebp
callq 0xed2c
leal 0x1(%rbx), %r12d
testl %ebp, %ebp
js 0x8f77
mulsd 0x8(%rsp), %xmm0
cvttsd2si %xmm0, %r14d
cmpl %r14d, %r15d
jbe 0x8f77
cmpl %ebp, %r15d
jle 0x8f77
testl %r14d, %r14d
js 0x8f77
movl %ebp, %edi
movl %r14d, %esi
callq 0x9c4a
testl %eax, %eax
je 0x8f84
cmpl $0x270f, %ebx           # imm = 0x270F
jb 0x8f30
jmp 0x956a
movl %r12d, 0x18(%rsp)
movq %rbx, 0x10(%rsp)
movq 0x1430b(%rip), %rbx     # 0x1d2a0
movslq 0x18(%rbx), %rax
testl %eax, %eax
movl $0x0, %r13d
movq %rax, 0x40(%rsp)
cmovgl %eax, %r13d
pushq $0x20
popq %r12
xorl %r15d, %r15d
cmpq %r15, %r13
je 0x9001
movq 0x10(%rbx), %rax
movq (%rax,%r12), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x8ff5
callq 0x98c7
movq %rax, %rcx
shrq $0x20, %rcx
subl %ebp, %eax
imull %eax, %eax
subl %r14d, %ecx
imull %ecx, %ecx
addl %eax, %ecx
cvtsi2sd %ecx, %xmm0
sqrtsd %xmm0, %xmm0
movsd 0xb041(%rip), %xmm1     # 0x14030
ucomisd %xmm0, %xmm1
jae 0x8ffe
incq %r15
addq $0x30, %r12
jmp 0x8fb1
movq %r15, %r13
cmpq 0x40(%rsp), %r13
movl 0x30(%rsp), %r15d
movq 0x10(%rsp), %rax
movl 0x18(%rsp), %r12d
jge 0x9022
cmpl $0x270f, %eax           # imm = 0x270F
jb 0x8f30
cmpl $0x270f, %eax           # imm = 0x270F
jae 0x956a
imull %r14d, %r15d
addl %ebp, %r15d
movslq %r15d, %rax
shlq $0x3, %rax
addq 0x8(%rbx), %rax
movq 0x58(%rsp), %rdx
movq 0x20(%rdx), %rcx
movq %rax, (%rcx)
movq 0x20(%rdx), %rax
movq (%rax), %rax
cvtsi2ss %ebp, %xmm1
movl $0x1, (%rax)
cvtsi2ss %r14d, %xmm2
movss 0xaf9f(%rip), %xmm0     # 0x14008
mulss %xmm0, %xmm1
movss %xmm1, 0x8(%rsp)
mulss %xmm0, %xmm2
movss %xmm2, 0x10(%rsp)
movq 0x20(%rsp), %r14
movq 0x60(%rsp), %r15
addq %r14, %r15
xorl %ebx, %ebx
cmpq $0x8, %rbx
je 0x90ba
cvtsi2ss %ebx, %xmm2
mulss 0xaf6e(%rip), %xmm2     # 0x1400c
movss 0x8(%rsp), %xmm0
movss 0x10(%rsp), %xmm1
callq 0xef70
movss %xmm0, (%r15,%rbx,4)
incq %rbx
jmp 0x908c
movq 0x141df(%rip), %rax     # 0x1d2a0
incl 0x18(%rax)
movq 0x38(%rsp), %rcx
incq %rcx
addq $0x30, %r14
jmp 0x8eb1
movl 0x34(%rsp), %ecx
cmpl %ecx, 0x18(%rax)
jne 0x9602
xorl %ecx, %ecx
cmpq 0x50(%rsp), %rcx
je 0x9557
movq 0x141aa(%rip), %rax     # 0x1d2a0
movq 0x10(%rax), %r14
movq %rcx, 0x38(%rsp)
imulq $0x30, %rcx, %rbx
movl 0x20(%rax), %eax
movl %eax, 0x30(%rsp)
movl 0x28(%r14,%rbx), %eax
leal (%rax,%rax,4), %eax
addl $0x5, %eax
movslq %eax, %r12
movq %r12, %rdi
pushq $0x8
popq %rsi
callq 0x2110
testq %rax, %rax
je 0x95bd
movq %rax, %rbp
andq $0x0, 0x28(%rsp)
addq %rbx, %r14
movq %r14, 0x40(%rsp)
movq 0x20(%r14), %rax
movq (%rax), %rdi
callq 0x98c7
movq %rax, %r14
leal 0x1(%rax), %r8d
movq %rax, %rbx
movabsq $-0x100000000, %r15     # imm = 0xFFFFFFFF00000000
andq %r15, %rbx
orq %rbx, %r8
movq %rbp, %rdi
xorl %esi, %esi
leaq 0x28(%rsp), %r13
movq %r13, %rdx
movq %r12, %rcx
callq 0xa516
movq %r12, 0x10(%rsp)
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
leal (%r12,%r14), %r8d
orq %rbx, %r8
movq %rbp, %rdi
xorl %esi, %esi
movq %r13, %rdx
movq 0x10(%rsp), %rcx
callq 0xa516
leaq (%r12,%rbx), %rax
movq 0x10(%rsp), %r12
movl %r14d, %r14d
leaq (%r14,%rax), %r8
incq %r8
movq %rbp, %rdi
xorl %esi, %esi
movq %r13, %rdx
movq %r12, %rcx
callq 0xa516
addq %r15, %rbx
orq %r14, %rbx
movq %rbp, 0x18(%rsp)
movq %rbp, %rdi
xorl %esi, %esi
movq %r13, %rdx
movq %r12, %rcx
movq %rbx, %r8
callq 0xa516
xorl %r13d, %r13d
xorl %r15d, %r15d
movq 0x40(%rsp), %rdx
movslq 0x28(%rdx), %rax
cmpq %rax, %r15
jae 0x9540
movq 0x28(%rsp), %r14
cmpq %r14, %r13
je 0x957e
movq 0x18(%rsp), %rax
movl (%rax,%r13,8), %esi
movl 0x4(%rax,%r13,8), %ebx
movq 0x14088(%rip), %rax     # 0x1d2a0
movl %ebx, %ecx
imull 0x30(%rsp), %ecx
movq %rsi, 0x8(%rsp)
addl %esi, %ecx
movslq %ecx, %rcx
shlq $0x3, %rcx
addq 0x8(%rax), %rcx
movq 0x20(%rdx), %rax
movq %rcx, (%rax,%r15,8)
movq 0x20(%rdx), %rax
movq %r15, %rbp
movq (%rax,%r15,8), %r15
callq 0xed2c
leaq 0xc130(%rip), %rax      # 0x15380
pushq $-0x1
popq %rcx
cvtss2sd (%rax), %xmm1
incl %ecx
addq $0x4, %rax
ucomisd %xmm1, %xmm0
ja 0x9253
incq %r13
movq %r13, %rax
xorl %edx, %edx
divq %r12
movl %ecx, (%r15)
leal -0x2(%rcx), %eax
cmpl $0x4, %eax
jae 0x959d
movq %rdx, %r13
movq 0x8(%rsp), %rdi
cmpl $-0x1, %edi
movq %rbp, %r15
jl 0x9464
movq 0x14008(%rip), %rax     # 0x1d2a0
movq %rax, 0x20(%rsp)
movl 0x20(%rax), %r12d
testl %ebx, %ebx
js 0x934e
leal 0x1(%rdi), %r15d
cmpl %r12d, %r15d
jge 0x934e
cmpl %r12d, %ebx
jae 0x934e
movl %r15d, %edi
movl %ebx, %esi
callq 0x9c4a
testl %eax, %eax
je 0x92d4
movq 0x8(%rsp), %rdi
jmp 0x934e
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rax
movl %r12d, %ecx
imull %ebx, %ecx
addl %r15d, %ecx
cmpl $0x0, (%rax,%rcx,8)
movq 0x8(%rsp), %rdi
jne 0x934e
movq 0x18(%rsp), %rdi
movq %r13, %rsi
movq %r14, %rdx
movq 0x10(%rsp), %rcx
movl %r15d, %r8d
movl %ebx, %r9d
callq 0xa551
movq 0x8(%rsp), %rdi
testl %eax, %eax
jne 0x934e
movq %rbx, %rax
shlq $0x20, %rax
movl %r15d, %r8d
orq %rax, %r8
movq 0x18(%rsp), %rdi
movq %r13, %rsi
leaq 0x28(%rsp), %rdx
movq 0x10(%rsp), %rcx
callq 0xa516
movq 0x8(%rsp), %rdi
movq 0x13f5b(%rip), %rax     # 0x1d2a0
movq %rax, 0x20(%rsp)
movl 0x20(%rax), %r12d
testl %edi, %edi
jle 0x9398
testl %ebx, %ebx
movq %rbp, %r15
js 0x93a3
cmpl %r12d, %edi
jg 0x93a3
cmpl %r12d, %ebx
jae 0x93a3
leal -0x1(%rdi), %r14d
movl %r14d, %edi
movl %ebx, %esi
callq 0x9c4a
testl %eax, %eax
jne 0x9391
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rax
movl %r12d, %ecx
imull %ebx, %ecx
addl %r14d, %ecx
cmpl $0x0, (%rax,%rcx,8)
je 0x94d8
movq 0x8(%rsp), %rdi
jmp 0x93a3
testl %edi, %edi
movq %rbp, %r15
js 0x9464
cmpl $-0x1, %ebx
jl 0x93e4
cmpl %r12d, %edi
jge 0x93e4
leal 0x1(%rbx), %r14d
cmpl %r12d, %r14d
jae 0x93e4
movl %r14d, %esi
movq %rdi, %rbp
callq 0x9c4a
testl %eax, %eax
jne 0x93e1
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rax
movl %r12d, %ecx
imull %r14d, %ecx
addl %ebp, %ecx
cmpl $0x0, (%rax,%rcx,8)
je 0x9471
movq %rbp, %rdi
testl %ebx, %ebx
jle 0x9464
cmpl %r12d, %edi
jge 0x9464
decl %ebx
cmpl %r12d, %ebx
jae 0x9464
movl %ebx, %esi
callq 0x9c4a
movq 0x8(%rsp), %rcx
testl %eax, %eax
jne 0x9464
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rax
imull %ebx, %r12d
addl %ecx, %r12d
cmpl $0x0, (%rax,%r12,8)
jne 0x9464
movq 0x28(%rsp), %rdx
movq 0x18(%rsp), %rdi
movq %r13, %rsi
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %r8
movl %ebx, %r9d
callq 0xa551
movq 0x8(%rsp), %rcx
testl %eax, %eax
jne 0x9464
shlq $0x20, %rbx
orq %rcx, %rbx
movq 0x18(%rsp), %rdi
movq %r13, %rsi
leaq 0x28(%rsp), %rdx
movq 0x10(%rsp), %rcx
movq %rbx, %r8
callq 0xa516
incq %r15
movq 0x10(%rsp), %r12
jmp 0x91e3
movq 0x28(%rsp), %rdx
movq 0x18(%rsp), %rdi
movq %r13, %rsi
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %r8
movl %r14d, %r9d
callq 0xa551
movq 0x8(%rsp), %rdi
testl %eax, %eax
jne 0x93e4
shlq $0x20, %r14
orq %rdi, %r14
movq 0x18(%rsp), %rdi
movq %r13, %rsi
leaq 0x28(%rsp), %rdx
movq 0x10(%rsp), %rcx
movq %r14, %r8
callq 0xa516
movq 0x8(%rsp), %rdi
movq 0x13dd6(%rip), %rax     # 0x1d2a0
movq %rax, 0x20(%rsp)
movl 0x20(%rax), %r12d
jmp 0x93e4
movq 0x28(%rsp), %rdx
movq 0x18(%rsp), %rdi
movq %r13, %rsi
movq 0x10(%rsp), %rcx
movl %r14d, %r8d
movl %ebx, %r9d
callq 0xa551
testl %eax, %eax
movq 0x8(%rsp), %rdi
jne 0x93a3
movq %rbx, %rax
shlq $0x20, %rax
movl %r14d, %r8d
orq %rax, %r8
movq 0x18(%rsp), %rdi
movq %r13, %rsi
leaq 0x28(%rsp), %rdx
movq 0x10(%rsp), %rcx
callq 0xa516
movq 0x8(%rsp), %rdi
movq 0x13d6e(%rip), %rax     # 0x1d2a0
movq %rax, 0x20(%rsp)
movl 0x20(%rax), %r12d
jmp 0x93a3
movq 0x18(%rsp), %rdi
callq 0x2250
movq 0x38(%rsp), %rcx
incq %rcx
jmp 0x90e4
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xef59
leaq 0xbafd(%rip), %rdi      # 0x1506e
callq 0x2190
pushq $0x1
popq %rdi
callq 0x2270
leaq 0xbbe6(%rip), %rdi      # 0x1516b
leaq 0xb0d3(%rip), %rsi      # 0x1465f
leaq 0xbbe6(%rip), %rcx      # 0x15179
movl $0xfd, %edx
callq 0x2100
cmpl $0x1, %ecx
jne 0x95ab
leaq 0xbc14(%rip), %rdi      # 0x151bd
jmp 0x9571
leaq 0xbc45(%rip), %rdi      # 0x151f7
jmp 0x9571
leaq 0xba41(%rip), %rdi      # 0x14ffc
jmp 0x9571
leaq 0xbb02(%rip), %rdi      # 0x150c6
jmp 0x9571
leaq 0xb4da(%rip), %rdi      # 0x14aa7
jmp 0x9571
leaq 0xb51b(%rip), %rdi      # 0x14af1
jmp 0x9571
leaq 0xb55e(%rip), %rdi      # 0x14b3d
jmp 0x9571
leaq 0xb5e6(%rip), %rdi      # 0x14bce
jmp 0x9571
leaq 0xb632(%rip), %rdi      # 0x14c23
jmp 0x9571
leaq 0xb698(%rip), %rdi      # 0x14c95
jmp 0x9571
leaq 0xb6fd(%rip), %rdi      # 0x14d06
leaq 0xb04f(%rip), %rsi      # 0x1465f
leaq 0xb714(%rip), %rcx      # 0x14d2b
movl $0x16e, %edx            # imm = 0x16E
callq 0x2100