Program: 14189
Project: 14189
Buggy source:
private ScalingProcessTask<?,?> perhapsTerminateInstances(final AutoScalingGroupScalingView group,final int terminateCount){
  final List<String> instancesToTerminate=Lists.newArrayList();
  int currentCapacity=0;
  try {
    final List<AutoScalingInstanceCoreView> currentInstances=autoScalingInstances.listByGroup(group,Predicates.alwaysTrue(),TypeMappers.lookup(AutoScalingInstance.class,AutoScalingInstanceCoreView.class));
    currentCapacity=currentInstances.size();
    if (currentInstances.size() == terminateCount) {
      Iterables.addAll(instancesToTerminate,Iterables.transform(currentInstances,RestrictedTypes.toDisplayName()));
    }
 else {
      final Set<String> groupZones=Sets.newLinkedHashSet(group.getAvailabilityZones());
      groupZones.removeAll(zoneMonitor.getUnavailableZones(AutoScalingConfiguration.getZoneFailureThresholdMillis()));
      final Set<String> unwantedZones=Sets.newHashSet(Iterables.transform(currentInstances,availabilityZone()));
      unwantedZones.removeAll(groupZones);
      final Set<String> targetZones;
      final List<AutoScalingInstanceCoreView> remainingInstances=Lists.newArrayList(currentInstances);
      if (!unwantedZones.isEmpty()) {
        int unwantedInstanceCount=CollectionUtils.reduce(currentInstances,0,CollectionUtils.count(withAvailabilityZone(unwantedZones)));
        if (unwantedInstanceCount < terminateCount) {
          Iterable<AutoScalingInstanceCoreView> unwantedInstances=Iterables.filter(currentInstances,withAvailabilityZone(unwantedZones));
          Iterables.addAll(instancesToTerminate,Iterables.transform(unwantedInstances,RestrictedTypes.toDisplayName()));
          Iterables.removeAll(remainingInstances,Lists.newArrayList(unwantedInstances));
          targetZones=groupZones;
        }
 else {
          targetZones=unwantedZones;
        }
      }
 else {
        targetZones=groupZones;
      }
      final Map<String,Integer> zoneCounts=buildAvailabilityZoneInstanceCounts(currentInstances,targetZones);
      for (int i=instancesToTerminate.size(); i < terminateCount && remainingInstances.size() >= 1; i++) {
        final Map.Entry<String,Integer> entry=selectEntry(zoneCounts,Ordering.natural().reverse());
        final AutoScalingInstanceCoreView instanceForTermination=TerminationPolicyType.selectForTermination(group.getTerminationPolicies(),Lists.newArrayList(Iterables.filter(remainingInstances,withAvailabilityZone(entry.getKey()))));
        remainingInstances.remove(instanceForTermination);
        entry.setValue(entry.getValue() - 1);
        instancesToTerminate.add(instanceForTermination.getInstanceId());
      }
    }
  }
 catch (  final Exception e) {
    logger.error(e,e);
  }
  final List<ActivityCause> causes=Lists.newArrayList();
  causes.add(new ActivityCause(String.format("String_Node_Str",group.getCapacity(),group.getCapacity() - instancesToTerminate.size())));
  for (  final String instanceId : instancesToTerminate) {
    causes.add(new ActivityCause(String.format("String_Node_Str",instanceId)));
  }
  return removeFromLoadBalancerOrTerminate(group,currentCapacity,instancesToTerminate,causes,false);
}

Fixed version (ground truth):
private ScalingProcessTask<?,?> perhapsTerminateInstances(final AutoScalingGroupScalingView group,final int terminateCount){
  final List<String> instancesToTerminate=Lists.newArrayList();
  boolean anyRegisteredInstances=false;
  int currentCapacity=0;
  try {
    final List<AutoScalingInstanceCoreView> currentInstances=autoScalingInstances.listByGroup(group,Predicates.alwaysTrue(),TypeMappers.lookup(AutoScalingInstance.class,AutoScalingInstanceCoreView.class));
    currentCapacity=currentInstances.size();
    if (currentInstances.size() == terminateCount) {
      Iterables.addAll(instancesToTerminate,Iterables.transform(currentInstances,RestrictedTypes.toDisplayName()));
      anyRegisteredInstances=Iterables.any(currentInstances,ConfigurationState.Registered.forView());
    }
 else {
      final Set<String> groupZones=Sets.newLinkedHashSet(group.getAvailabilityZones());
      groupZones.removeAll(zoneMonitor.getUnavailableZones(AutoScalingConfiguration.getZoneFailureThresholdMillis()));
      final Set<String> unwantedZones=Sets.newHashSet(Iterables.transform(currentInstances,availabilityZone()));
      unwantedZones.removeAll(groupZones);
      final Set<String> targetZones;
      final List<AutoScalingInstanceCoreView> remainingInstances=Lists.newArrayList(currentInstances);
      if (!unwantedZones.isEmpty()) {
        int unwantedInstanceCount=CollectionUtils.reduce(currentInstances,0,CollectionUtils.count(withAvailabilityZone(unwantedZones)));
        if (unwantedInstanceCount < terminateCount) {
          Iterable<AutoScalingInstanceCoreView> unwantedInstances=Iterables.filter(currentInstances,withAvailabilityZone(unwantedZones));
          Iterables.addAll(instancesToTerminate,Iterables.transform(unwantedInstances,RestrictedTypes.toDisplayName()));
          Iterables.removeAll(remainingInstances,Lists.newArrayList(unwantedInstances));
          anyRegisteredInstances=Iterables.any(unwantedInstances,ConfigurationState.Registered.forView());
          targetZones=groupZones;
        }
 else {
          targetZones=unwantedZones;
        }
      }
 else {
        targetZones=groupZones;
      }
      final Map<String,Integer> zoneCounts=buildAvailabilityZoneInstanceCounts(currentInstances,targetZones);
      for (int i=instancesToTerminate.size(); i < terminateCount && remainingInstances.size() >= 1; i++) {
        final Map.Entry<String,Integer> entry=selectEntry(zoneCounts,Ordering.natural().reverse());
        final AutoScalingInstanceCoreView instanceForTermination=TerminationPolicyType.selectForTermination(group.getTerminationPolicies(),Lists.newArrayList(Iterables.filter(remainingInstances,withAvailabilityZone(entry.getKey()))));
        remainingInstances.remove(instanceForTermination);
        entry.setValue(entry.getValue() - 1);
        instancesToTerminate.add(instanceForTermination.getInstanceId());
        anyRegisteredInstances|=ConfigurationState.Registered.forView().apply(instanceForTermination);
      }
    }
  }
 catch (  final Exception e) {
    logger.error(e,e);
  }
  final List<ActivityCause> causes=Lists.newArrayList();
  causes.add(new ActivityCause(String.format("String_Node_Str",group.getCapacity(),group.getCapacity() - instancesToTerminate.size())));
  for (  final String instanceId : instancesToTerminate) {
    causes.add(new ActivityCause(String.format("String_Node_Str",instanceId)));
  }
  return removeFromLoadBalancerOrTerminate(group,currentCapacity,anyRegisteredInstances,instancesToTerminate,causes,false);
}