// File: /workspace/github_c_makefile_star2_O0/fmadio[P]pcap_flow/main.c
// Function: FlowAdd
static u32 FlowAdd(FlowHash_t* Flow, u32 PktLength, u64 TS) 
{
	if (s_FlowListPos >= s_FlowListMax) return 0;

	FlowHash_t* F 	= NULL; 

	// first level has is 24b index, followed by list of leaf nodes
	Flow->Hash 		= FlowHash(Flow->Type, (u8*)Flow->Data, 64);
	u32 Index 		= Flow->Hash & s_FlowIndexMask;

	u32 FlowIndex 	= 0;
	bool IsFlowNew 	= false;

	u32 HashDepth	= 0;

	if (s_FlowIndex[Index] != 0)
	{
		F = s_FlowList +  s_FlowIndex[Index];
		bool Found = false;
		for (int t=0; t < 1e6; t++)
		{
			HashDepth++;

			// flow matched
			if (memcmp(F->Data, Flow->Data, 64) == 0)
			{
				Found = true;
				break;
			}

			if (F->Next == 0) break;
			F = s_FlowList + F->Next;
			assert(t < 99e3);
		}

		// keep stats on the max depth
		if (s_FlowIndexDepthMax  < HashDepth)
		{
			fprintf(stderr, "Hash New Max Depth: %i : %08x : ", HashDepth, Flow->Hash); 
			for (int i=0; i < 64/8; i++) fprintf(stderr, "%016llx ", Flow->Data[i]);
			fprintf(stderr, "\n");

			s_FlowIndexDepthMax = HashDepth;
		}
		s_FlowIndexDepthS0	+= 1; 
		s_FlowIndexDepthS1	+= HashDepth; 

		// new flow
		if (Found)
		{
		}
		else
		{
			F = &s_FlowList[ s_FlowListPos++ ];
			assert(s_FlowListPos < s_FlowListMax);

			memcpy(F, Flow, sizeof(FlowHash_t));
			F->Next = s_FlowIndex[Index];

			s_FlowIndex[Index] = F - s_FlowList;

			IsFlowNew = true; 
		}
	}
	else
	{
		F = &s_FlowList[ s_FlowListPos++ ];
		assert(s_FlowListPos < s_FlowListMax);

		memcpy(F, Flow, sizeof(FlowHash_t));
		F->Next = 0;

		s_FlowIndex[Index] = F - s_FlowList; 
		IsFlowNew = true; 
	}

	// update stats

	F->PktCnt++;
	F->Bytes += PktLength;
	F->TSFirst	= IsFlowNew ? TS : F->TSFirst;
	F->TSLast	= TS;

	// update flow log
	if (IsFlowNew && s_EnableFlowLog)
	{
		u32 ID = F - s_FlowList;
		switch (F->Type)
		{
		case FLOW_TYPE_TCP: PrintFlowTCP(s_FlowLogFile, F, ID, s_FlowListPos); break;
		case FLOW_TYPE_UDP: PrintFlowUDP(s_FlowLogFile, F, ID, s_FlowListPos); break;
		}
	}
	return F - s_FlowList;
}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x48(%rsp)
movl %esi, 0x44(%rsp)
movq %rdx, 0x38(%rsp)
movl 0x849c(%rip), %eax      # 0x10164
cmpl 0x9882(%rip), %eax      # 0x11550
jb 0x7cdd
movl $0x0, 0x54(%rsp)
jmp 0x8153
movq $0x0, 0x30(%rsp)
movq 0x48(%rsp), %rax
movl 0x44(%rax), %edi
movq 0x48(%rsp), %rsi
movl $0x40, %edx
callq 0x8670
movl %eax, %ecx
movq 0x48(%rsp), %rax
movl %ecx, 0x40(%rax)
movq 0x48(%rsp), %rax
movl 0x40(%rax), %eax
andq 0x9832(%rip), %rax      # 0x11548
movl %eax, 0x2c(%rsp)
movl $0x0, 0x28(%rsp)
movl $0x0, 0x24(%rsp)
movl $0x0, 0x20(%rsp)
movq 0x9807(%rip), %rax      # 0x11540
movl 0x2c(%rsp), %ecx
cmpl $0x0, (%rax,%rcx,4)
je 0x7faa
movq 0x980a(%rip), %rax      # 0x11558
movq 0x97eb(%rip), %rcx      # 0x11540
movl 0x2c(%rsp), %edx
movl (%rcx,%rdx,4), %ecx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x30(%rsp)
movl $0x0, 0x1c(%rsp)
movl $0x0, 0x18(%rsp)
cvtsi2sdl 0x18(%rsp), %xmm1
movsd 0x4942(%rip), %xmm0     # 0xc6c8
ucomisd %xmm1, %xmm0
jbe 0x7e2d
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
movq 0x30(%rsp), %rdi
movq 0x48(%rsp), %rsi
movl $0x40, %edx
callq 0x1120
cmpl $0x0, %eax
jne 0x7dbe
movl $0x1, 0x1c(%rsp)
jmp 0x7e2d
movq 0x30(%rsp), %rax
cmpl $0x0, 0x6c(%rax)
jne 0x7dcb
jmp 0x7e2d
movq 0x9786(%rip), %rax      # 0x11558
movq 0x30(%rsp), %rcx
movl 0x6c(%rcx), %ecx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x30(%rsp)
cvtsi2sdl 0x18(%rsp), %xmm1
movsd 0x48fc(%rip), %xmm0     # 0xc6f0
ucomisd %xmm1, %xmm0
jbe 0x7dfc
jmp 0x7e1b
leaq 0x5bc1(%rip), %rdi      # 0xd9c4
leaq 0x4ac8(%rip), %rsi      # 0xc8d2
movl $0x22d, %edx            # imm = 0x22D
leaq 0x5bb7(%rip), %rcx      # 0xd9cd
callq 0x1100
jmp 0x7e1d
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0x7d78
movl 0x975d(%rip), %eax      # 0x11590
cmpl 0x20(%rsp), %eax
jae 0x7ec5
movq 0x81b4(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movl 0x20(%rsp), %edx
movq 0x48(%rsp), %rax
movl 0x40(%rax), %ecx
leaq 0x5b97(%rip), %rsi      # 0xd9f1
movb $0x0, %al
callq 0x1150
movl $0x0, 0x14(%rsp)
cmpl $0x8, 0x14(%rsp)
jge 0x7ea3
movq 0x8181(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movq 0x48(%rsp), %rax
movslq 0x14(%rsp), %rcx
movq (%rax,%rcx,8), %rdx
leaq 0x5b83(%rip), %rsi      # 0xda12
movb $0x0, %al
callq 0x1150
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x7e69
movq 0x814e(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x45da(%rip), %rsi      # 0xc48e
movb $0x0, %al
callq 0x1150
movl 0x20(%rsp), %eax
movl %eax, 0x96cb(%rip)      # 0x11590
movq 0x96bc(%rip), %rax      # 0x11588
addq $0x1, %rax
movq %rax, 0x96b1(%rip)      # 0x11588
movl 0x20(%rsp), %eax
addq 0x969e(%rip), %rax      # 0x11580
movq %rax, 0x9697(%rip)      # 0x11580
cmpl $0x0, 0x1c(%rsp)
je 0x7ef5
jmp 0x7fa5
movq 0x965c(%rip), %rax      # 0x11558
movl 0x8262(%rip), %ecx      # 0x10164
movl %ecx, %edx
addl $0x1, %edx
movl %edx, 0x8257(%rip)      # 0x10164
movl %ecx, %ecx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x30(%rsp)
movl 0x8243(%rip), %eax      # 0x10164
cmpl 0x9629(%rip), %eax      # 0x11550
jae 0x7f2b
jmp 0x7f4a
leaq 0x5ae9(%rip), %rdi      # 0xda1b
leaq 0x4999(%rip), %rsi      # 0xc8d2
movl $0x243, %edx            # imm = 0x243
leaq 0x5a88(%rip), %rcx      # 0xd9cd
callq 0x1100
movq 0x30(%rsp), %rdi
movq 0x48(%rsp), %rsi
movl $0x70, %edx
callq 0x1170
movq 0x95db(%rip), %rax      # 0x11540
movl 0x2c(%rsp), %ecx
movl (%rax,%rcx,4), %ecx
movq 0x30(%rsp), %rax
movl %ecx, 0x6c(%rax)
movq 0x30(%rsp), %rax
movq 0x95d8(%rip), %rcx      # 0x11558
subq %rcx, %rax
movl $0x70, %ecx
cqto
idivq %rcx
movl %eax, %edx
movq 0x95aa(%rip), %rax      # 0x11540
movl 0x2c(%rsp), %ecx
movl %edx, (%rax,%rcx,4)
movl $0x1, 0x24(%rsp)
jmp 0x8050
movq 0x95a7(%rip), %rax      # 0x11558
movl 0x81ad(%rip), %ecx      # 0x10164
movl %ecx, %edx
addl $0x1, %edx
movl %edx, 0x81a2(%rip)      # 0x10164
movl %ecx, %ecx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x30(%rsp)
movl 0x818e(%rip), %eax      # 0x10164
cmpl 0x9574(%rip), %eax      # 0x11550
jae 0x7fe0
jmp 0x7fff
leaq 0x5a34(%rip), %rdi      # 0xda1b
leaq 0x48e4(%rip), %rsi      # 0xc8d2
movl $0x250, %edx            # imm = 0x250
leaq 0x59d3(%rip), %rcx      # 0xd9cd
callq 0x1100
movq 0x30(%rsp), %rdi
movq 0x48(%rsp), %rsi
movl $0x70, %edx
callq 0x1170
movq 0x30(%rsp), %rax
movl $0x0, 0x6c(%rax)
movq 0x30(%rsp), %rax
movq 0x952d(%rip), %rcx      # 0x11558
subq %rcx, %rax
movl $0x70, %ecx
cqto
idivq %rcx
movl %eax, %edx
movq 0x94ff(%rip), %rax      # 0x11540
movl 0x2c(%rsp), %ecx
movl %edx, (%rax,%rcx,4)
movl $0x1, 0x24(%rsp)
movq 0x30(%rsp), %rax
movq 0x48(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x48(%rax)
movl 0x44(%rsp), %eax
movl %eax, %ecx
movq 0x30(%rsp), %rax
addq 0x50(%rax), %rcx
movq %rcx, 0x50(%rax)
cmpl $0x0, 0x24(%rsp)
je 0x8087
movq 0x38(%rsp), %rax
movq %rax, 0x8(%rsp)
jmp 0x8095
movq 0x30(%rsp), %rax
movq 0x58(%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x58(%rax)
movq 0x38(%rsp), %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x60(%rax)
cmpl $0x0, 0x24(%rsp)
je 0x8136
cmpl $0x0, 0x80a1(%rip)      # 0x10160
je 0x8136
movq 0x30(%rsp), %rax
movq 0x948b(%rip), %rcx      # 0x11558
subq %rcx, %rax
shrq $0x4, %rax
imull $0xb6db6db7, %eax, %eax # imm = 0xB6DB6DB7
movl %eax, 0x10(%rsp)
movq 0x30(%rsp), %rax
movl 0x44(%rax), %eax
movl %eax, 0x4(%rsp)
subl $0x1, %eax
je 0x80fc
jmp 0x80f1
movl 0x4(%rsp), %eax
subl $0x2, %eax
je 0x8119
jmp 0x8134
movq 0x942d(%rip), %rdi      # 0x11530
movq 0x30(%rsp), %rsi
movl 0x10(%rsp), %edx
movl 0x8052(%rip), %ecx      # 0x10164
callq 0x88a0
jmp 0x8134
movq 0x9410(%rip), %rdi      # 0x11530
movq 0x30(%rsp), %rsi
movl 0x10(%rsp), %edx
movl 0x8035(%rip), %ecx      # 0x10164
callq 0x8ad0
jmp 0x8136
movq 0x30(%rsp), %rax
movq 0x9416(%rip), %rcx      # 0x11558
subq %rcx, %rax
movl $0x70, %ecx
cqto
idivq %rcx
movl %eax, 0x54(%rsp)
movl 0x54(%rsp), %eax
addq $0x58, %rsp
retq
nopl (%rax)