// File: /workspace/github_c_makefile_star2_O3/Ziddykins[P]Dawn/src/map.c
// Function: diamond_square
void diamond_square(float *heightmap, int dim, float sigma, int level) {
    if (level < 1) return;

    // diamonds
    for (int i = level; i < dim; i += level) {
        for (int j = level; j < dim; j += level) {
            float a = heightmap[(i-level)*dim + j-level];
            float b = heightmap[i*dim + j-level];
            float c = heightmap[(i-level)*dim + j];
            float d = heightmap[i*dim+j];
            heightmap[(i-level/2)*dim + j-level/2] = (a + b + c + d) / 4 + (float)gaussrand() * sigma;
        }
    }
    // squares
    for (int i = 2 * level; i < dim; i += level) {
        for (int j = 2 * level; j < dim; j += level) {
            float a = heightmap[(i-level)*dim + j-level];
            float b = heightmap[i*dim + j-level];
            float c = heightmap[(i-level)*dim + j];
            float e = heightmap[(i-level/2)*dim + j-level/2];

            heightmap[(i-level)*dim + (j-level/2)] = (a + c + e + heightmap[(i-3*level/2)*dim + (j-level/2)]) / 4 + (float)gaussrand() * sigma;
            heightmap[(i-level/2)*dim + j-level] = (a + b + e + heightmap[(i-level/2)*dim + (j-3*level/2)]) / 4 + (float)gaussrand() * sigma;
        }
    }
    diamond_square(heightmap, dim, sigma / 2, level / 2);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movl %esi, 0x4(%rsp)
testl %edx, %edx
jle 0xab6e
movslq 0x4(%rsp), %rax
movq %rax, 0x58(%rsp)
leaq 0x4(,%rax,4), %rcx
movq %rcx, 0x68(%rsp)
leaq (,%rax,4), %rcx
movq %rcx, 0x40(%rsp)
leal 0x1(,%rax,2), %eax
movl %eax, 0x3c(%rsp)
movss 0xa82a(%rip), %xmm1     # 0x15040
movq %rdi, 0x30(%rsp)
movss %xmm0, 0xc(%rsp)
movl %edx, %r9d
movq %rdx, 0x10(%rsp)
cmpl 0x4(%rsp), %edx
movq %r9, 0x60(%rsp)
jge 0xa91d
movq 0x10(%rsp), %rcx
movl %ecx, %eax
shrl %eax
movq 0x68(%rsp), %rbx
imulq %r9, %rbx
addq %rdi, %rbx
movq 0x40(%rsp), %rdx
imulq %r9, %rdx
movq %rdx, 0x20(%rsp)
leaq (%rdi,%rdx), %r13
leaq (%rdi,%r9,4), %rbp
movl %ecx, %r15d
subl %eax, %r15d
movl 0x4(%rsp), %edx
imull %edx, %r15d
addl %ecx, %r15d
subl %eax, %r15d
movl %edx, %eax
imull %ecx, %eax
movq %rax, 0x18(%rsp)
movq %rdi, %r14
movq %r9, %rdx
movq %rdx, 0x28(%rsp)
xorl %r12d, %r12d
movss (%r14,%r12,4), %xmm0
addss (%r13,%r12,4), %xmm0
addss (%rbp,%r12,4), %xmm0
addss (%rbx,%r12,4), %xmm0
mulss %xmm1, %xmm0
movss %xmm0, 0x8(%rsp)
callq 0xfffc
movq 0x60(%rsp), %r9
movss 0xa77b(%rip), %xmm1     # 0x15040
movq 0x58(%rsp), %rcx
movq 0x30(%rsp), %rdi
cvtsd2ss %xmm0, %xmm0
mulss 0xc(%rsp), %xmm0
addss 0x8(%rsp), %xmm0
leal (%r15,%r12), %eax
cltq
movss %xmm0, (%rdi,%rax,4)
addq %r9, %r12
leaq (%r9,%r12), %rax
cmpq %rcx, %rax
jl 0xa88f
movq 0x28(%rsp), %rdx
addq %r9, %rdx
movq 0x20(%rsp), %rax
addq %rax, %rbx
addq %rax, %r13
addq %rax, %rbp
addq %rax, %r14
addq 0x18(%rsp), %r15
cmpq %rcx, %rdx
jl 0xa887
movq 0x10(%rsp), %rax
leal (%rax,%rax), %r11d
shrl %eax
movq %rax, 0x48(%rsp)
cmpl 0x4(%rsp), %r11d
jge 0xab50
movq 0x48(%rsp), %r10
movq 0x10(%rsp), %r14
leal (%r10,%r14), %eax
movl %r11d, %ebx
movl %r10d, %ecx
movq %rbx, %rdx
subq %r9, %rdx
movq 0x40(%rsp), %r8
imulq %r8, %rdx
leaq (%rdx,%rbx,4), %r13
leaq (,%rcx,4), %rdx
movq %r13, %r15
subq %rdx, %r15
addq %rdi, %r15
movq %r8, %rsi
imulq %r9, %rsi
movq %rsi, 0x78(%rsp)
movq %rbx, %rsi
subq %rax, %rsi
imulq %r8, %rsi
leaq (%rsi,%rbx,4), %rbp
movq %rbx, %rsi
subq %rdx, %rbp
addq %rdi, %rbp
movq %rbx, %rax
subq %rcx, %rax
imulq %r8, %rax
leaq (%rax,%rbx,4), %r12
leaq (,%r9,4), %rax
movq %r12, %r9
subq %rax, %r9
addq %rdi, %r9
subq %rdx, %r12
addq %rdi, %r12
leaq (%rdi,%r13), %rbx
subq %rax, %r13
addq %rdi, %r13
subl %r10d, %r11d
movl 0x4(%rsp), %eax
imull %eax, %r11d
addl %r14d, %r11d
subl %r10d, %r11d
imull %r14d, %eax
movq %rax, 0x70(%rsp)
movl 0x3c(%rsp), %eax
movl %eax, %r10d
imull %r14d, %r10d
movq %rsi, %rdx
movq %r15, 0x50(%rsp)
movq %r11, 0x18(%rsp)
movq %rdx, 0x80(%rsp)
xorl %r14d, %r14d
movq %r13, 0x90(%rsp)
movq %r12, 0x88(%rsp)
movss (%r13,%r14,4), %xmm0
leal (%r10,%r14), %eax
movss (%r12,%r14,4), %xmm2
movss %xmm2, 0x28(%rsp)
movss (%rbx,%r14,4), %xmm3
addss %xmm0, %xmm3
addss %xmm2, %xmm3
addss (%rbp,%r14,4), %xmm3
cltq
mulss %xmm1, %xmm3
movss %xmm3, 0x20(%rsp)
addss (%rdi,%rax,4), %xmm0
movss %xmm0, 0x8(%rsp)
movq %rsi, %r13
movq %rbx, %r15
movq %r9, %rbx
movq %r10, %r12
callq 0xfffc
cvtsd2ss %xmm0, %xmm0
mulss 0xc(%rsp), %xmm0
addss 0x20(%rsp), %xmm0
movq 0x50(%rsp), %rax
movss %xmm0, (%rax,%r14,4)
movss 0x8(%rsp), %xmm0
addss 0x28(%rsp), %xmm0
movq 0x18(%rsp), %rax
addl %r14d, %eax
cltq
movq 0x30(%rsp), %rcx
addss (%rcx,%rax,4), %xmm0
mulss 0xa59a(%rip), %xmm0     # 0x15040
movss %xmm0, 0x8(%rsp)
callq 0xfffc
movq %r12, %r10
movq %rbx, %r9
movq %r15, %rbx
movq 0x88(%rsp), %r12
movq %r13, %rsi
movq 0x90(%rsp), %r13
movq 0x60(%rsp), %rdx
movss 0xa566(%rip), %xmm1     # 0x15040
movq 0x58(%rsp), %rcx
movq 0x30(%rsp), %rdi
cvtsd2ss %xmm0, %xmm0
mulss 0xc(%rsp), %xmm0
addss 0x8(%rsp), %xmm0
movss %xmm0, (%r9,%r14,4)
addq %rdx, %r14
leaq (%rsi,%r14), %rax
cmpq %rcx, %rax
jl 0xaa0f
movq 0x80(%rsp), %rax
addq %rdx, %rax
movq %rax, %rdx
movq 0x78(%rsp), %rax
movq 0x50(%rsp), %r14
addq %rax, %r14
addq %rax, %rbp
addq %rax, %r9
addq %rax, %r12
addq %rax, %rbx
addq %rax, %r13
movq 0x70(%rsp), %rax
movq 0x18(%rsp), %r11
addq %rax, %r11
addq %rax, %r10
cmpq %rcx, %rdx
movq %r14, %r15
jl 0xa9ea
movss 0xc(%rsp), %xmm0
mulss 0xa4e6(%rip), %xmm0     # 0x15044
cmpl $0x2, 0x10(%rsp)
movq 0x48(%rsp), %rdx
jae 0xa81b
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq