// File: /workspace/github_cpp_makefile_star2_O3/lns[P]team-tactic-topic-model/main.cpp
// Function: main
int main(int argc, char* argv[])
{
	if(argc<20) {
		printf(" Usage: %s\n",argv[0]);
		printf("    input_file_name\n");
		printf("    out_topics_file_name\n");
		printf("    out_position_file_name\n");
		printf("    out_assign_file_name\n");
		printf("    out_theta_file_name\n");
		printf("    use_pos\n");
		printf("    n_word\n");
		printf("    K\n");
		printf("    alpha\n");
		printf("    beta\n");
		printf("    mu0_x\n");
		printf("    mu0_y\n");
		printf("    kappa0\n");
		printf("    Lambda0_xx\n");
		printf("    Lambda0_xy\n");
		printf("    Lambda0_yy\n");
		printf("    nu0\n");
		printf("    n_iter\n");
		printf("    seed\n");
		printf("  (See main.cpp for details)\n");
		return 0;
	}
	// argv[1] is input, argv[2] is out_topics,
	// argv[3] is out_pos, argv[4] is out_assign,
	// argv[5] is out_theta,
	uint32_t _ = 6; 
	uint32_t use_pos =  strtol(argv[_++],NULL,10);
	uint32_t n_word =   strtol(argv[_++],NULL,10);
	uint32_t K =        strtol(argv[_++],NULL,10);
	double alpha =      strtod(argv[_++],NULL);
	double beta =       strtod(argv[_++],NULL);
	double mu0_x =      strtod(argv[_++],NULL);
	double mu0_y =      strtod(argv[_++],NULL);
	double kappa0 =     strtod(argv[_++],NULL);
	double Lambda0_xx = strtod(argv[_++],NULL);
	double Lambda0_xy = strtod(argv[_++],NULL);
	double Lambda0_yy = strtod(argv[_++],NULL);
	double nu0 =        strtod(argv[_++],NULL);
	uint32_t n_iter =   strtol(argv[_++],NULL,10);
	uint64_t seed =     strtol(argv[_++],NULL,10);

	lcg64(seed);
	LDA<uint32_t,uint32_t,uint8_t> lda;
	// Order matters: 1. Read data. 2. set K. 3. config prior
	//lda.read_lda_c("../hdp/ap/ap.dat");
	lda.read_data(argv[1]);
	lda.n_word = n_word;
	printf("Read file done: %u doc, %u word\n",lda.dat.size(),lda.n_word);
	lda.set_K(K);
	printf("Set K = %u\n",lda.topic.size());
	// alpha, beta, mu0_x, mu0_y, kappa0, Lambda0_xx, Lambda0_xy, Lambda0_yy, nu0
	lda.config({alpha,beta,mu0_x,mu0_y,kappa0,Lambda0_xx,Lambda0_xy,Lambda0_yy,nu0},65536*8);
	lda.use_position = use_pos>0?true:false;

	//lda.print();
	lda.init();
	for(uint32_t i=0;i<n_iter;i++) {
		lda.gibbs();
		if((i+1)%10==0) {
			printf("iter: %u\n",i+1);
			fflush(stdout);
		}
	}
	FILE* f_topic = fopen(argv[2],"w");
	lda.output_topics(f_topic);
	fclose(f_topic);
	FILE* f_pos = fopen(argv[3],"w");
	lda.output_positions(f_pos);
	fclose(f_pos);
	FILE* f_assign = fopen(argv[4],"w");
	lda.output_assignments(f_assign);
	fclose(f_assign);
	FILE* f_theta = fopen(argv[5],"w");
	lda.output_theta(f_theta);
	fclose(f_theta);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movq %rsi, %rbx
cmpl $0x13, %edi
jg 0x154f
movq (%rbx), %rsi
leaq 0x2c65(%rip), %rdi      # 0x40b8
xorl %eax, %eax
callq 0x1040
leaq 0x2d0d(%rip), %rdi      # 0x416e
callq 0x1160
leaq 0x2d15(%rip), %rdi      # 0x4182
callq 0x1160
leaq 0x2d22(%rip), %rdi      # 0x419b
callq 0x1160
leaq 0x2d31(%rip), %rdi      # 0x41b6
callq 0x1160
leaq 0x2d3e(%rip), %rdi      # 0x41cf
callq 0x1160
leaq 0x2d4a(%rip), %rdi      # 0x41e7
callq 0x1160
leaq 0x2d4a(%rip), %rdi      # 0x41f3
callq 0x1160
leaq 0x2d49(%rip), %rdi      # 0x41fe
callq 0x1160
leaq 0x2d43(%rip), %rdi      # 0x4204
callq 0x1160
leaq 0x2d41(%rip), %rdi      # 0x420e
callq 0x1160
leaq 0x2d3e(%rip), %rdi      # 0x4217
callq 0x1160
leaq 0x2d3c(%rip), %rdi      # 0x4221
callq 0x1160
leaq 0x2d3a(%rip), %rdi      # 0x422b
callq 0x1160
leaq 0x2d39(%rip), %rdi      # 0x4236
callq 0x1160
leaq 0x2d3c(%rip), %rdi      # 0x4245
callq 0x1160
leaq 0x2d3f(%rip), %rdi      # 0x4254
callq 0x1160
leaq 0x2d42(%rip), %rdi      # 0x4263
callq 0x1160
leaq 0x2d3e(%rip), %rdi      # 0x426b
callq 0x1160
leaq 0x2d3d(%rip), %rdi      # 0x4276
callq 0x1160
leaq 0x2d3a(%rip), %rdi      # 0x427f
callq 0x1160
jmp 0x1960
movq 0x30(%rbx), %rdi
xorl %ebp, %ebp
xorl %esi, %esi
movl $0xa, %edx
callq 0x1140
movq %rax, %r14
movq 0x38(%rbx), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x1140
movq %rax, %r12
movq 0x40(%rbx), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x1140
movq %rax, %r13
movq 0x48(%rbx), %rdi
xorl %esi, %esi
callq 0x10a0
movsd %xmm0, 0x48(%rsp)
movq 0x50(%rbx), %rdi
xorl %esi, %esi
callq 0x10a0
movsd %xmm0, 0x40(%rsp)
movq 0x58(%rbx), %rdi
xorl %esi, %esi
callq 0x10a0
movsd %xmm0, 0x38(%rsp)
movq 0x60(%rbx), %rdi
xorl %esi, %esi
callq 0x10a0
movsd %xmm0, 0x30(%rsp)
movq 0x68(%rbx), %rdi
xorl %esi, %esi
callq 0x10a0
movsd %xmm0, 0x28(%rsp)
movq 0x70(%rbx), %rdi
xorl %esi, %esi
callq 0x10a0
movsd %xmm0, 0x20(%rsp)
movq 0x78(%rbx), %rdi
xorl %esi, %esi
callq 0x10a0
movsd %xmm0, 0x18(%rsp)
movq 0x80(%rbx), %rdi
xorl %esi, %esi
callq 0x10a0
movsd %xmm0, 0x10(%rsp)
movq 0x88(%rbx), %rdi
xorl %esi, %esi
callq 0x10a0
movsd %xmm0, 0x8(%rsp)
movq 0x90(%rbx), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x1140
movq %rax, %r15
movq 0x98(%rbx), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x1140
movq %rax, 0x5aa7(%rip)      # 0x7100
leaq 0x50(%rsp), %rdi
movq %rbp, 0xf8(%rdi)
xorps %xmm0, %xmm0
movups %xmm0, 0xe8(%rdi)
movq %rbp, 0x4(%rdi)
movups %xmm0, 0x58(%rdi)
movups %xmm0, 0x68(%rdi)
movups %xmm0, 0x78(%rdi)
movups %xmm0, 0x88(%rdi)
movups %xmm0, 0x94(%rdi)
movups %xmm0, 0xa8(%rdi)
movups %xmm0, 0xb4(%rdi)
movups %xmm0, 0xc8(%rdi)
movups %xmm0, 0xd4(%rdi)
movaps 0x2960(%rip), %xmm1     # 0x4010
movups %xmm1, 0x10(%rdi)
movups %xmm0, 0x20(%rdi)
movups %xmm1, 0x30(%rdi)
movhps 0x29bd(%rip), %xmm0     # xmm0 = xmm0[0,1],mem[0,1]
movups %xmm0, 0x40(%rdi)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x50(%rdi)
movq %rbx, (%rsp)
movq 0x8(%rbx), %rsi
callq 0x198e
leaq 0x50(%rsp), %rbp
movl %r12d, 0x4(%rbp)
movq 0x60(%rbp), %rax
subq 0x58(%rbp), %rax
sarq $0x3, %rax
movabsq $-0x5555555555555555, %rsi # imm = 0xAAAAAAAAAAAAAAAB
imulq %rax, %rsi
leaq 0x29b8(%rip), %rdi      # 0x40c4
movl %r12d, %edx
xorl %eax, %eax
callq 0x1040
movzbl %r13b, %esi
movq %rbp, %rdi
callq 0x1b6c
leaq 0x50(%rsp), %r12
movq 0x78(%r12), %rsi
subq 0x70(%r12), %rsi
sarq $0x6, %rsi
leaq 0x29a9(%rip), %rdi      # 0x40e5
xorl %eax, %eax
callq 0x1040
leaq 0x150(%rsp), %rsi
movsd 0x48(%rsp), %xmm0
movsd %xmm0, (%rsi)
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x8(%rsi)
movsd 0x38(%rsp), %xmm0
movsd %xmm0, 0x10(%rsi)
movsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x18(%rsi)
movsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x20(%rsi)
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x28(%rsi)
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x30(%rsi)
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x38(%rsi)
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x40(%rsi)
movq %r12, %rdi
movl $0x80000, %edx          # imm = 0x80000
callq 0x1c90
testl %r14d, %r14d
leaq 0x50(%rsp), %rdi
setne (%rdi)
movl $0x1, %esi
callq 0x1d04
testl %r15d, %r15d
je 0x1846
negl %r15d
movl $0x1, %r12d
movl $0xcccccccd, %ebp       # imm = 0xCCCCCCCD
leaq 0x50(%rsp), %r14
leaq 0x2903(%rip), %r13      # 0x40f1
movq 0x57d3(%rip), %rbx      # 0x6fc8
movq %r14, %rdi
xorl %esi, %esi
callq 0x1d04
movl %r12d, %eax
imulq %rbp, %rax
shrq $0x23, %rax
addl %eax, %eax
leal (%rax,%rax,4), %eax
movl $0x1, %ecx
subl %eax, %ecx
addl %r12d, %ecx
cmpl $0x1, %ecx
jne 0x1833
movq %r13, %rdi
movl %r12d, %esi
xorl %eax, %eax
callq 0x1040
movq (%rbx), %rdi
callq 0x1100
leal (%r15,%r12), %eax
incl %eax
movl %r12d, %ecx
incl %ecx
movl %ecx, %r12d
cmpl $0x1, %eax
jne 0x17f5
movq (%rsp), %rbx
movq 0x10(%rbx), %rdi
leaq 0x28a6(%rip), %r15      # 0x40fb
movq %r15, %rsi
callq 0x1110
movq %rax, %r12
leaq 0x50(%rsp), %r14
movq %r14, %rdi
movq %rax, %rsi
callq 0x1ea8
movq %r12, %rdi
callq 0x10d0
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0x1110
movq %rax, %r12
movq %r14, %rdi
movq %rax, %rsi
callq 0x1f74
movq %r12, %rdi
callq 0x10d0
movq 0x20(%rbx), %rdi
movq %r15, %rsi
callq 0x1110
movq %rax, %r12
movq %r14, %rdi
movq %rax, %rsi
callq 0x20a0
movq %r12, %rdi
callq 0x10d0
movq 0x28(%rbx), %rdi
movq %r15, %rsi
callq 0x1110
movq %rax, %rbx
movq %r14, %rdi
movq %rax, %rsi
callq 0x218c
movq %rbx, %rdi
callq 0x10d0
movq 0xf8(%r14), %rdi
testq %rdi, %rdi
je 0x18fb
callq 0x1130
movq $0x0, 0x148(%rsp)
movq 0x128(%rsp), %rdi
testq %rdi, %rdi
je 0x1919
callq 0x1130
movq $0x0, 0x128(%rsp)
leaq 0xa8(%rsp), %rbx
leaq 0xc0(%rsp), %r14
movq 0x48(%r14), %rdi
testq %rdi, %rdi
je 0x1943
callq 0x1130
movq $0x0, 0x108(%rsp)
leaq 0xd8(%rsp), %rdi
callq 0x22e0
movq %r14, %rdi
callq 0x2334
movq %rbx, %rdi
callq 0x237c
xorl %eax, %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1978
jmp 0x1978
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x2266
movq %rbx, %rdi
callq 0x1180
nop