// File: lotuskip[P]yuxtapa/server/map.cpp
// Function: Map::gen_miniview(char*) const
void Map::gen_miniview(char *target) const
{
	short x, y, ax, ay, F;
	map<Tile, short> amounts;
	map<Tile, short>::iterator it;
	Tile t;
	
	// sample F*F map tiles for each miniview tile
	F = mapsize/VIEWSIZE + 1;
	while(VIEWSIZE*F >= mapsize)
		--F;

	for(y = 0; y < VIEWSIZE; ++y)
	{
		for(x = 0; x < VIEWSIZE; ++x)
		{
			// collect tile amounts in this area:
			for(ay = y*F; ay < (y+1)*F; ++ay)
			{
				for(ax = x*F; ax < (x+1)*F; ++ax)
					amounts[data[ay][ax]]++;
			}
			// Do some reduction. Think of this as a "crude sinc filter"
			for(it = amounts.begin(); it != amounts.end(); ++it)
			{
				// The reference value here is the fact that there are a total
				// of F*F tiles!
				if(it->second < 5*F*F/24
					&& (t = subsample_tile(it->first)) != it->first)
				{
					amounts[t] += it->second;
					amounts.erase(it);
					it = amounts.begin(); // start over!
				}
			}
			// now just represent this area by the most common tile:
			t = max_element(amounts.begin(), amounts.end(), pred)->first;
			*(target++) = t.cpair;
			*(target++) = t.symbol;
			amounts.clear();
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xc0(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x2ee50
leaq -0x58(%rbp), %rdi
callq 0x2ee70
movq -0xc0(%rbp), %rax
movswl (%rax), %eax
movl $0x17, %ecx
cltd
idivl %ecx
addl $0x1, %eax
movw %ax, -0x1a(%rbp)
movq -0xc0(%rbp), %rcx
movswl -0x1a(%rbp), %eax
imull $0x17, %eax, %eax
movswl (%rcx), %ecx
cmpl %ecx, %eax
jl 0x2bd4c
movw -0x1a(%rbp), %ax
addw $-0x1, %ax
movw %ax, -0x1a(%rbp)
jmp 0x2bd29
movw $0x0, -0x14(%rbp)
movswl -0x14(%rbp), %eax
cmpl $0x17, %eax
jge 0x2c077
movw $0x0, -0x12(%rbp)
movswl -0x12(%rbp), %eax
cmpl $0x17, %eax
jge 0x2c064
movswl -0x14(%rbp), %eax
movswl -0x1a(%rbp), %ecx
imull %ecx, %eax
movw %ax, -0x18(%rbp)
movswl -0x18(%rbp), %eax
movswl -0x14(%rbp), %ecx
addl $0x1, %ecx
movswl -0x1a(%rbp), %edx
imull %edx, %ecx
cmpl %ecx, %eax
jge 0x2be43
movswl -0x12(%rbp), %eax
movswl -0x1a(%rbp), %ecx
imull %ecx, %eax
movw %ax, -0x16(%rbp)
movswl -0x16(%rbp), %eax
movswl -0x12(%rbp), %ecx
addl $0x1, %ecx
movswl -0x1a(%rbp), %edx
imull %edx, %ecx
cmpl %ecx, %eax
jge 0x2be30
movq -0xc0(%rbp), %rdi
addq $0x8, %rdi
movswq -0x18(%rbp), %rsi
callq 0x2ef70
movq %rax, %rdi
movswq -0x16(%rbp), %rsi
callq 0x2ef90
movq %rax, %rsi
leaq -0x50(%rbp), %rdi
callq 0x2ee90
movq %rax, -0xc8(%rbp)
jmp 0x2bdf7
movq -0xc8(%rbp), %rax
movw (%rax), %cx
addw $0x1, %cx
movw %cx, (%rax)
movw -0x16(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x16(%rbp)
jmp 0x2bdaa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x2f140
jmp 0x2c089
jmp 0x2be32
movw -0x18(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x18(%rbp)
jmp 0x2bd81
leaq -0x50(%rbp), %rdi
callq 0x2efb0
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x58(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x2f010
movq %rax, -0x80(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x2efe0
testb $0x1, %al
jne 0x2be7b
jmp 0x2bfb6
leaq -0x58(%rbp), %rdi
callq 0x2f040
movswl 0x4(%rax), %ecx
movswl -0x1a(%rbp), %eax
imull $0x5, %eax, %eax
movswl -0x1a(%rbp), %edx
imull %edx, %eax
movl $0x18, %esi
cltd
idivl %esi
movl %eax, %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, -0xc9(%rbp)
jge 0x2bf0c
leaq -0x58(%rbp), %rdi
callq 0x2f040
movq -0xc0(%rbp), %rdi
movq %rax, %rsi
callq 0x2bc10
movl %eax, -0xd0(%rbp)
jmp 0x2becc
movl -0xd0(%rbp), %eax
movl %eax, -0x84(%rbp)
movl -0x84(%rbp), %eax
movl %eax, -0x5c(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x2f040
movq %rax, %rsi
leaq -0x5c(%rbp), %rdi
callq 0x2c0a0
movb %al, -0xd1(%rbp)
jmp 0x2befe
movb -0xd1(%rbp), %al
movb %al, -0xc9(%rbp)
jmp 0x2bf0c
movb -0xc9(%rbp), %al
testb $0x1, %al
jne 0x2bf1b
jmp 0x2bfa6
leaq -0x58(%rbp), %rdi
callq 0x2f040
movswl 0x4(%rax), %eax
movl %eax, -0xe4(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x5c(%rbp), %rsi
callq 0x2ee90
movq %rax, -0xe0(%rbp)
jmp 0x2bf44
movq -0xe0(%rbp), %rax
movl -0xe4(%rbp), %edx
movzwl (%rax), %ecx
addl %edx, %ecx
movw %cx, (%rax)
movq -0x58(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x2f060
movq %rax, -0xf0(%rbp)
jmp 0x2bf7d
movq -0xf0(%rbp), %rax
movq %rax, -0x98(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x2efb0
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
movq %rax, -0x58(%rbp)
jmp 0x2bfa8
leaq -0x58(%rbp), %rdi
callq 0x2f0a0
jmp 0x2be58
leaq -0x50(%rbp), %rdi
movq %rdi, -0x100(%rbp)
callq 0x2efb0
movq -0x100(%rbp), %rdi
movq %rax, -0xb0(%rbp)
callq 0x2f010
movq %rax, -0xb8(%rbp)
movq -0xb0(%rbp), %rdi
movq -0xb8(%rbp), %rsi
leaq 0xfb(%rip), %rdx        # 0x2c0f0
callq 0x2f0d0
movq %rax, -0xf8(%rbp)
jmp 0x2c003
movq -0xf8(%rbp), %rax
movq %rax, -0xa8(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x2f040
movl (%rax), %eax
movl %eax, -0x5c(%rbp)
movb -0x59(%rbp), %cl
movq -0x10(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x10(%rbp)
movb %cl, (%rax)
movb -0x5a(%rbp), %cl
movq -0x10(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x10(%rbp)
movb %cl, (%rax)
leaq -0x50(%rbp), %rdi
callq 0x2f120
movw -0x12(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x12(%rbp)
jmp 0x2bd65
jmp 0x2c066
movw -0x14(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x14(%rbp)
jmp 0x2bd52
leaq -0x50(%rbp), %rdi
callq 0x2f140
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
movq -0x68(%rbp), %rdi
callq 0x5920
nopw %cs:(%rax,%rax)