// File: 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:
subq $0xa8, %rsp
movl $0x0, 0xa4(%rsp)
movl %edi, 0xa0(%rsp)
movq %rsi, 0x98(%rsp)
movl 0xa0(%rsp), %edi
movq 0x98(%rsp), %rsi
callq 0x1230
movl %eax, 0x3c(%rsp)
cmpl $0x0, 0x3c(%rsp)
jge 0x13b0
movl $0xffffffff, 0xa4(%rsp) # imm = 0xFFFFFFFF
jmp 0x1721
movl $0x0, 0x2c(%rsp)
leaq 0x1d69(%rip), %rdi      # 0x3128
movb $0x0, %al
callq 0x1040
movq 0x3cb3(%rip), %rsi      # 0x5080
leaq 0x1d90(%rip), %rdi      # 0x3164
movb $0x0, %al
callq 0x1040
movq 0x3c9e(%rip), %rdi      # 0x5080
leaq 0x1da8(%rip), %rsi      # 0x3191
callq 0x1070
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x10(%rsp)
jne 0x141a
movq 0x3c7e(%rip), %rsi      # 0x5080
leaq 0x1d8b(%rip), %rdi      # 0x3194
movb $0x0, %al
callq 0x1040
movl $0x1, %edi
callq 0x1080
leaq 0x40(%rsp), %rdi
movq 0x10(%rsp), %rdx
movl $0x50, %esi
callq 0x10b0
leaq 0x40(%rsp), %rdi
leaq 0x1d7d(%rip), %rsi      # 0x31b7
leaq 0x28(%rsp), %rdx
movb $0x0, %al
callq 0x1050
leaq 0x40(%rsp), %rdi
movq 0x10(%rsp), %rdx
movl $0x50, %esi
callq 0x10b0
leaq 0x40(%rsp), %rdi
leaq 0x1d51(%rip), %rsi      # 0x31b7
leaq 0x24(%rsp), %rdx
movb $0x0, %al
callq 0x1050
leaq 0x40(%rsp), %rdi
movq 0x10(%rsp), %rdx
movl $0x50, %esi
callq 0x10b0
leaq 0x40(%rsp), %rdi
leaq 0x1d25(%rip), %rsi      # 0x31b7
leaq 0x2c(%rsp), %rdx
movb $0x0, %al
callq 0x1050
movslq 0x2c(%rsp), %rax
movl $0xc, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, 0x18(%rsp)
movl $0x0, 0xc(%rsp)
leaq 0x40(%rsp), %rdi
movq 0x10(%rsp), %rdx
movl $0x50, %esi
callq 0x10b0
cmpq $0x0, %rax
je 0x158e
leaq 0x40(%rsp), %rdi
leaq 0x1cb6(%rip), %rsi      # 0x31b1
leaq 0x38(%rsp), %rdx
leaq 0x34(%rsp), %rcx
leaq 0x30(%rsp), %r8
movb $0x0, %al
callq 0x1050
movl 0x38(%rsp), %ecx
movq 0x18(%rsp), %rax
movslq 0xc(%rsp), %rdx
imulq $0xc, %rdx, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movl 0x34(%rsp), %ecx
movq 0x18(%rsp), %rax
movslq 0xc(%rsp), %rdx
imulq $0xc, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
movl 0x30(%rsp), %ecx
movq 0x18(%rsp), %rax
movslq 0xc(%rsp), %rdx
imulq $0xc, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
addl $0x3, %eax
movl $0x1e8480, %ecx         # imm = 0x1E8480
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x1589
movl 0xc(%rsp), %esi
leaq 0x1c38(%rip), %rdi      # 0x31ba
movb $0x0, %al
callq 0x1040
jmp 0x14d1
movl 0xc(%rsp), %esi
addl $0x3, %esi
leaq 0x1c33(%rip), %rdi      # 0x31cf
movb $0x0, %al
callq 0x1040
movq 0x10(%rsp), %rdi
callq 0x1060
movq 0x18(%rsp), %rdi
movq 0x18(%rsp), %rsi
movslq 0x2c(%rsp), %rax
imulq $0xc, %rax, %rax
addq %rax, %rsi
callq 0x1730
movq 0x3ab9(%rip), %rdi      # 0x5088
leaq 0x1c16(%rip), %rsi      # 0x31ec
callq 0x1070
movq %rax, (%rsp)
cmpq $0x0, (%rsp)
jne 0x1605
movq 0x3a9b(%rip), %rsi      # 0x5088
leaq 0x1bfa(%rip), %rdi      # 0x31ee
movb $0x0, %al
callq 0x1040
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1080
movq 0x3a7c(%rip), %rsi      # 0x5088
leaq 0x1bfa(%rip), %rdi      # 0x320d
movb $0x0, %al
callq 0x1040
movq (%rsp), %rdi
movl 0x28(%rsp), %edx
leaq 0x1c46(%rip), %rsi      # 0x326f
movb $0x0, %al
callq 0x10a0
movq (%rsp), %rdi
movl 0x24(%rsp), %edx
leaq 0x1c30(%rip), %rsi      # 0x326f
movb $0x0, %al
callq 0x10a0
movq (%rsp), %rdi
movl 0x2c(%rsp), %edx
leaq 0x1c1a(%rip), %rsi      # 0x326f
movb $0x0, %al
callq 0x10a0
movl $0x0, 0x3c(%rsp)
movl 0x3c(%rsp), %eax
cmpl 0x2c(%rsp), %eax
jge 0x16f5
movq (%rsp), %rdi
movq 0x18(%rsp), %rax
movslq 0x3c(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %edx
movq 0x18(%rsp), %rax
movslq 0x3c(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %ecx
movq 0x18(%rsp), %rax
movslq 0x3c(%rsp), %rsi
imulq $0xc, %rsi, %rsi
addq %rsi, %rax
movl 0x8(%rax), %r8d
leaq 0x1b7f(%rip), %rsi      # 0x3238
movb $0x0, %al
callq 0x10a0
movl 0x3c(%rsp), %eax
movl $0x1e8480, %ecx         # imm = 0x1E8480
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x16e3
movl 0x3c(%rsp), %esi
leaq 0x1b66(%rip), %rdi      # 0x3242
movb $0x0, %al
callq 0x1040
jmp 0x16e5
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x1664
movq (%rsp), %rdi
callq 0x1060
movl 0x3c(%rsp), %esi
addl $0x3, %esi
leaq 0x1b48(%rip), %rdi      # 0x3254
movb $0x0, %al
callq 0x1040
leaq 0x1b59(%rip), %rdi      # 0x3273
movb $0x0, %al
callq 0x1040
movl 0xa4(%rsp), %eax
addq $0xa8, %rsp
retq