// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x48, %rsp
movq %rdi, 0x40(%rsp)
movq %rsi, 0x38(%rsp)
movl %edx, 0x34(%rsp)
movl $0xb, %edi
leaq 0x9c27(%rip), %rsi      # 0xc575
callq 0x17f0
movl $0x0, 0x30(%rsp)
movl $0x0, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
movq 0x40(%rsp), %rcx
cmpl 0x1c(%rcx), %eax
jae 0x2c57
movq 0x40(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movq 0x28(%rax,%rcx,8), %rax
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
movl 0x8(%rax), %eax
movq 0x40(%rsp), %rcx
subl 0x18(%rcx), %eax
movl %eax, 0x1c(%rsp)
movq 0x20(%rsp), %rax
movl 0x8(%rax), %eax
movq 0x40(%rsp), %rcx
subl 0x18(%rcx), %eax
movq 0x20(%rsp), %rcx
addl 0xc(%rcx), %eax
movl %eax, 0x18(%rsp)
cmpl $0x0, 0x1c(%rsp)
jne 0x2a70
leaq 0xe83d(%rip), %rax      # 0x11208
cmpl $0x0, (%rax)
je 0x2a1d
movq 0x20(%rsp), %rax
movq (%rax), %rdi
callq 0x2730
movq %rax, %rsi
movq 0x40(%rsp), %rdx
addq $0x2028, %rdx           # imm = 0x2028
movq 0x40(%rsp), %rax
movl 0x18(%rax), %ecx
movq 0x40(%rsp), %rax
movl 0x18(%rax), %r8d
movq 0x20(%rsp), %rax
addl 0xc(%rax), %r8d
movq 0x40(%rsp), %rax
movl 0x1c(%rax), %r9d
leaq 0x9b75(%rip), %rdi      # 0xc58b
movb $0x0, %al
callq 0x10e0
movq 0x40(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x20(%rsp), %rax
movl 0xc(%rax), %edx
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rcx
movl 0x34(%rsp), %r8d
orl $0x4000, %r8d           # imm = 0x4000
movq 0x40(%rsp), %rax
movl 0x18(%rax), %r9d
xorl %eax, %eax
movl $0x0, (%rsp)
movl $0x0, 0x8(%rsp)
callq 0x18a0
movl $0x1, 0x30(%rsp)
jmp 0x2bad
cmpl $0x0, 0x1c(%rsp)
jge 0x2adb
cmpl $0x0, 0x18(%rsp)
jge 0x2adb
leaq 0xe783(%rip), %rax      # 0x11208
cmpl $0x0, (%rax)
je 0x2ace
movq 0x20(%rsp), %rax
movq (%rax), %rdi
callq 0x2730
movq %rax, %rsi
movq 0x40(%rsp), %rdx
addq $0x2028, %rdx           # imm = 0x2028
movq 0x40(%rsp), %rax
movl 0x18(%rax), %ecx
movq 0x20(%rsp), %rax
movl 0x8(%rax), %r8d
movq 0x40(%rsp), %rax
movl 0x1c(%rax), %r9d
leaq 0x9aed(%rip), %rdi      # 0xc5b4
movb $0x0, %al
callq 0x10e0
movl $0x1, 0x30(%rsp)
jmp 0x2bab
cmpl $0x0, 0x1c(%rsp)
jge 0x2ba9
cmpl $0x0, 0x18(%rsp)
jle 0x2ba9
leaq 0xe710(%rip), %rax      # 0x11208
cmpl $0x0, (%rax)
je 0x2b52
movq 0x20(%rsp), %rax
movq (%rax), %rdi
callq 0x2730
movq %rax, %rsi
movq 0x40(%rsp), %rdx
addq $0x2028, %rdx           # imm = 0x2028
movq 0x40(%rsp), %rax
movl 0x18(%rax), %ecx
movq 0x20(%rsp), %rax
movl 0x8(%rax), %r8d
movl 0x1c(%rsp), %r9d
movl 0x18(%rsp), %r10d
movq 0x40(%rsp), %rax
movl 0x1c(%rax), %eax
leaq 0x9aaa(%rip), %rdi      # 0xc5ed
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
movb $0x0, %al
callq 0x10e0
movq 0x40(%rsp), %rdi
movq 0x38(%rsp), %rsi
movl 0x18(%rsp), %edx
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rcx
movslq 0x1c(%rsp), %r8
xorl %eax, %eax
subq %r8, %rax
addq %rax, %rcx
movl 0x34(%rsp), %r8d
orl $0x4000, %r8d           # imm = 0x4000
movq 0x40(%rsp), %rax
movl 0x18(%rax), %r9d
xorl %eax, %eax
movl $0x0, (%rsp)
movl $0x0, 0x8(%rsp)
callq 0x18a0
movl $0x1, 0x30(%rsp)
jmp 0x2bab
jmp 0x2bad
cmpl $0x0, 0x30(%rsp)
je 0x2c45
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x1030
movq 0x20(%rsp), %rax
movq $0x0, 0x10(%rax)
movq 0x20(%rsp), %rdi
callq 0x1030
movq 0xd5cc(%rip), %rax      # 0x101b0
subq $0x18, %rax
movq %rax, 0xd5c1(%rip)      # 0x101b0
movl 0x2c(%rsp), %eax
movl %eax, 0x14(%rsp)
movl 0x14(%rsp), %eax
movq 0x40(%rsp), %rcx
cmpl 0x1c(%rcx), %eax
jae 0x2c35
movq 0x40(%rsp), %rax
movl 0x14(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq 0x28(%rax,%rcx,8), %rdx
movq 0x40(%rsp), %rax
movslq 0x14(%rsp), %rcx
movq %rdx, 0x28(%rax,%rcx,8)
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x2bf7
movq 0x40(%rsp), %rax
movl 0x1c(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x1c(%rax)
jmp 0x2c57
jmp 0x2c47
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x2963
cmpl $0x0, 0x30(%rsp)
jne 0x2c60
jmp 0x2c65
jmp 0x2953
movl $0xb, %edi
callq 0x1860
addq $0x48, %rsp
retq
nopw %cs:(%rax,%rax)