// File: /workspace/github_c_makefile_star2_O1/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 0x226089(%rip), %rsi    # 0x242a60
movswq (%rsi,%r8,8), %r9
movzwl 0x2(%rsi,%r8,8), %r14d
addw %r9w, %r14w
movl %edx, %r10d
cmpw %r14w, %r9w
jge 0x1ca5f
movl %edx, %r11d
subl %r9d, %r11d
movl %ebx, %ebp
subl %edi, %ebp
movswq %r14w, %rdi
subq %r9, %rdi
shlq $0x5, %r9
addq 0x755ed(%rip), %r9      # 0x91ff8
leaq 0x19b03e(%rip), %r14    # 0x1b7a50
movl %edx, %r10d
movswq %r10w, %r15
shlq $0x5, %r15
movaps (%r9), %xmm0
movaps 0x10(%r9), %xmm1
movaps %xmm0, (%r14,%r15)
movaps %xmm1, 0x10(%r14,%r15)
addw %r11w, 0x8(%r14,%r15)
movzwl 0xa(%r14,%r15), %r12d
testw %r12w, %r12w
js 0x1ca53
addq %r14, %r15
addl %r11d, %r12d
movw %r12w, 0xa(%r15)
addw %bp, 0xc(%r15)
incl %r10d
addq $0x20, %r9
decq %rdi
jne 0x1ca15
cmpw %dx, %r10w
jle 0x1cb16
leaq (%rsi,%r8,8), %rdi
movswq %bx, %r8
leaq (%r8,%r8,4), %r8
movups (%rdi), %xmm0
movups 0x10(%rdi), %xmm1
movups %xmm0, (%rsi,%r8,8)
movq 0x20(%rdi), %rdi
movq %rdi, 0x20(%rsi,%r8,8)
movups %xmm1, 0x10(%rsi,%r8,8)
movw %dx, (%rsi,%r8,8)
subl %edx, %r10d
movw %r10w, 0x2(%rsi,%r8,8)
cmpb $0x1, %cl
jne 0x1cb16
leaq 0x38ea66(%rip), %rcx    # 0x3ab510
movzwl (%rcx,%rax,2), %r14d
testw %r14w, %r14w
js 0x1cb16
leaq 0x398a64(%rip), %r15    # 0x3b5520
leaq 0x25f1ed(%rip), %r12    # 0x27bcb0
movswl %bx, %ebp
movzwl %r14w, %eax
movzwl (%r15,%rax,2), %r14d
imulq $0x2c, %rax, %r13
movswl 0x1a(%r12,%r13), %esi
movl %ebp, %edi
callq 0x2f966
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 0x1cac6
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq