// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movq %rsi, 0x30(%rsp)
movl %edx, 0x2c(%rsp)
movl %ecx, 0x28(%rsp)
movq %r8, 0x20(%rsp)
movl $0x2460, %edi           # imm = 0x2460
callq 0x1190
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x18(%rsp)
je 0x13e4
jmp 0x1403
leaq 0xac67(%rip), %rdi      # 0xc052
leaq 0xac72(%rip), %rsi      # 0xc064
movl $0x9d, %edx
leaq 0xac72(%rip), %rcx      # 0xc070
callq 0x1100
movq 0x18(%rsp), %rdi
xorl %esi, %esi
movl $0x2460, %edx           # imm = 0x2460
callq 0x1110
leaq 0xfddd(%rip), %rax      # 0x111f8
movq (%rax), %rcx
addq $0x2460, %rcx           # imm = 0x2460
leaq 0xfdcc(%rip), %rax      # 0x111f8
movq %rcx, (%rax)
movl 0xed63(%rip), %ecx      # 0x10198
movl %ecx, %eax
addl $0x1, %eax
movl %eax, 0xed58(%rip)      # 0x10198
movq 0x18(%rsp), %rax
movl %ecx, (%rax)
movl 0xed4b(%rip), %eax      # 0x10198
cmpl 0xece5(%rip), %eax      # 0x10138
jge 0x1457
jmp 0x1476
leaq 0xac52(%rip), %rdi      # 0xc0b0
leaq 0xabff(%rip), %rsi      # 0xc064
movl $0xa2, %edx
leaq 0xabff(%rip), %rcx      # 0xc070
callq 0x1100
movq 0x18(%rsp), %rdx
movq 0xed0e(%rip), %rax      # 0x10190
movq 0x18(%rsp), %rcx
movl (%rcx), %ecx
movq %rdx, (%rax,%rcx,8)
movq 0x18(%rsp), %rax
movl $0x0, 0x18(%rax)
movq 0x18(%rsp), %rax
movl $0x0, 0x1c(%rax)
movq 0x18(%rsp), %rax
movl $0x400, 0x20(%rax)      # imm = 0x400
movq 0x18(%rsp), %rdi
addq $0x2028, %rdi           # imm = 0x2028
movq 0x30(%rsp), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x1050
movq 0x18(%rsp), %rax
movq $0x0, 0x8(%rax)
movl 0x2c(%rsp), %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x10(%rax)
movl 0x28(%rsp), %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x14(%rax)
movq 0x18(%rsp), %rax
movl $0x0, 0x2438(%rax)
movq 0x18(%rsp), %rax
movl $0x1, 0x2448(%rax)
leaq 0xfd0e(%rip), %rax      # 0x11224
cmpl $0x0, (%rax)
je 0x15ed
movq 0x18(%rsp), %rdi
addq $0x2028, %rdi           # imm = 0x2028
movq 0x30(%rsp), %rdx
leaq 0xab93(%rip), %rsi      # 0xc0ca
movb $0x0, %al
callq 0x1200
movq 0x18(%rsp), %rdi
addq $0x2028, %rdi           # imm = 0x2028
leaq 0xb8c7(%rip), %rsi      # 0xce18
callq 0xb7c0
movq %rax, %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x18(%rsp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x159d
movq 0xea83(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movq 0x18(%rsp), %rdx
addq $0x2028, %rdx           # imm = 0x2028
leaq 0xab47(%rip), %rsi      # 0xc0d2
movb $0x0, %al
callq 0x1150
movq $0x0, 0x40(%rsp)
jmp 0x15f7
movl $0xa1b23c4d, (%rsp)     # imm = 0xA1B23C4D
movw $0x2, 0x4(%rsp)
movw $0x4, 0x6(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x0, 0xc(%rsp)
movl $0x0, 0x10(%rsp)
movl $0x1, 0x14(%rsp)
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rdi
movq %rsp, %rsi
movl $0x18, %edx
movl $0x1, %ecx
callq 0xb900
movq 0x18(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rax
addq $0x48, %rsp
retq
nopw %cs:(%rax,%rax)