// File: /workspace/github_cpp_makefile_star2_O1/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 $0x98, %rsp
movl %ecx, 0x2c(%rsp)
movq %rdx, %r12
movq %rsi, %r15
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
leaq 0x10(%rdi), %rbx
movq %rbx, (%rdi)
leaq 0x3896(%rip), %rdx      # 0x61f2
movq %rdi, (%rsp)
movq %rdx, %rsi
callq 0x42f6
leaq 0x8(%r15), %r14
movq %r14, %rdi
callq 0x2320
testl %eax, %eax
movq %rbx, 0x70(%rsp)
jne 0x2bc7
cmpb $0x0, (%r15)
je 0x2bbd
leaq 0x60(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq $0x0, -0x8(%rbx)
movw $0x0, (%rbx)
movq (%r12), %rdi
movq 0x8(%r12), %rsi
leaq 0x50(%rsp), %rdx
callq 0x3b5b
movq 0x58(%rsp), %rax
testq %rax, %rax
je 0x2b03
movq 0x50(%rsp), %r12
leaq (%r12,%rax,2), %rcx
leaq 0x80(%rsp), %rbx
movq %rcx, 0x78(%rsp)
movq %rbx, %rdi
movq %r15, %r13
movq %r15, %rsi
movq %r12, %rdx
movl 0x2c(%rsp), %r8d
callq 0x27ec
movq 0x80(%rsp), %rbp
movq 0x88(%rsp), %r15
cmpq %r15, %rbp
sete %al
movq %rbp, %rbx
addq $0x48, %rbx
cmpq %r15, %rbx
sete %cl
orb %al, %cl
je 0x2a90
movq %rbp, %rax
movq (%rax), %rcx
movq 0x18(%rax), %rax
movq 0x8(%rax), %rbx
subq (%rax), %rbx
cmpl $0x0, (%rcx)
movq %r13, %r15
leaq 0x8(%rsp), %r13
je 0x2a6d
movq (%rax), %rsi
movq 0x8(%rax), %rdx
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %r13, %rdi
callq 0x4386
leaq 0x30(%rsp), %rdi
movq %r13, %rsi
callq 0x376e
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x2a6d
callq 0x2240
addq %rbx, %r12
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
callq 0x3746
movq 0x78(%rsp), %rcx
cmpq %rcx, %r12
jne 0x29d8
jmp 0x2b03
movq %r14, %rdi
callq 0x21d0
movq 0x30(%rbp), %rcx
movq 0x30(%rbx), %rdx
movq %rbx, %rax
cmpq %rdx, %rcx
jb 0x2ac2
jbe 0x2aaf
movq %rbp, %rax
jmp 0x2ac2
vmovss 0x38(%rbp), %xmm0
vmovss 0x38(%rbx), %xmm1
vucomiss %xmm1, %xmm0
movq %rbx, %rax
jae 0x2ad3
addq $0x48, %rbx
movq %rax, %rbp
cmpq %r15, %rbx
jne 0x2a90
jmp 0x2a18
vucomiss %xmm0, %xmm1
jb 0x2aaa
vmovss 0x3c(%rbp), %xmm0
vmovss 0x3c(%rbx), %xmm1
vucomiss %xmm1, %xmm0
movq %rbx, %rax
jb 0x2ac2
vucomiss %xmm0, %xmm1
jb 0x2aaa
vmovss 0x40(%rbp), %xmm0
vucomiss 0x40(%rbx), %xmm0
movq %rbx, %rax
jae 0x2aaa
jmp 0x2ac2
movq 0x30(%rsp), %r13
movq 0x38(%rsp), %rbp
cmpq %rbp, %r13
je 0x2b81
leaq 0x18(%rsp), %rbx
leaq 0x8(%rsp), %r15
leaq 0x37d9(%rip), %r12      # 0x62fc
movq %rbx, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
movb $0x0, 0x18(%rsp)
movq (%r13), %rdi
movq 0x8(%r13), %rax
leaq (%rdi,%rax,2), %rsi
movq %r15, %rdx
callq 0x3e2c
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq (%rsp), %rdi
callq 0x20c0
movq 0x8(%rsp), %rdi
cmpq %rbx, %rdi
je 0x2b6c
callq 0x2240
movq (%rsp), %rdi
movq %r12, %rsi
callq 0x23f0
addq $0x20, %r13
cmpq %rbp, %r13
jne 0x2b23
movq %r14, %rdi
callq 0x21d0
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x2b9d
callq 0x2240
leaq 0x30(%rsp), %rdi
callq 0x37c6
movq (%rsp), %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x2420
jmp 0x2b9d
movl %eax, %edi
callq 0x2170
movq %rax, %r14
movq 0x50(%rsp), %rdi
cmpq %rbx, %rdi
jne 0x2c3c
jmp 0x2c41
movq %rax, %r14
jmp 0x2c41
movq %rax, %r14
jmp 0x2c54
movq %rax, %r14
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x2c03
callq 0x2240
jmp 0x2c03
movq %rax, %r14
leaq 0x80(%rsp), %rdi
callq 0x3746
jmp 0x2c2d
jmp 0x2c14
movq %rax, %r14
movq 0x8(%rsp), %rdi
cmpq %rbx, %rdi
je 0x2c2d
callq 0x2240
jmp 0x2c2d
jmp 0x2c2a
movq %rax, %r14
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x2c41
callq 0x2240
movq (%rsp), %rax
movq (%rax), %rdi
cmpq 0x70(%rsp), %rdi
je 0x2c54
callq 0x2240
leaq 0x30(%rsp), %rdi
callq 0x37c6
movq %r14, %rdi
callq 0x2390