// File: /workspace/github_c_makefile_star2_O2/jonof[P]jftekwar/jfbuild/src/build.c
// Function: copysector
void copysector(short soursector, short destsector, short deststartwall, unsigned char copystat)
{
	short j, k, m, newnumwalls, startwall, endwall;

	newnumwalls = deststartwall;  //erase existing sector fragments

		//duplicate walls
	startwall = sector[soursector].wallptr;
	endwall = startwall + sector[soursector].wallnum;
	for(j=startwall;j<endwall;j++)
	{
		Bmemcpy(&wall[newnumwalls],&wall[j],sizeof(walltype));
		wall[newnumwalls].point2 += deststartwall-startwall;
		if (wall[newnumwalls].nextwall >= 0)
		{
			wall[newnumwalls].nextwall += deststartwall-startwall;
			wall[newnumwalls].nextsector += destsector-soursector;
		}
		newnumwalls++;
	}

	//for(j=deststartwall;j<newnumwalls;j++)
	//{
	//   if (wall[j].nextwall >= 0)
	//      checksectorpointer(wall[j].nextwall,wall[j].nextsector);
	//   checksectorpointer((short)j,destsector);
	//}

	if (newnumwalls > deststartwall)
	{
			//duplicate sectors
		Bmemcpy(&sector[destsector],&sector[soursector],sizeof(sectortype));
		sector[destsector].wallptr = deststartwall;
		sector[destsector].wallnum = newnumwalls-deststartwall;

		if (copystat == 1)
		{
				//duplicate sprites
			j = headspritesect[soursector];
			while (j >= 0)
			{
				k = nextspritesect[j];

				m = insertsprite(destsector,sprite[j].statnum);
				Bmemcpy(&sprite[m],&sprite[j],sizeof(spritetype));
				sprite[m].sectnum = destsector;   //Don't let memcpy overwrite sector!

				j = k;
			}
		}

	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %ecx, 0x4(%rsp)
movl %esi, %ebx
movslq %edi, %rax
imulq $0x28, %rax, %rdi
leaq 0x222151(%rip), %rsi    # 0x23ca60
leaq (%rsi,%rdi), %rcx
movswq (%rsi,%rdi), %r10
movzwl 0x2(%rsi,%rdi), %edi
addw %r10w, %di
movl %edx, %r11d
subl %r10d, %r11d
movl %ebx, %ebp
subl %eax, %ebp
movq %r10, %r14
shlq $0x5, %r14
addq 0x716bf(%rip), %r14     # 0x8bff8
movswq %di, %r15
movzwl %dx, %r8d
xorl %r9d, %r9d
leaq 0x197105(%rip), %r12    # 0x1b1a50
leaq (%r10,%r9), %rdi
cmpq %r15, %rdi
jge 0x1a99e
leaq (%r8,%r9), %rdi
movswq %di, %r13
shlq $0x5, %r13
movaps (%r14), %xmm0
movaps 0x10(%r14), %xmm1
movaps %xmm0, (%r12,%r13)
movaps %xmm1, 0x10(%r12,%r13)
addw %r11w, 0x8(%r12,%r13)
movzwl 0xa(%r12,%r13), %edi
testw %di, %di
js 0x1a995
addq %r12, %r13
addl %r11d, %edi
movw %di, 0xa(%r13)
addw %bp, 0xc(%r13)
incq %r9
addq $0x20, %r14
jmp 0x1a94b
leal (%r9,%rdx), %edi
cmpw %dx, %di
jle 0x1aa4d
movswq %bx, %rdx
imulq $0x28, %rdx, %rdx
movups (%rcx), %xmm0
movups 0x10(%rcx), %xmm1
movups %xmm0, (%rsi,%rdx)
movq 0x20(%rcx), %rcx
movq %rcx, 0x20(%rsi,%rdx)
movups %xmm1, 0x10(%rsi,%rdx)
movw %r8w, (%rsi,%rdx)
movw %r9w, 0x2(%rsi,%rdx)
cmpb $0x1, 0x4(%rsp)
jne 0x1aa4d
leaq 0x38ab2b(%rip), %rcx    # 0x3a5510
movzwl (%rcx,%rax,2), %r12d
leaq 0x394b2f(%rip), %r14    # 0x3af520
leaq 0x25b2b8(%rip), %r15    # 0x275cb0
movswl %bx, %ebp
testw %r12w, %r12w
js 0x1aa4d
movzwl %r12w, %eax
movzwl (%r14,%rax,2), %r12d
imulq $0x2c, %rax, %r13
movswl 0x1a(%r15,%r13), %esi
movl %ebp, %edi
callq 0x2d3f9
movswq %ax, %rax
imulq $0x2c, %rax, %rax
movups (%r15,%r13), %xmm0
movups 0x10(%r15,%r13), %xmm1
movups 0x1c(%r15,%r13), %xmm2
movups %xmm1, 0x10(%r15,%rax)
movups %xmm0, (%r15,%rax)
movups %xmm2, 0x1c(%r15,%rax)
movw %bx, 0x18(%r15,%rax)
jmp 0x1a9fb
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq