// File: /workspace/github_c_makefile_star2_O3/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 %esi, %ebx
movslq %edi, %rax
leaq (%rax,%rax,4), %r8
leaq 0x226c40(%rip), %rsi    # 0x243a60
movswq (%rsi,%r8,8), %r10
movzwl 0x2(%rsi,%r8,8), %r14d
addw %r10w, %r14w
cmpw %r14w, %r10w
jge 0x1cf69
leaq (%rsi,%r8,8), %r9
movzwl %dx, %r8d
movl %r8d, %r11d
subl %r10d, %r11d
movl %ebx, %ebp
subl %edi, %ebp
movswq %r14w, %r14
subq %r10, %r14
shlq $0x5, %r10
addq 0x7619b(%rip), %r10     # 0x92ff8
xorl %edi, %edi
leaq 0x19bbea(%rip), %r15    # 0x1b8a50
leal (%r8,%rdi), %r12d
movswq %r12w, %r12
shlq $0x5, %r12
movaps (%r10), %xmm0
movaps 0x10(%r10), %xmm1
movaps %xmm0, (%r15,%r12)
movaps %xmm1, 0x10(%r15,%r12)
addw %r11w, 0x8(%r15,%r12)
movzwl 0xa(%r15,%r12), %r13d
testw %r13w, %r13w
js 0x1ceaa
addq %r15, %r12
addl %r11d, %r13d
movw %r13w, 0xa(%r12)
addw %bp, 0xc(%r12)
addq $0x20, %r10
incq %rdi
cmpq %rdi, %r14
jne 0x1ce66
leal (%rdi,%rdx), %r10d
cmpw %dx, %r10w
jle 0x1cf69
movswq %bx, %rdx
leaq (%rdx,%rdx,4), %rdx
movups (%r9), %xmm0
movups 0x10(%r9), %xmm1
movups %xmm0, (%rsi,%rdx,8)
movq 0x20(%r9), %r9
movq %r9, 0x20(%rsi,%rdx,8)
movups %xmm1, 0x10(%rsi,%rdx,8)
movw %r8w, (%rsi,%rdx,8)
movw %di, 0x2(%rsi,%rdx,8)
cmpb $0x1, %cl
jne 0x1cf69
leaq 0x38f613(%rip), %rcx    # 0x3ac510
movzwl (%rcx,%rax,2), %r14d
testw %r14w, %r14w
js 0x1cf69
leaq 0x399611(%rip), %r15    # 0x3b6520
leaq 0x25fd9a(%rip), %r12    # 0x27ccb0
movswl %bx, %ebp
movzwl %r14w, %eax
movzwl (%r15,%rax,2), %r14d
imulq $0x2c, %rax, %r13
movswl 0x1a(%r12,%r13), %esi
movl %ebp, %edi
callq 0x304f8
movswq %ax, %rax
imulq $0x2c, %rax, %rax
movups (%r12,%r13), %xmm0
movups 0x10(%r12,%r13), %xmm1
movups 0x1c(%r12,%r13), %xmm2
movups %xmm1, 0x10(%r12,%rax)
movups %xmm0, (%r12,%rax)
movups %xmm2, 0x1c(%r12,%rax)
movw %bx, 0x18(%r12,%rax)
testw %r14w, %r14w
jns 0x1cf19
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq