// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;

// GitHub 20040: operand ordering bug with GT_INDEX_ADDR
// Requires minopts/tier0 to repro

namespace GitHub_20040
{
    class Program
    {
        static int Main(string[] args)
        {
            var array = new byte[] {0x00, 0x01};
            var reader = new BinaryTokenStreamReader(array);

            var val = reader.ReadByte();

            if (val == 0x01)
            {
                Console.WriteLine("Pass");                
                return 100;
            }
            else
            {
                Console.WriteLine($"Fail: val=0x{val:x2}, expected 0x01");
                return 0;
            }
        }
    }

    public class BinaryTokenStreamReader
    {
        private readonly byte[] currentBuffer;

        public BinaryTokenStreamReader(byte[] input)
        {
            this.currentBuffer = input;
        }

        byte[] CheckLength(out int offset)
        {
            // In the original code, this logic is more complicated.
            // It's simplified here to demonstrate the bug.
            offset = 1;
            return currentBuffer;
        }

        public byte ReadByte()
        {
            int offset;
            var buff = CheckLength(out offset);
            return buff[offset];
        }
    }
}

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Runtime.CompilerServices;

// GitHub 20040: operand ordering bug with GT_INDEX_ADDR
// Requires minopts/tier0 to repro

namespace GitHub_20040
{
    class Program
    {
        static int Main(string[] args)
        {
            var array = new byte[] {0x00, 0x01};
            var reader = new BinaryTokenStreamReader(array);

            var val = reader.ReadByte();

            if (val == 0x01)
            {
                Console.WriteLine("Pass");                
                return 100;
            }
            else
            {
                Console.WriteLine($"Fail: val=0x{val:x2}, expected 0x01");
                return 0;
            }
        }
    }

    public class BinaryTokenStreamReader
    {
        private readonly byte[] currentBuffer;

        public BinaryTokenStreamReader(byte[] input)
        {
            this.currentBuffer = input;
        }

        byte[] CheckLength(out int offset)
        {
            // In the original code, this logic is more complicated.
            // It's simplified here to demonstrate the bug.
            offset = 1;
            return currentBuffer;
        }

        [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
        public byte ReadByte()
        {
            int offset;
            var buff = CheckLength(out offset);
            return buff[offset];
        }
    }
}

Fix test for GT_INDEX_ADDR to forcibly compile with minopts
Fix test for GT_INDEX_ADDR to forcibly compile with minopts

Update test introduced in #20047 so that the key method `ReadBytes` is compiled
with minopts by default.
