// File: /workspace/github_cpp_makefile_star2_O3/jeffchan[P]blockchain-parser/BlockChain.cpp
// Function: BlockChainImpl::readBlockHeader()
bool readBlockHeader(void)
	{
		bool ok = false;
		FILE *fph = mBlockChain[mBlockIndex];
		if ( fph )
		{
			uint32_t magicID = 0;
			uint32_t lastBlockRead = (uint32_t)ftell(fph);
			size_t r = fread(&magicID,sizeof(magicID),1,fph);	// Attempt to read the magic id for the next block
			if ( r == 0 )
			{
				mBlockIndex++;	// advance to the next data file if we couldn't read any further in the current data file
				if ( openBlock() )
				{
					fph = mBlockChain[mBlockIndex];
					r = fread(&magicID,sizeof(magicID),1,fph); // if we opened up a new file; read the magic id from it's first block.
					lastBlockRead = ftell(fph);
				}
			}
			// If after reading the previous block, we did not encounter a block header, we need to scan for the next block header..
			if ( r == 1 && magicID != MAGIC_ID )
			{
				fseek(fph,lastBlockRead,SEEK_SET);
				printf("Warning: Missing block-header; scanning for next one.\r\n");
				uint8_t *temp = (uint8_t *)::malloc(MAX_BLOCK_SIZE);
				memset(temp,0,MAX_BLOCK_SIZE);
				uint32_t c = (uint32_t)fread(temp,1,MAX_BLOCK_SIZE,fph);
				bool found = false;
				if ( c > 0 )
				{
					for (uint32_t i=0; i<c; i++)
					{
						const uint32_t *check = (const uint32_t *)&temp[i];
						if ( *check == MAGIC_ID )
						{
							printf("Found the next block header after skipping: %s bytes forward in the file.\r\n", formatNumber(i) );
							lastBlockRead+=i; // advance to this location.
							found = true;
							break;
						}
					}
				}
				::free(temp);
				if ( found )
				{
					fseek(fph,lastBlockRead,SEEK_SET);
					r = fread(&magicID,sizeof(magicID),1,fph); // if we opened up a new file; read the magic id from it's first block.
					assert( magicID == MAGIC_ID );
				}

				if ( found ) // if we found it before the EOF, we are cool, otherwise, we need to advance to the next file.
				{
				}
				else
				{
					mBlockIndex++;	// advance to the next data file if we couldn't read any further in the current data file
					if ( openBlock() )
					{
						fph = mBlockChain[mBlockIndex];
						r = fread(&magicID,sizeof(magicID),1,fph); // if we opened up a new file; read the magic id from it's first block.
						if ( r == 1 )
						{
							if ( magicID != MAGIC_ID )
							{
								printf("Advanced to the next data file; but it does not start with a valid block.  Aborting reading the block-chain.\r\n");
								r = 0;
							}
						}
					}
					else
					{
						r = 0; // done
					}
				}
			}
			if ( r == 1 )	// Ok, this is a valid block, let's continue
			{
				BlockHeader header;
				BlockPrefix prefix;
				header.mFileIndex = mBlockIndex;
				r = fread(&header.mBlockLength,sizeof(header.mBlockLength),1,fph); // read the length of the block
				header.mFileOffset = (uint32_t)ftell(fph);
				if ( r == 1 )
				{
					assert( header.mBlockLength < MAX_BLOCK_SIZE ); // make sure the block length does not exceed our maximum expected ever possible block size
					if ( header.mBlockLength < MAX_BLOCK_SIZE )
					{
						r = fread(&prefix,sizeof(prefix),1,fph); // read the rest of the block (less the 8 byte header we have already consumed)
						if ( r == 1 )
						{
							Hash256 *blockHash = static_cast< Hash256 *>(&header);
							memcpy(header.mPreviousBlockHash,prefix.mPreviousBlock,32);
							BLOCKCHAIN_SHA256::computeSHA256((uint8_t *)&prefix,sizeof(prefix),(uint8_t *)blockHash);
							BLOCKCHAIN_SHA256::computeSHA256((uint8_t *)blockHash,32,(uint8_t *)blockHash);
							uint32_t currentFileOffset = ftell(fph); // get the current file offset.
							uint32_t advance = header.mBlockLength - sizeof(BlockPrefix);
							currentFileOffset+=advance;
							fseek(fph,currentFileOffset,SEEK_SET); // skip past the block to get to the next header.
							mLastBlockHeader = mBlockHeaderMap.insert(header);
							ok = true;
						}
					}
				}
			}
		}
		return ok;
	}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movl 0x1208(%rdi), %eax
