// File: /workspace/github_cpp_makefile_star2_O2/AndrewAlbert[P]Compiler-Theory/SourceCode/parser.cpp
// Function: Parser::LoopStatement()
bool Parser::LoopStatement(){
	int type, size;
	string id;
	bool resyncEnabled = true;
	
	//Determine if a loop statement is going to be declared
	if( !CheckToken(T_FOR) ) return false;
	string labelCond, labelTrue, labelEnd;
	labelCond = generator->newLabel("Loop_Check");
	labelTrue = generator->newLabel("Loop_True");
	labelEnd = generator->newLabel("Loop_End");
	
	generator->placeLabel(labelCond);

	/* get assignment statement and expression for loop: '( <assignment_statement> ; <expression> )'
	 * Throws errors if '(' or ')' is missing. Throws warnings for other missing components */
	if( !CheckToken(T_LPAREN) ) ReportFatalError("expected '(' before assignment and expression in for loop statement");

	if( !Assignment(id) ) ReportError("expected an assignment at start of for loop statement");

	if( !CheckToken(T_SEMICOLON) ) ReportError("expected ';' separating assignment statement and expression in for loop statement");

	if( !Expression(type, size) ) ReportError("expected a valid expression following assignment in for loop statement");
	else generator->condBranch( labelTrue, labelEnd );

	if( !CheckToken(T_RPAREN) ) ReportError("expected ')' after assignment and expression in for loop statement");
	generator->placeLabel(labelTrue);
	while(true){
		while( Statement() ){
			if( !CheckToken(T_SEMICOLON) ) ReportLineError("expected ';' after statement in for loop",true);
		}
		
		/* check for correct closure of loop: 'end loop' */
		generator->branch(labelCond);

		if( CheckToken(T_END) ){
			generator->placeLabel(labelEnd);
			if( !CheckToken(T_FOR) ) ReportError("missing 'for' in the 'end for' closure of the for loop statement");
			return true;
		}
		else if(resyncEnabled){
			resyncEnabled = false;
			ReportLineError("Bad line. Could not find a valid statement or 'end' reserved keyword in loop statement.");
		}
		else{
			ReportFatalError("expected 'end for' at end of for loop statement");
			return false;	
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2f8, %rsp            # imm = 0x2F8
movq %rdi, %rbx
leaq 0x248(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movl $0x113, %esi            # imm = 0x113
callq 0x49b0
movl %eax, %ebp
testb %al, %al
je 0x762b
leaq 0x88(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
leaq 0x68(%rsp), %rdx
movq %rdx, -0x10(%rdx)
andq $0x0, -0x8(%rdx)
leaq 0x48(%rsp), %rsi
movq %rsi, -0x10(%rsi)
andq $0x0, -0x8(%rsi)
movb %cl, (%rax)
movb %cl, (%rdx)
movb %cl, (%rsi)
movq 0x98(%rbx), %r14
leaq 0xae26(%rip), %rsi      # 0x11fb5
leaq 0x218(%rsp), %rdi
leaq 0xa(%rsp), %rdx
callq 0x36b8
leaq 0x10(%rsp), %rdi
leaq 0x218(%rsp), %rdx
movq %r14, %rsi
callq 0xeaa4
leaq 0x78(%rsp), %rdi
leaq 0x10(%rsp), %r14
movq %r14, %rsi
callq 0x2240
movq %r14, %rdi
callq 0x20f0
leaq 0x218(%rsp), %rdi
callq 0x20f0
movq 0x98(%rbx), %r14
leaq 0xadd5(%rip), %rsi      # 0x11fc0
leaq 0x1f8(%rsp), %rdi
leaq 0xa(%rsp), %rdx
callq 0x36b8
leaq 0x10(%rsp), %rdi
leaq 0x1f8(%rsp), %rdx
movq %r14, %rsi
callq 0xeaa4
leaq 0x58(%rsp), %rdi
leaq 0x10(%rsp), %r14
movq %r14, %rsi
callq 0x2240
movq %r14, %rdi
callq 0x20f0
leaq 0x1f8(%rsp), %rdi
callq 0x20f0
movq 0x98(%rbx), %r14
leaq 0xad83(%rip), %rsi      # 0x11fca
leaq 0x1d8(%rsp), %rdi
leaq 0xa(%rsp), %rdx
callq 0x36b8
leaq 0x10(%rsp), %rdi
leaq 0x1d8(%rsp), %rdx
movq %r14, %rsi
callq 0xeaa4
leaq 0x38(%rsp), %rdi
leaq 0x10(%rsp), %r14
movq %r14, %rsi
callq 0x2240
movq %r14, %rdi
callq 0x20f0
leaq 0x1d8(%rsp), %rdi
callq 0x20f0
movq 0x98(%rbx), %r14
leaq 0x1b8(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x2270
leaq 0x1b8(%rsp), %rsi
movq %r14, %rdi
callq 0xeb7e
leaq 0x1b8(%rsp), %rdi
callq 0x20f0
movq %rbx, %rdi
movl $0x12d, %esi            # imm = 0x12D
callq 0x49b0
testb %al, %al
je 0x7675
leaq 0x238(%rsp), %rsi
movq %rbx, %rdi
callq 0x7842
testb %al, %al
jne 0x732a
leaq 0xad1c(%rip), %rsi      # 0x12017
leaq 0x198(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x36b8
leaq 0x198(%rsp), %rsi
movq %rbx, %rdi
callq 0x47e0
leaq 0x198(%rsp), %rdi
callq 0x20f0
movq %rbx, %rdi
movl $0x12c, %esi            # imm = 0x12C
callq 0x49b0
testb %al, %al
jne 0x7371
leaq 0xad0b(%rip), %rsi      # 0x1204d
leaq 0x178(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x36b8
leaq 0x178(%rsp), %rsi
movq %rbx, %rdi
callq 0x47e0
leaq 0x178(%rsp), %rdi
callq 0x20f0
leaq 0x34(%rsp), %rsi
leaq 0x30(%rsp), %rdx
movq %rbx, %rdi
callq 0x64cc
testb %al, %al
je 0x73e1
movq 0x98(%rbx), %r14
leaq 0x138(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x2270
leaq 0x118(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x2270
leaq 0x138(%rsp), %rsi
leaq 0x118(%rsp), %rdx
movq %r14, %rdi
callq 0xeef0
leaq 0x118(%rsp), %rdi
callq 0x20f0
leaq 0x138(%rsp), %rdi
jmp 0x7412
leaq 0xacb7(%rip), %rsi      # 0x1209f
leaq 0x158(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x36b8
leaq 0x158(%rsp), %rsi
movq %rbx, %rdi
callq 0x47e0
leaq 0x158(%rsp), %rdi
callq 0x20f0
movq %rbx, %rdi
movl $0x12e, %esi            # imm = 0x12E
callq 0x49b0
testb %al, %al
jne 0x745e
leaq 0xacb7(%rip), %rsi      # 0x120e6
leaq 0xf8(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x36b8
leaq 0xf8(%rsp), %rsi
movq %rbx, %rdi
callq 0x47e0
leaq 0xf8(%rsp), %rdi
callq 0x20f0
movq 0x98(%rbx), %r14
leaq 0xd8(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x2270
movb %bpl, 0xb(%rsp)
leaq 0xd8(%rsp), %rsi
movq %r14, %rdi
callq 0xeb7e
leaq 0xd8(%rsp), %rdi
callq 0x20f0
movb $0x1, %al
movl %eax, 0xc(%rsp)
leaq 0xac83(%rip), %rbp      # 0x12129
leaq 0x2d8(%rsp), %r15
leaq 0x10(%rsp), %r13
leaq 0x2b8(%rsp), %r14
leaq 0x298(%rsp), %r12
movq %rbx, %rdi
callq 0x557c
testb %al, %al
je 0x7506
movq %rbx, %rdi
movl $0x12c, %esi            # imm = 0x12C
callq 0x49b0
testb %al, %al
jne 0x74c3
movq %r15, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x36b8
movq %rbx, %rdi
movq %r15, %rsi
pushq $0x1
popq %rdx
callq 0x444c
movq %r15, %rdi
callq 0x20f0
jmp 0x74c3
movq %rbp, %r13
movq 0x98(%rbx), %rbp
movq %r14, %rdi
leaq 0x78(%rsp), %rsi
callq 0x2270
movq %rbp, %rdi
movq %r14, %rsi
callq 0xefca
movq %r14, %rdi
callq 0x20f0
movq %rbx, %rdi
movl $0x104, %esi            # imm = 0x104
callq 0x49b0
testb %al, %al
jne 0x758b
testb $0x1, 0xc(%rsp)
je 0x764c
movq %r12, %rdi
leaq 0xac3d(%rip), %rsi      # 0x12193
leaq 0x10(%rsp), %rdx
callq 0x36b8
movq %r13, %rbp
movq %rbx, %rdi
movq %r12, %rsi
pushq $0x1
popq %rdx
callq 0x444c
movq %r12, %rdi
callq 0x20f0
movl $0x0, 0xc(%rsp)
leaq 0x10(%rsp), %r13
jmp 0x74c3
movq 0x98(%rbx), %r14
leaq 0xb8(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x2270
movb 0xb(%rsp), %bpl
leaq 0xb8(%rsp), %rsi
movq %r14, %rdi
callq 0xeb7e
leaq 0xb8(%rsp), %rdi
callq 0x20f0
movq %rbx, %rdi
movl $0x113, %esi            # imm = 0x113
callq 0x49b0
testb %al, %al
jne 0x760d
leaq 0xab74(%rip), %rsi      # 0x12152
leaq 0x98(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x36b8
leaq 0x98(%rsp), %rsi
movq %rbx, %rdi
callq 0x47e0
leaq 0x98(%rsp), %rdi
callq 0x20f0
leaq 0x38(%rsp), %rdi
callq 0x20f0
leaq 0x58(%rsp), %rdi
callq 0x20f0
leaq 0x78(%rsp), %rdi
callq 0x20f0
leaq 0x238(%rsp), %rdi
callq 0x20f0
movl %ebp, %eax
addq $0x2f8, %rsp            # imm = 0x2F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xab98(%rip), %rsi      # 0x121eb
leaq 0x258(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x36b8
leaq 0x258(%rsp), %rsi
movq %rbx, %rdi
callq 0x4278
leaq 0xa957(%rip), %rsi      # 0x11fd3
leaq 0x278(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x36b8
leaq 0x278(%rsp), %rsi
movq %rbx, %rdi
callq 0x4278
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
jmp 0x77d4
jmp 0x77dd
movq %rax, %rbx
leaq 0xf8(%rsp), %rdi
jmp 0x77d4
jmp 0x77dd
movq %rax, %rbx
leaq 0x118(%rsp), %rdi
callq 0x20f0
jmp 0x76ed
movq %rax, %rbx
leaq 0x158(%rsp), %rdi
jmp 0x77d4
movq %rax, %rbx
leaq 0x138(%rsp), %rdi
jmp 0x77d4
jmp 0x77dd
movq %rax, %rbx
leaq 0x178(%rsp), %rdi
jmp 0x77d4
jmp 0x77dd
movq %rax, %rbx
leaq 0x198(%rsp), %rdi
jmp 0x77d4
jmp 0x77dd
movq %rax, %rbx
leaq 0x278(%rsp), %rdi
jmp 0x77d4
jmp 0x77dd
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
jmp 0x77d4
movq %rax, %rbx
leaq 0xd8(%rsp), %rdi
jmp 0x77d4
movq %rax, %rbx
leaq 0x1b8(%rsp), %rdi
jmp 0x77d4
movq %rax, %rbx
leaq 0x1d8(%rsp), %rdi
jmp 0x77d4
jmp 0x77dd
movq %rax, %rbx
leaq 0x1f8(%rsp), %rdi
jmp 0x77d4
jmp 0x77dd
movq %rax, %rbx
leaq 0x218(%rsp), %rdi
jmp 0x77d4
jmp 0x77dd
movq %rax, %rbx
jmp 0x77fe
jmp 0x77dd
movq %rax, %rbx
leaq 0x258(%rsp), %rdi
jmp 0x77d4
jmp 0x77dd
jmp 0x77dd
movq %rax, %rbx
leaq 0x298(%rsp), %rdi
jmp 0x77d4
movq %rax, %rbx
leaq 0x2b8(%rsp), %rdi
jmp 0x77d4
jmp 0x77dd
movq %rax, %rbx
leaq 0x2d8(%rsp), %rdi
callq 0x20f0
jmp 0x77e0
jmp 0x77dd
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x20f0
leaq 0x58(%rsp), %rdi
callq 0x20f0
leaq 0x78(%rsp), %rdi
callq 0x20f0
leaq 0x238(%rsp), %rdi
callq 0x20f0
movq %rbx, %rdi
callq 0x2310
nop