Error in DC code generation (co function)

We have isolated the following sequence of statements in a single user cofunction that generates seemingly incorrect code, and fails to execute correctly.

Wondering if anyone has similar problems, or if there is anything we could be doing wrong.

Here is the sequence:

int i;

wfd i=cof_dvrRead(handle, reply, 2, , 500L*(rt+1));
if (dvrDebugRd) {
printf("FROM DVR1 (%d):
",i);
_printfHexBuf(reply, i);
}
if (i!=2) { lastErr=-2; continue; }

In a specific instance we know from the printf() that i == 0, yet the condition in the statement: if (i!=2) is not met and execution continues incorrectly…

The code in the LST file seems to explain the problem: While the variable i is generally accessed as (ix-5), consistent with a local variable, when the test on (i!=2) is performed, the i variable is accessed as (ix+251)!

Anyone able to explain this?

[DVR.LIB(263:1)]: wfd i=cof_dvrRead(handle, reply, 2, 500L*(rt+1)); // get AA+1 byte
[DVR.LIB(264:3)]: if (dvrDebugRd) {
24:e614 3AAC93 ld a,(0x93AC) 9
24:e617 CC bool hl 2
24:e618 6F ld l,a 2
24:e619 CC bool hl 2
24:e61a CA36E6 jp z,0xE636 7
[DVR.LIB(265:4)]: printf("FROM DVR1 (%d):
",i);
24:e61d E4FB ld hl,(ix-5) 9
24:e61f E5 push hl 10
24:e620 214341 ld hl,0x4143 6
24:e623 E5 push hl 10
[DVR.LIB(265:33)]: ;
24:e624 CF0DE636 lcall printf 19
24:e628 2704 add sp,0x04 4
[DVR.LIB(266:4)]: _printfHexBuf(reply, i);
24:e62a E4FB ld hl,(ix-5) 9
24:e62c E5 push hl 10
24:e62d E40A ld hl,(ix+10) 9
24:e62f E5 push hl 10
[DVR.LIB(266:27)]: ;
24:e630 CF4DE100 lcall _printfHexBuf 19
24:e634 2704 add sp,0x04 4
[DVR.LIB(268:3)]: if (i!=2) { lastErr=-2; continue; }
24:e636 DD5EFB ld e,(ix+251) 9
24:e639 DD56FC ld d,(ix+252) 9
24:e63c 210200 ld hl,0x0002 6
24:e63f B7 or a 2
24:e640 ED52 sbc hl,de 4
24:e642 CA4DE6 jp z,0xE64D 7

We solved the problem ourselves:
turns out ld xy,(ix-5)
is the same as
ld y,(ix+251)
ld x,(ix+252)
Since the 251 = -5 in two’s complement.
Very silly of us…