The optimized code below fails tests that the original code passes. Fix the optimized code to match the original's behavior while keeping as much of the optimization intact as possible.

### Original source code (correct behavior):

{original_source_code}

### Optimized source code (to be repaired):

{modified_source_code}

### Test failures

The following tests show where the optimized code diverges from the original. Each entry shows the test source, what the original produced vs what the optimized code produced.

{test_details}

Analyze the pattern across these failures, identify the root cause in the optimized code, and output the minimal SEARCH/REPLACE fix.
