// File: /workspace/github_cpp_makefile_star2_O1/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 $0xa28, %rsp            # imm = 0xA28
vmovsd %xmm0, 0x190(%rsp)
movq %rsi, %r14
movq %rdi, %rbx
movb %dl, 0xf(%rsp)
cmpl $0x47, %edx
je 0x28bf
cmpl $0x67, %edx
je 0x28b6
cmpl $0x71, %edx
jne 0x28e1
movl $0x18, %edi
callq 0x21e0
movq %rax, 0xa0(%rsp)
movq %rax, %rdi
callq 0x4e28
jmp 0x28ed
leaq 0x5b433(%rip), %r15     # 0x5dcf0
jmp 0x28c6
leaq 0x5b3d2(%rip), %r15     # 0x5dc98
addq $0x10, %r15
movl $0x8, %edi
callq 0x21e0
movq %rax, 0xa0(%rsp)
movq %r15, (%rax)
jmp 0x28ed
movq $0x0, 0xa0(%rsp)
vxorpd %xmm0, %xmm0, %xmm0
vmovapd %xmm0, 0x1a0(%rsp)
movq $0x0, 0x1b0(%rsp)
leaq 0x820(%rsp), %rdi
movq %r14, %rsi
movl $0x8, %edx
callq 0x22d0
leaq 0x1f0(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x820(%rsp), %r14
leaq 0x1e0(%rsp), %r15
leaq 0x1a0(%rsp), %r12
movq %r14, %rdi
movq %r15, %rsi
callq 0x2250
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x2970
movq %r12, %rdi
movq %r15, %rsi
callq 0x416a
jmp 0x294a
movq 0x5b671(%rip), %rdi     # 0x5dfe8
leaq 0x5673f(%rip), %rsi     # 0x590bd
movl $0x5, %edx
callq 0x2210
movq 0x1a8(%rsp), %rsi
subq 0x1a0(%rsp), %rsi
sarq $0x5, %rsi
movq 0x5b645(%rip), %rdi     # 0x5dfe8
callq 0x2120
leaq 0x56714(%rip), %rsi     # 0x590c3
movl $0x8, %edx
movq %rax, %rdi
callq 0x2210
leaq 0x138(%rsp), %rax
movq %rax, -0x10(%rax)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq 0x128(%rsp), %rdi
callq 0x4422
leaq 0x566e6(%rip), %rsi     # 0x590cc
leaq 0x128(%rsp), %rdi
callq 0x2340
leaq 0x118(%rsp), %rax
movq %rax, -0x10(%rax)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq 0x108(%rsp), %rdi
callq 0x4422
leaq 0x566b5(%rip), %rsi     # 0x590d2
leaq 0x108(%rsp), %rdi
callq 0x2340
leaq 0x180(%rsp), %rax
movq %rax, -0x10(%rax)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq 0x170(%rsp), %rdi
callq 0x4422
leaq 0x56685(%rip), %rsi     # 0x590d9
leaq 0x170(%rsp), %rdi
callq 0x2340
movq 0x128(%rsp), %rdi
leaq 0x56670(%rip), %rbx     # 0x590e0
movq %rbx, %rsi
callq 0x2220
movq %rax, %r13
movq 0x108(%rsp), %rdi
movq %rbx, %rsi
callq 0x2220
movq %rax, %rbp
movq 0x170(%rsp), %rdi
movq %rbx, %rsi
callq 0x2220
movq %rax, %r14
xorl %eax, %eax
leaq 0x20(%rsp), %rdi
movq %rax, 0x10(%rdi)
vxorpd %xmm0, %xmm0, %xmm0
vmovapd %xmm0, (%rdi)
movq %rax, 0xc0(%rsp)
vmovapd %xmm0, 0xb0(%rsp)
movq %rax, 0xe0(%rsp)
vmovapd %xmm0, 0xd0(%rsp)
movq %r13, %rsi
callq 0x25b1
movq 0x20(%rsp), %rax
movl (%rax), %ebx
leaq 0xd0(%rsp), %rdi
movq %r14, %rsi
callq 0x25b1
movq 0xd8(%rsp), %rax
subq 0xd0(%rsp), %rax
sarq $0x2, %rax
cmpq %rbx, %rax
jne 0x38d9
movq 0x5b4cf(%rip), %rdi     # 0x5dfe8
leaq 0x565fa(%rip), %rsi     # 0x5911a
movl $0xb, %edx
callq 0x2210
movq 0x5b4b7(%rip), %rdi     # 0x5dfe8
movq %rbx, %rsi
callq 0x2120
movq %rax, %r14
leaq 0x565e3(%rip), %rsi     # 0x59126
movl $0xb, %edx
movq %rax, %rdi
callq 0x2210
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21c0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
movq 0xd0(%rsp), %rax
movq 0xd8(%rsp), %rcx
vxorpd %xmm0, %xmm0, %xmm0
vmovsd %xmm0, 0x60(%rsp)
cmpq %rcx, %rax
je 0x2ba7
movl (%rax), %edx
vcvtsi2sd %rdx, %xmm2, %xmm1
vaddsd %xmm1, %xmm0, %xmm0
addq $0x4, %rax
jmp 0x2b91
vcvtsi2sd %rbx, %xmm2, %xmm1
vdivsd %xmm1, %xmm0, %xmm0
vcvtsd2ss %xmm0, %xmm0, %xmm0
vmovss %xmm0, 0x7c(%rsp)
vcvtsi2ss %rbx, %xmm2, %xmm0
vmovss %xmm0, 0x78(%rsp)
leaq 0x20(%rsp), %rbx
leaq 0xb0(%rsp), %r14
movq %r13, %rdi
callq 0x22c0
testl %eax, %eax
jne 0x2d2d
movq %rbx, %rdi
movq %r13, %rsi
callq 0x25b1
movq %r14, %rdi
movq %rbp, %rsi
callq 0x25b1
movq 0x20(%rsp), %r15
movq 0x28(%rsp), %r12
movq %r12, %rax
subq %r15, %rax
movq 0xb8(%rsp), %rcx
subq 0xb0(%rsp), %rcx
cmpq %rcx, %rax
jne 0x3881
cmpq %r15, %r12
je 0x2d18
sarq $0x2, %rax
js 0x2c37
vcvtsi2ss %rax, %xmm8, %xmm0
jmp 0x2c4c
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
vcvtsi2ss %rax, %xmm8, %xmm0
vaddss %xmm0, %xmm0, %xmm0
vmovss 0x78(%rsp), %xmm1
vsubss %xmm0, %xmm1, %xmm1
vmovss 0x563a6(%rip), %xmm2    # 0x59004
vaddss %xmm2, %xmm1, %xmm1
vaddss %xmm2, %xmm0, %xmm0
vdivss %xmm0, %xmm1, %xmm0
callq 0x2330
vmaxss 0x56391(%rip), %xmm0, %xmm0 # 0x59008
vmulss 0x5638d(%rip), %xmm0, %xmm0 # 0x5900c
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
subq %rax, %rcx
je 0x2d18
sarq $0x2, %rcx
movq 0xb0(%rsp), %rdx
movq 0xd0(%rsp), %rsi
cmpq $0x1, %rcx
adcq $0x0, %rcx
xorl %edi, %edi
vmovsd 0x60(%rsp), %xmm3
vmovss 0x7c(%rsp), %xmm4
vmovss 0x5634c(%rip), %xmm5    # 0x59010
vmovss 0x56348(%rip), %xmm6    # 0x59014
vmovss 0x56344(%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 0x2cd4
jmp 0x2d1e
vmovsd 0x60(%rsp), %xmm3
vmovsd %xmm3, 0x60(%rsp)
cmpq %r15, %r12
jne 0x2bd2
movq %r13, %rdi
callq 0x2060
movq %rbp, %rdi
callq 0x2060
leaq 0x20(%rsp), %rdi
movq %r13, %rsi
callq 0x25b1
leaq 0x563f9(%rip), %rsi     # 0x5914a
leaq 0x620(%rsp), %rdi
movl $0x10, %edx
callq 0x2200
leaq 0x563f2(%rip), %rsi     # 0x5915c
leaq 0x220(%rsp), %rdi
movl $0x4, %edx
callq 0x2200
leaq 0x563e5(%rip), %rsi     # 0x59168
leaq 0x420(%rsp), %rdi
movl $0x4, %edx
callq 0x2200
xorl %eax, %eax
movq %rax, 0x198(%rsp)
movq %rax, 0x70(%rsp)
leaq 0x420(%rsp), %rdi
leaq 0xf(%rsp), %rsi
movl $0x1, %edx
callq 0x2270
movq %rbp, 0x1c8(%rsp)
leaq 0x420(%rsp), %rdi
callq 0x2190
leaq 0x70(%rsp), %rcx
movq %rax, (%rcx)
vcvttsd2si 0x190(%rsp), %rax
movl %eax, %eax
movq %rax, 0x1c0(%rsp)
movq $0x0, 0x148(%rsp)
leaq 0xf8(%rsp), %r15
leaq 0x158(%rsp), %rbx
leaq 0x620(%rsp), %r12
leaq 0x40(%rsp), %r14
leaq 0x420(%rsp), %rbp
vbroadcastsd 0x5625a(%rip), %ymm0 # 0x59080
vmovups %ymm0, 0x200(%rsp)
movq %r13, 0x1b8(%rsp)
movq %r13, %rdi
vzeroupper
callq 0x22c0
testl %eax, %eax
jne 0x36ad
leaq 0x20(%rsp), %rdi
movq %r13, %rsi
callq 0x25b1
leaq 0xb0(%rsp), %rdi
movq 0x1c8(%rsp), %rsi
callq 0x25b1
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rsi
movq %rsi, %rax
subq %rdx, %rax
movq 0xb8(%rsp), %rcx
subq 0xb0(%rsp), %rcx
cmpq %rcx, %rax
jne 0x3896
cmpq %rdx, %rsi
je 0x36a4
sarq $0x2, %rax
movq %rdx, 0x1d8(%rsp)
movq %rsi, 0x1d0(%rsp)
js 0x2ebb
vcvtsi2ss %rax, %xmm8, %xmm0
jmp 0x2ed0
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
vcvtsi2ss %rax, %xmm8, %xmm0
vaddss %xmm0, %xmm0, %xmm0
vmovss 0x78(%rsp), %xmm1
vsubss %xmm0, %xmm1, %xmm1
vmovss 0x56122(%rip), %xmm2    # 0x59004
vaddss %xmm2, %xmm1, %xmm1
vaddss %xmm2, %xmm0, %xmm0
vdivss %xmm0, %xmm1, %xmm0
callq 0x2330
vmaxss 0x5610d(%rip), %xmm0, %xmm0 # 0x59008
vmulss 0x56109(%rip), %xmm0, %xmm0 # 0x5900c
vmovss %xmm0, 0x150(%rsp)
movq %rbx, %rdi
movq 0x1c0(%rsp), %rsi
leaq 0xe8(%rsp), %rdx
callq 0x41a8
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x80(%rsp)
movq $0x0, 0x90(%rsp)
movq 0x20(%rsp), %rax
cmpq %rax, 0x28(%rsp)
je 0x301a
xorl %ebx, %ebx
movl (%rax,%rbx,4), %eax
movq 0xb0(%rsp), %rcx
movl (%rcx,%rbx,4), %ecx
vcvtsi2ss %rcx, %xmm4, %xmm0
movq 0xd0(%rsp), %rcx
movl (%rcx,%rax,4), %ecx
vcvtsi2ss %rcx, %xmm4, %xmm1
vdivss 0x7c(%rsp), %xmm1, %xmm1
vmulss 0x56090(%rip), %xmm1, %xmm1 # 0x59010
vaddss 0x5608c(%rip), %xmm1, %xmm1 # 0x59014
vmulss 0x56088(%rip), %xmm1, %xmm1 # 0x59018
vaddss %xmm0, %xmm1, %xmm1
vdivss %xmm1, %xmm0, %xmm0
vmulss 0x150(%rsp), %xmm0, %xmm0
vcvtss2sd %xmm0, %xmm0, %xmm0
vmulsd 0x190(%rsp), %xmm0, %xmm0
vdivsd 0x60(%rsp), %xmm0, %xmm0
vroundsd $0xa, %xmm0, %xmm0, %xmm0
vcvttsd2si %xmm0, %rcx
decl %ecx
movq 0x158(%rsp), %rdx
leaq (%rcx,%rcx,2), %rcx
leaq (%rdx,%rcx,8), %rdi
incl %eax
movl %eax, 0xe8(%rsp)
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x2ff0
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x2ffd
leaq 0xe8(%rsp), %rdx
callq 0x49c4
incq %rbx
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %rcx, %rbx
jb 0x2f4f
movl 0x148(%rsp), %eax
movq 0x1a0(%rsp), %rcx
shlq $0x5, %rax
movq %r15, 0xe8(%rsp)
movq (%rcx,%rax), %rsi
movq 0x8(%rcx,%rax), %rdx
addq %rsi, %rdx
leaq 0xe8(%rsp), %rdi
callq 0x4422
movl $0x8, %edx
leaq 0x220(%rsp), %rdi
leaq 0x198(%rsp), %rsi
callq 0x2270
movl $0x8, %edx
leaq 0x220(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x2270
movq 0xe8(%rsp), %rsi
movq 0xf0(%rsp), %rdx
movq %r12, %rdi
callq 0x2210
movb $0x0, 0x40(%rsp)
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x2210
movq %r12, %rdi
callq 0x2190
movq %rax, 0x198(%rsp)
movq $0x0, 0xa8(%rsp)
movq 0x158(%rsp), %rax
cmpq %rax, 0x160(%rsp)
je 0x33ed
xorl %r14d, %r14d
leaq (%r14,%r14,2), %r13
movq 0x8(%rax,%r13,8), %rcx
cmpq (%rax,%r13,8), %rcx
je 0x33bc
leaq (%rax,%r13,8), %rax
movq 0x8(%rax), %rsi
subq (%rax), %rsi
sarq %rsi
addq $0x400, %rsi            # imm = 0x400
leaq 0x40(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x4232
movq 0x48(%rsp), %rax
subq 0x40(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x158(%rsp), %rax
movq (%rax,%r13,8), %rdx
movq 0x8(%rax,%r13,8), %rax
movq %rax, %rcx
subq %rdx, %rcx
sarq $0x2, %rcx
cmpq $0x4, %rcx
ja 0x320f
cmpq %rdx, %rax
je 0x384f
cmpq $0x1, %rcx
vpmovsxbq 0x55eba(%rip), %ymm4 # 0x5901c
vmovdqu 0x200(%rsp), %ymm5
vpmovsxbq 0x55eac(%rip), %ymm6 # 0x59020
vpcmpeqd %ymm7, %ymm7, %ymm7
je 0x3309
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 %ymm5, %ymm0, %ymm0
xorl %edx, %edx
vmovq %rdx, %xmm1
vpbroadcastq %xmm1, %ymm1
vpor %ymm4, %ymm1, %ymm2
vpor %ymm6, %ymm1, %ymm1
vpxor %ymm5, %ymm1, %ymm1
vpcmpgtq %ymm0, %ymm1, %ymm1
vpxor %ymm5, %ymm2, %ymm2
vpcmpgtq %ymm0, %ymm2, %ymm2
vpackssdw %ymm1, %ymm2, %ymm1
vpxor %ymm7, %ymm1, %ymm1
vextracti128 $0x1, %ymm1, %xmm2
vpackssdw %xmm2, %xmm1, %xmm1
vpshufb 0x55eb1(%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 0x31a0
jmp 0x3309
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 0x321e
cmpq %rcx, %rsi
vpmovsxbq 0x55dcf(%rip), %ymm4 # 0x5901c
vmovdqu 0x200(%rsp), %ymm5
vpmovsxbq 0x55dc1(%rip), %ymm6 # 0x59020
vpcmpeqd %ymm7, %ymm7, %ymm7
je 0x3309
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 %ymm5, %ymm0, %ymm0
xorl %edx, %edx
vmovq %rdx, %xmm2
vpbroadcastq %xmm2, %ymm2
vpor %ymm4, %ymm2, %ymm3
vpor %ymm6, %ymm2, %ymm2
vpxor %ymm5, %ymm2, %ymm2
vpcmpgtq %ymm0, %ymm2, %ymm2
vpxor %ymm5, %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 0x329b
movq 0x158(%rsp), %rax
movq (%rax,%r13,8), %rsi
movq 0x8(%rax,%r13,8), %rdx
subq %rsi, %rdx
sarq $0x2, %rdx
movq 0x40(%rsp), %rcx
movq 0xa0(%rsp), %rdi
movq (%rdi), %rax
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 0x40(%rsp), %rbx
movq %rbx, %rdi
callq 0x4138
leal 0x1(%r14), %eax
movw %ax, 0xc(%rsp)
movq 0x158(%rsp), %rax
movq 0x8(%rax,%r13,8), %rcx
subq (%rax,%r13,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 0x68(%rsp), %r8
movq %rbx, %r9
callq 0x42be
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x33bc
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x21f0
incq %r14
movq 0x158(%rsp), %rax
movq 0x160(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rdx, %rcx
cmpq %rcx, %r14
jb 0x30e1
movq 0x88(%rsp), %rax
subq 0x80(%rsp), %rax
sarq $0x4, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rax
movq %rax, 0xa8(%rsp)
movl $0x8, %edx
leaq 0x220(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x2270
movq 0xa8(%rsp), %r13
leaq (,%r13,8), %rax
addq 0x70(%rsp), %rax
movq %rax, 0x10(%rsp)
testq %r13, %r13
je 0x347a
xorl %ebx, %ebx
movl $0x8, %edx
movq %rbp, %rdi
leaq 0x10(%rsp), %rsi
callq 0x2270
addq $0x16, 0x10(%rsp)
incq %rbx
movq 0xa8(%rsp), %r13
cmpq %r13, %rbx
jb 0x3452
movq 0x70(%rsp), %rcx
leaq (,%r13,2), %rax
shlq $0x5, %r13
subq %rax, %r13
movq %rcx, 0x150(%rsp)
leaq (%rcx,%r13), %r14
addq $0x25, %r14
andq $-0x10, %r14
movq %r14, 0x68(%rsp)
movq 0x80(%rsp), %rax
cmpq %rax, 0x88(%rsp)
je 0x356c
movl $0x10, %ebx
xorl %r15d, %r15d
movzwl -0x10(%rax,%rbx), %ecx
movw %cx, 0xc(%rsp)
movl -0xc(%rax,%rbx), %ecx
movl %ecx, 0x1c(%rsp)
movq (%rax,%rbx), %r12
movq 0x68(%rsp), %rcx
addq -0x8(%rax,%rbx), %rcx
movq %rcx, 0x40(%rsp)
movl $0x2, %edx
movq %rbp, %rdi
leaq 0xc(%rsp), %rsi
callq 0x2270
movl $0x8, %edx
movq %rbp, %rdi
leaq 0x68(%rsp), %rsi
callq 0x2270
movl $0x8, %edx
movq %rbp, %rdi
leaq 0x40(%rsp), %rsi
callq 0x2270
movl $0x4, %edx
movq %rbp, %rdi
leaq 0x1c(%rsp), %rsi
callq 0x2270
addq %r12, 0x68(%rsp)
incq %r15
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, %r15
jb 0x34c5
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x40(%rsp)
movq $0x0, 0x4e(%rsp)
movl $0x16, %edx
movq %rbp, %rdi
leaq 0x40(%rsp), %rsi
leaq 0xf8(%rsp), %r15
callq 0x2270
movq 0x150(%rsp), %rax
addq %r13, %rax
addq $0x16, %rax
cmpq %rax, %r14
je 0x35ca
subq %rax, %r14
cmpq $0x17, %r14
jae 0x38b3
movq %rbp, %rdi
leaq 0x40(%rsp), %rsi
movq %r14, %rdx
callq 0x2270
movq 0x80(%rsp), %rax
cmpq %rax, 0x88(%rsp)
movq 0x1b8(%rsp), %r13
leaq 0x620(%rsp), %r12
je 0x3636
movl $0x18, %ebx
xorl %r14d, %r14d
movq -0x8(%rax,%rbx), %rdx
movq (%rax,%rbx), %rsi
movq %rbp, %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 0x35f4
movq %rbp, %rdi
callq 0x2190
movq %rax, 0x70(%rsp)
movq 0xe8(%rsp), %rdi
cmpq %r15, %rdi
leaq 0x158(%rsp), %rbx
leaq 0x40(%rsp), %r14
je 0x366d
movq 0xf8(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0x148(%rsp), %rax
incl %eax
movq %rax, 0x148(%rsp)
leaq 0x80(%rsp), %rdi
callq 0x4320
movq %rbx, %rdi
callq 0x436e
movq 0x1d8(%rsp), %rdx
movq 0x1d0(%rsp), %rsi
cmpq %rdx, %rsi
jne 0x2e37
movq 0x5a90c(%rip), %rax     # 0x5dfc0
movq (%rax), %rbx
movq 0x18(%rax), %r14
leaq 0x428(%rsp), %rdi
movq %rbx, -0x8(%rdi)
movq -0x18(%rbx), %rax
movq %r14, 0x420(%rsp,%rax)
callq 0x2180
movl $0xf8, %r15d
addq %r15, %rbp
movq %rbp, %rdi
callq 0x2090
leaq 0x228(%rsp), %rdi
movq %rbx, -0x8(%rdi)
movq -0x18(%rbx), %rax
movq %r14, 0x220(%rsp,%rax)
callq 0x2180
leaq 0x220(%rsp), %rdi
addq %r15, %rdi
callq 0x2090
leaq 0x628(%rsp), %rdi
movq %rbx, -0x8(%rdi)
movq -0x18(%rbx), %rax
movq %r14, 0x620(%rsp,%rax)
callq 0x2180
addq %r15, %r12
movq %r12, %rdi
callq 0x2090
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x375b
movq 0xe0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21f0
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x3778
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21f0
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x378f
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x21f0
movq 0x170(%rsp), %rdi
leaq 0x180(%rsp), %rax
cmpq %rax, %rdi
je 0x37b4
movq 0x180(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0x108(%rsp), %rdi
leaq 0x118(%rsp), %rax
cmpq %rax, %rdi
je 0x37d9
movq 0x118(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0x128(%rsp), %rdi
leaq 0x138(%rsp), %rax
cmpq %rax, %rdi
je 0x37fe
movq 0x138(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0x1e0(%rsp), %rdi
leaq 0x1f0(%rsp), %rax
cmpq %rax, %rdi
je 0x3823
movq 0x1f0(%rsp), %rsi
incq %rsi
callq 0x21f0
leaq 0x820(%rsp), %rdi
callq 0x2070
leaq 0x1a0(%rsp), %rdi
callq 0x43bc
addq $0xa28, %rsp            # imm = 0xA28
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x10, %edi
callq 0x20f0
movq %rax, %r14
leaq 0x55a8b(%rip), %rsi     # 0x592ee
movq %rax, %rdi
callq 0x20b0
movq 0x5a77e(%rip), %rsi     # 0x5dff0
movq 0x5a74f(%rip), %rdx     # 0x5dfc8
movq %r14, %rdi
callq 0x2300
movq 0x5a760(%rip), %rdi     # 0x5dfe8
leaq 0x558a3(%rip), %rsi     # 0x59132
callq 0x21d0
jmp 0x38a9
movq 0x5a74b(%rip), %rdi     # 0x5dfe8
leaq 0x5588e(%rip), %rsi     # 0x59132
callq 0x21d0
movl $0x1, %edi
callq 0x2230
movq 0x5a72e(%rip), %rdi     # 0x5dfe8
leaq 0x558b6(%rip), %rsi     # 0x59177
movl $0x2b, %edx
callq 0x2210
movq 0x5a716(%rip), %rdi     # 0x5dfe8
callq 0x20e0
jmp 0x38a9
movq 0x5a708(%rip), %rdi     # 0x5dfe8
leaq 0x557fc(%rip), %rsi     # 0x590e3
movl $0x36, %edx
callq 0x2210
movq 0x5a6f0(%rip), %rdi     # 0x5dfe8
callq 0x20e0
jmp 0x38a9
movq %rax, %rbx
movl $0x18, %esi
movq 0xa0(%rsp), %rdi
callq 0x21f0
jmp 0x3b74
jmp 0x39c4
jmp 0x39c4
movq %rax, %rbx
jmp 0x3a5b
movq %rax, %rbx
jmp 0x3a68
jmp 0x39cc
jmp 0x39cc
jmp 0x39cc
movq %rax, %rbx
jmp 0x3ac6
movq %rax, %rbx
jmp 0x3aeb
movq %rax, %rbx
movq 0x108(%rsp), %rdi
leaq 0x118(%rsp), %rax
jmp 0x3afb
movq %rax, %rbx
jmp 0x3b10
movq %rax, %rbx
movq 0x128(%rsp), %rdi
leaq 0x138(%rsp), %rax
jmp 0x3b20
jmp 0x39d6
movq %rax, %rbx
jmp 0x3b67
jmp 0x39d6
jmp 0x3a0c
jmp 0x39cc
jmp 0x39c4
jmp 0x39cc
jmp 0x3a07
jmp 0x3a07
jmp 0x39c4
jmp 0x39de
jmp 0x3a07
jmp 0x3a07
movq %rax, %rbx
jmp 0x39ba
movq %rax, %rbx
movq %r14, %rdi
callq 0x2150
leaq 0xf8(%rsp), %r15
jmp 0x39ea
movq %rax, %rbx
jmp 0x3a4e
movq %rax, %rbx
jmp 0x3a75
jmp 0x3a07
movq %rax, %rbx
jmp 0x3b35
movq %rax, %rbx
jmp 0x3a34
jmp 0x39e7
jmp 0x39e7
movq %rax, %rbx
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x3a17
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x21f0
jmp 0x3a17
jmp 0x3a07
jmp 0x3a07
movq %rax, %rbx
jmp 0x3a17
movq %rax, %rbx
leaq 0xf8(%rsp), %r15
movq 0xe8(%rsp), %rdi
cmpq %r15, %rdi
je 0x3a34
movq 0xf8(%rsp), %rsi
incq %rsi
callq 0x21f0
leaq 0x80(%rsp), %rdi
callq 0x4320
leaq 0x158(%rsp), %rdi
callq 0x436e
leaq 0x420(%rsp), %rdi
callq 0x2290
leaq 0x220(%rsp), %rdi
callq 0x2290
leaq 0x620(%rsp), %rdi
callq 0x2290
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x3a92
movq 0xe0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21f0
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x3aaf
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21f0
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x3ac6
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x21f0
movq 0x170(%rsp), %rdi
leaq 0x180(%rsp), %rax
cmpq %rax, %rdi
je 0x3aeb
movq 0x180(%rsp), %rsi
incq %rsi
callq 0x21f0
leaq 0x118(%rsp), %rax
movq 0x108(%rsp), %rdi
cmpq %rax, %rdi
je 0x3b10
movq 0x118(%rsp), %rsi
incq %rsi
callq 0x21f0
leaq 0x138(%rsp), %rax
movq 0x128(%rsp), %rdi
cmpq %rax, %rdi
je 0x3b35
movq 0x138(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0x1e0(%rsp), %rdi
leaq 0x1f0(%rsp), %rax
cmpq %rax, %rdi
je 0x3b5a
movq 0x1f0(%rsp), %rsi
incq %rsi
callq 0x21f0
leaq 0x820(%rsp), %rdi
callq 0x2070
leaq 0x1a0(%rsp), %rdi
callq 0x43bc
movq %rbx, %rdi
callq 0x2310