struct Record {
    char kind;
    int value;
    Record *next;
};

struct Reader {
    virtual int read() = 0;
};

struct ValueReader : Reader {
    int value;
    constexpr ValueReader(int initial) : value(initial) {}
    __declspec(noinline) int read() override { return value; }
};

extern "C" {
int values[] = {10, 20, 30, 40};
Record tail = {'B', 200, nullptr};
Record head = {'A', 100, &tail};
char narrow_text[] = "ABC";
wchar_t wide_text[] = L"ABC";
int copy_buffer[4] = {};
char text_copy[8] = {};
ValueReader reader_instance{123};
Reader *reader_pointer = &reader_instance;
unsigned int measured_length;
const char *found_text;
int reader_result;

__declspec(noinline) int read_value(const int *p) { return *p; }
__declspec(noinline) void write_value(int *p, int value) { *p = value; }
__declspec(noinline) int read_element(const int *p, unsigned int index) { return p[index]; }
__declspec(noinline) int read_next_value(const Record *p) { return p->next->value; }
__declspec(noinline) int read_record_value(const Record *p, unsigned int index) { return p[index].value; }
__declspec(noinline) int add_values(int a, int b) { return a + b; }
__declspec(noinline) int invoke(int (*callback)(int), int value) { return callback(value); }
__declspec(noinline) int invoke_reader(Reader *reader) { return reader->read(); }
__declspec(noinline) int identity(int value) { return value; }

__declspec(noinline) unsigned long long zero_extend(unsigned int value) { return value; }
__declspec(noinline) int signed_byte(const signed char *p) { return *p; }
__declspec(noinline) unsigned int unsigned_byte(const unsigned char *p) { return *p; }
__declspec(noinline) long long widen_value(int value) { return value; }
__declspec(noinline) int *element_address(int *p, unsigned int index) { return p + index; }
__declspec(noinline) unsigned int scaled_bytes(unsigned int count) { return count * 4u; }
__declspec(noinline) int divide_values(int dividend, int divisor) { return dividend / divisor; }
__declspec(noinline) unsigned int mask_flags(unsigned int value, unsigned int mask) { return (value & mask) | 0x10u; }
__declspec(noinline) int arithmetic_half(int value) { return value >> 1; }
__declspec(noinline) int positive_or_zero(int value) { return value > 0 ? value : 0; }
__declspec(noinline) int signed_less(int a, int b) { return a < b; }
__declspec(noinline) int unsigned_less(unsigned int a, unsigned int b) { return a < b; }
__declspec(noinline) int guarded_read(const int *p) { return p ? *p : -1; }
__declspec(noinline) int __stdcall add_stdcall(int a, int b) { return a + b; }

__declspec(noinline) int call_pair()
{
    int total = add_values(10, 20);
    return add_stdcall(total, 3);
}

__declspec(noinline) int frame_probe(int first, int second, int third, int fourth, int fifth)
{
    volatile int local[3] = {first + second, third + fourth, fifth};
    int total = identity(first + fifth);
    local[1] += total;
    __debugbreak();
    return local[0] + local[1] + local[2];
}

__declspec(noinline) void copy_ints(int *destination, const int *source, unsigned int count)
{
    for (unsigned int index = 0; index < count; ++index)
        destination[index] = source[index];
}

__declspec(noinline) void copy_text(char *destination, const char *source)
{
    while ((*destination++ = *source++) != '\0') {}
}

__declspec(noinline) unsigned int text_length(const char *text)
{
    unsigned int length = 0;
    while (text[length] != '\0')
        ++length;
    return length;
}

__declspec(noinline) const char *find_char(const char *text, char target)
{
    for (;; ++text) {
        if (*text == target)
            return text;
        if (*text == '\0')
            return nullptr;
    }
}

volatile int result;

__declspec(noinline) void inspect_results()
{
    result = result;
}

}

int main(int argc, char **)
{
    if (argc > 1)
        return frame_probe(11, 22, 33, 44, 55);

    result = read_value(values);
    write_value(values, 11);
    result = read_element(values, 2);
    result = read_next_value(&head);
    result = read_record_value(&head, 0);
    result = add_values(10, 20);
    result = invoke(identity, 5);
    copy_ints(copy_buffer, values, 4);
    copy_ints(copy_buffer, values, 3);
    copy_text(text_copy, narrow_text);
    measured_length = text_length(narrow_text);
    found_text = find_char(narrow_text, 'B');
    reader_result = invoke_reader(reader_pointer);
    inspect_results();
    return 0;
}
