// File: /workspace/github_cpp_makefile_star2_O3/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 $0x88, %rsp
callq 0x1335
movl $0xffffffff, 0x4080(%rip) # imm = 0xFFFFFFFF
leaq 0x2525(%rip), %rdi      # 0x55d8
callq 0x1140
xorl %eax, %eax
movl %eax, 0x4054(%rip)      # 0x7114
movl %eax, 0x4052(%rip)      # 0x7118
movl %eax, 0x405c(%rip)      # 0x7128
movq 0x400d(%rip), %rdi      # 0x70e0
leaq 0x202f(%rip), %rsi      # 0x5109
callq 0x10f0
testq %rax, %rax
je 0x3985
movq %rax, %rbx
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movl $0x50, %esi
movq %rax, %rdx
callq 0x1160
leaq 0x2051(%rip), %r15      # 0x5158
leaq 0x4006(%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 0x3fe6(%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 0x31e8
movl $0xfffffffc, %r12d      # imm = 0xFFFFFFFC
movl $0x4, %ebp
leaq 0x30(%rsp), %r15
leaq 0xc(%rsp), %r14
movl %ebp, %eax
imulq $0x431bde83, %rax, %rax # imm = 0x431BDE83
shrq $0x33, %rax
imull $0x1e8480, %eax, %r13d  # imm = 0x1E8480
movq %r15, %rdi
leaq 0x1fb7(%rip), %rsi      # 0x5152
leaq 0x14(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movq %r14, %r8
xorl %eax, %eax
callq 0x10d0
movl 0xc(%rsp), %eax
addl %eax, 0x3f6f(%rip)      # 0x7128
addl %r12d, %r13d
jne 0x31ce
leaq 0x1f96(%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 0x317d
movl 0x3f26(%rip), %esi      # 0x7114
movl 0x3f24(%rip), %edx      # 0x7118
movl 0x3f2e(%rip), %ecx      # 0x7128
leaq 0x1f7d(%rip), %rdi      # 0x517e
xorl %eax, %eax
callq 0x1040
movslq 0x3f19(%rip), %r14      # 0x7128
movl $0x4, %esi
movq %r14, %rdi
callq 0x1080
movq %rax, 0x3f2d(%rip)      # 0x7150
movl $0x4, %esi
movq %r14, %rdi
callq 0x1080
movq %rax, 0x3f21(%rip)      # 0x7158
movl $0x4, %esi
movq %r14, %rdi
callq 0x1080
movq %rax, 0x3f15(%rip)      # 0x7160
movslq 0x3ec2(%rip), %rdi      # 0x7114
movl $0x4, %esi
callq 0x1080
movq %rax, 0x3f05(%rip)      # 0x7168
movslq 0x3eae(%rip), %rdi      # 0x7118
movl $0x4, %esi
callq 0x1080
movq %rax, 0x3ef5(%rip)      # 0x7170
movq %rbx, %rdi
callq 0x1050
leaq 0x30(%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 0x33a6
movl $0x3, %ebp
leaq 0x30(%rsp), %r15
leaq 0x10(%rsp), %r13
leaq 0xc(%rsp), %r14
xorl %r12d, %r12d
movq %r15, %rdi
leaq 0x1e60(%rip), %rsi      # 0x5152
leaq 0x14(%rsp), %rdx
movq %r13, %rcx
movq %r14, %r8
xorl %eax, %eax
callq 0x10d0
movl 0xc(%rsp), %eax
movq 0x3e59(%rip), %rcx      # 0x7168
movslq 0x14(%rsp), %rdx
addl %eax, -0x4(%rcx,%rdx,4)
movl 0xc(%rsp), %eax
movq 0x3e4d(%rip), %rcx      # 0x7170
movslq 0x10(%rsp), %rdx
addl %eax, -0x4(%rcx,%rdx,4)
cmpl $0x0, 0xc(%rsp)
jle 0x336b
movslq %r12d, %rax
shlq $0x2, %rax
movq 0x3e0f(%rip), %rcx      # 0x7150
addq %rax, %rcx
addq 0x3e0d(%rip), %rax      # 0x7158
xorl %edx, %edx
movl 0x14(%rsp), %esi
decl %esi
movl %esi, (%rcx,%rdx,4)
movl 0x10(%rsp), %esi
decl %esi
movl %esi, (%rax,%rdx,4)
incq %rdx
cmpl 0xc(%rsp), %edx
jl 0x334d
addl %edx, %r12d
incl %ebp
imull $0x68c26139, %ebp, %eax # imm = 0x68C26139
rorl $0x6, %eax
cmpl $0x10c6, %eax           # imm = 0x10C6
ja 0x338d
leaq 0x1e33(%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 0x32e8
movq %rbx, %rdi
callq 0x10e0
movslq 0x3d63(%rip), %rax      # 0x7118
testq %rax, %rax
jle 0x33e0
movl 0x3d6c(%rip), %esi      # 0x712c
movq 0x3da9(%rip), %rcx      # 0x7170
xorl %edx, %edx
movl (%rcx,%rdx,4), %edi
cmpl %esi, %edi
jle 0x33d8
movl %edi, 0x3d56(%rip)      # 0x712c
movl %edi, %esi
incq %rdx
cmpq %rdx, %rax
jne 0x33c9
leaq 0x2231(%rip), %rdi      # 0x5618
callq 0x1140
movl 0x3d26(%rip), %esi      # 0x7118
leaq 0x1de2(%rip), %rdi      # 0x51db
xorl %eax, %eax
callq 0x1040
movl 0x3d0e(%rip), %esi      # 0x7114
leaq 0x1dec(%rip), %rdi      # 0x51f9
xorl %eax, %eax
callq 0x1040
movl 0x3cee(%rip), %esi      # 0x7108
leaq 0x1df6(%rip), %rdi      # 0x5217
xorl %eax, %eax
callq 0x1040
movl 0x3cde(%rip), %esi      # 0x710c
leaq 0x1e00(%rip), %rdi      # 0x5235
xorl %eax, %eax
callq 0x1040
movsd 0x3d44(%rip), %xmm0     # 0x7188
leaq 0x1e08(%rip), %rdi      # 0x5253
movb $0x1, %al
callq 0x1040
movsd 0x3d36(%rip), %xmm0     # 0x7190
leaq 0x1e13(%rip), %rdi      # 0x5274
movb $0x1, %al
callq 0x1040
movl 0x3ca2(%rip), %esi      # 0x7110
leaq 0x1e20(%rip), %rdi      # 0x5295
xorl %eax, %eax
callq 0x1040
movl 0x3ca6(%rip), %esi      # 0x7128
leaq 0x1e2a(%rip), %rdi      # 0x52b3
xorl %eax, %eax
callq 0x1040
movl 0x3c96(%rip), %esi      # 0x712c
leaq 0x1e34(%rip), %rdi      # 0x52d1
xorl %eax, %eax
callq 0x1040
movq 0x3c3d(%rip), %rsi      # 0x70e8
testq %rsi, %rsi
je 0x34c0
leaq 0x1e38(%rip), %rdi      # 0x52ef
xorl %eax, %eax
callq 0x1040
jmp 0x34cc
leaq 0x215c(%rip), %rdi      # 0x5623
callq 0x1140
movslq 0x3c35(%rip), %rbx      # 0x7108
leaq (,%rbx,4), %r15
testq %rbx, %rbx
movq $-0x1, %r14
movq %r15, %rdi
cmovsq %r14, %rdi
movq %rdi, 0x28(%rsp)
callq 0x1030
movq %rax, 0x3c33(%rip)      # 0x7130
testq %rbx, %rbx
jle 0x350f
movq %rax, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x1070
leaq (,%rbx,8), %rdi
testl %ebx, %ebx
cmovsq %r14, %rdi
movq %rdi, 0x20(%rsp)
callq 0x1030
movq %rax, 0x3c82(%rip)      # 0x71b0
testl %ebx, %ebx
jle 0x35e1
movsd 0x3c4a(%rip), %xmm0     # 0x7188
movabsq $0x1fffffffffffffff, %rdx # imm = 0x1FFFFFFFFFFFFFFF
movq %rbx, %rcx
addq %rdx, %rcx
andq %rcx, %rdx
andl $0x1, %ecx
negq %rcx
addq %rdx, %rcx
addq $0x2, %rcx
movq %rdx, %xmm1
pshufd $0x44, %xmm1, %xmm1     # xmm1 = xmm1[0,1,0,1]
xorl %edx, %edx
movdqa 0x1aae(%rip), %xmm2     # 0x5020
movdqa 0x1ab6(%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 0x35c3
movsd %xmm0, (%rax,%rdx,8)
pxor %xmm4, %xmm5
pextrw $0x4, %xmm5, %esi
testb $0x1, %sil
je 0x35d8
movsd %xmm0, 0x8(%rax,%rdx,8)
addq $0x2, %rdx
cmpq %rdx, %rcx
jne 0x3582
movslq 0x3b2c(%rip), %r12      # 0x7114
leaq (,%r12,8), %rdi
leaq (,%r12,4), %r14
testq %r12, %r12
movq $-0x1, %r13
movq %rdi, 0x18(%rsp)
cmovsq %r13, %rdi
movq %r14, %rbp
cmovsq %r13, %rbp
callq 0x1030
movq %rax, 0x3bb2(%rip)      # 0x71d0
movq %rbp, %rdi
callq 0x1030
movq %rax, 0x3b4b(%rip)      # 0x7178
testq %r12, %r12
jle 0x363f
movq %rax, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x1070
movslq 0x3ad2(%rip), %r15      # 0x7118
leaq (,%r15,4), %rbp
testq %r15, %r15
cmovnsq %rbp, %r13
movq %r13, %rdi
callq 0x1030
movq %rax, 0x3b1c(%rip)      # 0x7180
testq %r15, %r15
jle 0x3676
movq %rax, %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x1070
movq 0x28(%rsp), %rbp
movq %rbp, %rdi
callq 0x1030
movq %rax, 0x3aae(%rip)      # 0x7138
movq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0x1030
movq %rax, 0x3b1a(%rip)      # 0x71b8
movq %rbp, %rdi
callq 0x1030
movq %rax, 0x3a93(%rip)      # 0x7140
movq %r14, %rdi
callq 0x1030
movq %rax, 0x3b04(%rip)      # 0x71c0
movq %rbp, %rdi
callq 0x1030
movq %rax, 0x3a7d(%rip)      # 0x7148
movq %r14, %rdi
callq 0x1030
movq %rax, 0x3aee(%rip)      # 0x71c8
movslq 0x3a4b(%rip), %rax      # 0x712c
leaq (,%rax,8), %r14
testq %rax, %rax
movq $-0x1, %r13
cmovsq %r13, %r14
movq %r14, %rdi
callq 0x1030
movq %rax, 0x39ea(%rip)      # 0x70f0
movq %r14, %rdi
callq 0x1030
movq %rax, 0x39e3(%rip)      # 0x70f8
movq %r14, %rdi
callq 0x1030
movq %rax, 0x39dc(%rip)      # 0x7100
movq %rbp, %rdi
callq 0x1030
movq %rax, 0x3ab5(%rip)      # 0x71e8
movq %rbp, %rdi
callq 0x1030
movq %r15, %r14
shlq $0x3, %r14
testl %r15d, %r15d
movq %rax, 0x3aa4(%rip)      # 0x71f0
cmovsq %r13, %r14
movq %r14, %rdi
callq 0x1030
movq %rax, 0x3ac1(%rip)      # 0x7220
movq %r14, %rdi
callq 0x1030
movq %rax, 0x3a72(%rip)      # 0x71e0
testl %r15d, %r15d
jle 0x3829
xorl %r15d, %r15d
movslq %ebx, %r12
leaq (,%r12,8), %rdi
testl %r12d, %r12d
cmovsq %r13, %rdi
leaq (,%r12,4), %r14
cmovsq %r13, %r14
callq 0x1030
movq 0x3a7c(%rip), %rcx      # 0x7220
movq %rax, (%rcx,%r15,8)
movq %r14, %rdi
callq 0x1030
movq 0x3a29(%rip), %rcx      # 0x71e0
movq %rax, (%rcx,%r15,8)
testl %r12d, %r12d
jle 0x3806
movq 0x3a59(%rip), %rax      # 0x7220
movq (%rax,%r15,8), %rax
xorl %ecx, %ecx
movl $0xffffffff, (%rax,%rcx,8) # imm = 0xFFFFFFFF
incq %rcx
movslq 0x392a(%rip), %rbx      # 0x7108
cmpq %rbx, %rcx
jl 0x37cd
testl %ebx, %ebx
jle 0x3806
movq 0x39f2(%rip), %rax      # 0x71e0
movq (%rax,%r15,8), %rdi
shlq $0x2, %rbx
xorl %esi, %esi
movq %rbx, %rdx
callq 0x1070
movl 0x3902(%rip), %ebx      # 0x7108
incq %r15
movslq 0x3908(%rip), %rax      # 0x7118
cmpq %rax, %r15
jl 0x377a
movslq 0x38f4(%rip), %r12      # 0x7114
movq %r12, %rdi
shlq $0x3, %rdi
jmp 0x382e
movq 0x18(%rsp), %rdi
testl %r12d, %r12d
cmovsq %r13, %rdi
callq 0x1030
movq %rax, 0x39ef(%rip)      # 0x7230
testl %r12d, %r12d
jle 0x391f
movq %rax, %r14
movq 0x3914(%rip), %rax      # 0x7168
xorl %ebx, %ebx
movq $-0x1, %r15
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movslq (%rax,%rbx,4), %rax
movslq 0x389a(%rip), %r13      # 0x7108
cmpl %r13d, %eax
jge 0x38bc
movq %rax, %rdi
shlq $0x3, %rdi
testl %eax, %eax
cmovsq %r15, %rdi
callq 0x1030
movq %rax, (%r14,%rbx,8)
movq 0x39a0(%rip), %r14      # 0x7230
movq (%r14,%rbx,8), %rcx
movq %rcx, 0x398d(%rip)      # 0x7228
movq 0x38c6(%rip), %rax      # 0x7168
cmpl $0x0, (%rax,%rbx,4)
jle 0x3906
xorl %edx, %edx
movq %r12, (%rcx,%rdx,8)
incq %rdx
movslq (%rax,%rbx,4), %rsi
cmpq %rsi, %rdx
jl 0x38aa
jmp 0x3906
movq %r13, %rdi
shlq $0x3, %rdi
testl %r13d, %r13d
cmovsq %r15, %rdi
callq 0x1030
movq %rax, (%r14,%rbx,8)
movq 0x3956(%rip), %r14      # 0x7230
movq (%r14,%rbx,8), %rcx
movq %rcx, 0x3943(%rip)      # 0x7228
movq 0x387c(%rip), %rax      # 0x7168
testl %r13d, %r13d
jle 0x3906
xorl %edx, %edx
movq %r12, (%rcx,%rdx,8)
incq %rdx
movslq 0x3807(%rip), %rsi      # 0x7108
cmpq %rsi, %rdx
jl 0x38f3
incq %rbx
movslq 0x3804(%rip), %rcx      # 0x7114
cmpq %rcx, %rbx
jl 0x3863
movl 0x37e9(%rip), %ebx      # 0x7108
testl %ebx, %ebx
jle 0x393a
movq 0x38c6(%rip), %rdi      # 0x71f0
movl %ebx, %edx
shlq $0x2, %rdx
movl $0xff, %esi
callq 0x1070
callq 0x1c11
movq 0x37a2(%rip), %rdi      # 0x70e8
testq %rdi, %rdi
je 0x3965
callq 0x14f3
movq 0x3791(%rip), %rsi      # 0x70e8
leaq 0x19a6(%rip), %rdi      # 0x5304
xorl %eax, %eax
callq 0x1040
leaq 0x1cd4(%rip), %rdi      # 0x5640
callq 0x1140
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x3754(%rip), %rsi      # 0x70e0
leaq 0x1779(%rip), %rdi      # 0x510c
xorl %eax, %eax
callq 0x1040
movl $0x1, %edi
callq 0x1100