// File: /workspace/github_cpp_makefile_star2_O3/JMMackenzie[P]binary_to_jass/bin_to_jass.cpp
// Function: score_index(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, double, char)
void score_index(std::string ds2i_prefix,
                 std::string lex_file,
                 const double quant_level,
                 char compression){

	  // Tell JASS that we're using QMX-D1: we need to compute our own deltas
    ANT_compress *compression_scheme;
    if (compression == 'q')
        compression_scheme = new ANT_compress_qmx();
    else if (compression == 'G')
        compression_scheme = new JASS::compress_integer_elias_gamma_simd();
    else if (compression == 'g')
        compression_scheme = new JASS::compress_integer_elias_gamma_simd_vb();
    else
         compression_scheme = nullptr;
    ANT_compress &compressor = *compression_scheme;


    uint32_t sse_alignment = 16;
    std::vector<plist_t> m_postings_lists;

    // 1. Read lexicon file and start building posting metadata
    std::ifstream lex_data(lex_file);
    std::string term;
    uint32_t term_id = 0;
    while (lex_data >> term) {
      m_postings_lists.emplace_back(term, term_id);
      ++term_id;
    }
    std::cerr << "Read " << m_postings_lists.size() << " terms.\n";

    std::string docs_file = ds2i_prefix + ".docs";
    std::string freqs_file = ds2i_prefix + ".freqs";
    std::string sizes_file = ds2i_prefix + ".sizes";

    auto df = fopen(docs_file.c_str(), "rb");
    auto ff = fopen(freqs_file.c_str(), "rb");
    auto sf = fopen(sizes_file.c_str(), "rb");

    std::vector<uint32_t> doc_list;
    std::vector<uint32_t> freq_list;
    std::vector<uint32_t> size_list;

    double global_maximum = 0.0;

    // Get no. docs    
    read_uint32_list(doc_list,df);
    uint32_t no_docs = doc_list[0];

    // Read sizes
    read_uint32_list(size_list,sf);
  
    // validate sizes
    if (size_list.size() != no_docs) {
      std::cerr << "Something went wrong: Sizes not matched with no. docs." 
                << std::endl;
      exit(EXIT_FAILURE);
    }

    std::cerr << "Processing " << no_docs << " documents." << std::endl;

    // Compute mean document length
    float average_len = std::accumulate(size_list.begin(), size_list.end(), 0.0)/no_docs; 

    bm25_t ranker(float(no_docs), average_len);

    term_id = 0;
    while (!feof(df)) {
      read_uint32_list(doc_list,df);
      read_uint32_list(freq_list,ff);
      size_t doc_n = doc_list.size();
      size_t freq_n = freq_list.size();
      if (doc_n != freq_n) {
        std::cerr << "Files are not aligned.\n";
        exit(EXIT_FAILURE);
      }
      // Exhausted our lists
      if (doc_n == 0) {
        // Ensure validity
        if (term_id != m_postings_lists.size()) {
          std::cerr << "Error: Should have read more data. Exit." << std::endl;
          exit(EXIT_FAILURE);
        }
        break;
      }

      // Query term weight (compute once)
      float w_qt = ranker.query_term_weight(doc_n);

      // Resize our PL
      m_postings_lists[term_id].postings_list.resize(doc_list.size());

      // Score the PL and dump it
      for (size_t i = 0; i < doc_list.size(); ++i) {
        uint32_t doc_id = doc_list[i];
        uint32_t freq = freq_list[i];
        double impact = w_qt * ranker.doc_term_weight(float(freq), float(size_list[doc_id]));
        global_maximum = std::max(global_maximum, impact);
        m_postings_lists[term_id].postings_list[i].docid = doc_id;
        m_postings_lists[term_id].postings_list[i].score = impact;
      }
      ++term_id;
    }

    // We have scored all postings, now we must quantize them
    std::cerr << "Maximum score = " << global_maximum << std::endl;
    std::cerr << "Quantization range = [1, " << quant_level << "]" << std::endl;
    for(size_t i = 0; i < m_postings_lists.size(); ++i) {
      for (size_t j = 0; j < m_postings_lists[i].postings_list.size(); ++j) {
        double score = m_postings_lists[i].postings_list[j].score;
        m_postings_lists[i].postings_list[j].q_score = (uint32_t) std::ceil(quant_level * score / global_maximum);
      }
    }


    // prepare output
    std::ofstream vocab_terms("CIvocab_terms.bin");
    std::ofstream vocab("CIvocab.bin", std::ofstream::binary);
    std::ofstream postings("CIpostings.bin", std::ofstream::binary);
    size_t terms_offset = 0;
    size_t pl_offset = 0;





    postings.write((char *)&compression, sizeof(uint8_t));
    pl_offset = postings.tellp();

    // Now we need to get them out in the expected JASS format
    for(size_t i = 0; i < m_postings_lists.size(); ++i) {

      // Currently processing this list
      std::string current_term = m_postings_lists[i].term;

      // Write the offset in the vocab file
      vocab.write((char *)&terms_offset, sizeof(size_t));
      vocab.write((char *)&pl_offset, sizeof(size_t));

      // Write term to the terms file
      vocab_terms << current_term << '\0';
    
      // Update offsets
      terms_offset = vocab_terms.tellp();

      // Deal with postings
     
      uint32_t max_quant = uint32_t(quant_level);
      std::vector<std::vector<uint32_t>> pl_segments(max_quant);
      for(size_t j = 0; j < m_postings_lists[i].postings_list.size(); ++j) {
        uint32_t score = m_postings_lists[i].postings_list[j].q_score - 1;
        uint32_t docid = m_postings_lists[i].postings_list[j].docid;
        pl_segments[score].push_back(docid + 1); // DocID in JASS is 1->N, 0 is sentinel. Create this offset now.
        // Can now remove this PL...
      }
    
      std::vector<compressed_segment_t> jass_pl;

      // Count non-zero segments and compress them
      uint64_t valid_segments = 0;
      for (size_t j = 0; j < pl_segments.size(); ++j) {
        if (pl_segments[j].size() == 0)
          continue;
      
        // Give the buffer double + some incase of overflow
        std::vector<uint32_t> compressed_segment (2 * pl_segments[j].size() + 1024);
        uint64_t used = compressed_segment.size() * sizeof(compressed_segment[0]);
       
        // Delta encode the document identifiers
        fastDelta(pl_segments[j].data(), pl_segments[j].size()); 
        compressor.encodeArray(pl_segments[j].data(), pl_segments[j].size(), compressed_segment.data(), &used);

        uint64_t used_padded = ((used + sse_alignment - 1) / sse_alignment) * sse_alignment;  // SSE alignment
        // Resize to padded size
        size_t elements = used_padded/sizeof(uint32_t);
        compressed_segment.resize(elements);
        uint16_t impact = j + 1;
        uint32_t frequency = pl_segments[j].size();
        
        jass_pl.emplace_back(impact, frequency, used, used_padded, std::move(compressed_segment));
           
      } 
      // Tell vocab how many segments are here
      valid_segments = jass_pl.size();
      vocab.write((char *)&valid_segments, sizeof(uint64_t));
 
      // We can now write our PL's to file
      // 1. Write the k pointers to headers via arithmetic
      uint32_t header_size = sizeof(uint16_t) + sizeof(uint64_t) + sizeof(uint64_t) + sizeof(uint32_t);
      // Address of the first header is the (number of headers * size of the pointer) + current postings offset.
      uint64_t header_pointer = valid_segments * sizeof(uint64_t) + pl_offset;
      for (size_t i = 0; i < valid_segments; ++i) {
        postings.write((char *)&header_pointer, sizeof(uint64_t));
        header_pointer += header_size;
      }


      uint64_t posting_offset = header_size * (valid_segments + 1) + valid_segments * sizeof(uint64_t) + pl_offset;
      // Ensure SSE alignment is OK
      size_t sse_offset = ((posting_offset + sse_alignment - 1)/sse_alignment) * sse_alignment;
      size_t sse_padding = sse_offset - posting_offset;
      posting_offset = sse_offset;

      // 2. Let's write the header quads now
      for (size_t i = 0; i < jass_pl.size(); ++i) {
        uint16_t impact = jass_pl[i].impact;
        uint32_t frequency = jass_pl[i].frequency;
        uint64_t length = jass_pl[i].length;
        uint64_t length_with_padding = jass_pl[i].length_with_padding;
        uint64_t end = posting_offset + length;
      
        // Write the data
        postings.write((char *)&impact, sizeof(uint16_t));
        postings.write((char *)&posting_offset, sizeof(uint64_t));
        postings.write((char *)&end, sizeof(uint64_t));
        postings.write((char *)&frequency, sizeof(uint32_t));

        // Update offsets for next round
        posting_offset += length_with_padding;

      }

      // Don't forget the zero block
      uint8_t zero_block[] = {0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0};
      postings.write((char *)&zero_block, header_size);

      // More SSE checks
      if (sse_padding != 0) {
        if (sse_padding > sizeof(zero_block)) {
          std::cerr << "Padding is too large (sorry). Fix the code." << std::endl;
          exit(EXIT_FAILURE);
        }
        // Pad some more zeros
        postings.write((char *)zero_block, sse_padding);
      }

      // 4.  Now we write the actual postings lists
      for (size_t i = 0; i < jass_pl.size(); ++i) {
        postings.write((char *)jass_pl[i].compressed_ids.data(), jass_pl[i].length_with_padding);
      }
  
      pl_offset = postings.tellp();

    } 
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x9f8, %rsp            # imm = 0x9F8
vmovsd %xmm0, 0x68(%rsp)
movq %rsi, %r14
movq %rdi, %rbx
movb %dl, 0xf(%rsp)
cmpl $0x47, %edx
je 0x28c2
cmpl $0x67, %edx
je 0x28b9
cmpl $0x71, %edx
jne 0x28e4
movl $0x18, %edi
callq 0x21d0
movq %rax, 0x80(%rsp)
movq %rax, %rdi
callq 0x51f4
jmp 0x28f0
leaq 0x5b430(%rip), %r15     # 0x5dcf0
jmp 0x28c9
leaq 0x5b3cf(%rip), %r15     # 0x5dc98
addq $0x10, %r15
movl $0x8, %edi
callq 0x21d0
movq %rax, 0x80(%rsp)
movq %r15, (%rax)
jmp 0x28f0
movq $0x0, 0x80(%rsp)
vxorpd %xmm0, %xmm0, %xmm0
vmovapd %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
leaq 0x7f0(%rsp), %rdi
movq %r14, %rsi
movl $0x8, %edx
callq 0x22d0
leaq 0x160(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
xorl %eax, %eax
leaq 0x7f0(%rsp), %r14
leaq 0x150(%rsp), %r15
leaq 0x20(%rsp), %r12
leaq 0x8(%rsp), %r13
movl %eax, 0x8(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x2240
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x297e
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x423a
movl 0x8(%rsp), %eax
incl %eax
jmp 0x294b
movq 0x5b663(%rip), %rdi     # 0x5dfe8
leaq 0x56731(%rip), %rsi     # 0x590bd
movl $0x5, %edx
callq 0x2200
movq 0x28(%rsp), %rsi
subq 0x20(%rsp), %rsi
sarq $0x6, %rsi
movq 0x5b63d(%rip), %rdi     # 0x5dfe8
callq 0x2110
leaq 0x5670c(%rip), %rsi     # 0x590c3
movl $0x8, %edx
movq %rax, %rdi
callq 0x2200
leaq 0x100(%rsp), %rax
movq %rax, -0x10(%rax)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq 0xf0(%rsp), %rdi
callq 0x4500
leaq 0x566de(%rip), %rsi     # 0x590cc
leaq 0xf0(%rsp), %rdi
callq 0x2340
leaq 0xe0(%rsp), %rax
movq %rax, -0x10(%rax)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq 0xd0(%rsp), %rdi
callq 0x4500
leaq 0x566ad(%rip), %rsi     # 0x590d2
leaq 0xd0(%rsp), %rdi
callq 0x2340
leaq 0x198(%rsp), %rax
movq %rax, -0x10(%rax)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq 0x188(%rsp), %rdi
callq 0x4500
leaq 0x5667d(%rip), %rsi     # 0x590d9
leaq 0x188(%rsp), %rdi
callq 0x2340
movq 0xf0(%rsp), %rdi
leaq 0x56668(%rip), %r15     # 0x590e0
movq %r15, %rsi
callq 0x2210
movq %rax, %rbx
movq 0xd0(%rsp), %rdi
movq %r15, %rsi
callq 0x2210
movq %rax, %r14
movq 0x188(%rsp), %rdi
movq %r15, %rsi
callq 0x2210
movq %rax, %r15
xorl %eax, %eax
leaq 0xb0(%rsp), %rdi
movq %rax, 0x10(%rdi)
vxorpd %xmm0, %xmm0, %xmm0
vmovapd %xmm0, (%rdi)
movq %rax, 0x180(%rsp)
vmovapd %xmm0, 0x170(%rsp)
movq %rax, 0x120(%rsp)
vmovapd %xmm0, 0x110(%rsp)
movq %rbx, %rsi
callq 0x25d1
movq 0xb0(%rsp), %rax
movl (%rax), %r12d
leaq 0x110(%rsp), %rdi
movq %r15, %rsi
callq 0x25d1
movq 0x118(%rsp), %r15
subq 0x110(%rsp), %r15
sarq $0x2, %r15
cmpq %r12, %r15
jne 0x3996
movq 0x5b4c0(%rip), %rdi     # 0x5dfe8
leaq 0x565eb(%rip), %rsi     # 0x5911a
movl $0xb, %edx
callq 0x2200
movq 0x5b4a8(%rip), %rdi     # 0x5dfe8
movq %r15, %rsi
callq 0x2110
movq %rax, %r15
leaq 0x565d4(%rip), %rsi     # 0x59126
movl $0xb, %edx
movq %rax, %rdi
callq 0x2200
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %r15, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2120
movq 0x110(%rsp), %rax
movq 0x118(%rsp), %rcx
vxorpd %xmm0, %xmm0, %xmm0
vmovsd %xmm0, 0x1d0(%rsp)
vxorpd %xmm1, %xmm1, %xmm1
cmpq %rcx, %rax
je 0x2bbd
movl (%rax), %edx
vcvtsi2sd %rdx, %xmm2, %xmm0
vaddsd %xmm0, %xmm1, %xmm1
addq $0x4, %rax
jmp 0x2ba7
vmovsd %xmm1, 0x60(%rsp)
movl $0x0, 0x8(%rsp)
movq %rbx, %rdi
callq 0x22b0
testl %eax, %eax
jne 0x2e1a
vcvtsi2sd %r12, %xmm2, %xmm0
vmovsd 0x60(%rsp), %xmm1
vdivsd %xmm0, %xmm1, %xmm0
vcvtsd2ss %xmm0, %xmm0, %xmm0
vmovss %xmm0, 0x14c(%rsp)
vcvtsi2ss %r12, %xmm2, %xmm0
vmovss %xmm0, 0x128(%rsp)
vxorps %xmm0, %xmm0, %xmm0
vmovsd %xmm0, 0x1d0(%rsp)
leaq 0xb0(%rsp), %r15
leaq 0x170(%rsp), %r12
movq %r15, %rdi
movq %rbx, %rsi
callq 0x25d1
movq %r12, %rdi
movq %r14, %rsi
callq 0x25d1
movq 0xb0(%rsp), %rcx
movq 0xb8(%rsp), %rdx
movq %rdx, %rax
subq %rcx, %rax
movq 0x178(%rsp), %rsi
subq 0x170(%rsp), %rsi
cmpq %rsi, %rax
jne 0x3979
cmpq %rcx, %rdx
je 0x2dff
sarq $0x2, %rax
js 0x2c7d
vcvtsi2ss %rax, %xmm8, %xmm0
jmp 0x2c92
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
vcvtsi2ss %rax, %xmm8, %xmm0
vaddss %xmm0, %xmm0, %xmm0
vmovss 0x128(%rsp), %xmm1
vsubss %xmm0, %xmm1, %xmm1
vmovss 0x5635d(%rip), %xmm2    # 0x59004
vaddss %xmm2, %xmm1, %xmm1
vaddss %xmm2, %xmm0, %xmm0
vdivss %xmm0, %xmm1, %xmm0
callq 0x2330
vmaxss 0x56348(%rip), %xmm0, %xmm0 # 0x59008
vmulss 0x56344(%rip), %xmm0, %xmm0 # 0x5900c
vmovss %xmm0, 0x60(%rsp)
movl 0x8(%rsp), %eax
movq 0x20(%rsp), %rcx
shlq $0x6, %rax
leaq (%rcx,%rax), %rdi
addq $0x28, %rdi
movq 0xb8(%rsp), %rsi
subq 0xb0(%rsp), %rsi
sarq $0x2, %rsi
callq 0x4266
movq 0xb0(%rsp), %rax
movq 0xb8(%rsp), %rcx
movl 0x8(%rsp), %r9d
subq %rax, %rcx
je 0x2de5
sarq $0x2, %rcx
movq 0x170(%rsp), %rdx
movq 0x110(%rsp), %rsi
movq 0x20(%rsp), %rdi
cmpq $0x1, %rcx
adcq $0x0, %rcx
shlq $0x6, %r9
movq 0x28(%rdi,%r9), %r11
xorl %r8d, %r8d
xorl %r10d, %r10d
vmovsd 0x1d0(%rsp), %xmm2
vmovss 0x14c(%rsp), %xmm3
vmovss 0x562ac(%rip), %xmm4    # 0x59010
vmovss 0x562a8(%rip), %xmm5    # 0x59014
vmovss 0x562a4(%rip), %xmm6    # 0x59018
vmovss 0x60(%rsp), %xmm7
movl (%rax,%r10,4), %r9d
movl (%rdx,%r10,4), %r13d
vcvtsi2ss %r13, %xmm8, %xmm0
movl (%rsi,%r9,4), %r13d
vcvtsi2ss %r13, %xmm8, %xmm1
vdivss %xmm3, %xmm1, %xmm1
vmulss %xmm4, %xmm1, %xmm1
vaddss %xmm5, %xmm1, %xmm1
vmulss %xmm6, %xmm1, %xmm1
vaddss %xmm0, %xmm1, %xmm1
vdivss %xmm1, %xmm0, %xmm0
vmulss %xmm0, %xmm7, %xmm0
vcvtss2sd %xmm0, %xmm0, %xmm0
vmaxsd %xmm2, %xmm0, %xmm2
movl %r9d, (%r11,%r8)
movl 0x8(%rsp), %r9d
movq %r9, %r11
shlq $0x6, %r11
movq 0x28(%rdi,%r11), %r11
vmovsd %xmm0, 0x8(%r11,%r8)
incq %r10
addq $0x18, %r8
cmpq %r10, %rcx
jne 0x2d7a
vmovsd %xmm2, 0x1d0(%rsp)
incl %r9d
movl %r9d, 0x8(%rsp)
movq %rbx, %rdi
callq 0x22b0
testl %eax, %eax
je 0x2c22
jmp 0x2e1a
movl 0x8(%rsp), %eax
movq 0x28(%rsp), %rcx
subq 0x20(%rsp), %rcx
sarq $0x6, %rcx
cmpq %rax, %rcx
jne 0x39bc
movq 0x5b1c7(%rip), %rdi     # 0x5dfe8
leaq 0x5634b(%rip), %rsi     # 0x59173
movl $0x10, %edx
callq 0x2200
movq 0x5b1af(%rip), %rdi     # 0x5dfe8
vmovsd 0x1d0(%rsp), %xmm0
callq 0x22c0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2120
movq 0x5b170(%rip), %rdi     # 0x5dfe8
leaq 0x56305(%rip), %rsi     # 0x59184
movl $0x19, %edx
callq 0x2200
movq 0x5b158(%rip), %rdi     # 0x5dfe8
vmovsd 0x68(%rsp), %xmm0
callq 0x22c0
movq %rax, %rbx
leaq 0x562f9(%rip), %rsi     # 0x5919e
movl $0x1, %edx
movq %rax, %rdi
callq 0x2200
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2120
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
subq %rax, %rcx
je 0x2f61
sarq $0x6, %rcx
cmpq $0x1, %rcx
adcq $0x0, %rcx
xorl %edx, %edx
vmovsd 0x68(%rsp), %xmm1
vmovsd 0x1d0(%rsp), %xmm2
movq %rdx, %rdi
shlq $0x6, %rdi
movq 0x28(%rax,%rdi), %rsi
movq 0x30(%rax,%rdi), %rdi
subq %rsi, %rdi
je 0x2f59
sarq $0x3, %rdi
movabsq $-0x5555555555555555, %r8 # imm = 0xAAAAAAAAAAAAAAAB
imulq %r8, %rdi
cmpq $0x1, %rdi
adcq $0x0, %rdi
addq $0x10, %rsi
vmulsd -0x8(%rsi), %xmm1, %xmm0
vdivsd %xmm2, %xmm0, %xmm0
vroundsd $0xa, %xmm0, %xmm0, %xmm0
vcvttsd2si %xmm0, %r8
movl %r8d, (%rsi)
addq $0x18, %rsi
decq %rdi
jne 0x2f39
incq %rdx
cmpq %rcx, %rdx
jne 0x2f05
leaq 0x56238(%rip), %rsi     # 0x591a0
leaq 0x5f0(%rsp), %rdi
movl $0x10, %edx
callq 0x21f0
leaq 0x56231(%rip), %rsi     # 0x591b2
leaq 0x3f0(%rsp), %rdi
movl $0x4, %edx
callq 0x21f0
leaq 0x56224(%rip), %rsi     # 0x591be
leaq 0x1f0(%rsp), %rdi
movl $0x4, %edx
callq 0x21f0
xorl %eax, %eax
movq %rax, 0x1a8(%rsp)
movq %rax, 0x78(%rsp)
leaq 0x1f0(%rsp), %rdi
leaq 0xf(%rsp), %rsi
movl $0x1, %edx
callq 0x2260
leaq 0x1f0(%rsp), %rdi
callq 0x2180
movq %rax, 0x78(%rsp)
movq 0x20(%rsp), %rax
cmpq %rax, 0x28(%rsp)
je 0x376f
vcvttsd2si 0x68(%rsp), %rcx
movl %ecx, %ecx
movq %rcx, 0x128(%rsp)
leaq 0x5f0(%rsp), %rbx
leaq 0x130(%rsp), %r15
leaq 0x1f0(%rsp), %rbp
vbroadcastsd 0x5605a(%rip), %ymm0 # 0x59080
vmovupd %ymm0, 0x1d0(%rsp)
leaq 0x10(%rsp), %r13
xorl %r14d, %r14d
movq %r14, 0x68(%rsp)
shlq $0x6, %r14
leaq 0x1c0(%rsp), %rcx
movq %rcx, 0x1b0(%rsp)
movq (%rax,%r14), %rsi
movq 0x8(%rax,%r14), %rdx
addq %rsi, %rdx
leaq 0x1b0(%rsp), %rdi
vzeroupper
callq 0x4500
movl $0x8, %edx
leaq 0x3f0(%rsp), %rdi
leaq 0x1a8(%rsp), %rsi
callq 0x2260
movl $0x8, %edx
leaq 0x3f0(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x2260
movq 0x1b0(%rsp), %rsi
movq 0x1b8(%rsp), %rdx
movq %rbx, %rdi
callq 0x2200
movb $0x0, 0x130(%rsp)
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x2200
movq %rbx, %rdi
callq 0x2180
movq %rax, 0x1a8(%rsp)
movq %r15, %rdi
movq 0x128(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0x42aa
movq 0x20(%rsp), %rax
movq 0x28(%rax,%r14), %rcx
cmpq %rcx, 0x30(%rax,%r14)
je 0x317a
movl $0x10, %ebx
xorl %r15d, %r15d
movl -0x10(%rcx,%rbx), %edx
movl (%rcx,%rbx), %ecx
decl %ecx
movq 0x130(%rsp), %rsi
leaq (%rcx,%rcx,2), %rcx
leaq (%rsi,%rcx,8), %rdi
incl %edx
movl %edx, 0x40(%rsp)
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x3140
movl %edx, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x314f
leaq 0x40(%rsp), %rdx
callq 0x4db6
movq 0x20(%rsp), %rax
incq %r15
movq 0x28(%rax,%r14), %rcx
movq 0x30(%rax,%r14), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rsi # imm = 0xAAAAAAAAAAAAAAAB
imulq %rsi, %rdx
addq $0x18, %rbx
cmpq %rdx, %r15
jb 0x310b
vxorpd %xmm0, %xmm0, %xmm0
vmovapd %xmm0, 0x40(%rsp)
xorl %eax, %eax
movq %rax, 0x50(%rsp)
movq %rax, 0x88(%rsp)
movq 0x130(%rsp), %rax
movq 0x138(%rsp), %rcx
movl $0x0, %edx
cmpq %rax, %rcx
movq 0x80(%rsp), %rbx
je 0x34f2
xorl %r14d, %r14d
leaq 0x40(%rsp), %r15
leaq (%r14,%r14,2), %r12
movq 0x8(%rax,%r12,8), %rsi
subq (%rax,%r12,8), %rsi
je 0x34b2
sarq %rsi
addq $0x400, %rsi            # imm = 0x400
leaq 0x90(%rsp), %rdi
movq %r13, %rdx
callq 0x432c
movq 0x98(%rsp), %rax
subq 0x90(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x130(%rsp), %rax
movq (%rax,%r12,8), %rdx
movq 0x8(%rax,%r12,8), %rax
movq %rax, %rcx
subq %rdx, %rcx
sarq $0x2, %rcx
cmpq $0x4, %rcx
ja 0x32f0
cmpq %rdx, %rax
vpmovsxbq 0x55de8(%rip), %ymm4 # 0x5901c
vpmovsxbq 0x55de3(%rip), %ymm5 # 0x59020
vmovdqu 0x1d0(%rsp), %ymm6
vpcmpeqd %ymm7, %ymm7, %ymm7
vpmovsxbw 0x55e35(%rip), %xmm8 # 0x59088
je 0x391e
cmpq $0x1, %rcx
je 0x33ea
leal 0x6(%rcx), %eax
andl $0x8, %eax
leaq -0x2(%rcx), %rsi
vmovq %rsi, %xmm0
vpbroadcastq %xmm0, %ymm0
leaq (%rdx,%rcx,4), %rcx
addq $-0x20, %rcx
vpxor %ymm6, %ymm0, %ymm0
xorl %edx, %edx
vmovq %rdx, %xmm1
vpbroadcastq %xmm1, %ymm1
vpor %ymm4, %ymm1, %ymm2
vpor %ymm5, %ymm1, %ymm1
vpxor %ymm6, %ymm1, %ymm1
vpcmpgtq %ymm0, %ymm1, %ymm1
vpxor %ymm6, %ymm2, %ymm2
vpcmpgtq %ymm0, %ymm2, %ymm2
vpackssdw %ymm1, %ymm2, %ymm1
vpxor %ymm7, %ymm1, %ymm1
vextracti128 $0x1, %ymm1, %xmm2
vpackssdw %xmm2, %xmm1, %xmm1
vpshufb %xmm8, %xmm1, %xmm1
vpmovzxwd %xmm1, %ymm1    # ymm1 = xmm1[0],zero,xmm1[1],zero,xmm1[2],zero,xmm1[3],zero,xmm1[4],zero,xmm1[5],zero,xmm1[6],zero,xmm1[7],zero
vpslld $0x1f, %ymm1, %ymm1
vpmaskmovd -0x4(%rcx), %ymm1, %ymm2
vpmaskmovd (%rcx), %ymm1, %ymm3
vpsubd %ymm2, %ymm3, %ymm2
vpmaskmovd %ymm2, %ymm1, (%rcx)
addq $0x8, %rdx
addq $-0x20, %rcx
cmpq %rdx, %rax
jne 0x3285
jmp 0x33ea
movq %rcx, %rsi
andq $-0x4, %rsi
leaq (%rdx,%rsi,4), %rax
vxorpd %xmm0, %xmm0, %xmm0
vpmovsxbq 0x55d14(%rip), %ymm4 # 0x5901c
vpmovsxbq 0x55d0f(%rip), %ymm5 # 0x59020
vmovdqu 0x1d0(%rsp), %ymm6
vpcmpeqd %ymm7, %ymm7, %ymm7
vpsrldq $0xc, %xmm0, %xmm1      # xmm1 = xmm0[12,13,14,15],zero,zero,zero,zero,zero,zero,zero,zero,zero,zero,zero,zero
vmovdqa (%rdx), %xmm0
vpslldq $0x4, %xmm0, %xmm2      # xmm2 = zero,zero,zero,zero,xmm0[0,1,2,3,4,5,6,7,8,9,10,11]
vpaddd %xmm2, %xmm1, %xmm1
vpsubd %xmm1, %xmm0, %xmm1
vmovdqa %xmm1, (%rdx)
addq $0x10, %rdx
cmpq %rax, %rdx
jb 0x331e
cmpq %rcx, %rsi
je 0x33ea
movq %rsi, %rdx
orq $0x1, %rdx
cmpq %rdx, %rcx
cmovaq %rcx, %rdx
subq %rsi, %rdx
leaq 0x7(%rdx), %rcx
andq $-0x8, %rcx
decq %rdx
vinserti128 $0x1, %xmm0, %ymm0, %ymm1
vmovq %rdx, %xmm0
vpbroadcastq %xmm0, %ymm0
vpxor %ymm6, %ymm0, %ymm0
xorl %edx, %edx
vmovq %rdx, %xmm2
vpbroadcastq %xmm2, %ymm2
vpor %ymm4, %ymm2, %ymm3
vpor %ymm5, %ymm2, %ymm2
vpxor %ymm6, %ymm2, %ymm2
vpcmpgtq %ymm0, %ymm2, %ymm2
vpxor %ymm6, %ymm3, %ymm3
vpcmpgtq %ymm0, %ymm3, %ymm3
vpackssdw %ymm2, %ymm3, %ymm2
vpxor %ymm7, %ymm2, %ymm2
vextracti128 $0x1, %ymm2, %xmm3
vpackssdw %xmm3, %xmm2, %xmm2
vpshufd $0xd8, %xmm2, %xmm2     # xmm2 = xmm2[0,2,1,3]
vpmovzxwd %xmm2, %ymm2    # ymm2 = xmm2[0],zero,xmm2[1],zero,xmm2[2],zero,xmm2[3],zero,xmm2[4],zero,xmm2[5],zero,xmm2[6],zero,xmm2[7],zero
vpslld $0x1f, %ymm2, %ymm2
vpmaskmovd (%rax,%rdx,4), %ymm2, %ymm3
vperm2i128 $0x21, %ymm3, %ymm1, %ymm1 # ymm1 = ymm1[2,3],ymm3[0,1]
vpalignr $0xc, %ymm1, %ymm3, %ymm1 # ymm1 = ymm1[12,13,14,15],ymm3[0,1,2,3,4,5,6,7,8,9,10,11],ymm1[28,29,30,31],ymm3[16,17,18,19,20,21,22,23,24,25,26,27]
vpsubd %ymm1, %ymm3, %ymm1
vpmaskmovd %ymm1, %ymm2, (%rax,%rdx,4)
addq $0x8, %rdx
vmovdqa %ymm3, %ymm1
cmpq %rdx, %rcx
jne 0x337c
movq 0x130(%rsp), %rax
movq (%rax,%r12,8), %rsi
movq 0x8(%rax,%r12,8), %rdx
subq %rsi, %rdx
sarq $0x2, %rdx
movq 0x90(%rsp), %rcx
movq (%rbx), %rax
movq %rbx, %rdi
movq %r13, %r8
vzeroupper
callq *0x10(%rax)
movq 0x10(%rsp), %rsi
addq $0xf, %rsi
andq $-0x10, %rsi
movq %rsi, 0x70(%rsp)
shrq $0x2, %rsi
leaq 0x90(%rsp), %r13
movq %r13, %rdi
callq 0x4208
leal 0x1(%r14), %eax
movw %ax, 0xc(%rsp)
movq 0x130(%rsp), %rax
movq 0x8(%rax,%r12,8), %rcx
subq (%rax,%r12,8), %rcx
shrq $0x2, %rcx
movl %ecx, 0x1c(%rsp)
movq %r15, %rdi
leaq 0xc(%rsp), %rsi
leaq 0x1c(%rsp), %rdx
leaq 0x10(%rsp), %rcx
leaq 0x70(%rsp), %r8
movq %r13, %r9
callq 0x43bc
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x349d
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq 0x130(%rsp), %rax
movq 0x138(%rsp), %rcx
leaq 0x10(%rsp), %r13
incq %r14
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rsi # imm = 0xAAAAAAAAAAAAAAAB
imulq %rsi, %rdx
cmpq %rdx, %r14
jb 0x31c1
movq 0x48(%rsp), %rdx
subq 0x40(%rsp), %rdx
sarq $0x4, %rdx
movabsq $-0x5555555555555555, %rax # imm = 0xAAAAAAAAAAAAAAAB
imulq %rax, %rdx
movq %rdx, 0x88(%rsp)
movl $0x8, %edx
leaq 0x3f0(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x2260
movq 0x88(%rsp), %rax
movq 0x78(%rsp), %rdx
leaq (%rdx,%rax,8), %rcx
movq %rcx, 0x10(%rsp)
testq %rax, %rax
je 0x356d
xorl %ebx, %ebx
movl $0x8, %edx
movq %rbp, %rdi
movq %r13, %rsi
callq 0x2260
addq $0x16, 0x10(%rsp)
incq %rbx
movq 0x88(%rsp), %r12
cmpq %r12, %rbx
jb 0x3531
movq 0x78(%rsp), %rdx
leaq (%r12,%r12), %rax
shlq $0x5, %r12
subq %rax, %r12
addq $0x16, %r12
jmp 0x3573
movl $0x16, %r12d
movq %rdx, 0x60(%rsp)
leaq (%r12,%rdx), %r14
addq $0xf, %r14
andq $-0x10, %r14
movq %r14, 0x70(%rsp)
movq 0x40(%rsp), %rax
cmpq %rax, 0x48(%rsp)
je 0x364e
movl $0x10, %ebx
movq %r14, %rcx
xorl %r13d, %r13d
movzwl -0x10(%rax,%rbx), %edx
movw %dx, 0xc(%rsp)
movl -0xc(%rax,%rbx), %edx
movl %edx, 0x1c(%rsp)
movq (%rax,%rbx), %r15
addq -0x8(%rax,%rbx), %rcx
movq %rcx, 0x90(%rsp)
movl $0x2, %edx
movq %rbp, %rdi
leaq 0xc(%rsp), %rsi
callq 0x2260
movl $0x8, %edx
movq %rbp, %rdi
leaq 0x70(%rsp), %rsi
callq 0x2260
movl $0x8, %edx
movq %rbp, %rdi
leaq 0x90(%rsp), %rsi
callq 0x2260
movl $0x4, %edx
movq %rbp, %rdi
leaq 0x1c(%rsp), %rsi
callq 0x2260
addq 0x70(%rsp), %r15
movq %r15, 0x70(%rsp)
incq %r13
movq 0x40(%rsp), %rax
movq 0x48(%rsp), %rdx
subq %rax, %rdx
sarq $0x4, %rdx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
addq $0x30, %rbx
movq %r15, %rcx
cmpq %rdx, %r13
jb 0x35a4
vxorpd %xmm0, %xmm0, %xmm0
vmovapd %xmm0, 0x90(%rsp)
movq $0x0, 0x9e(%rsp)
movl $0x16, %edx
movq %rbp, %rdi
leaq 0x90(%rsp), %rbx
movq %rbx, %rsi
callq 0x2260
leaq 0x130(%rsp), %r15
addq 0x60(%rsp), %r12
cmpq %r12, %r14
je 0x36ac
subq %r12, %r14
cmpq $0x17, %r14
jae 0x3953
movq %rbp, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x2260
movq 0x40(%rsp), %rax
cmpq %rax, 0x48(%rsp)
leaq 0x10(%rsp), %r13
je 0x3701
movl $0x18, %ebx
xorl %r14d, %r14d
movq -0x8(%rax,%rbx), %rdx
movq (%rax,%rbx), %rsi
movq %rbp, %rdi
callq 0x2260
incq %r14
movq 0x40(%rsp), %rax
movq 0x48(%rsp), %rcx
subq %rax, %rcx
sarq $0x4, %rcx
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rdx, %rcx
addq $0x30, %rbx
cmpq %rcx, %r14
jb 0x36c5
movq %rbp, %rdi
callq 0x2180
movq %rax, 0x78(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x441e
movq %r15, %rdi
callq 0x4472
movq 0x1b0(%rsp), %rdi
leaq 0x1c0(%rsp), %rax
cmpq %rax, %rdi
leaq 0x5f0(%rsp), %rbx
je 0x374d
movq 0x1c0(%rsp), %rsi
incq %rsi
callq 0x21e0
movq 0x68(%rsp), %r14
incq %r14
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
subq %rax, %rcx
sarq $0x6, %rcx
cmpq %rcx, %r14
jb 0x3037
movq 0x5a84a(%rip), %rax     # 0x5dfc0
movq (%rax), %rbx
movq 0x18(%rax), %r14
leaq 0x1f8(%rsp), %rdi
movq %rbx, -0x8(%rdi)
movq -0x18(%rbx), %rax
movq %r14, 0x1f0(%rsp,%rax)
callq 0x2170
movl $0xf8, %r15d
leaq 0x1f0(%rsp), %rdi
addq %r15, %rdi
callq 0x2080
leaq 0x3f8(%rsp), %rdi
movq %rbx, -0x8(%rdi)
movq -0x18(%rbx), %rax
movq %r14, 0x3f0(%rsp,%rax)
callq 0x2170
leaq 0x3f0(%rsp), %rdi
addq %r15, %rdi
callq 0x2080
leaq 0x5f8(%rsp), %rdi
movq %rbx, -0x8(%rdi)
movq -0x18(%rbx), %rax
movq %r14, 0x5f0(%rsp,%rax)
callq 0x2170
leaq 0x5f0(%rsp), %rdi
addq %r15, %rdi
callq 0x2080
movq 0x110(%rsp), %rdi
testq %rdi, %rdi
je 0x3827
movq 0x120(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq 0x170(%rsp), %rdi
testq %rdi, %rdi
je 0x3844
movq 0x180(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x3861
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq 0x188(%rsp), %rdi
leaq 0x198(%rsp), %rax
cmpq %rax, %rdi
je 0x3886
movq 0x198(%rsp), %rsi
incq %rsi
callq 0x21e0
movq 0xd0(%rsp), %rdi
leaq 0xe0(%rsp), %rax
cmpq %rax, %rdi
je 0x38ab
movq 0xe0(%rsp), %rsi
incq %rsi
callq 0x21e0
movq 0xf0(%rsp), %rdi
leaq 0x100(%rsp), %rax
cmpq %rax, %rdi
je 0x38d0
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x21e0
movq 0x150(%rsp), %rdi
leaq 0x160(%rsp), %rax
cmpq %rax, %rdi
je 0x38f5
movq 0x160(%rsp), %rsi
incq %rsi
callq 0x21e0
leaq 0x7f0(%rsp), %rdi
callq 0x2060
leaq 0x20(%rsp), %rdi
callq 0x44c6
addq $0x9f8, %rsp            # imm = 0x9F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x10, %edi
vzeroupper
callq 0x20e0
movq %rax, %r14
leaq 0x55a23(%rip), %rsi     # 0x59358
movq %rax, %rdi
callq 0x20a0
movq 0x5a6ac(%rip), %rsi     # 0x5dff0
movq 0x5a67d(%rip), %rdx     # 0x5dfc8
movq %r14, %rdi
callq 0x2300
movq 0x5a68e(%rip), %rdi     # 0x5dfe8
leaq 0x5586c(%rip), %rsi     # 0x591cd
movl $0x2b, %edx
callq 0x2200
movq 0x5a676(%rip), %rdi     # 0x5dfe8
callq 0x20d0
jmp 0x398c
movq 0x5a668(%rip), %rdi     # 0x5dfe8
leaq 0x557ab(%rip), %rsi     # 0x59132
callq 0x21c0
movl $0x1, %edi
callq 0x2220
movq 0x5a64b(%rip), %rdi     # 0x5dfe8
leaq 0x5573f(%rip), %rsi     # 0x590e3
movl $0x36, %edx
callq 0x2200
movq 0x5a633(%rip), %rdi     # 0x5dfe8
callq 0x20d0
jmp 0x398c
movq 0x5a625(%rip), %rdi     # 0x5dfe8
leaq 0x55780(%rip), %rsi     # 0x5914a
movl $0x28, %edx
callq 0x2200
movq 0x5a60d(%rip), %rdi     # 0x5dfe8
callq 0x20d0
jmp 0x398c
movq %rax, %rbx
movl $0x18, %esi
movq 0x80(%rsp), %rdi
callq 0x21e0
jmp 0x3c44
jmp 0x3a81
jmp 0x3a81
movq %rax, %rbx
jmp 0x3b28
movq %rax, %rbx
jmp 0x3b35
jmp 0x3a75
jmp 0x3a75
movq %rax, %rbx
jmp 0x3b99
movq %rax, %rbx
jmp 0x3bbe
movq %rax, %rbx
movq 0xd0(%rsp), %rdi
leaq 0xe0(%rsp), %rax
jmp 0x3bce
movq %rax, %rbx
jmp 0x3be3
movq %rax, %rbx
movq 0xf0(%rsp), %rdi
leaq 0x100(%rsp), %rax
jmp 0x3bf3
jmp 0x3a9e
movq %rax, %rbx
jmp 0x3c3a
jmp 0x3a9e
jmp 0x3a75
jmp 0x3a75
jmp 0x3adc
jmp 0x3a75
jmp 0x3a75
movq %rax, %rbx
jmp 0x3b42
jmp 0x3adc
jmp 0x3adc
movq %rax, %rbx
jmp 0x3b1b
jmp 0x3aa6
jmp 0x3aa6
jmp 0x3adc
jmp 0x3ab4
movq %rax, %rbx
movq %r14, %rdi
callq 0x2140
jmp 0x3ab7
movq %rax, %rbx
jmp 0x3c08
movq %rax, %rbx
jmp 0x3af6
movq %rax, %rbx
jmp 0x3ae9
jmp 0x3ab4
jmp 0x3ab4
movq %rax, %rbx
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x3adf
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
jmp 0x3adf
jmp 0x3adc
jmp 0x3adc
jmp 0x3adc
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x441e
leaq 0x130(%rsp), %rdi
callq 0x4472
movq 0x1b0(%rsp), %rdi
leaq 0x1c0(%rsp), %rax
cmpq %rax, %rdi
je 0x3b1b
movq 0x1c0(%rsp), %rsi
incq %rsi
callq 0x21e0
leaq 0x1f0(%rsp), %rdi
callq 0x2280
leaq 0x3f0(%rsp), %rdi
callq 0x2280
leaq 0x5f0(%rsp), %rdi
callq 0x2280
movq 0x110(%rsp), %rdi
testq %rdi, %rdi
je 0x3b5f
movq 0x120(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq 0x170(%rsp), %rdi
testq %rdi, %rdi
je 0x3b7c
movq 0x180(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x3b99
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq 0x188(%rsp), %rdi
leaq 0x198(%rsp), %rax
cmpq %rax, %rdi
je 0x3bbe
movq 0x198(%rsp), %rsi
incq %rsi
callq 0x21e0
leaq 0xe0(%rsp), %rax
movq 0xd0(%rsp), %rdi
cmpq %rax, %rdi
je 0x3be3
movq 0xe0(%rsp), %rsi
incq %rsi
callq 0x21e0
leaq 0x100(%rsp), %rax
movq 0xf0(%rsp), %rdi
cmpq %rax, %rdi
je 0x3c08
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x21e0
leaq 0x160(%rsp), %rax
movq 0x150(%rsp), %rdi
cmpq %rax, %rdi
je 0x3c2d
movq 0x160(%rsp), %rsi
incq %rsi
callq 0x21e0
leaq 0x7f0(%rsp), %rdi
callq 0x2060
leaq 0x20(%rsp), %rdi
callq 0x44c6
movq %rbx, %rdi
callq 0x2310