import java.lang.foreign.*;

public class Test3 {
    public static long init  = 1000;
    public static long limit = 9000;
    public static final long BIG = 0x200000000L;
    public static long big = -BIG;

    public static void main() {
        //MemorySegment a = Arena.ofAuto().allocate(40_000);
        MemorySegment a = MemorySegment.ofArray(new byte[40_000]);
        for (int k = 0; k < 200; k++) {
            a.fill((byte)0);
            test(a, a);
            long s = sum(a);
            System.out.println("sum: " + s);
            if (s != 4000) {
                throw new RuntimeException("wrong value");
            }
        }
    }

    static int sum(MemorySegment a) {
        int s = 0;
        for (long i = init; i < limit; i++) {
            s += a.get(ValueLayout.JAVA_INT_UNALIGNED, 4L * i);
        }
        return s;
    }

    static void test(MemorySegment a, MemorySegment b) {
        long adr = 4L * 5000 + BIG + big;

        for (long i = init; i < limit; i++) {
            a.set(ValueLayout.JAVA_INT_UNALIGNED, 4L * i, 0);
            int v = b.get(ValueLayout.JAVA_INT_UNALIGNED, adr);
            b.set(ValueLayout.JAVA_INT_UNALIGNED, adr, v + 1);
        }
    }
}
