// File: /workspace/github_cpp_makefile_star2_O3/jeffchan[P]blockchain-parser/BlockChainAddresses.cpp
// Function: BlockChainAddressesImpl::BlockChainAddressesImpl(char const*)
BlockChainAddressesImpl(const char *fname)
	{
		mRowCount = 0;
		mAddressCount = 0;
		mAddresses = 0;
		mRows = 0;
		mCurrentRow = 0;
		mCurrentRowIndex = 0;
		FILE *fph = fopen(fname,"rb");
		if ( fph )
		{
			char headerBlock[22];
			size_t r = fread(headerBlock,sizeof(headerBlock),1,fph);	// read the block header
			if ( r == 1 )
			{
				const char *headerString = "BLOCK_CHAIN_ADDRESSES";
				if ( headerBlock[21] == 0 && strcmp(headerBlock,headerString) == 0 ) // confirm this is a valid block-header for the data file we are expecting
				{
					uint32_t version=0;
					fread(&version,sizeof(version),1,fph);
					if ( version == CURRENT_VERSION )	// Confirm this is for the same version number of this file we are one.
					{
						r = fread(&mAddressCount,sizeof(mAddressCount),1,fph);	// Read the number of unique public key addresses recorded
						if ( r == 1 )
						{
							mAddresses = new Address[mAddressCount];
							r  = fread(mAddresses,sizeof(Address)*mAddressCount,1,fph);	// Read all of the addresses into memory.
							if ( r == 1 )
							{
								fread(&mRowCount,sizeof(mRowCount),1,fph);		// Read the number of rows (days)
								if ( mRowCount )
								{
									mRows = new Row[mRowCount];
									// For each row read the start time, the number of new addresses, the number of changed addresses, and the number of removed addresses
									// Also allocate memory to hold the change data
									for (uint32_t i=0; i<mRowCount; i++)
									{
										Row &row = mRows[i];
										fread(&row.mStartTime,sizeof(row.mStartTime),1,fph);
										fread(&row.mAddressCount,sizeof(row.mAddressCount),1,fph);
										fread(&row.mChangeAddressCount,sizeof(row.mChangeAddressCount),1,fph);
										fread(&row.mDeleteAddressCount,sizeof(row.mDeleteAddressCount),1,fph);
										if ( row.mAddressCount )
										{
											row.mNewAddresses = new BlockChainAddresses::StatAddress[row.mAddressCount];
										}
										if ( row.mChangeAddressCount )
										{
											row.mChangedAddresses = new BlockChainAddresses::StatAddress[row.mChangeAddressCount];
										}
										if ( row.mDeleteAddressCount )
										{
											row.mDeletedAddresses = new uint32_t[row.mDeleteAddressCount];
										}
									}
									// Now for each row, read the new address data, the change address data, and the deleted address data
									for (uint32_t i=0; i<mRowCount; i++)
									{
										Row &row = mRows[i];
										if ( row.mNewAddresses )
										{
											fread(row.mNewAddresses,sizeof(BlockChainAddresses::StatAddress)*row.mAddressCount,1,fph);
										}
										if ( row.mChangedAddresses )
										{
											fread(row.mChangedAddresses,sizeof(BlockChainAddresses::StatAddress)*row.mChangeAddressCount,1,fph);
										}
										if ( row.mDeletedAddresses )
										{
											fread(row.mDeletedAddresses,sizeof(uint32_t)*row.mDeleteAddressCount,1,fph);
										}
									}
								}
								else
								{
									printf("Failed to read the row counter.\r\n");
								}
							}
							else
							{
								printf("Failed to read public-key addresses.\r\n");
							}
						}
						else
						{
							printf("Failed to read address count.\r\n");
						}
					}
				}
				else
				{
					printf("Invalid header block.\r\n");
				}
			}
			else
			{
				printf("Failed to read header block!\r\n");
			}
			fclose(fph);
		}
		else
		{
			printf("Failed to open BitcoinAddress file: %s\r\n", fname );
		}
	}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %r15
