// File: /workspace/github_cpp_makefile_star2_O2/jkom-cloud[P]mmseg/src/Mmseg.cpp
// Function: mmsegSpace::MMSeg::segment(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, int)
std::string MMSeg::segment(const std::string& ss, int depth) {
    std::vector<String> retv;
    std::string ret = "";
    mtx.lock();
    if (is_inited == 0) {
      fprintf(stderr, "PLease load dictionary first!\n");
      mtx.unlock();
      return ret;
    }

    String s = TransCode::from_utf8(ss);

    auto start = s.begin(), end = s.end();
    while (start != end) {
      auto chunks = get_chunks(start, end, depth);
      //std::cout<<"Chunks Length:"<<chunks.size()<<std::endl;

      auto best = std::max_element(chunks.begin(), chunks.end(), [&](const Chunk& x, const Chunk& y) {
        mtx.unlock();
        return std::tie(x.length_, x.mean_, x.var_, x.degree_) < std::tie(y.length_, y.mean_, y.var_, y.degree_);
      });

      auto& word = best->words_.front();// 取出分词结果最好的第一个分词结果
      start += length(word);// 处理后续字串

      auto& inDict = best->inDict_.front();
      if (inDict != 0)
        retv.emplace_back(String(word.first, word.second));

#ifdef DEBUG_LEVEL // 输出计算过程
      static int times;
      if(s.begin() == word.first)
        times = 1;//处理一个新的字串

      std::cout<<"Step: "<<times<<std::endl;

      for(auto &item:chunks) {
        cout<<item.to_string()<<std::endl;
      }

      //std::cout<<"Best one is: "<<TransCode::to_utf8(retv.back())<<std::endl;
      std::cout<<"Best one is: "<<TransCode::to_utf8(String(word.first, word.second))<<std::endl;
      ++times;
#endif
    }// while
#ifdef DEBUG_LEVEL
    std::cout<<"Result is: ";
#endif

    for(auto &m : retv) {
      ret += TransCode::to_utf8(m);
      ret += " ";
    }

    mtx.unlock();
    return ret;
  }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
andq $0x0, 0x20(%rsp)
vpxor %xmm0, %xmm0, %xmm0
movl %ecx, 0x4(%rsp)
movq %rdx, %rbx
movq %rsi, %r12
movq %rdi, %rbp
vmovdqa %xmm0, 0x10(%rsp)
leaq 0x3ac7(%rip), %rsi      # 0x613a
leaq 0x40(%rsp), %rdx
callq 0x20e0
leaq 0x8(%r12), %r14
movq %r14, %rdi
callq 0x319a
cmpb $0x0, (%r12)
je 0x2846
leaq 0x78(%rsp), %rdi
movq %rbx, %rsi
callq 0x31ae
movq 0x78(%rsp), %rbx
movq 0x80(%rsp), %rax
leaq 0x60(%rsp), %r13
leaq 0x40(%rsp), %r15
movq %rbp, 0x8(%rsp)
movq %r12, 0x30(%rsp)
leaq (%rbx,%rax,2), %rcx
movq %rcx, 0x28(%rsp)
cmpq %rcx, %rbx
je 0x27cb
movl 0x4(%rsp), %r8d
movq %r13, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x254c
movq 0x60(%rsp), %r12
movq 0x68(%rsp), %r13
cmpq %r13, %r12
je 0x2762
movq %rbx, 0x38(%rsp)
movq %r12, %rbx
leaq 0x48(%r12), %rbp
cmpq %r13, %rbp
je 0x275a
movq %r14, %rdi
callq 0x21a0
vpbroadcastq %rbx, %ymm0
vpaddq 0x3901(%rip), %ymm0, %ymm0 # 0x6020
vmovdqu %ymm0, 0x40(%rsp)
vpbroadcastq %r12, %ymm0
vpaddq 0x390d(%rip), %ymm0, %ymm0 # 0x6040
vmovdqu %ymm0, 0x98(%rsp)
leaq 0x98(%rsp), %rsi
movq %r15, %rdi
vzeroupper
callq 0x4a7e
testb %al, %al
movq %rbp, %r12
cmovneq %rbp, %rbx
jmp 0x26ff
movq %rbx, %r12
movq 0x38(%rsp), %rbx
movq 0x18(%r12), %rcx
movq (%r12), %rax
movq (%rcx), %r13
movq 0x8(%rcx), %rbp
cmpl $0x0, (%rax)
je 0x27a4
leaq 0x50(%rsp), %rax
movq %rax, 0x40(%rsp)
movq %r15, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x3f0e
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x31f0
movq %r15, %rdi
callq 0x321c
subq %r13, %rbp
leaq 0x60(%rsp), %r13
movq %r13, %rdi
addq %rbp, %rbx
callq 0x3178
movq 0x8(%rsp), %rbp
movq 0x30(%rsp), %r12
movq 0x28(%rsp), %rcx
jmp 0x26cc
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %r13
leaq 0x40(%rsp), %r15
leaq 0x3a39(%rip), %r12      # 0x621a
cmpq %r13, %rbx
je 0x2815
movq %r15, %rdi
movq %rbx, %rsi
callq 0x322c
movq %rbp, %rdi
movq %r15, %rsi
callq 0x23b0
movq %r15, %rdi
callq 0x2100
movq %rbp, %rdi
movq %r12, %rsi
callq 0x2390
addq $0x20, %rbx
jmp 0x27e1
movq %r14, %rdi
callq 0x21a0
leaq 0x78(%rsp), %rdi
callq 0x321c
leaq 0x10(%rsp), %rdi
callq 0x3274
movq %rbp, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x7773(%rip), %rax      # 0x9fc0
leaq 0x38c8(%rip), %rdi      # 0x611c
movq (%rax), %rcx
pushq $0x1e
popq %rsi
pushq $0x1
popq %rdx
callq 0x23a0
movq %r14, %rdi
callq 0x21a0
jmp 0x2827
jmp 0x286e
movq %rax, %rbx
jmp 0x28bd
movq %rax, %rbx
jmp 0x28c5
leaq 0x40(%rsp), %rdi
movq %rax, %rbx
callq 0x321c
jmp 0x28a4
jmp 0x28a1
jmp 0x289c
leaq 0x40(%rsp), %rdi
movq %rax, %rbx
callq 0x2100
jmp 0x28b3
jmp 0x289c
movq %rax, %rbx
jmp 0x28b3
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x3178
movq 0x8(%rsp), %rbp
leaq 0x78(%rsp), %rdi
callq 0x321c
movq %rbp, %rdi
callq 0x2100
leaq 0x10(%rsp), %rdi
callq 0x3274
movq %rbx, %rdi
callq 0x2350
nop