// File: /workspace/github_cpp_makefile_star2_O3/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 $0x298, %rsp            # imm = 0x298
movq %rdi, %rbx
movq (%rsi), %rax
movq %rax, (%rdi)
addq $0x8, %rdi
addq $0x8, %rsi
callq 0x77de
leaq 0x98(%rsp), %rdi
movq $0x0, -0x8(%rdi)
leaq 0x90(%rsp), %r14
leaq 0x50(%rsp), %rdx
movq %r14, %rsi
movq %rbx, %rcx
callq 0xf08a
movaps (%r14), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, (%r14)
movq 0x18(%rbx), %rdi
movups %xmm0, 0x10(%rbx)
testq %rdi, %rdi
je 0xe7ce
callq 0x7832
movq 0x98(%rsp), %rdi
testq %rdi, %rdi
je 0xe7ce
callq 0x7832
movq (%rbx), %rdi
leaq 0x3784(%rip), %rsi      # 0x11f5c
callq 0x3090
testl %eax, %eax
je 0xeccb
movq (%rbx), %rsi
leaq 0x90(%rsp), %rdi
movl $0x8, %edx
callq 0x33c0
leaq 0x108(%rsp), %rdi
callq 0x3060
testb %al, %al
je 0xecf0
movq 0x10(%rbx), %rdi
leaq 0x90(%rsp), %rsi
callq 0x5704
leaq 0x90(%rsp), %rdi
callq 0x3050
movq (%rbx), %rax
movl 0x8c(%rax), %eax
cmpl $0x4, %eax
jne 0xe850
movq 0x10(%rbx), %rdi
callq 0x4dbe
movq (%rbx), %rcx
movl %eax, 0x6c(%rcx)
movl 0x8c(%rcx), %eax
cmpl $0x3, %eax
jne 0xe88a
movq 0x10(%rbx), %rdi
callq 0x4dbe
leaq 0x10(%rsp), %rcx
movl %eax, (%rcx)
movq (%rbx), %r8
addq $0x6c, %r8
leaq 0x58(%rsp), %rdi
movq $0x0, -0x8(%rdi)
leaq 0x50(%rsp), %rsi
leaq 0x30(%rsp), %rdx
callq 0xfc9c
jmp 0xe8bd
movq 0x10(%rbx), %rdi
callq 0x4dba
leaq 0x10(%rsp), %rcx
movl %eax, (%rcx)
movq (%rbx), %r8
addq $0x6c, %r8
leaq 0x58(%rsp), %rdi
movq $0x0, -0x8(%rdi)
leaq 0x50(%rsp), %rsi
leaq 0x30(%rsp), %rdx
callq 0xfc9c
movaps 0x50(%rsp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, 0x50(%rsp)
movq 0x38(%rbx), %rdi
movups %xmm0, 0x30(%rbx)
testq %rdi, %rdi
je 0xe8eb
callq 0x7832
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0xe8eb
callq 0x7832
movq 0x10(%rbx), %rdi
callq 0x4dc8
leaq 0x50(%rsp), %rdi
movq %rax, %rsi
callq 0xee5e
movq 0x30(%rbx), %rdi
callq 0x9214
movq (%rbx), %rax
movq 0xd8(%rax), %rdx
testq %rdx, %rdx
je 0xe969
leaq 0x80(%rsp), %r14
movq %r14, -0x10(%r14)
movq 0xd0(%rax), %rsi
addq %rsi, %rdx
leaq 0x70(%rsp), %rdi
callq 0x78a2
leaq 0x70(%rsp), %rsi
movq %rbx, %rdi
callq 0xe3c2
movq 0x70(%rsp), %rdi
cmpq %r14, %rdi
je 0xeb4f
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x3260
jmp 0xeb4f
movq 0x10(%rbx), %rdi
callq 0x4dba
movq (%rbx), %r8
addl 0x90(%r8), %eax
leaq 0x30(%rsp), %rcx
movl %eax, (%rcx)
addq $0x6c, %r8
leaq 0x18(%rsp), %rdi
movq $0x0, -0x8(%rdi)
leaq 0x10(%rsp), %rsi
leaq 0x48(%rsp), %rdx
callq 0xfc9c
movaps 0x10(%rsp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, 0x10(%rsp)
movq 0x28(%rbx), %rdi
movups %xmm0, 0x20(%rbx)
testq %rdi, %rdi
je 0xe9d1
callq 0x7832
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0xe9d1
callq 0x7832
movq (%rbx), %rax
movq 0x20(%rbx), %rdi
cmpl $0x4, 0x8c(%rax)
jne 0xeab6
callq 0x9214
movl $0x48, %r15d
movq $0x0, 0x68(%rsp)
leaq 0x10(%rsp), %r12
leaq 0x30(%rsp), %rbp
movq 0x10(%rbx), %rdi
callq 0x4dba
cltq
movq 0x68(%rsp), %rdx
cmpq %rax, %rdx
jge 0xead8
movq 0x50(%rsp), %rax
movq %r15, %r14
movb (%rax,%r15), %r15b
movq (%rbx), %rax
movslq 0x6c(%rax), %r13
movq 0x10(%rbx), %rsi
movq %r12, %rdi
callq 0x4dce
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x94f2
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xea60
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x3260
movq 0x20(%rbx), %rdi
xorps %xmm0, %xmm0
testb %r15b, %r15b
je 0xea8a
movsbl %r15b, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd 0x2bb1(%rip), %xmm1     # 0x11630
divsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
movq %rbp, %rsi
movq 0x68(%rsp), %r15
movq %r15, %rdx
callq 0x92b8
movq %rbp, %rdi
callq 0x95a2
incq %r15
movq %r15, 0x68(%rsp)
movq %r14, %r15
addq $0x68, %r15
jmp 0xea03
xorps %xmm0, %xmm0
cvtsi2sdl 0x6c(%rax), %xmm0
movsd 0x2b6a(%rip), %xmm1     # 0x11630
divsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
callq 0x9232
jmp 0xeb4f
movq (%rbx), %rax
cmpl $0x0, 0x90(%rax)
jle 0xeb4f
xorl %ebp, %ebp
leaq 0x10(%rsp), %r14
movslq 0x6c(%rax), %rsi
movq %r14, %rdi
callq 0x94c8
movq %r14, %rdi
callq 0x95b4
movq 0x10(%rbx), %rdi
movq 0x20(%rbx), %r15
callq 0x4dba
addl %ebp, %eax
movslq %eax, %rdx
movq (%rbx), %rax
xorps %xmm0, %xmm0
cvtsi2sdl 0x6c(%rax), %xmm0
movsd 0x2b0c(%rip), %xmm1     # 0x11630
divsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
movq %r15, %rdi
movq %r14, %rsi
callq 0x92b8
movq %r14, %rdi
callq 0x95a2
incl %ebp
movq (%rbx), %rax
cmpl 0x90(%rax), %ebp
jl 0xeaeb
callq 0x3170
movq %rax, 0x58(%rbx)
xorl %eax, %eax
xchgq %rax, 0x50(%rbx)
xorl %r13d, %r13d
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq (%rbx), %rax
cmpl $0x0, 0x9c(%rax)
jle 0xec18
leaq 0x91d7(%rip), %rbp      # 0x17d60
leaq 0x48(%rsp), %r14
leaq 0x30(%rsp), %r15
leaq 0x10(%rsp), %r12
movq $0x0, 0x48(%rsp)
movl $0x18, %edi
callq 0x3250
movq %rbp, (%rax)
movq %rbx, 0x8(%rax)
movl %r13d, 0x10(%rax)
movq %rax, 0x30(%rsp)
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x3210
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xebd8
movq (%rdi), %rax
callq *0x8(%rax)
movq %r12, %rdi
movq %r14, %rsi
callq 0xfe12
cmpq $0x0, 0x48(%rsp)
jne 0xedd2
incl %r13d
movq (%rbx), %rax
cmpl 0x9c(%rax), %r13d
jl 0xeb98
movq 0x10(%rsp), %r14
cmpq 0x18(%rsp), %r14
je 0xec18
movq %r14, %rdi
callq 0x3410
addq $0x8, %r14
jmp 0xec03
leaq 0x30(%rbx), %r8
leaq 0x20(%rbx), %rcx
leaq 0x48(%rsp), %rax
movl $0x0, (%rax)
leaq 0x38(%rsp), %rdi
movq $0x0, -0x8(%rdi)
movq %rax, (%rsp)
leaq 0x30(%rsp), %rsi
leaq 0xf(%rsp), %rdx
movq %rbx, %r9
callq 0xf304
movaps 0x30(%rsp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, 0x30(%rsp)
movq 0x48(%rbx), %rdi
movups %xmm0, 0x40(%rbx)
testq %rdi, %rdi
je 0xec7c
callq 0x7832
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0xec7c
callq 0x7832
movq %rbx, %rdi
callq 0xc95e
movq (%rbx), %rax
cmpl $0x3, 0x8c(%rax)
je 0xec98
movq %rbx, %rdi
callq 0xc6aa
leaq 0x10(%rsp), %rdi
callq 0xeef6
leaq 0x50(%rsp), %rdi
callq 0x7516
leaq 0x90(%rsp), %rdi
callq 0x3070
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x931e(%rip), %rdi      # 0x17ff0
leaq 0x3285(%rip), %rsi      # 0x11f5e
callq 0x3240
movq %rax, %rdi
callq 0x3110
movl $0x1, %edi
callq 0x32b0
movq 0x92f9(%rip), %rdi      # 0x17ff0
leaq 0x327f(%rip), %rsi      # 0x11f7d
movl $0x1c, %edx
callq 0x3290
movq 0x92e1(%rip), %rdi      # 0x17ff0
callq 0x3110
jmp 0xece6
movq %rax, %rbx
movq 0x70(%rsp), %rdi
cmpq %r14, %rdi
je 0xedb3
movq 0x80(%rsp), %rsi
incq %rsi
jmp 0xed68
jmp 0xed71
jmp 0xeda6
jmp 0xed42
jmp 0xed42
jmp 0xeda6
jmp 0xed71
jmp 0xed42
movq %rax, %rbx
jmp 0xedbd
jmp 0xed71
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
jmp 0xed7e
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xedb3
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x3260
jmp 0xedb3
jmp 0xed71
movq %rax, %rbx
jmp 0xedb3
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x95a2
jmp 0xedb3
jmp 0xeda6
jmp 0xeda6
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xeda9
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0xeda9
cmpq $0x0, 0x48(%rsp)
jne 0xedd2
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0xeef6
leaq 0x50(%rsp), %rdi
callq 0x7516
leaq 0x90(%rsp), %rdi
callq 0x3070
movq %rbx, %rdi
callq 0x3450
callq 0x3080
nop