// File: /workspace/github_cpp_makefile_star2_O2/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 0x15b6
andl $0x0, 0x8(%rsp)
leaq 0xd02(%rip), %rdi       # 0x201b
xorl %eax, %eax
callq 0x1040
movq 0x2d61(%rip), %rsi      # 0x4088
leaq 0xd29(%rip), %rdi       # 0x2057
xorl %eax, %eax
callq 0x1040
movq 0x2d4c(%rip), %rdi      # 0x4088
leaq 0xd41(%rip), %rsi       # 0x2084
callq 0x1070
testq %rax, %rax
je 0x15cd
movq %rax, %r14
leaq 0x30(%rsp), %r15
pushq $0x50
popq %rbp
movq %r15, %rdi
movl %ebp, %esi
movq %rax, %rdx
callq 0x10c0
leaq 0xd3a(%rip), %rbx       # 0x20aa
leaq 0x10(%rsp), %rdx
movq %r15, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1050
movq %r15, %rdi
movl %ebp, %esi
movq %r14, %rdx
callq 0x10c0
leaq 0xc(%rsp), %rdx
movq %r15, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1050
movq %r15, %rdi
movl %ebp, %esi
movq %r14, 0x20(%rsp)
movq %r14, %rdx
callq 0x10c0
leaq 0x8(%rsp), %r12
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x1050
movslq (%r12), %rax
pushq $0xc
popq %rcx
mulq %rcx
pushq $-0x1
popq %rdi
cmovnoq %rax, %rdi
callq 0x1030
movq %rax, %rbx
leaq 0x8(%rax), %r14
pushq $0x3
popq %r12
pushq $0x4
popq %r13
movq %rax, 0x28(%rsp)
movl %r13d, %eax
xorl %edx, %edx
movl $0x1e8480, %ecx         # imm = 0x1E8480
divl %ecx
movl %eax, %ebp
movq %r15, %rdi
pushq $0x50
popq %rsi
movq 0x20(%rsp), %rdx
callq 0x10c0
testq %rax, %rax
je 0x148c
imull $0x1e8480, %ebp, %eax   # imm = 0x1E8480
pushq $0x4
popq %rbp
subl %eax, %ebp
movq %r15, %rbx
movq %r15, %rdi
leaq 0xc77(%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(%r14)
movl 0x18(%rsp), %eax
movl %eax, -0x4(%r14)
movl 0x14(%rsp), %eax
movl %eax, (%r14)
addl %r12d, %ebp
cmpl $0x3, %ebp
jne 0x1475
leal -0x2(%r12), %esi
leaq 0xc3f(%rip), %rdi       # 0x20ad
xorl %eax, %eax
callq 0x1040
incl %r12d
incl %r13d
addq $0xc, %r14
movq %rbx, %r15
movq 0x28(%rsp), %rbx
jmp 0x13f2
leaq 0xc2f(%rip), %rdi       # 0x20c2
movl %r12d, %esi
xorl %eax, %eax
callq 0x1040
movq 0x20(%rsp), %rdi
callq 0x1060
movslq 0x8(%rsp), %rax
imulq $0xc, %rax, %rsi
addq %rbx, %rsi
movq %rbx, %rdi
callq 0x15fc
movq 0x2bce(%rip), %rdi      # 0x4090
leaq 0xc16(%rip), %rsi       # 0x20df
callq 0x1070
movq 0x2bbb(%rip), %rsi      # 0x4090
testq %rax, %rax
je 0x15e6
movq %rax, %r14
leaq 0xc18(%rip), %rdi       # 0x2100
xorl %eax, %eax
callq 0x1040
movl 0x10(%rsp), %edx
leaq 0xc68(%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
addq $0x8, %rbx
movl $0x1e8480, %ebp         # imm = 0x1E8480
leaq 0xbf2(%rip), %r12       # 0x212b
xorl %r15d, %r15d
movl %r15d, %eax
xorl %edx, %edx
divl %ebp
movslq 0x8(%rsp), %rcx
cmpq %rcx, %r15
jge 0x1589
imull $0xffe17b80, %eax, %r13d # 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, %r13d
jne 0x1580
leaq 0xbbf(%rip), %rdi       # 0x2135
movl %r15d, %esi
xorl %eax, %eax
callq 0x1040
incq %r15
addq $0xc, %rbx
jmp 0x153c
movq %r14, %rdi
callq 0x1060
addl $0x3, %r15d
leaq 0xbab(%rip), %rdi       # 0x2147
xorl %ebx, %ebx
movl %r15d, %esi
xorl %eax, %eax
callq 0x1040
leaq 0xcbe(%rip), %rdi       # 0x226d
callq 0x10b0
jmp 0x15b9
pushq $-0x1
popq %rbx
movl %ebx, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2ab4(%rip), %rsi      # 0x4088
leaq 0xaac(%rip), %rdi       # 0x2087
xorl %eax, %eax
callq 0x1040
pushq $0x1
jmp 0x15f6
leaq 0xaf4(%rip), %rdi       # 0x20e1
xorl %eax, %eax
callq 0x1040
pushq $-0x1
popq %rdi
callq 0x1080