// File: /workspace/github_cpp_makefile_star2_O3/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, %r12
xorl %edi, %edi
callq 0x21c0
movl %eax, %edi
callq 0x21e0
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
leaq 0x3b38(%rip), %rdx      # 0x62ad
leaq -0xb0(%rbp), %rdi
movq %r12, %rsi
callq 0x3710
movaps -0x50(%rbp), %xmm0
movq -0x40(%rbp), %rax
leaq -0xb0(%rbp), %rbx
movaps (%rbx), %xmm1
movaps %xmm1, -0x50(%rbp)
movq 0x10(%rbx), %rcx
movq %rcx, -0x40(%rbp)
leaq -0x101c0(%rbp), %rdi
movups %xmm0, (%rdi)
movq %rax, 0x10(%rdi)
xorps %xmm0, %xmm0
movaps %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
callq 0x3e0e
movq %rbx, %rdi
callq 0x3e0e
movq -0x50(%rbp), %rbx
movq -0x48(%rbp), %r14
movq %r14, %rax
subq %rbx, %rax
sarq $0x5, %rax
movslq 0x8(%r12), %rcx
cmpq %rcx, %rax
jae 0x2835
movq 0x67d2(%rip), %rdi      # 0x8fc0
leaq 0x39ff(%rip), %rsi      # 0x61f4
movl $0x2a, %edx
callq 0x2230
movq -0x48(%rbp), %rsi
subq -0x50(%rbp), %rsi
sarq $0x5, %rsi
movq 0x67ae(%rip), %rdi      # 0x8fc0
callq 0x2140
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
leaq 0x39fc(%rip), %rsi      # 0x621f
movl $0x2, %edx
movq %rax, %rdi
callq 0x2230
jmp 0x2dfd
cmpq %r14, %rbx
sete %al
leaq 0x20(%rbx), %rcx
cmpq %r14, %rcx
sete %cl
orb %al, %cl
jne 0x288e
movl $0x20, %r15d
callq 0x2060
cltq
movq %r15, %rcx
sarq $0x5, %rcx
incq %rcx
cqto
idivq %rcx
shlq $0x5, %rdx
cmpq %rdx, %r15
je 0x287d
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 0x284f
movq %r12, -0x58(%rbp)
xorl %eax, %eax
movq %rax, %r13
movq -0x58(%rbp), %rax
movslq 0x8(%rax), %rax
movq %rax, -0xd8(%rbp)
cmpq %rax, %r13
jge 0x2def
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rdx
subq %rax, %rdx
sarq $0x5, %rdx
cmpq %r13, %rdx
jbe 0x2e4a
movq %r13, %rbx
shlq $0x5, %rbx
movq (%rax,%rbx), %rsi
leaq -0xf8(%rbp), %rdi
callq 0x3e7c
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
cmpq %r13, %rdx
jbe 0x2e39
movq 0x8(%rsi,%rbx), %rcx
addq %rbx, %rsi
addq $-0x9, %rcx
movl $0x5, %edx
leaq -0xb0(%rbp), %rdi
callq 0x2210
movaps 0x36f5(%rip), %xmm0     # 0x6060
movaps %xmm0, -0x80(%rbp)
movq -0xf8(%rbp), %rsi
leaq -0x101c0(%rbp), %rdi
callq 0x4082
leaq -0x101c0(%rbp), %rdi
leaq 0x3892(%rip), %rsi      # 0x6222
xorl %edx, %edx
movl $0x1, %ecx
callq 0x41aa
movq 0x30(%rax), %rcx
testq %rcx, %rcx
je 0x29c7
movq $0x0, -0x60(%rbp)
cmpq $0x0, 0x20(%rcx)
je 0x2e1a
incq -0x60(%rbp)
movq 0x58(%rcx), %rcx
testq %rcx, %rcx
jne 0x29ad
jmp 0x29cf
movq $0x0, -0x60(%rbp)
movq -0x58(%rbp), %rcx
movslq 0x8(%rcx), %rcx
movq %rcx, -0xe0(%rbp)
cmpq %rcx, -0x60(%rbp)
jae 0x2a42
movl $0x2f, %edx
movq 0x65d0(%rip), %rdi      # 0x8fc0
leaq 0x3831(%rip), %rsi      # 0x6228
callq 0x2230
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rdx
subq %rax, %rdx
sarq $0x5, %rdx
cmpq %r13, %rdx
jbe 0x2e5b
movq (%rax,%rbx), %rsi
movq 0x8(%rax,%rbx), %rdx
movq 0x659c(%rip), %rdi      # 0x8fc0
callq 0x2230
movl $0x2, %edx
movq %rax, %rdi
leaq 0x3724(%rip), %rsi      # 0x615c
callq 0x2230
jmp 0x2d91
movq %rax, %rdi
leaq 0x380c(%rip), %rsi      # 0x6258
xorl %edx, %edx
movl $0x1, %ecx
callq 0x41aa
movq %rax, %rdi
testq %rax, %rax
je 0x2d91
movl $0x1, %eax
movq %rax, -0x68(%rbp)
movq %r13, -0x70(%rbp)
movq %rdi, -0x88(%rbp)
leaq 0x3868(%rip), %rsi      # 0x62e7
xorl %edx, %edx
movl $0x1, %ecx
callq 0x4290
movq -0x88(%rbp), %rbx
movq 0x30(%rbx), %rax
movl $0x1, %r15d
testq %rax, %rax
je 0x2af1
xorl %ecx, %ecx
cmpq $0x0, 0x20(%rax)
je 0x2e1a
incq %rcx
movq 0x58(%rax), %rax
testq %rax, %rax
jne 0x2aa3
cmpq $0x2, %rcx
jb 0x2af1
callq 0x2060
movq 0x30(%rbx), %rdx
xorl %ecx, %ecx
testq %rdx, %rdx
je 0x2ae4
cmpq $0x0, 0x20(%rdx)
je 0x2e1a
incq %rcx
movq 0x58(%rdx), %rdx
jmp 0x2acb
cltq
xorl %edx, %edx
divq %rcx
movq %rdx, %r15
incl %r15d
movq %rbx, %rdi
leaq 0x3764(%rip), %rsi      # 0x625f
xorl %edx, %edx
movl $0x1, %ecx
callq 0x41aa
movq %rax, %rbx
cmpl $0x2, %r15d
jl 0x2b27
decl %r15d
cmpq $0x0, 0x20(%rbx)
je 0x2e1a
movq 0x58(%rbx), %rbx
decl %r15d
jne 0x2b13
movq %rbx, %rdi
leaq 0x3733(%rip), %rsi      # 0x6264
xorl %edx, %edx
movl $0x1, %ecx
callq 0x41aa
movq 0x8(%rax), %r15
testq %r15, %r15
leaq 0x66b8(%rip), %rax      # 0x9203
cmoveq %rax, %r15
movl $0xd0, %edi
callq 0x21f0
movq %rax, -0x38(%rbp)
leaq -0xc0(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0xb0(%rbp), %rsi
movq -0xa8(%rbp), %rdx
addq %rsi, %rdx
leaq -0xd0(%rbp), %rdi
callq 0x43c2
leaq -0x138(%rbp), %r12
movq %r12, %rdi
movq %r15, %rsi
leaq -0x29(%rbp), %rdx
callq 0x3dd0
movq -0x68(%rbp), %r15
imull $0x64, %r15d, %edx
movq -0x38(%rbp), %r14
movq %r14, %rdi
leaq -0xd0(%rbp), %rsi
movq %r12, %rcx
callq 0x5bf8
movq -0x58(%rbp), %rax
movq 0x10(%rax), %rax
movq (%rax,%r13,8), %rax
movq %r14, -0x8(%rax,%r15,8)
movq -0x138(%rbp), %rdi
leaq -0x128(%rbp), %rax
cmpq %rax, %rdi
je 0x2bef
movq -0x128(%rbp), %rsi
incq %rsi
callq 0x2200
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0x2c11
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 0x2c48
movl -0x80(%rbp,%r15), %eax
movl -0x80(%rbp,%rdx), %ecx
movl %ecx, -0x80(%rbp,%r15)
movl %eax, -0x80(%rbp,%rdx)
addq $0x4, %r15
cmpq $0x10, %r15
jne 0x2c17
movq %rbx, %rdi
leaq 0x3611(%rip), %rsi      # 0x626d
xorl %edx, %edx
movl $0x1, %ecx
callq 0x41aa
movq %rax, %rdi
leaq 0x3603(%rip), %rsi      # 0x6275
xorl %edx, %edx
movl $0x1, %ecx
callq 0x41aa
movq %rax, %r13
xorl %r15d, %r15d
movq %r13, %rdi
leaq 0x35eb(%rip), %rsi      # 0x6279
xorl %edx, %edx
movl $0x1, %ecx
callq 0x4290
movq %r13, -0x38(%rbp)
testq %rax, %rax
je 0x2ccf
movq -0x58(%rbp), %r12
movq 0x10(%r12), %rax
movq -0x70(%rbp), %r14
movq (%rax,%r14,8), %rax
movq -0x68(%rbp), %r13
movq -0x8(%rax,%r13,8), %rdi
movq %r15, %rbx
movl -0x80(%rbp,%r15,4), %r15d
movl %r15d, %esi
callq 0x5edc
jmp 0x2ce3
movq %r15, %rbx
movl -0x80(%rbp,%r15,4), %r15d
movq -0x58(%rbp), %r12
movq -0x70(%rbp), %r14
movq -0x68(%rbp), %r13
movq 0x10(%r12), %rax
movq (%rax,%r14,8), %rax
movq -0x8(%rax,%r13,8), %r12
movq -0x38(%rbp), %r13
movq 0x8(%r13), %rsi
testq %rsi, %rsi
leaq 0x6500(%rip), %rax      # 0x9203
cmoveq %rax, %rsi
leaq -0x118(%rbp), %r14
movq %r14, %rdi
leaq -0x29(%rbp), %rdx
callq 0x3dd0
movq %r12, %rdi
movl %r15d, %esi
movq %r14, %rdx
callq 0x5d92
movq -0x118(%rbp), %rdi
leaq -0x108(%rbp), %rax
cmpq %rax, %rdi
je 0x2d4a
movq -0x108(%rbp), %rsi
incq %rsi
callq 0x2200
cmpq $0x3, %rbx
je 0x2d6a
cmpq $0x0, 0x20(%r13)
je 0x2e1a
movq %rbx, %r15
movq 0x58(%r13), %r13
incq %r15
jmp 0x2c84
movq -0x88(%rbp), %rdi
cmpq $0x0, 0x20(%rdi)
je 0x2e1a
movq 0x58(%rdi), %rdi
incq -0x68(%rbp)
testq %rdi, %rdi
movq -0x70(%rbp), %r13
jne 0x2a71
movq -0xb0(%rbp), %rdi
leaq -0xa0(%rbp), %rax
cmpq %rax, %rdi
je 0x2db3
movq -0xa0(%rbp), %rsi
incq %rsi
callq 0x2200
leaq -0x10160(%rbp), %rdi
callq 0x4352
movq -0xf8(%rbp), %rdi
testq %rdi, %rdi
je 0x2dda
movq -0xe8(%rbp), %rsi
subq %rdi, %rsi
callq 0x2200
leaq 0x1(%r13), %rax
movq -0xe0(%rbp), %rcx
cmpq %rcx, -0x60(%rbp)
jae 0x2894
xorl %ebx, %ebx
cmpq -0xd8(%rbp), %r13
setl %bl
negl %ebx
leaq -0x50(%rbp), %rdi
callq 0x3e0e
movl %ebx, %eax
addq $0x10198, %rsp          # imm = 0x10198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3762(%rip), %rdi      # 0x6583
leaq 0x353d(%rip), %rsi      # 0x6365
leaq 0x3763(%rip), %rcx      # 0x6592
movl $0x3ee, %edx            # imm = 0x3EE
callq 0x2130
leaq 0x34aa(%rip), %rdi      # 0x62ea
movq %r13, %rsi
xorl %eax, %eax
callq 0x22b0
leaq 0x3499(%rip), %rdi      # 0x62ea
movq %r13, %rsi
xorl %eax, %eax
callq 0x22b0
leaq 0x3488(%rip), %rdi      # 0x62ea
movq %r13, %rsi
xorl %eax, %eax
callq 0x22b0
jmp 0x2e7e
jmp 0x2e7e
jmp 0x2f1e
jmp 0x2f7b
jmp 0x2e7e
jmp 0x2e86
movq %rax, %rbx
jmp 0x2f6a
movq %rax, %rbx
jmp 0x2f43
jmp 0x2f1e
movq %rax, %rbx
movq -0x138(%rbp), %rdi
leaq -0x128(%rbp), %rax
cmpq %rax, %rdi
je 0x2ebd
movq -0x128(%rbp), %rsi
incq %rsi
callq 0x2200
jmp 0x2ebd
movq %rax, %rbx
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0x2ee4
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x2200
jmp 0x2ee4
movq %rax, %rbx
movq -0x38(%rbp), %rdi
movl $0xd0, %esi
jmp 0x2f17
jmp 0x2f1e
jmp 0x2f1e
jmp 0x2f1e
jmp 0x2f1e
movq %rax, %rbx
movq -0x118(%rbp), %rdi
leaq -0x108(%rbp), %rax
cmpq %rax, %rdi
je 0x2f21
movq -0x108(%rbp), %rsi
incq %rsi
callq 0x2200
jmp 0x2f21
movq %rax, %rbx
movq -0xb0(%rbp), %rdi
leaq -0xa0(%rbp), %rax
cmpq %rax, %rdi
je 0x2f43
movq -0xa0(%rbp), %rsi
incq %rsi
callq 0x2200
leaq -0x10160(%rbp), %rdi
callq 0x4352
movq -0xf8(%rbp), %rdi
testq %rdi, %rdi
je 0x2f6a
movq -0xe8(%rbp), %rsi
subq %rdi, %rsi
callq 0x2200
leaq -0x50(%rbp), %rdi
callq 0x3e0e
movq %rbx, %rdi
callq 0x2370
movq %rax, %rdi
callq 0x43b3
nop