// File: /workspace/github_cpp_makefile_star2_O2/gulamf[P]NRF24LE1Flash/src/HexUtils.cpp
// Function: HexUtils::ParseHexFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&)
vector<HexRecord> *HexUtils::ParseHexFile(string fileName, string &debugInfo)
{
    ifstream *streamReader;
    uint numHexRecords = 0;
    HexRecord curHexRecord;
    vector<HexRecord> *hexRecords = new vector<HexRecord>();

    try
    {
        streamReader = new ifstream(fileName.c_str());
    }
    catch (...)
    {
        return NULL;
    }

    debugInfo = "";

    if (!streamReader->is_open()) return NULL;

    while (!streamReader->eof())
    {
        char line[4096];
        debugInfo += ToString(numHexRecords + 1) + ") ";
        line[0]=0;
        streamReader->getline(line,4096);
        if(streamReader->eof() && line[0]==0)
            break;
        if(strcmp(line,":00000001FF")==0) {
            int y;
            y++;
        }
        curHexRecord.rawStr =  line;
        debugInfo += curHexRecord.rawStr;
        debugInfo += "\r\n";

        if (curHexRecord.rawStr[0] != ':')
        {
            debugInfo += "\tInvalid Record\r\n";
        }
        else
        {
            debugInfo += "\tValid Record\r\n";
        }

        curHexRecord.len = ParseHex<int>(curHexRecord.rawStr.substr(1, 2));
        debugInfo += "\tLength: 0x" + ToHexString(curHexRecord.len) + "\r\n";

        curHexRecord.address = ParseHex<int>(curHexRecord.rawStr.substr(3, 4));
        debugInfo += "\tAddress: 0x" + ToHexString(curHexRecord.address) + "\r\n";

        curHexRecord.type = ParseHex<int>(curHexRecord.rawStr.substr(7, 2));

        debugInfo += "\tType: 0x" + ToHexString(curHexRecord.type);

        switch (curHexRecord.type)
        {
            case 0:
                debugInfo += " (Data record)\r\n";
                break;
            case 1:
                debugInfo += " (End of file record)\r\n";
                break;
            case 2:
                debugInfo += " (Extended segment address record)\r\n";
                break;
            case 3:
                debugInfo += " (Start segment address record)\r\n";
                break;
            case 4:
                debugInfo += " (Extended linear address record)\r\n";
                break;
            case 5:
                debugInfo += " (Start linear address record)\r\n";
                break;
            default:
                debugInfo += " (Unknown record type)\r\n";
                break;
        }

        if (curHexRecord.len > 0)
        {
            curHexRecord.data = new uint8_t[curHexRecord.len];
            debugInfo += "\tData: 0x";

            for (uint x = 0; x < curHexRecord.len; x++)
            {
                string s = curHexRecord.rawStr.substr(9 + (x * 2), 2);
                uint8_t t = ParseHex<uint8_t>(s);
                curHexRecord.data[x]=t;

                if (curHexRecord.data[x] < 0x10)
                    debugInfo += "0";

                debugInfo += ToHexString(curHexRecord.data[x]);
            }

            debugInfo += "\r\n";
        }
        else
        {
            debugInfo += "\tNo data\r\n";
        }

        curHexRecord.checkSum = ParseHex<int>(curHexRecord.rawStr.substr(9 + (curHexRecord.len * 2), 2));
        debugInfo += "\tRecord checksum: 0x" + ToHexString(curHexRecord.checkSum) + "\r\n";

        curHexRecord.calcSum = 0;

        for (uint x = 1; x < 9 + (curHexRecord.len * 2); x += 2)
        {
            curHexRecord.calcSum += ParseHex<int>(curHexRecord.rawStr.substr(x, 2));
        }

        curHexRecord.calcSum = -curHexRecord.calcSum;
        curHexRecord.calcSum &= 0xFF;

        debugInfo += "\tCalculated checksum: 0x" + ToHexString(curHexRecord.calcSum) + "\r\n";

        if (curHexRecord.calcSum == curHexRecord.checkSum)
        {
            debugInfo += "\tChecksums match\r\n\r\n";
        }
        else
        {
            debugInfo += "\tChecksums do not match\r\n\r\n";
            return NULL;
        }

        hexRecords->push_back(curHexRecord);
        numHexRecords++;
    }

    streamReader->close();

    return hexRecords;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10b8, %rsp           # imm = 0x10B8
movq %rsi, %r14
movq %rdi, %rbx
leaq -0x58(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
pushq $0x18
popq %rdi
callq 0x2210
movq %rax, -0x30(%rbp)
xorps %xmm0, %xmm0
movq -0x30(%rbp), %rax
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movl $0x208, %edi            # imm = 0x208
callq 0x2210
movq %rax, %r15
movq (%rbx), %rsi
pushq $0x8
popq %rdx
movq %rax, %rdi
callq 0x2390
leaq 0x1d74(%rip), %rsi      # 0x6a51
movq %r14, %rdi
callq 0x23b0
movq %r15, %rdi
addq $0x78, %rdi
callq 0x2080
testb %al, %al
je 0x51c9
movq $0x0, -0x98(%rbp)
movq %r15, -0x70(%rbp)
leaq -0xb8(%rbp), %r12
leaq -0xd8(%rbp), %rbx
movq -0x70(%rbp), %rcx
movq (%rcx), %rax
movq -0x18(%rax), %rax
testb $0x2, 0x20(%rcx,%rax)
jne 0x51af
movq -0x98(%rbp), %rax
incl %eax
movq %rax, -0x98(%rbp)
movl %eax, -0x90(%rbp)
movq %r12, %rdi
leaq -0x90(%rbp), %rsi
callq 0x52db
movq %rbx, %rdi
movq %r12, %rsi
leaq 0x1b86(%rip), %rdx      # 0x68e4
callq 0x49f3
movq %r14, %rdi
movq %rbx, %rsi
callq 0x23c0
movq %rbx, %rdi
callq 0x23e8
movq %r12, %rdi
callq 0x23e8
movb $0x0, -0x10e0(%rbp)
movl $0x1000, %edx           # imm = 0x1000
movq -0x70(%rbp), %rdi
leaq -0x10e0(%rbp), %rsi
callq 0x20d0
movq -0x70(%rbp), %rcx
movq (%rcx), %rax
movq -0x18(%rax), %rax
testb $0x2, 0x20(%rcx,%rax)
je 0x4db9
cmpb $0x0, -0x10e0(%rbp)
je 0x51af
leaq -0x68(%rbp), %rdi
leaq -0x10e0(%rbp), %rsi
callq 0x23b0
movq %r14, %rdi
leaq -0x68(%rbp), %rsi
callq 0x23c0
movq %r14, %rdi
leaq 0x1c70(%rip), %rsi      # 0x6a4f
callq 0x23a0
movq -0x68(%rbp), %rax
cmpb $0x3a, (%rax)
leaq 0x1af5(%rip), %rsi      # 0x68e7
leaq 0x1b00(%rip), %rax      # 0x68f9
cmoveq %rax, %rsi
movq %r14, %rdi
callq 0x23a0
movq %rbx, %rdi
leaq -0x68(%rbp), %rsi
pushq $0x1
popq %rdx
pushq $0x2
popq %rcx
callq 0x2230
movq %rbx, %rdi
callq 0x534e
movw %ax, -0x48(%rbp)
movq %rbx, %rdi
callq 0x23e8
movzwl -0x48(%rbp), %esi
leaq -0x90(%rbp), %r15
movq %r15, %rdi
callq 0x569c
movq %r12, %rdi
leaq 0x1ac1(%rip), %rsi      # 0x6909
movq %r15, %rdx
callq 0x4a2a
movq %rbx, %rdi
movq %r12, %rsi
leaq 0x1bf2(%rip), %rdx      # 0x6a4f
callq 0x49f3
movq %r14, %rdi
movq %rbx, %rsi
callq 0x23c0
movq %rbx, %rdi
callq 0x23e8
movq %r12, %rdi
callq 0x23e8
movq %r15, %rdi
callq 0x23e8
movq %rbx, %rdi
leaq -0x68(%rbp), %rsi
pushq $0x3
popq %rdx
pushq $0x4
popq %rcx
callq 0x2230
movq %rbx, %rdi
callq 0x534e
movw %ax, -0x46(%rbp)
movq %rbx, %rdi
callq 0x23e8
movzwl -0x46(%rbp), %esi
movq %r15, %rdi
callq 0x569c
movq %r12, %rdi
leaq 0x1a54(%rip), %rsi      # 0x6915
movq %r15, %rdx
callq 0x4a2a
movq %rbx, %rdi
movq %r12, %rsi
leaq 0x1b79(%rip), %rdx      # 0x6a4f
callq 0x49f3
movq %r14, %rdi
movq %rbx, %rsi
callq 0x23c0
movq %rbx, %rdi
callq 0x23e8
movq %r12, %rdi
callq 0x23e8
leaq -0x90(%rbp), %rdi
callq 0x23e8
movq %rbx, %rdi
leaq -0x68(%rbp), %rsi
pushq $0x7
popq %rdx
pushq $0x2
popq %rcx
callq 0x2230
movq %rbx, %rdi
callq 0x534e
movw %ax, -0x44(%rbp)
movq %rbx, %rdi
callq 0x23e8
movzwl -0x44(%rbp), %esi
movq %r12, %rdi
callq 0x569c
movq %rbx, %rdi
leaq 0x19e4(%rip), %rsi      # 0x6922
movq %r12, %rdx
callq 0x4a2a
movq %r14, %rdi
movq %rbx, %rsi
callq 0x23c0
movq %rbx, %rdi
callq 0x23e8
movq %r12, %rdi
callq 0x23e8
movzwl -0x44(%rbp), %eax
leaq 0x1a75(%rip), %rsi      # 0x69e1
cmpq $0x6, %rax
jae 0x4f80
leaq 0x1b1b(%rip), %rcx      # 0x6a94
movslq (%rcx,%rax,4), %rsi
addq %rcx, %rsi
movq %r14, %rdi
callq 0x23a0
movzwl -0x48(%rbp), %edi
leaq 0x1a71(%rip), %rsi      # 0x6a04
testq %rdi, %rdi
je 0x503f
callq 0x2030
movq %rax, -0x40(%rbp)
movq %r14, %rdi
leaq 0x1a4b(%rip), %rsi      # 0x69fa
callq 0x23a0
pushq $0x9
popq %r13
xorl %r15d, %r15d
movzwl -0x48(%rbp), %eax
cmpq %rax, %r15
jae 0x5038
movq %rbx, %rdi
leaq -0x68(%rbp), %rsi
movq %r13, %rdx
pushq $0x2
popq %rcx
callq 0x2230
movq %rbx, %rdi
callq 0x5392
movq -0x40(%rbp), %rcx
movb %al, (%rcx,%r15)
movq -0x40(%rbp), %rax
movb (%rax,%r15), %al
cmpb $0xf, %al
ja 0x5009
movq %r14, %rdi
leaq 0x1299(%rip), %rsi      # 0x6295
callq 0x23a0
movq -0x40(%rbp), %rax
movb (%rax,%r15), %al
movzbl %al, %esi
movq %r12, %rdi
callq 0x569c
movq %r14, %rdi
movq %r12, %rsi
callq 0x23c0
movq %r12, %rdi
callq 0x23e8
movq %rbx, %rdi
callq 0x23e8
incq %r15
addq $0x2, %r13
jmp 0x4fbb
leaq 0x1a10(%rip), %rsi      # 0x6a4f
movq %r14, %rdi
callq 0x23a0
movzwl -0x48(%rbp), %eax
leaq 0x9(,%rax,2), %rdx
movq %rbx, %rdi
leaq -0x68(%rbp), %rsi
pushq $0x2
popq %rcx
callq 0x2230
movq %rbx, %rdi
callq 0x534e
movw %ax, -0x38(%rbp)
movq %rbx, %rdi
callq 0x23e8
movzwl -0x38(%rbp), %esi
leaq -0x90(%rbp), %r15
movq %r15, %rdi
callq 0x569c
movq %r12, %rdi
leaq 0x197c(%rip), %rsi      # 0x6a0f
movq %r15, %rdx
callq 0x4a2a
movq %rbx, %rdi
movq %r12, %rsi
leaq 0x19a7(%rip), %rdx      # 0x6a4f
callq 0x49f3
movq %r14, %rdi
movq %rbx, %rsi
callq 0x23c0
movq %rbx, %rdi
callq 0x23e8
movq %r12, %rdi
callq 0x23e8
leaq -0x90(%rbp), %rdi
callq 0x23e8
andw $0x0, -0x36(%rbp)
pushq $0x1
popq %r13
movzwl -0x48(%rbp), %eax
leaq 0x9(,%rax,2), %rax
cmpq %rax, %r13
jae 0x511a
movq %rbx, %rdi
leaq -0x68(%rbp), %rsi
movq %r13, %rdx
pushq $0x2
popq %rcx
callq 0x2230
movq %rbx, %rdi
callq 0x534e
addw %ax, -0x36(%rbp)
movq %rbx, %rdi
callq 0x23e8
addq $0x2, %r13
jmp 0x50dd
movzwl -0x36(%rbp), %eax
xorl %ecx, %ecx
subw %ax, %cx
movzbl %cl, %esi
movw %si, -0x36(%rbp)
leaq -0x90(%rbp), %r15
movq %r15, %rdi
callq 0x569c
movq %r12, %rdi
leaq 0x18e1(%rip), %rsi      # 0x6a24
movq %r15, %rdx
callq 0x4a2a
movq %rbx, %rdi
movq %r12, %rsi
leaq 0x18f7(%rip), %rdx      # 0x6a4f
callq 0x49f3
movq %r14, %rdi
movq %rbx, %rsi
callq 0x23c0
movq %rbx, %rdi
callq 0x23e8
movq %r12, %rdi
callq 0x23e8
leaq -0x90(%rbp), %rdi
callq 0x23e8
movzwl -0x36(%rbp), %eax
cmpw -0x38(%rbp), %ax
jne 0x51ba
movq %r14, %rdi
leaq 0x18a5(%rip), %rsi      # 0x6a3d
callq 0x23a0
movq -0x30(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x53de
jmp 0x4d16
movq -0x70(%rbp), %rdi
callq 0x2070
jmp 0x51d1
leaq 0x1891(%rip), %rsi      # 0x6a52
movq %r14, %rdi
callq 0x23a0
movq $0x0, -0x30(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x23e8
movq -0x30(%rbp), %rax
addq $0x10b8, %rsp           # imm = 0x10B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x52c7
movq %rax, %rbx
movl $0x208, %esi            # imm = 0x208
movq %r15, %rdi
callq 0x2220
jmp 0x520a
movq %rax, %rbx
movq %rbx, %rdi
callq 0x2120
callq 0x2340
jmp 0x51c9
jmp 0x52c7
jmp 0x523d
jmp 0x52c7
jmp 0x5253
jmp 0x5268
jmp 0x5253
jmp 0x5268
jmp 0x523d
jmp 0x52c7
jmp 0x52c7
jmp 0x5291
jmp 0x523d
movq %rax, %rbx
jmp 0x5277
jmp 0x52c7
jmp 0x52c7
jmp 0x52c7
jmp 0x52c7
jmp 0x52c7
jmp 0x52c7
jmp 0x5253
movq %rax, %rbx
leaq -0xd8(%rbp), %rdi
callq 0x23e8
jmp 0x526b
jmp 0x5280
jmp 0x5268
movq %rax, %rbx
leaq -0xb8(%rbp), %rdi
callq 0x23e8
leaq -0x90(%rbp), %rdi
jmp 0x52be
movq %rax, %rbx
leaq -0xd8(%rbp), %rdi
callq 0x23e8
jmp 0x5294
movq %rax, %rbx
leaq -0xb8(%rbp), %rdi
jmp 0x52be
jmp 0x52c7
jmp 0x52b4
jmp 0x52c7
movq %rax, %rbx
leaq -0xb8(%rbp), %rdi
callq 0x23e8
jmp 0x52b7
movq %rax, %rbx
leaq -0xd8(%rbp), %rdi
callq 0x23e8
jmp 0x52ca
jmp 0x52c7
movq %rax, %rbx
leaq -0x68(%rbp), %rdi
callq 0x23e8
movq %rbx, %rdi
callq 0x2370