// File: epfml[P]sent2vec/src/fasttext.cc
// Function: fasttext::FastText::train(std::shared_ptr<fasttext::Args>)
void FastText::train(std::shared_ptr<Args> args) {
  args_ = args;
  dict_ = std::make_shared<Dictionary>(args_);
  if (args_->input == "-") {
    // manage expectations
    std::cerr << "Cannot use stdin for training!" << std::endl;
    exit(EXIT_FAILURE);
  }
  std::ifstream ifs(args_->input);
  if (!ifs.is_open()) {
    std::cerr << "Input file cannot be opened!" << std::endl;
    exit(EXIT_FAILURE);
  }
  dict_->readFromFile(ifs);
  ifs.close();

  if (args_->pretrainedVectors.size() != 0) {
    loadVectors(args_->pretrainedVectors);
  } else {
    input_ = std::make_shared<Matrix>(dict_->nwords()+args_->bucket+args_->bucketChar, args_->dim);
    input_->uniform(1.0 / args_->dim);
  }

  if (args_->model == model_name::sup) {
    output_ = std::make_shared<Matrix>(dict_->nlabels(), args_->dim);
  } else {
    output_ = std::make_shared<Matrix>(dict_->nwords(), args_->dim);
  }
  output_->zero();

  start = clock();
  tokenCount = 0;
  if (args_->thread > 1) {
    std::vector<std::thread> threads;
    for (int32_t i = 0; i < args_->thread; i++) {
      threads.push_back(std::thread([=]() { trainThread(i); }));
    }
    for (auto it = threads.begin(); it != threads.end(); ++it) {
      it->join();
    }
  } else {
    trainThread(0);
  }
  model_ = std::make_shared<Model>(input_, output_, args_, 0);

  saveModel();
  if (args_->model != model_name::sup && args_->model != model_name::sent2vec && args_->model != model_name::cbowCWNgrams) {
    saveVectors();
    if (args_->saveOutput > 0) {
      saveOutput();
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x360, %rsp            # imm = 0x360
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x308(%rbp)
callq 0x23a40
movq -0x308(%rbp), %rsi
leaq -0x20(%rbp), %rdi
callq 0x33bb0
movq -0x308(%rbp), %rdi
addq $0x10, %rdi
leaq -0x20(%rbp), %rsi
callq 0x33c20
leaq -0x20(%rbp), %rdi
callq 0x33c50
movq -0x308(%rbp), %rdi
callq 0xea70
movq %rax, %rdi
leaq 0x124c3(%rip), %rsi     # 0x439b9
callq 0x7ec0
testb $0x1, %al
jne 0x31501
jmp 0x3152d
movq 0x2dae8(%rip), %rdi     # 0x5eff0
leaq 0x124ac(%rip), %rsi     # 0x439bb
callq 0x5420
movq %rax, %rdi
movq 0x2da72(%rip), %rsi     # 0x5ef90
callq 0x5460
movl $0x1, %edi
callq 0x54f0
movq -0x308(%rbp), %rdi
callq 0xea70
movq %rax, %rsi
leaq -0x228(%rbp), %rdi
movq %rdi, -0x318(%rbp)
movl $0x8, %edx
callq 0x56d0
movq -0x318(%rbp), %rdi
callq 0x56c0
movb %al, -0x309(%rbp)
jmp 0x31568
movb -0x309(%rbp), %al
testb $0x1, %al
jne 0x315c6
jmp 0x31574
movq 0x2da75(%rip), %rdi     # 0x5eff0
leaq 0x12458(%rip), %rsi     # 0x439da
callq 0x5420
movq %rax, -0x320(%rbp)
jmp 0x31590
movq -0x320(%rbp), %rdi
movq 0x2d9f2(%rip), %rsi     # 0x5ef90
callq 0x5460
jmp 0x315a5
movl $0x1, %edi
callq 0x54f0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x230(%rbp)
movl %eax, -0x234(%rbp)
jmp 0x31b50
movq -0x308(%rbp), %rdi
addq $0x10, %rdi
callq 0x33af0
movq %rax, %rdi
leaq -0x228(%rbp), %rsi
callq 0x9590
jmp 0x315e7
leaq -0x228(%rbp), %rdi
callq 0x50f0
jmp 0x315f5
movq -0x308(%rbp), %rdi
callq 0xea70
movq %rax, %rdi
addq $0x108, %rdi            # imm = 0x108
callq 0x5300
cmpq $0x0, %rax
je 0x31683
movq -0x308(%rbp), %rdi
callq 0xea70
movq %rax, %rsi
addq $0x108, %rsi            # imm = 0x108
leaq -0x258(%rbp), %rdi
callq 0x51d0
jmp 0x3163a
movq -0x308(%rbp), %rdi
leaq -0x258(%rbp), %rsi
callq 0x305e0
jmp 0x3164f
leaq -0x258(%rbp), %rdi
callq 0x58d8
jmp 0x3178c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x230(%rbp)
movl %eax, -0x234(%rbp)
leaq -0x258(%rbp), %rdi
callq 0x58d8
jmp 0x31b50
movq -0x308(%rbp), %rdi
addq $0x10, %rdi
callq 0x33af0
movq %rax, %rdi
callq 0x8540
movl %eax, -0x324(%rbp)
jmp 0x316a3
movq -0x308(%rbp), %rdi
callq 0xea70
movq -0x308(%rbp), %rdi
movq %rax, %rcx
movl -0x324(%rbp), %eax
movl 0xc4(%rcx), %ecx
addl %ecx, %eax
movl %eax, -0x328(%rbp)
callq 0xea70
movq -0x308(%rbp), %rdi
movq %rax, %rcx
movl -0x328(%rbp), %eax
movl 0xc8(%rcx), %ecx
addl %ecx, %eax
movl %eax, -0x26c(%rbp)
callq 0xea70
movq %rax, %rdx
addq $0x94, %rdx
leaq -0x268(%rbp), %rdi
leaq -0x26c(%rbp), %rsi
callq 0x352a0
jmp 0x31714
movq -0x308(%rbp), %rdi
addq $0x20, %rdi
movq %rdi, -0x338(%rbp)
leaq -0x268(%rbp), %rsi
movq %rsi, -0x340(%rbp)
callq 0x33f20
movq -0x340(%rbp), %rdi
callq 0x23bb0
movq -0x338(%rbp), %rdi
callq 0x23990
movq -0x308(%rbp), %rdi
movq %rax, -0x330(%rbp)
callq 0xea70
movq -0x330(%rbp), %rdi
cvtsi2sdl 0x94(%rax), %xmm1
movsd 0x111b5(%rip), %xmm0    # 0x42930
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
callq 0x1e3b0
jmp 0x3178a
jmp 0x3178c
movq -0x308(%rbp), %rdi
callq 0xea70
cmpl $0x3, 0xc0(%rax)
jne 0x3181d
movq -0x308(%rbp), %rdi
addq $0x10, %rdi
callq 0x33af0
movq %rax, %rdi
callq 0x8560
movl %eax, -0x344(%rbp)
jmp 0x317c1
movq -0x308(%rbp), %rdi
movl -0x344(%rbp), %eax
movl %eax, -0x284(%rbp)
callq 0xea70
movq %rax, %rdx
addq $0x94, %rdx
leaq -0x280(%rbp), %rdi
leaq -0x284(%rbp), %rsi
callq 0x352a0
jmp 0x317f8
movq -0x308(%rbp), %rdi
addq $0x30, %rdi
leaq -0x280(%rbp), %rsi
callq 0x33f20
leaq -0x280(%rbp), %rdi
callq 0x23bb0
jmp 0x31897
movq -0x308(%rbp), %rdi
addq $0x10, %rdi
callq 0x33af0
movq %rax, %rdi
callq 0x8540
movl %eax, -0x348(%rbp)
jmp 0x3183d
movq -0x308(%rbp), %rdi
movl -0x348(%rbp), %eax
movl %eax, -0x29c(%rbp)
callq 0xea70
movq %rax, %rdx
addq $0x94, %rdx
leaq -0x298(%rbp), %rdi
leaq -0x29c(%rbp), %rsi
callq 0x352a0
jmp 0x31874
movq -0x308(%rbp), %rdi
addq $0x30, %rdi
leaq -0x298(%rbp), %rsi
callq 0x33f20
leaq -0x298(%rbp), %rdi
callq 0x23bb0
movq -0x308(%rbp), %rdi
addq $0x30, %rdi
callq 0x23990
movq %rax, %rdi
callq 0x1e350
jmp 0x318b1
callq 0x52e0
movq -0x308(%rbp), %rdi
movq %rax, 0x78(%rdi)
addq $0x70, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x34390
movq -0x308(%rbp), %rdi
callq 0xea70
cmpl $0x1, 0xd4(%rax)
jle 0x31a49
leaq -0x2b8(%rbp), %rdi
callq 0x34420
movl $0x0, -0x2bc(%rbp)
movq -0x308(%rbp), %rdi
movl -0x2bc(%rbp), %eax
movl %eax, -0x34c(%rbp)
callq 0xea70
movq %rax, %rcx
movl -0x34c(%rbp), %eax
cmpl 0xd4(%rcx), %eax
jge 0x319c5
movq -0x308(%rbp), %rax
movq %rax, -0x2d8(%rbp)
movl -0x2bc(%rbp), %eax
movl %eax, -0x2d0(%rbp)
leaq -0x2c8(%rbp), %rdi
leaq -0x2d8(%rbp), %rsi
callq 0x31b70
jmp 0x31959
leaq -0x2b8(%rbp), %rdi
leaq -0x2c8(%rbp), %rsi
callq 0x34440
jmp 0x3196e
leaq -0x2c8(%rbp), %rdi
callq 0x34470
movl -0x2bc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2bc(%rbp)
jmp 0x318fd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x230(%rbp)
movl %eax, -0x234(%rbp)
jmp 0x31a38
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x230(%rbp)
movl %eax, -0x234(%rbp)
leaq -0x2c8(%rbp), %rdi
callq 0x34470
jmp 0x31a38
leaq -0x2b8(%rbp), %rdi
callq 0x344a0
movq %rax, -0x2e0(%rbp)
leaq -0x2b8(%rbp), %rdi
callq 0x34510
movq %rax, -0x2e8(%rbp)
leaq -0x2e0(%rbp), %rdi
leaq -0x2e8(%rbp), %rsi
callq 0x344d0
testb $0x1, %al
jne 0x31a04
jmp 0x31a2a
leaq -0x2e0(%rbp), %rdi
callq 0x34540
movq %rax, %rdi
callq 0x5750
jmp 0x31a1a
jmp 0x31a1c
leaq -0x2e0(%rbp), %rdi
callq 0x34560
jmp 0x319d8
leaq -0x2b8(%rbp), %rdi
callq 0x34580
jmp 0x31a5b
leaq -0x2b8(%rbp), %rdi
callq 0x34580
jmp 0x31b50
movq -0x308(%rbp), %rdi
xorl %esi, %esi
callq 0x2fd30
jmp 0x31a59
jmp 0x31a5b
movq -0x308(%rbp), %rcx
movq %rcx, %rsi
addq $0x20, %rsi
movq %rcx, %rdx
addq $0x30, %rdx
movl $0x0, -0x2fc(%rbp)
leaq -0x2f8(%rbp), %rdi
leaq -0x2fc(%rbp), %r8
callq 0x33ff0
jmp 0x31a8f
movq -0x308(%rbp), %rdi
addq $0x60, %rdi
leaq -0x2f8(%rbp), %rsi
movq %rsi, -0x358(%rbp)
callq 0x34080
movq -0x358(%rbp), %rdi
callq 0x340b0
movq -0x308(%rbp), %rdi
callq 0x28ea0
jmp 0x31ac7
movq -0x308(%rbp), %rdi
callq 0xea70
cmpl $0x3, 0xc0(%rax)
je 0x31b3b
movq -0x308(%rbp), %rdi
callq 0xea70
cmpl $0x4, 0xc0(%rax)
je 0x31b3b
movq -0x308(%rbp), %rdi
callq 0xea70
cmpl $0x5, 0xc0(%rax)
je 0x31b3b
movq -0x308(%rbp), %rdi
callq 0x283c0
jmp 0x31b14
movq -0x308(%rbp), %rdi
callq 0xea70
cmpl $0x0, 0x128(%rax)
jle 0x31b39
movq -0x308(%rbp), %rdi
callq 0x28710
jmp 0x31b37
jmp 0x31b39
jmp 0x31b3b
leaq -0x228(%rbp), %rdi
callq 0x5130
addq $0x360, %rsp            # imm = 0x360
popq %rbp
retq
leaq -0x228(%rbp), %rdi
callq 0x5130
movq -0x230(%rbp), %rdi
callq 0x57a0
nopl (%rax,%rax)