// File: /workspace/github_c_makefile_star2_O1/fmadio[P]pcap_flow/tcpstream.c
// Function: fTCPStream_Reassembly
static void fTCPStream_Reassembly(TCPStream_t* S, u64 TS, u32 Flag)
{
	fProfile_Start(11, "TCP PktAdd Reassembly");
	while (true)
	{
		bool Hit = false;
		for (int i=0; i < S->BufferListPos; i++)
		{
			TCPBuffer_t* Buffer = S->BufferList[i];

			s32 dSeqStart = Buffer->SeqNo - S->SeqNo;
			s32 dSeqEnd   = Buffer->SeqNo - S->SeqNo + Buffer->Length;

			// perfectly aligned start packet
			if (dSeqStart == 0)
			{
				if (g_Verbose) printf("[%s] [%s] reassembly hit Seq:%i:%i : %i\n", FormatTS(Buffer->TS), S->Path, S->SeqNo, S->SeqNo + Buffer->Length, S->BufferListPos);
				fTCPStream_OutputPayload(S, TS, Buffer->Length, Buffer->Payload, Flag | TCPHEADER_FLAG_REASSEMBLE, S->SeqNo, 0, 0);
				Hit = true;
			}
			// redundant packet fully before the current packet 
			else if ((dSeqStart < 0) && (dSeqEnd < 0))
			{
				if (g_Verbose) printf("[%s] [%s] redundant packet hit Seq:%i BufferSeq:%i : %i\n", FormatTS(Buffer->TS), S->Path, S->SeqNo, Buffer->SeqNo, S->BufferListPos);
				Hit = true;
			}
			// partialy straddled packet. started before the current Seq number but adds to the seq no
			else if ((dSeqStart < 0) && (dSeqEnd > 0))
			{
				if (g_Verbose) printf("[%s] [%s] partial packet hit Seq:%i BufferSeq:%i : %i %i : %i\n", FormatTS(Buffer->TS), S->Path, S->SeqNo, Buffer->SeqNo, dSeqStart, dSeqEnd, S->BufferListPos);
				fTCPStream_OutputPayload(S, TS, dSeqEnd, Buffer->Payload  - dSeqStart, Flag | TCPHEADER_FLAG_REASSEMBLE, S->SeqNo, 0, 0);

				Hit = true;
			}

			// free and remove buffer 
			if (Hit)
			{
				free(Buffer->Payload);
				Buffer->Payload = NULL;	
				free(Buffer);

				s_TCPBufferMemoryTotal -= sizeof(TCPBuffer_t);

				for (int j=i; j < S->BufferListPos; j++)
				{
					S->BufferList[j] = S->BufferList[j+1];
				}
				S->BufferListPos--;
				break;
			}
		}
		if (!Hit) break;
	}
	fProfile_Stop(11);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %edx, 0x4(%rsp)
movq %rsi, 0x18(%rsp)
rdtsc
shlq $0x20, %rdx
movl %eax, %eax
orq %rdx, %rax
leaq 0xbae4(%rip), %rcx      # 0xedb0
movq %rax, 0x58(%rcx)
leaq 0x629a(%rip), %rax      # 0x9571
leaq 0xbdd2(%rip), %rcx      # 0xf0b0
movq %rax, 0x58(%rcx)
cmpl $0x0, 0x1c(%rdi)
je 0x37de
movq %rdi, %r15
leaq 0x2028(%rdi), %rax
movq %rax, 0x10(%rsp)
orl $0x4000, 0x4(%rsp)      # imm = 0x4000
xorl %ebx, %ebx
xorl %r14d, %r14d
movq 0x28(%r15,%rbx,8), %rbp
movl 0x8(%rbp), %r12d
movl 0x18(%r15), %eax
movl %r12d, %r13d
subl %eax, %r13d
subl %eax, %r12d
jne 0x34ed
leaq 0xaecd(%rip), %rax      # 0xe1f8
cmpl $0x0, (%rax)
je 0x3461
movq (%rbp), %rsi
movq %rsi, %rax
shrq $0x3, %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
mulq %rcx
movq %rdx, %rdi
shrq $0x4, %rdi
movq %rsi, %rax
movabsq $0x431bde82d7b634db, %rcx # imm = 0x431BDE82D7B634DB
mulq %rcx
movq %rdx, %r8
shrq $0x12, %r8
movq %rsi, %rax
shrq $0x9, %rax
movabsq $0x44b82fa09b5a53, %rcx # imm = 0x44B82FA09B5A53
mulq %rcx
movq %rdx, %rcx
movq %rsi, %rax
shrq $0xb, %rax
movabsq $0x9299ff347e9e9, %rdx  # imm = 0x9299FF347E9E9
mulq %rdx
movq %rdx, %r11
shrq $0xb, %rcx
shrq $0xc, %r11
movq %rsi, %rax
movabsq $-0x63a000d9128a12ab, %rdx # imm = 0x9C5FFF26ED75ED55
mulq %rdx
movq %rdx, %r10
shrq $0x29, %r10
imulq $-0x3e8, %rdi, %r14     # imm = 0xFC18
addq %rsi, %r14
imulq $-0x3e8, %r8, %r13      # imm = 0xFC18
addq %rdi, %r13
imulq $-0x3e8, %rcx, %r9      # imm = 0xFC18
addq %r8, %r9
imulq $-0x3c, %r11, %r8
addq %rcx, %r8
imulq $-0x3c, %r10, %rcx
addq %r11, %rcx
movslq 0xa5d6(%rip), %r12      # 0xd9c0
leal 0x1(%r12), %esi
andl $0xf, %esi
movq %r10, %rax
movabsq $0xaaaaaaaaaaaaaab, %rdx # imm = 0xAAAAAAAAAAAAAAB
mulq %rdx
movl %esi, 0xa5b8(%rip)      # 0xd9c0
shll $0x3, %edx
leaq (%rdx,%rdx,2), %rax
subq %rax, %r10
shlq $0x7, %r12
leaq 0x9da3(%rip), %rax      # 0xd1c0
addq %rax, %r12
movq %r12, %rdi
leaq 0x611d(%rip), %rsi      # 0x9547
movq %r10, %rdx
xorl %eax, %eax
pushq %r14
pushq %r13
callq 0x1220
addq $0x10, %rsp
movl 0x18(%r15), %ecx
movl 0x1c(%r15), %r9d
movl 0xc(%rbp), %r8d
addl %ecx, %r8d
leaq 0x6135(%rip), %rdi      # 0x9587
movq %r12, %rsi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x10f0
movl 0xc(%rbp), %edx
movq 0x10(%rbp), %rcx
movl 0x18(%r15), %r9d
movq %r15, %rdi
movq 0x18(%rsp), %rsi
movl 0x4(%rsp), %r8d
xorl %eax, %eax
pushq %rax
pushq %rax
callq 0x2492
addq $0x10, %rsp
movl $0x1, %r14d
testl %r14d, %r14d
je 0x3642
movq 0x10(%rbp), %rdi
callq 0x1030
movq $0x0, 0x10(%rbp)
movq %rbp, %rdi
callq 0x1030
addq $-0x18, 0x9cea(%rip)    # 0xd1a0
movl 0x1c(%r15), %eax
cmpl %ebx, %eax
jbe 0x34d4
movq 0x30(%r15,%rbx,8), %rcx
movq %rcx, 0x28(%r15,%rbx,8)
leaq 0x1(%rbx), %rcx
movq %rcx, %rbx
cmpq %rcx, %rax
jne 0x34be
xorl %ebx, %ebx
movl $0x0, %r14d
decl %eax
movl %eax, 0x1c(%r15)
jne 0x3308
jmp 0x37de
addl 0xc(%rbp), %r13d
testl %r13d, %r12d
jns 0x3657
movl $0x1, %r14d
leaq 0xacf1(%rip), %rax      # 0xe1f8
cmpl $0x0, (%rax)
je 0x348c
movq (%rbp), %rsi
movq %rsi, %rax
shrq $0x3, %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
mulq %rcx
movq %rdx, %rdi
shrq $0x4, %rdi
movq %rsi, %rax
movabsq $0x431bde82d7b634db, %rcx # imm = 0x431BDE82D7B634DB
mulq %rcx
movq %rdx, %r8
shrq $0x12, %r8
movq %rsi, %rax
shrq $0x9, %rax
movabsq $0x44b82fa09b5a53, %rcx # imm = 0x44B82FA09B5A53
mulq %rcx
movq %rdx, %rcx
movq %rsi, %rax
shrq $0xb, %rax
movabsq $0x9299ff347e9e9, %rdx  # imm = 0x9299FF347E9E9
mulq %rdx
movq %rdx, %r11
shrq $0xb, %rcx
shrq $0xc, %r11
movq %rsi, %rax
movabsq $-0x63a000d9128a12ab, %rdx # imm = 0x9C5FFF26ED75ED55
mulq %rdx
movq %rdx, %r10
shrq $0x29, %r10
imulq $-0x3e8, %rdi, %rax     # imm = 0xFC18
addq %rsi, %rax
movq %rax, 0x8(%rsp)
imulq $-0x3e8, %r8, %r13      # imm = 0xFC18
addq %rdi, %r13
imulq $-0x3e8, %rcx, %r9      # imm = 0xFC18
addq %r8, %r9
imulq $-0x3c, %r11, %r8
addq %rcx, %r8
imulq $-0x3c, %r10, %rcx
addq %r11, %rcx
movslq 0xa3f9(%rip), %r12      # 0xd9c0
leal 0x1(%r12), %esi
andl $0xf, %esi
movq %r10, %rax
movabsq $0xaaaaaaaaaaaaaab, %rdx # imm = 0xAAAAAAAAAAAAAAB
mulq %rdx
movl %esi, 0xa3db(%rip)      # 0xd9c0
shll $0x3, %edx
leaq (%rdx,%rdx,2), %rax
subq %rax, %r10
shlq $0x7, %r12
leaq 0x9bc6(%rip), %rax      # 0xd1c0
addq %rax, %r12
movq %r12, %rdi
leaq 0x5f40(%rip), %rsi      # 0x9547
movq %r10, %rdx
xorl %eax, %eax
pushq 0x8(%rsp)
pushq %r13
callq 0x1220
addq $0x10, %rsp
movl 0x8(%rbp), %r8d
movl 0x18(%r15), %ecx
movl 0x1c(%r15), %r9d
leaq 0x5f82(%rip), %rdi      # 0x95b0
movq %r12, %rsi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x10f0
jmp 0x348c
incq %rbx
movl 0x1c(%r15), %eax
cmpq %rax, %rbx
jb 0x3308
jmp 0x37de
testl %r12d, %r12d
sets %al
testl %r13d, %r13d
setg %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x348c
leaq 0xab83(%rip), %rax      # 0xe1f8
cmpl $0x0, (%rax)
je 0x37c0
movq (%rbp), %rsi
movq %rsi, %rax
shrq $0x3, %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
mulq %rcx
movq %rdx, %rdi
shrq $0x4, %rdi
movq %rsi, %rax
movabsq $0x431bde82d7b634db, %rcx # imm = 0x431BDE82D7B634DB
mulq %rcx
movq %rdx, %r8
shrq $0x12, %r8
movq %rsi, %rax
shrq $0x9, %rax
movabsq $0x44b82fa09b5a53, %rcx # imm = 0x44B82FA09B5A53
mulq %rcx
movq %rdx, %rcx
movq %rsi, %rax
shrq $0xb, %rax
movabsq $0x9299ff347e9e9, %rdx  # imm = 0x9299FF347E9E9
mulq %rdx
movq %rdx, %r11
shrq $0xb, %rcx
shrq $0xc, %r11
movq %rsi, %rax
movabsq $-0x63a000d9128a12ab, %rdx # imm = 0x9C5FFF26ED75ED55
mulq %rdx
movq %rdx, %r10
shrq $0x29, %r10
imulq $-0x3e8, %rdi, %rax     # imm = 0xFC18
addq %rsi, %rax
movq %rax, 0x8(%rsp)
imulq $-0x3e8, %r8, %rax      # imm = 0xFC18
addq %rdi, %rax
movq %rax, 0x20(%rsp)
imulq $-0x3e8, %rcx, %r9      # imm = 0xFC18
addq %r8, %r9
imulq $-0x3c, %r11, %r8
addq %rcx, %r8
imulq $-0x3c, %r10, %rcx
addq %r11, %rcx
movslq 0xa282(%rip), %r14      # 0xd9c0
leal 0x1(%r14), %esi
andl $0xf, %esi
movq %r10, %rax
movabsq $0xaaaaaaaaaaaaaab, %rdx # imm = 0xAAAAAAAAAAAAAAB
mulq %rdx
movl %esi, 0xa265(%rip)      # 0xd9c0
shll $0x3, %edx
leaq (%rdx,%rdx,2), %rax
subq %rax, %r10
shlq $0x7, %r14
leaq 0x9a50(%rip), %rax      # 0xd1c0
addq %rax, %r14
movq %r14, %rdi
leaq 0x5dca(%rip), %rsi      # 0x9547
movq %r10, %rdx
xorl %eax, %eax
pushq 0x8(%rsp)
pushq 0x28(%rsp)
callq 0x1220
addq $0x10, %rsp
movl 0x8(%rbp), %r8d
movl 0x18(%r15), %ecx
movl 0x1c(%r15), %r10d
leaq 0x5e43(%rip), %rdi      # 0x95e9
movq %r14, %rsi
movq 0x10(%rsp), %rdx
movl %r12d, %r9d
xorl %eax, %eax
pushq %r10
pushq %r13
callq 0x10f0
addq $0x10, %rsp
movq 0x10(%rbp), %rcx
movslq %r12d, %rax
subq %rax, %rcx
movl 0x18(%r15), %r9d
movq %r15, %rdi
movq 0x18(%rsp), %rsi
movl %r13d, %edx
jmp 0x3474
rdtsc
shlq $0x20, %rdx
movl %eax, %eax
leaq 0xb7c3(%rip), %rcx      # 0xefb0
leaq 0xb5bc(%rip), %rsi      # 0xedb0
subq 0x58(%rsi), %rax
addq 0x58(%rcx), %rax
addq %rdx, %rax
movq %rax, 0x58(%rcx)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq