// File: /workspace/github_cpp_makefile_star2_O3/estwings57[P]CasHMC/sources/Packet.cpp
// Function: CasHMC::Packet::GetCRC()
unsigned long Packet::GetCRC()
{
	uint64_t packetData[LNG*2];

	if(packetType == REQUEST) {
		packetData[0] <<= 3;	packetData[0] |= (CUB&0x7);
		packetData[0] <<= 3;	packetData[0] |= (RES&0x7);
		packetData[0] <<= 34;	packetData[0] |= (ADRS&0x3FFFFFFFF);
		packetData[0] <<= 1;	packetData[0] |= (RES&0x1);
		packetData[0] <<= 11;	packetData[0] |= (TAG&0x7FF);
		packetData[0] <<= 5;	packetData[0] |= (LNG&0x1F);
		packetData[0] <<= 7;	packetData[0] |= (CMD&0x7F);
	}
	else {
		packetData[0] <<= 3;	packetData[0] |= (CUB&0x7);
		packetData[0] <<= 19;	packetData[0] |= (RES&0x7FFFF);
		packetData[0] <<= 3;	packetData[0] |= (SLID&0x7);
		packetData[0] <<= 5;	packetData[0] |= (RES&0x1F);
		packetData[0] <<= 1;	packetData[0] |= (AF&0x1);
		packetData[0] <<= 10;	packetData[0] |= (RES&0x3FF);
		packetData[0] <<= 11;	packetData[0] |= (TAG&0x7FF);
		packetData[0] <<= 5;	packetData[0] |= (LNG&0x1F);
		packetData[0] <<= 7;	packetData[0] |= (CMD&0x7F);
	}
	
	for(int i=1; i<=(LNG-1)*2; i++){
		packetData[i]=DATA[i-1];
	}
	
	if(packetType == REQUEST) {
		packetData[LNG*2-1] <<= 32;		packetData[LNG*2-1] |= (0&0xFFFFFFFF);	//CRC initial value is '0' before CRC calculation
		packetData[LNG*2-1] <<= 3;		packetData[LNG*2-1] |= (RTC&0x7);
		packetData[LNG*2-1] <<= 3;		packetData[LNG*2-1] |= (SLID&0x7);
		packetData[LNG*2-1] <<= 4;		packetData[LNG*2-1] |= (RES&0xF);
		packetData[LNG*2-1] <<= 1;		packetData[LNG*2-1] |= (Pb&0x1);
		packetData[LNG*2-1] <<= 3;		packetData[LNG*2-1] |= (SEQ&0x7);
		packetData[LNG*2-1] <<= 9;		packetData[LNG*2-1] |= (FRP&0x1FF);
		packetData[LNG*2-1] <<= 9;		packetData[LNG*2-1] |= (RRP&0x1FF);
	}
	else {
		packetData[LNG*2-1] <<= 32;		packetData[LNG*2-1] |= (0&0xFFFFFFFF);	//CRC initial value is '0' before CRC calculation
		packetData[LNG*2-1] <<= 3;		packetData[LNG*2-1] |= (RTC&0x7);
		packetData[LNG*2-1] <<= 7;		packetData[LNG*2-1] |= (ERRSTAT&0x7F);
		packetData[LNG*2-1] <<= 1;		packetData[LNG*2-1] |= (DINV&0x1);
		packetData[LNG*2-1] <<= 3;		packetData[LNG*2-1] |= (SEQ&0x7);
		packetData[LNG*2-1] <<= 9;		packetData[LNG*2-1] |= (FRP&0x1FF);
		packetData[LNG*2-1] <<= 9;		packetData[LNG*2-1] |= (RRP&0x1FF);
	}
	
	unsigned char buf[8];
	uint32_t crc = 0;
	
	for(int j=0; j<LNG*2; j++){
		for(int i=0; i<8; i++){
			buf[i] = packetData[j];
			packetData[j] >>= 8;
		}
		crc = CalcCRC(buf, 8, crc, CRCtable);
	}
	return crc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x10, %rsp
movq %rdi, %rbx
movl 0x430(%rdi), %eax
leal (%rax,%rax), %r14d
movq %rsp, %r15
leaq (,%r14,8), %rcx
subq %rcx, %r15
movq %r15, %rsp
movl 0x10(%rdi), %r12d
movl 0x428(%rdi), %edx
andl $0x7, %edx
testl %r12d, %r12d
je 0x280d5
shll $0x16, %edx
movl 0x440(%rbx), %esi
movl 0x45c(%rbx), %ecx
andl $0x7, %esi
orl %edx, %esi
shll $0x6, %esi
andl $0x1, %ecx
orl %esi, %ecx
shlq $0x15, %rcx
jmp 0x280f4
movq 0x450(%rbx), %rsi
shlq $0x31, %rdx
shlq $0xc, %rsi
movabsq $0x3ffffffff000, %rcx   # imm = 0x3FFFFFFFF000
andq %rsi, %rcx
orq %rdx, %rcx
movl 0x42c(%rbx), %edx
movl 0x434(%rbx), %esi
andl $0x7ff, %edx            # imm = 0x7FF
orq %rcx, %rdx
shlq $0xc, %rdx
andl $0x1f, %eax
shll $0x7, %eax
orq %rdx, %rax
andl $0x7f, %esi
orq %rax, %rsi
movq %rsi, (%r15)
movl %r14d, %eax
addl $-0x2, %eax
je 0x2813a
movq 0x18(%rbx), %rsi
leaq 0x8(%r15), %rdi
movl %eax, %edx
shlq $0x3, %rdx
callq 0x4260
leal -0x1(%r14), %eax
movq (%r15,%rax,8), %rcx
movl 0x43c(%rbx), %edi
movl 0x448(%rbx), %edx
movl $0x1ff, %r8d            # imm = 0x1FF
andq %r8, %rdx
movl 0x44c(%rbx), %esi
andq %r8, %rsi
shlq $0x2b, %rcx
andl $0x7, %edi
shll $0x8, %edi
orq %rcx, %rdi
leaq 0x464(%rbx), %r8
xorl %r9d, %r9d
testl %r12d, %r12d
movl $0xe0, %ecx
movl $0xfe, %r10d
cmovel %ecx, %r10d
leaq 0x458(%rbx), %rcx
cmoveq %rcx, %r8
sete %cl
setne %r9b
shll $0x5, %r9d
movl 0x440(%rbx,%r9), %r9d
shlb $0x2, %cl
incb %cl
shll %cl, %r9d
andl %r10d, %r9d
orq %rdi, %r9
movl (%r8), %ecx
andl $0x1, %ecx
orq %r9, %rcx
movl 0x444(%rbx), %edi
shlq $0xc, %rcx
andl $0x7, %edi
shll $0x9, %edi
orq %rcx, %rdi
orq %rdx, %rdi
shlq $0x9, %rdi
orq %rsi, %rdi
movq %rdi, (%r15,%rax,8)
testq %r14, %r14
je 0x2822f
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
movq (%r15,%rcx,8), %rdx
xorl %esi, %esi
movb %dl, -0x28(%rbp,%rsi)
shrq $0x8, %rdx
incq %rsi
cmpq $0x8, %rsi
jne 0x281f1
movq %rdx, (%r15,%rcx,8)
xorl %edx, %edx
movb -0x28(%rbp,%rdx), %sil
xorb %al, %sil
movzbl %sil, %esi
shrl $0x8, %eax
xorl 0x20(%rbx,%rsi,4), %eax
incq %rdx
cmpl $0x8, %edx
jne 0x28208
incq %rcx
cmpq %r14, %rcx
jne 0x281eb
notl %eax
jmp 0x28231
xorl %eax, %eax
leaq -0x20(%rbp), %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq