// File: /workspace/github_c_makefile_star2_O3/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, %r14d
movq %rsi, 0x10(%rsp)
rdtsc
shlq $0x20, %rdx
movl %eax, %eax
orq %rdx, %rax
leaq 0xbb64(%rip), %rcx      # 0xedb0
movq %rax, 0x58(%rcx)
leaq 0x631a(%rip), %rax      # 0x9571
leaq 0xbe52(%rip), %rcx      # 0xf0b0
movq %rax, 0x58(%rcx)
movl 0x1c(%rdi), %eax
testl %eax, %eax
je 0x370d
movq %rdi, %r15
leaq 0x2028(%rdi), %rcx
movq %rcx, 0x8(%rsp)
orl $0x4000, %r14d          # imm = 0x4000
movl %r14d, 0x4(%rsp)
movl 0x18(%r15), %r9d
movl %eax, %eax
xorl %ebx, %ebx
movq 0x28(%r15,%rbx,8), %rbp
movl 0x8(%rbp), %r13d
movl 0xc(%rbp), %edx
subl %r9d, %r13d
je 0x32d2
leal (%rdx,%r13), %r12d
testl %r12d, %r13d
js 0x341f
testl %r13d, %r13d
sets %cl
testl %r12d, %r12d
setg %dl
andb %cl, %dl
cmpb $0x1, %dl
je 0x3555
incq %rbx
cmpq %rbx, %rax
jne 0x3290
jmp 0x370d
leaq 0xaf1f(%rip), %rax      # 0xe1f8
cmpl $0x0, (%rax)
je 0x340e
movq (%rbp), %rcx
movq %rcx, %rax
shrq $0x3, %rax
movabsq $0x20c49ba5e353f7cf, %rdx # imm = 0x20C49BA5E353F7CF
mulq %rdx
movq %rdx, %rsi
movq %rcx, %rax
movabsq $0x431bde82d7b634db, %rdx # imm = 0x431BDE82D7B634DB
mulq %rdx
movq %rdx, %rdi
shrq $0x4, %rsi
shrq $0x12, %rdi
movq %rcx, %rax
shrq $0x9, %rax
movabsq $0x44b82fa09b5a53, %rdx # imm = 0x44B82FA09B5A53
mulq %rdx
movq %rdx, %r10
shrq $0xb, %r10
movq %rcx, %rax
shrq $0xb, %rax
movabsq $0x9299ff347e9e9, %rdx  # imm = 0x9299FF347E9E9
mulq %rdx
movq %rdx, %r11
shrq $0xc, %r11
movq %rcx, %rax
movabsq $-0x63a000d9128a12ab, %rdx # imm = 0x9C5FFF26ED75ED55
mulq %rdx
shrq $0x29, %rdx
imulq $-0x3e8, %rsi, %r14     # imm = 0xFC18
addq %rcx, %r14
imulq $-0x3e8, %rdi, %r13     # imm = 0xFC18
addq %rsi, %r13
imulq $-0x3e8, %r10, %r9      # imm = 0xFC18
addq %rdi, %r9
imulq $-0x3c, %r11, %r8
addq %r10, %r8
imulq $-0x3c, %rdx, %rcx
addq %r11, %rcx
movslq 0xa62b(%rip), %r12      # 0xd9c0
leal 0x1(%r12), %eax
shlq $0x7, %r12
leaq 0x9e1b(%rip), %rsi      # 0xd1c0
addq %rsi, %r12
andl $0xf, %eax
movl %eax, 0xa60f(%rip)      # 0xd9c0
imulq $0xaaaaaab, %rdx, %rax  # imm = 0xAAAAAAB
shrq $0x20, %rax
shll $0x3, %eax
leal (%rax,%rax,2), %eax
subl %eax, %edx
movq %r12, %rdi
leaq 0x6179(%rip), %rsi      # 0x9547
xorl %eax, %eax
pushq %r14
movl 0xc(%rsp), %r14d
pushq %r13
callq 0x1230
addq $0x10, %rsp
movl 0x18(%r15), %ecx
movl 0x1c(%r15), %r9d
movl 0xc(%rbp), %r8d
addl %ecx, %r8d
leaq 0x618f(%rip), %rdi      # 0x9587
movq %r12, %rsi
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x10f0
movl 0xc(%rbp), %edx
movl 0x18(%r15), %r9d
movq 0x10(%rbp), %rcx
movq %r15, %rdi
movq 0x10(%rsp), %rsi
jmp 0x36b8
leaq 0xadd2(%rip), %rax      # 0xe1f8
cmpl $0x0, (%rax)
je 0x36c8
movq (%rbp), %rcx
movq %rcx, %rax
shrq $0x3, %rax
movabsq $0x20c49ba5e353f7cf, %rdx # imm = 0x20C49BA5E353F7CF
mulq %rdx
movq %rdx, %rsi
movq %rcx, %rax
movabsq $0x431bde82d7b634db, %rdx # imm = 0x431BDE82D7B634DB
mulq %rdx
movq %rdx, %rdi
shrq $0x4, %rsi
shrq $0x12, %rdi
movq %rcx, %rax
shrq $0x9, %rax
movabsq $0x44b82fa09b5a53, %rdx # imm = 0x44B82FA09B5A53
mulq %rdx
movq %rdx, %r10
shrq $0xb, %r10
movq %rcx, %rax
shrq $0xb, %rax
movabsq $0x9299ff347e9e9, %rdx  # imm = 0x9299FF347E9E9
mulq %rdx
movq %rdx, %r11
shrq $0xc, %r11
movq %rcx, %rax
movabsq $-0x63a000d9128a12ab, %rdx # imm = 0x9C5FFF26ED75ED55
mulq %rdx
shrq $0x29, %rdx
imulq $-0x3e8, %rsi, %r12     # imm = 0xFC18
addq %rcx, %r12
imulq $-0x3e8, %rdi, %r13     # imm = 0xFC18
addq %rsi, %r13
imulq $-0x3e8, %r10, %r9      # imm = 0xFC18
addq %rdi, %r9
imulq $-0x3c, %r11, %r8
addq %r10, %r8
imulq $-0x3c, %rdx, %rcx
addq %r11, %rcx
movslq 0xa4de(%rip), %r14      # 0xd9c0
leal 0x1(%r14), %eax
shlq $0x7, %r14
leaq 0x9ccf(%rip), %rsi      # 0xd1c0
addq %rsi, %r14
andl $0xf, %eax
movl %eax, 0xa4c3(%rip)      # 0xd9c0
imulq $0xaaaaaab, %rdx, %rax  # imm = 0xAAAAAAB
shrq $0x20, %rax
shll $0x3, %eax
leal (%rax,%rax,2), %eax
subl %eax, %edx
movq %r14, %rdi
leaq 0x602d(%rip), %rsi      # 0x9547
xorl %eax, %eax
pushq %r12
pushq %r13
callq 0x1230
addq $0x10, %rsp
movl 0x8(%rbp), %r8d
movl 0x18(%r15), %ecx
movl 0x1c(%r15), %r9d
leaq 0x6074(%rip), %rdi      # 0x95b0
movq %r14, %rsi
movl 0x4(%rsp), %r14d
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x10f0
jmp 0x36c8
leaq 0xac9c(%rip), %rax      # 0xe1f8
cmpl $0x0, (%rax)
je 0x36a3
movq (%rbp), %rcx
movq %rcx, %rax
shrq $0x3, %rax
movabsq $0x20c49ba5e353f7cf, %rdx # imm = 0x20C49BA5E353F7CF
mulq %rdx
movq %rdx, %rsi
movq %rcx, %rax
movabsq $0x431bde82d7b634db, %rdx # imm = 0x431BDE82D7B634DB
mulq %rdx
movq %rdx, %rdi
shrq $0x4, %rsi
shrq $0x12, %rdi
movq %rcx, %rax
shrq $0x9, %rax
movabsq $0x44b82fa09b5a53, %rdx # imm = 0x44B82FA09B5A53
mulq %rdx
movq %rdx, %r10
shrq $0xb, %r10
movq %rcx, %rax
shrq $0xb, %rax
movabsq $0x9299ff347e9e9, %rdx  # imm = 0x9299FF347E9E9
mulq %rdx
movq %rdx, %r11
shrq $0xc, %r11
movq %rcx, %rax
movabsq $-0x63a000d9128a12ab, %rdx # imm = 0x9C5FFF26ED75ED55
mulq %rdx
shrq $0x29, %rdx
imulq $-0x3e8, %rsi, %rax     # imm = 0xFC18
addq %rcx, %rax
movq %rax, 0x20(%rsp)
imulq $-0x3e8, %rdi, %rax     # imm = 0xFC18
addq %rsi, %rax
movq %rax, 0x18(%rsp)
imulq $-0x3e8, %r10, %r9      # imm = 0xFC18
addq %rdi, %r9
imulq $-0x3c, %r11, %r8
addq %r10, %r8
imulq $-0x3c, %rdx, %rcx
addq %r11, %rcx
movslq 0xa39e(%rip), %r14      # 0xd9c0
leal 0x1(%r14), %eax
shlq $0x7, %r14
leaq 0x9b8f(%rip), %rsi      # 0xd1c0
addq %rsi, %r14
andl $0xf, %eax
movl %eax, 0xa383(%rip)      # 0xd9c0
imulq $0xaaaaaab, %rdx, %rax  # imm = 0xAAAAAAB
shrq $0x20, %rax
shll $0x3, %eax
leal (%rax,%rax,2), %eax
subl %eax, %edx
movq %r14, %rdi
leaq 0x5eed(%rip), %rsi      # 0x9547
xorl %eax, %eax
pushq 0x20(%rsp)
pushq 0x20(%rsp)
callq 0x1230
addq $0x10, %rsp
movl 0x8(%rbp), %r8d
movl 0x18(%r15), %ecx
movl 0x1c(%r15), %r10d
leaq 0x5f69(%rip), %rdi      # 0x95e9
movq %r14, %rsi
movl 0x4(%rsp), %r14d
movq 0x8(%rsp), %rdx
movl %r13d, %r9d
xorl %eax, %eax
pushq %r10
pushq %r12
callq 0x10f0
addq $0x10, %rsp
movl 0x18(%r15), %r9d
movq 0x10(%rbp), %rcx
movslq %r13d, %rax
subq %rax, %rcx
movq %r15, %rdi
movq 0x10(%rsp), %rsi
movl %r12d, %edx
movl %r14d, %r8d
xorl %eax, %eax
pushq %rax
pushq %rax
callq 0x2459
addq $0x10, %rsp
movq 0x10(%rbp), %rdi
callq 0x1030
movq %rbp, %rdi
callq 0x1030
addq $-0x18, 0x9abf(%rip)    # 0xd1a0
movl 0x1c(%r15), %eax
cmpl %ebx, %eax
jbe 0x3701
movl %ebx, %ecx
movq 0x30(%r15,%rcx,8), %rdx
movq %rdx, 0x28(%r15,%rcx,8)
leaq 0x1(%rcx), %rdx
movq %rdx, %rcx
cmpq %rdx, %rax
jne 0x36eb
decl %eax
movl %eax, 0x1c(%r15)
jne 0x3288
rdtsc
shlq $0x20, %rdx
movl %eax, %eax
leaq 0xb894(%rip), %rcx      # 0xefb0
leaq 0xb68d(%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