// File: /workspace/github_cpp_makefile_star2_O1/Infinidat[P]slimfastq/recs.cpp
// Function: RecLoad::load(unsigned char*, unsigned char const*)
size_t RecLoad::load(UCHAR* buf, const UCHAR* prev) {
    rarely_if(not m_last.initilized) {
        comp_version = conf.decoder_version;
        bzero(ctype[0], sizeof(ctype[0])); // clear cache
        bzero(ctype[1], sizeof(ctype[1])); // clear cache
        imap = 0;
        return load_first_line(buf);
    }

    bool pmap = imap;
    imap = imap ? 0 : 1;

    rarely_if(m_last.index == g_record_count) {

        UCHAR* b = x_file->get_str(buf);
        m_last.index += x_file->get();
        bzero(ctype[imap], sizeof(ctype[imap])); // clear cache

        return b - buf;
    }

    map_space(prev, 0);         // TODO? optimize by caching prev

    rarely_if (comp_version < 5)
        return load_pre5(buf, prev);

    UINT64 map = get_num(0);
    UCHAR* b = buf;

    for (int i = 0; i < smap[0].len; i++) {
        // if (IS_SET(last_map, i)) {
        if (not IS_SET(map, i)) {
            // b = (UCHAR*)mempcpy(b, prev + smap[0].off[i], smap[0].wln[i]);
            // mempcpy is not compatible with Mac OS, must do it the hard way ..
            UINT32 count = smap[0].wln[i] ;
            memcpy(b, prev + smap[0].off[i], count );
            b += count;
            *b ++ = smap[0].str[i];
            ctype[imap][i] = ctype[pmap][i];
            cnumb[imap][i] = cnumb[pmap][i];
            continue;
        }

        UCHAR type = get_type(i+1);

        if ( type == ST_STR ) {
            b = get_str(i+1, b);
            ctype[imap][i] = 0;
            *b ++ = smap[0].str[i];
            continue;
        }

        UINT64 pval = ctype[pmap][i] == 0 ? 0 : cnumb[pmap][i];
        UINT64 gap = get_num(i+1);
        UINT64 val ;
        const char* fmt;
#define ITEM(A, B) fmt=A; val=B; break
        switch ((seg_type)type) {

        case ST_DGT    : ITEM("%lld", pval + gap);
        case ST_DLT    : ITEM("%lld", pval - gap);
        case ST_STR    :   croak("WTF"); break;
        case ST_HGT    : ITEM("%llx", pval + gap);
        case ST_HLT    : ITEM("%llx", pval - gap);
        case ST_HGT_Z  : ITEM("0%llx", pval + gap);
        case ST_HLT_Z  : ITEM("0%llx", pval - gap);
        case ST_HGTC   : ITEM("%llX", pval + gap);
        case ST_HLTC   : ITEM("%llX", pval - gap);
        case ST_HGTC_Z : ITEM("0%llX", pval + gap);
        case ST_HLTC_Z : ITEM("0%llX", pval - gap);
        case ST_DGT_Z  : ITEM("0%lld", pval + gap);
        case ST_DLT_Z  : ITEM("0%lld", pval - gap);

        default:
            croak("REC: bad type value %d", type);
        }
#undef ITEM
        ctype[imap][i] = (type < ST_STR or type >= ST_DGT_Z) ? 1 : 2;
        cnumb[imap][i] = val;
        if (val == 0)
            *b ++ = '0'; // Under certain conditions sprintf(b, '%lld', 0) was printing "00". Yack!
        else
            b += sprintf((char*)b, fmt, val);

        *b ++ = smap[0].str[i];
    }
    return b-buf-1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, 0x10(%rsp)
movq %rsi, %rbp
movq %rdi, %r14
cmpb $0x0, 0xc7d08(%rdi)
je 0x7c6f
movzbl 0xc8660(%r14), %ebx
movl %ebx, %eax
xorb $0x1, %al
movb %al, 0xc8660(%r14)
movq 0xc7d10(%r14), %rax
leaq 0x881d(%rip), %rcx      # 0x101a0
cmpq (%rcx), %rax
je 0x7cb4
movq %r14, %rdi
movq 0x10(%rsp), %rsi
xorl %edx, %edx
callq 0x731e
cmpl $0x4, 0xc8664(%r14)
jle 0x7d24
leaq 0x60e(%r14), %rdi
leaq 0xc7ce0(%r14), %rsi
movq %rsi, 0x20(%rsp)
callq 0x7fea
movq %rax, 0x30(%rsp)
cmpb $0x0, 0xc7f79(%r14)
movq %rbp, 0x18(%rsp)
je 0x7c55
movl %ebx, %r15d
shll $0x6, %r15d
orq %rbx, %r15
movl $0x367e, %eax           # imm = 0x367E
movq %rax, 0x28(%rsp)
movl $0x3070, %r13d          # imm = 0x3070
xorl %r12d, %r12d
movq 0x18(%rsp), %rbp
movq 0x30(%rsp), %rax
btq %r12, %rax
jae 0x7a56
leaq (%r14,%r13), %rdi
movq 0x20(%rsp), %rsi
callq 0x34b4
movl %eax, %ebx
cmpb $0x2, %bl
jne 0x7ad0
leaq 0x1(%r12), %rax
movzbl %al, %esi
movq %r14, %rdi
movq %rbp, %rdx
callq 0x72a6
movq %rax, %rbp
movzbl 0xc8660(%r14), %eax
movl %eax, %ecx
shll $0x6, %ecx
orq %rax, %rcx
leaq (%r14,%r12), %rax
movb $0x0, 0xc81c8(%rax,%rcx)
jmp 0x7c23
movl 0xc7e34(%r14,%r12,4), %ebx
movslq 0xc7d30(%r14,%r12,4), %rsi
addq 0x10(%rsp), %rsi
movq %rbp, %rdi
movq %rbx, %rdx
callq 0x2170
movb 0xc7f38(%r14,%r12), %al
movb %al, (%rbp,%rbx)
addq %rbx, %rbp
leaq (%r14,%r12), %rax
movb 0xc81c8(%r14,%r15), %cl
movzbl 0xc8660(%r14), %edx
movl %edx, %esi
shll $0x6, %esi
orq %rdx, %rsi
movb %cl, 0xc81c8(%rax,%rsi)
movq 0xc8250(%r14,%r15,8), %rax
movzbl 0xc8660(%r14), %ecx
movl %ecx, %edx
shll $0x9, %edx
leal (%rdx,%rcx,8), %ecx
addq %r14, %rcx
movq %rax, 0xc8250(%rcx,%r12,8)
jmp 0x7c2e
cmpb $0x0, 0xc81c8(%r14,%r15)
je 0x7aea
movq 0xc8250(%r14,%r15,8), %rax
movq %rax, 0x8(%rsp)
jmp 0x7af3
movq $0x0, 0x8(%rsp)
movq 0x28(%rsp), %rax
leaq (%r14,%rax), %rdi
movq 0x20(%rsp), %rsi
callq 0x7fea
cmpb $0xc, %bl
ja 0x7d47
movzbl %bl, %ecx
leaq 0x3457(%rip), %rdx      # 0xaf70
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
addq 0x8(%rsp), %rax
jmp 0x7b6a
addq 0x8(%rsp), %rax
jmp 0x7b7e
movq 0x8(%rsp), %rcx
subq %rax, %rcx
movq %rcx, %rax
jmp 0x7b49
addq 0x8(%rsp), %rax
jmp 0x7ba7
addq 0x8(%rsp), %rax
leaq 0x34c3(%rip), %rsi      # 0xb013
jmp 0x7bc2
movq 0x8(%rsp), %rcx
subq %rax, %rcx
movq %rcx, %rax
jmp 0x7b93
movq 0x8(%rsp), %rcx
subq %rax, %rcx
movq %rcx, %rax
leaq 0x2f68(%rip), %rsi      # 0xaad9
jmp 0x7bc2
movq 0x8(%rsp), %rcx
subq %rax, %rcx
movq %rcx, %rax
leaq 0x348d(%rip), %rsi      # 0xb012
jmp 0x7bc2
addq 0x8(%rsp), %rax
jmp 0x7bbb
addq 0x8(%rsp), %rax
leaq 0x347f(%rip), %rsi      # 0xb019
jmp 0x7bc2
movq 0x8(%rsp), %rcx
subq %rax, %rcx
movq %rcx, %rax
leaq 0x346a(%rip), %rsi      # 0xb018
jmp 0x7bc2
movq 0x8(%rsp), %rcx
subq %rax, %rcx
movq %rcx, %rax
leaq 0x345c(%rip), %rsi      # 0xb01e
leal -0xb(%rbx), %ecx
cmpb $-0x9, %cl
movb $0x2, %cl
sbbb $0x0, %cl
movzbl 0xc8660(%r14), %edx
movl %edx, %edi
shll $0x6, %edi
orq %rdx, %rdi
addq %r14, %rdi
movb %cl, 0xc81c8(%r12,%rdi)
movzbl 0xc8660(%r14), %ecx
movl %ecx, %edx
shll $0x9, %edx
leal (%rdx,%rcx,8), %ecx
addq %r14, %rcx
movq %rax, 0xc8250(%rcx,%r12,8)
testq %rax, %rax
je 0x7c1c
movq %rbp, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x20a0
cltq
addq %rax, %rbp
jmp 0x7c23
movb $0x30, (%rbp)
incq %rbp
movb 0xc7f38(%r14,%r12), %al
movb %al, (%rbp)
incq %rbp
incq %r12
movzbl 0xc7f79(%r14), %eax
movl $0x3070, %ecx           # imm = 0x3070
addq %rcx, 0x28(%rsp)
addq %rcx, %r13
incq %r15
cmpq %rax, %r12
jb 0x79fb
movq 0x18(%rsp), %rax
notq %rax
addq %rbp, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa6aa(%rip), %rax      # 0x12320
movl 0x8(%rax), %eax
movl %eax, 0xc8664(%r14)
leaq 0xc81c8(%r14), %rdi
movb $0x0, 0xc8660(%r14)
movl $0x82, %edx
xorl %esi, %esi
callq 0x2110
movq %r14, %rdi
movq %rbp, %rsi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x7228
movq 0xc8670(%r14), %rdi
movq %rbp, %rsi
callq 0x9abe
movq %rax, %rbx
movq 0xc8670(%r14), %rdi
callq 0x9a38
addq %rax, 0xc7d10(%r14)
movzbl 0xc8660(%r14), %eax
movl %eax, %ecx
shll $0x6, %ecx
orq %rax, %rcx
xorps %xmm0, %xmm0
movups %xmm0, 0xc81c8(%r14,%rcx)
movups %xmm0, 0xc81d8(%r14,%rcx)
movups %xmm0, 0xc81e8(%r14,%rcx)
movups %xmm0, 0xc81f8(%r14,%rcx)
movb $0x0, 0xc8208(%r14,%rcx)
subq %rbp, %rbx
movq %rbx, %rax
jmp 0x7c60
movq %r14, %rdi
movq %rbp, %rsi
movq 0x10(%rsp), %rdx
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x7d4e
callq 0x29eb
movl %ebx, %edi
callq 0x29fa