// File: /workspace/github_cpp_makefile_star2_O3/JMMackenzie[P]binary_to_jass/bin_to_jass_disk.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<std::string> m_terms;

    // 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_terms.emplace_back(term);
	     ++term_id;
	 }

    std::cerr << "Read " << m_terms.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) {
        break;
      }

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

      // Resize our PL

      // 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);
      }
      ++term_id;
    }

    // Re-read files
    rewind(df);
    rewind(ff);
 
    // Make sure we forward to the right segment
    read_uint32_list(doc_list,df);

    // 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;

    // Tell JASS that we're using QMX-D1: we need to compute our own deltas
    postings.write((char *)&compression, sizeof(uint8_t));
    pl_offset = postings.tellp();


    // Reiterate, quantize, build, and write JASS data
    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) {
        break;
      }

      float w_qt = ranker.query_term_weight(doc_n);

      // prepare segments vector
      uint32_t max_quant = uint32_t(quant_level);
      std::vector<std::vector<uint32_t>> pl_segments(max_quant);
      std::vector<compressed_segment_t> jass_pl;


      // Score the PL and add it to our structure
      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]));
        uint32_t quant_score = std::ceil(quant_level * impact / global_maximum);
        pl_segments[quant_score-1].push_back(doc_id + 1); 
      }

      std::string current_term = m_terms[term_id];

      // 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
     

      // 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();

    ++term_id;
    } 
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa18, %rsp            # imm = 0xA18
vmovsd %xmm0, 0x180(%rsp)
movq %rsi, %r14
movq %rdi, %rbx
movb %dl, 0xf(%rsp)
cmpl $0x47, %edx
je 0x28a0
cmpl $0x67, %edx
je 0x2897
cmpl $0x71, %edx
jne 0x28bd
movl $0x18, %edi
callq 0x21e0
movq %rax, %rbp
movq %rax, %rdi
callq 0x4d84
jmp 0x28bf
leaq 0x5b452(%rip), %r15     # 0x5dcf0
jmp 0x28a7
leaq 0x5b3f1(%rip), %r15     # 0x5dc98
addq $0x10, %r15
movl $0x8, %edi
callq 0x21e0
movq %rax, %rbp
movq %r15, (%rax)
jmp 0x28bf
xorl %ebp, %ebp
vxorpd %xmm0, %xmm0, %xmm0
vmovapd %xmm0, 0x190(%rsp)
movq $0x0, 0x1a0(%rsp)
leaq 0x810(%rsp), %rdi
movq %r14, %rsi
movl $0x8, %edx
callq 0x22d0
leaq 0x1e0(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x810(%rsp), %r14
leaq 0x1d0(%rsp), %r15
leaq 0x190(%rsp), %r12
movq %r14, %rdi
movq %r15, %rsi
callq 0x2250
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x2942
movq %r12, %rdi
movq %r15, %rsi
callq 0x410e
jmp 0x291c
movq 0x5b69f(%rip), %rdi     # 0x5dfe8
leaq 0x5676d(%rip), %rsi     # 0x590bd
movl $0x5, %edx
callq 0x2210
movq 0x198(%rsp), %rsi
subq 0x190(%rsp), %rsi
sarq $0x5, %rsi
movq 0x5b673(%rip), %rdi     # 0x5dfe8
callq 0x2120
leaq 0x56742(%rip), %rsi     # 0x590c3
movl $0x8, %edx
movq %rax, %rdi
callq 0x2210
leaq 0x108(%rsp), %rax
movq %rax, -0x10(%rax)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq 0xf8(%rsp), %rdi
callq 0x43d2
leaq 0x56714(%rip), %rsi     # 0x590cc
leaq 0xf8(%rsp), %rdi
callq 0x2340
leaq 0xe8(%rsp), %rax
movq %rax, -0x10(%rax)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq 0xd8(%rsp), %rdi
callq 0x43d2
leaq 0x566e3(%rip), %rsi     # 0x590d2
leaq 0xd8(%rsp), %rdi
callq 0x2340
leaq 0x170(%rsp), %rax
movq %rax, -0x10(%rax)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq 0x160(%rsp), %rdi
callq 0x43d2
leaq 0x566b3(%rip), %rsi     # 0x590d9
leaq 0x160(%rsp), %rdi
callq 0x2340
movq 0xf8(%rsp), %rdi
leaq 0x5669e(%rip), %rbx     # 0x590e0
movq %rbx, %rsi
callq 0x2220
movq %rax, %r12
movq 0xd8(%rsp), %rdi
movq %rbx, %rsi
callq 0x2220
movq %rax, %r15
movq 0x160(%rsp), %rdi
movq %rbx, %rsi
callq 0x2220
movq %rax, %rbx
xorl %eax, %eax
leaq 0x20(%rsp), %rdi
movq %rax, 0x10(%rdi)
vxorpd %xmm0, %xmm0, %xmm0
vmovapd %xmm0, (%rdi)
movq %rax, 0xb0(%rsp)
vmovapd %xmm0, 0xa0(%rsp)
movq %rax, 0xd0(%rsp)
vmovapd %xmm0, 0xc0(%rsp)
movq %r12, %rsi
callq 0x25b1
movq 0x20(%rsp), %rax
movl (%rax), %r14d
leaq 0xc0(%rsp), %rdi
movq %rbx, %rsi
callq 0x25b1
movq 0xc8(%rsp), %rbx
subq 0xc0(%rsp), %rbx
sarq $0x2, %rbx
cmpq %r14, %rbx
jne 0x389b
movq 0x5b4fc(%rip), %rdi     # 0x5dfe8
leaq 0x56627(%rip), %rsi     # 0x5911a
movl $0xb, %edx
callq 0x2210
movq 0x5b4e4(%rip), %rdi     # 0x5dfe8
movq %rbx, %rsi
callq 0x2120
movq %rax, %rbx
leaq 0x56610(%rip), %rsi     # 0x59126
movl $0xb, %edx
movq %rax, %rdi
callq 0x2210
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21c0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
movq 0xc0(%rsp), %rax
movq 0xc8(%rsp), %rcx
vxorpd %xmm0, %xmm0, %xmm0
vmovsd %xmm0, 0x40(%rsp)
cmpq %rcx, %rax
je 0x2b7a
movl (%rax), %edx
vcvtsi2sd %rdx, %xmm2, %xmm1
vaddsd %xmm1, %xmm0, %xmm0
addq $0x4, %rax
jmp 0x2b64
vcvtsi2sd %r14, %xmm2, %xmm1
vdivsd %xmm1, %xmm0, %xmm0
vcvtsd2ss %xmm0, %xmm0, %xmm0
vmovss %xmm0, 0x5c(%rsp)
vcvtsi2ss %r14, %xmm2, %xmm0
vmovss %xmm0, 0x58(%rsp)
movq %r12, %rdi
callq 0x22c0
testl %eax, %eax
jne 0x2d11
vxorps %xmm0, %xmm0, %xmm0
vmovsd %xmm0, 0x40(%rsp)
leaq 0x20(%rsp), %rbx
leaq 0xa0(%rsp), %r14
movq %rbx, %rdi
movq %r12, %rsi
callq 0x25b1
movq %r14, %rdi
movq %r15, %rsi
callq 0x25b1
movq 0x20(%rsp), %rcx
movq 0x28(%rsp), %rdx
movq %rdx, %rax
subq %rcx, %rax
movq 0xa8(%rsp), %rsi
subq 0xa0(%rsp), %rsi
cmpq %rsi, %rax
jne 0x3843
cmpq %rcx, %rdx
je 0x2d11
sarq $0x2, %rax
js 0x2c14
vcvtsi2ss %rax, %xmm8, %xmm0
jmp 0x2c29
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
vcvtsi2ss %rax, %xmm8, %xmm0
vaddss %xmm0, %xmm0, %xmm0
vmovss 0x58(%rsp), %xmm1
vsubss %xmm0, %xmm1, %xmm1
vmovss 0x563c9(%rip), %xmm2    # 0x59004
vaddss %xmm2, %xmm1, %xmm1
vaddss %xmm2, %xmm0, %xmm0
vdivss %xmm0, %xmm1, %xmm0
callq 0x2330
vmaxss 0x563b4(%rip), %xmm0, %xmm0 # 0x59008
vmulss 0x563b0(%rip), %xmm0, %xmm0 # 0x5900c
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
subq %rax, %rcx
je 0x2cf5
sarq $0x2, %rcx
movq 0xa0(%rsp), %rdx
movq 0xc0(%rsp), %rsi
cmpq $0x1, %rcx
adcq $0x0, %rcx
xorl %edi, %edi
vmovsd 0x40(%rsp), %xmm3
vmovss 0x5c(%rsp), %xmm4
vmovss 0x5636f(%rip), %xmm5    # 0x59010
vmovss 0x5636b(%rip), %xmm6    # 0x59014
vmovss 0x56367(%rip), %xmm7    # 0x59018
movl (%rax,%rdi,4), %r8d
movl (%rsi,%r8,4), %r8d
vcvtsi2ss %r8, %xmm8, %xmm1
vdivss %xmm4, %xmm1, %xmm1
vmulss %xmm5, %xmm1, %xmm1
movl (%rdx,%rdi,4), %r8d
vaddss %xmm6, %xmm1, %xmm1
vcvtsi2ss %r8, %xmm8, %xmm2
vmulss %xmm7, %xmm1, %xmm1
vaddss %xmm2, %xmm1, %xmm1
vdivss %xmm1, %xmm2, %xmm1
vmulss %xmm1, %xmm0, %xmm1
vcvtss2sd %xmm1, %xmm1, %xmm1
vmaxsd %xmm3, %xmm1, %xmm3
incq %rdi
cmpq %rdi, %rcx
jne 0x2cb1
jmp 0x2cfb
vmovsd 0x40(%rsp), %xmm3
vmovsd %xmm3, 0x40(%rsp)
movq %r12, %rdi
callq 0x22c0
testl %eax, %eax
je 0x2bbf
movq %r12, %rdi
callq 0x2060
movq %r15, %rdi
callq 0x2060
leaq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0x25b1
leaq 0x56415(%rip), %rsi     # 0x5914a
leaq 0x610(%rsp), %rdi
movl $0x10, %edx
callq 0x2200
leaq 0x5640e(%rip), %rsi     # 0x5915c
leaq 0x410(%rsp), %rdi
movl $0x4, %edx
callq 0x2200
leaq 0x56401(%rip), %rsi     # 0x59168
leaq 0x210(%rsp), %rdi
movl $0x4, %edx
callq 0x2200
xorl %eax, %eax
movq %rax, 0x188(%rsp)
movq %rax, 0x50(%rsp)
leaq 0x210(%rsp), %rdi
leaq 0xf(%rsp), %rsi
movl $0x1, %edx
callq 0x2270
leaq 0x210(%rsp), %rdi
callq 0x2190
movq %rax, 0x50(%rsp)
movq %r12, %rdi
callq 0x22c0
testl %eax, %eax
jne 0x3665
vcvttsd2si 0x180(%rsp), %rax
movl %eax, %eax
movq %rax, 0x1c8(%rsp)
movq $0x0, 0x118(%rsp)
leaq 0x128(%rsp), %rbx
leaq 0x210(%rsp), %r13
vbroadcastsd 0x56286(%rip), %ymm0 # 0x59080
vmovups %ymm0, 0x1f0(%rsp)
movq %rbp, 0x1b0(%rsp)
movq %r12, 0x1c0(%rsp)
movq %r15, 0x1b8(%rsp)
leaq 0x20(%rsp), %rdi
movq %r12, %rsi
vzeroupper
callq 0x25b1
leaq 0xa0(%rsp), %rdi
movq %r15, %rsi
callq 0x25b1
movq 0x20(%rsp), %rcx
movq 0x28(%rsp), %rdx
movq %rdx, %rax
subq %rcx, %rax
movq 0xa8(%rsp), %rsi
subq 0xa0(%rsp), %rsi
cmpq %rsi, %rax
jne 0x3858
cmpq %rcx, %rdx
je 0x3665
sarq $0x2, %rax
js 0x2e7a
vcvtsi2ss %rax, %xmm8, %xmm0
jmp 0x2e8f
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
vcvtsi2ss %rax, %xmm8, %xmm0
vaddss %xmm0, %xmm0, %xmm0
leaq 0x140(%rsp), %r14
leaq 0x410(%rsp), %r12
vmovss 0x58(%rsp), %xmm1
vsubss %xmm0, %xmm1, %xmm1
vmovss 0x56153(%rip), %xmm2    # 0x59004
vaddss %xmm2, %xmm1, %xmm1
vaddss %xmm2, %xmm0, %xmm0
vdivss %xmm0, %xmm1, %xmm0
callq 0x2330
vmaxss 0x5613e(%rip), %xmm0, %xmm0 # 0x59008
vmulss 0x5613a(%rip), %xmm0, %xmm0 # 0x5900c
vmovss %xmm0, 0x120(%rsp)
movq %rbx, %rdi
movq 0x1c8(%rsp), %rsi
movq %r14, %rdx
callq 0x414c
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x80(%rsp)
movq $0x0, 0x90(%rsp)
movq 0x20(%rsp), %rax
cmpq %rax, 0x28(%rsp)
je 0x2fdf
xorl %ebx, %ebx
movl (%rax,%rbx,4), %ecx
movq 0xa0(%rsp), %rdx
movl (%rdx,%rbx,4), %edx
vcvtsi2ss %rdx, %xmm4, %xmm0
movq 0xc0(%rsp), %rdx
movl (%rdx,%rcx,4), %edx
vcvtsi2ss %rdx, %xmm4, %xmm1
vdivss 0x5c(%rsp), %xmm1, %xmm1
vmulss 0x560c6(%rip), %xmm1, %xmm1 # 0x59010
vaddss 0x560c2(%rip), %xmm1, %xmm1 # 0x59014
vmulss 0x560be(%rip), %xmm1, %xmm1 # 0x59018
vaddss %xmm0, %xmm1, %xmm1
vdivss %xmm1, %xmm0, %xmm0
vmulss 0x120(%rsp), %xmm0, %xmm0
vcvtss2sd %xmm0, %xmm0, %xmm0
vmulsd 0x180(%rsp), %xmm0, %xmm0
vdivsd 0x40(%rsp), %xmm0, %xmm0
vroundsd $0xa, %xmm0, %xmm0, %xmm0
vcvttsd2si %xmm0, %rdx
decl %edx
movq 0x128(%rsp), %rsi
leaq (%rdx,%rdx,2), %rdx
leaq (%rsi,%rdx,8), %rdi
incl %ecx
movl %ecx, 0x140(%rsp)
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x2fba
movl %ecx, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x2fc7
movq %r14, %rdx
callq 0x4948
movq 0x20(%rsp), %rax
incq %rbx
movq 0x28(%rsp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %rcx, %rbx
jb 0x2f19
movl 0x118(%rsp), %eax
movq 0x190(%rsp), %rcx
shlq $0x5, %rax
leaq 0x150(%rsp), %rdx
movq %rdx, 0x140(%rsp)
movq (%rcx,%rax), %rsi
movq 0x8(%rcx,%rax), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x43d2
movl $0x8, %edx
movq %r12, %rdi
leaq 0x188(%rsp), %rsi
callq 0x2270
leaq 0x610(%rsp), %rbx
movl $0x8, %edx
movq %r12, %rdi
leaq 0x50(%rsp), %rsi
callq 0x2270
movq 0x140(%rsp), %rsi
movq 0x148(%rsp), %rdx
movq %rbx, %rdi
callq 0x2210
movb $0x0, 0x60(%rsp)
movl $0x1, %edx
movq %rax, %rdi
leaq 0x60(%rsp), %rsi
callq 0x2210
movq %rbx, %rdi
callq 0x2190
movq %rax, 0x188(%rsp)
movq $0x0, 0x98(%rsp)
movq 0x128(%rsp), %rax
movq 0x130(%rsp), %rcx
cmpq %rax, %rcx
je 0x33ac
xorl %r14d, %r14d
leaq (%r14,%r14,2), %r15
movq 0x8(%rax,%r15,8), %rsi
subq (%rax,%r15,8), %rsi
je 0x3388
sarq %rsi
addq $0x400, %rsi            # imm = 0x400
leaq 0x60(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x41ce
movq 0x68(%rsp), %rax
subq 0x60(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x128(%rsp), %rax
movq (%rax,%r15,8), %rdx
movq 0x8(%rax,%r15,8), %rax
movq %rax, %rcx
subq %rdx, %rcx
sarq $0x2, %rcx
cmpq $0x4, %rcx
ja 0x31cf
cmpq %rdx, %rax
je 0x3811
cmpq $0x1, %rcx
vpmovsxbq 0x55efa(%rip), %ymm4 # 0x5901c
vpmovsxbq 0x55ef5(%rip), %ymm5 # 0x59020
vmovdqu 0x1f0(%rsp), %ymm6
vpcmpeqd %ymm7, %ymm7, %ymm7
je 0x32c9
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 0x55ef1(%rip), %xmm1, %xmm1 # 0x59090
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 0x3160
jmp 0x32c9
movq %rcx, %rsi
andq $-0x4, %rsi
leaq (%rdx,%rsi,4), %rax
vxorps %xmm0, %xmm0, %xmm0
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 0x31de
cmpq %rcx, %rsi
vpmovsxbq 0x55e0f(%rip), %ymm4 # 0x5901c
vpmovsxbq 0x55e0a(%rip), %ymm5 # 0x59020
vmovdqu 0x1f0(%rsp), %ymm6
vpcmpeqd %ymm7, %ymm7, %ymm7
je 0x32c9
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 0x325b
movq 0x128(%rsp), %rax
movq (%rax,%r15,8), %rsi
movq 0x8(%rax,%r15,8), %rdx
subq %rsi, %rdx
sarq $0x2, %rdx
movq 0x60(%rsp), %rcx
movq (%rbp), %rax
movq %rbp, %rdi
leaq 0x10(%rsp), %r8
vzeroupper
callq *0x10(%rax)
movq 0x10(%rsp), %rsi
addq $0xf, %rsi
andq $-0x10, %rsi
movq %rsi, 0x48(%rsp)
shrq $0x2, %rsi
leaq 0x60(%rsp), %rbx
movq %rbx, %rdi
callq 0x40dc
leal 0x1(%r14), %eax
movw %ax, 0xc(%rsp)
movq 0x128(%rsp), %rax
movq 0x8(%rax,%r15,8), %rcx
subq (%rax,%r15,8), %rcx
shrq $0x2, %rcx
movl %ecx, 0x1c(%rsp)
leaq 0x80(%rsp), %rdi
leaq 0xc(%rsp), %rsi
leaq 0x1c(%rsp), %rdx
leaq 0x10(%rsp), %rcx
leaq 0x48(%rsp), %r8
movq %rbx, %r9
callq 0x425e
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x3378
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x21f0
movq 0x128(%rsp), %rax
movq 0x130(%rsp), %rcx
incq %r14
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rsi # imm = 0xAAAAAAAAAAAAAAAB
imulq %rsi, %rdx
cmpq %rdx, %r14
jb 0x30ac
movq 0x88(%rsp), %rax
subq 0x80(%rsp), %rax
sarq $0x4, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rax
movq %rax, 0x98(%rsp)
movl $0x8, %edx
movq %r12, %rdi
leaq 0x98(%rsp), %rsi
callq 0x2270
movq 0x98(%rsp), %rax
movq 0x50(%rsp), %rdx
leaq (%rdx,%rax,8), %rcx
movq %rcx, 0x10(%rsp)
testq %rax, %rax
je 0x3446
xorl %ebx, %ebx
movl $0x8, %edx
movq %r13, %rdi
leaq 0x10(%rsp), %rsi
callq 0x2270
addq $0x16, 0x10(%rsp)
incq %rbx
movq 0x98(%rsp), %r15
cmpq %r15, %rbx
jb 0x3408
movq 0x50(%rsp), %rdx
leaq (%r15,%r15), %rax
shlq $0x5, %r15
subq %rax, %r15
addq $0x16, %r15
jmp 0x344c
movl $0x16, %r15d
movq %rdx, 0x120(%rsp)
leaq (%r15,%rdx), %r14
addq $0xf, %r14
andq $-0x10, %r14
movq %r14, 0x48(%rsp)
movq 0x80(%rsp), %rax
cmpq %rax, 0x88(%rsp)
je 0x3530
movl $0x10, %ebx
movq %r14, %rcx
xorl %r12d, %r12d
movzwl -0x10(%rax,%rbx), %edx
movw %dx, 0xc(%rsp)
movl -0xc(%rax,%rbx), %edx
movl %edx, 0x1c(%rsp)
movq (%rax,%rbx), %rbp
addq -0x8(%rax,%rbx), %rcx
movq %rcx, 0x60(%rsp)
movl $0x2, %edx
movq %r13, %rdi
leaq 0xc(%rsp), %rsi
callq 0x2270
movl $0x8, %edx
movq %r13, %rdi
leaq 0x48(%rsp), %rsi
callq 0x2270
movl $0x8, %edx
movq %r13, %rdi
leaq 0x60(%rsp), %rsi
callq 0x2270
movl $0x4, %edx
movq %r13, %rdi
leaq 0x1c(%rsp), %rsi
callq 0x2270
addq 0x48(%rsp), %rbp
movq %rbp, 0x48(%rsp)
incq %r12
movq 0x80(%rsp), %rax
movq 0x88(%rsp), %rdx
subq %rax, %rdx
sarq $0x4, %rdx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
addq $0x30, %rbx
movq %rbp, %rcx
cmpq %rdx, %r12
jb 0x3486
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x60(%rsp)
movq $0x0, 0x6e(%rsp)
movl $0x16, %edx
movq %r13, %rdi
leaq 0x60(%rsp), %rbx
movq %rbx, %rsi
callq 0x2270
movq 0x1c0(%rsp), %r12
addq 0x120(%rsp), %r15
cmpq %r15, %r14
je 0x3588
subq %r15, %r14
cmpq $0x17, %r14
jae 0x3875
movq %r13, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x2270
movq 0x80(%rsp), %rax
cmpq %rax, 0x88(%rsp)
movq 0x1b8(%rsp), %r15
je 0x35ec
movl $0x18, %ebx
xorl %r14d, %r14d
movq -0x8(%rax,%rbx), %rdx
movq (%rax,%rbx), %rsi
movq %r13, %rdi
callq 0x2270
incq %r14
movq 0x80(%rsp), %rax
movq 0x88(%rsp), %rcx
subq %rax, %rcx
sarq $0x4, %rcx
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rdx, %rcx
addq $0x30, %rbx
cmpq %rcx, %r14
jb 0x35aa
movq %r13, %rdi
callq 0x2190
movq %rax, 0x50(%rsp)
movq 0x140(%rsp), %rdi
leaq 0x150(%rsp), %rax
cmpq %rax, %rdi
movq 0x1b0(%rsp), %rbp
leaq 0x128(%rsp), %rbx
je 0x362e
movq 0x150(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0x118(%rsp), %rax
incl %eax
movq %rax, 0x118(%rsp)
leaq 0x80(%rsp), %rdi
callq 0x42c0
movq %rbx, %rdi
callq 0x4314
movq %r12, %rdi
callq 0x22c0
testl %eax, %eax
je 0x2e1b
movq 0x5a954(%rip), %rax     # 0x5dfc0
movq (%rax), %rbx
movq 0x18(%rax), %r14
leaq 0x218(%rsp), %rdi
movq %rbx, -0x8(%rdi)
movq -0x18(%rbx), %rax
movq %r14, 0x210(%rsp,%rax)
callq 0x2180
movl $0xf8, %r15d
leaq 0x210(%rsp), %rdi
addq %r15, %rdi
callq 0x2090
leaq 0x418(%rsp), %rdi
movq %rbx, -0x8(%rdi)
movq -0x18(%rbx), %rax
movq %r14, 0x410(%rsp,%rax)
callq 0x2180
leaq 0x410(%rsp), %rdi
addq %r15, %rdi
callq 0x2090
leaq 0x618(%rsp), %rdi
movq %rbx, -0x8(%rdi)
movq -0x18(%rbx), %rax
movq %r14, 0x610(%rsp,%rax)
callq 0x2180
leaq 0x610(%rsp), %rdi
addq %r15, %rdi
callq 0x2090
movq 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0x371d
movq 0xd0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21f0
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x373a
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21f0
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x3751
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x21f0
movq 0x160(%rsp), %rdi
leaq 0x170(%rsp), %rax
cmpq %rax, %rdi
je 0x3776
movq 0x170(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0xd8(%rsp), %rdi
leaq 0xe8(%rsp), %rax
cmpq %rax, %rdi
je 0x379b
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0xf8(%rsp), %rdi
leaq 0x108(%rsp), %rax
cmpq %rax, %rdi
je 0x37c0
movq 0x108(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0x1d0(%rsp), %rdi
leaq 0x1e0(%rsp), %rax
cmpq %rax, %rdi
je 0x37e5
movq 0x1e0(%rsp), %rsi
incq %rsi
callq 0x21f0
leaq 0x810(%rsp), %rdi
callq 0x2070
leaq 0x190(%rsp), %rdi
callq 0x4368
addq $0xa18, %rsp            # imm = 0xA18
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x10, %edi
callq 0x20f0
movq %rax, %r14
leaq 0x55ac9(%rip), %rsi     # 0x592ee
movq %rax, %rdi
callq 0x20b0
movq 0x5a7bc(%rip), %rsi     # 0x5dff0
movq 0x5a78d(%rip), %rdx     # 0x5dfc8
movq %r14, %rdi
callq 0x2300
movq 0x5a79e(%rip), %rdi     # 0x5dfe8
leaq 0x558e1(%rip), %rsi     # 0x59132
callq 0x21d0
jmp 0x386b
movq 0x5a789(%rip), %rdi     # 0x5dfe8
leaq 0x558cc(%rip), %rsi     # 0x59132
callq 0x21d0
movl $0x1, %edi
callq 0x2230
movq 0x5a76c(%rip), %rdi     # 0x5dfe8
leaq 0x558f4(%rip), %rsi     # 0x59177
movl $0x2b, %edx
callq 0x2210
movq 0x5a754(%rip), %rdi     # 0x5dfe8
callq 0x20e0
jmp 0x386b
movq 0x5a746(%rip), %rdi     # 0x5dfe8
leaq 0x5583a(%rip), %rsi     # 0x590e3
movl $0x36, %edx
callq 0x2210
movq 0x5a72e(%rip), %rdi     # 0x5dfe8
callq 0x20e0
jmp 0x386b
movq %rax, %rbx
movl $0x18, %esi
movq %rbp, %rdi
callq 0x21f0
jmp 0x3b17
jmp 0x395b
jmp 0x395b
movq %rax, %rbx
jmp 0x39fe
movq %rax, %rbx
jmp 0x3a0b
jmp 0x3963
jmp 0x3963
jmp 0x3963
movq %rax, %rbx
jmp 0x3a69
movq %rax, %rbx
jmp 0x3a8e
movq %rax, %rbx
movq 0xd8(%rsp), %rdi
leaq 0xe8(%rsp), %rax
jmp 0x3a9e
movq %rax, %rbx
jmp 0x3ab3
movq %rax, %rbx
movq 0xf8(%rsp), %rdi
leaq 0x108(%rsp), %rax
jmp 0x3ac3
jmp 0x3981
movq %rax, %rbx
jmp 0x3b0a
jmp 0x39af
jmp 0x3981
jmp 0x395b
jmp 0x3963
jmp 0x39af
jmp 0x39af
jmp 0x39af
jmp 0x397c
jmp 0x395b
jmp 0x3963
jmp 0x39af
movq %rax, %rbx
jmp 0x39f1
movq %rax, %rbx
jmp 0x3a18
jmp 0x398d
movq %rax, %rbx
movq %r14, %rdi
callq 0x2150
jmp 0x3990
jmp 0x39af
movq %rax, %rbx
jmp 0x39d7
movq %rax, %rbx
jmp 0x3ad8
jmp 0x398d
jmp 0x398d
movq %rax, %rbx
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x39b2
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x21f0
jmp 0x39b2
jmp 0x39af
jmp 0x39af
jmp 0x39af
movq %rax, %rbx
movq 0x140(%rsp), %rdi
leaq 0x150(%rsp), %rax
cmpq %rax, %rdi
je 0x39d7
movq 0x150(%rsp), %rsi
incq %rsi
callq 0x21f0
leaq 0x80(%rsp), %rdi
callq 0x42c0
leaq 0x128(%rsp), %rdi
callq 0x4314
leaq 0x210(%rsp), %rdi
callq 0x2290
leaq 0x410(%rsp), %rdi
callq 0x2290
leaq 0x610(%rsp), %rdi
callq 0x2290
movq 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0x3a35
movq 0xd0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21f0
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x3a52
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21f0
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x3a69
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x21f0
movq 0x160(%rsp), %rdi
leaq 0x170(%rsp), %rax
cmpq %rax, %rdi
je 0x3a8e
movq 0x170(%rsp), %rsi
incq %rsi
callq 0x21f0
leaq 0xe8(%rsp), %rax
movq 0xd8(%rsp), %rdi
cmpq %rax, %rdi
je 0x3ab3
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x21f0
leaq 0x108(%rsp), %rax
movq 0xf8(%rsp), %rdi
cmpq %rax, %rdi
je 0x3ad8
movq 0x108(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0x1d0(%rsp), %rdi
leaq 0x1e0(%rsp), %rax
cmpq %rax, %rdi
je 0x3afd
movq 0x1e0(%rsp), %rsi
incq %rsi
callq 0x21f0
leaq 0x810(%rsp), %rdi
callq 0x2070
leaq 0x190(%rsp), %rdi
callq 0x4368
movq %rbx, %rdi
callq 0x2310