summaryrefslogtreecommitdiff
path: root/targets/agent_sort.c
blob: 7e29a9d0c374a2667714321da9aa644d35f37aa6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// A program that knows nothing of the tracer: a global array sorted by
// swaps. Hooked at `swap` and watched at `numbers` from outside, each moment
// of its recording is the array one swap later.
#include <stdio.h>

int numbers[16] = {9, 3, 14, 0, 7, 12, 5, 1, 15, 8, 2, 11, 6, 13, 4, 10};

__attribute__((noinline)) void swap(int *a, int *b) {
    int t = *a;
    *a = *b;
    *b = t;
}

int main(void) {
    int n = sizeof numbers / sizeof *numbers;
    for (int i = 0; i < n; i++)
        for (int j = 0; j + 1 < n - i; j++)
            if (numbers[j] > numbers[j + 1]) swap(&numbers[j], &numbers[j + 1]);
    for (int i = 0; i < n; i++) printf("%d ", numbers[i]);
    printf("\n");
    return 0;
}