// File: jkom-cloud[P]mmseg/src/Mmseg.cpp
// Function: mmsegSpace::MMSeg::load(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
int mmsegSpace::MMSeg::load(const std::string& dict, const std::string& char_freqs) {
    mtx.lock();
    if (is_inited) {// 已经加载词典
      fprintf(stderr, "Dict has been loaded!");
      mtx.unlock();
      return 0;
    }

    std::ifstream ifs(dict);
    if (!ifs.is_open()) {
      fprintf(stderr, "open %s failed!!", dict.c_str());
      mtx.unlock();
      return -1;
    }

    while (ifs.good()) {
      std::string line;
      if (! std::getline(ifs, line)) break;
      auto word = TransCode::from_utf8(trim(line));
      dict_.add(word);
    }
    ifs.close();

    if (char_freqs.empty()) {
      is_inited = true;
      mtx.unlock();
      return 0;
    }

    std::ifstream fin(char_freqs);
    if (!fin.is_open()) {
      fprintf(stderr, "open %s failed!!", char_freqs.c_str());
      mtx.unlock();
      return -1;
    }

    while (fin.good()) {
      std::string line;
      if (! std::getline(fin, line)) break;
      auto s = trim(line);
      size_t pos = s.find(' ');
      if (pos != std::string::npos) {
        auto word = TransCode::from_utf8(s.substr(0, pos));
        int freq = std::stoi(s.substr(pos+1));
        char_freqs_.emplace(word[0], freq);
      }
    }
    fin.close();

    std::cout << "Loaded Dict: " << dict_.size() << ", Freq: " << char_freqs_.size() << std::endl;
    is_inited = true;
    mtx.unlock();
    return 0;
  }