movq 0x208(%rdi,%rax,8), %r14
testq %r14, %r14
je 0x10868
movq %rdi, %rbx
leaq -0x2c(%rbp), %r12
movl $0x0, (%r12)
movq %r14, %rdi
callq 0x2030
movq %rax, %r15
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x2250
testq %rax, %rax
je 0x10534
movq %rax, %r12
jmp 0x1057c
incl 0x1208(%rbx)
movq %rbx, %rdi
callq 0xbacc
testb %al, %al
je 0x10868
movl 0x1208(%rbx), %eax
movq 0x208(%rbx,%rax,8), %r14
leaq -0x2c(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x2250
movq %rax, %r12
movq %r14, %rdi
callq 0x2030
movq %rax, %r15
cmpq $0x1, %r12
jne 0x106ad
cmpl $0xd9b4bef9, -0x2c(%rbp) # imm = 0xD9B4BEF9
je 0x106ad
movl %r15d, %esi
movq %r14, %rdi
xorl %edx, %edx
callq 0x2190
leaq 0x9562(%rip), %rdi      # 0x19b09
callq 0x2260
movl $0x1, %edi
movl $0xa00000, %esi         # imm = 0xA00000
callq 0x20d0
movq %rax, %r12
movl $0x1, %esi
movl $0xa00000, %edx         # imm = 0xA00000
movq %rax, %rdi
movq %r14, %rcx
callq 0x2250
testl %eax, %eax
je 0x105ee
movl %eax, %eax
xorl %r13d, %r13d
cmpl $0xd9b4bef9, (%r12,%r13) # imm = 0xD9B4BEF9
je 0x10653
incq %r13
cmpq %r13, %rax
jne 0x105dc
movq %r12, %rdi
callq 0x21c0
incl 0x1208(%rbx)
movq %rbx, %rdi
callq 0xbacc
testb %al, %al
je 0x10868
movl 0x1208(%rbx), %eax
movq 0x208(%rbx,%rax,8), %r14
leaq -0x2c(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x2250
movq %rax, %r12
cmpq $0x1, %rax
jne 0x106ad
cmpl $0xd9b4bef9, -0x2c(%rbp) # imm = 0xD9B4BEF9
je 0x106ad
leaq 0x94f7(%rip), %rdi      # 0x19b40
callq 0x2260
jmp 0x10868
movl %r13d, %edi
callq 0xb535
leaq 0x896b(%rip), %rdi      # 0x18fcd
movq %rax, %rsi
xorl %eax, %eax
callq 0x2050
addl %r13d, %r15d
movq %r12, %rdi
callq 0x21c0
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x2190
leaq -0x2c(%rbp), %r15
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x2250
cmpl $0xd9b4bef9, (%r15)     # imm = 0xD9B4BEF9
jne 0x1089b
movq %rax, %r12
cmpq $0x1, %r12
jne 0x10868
xorps %xmm0, %xmm0
leaq -0x58(%rbp), %r15
movups %xmm0, -0xc(%r15)
movaps %xmm0, -0x18(%r15)
movaps %xmm0, -0x28(%r15)
movl 0x1208(%rbx), %eax
movl %eax, -0x8(%r15)
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x2250
movq %rax, %r12
movq %r14, %rdi
callq 0x2030
movl %eax, -0x4(%r15)
cmpq $0x1, %r12
jne 0x10868
cmpl $0xa00000, -0x58(%rbp)  # imm = 0xA00000
jae 0x1087c
leaq -0xd0(%rbp), %rdi
movl $0x50, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x2250
cmpq $0x1, %rax
jne 0x10868
leaq -0xd0(%rbp), %rdi
movups 0x4(%rdi), %xmm0
movups 0x14(%rdi), %xmm1
leaq -0x80(%rbp), %r15
movups %xmm1, 0x3c(%r15)
movups %xmm0, 0x2c(%r15)
movl $0x50, %esi
movq %r15, %rdx
callq 0xa7a4
movq %r15, %rdi
movl $0x20, %esi
movq %r15, %rdx
callq 0xa7a4
movq %r14, %rdi
callq 0x2030
movl 0x28(%r15), %ecx
leal (%rcx,%rax), %esi
addl $-0x50, %esi
movq %r14, %rdi
xorl %edx, %edx
callq 0x2190
movq 0x3771330(%rbx), %rax
testq %rax, %rax
jne 0x107d4
movl $0x29f6300, %edi        # imm = 0x29F6300
callq 0x2040
movl $0x50, %ecx
xorps %xmm0, %xmm0
movups %xmm0, -0x34(%rax,%rcx)
movups %xmm0, -0x40(%rax,%rcx)
movups %xmm0, -0x50(%rax,%rcx)
movq $0x0, (%rax,%rcx)
addq $0x58, %rcx
cmpq $0x29f6350, %rcx        # imm = 0x29F6350
jne 0x107a9
movq %rax, 0x3771330(%rbx)
movl 0x3771328(%rbx), %ecx
cmpq $0x7a11f, %rcx          # imm = 0x7A11F
ja 0x108ba
imulq $0x58, %rcx, %rcx
leaq (%rax,%rcx), %rdx
leaq -0x80(%rbp), %r8
movl 0x48(%r8), %esi
movl %esi, 0x48(%rax,%rcx)
movl $0x9, %ecx
movq %rdx, %rdi
movq %r8, %rsi
rep  movsq (%rsi), %es:(%rdi)
incl 0x3771328(%rbx)
movzwl (%r8), %eax
movzwl 0x4(%r8), %ecx
xorl %eax, %ecx
movzwl 0x8(%r8), %eax
movzwl 0xc(%r8), %esi
xorl %eax, %esi
xorl %ecx, %esi
movzwl 0x10(%r8), %eax
movzwl 0x14(%r8), %ecx
xorl %eax, %ecx
movzwl 0x18(%r8), %eax
xorl %ecx, %eax
xorl %esi, %eax
movzwl 0x1c(%r8), %ecx
xorl %eax, %ecx
movq 0x36f1328(%rbx,%rcx,8), %rax
movq %rdx, 0x36f1328(%rbx,%rcx,8)
testq %rax, %rax
je 0x1085d
movq %rax, 0x50(%rdx)
movq %rdx, 0x36f1318(%rbx)
movb $0x1, %al
jmp 0x1086a
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x87d1(%rip), %rdi      # 0x19054
leaq 0x6f66(%rip), %rsi      # 0x177f0
leaq 0x879c(%rip), %rcx      # 0x1902d
movl $0x13c4, %edx           # imm = 0x13C4
callq 0x20c0
leaq 0x8777(%rip), %rdi      # 0x19019
leaq 0x6f47(%rip), %rsi      # 0x177f0
leaq 0x877d(%rip), %rcx      # 0x1902d
movl $0x139f, %edx           # imm = 0x139F
callq 0x20c0
leaq 0x74af(%rip), %rdi      # 0x17d70
leaq 0x6f28(%rip), %rsi      # 0x177f0
leaq 0x87aa(%rip), %rcx      # 0x19079
movl $0x175, %edx            # imm = 0x175
callq 0x20c0
nop