// File: 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:
subq $0xd08, %rsp            # imm = 0xD08
movq %rsi, 0x1b8(%rsp)
movq %rdi, 0x1c0(%rsp)
movb %dl, %al
movq %rdi, 0xd00(%rsp)
movq %rsi, 0xcf8(%rsp)
vmovsd %xmm0, 0xcf0(%rsp)
movb %al, 0xcef(%rsp)
movsbl 0xcef(%rsp), %eax
cmpl $0x71, %eax
jne 0x29e5
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x1b0(%rsp)
callq 0x94a0
jmp 0x29a5
movq 0x1b0(%rsp), %rax
movq %rax, 0xce0(%rsp)
jmp 0x2ad8
movq 0x1b0(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xcd8(%rsp)
movl %eax, 0xcd4(%rsp)
movl $0x18, %esi
callq 0x21a0
jmp 0x4118
movsbl 0xcef(%rsp), %eax
cmpl $0x47, %eax
jne 0x2a58
movl $0x8, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x1a8(%rsp)
movq $0x0, (%rdi)
callq 0x4d30
jmp 0x2a18
movq 0x1a8(%rsp), %rax
movq %rax, 0xce0(%rsp)
jmp 0x2ad6
movq 0x1a8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xcd8(%rsp)
movl %eax, 0xcd4(%rsp)
movl $0x8, %esi
callq 0x21a0
jmp 0x4118
movsbl 0xcef(%rsp), %eax
cmpl $0x67, %eax
jne 0x2ac8
movl $0x8, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x1a0(%rsp)
movq $0x0, (%rdi)
callq 0x4d60
jmp 0x2a8b
movq 0x1a0(%rsp), %rax
movq %rax, 0xce0(%rsp)
jmp 0x2ad4
movq 0x1a0(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xcd8(%rsp)
movl %eax, 0xcd4(%rsp)
movl $0x8, %esi
callq 0x21a0
jmp 0x4118
movq $0x0, 0xce0(%rsp)
jmp 0x2ad6
jmp 0x2ad8
movq 0xce0(%rsp), %rax
movq %rax, 0xcc8(%rsp)
movl $0x10, 0xcc4(%rsp)
leaq 0xca8(%rsp), %rdi
callq 0x4d90
movq 0x1b8(%rsp), %rsi
leaq 0xaa0(%rsp), %rdi
movl $0x8, %edx
callq 0x22e0
jmp 0x2b1c
leaq 0xa80(%rsp), %rdi
callq 0x2280
movl $0x0, 0xa7c(%rsp)
leaq 0xaa0(%rsp), %rdi
leaq 0xa80(%rsp), %rsi
callq 0x2230
movq %rax, 0x198(%rsp)
jmp 0x2b53
movq 0x198(%rsp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x2220
movb %al, 0x197(%rsp)
jmp 0x2b73
movb 0x197(%rsp), %al
testb $0x1, %al
jne 0x2b80
jmp 0x2bdc
leaq 0xca8(%rsp), %rdi
leaq 0xa80(%rsp), %rsi
callq 0x4da0
jmp 0x2b97
movl 0xa7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa7c(%rsp)
jmp 0x2b34
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xcd8(%rsp)
movl %eax, 0xcd4(%rsp)
jmp 0x410b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xcd8(%rsp)
movl %eax, 0xcd4(%rsp)
jmp 0x40f1
movq 0x307405(%rip), %rdi    # 0x309fe8
leaq 0x30043b(%rip), %rsi    # 0x303025
callq 0x2180
movq %rax, 0x188(%rsp)
jmp 0x2bf9
leaq 0xca8(%rsp), %rdi
callq 0x4e10
movq 0x188(%rsp), %rdi
movq %rax, %rsi
callq 0x2060
movq %rax, 0x180(%rsp)
jmp 0x2c20
movq 0x180(%rsp), %rdi
leaq 0x3003fc(%rip), %rsi    # 0x30302b
callq 0x2180
jmp 0x2c36
movq 0x1c0(%rsp), %rsi
leaq 0x3003ef(%rip), %rdx    # 0x303034
leaq 0xa58(%rsp), %rdi
callq 0x4e30
jmp 0x2c54
movq 0x1c0(%rsp), %rsi
leaq 0x3003d7(%rip), %rdx    # 0x30303a
leaq 0xa38(%rsp), %rdi
callq 0x4e30
jmp 0x2c72
movq 0x1c0(%rsp), %rsi
leaq 0x3003c0(%rip), %rdx    # 0x303041
leaq 0xa18(%rsp), %rdi
callq 0x4e30
jmp 0x2c90
leaq 0xa58(%rsp), %rdi
callq 0x20c0
movq %rax, %rdi
leaq 0x3003a1(%rip), %rsi    # 0x303048
callq 0x21f0
movq %rax, 0x178(%rsp)
jmp 0x2cb6
movq 0x178(%rsp), %rax
movq %rax, 0xa10(%rsp)
leaq 0xa38(%rsp), %rdi
callq 0x20c0
movq %rax, %rdi
leaq 0x30036b(%rip), %rsi    # 0x303048
callq 0x21f0
movq %rax, 0x170(%rsp)
jmp 0x2cec
movq 0x170(%rsp), %rax
movq %rax, 0xa08(%rsp)
leaq 0xa18(%rsp), %rdi
callq 0x20c0
movq %rax, %rdi
leaq 0x300335(%rip), %rsi    # 0x303048
callq 0x21f0
movq %rax, 0x168(%rsp)
jmp 0x2d22
movq 0x168(%rsp), %rax
movq %rax, 0xa00(%rsp)
leaq 0x9e8(%rsp), %rdi
movq %rdi, 0x160(%rsp)
callq 0x4ec0
leaq 0x9d0(%rsp), %rdi
callq 0x4ec0
leaq 0x9b8(%rsp), %rdi
callq 0x4ec0
movq 0x160(%rsp), %rdi
movq $0x0, 0x9b0(%rsp)
movq 0xa10(%rsp), %rsi
callq 0x25f0
jmp 0x2d84
xorl %eax, %eax
movl %eax, %esi
leaq 0x9e8(%rsp), %rdi
callq 0x4ed0
movl (%rax), %eax
movl %eax, 0x9ac(%rsp)
movq 0xa00(%rsp), %rsi
leaq 0x9b8(%rsp), %rdi
callq 0x25f0
jmp 0x2db5
leaq 0x9b8(%rsp), %rdi
callq 0x4ef0
movl 0x9ac(%rsp), %ecx
cmpq %rcx, %rax
je 0x2e73
movq 0x30720f(%rip), %rdi    # 0x309fe8
leaq 0x30026b(%rip), %rsi    # 0x30304b
callq 0x2180
movq %rax, 0x158(%rsp)
jmp 0x2def
movq 0x158(%rsp), %rdi
movq 0x3071ba(%rip), %rsi    # 0x309fb8
callq 0x21b0
jmp 0x2e05
movl $0x1, %edi
callq 0x2200
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xcd8(%rsp)
movl %eax, 0xcd4(%rsp)
jmp 0x40e4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xcd8(%rsp)
movl %eax, 0xcd4(%rsp)
jmp 0x40d7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xcd8(%rsp)
movl %eax, 0xcd4(%rsp)
jmp 0x40ca
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xcd8(%rsp)
movl %eax, 0xcd4(%rsp)
jmp 0x40a3
movq 0x30716e(%rip), %rdi    # 0x309fe8
leaq 0x300201(%rip), %rsi    # 0x303082
callq 0x2180
movq %rax, 0x150(%rsp)
jmp 0x2e90
movq 0x150(%rsp), %rdi
movl 0x9ac(%rsp), %esi
callq 0x2260
movq %rax, 0x148(%rsp)
jmp 0x2eae
movq 0x148(%rsp), %rdi
leaq 0x3001d1(%rip), %rsi    # 0x30308e
callq 0x2180
movq %rax, 0x140(%rsp)
jmp 0x2ecc
movq 0x140(%rsp), %rdi
movq 0x3070dd(%rip), %rsi    # 0x309fb8
callq 0x21b0
jmp 0x2ee2
leaq 0x9b8(%rsp), %rdi
movq %rdi, 0x130(%rsp)
callq 0x4f80
movq 0x130(%rsp), %rdi
movq %rax, 0x9a0(%rsp)
callq 0x4fb0
movq %rax, 0x998(%rsp)
movq 0x9a0(%rsp), %rdi
movq 0x998(%rsp), %rsi
vxorps %xmm0, %xmm0, %xmm0
callq 0x4f10
vmovsd %xmm0, 0x138(%rsp)
jmp 0x2f38
vmovsd 0x138(%rsp), %xmm0
movl 0x9ac(%rsp), %eax
vcvtsi2sd %rax, %xmm1, %xmm1
vdivsd %xmm1, %xmm0, %xmm1
vcvtsd2ss %xmm1, %xmm0, %xmm0
vmovss %xmm0, 0x9a8(%rsp)
movl 0x9ac(%rsp), %eax
vcvtsi2ss %rax, %xmm0, %xmm0
vmovss 0x9a8(%rsp), %xmm1
leaq 0x990(%rsp), %rdi
callq 0x4fe0
jmp 0x2f82
movl $0x0, 0xa7c(%rsp)
movq 0xa10(%rsp), %rdi
callq 0x22d0
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x2fab
jmp 0x3228
movq 0xa10(%rsp), %rsi
leaq 0x9e8(%rsp), %rdi
callq 0x25f0
jmp 0x2fc2
movq 0xa08(%rsp), %rsi
leaq 0x9d0(%rsp), %rdi
callq 0x25f0
jmp 0x2fd9
leaq 0x9e8(%rsp), %rdi
callq 0x4ef0
movq %rax, 0x988(%rsp)
leaq 0x9d0(%rsp), %rdi
callq 0x4ef0
movq %rax, 0x980(%rsp)
movq 0x988(%rsp), %rax
cmpq 0x980(%rsp), %rax
je 0x3034
movq 0x306fcc(%rip), %rdi    # 0x309fe8
leaq 0x300077(%rip), %rsi    # 0x30309a
callq 0x2180
jmp 0x302a
movl $0x1, %edi
callq 0x2200
cmpq $0x0, 0x988(%rsp)
jne 0x3044
jmp 0x3228
movq 0x988(%rsp), %rax
vcvtsi2ss %rax, %xmm0, %xmm0
vmovss %xmm0, 0x128(%rsp)
movq %rax, %rdx
shrq %rdx
movl %eax, %ecx
andl $0x1, %ecx
orq %rdx, %rcx
vcvtsi2ss %rcx, %xmm0, %xmm0
vaddss %xmm0, %xmm0, %xmm0
testq %rax, %rax
vmovss %xmm0, 0x12c(%rsp)
js 0x3091
vmovss 0x128(%rsp), %xmm0
vmovss %xmm0, 0x12c(%rsp)
vmovss 0x12c(%rsp), %xmm0
leaq 0x990(%rsp), %rdi
callq 0x5010
vmovss %xmm0, 0x124(%rsp)
jmp 0x30b2
vmovss 0x124(%rsp), %xmm0
vmovss %xmm0, 0x97c(%rsp)
movq $0x0, 0x970(%rsp)
movq 0x970(%rsp), %rax
movq %rax, 0x118(%rsp)
leaq 0x9e8(%rsp), %rdi
callq 0x4ef0
movq %rax, %rcx
movq 0x118(%rsp), %rax
cmpq %rcx, %rax
jae 0x3212
movq 0x970(%rsp), %rsi
leaq 0x9e8(%rsp), %rdi
callq 0x4ed0
movl (%rax), %eax
movl %eax, 0x96c(%rsp)
movq 0x970(%rsp), %rsi
leaq 0x9d0(%rsp), %rdi
callq 0x4ed0
movl (%rax), %eax
movl %eax, 0x968(%rsp)
vmovss 0x97c(%rsp), %xmm0
vmovss %xmm0, 0x10c(%rsp)
movl 0x968(%rsp), %eax
vcvtsi2ss %rax, %xmm0, %xmm0
vmovss %xmm0, 0x110(%rsp)
movl 0x96c(%rsp), %eax
movl %eax, %esi
leaq 0x9b8(%rsp), %rdi
callq 0x4ed0
vmovss 0x110(%rsp), %xmm0
movl (%rax), %eax
vcvtsi2ss %rax, %xmm1, %xmm1
leaq 0x990(%rsp), %rdi
callq 0x5080
vmovss %xmm0, 0x114(%rsp)
jmp 0x31a2
vmovss 0x10c(%rsp), %xmm0
vmovss 0x114(%rsp), %xmm1
vmulss %xmm1, %xmm0, %xmm1
vcvtss2sd %xmm1, %xmm0, %xmm0
vmovsd %xmm0, 0x960(%rsp)
leaq 0x9b0(%rsp), %rdi
leaq 0x960(%rsp), %rsi
callq 0x50f0
movq %rax, 0x100(%rsp)
jmp 0x31e4
movq 0x100(%rsp), %rax
vmovsd (%rax), %xmm0
vmovsd %xmm0, 0x9b0(%rsp)
movq 0x970(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x970(%rsp)
jmp 0x30d0
movl 0xa7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa7c(%rsp)
jmp 0x2f8d
movq 0xa10(%rsp), %rdi
callq 0x2050
jmp 0x3237
movq 0xa08(%rsp), %rdi
callq 0x2050
jmp 0x3246
movq 0xa10(%rsp), %rsi
leaq 0x9e8(%rsp), %rdi
callq 0x25f0
jmp 0x325d
leaq 0x2ffe4e(%rip), %rsi    # 0x3030b2
leaq 0x760(%rsp), %rdi
movl $0x10, %edx
callq 0x21d0
jmp 0x3278
leaq 0x2ffe45(%rip), %rsi    # 0x3030c4
leaq 0x560(%rsp), %rdi
movl $0x4, %edx
callq 0x21d0
jmp 0x3293
leaq 0x2ffe36(%rip), %rsi    # 0x3030d0
leaq 0x360(%rsp), %rdi
movl $0x4, %edx
callq 0x21d0
jmp 0x32ae
movq $0x0, 0x358(%rsp)
movq $0x0, 0x350(%rsp)
leaq 0x360(%rsp), %rdi
leaq 0xcef(%rsp), %rsi
movl $0x1, %edx
callq 0x2250
jmp 0x32e2
leaq 0x360(%rsp), %rdi
callq 0x2150
movq %rdx, 0xf0(%rsp)
movq %rax, 0xf8(%rsp)
jmp 0x3301
movq 0xf0(%rsp), %rax
movq 0xf8(%rsp), %rcx
movq %rcx, 0x340(%rsp)
movq %rax, 0x348(%rsp)
leaq 0x340(%rsp), %rdi
callq 0x4c80
movq %rax, 0xe8(%rsp)
jmp 0x3338
movq 0xe8(%rsp), %rax
movq %rax, 0x350(%rsp)
movl $0x0, 0xa7c(%rsp)
movq 0xa10(%rsp), %rdi
callq 0x22d0
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x3371
jmp 0x3fd8
movq 0xa10(%rsp), %rsi
leaq 0x9e8(%rsp), %rdi
callq 0x25f0
jmp 0x3388
movq 0xa08(%rsp), %rsi
leaq 0x9d0(%rsp), %rdi
callq 0x25f0
jmp 0x339f
leaq 0x9e8(%rsp), %rdi
callq 0x4ef0
movq %rax, 0x338(%rsp)
leaq 0x9d0(%rsp), %rdi
callq 0x4ef0
movq %rax, 0x330(%rsp)
movq 0x338(%rsp), %rax
cmpq 0x330(%rsp), %rax
je 0x3445
movq 0x306c06(%rip), %rdi    # 0x309fe8
leaq 0x2ffcb1(%rip), %rsi    # 0x30309a
callq 0x2180
jmp 0x33f0
movl $0x1, %edi
callq 0x2200
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xcd8(%rsp)
movl %eax, 0xcd4(%rsp)
jmp 0x4096
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xcd8(%rsp)
movl %eax, 0xcd4(%rsp)
jmp 0x4089
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xcd8(%rsp)
movl %eax, 0xcd4(%rsp)
jmp 0x407c
cmpq $0x0, 0x338(%rsp)
jne 0x3455
jmp 0x3fd8
movq 0x338(%rsp), %rax
vcvtsi2ss %rax, %xmm0, %xmm0
vmovss %xmm0, 0xe0(%rsp)
movq %rax, %rdx
shrq %rdx
movl %eax, %ecx
andl $0x1, %ecx
orq %rdx, %rcx
vcvtsi2ss %rcx, %xmm0, %xmm0
vaddss %xmm0, %xmm0, %xmm0
testq %rax, %rax
vmovss %xmm0, 0xe4(%rsp)
js 0x34a2
vmovss 0xe0(%rsp), %xmm0
vmovss %xmm0, 0xe4(%rsp)
vmovss 0xe4(%rsp), %xmm0
leaq 0x990(%rsp), %rdi
callq 0x5010
vmovss %xmm0, 0xdc(%rsp)
jmp 0x34c3
vmovss 0xdc(%rsp), %xmm0
vmovss %xmm0, 0x32c(%rsp)
vcvttsd2si 0xcf0(%rsp), %rax
movl %eax, 0x328(%rsp)
movl 0x328(%rsp), %eax
movq %rax, 0xc8(%rsp)
leaq 0x30f(%rsp), %rdi
movq %rdi, 0xd0(%rsp)
callq 0x5130
movq 0xc8(%rsp), %rsi
movq 0xd0(%rsp), %rdx
leaq 0x310(%rsp), %rdi
callq 0x5140
jmp 0x3529
leaq 0x30f(%rsp), %rdi
callq 0x51c0
leaq 0x2f0(%rsp), %rdi
callq 0x51d0
movq $0x0, 0x2e8(%rsp)
movq 0x2e8(%rsp), %rax
movq %rax, 0xc0(%rsp)
leaq 0x9e8(%rsp), %rdi
callq 0x4ef0
movq %rax, %rcx
movq 0xc0(%rsp), %rax
cmpq %rcx, %rax
jae 0x370d
movq 0x2e8(%rsp), %rsi
leaq 0x9e8(%rsp), %rdi
callq 0x4ed0
movl (%rax), %eax
movl %eax, 0x2e4(%rsp)
movq 0x2e8(%rsp), %rsi
leaq 0x9d0(%rsp), %rdi
callq 0x4ed0
movl (%rax), %eax
movl %eax, 0x2e0(%rsp)
vmovss 0x32c(%rsp), %xmm0
vmovss %xmm0, 0xb4(%rsp)
movl 0x2e0(%rsp), %eax
vcvtsi2ss %rax, %xmm0, %xmm0
vmovss %xmm0, 0xb8(%rsp)
movl 0x2e4(%rsp), %eax
movl %eax, %esi
leaq 0x9b8(%rsp), %rdi
callq 0x4ed0
vmovss 0xb8(%rsp), %xmm0
movl (%rax), %eax
vcvtsi2ss %rax, %xmm1, %xmm1
leaq 0x990(%rsp), %rdi
callq 0x5080
vmovss %xmm0, 0xbc(%rsp)
jmp 0x3621
vmovss 0xb4(%rsp), %xmm0
vmovss 0xbc(%rsp), %xmm1
vmulss %xmm1, %xmm0, %xmm1
vcvtss2sd %xmm1, %xmm0, %xmm0
vmovsd %xmm0, 0x2d8(%rsp)
vmovsd 0xcf0(%rsp), %xmm0
vmovsd 0x2d8(%rsp), %xmm1
vmulsd %xmm1, %xmm0, %xmm0
vmovsd 0x9b0(%rsp), %xmm1
vdivsd %xmm1, %xmm0, %xmm1
vroundsd $0xa, %xmm1, %xmm0, %xmm0
vcvttsd2si %xmm0, %rax
movl %eax, 0x2d4(%rsp)
movl 0x2d4(%rsp), %eax
decl %eax
movl %eax, %esi
leaq 0x310(%rsp), %rdi
callq 0x51e0
movq %rax, %rdi
movl 0x2e4(%rsp), %eax
incl %eax
movl %eax, 0x2d0(%rsp)
leaq 0x2d0(%rsp), %rsi
callq 0x5200
jmp 0x36b3
jmp 0x36b5
movq 0x2e8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x2e8(%rsp)
jmp 0x354f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xcd8(%rsp)
movl %eax, 0xcd4(%rsp)
leaq 0x30f(%rsp), %rdi
callq 0x51c0
jmp 0x407c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xcd8(%rsp)
movl %eax, 0xcd4(%rsp)
jmp 0x3fb9
movl 0xa7c(%rsp), %eax
movl %eax, %esi
leaq 0xca8(%rsp), %rdi
callq 0x5230
movq %rax, %rsi
leaq 0x2b0(%rsp), %rdi
callq 0x20d0
jmp 0x3735
leaq 0x560(%rsp), %rdi
leaq 0x358(%rsp), %rsi
movl $0x8, %edx
callq 0x2250
jmp 0x3751
leaq 0x560(%rsp), %rdi
leaq 0x350(%rsp), %rsi
movl $0x8, %edx
callq 0x2250
jmp 0x376d
leaq 0x760(%rsp), %rdi
leaq 0x2b0(%rsp), %rsi
callq 0x2170
movq %rax, 0xa8(%rsp)
jmp 0x378c
movq 0xa8(%rsp), %rdi
xorl %esi, %esi
callq 0x21e0
jmp 0x379d
leaq 0x760(%rsp), %rdi
callq 0x2150
movq %rdx, 0x98(%rsp)
movq %rax, 0xa0(%rsp)
jmp 0x37bc
movq 0x98(%rsp), %rax
movq 0xa0(%rsp), %rcx
movq %rcx, 0x2a0(%rsp)
movq %rax, 0x2a8(%rsp)
leaq 0x2a0(%rsp), %rdi
callq 0x4c80
movq %rax, 0x90(%rsp)
jmp 0x37f3
movq 0x90(%rsp), %rax
movq %rax, 0x358(%rsp)
movq $0x0, 0x298(%rsp)
movq $0x0, 0x290(%rsp)
movq 0x290(%rsp), %rax
movq %rax, 0x88(%rsp)
leaq 0x310(%rsp), %rdi
callq 0x5250
movq %rax, %rcx
movq 0x88(%rsp), %rax
cmpq %rcx, %rax
jae 0x3b11
movq 0x290(%rsp), %rsi
leaq 0x310(%rsp), %rdi
callq 0x51e0
movq %rax, %rdi
callq 0x4ef0
cmpq $0x0, %rax
jne 0x388d
jmp 0x3aac
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xcd8(%rsp)
movl %eax, 0xcd4(%rsp)
jmp 0x3fac
movq 0x290(%rsp), %rsi
leaq 0x310(%rsp), %rdi
callq 0x51e0
movq %rax, %rdi
callq 0x4ef0
leaq 0x400(%rax,%rax), %rax
movq %rax, 0x78(%rsp)
leaq 0x277(%rsp), %rdi
movq %rdi, 0x80(%rsp)
callq 0x5270
movq 0x78(%rsp), %rsi
movq 0x80(%rsp), %rdx
leaq 0x278(%rsp), %rdi
callq 0x5280
jmp 0x38e8
leaq 0x277(%rsp), %rdi
callq 0x5300
leaq 0x278(%rsp), %rdi
callq 0x4ef0
shlq $0x2, %rax
movq %rax, 0x268(%rsp)
movq 0x290(%rsp), %rsi
leaq 0x310(%rsp), %rdi
movq %rdi, 0x68(%rsp)
callq 0x51e0
movq %rax, %rdi
callq 0x4c20
movq 0x68(%rsp), %rdi
movq %rax, 0x70(%rsp)
movq 0x290(%rsp), %rsi
callq 0x51e0
movq %rax, %rdi
callq 0x4ef0
movq 0x70(%rsp), %rdi
movq %rax, %rsi
callq 0x4130
jmp 0x395e
movq 0xcc8(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x290(%rsp), %rsi
leaq 0x310(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0x51e0
movq %rax, %rdi
callq 0x4c20
movq 0x48(%rsp), %rdi
movq %rax, 0x58(%rsp)
movq 0x290(%rsp), %rsi
callq 0x51e0
movq %rax, %rdi
callq 0x4ef0
movq %rax, 0x60(%rsp)
leaq 0x278(%rsp), %rdi
callq 0x4c20
movq 0x50(%rsp), %rdi
movq 0x58(%rsp), %rsi
movq 0x60(%rsp), %rdx
movq %rax, %rcx
movq (%rdi), %rax
movq 0x10(%rax), %rax
leaq 0x268(%rsp), %r8
callq *%rax
jmp 0x39e3
movq 0x268(%rsp), %rax
movl 0xcc4(%rsp), %ecx
leaq -0x1(%rax,%rcx), %rax
xorl %edx, %edx
divq %rcx
imulq %rcx, %rax
movq %rax, 0x260(%rsp)
movq 0x260(%rsp), %rax
shrq $0x2, %rax
movq %rax, 0x258(%rsp)
movq 0x258(%rsp), %rsi
leaq 0x278(%rsp), %rdi
callq 0x4b80
jmp 0x3a33
movl 0x290(%rsp), %eax
incl %eax
movw %ax, 0x256(%rsp)
movq 0x290(%rsp), %rsi
leaq 0x310(%rsp), %rdi
callq 0x51e0
movq %rax, %rdi
callq 0x4ef0
movl %eax, 0x250(%rsp)
leaq 0x2f0(%rsp), %rdi
leaq 0x256(%rsp), %rsi
leaq 0x250(%rsp), %rdx
leaq 0x268(%rsp), %rcx
leaq 0x260(%rsp), %r8
leaq 0x278(%rsp), %r9
callq 0x5310
jmp 0x3a9f
leaq 0x278(%rsp), %rdi
callq 0x53d0
movq 0x290(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x290(%rsp)
jmp 0x381b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xcd8(%rsp)
movl %eax, 0xcd4(%rsp)
leaq 0x277(%rsp), %rdi
callq 0x5300
jmp 0x3fac
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xcd8(%rsp)
movl %eax, 0xcd4(%rsp)
leaq 0x278(%rsp), %rdi
callq 0x53d0
jmp 0x3fac
leaq 0x2f0(%rsp), %rdi
callq 0x5430
movq %rax, 0x298(%rsp)
leaq 0x560(%rsp), %rdi
leaq 0x298(%rsp), %rsi
movl $0x8, %edx
callq 0x2250
jmp 0x3b42
movl $0x16, 0x24c(%rsp)
movq 0x298(%rsp), %rax
shlq $0x3, %rax
addq 0x350(%rsp), %rax
movq %rax, 0x240(%rsp)
movq $0x0, 0x238(%rsp)
movq 0x238(%rsp), %rax
cmpq 0x298(%rsp), %rax
jae 0x3bd0
leaq 0x360(%rsp), %rdi
leaq 0x240(%rsp), %rsi
movl $0x8, %edx
callq 0x2250
jmp 0x3ba3
movl 0x24c(%rsp), %eax
addq 0x240(%rsp), %rax
movq %rax, 0x240(%rsp)
movq 0x238(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x238(%rsp)
jmp 0x3b75
movl 0x24c(%rsp), %eax
movq 0x298(%rsp), %rcx
addq $0x1, %rcx
imulq %rcx, %rax
movq 0x298(%rsp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
addq 0x350(%rsp), %rax
movq %rax, 0x230(%rsp)
movq 0x230(%rsp), %rax
movl 0xcc4(%rsp), %ecx
addq %rcx, %rax
subq $0x1, %rax
movl 0xcc4(%rsp), %ecx
xorl %edx, %edx
divq %rcx
movl 0xcc4(%rsp), %ecx
imulq %rcx, %rax
movq %rax, 0x228(%rsp)
movq 0x228(%rsp), %rax
subq 0x230(%rsp), %rax
movq %rax, 0x220(%rsp)
movq 0x228(%rsp), %rax
movq %rax, 0x230(%rsp)
movq $0x0, 0x218(%rsp)
movq 0x218(%rsp), %rax
movq %rax, 0x40(%rsp)
leaq 0x2f0(%rsp), %rdi
callq 0x5430
movq %rax, %rcx
movq 0x40(%rsp), %rax
cmpq %rcx, %rax
jae 0x3dd3
movq 0x218(%rsp), %rsi
leaq 0x2f0(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x5450
movq 0x38(%rsp), %rdi
movw (%rax), %ax
movw %ax, 0x216(%rsp)
movq 0x218(%rsp), %rsi
callq 0x5450
movq 0x38(%rsp), %rdi
movl 0x4(%rax), %eax
movl %eax, 0x210(%rsp)
movq 0x218(%rsp), %rsi
callq 0x5450
movq 0x38(%rsp), %rdi
movq 0x8(%rax), %rax
movq %rax, 0x208(%rsp)
movq 0x218(%rsp), %rsi
callq 0x5450
movq 0x10(%rax), %rax
movq %rax, 0x200(%rsp)
movq 0x230(%rsp), %rax
movq 0x208(%rsp), %rcx
addq %rcx, %rax
movq %rax, 0x1f8(%rsp)
leaq 0x360(%rsp), %rdi
leaq 0x216(%rsp), %rsi
movl $0x2, %edx
callq 0x2250
jmp 0x3d4e
leaq 0x360(%rsp), %rdi
leaq 0x230(%rsp), %rsi
movl $0x8, %edx
callq 0x2250
jmp 0x3d6a
leaq 0x360(%rsp), %rdi
leaq 0x1f8(%rsp), %rsi
movl $0x8, %edx
callq 0x2250
jmp 0x3d86
leaq 0x360(%rsp), %rdi
leaq 0x210(%rsp), %rsi
movl $0x4, %edx
callq 0x2250
jmp 0x3da2
movq 0x200(%rsp), %rax
addq 0x230(%rsp), %rax
movq %rax, 0x230(%rsp)
movq 0x218(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x218(%rsp)
jmp 0x3c6f
vxorps %xmm0, %xmm0, %xmm0
vmovdqa %xmm0, 0x1e0(%rsp)
movq $0x0, 0x1ee(%rsp)
movl 0x24c(%rsp), %eax
movl %eax, %edx
leaq 0x360(%rsp), %rdi
leaq 0x1e0(%rsp), %rsi
callq 0x2250
jmp 0x3e0c
cmpq $0x0, 0x220(%rsp)
je 0x3e7a
cmpq $0x16, 0x220(%rsp)
jbe 0x3e59
movq 0x3061bf(%rip), %rdi    # 0x309fe8
leaq 0x2ff2af(%rip), %rsi    # 0x3030df
callq 0x2180
movq %rax, 0x30(%rsp)
jmp 0x3e3c
movq 0x30(%rsp), %rdi
movq 0x306170(%rip), %rsi    # 0x309fb8
callq 0x21b0
jmp 0x3e4f
movl $0x1, %edi
callq 0x2200
movq 0x220(%rsp), %rdx
leaq 0x360(%rsp), %rdi
leaq 0x1e0(%rsp), %rsi
callq 0x2250
jmp 0x3e78
jmp 0x3e7a
movq $0x0, 0x1d8(%rsp)
movq 0x1d8(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0x2f0(%rsp), %rdi
callq 0x5430
movq %rax, %rcx
movq 0x28(%rsp), %rax
cmpq %rcx, %rax
jae 0x3f1d
movq 0x1d8(%rsp), %rsi
leaq 0x2f0(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x5450
movq %rax, %rdi
addq $0x18, %rdi
callq 0x4c20
movq 0x18(%rsp), %rdi
movq %rax, 0x20(%rsp)
movq 0x1d8(%rsp), %rsi
callq 0x5450
movq 0x20(%rsp), %rsi
movq 0x10(%rax), %rdx
leaq 0x360(%rsp), %rdi
callq 0x2250
jmp 0x3f02
jmp 0x3f04
movq 0x1d8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x1d8(%rsp)
jmp 0x3e86
leaq 0x360(%rsp), %rdi
callq 0x2150
movq %rdx, 0x8(%rsp)
movq %rax, 0x10(%rsp)
jmp 0x3f36
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rcx, 0x1c8(%rsp)
movq %rax, 0x1d0(%rsp)
leaq 0x1c8(%rsp), %rdi
callq 0x4c80
movq %rax, (%rsp)
jmp 0x3f63
movq (%rsp), %rax
movq %rax, 0x350(%rsp)
movl 0xa7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa7c(%rsp)
leaq 0x2b0(%rsp), %rdi
callq 0x2120
leaq 0x2f0(%rsp), %rdi
callq 0x5470
leaq 0x310(%rsp), %rdi
callq 0x54d0
jmp 0x3353
leaq 0x2b0(%rsp), %rdi
callq 0x2120
leaq 0x2f0(%rsp), %rdi
callq 0x5470
leaq 0x310(%rsp), %rdi
callq 0x54d0
jmp 0x407c
leaq 0x360(%rsp), %rdi
callq 0x22a0
leaq 0x560(%rsp), %rdi
callq 0x22a0
leaq 0x760(%rsp), %rdi
callq 0x22a0
leaq 0x9b8(%rsp), %rdi
callq 0x53d0
leaq 0x9d0(%rsp), %rdi
callq 0x53d0
leaq 0x9e8(%rsp), %rdi
callq 0x53d0
leaq 0xa18(%rsp), %rdi
callq 0x2120
leaq 0xa38(%rsp), %rdi
callq 0x2120
leaq 0xa58(%rsp), %rdi
callq 0x2120
leaq 0xa80(%rsp), %rdi
callq 0x2120
leaq 0xaa0(%rsp), %rdi
callq 0x2070
leaq 0xca8(%rsp), %rdi
callq 0x5530
addq $0xd08, %rsp            # imm = 0xD08
retq
leaq 0x360(%rsp), %rdi
callq 0x22a0
leaq 0x560(%rsp), %rdi
callq 0x22a0
leaq 0x760(%rsp), %rdi
callq 0x22a0
leaq 0x9b8(%rsp), %rdi
callq 0x53d0
leaq 0x9d0(%rsp), %rdi
callq 0x53d0
leaq 0x9e8(%rsp), %rdi
callq 0x53d0
leaq 0xa18(%rsp), %rdi
callq 0x2120
leaq 0xa38(%rsp), %rdi
callq 0x2120
leaq 0xa58(%rsp), %rdi
callq 0x2120
leaq 0xa80(%rsp), %rdi
callq 0x2120
leaq 0xaa0(%rsp), %rdi
callq 0x2070
leaq 0xca8(%rsp), %rdi
callq 0x5530
movq 0xcd8(%rsp), %rdi
callq 0x2320
nopw %cs:(%rax,%rax)