// File: /workspace/github_c_makefile_star2_O1/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 $0x78, %rsp
movl %edi, %ebp
leaq 0x3ef32e(%rip), %rax    # 0x406fec
movl (%rax), %eax
movl %eax, 0x14(%rsp)
leaq 0x3ef325(%rip), %rax    # 0x406ff0
movl (%rax), %eax
sarl $0x6, %eax
movl %eax, 0x10(%rsp)
leaq 0x1655b5(%rip), %rax    # 0x17d290
movb $0x0, 0x2f(%rax)
leaq 0x1200ba(%rip), %r15    # 0x137da0
xorl %ebx, %ebx
movl $0x4800, %edx           # imm = 0x4800
movq %r15, %rdi
xorl %esi, %esi
callq 0x62f0
leaq 0x1248b2(%rip), %rax    # 0x13c5b0
movw %bx, (%rax)
incq %rbx
addq $0x2, %rax
cmpq $0x2400, %rbx           # imm = 0x2400
jne 0x17cfe
xorl %r8d, %r8d
testl %ebp, %ebp
cmovgl %ebp, %r8d
leaq 0x37fdb1(%rip), %rax    # 0x397ad2
movzwl (%rax), %eax
leal -0x1(%rax), %ecx
cmpw $0x1, %cx
ja 0x17d6d
leaq 0x3b722c(%rip), %rcx    # 0x3cef60
movswq (%rcx), %rcx
testq %rcx, %rcx
jle 0x17d6d
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rcx
xorl %edx, %edx
leaq 0x22ad12(%rip), %rsi    # 0x242a60
movswq 0x10(%rsi,%rdx), %rdi
incw (%r15,%rdi,2)
movswq 0x18(%rsi,%rdx), %rdi
incw (%r15,%rdi,2)
addq $0x28, %rdx
cmpq %rdx, %rcx
jne 0x17d4e
sarl $0x6, 0x14(%rsp)
cmpl $0x4, %eax
je 0x17dbf
cmpl $0x3, %eax
je 0x17df5
testl %eax, %eax
jne 0x17e20
leaq 0x3fdedd(%rip), %rcx    # 0x415c68
movswq (%rcx), %rcx
testq %rcx, %rcx
jle 0x17db5
shlq $0x5, %rcx
xorl %edx, %edx
leaq 0x19fcaf(%rip), %rsi    # 0x1b7a50
movswq 0x10(%rsi,%rdx), %rdi
incw (%r15,%rdi,2)
addq $0x20, %rdx
cmpq %rdx, %rcx
jne 0x17da1
cmpl $0x3, %eax
je 0x17df5
cmpl $0x4, %eax
jne 0x17e20
leaq 0x3fdea2(%rip), %rcx    # 0x415c68
movswq (%rcx), %rcx
testq %rcx, %rcx
jle 0x17df0
shlq $0x5, %rcx
xorl %edx, %edx
leaq 0x19fc74(%rip), %rsi    # 0x1b7a50
movswq 0x12(%rsi,%rdx), %rdi
incw (%r15,%rdi,2)
addq $0x20, %rdx
cmpq %rdx, %rcx
jne 0x17ddc
cmpl $0x3, %eax
jne 0x17e20
movl $0x1a, %eax
leaq 0x263eaf(%rip), %rcx    # 0x27bcb0
cmpw $0x3ff, (%rcx,%rax)     # imm = 0x3FF
jg 0x17e14
movswq -0xc(%rcx,%rax), %rdx
incw (%r15,%rdx,2)
addq $0x2c, %rax
cmpq $0xb001a, %rax          # imm = 0xB001A
jne 0x17e01
movl 0x10(%rsp), %eax
imull 0x14(%rsp), %eax
movl %eax, 0x10(%rsp)
movl $0x1200, %eax           # imm = 0x1200
leaq 0x124777(%rip), %r12    # 0x13c5b0
movl %r8d, 0xc(%rsp)
movl %r8d, %r14d
movl $0x2400, %ecx           # imm = 0x2400
subl %eax, %ecx
movq %rcx, 0x18(%rsp)
movl %eax, %ecx
leaq (%r12,%rcx,2), %rsi
negl %eax
movslq %eax, %rdi
addq %rdi, %rdi
xorl %r8d, %r8d
movzwl (%r15,%r8,2), %r11d
leaq (%r8,%rcx), %r9
movzwl (%r15,%r9,2), %ebp
cmpw %bp, %r11w
jge 0x17ed8
leaq (%r15,%r9,2), %rbx
movq %rsi, %r9
movq %r8, %r13
movl %r8d, %r10d
leal (%rcx,%r10), %edx
movw %bp, (%r15,%r13,2)
movw %r11w, (%rbx)
movzwl (%r12,%r13,2), %r11d
movzwl (%r9), %ebx
movw %bx, (%r12,%r13,2)
movw %r11w, (%r9)
cmpl %r14d, %edx
movl %r14d, %r11d
cmovel %r10d, %r11d
cmpl %r14d, %r10d
movl %r11d, %r14d
cmovel %edx, %r14d
movl %eax, %r11d
addl %r10d, %r11d
js 0x17ed8
movslq %r11d, %r13
movzwl (%r15,%r13,2), %r11d
movslq %r10d, %rdx
subl %ecx, %r10d
leaq (%r15,%rdx,2), %rbx
movzwl (%rbx), %ebp
addq %rdi, %r9
cmpw %bp, %r11w
jl 0x17e7f
incq %r8
addq $0x2, %rsi
cmpq 0x18(%rsp), %r8
jne 0x17e5e
movl %ecx, %eax
shrl %eax
cmpl $0x1, %ecx
ja 0x17e41
movw $0xffff, %ax            # imm = 0xFFFF
incl %eax
movswq %ax, %rcx
cmpw $0x0, (%r15,%rcx,2)
jg 0x17efb
movw %ax, 0x128ea0(%rip)     # 0x140db0
cmpw $0x0, 0x11fe88(%rip)    # 0x137da0
leaq 0x165371(%rip), %rbx    # 0x17d290
movl 0x14(%rsp), %ebp
movl 0x10(%rsp), %r15d
jne 0x17f56
movw $0x2400, 0x128e7d(%rip) # imm = 0x2400
leaq 0x124676(%rip), %rax    # 0x13c5b0
xorl %ecx, %ecx
movw %cx, (%rax)
incq %rcx
addq $0x2, %rax
cmpq $0x2400, %rcx           # imm = 0x2400
jne 0x17f3c
movl 0xc(%rsp), %eax
movl %eax, %r14d
movb 0x1c(%rbx), %al
movb 0x1(%rbx), %cl
orb %al, %cl
jne 0x18382
movl 0x8b09e(%rip), %ecx     # 0xa3008
movl %ebp, %esi
shll %cl, %esi
movl %r14d, %eax
cltd
idivl %esi
addl %esi, %edx
movl %r14d, %eax
subl %edx, %eax
xorl %edx, %edx
testl %eax, %eax
cmovlel %edx, %eax
addl %ecx, %ecx
movl %r15d, %edx
shll %cl, %edx
movl $0x2400, %r13d          # imm = 0x2400
subl %edx, %r13d
cmpl %r13d, %eax
cmovll %eax, %r13d
movl %r13d, %edi
movl %r14d, %esi
callq 0x1878b
callq 0x4a4cd
callq 0x6d200
callq 0x6d55e
testl %eax, %eax
je 0x17fcd
leaq 0x1650e2(%rip), %rax    # 0x17d0a0
cmpb $0x0, (%rax)
je 0x17fcd
leaq 0x1650d6(%rip), %rax    # 0x17d0a0
movb $0x0, (%rax)
leaq 0x393514(%rip), %rax    # 0x3ab4e8
movl (%rax), %eax
movl %eax, %ecx
subl 0x114d82(%rip), %ecx    # 0x12cd60
movl %ecx, 0x114d80(%rip)    # 0x12cd64
movl %eax, 0x114d76(%rip)    # 0x12cd60
cmpb $0x0, 0x37(%rbx)
setg %al
movl 0x8b011(%rip), %esi     # 0xa3008
cmpl $0x2, %esi
setl %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x18048
leal 0x1(%rsi), %ecx
movl %ecx, 0x8affb(%rip)     # 0xa3008
movl %ebp, %edi
shll %cl, %edi
movl %r14d, %eax
cltd
idivl %edi
addl %edi, %edx
movl %r14d, %eax
subl %edx, %eax
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
leal 0x2(,%rsi,2), %ecx
movl %r15d, %edx
shll %cl, %edx
movl $0x2400, %r13d          # imm = 0x2400
subl %edx, %r13d
cmpl %r13d, %eax
cmovll %eax, %r13d
movb $0x0, 0x37(%rbx)
cmpb $0x0, 0xb5(%rbx)
setg %al
movl 0x8afb0(%rip), %esi     # 0xa3008
testl %esi, %esi
setg %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x180ab
leal -0x1(%rsi), %ecx
movl %ecx, 0x8af9b(%rip)     # 0xa3008
movl %ebp, %edi
shll %cl, %edi
movl %r14d, %eax
cltd
idivl %edi
addl %edi, %edx
movl %r14d, %eax
subl %edx, %eax
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
leal -0x2(,%rsi,2), %ecx
movl %r15d, %edx
shll %cl, %edx
movl $0x2400, %r13d          # imm = 0x2400
subl %edx, %r13d
cmpl %r13d, %eax
cmovll %eax, %r13d
movb $0x0, 0xb5(%rbx)
testl %r14d, %r14d
jle 0x180c3
cmpb $0x0, 0xcb(%rbx)
jle 0x180c3
decl %r14d
movb $0x0, 0xcb(%rbx)
cmpb $0x0, 0xcd(%rbx)
jle 0x180df
cmpl $0x23fe, %r14d          # imm = 0x23FE
jg 0x180df
incl %r14d
movb $0x0, 0xcd(%rbx)
cmpb $0x0, 0xc8(%rbx)
jle 0x18103
movb 0x8af1a(%rip), %cl      # 0xa3008
movl %ebp, %edx
shll %cl, %edx
movl %r14d, %eax
subl %edx, %eax
jl 0x18103
movb $0x0, 0xc8(%rbx)
movl %eax, %r14d
cmpb $0x0, 0xd0(%rbx)
jle 0x1812f
movb 0x8aef6(%rip), %cl      # 0xa3008
movl %ebp, %eax
shll %cl, %eax
movl $0x2400, %ecx           # imm = 0x2400
subl %eax, %ecx
cmpl %ecx, %r14d
jge 0x1812f
addl %r14d, %eax
movb $0x0, 0xd0(%rbx)
movl %eax, %r14d
cmpb $0x0, 0xc9(%rbx)
jle 0x18164
movl 0x8aeca(%rip), %ecx     # 0xa3008
movl %ebp, %eax
shll %cl, %eax
cmpl %eax, %r14d
jl 0x18164
addl %ecx, %ecx
movl %r15d, %eax
shll %cl, %eax
subl %eax, %r14d
testl %r14d, %r14d
movl $0x0, %eax
cmovlel %eax, %r14d
movb $0x0, 0xc9(%rbx)
cmpb $0x0, 0xd1(%rbx)
jle 0x181a1
movl 0x8ae95(%rip), %ecx     # 0xa3008
movl %ebp, %eax
shll %cl, %eax
movl $0x2400, %edx           # imm = 0x2400
subl %eax, %edx
cmpl %edx, %r14d
jge 0x181a1
addl %ecx, %ecx
movl %r15d, %eax
shll %cl, %eax
addl %r14d, %eax
movl $0x23ff, %ecx           # imm = 0x23FF
cmpl %ecx, %eax
cmovgel %ecx, %eax
movb $0x0, 0xd1(%rbx)
movl %eax, %r14d
cmpb $0x0, 0x2f(%rbx)
jle 0x181e5
movb $0x0, 0x2f(%rbx)
movswl 0x128bfe(%rip), %ecx    # 0x140db0
xorl %eax, %eax
cmpl %ecx, %r14d
jge 0x181c1
movslq %r14d, %rax
movswl (%r12,%rax,2), %eax
movw $0x2400, 0x128be6(%rip) # imm = 0x2400
movq %r12, %rcx
xorl %edx, %edx
movw %dx, (%rcx)
incq %rdx
addq $0x2, %rcx
cmpq $0x2400, %rdx           # imm = 0x2400
jne 0x181cf
movl %eax, %r14d
cmpb $0x0, 0x22(%rbx)
jle 0x18313
movswl 0x128bba(%rip), %eax    # 0x140db0
xorl %ebp, %ebp
cmpl %eax, %r14d
jge 0x18205
movslq %r14d, %rax
movswl (%r12,%rax,2), %ebp
movw $0x2400, 0x128ba2(%rip) # imm = 0x2400
movq %r12, %rax
xorl %ecx, %ecx
movw %cx, (%rax)
incq %rcx
addq $0x2, %rax
cmpq $0x2400, %rcx           # imm = 0x2400
jne 0x18213
movb $0x0, 0x22(%rbx)
callq 0x1ec2f
cmpb $0x0, 0x1(%rbx)
je 0x1823d
movl %ebp, %r14d
jmp 0x182ff
movl %ebp, 0x18(%rsp)
movl %ebp, %r14d
callq 0x6d55e
testl %eax, %eax
je 0x18263
leaq 0x164e4c(%rip), %rax    # 0x17d0a0
cmpb $0x0, (%rax)
je 0x18263
leaq 0x164e40(%rip), %rax    # 0x17d0a0
movb $0x0, (%rax)
callq 0x1ebf5
movl %eax, %ebp
leaq 0x20(%rsp), %r15
movq %r15, %rdi
leaq 0x5b23c(%rip), %rsi     # 0x734b5
movl %r14d, %edx
xorl %eax, %eax
callq 0x6b00
movswl 0x114abe(%rip), %edx    # 0x12cd48
movswl 0x114abb(%rip), %ecx    # 0x12cd4c
xorl %edi, %edi
xorl %esi, %esi
movq %r15, %r8
xorl %r9d, %r9d
callq 0x3c433
callq 0x6d200
leal -0x30(%rbp), %eax
cmpb $0x9, %al
ja 0x182c5
movzbl %bpl, %eax
leal (%r14,%r14,4), %ecx
leal (%rax,%rcx,2), %eax
addl $-0x30, %eax
cmpl $0x2400, %eax           # imm = 0x2400
cmovll %eax, %r14d
jmp 0x182ee
cmpb $0x8, %bpl
je 0x182d6
movzbl %bpl, %eax
cmpl $0xd, %eax
jne 0x182ee
jmp 0x182ff
movslq %r14d, %rax
imulq $0x66666667, %rax, %r14 # imm = 0x66666667
movq %r14, %rax
shrq $0x3f, %rax
sarq $0x22, %r14
addl %eax, %r14d
cmpb $0x0, 0x1(%rbx)
je 0x18244
movl 0x18(%rsp), %eax
movl %eax, %r14d
movl $0x100, %edx            # imm = 0x100
movq %rbx, %rdi
xorl %esi, %esi
callq 0x62f0
movl 0x10(%rsp), %r15d
cmpl %r13d, %r14d
movl 0x14(%rsp), %ebp
jge 0x1832e
movb 0x8ace6(%rip), %cl      # 0xa3008
movl %ebp, %eax
shll %cl, %eax
subl %eax, %r13d
cmpl %r13d, %r14d
jl 0x18326
movl 0x8acd4(%rip), %eax     # 0xa3008
leal (%rax,%rax), %ecx
movl %r15d, %edx
shll %cl, %edx
leal (%rdx,%r13), %ecx
cmpl %ecx, %r14d
jl 0x18357
movl %ebp, %esi
movl %eax, %ecx
shll %cl, %esi
addl %esi, %r13d
leal (%rdx,%r13), %eax
cmpl %eax, %r14d
jge 0x1834b
testl %r13d, %r13d
movl $0x0, %eax
cmovlel %eax, %r13d
movl $0x2400, %ecx           # imm = 0x2400
subl %edx, %ecx
cmpl %ecx, %r13d
cmovll %r13d, %ecx
movb 0x1c(%rbx), %al
movb 0x1(%rbx), %dl
orb %al, %dl
movl %ecx, %r13d
je 0x17f99
testb %al, %al
je 0x183b6
movswl 0x128a23(%rip), %eax    # 0x140db0
cmpl %eax, %r14d
jge 0x183b6
movslq %r14d, %rax
movswq (%r12,%rax,2), %rax
leaq 0x3f6c7f(%rip), %rcx    # 0x40f020
cmpw $0x0, (%rcx,%rax,2)
je 0x183b6
leaq 0x3e8431(%rip), %rcx    # 0x4007e0
cmpw $0x0, (%rcx,%rax,2)
jne 0x183ba
movl 0xc(%rsp), %eax
xorl %ecx, %ecx
movb %cl, 0x1(%rbx)
movb %cl, 0x1c(%rbx)
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq