import logging
from typing import Iterable, Tuple, Any

from .. import MemoryMixin

l = logging.getLogger(name=__name__)


class AbstractMergerMixin(MemoryMixin):

    def _merge_values(self, values: Iterable[Tuple[Any,Any]], merged_size: int):

        if self.category == 'reg' and self.state.arch.register_endness == 'Iend_LE':
            should_reverse = True
        elif self.state.arch.memory_endness == 'Iend_LE':
            should_reverse = True
        else:
            should_reverse = False

        values = list(values)
        merged_val = values[0][0]

        if should_reverse: merged_val = merged_val.reversed

        for tm, _ in values[1:]:
            if should_reverse: tm = tm.reversed

            if self._is_uninitialized(tm):
                continue
            l.info("Merging %s %s...", merged_val, tm)
            merged_val = merged_val.union(tm)
            l.info("... Merged to %s", merged_val)

        if should_reverse:
            merged_val = merged_val.reversed

        return merged_val

    @staticmethod
    def _is_uninitialized(a):
        return getattr(a._model_vsa, 'uninitialized', False)

import logging
from typing import Iterable, Tuple, Any

from .. import MemoryMixin

l = logging.getLogger(name=__name__)


class AbstractMergerMixin(MemoryMixin):

    def _merge_values(self, values: Iterable[Tuple[Any,Any]], merged_size: int):

        # if self.category == 'reg' and self.state.arch.register_endness == 'Iend_LE':
        #     should_reverse = True
        # elif self.state.arch.memory_endness == 'Iend_LE':
        #     should_reverse = True
        # else:
        #     should_reverse = False

        values = list(values)
        merged_val = values[0][0]

        # if should_reverse: merged_val = merged_val.reversed

        for tm, _ in values[1:]:
            # if should_reverse: tm = tm.reversed

            if self._is_uninitialized(tm):
                continue
            l.info("Merging %s %s...", merged_val, tm)
            merged_val = merged_val.union(tm)
            l.info("... Merged to %s", merged_val)

        # if should_reverse:
        #     merged_val = merged_val.reversed

        if not values[0][0].uninitialized and self.state.solver.backends.vsa.identical(merged_val, values[0][0]):
            return None

        return merged_val

    @staticmethod
    def _is_uninitialized(a):
        return getattr(a._model_vsa, 'uninitialized', False)

Remove reversing heuristics from merge_values for abstract memory.
Remove reversing heuristics from merge_values for abstract memory.

This is because SimMemoryObject handles endness now.

Also re-introduce the logic for dealing with uninit memory values.

  import logging
  from typing import Iterable, Tuple, Any
  
  from .. import MemoryMixin
  
  l = logging.getLogger(name=__name__)
  
  
  class AbstractMergerMixin(MemoryMixin):
  
      def _merge_values(self, values: Iterable[Tuple[Any,Any]], merged_size: int):
  
-         if self.category == 'reg' and self.state.arch.register_endness == 'Iend_LE':
+         # if self.category == 'reg' and self.state.arch.register_endness == 'Iend_LE':
-             should_reverse = True
+         #     should_reverse = True
-         elif self.state.arch.memory_endness == 'Iend_LE':
+         # elif self.state.arch.memory_endness == 'Iend_LE':
-             should_reverse = True
+         #     should_reverse = True
-         else:
+         # else:
-             should_reverse = False
+         #     should_reverse = False
  
          values = list(values)
          merged_val = values[0][0]
  
-         if should_reverse: merged_val = merged_val.reversed
+         # if should_reverse: merged_val = merged_val.reversed
  
          for tm, _ in values[1:]:
-             if should_reverse: tm = tm.reversed
+             # if should_reverse: tm = tm.reversed
  
              if self._is_uninitialized(tm):
                  continue
              l.info("Merging %s %s...", merged_val, tm)
              merged_val = merged_val.union(tm)
              l.info("... Merged to %s", merged_val)
  
-         if should_reverse:
+         # if should_reverse:
-             merged_val = merged_val.reversed
+         #     merged_val = merged_val.reversed
+ 
+         if not values[0][0].uninitialized and self.state.solver.backends.vsa.identical(merged_val, values[0][0]):
+             return None
  
          return merged_val
  
      @staticmethod
      def _is_uninitialized(a):
          return getattr(a._model_vsa, 'uninitialized', False)
  
Remove reversing heuristics from merge_values for abstract memory.
## Code Before:
import logging
from typing import Iterable, Tuple, Any

from .. import MemoryMixin

l = logging.getLogger(name=__name__)


class AbstractMergerMixin(MemoryMixin):

    def _merge_values(self, values: Iterable[Tuple[Any,Any]], merged_size: int):

        if self.category == 'reg' and self.state.arch.register_endness == 'Iend_LE':
            should_reverse = True
        elif self.state.arch.memory_endness == 'Iend_LE':
            should_reverse = True
        else:
            should_reverse = False

        values = list(values)
        merged_val = values[0][0]

        if should_reverse: merged_val = merged_val.reversed

        for tm, _ in values[1:]:
            if should_reverse: tm = tm.reversed

            if self._is_uninitialized(tm):
                continue
            l.info("Merging %s %s...", merged_val, tm)
            merged_val = merged_val.union(tm)
            l.info("... Merged to %s", merged_val)

        if should_reverse:
            merged_val = merged_val.reversed

        return merged_val

    @staticmethod
    def _is_uninitialized(a):
        return getattr(a._model_vsa, 'uninitialized', False)

## Instruction:
Remove reversing heuristics from merge_values for abstract memory.
## Code After:
import logging
from typing import Iterable, Tuple, Any

from .. import MemoryMixin

l = logging.getLogger(name=__name__)


class AbstractMergerMixin(MemoryMixin):

    def _merge_values(self, values: Iterable[Tuple[Any,Any]], merged_size: int):

        # if self.category == 'reg' and self.state.arch.register_endness == 'Iend_LE':
        #     should_reverse = True
        # elif self.state.arch.memory_endness == 'Iend_LE':
        #     should_reverse = True
        # else:
        #     should_reverse = False

        values = list(values)
        merged_val = values[0][0]

        # if should_reverse: merged_val = merged_val.reversed

        for tm, _ in values[1:]:
            # if should_reverse: tm = tm.reversed

            if self._is_uninitialized(tm):
                continue
            l.info("Merging %s %s...", merged_val, tm)
            merged_val = merged_val.union(tm)
            l.info("... Merged to %s", merged_val)

        # if should_reverse:
        #     merged_val = merged_val.reversed

        if not values[0][0].uninitialized and self.state.solver.backends.vsa.identical(merged_val, values[0][0]):
            return None

        return merged_val

    @staticmethod
    def _is_uninitialized(a):
        return getattr(a._model_vsa, 'uninitialized', False)
