// File: /workspace/github_cpp_makefile_star2_O3/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 0x8745
movzbl 0xc8660(%r14), %ebx
movl %ebx, %eax
xorb $0x1, %al
movb %al, 0xc8660(%r14)
movq 0xc7d10(%r14), %rax
leaq 0x8d47(%rip), %rcx      # 0x111a0
cmpq (%rcx), %rax
je 0x878a
movq %r14, %rdi
movq 0x10(%rsp), %rsi
xorl %edx, %edx
callq 0x7e3e
cmpl $0x4, 0xc8664(%r14)
jle 0x87fa
leaq 0x60e(%r14), %rdi
leaq 0xc7ce0(%r14), %rsi
movq %rsi, 0x20(%rsp)
callq 0x8a62
movq %rax, 0x30(%rsp)
cmpb $0x0, 0xc7f79(%r14)
movq %rbp, 0x18(%rsp)
je 0x872b
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 0x852c
leaq (%r14,%r13), %rdi
movq 0x20(%rsp), %rsi
callq 0x406a
movl %eax, %ebx
cmpb $0x2, %bl
jne 0x85a6
leaq 0x1(%r12), %rax
movzbl %al, %esi
movq %r14, %rdi
movq %rbp, %rdx
callq 0x7dc6
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 0x86f9
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 0x8704
cmpb $0x0, 0xc81c8(%r14,%r15)
je 0x85c0
movq 0xc8250(%r14,%r15,8), %rax
movq %rax, 0x8(%rsp)
jmp 0x85c9
movq $0x0, 0x8(%rsp)
movq 0x28(%rsp), %rax
leaq (%r14,%rax), %rdi
movq 0x20(%rsp), %rsi
callq 0x8a62
cmpb $0xc, %bl
ja 0x8818
movzbl %bl, %ecx
leaq 0x39c1(%rip), %rdx      # 0xbfb0
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
addq 0x8(%rsp), %rax
jmp 0x8647
movq 0x8(%rsp), %rcx
subq %rax, %rcx
movq %rcx, %rax
jmp 0x861e
movq 0x8(%rsp), %rcx
subq %rax, %rcx
movq %rcx, %rax
jmp 0x862c
addq 0x8(%rsp), %rax
leaq 0x3a35(%rip), %rsi      # 0xc05a
jmp 0x8698
addq 0x8(%rsp), %rax
leaq 0x3a21(%rip), %rsi      # 0xc054
jmp 0x8698
addq 0x8(%rsp), %rax
jmp 0x865b
movq 0x8(%rsp), %rcx
subq %rax, %rcx
movq %rcx, %rax
leaq 0x351b(%rip), %rsi      # 0xbb69
jmp 0x8698
movq 0x8(%rsp), %rcx
subq %rax, %rcx
movq %rcx, %rax
leaq 0x39ec(%rip), %rsi      # 0xc04e
jmp 0x8698
addq 0x8(%rsp), %rax
jmp 0x8683
movq 0x8(%rsp), %rcx
subq %rax, %rcx
movq %rcx, %rax
jmp 0x8691
movq 0x8(%rsp), %rcx
subq %rax, %rcx
movq %rcx, %rax
leaq 0x39c5(%rip), %rsi      # 0xc04f
jmp 0x8698
addq 0x8(%rsp), %rax
leaq 0x39bd(%rip), %rsi      # 0xc055
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 0x86f2
movq %rbp, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x20a0
cltq
addq %rax, %rbp
jmp 0x86f9
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 0x84d1
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 0xabd4(%rip), %rax      # 0x13320
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 0x7d48
movq 0xc8670(%r14), %rdi
movq %rbp, %rsi
callq 0xa612
movq %rax, %rbx
movq 0xc8670(%r14), %rdi
callq 0xa58c
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 0x8736
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 0x8820
movl %ebx, %edi
callq 0x3039
nop