// File: /workspace/github_cpp_makefile_star2_O2/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 0x141f
movq (%rbx), %rsi
leaq 0x2d65(%rip), %rdi      # 0x4088
xorl %eax, %eax
callq 0x1040
leaq 0x2e0d(%rip), %rdi      # 0x413e
callq 0x1190
leaq 0x2e15(%rip), %rdi      # 0x4152
callq 0x1190
leaq 0x2e22(%rip), %rdi      # 0x416b
callq 0x1190
leaq 0x2e31(%rip), %rdi      # 0x4186
callq 0x1190
leaq 0x2e3e(%rip), %rdi      # 0x419f
callq 0x1190
leaq 0x2e4a(%rip), %rdi      # 0x41b7
callq 0x1190
leaq 0x2e4a(%rip), %rdi      # 0x41c3
callq 0x1190
leaq 0x2e49(%rip), %rdi      # 0x41ce
callq 0x1190
leaq 0x2e43(%rip), %rdi      # 0x41d4
callq 0x1190
leaq 0x2e41(%rip), %rdi      # 0x41de
callq 0x1190
leaq 0x2e3e(%rip), %rdi      # 0x41e7
callq 0x1190
leaq 0x2e3c(%rip), %rdi      # 0x41f1
callq 0x1190
leaq 0x2e3a(%rip), %rdi      # 0x41fb
callq 0x1190
leaq 0x2e39(%rip), %rdi      # 0x4206
callq 0x1190
leaq 0x2e3c(%rip), %rdi      # 0x4215
callq 0x1190
leaq 0x2e3f(%rip), %rdi      # 0x4224
callq 0x1190
leaq 0x2e42(%rip), %rdi      # 0x4233
callq 0x1190
leaq 0x2e3e(%rip), %rdi      # 0x423b
callq 0x1190
leaq 0x2e3d(%rip), %rdi      # 0x4246
callq 0x1190
leaq 0x2e3a(%rip), %rdi      # 0x424f
callq 0x1190
jmp 0x178b
movq 0x30(%rbx), %rdi
pushq $0xa
popq %rbp
xorl %esi, %esi
movl %ebp, %edx
callq 0x1160
movq %rax, %r14
movq 0x38(%rbx), %rdi
xorl %esi, %esi
movl %ebp, %edx
callq 0x1160
movq %rax, %r12
movq 0x40(%rbx), %rdi
xorl %esi, %esi
movl %ebp, %edx
callq 0x1160
movq %rax, %r13
movq 0x48(%rbx), %rdi
xorl %esi, %esi
callq 0x10c0
movsd %xmm0, 0x48(%rsp)
movq 0x50(%rbx), %rdi
xorl %esi, %esi
callq 0x10c0
movsd %xmm0, 0x40(%rsp)
movq 0x58(%rbx), %rdi
xorl %esi, %esi
callq 0x10c0
movsd %xmm0, 0x38(%rsp)
movq 0x60(%rbx), %rdi
xorl %esi, %esi
callq 0x10c0
movsd %xmm0, 0x30(%rsp)
movq 0x68(%rbx), %rdi
xorl %esi, %esi
callq 0x10c0
movsd %xmm0, 0x28(%rsp)
movq 0x70(%rbx), %rdi
xorl %esi, %esi
callq 0x10c0
movsd %xmm0, 0x20(%rsp)
movq 0x78(%rbx), %rdi
xorl %esi, %esi
callq 0x10c0
movsd %xmm0, 0x18(%rsp)
movq 0x80(%rbx), %rdi
xorl %esi, %esi
callq 0x10c0
movsd %xmm0, 0x10(%rsp)
movq 0x88(%rbx), %rdi
xorl %esi, %esi
callq 0x10c0
movsd %xmm0, 0x8(%rsp)
movq 0x90(%rbx), %rdi
xorl %esi, %esi
movl %ebp, %edx
callq 0x1160
movq %rax, %r15
movq 0x98(%rbx), %rdi
xorl %esi, %esi
movl %ebp, %edx
callq 0x1160
xorps %xmm0, %xmm0
leaq 0x50(%rsp), %rdi
andq $0x0, 0xf8(%rdi)
movups %xmm0, 0xe8(%rdi)
andq $0x0, 0x4(%rdi)
movq %rax, 0x5be9(%rip)      # 0x7120
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 0x2a9c(%rip), %xmm1     # 0x4010
movups %xmm1, 0x10(%rdi)
movups %xmm0, 0x20(%rdi)
movups %xmm1, 0x30(%rdi)
movhps 0x2ac9(%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 0x17b8
leaq 0x50(%rsp), %rbx
movl %r12d, 0x4(%rbx)
movq 0x60(%rbx), %rax
subq 0x58(%rbx), %rax
pushq $0x18
popq %rcx
cqto
idivq %rcx
leaq 0x2ace(%rip), %rdi      # 0x4094
movq %rax, %rsi
movl %r12d, %edx
xorl %eax, %eax
callq 0x1040
movzbl %r13b, %esi
movq %rbx, %rdi
callq 0x1900
leaq 0x50(%rsp), %rbx
movq 0x78(%rbx), %rsi
subq 0x70(%rbx), %rsi
sarq $0x6, %rsi
leaq 0x2abe(%rip), %rdi      # 0x40b5
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 %rbx, %rdi
movl $0x80000, %edx          # imm = 0x80000
callq 0x19ca
testl %r14d, %r14d
leaq 0x50(%rsp), %rdi
setne (%rdi)
callq 0x1a38
negl %r15d
pushq $0x1
popq %rbp
leaq 0x50(%rsp), %r14
movq 0x5931(%rip), %r12      # 0x6fc8
movl %ebp, %r13d
leal (%r15,%r13), %ecx
movl %r13d, %eax
xorl %edx, %edx
pushq $0xa
popq %rsi
divl %esi
cmpl $0x1, %ecx
je 0x16e7
movl %eax, %ebx
movq %r14, %rdi
xorl %esi, %esi
callq 0x1a40
imull $0xa, %ebx, %eax
movl %ebp, %ecx
subl %eax, %ecx
addl %r13d, %ecx
cmpl $0x1, %ecx
jne 0x16e2
leaq 0x29f2(%rip), %rdi      # 0x40c1
movl %r13d, %esi
xorl %eax, %eax
callq 0x1040
movq (%r12), %rdi
callq 0x1120
incl %r13d
jmp 0x169a
movq (%rsp), %r12
movq 0x10(%r12), %rdi
leaq 0x29d4(%rip), %r15      # 0x40cb
movq %r15, %rsi
callq 0x1130
movq %rax, %rbx
leaq 0x50(%rsp), %r14
movq %r14, %rdi
movq %rax, %rsi
callq 0x1bb2
movq %rbx, %rdi
callq 0x10f0
movq 0x18(%r12), %rdi
movq %r15, %rsi
callq 0x1130
movq %rax, %rbx
movq %r14, %rdi
movq %rax, %rsi
callq 0x1c3e
movq %rbx, %rdi
callq 0x10f0
movq 0x20(%r12), %rdi
movq %r15, %rsi
callq 0x1130
movq %rax, %rbx
movq %r14, %rdi
movq %rax, %rsi
callq 0x1d34
movq %rbx, %rdi
callq 0x10f0
movq 0x28(%r12), %rdi
movq %r15, %rsi
callq 0x1130
movq %rax, %rbx
movq %r14, %rdi
movq %rax, %rsi
callq 0x1de6
movq %rbx, %rdi
callq 0x10f0
movq %r14, %rdi
callq 0x1e88
xorl %eax, %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x17a3
jmp 0x17a3
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x1e88
movq %rbx, %rdi
callq 0x11c0