Program: 181447
Project: 181447
Buggy source:
void setOnBattery(boolean onBattery,int oldStatus,int level){
synchronized (this) {
    boolean doWrite=false;
    mOnBattery=mOnBatteryInternal=onBattery;
    long uptime=SystemClock.uptimeMillis() * 1000;
    long mSecRealtime=SystemClock.elapsedRealtime();
    long realtime=mSecRealtime * 1000;
    if (onBattery) {
      if (oldStatus == BatteryManager.BATTERY_STATUS_FULL || level >= 100 || (mDischargeCurrentLevel < 20 && level > 90)) {
        doWrite=true;
        resetAllStatsLocked();
        mDischargeStartLevel=level;
        mLowDischargeAmountSinceCharge=0;
        mHighDischargeAmountSinceCharge=0;
      }
      updateKernelWakelocksLocked();
      mHistoryCur.batteryLevel=(byte)level;
      mHistoryCur.states&=~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
      if (DEBUG_HISTORY)       Slog.v(TAG,"String_Node_Str" + Integer.toHexString(mHistoryCur.states));
      addHistoryRecordLocked(mSecRealtime);
      mTrackBatteryUptimeStart=uptime;
      mTrackBatteryRealtimeStart=realtime;
      mUnpluggedBatteryUptime=getBatteryUptimeLocked(uptime);
      mUnpluggedBatteryRealtime=getBatteryRealtimeLocked(realtime);
      mDischargeCurrentLevel=mDischargeUnplugLevel=level;
      doUnplugLocked(mUnpluggedBatteryUptime,mUnpluggedBatteryRealtime);
    }
 else {
      updateKernelWakelocksLocked();
      mHistoryCur.batteryLevel=(byte)level;
      mHistoryCur.states|=HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
      if (DEBUG_HISTORY)       Slog.v(TAG,"String_Node_Str" + Integer.toHexString(mHistoryCur.states));
      addHistoryRecordLocked(mSecRealtime);
      mTrackBatteryPastUptime+=uptime - mTrackBatteryUptimeStart;
      mTrackBatteryPastRealtime+=realtime - mTrackBatteryRealtimeStart;
      mDischargeCurrentLevel=level;
      if (level < mDischargeUnplugLevel) {
        mLowDischargeAmountSinceCharge=mDischargeUnplugLevel - level - 1;
        mHighDischargeAmountSinceCharge=mDischargeUnplugLevel - level;
      }
      doPlugLocked(getBatteryUptimeLocked(uptime),getBatteryRealtimeLocked(realtime));
    }
    if (doWrite || (mLastWriteTime + (60 * 1000)) < mSecRealtime) {
      if (mFile != null) {
        writeLocked();
      }
    }
  }
}

Fixed version (ground truth):
void setOnBattery(boolean onBattery,int oldStatus,int level){
synchronized (this) {
    boolean doWrite=false;
    mOnBattery=mOnBatteryInternal=onBattery;
    long uptime=SystemClock.uptimeMillis() * 1000;
    long mSecRealtime=SystemClock.elapsedRealtime();
    long realtime=mSecRealtime * 1000;
    if (onBattery) {
      if (oldStatus == BatteryManager.BATTERY_STATUS_FULL || level >= 100 || (mDischargeCurrentLevel < 20 && level > 90)) {
        doWrite=true;
        resetAllStatsLocked();
        mDischargeStartLevel=level;
        mLowDischargeAmountSinceCharge=0;
        mHighDischargeAmountSinceCharge=0;
      }
      updateKernelWakelocksLocked();
      mHistoryCur.batteryLevel=(byte)level;
      mHistoryCur.states&=~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
      if (DEBUG_HISTORY)       Slog.v(TAG,"String_Node_Str" + Integer.toHexString(mHistoryCur.states));
      addHistoryRecordLocked(mSecRealtime);
      mTrackBatteryUptimeStart=uptime;
      mTrackBatteryRealtimeStart=realtime;
      mUnpluggedBatteryUptime=getBatteryUptimeLocked(uptime);
      mUnpluggedBatteryRealtime=getBatteryRealtimeLocked(realtime);
      mDischargeCurrentLevel=mDischargeUnplugLevel=level;
      doUnplugLocked(mUnpluggedBatteryUptime,mUnpluggedBatteryRealtime);
    }
 else {
      updateKernelWakelocksLocked();
      mHistoryCur.batteryLevel=(byte)level;
      mHistoryCur.states|=HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
      if (DEBUG_HISTORY)       Slog.v(TAG,"String_Node_Str" + Integer.toHexString(mHistoryCur.states));
      addHistoryRecordLocked(mSecRealtime);
      mTrackBatteryPastUptime+=uptime - mTrackBatteryUptimeStart;
      mTrackBatteryPastRealtime+=realtime - mTrackBatteryRealtimeStart;
      mDischargeCurrentLevel=level;
      if (level < mDischargeUnplugLevel) {
        mLowDischargeAmountSinceCharge+=mDischargeUnplugLevel - level - 1;
        mHighDischargeAmountSinceCharge+=mDischargeUnplugLevel - level;
      }
      doPlugLocked(getBatteryUptimeLocked(uptime),getBatteryRealtimeLocked(realtime));
    }
    if (doWrite || (mLastWriteTime + (60 * 1000)) < mSecRealtime) {
      if (mFile != null) {
        writeLocked();
      }
    }
  }
}