// File: /workspace/github_cpp_makefile_star2_O1/ddu1[P]polarizedtext/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();

// set dim to number of labels (ddu)
  if( args_->model == model_name::pwv) { 
    args_->dim = dict_->nlabels();
  }

  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);
  }

  //input_->uniform(1.0 / args_->dim);
  //output_->zero();

  // initialized vectors with labels (ddu)
  auto words = dict_->getWords();
  output_->zero();

    if (args_->pretrainedVectors.size() != 0) {
        loadVectors(args_->pretrainedVectors);
    } else {

        input_ = std::make_shared<Matrix>(dict_->nwords()+args_->bucket, args_->dim);
        if(args_->model == model_name::pwv ) {
          input_->zero();
          for( int32_t i = 0; i < dict_->nwords(); ++i) { // for words
            int32_t non_zero_size = words[i].nlabels;
            Vector vec = Vector(args_->dim, dict_->getLabels(i));
            input_->addRow(vec, i, non_zero_size? (1.0/non_zero_size): 0);
          }
          for( int32_t i = 0; i < args_->bucket; ++i) { // for buckets (subwords)
            Vector ones = Vector(args_->dim);
            ones.ones();
            input_->addRow(ones, dict_->nwords()+i, 1.0 / args_->dim);
          }
        }
        else {
          input_->uniform(1.0 / args_->dim);
        }  
    }

  start = clock();
  tokenCount = 0;
  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();
  }
  model_ = std::make_shared<Model>(input_, output_, args_, 0);

  saveModel();
  if (args_->model != model_name::sup) {
    saveVectors();
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x288, %rsp            # imm = 0x288
movq %rdi, %rbx
movq (%rsi), %rax
movq %rax, (%rdi)
addq $0x8, %rdi
addq $0x8, %rsi
callq 0x7782
leaq 0x88(%rsp), %rdi
movq $0x0, -0x8(%rdi)
leaq 0x80(%rsp), %r14
leaq 0x40(%rsp), %rdx
movq %r14, %rsi
movq %rbx, %rcx
callq 0xf194
movaps (%r14), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, (%r14)
movq 0x18(%rbx), %rdi
movups %xmm0, 0x10(%rbx)
testq %rdi, %rdi
je 0xe8be
callq 0x77d4
movq 0x88(%rsp), %rdi
testq %rdi, %rdi
je 0xe8d0
callq 0x77d4
movq (%rbx), %rdi
leaq 0x3652(%rip), %rsi      # 0x11f2c
callq 0x3090
testl %eax, %eax
je 0xedc2
movq (%rbx), %rsi
leaq 0x80(%rsp), %rdi
movl $0x8, %edx
callq 0x33c0
leaq 0xf8(%rsp), %rdi
callq 0x3060
testb %al, %al
je 0xede7
movq 0x10(%rbx), %rdi
leaq 0x80(%rsp), %rsi
callq 0x56e4
leaq 0x80(%rsp), %rdi
callq 0x3050
movq (%rbx), %rax
cmpl $0x4, 0x8c(%rax)
jne 0xe94a
movq 0x10(%rbx), %rdi
callq 0x4db4
movq (%rbx), %rcx
movl %eax, 0x6c(%rcx)
movq (%rbx), %rax
cmpl $0x3, 0x8c(%rax)
jne 0xe98b
movq 0x10(%rbx), %rdi
callq 0x4db4
leaq 0x20(%rsp), %rcx
movl %eax, (%rcx)
movq (%rbx), %r8
addq $0x6c, %r8
leaq 0x48(%rsp), %rdi
movq $0x0, -0x8(%rdi)
leaq 0x40(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0xfddc
jmp 0xe9be
movq 0x10(%rbx), %rdi
callq 0x4db0
leaq 0x20(%rsp), %rcx
movl %eax, (%rcx)
movq (%rbx), %r8
addq $0x6c, %r8
leaq 0x48(%rsp), %rdi
movq $0x0, -0x8(%rdi)
leaq 0x40(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0xfddc
movaps 0x40(%rsp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, 0x40(%rsp)
movq 0x38(%rbx), %rdi
movups %xmm0, 0x30(%rbx)
testq %rdi, %rdi
je 0xe9dd
callq 0x77d4
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0xe9ec
callq 0x77d4
movq 0x10(%rbx), %rdi
callq 0x4dbe
leaq 0x40(%rsp), %rdi
movq %rax, %rsi
callq 0xef5a
movq 0x30(%rbx), %rdi
callq 0x924e
movq (%rbx), %rax
movq 0xd8(%rax), %rdx
testq %rdx, %rdx
je 0xea64
leaq 0x70(%rsp), %r14
movq %r14, -0x10(%r14)
movq 0xd0(%rax), %rsi
addq %rsi, %rdx
leaq 0x60(%rsp), %rdi
callq 0x7844
leaq 0x60(%rsp), %rsi
movq %rbx, %rdi
callq 0xe4aa
movq 0x60(%rsp), %rdi
cmpq %r14, %rdi
je 0xec4a
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x3260
jmp 0xec4a
movq 0x10(%rbx), %rdi
callq 0x4db0
movq (%rbx), %r8
addl 0x90(%r8), %eax
leaq 0x10(%rsp), %rcx
movl %eax, (%rcx)
addq $0x6c, %r8
leaq 0x28(%rsp), %rdi
movq $0x0, -0x8(%rdi)
leaq 0x20(%rsp), %rsi
leaq 0x38(%rsp), %rdx
callq 0xfddc
movaps 0x20(%rsp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, 0x20(%rsp)
movq 0x28(%rbx), %rdi
movups %xmm0, 0x20(%rbx)
testq %rdi, %rdi
je 0xeabd
callq 0x77d4
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0xeacc
callq 0x77d4
movq (%rbx), %rax
movq 0x20(%rbx), %rdi
cmpl $0x4, 0x8c(%rax)
jne 0xebb1
callq 0x924e
movl $0x48, %r15d
movq $0x0, 0x58(%rsp)
leaq 0x20(%rsp), %r12
leaq 0x10(%rsp), %rbp
movq 0x10(%rbx), %rdi
callq 0x4db0
cltq
movq 0x58(%rsp), %rdx
cmpq %rax, %rdx
jge 0xebd3
movq 0x40(%rsp), %rax
movq %r15, %r14
movb (%rax,%r15), %r15b
movq (%rbx), %rax
movslq 0x6c(%rax), %r13
movq 0x10(%rbx), %rsi
movq %r12, %rdi
callq 0x4dc4
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x952a
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0xeb5b
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x3260
movq 0x20(%rbx), %rdi
xorps %xmm0, %xmm0
testb %r15b, %r15b
je 0xeb85
movsbl %r15b, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd 0x2a86(%rip), %xmm1     # 0x11600
divsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
movq %rbp, %rsi
movq 0x58(%rsp), %r15
movq %r15, %rdx
callq 0x92f2
movq %rbp, %rdi
callq 0x95da
incq %r15
movq %r15, 0x58(%rsp)
movq %r14, %r15
addq $0x68, %r15
jmp 0xeafe
xorps %xmm0, %xmm0
cvtsi2sdl 0x6c(%rax), %xmm0
movsd 0x2a3f(%rip), %xmm1     # 0x11600
divsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
callq 0x926c
jmp 0xec4a
movq (%rbx), %rax
cmpl $0x0, 0x90(%rax)
jle 0xec4a
xorl %ebp, %ebp
leaq 0x20(%rsp), %r14
movslq 0x6c(%rax), %rsi
movq %r14, %rdi
callq 0x9500
movq %r14, %rdi
callq 0x95ec
movq 0x10(%rbx), %rdi
movq 0x20(%rbx), %r15
callq 0x4db0
addl %ebp, %eax
movslq %eax, %rdx
movq (%rbx), %rax
xorps %xmm0, %xmm0
cvtsi2sdl 0x6c(%rax), %xmm0
movsd 0x29e1(%rip), %xmm1     # 0x11600
divsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
movq %r15, %rdi
movq %r14, %rsi
callq 0x92f2
movq %r14, %rdi
callq 0x95da
incl %ebp
movq (%rbx), %rax
cmpl 0x90(%rax), %ebp
jl 0xebe6
callq 0x3170
movq %rax, 0x58(%rbx)
xorl %eax, %eax
xchgq %rax, 0x50(%rbx)
xorl %r13d, %r13d
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
movq (%rbx), %rax
cmpl $0x0, 0x9c(%rax)
jle 0xecf5
leaq 0x90e0(%rip), %rbp      # 0x17d60
leaq 0x38(%rsp), %r14
leaq 0x10(%rsp), %r15
leaq 0x20(%rsp), %r12
movq $0x0, 0x38(%rsp)
movl $0x18, %edi
callq 0x3250
movq %rbp, (%rax)
movq %rbx, 0x8(%rax)
movl %r13d, 0x10(%rax)
movq %rax, 0x10(%rsp)
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x3210
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xeccf
movq (%rdi), %rax
callq *0x8(%rax)
movq %r12, %rdi
movq %r14, %rsi
callq 0xff52
cmpq $0x0, 0x38(%rsp)
jne 0xeea4
incl %r13d
movq (%rbx), %rax
cmpl 0x9c(%rax), %r13d
jl 0xec8f
movq 0x20(%rsp), %r14
cmpq 0x28(%rsp), %r14
je 0xed0f
movq %r14, %rdi
callq 0x3410
addq $0x8, %r14
jmp 0xecfa
leaq 0x30(%rbx), %r8
leaq 0x20(%rbx), %rcx
leaq 0x38(%rsp), %rax
movl $0x0, (%rax)
leaq 0x18(%rsp), %rdi
movq $0x0, -0x8(%rdi)
movq %rax, (%rsp)
leaq 0x10(%rsp), %rsi
leaq 0xf(%rsp), %rdx
movq %rbx, %r9
callq 0xf41a
movaps 0x10(%rsp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, 0x10(%rsp)
movq 0x48(%rbx), %rdi
movups %xmm0, 0x40(%rbx)
testq %rdi, %rdi
je 0xed64
callq 0x77d4
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0xed73
callq 0x77d4
movq %rbx, %rdi
callq 0xca66
movq (%rbx), %rax
cmpl $0x3, 0x8c(%rax)
je 0xed8f
movq %rbx, %rdi
callq 0xc7b2
leaq 0x20(%rsp), %rdi
callq 0xeff2
leaq 0x40(%rsp), %rdi
callq 0x74bc
leaq 0x80(%rsp), %rdi
callq 0x3070
addq $0x288, %rsp            # imm = 0x288
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x9227(%rip), %rdi      # 0x17ff0
leaq 0x315e(%rip), %rsi      # 0x11f2e
callq 0x3240
movq %rax, %rdi
callq 0x3110
movl $0x1, %edi
callq 0x32b0
movq 0x9202(%rip), %rdi      # 0x17ff0
leaq 0x3158(%rip), %rsi      # 0x11f4d
movl $0x1c, %edx
callq 0x3290
movq 0x91ea(%rip), %rdi      # 0x17ff0
callq 0x3110
jmp 0xeddd
movq %rax, %rbx
movq 0x60(%rsp), %rdi
cmpq %r14, %rdi
je 0xeeb6
movq 0x70(%rsp), %rsi
incq %rsi
jmp 0xee5f
jmp 0xee68
jmp 0xeea9
jmp 0xee36
jmp 0xee36
jmp 0xeea9
jmp 0xee68
jmp 0xee36
movq %rax, %rbx
jmp 0xeec0
jmp 0xee68
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
jmp 0xee75
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0xeeb6
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x3260
jmp 0xeeb6
jmp 0xee68
movq %rax, %rbx
jmp 0xeeb6
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x95da
jmp 0xeeb6
jmp 0xeea9
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xee91
movq (%rdi), %rax
callq *0x8(%rax)
movq $0x0, 0x10(%rsp)
jmp 0xeeac
cmpq $0x0, 0x38(%rsp)
je 0xeea9
callq 0x3080
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0xeff2
leaq 0x40(%rsp), %rdi
callq 0x74bc
leaq 0x80(%rsp), %rdi
callq 0x3070
movq %rbx, %rdi
callq 0x3450
nop