// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movq 0xc052(%rip), %r15      # 0x1cfc8
leaq 0x69ea(%rip), %rsi      # 0x17967
movl $0x1a, %edx
movq %r15, %rdi
callq 0x3300
movq %r15, %rdi
callq 0x31e0
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
je 0x10fb4
movq %rax, 0x18(%rsp)
movq 0x18(%r14), %rax
movq %rax, 0x30(%rsp)
movq 0x20(%r14), %rax
movq %rax, 0x38(%rsp)
movq 0x28(%r14), %r13
movl $0x0, 0xc(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x10fef
movl $0x0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x10ffe
leaq 0x10(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0xef54
movl 0x30(%rsp), %eax
movq %rax, 0x40(%rsp)
movl 0x38(%rsp), %ebp
movl %r13d, %eax
movq %rax, 0x50(%rsp)
movq (%rbx), %rax
cmpq %rax, 0x8(%rbx)
je 0x11020
movq %rax, 0x8(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rbx)
movq $0x0, 0x28(%rbx)
movslq 0x40(%rsp), %rsi
movslq %ebp, %rdx
movslq 0x50(%rsp), %rcx
movq %rbx, %rdi
xorl %r8d, %r8d
callq 0xe35c
testl %r13d, %r13d
je 0x11307
movq 0x30(%rsp), %rax
decl %eax
movl %eax, 0x64(%rsp)
movq 0x38(%rsp), %rax
decl %eax
movl %eax, 0x60(%rsp)
leal -0x1(%r13), %eax
movl %eax, 0x5c(%rsp)
xorl %ecx, %ecx
movq %r13, 0xa0(%rsp)
movq %rbp, 0x98(%rsp)
cmpl $0x2, %ecx
movl $0x1, %eax
cmovgel %ecx, %eax
movq %rax, 0x90(%rsp)
cmpl $0x0, 0x38(%rsp)
je 0x112f9
addq $-0x2, 0x90(%rsp)
cmpl $0x2, %ecx
movl $0x1, %eax
cmovgel %ecx, %eax
decl %eax
movl %eax, 0x74(%rsp)
leal 0x1(%rcx), %edx
movl 0x5c(%rsp), %eax
cmpl %edx, %eax
cmovll %eax, %edx
movl %edx, 0x70(%rsp)
movslq %edx, %r12
xorl %edx, %edx
movq %rcx, 0x78(%rsp)
cmpl $0x2, %edx
movl $0x1, %eax
cmovgel %edx, %eax
movq %rax, 0x88(%rsp)
cmpl $0x0, 0x30(%rsp)
je 0x112ed
decq 0x88(%rsp)
cmpl $0x2, %edx
movl $0x1, %eax
cmovgel %edx, %eax
decl %eax
movl %eax, 0x6c(%rsp)
leal 0x1(%rdx), %esi
movl 0x60(%rsp), %eax
cmpl %esi, %eax
cmovll %eax, %esi
movl %esi, 0x68(%rsp)
movslq %esi, %rax
movq %rax, 0xb0(%rsp)
xorl %esi, %esi
movq %rdx, 0x80(%rsp)
cmpl $0x2, %esi
movl $0x1, %r15d
cmovgel %esi, %r15d
movq %r14, %rdi
movq %rsi, 0x48(%rsp)
callq 0xeaa4
testb %al, %al
je 0x112cd
movq 0x48(%rsp), %rax
cmpl $0x2, %eax
movl $0x1, %ecx
cmovgel %eax, %ecx
decl %ecx
incl %eax
movl 0x64(%rsp), %edx
cmpl %eax, %edx
cmovll %edx, %eax
movq 0x18(%rsp), %rdx
subq 0x10(%rsp), %rdx
shrq $0x2, %rdx
movl %edx, 0xc(%rsp)
cmpl %eax, %ecx
jg 0x1125f
decq %r15
cltq
movq %rax, 0xa8(%rsp)
movq 0x88(%rsp), %rbp
movl 0x68(%rsp), %eax
cmpl %eax, 0x6c(%rsp)
jg 0x1124a
movq 0x90(%rsp), %r13
movl 0x70(%rsp), %eax
cmpl %eax, 0x74(%rsp)
jg 0x11235
incq %r13
movq %r14, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %r13, %rcx
callq 0xeaa4
testb %al, %al
je 0x11230
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %r13, %rcx
callq 0xd03a
testl %eax, %eax
je 0x11230
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %r13, %rcx
callq 0xd03a
movl %eax, %edi
leaq 0x10(%rsp), %rsi
callq 0x12e84
movl 0xc(%rsp), %esi
movq 0x18(%rsp), %rcx
subq 0x10(%rsp), %rcx
shrq $0x2, %rcx
cmpl %ecx, %esi
je 0x1122c
cmpl %eax, %esi
je 0x11230
movl %eax, %edi
leaq 0x10(%rsp), %rdx
callq 0x12ede
movl %eax, 0xc(%rsp)
cmpq %r12, %r13
jl 0x111bc
leaq 0x1(%rbp), %rax
cmpq 0xb0(%rsp), %rbp
movq %rax, %rbp
jl 0x111aa
leaq 0x1(%r15), %rax
cmpq 0xa8(%rsp), %r15
movq %rax, %r15
jl 0x11194
movl 0xc(%rsp), %eax
movq 0x18(%rsp), %rsi
movq %rsi, %rcx
subq 0x10(%rsp), %rcx
shrq $0x2, %rcx
cmpl %ecx, %eax
movq 0xa0(%rsp), %r13
movq 0x98(%rsp), %rbp
movq 0x48(%rsp), %r15
jne 0x112b0
cmpq 0x20(%rsp), %rsi
je 0x112a1
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x112b0
leaq 0x10(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0xedfa
movl 0xc(%rsp), %r8d
movq %rbx, %rdi
movq %r15, %rsi
movq 0x80(%rsp), %rdx
movq 0x78(%rsp), %rcx
callq 0xe52c
movq 0x48(%rsp), %rsi
incq %rsi
cmpq 0x40(%rsp), %rsi
movq 0x78(%rsp), %rcx
movq 0x80(%rsp), %rdx
jne 0x1112e
incq %rdx
cmpq %rbp, %rdx
jne 0x110d2
incq %rcx
cmpq 0x50(%rsp), %rcx
jne 0x11080
movabsq $0x3fffffffc, %rax      # imm = 0x3FFFFFFFC
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rdx
subq %rcx, %rdx
testq %rax, %rdx
je 0x1134c
sarq $0x2, %rdx
movl %edx, %edi
xorl %esi, %esi
xorl %r8d, %r8d
cmpq %rsi, %rdx
je 0x11459
cmpl $0x0, (%rcx,%rsi,4)
js 0x11344
movl %r8d, (%rcx,%rsi,4)
incl %r8d
incq %rsi
cmpq %rsi, %rdi
jne 0x1132e
movq 0x18(%rsp), %rcx
subq 0x10(%rsp), %rcx
testq %rax, %rcx
je 0x1138d
xorl %r14d, %r14d
leaq 0x10(%rsp), %r15
movl %r14d, %edi
movq %r15, %rsi
callq 0x12e84
movq 0x10(%rsp), %rcx
movl %eax, (%rcx,%r14,4)
incq %r14
movq 0x18(%rsp), %rax
subq %rcx, %rax
shrq $0x2, %rax
movl %eax, %eax
cmpq %rax, %r14
jne 0x11363
cmpl $0x0, 0x30(%rsp)
je 0x113f2
xorl %r14d, %r14d
cmpl $0x0, 0x38(%rsp)
je 0x113e8
xorl %r15d, %r15d
testl %r13d, %r13d
je 0x113e0
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0xd03a
cltq
movq 0x10(%rsp), %rcx
movl (%rcx,%rax,4), %r8d
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0xe52c
incq %r12
cmpq %r12, 0x50(%rsp)
jne 0x113a9
incq %r15
cmpq %rbp, %r15
jne 0x113a1
incq %r14
cmpq 0x40(%rsp), %r14
jne 0x11397
movq 0xbbcf(%rip), %rbx      # 0x1cfc8
leaq 0x64cd(%rip), %rsi      # 0x178cd
movl $0x5, %edx
movq %rbx, %rdi
callq 0x3300
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x32b0
movsbl %al, %esi
movq 0xbb9d(%rip), %rdi      # 0x1cfc8
callq 0x3040
movq %rax, %rdi
callq 0x31e0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x11447
callq 0x3290
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6309(%rip), %rdi      # 0x17769
xorl %eax, %eax
callq 0x33a0
jmp 0x11479
jmp 0x11479
jmp 0x11479
jmp 0x11479
jmp 0x11479
jmp 0x11479
jmp 0x11479
jmp 0x11479
jmp 0x11479
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x1148b
callq 0x3290
movq %rbx, %rdi
callq 0x3440
nop