// File: /workspace/github_cpp_makefile_star2_O3/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 0x14bc
movl $0x0, 0x8(%rsp)
leaq 0xcfd(%rip), %rdi       # 0x201b
xorl %eax, %eax
callq 0x1040
movq 0x2d5c(%rip), %rsi      # 0x4088
leaq 0xd24(%rip), %rdi       # 0x2057
xorl %eax, %eax
callq 0x1040
movq 0x2d47(%rip), %rdi      # 0x4088
leaq 0xd3c(%rip), %rsi       # 0x2084
callq 0x1070
testq %rax, %rax
je 0x163f
movq %rax, %r13
leaq 0x30(%rsp), %r15
movq %r15, %rdi
movl $0x50, %esi
movq %rax, %rdx
callq 0x10c0
leaq 0xd35(%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 0x14c6
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 0xc5b(%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 0x1490
leal -0x2(%r15), %esi
leaq 0xc24(%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 0x142a
jmp 0x14cc
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x162b
movl $0x3, %r15d
leaq 0xbef(%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 0x151f
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 0x1676
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1735
movq 0x2b6a(%rip), %rdi      # 0x4090
leaq 0xbb2(%rip), %rsi       # 0x20df
callq 0x1070
movq 0x2b57(%rip), %rsi      # 0x4090
testq %rax, %rax
je 0x165e
movq %rax, %r14
leaq 0xbb4(%rip), %rdi       # 0x2100
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x1040
movl 0x10(%rsp), %edx
leaq 0xc02(%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 0x1600
addq $0x8, %rbx
leaq 0xb8a(%rip), %r12       # 0x212b
leaq 0xb8d(%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 0x15e7
movq %r13, %rdi
movl %r15d, %esi
xorl %eax, %eax
callq 0x1040
incq %r15
movslq 0x8(%rsp), %rax
addq $0xc, %rbx
cmpq %rax, %r15
jl 0x15ab
addl $0x3, %r15d
xorl %ebp, %ebp
jmp 0x1606
movl $0x3, %r15d
movq %r14, %rdi
callq 0x1060
leaq 0xb32(%rip), %rdi       # 0x2147
movl %r15d, %esi
xorl %eax, %eax
callq 0x1040
leaq 0xc47(%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 0x2a42(%rip), %rsi      # 0x4088
leaq 0xa3a(%rip), %rdi       # 0x2087
xorl %eax, %eax
callq 0x1040
movl $0x1, %edi
callq 0x1080
leaq 0xa7c(%rip), %rdi       # 0x20e1
xorl %eax, %eax
callq 0x1040
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1080