// File: spatialfruitsalad[P]volume2position/./hk.hpp
// Function: HoshenKopelman::Process(tomogram3d<unsigned char>&, tomogram3d<int>&)
static void Process (tomogram3d <uint8_t>& matrix_in, tomogram3d<int>& out) {
        // code by fabian.
        std::cout << "start hoshen_kopelman ... " << std::flush;

        std::vector <int> rename;

        int nx = matrix_in.get_sx();
        int ny = matrix_in.get_sy();
        int nz = matrix_in.get_sz();

        // initialise
        rename.clear ();
        rename.push_back (0);
        out.clear();
        out.initialise (nx, ny, nz, 0);

        for (int k = 0; k != nz; ++k){
            for (int j = 0; j != ny; ++j)
                for (int i = 0; i != nx; ++i){
                    if (voxset (i,j,k,matrix_in)) {
                        int imin = std::max (i-1, 0);
                        int imax = std::min (i+1, int (nx-1));
                        int jmin = std::max (j-1, 0);
                        int jmax = std::min (j+1, int (ny-1));
                        int kmin = std::max (k-1, 0);
                        int kmax = std::min (k+1, int (nz-1));
                        int cluster_id = rename.size (); // possibly start a new cluster.

                        // this is all very suboptimal, but coding it the
                        // _right_ way generates a 100-line if-then-else block.
                        for (int ii = imin; ii <= imax; ++ii)
                            for (int jj = jmin; jj <= jmax; ++jj)
                                for (int kk = kmin; kk <= kmax; ++kk) {
                                    if (voxset (ii,jj,kk,matrix_in) && out.get(ii,jj,kk) != 0) {
                                        int oclid = lookup (out.get(ii,jj,kk),rename);
                                        if (cluster_id == (int)rename.size ())
                                            cluster_id = oclid;
                                        else if (cluster_id != oclid)
                                            cluster_id = merge_clusters (oclid, cluster_id, rename);
                                    }
                                }

                        // start a new cluster
                        if (cluster_id == (int)rename.size ())
                            rename.push_back (cluster_id);

                        out.set(i,j,k,cluster_id);
                    }
                }
        }

        int newindex = 0;
        for (int i = 0; i != (int)rename.size (); ++i)
            if (rename.at(i) >= 0)
                rename.at(i) = newindex++;

        // resolve all indirections in the indirection map
        for (int i = 0; i != (int)rename.size (); ++i)
            rename[i] = lookup (i, rename);

        // resolve the indirections on the facing planes
        // in the cluster map (other data is not needed)
    //    rename.insert (rename.begin (), -1);
        for (int i = 0; i != nx; ++i)
        for (int j = 0; j != ny; ++j)
        for (int k = 0; k != nz; ++k)
            out.set(i,j,k, rename[out.get(i,j,k)]);

        std::cout << " done" << std::endl;
    }

