// File: /workspace/github_cpp_makefile_star2_O2/intheswim[P]FractalTubes/infra.cpp
// Function: selectNextImage()
void selectNextImage()
{
    std::vector<int> indexes;
    
    for (int i=0; i < PRESET_COUNT; i++)
    {
        bool selected = AppParams::getImageSelected (i);
        
        if (selected)
        {
            indexes.push_back (i);
        }
    }

    if (indexes.size() == 0)
    {
        current_set_index = 0;
        return;
    }
    
    if (AppParams::getShuffle() )
    {
        if (indexes.size() > 1)
        {
            size_t temp = std::find(indexes.begin(), indexes.end(), current_set_index) - indexes.begin();

            if (temp < indexes.size() ) // current index is among selected
            {
                int increase = rand() % (indexes.size() - 1);
                
                temp = (temp + increase + 1) % indexes.size();
                
                current_set_index = indexes [ temp] ;
            }
            else // current index is NOT among selected. Select any of the selected randomly.
            {
                temp = ( rand() % indexes.size() );
                
                current_set_index = indexes [temp] ;
            }
        }
        else
        {
            current_set_index = indexes [0] ;
        }
    }
    else
    {
        current_set_index++;
        
        if (current_set_index >= PRESET_COUNT)
        {
            current_set_index = 0;
        }
        
        while (false == AppParams::getImageSelected (current_set_index))
        {
            current_set_index++;
            if (current_set_index >= PRESET_COUNT)
            {
                current_set_index = 0;
            }
        }
    }

}

// Assembly:
pushq %r14
pushq %rbx
subq $0x28, %rsp
leaq 0x10(%rsp), %rbx
andq $0x0, 0x10(%rbx)
xorps %xmm0, %xmm0
movaps %xmm0, (%rbx)
xorl %edi, %edi
leaq 0xc(%rsp), %r14
movl %edi, 0xc(%rsp)
cmpl $0x5, %edi
jge 0x5105
callq 0x7b58
testb %al, %al
je 0x50fd
movq %rbx, %rdi
movq %r14, %rsi
callq 0x529c
movl 0xc(%rsp), %edi
incl %edi
jmp 0x50e0
movq 0x18(%rsp), %rax
cmpq 0x10(%rsp), %rax
je 0x5188
callq 0x7b06
testb %al, %al
je 0x518c
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %rsi, %rax
subq %rdi, %rax
cmpq $0x5, %rax
jb 0x51cf
leaq 0x83d1(%rip), %rdx      # 0xd50c
callq 0x5c43
movq %rax, %rbx
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rbx
subq %rax, %rcx
cmpq %rcx, %rbx
jae 0x51ae
sarq $0x2, %rbx
callq 0x30e0
cltq
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rsi
subq %rcx, %rsi
sarq $0x2, %rsi
leaq -0x1(%rsi), %rdi
xorl %edx, %edx
divq %rdi
movslq %edx, %rax
addq %rbx, %rax
incq %rax
jmp 0x51c6
xorl %eax, %eax
jmp 0x51d1
xorl %ebx, %ebx
movl 0x8378(%rip), %eax      # 0xd50c
leal 0x1(%rax), %edi
cmpl $0x4, %eax
cmovgel %ebx, %edi
movl %edi, 0x8369(%rip)      # 0xd50c
callq 0x7b58
testb %al, %al
je 0x518e
jmp 0x51d7
callq 0x30e0
cltq
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rsi
subq %rcx, %rsi
sarq $0x2, %rsi
xorl %edx, %edx
divq %rsi
leaq (%rcx,%rdx,4), %rdi
movl (%rdi), %eax
movl %eax, 0x8335(%rip)      # 0xd50c
leaq 0x10(%rsp), %rdi
callq 0x5ace
addq $0x28, %rsp
popq %rbx
popq %r14
retq
jmp 0x51ef
jmp 0x51ef
jmp 0x51ef
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x5ace
movq %rbx, %rdi
callq 0x35d0