// File: /workspace/github_cpp_makefile_star2_O2/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, %r13
movq %rdi, %r14
cmpb $0x0, 0xc7d08(%rdi)
je 0x695c
movzbl 0xc8660(%r14), %r15d
movl %r15d, %eax
xorb $0x1, %al
movb %al, 0xc8660(%r14)
movq 0xc7d10(%r14), %rax
leaq 0x8b28(%rip), %rcx      # 0xf1d0
cmpq (%rcx), %rax
je 0x69a1
movq %r14, %rdi
movq 0x10(%rsp), %rsi
xorl %edx, %edx
callq 0x60a2
cmpl $0x4, 0xc8664(%r14)
jle 0x6a0d
leaq 0x60e(%r14), %rdi
leaq 0xc7ce0(%r14), %rsi
callq 0x6d04
movq %rax, %r12
leaq 0xc8250(%r14), %rax
movq %rax, 0x8(%rsp)
imulq $0x208, %r15, %rax      # imm = 0x208
addq %r14, %rax
addq $0xc8250, %rax          # imm = 0xC8250
movq %rax, 0x18(%rsp)
imulq $0x41, %r15, %rax
addq %r14, %rax
addq $0xc81c8, %rax          # imm = 0xC81C8
movq %rax, 0x20(%rsp)
xorl %r15d, %r15d
movq %r13, 0x28(%rsp)
movq %r12, 0x30(%rsp)
movzbl 0xc7f79(%r14), %eax
cmpq %rax, %r15
jae 0x693f
btq %r15, %r12
jae 0x678a
leaq 0x1(%r15), %rbx
movzbl %bl, %esi
movq %r14, %rdi
callq 0x5fec
cmpb $0x2, %al
jne 0x6803
movzbl %bl, %esi
movq %r14, %rdi
movq %r13, %rdx
callq 0x602c
movq %rax, %r13
movzbl 0xc8660(%r14), %eax
imulq $0x41, %rax, %rax
addq 0x8(%rsp), %rax
movb $0x0, -0x88(%r15,%rax)
movb 0xc7f38(%r14,%r15), %al
jmp 0x6930
movl 0xc7e34(%r14,%r15,4), %ebp
movslq 0xc7d30(%r14,%r15,4), %rsi
addq 0x10(%rsp), %rsi
movq %r13, %rdi
movq %rbp, %rdx
callq 0x21a0
movb 0xc7f38(%r14,%r15), %al
movb %al, (%r13,%rbp)
addq %rbp, %r13
movq 0x20(%rsp), %rax
movb (%rax,%r15), %al
movzbl 0xc8660(%r14), %ecx
imulq $0x41, %rcx, %rcx
movq 0x8(%rsp), %rdx
addq %rdx, %rcx
movb %al, -0x88(%r15,%rcx)
movq 0x18(%rsp), %rax
movq (%rax,%r15,8), %rax
movzbl 0xc8660(%r14), %ecx
imulq $0x208, %rcx, %rcx      # imm = 0x208
addq %rdx, %rcx
movq %rax, (%rcx,%r15,8)
jmp 0x6934
movl %eax, %ebp
movq 0x20(%rsp), %rax
cmpb $0x0, (%rax,%r15)
je 0x681c
movq 0x18(%rsp), %rax
movq (%rax,%r15,8), %r12
jmp 0x681f
xorl %r12d, %r12d
movzbl %bl, %esi
movq %r14, %rdi
callq 0x600a
movzbl %bpl, %esi
cmpb $0xc, %bpl
ja 0x6a2b
leaq 0x36f1(%rip), %rdx      # 0x9f30
movslq (%rdx,%rsi,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
addq %r12, %rax
jmp 0x6880
subq %rax, %r12
movq %r12, %rax
jmp 0x6860
subq %rax, %r12
movq %r12, %rax
jmp 0x686c
addq %r12, %rax
leaq 0x3773(%rip), %rsi      # 0x9fda
jmp 0x68be
addq %r12, %rax
leaq 0x3761(%rip), %rsi      # 0x9fd4
jmp 0x68be
addq %r12, %rax
jmp 0x688f
subq %rax, %r12
movq %r12, %rax
leaq 0x3252(%rip), %rsi      # 0x9ad9
jmp 0x68be
subq %rax, %r12
movq %r12, %rax
leaq 0x3738(%rip), %rsi      # 0x9fce
jmp 0x68be
addq %r12, %rax
jmp 0x68ab
subq %rax, %r12
movq %r12, %rax
jmp 0x68b7
subq %rax, %r12
movq %r12, %rax
leaq 0x371d(%rip), %rsi      # 0x9fcf
jmp 0x68be
addq %r12, %rax
leaq 0x3717(%rip), %rsi      # 0x9fd5
addb $-0xb, %bpl
cmpb $-0x9, %bpl
movb $0x2, %cl
sbbb $0x0, %cl
movzbl 0xc8660(%r14), %edx
imulq $0x41, %rdx, %rdx
movq 0x8(%rsp), %rdi
addq %rdi, %rdx
movb %cl, -0x88(%r15,%rdx)
movzbl 0xc8660(%r14), %ecx
imulq $0x208, %rcx, %rcx      # imm = 0x208
addq %rdi, %rcx
movq %rax, (%rcx,%r15,8)
testq %rax, %rax
movq 0x30(%rsp), %r12
je 0x691b
movq %r13, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x20a0
cltq
addq %rax, %r13
jmp 0x6923
movb $0x30, (%r13)
incq %r13
movq 0x8(%rsp), %rax
movb -0x318(%rax,%r15), %al
movb %al, (%r13)
incq %r13
incq %r15
jmp 0x6724
movq 0x28(%rsp), %rax
notq %rax
addq %rax, %r13
movq %r13, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa9ed(%rip), %rax      # 0x11350
movl 0x8(%rax), %eax
movl %eax, 0xc8664(%r14)
leaq 0xc81c8(%r14), %rdi
movb $0x0, 0xc8660(%r14)
movl $0x82, %edx
xorl %esi, %esi
callq 0x2130
movq %r14, %rdi
movq %r13, %rsi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5fae
movq 0xc8670(%r14), %rdi
movq %r13, %rsi
callq 0x883e
movq %r13, %rbx
movq %rax, %r13
movq 0xc8670(%r14), %rdi
callq 0x87ba
addq %rax, 0xc7d10(%r14)
movzbl 0xc8660(%r14), %eax
imulq $0x41, %rax, %rax
xorps %xmm0, %xmm0
movups %xmm0, 0xc81c8(%r14,%rax)
movups %xmm0, 0xc81d8(%r14,%rax)
movups %xmm0, 0xc81e8(%r14,%rax)
movups %xmm0, 0xc81f8(%r14,%rax)
movb $0x0, 0xc8208(%r14,%rax)
subq %rbx, %r13
jmp 0x694a
movq %r14, %rdi
movq %r13, %rsi
movq 0x10(%rsp), %rdx
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6a3a
leaq 0x35ae(%rip), %rdi      # 0x9fe0
xorl %eax, %eax
callq 0x2350
nop