// File: /workspace/github_c_makefile_star2_O2/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 %r12
pushq %rbx
subq $0x20, %rsp
movl %ecx, %ebp
movl %edx, %r12d
movq %rsi, %r14
pushq $0x1
popq %rdi
movl $0x2460, %esi           # imm = 0x2460
callq 0x1140
testq %rax, %rax
je 0x14e0
movq %rax, %rbx
leaq 0xbe0c(%rip), %rax      # 0xd1e8
addq $0x2460, (%rax)         # imm = 0x2460
movl 0xad9f(%rip), %eax      # 0xc188
leal 0x1(%rax), %ecx
movl %ecx, 0xad96(%rip)      # 0xc188
movl %eax, (%rbx)
cmpl 0xad4e(%rip), %ecx      # 0xc148
jge 0x14ff
movq 0xad79(%rip), %rcx      # 0xc180
movq %rbx, (%rcx,%rax,8)
andq $0x0, 0x18(%rbx)
movl $0x400, 0x20(%rbx)      # imm = 0x400
movq %rbx, %r15
addq $0x2028, %r15           # imm = 0x2028
movl $0x400, %edx            # imm = 0x400
movq %r15, %rdi
movq %r14, %rsi
callq 0x1050
andq $0x0, 0x8(%rbx)
movl %r12d, 0x10(%rbx)
movl %ebp, 0x14(%rbx)
andl $0x0, 0x2438(%rbx)
movl $0x1, 0x2448(%rbx)
leaq 0xbdbf(%rip), %rax      # 0xd214
cmpl $0x0, (%rax)
je 0x14b1
leaq 0x6c71(%rip), %rsi      # 0x80d2
movq %r15, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x1230
leaq 0x79c3(%rip), %rsi      # 0x8e38
movq %r15, %rdi
callq 0x6e34
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0x14c1
movabsq $0x40002a1b23c4d, %rcx  # imm = 0x40002A1B23C4D
leaq 0x8(%rsp), %rsi
movq %rcx, (%rsi)
movaps 0x6b71(%rip), %xmm0     # 0x8010
movups %xmm0, 0x8(%rsi)
pushq $0x18
popq %rdx
pushq $0x1
popq %rcx
movq %rax, %rdi
callq 0x6ec0
movq %rbx, %rax
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq 0xab30(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x6c08(%rip), %rsi      # 0x80da
xorl %ebx, %ebx
movq %r15, %rdx
xorl %eax, %eax
callq 0x1170
jmp 0x14b1
leaq 0x6b73(%rip), %rdi      # 0x805a
leaq 0x6b7e(%rip), %rsi      # 0x806c
leaq 0x6b83(%rip), %rcx      # 0x8078
movl $0x9d, %edx
callq 0x1110
leaq 0x6bb2(%rip), %rdi      # 0x80b8
leaq 0x6b5f(%rip), %rsi      # 0x806c
leaq 0x6b64(%rip), %rcx      # 0x8078
movl $0xa2, %edx
callq 0x1110