// Generated by jextract

package me.x150.j2cc.util.natives;

import java.lang.invoke.*;
import java.lang.foreign.*;
import java.nio.ByteOrder;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;

import static java.lang.foreign.ValueLayout.*;
import static java.lang.foreign.MemoryLayout.PathElement.*;

public class chacha20_h {

    chacha20_h() {
        // Should not be called directly
    }

    static final Arena LIBRARY_ARENA = Arena.ofAuto();
    static final boolean TRACE_DOWNCALLS = Boolean.getBoolean("jextract.trace.downcalls");

    static void traceDowncall(String name, Object... args) {
         String traceArgs = Arrays.stream(args)
                       .map(Object::toString)
                       .collect(Collectors.joining(", "));
         System.out.printf("%s(%s)\n", name, traceArgs);
    }

    static MemorySegment findOrThrow(String symbol) {
        return SYMBOL_LOOKUP.find(symbol)
            .orElseThrow(() -> new UnsatisfiedLinkError("unresolved symbol: " + symbol));
    }

    static MethodHandle upcallHandle(Class<?> fi, String name, FunctionDescriptor fdesc) {
        try {
            return MethodHandles.lookup().findVirtual(fi, name, fdesc.toMethodType());
        } catch (ReflectiveOperationException ex) {
            throw new AssertionError(ex);
        }
    }

    static MemoryLayout align(MemoryLayout layout, long align) {
        return switch (layout) {
            case PaddingLayout p -> p;
            case ValueLayout v -> v.withByteAlignment(align);
            case GroupLayout g -> {
                MemoryLayout[] alignedMembers = g.memberLayouts().stream()
                        .map(m -> align(m, align)).toArray(MemoryLayout[]::new);
                yield g instanceof StructLayout ?
                        MemoryLayout.structLayout(alignedMembers) : MemoryLayout.unionLayout(alignedMembers);
            }
            case SequenceLayout s -> MemoryLayout.sequenceLayout(s.elementCount(), align(s.elementLayout(), align));
        };
    }

    static final SymbolLookup SYMBOL_LOOKUP = SymbolLookup.libraryLookup(System.mapLibraryName("nativeUtils"), LIBRARY_ARENA)
            .or(SymbolLookup.loaderLookup())
            .or(Linker.nativeLinker().defaultLookup());

    public static final ValueLayout.OfBoolean C_BOOL = ValueLayout.JAVA_BOOLEAN;
    public static final ValueLayout.OfByte C_CHAR = ValueLayout.JAVA_BYTE;
    public static final ValueLayout.OfShort C_SHORT = ValueLayout.JAVA_SHORT;
    public static final ValueLayout.OfInt C_INT = ValueLayout.JAVA_INT;
    public static final ValueLayout.OfLong C_LONG_LONG = ValueLayout.JAVA_LONG;
    public static final ValueLayout.OfFloat C_FLOAT = ValueLayout.JAVA_FLOAT;
    public static final ValueLayout.OfDouble C_DOUBLE = ValueLayout.JAVA_DOUBLE;
    public static final AddressLayout C_POINTER = ValueLayout.ADDRESS
            .withTargetLayout(MemoryLayout.sequenceLayout(java.lang.Long.MAX_VALUE, JAVA_BYTE));
    public static final ValueLayout.OfLong C_LONG = ValueLayout.JAVA_LONG;

    private static class fuckMyShitUp {
        public static final FunctionDescriptor DESC = FunctionDescriptor.ofVoid(
            chacha20_h.C_POINTER,
            chacha20_h.C_POINTER,
            chacha20_h.C_LONG
        );

        public static final MemorySegment ADDR = chacha20_h.findOrThrow("fuckMyShitUp");

        public static final MethodHandle HANDLE = Linker.nativeLinker().downcallHandle(ADDR, DESC);
    }

    /**
     * Function descriptor for:
     * {@snippet lang=c :
     * void fuckMyShitUp(const unsigned char *keyData, unsigned char chachaTable[], size_t nBlocks)
     * }
     */
    public static FunctionDescriptor fuckMyShitUp$descriptor() {
        return fuckMyShitUp.DESC;
    }

    /**
     * Downcall method handle for:
     * {@snippet lang=c :
     * void fuckMyShitUp(const unsigned char *keyData, unsigned char chachaTable[], size_t nBlocks)
     * }
     */
    public static MethodHandle fuckMyShitUp$handle() {
        return fuckMyShitUp.HANDLE;
    }

    /**
     * Address for:
     * {@snippet lang=c :
     * void fuckMyShitUp(const unsigned char *keyData, unsigned char chachaTable[], size_t nBlocks)
     * }
     */
    public static MemorySegment fuckMyShitUp$address() {
        return fuckMyShitUp.ADDR;
    }

    /**
     * {@snippet lang=c :
     * void fuckMyShitUp(const unsigned char *keyData, unsigned char chachaTable[], size_t nBlocks)
     * }
     */
    public static void fuckMyShitUp(MemorySegment keyData, MemorySegment chachaTable, long nBlocks) {
        var mh$ = fuckMyShitUp.HANDLE;
        try {
            if (TRACE_DOWNCALLS) {
                traceDowncall("fuckMyShitUp", keyData, chachaTable, nBlocks);
            }
            mh$.invokeExact(keyData, chachaTable, nBlocks);
        } catch (Throwable ex$) {
           throw new AssertionError("should not reach here", ex$);
        }
    }
}

