// File: 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
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x40(%rbp)
movl 0x430(%rax), %ecx
addl %ecx, %ecx
movq %rsp, %rdx
movq %rdx, -0x10(%rbp)
movq %rcx, %rsi
shlq $0x3, %rsi
movq %rsp, %rdx
subq %rsi, %rdx
movq %rdx, -0x38(%rbp)
movq %rdx, %rsp
movq %rcx, -0x18(%rbp)
cmpl $0x0, 0x10(%rax)
jne 0x4437a
movq -0x38(%rbp), %rax
movq -0x40(%rbp), %rcx
movq (%rax), %rdx
shlq $0x3, %rdx
movq %rdx, (%rax)
movl 0x428(%rcx), %edx
andl $0x7, %edx
movl %edx, %edx
orq (%rax), %rdx
movq %rdx, (%rax)
movq (%rax), %rdx
shlq $0x3, %rdx
movq %rdx, (%rax)
movq (%rax), %rdx
orq $0x0, %rdx
movq %rdx, (%rax)
movq (%rax), %rdx
shlq $0x22, %rdx
movq %rdx, (%rax)
movabsq $0x3ffffffff, %rdx      # imm = 0x3FFFFFFFF
andq 0x450(%rcx), %rdx
orq (%rax), %rdx
movq %rdx, (%rax)
movq (%rax), %rdx
shlq %rdx
movq %rdx, (%rax)
movq (%rax), %rdx
orq $0x0, %rdx
movq %rdx, (%rax)
movq (%rax), %rdx
shlq $0xb, %rdx
movq %rdx, (%rax)
movl 0x42c(%rcx), %edx
andl $0x7ff, %edx            # imm = 0x7FF
movl %edx, %edx
orq (%rax), %rdx
movq %rdx, (%rax)
movq (%rax), %rdx
shlq $0x5, %rdx
movq %rdx, (%rax)
movl 0x430(%rcx), %edx
andl $0x1f, %edx
movl %edx, %edx
orq (%rax), %rdx
movq %rdx, (%rax)
movq (%rax), %rdx
shlq $0x7, %rdx
movq %rdx, (%rax)
movl 0x434(%rcx), %ecx
andl $0x7f, %ecx
movslq %ecx, %rcx
orq (%rax), %rcx
movq %rcx, (%rax)
jmp 0x44463
movq -0x38(%rbp), %rax
movq -0x40(%rbp), %rcx
movq (%rax), %rdx
shlq $0x3, %rdx
movq %rdx, (%rax)
movl 0x428(%rcx), %edx
andl $0x7, %edx
movl %edx, %edx
orq (%rax), %rdx
movq %rdx, (%rax)
movq (%rax), %rdx
shlq $0x13, %rdx
movq %rdx, (%rax)
movq (%rax), %rdx
orq $0x0, %rdx
movq %rdx, (%rax)
movq (%rax), %rdx
shlq $0x3, %rdx
movq %rdx, (%rax)
movl 0x440(%rcx), %edx
andl $0x7, %edx
movl %edx, %edx
orq (%rax), %rdx
movq %rdx, (%rax)
movq (%rax), %rdx
shlq $0x5, %rdx
movq %rdx, (%rax)
movq (%rax), %rdx
orq $0x0, %rdx
movq %rdx, (%rax)
movq (%rax), %rdx
shlq %rdx
movq %rdx, (%rax)
movl 0x45c(%rcx), %edx
andl $0x1, %edx
movl %edx, %edx
orq (%rax), %rdx
movq %rdx, (%rax)
movq (%rax), %rdx
shlq $0xa, %rdx
movq %rdx, (%rax)
movq (%rax), %rdx
orq $0x0, %rdx
movq %rdx, (%rax)
movq (%rax), %rdx
shlq $0xb, %rdx
movq %rdx, (%rax)
movl 0x42c(%rcx), %edx
andl $0x7ff, %edx            # imm = 0x7FF
movl %edx, %edx
orq (%rax), %rdx
movq %rdx, (%rax)
movq (%rax), %rdx
shlq $0x5, %rdx
movq %rdx, (%rax)
movl 0x430(%rcx), %edx
andl $0x1f, %edx
movl %edx, %edx
orq (%rax), %rdx
movq %rdx, (%rax)
movq (%rax), %rdx
shlq $0x7, %rdx
movq %rdx, (%rax)
movl 0x434(%rcx), %ecx
andl $0x7f, %ecx
movslq %ecx, %rcx
orq (%rax), %rcx
movq %rcx, (%rax)
movl $0x1, -0x1c(%rbp)
movq -0x40(%rbp), %rcx
movl -0x1c(%rbp), %eax
movl 0x430(%rcx), %ecx
subl $0x1, %ecx
shll %ecx
cmpl %ecx, %eax
ja 0x444ac
movq -0x38(%rbp), %rax
movq -0x40(%rbp), %rcx
movq 0x18(%rcx), %rcx
movl -0x1c(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
movq (%rcx,%rdx,8), %rdx
movslq -0x1c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x4446a
movq -0x40(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jne 0x44690
movq -0x38(%rbp), %rax
movq -0x40(%rbp), %rcx
movl 0x430(%rcx), %edx
shll %edx
subl $0x1, %edx
movl %edx, %edx
movq (%rax,%rdx,8), %rsi
shlq $0x20, %rsi
movq %rsi, (%rax,%rdx,8)
movl 0x430(%rcx), %edx
shll %edx
subl $0x1, %edx
movl %edx, %edx
movq (%rax,%rdx,8), %rsi
orq $0x0, %rsi
movq %rsi, (%rax,%rdx,8)
movl 0x430(%rcx), %edx
shll %edx
subl $0x1, %edx
movl %edx, %edx
movq (%rax,%rdx,8), %rsi
shlq $0x3, %rsi
movq %rsi, (%rax,%rdx,8)
movl 0x43c(%rcx), %edx
andl $0x7, %edx
movl %edx, %edx
movl %edx, %esi
movl 0x430(%rcx), %edx
shll %edx
subl $0x1, %edx
movl %edx, %edx
orq (%rax,%rdx,8), %rsi
movq %rsi, (%rax,%rdx,8)
movl 0x430(%rcx), %edx
shll %edx
subl $0x1, %edx
movl %edx, %edx
movq (%rax,%rdx,8), %rsi
shlq $0x3, %rsi
movq %rsi, (%rax,%rdx,8)
movl 0x440(%rcx), %edx
andl $0x7, %edx
movl %edx, %edx
movl %edx, %esi
movl 0x430(%rcx), %edx
shll %edx
subl $0x1, %edx
movl %edx, %edx
orq (%rax,%rdx,8), %rsi
movq %rsi, (%rax,%rdx,8)
movl 0x430(%rcx), %edx
shll %edx
subl $0x1, %edx
movl %edx, %edx
movq (%rax,%rdx,8), %rsi
shlq $0x4, %rsi
movq %rsi, (%rax,%rdx,8)
movl 0x430(%rcx), %edx
shll %edx
subl $0x1, %edx
movl %edx, %edx
movq (%rax,%rdx,8), %rsi
orq $0x0, %rsi
movq %rsi, (%rax,%rdx,8)
movl 0x430(%rcx), %edx
shll %edx
subl $0x1, %edx
movl %edx, %edx
movq (%rax,%rdx,8), %rsi
shlq %rsi
movq %rsi, (%rax,%rdx,8)
movl 0x458(%rcx), %edx
andl $0x1, %edx
movl %edx, %edx
movl %edx, %esi
movl 0x430(%rcx), %edx
shll %edx
subl $0x1, %edx
movl %edx, %edx
orq (%rax,%rdx,8), %rsi
movq %rsi, (%rax,%rdx,8)
movl 0x430(%rcx), %edx
shll %edx
subl $0x1, %edx
movl %edx, %edx
movq (%rax,%rdx,8), %rsi
shlq $0x3, %rsi
movq %rsi, (%rax,%rdx,8)
movl 0x444(%rcx), %edx
andl $0x7, %edx
movl %edx, %edx
movl %edx, %esi
movl 0x430(%rcx), %edx
shll %edx
subl $0x1, %edx
movl %edx, %edx
orq (%rax,%rdx,8), %rsi
movq %rsi, (%rax,%rdx,8)
movl 0x430(%rcx), %edx
shll %edx
subl $0x1, %edx
movl %edx, %edx
movq (%rax,%rdx,8), %rsi
shlq $0x9, %rsi
movq %rsi, (%rax,%rdx,8)
movl 0x448(%rcx), %edx
andl $0x1ff, %edx            # imm = 0x1FF
movl %edx, %edx
movl %edx, %esi
movl 0x430(%rcx), %edx
shll %edx
subl $0x1, %edx
movl %edx, %edx
orq (%rax,%rdx,8), %rsi
movq %rsi, (%rax,%rdx,8)
movl 0x430(%rcx), %edx
shll %edx
subl $0x1, %edx
movl %edx, %edx
movq (%rax,%rdx,8), %rsi
shlq $0x9, %rsi
movq %rsi, (%rax,%rdx,8)
movl 0x44c(%rcx), %edx
andl $0x1ff, %edx            # imm = 0x1FF
movl %edx, %edx
movl 0x430(%rcx), %ecx
shll %ecx
subl $0x1, %ecx
movl %ecx, %ecx
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
jmp 0x4482f
movq -0x38(%rbp), %rax
movq -0x40(%rbp), %rcx
movl 0x430(%rcx), %edx
shll %edx
subl $0x1, %edx
movl %edx, %edx
movq (%rax,%rdx,8), %rsi
shlq $0x20, %rsi
movq %rsi, (%rax,%rdx,8)
movl 0x430(%rcx), %edx
shll %edx
subl $0x1, %edx
movl %edx, %edx
movq (%rax,%rdx,8), %rsi
orq $0x0, %rsi
movq %rsi, (%rax,%rdx,8)
movl 0x430(%rcx), %edx
shll %edx
subl $0x1, %edx
movl %edx, %edx
movq (%rax,%rdx,8), %rsi
shlq $0x3, %rsi
movq %rsi, (%rax,%rdx,8)
movl 0x43c(%rcx), %edx
andl $0x7, %edx
movl %edx, %edx
movl %edx, %esi
movl 0x430(%rcx), %edx
shll %edx
subl $0x1, %edx
movl %edx, %edx
orq (%rax,%rdx,8), %rsi
movq %rsi, (%rax,%rdx,8)
movl 0x430(%rcx), %edx
shll %edx
subl $0x1, %edx
movl %edx, %edx
movq (%rax,%rdx,8), %rsi
shlq $0x7, %rsi
movq %rsi, (%rax,%rdx,8)
movl 0x460(%rcx), %edx
andl $0x7f, %edx
movl %edx, %edx
movl %edx, %esi
movl 0x430(%rcx), %edx
shll %edx
subl $0x1, %edx
movl %edx, %edx
orq (%rax,%rdx,8), %rsi
movq %rsi, (%rax,%rdx,8)
movl 0x430(%rcx), %edx
shll %edx
subl $0x1, %edx
movl %edx, %edx
movq (%rax,%rdx,8), %rsi
shlq %rsi
movq %rsi, (%rax,%rdx,8)
movl 0x464(%rcx), %edx
andl $0x1, %edx
movl %edx, %edx
movl %edx, %esi
movl 0x430(%rcx), %edx
shll %edx
subl $0x1, %edx
movl %edx, %edx
orq (%rax,%rdx,8), %rsi
movq %rsi, (%rax,%rdx,8)
movl 0x430(%rcx), %edx
shll %edx
subl $0x1, %edx
movl %edx, %edx
movq (%rax,%rdx,8), %rsi
shlq $0x3, %rsi
movq %rsi, (%rax,%rdx,8)
movl 0x444(%rcx), %edx
andl $0x7, %edx
movl %edx, %edx
movl %edx, %esi
movl 0x430(%rcx), %edx
shll %edx
subl $0x1, %edx
movl %edx, %edx
orq (%rax,%rdx,8), %rsi
movq %rsi, (%rax,%rdx,8)
movl 0x430(%rcx), %edx
shll %edx
subl $0x1, %edx
movl %edx, %edx
movq (%rax,%rdx,8), %rsi
shlq $0x9, %rsi
movq %rsi, (%rax,%rdx,8)
movl 0x448(%rcx), %edx
andl $0x1ff, %edx            # imm = 0x1FF
movl %edx, %edx
movl %edx, %esi
movl 0x430(%rcx), %edx
shll %edx
subl $0x1, %edx
movl %edx, %edx
orq (%rax,%rdx,8), %rsi
movq %rsi, (%rax,%rdx,8)
movl 0x430(%rcx), %edx
shll %edx
subl $0x1, %edx
movl %edx, %edx
movq (%rax,%rdx,8), %rsi
shlq $0x9, %rsi
movq %rsi, (%rax,%rdx,8)
movl 0x44c(%rcx), %edx
andl $0x1ff, %edx            # imm = 0x1FF
movl %edx, %edx
movl 0x430(%rcx), %ecx
shll %ecx
subl $0x1, %ecx
movl %ecx, %ecx
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
movl $0x0, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x40(%rbp), %rcx
movl -0x2c(%rbp), %eax
movl 0x430(%rcx), %ecx
shll %ecx
cmpl %ecx, %eax
jae 0x448b8
movl $0x0, -0x30(%rbp)
cmpl $0x8, -0x30(%rbp)
jge 0x4488e
movq -0x38(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movb %cl, %dl
movslq -0x30(%rbp), %rcx
movb %dl, -0x24(%rbp,%rcx)
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
shrq $0x8, %rdx
movq %rdx, (%rax,%rcx,8)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x44857
movq -0x40(%rbp), %rdi
leaq -0x24(%rbp), %rsi
movl -0x28(%rbp), %ecx
movq %rdi, %r8
addq $0x20, %r8
movl $0x8, %edx
callq 0x448d0
movl %eax, -0x28(%rbp)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x4483d
movl -0x28(%rbp), %eax
movq -0x10(%rbp), %rcx
movq %rcx, %rsp
movq %rbp, %rsp
popq %rbp
retq
nopw (%rax,%rax)