movq %rdi, %r14
leaq 0xcf90(%rip), %rax      # 0x1ed00
movq %rax, (%rdi)
xorl %eax, %eax
movl %eax, 0x18(%rdi)
movl %eax, 0x8(%rdi)
movq %rax, 0x10(%rdi)
movq %rax, 0x20(%rdi)
movq %rax, 0x30(%rdi)
movl %eax, 0x28(%rdi)
leaq 0x5cd0(%rip), %rsi      # 0x17a61
movq %r15, %rdi
callq 0x21b0
testq %rax, %rax
je 0x11df5
movq %rax, %rbx
leaq -0x70(%rbp), %rdi
movl $0x16, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x2250
cmpq $0x1, %rax
jne 0x11e14
cmpb $0x0, -0x5b(%rbp)
jne 0x11dec
movdqa -0x70(%rbp), %xmm0
movdqu -0x6a(%rbp), %xmm1
pcmpeqb 0x7ebb(%rip), %xmm1     # 0x19c90
pcmpeqb 0x7ec3(%rip), %xmm0     # 0x19ca0
pand %xmm1, %xmm0
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x11e36
leaq 0x7f04(%rip), %rdi      # 0x19cf7
jmp 0x11e1b
leaq 0x7eb4(%rip), %rdi      # 0x19cb0
movq %r15, %rsi
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2050
leaq 0x7ebe(%rip), %rdi      # 0x19cd9
callq 0x2260
movq %rbx, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2160
leaq -0x4c(%rbp), %r15
movl $0x0, (%r15)
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %rbx, %rcx
callq 0x2250
cmpl $0x1, (%r15)
jne 0x11e20
leaq 0x8(%r14), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x2250
cmpq $0x1, %rax
jne 0x1210b
movl 0x8(%r14), %eax
shlq $0x2, %rax
leaq (%rax,%rax,4), %r15
movq %r15, %rdi
callq 0x2040
movq %rax, 0x10(%r14)
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
movq %rbx, %rcx
callq 0x2250
cmpq $0x1, %rax
jne 0x12117
leaq 0x18(%r14), %r15
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %rbx, %rcx
callq 0x2250
movq %r15, -0x38(%rbp)
movl (%r15), %eax
testl %eax, %eax
je 0x12123
movl %eax, %r12d
leaq (,%r12,8), %r13
leaq 0x8(,%r13,4), %rdi
addq %r13, %rdi
callq 0x2040
movq %rax, %r15
movq %r12, (%rax)
addq $0x8, %r15
leaq -0x28(,%r13,4), %rax
addq %r13, %rax
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
mulq %rcx
shrq $0x5, %rdx
leaq (%rdx,%rdx,4), %rax
leaq 0x28(,%rax,8), %rdx
xorl %r13d, %r13d
movq %r15, %rdi
xorl %esi, %esi
callq 0x20b0
movq %r15, 0x20(%r14)
xorl %ecx, %ecx
movq 0x20(%r14), %r12
movq %rcx, -0x48(%rbp)
leaq (%rcx,%rcx,4), %r15
leaq (%r12,%r15,8), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rdi, -0x30(%rbp)
movq %rbx, %rcx
callq 0x2250
leaq (%r12,%r15,8), %rdi
addq $0x4, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x2250
leaq (%r12,%r15,8), %rdi
addq $0x8, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rdi, -0x40(%rbp)
movq %rbx, %rcx
callq 0x2250
leaq (%r12,%r15,8), %r15
addq $0xc, %r15
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %rbx, %rcx
callq 0x2250
movl -0x8(%r15), %eax
testq %rax, %rax
je 0x11ffa
shlq $0x3, %rax
leaq (%rax,%rax,2), %r12
movq %r12, %rdi
callq 0x2040
movq %rax, %rcx
addq %r12, %rcx
movq %rax, %rdx
movq $0x0, (%rdx)
movl %r13d, 0x8(%rdx)
movl %r13d, 0x10(%rdx)
movl %r13d, 0x13(%rdx)
addq $0x18, %rdx
cmpq %rcx, %rdx
jne 0x11fd6
movq -0x30(%rbp), %rcx
movq %rax, 0x10(%rcx)
movq -0x40(%rbp), %rax
movl (%rax), %eax
testq %rax, %rax
je 0x12048
movq %r15, %r12
shlq $0x3, %rax
leaq (%rax,%rax,2), %r15
movq %r15, %rdi
callq 0x2040
movq %rax, %rcx
addq %r15, %rcx
movq %rax, %rdx
movq $0x0, (%rdx)
movl %r13d, 0x8(%rdx)
movl %r13d, 0x10(%rdx)
movl %r13d, 0x13(%rdx)
addq $0x18, %rdx
cmpq %rcx, %rdx
jne 0x12021
movq -0x30(%rbp), %rcx
movq %rax, 0x18(%rcx)
movq %r12, %r15
movl (%r15), %edi
testq %rdi, %rdi
je 0x12061
shlq $0x2, %rdi
callq 0x2040
movq -0x30(%rbp), %rcx
movq %rax, 0x20(%rcx)
movq -0x48(%rbp), %rcx
incq %rcx
movq -0x38(%rbp), %rax
movl (%rax), %eax
cmpq %rax, %rcx
jb 0x11f39
testl %eax, %eax
je 0x11e20
movl $0x4, %r15d
xorl %r12d, %r12d
movq 0x20(%r14), %r13
movq 0xc(%r13,%r15), %rdi
testq %rdi, %rdi
je 0x120b0
movl (%r13,%r15), %eax
shlq $0x3, %rax
leaq (%rax,%rax,2), %rsi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x2250
movq 0x14(%r13,%r15), %rdi
testq %rdi, %rdi
je 0x120d4
movl 0x4(%r13,%r15), %eax
shlq $0x3, %rax
leaq (%rax,%rax,2), %rsi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x2250
movq 0x1c(%r13,%r15), %rdi
testq %rdi, %rdi
je 0x120f4
movl 0x8(%r13,%r15), %esi
shlq $0x2, %rsi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x2250
incq %r12
movq -0x38(%rbp), %rax
movl (%rax), %eax
addq $0x28, %r15
cmpq %rax, %r12
jb 0x12088
jmp 0x11e20
leaq 0x7bfc(%rip), %rdi      # 0x19d0e
jmp 0x11e1b
leaq 0x7c0f(%rip), %rdi      # 0x19d2d
jmp 0x11e1b
leaq 0x7c29(%rip), %rdi      # 0x19d53
jmp 0x11e1b
nop