// File: /workspace/github_cpp_makefile_star2_O2/CMU-SAFARI[P]Register-Interval/./src/implementation/RegisterIntervalCreation.cpp
// Function: RegisterIntervalCreationPass::registerIntervalCreationPassTwo(std::vector<registerInterval*, std::allocator<registerInterval*>>, int)
vector<registerInterval*> RegisterIntervalCreationPass::registerIntervalCreationPassTwo(vector<registerInterval*> RegisterInterval_in,int N){
	vector<registerInterval*> RegisterInterval_out;//The registerInterval set that contains the reduced register-intervals

        /*Initialization*/
	for (auto i = RegisterInterval_in.begin(); i != RegisterInterval_in.end(); i++){
		(*i)->set_next_level_registerInterval(nullptr);
	}

	queue<registerInterval*> WorkingSet; //The working set of registerInterval creation algorithm (pass 2)

        
	for(auto i = RegisterInterval_in.begin(); i != RegisterInterval_in.end(); i++){
		auto Predecessors = (*i)->get_predecessors();
		if(Predecessors.size() == 0){// each register-interval with no predecessors is assumed as an entry registerInterval
			auto temp = new registerInterval();
			RegisterInterval_out.push_back(temp);
                        (*i)->set_next_level_registerInterval(temp);
			WorkingSet.push(*i);//The register-interval is added to working set of the algorithm
		}
	}

	while (!WorkingSet.empty()){
		auto i = WorkingSet.front();//Get a register-interval form the working set
		WorkingSet.pop();
		auto ii = i->nextLevelInterval();//ii is the current next-level register-interval that i belongs to 
		if (i->get_register_list().size() < N){//Just register-intervals will be processed that contain less registers than the allowed number of registers
			ii->set_register_list(i->get_register_list());//At the beginning the register list of the current next-level register registerInterval is equal to i
                        
                        /*This loop looks at all register-interval as a candidate for adding to the current register-interval
                                                            and checks the required conditions*/
			for (auto Interval = RegisterInterval_in.begin(); Interval != RegisterInterval_in.end(); Interval++){
				auto Predecessors = (*Interval)->get_predecessors();
				bool C = true;//This boolean value will determine that all predecessors of the candidate register-interval belong to the current next-level register-interval
                                set<string> Union;//This set will contain the union of register sets of all register-intervals that belong to the current next-level register-interval
                                Union.clear();

				visit_registerInterval_all_predecessors(*Interval);//All predecessors of the candidate register-interval in CFG will be marked

                                /*This loop will determine that all predecessors of the current register-interval belong to the current next-level register-interval*/
				for(auto p = Predecessors.begin(); p != Predecessors.end(); p++){
					if((*p)->nextLevelInterval() != ii)
						if(*p != *Interval)
							C = false;
				}

                                /*This loop will set the union set of registers of all register-intervals that the the candidate register-interval can be reached from
                                                                        and also belong to the current next-level register registerInterval*/
				for(auto iterator = RegisterInterval_in.begin(); iterator != RegisterInterval_in.end(); iterator++){
					auto s = (*iterator)->get_register_list();
					for (auto it = s.begin(); it != s.end(); it++){
						Union.insert(*it);
					}
				}

                                /*At this point we add the candidate register-interval to the current register-interval if all required conditions are satisfied*/
				if ((*Interval)->nextLevelInterval() == nullptr //the register-interval should belong to no other next-level register-intervals
                                        && C //the register-interval can be reached from the current next-level register-interval
                                        && Union.size() < N //the size of the union set should not be more than the allowed number of registers in each register-interval
                                        )
                                {
					(*Interval)->set_next_level_registerInterval(ii);
					auto s = (*Interval)->get_register_list();
					Union = ii->get_register_list();
					for (auto it = s.begin(); it != s.end(); ++it){
						Union.insert(*it);
					}
					ii->set_register_list(Union);
				}
			}
		}
                /*At this point no more register-intervals can be added to the current next-level register-interval*/
                
		set<registerInterval*> Successors;//This set will contain the successors the current next-level register-interval
		Successors.clear();
                /*This loop determines the members of Successors set*/
		for (auto Interval = RegisterInterval_in.begin(); Interval != RegisterInterval_in.end(); Interval++){
			if ((*Interval)->nextLevelInterval() == ii){
				auto S = (*Interval)->get_successors();
				for (auto s = S.begin(); s != S.end(); s++){
					Successors.insert(*s);
				}
			}
		}
                
                /*This loop will assign  new register-intervals to all successors of the current next-level registerInterval (as their next-level register registerIntervals)*/
		for (auto s = Successors.begin(); s != Successors.end(); s++){
			if ((*s)->nextLevelInterval() == nullptr){
				auto temp = new registerInterval();
				RegisterInterval_out.push_back(temp);
				(*s)->set_next_level_registerInterval(temp);
				WorkingSet.push(*s);
			}
		}
	}
        /*At this point all register-intervals of the next level are created
                    and rest of the function sets the register lists and the forms the new CFG*/

	/*this loop sets the register list of all output register-intervals*/
	for (auto Interval = RegisterInterval_out.begin(); Interval != RegisterInterval_out.end(); Interval++){
		set<string> regList;
            	regList.clear();
		for (auto iterator = RegisterInterval_in.begin(); iterator != RegisterInterval_in.end(); iterator++){
			if ((*iterator)->nextLevelInterval() == *Interval){
				auto R = (*iterator)->get_register_list();
				for (auto it = R.begin(); it != R.end(); it++){
					regList.insert(*it);
				}
			}
		}
		(*Interval)->set_register_list(regList);
	}

	/*this loop sets the series of instructions in all output register-intervals*/
	string code;
	for (auto Interval = RegisterInterval_out.begin(); Interval != RegisterInterval_out.end(); Interval++){
		code = "";
		for (auto iterator = RegisterInterval_in.begin(); iterator != RegisterInterval_in.end(); iterator++){
			if ((*iterator)->nextLevelInterval() == *Interval){
				code = code + (*iterator)->get_code();
			}
		}
		(*Interval)->set_code(code);
	}

	/*this loop sets the predecessors and successors of all output register-intervals*/
	for (auto Interval = RegisterInterval_out.begin(); Interval != RegisterInterval_out.end(); Interval++){
		for (auto iterator = RegisterInterval_in.begin(); iterator != RegisterInterval_in.end(); iterator++){
			if ((*iterator)->nextLevelInterval() == *Interval){
				auto Predecessors = (*iterator)->get_predecessors();
                                for (auto p = Predecessors.begin(); p != Predecessors.end(); p++){
					if ((*p)->nextLevelInterval() != *Interval)
						(*Interval)->add_successors((*p)->nextLevelInterval());
				}
                                
				auto Successors = (*iterator)->get_successors();
				for (auto s = Successors.begin(); s != Successors.end(); s++){
					if ((*s)->nextLevelInterval() != *Interval){
						(*Interval)->add_predecessor((*s)->nextLevelInterval());
					}
				}
			}
		}
	}

	return RegisterInterval_out;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1d8, %rsp            # imm = 0x1D8
movl %edx, -0x40(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, (%rdi)
movq %rdi, -0x38(%rbp)
andq $0x0, 0x10(%rdi)
movq %rsi, -0x30(%rbp)
movq (%rsi), %rax
movq -0x30(%rbp), %rcx
cmpq 0x8(%rcx), %rax
je 0x3fdf
movq (%rax), %rcx
andq $0x0, 0x30(%rcx)
addq $0x8, %rax
jmp 0x3fc7
leaq -0x150(%rbp), %rdi
callq 0x6c8c
movq -0x30(%rbp), %rax
movq (%rax), %r15
movl $0x90, %r14d
leaq -0x70(%rbp), %r12
leaq -0x150(%rbp), %rbx
movq -0x30(%rbp), %rax
cmpq 0x8(%rax), %r15
je 0x4078
movq (%r15), %rsi
addq %r14, %rsi
movq %r12, %rdi
callq 0x6a50
movq -0x68(%rbp), %rax
cmpq -0x70(%rbp), %rax
jne 0x406a
movl $0xc0, %edi
callq 0x21c0
movq %rax, %r13
movq %rax, %rdi
callq 0x25cc
movq %r13, -0xa0(%rbp)
movq -0x38(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x6a2c
movq (%r15), %rax
movq -0xa0(%rbp), %rcx
movq %rcx, 0x30(%rax)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x89fe
movq %r12, %rdi
callq 0x781c
addq $0x8, %r15
jmp 0x4003
movslq -0x40(%rbp), %rax
movq %rax, -0xa8(%rbp)
leaq -0x98(%rbp), %r14
leaq -0x70(%rbp), %r12
movq -0x140(%rbp), %rax
cmpq %rax, -0x120(%rbp)
je 0x43f6
movq (%rax), %r13
leaq -0x150(%rbp), %rdi
callq 0x8c4a
movq 0x30(%r13), %rax
movq %rax, -0x40(%rbp)
addq $0x60, %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x6ef4
movq -0x48(%rbp), %rbx
movq %r12, %rdi
callq 0x6e7c
cmpq -0xa8(%rbp), %rbx
leaq -0x68(%rbp), %rbx
jae 0x42f8
leaq -0x200(%rbp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x6ef4
movq -0x40(%rbp), %rdi
movq %r15, %rsi
callq 0x2746
movq %r15, %rdi
callq 0x6e7c
movq -0x30(%rbp), %rax
movq (%rax), %r13
movq -0x40(%rbp), %rax
addq $0x60, %rax
movq %rax, -0xb8(%rbp)
movq -0x30(%rbp), %rax
cmpq 0x8(%rax), %r13
je 0x42f8
movq (%r13), %rsi
movl $0x90, %eax
addq %rax, %rsi
leaq -0xd0(%rbp), %rdi
callq 0x6a50
andl $0x0, -0x68(%rbp)
andq $0x0, -0x60(%rbp)
movq %rbx, -0x58(%rbp)
movq %rbx, -0x50(%rbp)
andq $0x0, -0x48(%rbp)
movq %r12, %rdi
callq 0x75c8
movq (%r13), %rdi
callq 0x47fe
movq -0xd0(%rbp), %rax
movq -0xc8(%rbp), %rcx
movb $0x1, %r15b
movq -0x40(%rbp), %rsi
cmpq %rcx, %rax
je 0x41a1
movq (%rax), %rdx
cmpq %rsi, 0x30(%rdx)
je 0x419b
cmpq (%r13), %rdx
sete %dl
andb %dl, %r15b
addq $0x8, %rax
jmp 0x4183
movq %r13, -0xb0(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rbx
movq -0x30(%rbp), %rax
cmpq 0x8(%rax), %rbx
je 0x4203
movq (%rbx), %rsi
addq $0x60, %rsi
leaq -0xa0(%rbp), %rdi
callq 0x6ef4
movq -0x88(%rbp), %r13
cmpq %r14, %r13
je 0x41f1
leaq 0x20(%r13), %rsi
movq %r12, %rdi
callq 0x7ee6
movq %r13, %rdi
callq 0x2200
movq %rax, %r13
jmp 0x41d3
leaq -0xa0(%rbp), %rdi
callq 0x6e7c
addq $0x8, %rbx
jmp 0x41af
movq -0xb0(%rbp), %r13
movq (%r13), %rax
cmpq $0x0, 0x30(%rax)
leaq -0x68(%rbp), %rbx
jne 0x42db
movq -0xa8(%rbp), %rcx
cmpq %rcx, -0x48(%rbp)
setb %cl
andb %cl, %r15b
cmpb $0x1, %r15b
jne 0x42db
movq -0x40(%rbp), %rcx
movq %rcx, 0x30(%rax)
movq (%r13), %rsi
addq $0x60, %rsi
leaq -0xa0(%rbp), %rdi
callq 0x6ef4
leaq -0x100(%rbp), %r15
movq %r15, %rdi
movq -0xb8(%rbp), %rsi
callq 0x6ef4
movq %r12, %rdi
movq %r15, %rsi
callq 0x7572
movq %r15, %rdi
callq 0x6e7c
movq -0x88(%rbp), %r13
cmpq %r14, %r13
je 0x42a2
leaq 0x20(%r13), %rsi
movq %r12, %rdi
callq 0x7ee6
movq %r13, %rdi
callq 0x2200
movq %rax, %r13
jmp 0x4284
leaq -0x1d0(%rbp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x6ef4
movq -0x40(%rbp), %rdi
movq %r15, %rsi
callq 0x2746
movq %r15, %rdi
callq 0x6e7c
leaq -0xa0(%rbp), %rdi
callq 0x6e7c
movq -0xb0(%rbp), %r13
movq %r12, %rdi
callq 0x6e7c
leaq -0xd0(%rbp), %rdi
callq 0x781c
addq $0x8, %r13
jmp 0x4121
andl $0x0, -0x68(%rbp)
andq $0x0, -0x60(%rbp)
movq %rbx, -0x58(%rbp)
movq %rbx, -0x50(%rbp)
andq $0x0, -0x48(%rbp)
movq %r12, %rdi
callq 0x8cac
movq -0x30(%rbp), %rax
movq (%rax), %rbx
movq -0x30(%rbp), %rax
cmpq 0x8(%rax), %rbx
je 0x437a
movq (%rbx), %rsi
movq -0x40(%rbp), %rax
cmpq %rax, 0x30(%rsi)
jne 0x4374
addq $0xa8, %rsi
leaq -0xa0(%rbp), %rdi
callq 0x6a50
movq -0xa0(%rbp), %r13
cmpq -0x98(%rbp), %r13
je 0x4368
movq %r12, %rdi
movq %r13, %rsi
callq 0x8ce4
addq $0x8, %r13
jmp 0x434e
leaq -0xa0(%rbp), %rdi
callq 0x781c
addq $0x8, %rbx
jmp 0x431d
movq -0x58(%rbp), %r15
leaq -0x68(%rbp), %rbx
cmpq %rbx, %r15
je 0x43e9
movq 0x20(%r15), %rax
cmpq $0x0, 0x30(%rax)
jne 0x43dc
movl $0xc0, %edi
callq 0x21c0
movq %rax, %r13
movq %rax, %rdi
callq 0x25cc
movq %r13, -0xa0(%rbp)
movq -0x38(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x6a2c
leaq 0x20(%r15), %rsi
movq (%rsi), %rax
movq -0xa0(%rbp), %rcx
movq %rcx, 0x30(%rax)
leaq -0x150(%rbp), %rdi
callq 0x89fe
movq %r15, %rdi
callq 0x2200
movq %rax, %r15
jmp 0x4382
movq %r12, %rdi
callq 0x7696
jmp 0x408e
movq -0x38(%rbp), %rax
movq (%rax), %r12
leaq -0x70(%rbp), %rbx
movq -0x38(%rbp), %rax
cmpq 0x8(%rax), %r12
je 0x44d0
andl $0x0, -0x68(%rbp)
andq $0x0, -0x60(%rbp)
leaq -0x68(%rbp), %rax
movq %rax, -0x58(%rbp)
movq %rax, -0x50(%rbp)
andq $0x0, -0x48(%rbp)
movq %rbx, %rdi
callq 0x75c8
movq -0x30(%rbp), %rax
movq (%rax), %r15
movq -0x30(%rbp), %rax
cmpq 0x8(%rax), %r15
je 0x4496
movq (%r15), %rsi
movq 0x30(%rsi), %rax
cmpq (%r12), %rax
jne 0x4490
addq $0x60, %rsi
leaq -0xa0(%rbp), %rdi
callq 0x6ef4
movq -0x88(%rbp), %r13
cmpq %r14, %r13
je 0x4484
leaq 0x20(%r13), %rsi
movq %rbx, %rdi
callq 0x7ee6
movq %r13, %rdi
callq 0x2200
movq %rax, %r13
jmp 0x4466
leaq -0xa0(%rbp), %rdi
callq 0x6e7c
addq $0x8, %r15
jmp 0x4438
movq (%r12), %r13
leaq -0x1a0(%rbp), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x6ef4
movq %r13, %rdi
movq %r15, %rsi
callq 0x2746
movq %r15, %rdi
callq 0x6e7c
movq %rbx, %rdi
callq 0x6e7c
addq $0x8, %r12
jmp 0x4401
leaq -0x60(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq -0x38(%rbp), %rax
movq (%rax), %r14
leaq -0x70(%rbp), %r15
leaq -0x100(%rbp), %r13
leaq -0xa0(%rbp), %rbx
cmpq %r12, %r14
je 0x45a7
movq %r15, %rdi
leaq 0x4baa(%rip), %rsi      # 0x90b6
callq 0x2360
movq -0x30(%rbp), %rax
movq (%rax), %r12
movq -0x30(%rbp), %rax
cmpq 0x8(%rax), %r12
je 0x456a
movq (%r12), %rsi
movq 0x30(%rsi), %rax
cmpq (%r14), %rax
jne 0x4564
addq $0x38, %rsi
movq %r13, %rdi
callq 0x2270
movq %rbx, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x6c5e
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2220
movq %rbx, %rdi
callq 0x2100
movq %r13, %rdi
callq 0x2100
addq $0x8, %r12
jmp 0x4518
movq (%r14), %r12
leaq -0x170(%rbp), %rdi
movq %r15, %rsi
callq 0x2270
movq %r12, %rdi
leaq -0x170(%rbp), %r12
movq %r12, %rsi
callq 0x26a0
movq %r12, %rdi
callq 0x2100
addq $0x8, %r14
movq -0x38(%rbp), %rax
movq 0x8(%rax), %r12
jmp 0x44f9
movq -0x38(%rbp), %rax
movq (%rax), %r14
leaq -0xa0(%rbp), %rbx
leaq -0x100(%rbp), %r15
cmpq %r12, %r14
je 0x468e
movq -0x30(%rbp), %rax
movq (%rax), %r12
movq -0x30(%rbp), %rax
cmpq 0x8(%rax), %r12
je 0x467d
movq (%r12), %rsi
movq 0x30(%rsi), %rax
cmpq (%r14), %rax
jne 0x4674
addq $0x90, %rsi
movq %rbx, %rdi
callq 0x6a50
movq -0xa0(%rbp), %r13
cmpq -0x98(%rbp), %r13
je 0x4625
movq (%r13), %rax
movq 0x30(%rax), %rsi
movq (%r14), %rdi
cmpq %rdi, %rsi
je 0x461f
callq 0x2870
addq $0x8, %r13
jmp 0x4601
movq (%r12), %rsi
movl $0xa8, %eax
addq %rax, %rsi
movq %r15, %rdi
callq 0x6a50
movq -0x100(%rbp), %r13
cmpq -0xf8(%rbp), %r13
je 0x4664
movq (%r13), %rax
movq 0x30(%rax), %rsi
movq (%r14), %rdi
cmpq %rdi, %rsi
je 0x465e
callq 0x2816
addq $0x8, %r13
jmp 0x4640
movq %r15, %rdi
callq 0x781c
movq %rbx, %rdi
callq 0x781c
addq $0x8, %r12
jmp 0x45cc
addq $0x8, %r14
movq -0x38(%rbp), %rax
movq 0x8(%rax), %r12
jmp 0x45bc
leaq -0x70(%rbp), %rdi
callq 0x2100
leaq -0x150(%rbp), %rdi
callq 0x76ec
movq -0x38(%rbp), %rax
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x47ed
jmp 0x474a
movq %rax, %rbx
leaq -0x200(%rbp), %rdi
jmp 0x47b1
movq %rax, %rbx
leaq -0x170(%rbp), %rdi
jmp 0x473c
jmp 0x4743
movq %rax, %rbx
leaq -0x1a0(%rbp), %rdi
jmp 0x47a8
jmp 0x474a
jmp 0x474a
movq %rax, %rbx
leaq -0x70(%rbp), %rdi
jmp 0x47dc
jmp 0x472d
movq %rax, %rbx
leaq -0x1d0(%rbp), %rdi
callq 0x6e7c
jmp 0x47c0
jmp 0x47bd
jmp 0x47bd
jmp 0x47b8
jmp 0x472d
jmp 0x476a
jmp 0x4752
movq %rax, %rbx
jmp 0x4779
movq %rax, %rbx
leaq -0x100(%rbp), %rdi
callq 0x2100
jmp 0x4779
movq %rax, %rbx
jmp 0x47ad
jmp 0x47b8
movq %rax, %rbx
jmp 0x47e1
movq %rax, %rbx
jmp 0x4793
jmp 0x47bd
movq %rax, %rbx
leaq -0x100(%rbp), %rdi
callq 0x781c
jmp 0x476d
movq %rax, %rbx
leaq -0xa0(%rbp), %rdi
callq 0x781c
leaq -0x70(%rbp), %rdi
callq 0x2100
jmp 0x47e1
movq %rax, %rbx
leaq -0xa0(%rbp), %rdi
callq 0x781c
leaq -0x70(%rbp), %rdi
callq 0x7696
jmp 0x47e1
movq %rax, %rbx
leaq -0xa0(%rbp), %rdi
callq 0x6e7c
leaq -0x70(%rbp), %rdi
callq 0x6e7c
jmp 0x47e1
movq %rax, %rbx
jmp 0x47cc
movq %rax, %rbx
leaq -0xa0(%rbp), %rdi
callq 0x6e7c
leaq -0x70(%rbp), %rdi
callq 0x6e7c
leaq -0xd0(%rbp), %rdi
callq 0x781c
leaq -0x150(%rbp), %rdi
callq 0x76ec
movq -0x38(%rbp), %rdi
callq 0x781c
movq %rbx, %rdi
callq 0x2340