// File: /workspace/github_c_makefile_star2_O3/jonof[P]jftekwar/jfbuild/src/engine.c
// Function: setfirstwall
void setfirstwall(short sectnum, short newfirstwall)
{
	int i, j, k, numwallsofloop;
	int startwall, endwall, danumwalls, dagoalloop;

	startwall = sector[sectnum].wallptr;
	danumwalls = sector[sectnum].wallnum;
	endwall = startwall+danumwalls;
	if ((newfirstwall < startwall) || (newfirstwall >= startwall+danumwalls)) return;
	for(i=0;i<danumwalls;i++)
		Bmemcpy(&wall[i+numwalls],&wall[i+startwall],sizeof(walltype));

	numwallsofloop = 0;
	i = newfirstwall;
	do
	{
		numwallsofloop++;
		i = wall[i].point2;
	} while (i != newfirstwall);

		//Put correct loop at beginning
	dagoalloop = loopnumofsector(sectnum,newfirstwall);
	if (dagoalloop > 0)
	{
		j = 0;
		while (loopnumofsector(sectnum,j+startwall) != dagoalloop) j++;
		for(i=0;i<danumwalls;i++)
		{
			k = i+j; if (k >= danumwalls) k -= danumwalls;
			Bmemcpy(&wall[startwall+i],&wall[numwalls+k],sizeof(walltype));

			wall[startwall+i].point2 += danumwalls-startwall-j;
			if (wall[startwall+i].point2 >= danumwalls)
				wall[startwall+i].point2 -= danumwalls;
			wall[startwall+i].point2 += startwall;
		}
		newfirstwall += danumwalls-j;
		if (newfirstwall >= startwall+danumwalls) newfirstwall -= danumwalls;
	}

	for(i=0;i<numwallsofloop;i++)
		Bmemcpy(&wall[i+numwalls],&wall[i+startwall],sizeof(walltype));
	for(i=0;i<numwallsofloop;i++)
	{
		k = i+newfirstwall-startwall;
		if (k >= numwallsofloop) k -= numwallsofloop;
		Bmemcpy(&wall[startwall+i],&wall[numwalls+k],sizeof(walltype));

		wall[startwall+i].point2 += numwallsofloop-newfirstwall;
		if (wall[startwall+i].point2 >= numwallsofloop)
			wall[startwall+i].point2 -= numwallsofloop;
		wall[startwall+i].point2 += startwall;
	}

	for(i=startwall;i<endwall;i++)
		if (wall[i].nextwall >= 0) wall[wall[i].nextwall].nextwall = i;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %esi, %r14d
movslq %edi, %rax
leaq (%rax,%rax,4), %rax
leaq 0x2067ee(%rip), %rcx    # 0x243a60
movswq (%rcx,%rax,8), %r15
movswl 0x2(%rcx,%rax,8), %r8d
leal (%r8,%r15), %esi
movswl %r14w, %eax
cmpw %ax, %r15w
setle %cl
cmpl %eax, %esi
setg %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x3d50b
movl %esi, 0xc(%rsp)
movzwl %r8w, %r9d
movswq 0x3d99bc(%rip), %rcx    # 0x416c68
movl %r8d, %ebp
movq %r15, %r10
shlq $0x5, %r10
shlq $0x5, %rcx
leaq 0x17b78f(%rip), %rdx    # 0x1b8a50
movq %rbp, %rsi
movaps (%rdx,%r10), %xmm0
movaps 0x10(%rdx,%r10), %xmm1
movaps %xmm1, 0x10(%rdx,%rcx)
movaps %xmm0, (%rdx,%rcx)
addq $0x20, %rdx
decq %rsi
jne 0x3d2c4
movq %r10, 0x20(%rsp)
movq %r9, 0x28(%rsp)
movq %r8, 0x30(%rsp)
xorl %r12d, %r12d
leaq 0x17b756(%rip), %rbx    # 0x1b8a50
cltq
shlq $0x5, %rax
movswl 0x8(%rax,%rbx), %eax
decq %r12
cmpw %r14w, %ax
jne 0x3d2fa
movswl %di, %edi
movswl %r14w, %esi
movl %edi, %r13d
callq 0x3d1e9
testl %eax, %eax
jle 0x3d421
movl %r14d, 0x18(%rsp)
movzwl %r15w, %ecx
movq %rcx, 0x10(%rsp)
movq $-0x1, %r14
movl %r13d, 0x1c(%rsp)
movl %eax, %r13d
movq 0x10(%rsp), %rax
addl %r14d, %eax
incl %eax
movswl %ax, %esi
movl 0x1c(%rsp), %edi
callq 0x3d1e9
incq %r14
cmpl %r13d, %eax
movl %r13d, %eax
jne 0x3d33f
movswl 0x3d98fe(%rip), %eax    # 0x416c68
movq %rax, 0x10(%rsp)
leaq 0x17b6da(%rip), %rax    # 0x1b8a50
movq 0x20(%rsp), %r13
leaq (%rax,%r13), %rdx
addq $0x8, %rdx
movq 0x28(%rsp), %rsi
movq %rsi, %rcx
subq %r14, %rcx
movl %esi, %edi
subl %r15d, %edi
movzwl %di, %edi
subq %r14, %rdi
xorl %r8d, %r8d
movq %r14, %r9
movq 0x30(%rsp), %rax
cmpq %rsi, %r9
movl %eax, %r10d
cmovbl %r8d, %r10d
movq 0x10(%rsp), %r11
addl %r9d, %r11d
subl %r10d, %r11d
movslq %r11d, %r10
shlq $0x5, %r10
leaq 0x17b689(%rip), %r11    # 0x1b8a50
movaps (%r10,%r11), %xmm0
movaps 0x10(%r10,%r11), %xmm1
movaps %xmm0, -0x8(%rdx)
movaps %xmm1, 0x8(%rdx)
movl (%rdx), %r10d
leal (%rdi,%r10), %r11d
cmpw %si, %r11w
movl %esi, %r11d
cmovll %r8d, %r11d
subl %r11d, %r10d
addl %ecx, %r10d
movw %r10w, (%rdx)
addq $0x20, %rdx
incq %r9
decq %rbp
jne 0x3d3a4
movl 0x18(%rsp), %edx
addl %edx, %eax
subl %r14d, %eax
cwtl
xorl %ecx, %ecx
cmpl %eax, 0xc(%rsp)
cmovlel %esi, %ecx
addl %esi, %edx
subl %ecx, %edx
subl %r14d, %edx
movl %edx, %r14d
jmp 0x3d426
movq 0x20(%rsp), %r13
movq %r12, %rax
negq %rax
movq %r12, %rcx
notq %rcx
movswq 0x3d982e(%rip), %rsi    # 0x416c68
movq %rsi, %rdx
shlq $0x5, %rdx
movq %rax, %rdi
movaps (%rbx,%r13), %xmm0
movaps 0x10(%rbx,%r13), %xmm1
movaps %xmm1, 0x10(%rbx,%rdx)
movaps %xmm0, (%rbx,%rdx)
addq $0x20, %rbx
decq %rdi
jne 0x3d444
movswl %r14w, %edi
movq %r12, %r8
negq %r8
leaq 0x17b5de(%rip), %rdx    # 0x1b8a50
leaq (%rdx,%r13), %r9
addq $0x8, %r9
subl %r15d, %edi
negl %r14d
movzwl %r15w, %r10d
subq %r12, %r10
xorl %r11d, %r11d
cmpl %ecx, %edi
cmovgl %r12d, %r11d
addl %esi, %r11d
addl %edi, %r11d
movslq %r11d, %r11
shlq $0x5, %r11
movaps (%r11,%rdx), %xmm0
movaps 0x10(%r11,%rdx), %xmm1
movaps %xmm0, -0x8(%r9)
movaps %xmm1, 0x8(%r9)
movzwl (%r9), %r11d
addw %r14w, %r11w
leal (%r8,%r11), %ebx
movswl %bx, %ebp
movl %r8d, %ebx
cmpl %ebp, %ecx
jl 0x3d4ca
xorl %ebx, %ebx
subl %ebx, %r11d
addl %r10d, %r11d
movw %r11w, (%r9)
addq $0x20, %r9
incl %edi
decq %rax
jne 0x3d487
movslq 0xc(%rsp), %rax
leaq (%rdx,%r13), %rcx
addq $0xa, %rcx
movswq (%rcx), %rsi
testq %rsi, %rsi
js 0x3d4ff
shlq $0x5, %rsi
movw %r15w, 0xa(%rsi,%rdx)
incq %r15
addq $0x20, %rcx
cmpq %rax, %r15
jl 0x3d4ec
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq