// File: /workspace/github_cpp_makefile_star2_O1/Infinidat[P]slimfastq/recs.cpp
// Function: RecSave::save(unsigned char const*, unsigned char const*, unsigned char const*, unsigned char const*)
void RecSave::save(const UCHAR* buf, const UCHAR* end, const UCHAR* prev_buf, const UCHAR* prev_end) {

    rarely_if(not m_last.initilized) {
        imap = 0;
        save_first_line(buf, end);
        map_space(buf, imap);
        bzero(ctype[0], sizeof(ctype[0])); // clear cache
        bzero(ctype[1], sizeof(ctype[1])); // clear cache
        return;
    }
    bool pmap = imap;
    imap = imap ? 0 : 1;
    map_space(buf, imap);

    rarely_if (smap[imap].len != smap[pmap].len or
               memcmp(smap[imap].str, smap[pmap].str, smap[imap].len)) {
        // Too bad, new kind of header
        // must push to extension file

        x_file->put(g_record_count - m_last.index);
        m_last.index = g_record_count;
        x_file->put_str(buf, end-buf);
        bzero(ctype[imap], sizeof(ctype[imap])); // clear cache

        stats.new_n ++ ;
        stats.new_l += end-buf;
        return;
    }

    UINT64 map = 0;
    for (int i = 0; i < smap[imap].len; i ++)
        if ( smap[imap].wln[i] != smap[pmap].wln[i] or
             memcmp(buf + smap[imap].off[i], prev_buf + smap[pmap].off[i], smap[imap].wln[i]))
            DO_SET(map, i);

    put_num (0, map);           // mapping of changes. (zero could have meant copy prev)

    for (int i = 0; i < smap[0].len; i++) {
        if (IS_SET(map, i)) {

            // int i = BFIRST(map)-1;
            // DO_CLR(map, i);
            const UCHAR* b = buf + smap[imap].off[i];
            // const UCHAR* p = prev_buf + smap[pmap].off[i];
            UINT64 bnum;

            UCHAR type = numberwang(b, smap[imap].wln[i], bnum, ctype[pmap][i]);
            if  ( type == ST_STR) {
                put_type(i+1, type);
                put_str (i+1, b, smap[imap].wln[i]);
                stats.str_n ++ ;
                stats.str_l += smap[imap].wln[i];
                ctype[imap][i] = 0; // NAN
                continue;
            }
            // HERE: this is a number
            UINT64 pnum = ctype[pmap][i] ? cnumb[pmap][i] : 0;
            UINT64 gap ;

            ctype[imap][i] = (type < ST_STR or type >= ST_DGT_Z) ? 1 : 2;
            cnumb[imap][i] = bnum;

            if (bnum < pnum) {
                gap  = pnum - bnum;
                type ++ ;
            }
            else
                gap  = bnum - pnum;

            put_type(i+1, type);
            put_num (i+1, gap);

            //     if (is_number(b, smap[imap].wln[i], bnum) and
            //         is_number(p, smap[pmap].wln[i], pnum)) {
            //         if (bnum > pnum) {
            //             put_type(i+1, ST_DGT);
            //             put_num (i+1, bnum - pnum);
            //         }
            //         else {
            //             put_type(i+1, ST_DLT);
            //             put_num (i+1, pnum - bnum);
            //         }
            //     }
            //     else {
            //         put_type(i+1, ST_STR);
            //         put_str (i+1, b, smap[imap].wln[i]);
            //         stats.str_n ++ ;
            //         stats.str_l += smap[imap].wln[i];
            //     }
        }
        else {
            ctype[imap][i] = ctype[pmap][i];
            cnumb[imap][i] = cnumb[pmap][i];
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdx, %r14
movq %rsi, 0x18(%rsp)
leaq 0xc8660(%rdi), %rax
movq %rax, 0x28(%rsp)
movq %rdi, 0x8(%rsp)
cmpb $0x0, 0xc7d08(%rdi)
je 0x7916
movq %rcx, %r15
movq 0x8(%rsp), %rbx
movzbl 0xc8660(%rbx), %r13d
movl %r13d, %eax
xorb $0x1, %al
movb %al, 0xc8660(%rbx)
movzbl %al, %edx
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
callq 0x731e
movzbl 0xc8660(%rbx), %eax
imulq $0x24c, %rax, %rax      # imm = 0x24C
movzbl 0xc7f79(%rbx,%rax), %ebp
imulq $0x24c, %r13, %rcx      # imm = 0x24C
cmpb 0xc7f79(%rbx,%rcx), %bpl
jne 0x78fd
movq 0x8(%rsp), %rdx
leaq (%rdx,%rax), %rbx
addq $0xc7d30, %rbx          # imm = 0xC7D30
leaq (%rdx,%rcx), %r12
addq $0xc7d30, %r12          # imm = 0xC7D30
movl $0x208, %esi            # imm = 0x208
leaq (%rbx,%rsi), %rdi
addq %r12, %rsi
movq %rbp, %rdx
callq 0x21d0
testl %eax, %eax
jne 0x78fd
movq %r13, 0x10(%rsp)
testq %rbp, %rbp
je 0x74cf
xorl %r13d, %r13d
xorl %r14d, %r14d
movslq 0x104(%rbx,%r13,4), %rdx
cmpl 0x104(%r12,%r13,4), %edx
jne 0x74c1
movslq (%rbx,%r13,4), %rdi
movq 0x18(%rsp), %rax
addq %rax, %rdi
movslq (%r12,%r13,4), %rsi
addq %r15, %rsi
callq 0x21d0
testl %eax, %eax
je 0x74c5
btsq %r13, %r14
incq %r13
cmpq %r13, %rbp
jne 0x7493
jmp 0x74d2
xorl %r14d, %r14d
movq 0x8(%rsp), %rax
leaq 0x60e(%rax), %rdi
leaq 0xc7ce0(%rax), %rsi
movq %rsi, 0x40(%rsp)
movq %r14, 0x20(%rsp)
movq %r14, %rdx
callq 0x7f6c
testb %al, %al
je 0x7506
movq 0x8(%rsp), %rax
incl 0xc7d18(%rax)
movq 0x8(%rsp), %rax
cmpb $0x0, 0xc7f79(%rax)
movq 0x10(%rsp), %r13
movq 0x20(%rsp), %rdi
je 0x792d
movq 0x8(%rsp), %rax
leaq 0xc7d30(%rax), %rcx
movq %rcx, 0x48(%rsp)
leaq 0xc81c8(%rax), %r15
leaq 0xc8250(%rax), %rcx
movq %rcx, 0x38(%rsp)
xorl %ebx, %ebx
addq $0x3070, %rax           # imm = 0x3070
movq %rax, 0x50(%rsp)
movq %r15, 0x30(%rsp)
btq %rbx, %rdi
jae 0x75be
movq 0x28(%rsp), %rax
movzbl (%rax), %eax
imulq $0x24c, %rax, %rcx      # imm = 0x24C
addq 0x48(%rsp), %rcx
movslq (%rcx,%rbx,4), %rdx
movq 0x18(%rsp), %rdi
leaq (%rdi,%rdx), %r12
movl 0x104(%rcx,%rbx,4), %esi
movl %r13d, %ecx
shll $0x6, %ecx
orq %r13, %rcx
addq %r15, %rcx
movb (%rbx,%rcx), %cl
movb (%rdi,%rdx), %dl
xorl %r9d, %r9d
cmpb $0x30, %dl
je 0x78e3
cmpb $0x2, %cl
jne 0x7610
xorl %edi, %edi
movb $0x2, %r8b
cmpl $0x10, %esi
jle 0x7660
xorl %ebp, %ebp
jmp 0x7748
movl %r13d, %eax
shll $0x6, %eax
orq %r13, %rax
addq %r15, %rax
movb (%rbx,%rax), %al
movq 0x28(%rsp), %rsi
movzbl (%rsi), %ecx
movl %ecx, %edx
shll $0x6, %edx
orq %rcx, %rdx
addq %r15, %rdx
movb %al, (%rbx,%rdx)
movl %r13d, %eax
shll $0x9, %eax
leal (%rax,%r13,8), %eax
movq 0x38(%rsp), %r8
addq %r8, %rax
movq (%rax,%rbx,8), %rax
movzbl (%rsi), %ecx
movl %ecx, %edx
shll $0x9, %edx
leal (%rdx,%rcx,8), %ecx
addq %r8, %rcx
movq %rax, (%rcx,%rbx,8)
jmp 0x78c9
movl %esi, %edi
cmpl %esi, %r9d
cmovgl %r9d, %edi
jge 0x76da
movl %r9d, %r8d
xorl %ebp, %ebp
movzbl (%r12,%r8), %r10d
leal -0x30(%r10), %r9d
cmpl $0x9, %r9d
ja 0x7718
leaq (,%rbp,4), %r9
addq %rbp, %r9
leaq (%r10,%r9,2), %r9
addq $-0x30, %r9
cmpq %rbp, %r9
jb 0x7745
incq %r8
movq %r9, %rbp
cmpq %r8, %rdi
jne 0x7624
jmp 0x76dd
cmpl %esi, %r9d
jge 0x76f4
movl %r9d, %r9d
xorl %ebp, %ebp
xorl %r10d, %r10d
movzbl (%r12,%r9), %r11d
leal -0x30(%r11), %r13d
cmpl $0xa, %r13d
jb 0x76bb
leal -0x61(%r11), %r13d
cmpb $0x5, %r13b
ja 0x7695
cmpb $0x2, %dil
jne 0x76b1
xorl %r13d, %r13d
jmp 0x76cb
leal -0x47(%r11), %r14d
xorl %r13d, %r13d
cmpb $-0x6, %r14b
jb 0x76cb
cmpb $0x1, %dil
je 0x76cb
addl $-0x37, %r11d
movb $0x2, %dil
jmp 0x76b8
addl $-0x57, %r11d
movb $0x1, %dil
movl %r11d, %r13d
shlq $0x4, %r10
movslq %r13d, %rbp
addq %r10, %rbp
movb $0x1, %r13b
movq %rbp, %r10
testb %r13b, %r13b
je 0x7711
incq %r9
cmpl %r9d, %esi
jne 0x7671
jmp 0x76f6
xorl %r9d, %r9d
cmpb $0x30, %dl
movl $0x0, %r8d
movl $0xb, %edx
cmovel %edx, %r8d
movq %r9, %rbp
jmp 0x7748
xorl %ebp, %ebp
cmpb $0x30, %dl
sete %dl
cmpb $0x2, %dil
sete %r8b
addb %dl, %dl
shlb $0x2, %r8b
orb %dl, %r8b
addb $0x3, %r8b
movq 0x10(%rsp), %r13
jmp 0x7748
movl %r10d, %edi
orl $0x20, %edi
addl $-0x67, %edi
movb $0x2, %r8b
cmpl $-0x6, %edi
jb 0x7748
xorl %r9d, %r9d
cmpb $0x30, %dl
sete %r9b
cmpb $0x61, %r10b
movl $0x0, %edi
adcb $0x1, %dil
jmp 0x75ab
movb $0x2, %r8b
cmpb $0x2, %r8b
jne 0x7802
leal 0x1(%rbx), %eax
movzbl %al, %r13d
imulq $0x3070, %r13, %rdi     # imm = 0x3070
movq 0x8(%rsp), %r15
addq %r15, %rdi
movzbl %r8b, %edx
movq 0x40(%rsp), %rsi
callq 0x3270
movzbl 0xc8660(%r15), %eax
imulq $0x24c, %rax, %rax      # imm = 0x24C
movq 0x48(%rsp), %r14
addq %r14, %rax
movl 0x104(%rax,%rbx,4), %ecx
movq %r15, %rdi
movl %r13d, %esi
movq 0x10(%rsp), %r13
movq %r12, %rdx
callq 0x7042
movzbl 0xc8660(%r15), %eax
imulq $0x24c, %rax, %rcx      # imm = 0x24C
addq %r14, %rcx
movq 0xc7d1c(%r15), %xmm0
movd 0x104(%rcx,%rbx,4), %xmm1
movdqa 0x378d(%rip), %xmm2     # 0xaf60
punpckldq %xmm1, %xmm2    # xmm2 = xmm2[0],xmm1[0],xmm2[1],xmm1[1]
paddd %xmm0, %xmm2
movq %xmm2, 0xc7d1c(%r15)
movq 0x30(%rsp), %r15
movl %eax, %ecx
shll $0x6, %ecx
orq %rax, %rcx
addq %r15, %rcx
movb $0x0, (%rbx,%rcx)
movq 0x20(%rsp), %rdi
jmp 0x78c9
testb %cl, %cl
je 0x781e
movl %r13d, %ecx
shll $0x9, %ecx
leal (%rcx,%r13,8), %ecx
movq 0x38(%rsp), %rdi
addq %rdi, %rcx
movq (%rcx,%rbx,8), %rdx
jmp 0x7825
xorl %edx, %edx
movq 0x38(%rsp), %rdi
movq %rbp, %rsi
subq %rdx, %rsi
movl %r8d, %ecx
adcb $0x0, %cl
negq %rsi
movq %rbp, %r12
subq %rdx, %r12
cmovbq %rsi, %r12
addb $-0xb, %r8b
cmpb $-0x9, %r8b
movb $0x2, %dl
sbbb $0x0, %dl
movl %eax, %esi
shll $0x6, %esi
orq %rax, %rsi
addq %r15, %rsi
movb %dl, (%rbx,%rsi)
movq 0x8(%rsp), %r13
movzbl 0xc8660(%r13), %eax
movl %eax, %edx
shll $0x9, %edx
leal (%rdx,%rax,8), %eax
addq %rdi, %rax
movq %rbp, (%rax,%rbx,8)
imulq $0x3070, %rbx, %r14     # imm = 0x3070
movq 0x50(%rsp), %rax
leaq (%r14,%rax), %rdi
movzbl %cl, %edx
movq 0x40(%rsp), %r15
movq %r15, %rsi
callq 0x3270
leaq (%r14,%r13), %rdi
addq $0x367e, %rdi           # imm = 0x367E
movq %r15, %rsi
movq %r12, %rdx
callq 0x7f6c
testb %al, %al
je 0x78ba
movq 0x8(%rsp), %rax
incl 0xc7d18(%rax)
movq 0x20(%rsp), %rdi
movq 0x30(%rsp), %r15
movq 0x10(%rsp), %r13
incq %rbx
movq 0x8(%rsp), %rax
movzbl 0xc7f79(%rax), %eax
cmpq %rax, %rbx
jb 0x7558
jmp 0x792d
movl $0x1, %r9d
movb $0x2, %r8b
cmpb $0x30, 0x1(%r12)
jne 0x75a4
jmp 0x7748
movq 0x8(%rsp), %rdi
movq %r14, %rsi
movq 0x18(%rsp), %rdx
movq 0x28(%rsp), %rcx
callq 0x2936
jmp 0x792d
movq 0x28(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %r14, %rcx
callq 0x28ed
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq