// java --add-modules java.base --add-exports java.base/jdk.internal.misc=ALL-UNNAMED --add-exports java.base/jdk.internal.util=ALL-UNNAMED -Xbatch -XX:-TieredCompilation -XX:CompileCommand=compileonly,Test.test -XX:CompileCommand=printcompilation,Test.test -XX:PrintPhaseLevel=4 Test.java

import jdk.internal.misc.Unsafe;

public class Test {
    private static final Unsafe UNSAFE = Unsafe.getUnsafe();
    private static final int SIZE = 10;

    public static void main(String[] args) {
        byte[] aB = new byte[SIZE];
        int[] aI = new int[SIZE];

        byte[] goldB = aB.clone();
        int[] goldI = aI.clone();
        test(goldB, goldI, 0, goldB);


        for (int i = 0; i < 9_000; i++) {
            byte[] bb = aB.clone();
            int[] ii = aI.clone();
            test(bb, ii, i, bb);
        }

        byte[] bb = aB.clone();
        int[] ii = aI.clone();
        test(bb, ii, 0, bb);

        for (int i = 0; i < SIZE; i++) {
            if (bb[i] != goldB[i] || ii[i] != goldI[i]) {
                throw new RuntimeException("wrong value");
            }
        }
    }

    static void test(byte[] aB, int[] aI, int i, byte[] bB) {
        Object a = null;
        long base = 0;
        if (i % 2 == 0) {
            a = aB;
            base = UNSAFE.ARRAY_BYTE_BASE_OFFSET;
        } else {
            a = aI;
            base = UNSAFE.ARRAY_INT_BASE_OFFSET;
        }

        byte v1 = UNSAFE.getByte(a, base);
        UNSAFE.putByte(bB, base + 1, (byte)(v1 + 1));
        byte v2 = UNSAFE.getByte(a, base + 1);
        UNSAFE.putByte(bB, base + 2, (byte)(v2 + 1));
    }
}
