// File: /workspace/github_cpp_makefile_star2_O1/hkbu-victor[P]SFastLDA/doc_uci_to_word_uci.cpp
// Function: main
int main(int argc, char **argv){
  	char line[ 80 ];
    int i, docId, wordId, count;
    int nlines, D, W;

    SparseData *data_lines;
    FILE *fr;

    i = parse_args(argc, argv);

    if (i < 0)
    	return -1;

    nlines = 0;
    printf("\n*** Conversion Tool: doc-by-doc UCI --->  word-by-word UCI");
    printf("\n*** Opening and reading dataset file %s...\n", inputFile);

    fr = fopen(inputFile,"rt");
    if (fr == NULL){
          	printf("Cannot find the file %s !!!\n", inputFile);
          	exit(1);
     }
    fgets(line, 80, fr);
    sscanf(line, "%d", &D);
    fgets(line, 80, fr);
    sscanf(line, "%d", &W);
    fgets(line, 80, fr);
    sscanf(line, "%d", &nlines);

    data_lines = new SparseData[nlines];

    int lineNo = 0;
    while (fgets(line, 80, fr) != NULL){
      	sscanf( line, "%d %d %d", &docId, &wordId, &count);

      	data_lines[lineNo].doc_id = docId;
      	data_lines[lineNo].word_id = wordId;
      	data_lines[lineNo].count = count;
      	lineNo++;

      	if (((lineNo+3) % 2000000) == 0)
			printf("processing line %d \n", lineNo);
    }
    printf("Total data lines read : %d\n\n", lineNo + 3);
    fclose(fr);

    std::sort(data_lines, data_lines + nlines, compare_DoublePair());


    FILE *fh;
    fh = fopen(outputFile, "w");
        if (fh == NULL){
          printf("\nFile writting error : %s \n!!!", outputFile);
          exit (-1);
        }
        //write 3 lines of meta-information
        printf("*** Write word-by-word UCI data to : %s.\n\n", outputFile);
        fprintf(fh, "%d\n", D);
        fprintf(fh, "%d\n", W);
        fprintf(fh, "%d\n", nlines);
        for (i=0; i<nlines; i++){
        	fprintf(fh, "%d %d %d\n", data_lines[i].doc_id, data_lines[i].word_id, data_lines[i].count);
        	if ((i % 2000000) == 0)
        		printf("writing line %d \n", i);
        }

        fclose(fh);
        printf("Total data lines written : %d\n", i + 3);
    printf("\nFinished!!!\n");
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
callq 0x1225
testl %eax, %eax
js 0x14aa
movl $0x0, 0x8(%rsp)
leaq 0xd0f(%rip), %rdi       # 0x201b
xorl %eax, %eax
callq 0x1040
movq 0x2d6e(%rip), %rsi      # 0x4088
leaq 0xd36(%rip), %rdi       # 0x2057
xorl %eax, %eax
callq 0x1040
movq 0x2d59(%rip), %rdi      # 0x4088
leaq 0xd4e(%rip), %rsi       # 0x2084
callq 0x1070
testq %rax, %rax
je 0x162d
movq %rax, %r13
leaq 0x30(%rsp), %r15
movq %r15, %rdi
movl $0x50, %esi
movq %rax, %rdx
callq 0x10c0
leaq 0xd47(%rip), %rbx       # 0x20aa
leaq 0x10(%rsp), %rdx
movq %r15, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1050
movq %r15, %rdi
movl $0x50, %esi
movq %r13, %rdx
callq 0x10c0
leaq 0xc(%rsp), %rdx
movq %r15, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1050
movq %r15, %rdi
movl $0x50, %esi
movq %r13, %rdx
callq 0x10c0
leaq 0x8(%rsp), %r12
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x1050
movslq (%r12), %rax
movl $0xc, %ecx
mulq %rcx
movq $-0x1, %rdi
cmovnoq %rax, %rdi
callq 0x1030
movq %rax, %rbx
movq %r15, %rdi
movl $0x50, %esi
movq %r13, %rdx
callq 0x10c0
testq %rax, %rax
movq %rbx, 0x20(%rsp)
je 0x14b4
leaq 0x8(%rbx), %r12
movl $0xfffffffc, %ebp       # imm = 0xFFFFFFFC
movl $0x4, %r14d
movl $0x3, %r15d
leaq 0x30(%rsp), %rbx
movq %r13, 0x28(%rsp)
movl %r14d, %eax
imulq $0x431bde83, %rax, %rax # imm = 0x431BDE83
shrq $0x33, %rax
imull $0x1e8480, %eax, %r13d  # imm = 0x1E8480
movq %rbx, %rdi
leaq 0xc6d(%rip), %rsi       # 0x20a4
leaq 0x1c(%rsp), %rdx
leaq 0x18(%rsp), %rcx
leaq 0x14(%rsp), %r8
xorl %eax, %eax
callq 0x1050
movl 0x1c(%rsp), %eax
movl %eax, -0x8(%r12)
movl 0x18(%rsp), %eax
movl %eax, -0x4(%r12)
movl 0x14(%rsp), %eax
movl %eax, (%r12)
addl %ebp, %r13d
jne 0x147e
leal -0x2(%r15), %esi
leaq 0xc36(%rip), %rdi       # 0x20ad
xorl %eax, %eax
callq 0x1040
movq %rbx, %rdi
movl $0x50, %esi
movq 0x28(%rsp), %r13
movq %r13, %rdx
callq 0x10c0
decl %ebp
incl %r14d
addq $0xc, %r12
incl %r15d
testq %rax, %rax
jne 0x1418
jmp 0x14ba
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x1619
movl $0x3, %r15d
leaq 0xc01(%rip), %rdi       # 0x20c2
movl %r15d, %esi
xorl %eax, %eax
callq 0x1040
movq %r13, %rdi
callq 0x1060
movslq 0x8(%rsp), %rax
testq %rax, %rax
movq 0x20(%rsp), %rbx
je 0x150d
leaq (%rax,%rax,2), %rcx
leaq (%rbx,%rcx,4), %r14
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1664
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1723
movq 0x2b7c(%rip), %rdi      # 0x4090
leaq 0xbc4(%rip), %rsi       # 0x20df
callq 0x1070
movq 0x2b69(%rip), %rsi      # 0x4090
testq %rax, %rax
je 0x164c
movq %rax, %r14
leaq 0xbc6(%rip), %rdi       # 0x2100
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x1040
movl 0x10(%rsp), %edx
leaq 0xc14(%rip), %r15       # 0x2162
movq %r14, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x10a0
movl 0xc(%rsp), %edx
movq %r14, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x10a0
movl 0x8(%rsp), %edx
movq %r14, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x10a0
cmpl $0x0, 0x8(%rsp)
jle 0x15ee
addq $0x8, %rbx
leaq 0xb9c(%rip), %r12       # 0x212b
leaq 0xb9f(%rip), %r13       # 0x2135
xorl %r15d, %r15d
movl %r15d, %eax
imulq $0x431bde83, %rax, %rax # imm = 0x431BDE83
shrq $0x33, %rax
imull $0xffe17b80, %eax, %ebp # imm = 0xFFE17B80
movl -0x8(%rbx), %edx
movl -0x4(%rbx), %ecx
movl (%rbx), %r8d
movq %r14, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0x10a0
addl %r15d, %ebp
jne 0x15d5
movq %r13, %rdi
movl %r15d, %esi
xorl %eax, %eax
callq 0x1040
incq %r15
movslq 0x8(%rsp), %rax
addq $0xc, %rbx
cmpq %rax, %r15
jl 0x1599
addl $0x3, %r15d
xorl %ebp, %ebp
jmp 0x15f4
movl $0x3, %r15d
movq %r14, %rdi
callq 0x1060
leaq 0xb44(%rip), %rdi       # 0x2147
movl %r15d, %esi
xorl %eax, %eax
callq 0x1040
leaq 0xc59(%rip), %rdi       # 0x226d
callq 0x10b0
movl %ebp, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2a54(%rip), %rsi      # 0x4088
leaq 0xa4c(%rip), %rdi       # 0x2087
xorl %eax, %eax
callq 0x1040
movl $0x1, %edi
callq 0x1080
leaq 0xa8e(%rip), %rdi       # 0x20e1
xorl %eax, %eax
callq 0x1040
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1080