// File: /workspace/github_c_makefile_star2_O2/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 $0x48, %rsp
movslq %edi, %rax
imulq $0x28, %rax, %rax
leaq 0x2035f7(%rip), %rcx    # 0x23ca60
movswq (%rax,%rcx), %r15
movswl 0x2(%rax,%rcx), %r9d
leal (%r9,%r15), %r8d
movswl %si, %eax
cmpw %ax, %r15w
setle %cl
cmpl %eax, %r8d
setg %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x3972c
movq %r9, 0x18(%rsp)
movslq %r9d, %r9
movzwl %r9w, %ecx
movq %rcx, 0x10(%rsp)
movswq 0x3d67bc(%rip), %rdx    # 0x40fc68
movq %r9, 0x38(%rsp)
movl %r9d, %r10d
shlq $0x5, %r10
movq %rdx, 0x20(%rsp)
movq %rdx, %rbx
shlq $0x5, %rbx
leaq 0x178585(%rip), %r12    # 0x1b1a50
leaq (%r12,%rbx), %rcx
movq %r15, %rdx
shlq $0x5, %rdx
movq %rdx, 0x8(%rsp)
addq %r12, %rdx
xorl %r9d, %r9d
cmpq %r9, %r10
je 0x39502
movaps (%rdx,%r9), %xmm0
movaps 0x10(%rdx,%r9), %xmm1
movaps %xmm1, 0x10(%rcx,%r9)
movaps %xmm0, (%rcx,%r9)
addq $0x20, %r9
jmp 0x394e1
movq %r10, 0x40(%rsp)
movl %r8d, 0x4(%rsp)
xorl %r13d, %r13d
cltq
shlq $0x5, %rax
movswl 0x8(%rax,%r12), %eax
decq %r13
cmpw %si, %ax
jne 0x3950f
movswl %di, %ebp
movl %esi, (%rsp)
movswl %si, %esi
movl %ebp, %edi
callq 0x393ec
testl %eax, %eax
jle 0x39639
movl %eax, %r14d
movq %rbx, 0x28(%rsp)
movq %r15, 0x30(%rsp)
movzwl %r15w, %r15d
pushq $-0x1
popq %rbx
leal (%r15,%rbx), %eax
incl %eax
movswl %ax, %esi
movl %ebp, %edi
callq 0x393ec
incq %rbx
cmpl %r14d, %eax
jne 0x3954f
movl 0x20(%rsp), %eax
leaq 0x1784de(%rip), %rcx    # 0x1b1a50
movq 0x8(%rsp), %rdx
addq %rcx, %rdx
addq $0x8, %rdx
movl $0xffff, %r8d           # imm = 0xFFFF
movq 0x18(%rsp), %rsi
andl %r8d, %esi
subq %rbx, %rsi
movq 0x10(%rsp), %rdi
movq 0x30(%rsp), %r15
subl %r15d, %edi
andl %r8d, %edi
subq %rbx, %rdi
xorl %r8d, %r8d
movq %rbx, %r9
xorl %r10d, %r10d
movq 0x38(%rsp), %r14
cmpq %r10, 0x40(%rsp)
je 0x39611
cmpq %r14, %r9
movq 0x18(%rsp), %r11
cmovll %r8d, %r11d
leal (%rax,%r9), %ebp
subl %r11d, %ebp
movslq %ebp, %r11
shlq $0x5, %r11
movaps (%r11,%rcx), %xmm0
movaps 0x10(%r11,%rcx), %xmm1
movaps %xmm0, -0x8(%rdx,%r10)
movaps %xmm1, 0x8(%rdx,%r10)
movl (%rdx,%r10), %r11d
leal (%rdi,%r11), %ebp
cmpw %r14w, %bp
movq 0x10(%rsp), %rbp
cmovll %r8d, %ebp
subl %ebp, %r11d
addl %esi, %r11d
movw %r11w, (%rdx,%r10)
addq $0x20, %r10
incq %r9
jmp 0x395b0
movq 0x18(%rsp), %rdx
addl (%rsp), %edx
movl %edx, %eax
subl %ebx, %eax
cwtl
xorl %ecx, %ecx
cmpl %eax, 0x4(%rsp)
movq 0x10(%rsp), %rax
cmovgl %ecx, %eax
subl %eax, %edx
subl %ebx, %edx
movl %edx, %ebp
movq 0x28(%rsp), %rbx
jmp 0x3963c
movl (%rsp), %ebp
movq %r13, %rcx
negq %rcx
movq %r13, %rdx
notq %rdx
movq %rcx, %rax
movq 0x8(%rsp), %r8
subq $0x1, %rax
jb 0x39672
movaps (%r12,%r8), %xmm0
movaps 0x10(%r12,%r8), %xmm1
movaps %xmm1, 0x10(%r12,%rbx)
movaps %xmm0, (%r12,%rbx)
addq $0x20, %r12
jmp 0x39650
movswl %bp, %esi
movq %r13, %rdi
negq %rdi
subl %r15d, %esi
leaq 0x1783cb(%rip), %rax    # 0x1b1a50
addq %rax, %r8
addq $0x8, %r8
negl %ebp
movzwl %r15w, %r9d
subq %r13, %r9
movq 0x20(%rsp), %r12
subq $0x1, %rcx
jb 0x396fa
xorl %r10d, %r10d
cmpl %edx, %esi
movl %r13d, %r11d
cmovlel %r10d, %r11d
addl %r12d, %r11d
addl %esi, %r11d
movslq %r11d, %r10
shlq $0x5, %r10
movaps (%r10,%rax), %xmm0
movaps 0x10(%r10,%rax), %xmm1
movaps %xmm0, -0x8(%r8)
movaps %xmm1, 0x8(%r8)
movzwl (%r8), %r10d
addw %bp, %r10w
leal (%rdi,%r10), %r11d
movswl %r11w, %ebx
movl %edi, %r11d
cmpl %ebx, %edx
jl 0x396e8
xorl %r11d, %r11d
subl %r11d, %r10d
addl %r9d, %r10d
movw %r10w, (%r8)
incl %esi
addq $0x20, %r8
jmp 0x3969a
movslq 0x4(%rsp), %rcx
movq 0x8(%rsp), %rdx
addq %rax, %rdx
addq $0xa, %rdx
cmpq %rcx, %r15
jge 0x3972c
movswq (%rdx), %rsi
testq %rsi, %rsi
js 0x39723
shlq $0x5, %rsi
movw %r15w, 0xa(%rsi,%rax)
incq %r15
addq $0x20, %rdx
jmp 0x3970b
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq