// File: /workspace/github_cpp_makefile_star2_O3/szmer[P]brill/Brill-train.cpp
// Function: main
int main(int argc, char * argv[]) {
	if(argc < 3 || (argc > 3 && (argc-3) % 2 != 0)) {
    print_usage();
		return 1;
	}

  string output_file_name = string("");
  double improv_margin = 0.0;
  int max_rules = 1000;
  ofstream out;

  // Parse the arguments.
  for (int i = 3; i+1 < argc; i += 2) {
    if(strcmp(argv[i], "-o") == 0)
      output_file_name = string(argv[i+1]);
    if(strcmp(argv[i], "-m") == 0)
      improv_margin = atof(argv[i+1]);
    if(strcmp(argv[i], "-r") == 0)
      max_rules = atoi(argv[i+1]);
  }

	try {
    // Make the base corpus (containing the truth) and the tagset vector.
		Corpus base_corp = Corpus::load_file(argv[1]);
		vector<string> tagset = Corpus::load_tagset(argv[2]);

		// Tag the corpus with the baseline unigram tagger:
		Corpus old_corp = base_corp.tag_all_unigram(output_file_name != "");
    // /\ request leaving unigram table(if we intend to make a tagger file)
		double old_score = Corpus::compare_corpora(& base_corp, & old_corp);
		cout << "Unigram accuracy: " << old_score << endl;

		// Prepare a file where we will output discovered rules (btw put unigrams there).
    prepare_output_file(&out, output_file_name, &base_corp);

		RulesScoreTab tab(tagset, & base_corp);
		Corpus new_corp;
    double new_score = 10.0; // to be compared to the old score (first defined on unigrams above)
    int counter = 0;
		while(counter != max_rules && improv_margin < new_score-old_score) {

      if (counter > 0) { // some cleanup necessary, but not in the first iteration
        tab.clear(); // remove the old rules scores
        old_score = new_score;
        old_corp = new_corp;
      }

      // Try to find some useful rules.
			try { tab.examine(& old_corp); }
			catch(const char * e) { // if the program couldn't find any new good rules, just terminate
						// the loop, and rethrow any other exception
				if(strcmp(e,"No good rules found") == 0)	break;
				else	throw e;
			}

			Rule * winner = tab.get_winner();
			new_corp = winner -> transform_corp(& old_corp);

      // print the rule to the output file, if present
			if(out.is_open())
				out << winner->id << "\n";

			new_score = Corpus::compare_corpora(& base_corp, & new_corp);
			cout << (counter+1) << ". " << winner->description() << "-> " << new_score << endl;

      counter ++;
		} // generate the rules

		cout << "Final score, with " << counter << " rules: " << new_score << endl;
  } // try

  // Catch and print any errors, and exit.
	catch(const char * e) {
		cout << e << endl;
		return 1;
	}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x458, %rsp            # imm = 0x458
movl %edi, %r14d
cmpl $0x3, %edi
setge %al
testb %r14b, %al
jne 0x9f67
callq 0x9b8b
movl $0x1, %ebx
jmp 0xa5cd
movq %rsi, %rbx
leaq -0x58(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x41cd(%rip), %rdx      # 0xe146
leaq -0x68(%rbp), %rdi
movq %rdx, %rsi
callq 0xbf66
leaq -0x478(%rbp), %rdi
callq 0x2360
cmpl $0x5, %r14d
jb 0xa068
xorpd %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
movl $0x3e8, -0x2c(%rbp)     # imm = 0x3E8
movl $0x4, %r12d
leaq -0x278(%rbp), %r15
movq -0x8(%rbx,%r12,8), %r13
cmpb $0x2d, (%r13)
jne 0xa059
cmpb $0x6f, 0x1(%r13)
jne 0xa023
cmpb $0x0, 0x2(%r13)
jne 0xa023
movq (%rbx,%r12,8), %rsi
movq %r15, %rdi
leaq -0x180(%rbp), %rdx
callq 0xa7ba
leaq -0x68(%rbp), %rdi
movq %r15, %rsi
callq 0x2290
movq -0x278(%rbp), %rdi
leaq -0x268(%rbp), %rax
cmpq %rax, %rdi
je 0xa017
movq -0x268(%rbp), %rsi
incq %rsi
callq 0x2230
movq -0x8(%rbx,%r12,8), %r13
cmpb $0x2d, (%r13)
jne 0xa059
cmpb $0x6d, 0x1(%r13)
jne 0xa03f
cmpb $0x0, 0x2(%r13)
jne 0xa03f
movq (%rbx,%r12,8), %rdi
callq 0x22a0
movsd %xmm0, -0x40(%rbp)
cmpb $0x72, 0x1(%r13)
jne 0xa059
cmpb $0x0, 0x2(%r13)
jne 0xa059
movq (%rbx,%r12,8), %rdi
callq 0x2320
movl %eax, -0x2c(%rbp)
addq $0x2, %r12
cmpl %r14d, %r12d
jl 0x9fb8
jmp 0xa078
movl $0x3e8, -0x2c(%rbp)     # imm = 0x3E8
xorpd %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
movq 0x8(%rbx), %rsi
leaq -0x180(%rbp), %rdi
callq 0x26fc
movq 0x10(%rbx), %rsi
leaq -0x138(%rbp), %rdi
callq 0x2bc8
leaq 0x40a7(%rip), %rsi      # 0xe146
leaq -0x68(%rbp), %rdi
callq 0x20b0
xorl %edx, %edx
testl %eax, %eax
setne %dl
leaq -0x1c8(%rbp), %rdi
leaq -0x180(%rbp), %rsi
callq 0x2d98
leaq -0x180(%rbp), %rdi
leaq -0x1c8(%rbp), %rsi
callq 0x257c
movsd %xmm0, -0x48(%rbp)
movq 0x8ef7(%rip), %rdi      # 0x12fd8
leaq 0x4476(%rip), %rsi      # 0xe55e
movl $0x12, %edx
callq 0x2270
movq 0x8edf(%rip), %rdi      # 0x12fd8
movsd -0x48(%rbp), %xmm0
callq 0x2340
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2200
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2180
leaq -0x110(%rbp), %r14
movq %r14, -0x10(%r14)
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
addq %rsi, %rdx
leaq -0x120(%rbp), %rdi
callq 0xbec8
leaq -0x478(%rbp), %rdi
leaq -0x120(%rbp), %rsi
leaq -0x180(%rbp), %rdx
callq 0x9e48
movq -0x120(%rbp), %rdi
cmpq %r14, %rdi
je 0xa184
movq -0x110(%rbp), %rsi
incq %rsi
callq 0x2230
leaq -0x278(%rbp), %rdi
leaq -0x138(%rbp), %rsi
leaq -0x180(%rbp), %rdx
callq 0x52ce
xorpd %xmm0, %xmm0
leaq -0xe0(%rbp), %rax
movapd %xmm0, -0x20(%rax)
xorl %r13d, %r13d
movq %r13, -0x10(%rax)
movl $0x0, (%rax)
movq %r13, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %r13, 0x20(%rax)
movsd 0x3e4d(%rip), %xmm0     # 0xe020
cmpl $0x0, -0x2c(%rbp)
movsd %xmm0, -0x38(%rbp)
je 0xa48f
subsd -0x48(%rbp), %xmm0
ucomisd -0x40(%rbp), %xmm0
jbe 0xa48f
decl -0x2c(%rbp)
movsd 0x3e23(%rip), %xmm0     # 0xe020
movsd %xmm0, -0x38(%rbp)
leaq -0xb0(%rbp), %r15
xorl %r14d, %r14d
testl %r14d, %r14d
je 0xa259
leaq -0x240(%rbp), %rdi
callq 0xd9ca
leaq -0x270(%rbp), %rdi
callq 0xd992
leaq -0x1c8(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0xb362
leaq -0x1b0(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
callq 0xbb00
movsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
leaq -0x278(%rbp), %rbx
movq %rbx, %rdi
leaq -0x1c8(%rbp), %rsi
callq 0x5e08
movq %rbx, %rdi
callq 0x9ae8
movq %rax, %rbx
movq %r15, %rdi
movq %rax, %rsi
leaq -0x1c8(%rbp), %rdx
callq 0x3e22
movapd -0x100(%rbp), %xmm0
movq -0xf0(%rbp), %rax
movaps -0xb0(%rbp), %xmm1
movaps %xmm1, -0x100(%rbp)
movq -0xa0(%rbp), %rcx
movq %rcx, -0xf0(%rbp)
movupd %xmm0, -0x1e0(%rbp)
movq %rax, -0x1d0(%rbp)
xorpd %xmm0, %xmm0
movapd %xmm0, -0xb0(%rbp)
movq $0x0, -0xa0(%rbp)
leaq -0x1e0(%rbp), %rdi
callq 0xafee
leaq -0xe8(%rbp), %rdi
callq 0xbe90
movq -0x88(%rbp), %rax
testq %rax, %rax
je 0xa353
movl -0x90(%rbp), %ecx
movl %ecx, -0xe0(%rbp)
movq %rax, -0xd8(%rbp)
movapd -0x80(%rbp), %xmm0
movapd %xmm0, -0xd0(%rbp)
leaq -0xe0(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x70(%rbp), %rax
movq %rax, -0xc0(%rbp)
xorl %ecx, %ecx
movq %rcx, -0x88(%rbp)
leaq -0x90(%rbp), %rax
movq %rax, -0x80(%rbp)
movq %rax, -0x78(%rbp)
movq %rcx, -0x70(%rbp)
leaq -0x98(%rbp), %rdi
callq 0xb044
movq %r15, %rdi
callq 0xafee
leaq -0x408(%rbp), %rdi
callq 0x2080
testb %al, %al
je 0xa39e
movq 0x80(%rbx), %rsi
leaq -0x478(%rbp), %rdi
callq 0x20f0
movl $0x1, %edx
movq %rax, %rdi
leaq 0x41c3(%rip), %rsi      # 0xe55c
callq 0x2270
leaq -0x180(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0x257c
movsd %xmm0, -0x38(%rbp)
leal 0x1(%r14), %r13d
movq 0x8c17(%rip), %rdi      # 0x12fd8
movl %r13d, %esi
callq 0x2380
movq %rax, %r12
movl $0x2, %edx
movq %rax, %rdi
leaq 0x4196(%rip), %rsi      # 0xe571
callq 0x2270
movq %r15, %rdi
movq %rbx, %rsi
callq 0x38dc
movq -0xb0(%rbp), %rsi
movq -0xa8(%rbp), %rdx
movq %r12, %rdi
callq 0x2270
movq %rax, %rbx
movl $0x3, %edx
movq %rax, %rdi
leaq 0x4161(%rip), %rsi      # 0xe574
callq 0x2270
movq %rbx, %rdi
movsd -0x38(%rbp), %xmm0
callq 0x2340
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2200
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2180
movq -0xb0(%rbp), %rdi
leaq -0xa0(%rbp), %rax
cmpq %rax, %rdi
je 0xa471
movq -0xa0(%rbp), %rsi
incq %rsi
callq 0x2230
cmpl %r14d, -0x2c(%rbp)
je 0xa48f
movsd -0x38(%rbp), %xmm0
subsd -0x48(%rbp), %xmm0
ucomisd -0x40(%rbp), %xmm0
movl %r13d, %r14d
ja 0xa20c
movq 0x8b42(%rip), %rdi      # 0x12fd8
leaq 0x40db(%rip), %rsi      # 0xe578
movl $0x12, %edx
callq 0x2270
movq 0x8b2a(%rip), %rdi      # 0x12fd8
movl %r13d, %esi
callq 0x2380
movq %rax, %rbx
leaq 0x40cb(%rip), %rsi      # 0xe58b
movl $0x8, %edx
movq %rax, %rdi
callq 0x2270
movq %rbx, %rdi
movsd -0x38(%rbp), %xmm0
callq 0x2340
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2200
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2180
leaq -0xe8(%rbp), %rdi
callq 0xb044
leaq -0x100(%rbp), %rdi
callq 0xafee
leaq -0x210(%rbp), %rdi
callq 0xb194
leaq -0x240(%rbp), %rdi
callq 0xb224
leaq -0x270(%rbp), %rdi
callq 0xb304
leaq -0x1b0(%rbp), %rdi
callq 0xb044
leaq -0x1c8(%rbp), %rdi
callq 0xafee
leaq -0x138(%rbp), %rdi
callq 0xa9ee
leaq -0x168(%rbp), %rdi
callq 0xb044
leaq -0x180(%rbp), %rdi
callq 0xafee
xorl %ebx, %ebx
leaq -0x58(%rbp), %r14
movq 0x8a47(%rip), %rax      # 0x12fd0
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq -0x470(%rbp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, -0x478(%rbp,%rcx)
callq 0x21e0
leaq -0x380(%rbp), %rdi
callq 0x20d0
movq -0x68(%rbp), %rdi
cmpq %r14, %rdi
je 0xa5cd
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2230
movl %ebx, %eax
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xa66d
movq %rdx, %r15
movq %rax, %rbx
movq -0x120(%rbp), %rdi
cmpq %r14, %rdi
je 0xa738
movq -0x110(%rbp), %rsi
incq %rsi
callq 0x2230
jmp 0xa738
movq %rdx, %r15
movq %rax, %rbx
jmp 0xa750
movq %rdx, %r15
movq %rax, %rbx
jmp 0xa75c
movq %rdx, %r15
movq %rax, %rbx
jmp 0xa774
movq %rax, %rbx
jmp 0xa64a
movq %rax, %rbx
jmp 0xa663
movq %rax, %rbx
leaq -0x478(%rbp), %rdi
callq 0x22e0
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0xa663
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2230
movq %rbx, %rdi
callq 0x23a0
jmp 0xa6e4
movq %rdx, %r15
movq %rax, %rbx
jmp 0xa738
movq %rdx, %r15
movq %rax, %rbx
cmpl $0x1, %r15d
jne 0xa714
movq %rbx, %rdi
callq 0x2100
movq %rax, %rbx
leaq 0x3c4a(%rip), %rsi      # 0xe2e4
movq %rax, %rdi
callq 0x22c0
testl %eax, %eax
jne 0xa6b3
callq 0x2350
movl %r14d, %r13d
jmp 0xa48f
movl $0x8, %edi
callq 0x2130
movq %rbx, (%rax)
movq 0x88f9(%rip), %rsi      # 0x12fc0
movq %rax, %rdi
xorl %edx, %edx
callq 0x2370
movq %rdx, %r15
movq %rax, %rbx
callq 0x2350
jmp 0xa714
jmp 0xa6e4
jmp 0xa6e4
jmp 0xa6e4
movq %rdx, %r15
movq %rax, %rbx
jmp 0xa714
movq %rdx, %r15
movq %rax, %rbx
movq -0xb0(%rbp), %rdi
leaq -0xa0(%rbp), %rax
cmpq %rax, %rdi
je 0xa714
movq -0xa0(%rbp), %rsi
incq %rsi
callq 0x2230
leaq -0xe8(%rbp), %rdi
callq 0xb044
leaq -0x100(%rbp), %rdi
callq 0xafee
leaq -0x278(%rbp), %rdi
callq 0xafb2
leaq -0x1b0(%rbp), %rdi
callq 0xb044
leaq -0x1c8(%rbp), %rdi
callq 0xafee
leaq -0x138(%rbp), %rdi
callq 0xa9ee
leaq -0x168(%rbp), %rdi
callq 0xb044
leaq -0x180(%rbp), %rdi
callq 0xafee
cmpl $0x1, %r15d
jne 0xa63e
movq %rbx, %rdi
callq 0x2100
movq 0x884b(%rip), %rdi      # 0x12fd8
movq %rax, %rsi
callq 0x2210
movq %rax, %rdi
callq 0x2120
callq 0x2350
movl $0x1, %ebx
jmp 0xa57e
movq %rax, %rbx
callq 0x2350
jmp 0xa63e
nop