// File: /workspace/github_cpp_makefile_star2_O1/Matlock42[P]Jeopardy/jgameClass.cpp
// Function: Game::loadQuestions()
int Game::loadQuestions(void)
{	
	/* initialize random seed: */
	srand (time(NULL));
	
	std::vector <std::string> files;
	// get list of xml files
	files = findXML(".");
	
	if( files.size() < mSize)
	{
		cout << "Unable load game: inadequate categories. ("<< files.size() <<")\n";
		return -1;
	}
	
	// shuffle up the list
	std::random_shuffle ( files.begin(), files.end() );
	
	// For each of the files found, go get some questions (up to the board size)
	for( int f=0; f<mSize; f++ )
	{
		// string lFileLocation = files.at(f);
		// rapidxml::file<> lXmlFile(lFileLocation.data());
		rapidxml::file<> lXmlFile(files.at(f).data());
		rapidxml::xml_document<> lDoc;

		// everything but the first 5 and last 4 charactors
		string lFileName = files.at(f).substr(5,(files.at(f).length()-5-4));
		
		int lFileCounter = f + 1;	// start at 1
		int lLevelCounter = 1;		// start at $100
		int lItemCounter;
		int lChosenItem;
		int lAns[4] = {0, 1, 2, 3};

		rapidxml::xml_node<> * lRoot_node;
		rapidxml::xml_node<> * lLevel;
		rapidxml::xml_node<> * lItem;
		rapidxml::xml_node<> * lQuestion;
		rapidxml::xml_node<> * lAnswerGroup;
		rapidxml::xml_node<> * lAnswerNode;
		rapidxml::xml_attribute<> * lAttr;
		
		// Parse the buffer using the xml file parsing library into lDoc
		lDoc.parse<0>(lXmlFile.data());
		//set the root node
		lRoot_node = lDoc.first_node("items");
		// make sure there are the right number of levels
		if( rapidxml::count_children(lRoot_node) < mSize )
		{
			// bad things are happening
			cout << "Unable load game: inadequate questions in Data/" << files.at(f) << ".\n";
			return -1;
		}
		// go through each Level of question (100,200,300,...)
		for( lLevel = lRoot_node->first_node("levels"); lLevel; lLevel = lLevel->next_sibling() )
		{
			lAttr = lLevel->first_attribute("id");
			// make sure that the level id matches
			if( lAttr )
			{
				char * lLevelNum = lAttr->value();

				if( * lLevelNum != (lLevelCounter * 100) )
				{
					// level doesn't match
					// complain with error/exception
					//cout << "Unable to load Game: Bad level values. (" << * lLevelNum << ")(" << lLevelCounter << ")\n";
				}
			}
			
			// count the # of items in the level
			//set default to 1
			lChosenItem = 1;
			
			if( rapidxml::count_children(lLevel) > 1 ) // if more than one item in the level
			{
				// pick one at random
				lChosenItem = rand() % rapidxml::count_children(lLevel) + 1;
			}
			
			lItemCounter = 1;
			lItem = lLevel->first_node("item");
			
			// now open the chosen item
			while(lChosenItem > lItemCounter)
			{
				lItem = lItem->next_sibling();
				lItemCounter ++;
			}
			
			//Have the right item, now extract the question and answers.
			lQuestion = lItem->first_node("question");
			char * lQuestionValue = lQuestion->value();
			mQuestionSet[lFileCounter - 1][lLevelCounter - 1] = new Question(lFileName,(lLevelCounter * 100), lQuestionValue);
			
			// mix up the answers
			random_shuffle(&lAns[0],&lAns[4]);
			lAnswerGroup = lItem->first_node("answers");
			lAnswerNode =  lAnswerGroup->first_node("ans");
			// loop through the answers
			for(int i = 0; i < 4; i++)
			{
				//Check if this is the correct answer
				lAttr = lAnswerNode->first_attribute("correct");
				if(lAttr)
				{
					char * lAnsCorrect = lAttr->value();
					// set the correct answer
					mQuestionSet[lFileCounter - 1][lLevelCounter - 1]->setCorrectAns(lAns[i]);
				}
				// add the answer to the stack
				mQuestionSet[lFileCounter - 1][lLevelCounter - 1]->addAnswer(lAns[i], lAnswerNode->value());
				// go to the next answer exept the last loop
				if( i < 3 ) lAnswerNode = lAnswerNode->next_sibling();
			}
			lLevelCounter ++;
		}
	}
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10198, %rsp          # imm = 0x10198
movq %rdi, %r15
xorl %edi, %edi
callq 0x21c0
movl %eax, %edi
callq 0x21e0
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
leaq 0x3b34(%rip), %rdx      # 0x62ad
leaq -0xb0(%rbp), %rdi
movq %r15, %rsi
callq 0x370c
leaq -0x101c0(%rbp), %rdi
xorps %xmm0, %xmm0
movaps %xmm0, (%rdi)
movaps -0x50(%rbp), %xmm0
movq -0x40(%rbp), %rax
leaq -0xb0(%rbp), %rbx
movups (%rbx), %xmm1
movaps %xmm1, -0x50(%rbp)
movq 0x10(%rbx), %rcx
movq %rcx, -0x40(%rbp)
movaps (%rdi), %xmm1
movaps %xmm0, (%rdi)
movq %rax, 0x10(%rdi)
movups %xmm1, (%rbx)
movq $0x0, 0x10(%rbx)
callq 0x3e08
movq %rbx, %rdi
callq 0x3e08
movq -0x50(%rbp), %rbx
movq -0x48(%rbp), %r14
movq %r14, %rax
subq %rbx, %rax
sarq $0x5, %rax
movq %r15, -0x70(%rbp)
movslq 0x8(%r15), %rcx
cmpq %rcx, %rax
jae 0x2842
movq 0x67c5(%rip), %rdi      # 0x8fc0
leaq 0x39f2(%rip), %rsi      # 0x61f4
movl $0x2a, %edx
callq 0x2230
movq -0x48(%rbp), %rsi
subq -0x50(%rbp), %rsi
sarq $0x5, %rsi
movq 0x67a1(%rip), %rdi      # 0x8fc0
callq 0x2140
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
leaq 0x39ef(%rip), %rsi      # 0x621f
movl $0x2, %edx
movq %rax, %rdi
callq 0x2230
jmp 0x2e09
cmpq %r14, %rbx
sete %al
leaq 0x20(%rbx), %rcx
cmpq %r14, %rcx
sete %cl
orb %al, %cl
jne 0x289b
movl $0x20, %r15d
callq 0x2060
cltq
movq %r15, %rcx
sarq $0x5, %rcx
incq %rcx
cqto
idivq %rcx
shlq $0x5, %rdx
cmpq %rdx, %r15
je 0x288a
leaq (%rbx,%r15), %rdi
addq %rbx, %rdx
movq %rdx, %rsi
callq 0x2160
leaq (%rbx,%r15), %rax
addq $0x20, %rax
addq $0x20, %r15
cmpq %r14, %rax
jne 0x285c
leaq -0xd0(%rbp), %r13
xorl %eax, %eax
movq %rax, %rbx
movq -0x70(%rbp), %rax
movslq 0x8(%rax), %rax
movq %rax, -0xd8(%rbp)
cmpq %rax, %rbx
jge 0x2df8
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rdx
subq %rax, %rdx
sarq $0x5, %rdx
cmpq %rbx, %rdx
jbe 0x2e45
movq %rbx, %r14
shlq $0x5, %rbx
movq (%rax,%rbx), %rsi
leaq -0xf8(%rbp), %rdi
callq 0x3e72
xorl %eax, %eax
movq %rax, -0x101a0(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x101c0(%rbp)
movl $0x0, -0x10198(%rbp)
movq %rax, -0x10190(%rbp)
movq %rax, -0x10180(%rbp)
leaq -0x148(%rbp), %rax
movups %xmm0, (%rax)
leaq -0x10148(%rbp), %rcx
movq %rcx, -0x10160(%rbp)
movq %rcx, -0x10158(%rbp)
movq %rax, -0x10150(%rbp)
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
subq %rsi, %rdx
sarq $0x5, %rdx
movq %r14, -0x38(%rbp)
cmpq %r14, %rdx
jbe 0x2e56
movq 0x8(%rsi,%rbx), %rcx
addq %rbx, %rsi
addq $-0x9, %rcx
movl $0x5, %edx
leaq -0xb0(%rbp), %rdi
callq 0x2210
movaps 0x36e1(%rip), %xmm0     # 0x6060
movaps %xmm0, -0x90(%rbp)
movq -0xf8(%rbp), %rsi
leaq -0x101c0(%rbp), %rdi
callq 0x4068
leaq -0x101c0(%rbp), %rdi
leaq 0x387b(%rip), %rsi      # 0x6222
xorl %edx, %edx
movl $0x1, %ecx
callq 0x4192
movq 0x30(%rax), %rcx
testq %rcx, %rcx
je 0x29de
movq $0x0, -0x60(%rbp)
cmpq $0x0, 0x20(%rcx)
je 0x2e26
incq -0x60(%rbp)
movq 0x58(%rcx), %rcx
testq %rcx, %rcx
jne 0x29c4
jmp 0x29e6
movq $0x0, -0x60(%rbp)
movq -0x70(%rbp), %rcx
movslq 0x8(%rcx), %rcx
movq %rcx, -0xe0(%rbp)
cmpq %rcx, -0x60(%rbp)
jae 0x2a5a
movl $0x2f, %edx
movq 0x65b9(%rip), %rdi      # 0x8fc0
leaq 0x381a(%rip), %rsi      # 0x6228
callq 0x2230
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rdx
subq %rax, %rdx
sarq $0x5, %rdx
cmpq -0x38(%rbp), %rdx
jbe 0x2e68
movq (%rax,%rbx), %rsi
movq 0x8(%rax,%rbx), %rdx
movq 0x6584(%rip), %rdi      # 0x8fc0
callq 0x2230
movl $0x2, %edx
movq %rax, %rdi
leaq 0x370c(%rip), %rsi      # 0x615c
callq 0x2230
jmp 0x2d96
movq %rax, %rdi
leaq 0x37f4(%rip), %rsi      # 0x6258
xorl %edx, %edx
movl $0x1, %ecx
callq 0x4192
movq %rax, %rdi
testq %rax, %rax
je 0x2d96
movl $0x1, %eax
movq %rax, -0x78(%rbp)
movq %rdi, -0x68(%rbp)
leaq 0x3857(%rip), %rsi      # 0x62e7
xorl %edx, %edx
movl $0x1, %ecx
callq 0x42c0
movq -0x68(%rbp), %rax
movq 0x30(%rax), %rax
movl $0x1, %r15d
testq %rax, %rax
je 0x2b03
xorl %ecx, %ecx
cmpq $0x0, 0x20(%rax)
je 0x2e26
incq %rcx
movq 0x58(%rax), %rax
testq %rax, %rax
jne 0x2ab1
cmpq $0x2, %rcx
jb 0x2b03
callq 0x2060
movq -0x68(%rbp), %rcx
movq 0x30(%rcx), %rdx
xorl %ecx, %ecx
testq %rdx, %rdx
je 0x2af6
cmpq $0x0, 0x20(%rdx)
je 0x2e26
incq %rcx
movq 0x58(%rdx), %rdx
jmp 0x2add
cltq
xorl %edx, %edx
divq %rcx
movq %rdx, %r15
incl %r15d
movq -0x68(%rbp), %rdi
leaq 0x3751(%rip), %rsi      # 0x625f
xorl %edx, %edx
movl $0x1, %ecx
callq 0x4192
movq %rax, %rbx
cmpl $0x2, %r15d
jl 0x2b3a
decl %r15d
cmpq $0x0, 0x20(%rbx)
je 0x2e26
movq 0x58(%rbx), %rbx
decl %r15d
jne 0x2b26
movq %rbx, %rdi
leaq 0x3720(%rip), %rsi      # 0x6264
xorl %edx, %edx
movl $0x1, %ecx
callq 0x4192
movq 0x8(%rax), %r15
testq %r15, %r15
leaq 0x66a5(%rip), %rax      # 0x9203
cmoveq %rax, %r15
movl $0xd0, %edi
callq 0x21f0
movq %rax, -0x58(%rbp)
leaq -0xc0(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0xb0(%rbp), %rsi
movq -0xa8(%rbp), %rdx
addq %rsi, %rdx
movq %r13, %rdi
callq 0x4432
movq %r13, %r14
leaq -0x138(%rbp), %r12
movq %r12, %rdi
movq %r15, %rsi
leaq -0x29(%rbp), %rdx
callq 0x3dca
movq -0x78(%rbp), %r15
imull $0x64, %r15d, %edx
movq -0x58(%rbp), %r13
movq %r13, %rdi
movq %r14, %rsi
movq %r12, %rcx
callq 0x5ce0
movq -0x70(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %r13, -0x8(%rax,%r15,8)
movq -0x138(%rbp), %rdi
leaq -0x128(%rbp), %rax
cmpq %rax, %rdi
je 0x2c01
movq -0x128(%rbp), %rsi
incq %rsi
callq 0x2200
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0x2c23
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x2200
movl $0x4, %r15d
callq 0x2060
cltq
movq %r15, %rcx
shrq $0x2, %rcx
incq %rcx
cqto
idivq %rcx
shlq $0x2, %rdx
cmpq %rdx, %r15
je 0x2c66
movl -0x90(%rbp,%r15), %eax
movl -0x90(%rbp,%rdx), %ecx
movl %ecx, -0x90(%rbp,%r15)
movl %eax, -0x90(%rbp,%rdx)
addq $0x4, %r15
cmpq $0x10, %r15
jne 0x2c29
movq %rbx, %rdi
leaq 0x35f3(%rip), %rsi      # 0x626d
xorl %edx, %edx
movl $0x1, %ecx
callq 0x4192
movq %rax, %rdi
leaq 0x35e5(%rip), %rsi      # 0x6275
xorl %edx, %edx
movl $0x1, %ecx
callq 0x4192
movq %rax, %rbx
xorl %r13d, %r13d
movq %rbx, %rdi
leaq 0x35cd(%rip), %rsi      # 0x6279
xorl %edx, %edx
movl $0x1, %ecx
callq 0x42c0
testq %rax, %rax
movq -0x70(%rbp), %r15
movq -0x38(%rbp), %r12
movq -0x78(%rbp), %r14
je 0x2ce3
movq 0x10(%r15), %rax
movq (%rax,%r12,8), %rax
movq -0x8(%rax,%r14,8), %rdi
movl -0x90(%rbp,%r13,4), %esi
callq 0x5fc4
movq 0x10(%r15), %rax
movq (%rax,%r12,8), %rax
movq -0x8(%rax,%r14,8), %r15
movl -0x90(%rbp,%r13,4), %r12d
movq 0x8(%rbx), %rsi
testq %rsi, %rsi
leaq 0x64fd(%rip), %rax      # 0x9203
cmoveq %rax, %rsi
leaq -0x118(%rbp), %r14
movq %r14, %rdi
leaq -0x29(%rbp), %rdx
callq 0x3dca
movq %r15, %rdi
movl %r12d, %esi
movq %r14, %rdx
callq 0x5e7a
movq -0x118(%rbp), %rdi
leaq -0x108(%rbp), %rax
cmpq %rax, %rdi
je 0x2d4d
movq -0x108(%rbp), %rsi
incq %rsi
callq 0x2200
cmpq $0x3, %r13
je 0x2d62
cmpq $0x0, 0x20(%rbx)
je 0x2e26
movq 0x58(%rbx), %rbx
incq %r13
cmpq $0x4, %r13
jne 0x2ca2
movq -0x68(%rbp), %rdi
cmpq $0x0, 0x20(%rdi)
je 0x2e26
movq 0x58(%rdi), %rdi
incq -0x78(%rbp)
testq %rdi, %rdi
leaq -0xd0(%rbp), %r13
jne 0x2a85
movq -0xb0(%rbp), %rdi
leaq -0xa0(%rbp), %rax
cmpq %rax, %rdi
je 0x2db8
movq -0xa0(%rbp), %rsi
incq %rsi
callq 0x2200
leaq -0x10160(%rbp), %rdi
callq 0x43c0
movq -0x38(%rbp), %rbx
movq -0xf8(%rbp), %rdi
testq %rdi, %rdi
je 0x2de3
movq -0xe8(%rbp), %rsi
subq %rdi, %rsi
callq 0x2200
leaq 0x1(%rbx), %rax
movq -0xe0(%rbp), %rcx
cmpq %rcx, -0x60(%rbp)
jae 0x28a4
movq %rbx, %rax
xorl %ebx, %ebx
cmpq -0xd8(%rbp), %rax
setl %bl
negl %ebx
leaq -0x50(%rbp), %rdi
callq 0x3e08
movl %ebx, %eax
addq $0x10198, %rsp          # imm = 0x10198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3756(%rip), %rdi      # 0x6583
leaq 0x3531(%rip), %rsi      # 0x6365
leaq 0x3757(%rip), %rcx      # 0x6592
movl $0x3ee, %edx            # imm = 0x3EE
callq 0x2130
leaq 0x349e(%rip), %rdi      # 0x62ea
movq %rbx, %rsi
xorl %eax, %eax
callq 0x22b0
leaq 0x348d(%rip), %rdi      # 0x62ea
movq -0x38(%rbp), %rsi
xorl %eax, %eax
callq 0x22b0
leaq 0x347b(%rip), %rdi      # 0x62ea
movq -0x38(%rbp), %rsi
xorl %eax, %eax
callq 0x22b0
jmp 0x2e94
jmp 0x2e94
jmp 0x2f2c
jmp 0x2f89
jmp 0x2e8c
jmp 0x2e94
movq %rax, %rbx
jmp 0x2f51
movq %rax, %rbx
jmp 0x2f78
jmp 0x2f2c
movq %rax, %rbx
movq -0x138(%rbp), %rdi
leaq -0x128(%rbp), %rax
cmpq %rax, %rdi
je 0x2ed0
movq -0x128(%rbp), %rsi
incq %rsi
callq 0x2200
jmp 0x2ed0
movq %rax, %rbx
jmp 0x2ef2
movq %rax, %rbx
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0x2ef2
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x2200
movq -0x58(%rbp), %rdi
movl $0xd0, %esi
jmp 0x2f23
jmp 0x2f2c
jmp 0x2f2c
jmp 0x2f2c
movq %rax, %rbx
movq -0x118(%rbp), %rdi
leaq -0x108(%rbp), %rax
cmpq %rax, %rdi
je 0x2f2f
movq -0x108(%rbp), %rsi
incq %rsi
callq 0x2200
jmp 0x2f2f
jmp 0x2f2c
movq %rax, %rbx
movq -0xb0(%rbp), %rdi
leaq -0xa0(%rbp), %rax
cmpq %rax, %rdi
je 0x2f51
movq -0xa0(%rbp), %rsi
incq %rsi
callq 0x2200
leaq -0x10160(%rbp), %rdi
callq 0x43c0
movq -0xf8(%rbp), %rdi
testq %rdi, %rdi
je 0x2f78
movq -0xe8(%rbp), %rsi
subq %rdi, %rsi
callq 0x2200
leaq -0x50(%rbp), %rdi
callq 0x3e08
movq %rbx, %rdi
callq 0x2370
movq %rax, %rdi
callq 0x4423
nop