Topic: 3DES CBC Encryption and Decryption mismatch

Here is my code for 3DES CBC implementation:

/* CBC mode
     */
///////////////////////////////
        
    for( i = 0; i < 6; i++ )
    {
        
        u = i >> 1;
        v = (i & 1);
        printf("The value of u is: %d\n", u);
        printf("The value of v is: %d\n", v);
        if( verbose != 0 )
            polarssl_printf( "  DES%c-CBC-%3d (%s): ",
                             ( u == 0 ) ? ' ' : '3', 56 + u * 56,
                             ( v == DES_DECRYPT ) ? "dec" : "enc" );

        memcpy( iv,  des3_test_iv,  8 );
        memcpy( prv, des3_test_iv,  8 );
        memcpy( buf, des3_test_buf, 8 );
        ///////////////////////////////
        printf("The buffer at this step is:\n");
            for(k = 0; k <8; k++)
            printf("0x%x-", buf[k]);
            printf("\n");
        ///////////////////////////////
        ///////////////////////////////
        printf("The IV at this step is:\n");
            for(k = 0; k <8; k++)
            printf("0x%x-", iv[k]);
            printf("\n");
        ///////////////////////////////
            ///////////////////////////////
        printf("The PRV at this step is:\n");
            for(k = 0; k <8; k++)
            printf("0x%x-", prv[k]);
            printf("\n");
        ///////////////////////////////
        switch( i )
        {
        case 0:
            des_setkey_dec( &ctx, des3_test_keys );
            break;

        case 1:
            des_setkey_enc( &ctx, des3_test_keys );
            break;

        case 2:
            des3_set2key_dec( &ctx3, des3_test_keys );
            break;

        case 3:
            des3_set2key_enc( &ctx3, des3_test_keys );
            break;

        case 4:
            des3_set3key_dec( &ctx3, des3_test_keys );
            break;

        case 5:
            des3_set3key_enc( &ctx3, des3_test_keys );
            break;

        default:
            return( 1 );
        }

        if( v == DES_DECRYPT )
        {
            for( j = 0; j < 10000; j++ )
            {
                if( u == 0 )
                    des_crypt_cbc( &ctx, v, 8, iv, buf, buf );
                else
                    des3_crypt_cbc( &ctx3, v, 8, iv, buf, buf );
            }
        }
        else
        {
            for( j = 0; j < 10000; j++ )
            {
                unsigned char tmp[8];

                if( u == 0 )
                    des_crypt_cbc( &ctx, v, 8, iv, buf, buf );
                else
                    des3_crypt_cbc( &ctx3, v, 8, iv, buf, buf );

                memcpy( tmp, prv, 8 );
                memcpy( prv, buf, 8 );
                memcpy( buf, tmp, 8 );
            }

            memcpy( buf, prv, 8 );
        }

        if( ( v == DES_DECRYPT &&
                memcmp( buf, des3_test_cbc_dec[u], 8 ) != 0 ) ||
            ( v != DES_DECRYPT &&
                memcmp( buf, des3_test_cbc_enc[u], 8 ) != 0 ) )
        {
            if( verbose != 0 )
                polarssl_printf( "failed\n" );

            //return( 1 );
        }

        if( verbose != 0 )
            polarssl_printf( "passed\n" );
        ///////////////////////////////
        printf("The buffer AFTER this step is:\n");
            for(k = 0; k <8; k++)
            printf("0x%x-", buf[k]);
            printf("\n");
        ///////////////////////////////
        ///////////////////////////////
        printf("The IV AFTER this step is:\n");
            for(k = 0; k <8; k++)
            printf("0x%x-", iv[k]);
            printf("\n");
        ///////////////////////////////
    }

    if( verbose != 0 )
        polarssl_printf( "\n" );
    return( 0 );

When I run
ENCRYPTION(buf with IV) it will give answer A, and the next time it's still the same buffer,same IV but DECRYPTION, it will give answer B
then, I try to change DECRYPTION(buf with IV) it will give answer B', and then ENCRYPTION that same buffer same IV, it will give answer A'.
And surprisingly A != A', and B != B',
I don't know what I am doing wrong here