// File: sagarvare[P]reinforcement-learning-rock-paper-scissor/helper.cpp
// Function: draw_sample(int*, float)
int draw_sample(int trans_counts[],float prior_count){
	/*
	TO DO::
	Takes in a distribution and draws a random variable from that distribution
	
	@params: dist contains the number of transitions. Need not be integer.
	Returns: A character number : 1[P],2[R],3[S]
	*/
	int trans_counts_len = 1+(sizeof(trans_counts)/sizeof(*trans_counts));
	float summed_trans[trans_counts_len+1];
	summed_trans[0] = 0;
 	for (int i=0;i<trans_counts_len;i++){
		summed_trans[i+1] = summed_trans[i] + trans_counts[i] + prior_count;
	}

	//Test the computed
	// print_array(summed_trans,trans_counts_len + 1);
	// print_array(trans_counts,trans_counts_len);
	//Generate a random number
    random_device rd;  //Will be used to obtain a seed for the random number engine
    mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
    uniform_real_distribution<> dis(0,1);

	float rand_number = dis(gen);
	rand_number = rand_number * summed_trans[trans_counts_len];
	//Get the lower bound of the nearest element
	int pos = lower_bound(summed_trans,summed_trans+ trans_counts_len + 1,rand_number) - summed_trans;

	return pos;


}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2790, %rsp           # imm = 0x2790
movq %rdi, -0x8(%rbp)
movss %xmm0, -0xc(%rbp)
movl $0x3, -0x10(%rbp)
movl -0x10(%rbp), %eax
incl %eax
movl %eax, %ecx
movq %rsp, %rax
movq %rax, -0x18(%rbp)
leaq 0xf(,%rcx,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rax
subq %rdx, %rax
movq %rax, -0x2768(%rbp)
movq %rax, %rsp
movq %rcx, -0x20(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, (%rax)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0xa44e
movq -0x2768(%rbp), %rax
movslq -0x24(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
movq -0x8(%rbp), %rcx
movslq -0x24(%rbp), %rdx
cvtsi2ssl (%rcx,%rdx,4), %xmm1
addss %xmm1, %xmm0
addss -0xc(%rbp), %xmm0
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movss %xmm0, (%rax,%rcx,4)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0xa407
leaq -0x13b0(%rbp), %rdi
movq %rdi, -0x2778(%rbp)
callq 0x54e0
movq -0x2778(%rbp), %rdi
callq 0x5580
movl %eax, -0x276c(%rbp)
jmp 0xa475
movl -0x276c(%rbp), %eax
movl %eax, %eax
movl %eax, %esi
leaq -0x2738(%rbp), %rdi
callq 0x9620
jmp 0xa48d
leaq -0x2758(%rbp), %rdi
xorps %xmm0, %xmm0
movsd 0x1e31(%rip), %xmm1     # 0xc2d0
callq 0xaa30
jmp 0xa4a6
leaq -0x2758(%rbp), %rdi
leaq -0x2738(%rbp), %rsi
callq 0xaa60
movsd %xmm0, -0x2780(%rbp)
jmp 0xa4c3
movq -0x2768(%rbp), %rdi
movsd -0x2780(%rbp), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x275c(%rbp)
movss -0x275c(%rbp), %xmm0
movslq -0x10(%rbp), %rax
movss (%rdi,%rax,4), %xmm1
mulss %xmm1, %xmm0
movss %xmm0, -0x275c(%rbp)
movslq -0x10(%rbp), %rax
leaq 0x4(%rdi,%rax,4), %rsi
leaq -0x275c(%rbp), %rdx
callq 0xaa90
movq %rax, -0x2788(%rbp)
jmp 0xa519
movq -0x2768(%rbp), %rcx
movq -0x2788(%rbp), %rax
subq %rcx, %rax
sarq $0x2, %rax
movl %eax, -0x2760(%rbp)
movl -0x2760(%rbp), %eax
movl %eax, -0x278c(%rbp)
leaq -0x13b0(%rbp), %rdi
callq 0x55a0
movl -0x278c(%rbp), %eax
movq -0x18(%rbp), %rcx
movq %rcx, %rsp
movq %rbp, %rsp
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x2740(%rbp)
movl %eax, -0x2744(%rbp)
leaq -0x13b0(%rbp), %rdi
callq 0x55a0
movq -0x2740(%rbp), %rdi
callq 0x33f0
nopl (%rax,%rax)