// Assembly:
subq $0x618, %rsp            # imm = 0x618
movq %rdi, 0x608(%rsp)
movq %rsi, 0x600(%rsp)
movq %rdx, 0x5f8(%rsp)
movq 0x608(%rsp), %rdi
movq %rdi, 0xd0(%rsp)
addq $0x8, %rdi
callq 0x54f0
movq 0xd0(%rsp), %rax
testb $0x1, (%rax)
je 0x3f7e
movq 0x15074(%rip), %rax     # 0x18fc0
movq (%rax), %rdi
leaq 0xd188(%rip), %rsi      # 0x110de
movb $0x0, %al
callq 0x3380
movq 0xd0(%rsp), %rdi
addq $0x8, %rdi
callq 0x5520
movl $0x0, 0x614(%rsp)
jmp 0x46c8
movq 0x600(%rsp), %rsi
leaq 0x3f0(%rsp), %rdi
movq %rdi, 0xc0(%rsp)
movl $0x8, %edx
callq 0x3400
movq 0xc0(%rsp), %rdi
callq 0x33f0
movb %al, 0xcf(%rsp)
jmp 0x3fb6
movb 0xcf(%rsp), %al
testb $0x1, %al
jne 0x4046
jmp 0x3fc7
movq 0x14ff2(%rip), %rax     # 0x18fc0
movq (%rax), %rax
movq %rax, 0xb8(%rsp)
movq 0x600(%rsp), %rdi
callq 0x30f0
movq 0xb8(%rsp), %rdi
movq %rax, %rdx
leaq 0xd0fc(%rip), %rsi      # 0x110f4
xorl %eax, %eax
callq 0x3380
movq 0xd0(%rsp), %rdi
addq $0x8, %rdi
callq 0x5520
jmp 0x4012
movl $0xffffffff, 0x614(%rsp) # imm = 0xFFFFFFFF
movl $0x1, 0x3e0(%rsp)
jmp 0x46aa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3e8(%rsp)
movl %eax, 0x3e4(%rsp)
jmp 0x46b9
jmp 0x4048
movq 0x3f0(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x3f0(%rsp,%rax), %rdi
callq 0x3450
movb %al, 0xb7(%rsp)
jmp 0x406a
movb 0xb7(%rsp), %al
testb $0x1, %al
jne 0x407a
jmp 0x41b6
leaq 0x3c0(%rsp), %rdi
movq %rdi, 0xa0(%rsp)
callq 0x3370
movq 0xa0(%rsp), %rsi
leaq 0x3f0(%rsp), %rdi
callq 0x3050
movq %rax, 0xa8(%rsp)
jmp 0x40ae
movq 0xa8(%rsp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x3110
movb %al, 0x9f(%rsp)
jmp 0x40ce
movb 0x9f(%rsp), %al
testb $0x1, %al
jne 0x40db
jmp 0x4101
movl $0x3, 0x3e0(%rsp)
jmp 0x4162
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3e8(%rsp)
movl %eax, 0x3e4(%rsp)
jmp 0x41a4
leaq 0x3c0(%rsp), %rdi
callq 0x5b20
movq %rax, 0x90(%rsp)
jmp 0x4118
movq 0x90(%rsp), %rsi
leaq 0x3a0(%rsp), %rdi
callq 0x5530
jmp 0x412f
movq 0xd0(%rsp), %rdi
addq $0x68, %rdi
leaq 0x3a0(%rsp), %rsi
callq 0x5b40
jmp 0x414a
leaq 0x3a0(%rsp), %rdi
callq 0x5900
movl $0x0, 0x3e0(%rsp)
leaq 0x3c0(%rsp), %rdi
callq 0x3160
movl 0x3e0(%rsp), %eax
testl %eax, %eax
je 0x417e
jmp 0x417c
jmp 0x41b6
jmp 0x4048
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3e8(%rsp)
movl %eax, 0x3e4(%rsp)
leaq 0x3a0(%rsp), %rdi
callq 0x5900
leaq 0x3c0(%rsp), %rdi
callq 0x3160
jmp 0x46b9
leaq 0x3f0(%rsp), %rdi
callq 0x3060
jmp 0x41c5
movq 0x5f8(%rsp), %rdi
callq 0x3430
testb $0x1, %al
jne 0x41d8
jmp 0x4209
movq 0xd0(%rsp), %rdi
movb $0x1, (%rdi)
addq $0x8, %rdi
callq 0x5520
jmp 0x41ee
movl $0x0, 0x614(%rsp)
movl $0x1, 0x3e0(%rsp)
jmp 0x46aa
movq 0x5f8(%rsp), %rsi
leaq 0x198(%rsp), %rdi
movl $0x8, %edx
callq 0x3400
jmp 0x4225
leaq 0x198(%rsp), %rdi
callq 0x33f0
movb %al, 0x8f(%rsp)
jmp 0x423b
movb 0x8f(%rsp), %al
testb $0x1, %al
jne 0x42cb
jmp 0x424c
movq 0x14d6d(%rip), %rax     # 0x18fc0
movq (%rax), %rax
movq %rax, 0x80(%rsp)
movq 0x5f8(%rsp), %rdi
callq 0x30f0
movq 0x80(%rsp), %rdi
movq %rax, %rdx
leaq 0xce77(%rip), %rsi      # 0x110f4
xorl %eax, %eax
callq 0x3380
movq 0xd0(%rsp), %rdi
addq $0x8, %rdi
callq 0x5520
jmp 0x4297
movl $0xffffffff, 0x614(%rsp) # imm = 0xFFFFFFFF
movl $0x1, 0x3e0(%rsp)
jmp 0x468c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3e8(%rsp)
movl %eax, 0x3e4(%rsp)
jmp 0x469b
jmp 0x42cd
movq 0x198(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x198(%rsp,%rax), %rdi
callq 0x3450
movb %al, 0x7f(%rsp)
jmp 0x42ec
movb 0x7f(%rsp), %al
testb $0x1, %al
jne 0x42f9
jmp 0x45b9
leaq 0x178(%rsp), %rdi
movq %rdi, 0x68(%rsp)
callq 0x3370
movq 0x68(%rsp), %rsi
leaq 0x198(%rsp), %rdi
callq 0x3050
movq %rax, 0x70(%rsp)
jmp 0x4324
movq 0x70(%rsp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x3110
movb %al, 0x67(%rsp)
jmp 0x433e
movb 0x67(%rsp), %al
testb $0x1, %al
jne 0x4348
jmp 0x4371
movl $0x5, 0x3e0(%rsp)
jmp 0x4579
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3e8(%rsp)
movl %eax, 0x3e4(%rsp)
jmp 0x45a7
leaq 0x178(%rsp), %rdi
callq 0x5b20
movq %rax, 0x58(%rsp)
jmp 0x4385
movq 0x58(%rsp), %rsi
leaq 0x158(%rsp), %rdi
callq 0x3100
jmp 0x4399
leaq 0x158(%rsp), %rdi
movl $0x20, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x3330
movq %rax, 0x150(%rsp)
cmpq $-0x1, 0x150(%rsp)
je 0x4561
movq 0x150(%rsp), %rcx
xorl %eax, %eax
movl %eax, %edx
leaq 0x110(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0x32c0
jmp 0x43e9
leaq 0x130(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x5530
jmp 0x4400
leaq 0x110(%rsp), %rdi
callq 0x3160
movq 0x150(%rsp), %rdx
incq %rdx
leaq 0xe8(%rsp), %rdi
leaq 0x158(%rsp), %rsi
movq $-0x1, %rcx
callq 0x32c0
jmp 0x4436
xorl %eax, %eax
movl %eax, %esi
leaq 0xe8(%rsp), %rdi
movl $0xa, %edx
callq 0x5ca0
movl %eax, 0x54(%rsp)
jmp 0x4452
leaq 0xe8(%rsp), %rdi
callq 0x3160
movl 0x54(%rsp), %ecx
movq 0xd0(%rsp), %rax
movl %ecx, 0x10c(%rsp)
addq $0x30, %rax
movq %rax, 0x40(%rsp)
xorl %eax, %eax
movl %eax, %esi
leaq 0x130(%rsp), %rdi
callq 0x5d40
movq %rax, 0x48(%rsp)
jmp 0x4493
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rdi
leaq 0x10c(%rsp), %rdx
callq 0x5cf0
movb %dl, 0x37(%rsp)
movq %rax, 0x38(%rsp)
jmp 0x44b5
movb 0x37(%rsp), %al
movq 0x38(%rsp), %rcx
movq %rcx, 0xd8(%rsp)
movb %al, 0xe0(%rsp)
leaq 0x130(%rsp), %rdi
callq 0x5900
jmp 0x4561
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3e8(%rsp)
movl %eax, 0x3e4(%rsp)
jmp 0x459a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3e8(%rsp)
movl %eax, 0x3e4(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x3160
jmp 0x459a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3e8(%rsp)
movl %eax, 0x3e4(%rsp)
jmp 0x4552
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3e8(%rsp)
movl %eax, 0x3e4(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x3160
leaq 0x130(%rsp), %rdi
callq 0x5900
jmp 0x459a
leaq 0x158(%rsp), %rdi
callq 0x3160
movl $0x0, 0x3e0(%rsp)
leaq 0x178(%rsp), %rdi
callq 0x3160
movl 0x3e0(%rsp), %eax
testl %eax, %eax
je 0x4595
jmp 0x4593
jmp 0x45b9
jmp 0x42cd
leaq 0x158(%rsp), %rdi
callq 0x3160
leaq 0x178(%rsp), %rdi
callq 0x3160
jmp 0x469b
leaq 0x198(%rsp), %rdi
callq 0x3060
jmp 0x45c8
movq 0x149e9(%rip), %rdi     # 0x18fb8
leaq 0xcb2f(%rip), %rsi      # 0x11105
callq 0x32a0
movq %rax, 0x28(%rsp)
jmp 0x45e2
movq 0xd0(%rsp), %rdi
addq $0x68, %rdi
callq 0x5d80
movq %rax, 0x20(%rsp)
jmp 0x45fa
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdi
callq 0x3090
movq %rax, 0x18(%rsp)
jmp 0x4610
movq 0x18(%rsp), %rdi
leaq 0xcaf7(%rip), %rsi      # 0x11113
callq 0x32a0
movq %rax, 0x10(%rsp)
jmp 0x4628
movq 0xd0(%rsp), %rdi
addq $0x30, %rdi
callq 0x5d90
movq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x3090
movq %rax, 0x8(%rsp)
jmp 0x464d
movq 0x8(%rsp), %rdi
movq 0x1494f(%rip), %rsi     # 0x18fa8
callq 0x32d0
jmp 0x4660
movq 0xd0(%rsp), %rdi
movb $0x1, (%rdi)
addq $0x8, %rdi
callq 0x5520
jmp 0x4676
movl $0x0, 0x614(%rsp)
movl $0x1, 0x3e0(%rsp)
leaq 0x198(%rsp), %rdi
callq 0x30a0
jmp 0x46aa
leaq 0x198(%rsp), %rdi
callq 0x30a0
jmp 0x46b9
leaq 0x3f0(%rsp), %rdi
callq 0x30a0
jmp 0x46c8
leaq 0x3f0(%rsp), %rdi
callq 0x30a0
jmp 0x46d7
movl 0x614(%rsp), %eax
addq $0x618, %rsp            # imm = 0x618
retq
movq 0x3e8(%rsp), %rdi
callq 0x3470
nopw %cs:(%rax,%rax)