// Assembly:
subq $0x138, %rsp            # imm = 0x138
movq %rdi, 0x130(%rsp)
movq %rsi, 0x128(%rsp)
movq 0x1c4e2(%rip), %rdi     # 0x33fc0
leaq 0x10dff(%rip), %rsi     # 0x288e4
callq 0x3290
movq %rax, %rdi
movq 0x1c4d4(%rip), %rsi     # 0x33fc8
callq 0x32c0
leaq 0x110(%rsp), %rdi
movq %rdi, 0x68(%rsp)
callq 0x125d0
movq 0x130(%rsp), %rdi
callq 0x125e0
movl %eax, 0x10c(%rsp)
movq 0x130(%rsp), %rdi
callq 0x125f0
movl %eax, 0x108(%rsp)
movq 0x130(%rsp), %rdi
callq 0x12600
movq 0x68(%rsp), %rdi
movl %eax, 0x104(%rsp)
callq 0x12610
movq 0x68(%rsp), %rdi
movl $0x0, 0x100(%rsp)
leaq 0x100(%rsp), %rsi
callq 0x18310
jmp 0x17b70
movq 0x128(%rsp), %rdi
callq 0x1c170
jmp 0x17b7f
movq 0x128(%rsp), %rdi
movslq 0x10c(%rsp), %rsi
movslq 0x108(%rsp), %rdx
movslq 0x104(%rsp), %rcx
xorl %r8d, %r8d
callq 0x12630
jmp 0x17ba9
movl $0x0, 0xf0(%rsp)
movl 0xf0(%rsp), %eax
cmpl 0x104(%rsp), %eax
je 0x18073
movl $0x0, 0xec(%rsp)
movl 0xec(%rsp), %eax
cmpl 0x108(%rsp), %eax
je 0x1805b
movl $0x0, 0xe8(%rsp)
movl 0xe8(%rsp), %eax
cmpl 0x10c(%rsp), %eax
je 0x18043
movl 0xe8(%rsp), %edi
movl 0xec(%rsp), %esi
movl 0xf0(%rsp), %edx
movq 0x130(%rsp), %rcx
callq 0x1c1b0
movb %al, 0x67(%rsp)
jmp 0x17c2e
movb 0x67(%rsp), %al
testb $0x1, %al
jne 0x17c3b
jmp 0x1802b
movl 0xe8(%rsp), %eax
subl $0x1, %eax
movl %eax, 0xe0(%rsp)
movl $0x0, 0xdc(%rsp)
leaq 0xe0(%rsp), %rdi
leaq 0xdc(%rsp), %rsi
callq 0x127b0
movl (%rax), %eax
movl %eax, 0xe4(%rsp)
movl 0xe8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd4(%rsp)
movl 0x10c(%rsp), %eax
subl $0x1, %eax
movl %eax, 0xd0(%rsp)
leaq 0xd4(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x127f0
movl (%rax), %eax
movl %eax, 0xd8(%rsp)
movl 0xec(%rsp), %eax
subl $0x1, %eax
movl %eax, 0xc8(%rsp)
movl $0x0, 0xc4(%rsp)
leaq 0xc8(%rsp), %rdi
leaq 0xc4(%rsp), %rsi
callq 0x127b0
movl (%rax), %eax
movl %eax, 0xcc(%rsp)
movl 0xec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xbc(%rsp)
movl 0x108(%rsp), %eax
subl $0x1, %eax
movl %eax, 0xb8(%rsp)
leaq 0xbc(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x127f0
movl (%rax), %eax
movl %eax, 0xc0(%rsp)
movl 0xf0(%rsp), %eax
subl $0x1, %eax
movl %eax, 0xb0(%rsp)
movl $0x0, 0xac(%rsp)
leaq 0xb0(%rsp), %rdi
leaq 0xac(%rsp), %rsi
callq 0x127b0
movl (%rax), %eax
movl %eax, 0xb4(%rsp)
movl 0xf0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
movl 0x104(%rsp), %eax
subl $0x1, %eax
movl %eax, 0xa0(%rsp)
leaq 0xa4(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x127f0
movl (%rax), %eax
movl %eax, 0xa8(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x12830
movl %eax, 0x9c(%rsp)
movl 0xe4(%rsp), %eax
movl %eax, 0x98(%rsp)
movl 0x98(%rsp), %eax
cmpl 0xd8(%rsp), %eax
jg 0x17fbe
movl 0xcc(%rsp), %eax
movl %eax, 0x94(%rsp)
movl 0x94(%rsp), %eax
cmpl 0xc0(%rsp), %eax
jg 0x17fa6
movl 0xb4(%rsp), %eax
movl %eax, 0x90(%rsp)
movl 0x90(%rsp), %eax
cmpl 0xa8(%rsp), %eax
jg 0x17f8e
movl 0x98(%rsp), %edi
movl 0x94(%rsp), %esi
movl 0x90(%rsp), %edx
movq 0x130(%rsp), %rcx
callq 0x1c1b0
movb %al, 0x66(%rsp)
jmp 0x17e4b
movb 0x66(%rsp), %al
testb $0x1, %al
jne 0x17e58
jmp 0x17f76
movq 0x128(%rsp), %rdi
movslq 0x98(%rsp), %rsi
movslq 0x94(%rsp), %rdx
movslq 0x90(%rsp), %rcx
callq 0xefc0
movl %eax, 0x60(%rsp)
jmp 0x17e83
movl 0x60(%rsp), %eax
cmpl $0x0, %eax
je 0x17f76
movq 0x128(%rsp), %rdi
movslq 0x98(%rsp), %rsi
movslq 0x94(%rsp), %rdx
movslq 0x90(%rsp), %rcx
callq 0xefc0
movl %eax, 0x5c(%rsp)
jmp 0x17ebb
movl 0x5c(%rsp), %edi
leaq 0x110(%rsp), %rsi
callq 0x1c1f0
movl %eax, 0x58(%rsp)
jmp 0x17ed2
movl 0x58(%rsp), %eax
movl %eax, 0x8c(%rsp)
movl 0x9c(%rsp), %eax
movl %eax, 0x54(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x12830
movq %rax, %rcx
movl 0x54(%rsp), %eax
cmpl %ecx, %eax
jne 0x17f36
movl 0x8c(%rsp), %eax
movl %eax, 0x9c(%rsp)
jmp 0x17f74
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf8(%rsp)
movl %eax, 0xf4(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x12fb0
jmp 0x182c0
movl 0x9c(%rsp), %eax
cmpl 0x8c(%rsp), %eax
je 0x17f72
movl 0x8c(%rsp), %edi
movl 0x9c(%rsp), %esi
leaq 0x110(%rsp), %rdx
callq 0x1c270
movl %eax, 0x50(%rsp)
jmp 0x17f67
movl 0x50(%rsp), %eax
movl %eax, 0x9c(%rsp)
jmp 0x17f74
jmp 0x17f76
jmp 0x17f78
movl 0x90(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x90(%rsp)
jmp 0x17e0f
jmp 0x17f90
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x17ded
jmp 0x17fa8
movl 0x98(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x98(%rsp)
jmp 0x17dcb
movl 0x9c(%rsp), %eax
movl %eax, 0x4c(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x12830
movq %rax, %rcx
movl 0x4c(%rsp), %eax
cmpl %ecx, %eax
jne 0x17ffa
leaq 0x110(%rsp), %rdi
leaq 0x9c(%rsp), %rsi
callq 0x12910
jmp 0x17ff8
jmp 0x17ffa
movq 0x128(%rsp), %rdi
movslq 0xe8(%rsp), %rsi
movslq 0xec(%rsp), %rdx
movslq 0xf0(%rsp), %rcx
movl 0x9c(%rsp), %r8d
callq 0x12980
jmp 0x18029
jmp 0x1802b
jmp 0x1802d
movl 0xe8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe8(%rsp)
jmp 0x17bf2
jmp 0x18045
movl 0xec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xec(%rsp)
jmp 0x17bd3
jmp 0x1805d
movl 0xf0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf0(%rsp)
jmp 0x17bb4
movl $0x0, 0x88(%rsp)
movl $0x0, 0x84(%rsp)
movl 0x84(%rsp), %eax
movl %eax, 0x48(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x12830
movq %rax, %rcx
movl 0x48(%rsp), %eax
cmpl %ecx, %eax
je 0x18125
movslq 0x84(%rsp), %rsi
leaq 0x110(%rsp), %rdi
callq 0x12f20
movq %rax, 0x40(%rsp)
jmp 0x180c8
movq 0x40(%rsp), %rax
cmpl $0x0, (%rax)
jl 0x1810d
movl 0x88(%rsp), %eax
movl %eax, 0x34(%rsp)
incl %eax
movl %eax, 0x88(%rsp)
movslq 0x84(%rsp), %rsi
leaq 0x110(%rsp), %rdi
callq 0x12f20
movq %rax, 0x38(%rsp)
jmp 0x18102
movq 0x38(%rsp), %rax
movl 0x34(%rsp), %ecx
movl %ecx, (%rax)
jmp 0x1810f
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x84(%rsp)
jmp 0x18089
movl $0x0, 0x80(%rsp)
movl 0x80(%rsp), %eax
movl %eax, 0x30(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x12830
movq %rax, %rcx
movl 0x30(%rsp), %eax
cmpl %ecx, %eax
je 0x1819b
movl 0x80(%rsp), %edi
leaq 0x110(%rsp), %rsi
callq 0x1c1f0
movl %eax, 0x2c(%rsp)
jmp 0x1816d
movslq 0x80(%rsp), %rsi
leaq 0x110(%rsp), %rdi
callq 0x12d40
movl 0x2c(%rsp), %ecx
movl %ecx, (%rax)
movl 0x80(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x80(%rsp)
jmp 0x18130
movl $0x0, 0x7c(%rsp)
movl 0x7c(%rsp), %eax
cmpl 0x10c(%rsp), %eax
je 0x18280
movl $0x0, 0x78(%rsp)
movl 0x78(%rsp), %eax
cmpl 0x108(%rsp), %eax
je 0x1826e
movl $0x0, 0x74(%rsp)
movl 0x74(%rsp), %eax
cmpl 0x104(%rsp), %eax
je 0x1825c
movq 0x128(%rsp), %rdi
movq %rdi, 0x8(%rsp)
movslq 0x7c(%rsp), %rsi
movq %rsi, 0x10(%rsp)
movslq 0x78(%rsp), %rdx
movq %rdx, 0x18(%rsp)
movslq 0x74(%rsp), %rcx
movq %rcx, 0x20(%rsp)
callq 0xefc0
movl %eax, 0x28(%rsp)
jmp 0x18218
movl 0x28(%rsp), %eax
movslq %eax, %rsi
leaq 0x110(%rsp), %rdi
callq 0x12d40
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rcx
movl (%rax), %r8d
callq 0x12980
jmp 0x1824a
jmp 0x1824c
movl 0x74(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x74(%rsp)
jmp 0x181d5
jmp 0x1825e
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0x181bc
jmp 0x18270
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x181a3
movq 0x1bd39(%rip), %rdi     # 0x33fc0
leaq 0x105bc(%rip), %rsi     # 0x2884a
callq 0x3290
movq %rax, (%rsp)
jmp 0x18299
movq (%rsp), %rdi
movq 0x1bcfc(%rip), %rsi     # 0x33fa0
callq 0x32c0
jmp 0x182ab
leaq 0x110(%rsp), %rdi
callq 0x12fb0
addq $0x138, %rsp            # imm = 0x138
retq
movq 0xf8(%rsp), %rdi
callq 0x34a0
nopl (%rax)