// File: /workspace/github_cpp_makefile_star2_O1/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 $0xb8, %rsp
movl 0x1208(%rdi), %eax
movq 0x208(%rdi,%rax,8), %r13
testq %r13, %r13
je 0x10946
movq %rdi, %r15
leaq -0x2c(%rbp), %rbx
movl $0x0, (%rbx)
movq %r13, %rdi
callq 0x2030
movq %rax, %r12
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %r13, %rcx
callq 0x2250
testq %rax, %rax
je 0x105c8
movq %rax, %r14
jmp 0x10613
incl 0x1208(%r15)
movq %r15, %rdi
callq 0xbb6a
testb %al, %al
je 0x10610
movl 0x1208(%r15), %eax
movq 0x208(%r15,%rax,8), %r13
leaq -0x2c(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r13, %rcx
callq 0x2250
movq %rax, %r14
movq %r13, %rdi
callq 0x2030
movq %rax, %r12
jmp 0x10613
xorl %r14d, %r14d
cmpq $0x1, %r14
jne 0x1078f
cmpl $0xd9b4bef9, -0x2c(%rbp) # imm = 0xD9B4BEF9
je 0x1078f
movl %r12d, %esi
movq %r13, %rdi
xorl %edx, %edx
callq 0x2180
leaq 0x94cb(%rip), %rdi      # 0x19b09
callq 0x2260
movl $0xa00000, %edi         # imm = 0xA00000
callq 0x2200
movq %r13, %rbx
movq %rax, %r13
movl $0xa00000, %edx         # imm = 0xA00000
movq %rax, %rdi
xorl %esi, %esi
callq 0x20b0
movl $0x1, %r14d
movl $0x1, %esi
movl $0xa00000, %edx         # imm = 0xA00000
movq %r13, %rdi
movq %rbx, -0x40(%rbp)
movq %rbx, %rcx
callq 0x2250
testl %eax, %eax
je 0x106e3
movq %r15, -0x38(%rbp)
movl %eax, %ebx
decq %rbx
xorl %ecx, %ecx
xorl %r14d, %r14d
movl (%r13,%r14), %r15d
cmpl $0xd9b4bef9, %r15d      # imm = 0xD9B4BEF9
jne 0x106bf
movl %r14d, %edi
callq 0xb5d3
leaq 0x891d(%rip), %rdi      # 0x18fcd
movq %rax, %rsi
xorl %eax, %eax
callq 0x2050
addl %r14d, %r12d
movb $0x1, %cl
cmpl $0xd9b4bef9, %r15d      # imm = 0xD9B4BEF9
je 0x106d4
leaq 0x1(%r14), %rax
cmpq %r14, %rbx
movq %rax, %r14
jne 0x10693
movq %rcx, %rbx
movq -0x38(%rbp), %r15
movl $0x1, %r14d
jmp 0x106e5
xorl %ebx, %ebx
movq %r13, %rdi
callq 0x21b0
movq %rbx, %rax
testb $0x1, %al
movq -0x40(%rbp), %r13
je 0x10732
movl %r12d, %esi
movq %r13, %rdi
xorl %edx, %edx
callq 0x2180
leaq -0x2c(%rbp), %r12
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r13, %rcx
callq 0x2250
cmpl $0xd9b4bef9, (%r12)     # imm = 0xD9B4BEF9
jne 0x1095a
movq %rax, %r14
movq %rbx, %rax
testb $0x1, %al
jne 0x1078f
incl 0x1208(%r15)
movq %r15, %rdi
callq 0xbb6a
testb %al, %al
je 0x1078c
movl 0x1208(%r15), %eax
movq 0x208(%r15,%rax,8), %r13
leaq -0x2c(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r13, %rcx
callq 0x2250
movq %rax, %r14
cmpq $0x1, %rax
jne 0x1078f
cmpl $0xd9b4bef9, -0x2c(%rbp) # imm = 0xD9B4BEF9
je 0x1078f
leaq 0x93b9(%rip), %rdi      # 0x19b40
callq 0x2260
xorl %r14d, %r14d
cmpq $0x1, %r14
jne 0x10946
xorps %xmm0, %xmm0
leaq -0x68(%rbp), %rbx
movups %xmm0, -0xc(%rbx)
movaps %xmm0, -0x18(%rbx)
movaps %xmm0, -0x28(%rbx)
movl 0x1208(%r15), %eax
movl %eax, -0x8(%rbx)
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %r13, %rcx
callq 0x2250
movq %rax, %r14
movq %r13, %rdi
callq 0x2030
movl %eax, -0x4(%rbx)
cmpq $0x1, %r14
jne 0x10946
cmpl $0xa00000, -0x68(%rbp)  # imm = 0xA00000
jae 0x10979
leaq -0xe0(%rbp), %rdi
movl $0x50, %esi
movl $0x1, %edx
movq %r13, %rcx
callq 0x2250
cmpq $0x1, %rax
jne 0x10946
leaq -0xe0(%rbp), %rdi
movups 0x4(%rdi), %xmm0
movups 0x14(%rdi), %xmm1
leaq -0x90(%rbp), %rbx
movups %xmm1, 0x3c(%rbx)
movups %xmm0, 0x2c(%rbx)
movl $0x50, %esi
movq %rbx, %rdx
callq 0xa814
movq %rbx, %rdi
movl $0x20, %esi
movq %rbx, %rdx
callq 0xa814
movq %r13, %rdi
callq 0x2030
movl 0x28(%rbx), %ecx
leal (%rcx,%rax), %esi
addl $-0x50, %esi
movq %r13, %rdi
xorl %edx, %edx
callq 0x2180
cmpq $0x0, 0x3771330(%r15)
jne 0x108b0
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 0x10885
movq %rax, 0x3771330(%r15)
movl 0x3771328(%r15), %eax
cmpq $0x7a11f, %rax          # imm = 0x7A11F
ja 0x10998
movq 0x3771330(%r15), %rcx
imulq $0x58, %rax, %rsi
leaq (%rcx,%rsi), %rax
leaq -0x90(%rbp), %rdx
movl 0x48(%rdx), %edi
movl %edi, 0x48(%rcx,%rsi)
movl $0x9, %ecx
movq %rax, %rdi
movq %rdx, %rsi
rep  movsq (%rsi), %es:(%rdi)
incl 0x3771328(%r15)
movzwl (%rdx), %ecx
movzwl 0x4(%rdx), %esi
xorl %ecx, %esi
movzwl 0x8(%rdx), %ecx
movzwl 0xc(%rdx), %edi
xorl %ecx, %edi
xorl %esi, %edi
movzwl 0x10(%rdx), %ecx
movzwl 0x14(%rdx), %esi
xorl %ecx, %esi
movzwl 0x18(%rdx), %ecx
xorl %esi, %ecx
xorl %edi, %ecx
movzwl 0x1c(%rdx), %edx
xorl %ecx, %edx
movq 0x36f1328(%r15,%rdx,8), %rcx
movq %rax, 0x36f1328(%r15,%rdx,8)
testq %rcx, %rcx
je 0x1093b
movq %rcx, 0x50(%rax)
movq %rax, 0x36f1318(%r15)
movb $0x1, %al
jmp 0x10948
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x86b8(%rip), %rdi      # 0x19019
leaq 0x6e88(%rip), %rsi      # 0x177f0
leaq 0x86be(%rip), %rcx      # 0x1902d
movl $0x139f, %edx           # imm = 0x139F
callq 0x20c0
leaq 0x86d4(%rip), %rdi      # 0x19054
leaq 0x6e69(%rip), %rsi      # 0x177f0
leaq 0x869f(%rip), %rcx      # 0x1902d
movl $0x13c4, %edx           # imm = 0x13C4
callq 0x20c0
leaq 0x73d1(%rip), %rdi      # 0x17d70
leaq 0x6e4a(%rip), %rsi      # 0x177f0
leaq 0x86cc(%rip), %rcx      # 0x19079
movl $0x175, %edx            # imm = 0x175
callq 0x20c0
nop