// File: /workspace/github_c_makefile_star2_O2/jonof[P]jftekwar/jfbuild/src/build.c
// Function: gettile
int gettile(int tilenum)
{
	char snotbuf[80];
	int i, j, otilenum, topleft, gap, temp, templong, ch;
	int xtiles, ytiles, tottiles;

	if (tilenum < 0) tilenum = 0;

	xtiles = (xdim>>6); ytiles = (ydim>>6); tottiles = xtiles*ytiles;
	otilenum = tilenum;

	keystatus[0x2f] = 0;
	for(i=0;i<MAXTILES;i++)
	{
		localartfreq[i] = 0;
		localartlookup[i] = i;
	}
	if ((searchstat == 1) || (searchstat == 2))
		for(i=0;i<numsectors;i++)
		{
			localartfreq[sector[i].ceilingpicnum]++;
			localartfreq[sector[i].floorpicnum]++;
		}
	if (searchstat == 0)
		for(i=0;i<numwalls;i++)
			localartfreq[wall[i].picnum]++;
	if (searchstat == 4)
		for(i=0;i<numwalls;i++)
			localartfreq[wall[i].overpicnum]++;
	if (searchstat == 3)
		for(i=0;i<MAXSPRITES;i++)
			if (sprite[i].statnum < MAXSTATUS)
				localartfreq[sprite[i].picnum]++;
	gap = (MAXTILES>>1);
	do
	{
		for(i=0;i<MAXTILES-gap;i++)
		{
			temp = i;
			while ((localartfreq[temp] < localartfreq[temp+gap]) && (temp >= 0))
			{
				templong = localartfreq[temp];
				localartfreq[temp] = localartfreq[temp+gap];
				localartfreq[temp+gap] = templong;
				templong = localartlookup[temp];
				localartlookup[temp] = localartlookup[temp+gap];
				localartlookup[temp+gap] = templong;

				if (tilenum == temp)
					tilenum = temp+gap;
				else if (tilenum == temp+gap)
					tilenum = temp;

				temp -= gap;
			}
		}
		gap >>= 1;
	}
	while (gap > 0);
	localartlookupnum = 0;
	while (localartfreq[localartlookupnum] > 0)
		localartlookupnum++;

	if (localartfreq[0] == 0)
	{
		tilenum = otilenum;
		localartlookupnum = MAXTILES;
		for(i=0;i<MAXTILES;i++)
			localartlookup[i] = i;
	}

	topleft = ((tilenum/(xtiles<<gettilezoom))*(xtiles<<gettilezoom))-(xtiles<<gettilezoom);
	if (topleft < 0) topleft = 0;
	if (topleft > MAXTILES-(tottiles<<(gettilezoom<<1))) topleft = MAXTILES-(tottiles<<(gettilezoom<<1));
	while ((keystatus[0x1c]|keystatus[1]) == 0)
	{
		drawtilescreen(topleft,tilenum);
		OSD_Draw();
		showframe();

		if (handleevents()) {
			if (quitevent) quitevent = 0;
		}

		synctics = totalclock-lockclock;
		lockclock += synctics;

		if ((keystatus[0x37] > 0) && (gettilezoom < 2))
		{
			gettilezoom++;
			topleft = ((tilenum/(xtiles<<gettilezoom))*(xtiles<<gettilezoom))-(xtiles<<gettilezoom);
			if (topleft < 0) topleft = 0;
			if (topleft > MAXTILES-(tottiles<<(gettilezoom<<1))) topleft = MAXTILES-(tottiles<<(gettilezoom<<1));
			keystatus[0x37] = 0;
		}
		if ((keystatus[0xb5] > 0) && (gettilezoom > 0))
		{
			gettilezoom--;
			topleft = ((tilenum/(xtiles<<gettilezoom))*(xtiles<<gettilezoom))-(xtiles<<gettilezoom);
			if (topleft < 0) topleft = 0;
			if (topleft > MAXTILES-(tottiles<<(gettilezoom<<1))) topleft = MAXTILES-(tottiles<<(gettilezoom<<1));
			keystatus[0xb5] = 0;
		}
		if ((keystatus[0xcb] > 0) && (tilenum > 0))
			tilenum--, keystatus[0xcb] = 0;
		if ((keystatus[0xcd] > 0) && (tilenum < MAXTILES-1))
			tilenum++, keystatus[0xcd] = 0;
		if ((keystatus[0xc8] > 0) && (tilenum >= (xtiles<<gettilezoom)))
			tilenum-=(xtiles<<gettilezoom), keystatus[0xc8] = 0;
		if ((keystatus[0xd0] > 0) && (tilenum < MAXTILES-(xtiles<<gettilezoom)))
			tilenum+=(xtiles<<gettilezoom), keystatus[0xd0] = 0;
		if ((keystatus[0xc9] > 0) && (tilenum >= (xtiles<<gettilezoom)))
		{
			tilenum-=(tottiles<<(gettilezoom<<1));
			if (tilenum < 0) tilenum = 0;
			keystatus[0xc9] = 0;
		}
		if ((keystatus[0xd1] > 0) && (tilenum < MAXTILES-(xtiles<<gettilezoom)))
		{
			tilenum+=(tottiles<<(gettilezoom<<1));
			if (tilenum >= MAXTILES) tilenum = MAXTILES-1;
			keystatus[0xd1] = 0;
		}
		if (keystatus[0x2f] > 0)   //V
		{
			keystatus[0x2f] = 0;
			if (tilenum < localartlookupnum)
				tilenum = localartlookup[tilenum];
			else
				tilenum = 0;
			localartlookupnum = MAXTILES;
			for(i=0;i<MAXTILES;i++)
				localartlookup[i] = i;
		}
		if (keystatus[0x22] > 0)       //G (goto)
		{
			if (tilenum < localartlookupnum)         //Automatically press 'V'
				tilenum = localartlookup[tilenum];
			else
				tilenum = 0;
			localartlookupnum = MAXTILES;
			for(i=0;i<MAXTILES;i++)
				localartlookup[i] = i;

			keystatus[0x22] = 0;
			bflushchars();

			j = tilenum;
			while (keystatus[1] == 0)
			{
				if (handleevents()) {
					if (quitevent) quitevent = 0;
				}

				ch = bgetchar();

				//drawtilescreen(topleft,tilenum);
				Bsprintf(snotbuf,"Goto tile: %d_ ",j);
				printext256(0,0,whitecol,blackcol,snotbuf,0);
				showframe();

				if (ch >= '0' && ch <= '9') {
					i = (j*10)+(ch-'0');
					if (i < MAXTILES) j = i;
				} else if (ch == 8) {
					j /= 10;
				} else if (ch == 13) {
					tilenum = j;
					break;
				}
			}
			clearkeys();
		}
		while (tilenum < topleft) topleft -= (xtiles<<gettilezoom);
		while (tilenum >= topleft+(tottiles<<(gettilezoom<<1))) topleft += (xtiles<<gettilezoom);
		if (topleft < 0) topleft = 0;
		if (topleft > MAXTILES-(tottiles<<(gettilezoom<<1))) topleft = MAXTILES-(tottiles<<(gettilezoom<<1));
	}

	if (keystatus[0x1c] == 0)
	{
		tilenum = otilenum;
	}
	else
	{
		if (tilenum < localartlookupnum)
		{
			tilenum = localartlookup[tilenum];
			if ((tilesizx[tilenum] == 0) || (tilesizy[tilenum] == 0))
				tilenum = otilenum;
		}
		else
			tilenum = otilenum;
	}
	keystatus[0x1] = 0;
	keystatus[0x1c] = 0;
	return(tilenum);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
leaq 0x3eac24(%rip), %rax    # 0x400fec
movl (%rax), %eax
movl %eax, (%rsp)
leaq 0x3eac1c(%rip), %rax    # 0x400ff0
movl (%rax), %eax
sarl $0x6, %eax
movl %eax, 0x4(%rsp)
leaq 0x160eac(%rip), %rbp    # 0x177290
movb $0x0, 0x2f(%rbp)
xorl %ecx, %ecx
leaq 0x11b9af(%rip), %rax    # 0x131da0
leaq 0x1201b8(%rip), %r14    # 0x1365b0
xorl %edx, %edx
cmpq $0x2400, %rdx           # imm = 0x2400
je 0x16416
andw $0x0, (%rcx,%rax)
movw %dx, (%rcx,%r14)
incq %rdx
addq $0x2, %rcx
jmp 0x163fa
xorl %edx, %edx
testl %edi, %edi
cmovlel %edx, %edi
leaq 0x37b6ae(%rip), %rcx    # 0x391ad2
movzwl (%rcx), %ecx
leal -0x1(%rcx), %esi
cmpw $0x1, %si
ja 0x1646f
leaq 0x3b2b29(%rip), %rsi    # 0x3c8f60
movzwl (%rsi), %esi
testw %si, %si
cmovlel %edx, %esi
movzwl %si, %esi
imulq $0x28, %rsi, %rsi
leaq 0x226612(%rip), %r9     # 0x23ca60
cmpq %rdx, %rsi
je 0x1646f
movswq 0x10(%r9,%rdx), %r8
incw (%rax,%r8,2)
movswq 0x18(%r9,%rdx), %r8
incw (%rax,%r8,2)
addq $0x28, %rdx
jmp 0x1644e
sarl $0x6, (%rsp)
testl %ecx, %ecx
je 0x164e9
cmpl $0x4, %ecx
je 0x164b4
cmpl $0x3, %ecx
jne 0x1651e
pushq $0x1a
popq %rcx
leaq 0x25f821(%rip), %rdx    # 0x275cb0
cmpq $0xb001a, %rcx          # imm = 0xB001A
je 0x1651e
cmpw $0x3ff, (%rdx,%rcx)     # imm = 0x3FF
jg 0x164ae
movswq -0xc(%rdx,%rcx), %rsi
incw (%rax,%rsi,2)
addq $0x2c, %rcx
jmp 0x1648f
leaq 0x3f97ad(%rip), %rcx    # 0x40fc68
movzwl (%rcx), %edx
xorl %ecx, %ecx
testw %dx, %dx
cmovlel %ecx, %edx
movzwl %dx, %edx
shll $0x5, %edx
leaq 0x19b57d(%rip), %rsi    # 0x1b1a50
cmpq %rcx, %rdx
je 0x1651e
movswq 0x12(%rsi,%rcx), %r8
incw (%rax,%r8,2)
addq $0x20, %rcx
jmp 0x164d3
leaq 0x3f9778(%rip), %rcx    # 0x40fc68
movzwl (%rcx), %edx
xorl %ecx, %ecx
testw %dx, %dx
cmovlel %ecx, %edx
movzwl %dx, %edx
shll $0x5, %edx
leaq 0x19b548(%rip), %rsi    # 0x1b1a50
cmpq %rcx, %rdx
je 0x1651e
movswq 0x10(%rsi,%rcx), %r8
incw (%rax,%r8,2)
addq $0x20, %rcx
jmp 0x16508
movl 0x4(%rsp), %ecx
imull (%rsp), %ecx
movl %ecx, 0x4(%rsp)
movl $0x1200, %ecx           # imm = 0x1200
movl %edi, %r12d
movl $0x2400, %edx           # imm = 0x2400
subl %ecx, %edx
xorl %esi, %esi
cmpl %edx, %esi
je 0x16599
movl %esi, %ebx
testl %ebx, %ebx
js 0x16595
movslq %ebx, %r9
movzwl (%rax,%r9,2), %r10d
leal (%rcx,%rbx), %r8d
movslq %r8d, %r8
movzwl (%rax,%r8,2), %r11d
cmpw %r11w, %r10w
jge 0x16595
movw %r11w, (%rax,%r9,2)
movw %r10w, (%rax,%r8,2)
movzwl (%r14,%r9,2), %r10d
movzwl (%r14,%r8,2), %r11d
movw %r11w, (%r14,%r9,2)
movw %r10w, (%r14,%r8,2)
cmpl %r12d, %r8d
movl %r12d, %r9d
cmovel %ebx, %r9d
cmpl %r12d, %ebx
cmovel %r8d, %r9d
subl %ecx, %ebx
movl %r9d, %r12d
jmp 0x16541
incl %esi
jmp 0x1653b
movl %ecx, %edx
shrl %edx
cmpl $0x1, %ecx
movl %edx, %ecx
ja 0x16532
movw $0xffff, %cx            # imm = 0xFFFF
incl %ecx
movswq %cx, %rdx
cmpw $0x0, (%rax,%rdx,2)
jg 0x165a8
movw %cx, 0x1247f4(%rip)     # 0x13adb0
cmpw $0x0, 0x11b7dc(%rip)    # 0x131da0
jne 0x165f0
movw $0x2400, 0x1247e1(%rip) # imm = 0x2400
leaq 0x11ffda(%rip), %rax    # 0x1365b0
xorl %ecx, %ecx
cmpq $0x2400, %rcx           # imm = 0x2400
je 0x165ed
movw %cx, (%rax)
incq %rcx
addq $0x2, %rax
jmp 0x165d8
movl %edi, %r12d
movl %edi, 0xc(%rsp)
movl 0x86a0e(%rip), %ecx     # 0x9d008
movl (%rsp), %esi
shll %cl, %esi
movl %r12d, %eax
cltd
idivl %esi
addl %esi, %edx
movl %r12d, %eax
subl %edx, %eax
xorl %edx, %edx
testl %eax, %eax
cmovlel %edx, %eax
addl %ecx, %ecx
movl 0x4(%rsp), %edx
shll %cl, %edx
movl $0x2400, %r13d          # imm = 0x2400
subl %edx, %r13d
cmpl %r13d, %eax
cmovll %eax, %r13d
leaq 0x10(%rsp), %r15
movb 0x1c(%rbp), %al
movb 0x1(%rbp), %cl
orb %al, %cl
jne 0x16a11
movl %r13d, %edi
movl %r12d, %esi
callq 0x16dc9
callq 0x43e86
callq 0x63720
callq 0x63a71
testl %eax, %eax
je 0x16672
leaq 0x160a3d(%rip), %rax    # 0x1770a0
cmpb $0x0, (%rax)
je 0x16672
leaq 0x160a31(%rip), %rax    # 0x1770a0
movb $0x0, (%rax)
leaq 0x38ee6f(%rip), %rax    # 0x3a54e8
movl (%rax), %eax
movl %eax, %ecx
subl 0x1106dd(%rip), %ecx    # 0x126d60
movl %ecx, 0x1106db(%rip)    # 0x126d64
movl %eax, 0x1106d1(%rip)    # 0x126d60
cmpb $0x0, 0x37(%rbp)
setg %cl
movl 0x8696c(%rip), %eax     # 0x9d008
cmpl $0x2, %eax
setl %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x166f9
leal 0x2(,%rax,2), %esi
leal 0x1(%rax), %edi
movl (%rsp), %r8d
movl %edi, %ecx
shll %cl, %r8d
movl %edi, 0x86946(%rip)     # 0x9d008
movl %r12d, %eax
cltd
idivl %r8d
addl %r8d, %edx
movl %r12d, %eax
subl %edx, %eax
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
movl 0x4(%rsp), %edx
movl %esi, %ecx
shll %cl, %edx
movl $0x2400, %r13d          # imm = 0x2400
subl %edx, %r13d
cmpl %r13d, %eax
cmovll %eax, %r13d
movb $0x0, 0x37(%rbp)
movl %edi, %eax
cmpb $0x0, 0xb5(%rbp)
setg %cl
testl %eax, %eax
setg %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x16762
leal -0x2(,%rax,2), %esi
leal -0x1(%rax), %edi
movl (%rsp), %r8d
movl %edi, %ecx
shll %cl, %r8d
movl %edi, 0x868e0(%rip)     # 0x9d008
movl %r12d, %eax
cltd
idivl %r8d
addl %r8d, %edx
movl %r12d, %eax
subl %edx, %eax
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
movl 0x4(%rsp), %edx
movl %esi, %ecx
shll %cl, %edx
movl $0x2400, %r13d          # imm = 0x2400
subl %edx, %r13d
cmpl %r13d, %eax
cmovll %eax, %r13d
movb $0x0, 0xb5(%rbp)
movl %edi, %eax
testl %r12d, %r12d
jle 0x1677a
cmpb $0x0, 0xcb(%rbp)
jle 0x1677a
decl %r12d
movb $0x0, 0xcb(%rbp)
cmpb $0x0, 0xcd(%rbp)
jle 0x16796
cmpl $0x23fe, %r12d          # imm = 0x23FE
jg 0x16796
incl %r12d
movb $0x0, 0xcd(%rbp)
cmpb $0x0, 0xc8(%rbp)
jle 0x167b7
movl (%rsp), %edx
movl %eax, %ecx
shll %cl, %edx
movl %r12d, %ecx
subl %edx, %ecx
jl 0x167b7
movb $0x0, 0xc8(%rbp)
movl %ecx, %r12d
cmpb $0x0, 0xd0(%rbp)
jle 0x167dd
movl (%rsp), %edx
movl %eax, %ecx
shll %cl, %edx
movl $0x2400, %ecx           # imm = 0x2400
subl %edx, %ecx
cmpl %ecx, %r12d
jge 0x167dd
addl %edx, %r12d
movb $0x0, 0xd0(%rbp)
movl (%rsp), %edx
movl %eax, %ecx
shll %cl, %edx
cmpb $0x0, 0xc9(%rbp)
jle 0x16811
cmpl %edx, %r12d
jl 0x16811
leal (%rax,%rax), %ecx
movl 0x4(%rsp), %edx
shll %cl, %edx
subl %edx, %r12d
testl %r12d, %r12d
movl $0x0, %ecx
cmovlel %ecx, %r12d
movb $0x0, 0xc9(%rbp)
cmpb $0x0, 0xd1(%rbp)
jle 0x1684d
movl (%rsp), %edx
movl %eax, %ecx
shll %cl, %edx
movl $0x2400, %ecx           # imm = 0x2400
subl %edx, %ecx
cmpl %ecx, %r12d
jge 0x1684d
leal (%rax,%rax), %ecx
movl 0x4(%rsp), %edx
shll %cl, %edx
addl %r12d, %edx
movl $0x23ff, %ecx           # imm = 0x23FF
cmpl %ecx, %edx
cmovgel %ecx, %edx
movb $0x0, 0xd1(%rbp)
movl %edx, %r12d
cmpb $0x0, 0x2f(%rbp)
jle 0x16893
movb $0x0, 0x2f(%rbp)
movswl 0x124552(%rip), %edx    # 0x13adb0
xorl %ecx, %ecx
cmpl %edx, %r12d
jge 0x1686d
movslq %r12d, %rcx
movswl (%r14,%rcx,2), %ecx
movw $0x2400, 0x12453a(%rip) # imm = 0x2400
movq %r14, %rdx
xorl %esi, %esi
cmpq $0x2400, %rsi           # imm = 0x2400
je 0x16890
movw %si, (%rdx)
incq %rsi
addq $0x2, %rdx
jmp 0x1687b
movl %ecx, %r12d
cmpb $0x0, 0x22(%rbp)
jle 0x169bf
movswl 0x12450c(%rip), %eax    # 0x13adb0
movl $0x0, 0x8(%rsp)
cmpl %eax, %r12d
jge 0x168bd
movslq %r12d, %rax
movswl (%r14,%rax,2), %eax
movl %eax, 0x8(%rsp)
movw $0x2400, 0x1244ea(%rip) # imm = 0x2400
movq %r14, %rax
xorl %ecx, %ecx
movl 0x8(%rsp), %r12d
cmpq $0x2400, %rcx           # imm = 0x2400
je 0x168e5
movw %cx, (%rax)
incq %rcx
addq $0x2, %rax
jmp 0x168d0
movb $0x0, 0x22(%rbp)
callq 0x1cac9
cmpb $0x0, 0x1(%rbp)
jne 0x169a0
movq %rbp, %rbx
callq 0x63a71
testl %eax, %eax
je 0x1691a
leaq 0x160795(%rip), %rax    # 0x1770a0
cmpb $0x0, (%rax)
je 0x1691a
leaq 0x160789(%rip), %rax    # 0x1770a0
movb $0x0, (%rax)
callq 0x1ca8f
movl %eax, %ebp
movq %r15, %rdi
leaq 0x52c0c(%rip), %rsi     # 0x69537
movl %r12d, %edx
xorl %eax, %eax
callq 0x6b00
movswl 0x11040c(%rip), %edx    # 0x126d48
movswl 0x110409(%rip), %ecx    # 0x126d4c
xorl %edi, %edi
xorl %esi, %esi
movq %r15, %r8
xorl %r9d, %r9d
callq 0x39aab
callq 0x63720
leal -0x30(%rbp), %ecx
movzbl %bpl, %eax
cmpb $0x9, %cl
jbe 0x16986
cmpb $0xd, %bpl
je 0x169a7
cmpl $0x8, %eax
movq %rbx, %rbp
jne 0x168ee
movl %r12d, %eax
cltd
pushq $0xa
popq %rcx
idivl %ecx
movl %eax, %r12d
jmp 0x168ee
imull $0xa, %r12d, %ecx
addl %ecx, %eax
addl $-0x30, %eax
cmpl $0x2400, %eax           # imm = 0x2400
cmovll %eax, %r12d
movq %rbx, %rbp
jmp 0x168ee
movl 0x8(%rsp), %r12d
jmp 0x169aa
movq %rbx, %rbp
movl $0x100, %edx            # imm = 0x100
movq %rbp, %rdi
xorl %esi, %esi
callq 0x62f0
movl 0x86649(%rip), %eax     # 0x9d008
movl (%rsp), %ecx
movl %ecx, %edx
movl %eax, %ecx
shll %cl, %edx
leal (%rdx,%r13), %ecx
subl %edx, %r13d
movl %r13d, %esi
subl %edx, %ecx
subl %edx, %r13d
cmpl %ecx, %r12d
jl 0x169cf
addl %eax, %eax
movl 0x4(%rsp), %ecx
movl %ecx, %edi
movl %eax, %ecx
shll %cl, %edi
addl %edx, %esi
leal (%rdi,%rsi), %eax
cmpl %eax, %r12d
jge 0x169e8
testl %esi, %esi
movl $0x0, %eax
cmovlel %eax, %esi
movl $0x2400, %r13d          # imm = 0x2400
subl %edi, %r13d
cmpl %r13d, %esi
cmovll %esi, %r13d
jmp 0x16630
testb %al, %al
je 0x16a54
movswl 0x124394(%rip), %eax    # 0x13adb0
cmpl %eax, %r12d
movl 0xc(%rsp), %edx
jge 0x16a50
movslq %r12d, %rax
leaq 0x11fb81(%rip), %rcx    # 0x1365b0
movswq (%rcx,%rax,2), %rax
leaq 0x3f25e5(%rip), %rcx    # 0x409020
cmpw $0x0, (%rcx,%rax,2)
je 0x16a50
leaq 0x3e3d97(%rip), %rcx    # 0x3fa7e0
cmpw $0x0, (%rcx,%rax,2)
jne 0x16a58
movl %edx, %eax
jmp 0x16a58
movl 0xc(%rsp), %eax
xorl %ecx, %ecx
movb %cl, 0x1(%rbp)
movb %cl, 0x1c(%rbp)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq