// File: /workspace/github_c_makefile_star2_O2/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 $0x18, %rsp
movl %edx, %ebx
movq %rsi, 0x10(%rsp)
movq %rdi, %r15
rdtsc
shlq $0x20, %rdx
movl %eax, %eax
orq %rdx, %rax
leaq 0xbd58(%rip), %rcx      # 0xddb0
movq %rax, 0x58(%rcx)
leaq 0x650e(%rip), %rax      # 0x8571
leaq 0xc046(%rip), %rcx      # 0xe0b0
movq %rax, 0x58(%rcx)
leaq 0x2028(%rdi), %rax
movq %rax, 0x8(%rsp)
orl $0x4000, %ebx           # imm = 0x4000
movl 0x1c(%rdi), %eax
movl %eax, %ecx
xorl %r12d, %r12d
cmpq %r12, %rcx
je 0x2207
movq 0x28(%r15,%r12,8), %rbp
movl 0x8(%rbp), %eax
movl 0xc(%rbp), %edx
movl 0x18(%r15), %r9d
movl %eax, %r13d
subl %r9d, %r13d
je 0x20cd
leal (%rdx,%r13), %r14d
testl %r14d, %r13d
js 0x2125
testl %r13d, %r13d
sets %al
testl %r14d, %r14d
setg %dl
andb %al, %dl
cmpb $0x1, %dl
je 0x2162
incq %r12
jmp 0x2088
leaq 0xb124(%rip), %rcx      # 0xd1f8
cmpl $0x0, (%rcx)
je 0x210e
movq (%rbp), %rdi
callq 0x1f5d
movl 0x18(%r15), %ecx
movl 0x1c(%r15), %r9d
movl 0xc(%rbp), %r8d
addl %ecx, %r8d
leaq 0x648f(%rip), %rdi      # 0x8587
movq %rax, %rsi
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x10f0
movl 0xc(%rbp), %edx
movl 0x18(%r15), %eax
movq 0x10(%rbp), %rcx
movq %r15, %rdi
movq 0x10(%rsp), %rsi
movl %ebx, %r8d
movl %eax, %r9d
jmp 0x21be
leaq 0xb0cc(%rip), %rax      # 0xd1f8
cmpl $0x0, (%rax)
je 0x21cb
movq (%rbp), %rdi
callq 0x1f5d
movl 0x8(%rbp), %r8d
movl 0x18(%r15), %ecx
movl 0x1c(%r15), %r9d
leaq 0x645f(%rip), %rdi      # 0x85b0
movq %rax, %rsi
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x10f0
jmp 0x21cb
leaq 0xb08f(%rip), %rax      # 0xd1f8
cmpl $0x0, (%rax)
je 0x21a6
movq (%rbp), %rdi
callq 0x1f5d
movl 0x8(%rbp), %r8d
movl 0x18(%r15), %ecx
movl 0x1c(%r15), %r10d
leaq 0x645f(%rip), %rdi      # 0x85e9
movq %rax, %rsi
movq 0x8(%rsp), %rdx
movl %r13d, %r9d
xorl %eax, %eax
pushq %r10
pushq %r14
callq 0x10f0
popq %rcx
popq %rdx
movl 0x18(%r15), %r9d
movq 0x10(%rbp), %rcx
movslq %r13d, %rax
subq %rax, %rcx
movq %r15, %rdi
movq 0x10(%rsp), %rsi
movl %r14d, %edx
movl %ebx, %r8d
pushq $0x0
pushq $0x0
callq 0x1698
addq $0x10, %rsp
movq 0x10(%rbp), %rdi
callq 0x1030
movq %rbp, %rdi
callq 0x1030
addq $-0x18, 0x9fbc(%rip)    # 0xc1a0
movl 0x1c(%r15), %eax
cmpq %rax, %r12
jae 0x21fc
movq 0x30(%r15,%r12,8), %rcx
movq %rcx, 0x28(%r15,%r12,8)
incq %r12
jmp 0x21e8
decl %eax
movl %eax, 0x1c(%r15)
jmp 0x2083
pushq $0xb
popq %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1670