// File: /workspace/github_c_makefile_star2_O1/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 $0x28, %rsp
movslq %edi, %rax
leaq (%rax,%rax,4), %rax
leaq 0x206cae(%rip), %rcx    # 0x242a60
movswq (%rcx,%rax,8), %r15
movswl 0x2(%rcx,%rax,8), %ebx
leal (%rbx,%r15), %edx
movswl %si, %eax
cmpw %ax, %r15w
setle %cl
movl %edx, 0x4(%rsp)
cmpl %eax, %edx
setg %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x3c08b
movzwl %bx, %r10d
movl %ebx, %ebp
testw %r10w, %r10w
jle 0x3be33
movswq 0x3d9e76(%rip), %rcx    # 0x415c68
movq %r15, %rdx
shlq $0x5, %rdx
leaq 0x17bc50(%rip), %r8     # 0x1b7a50
addq %r8, %rdx
shlq $0x5, %rcx
addq %r8, %rcx
movq %rbp, %r9
shlq $0x5, %r9
xorl %r8d, %r8d
movaps (%rdx,%r8), %xmm0
movaps 0x10(%rdx,%r8), %xmm1
movaps %xmm1, 0x10(%rcx,%r8)
movaps %xmm0, (%rcx,%r8)
addq $0x20, %r8
cmpq %r8, %r9
jne 0x3be14
movq %r10, 0x10(%rsp)
xorl %r12d, %r12d
leaq 0x17bc0e(%rip), %r13    # 0x1b7a50
cltq
shlq $0x5, %rax
movswl 0x8(%rax,%r13), %eax
decq %r12
cmpw %si, %ax
jne 0x3be42
movswl %di, %edi
movl %esi, (%rsp)
movswl %si, %esi
movl %edi, %r14d
callq 0x3bd2c
testl %eax, %eax
jle 0x3bf8b
movq %rbp, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movzwl %r15w, %ecx
movq %rcx, 0x8(%rsp)
movq $-0x1, %rbx
movl %eax, %ebp
movq 0x8(%rsp), %rax
addl %ebx, %eax
incl %eax
movswl %ax, %esi
movl %r14d, %edi
callq 0x3bd2c
incq %rbx
cmpl %ebp, %eax
movl %ebp, %eax
jne 0x3be89
movq 0x10(%rsp), %r14
testw %r14w, %r14w
movq 0x20(%rsp), %rax
movq 0x18(%rsp), %rbp
jle 0x3bf6a
movswl 0x3d9da0(%rip), %ecx    # 0x415c68
movq %rcx, 0x8(%rsp)
movq %r15, %rdx
shlq $0x5, %rdx
leaq 0x17bb75(%rip), %rcx    # 0x1b7a50
addq %rcx, %rdx
addq $0x8, %rdx
shlq $0x5, %rbp
movq %r14, %rsi
subq %rbx, %rsi
movl %r14d, %edi
subl %r15d, %edi
movzwl %di, %edi
subq %rbx, %rdi
xorl %r8d, %r8d
movq %rbx, %r9
xorl %r10d, %r10d
cmpq %r14, %r9
movl %eax, %r11d
cmovbl %r8d, %r11d
movq %rbp, %rcx
movq 0x8(%rsp), %rbp
addl %r9d, %ebp
subl %r11d, %ebp
movslq %ebp, %r11
shlq $0x5, %r11
leaq 0x17bb29(%rip), %rbp    # 0x1b7a50
movaps (%r11,%rbp), %xmm0
movaps 0x10(%r11,%rbp), %xmm1
movaps %xmm0, -0x8(%rdx,%r10)
movaps %xmm1, 0x8(%rdx,%r10)
movl (%rdx,%r10), %r11d
leal (%rdi,%r11), %ebp
cmpw %r14w, %bp
movl %eax, %ebp
cmovll %r8d, %ebp
subl %ebp, %r11d
movq %rcx, %rbp
addl %esi, %r11d
movw %r11w, (%rdx,%r10)
addq $0x20, %r10
incq %r9
cmpq %r10, %rcx
jne 0x3bf01
movq %r14, %rdx
movl (%rsp), %r14d
addl %r14d, %eax
subl %ebx, %eax
cwtl
xorl %ecx, %ecx
cmpl %eax, 0x4(%rsp)
cmovlel %edx, %ecx
addl %edx, %r14d
subl %ecx, %r14d
subl %ebx, %r14d
jmp 0x3bf8f
movl (%rsp), %r14d
movq %r12, %rdx
negq %rdx
movq %r12, %rsi
notq %rsi
movswq 0x3d9cc5(%rip), %rcx    # 0x415c68
movq %r15, %rax
shlq $0x5, %rax
shlq $0x5, %rcx
movq %rdx, %rdi
movaps (%r13,%rax), %xmm0
movaps 0x10(%r13,%rax), %xmm1
movaps %xmm1, 0x10(%r13,%rcx)
movaps %xmm0, (%r13,%rcx)
addq $0x20, %r13
decq %rdi
jne 0x3bfb1
movswl %r14w, %edi
movswl 0x3d9c8a(%rip), %r8d    # 0x415c68
movq %r12, %r9
negq %r9
leaq 0x17ba65(%rip), %rcx    # 0x1b7a50
leaq (%rax,%rcx), %r10
addq $0x8, %r10
subl %r15d, %edi
negl %r14d
movzwl %r15w, %r11d
subq %r12, %r11
cmpl %esi, %edi
movl $0x0, %ebx
cmovgl %r12d, %ebx
addl %r8d, %ebx
addl %edi, %ebx
movslq %ebx, %rbx
shlq $0x5, %rbx
movaps (%rbx,%rcx), %xmm0
movaps 0x10(%rbx,%rcx), %xmm1
movaps %xmm0, -0x8(%r10)
movaps %xmm1, 0x8(%r10)
movzwl (%r10), %ebx
addw %r14w, %bx
leal (%r9,%rbx), %ebp
movswl %bp, %ebp
cmpl %ebp, %esi
movl $0x0, %ebp
cmovll %r9d, %ebp
subl %ebp, %ebx
addl %r11d, %ebx
movw %bx, (%r10)
addq $0x20, %r10
incl %edi
decq %rdx
jne 0x3c000
cmpw $0x0, 0x10(%rsp)
jle 0x3c08b
movslq 0x4(%rsp), %rdx
addq %rcx, %rax
addq $0xa, %rax
movswq (%rax), %rsi
testq %rsi, %rsi
js 0x3c07f
shlq $0x5, %rsi
movw %r15w, 0xa(%rsi,%rcx)
incq %r15
addq $0x20, %rax
cmpq %rdx, %r15
jl 0x3c06c
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq