global a[10], b[10]

// PR24811: Be sure to test non-constant iteration limit -- set to 0/1 in begin.
global lim0 = 1
global lim1 = 0

probe begin {
    printf("BEGIN\n")

    a["p"] = -1
    a["q"] = 0
    a["r"] = 1

    b[-1] = "p"
    b[0] = "q"
    b[1] = "r"

    lim0 = 0
    lim1 = 1
    exit()
}

global flag = 1

// XXX Split into separate probes due to stack size constraint.
global end_probes = 0 // TODO: Remove this workaround for PR24812.

probe end(1) {
    printf("first end probe\n")

    found = 0
    foreach (ks+ in a limit lim1) {
        found++
        if (a[ks] != -1)
            flag = 0
    }

    foreach (k in b+ limit lim1) {
        found++
        if (k != -1)
            flag = 0
    }

    if (found != 2)
        flag = 0

    if (end_probes < 3)
        end_probes++ // { end_probes++; next; } // TODO: Investigate using next here.
    else if (flag)
        printf("END PASS\n")
    else
        printf("END FAIL\n")
}

probe end(2) {
    printf("second end probe\n")

    foreach (ks- in a limit -10)
        flag = 0

    foreach (ks- in a limit lim0)
        flag = 0
     
    foreach (ks1 in a limit lim0)
        foreach (ks2 in a)
            flag = 0

    foreach (ks1 in a)
        foreach (ks2 in a limit lim0)
            flag = 0

    x = 0
    foreach (ks in a)
        x += a[ks]
    flag = flag && x == 0

    if (end_probes < 3)
        end_probes++ // { end_probes++; next; } // TODO: Investigate using next here.
    else if (flag)
        printf("END PASS\n")
    else
        printf("END FAIL\n")
}

probe end(3) {
    printf("third end probe\n")

    x = -1
    foreach (ks+ in a) {
        flag = flag && x++ == a[ks]
    }
    flag = flag && x == 2

    x = 1
    foreach (ks- in a) {
        flag = flag && x-- == a[ks]
    }
    flag = flag && x == -2

    x = -1
    y = 1
    foreach (ks1+ in a) {
        foreach (k2 in b-) {
            flag = flag && x == a[ks1] && y-- == k2
        }
        x++
        y = 1
    }
        
    if (end_probes < 3)
        end_probes++ // { end_probes++; next; } // TODO: Investigate using next here.
    else if (flag)
        printf("END PASS\n")
    else
        printf("END FAIL\n")
}

probe end(4) {
    printf("fourth end probe\n")

    x = -1
    y = 1
    foreach (ks1+ in a) {
        foreach (ks2- in a) {
            flag = flag && x == a[ks1] && y-- == a[ks2]
        }
        x++
        y = 1
    }

    // Also test outer loop with no sorting, inner loop with sorting.
    x = -1
    y = 1
    foreach (ks1 in a) {
        foreach (ks2- in a) {
            flag = flag && y-- == a[ks2]
        }
        x++
        y = 1
    }

    if (end_probes < 3)
        end_probes++ // { end_probes++; next; } // TODO: Investigate using next here.
    else if (flag)
        printf("END PASS\n")
    else
        printf("END FAIL\n")
}
