// File: /workspace/github_cpp_makefile_star2_O1/hkbu-victor[P]SFastLDA/super_fast_final1_w.cpp
// Function: main
int main(int argc, char **argv){
  	char line[ 80 ];
    int n, i, j, docId, wordId, count;
    FILE *fr;

    parse_args(argc, argv);

    square_limit = -1;
    printf("\nFinal Sparse word-by-word: Opening and reading dataset file...\n");

    D = 0; W = 0; nTokens = 0;
    fr = fopen(dFile,"rt");
    if (fr == NULL){
       	printf("\n\nCannot find the UCI bag-of-words format input corpus file %s !!!\n\n\n", dFile);
       	exit(1);
    }
    fgets(line, 80, fr);
    sscanf(line, "%d", &D);
    fgets(line, 80, fr);
    sscanf(line, "%d", &W);
    fgets(line, 80, fr);

    int lineNo = 3;

    while (fgets(line, 80, fr) != NULL){
      	sscanf( line, "%d %d %d", &docId, &wordId, &count);
      	lineNo++;
      	nTokens += count;
      	if ((lineNo % 2000000) == 0)
			printf("first parse : processing line %d \n", lineNo);
    }
    printf("\nTotal documents: %d, Vocab Size : %d, Num. Tokens : %d\n", D, W, nTokens);

    //allocate memory
    d = (int *) calloc(nTokens, sizeof(int));
    w = (int *) calloc(nTokens, sizeof(int));
    z = (int *) calloc(nTokens, sizeof(int));
    doc_lens = (int *) calloc(D, sizeof(int));
    word_lens = (int *) calloc(W, sizeof(int));

    rewind(fr);

    fgets(line, 80, fr); fgets(line, 80, fr); fgets(line, 80, fr);
    lineNo = 3;
    n = 0;
    while (fgets(line, 80, fr) != NULL){
    	sscanf( line, "%d %d %d", &docId, &wordId, &count);
    	lineNo++;
    	doc_lens[ docId-1 ] += count;
    	word_lens[ wordId-1 ] += count;
    	for (i=0; i<count; i++){
    		d[n] = docId - 1;
    		w[n] = wordId - 1;
    		n++;
    	}
    	if ((lineNo % 1000000) == 0)
    		printf("second parse : processing line %d \n", lineNo);
    }
    fclose(fr);

    for (i=0; i<W; i++){
    	if (word_lens[i] > square_limit)
    		square_limit = word_lens[i];
    }

    printf( "Arguments:\n" );
    printf( "\tNumber of words      W = %d\n"    , W );
    printf( "\tNumber of docs       D = %d\n"    , D );
    printf( "\tNumber of topics     T = %d\n"    , T );
    printf( "\tNumber of iterations N = %d\n"    , NN );
    printf( "\tHyperparameter   ALPHA = %4.4f\n" , argALPHA );
    printf( "\tHyperparameter    BETA = %4.4f\n" , BETA );
    printf( "\tSeed number            = %d\n"    , SEED );
    printf( "\tNumber of tokens       = %d\n"    , nTokens );
    printf( "\tSquare Limit           = %d\n"    , square_limit);

    if (oFile != NULL){
    	printf("\n\tOutput file: %s.\n\n", oFile);
    }else{
    	printf("\n\tNo output file specified.\n\n");
    }


    ztot = new int[T]; std::fill_n(ztot, T, 0);
    ALPHA = new double[T]; std::fill_n(ALPHA, T, argALPHA);
    BETA_D_2_norm = new double[D];	//summation over topics of ALPHA*wi
    D_pair_count = new int[D]; std::fill_n(D_pair_count, D, 0);
    W_pair_count = new int[W]; std::fill_n(W_pair_count, W, 0);
    pivotal_topic_indexes = new int[T];
    pivotal_topic_values = new double[T];
    d_pivotal_topic_indexes = new int[T];
    d_pivotal_topic_values = new double[T];
    common_topic_pos = new int[T];
    ONE_OVER_Z = new double[T];
    square_increment = new long[square_limit];
    square_decrement = new long[square_limit];
    square_of = new long[square_limit];
    wi_array = new int[T];
    wi_index_to_pos = new int[T];

    wpIdx = new W_DoublePair*[W];
    wpVal = new int*[W];
    for (i=0; i<W; i++){
   		wpIdx[i] = new W_DoublePair[T];
    	wpVal[i] = new int[T];
    	for (j=0; j<T; j++){
    		wpIdx[i][j].index = -1;
    	}
		std::fill_n(wpVal[i], T, 0);
    }

    dpPair = new D_DoublePair*[D];
    for (i=0; i<D; i++){
    	if (doc_lens[i] < T){
        	dpPair[i] = new D_DoublePair[doc_lens[i]];
        	di_pair = dpPair[i];
    		for (j=0; j<doc_lens[i]; j++){
    			di_pair[j].index = -1;
    			di_pair[j].value = 0;
    		}
    	}else{
        	dpPair[i] = new D_DoublePair[T];
        	di_pair = dpPair[i];
    		for (j=0; j<T; j++){
    			di_pair[j].index = -1;
    			di_pair[j].value = 0;
    		}
    	}
    }

    std::fill_n(wi_index_to_pos, T, -1);

    GibbsSamplerLDA();

    if (oFile != NULL){
    	export_results(oFile);
    	printf("\nOutput file is written to %s", oFile);
    }

    printf("\nFinished !!!\n");
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
callq 0x1335
movl $0xffffffff, 0x40fd(%rip) # imm = 0xFFFFFFFF
leaq 0x25a2(%rip), %rdi      # 0x55d8
callq 0x1140
xorl %eax, %eax
movl %eax, 0x40d1(%rip)      # 0x7114
movl %eax, 0x40cf(%rip)      # 0x7118
movl %eax, 0x40d9(%rip)      # 0x7128
movq 0x408a(%rip), %rdi      # 0x70e0
leaq 0x20ac(%rip), %rsi      # 0x5109
callq 0x10f0
testq %rax, %rax
je 0x38fd
movq %rax, %rbx
leaq 0x10(%rsp), %r14
movq %r14, %rdi
movl $0x50, %esi
movq %rax, %rdx
callq 0x1160
leaq 0x20ce(%rip), %r15      # 0x5158
leaq 0x4083(%rip), %rdx      # 0x7114
movq %r14, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x10d0
movq %r14, %rdi
movl $0x50, %esi
movq %rbx, %rdx
callq 0x1160
leaq 0x4063(%rip), %rdx      # 0x7118
movq %r14, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x10d0
movq %r14, %rdi
movl $0x50, %esi
movq %rbx, %rdx
callq 0x1160
movq %r14, %rdi
movl $0x50, %esi
movq %rbx, %rdx
callq 0x1160
testq %rax, %rax
je 0x316b
movl $0xfffffffc, %r12d      # imm = 0xFFFFFFFC
movl $0x4, %ebp
leaq 0x10(%rsp), %r15
leaq 0x4(%rsp), %r14
movl %ebp, %eax
imulq $0x431bde83, %rax, %rax # imm = 0x431BDE83
shrq $0x33, %rax
imull $0x1e8480, %eax, %r13d  # imm = 0x1E8480
movq %r15, %rdi
leaq 0x2034(%rip), %rsi      # 0x5152
leaq 0xc(%rsp), %rdx
leaq 0x8(%rsp), %rcx
movq %r14, %r8
xorl %eax, %eax
callq 0x10d0
movl 0x4(%rsp), %eax
addl %eax, 0x3fec(%rip)      # 0x7128
addl %r12d, %r13d
jne 0x3151
leaq 0x2013(%rip), %rdi      # 0x515b
movl %ebp, %esi
xorl %eax, %eax
callq 0x1040
movq %r15, %rdi
movl $0x50, %esi
movq %rbx, %rdx
callq 0x1160
decl %r12d
incl %ebp
testq %rax, %rax
jne 0x3100
movl 0x3fa3(%rip), %esi      # 0x7114
movl 0x3fa1(%rip), %edx      # 0x7118
movl 0x3fab(%rip), %ecx      # 0x7128
leaq 0x1ffa(%rip), %rdi      # 0x517e
xorl %eax, %eax
callq 0x1040
movslq 0x3f96(%rip), %r14      # 0x7128
movl $0x4, %esi
movq %r14, %rdi
callq 0x1080
movq %rax, 0x3faa(%rip)      # 0x7150
movl $0x4, %esi
movq %r14, %rdi
callq 0x1080
movq %rax, 0x3f9e(%rip)      # 0x7158
movl $0x4, %esi
movq %r14, %rdi
callq 0x1080
movq %rax, 0x3f92(%rip)      # 0x7160
movslq 0x3f3f(%rip), %rdi      # 0x7114
movl $0x4, %esi
callq 0x1080
movq %rax, 0x3f82(%rip)      # 0x7168
movslq 0x3f2b(%rip), %rdi      # 0x7118
movl $0x4, %esi
callq 0x1080
movq %rax, 0x3f72(%rip)      # 0x7170
movq %rbx, %rdi
callq 0x1050
leaq 0x10(%rsp), %r14
movq %r14, %rdi
movl $0x50, %esi
movq %rbx, %rdx
callq 0x1160
movq %r14, %rdi
movl $0x50, %esi
movq %rbx, %rdx
callq 0x1160
movq %r14, %rdi
movl $0x50, %esi
movq %rbx, %rdx
callq 0x1160
movq %r14, %rdi
movl $0x50, %esi
movq %rbx, %rdx
callq 0x1160
testq %rax, %rax
je 0x3329
movl $0x3, %ebp
leaq 0x10(%rsp), %r15
leaq 0x8(%rsp), %r13
leaq 0x4(%rsp), %r14
xorl %r12d, %r12d
movq %r15, %rdi
leaq 0x1edd(%rip), %rsi      # 0x5152
leaq 0xc(%rsp), %rdx
movq %r13, %rcx
movq %r14, %r8
xorl %eax, %eax
callq 0x10d0
movl 0x4(%rsp), %eax
movq 0x3ed6(%rip), %rcx      # 0x7168
movslq 0xc(%rsp), %rdx
addl %eax, -0x4(%rcx,%rdx,4)
movl 0x4(%rsp), %eax
movq 0x3eca(%rip), %rcx      # 0x7170
movslq 0x8(%rsp), %rdx
addl %eax, -0x4(%rcx,%rdx,4)
cmpl $0x0, 0x4(%rsp)
jle 0x32ee
movslq %r12d, %rax
shlq $0x2, %rax
movq 0x3e8c(%rip), %rcx      # 0x7150
addq %rax, %rcx
addq 0x3e8a(%rip), %rax      # 0x7158
xorl %edx, %edx
movl 0xc(%rsp), %esi
decl %esi
movl %esi, (%rcx,%rdx,4)
movl 0x8(%rsp), %esi
decl %esi
movl %esi, (%rax,%rdx,4)
incq %rdx
cmpl 0x4(%rsp), %edx
jl 0x32d0
addl %edx, %r12d
incl %ebp
imull $0x68c26139, %ebp, %eax # imm = 0x68C26139
rorl $0x6, %eax
cmpl $0x10c6, %eax           # imm = 0x10C6
ja 0x3310
leaq 0x1eb0(%rip), %rdi      # 0x51b7
movl %ebp, %esi
xorl %eax, %eax
callq 0x1040
movq %r15, %rdi
movl $0x50, %esi
movq %rbx, %rdx
callq 0x1160
testq %rax, %rax
jne 0x326b
movq %rbx, %rdi
callq 0x10e0
movslq 0x3de0(%rip), %rax      # 0x7118
testq %rax, %rax
jle 0x3363
movl 0x3de9(%rip), %esi      # 0x712c
movq 0x3e26(%rip), %rcx      # 0x7170
xorl %edx, %edx
movl (%rcx,%rdx,4), %edi
cmpl %esi, %edi
jle 0x335b
movl %edi, 0x3dd3(%rip)      # 0x712c
movl %edi, %esi
incq %rdx
cmpq %rdx, %rax
jne 0x334c
leaq 0x22ae(%rip), %rdi      # 0x5618
callq 0x1140
movl 0x3da3(%rip), %esi      # 0x7118
leaq 0x1e5f(%rip), %rdi      # 0x51db
xorl %eax, %eax
callq 0x1040
movl 0x3d8b(%rip), %esi      # 0x7114
leaq 0x1e69(%rip), %rdi      # 0x51f9
xorl %eax, %eax
callq 0x1040
movl 0x3d6b(%rip), %esi      # 0x7108
leaq 0x1e73(%rip), %rdi      # 0x5217
xorl %eax, %eax
callq 0x1040
movl 0x3d5b(%rip), %esi      # 0x710c
leaq 0x1e7d(%rip), %rdi      # 0x5235
xorl %eax, %eax
callq 0x1040
movsd 0x3dc1(%rip), %xmm0     # 0x7188
leaq 0x1e85(%rip), %rdi      # 0x5253
movb $0x1, %al
callq 0x1040
movsd 0x3db3(%rip), %xmm0     # 0x7190
leaq 0x1e90(%rip), %rdi      # 0x5274
movb $0x1, %al
callq 0x1040
movl 0x3d1f(%rip), %esi      # 0x7110
leaq 0x1e9d(%rip), %rdi      # 0x5295
xorl %eax, %eax
callq 0x1040
movl 0x3d23(%rip), %esi      # 0x7128
leaq 0x1ea7(%rip), %rdi      # 0x52b3
xorl %eax, %eax
callq 0x1040
movl 0x3d13(%rip), %esi      # 0x712c
leaq 0x1eb1(%rip), %rdi      # 0x52d1
xorl %eax, %eax
callq 0x1040
movq 0x3cba(%rip), %rsi      # 0x70e8
testq %rsi, %rsi
je 0x3443
leaq 0x1eb5(%rip), %rdi      # 0x52ef
xorl %eax, %eax
callq 0x1040
jmp 0x344f
leaq 0x21d9(%rip), %rdi      # 0x5623
callq 0x1140
movslq 0x3cb2(%rip), %r15      # 0x7108
leaq (,%r15,4), %r14
testq %r15, %r15
movq $-0x1, %r12
movq %r14, %rbx
cmovsq %r12, %rbx
movq %rbx, %rdi
callq 0x1030
movq %rax, 0x3cb2(%rip)      # 0x7130
testq %r15, %r15
jle 0x3490
movq %rax, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x1070
leaq (,%r15,8), %r14
testl %r15d, %r15d
cmovsq %r12, %r14
movq %r14, %rdi
callq 0x1030
movq %rax, 0x3d02(%rip)      # 0x71b0
testl %r15d, %r15d
jle 0x3561
movsd 0x3cc9(%rip), %xmm0     # 0x7188
movabsq $0x1fffffffffffffff, %rdx # imm = 0x1FFFFFFFFFFFFFFF
addq %rdx, %r15
andq %r15, %rdx
andl $0x1, %r15d
negq %r15
leaq (%rdx,%r15), %rcx
addq $0x2, %rcx
movq %rdx, %xmm1
pshufd $0x44, %xmm1, %xmm1     # xmm1 = xmm1[0,1,0,1]
xorl %edx, %edx
movdqa 0x1b2e(%rip), %xmm2     # 0x5020
movdqa 0x1b36(%rip), %xmm3     # 0x5030
pxor %xmm3, %xmm1
pcmpeqd %xmm4, %xmm4
movq %rdx, %xmm5
pshufd $0x44, %xmm5, %xmm5     # xmm5 = xmm5[0,1,0,1]
por %xmm2, %xmm5
pxor %xmm3, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm1, %xmm6
pcmpeqd %xmm1, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
movd %xmm5, %esi
notl %esi
testb $0x1, %sil
je 0x3543
movsd %xmm0, (%rax,%rdx,8)
pxor %xmm4, %xmm5
pextrw $0x4, %xmm5, %esi
testb $0x1, %sil
je 0x3558
movsd %xmm0, 0x8(%rax,%rdx,8)
addq $0x2, %rdx
cmpq %rdx, %rcx
jne 0x3502
movslq 0x3bac(%rip), %rbp      # 0x7114
leaq (,%rbp,8), %rdi
leaq (,%rbp,4), %r12
testq %rbp, %rbp
movq $-0x1, %r15
cmovsq %r15, %rdi
movq %r12, %r13
cmovsq %r15, %r13
callq 0x1030
movq %rax, 0x3c37(%rip)      # 0x71d0
movq %r13, %rdi
callq 0x1030
movq %rax, 0x3bd0(%rip)      # 0x7178
testq %rbp, %rbp
jle 0x35ba
movq %rax, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x1070
movslq 0x3b57(%rip), %r13      # 0x7118
leaq (,%r13,4), %r12
testq %r13, %r13
cmovnsq %r12, %r15
movq %r15, %rdi
callq 0x1030
movq %rax, 0x3ba1(%rip)      # 0x7180
testq %r13, %r13
jle 0x35f1
movq %rax, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x1070
movq %rbx, %rdi
callq 0x1030
movq %rax, 0x3b38(%rip)      # 0x7138
movq %r14, %rdi
callq 0x1030
movq %rax, 0x3ba9(%rip)      # 0x71b8
movq %rbx, %rdi
callq 0x1030
movq %rax, 0x3b22(%rip)      # 0x7140
movq %r14, %rdi
callq 0x1030
movq %rax, 0x3b93(%rip)      # 0x71c0
movq %rbx, %rdi
callq 0x1030
movq %rax, 0x3b0c(%rip)      # 0x7148
movq %r14, %rdi
callq 0x1030
movq %rax, 0x3b7d(%rip)      # 0x71c8
movslq 0x3ada(%rip), %rax      # 0x712c
leaq (,%rax,8), %r14
testq %rax, %rax
movq $-0x1, %r15
cmovsq %r15, %r14
movq %r14, %rdi
callq 0x1030
movq %rax, 0x3a79(%rip)      # 0x70f0
movq %r14, %rdi
callq 0x1030
movq %rax, 0x3a72(%rip)      # 0x70f8
movq %r14, %rdi
callq 0x1030
movq %rax, 0x3a6b(%rip)      # 0x7100
movq %rbx, %rdi
callq 0x1030
movq %rax, 0x3b44(%rip)      # 0x71e8
movq %rbx, %rdi
callq 0x1030
movq %r13, %rbx
shlq $0x3, %rbx
testl %r13d, %r13d
movq %rax, 0x3b33(%rip)      # 0x71f0
cmovsq %r15, %rbx
movq %rbx, %rdi
callq 0x1030
movq %rax, 0x3b50(%rip)      # 0x7220
movq %rbx, %rdi
callq 0x1030
movq %rax, 0x3b01(%rip)      # 0x71e0
cmpl $0x0, 0x3a32(%rip)      # 0x7118
jle 0x3790
xorl %r14d, %r14d
movslq 0x3a12(%rip), %rax      # 0x7108
leaq (,%rax,8), %rdi
testq %rax, %rax
cmovsq %r15, %rdi
leaq (,%rax,4), %rbx
cmovsq %r15, %rbx
callq 0x1030
movq 0x3b03(%rip), %rcx      # 0x7220
movq %rax, (%rcx,%r14,8)
movq %rbx, %rdi
callq 0x1030
movq 0x3ab0(%rip), %rcx      # 0x71e0
movq %rax, (%rcx,%r14,8)
movl 0x39ce(%rip), %eax      # 0x7108
testl %eax, %eax
jle 0x3761
movq 0x3adb(%rip), %rax      # 0x7220
movq (%rax,%r14,8), %rcx
xorl %edx, %edx
movl $0xffffffff, (%rcx,%rdx,8) # imm = 0xFFFFFFFF
incq %rdx
movslq 0x39ac(%rip), %rax      # 0x7108
cmpq %rax, %rdx
jl 0x374b
testl %eax, %eax
jle 0x377d
movq 0x3a74(%rip), %rcx      # 0x71e0
movq (%rcx,%r14,8), %rdi
movl %eax, %edx
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x1070
incq %r14
movslq 0x3991(%rip), %rax      # 0x7118
cmpq %rax, %r14
jl 0x36ef
movslq 0x397d(%rip), %rax      # 0x7114
leaq (,%rax,8), %rdi
testq %rax, %rax
cmovsq %r15, %rdi
callq 0x1030
movq %rax, 0x3a7e(%rip)      # 0x7230
cmpl $0x0, 0x395b(%rip)      # 0x7114
jle 0x3894
xorl %ebx, %ebx
movq $-0x1, %r14
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movq 0x3993(%rip), %rax      # 0x7168
movslq (%rax,%rbx,4), %rax
movslq 0x3928(%rip), %rcx      # 0x7108
cmpl %ecx, %eax
jge 0x3834
movq %rax, %rdi
shlq $0x3, %rdi
testl %eax, %eax
cmovsq %r14, %rdi
callq 0x1030
movq 0x3a33(%rip), %rcx      # 0x7230
movq %rax, (%rcx,%rbx,8)
movq 0x3a28(%rip), %rax      # 0x7230
movq (%rax,%rbx,8), %rax
movq %rax, 0x3a15(%rip)      # 0x7228
movq 0x394e(%rip), %rcx      # 0x7168
cmpl $0x0, (%rcx,%rbx,4)
jle 0x3881
xorl %edx, %edx
movq %r15, (%rax,%rdx,8)
incq %rdx
movslq (%rcx,%rbx,4), %rsi
cmpq %rsi, %rdx
jl 0x3822
jmp 0x3881
movq %rcx, %rdi
shlq $0x3, %rdi
testl %ecx, %ecx
cmovsq %r14, %rdi
callq 0x1030
movq 0x39e3(%rip), %rcx      # 0x7230
movq %rax, (%rcx,%rbx,8)
movq 0x39d8(%rip), %rax      # 0x7230
movq (%rax,%rbx,8), %rax
movq %rax, 0x39c5(%rip)      # 0x7228
cmpl $0x0, 0x389e(%rip)      # 0x7108
jle 0x3881
xorl %ecx, %ecx
movq %r15, (%rax,%rcx,8)
incq %rcx
movslq 0x388c(%rip), %rdx      # 0x7108
cmpq %rdx, %rcx
jl 0x386e
incq %rbx
movslq 0x3889(%rip), %rax      # 0x7114
cmpq %rax, %rbx
jl 0x37ce
movslq 0x386d(%rip), %rdx      # 0x7108
testq %rdx, %rdx
jle 0x38b5
movq 0x3949(%rip), %rdi      # 0x71f0
shlq $0x2, %rdx
movl $0xff, %esi
callq 0x1070
callq 0x1c04
movq 0x3827(%rip), %rdi      # 0x70e8
testq %rdi, %rdi
je 0x38e0
callq 0x14f3
movq 0x3816(%rip), %rsi      # 0x70e8
leaq 0x1a2b(%rip), %rdi      # 0x5304
xorl %eax, %eax
callq 0x1040
leaq 0x1d59(%rip), %rdi      # 0x5640
callq 0x1140
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x37dc(%rip), %rsi      # 0x70e0
leaq 0x1801(%rip), %rdi      # 0x510c
xorl %eax, %eax
callq 0x1040
movl $0x1, %edi
callq 0x1100