// File: /workspace/github_cpp_makefile_star2_O3/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 $0x48, %rsp
movq %rcx, 0x18(%rsp)
movq %rdx, %r12
movq %rsi, %rcx
movq %rdi, %r15
leaq 0xc8660(%rdi), %rax
movq %rax, 0x10(%rsp)
cmpb $0x0, 0xc7d08(%rdi)
je 0x83ef
movzbl 0xc8660(%r15), %r14d
movl %r14d, %eax
xorb $0x1, %al
movb %al, 0xc8660(%r15)
movzbl %al, %edx
movq %r15, %rdi
movq %rcx, 0x20(%rsp)
movq %rcx, %rsi
callq 0x7e3e
movzbl 0xc8660(%r15), %eax
imulq $0x24c, %rax, %rax      # imm = 0x24C
movzbl 0xc7f79(%r15,%rax), %ebp
imulq $0x24c, %r14, %rcx      # imm = 0x24C
cmpb 0xc7f79(%r15,%rcx), %bpl
jne 0x83d8
leaq (%r15,%rax), %rbx
addq $0xc7d30, %rbx          # imm = 0xC7D30
leaq (%r15,%rcx), %r13
addq $0xc7d30, %r13          # imm = 0xC7D30
movl $0x208, %esi            # imm = 0x208
leaq (%rbx,%rsi), %rdi
addq %r13, %rsi
movq %rbp, %rdx
callq 0x21d0
testl %eax, %eax
jne 0x83d8
testq %rbp, %rbp
je 0x8015
xorl %r12d, %r12d
xorl %ecx, %ecx
movq %r13, 0x8(%rsp)
movslq 0x104(%rbx,%r12,4), %rdx
cmpl 0x104(%r13,%r12,4), %edx
jne 0x8007
movslq (%rbx,%r12,4), %rdi
addq 0x20(%rsp), %rdi
movslq (%r13,%r12,4), %rsi
addq 0x18(%rsp), %rsi
movq %rbx, %r13
movq %rbp, %rbx
movq %r15, %rbp
movq %r14, %r15
movq %rcx, %r14
callq 0x21d0
movq %r14, %rcx
movq %r15, %r14
movq %rbp, %r15
movq %rbx, %rbp
movq %r13, %rbx
movq 0x8(%rsp), %r13
testl %eax, %eax
je 0x800b
btsq %r12, %rcx
incq %r12
cmpq %r12, %rbp
jne 0x7fb6
jmp 0x8017
xorl %ecx, %ecx
leaq 0x60e(%r15), %rdi
leaq 0xc7ce0(%r15), %rsi
movq %rsi, 0x38(%rsp)
movq %rcx, 0x8(%rsp)
movq %rcx, %rdx
callq 0x89e4
testb %al, %al
je 0x8042
incl 0xc7d18(%r15)
cmpb $0x0, 0xc7f79(%r15)
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
je 0x8402
leaq 0xc7d30(%r15), %rax
movq %rax, 0x18(%rsp)
leaq 0xc81c8(%r15), %r12
leaq 0xc8250(%r15), %r13
xorl %ebx, %ebx
leaq 0x3070(%r15), %rax
movq %rax, 0x40(%rsp)
movq %r12, 0x30(%rsp)
movq %r13, 0x28(%rsp)
btq %rbx, %rsi
jae 0x819c
movzbl (%rdi), %eax
imulq $0x24c, %rax, %rcx      # imm = 0x24C
addq 0x18(%rsp), %rcx
movslq (%rcx,%rbx,4), %rdx
movq 0x20(%rsp), %rdi
leaq (%rdi,%rdx), %rbp
movl 0x104(%rcx,%rbx,4), %esi
movl %r14d, %ecx
shll $0x6, %ecx
orq %r14, %rcx
addq %r12, %rcx
movb (%rbx,%rcx), %cl
movb (%rdi,%rdx), %dl
xorl %edi, %edi
cmpb $0x30, %dl
je 0x83c4
cmpb $0x2, %cl
jne 0x81e4
xorl %r8d, %r8d
cmpl $0x10, %esi
jle 0x822e
leal 0x1(%rbx), %eax
movzbl %al, %r13d
imulq $0x3070, %r13, %rdi     # imm = 0x3070
addq %r15, %rdi
movq 0x38(%rsp), %rsi
movl $0x2, %edx
callq 0x3842
movzbl 0xc8660(%r15), %eax
imulq $0x24c, %rax, %rax      # imm = 0x24C
movq 0x18(%rsp), %r12
addq %r12, %rax
movl 0x104(%rax,%rbx,4), %ecx
movq %r15, %rdi
movl %r13d, %esi
movq 0x28(%rsp), %r13
movq %rbp, %rdx
callq 0x7b62
movzbl 0xc8660(%r15), %eax
imulq $0x24c, %rax, %rcx      # imm = 0x24C
addq %r12, %rcx
movq 0x30(%rsp), %r12
movq 0xc7d1c(%r15), %xmm0
movd 0x104(%rcx,%rbx,4), %xmm1
movdqa 0x3e33(%rip), %xmm2     # 0xbfa0
punpckldq %xmm1, %xmm2    # xmm2 = xmm2[0],xmm1[0],xmm2[1],xmm1[1]
paddd %xmm0, %xmm2
movq %xmm2, 0xc7d1c(%r15)
movl %eax, %ecx
shll $0x6, %ecx
orq %rax, %rcx
addq %r12, %rcx
movb $0x0, (%rbx,%rcx)
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdi
jmp 0x8380
movl %r14d, %eax
shll $0x6, %eax
orq %r14, %rax
addq %r12, %rax
movb (%rbx,%rax), %al
movzbl (%rdi), %ecx
movl %ecx, %edx
shll $0x6, %edx
orq %rcx, %rdx
addq %r12, %rdx
movb %al, (%rbx,%rdx)
movl %r14d, %eax
shll $0x9, %eax
leal (%rax,%r14,8), %eax
addq %r13, %rax
movq (%rax,%rbx,8), %rax
movzbl (%rdi), %ecx
movl %ecx, %edx
shll $0x9, %edx
leal (%rdx,%rcx,8), %ecx
addq %r13, %rcx
movq %rax, (%rcx,%rbx,8)
jmp 0x8380
movl %esi, %r8d
cmpl %esi, %edi
cmovgl %edi, %r8d
jge 0x829a
movl %edi, %r9d
xorl %r11d, %r11d
movzbl (%rbp,%r9), %r10d
leal -0x30(%r10), %edi
cmpl $0x9, %edi
ja 0x8396
leaq (%r11,%r11,4), %rdi
leaq (%r10,%rdi,2), %rdi
addq $-0x30, %rdi
cmpq %r11, %rdi
jb 0x80eb
incq %r9
movq %rdi, %r11
cmpq %r9, %r8
jne 0x81f9
jmp 0x829c
cmpl %esi, %edi
jge 0x82ae
movl %edi, %r9d
xorl %edi, %edi
movzbl (%rbp,%r9), %r10d
leal -0x30(%r10), %r11d
cmpl $0xa, %r11d
jb 0x8286
leal -0x61(%r10), %r11d
cmpb $0x5, %r11b
ja 0x8264
cmpb $0x2, %r8b
je 0x80eb
addl $-0x57, %r10d
movb $0x1, %r8b
jmp 0x8283
leal -0x47(%r10), %r11d
cmpb $-0x6, %r11b
jb 0x80eb
cmpb $0x1, %r8b
je 0x80eb
addl $-0x37, %r10d
movb $0x2, %r8b
movl %r10d, %r11d
shlq $0x4, %rdi
movslq %r11d, %r10
addq %r10, %rdi
incq %r9
cmpl %r9d, %esi
jne 0x8237
jmp 0x82b0
xorl %edi, %edi
cmpb $0x30, %dl
movl $0x0, %edx
movl $0xb, %esi
cmovel %esi, %edx
jmp 0x82ca
xorl %edi, %edi
cmpb $0x30, %dl
sete %sil
cmpb $0x2, %r8b
sete %dl
addb %sil, %sil
shlb $0x2, %dl
orb %sil, %dl
addb $0x3, %dl
testb %cl, %cl
je 0x82e1
movl %r14d, %ecx
shll $0x9, %ecx
leal (%rcx,%r14,8), %ecx
addq %r13, %rcx
movq (%rcx,%rbx,8), %rsi
jmp 0x82e3
xorl %esi, %esi
movq %rdi, %r8
subq %rsi, %r8
movl %edx, %ecx
adcb $0x0, %cl
negq %r8
movq %rdi, %rbp
subq %rsi, %rbp
cmovbq %r8, %rbp
addb $-0xb, %dl
cmpb $-0x9, %dl
movb $0x2, %dl
sbbb $0x0, %dl
movl %eax, %esi
shll $0x6, %esi
orq %rax, %rsi
addq %r12, %rsi
movb %dl, (%rbx,%rsi)
movzbl 0xc8660(%r15), %eax
movl %eax, %edx
shll $0x9, %edx
leal (%rdx,%rax,8), %eax
addq %r13, %rax
movq %rdi, (%rax,%rbx,8)
imulq $0x3070, %rbx, %r12     # imm = 0x3070
movq 0x40(%rsp), %rax
leaq (%r12,%rax), %rdi
movzbl %cl, %edx
movq 0x38(%rsp), %r13
movq %r13, %rsi
callq 0x3842
leaq (%r12,%r15), %rdi
addq $0x367e, %rdi           # imm = 0x367E
movq 0x30(%rsp), %r12
movq %r13, %rsi
movq 0x28(%rsp), %r13
movq %rbp, %rdx
callq 0x89e4
testb %al, %al
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
je 0x8380
incl 0xc7d18(%r15)
incq %rbx
movzbl 0xc7f79(%r15), %eax
cmpq %rax, %rbx
jb 0x808c
jmp 0x8402
movl %r10d, %edi
orl $0x20, %edi
addl $-0x67, %edi
cmpl $-0x6, %edi
jb 0x80eb
xorl %edi, %edi
cmpb $0x30, %dl
sete %dil
cmpb $0x61, %r10b
movl $0x0, %r8d
adcb $0x1, %r8b
jmp 0x80e2
movl $0x1, %edi
cmpb $0x30, 0x1(%rbp)
jne 0x80d6
jmp 0x80eb
movq %r15, %rdi
movq %r12, %rsi
movq 0x20(%rsp), %rdx
movq 0x10(%rsp), %rcx
callq 0x2f84
jmp 0x8402
movq 0x10(%rsp), %rdi
movq %r15, %rsi
movq %rcx, %rdx
movq %r12, %rcx
callq 0x2f3b
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop