// File: /workspace/github_c_makefile_star2_O1/fmadio[P]pcap_flow/tcpstream.c
// Function: fTCPStream_Init
TCPStream_t* fTCPStream_Init(u64 MemorySize, char* OutputName, u32 FlowID, u32 FlowHash, u64 TS)
{
	TCPStream_t* TCPStream = malloc( sizeof( TCPStream_t) );
	assert(TCPStream != NULL);
	memset(TCPStream, 0, sizeof( TCPStream_t) );
	g_TotalMemory += sizeof( TCPStream_t);

	TCPStream->ID				= s_StreamCnt++;
	assert(s_StreamCnt < s_StreamMax);

	s_Stream[TCPStream->ID] 	= TCPStream;

	TCPStream->SeqNo 			= 0;
	TCPStream->BufferListPos 	= 0;
	TCPStream->BufferListMax 	= 1*1024;

	strncpy(TCPStream->Path, OutputName, sizeof(TCPStream->Path));
	//TCPStream->fd = -1; 
	TCPStream->File 			= NULL;

	TCPStream->FlowID 			= FlowID;
	TCPStream->FlowHash	 		= FlowHash;

	TCPStream->IsFileCreate		= false;

	TCPStream->WindowScale		= 1;

	// if want raw PCAP only
	if (g_OutputPCAP)
	{
		sprintf(TCPStream->Path, "%s.pcap", OutputName);

		TCPStream->File = fFile_Open(TCPStream->Path, "w");
		if (!TCPStream->File)
		{
			fprintf(stderr, "failed to create file [%s]\n", TCPStream->Path);
			return 0;
		}

		PCAPHeader_t Header;
		Header.Magic 	= PCAPHEADER_MAGIC_NANO;
		Header.Major 	= PCAPHEADER_MAJOR;
		Header.Minor 	= PCAPHEADER_MINOR;
		Header.TimeZone = 0;
		Header.SigFlag 	= 0;
		Header.SnapLen 	= 0;
		Header.Link		= PCAPHEADER_LINK_ETHERNET;

		fFile_Write(TCPStream->File, &Header, sizeof(Header), true);
	}



	//printf("[%s] FlowID:%i TCP Stream: [%s]\n", FormatTS(TS), FlowID, OutputName);
	return TCPStream; 
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %ecx, %ebp
movl %edx, %r15d
movq %rsi, %r14
movl $0x2460, %edi           # imm = 0x2460
callq 0x11a0
testq %rax, %rax
je 0x22da
movq %rax, %rbx
movl $0x2460, %edx           # imm = 0x2460
movq %rax, %rdi
xorl %esi, %esi
callq 0x1120
leaq 0xc01a(%rip), %rax      # 0xe1e8
addq $0x2460, (%rax)         # imm = 0x2460
movl 0xafad(%rip), %eax      # 0xd188
leal 0x1(%rax), %ecx
movl %ecx, 0xafa4(%rip)      # 0xd188
movl %eax, (%rbx)
cmpl 0xaf54(%rip), %ecx      # 0xd140
jge 0x22f9
movq 0xaf87(%rip), %rcx      # 0xd180
movq %rbx, (%rcx,%rax,8)
xorl %r13d, %r13d
movq %r13, 0x18(%rbx)
movl $0x400, 0x20(%rbx)      # imm = 0x400
movq %rbx, %r12
addq $0x2028, %r12           # imm = 0x2028
movl $0x400, %edx            # imm = 0x400
movq %r12, %rdi
movq %r14, %rsi
callq 0x1050
movq %r13, 0x8(%rbx)
movl %r15d, 0x10(%rbx)
movl %ebp, 0x14(%rbx)
movl $0x0, 0x2438(%rbx)
movl $0x1, 0x2448(%rbx)
leaq 0xbfc9(%rip), %rax      # 0xe214
cmpl $0x0, (%rax)
je 0x22a9
leaq 0x6e7b(%rip), %rsi      # 0x90d2
movq %r12, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x1220
leaq 0x7bbd(%rip), %rsi      # 0x9e28
movq %r12, %rdi
callq 0x874c
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0x22bb
movabsq $0x40002a1b23c4d, %rcx  # imm = 0x40002A1B23C4D
movq %rsp, %rsi
movq %rcx, (%rsi)
movaps 0x6d7d(%rip), %xmm0     # 0x9010
movups %xmm0, 0x8(%rsi)
movq %rax, %rdi
movl $0x18, %edx
movl $0x1, %ecx
callq 0x87fe
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xad36(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x6e0e(%rip), %rsi      # 0x90da
xorl %ebx, %ebx
movq %r12, %rdx
xorl %eax, %eax
callq 0x1160
jmp 0x22a9
leaq 0x6d79(%rip), %rdi      # 0x905a
leaq 0x6d84(%rip), %rsi      # 0x906c
leaq 0x6d89(%rip), %rcx      # 0x9078
movl $0x9d, %edx
callq 0x1110
leaq 0x6db8(%rip), %rdi      # 0x90b8
leaq 0x6d65(%rip), %rsi      # 0x906c
leaq 0x6d6a(%rip), %rcx      # 0x9078
movl $0xa2, %edx
callq 0x1110