// File: /workspace/github_c_makefile_star2_O3/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, %r12d
movq %rsi, %r14
movl $0x1, %edi
movl $0x2460, %esi           # imm = 0x2460
callq 0x1140
testq %rax, %rax
je 0x22b0
movq %rax, %rbx
leaq 0xc044(%rip), %rax      # 0xe1e8
addq $0x2460, (%rax)         # imm = 0x2460
movl 0xafd7(%rip), %eax      # 0xd188
leal 0x1(%rax), %ecx
movl %ecx, 0xafce(%rip)      # 0xd188
movl %eax, (%rbx)
cmpl 0xaf86(%rip), %ecx      # 0xd148
jge 0x22cf
movq 0xafb1(%rip), %rcx      # 0xd180
movq %rbx, (%rcx,%rax,8)
xorl %r13d, %r13d
movq %r13, 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
movq %r13, 0x8(%rbx)
movl %r12d, 0x10(%rbx)
movl %ebp, 0x14(%rbx)
movl $0x0, 0x2438(%rbx)
movl $0x1, 0x2448(%rbx)
leaq 0xbff3(%rip), %rax      # 0xe214
cmpl $0x0, (%rax)
je 0x227f
leaq 0x6ea5(%rip), %rsi      # 0x90d2
movq %r15, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x1230
leaq 0x7be7(%rip), %rsi      # 0x9e28
movq %r15, %rdi
callq 0x82e4
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0x2291
movabsq $0x40002a1b23c4d, %rcx  # imm = 0x40002A1B23C4D
movq %rsp, %rsi
movq %rcx, (%rsi)
movaps 0x6da7(%rip), %xmm0     # 0x9010
movups %xmm0, 0x8(%rsi)
movq %rax, %rdi
movl $0x18, %edx
movl $0x1, %ecx
callq 0x836e
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xad60(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x6e38(%rip), %rsi      # 0x90da
xorl %ebx, %ebx
movq %r15, %rdx
xorl %eax, %eax
callq 0x1170
jmp 0x227f
leaq 0x6da3(%rip), %rdi      # 0x905a
leaq 0x6dae(%rip), %rsi      # 0x906c
leaq 0x6db3(%rip), %rcx      # 0x9078
movl $0x9d, %edx
callq 0x1110
leaq 0x6de2(%rip), %rdi      # 0x90b8
leaq 0x6d8f(%rip), %rsi      # 0x906c
leaq 0x6d94(%rip), %rcx      # 0x9078
movl $0xa2, %edx
callq 0x1110