1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// -- Some adjustments for didactic reasons ----------
typedef unsigned long   uint64_t;   // ok on theseus

int putchar ( int character );
// ---------------------------------------------------

void
bubblesort(char *A, uint64_t n)
{
    uint64_t swapped;
    uint64_t i;
    char tmp;

bubblesort_do:
    swapped = 0;
    i = 1;
bubblesort_for:
    if (i >= n)
        goto bubblesort_endfor;
    if (A[i-1] <= A[i])
        goto bubblesort_endif;
    tmp = A[i];
    A[i] = A[i-1];
    A[i-1] = tmp;
    swapped = 1;
bubblesort_endif:
    i = i + 1;
    goto bubblesort_for;
    n = n - 1;
bubblesort_endfor:
    if (swapped)
        goto bubblesort_do;
}

void
puts(char *str)
{
    while (*str) {
        putchar(*str++);
    }
    putchar('\n');
}

uint64_t
strlen(char *str)
{
    char *ch = str;
    while (*ch) {
        ++ch;
    }
    return ch - str;
}

char msg[] = "hello, world!";

int
main()
{
    puts(msg);
    bubblesort(msg, strlen(msg));
    puts(msg);
}