def remove_refct_pairs(bb):
 didsomething = True
 while didsomething:
  didsomething = False
  increfs = {}
  decrefs = {}
  for inst in bb.instructions:
   if isinstance(inst, lc.CallOrInvokeInstruction):
    fname = inst.called_function.name
    if (fname == 'Py_IncRef'):
     arg = inst.operands[0]
     increfs[arg] = inst
    elif (fname == 'Py_DecRef'):
     arg = inst.operands[0]
     decrefs[arg] = inst
  for val in increfs.keys():
   if (val in decrefs):
    increfs[val].erase_from_parent()
    decrefs[val].erase_from_parent()
    didsomething = True

def remove_refct_pairs ( bb ) : didsomething = True while didsomething : didsomething = False increfs = { } decrefs = { } for inst in bb . instructions : if isinstance ( inst , lc . CallOrInvokeInstruction ) : fname = inst . called_function . name if ( fname == 'Py_IncRef' ) : arg = inst . operands [ 0 ] increfs [ arg ] = inst elif ( fname == 'Py_DecRef' ) : arg = inst . operands [ 0 ] decrefs [ arg ] = inst for val in increfs . keys ( ) : if ( val in decrefs ) : increfs [ val ] . erase_from_parent ( ) decrefs [ val ] . erase_from_parent ( ) didsomething = True
remove incref decref pairs on the same variable .