// File: /workspace/github_cpp_makefile_star2_O2/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 $0x9d8, %rsp            # imm = 0x9D8
vmovsd %xmm0, 0x18(%rsp)
movq %rsi, %r14
movq %rdi, %rbx
movb %dl, 0xb(%rsp)
cmpl $0x47, %edx
je 0x272e
cmpl $0x67, %edx
je 0x2725
cmpl $0x71, %edx
jne 0x274b
pushq $0x18
popq %rdi
callq 0x2160
movq %rax, 0x48(%rsp)
movq %rax, %rdi
callq 0x4870
jmp 0x2754
leaq 0x5a5cc(%rip), %r15     # 0x5ccf8
jmp 0x2735
leaq 0x5a56b(%rip), %r15     # 0x5cca0
addq $0x10, %r15
pushq $0x8
popq %rdi
callq 0x2160
movq %rax, 0x48(%rsp)
movq %r15, (%rax)
jmp 0x2754
movq $0x0, 0x48(%rsp)
vxorpd %xmm0, %xmm0, %xmm0
vmovapd %xmm0, 0x30(%rsp)
andq $0x0, 0x40(%rsp)
leaq 0x7d0(%rsp), %rdi
pushq $0x8
popq %rdx
movq %r14, %rsi
callq 0x2280
leaq 0x1a0(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
xorl %eax, %eax
leaq 0x7d0(%rsp), %r14
leaq 0x190(%rsp), %r15
leaq 0x30(%rsp), %r12
leaq 0x4(%rsp), %r13
movl %eax, 0x4(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x21d0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x27da
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x37d8
movl 0x4(%rsp), %eax
incl %eax
jmp 0x27a7
movq 0x5a807(%rip), %rdi     # 0x5cfe8
leaq 0x55855(%rip), %rsi     # 0x5803d
callq 0x2150
movq 0x38(%rsp), %rsi
subq 0x30(%rsp), %rsi
sarq $0x6, %rsi
movq %rax, %rdi
callq 0x20f0
leaq 0x55839(%rip), %rsi     # 0x58043
movq %rax, %rdi
callq 0x2150
leaq 0x55833(%rip), %rdx     # 0x5804c
leaq 0x170(%rsp), %rdi
movq %rbx, %rsi
callq 0x3804
leaq 0x55822(%rip), %rdx     # 0x58052
leaq 0x150(%rsp), %rdi
movq %rbx, %rsi
callq 0x3804
leaq 0x55812(%rip), %rdx     # 0x58059
leaq 0x130(%rsp), %rdi
movq %rbx, %rsi
callq 0x3804
movq 0x170(%rsp), %rdi
leaq 0x557fa(%rip), %r15     # 0x58060
movq %r15, %rsi
callq 0x21a0
movq %rax, %rbx
movq 0x150(%rsp), %rdi
movq %r15, %rsi
callq 0x21a0
movq %rax, %r14
movq 0x130(%rsp), %rdi
movq %r15, %rsi
callq 0x21a0
movq %rax, %r15
vxorpd %xmm0, %xmm0, %xmm0
leaq 0x80(%rsp), %rdi
vmovapd %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
vmovapd %xmm0, 0x100(%rsp)
andq $0x0, 0x110(%rsp)
vmovapd %xmm0, 0xa0(%rsp)
andq $0x0, 0xb0(%rsp)
movq %rbx, %rsi
callq 0x24e7
movq 0x80(%rsp), %rax
movl (%rax), %r12d
leaq 0xa0(%rsp), %rdi
movq %r15, %rsi
callq 0x24e7
movq 0xa8(%rsp), %r15
subq 0xa0(%rsp), %r15
sarq $0x2, %r15
cmpq %r12, %r15
jne 0x32a4
movq 0x5a6d1(%rip), %rdi     # 0x5cfe8
leaq 0x5577c(%rip), %rsi     # 0x5809a
callq 0x2150
movq %rax, %rdi
movq %r15, %rsi
callq 0x20f0
leaq 0x55771(%rip), %rsi     # 0x580a6
movq %rax, %rdi
callq 0x2150
movq %rax, %rdi
callq 0x20c0
movq 0xa0(%rsp), %rdi
movq 0xa8(%rsp), %rsi
vxorpd %xmm0, %xmm0, %xmm0
vmovsd %xmm0, 0x10(%rsp)
callq 0x383c
vcvtsi2sd %r12, %xmm1, %xmm1
vdivsd %xmm1, %xmm0, %xmm0
vcvtsd2ss %xmm0, %xmm0, %xmm0
vcvtsi2ss %r12, %xmm2, %xmm1
leaq 0x128(%rsp), %r15
vmovss %xmm1, (%r15)
vmovss %xmm0, 0x4(%r15)
xorl %eax, %eax
leaq 0x80(%rsp), %r12
leaq 0x100(%rsp), %r13
movl %eax, 0x4(%rsp)
movq %rbx, %rdi
callq 0x2260
testl %eax, %eax
jne 0x2b50
movq %r12, %rdi
movq %rbx, %rsi
callq 0x24e7
movq %r13, %rdi
movq %r14, %rsi
callq 0x24e7
movq 0x80(%rsp), %rcx
movq 0x88(%rsp), %rdx
movq %rdx, %rax
subq %rcx, %rax
movq 0x108(%rsp), %rsi
subq 0x100(%rsp), %rsi
cmpq %rsi, %rax
jne 0x326c
cmpq %rcx, %rdx
je 0x2b35
sarq $0x2, %rax
js 0x2a0a
vcvtsi2ss %rax, %xmm2, %xmm0
jmp 0x2a1f
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
vcvtsi2ss %rax, %xmm2, %xmm0
vaddss %xmm0, %xmm0, %xmm0
movq %r15, %rdi
callq 0x3854
vmovss %xmm0, 0x78(%rsp)
movl 0x4(%rsp), %eax
movq 0x30(%rsp), %rcx
shlq $0x6, %rax
leaq (%rcx,%rax), %rdi
addq $0x28, %rdi
movq 0x88(%rsp), %rsi
subq 0x80(%rsp), %rsi
sarq $0x2, %rsi
callq 0x3888
movq 0x80(%rsp), %rax
movq 0x88(%rsp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
movq 0x100(%rsp), %rdx
movq 0xa0(%rsp), %rsi
vmovss 0x12c(%rsp), %xmm0
movq 0x30(%rsp), %rdi
xorl %r8d, %r8d
xorl %r9d, %r9d
vmovss 0x78(%rsp), %xmm3
cmpq %r9, %rcx
je 0x2b2a
movl (%rax,%r9,4), %r10d
movl (%rdx,%r9,4), %r11d
vcvtsi2ss %r11, %xmm4, %xmm1
movl (%rsi,%r10,4), %r11d
vcvtsi2ss %r11, %xmm4, %xmm2
vdivss %xmm0, %xmm2, %xmm2
vmulss 0x5553d(%rip), %xmm2, %xmm2 # 0x58004
vaddss 0x55539(%rip), %xmm2, %xmm2 # 0x58008
vmulss 0x55535(%rip), %xmm2, %xmm2 # 0x5800c
vaddss %xmm1, %xmm2, %xmm2
vdivss %xmm2, %xmm1, %xmm1
vmulss %xmm1, %xmm3, %xmm1
vcvtss2sd %xmm1, %xmm1, %xmm1
vmovsd 0x10(%rsp), %xmm2
vmaxsd %xmm2, %xmm1, %xmm2
vmovsd %xmm2, 0x10(%rsp)
movl 0x4(%rsp), %r11d
shlq $0x6, %r11
movq 0x28(%rdi,%r11), %r11
movl %r10d, (%r11,%r8)
movl 0x4(%rsp), %r10d
shlq $0x6, %r10
movq 0x28(%rdi,%r10), %r10
vmovsd %xmm1, 0x8(%r10,%r8)
incq %r9
addq $0x18, %r8
jmp 0x2a9c
movl 0x4(%rsp), %eax
incl %eax
jmp 0x299b
movl 0x4(%rsp), %eax
movq 0x38(%rsp), %rcx
subq 0x30(%rsp), %rcx
sarq $0x6, %rcx
cmpq %rax, %rcx
jne 0x32c1
movq 0x5a491(%rip), %rdi     # 0x5cfe8
leaq 0x55595(%rip), %rsi     # 0x580f3
callq 0x2150
movq %rax, %rdi
vmovsd 0x10(%rsp), %xmm0
callq 0x2270
movq %rax, %rdi
callq 0x20c0
movq 0x5a468(%rip), %rdi     # 0x5cfe8
leaq 0x5557d(%rip), %rsi     # 0x58104
callq 0x2150
movq %rax, %rdi
vmovsd 0x18(%rsp), %xmm0
callq 0x2270
leaq 0x5557d(%rip), %rsi     # 0x5811e
movq %rax, %rdi
callq 0x2150
movq %rax, %rdi
callq 0x20c0
movq 0x30(%rsp), %rcx
movq 0x38(%rsp), %rsi
subq %rcx, %rsi
sarq $0x6, %rsi
xorl %edi, %edi
pushq $0x18
popq %r9
cmpq %rsi, %rdi
je 0x2c1b
movq %rdi, %rax
shlq $0x6, %rax
movq 0x28(%rcx,%rax), %r8
movq 0x30(%rcx,%rax), %rax
subq %r8, %rax
cqto
idivq %r9
addq $0x10, %r8
subq $0x1, %rax
jb 0x2c16
vmovsd 0x18(%rsp), %xmm0
vmulsd -0x8(%r8), %xmm0, %xmm0
vdivsd 0x10(%rsp), %xmm0, %xmm0
vroundsd $0xa, %xmm0, %xmm0, %xmm0
vcvttsd2si %xmm0, %rdx
movl %edx, (%r8)
addq $0x18, %r8
jmp 0x2bea
incq %rdi
jmp 0x2bc8
leaq 0x554fe(%rip), %rsi     # 0x58120
leaq 0x5d0(%rsp), %rdi
pushq $0x10
popq %rdx
callq 0x2180
leaq 0x554f9(%rip), %rsi     # 0x58132
leaq 0x3d0(%rsp), %rdi
pushq $0x4
popq %rdx
callq 0x2180
leaq 0x554ee(%rip), %rsi     # 0x5813e
leaq 0x1d0(%rsp), %rdi
pushq $0x4
popq %rdx
callq 0x2180
andq $0x0, 0xd8(%rsp)
andq $0x0, 0x28(%rsp)
leaq 0x1d0(%rsp), %rdi
leaq 0xb(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x2200
leaq 0x1d0(%rsp), %rdi
callq 0x2120
leaq 0x28(%rsp), %rcx
movq %rax, (%rcx)
vcvttsd2si 0x18(%rsp), %rax
movl %eax, %eax
movq %rax, 0x118(%rsp)
xorl %r12d, %r12d
leaq 0x1b0(%rsp), %r13
pushq $0x8
popq %rbp
leaq 0xe0(%rsp), %r15
leaq 0x1d0(%rsp), %rbx
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rax
subq %rsi, %rax
sarq $0x6, %rax
cmpq %rax, %r12
jae 0x3191
movq %r12, %r14
shlq $0x6, %r14
addq %r14, %rsi
movq %r13, %rdi
callq 0x2210
leaq 0x3d0(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
movq %rbp, %rdx
callq 0x2200
leaq 0x3d0(%rsp), %rdi
leaq 0x28(%rsp), %rsi
movq %rbp, %rdx
callq 0x2200
leaq 0x5d0(%rsp), %rdi
movq %r13, %rsi
callq 0x2140
movq %rax, %rdi
xorl %esi, %esi
callq 0x2190
leaq 0x5d0(%rsp), %rdi
callq 0x2120
movq %r12, 0x10(%rsp)
movq %rax, 0xd8(%rsp)
leaq 0xc0(%rsp), %rdi
movq 0x118(%rsp), %rsi
leaq 0x60(%rsp), %rdx
callq 0x38c0
pushq $0x10
popq %r12
xorl %r13d, %r13d
movq 0x30(%rsp), %rax
movq 0x28(%rax,%r14), %rcx
movq 0x30(%rax,%r14), %rax
subq %rcx, %rax
cqto
pushq $0x18
popq %rsi
idivq %rsi
cmpq %rax, %r13
jae 0x2dc5
movl -0x10(%rcx,%r12), %eax
movl (%rcx,%r12), %ecx
decl %ecx
imulq $0x18, %rcx, %rdi
addq 0xc0(%rsp), %rdi
incl %eax
movl %eax, 0x60(%rsp)
leaq 0x60(%rsp), %rsi
callq 0x440e
incq %r13
addq $0x18, %r12
jmp 0x2d76
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x60(%rsp)
andq $0x0, 0x70(%rsp)
andq $0x0, 0x58(%rsp)
xorl %r14d, %r14d
leaq 0x50(%rsp), %r12
movq 0xc0(%rsp), %rcx
movq 0xc8(%rsp), %rax
subq %rcx, %rax
cqto
pushq $0x18
popq %rsi
idivq %rsi
cmpq %rax, %r14
jae 0x2f89
imulq $0x18, %r14, %r13
movq 0x8(%rcx,%r13), %rsi
subq (%rcx,%r13), %rsi
je 0x2f5e
sarq %rsi
addq $0x400, %rsi            # imm = 0x400
movq %r15, %rdi
movq %r12, %rdx
callq 0x3914
movq 0xe8(%rsp), %rax
subq 0xe0(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0xc0(%rsp), %rcx
movq (%rcx,%r13), %rax
movq 0x8(%rcx,%r13), %rdx
movq %rdx, %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq $0x4, %rcx
ja 0x2e85
cmpq %rax, %rdx
je 0x323c
decq %rcx
movq %rcx, %rdx
subq $0x1, %rdx
jb 0x2ec9
movl (%rax,%rdx,4), %esi
subl %esi, (%rax,%rcx,4)
movq %rdx, %rcx
jmp 0x2e74
movq %rcx, %rdx
andq $-0x4, %rdx
leaq (%rax,%rdx,4), %rsi
vxorps %xmm0, %xmm0, %xmm0
movq %rax, %rdi
cmpq %rsi, %rdi
jae 0x2ec0
vpsrldq $0xc, %xmm0, %xmm0      # xmm0 = xmm0[12,13,14,15],zero,zero,zero,zero,zero,zero,zero,zero,zero,zero,zero,zero
vmovdqa (%rdi), %xmm1
vpslldq $0x4, %xmm1, %xmm2      # xmm2 = zero,zero,zero,zero,xmm1[0,1,2,3,4,5,6,7,8,9,10,11]
vpaddd %xmm2, %xmm0, %xmm0
vpsubd %xmm0, %xmm1, %xmm0
vmovdqa %xmm0, (%rdi)
addq $0x10, %rdi
vmovdqa %xmm1, %xmm0
jmp 0x2e97
cmpq %rcx, %rdx
jne 0x2f66
movq 0xc0(%rsp), %rax
movq (%rax,%r13), %rsi
movq 0x8(%rax,%r13), %rdx
subq %rsi, %rdx
sarq $0x2, %rdx
movq 0xe0(%rsp), %rcx
movq 0x48(%rsp), %rdi
movq (%rdi), %rax
movq %r12, %r8
callq *0x10(%rax)
movq 0x50(%rsp), %rsi
addq $0xf, %rsi
andq $-0x10, %rsi
movq %rsi, 0x20(%rsp)
shrq $0x2, %rsi
movq %r15, %rdi
callq 0x37a6
leal 0x1(%r14), %eax
movw %ax, 0x8(%rsp)
movq 0xc0(%rsp), %rax
movq 0x8(%rax,%r13), %rcx
subq (%rax,%r13), %rcx
shrq $0x2, %rcx
movl %ecx, 0xc(%rsp)
leaq 0x60(%rsp), %rdi
leaq 0x8(%rsp), %rsi
leaq 0xc(%rsp), %rdx
movq %r12, %rcx
leaq 0x20(%rsp), %r8
movq %r15, %r9
callq 0x3968
movq %r15, %rdi
callq 0x3bf0
incq %r14
jmp 0x2de3
vextractps $0x3, %xmm0, %esi
cmpq %rcx, %rdx
jae 0x2ec9
movl (%rax,%rdx,4), %edi
movl %edi, %r8d
subl %esi, %r8d
movl %r8d, (%rax,%rdx,4)
incq %rdx
movl %edi, %esi
jmp 0x2f6c
movq 0x68(%rsp), %rax
subq 0x60(%rsp), %rax
cqto
pushq $0x30
popq %rcx
idivq %rcx
movq %rax, 0x58(%rsp)
leaq 0x3d0(%rsp), %rdi
leaq 0x58(%rsp), %rsi
movq %rbp, %rdx
callq 0x2200
movq 0x58(%rsp), %rax
leaq (,%rax,8), %rcx
addq 0x28(%rsp), %rcx
movq %rcx, 0x50(%rsp)
xorl %r14d, %r14d
cmpq %rax, %r14
jae 0x2ff2
movq %rbx, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x2200
addq $0x16, 0x50(%rsp)
incq %r14
movq 0x58(%rsp), %rax
jmp 0x2fcf
movq 0x28(%rsp), %rcx
imulq $0x1e, %rax, %rax
leaq (%rax,%rcx), %rdx
addq $0x16, %rdx
addq %rax, %rcx
addq $0x25, %rcx
andq $-0x10, %rcx
movq %rcx, %rax
movq %rdx, 0x18(%rsp)
subq %rdx, %rax
movq %rax, 0x120(%rsp)
movq %rcx, 0x20(%rsp)
pushq $0x10
popq %r14
movq %rcx, 0x78(%rsp)
xorl %r13d, %r13d
movq 0x60(%rsp), %rsi
movq 0x68(%rsp), %rax
subq %rsi, %rax
cqto
pushq $0x30
popq %rdi
idivq %rdi
cmpq %rax, %r13
jae 0x30c8
movzwl -0x10(%rsi,%r14), %eax
movw %ax, 0x8(%rsp)
movl -0xc(%rsi,%r14), %eax
movl %eax, 0xc(%rsp)
movq (%rsi,%r14), %r12
addq -0x8(%rsi,%r14), %rcx
movq %rcx, 0xe0(%rsp)
movq %rbx, %rdi
leaq 0x8(%rsp), %rsi
pushq $0x2
popq %rdx
callq 0x2200
movq %rbx, %rdi
leaq 0x20(%rsp), %rsi
movq %rbp, %rdx
callq 0x2200
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x2200
movq %rbx, %rdi
leaq 0xc(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x2200
addq 0x20(%rsp), %r12
movq %r12, 0x20(%rsp)
incq %r13
addq $0x30, %r14
movq %r12, %rcx
jmp 0x3032
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0xe0(%rsp)
andq $0x0, 0xee(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
pushq $0x16
popq %rdx
callq 0x2200
leaq 0x1b0(%rsp), %r13
movq 0x78(%rsp), %rax
cmpq 0x18(%rsp), %rax
je 0x311d
movq 0x120(%rsp), %rdx
cmpq $0x17, %rdx
jae 0x3281
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2200
pushq $0x18
popq %r14
xorl %r12d, %r12d
movq 0x60(%rsp), %rcx
movq 0x68(%rsp), %rax
subq %rcx, %rax
cqto
pushq $0x30
popq %rsi
idivq %rsi
cmpq %rax, %r12
jae 0x3158
movq -0x8(%rcx,%r14), %rdx
movq (%rcx,%r14), %rsi
movq %rbx, %rdi
callq 0x2200
incq %r12
addq $0x30, %r14
jmp 0x3124
movq %rbx, %rdi
callq 0x2120
movq %rax, 0x28(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x39b2
leaq 0xc0(%rsp), %rdi
callq 0x39d4
movq %r13, %rdi
callq 0x20b0
movq 0x10(%rsp), %r12
incq %r12
jmp 0x2cc8
leaq 0x1d0(%rsp), %rdi
callq 0x2230
leaq 0x3d0(%rsp), %rdi
callq 0x2230
leaq 0x5d0(%rsp), %rdi
callq 0x2230
leaq 0xa0(%rsp), %rdi
callq 0x3bf0
leaq 0x100(%rsp), %rdi
callq 0x3bf0
leaq 0x80(%rsp), %rdi
callq 0x3bf0
leaq 0x130(%rsp), %rdi
callq 0x20b0
leaq 0x150(%rsp), %rdi
callq 0x20b0
leaq 0x170(%rsp), %rdi
callq 0x20b0
leaq 0x190(%rsp), %rdi
callq 0x20b0
leaq 0x7d0(%rsp), %rdi
callq 0x2050
leaq 0x30(%rsp), %rdi
callq 0x39f6
addq $0x9d8, %rsp            # imm = 0x9D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x10
popq %rdi
callq 0x20d0
movq %rax, %r14
leaq 0x55060(%rip), %rsi     # 0x582ae
movq %rax, %rdi
callq 0x2080
movq 0x59d93(%rip), %rsi     # 0x5cff0
movq 0x59d64(%rip), %rdx     # 0x5cfc8
movq %r14, %rdi
callq 0x22b0
movq 0x59d75(%rip), %rdi     # 0x5cfe8
leaq 0x54e38(%rip), %rsi     # 0x580b2
callq 0x2150
jmp 0x329c
movq 0x59d60(%rip), %rdi     # 0x5cfe8
leaq 0x54ebe(%rip), %rsi     # 0x5814d
callq 0x2150
movq %rax, %rdi
callq 0x20c0
pushq $0x1
popq %rdi
callq 0x21b0
movq 0x59d3d(%rip), %rdi     # 0x5cfe8
leaq 0x54db1(%rip), %rsi     # 0x58063
callq 0x2150
movq %rax, %rdi
callq 0x20c0
jmp 0x329c
movq 0x59d20(%rip), %rdi     # 0x5cfe8
leaq 0x54dfb(%rip), %rsi     # 0x580ca
callq 0x2150
movq %rax, %rdi
callq 0x20c0
jmp 0x329c
movq %rax, %rbx
pushq $0x18
popq %rsi
movq 0x48(%rsp), %rdi
callq 0x2170
jmp 0x3445
jmp 0x3337
jmp 0x3337
movq %rax, %rbx
jmp 0x33b9
movq %rax, %rbx
jmp 0x33c6
jmp 0x333e
jmp 0x333e
movq %rax, %rbx
jmp 0x3407
movq %rax, %rbx
jmp 0x3414
jmp 0x3348
movq %rax, %rbx
jmp 0x343b
jmp 0x3348
jmp 0x333e
jmp 0x333e
jmp 0x333e
jmp 0x3385
jmp 0x333e
jmp 0x3385
jmp 0x3385
jmp 0x3350
movq %rax, %rbx
jmp 0x33ac
jmp 0x3350
movq %rax, %rbx
jmp 0x33d3
jmp 0x3385
movq %rax, %rbx
jmp 0x3421
movq %rax, %rbx
jmp 0x339f
jmp 0x3368
movq %rax, %rbx
movq %r14, %rdi
callq 0x2100
jmp 0x336b
jmp 0x3368
jmp 0x3368
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x3bf0
jmp 0x3388
jmp 0x3385
jmp 0x3385
jmp 0x3385
movq %rax, %rbx
jmp 0x3392
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x39b2
leaq 0xc0(%rsp), %rdi
callq 0x39d4
leaq 0x1b0(%rsp), %rdi
callq 0x20b0
leaq 0x1d0(%rsp), %rdi
callq 0x2230
leaq 0x3d0(%rsp), %rdi
callq 0x2230
leaq 0x5d0(%rsp), %rdi
callq 0x2230
leaq 0xa0(%rsp), %rdi
callq 0x3bf0
leaq 0x100(%rsp), %rdi
callq 0x3bf0
leaq 0x80(%rsp), %rdi
callq 0x3bf0
leaq 0x130(%rsp), %rdi
callq 0x20b0
leaq 0x150(%rsp), %rdi
callq 0x20b0
leaq 0x170(%rsp), %rdi
callq 0x20b0
leaq 0x190(%rsp), %rdi
callq 0x20b0
leaq 0x7d0(%rsp), %rdi
callq 0x2050
leaq 0x30(%rsp), %rdi
callq 0x39f6
movq %rbx, %rdi
callq 0x22c0