// File: /workspace/github_cpp_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movl 0x430(%rdi), %ecx
leal (%rcx,%rcx), %eax
movq %rsp, %r13
movq %rax, -0x30(%rbp)
leaq (,%rax,8), %rax
subq %rax, %r13
movq %r13, %rsp
movl 0x10(%rdi), %eax
movl 0x428(%rdi), %esi
andl $0x7, %esi
testl %eax, %eax
je 0x1e128
shll $0x16, %esi
movl 0x440(%rbx), %edi
movl 0x45c(%rbx), %edx
andl $0x7, %edi
orl %esi, %edi
shll $0x6, %edi
andl $0x1, %edx
orl %edi, %edx
shlq $0x15, %rdx
jmp 0x1e147
movq 0x450(%rbx), %rdi
shlq $0x31, %rsi
shlq $0xc, %rdi
movabsq $0x3ffffffff000, %rdx   # imm = 0x3FFFFFFFF000
andq %rdi, %rdx
orq %rsi, %rdx
movl 0x42c(%rbx), %esi
movl 0x434(%rbx), %edi
andl $0x7ff, %esi            # imm = 0x7FF
orq %rdx, %rsi
shlq $0xc, %rsi
andl $0x1f, %ecx
shll $0x7, %ecx
orq %rsi, %rcx
andl $0x7f, %edi
orq %rcx, %rdi
movq %rdi, (%r13)
movq 0x18(%rbx), %rcx
movq -0x30(%rbp), %rsi
leal -0x1(%rsi), %edx
addl $-0x2, %esi
xorl %edi, %edi
cmpq %rdi, %rsi
je 0x1e196
movq (%rcx,%rdi,8), %r8
movq %r8, 0x8(%r13,%rdi,8)
incq %rdi
jmp 0x1e183
movq (%r13,%rdx,8), %rcx
movl 0x43c(%rbx), %r8d
movl 0x448(%rbx), %esi
movl $0x1ff, %r9d            # imm = 0x1FF
andq %r9, %rsi
movl 0x44c(%rbx), %edi
andq %r9, %rdi
shlq $0x2b, %rcx
andl $0x7, %r8d
shll $0x8, %r8d
orq %rcx, %r8
leaq 0x464(%rbx), %r9
leaq 0x458(%rbx), %r10
xorl %r11d, %r11d
testl %eax, %eax
sete %cl
setne %r11b
movl $0xe0, %eax
movl $0xfe, %r14d
cmovel %eax, %r14d
cmoveq %r10, %r9
shll $0x5, %r11d
movl 0x440(%rbx,%r11), %r10d
shlb $0x2, %cl
incb %cl
shll %cl, %r10d
movl 0x444(%rbx), %eax
andl %r14d, %r10d
orq %r8, %r10
movl (%r9), %ecx
andl $0x1, %ecx
orq %r10, %rcx
shlq $0xc, %rcx
andl $0x7, %eax
shll $0x9, %eax
orq %rcx, %rax
orq %rsi, %rax
shlq $0x9, %rax
orq %rdi, %rax
movq %rax, (%r13,%rdx,8)
addq $0x20, %rbx
xorl %r12d, %r12d
leaq -0x38(%rbp), %r14
pushq $0x8
popq %r15
xorl %eax, %eax
cmpq -0x30(%rbp), %r12
je 0x1e286
movq (%r13,%r12,8), %rcx
xorl %edx, %edx
cmpq $0x8, %rdx
je 0x1e26c
movb %cl, -0x38(%rbp,%rdx)
shrq $0x8, %rcx
incq %rdx
jmp 0x1e259
movq %rcx, (%r13,%r12,8)
movq %r14, %rsi
movl %r15d, %edx
movl %eax, %ecx
movq %rbx, %r8
callq 0x1e298
incq %r12
jmp 0x1e24c
movl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop