import java.lang.foreign.*;

public class TestLoop {
    public static int SIZE = 100_000;

    public static int[] a = new int[SIZE];
    public static long[] b = new long[SIZE];

    public static MemorySegment msA = MemorySegment.ofArray(a);
    public static MemorySegment msB = MemorySegment.ofArray(b);

    public static void main(String[] args) {
        for (int i = 0; i < 1000; i++) {
            test0();
            test1();
            test10();
            test11();
            test12();
	}
    }

    public static void test0() {
        for (int i = 0; i < ((int)msA.byteSize() / 8); i++) {
            int v = msA.get(ValueLayout.JAVA_INT_UNALIGNED, i * 4L);
            msB.set(ValueLayout.JAVA_LONG_UNALIGNED, i * 8L, v + 1);
	}
    }

    public static void test1() {
        int x1 = (int)msA.byteSize() / 8;
        for (int i = 0; i < x1; i++) {
            int v = msA.get(ValueLayout.JAVA_INT_UNALIGNED, i * 4L);
            msB.set(ValueLayout.JAVA_LONG_UNALIGNED, i * 8L, v + 1);
	}
    }

    // The only one that seems not to do LoopOpts right.
    // I suspect the reason could be an issue with RangeCheck elimination,
    // detecting a parallel iv. At least I see that the RangeCheck could
    // not be eliminated when it was eliminated for the other examples.
    public static void test10() {
        for (long i = 0; i < msA.byteSize() / 8L; i++) {
            int v = msA.get(ValueLayout.JAVA_INT_UNALIGNED, i * 4L);
            msB.set(ValueLayout.JAVA_LONG_UNALIGNED, i * 8L, v + 1);
	}
    }

    public static void test11() {
        long x11 = msA.byteSize() / 8L;
        for (long i = 0; i < x11; i++) {
            int v = msA.get(ValueLayout.JAVA_INT_UNALIGNED, i * 4L);
            msB.set(ValueLayout.JAVA_LONG_UNALIGNED, i * 8L, v + 1);
	}
    }

    public static long x12 = msA.byteSize() / 8;
    public static void test12() {
        for (long i = 0; i < x12; i++) {
            int v = msA.get(ValueLayout.JAVA_INT_UNALIGNED, i * 4L);
            msB.set(ValueLayout.JAVA_LONG_UNALIGNED, i * 8L, v + 1);
	}
    }
}
