// File: /workspace/github_cpp_makefile_star2_O2/BUSEC[P]RPKI_Downgrade_Detector/./exp.cpp
// Function: UnknownToInvalid(ROAGroup*, ROAGroup*, std::vector<ROA*, std::allocator<ROA*>>*)
vector<UnknownToInvalidDowngrade> UnknownToInvalid( ROAGroup* newPrefixes, ROAGroup* oldPrefixes, vector<ROA*>* rNews){
  vector<UnknownToInvalidDowngrade> downgrades;
  
  unordered_map<int, PrefixRange*> notValidToValidPrefixes; // This map is every Prefix which was invalid/unknown in old and is valid in new
  NotValidToValid( *newPrefixes, *oldPrefixes, &notValidToValidPrefixes);

  // Find those AS \in A, where A is all the ASes which created newly valid prefixes.
  // A should just much smaller than the set of all ASes.
  vector<int> A;

  // Save all the keys of notValidToValidPrefixes, done to maintain consistant notion between paper and program. A = notValidToValidPrefixes.keys()
  for(unordered_map<int,PrefixRange*>::iterator i = notValidToValidPrefixes.begin(); i != notValidToValidPrefixes.end(); ++i) A.push_back(i->first);
  
  vector<ROA*> rNewlyValid;
  map<ROA*, PrefixRange*> unknownToKnownByR;
  // Compute all the ROAs that creately newly valid prefixes, since we need to compute 
  // unknown to known to do this lets save the result and reuse it below.

  const PrefixRange* oldUnknown = oldPrefixes->getUnknown(); 

  for( ROA* r: *rNews){  
    PrefixRange* rUnknownToKnown = new PrefixRange(); // Don't free this
    UnknownToKnown( oldUnknown, *r, rUnknownToKnown);

    if( ! rUnknownToKnown->empty() ){
      unknownToKnownByR[r] = rUnknownToKnown;
      rNewlyValid.push_back(r); 
    }
  }

  for (int as: A) {

    PrefixRange complementNotValidToValidPrefixes = notValidToValidPrefixes[as]->complement();

    for( ROA* r: rNewlyValid){      
      PrefixRange* rUnknownToKnown = new PrefixRange( *unknownToKnownByR[r] );

      UnknownToInvalidDowngrade downgrade = UnknownToInvalidDowngrade(*r, *rUnknownToKnown);

      rUnknownToKnown->denseIntersectWith( complementNotValidToValidPrefixes, true);

      PrefixRange* unknownToInvalid = rUnknownToKnown;

      downgrade.addException(as, unknownToInvalid); 

      if ( ! downgrade.empty() ){
        downgrades.push_back(downgrade);
      }
    } // R.New.valid loop
  } // as loop

  //clean up
  for( ROA* r: rNewlyValid) delete unknownToKnownByR[r];

  return downgrades;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2d8, %rsp            # imm = 0x2D8
movq %rcx, %rbx
movq %rdx, %r14
andq $0x0, 0x10(%rdi)
xorps %xmm0, %xmm0
movq %rdi, 0x8(%rsp)
movups %xmm0, (%rdi)
leaq 0xd8(%rsp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
movups %xmm0, -0x20(%rax)
movl $0x3f800000, -0x10(%rax) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
leaq 0x258(%rsp), %rdi
callq 0x3e74
leaq 0x1d8(%rsp), %rdi
movq %r14, %rsi
callq 0x3e74
leaq 0x258(%rsp), %rdi
leaq 0x1d8(%rsp), %rsi
leaq 0xa8(%rsp), %rdx
callq 0x2989
leaq 0xb8(%rsp), %r12
leaq 0x1d8(%rsp), %rdi
callq 0x3fc6
leaq 0x258(%rsp), %rdi
callq 0x3fc6
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %r15
movaps %xmm0, (%r15)
andq $0x0, 0x10(%r15)
movq (%r12), %r12
testq %r12, %r12
je 0x2bf6
leaq 0x8(%r12), %rsi
movq %r15, %rdi
callq 0x406e
jmp 0x2bde
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
leaq 0x80(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
movq %r14, %rdi
callq 0x3a7e
movq %rax, %r14
movq (%rbx), %r15
movq 0x8(%rbx), %rbx
leaq 0x128(%rsp), %r12
leaq 0x158(%rsp), %rbp
cmpq %rbx, %r15
je 0x2cc0
movq (%r15), %rax
movq %rax, 0x158(%rsp)
pushq $0x18
popq %rdi
callq 0x2240
movq %rax, %r13
movq %rax, %rdi
callq 0x3b0e
movq 0x158(%rsp), %rsi
movq %r12, %rdi
callq 0x408c
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x2abc
leaq 0x138(%rsp), %rdi
callq 0x20e0
movq %r13, %rdi
callq 0x3c52
testb %al, %al
jne 0x2cba
leaq 0x78(%rsp), %rdi
movq %rbp, %rsi
callq 0x40a8
movq %r13, (%rax)
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x40f8
addq $0x8, %r15
jmp 0x2c43
movq 0x40(%rsp), %rax
movq 0x48(%rsp), %rcx
movq %rcx, 0x30(%rsp)
leaq 0xf8(%rsp), %rbx
leaq 0xe0(%rsp), %r14
leaq 0x158(%rsp), %rbp
cmpq 0x30(%rsp), %rax
je 0x2df8
movq %rax, 0x38(%rsp)
movl (%rax), %eax
movl %eax, 0x4(%rsp)
leaq 0xa8(%rsp), %rdi
leaq 0x4(%rsp), %rsi
callq 0x611c
movq (%rax), %rsi
leaq 0x60(%rsp), %rdi
callq 0x3b34
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %r13
cmpq %r13, %r15
je 0x2de0
movq (%r15), %rax
movq %rax, 0x28(%rsp)
pushq $0x18
popq %rdi
callq 0x2240
movq %rax, %r12
leaq 0x78(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x40a8
movq (%rax), %rsi
movq %r12, %rdi
callq 0x3c86
movq 0x28(%rsp), %rsi
movq %rbx, %rdi
callq 0x408c
movq %r14, %rdi
movq %r12, %rsi
callq 0x3c86
movq %rbp, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x4116
movq %r14, %rdi
callq 0x3bf6
leaq 0x108(%rsp), %rdi
callq 0x20e0
movq %r12, %rdi
leaq 0x60(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x3a9a
movl 0x4(%rsp), %esi
movq %rbp, %rdi
movq %r12, %rdx
callq 0x4178
movq %rbp, %rdi
callq 0x41d8
testb %al, %al
jne 0x2dcf
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x41f2
movq %rbp, %rdi
callq 0x421e
addq $0x8, %r15
jmp 0x2d26
leaq 0x60(%rsp), %rdi
callq 0x3bf6
movq 0x38(%rsp), %rax
addq $0x4, %rax
jmp 0x2ce7
movq 0x10(%rsp), %r13
movq 0x18(%rsp), %rbp
leaq 0x78(%rsp), %rbx
leaq 0x158(%rsp), %r14
pushq $0x18
popq %r15
cmpq %rbp, %r13
je 0x2e50
movq (%r13), %rax
movq %rax, 0x158(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x40a8
movq (%rax), %r12
testq %r12, %r12
je 0x2e3f
movq %r12, %rdi
callq 0x3bf6
movq %r12, %rdi
movq %r15, %rsi
callq 0x2250
addq $0x8, %r13
jmp 0x2e13
leaq 0x78(%rsp), %rdi
callq 0x5e92
leaq 0x10(%rsp), %rdi
callq 0x6612
leaq 0x40(%rsp), %rdi
callq 0x649c
leaq 0xa8(%rsp), %rdi
callq 0x5eea
movq 0x8(%rsp), %rax
addq $0x2d8, %rsp            # imm = 0x2D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2eee
movq %rax, %rbx
leaq 0x1d8(%rsp), %rdi
callq 0x3fc6
jmp 0x2ea9
movq %rax, %rbx
leaq 0x258(%rsp), %rdi
callq 0x3fc6
jmp 0x2f68
movq %rax, %rbx
jmp 0x2f68
jmp 0x2eee
movq %rax, %rbx
pushq $0x18
popq %rsi
movq %r13, %rdi
callq 0x2250
jmp 0x2f4a
movq %rax, %rbx
leaq 0x138(%rsp), %rdi
callq 0x20e0
jmp 0x2f4a
movq %rax, %rbx
jmp 0x2f5e
jmp 0x2eee
movq %rax, %rbx
jmp 0x2f4a
jmp 0x2f30
jmp 0x2ef7
movq %rax, %rbx
jmp 0x2f40
movq %rax, %rbx
jmp 0x2f11
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x3bf6
leaq 0x108(%rsp), %rdi
callq 0x20e0
jmp 0x2f40
movq %rax, %rbx
pushq $0x18
popq %rsi
movq %r12, %rdi
callq 0x2250
jmp 0x2f40
movq %rax, %rbx
leaq 0x158(%rsp), %rdi
callq 0x421e
leaq 0x60(%rsp), %rdi
callq 0x3bf6
leaq 0x78(%rsp), %rdi
callq 0x5e92
leaq 0x10(%rsp), %rdi
callq 0x6612
leaq 0x40(%rsp), %rdi
callq 0x649c
leaq 0xa8(%rsp), %rdi
callq 0x5eea
movq 0x8(%rsp), %rdi
callq 0x4242
movq %rbx, %rdi
callq 0x2350