// File: /workspace/github_cpp_makefile_star2_O3/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 $0xc8, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movq 0xc11a(%rip), %r15      # 0x1cfc8
leaq 0x6aa2(%rip), %rsi      # 0x17957
movl $0x1a, %edx
movq %r15, %rdi
callq 0x32f0
movq %r15, %rdi
callq 0x31e0
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movq 0x18(%r14), %r12
movq 0x20(%r14), %r13
movq 0x28(%r14), %rbp
leaq 0xc(%rsp), %rdx
movl $0x0, (%rdx)
xorl %esi, %esi
callq 0xee26
movl %r12d, %eax
movq %rax, 0x28(%rsp)
movl %r13d, %eax
movq %rax, 0x30(%rsp)
movl %ebp, %eax
movq %rax, 0x40(%rsp)
movq (%rbx), %rax
cmpq %rax, 0x8(%rbx)
je 0x10f1f
movq %rax, 0x8(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rbx)
movq $0x0, 0x28(%rbx)
movslq 0x28(%rsp), %rsi
movslq 0x30(%rsp), %rdx
movslq 0x40(%rsp), %rcx
movq %rbx, %rdi
xorl %r8d, %r8d
callq 0xe244
testl %ebp, %ebp
je 0x11228
leal -0x1(%r12), %eax
movl %eax, 0x50(%rsp)
leal -0x1(%r13), %eax
movl %eax, 0x4c(%rsp)
leal -0x1(%rbp), %eax
movl %eax, 0x48(%rsp)
xorl %ecx, %ecx
movq %r13, 0x90(%rsp)
movq %rbp, 0x88(%rsp)
movq %r12, 0x60(%rsp)
cmpl $0x2, %ecx
movl $0x1, %edx
cmovgel %ecx, %edx
testl %r13d, %r13d
je 0x11217
leaq 0x1(%rcx), %rsi
movl 0x48(%rsp), %eax
cmpl %esi, %eax
movq %rsi, 0x78(%rsp)
cmovll %eax, %esi
leal -0x1(%rdx), %eax
movl %eax, 0x58(%rsp)
movl %esi, 0x5c(%rsp)
movslq %esi, %r12
addq $-0x2, %rdx
xorl %eax, %eax
movq %rcx, 0x68(%rsp)
movq %rdx, 0xb8(%rsp)
cmpl $0x2, %eax
movl $0x1, %esi
cmovgel %eax, %esi
cmpl $0x0, 0x60(%rsp)
je 0x111f7
leaq -0x1(%rsi), %rcx
movq %rcx, 0xa0(%rsp)
decl %esi
movq %rsi, 0xa8(%rsp)
movq %rax, 0x70(%rsp)
leaq 0x1(%rax), %rcx
movl 0x4c(%rsp), %eax
cmpl %ecx, %eax
movq %rcx, 0x80(%rsp)
cmovll %eax, %ecx
movl %ecx, 0x54(%rsp)
movslq %ecx, %rax
movq %rax, 0xc0(%rsp)
xorl %esi, %esi
cmpl $0x2, %esi
movl $0x1, %r15d
cmovgel %esi, %r15d
movq %r14, %rdi
movq %rsi, 0x38(%rsp)
movq 0x70(%rsp), %rdx
movq 0x68(%rsp), %rcx
callq 0xe968
testb %al, %al
je 0x1119c
movq 0x38(%rsp), %rax
leaq 0x1(%rax), %rcx
movl 0x50(%rsp), %eax
cmpl %ecx, %eax
movq %rcx, 0x98(%rsp)
cmovll %eax, %ecx
movq 0x18(%rsp), %rsi
movq %rsi, %rax
subq 0x10(%rsp), %rax
leal -0x1(%r15), %edx
shrq $0x2, %rax
movl %eax, 0xc(%rsp)
cmpl %ecx, %edx
jg 0x11188
decq %r15
movl %ecx, %eax
movq %rax, 0xb0(%rsp)
movq 0xa0(%rsp), %rbp
movl 0x54(%rsp), %eax
cmpl %eax, 0xa8(%rsp)
jg 0x1114a
movq 0xb8(%rsp), %r13
movl 0x58(%rsp), %eax
cmpl 0x5c(%rsp), %eax
jg 0x11135
incq %r13
movq %r14, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %r13, %rcx
callq 0xe968
testb %al, %al
je 0x11130
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %r13, %rcx
callq 0xcef2
testl %eax, %eax
je 0x11130
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %r13, %rcx
callq 0xcef2
movl %eax, %edi
leaq 0x10(%rsp), %rsi
callq 0x12d26
movl 0xc(%rsp), %esi
movq 0x18(%rsp), %rcx
subq 0x10(%rsp), %rcx
shrq $0x2, %rcx
cmpl %ecx, %esi
je 0x1112c
cmpl %eax, %esi
je 0x11130
movl %eax, %edi
leaq 0x10(%rsp), %rdx
callq 0x12d7e
movl %eax, 0xc(%rsp)
cmpq %r12, %r13
jl 0x110bc
leaq 0x1(%rbp), %rax
cmpq 0xc0(%rsp), %rbp
movq %rax, %rbp
jl 0x110aa
leaq 0x1(%r15), %rax
cmpq 0xb0(%rsp), %r15
movq %rax, %r15
jl 0x11091
movl 0xc(%rsp), %eax
movq 0x18(%rsp), %rsi
movq %rsi, %rcx
subq 0x10(%rsp), %rcx
shrq $0x2, %rcx
cmpl %ecx, %eax
movq 0x90(%rsp), %r13
movq 0x88(%rsp), %rbp
jne 0x111b8
cmpq 0x20(%rsp), %rsi
je 0x111a9
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x111b8
movq 0x38(%rsp), %rsi
incq %rsi
movq %rsi, %rax
jmp 0x111df
leaq 0x10(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0xeccc
movl 0xc(%rsp), %r8d
movq %rbx, %rdi
movq 0x38(%rsp), %rsi
movq 0x70(%rsp), %rdx
movq 0x68(%rsp), %rcx
callq 0xe3de
movq 0x98(%rsp), %rax
movq %rax, %rsi
cmpq 0x28(%rsp), %rax
jne 0x1101d
movq 0x80(%rsp), %rsi
movq %rsi, %rax
jmp 0x111fd
incq %rax
movq %rax, %rsi
cmpq 0x30(%rsp), %rsi
jne 0x10fc6
movq 0x78(%rsp), %rax
movq %rax, %rcx
movq 0x60(%rsp), %r12
jmp 0x1121d
incq %rcx
movq %rcx, %rax
cmpq 0x40(%rsp), %rax
jne 0x10f7f
movabsq $0x3fffffffc, %rax      # imm = 0x3FFFFFFFC
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rcx
subq %rsi, %rcx
testq %rax, %rcx
je 0x11272
movq %rcx, %rdx
sarq $0x2, %rdx
movl %edx, %edi
xorl %r8d, %r8d
xorl %r9d, %r9d
cmpq %r8, %rdx
je 0x11372
cmpl $0x0, (%rsi,%r8,4)
js 0x1126a
movl %r9d, (%rsi,%r8,4)
incl %r9d
incq %r8
cmpq %r8, %rdi
jne 0x11253
testq %rax, %rcx
je 0x112a9
xorl %r14d, %r14d
leaq 0x10(%rsp), %r15
movl %r14d, %edi
movq %r15, %rsi
callq 0x12d26
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 0x1127f
testl %r12d, %r12d
je 0x1130b
xorl %r14d, %r14d
testl %r13d, %r13d
je 0x11301
xorl %r15d, %r15d
testl %ebp, %ebp
je 0x112f7
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0xcef2
cltq
movq 0x10(%rsp), %rcx
movl (%rcx,%rax,4), %r8d
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0xe3de
incq %r12
cmpq %r12, 0x40(%rsp)
jne 0x112c0
incq %r15
cmpq 0x30(%rsp), %r15
jne 0x112b9
incq %r14
cmpq 0x28(%rsp), %r14
jne 0x112b1
movq 0xbcb6(%rip), %rbx      # 0x1cfc8
leaq 0x65a4(%rip), %rsi      # 0x178bd
movl $0x5, %edx
movq %rbx, %rdi
callq 0x32f0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x32a0
movsbl %al, %esi
movq 0xbc84(%rip), %rdi      # 0x1cfc8
callq 0x3040
movq %rax, %rdi
callq 0x31e0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x11360
callq 0x3280
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x63e0(%rip), %rdi      # 0x17759
movq %rdx, %rsi
xorl %eax, %eax
callq 0x3390
jmp 0x11395
jmp 0x11395
jmp 0x11395
jmp 0x11395
jmp 0x11395
jmp 0x11395
jmp 0x11395
jmp 0x11395
jmp 0x11395
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x113a7
callq 0x3280
movq %rbx, %rdi
callq 0x3430
nop