import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

import sun.misc.Unsafe;
import java.lang.reflect.Field;

import static org.objectweb.asm.Opcodes.*;

public class Test8367002 {

    public static void main(String[] args) throws Throwable {
        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES) {
            @Override
            protected String getCommonSuperClass(String type1, String type2) {
                return "java/lang/Throwable";
            }
        };

        cw.visit(V1_8, ACC_PUBLIC, "Test", null, "java/lang/Object", null);
        MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
        mv.visitCode();
        mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
        mv.visitMethodInsn(INVOKESTATIC, "Test", "run", "()I", false);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(I)V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(-1, -1);
        mv.visitEnd();
        mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "run", "()I", null, null);

        Label div_start = new Label();
        Label div_end = new Label();
        Label div_handler = new Label();
        Label guard_start = div_handler;
        Label guard_end = new Label();
        Label guard_handler = new Label();

        mv.visitInsn(ICONST_0);
        mv.visitInsn(ICONST_0);
        mv.visitLabel(div_start);
        mv.visitInsn(IDIV);
        mv.visitLabel(div_end);
        mv.visitInsn(IRETURN);
        mv.visitLabel(div_handler);
        mv.visitInsn(NOP); // Non-empty exception range.
        mv.visitLabel(guard_end);
        mv.visitInsn(ICONST_0);
        mv.visitInsn(IRETURN);
        mv.visitLabel(guard_handler);
        mv.visitInsn(ICONST_1);
        mv.visitInsn(IRETURN);
        mv.visitTryCatchBlock(div_start, div_end, div_handler, "jdk/internal/agent/AgentConfigurationError");
        mv.visitTryCatchBlock(guard_start, guard_end, guard_handler, "java/lang/IllegalAccessError");
        mv.visitMaxs(-1, -1);
        mv.visitEnd();
        cw.visitEnd();
        byte[] bytes = cw.toByteArray();
        
        //var l = MethodHandles.lookup().defineHiddenClass(bytes, true);

        Field f = Unsafe.class.getDeclaredField("theUnsafe");
        f.setAccessible(true);
        Unsafe unsafe = (Unsafe) f.get(null);
        Class<?> anonClass = unsafe.defineAnonymousClass(Test8367002.class, bytes, null);
        MethodHandles.Lookup l = MethodHandles.lookup();

        MethodHandle mh = l.findStatic(anonClass, "run", MethodType.methodType(int.class));
        for (int i = 0; i < 16_000; i++) {
            invoke(mh);
        }
        System.out.println(invoke(mh));
    }

    private static int invoke(MethodHandle mh) throws Throwable {
        return (int) mh.invokeExact();
    }
}
