// File: /workspace/github_cpp_makefile_star2_O1/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 $0xa08, %rsp            # imm = 0xA08
vmovsd %xmm0, 0x60(%rsp)
movq %rsi, %r14
movq %rdi, %rbx
movb %dl, 0xf(%rsp)
cmpl $0x47, %edx
je 0x28d7
cmpl $0x67, %edx
je 0x28ce
cmpl $0x71, %edx
jne 0x28f4
movl $0x18, %edi
callq 0x21d0
movq %rax, %r15
movq %rax, %rdi
callq 0x51e8
jmp 0x28f7
leaq 0x5b41b(%rip), %r15     # 0x5dcf0
jmp 0x28de
leaq 0x5b3ba(%rip), %r15     # 0x5dc98
addq $0x10, %r15
movl $0x8, %edi
callq 0x21d0
movq %r15, (%rax)
movq %rax, %r15
jmp 0x28f7
xorl %r15d, %r15d
movq %r15, 0x1d0(%rsp)
vxorpd %xmm0, %xmm0, %xmm0
vmovapd %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
leaq 0x800(%rsp), %rdi
movq %r14, %rsi
movl $0x8, %edx
callq 0x22d0
leaq 0x180(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
xorl %eax, %eax
leaq 0x800(%rsp), %r14
leaq 0x170(%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 0x298d
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x4222
movl 0x8(%rsp), %eax
incl %eax
jmp 0x295a
movq 0x5b654(%rip), %rdi     # 0x5dfe8
leaq 0x56722(%rip), %rsi     # 0x590bd
movl $0x5, %edx
callq 0x2200
movq 0x28(%rsp), %rsi
subq 0x20(%rsp), %rsi
sarq $0x6, %rsi
movq 0x5b62e(%rip), %rdi     # 0x5dfe8
callq 0x2110
leaq 0x566fd(%rip), %rsi     # 0x590c3
movl $0x8, %edx
movq %rax, %rdi
callq 0x2200
leaq 0xf0(%rsp), %rax
movq %rax, -0x10(%rax)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq 0xe0(%rsp), %rdi
callq 0x44e0
leaq 0x566cf(%rip), %rsi     # 0x590cc
leaq 0xe0(%rsp), %rdi
callq 0x2340
leaq 0xd0(%rsp), %rax
movq %rax, -0x10(%rax)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq 0xc0(%rsp), %rdi
callq 0x44e0
leaq 0x5669e(%rip), %rsi     # 0x590d2
leaq 0xc0(%rsp), %rdi
callq 0x2340
leaq 0x1c0(%rsp), %rax
movq %rax, -0x10(%rax)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq 0x1b0(%rsp), %rdi
callq 0x44e0
leaq 0x5666e(%rip), %rsi     # 0x590d9
leaq 0x1b0(%rsp), %rdi
callq 0x2340
movq 0xe0(%rsp), %rdi
leaq 0x56659(%rip), %r15     # 0x590e0
movq %r15, %rsi
callq 0x2210
movq %rax, %rbx
movq 0xc0(%rsp), %rdi
movq %r15, %rsi
callq 0x2210
movq %rax, %r14
movq 0x1b0(%rsp), %rdi
movq %r15, %rsi
callq 0x2210
movq %rax, %r12
xorl %eax, %eax
leaq 0xa0(%rsp), %rdi
movq %rax, 0x10(%rdi)
vxorpd %xmm0, %xmm0, %xmm0
vmovapd %xmm0, (%rdi)
movq %rax, 0x1a0(%rsp)
vmovapd %xmm0, 0x190(%rsp)
movq %rax, 0x110(%rsp)
vmovapd %xmm0, 0x100(%rsp)
movq %rbx, %rsi
callq 0x25d1
movq 0xa0(%rsp), %rax
movl (%rax), %r15d
leaq 0x100(%rsp), %rdi
movq %r12, %rsi
callq 0x25d1
movq 0x108(%rsp), %rax
subq 0x100(%rsp), %rax
sarq $0x2, %rax
cmpq %r15, %rax
jne 0x399d
movq 0x5b4b1(%rip), %rdi     # 0x5dfe8
leaq 0x565dc(%rip), %rsi     # 0x5911a
movl $0xb, %edx
callq 0x2200
movq 0x5b499(%rip), %rdi     # 0x5dfe8
movq %r15, %rsi
callq 0x2110
movq %rax, %r12
leaq 0x565c5(%rip), %rsi     # 0x59126
movl $0xb, %edx
movq %rax, %rdi
callq 0x2200
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %r12, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2120
movq 0x100(%rsp), %rax
movq 0x108(%rsp), %rcx
vxorpd %xmm0, %xmm0, %xmm0
vmovsd %xmm0, 0x1e0(%rsp)
cmpq %rcx, %rax
je 0x2bc9
movl (%rax), %edx
vcvtsi2sd %rdx, %xmm2, %xmm1
vaddsd %xmm1, %xmm0, %xmm0
addq $0x4, %rax
jmp 0x2bb3
vcvtsi2sd %r15, %xmm2, %xmm1
vdivsd %xmm1, %xmm0, %xmm0
vcvtsd2ss %xmm0, %xmm0, %xmm0
vmovss %xmm0, 0x14c(%rsp)
vcvtsi2ss %r15, %xmm2, %xmm0
vmovss %xmm0, 0x120(%rsp)
movl $0x0, 0x8(%rsp)
leaq 0xa0(%rsp), %r15
leaq 0x190(%rsp), %r12
movq %rbx, %rdi
callq 0x22b0
testl %eax, %eax
jne 0x2e0f
movq %r15, %rdi
movq %rbx, %rsi
callq 0x25d1
movq %r12, %rdi
movq %r14, %rsi
callq 0x25d1
movq 0xa0(%rsp), %r13
movq 0xa8(%rsp), %rbp
movq %rbp, %rax
subq %r13, %rax
movq 0x198(%rsp), %rcx
subq 0x190(%rsp), %rcx
cmpq %rcx, %rax
jne 0x3934
cmpq %r13, %rbp
je 0x2c6c
sarq $0x2, %rax
js 0x2c8c
vcvtsi2ss %rax, %xmm8, %xmm0
jmp 0x2ca1
movl 0x8(%rsp), %eax
movq 0x28(%rsp), %rcx
subq 0x20(%rsp), %rcx
sarq $0x6, %rcx
cmpq %rax, %rcx
je 0x2e06
jmp 0x396f
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
vcvtsi2ss %rax, %xmm8, %xmm0
vaddss %xmm0, %xmm0, %xmm0
vmovss 0x120(%rsp), %xmm1
vsubss %xmm0, %xmm1, %xmm1
vmovss 0x5634e(%rip), %xmm2    # 0x59004
vaddss %xmm2, %xmm1, %xmm1
vaddss %xmm2, %xmm0, %xmm0
vdivss %xmm0, %xmm1, %xmm0
callq 0x2330
vmaxss 0x56339(%rip), %xmm0, %xmm0 # 0x59008
vmulss 0x56335(%rip), %xmm0, %xmm0 # 0x5900c
vmovss %xmm0, 0x128(%rsp)
movl 0x8(%rsp), %eax
movq 0x20(%rsp), %rcx
shlq $0x6, %rax
leaq (%rcx,%rax), %rdi
addq $0x28, %rdi
movq 0xa8(%rsp), %rsi
subq 0xa0(%rsp), %rsi
sarq $0x2, %rsi
callq 0x424e
movq 0xa0(%rsp), %rax
movq 0xa8(%rsp), %rcx
subq %rax, %rcx
je 0x2df0
sarq $0x2, %rcx
movq 0x190(%rsp), %rdx
movq 0x100(%rsp), %rsi
movq 0x20(%rsp), %rdi
cmpq $0x1, %rcx
adcq $0x0, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
vmovsd 0x1e0(%rsp), %xmm2
vmovss 0x14c(%rsp), %xmm3
vmovss 0x562a8(%rip), %xmm4    # 0x59010
vmovss 0x562a4(%rip), %xmm5    # 0x59014
vmovss 0x562a0(%rip), %xmm6    # 0x59018
vmovss 0x128(%rsp), %xmm7
movl (%rax,%r9,4), %r10d
movl (%rdx,%r9,4), %r11d
vcvtsi2ss %r11, %xmm8, %xmm0
movl (%rsi,%r10,4), %r11d
vcvtsi2ss %r11, %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 0x8(%rsp), %r11d
shlq $0x6, %r11
movq 0x28(%rdi,%r11), %r11
movl %r10d, (%r11,%r8)
movl 0x8(%rsp), %r10d
shlq $0x6, %r10
movq 0x28(%rdi,%r10), %r10
vmovsd %xmm0, 0x8(%r10,%r8)
incq %r9
addq $0x18, %r8
cmpq %r9, %rcx
jne 0x2d81
jmp 0x2df9
vmovsd 0x1e0(%rsp), %xmm2
vmovsd %xmm2, 0x1e0(%rsp)
incl 0x8(%rsp)
cmpq %r13, %rbp
jne 0x2c05
movq 0x5b1d2(%rip), %rdi     # 0x5dfe8
leaq 0x56356(%rip), %rsi     # 0x59173
movl $0x10, %edx
callq 0x2200
movq 0x5b1ba(%rip), %rdi     # 0x5dfe8
vmovsd 0x1e0(%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 0x5b17b(%rip), %rdi     # 0x5dfe8
leaq 0x56310(%rip), %rsi     # 0x59184
movl $0x19, %edx
callq 0x2200
movq 0x5b163(%rip), %rdi     # 0x5dfe8
vmovsd 0x60(%rsp), %xmm0
callq 0x22c0
movq %rax, %rbx
leaq 0x56304(%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
movq 0x1d0(%rsp), %r15
je 0x2f5e
sarq $0x6, %rcx
cmpq $0x1, %rcx
adcq $0x0, %rcx
xorl %edx, %edx
vmovsd 0x60(%rsp), %xmm1
vmovsd 0x1e0(%rsp), %xmm2
movq %rdx, %rdi
shlq $0x6, %rdi
movq 0x28(%rax,%rdi), %rsi
movq 0x30(%rax,%rdi), %rdi
subq %rsi, %rdi
je 0x2f56
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 0x2f36
incq %rdx
cmpq %rcx, %rdx
jne 0x2f02
leaq 0x5623b(%rip), %rsi     # 0x591a0
leaq 0x600(%rsp), %rdi
movl $0x10, %edx
callq 0x21f0
leaq 0x56234(%rip), %rsi     # 0x591b2
leaq 0x400(%rsp), %rdi
movl $0x4, %edx
callq 0x21f0
leaq 0x56227(%rip), %rsi     # 0x591be
leaq 0x200(%rsp), %rdi
movl $0x4, %edx
callq 0x21f0
xorl %eax, %eax
movq %rax, 0x1d8(%rsp)
movq %rax, 0x70(%rsp)
leaq 0x200(%rsp), %rdi
leaq 0xf(%rsp), %rsi
movl $0x1, %edx
callq 0x2260
leaq 0x200(%rsp), %rdi
callq 0x2180
movq %rax, 0x70(%rsp)
movq 0x20(%rsp), %rax
cmpq %rax, 0x28(%rsp)
je 0x3750
leaq 0x160(%rsp), %r13
vcvttsd2si 0x60(%rsp), %rcx
movl %ecx, %ecx
movq %rcx, 0x120(%rsp)
leaq 0x600(%rsp), %rbx
leaq 0x130(%rsp), %r12
leaq 0x200(%rsp), %rbp
vbroadcastsd 0x56055(%rip), %ymm0 # 0x59080
vmovups %ymm0, 0x1e0(%rsp)
xorl %r14d, %r14d
movq %r14, 0x60(%rsp)
shlq $0x6, %r14
movq %r13, 0x150(%rsp)
movq (%rax,%r14), %rsi
movq 0x8(%rax,%r14), %rdx
addq %rsi, %rdx
leaq 0x150(%rsp), %rdi
vzeroupper
callq 0x44e0
movl $0x8, %edx
leaq 0x400(%rsp), %rdi
leaq 0x1d8(%rsp), %rsi
callq 0x2260
movl $0x8, %edx
leaq 0x400(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x2260
movq 0x150(%rsp), %rsi
movq 0x158(%rsp), %rdx
movq %rbx, %rdi
callq 0x2200
movb $0x0, 0x130(%rsp)
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x2200
movq %rbx, %rdi
callq 0x2180
movq %rax, 0x1d8(%rsp)
movq %r12, %rdi
movq 0x120(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0x4292
movq 0x20(%rsp), %rcx
movq 0x28(%rcx,%r14), %rax
cmpq %rax, 0x30(%rcx,%r14)
je 0x3172
movl $0x10, %ebx
xorl %r12d, %r12d
movl -0x10(%rax,%rbx), %ecx
movl (%rax,%rbx), %eax
decl %eax
movq 0x130(%rsp), %rdx
leaq (%rax,%rax,2), %rax
leaq (%rdx,%rax,8), %rdi
incl %ecx
movl %ecx, 0x40(%rsp)
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x3138
movl %ecx, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x3142
leaq 0x40(%rsp), %rdx
callq 0x4d84
incq %r12
movq 0x20(%rsp), %rcx
movq 0x28(%rcx,%r14), %rax
movq 0x30(%rcx,%r14), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rdx, %rcx
addq $0x18, %rbx
cmpq %rcx, %r12
jb 0x3103
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x40(%rsp)
xorl %eax, %eax
movq %rax, 0x50(%rsp)
movq %rax, 0x78(%rsp)
movq 0x130(%rsp), %rax
cmpq %rax, 0x138(%rsp)
je 0x34bf
xorl %r14d, %r14d
leaq (%r14,%r14,2), %r12
movq 0x8(%rax,%r12,8), %rcx
cmpq (%rax,%r12,8), %rcx
je 0x348e
leaq (%rax,%r12,8), %rax
movq 0x8(%rax), %rsi
subq (%rax), %rsi
sarq %rsi
addq $0x400, %rsi            # imm = 0x400
leaq 0x80(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x431c
movq 0x88(%rsp), %rax
subq 0x80(%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 0x32dd
cmpq %rdx, %rax
vpmovsxbq 0x55dfb(%rip), %ymm4 # 0x5901c
vpmovsxbq 0x55df6(%rip), %ymm5 # 0x59020
vmovdqu 0x1e0(%rsp), %ymm6
vpcmpeqd %ymm7, %ymm7, %ymm7
vpmovsxbw 0x55e48(%rip), %xmm8 # 0x59088
je 0x38ff
cmpq $0x1, %rcx
je 0x33d7
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 0x3272
jmp 0x33d7
movq %rcx, %rsi
andq $-0x4, %rsi
leaq (%rdx,%rsi,4), %rax
vxorps %xmm0, %xmm0, %xmm0
vpmovsxbq 0x55d27(%rip), %ymm4 # 0x5901c
vpmovsxbq 0x55d22(%rip), %ymm5 # 0x59020
vmovdqu 0x1e0(%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 0x330b
cmpq %rcx, %rsi
je 0x33d7
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 0x3369
movq 0x130(%rsp), %rax
movq (%rax,%r12,8), %rsi
movq 0x8(%rax,%r12,8), %rdx
subq %rsi, %rdx
sarq $0x2, %rdx
movq 0x80(%rsp), %rcx
movq (%r15), %rax
movq %r15, %rdi
leaq 0x10(%rsp), %r8
vzeroupper
callq *0x10(%rax)
movq 0x10(%rsp), %rsi
addq $0xf, %rsi
andq $-0x10, %rsi
movq %rsi, 0x68(%rsp)
shrq $0x2, %rsi
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
callq 0x41f0
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)
leaq 0x40(%rsp), %rdi
leaq 0xc(%rsp), %rsi
leaq 0x1c(%rsp), %rdx
leaq 0x10(%rsp), %rcx
leaq 0x68(%rsp), %r8
movq %rbx, %r9
callq 0x43a8
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x348e
movq 0x90(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
incq %r14
movq 0x130(%rsp), %rax
movq 0x138(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rdx, %rcx
cmpq %rcx, %r14
jb 0x31a1
movq 0x48(%rsp), %rax
subq 0x40(%rsp), %rax
sarq $0x4, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rax
movq %rax, 0x78(%rsp)
movl $0x8, %edx
leaq 0x400(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x2260
movq 0x78(%rsp), %r12
leaq (,%r12,8), %rax
addq 0x70(%rsp), %rax
movq %rax, 0x10(%rsp)
testq %r12, %r12
je 0x353a
xorl %ebx, %ebx
movl $0x8, %edx
movq %rbp, %rdi
leaq 0x10(%rsp), %rsi
callq 0x2260
addq $0x16, 0x10(%rsp)
incq %rbx
movq 0x78(%rsp), %r12
cmpq %r12, %rbx
jb 0x3515
movq 0x70(%rsp), %rcx
leaq (%r12,%r12), %rax
shlq $0x5, %r12
subq %rax, %r12
movq %rcx, 0x128(%rsp)
leaq (%r12,%rcx), %r14
addq $0x25, %r14
andq $-0x10, %r14
movq %r14, 0x68(%rsp)
movq 0x40(%rsp), %rax
cmpq %rax, 0x48(%rsp)
je 0x3622
movl $0x10, %ebx
xorl %r13d, %r13d
movzwl -0x10(%rax,%rbx), %ecx
movw %cx, 0xc(%rsp)
movl -0xc(%rax,%rbx), %ecx
movl %ecx, 0x1c(%rsp)
movq (%rax,%rbx), %r15
movq 0x68(%rsp), %rcx
addq -0x8(%rax,%rbx), %rcx
movq %rcx, 0x80(%rsp)
movl $0x2, %edx
movq %rbp, %rdi
leaq 0xc(%rsp), %rsi
callq 0x2260
movl $0x8, %edx
movq %rbp, %rdi
leaq 0x68(%rsp), %rsi
callq 0x2260
movl $0x8, %edx
movq %rbp, %rdi
leaq 0x80(%rsp), %rsi
callq 0x2260
movl $0x4, %edx
movq %rbp, %rdi
leaq 0x1c(%rsp), %rsi
callq 0x2260
addq %r15, 0x68(%rsp)
incq %r13
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, %r13
jb 0x357b
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x80(%rsp)
movq $0x0, 0x8e(%rsp)
movl $0x16, %edx
movq %rbp, %rdi
leaq 0x80(%rsp), %rbx
movq %rbx, %rsi
leaq 0x160(%rsp), %r13
callq 0x2260
movq 0x128(%rsp), %rax
addq %r12, %rax
addq $0x16, %rax
cmpq %rax, %r14
je 0x368a
subq %rax, %r14
cmpq $0x17, %r14
jae 0x3949
movq %rbp, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x2260
movq 0x40(%rsp), %rax
cmpq %rax, 0x48(%rsp)
leaq 0x130(%rsp), %r12
je 0x36e2
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 0x36a6
movq %rbp, %rdi
callq 0x2180
movq %rax, 0x70(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x440a
movq %r12, %rdi
callq 0x4458
movq 0x150(%rsp), %rdi
cmpq %r13, %rdi
movq 0x1d0(%rsp), %r15
leaq 0x600(%rsp), %rbx
je 0x372e
movq 0x160(%rsp), %rsi
incq %rsi
callq 0x21e0
movq 0x60(%rsp), %r14
incq %r14
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
subq %rax, %rcx
sarq $0x6, %rcx
cmpq %rcx, %r14
jb 0x3037
movq 0x5a869(%rip), %rax     # 0x5dfc0
movq (%rax), %rbx
movq 0x18(%rax), %r14
leaq 0x208(%rsp), %rdi
movq %rbx, -0x8(%rdi)
movq -0x18(%rbx), %rax
movq %r14, 0x200(%rsp,%rax)
callq 0x2170
movl $0xf8, %r15d
leaq 0x200(%rsp), %rdi
addq %r15, %rdi
callq 0x2080
leaq 0x408(%rsp), %rdi
movq %rbx, -0x8(%rdi)
movq -0x18(%rbx), %rax
movq %r14, 0x400(%rsp,%rax)
callq 0x2170
leaq 0x400(%rsp), %rdi
addq %r15, %rdi
callq 0x2080
leaq 0x608(%rsp), %rdi
movq %rbx, -0x8(%rdi)
movq -0x18(%rbx), %rax
movq %r14, 0x600(%rsp,%rax)
callq 0x2170
leaq 0x600(%rsp), %rdi
addq %r15, %rdi
callq 0x2080
movq 0x100(%rsp), %rdi
testq %rdi, %rdi
je 0x3808
movq 0x110(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq 0x190(%rsp), %rdi
testq %rdi, %rdi
je 0x3825
movq 0x1a0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x3842
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq 0x1b0(%rsp), %rdi
leaq 0x1c0(%rsp), %rax
cmpq %rax, %rdi
je 0x3867
movq 0x1c0(%rsp), %rsi
incq %rsi
callq 0x21e0
movq 0xc0(%rsp), %rdi
leaq 0xd0(%rsp), %rax
cmpq %rax, %rdi
je 0x388c
movq 0xd0(%rsp), %rsi
incq %rsi
callq 0x21e0
movq 0xe0(%rsp), %rdi
leaq 0xf0(%rsp), %rax
cmpq %rax, %rdi
je 0x38b1
movq 0xf0(%rsp), %rsi
incq %rsi
callq 0x21e0
movq 0x170(%rsp), %rdi
leaq 0x180(%rsp), %rax
cmpq %rax, %rdi
je 0x38d6
movq 0x180(%rsp), %rsi
incq %rsi
callq 0x21e0
leaq 0x800(%rsp), %rdi
callq 0x2060
leaq 0x20(%rsp), %rdi
callq 0x44a6
addq $0xa08, %rsp            # imm = 0xA08
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x10, %edi
vzeroupper
callq 0x20e0
movq %rax, %r14
leaq 0x55a42(%rip), %rsi     # 0x59358
movq %rax, %rdi
callq 0x20a0
movq 0x5a6cb(%rip), %rsi     # 0x5dff0
movq 0x5a69c(%rip), %rdx     # 0x5dfc8
movq %r14, %rdi
callq 0x2300
movq 0x5a6ad(%rip), %rdi     # 0x5dfe8
leaq 0x557f0(%rip), %rsi     # 0x59132
callq 0x21c0
jmp 0x3993
movq 0x5a698(%rip), %rdi     # 0x5dfe8
leaq 0x55876(%rip), %rsi     # 0x591cd
movl $0x2b, %edx
callq 0x2200
movq 0x5a680(%rip), %rdi     # 0x5dfe8
callq 0x20d0
jmp 0x3993
movq 0x5a672(%rip), %rdi     # 0x5dfe8
leaq 0x557cd(%rip), %rsi     # 0x5914a
movl $0x28, %edx
callq 0x2200
movq 0x5a65a(%rip), %rdi     # 0x5dfe8
callq 0x20d0
movl $0x1, %edi
callq 0x2220
movq 0x5a644(%rip), %rdi     # 0x5dfe8
leaq 0x55738(%rip), %rsi     # 0x590e3
movl $0x36, %edx
callq 0x2200
movq 0x5a62c(%rip), %rdi     # 0x5dfe8
callq 0x20d0
jmp 0x3993
movq %rax, %rbx
movl $0x18, %esi
movq %r15, %rdi
callq 0x21e0
jmp 0x3c2b
jmp 0x3a52
jmp 0x3a52
movq %rax, %rbx
jmp 0x3b0f
movq %rax, %rbx
jmp 0x3b1c
jmp 0x3a60
jmp 0x3a60
movq %rax, %rbx
jmp 0x3b80
movq %rax, %rbx
jmp 0x3ba5
movq %rax, %rbx
movq 0xc0(%rsp), %rdi
leaq 0xd0(%rsp), %rax
jmp 0x3bb5
movq %rax, %rbx
jmp 0x3bca
movq %rax, %rbx
movq 0xe0(%rsp), %rdi
leaq 0xf0(%rsp), %rax
jmp 0x3bda
jmp 0x3a82
movq %rax, %rbx
jmp 0x3c21
jmp 0x3a82
jmp 0x3ac3
jmp 0x3a60
jmp 0x3a60
jmp 0x3a60
jmp 0x3a60
jmp 0x3abc
jmp 0x3ac3
movq %rax, %rbx
jmp 0x3b02
jmp 0x3a8a
jmp 0x3a8a
jmp 0x3abc
movq %rax, %rbx
jmp 0x3b29
movq %rax, %rbx
leaq 0x160(%rsp), %r13
jmp 0x3a9b
movq %rax, %rbx
movq %r14, %rdi
callq 0x2140
jmp 0x3a9b
movq %rax, %rbx
jmp 0x3bef
movq %rax, %rbx
jmp 0x3ae5
movq %rax, %rbx
jmp 0x3ad8
jmp 0x3a98
jmp 0x3a98
movq %rax, %rbx
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x3ace
movq 0x90(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
jmp 0x3ace
jmp 0x3abc
movq %rax, %rbx
jmp 0x3ace
jmp 0x3ac3
movq %rax, %rbx
leaq 0x160(%rsp), %r13
leaq 0x40(%rsp), %rdi
callq 0x440a
leaq 0x130(%rsp), %rdi
callq 0x4458
movq 0x150(%rsp), %rdi
cmpq %r13, %rdi
je 0x3b02
movq 0x160(%rsp), %rsi
incq %rsi
callq 0x21e0
leaq 0x200(%rsp), %rdi
callq 0x2280
leaq 0x400(%rsp), %rdi
callq 0x2280
leaq 0x600(%rsp), %rdi
callq 0x2280
movq 0x100(%rsp), %rdi
testq %rdi, %rdi
je 0x3b46
movq 0x110(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq 0x190(%rsp), %rdi
testq %rdi, %rdi
je 0x3b63
movq 0x1a0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x3b80
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq 0x1b0(%rsp), %rdi
leaq 0x1c0(%rsp), %rax
cmpq %rax, %rdi
je 0x3ba5
movq 0x1c0(%rsp), %rsi
incq %rsi
callq 0x21e0
leaq 0xd0(%rsp), %rax
movq 0xc0(%rsp), %rdi
cmpq %rax, %rdi
je 0x3bca
movq 0xd0(%rsp), %rsi
incq %rsi
callq 0x21e0
leaq 0xf0(%rsp), %rax
movq 0xe0(%rsp), %rdi
cmpq %rax, %rdi
je 0x3bef
movq 0xf0(%rsp), %rsi
incq %rsi
callq 0x21e0
leaq 0x180(%rsp), %rax
movq 0x170(%rsp), %rdi
cmpq %rax, %rdi
je 0x3c14
movq 0x180(%rsp), %rsi
incq %rsi
callq 0x21e0
leaq 0x800(%rsp), %rdi
callq 0x2060
leaq 0x20(%rsp), %rdi
callq 0x44a6
movq %rbx, %rdi
callq 0x2310