// File: 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 %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
leaq -0x18(%rbp), %rdi
callq 0x6660
movl $0x0, -0x1c(%rbp)
cmpl $0x5, -0x1c(%rbp)
jge 0x6304
movl -0x1c(%rbp), %edi
callq 0xba90
movb %al, -0x5d(%rbp)
jmp 0x62be
movb -0x5d(%rbp), %al
andb $0x1, %al
movb %al, -0x1d(%rbp)
testb $0x1, -0x1d(%rbp)
je 0x62f7
leaq -0x18(%rbp), %rdi
leaq -0x1c(%rbp), %rsi
callq 0x6680
jmp 0x62db
jmp 0x62f7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x6820
jmp 0x651e
jmp 0x62f9
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x62ab
leaq -0x18(%rbp), %rdi
callq 0x66f0
cmpq $0x0, %rax
jne 0x6329
movl $0x0, 0xd26f(%rip)      # 0x1358c
movl $0x1, -0x30(%rbp)
jmp 0x650c
callq 0xb9f0
movb %al, -0x5e(%rbp)
jmp 0x6333
movb -0x5e(%rbp), %al
testb $0x1, %al
jne 0x633f
jmp 0x6497
leaq -0x18(%rbp), %rdi
callq 0x66f0
cmpq $0x1, %rax
jbe 0x6480
leaq -0x18(%rbp), %rdi
movq %rdi, -0x70(%rbp)
callq 0x67a0
movq -0x70(%rbp), %rdi
movq %rax, -0x48(%rbp)
callq 0x67d0
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
leaq 0xd20d(%rip), %rdx      # 0x1358c
callq 0x6750
movq %rax, -0x68(%rbp)
jmp 0x638a
movq -0x68(%rbp), %rax
movq %rax, -0x40(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x67a0
movq %rax, -0x58(%rbp)
leaq -0x40(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x6710
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x78(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x66f0
movq %rax, %rcx
movq -0x78(%rbp), %rax
cmpq %rcx, %rax
jae 0x643f
callq 0x30f0
cltq
movq %rax, -0x88(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x66f0
movq %rax, %rcx
movq -0x88(%rbp), %rax
subq $0x1, %rcx
xorl %edx, %edx
divq %rcx
movl %edx, %eax
movl %eax, -0x5c(%rbp)
movq -0x38(%rbp), %rax
movslq -0x5c(%rbp), %rcx
addq %rcx, %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x66f0
movq %rax, %rcx
movq -0x80(%rbp), %rax
xorl %edx, %edx
divq %rcx
movq %rdx, -0x38(%rbp)
movq -0x38(%rbp), %rsi
leaq -0x18(%rbp), %rdi
callq 0x6800
movl (%rax), %eax
movl %eax, 0xd14f(%rip)      # 0x1358c
jmp 0x647e
callq 0x30f0
cltq
movq %rax, -0x90(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x66f0
movq %rax, %rcx
movq -0x90(%rbp), %rax
xorl %edx, %edx
divq %rcx
movq %rdx, -0x38(%rbp)
movq -0x38(%rbp), %rsi
leaq -0x18(%rbp), %rdi
callq 0x6800
movl (%rax), %eax
movl %eax, 0xd10e(%rip)      # 0x1358c
jmp 0x6495
leaq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x6800
movl (%rax), %eax
movl %eax, 0xd0f7(%rip)      # 0x1358c
jmp 0x6505
movl 0xd0ef(%rip), %eax      # 0x1358c
addl $0x1, %eax
movl %eax, 0xd0e6(%rip)      # 0x1358c
cmpl $0x5, 0xd0df(%rip)      # 0x1358c
jl 0x64b9
movl $0x0, 0xd0d3(%rip)      # 0x1358c
jmp 0x64bb
movl 0xd0cb(%rip), %edi      # 0x1358c
callq 0xba90
movb %al, -0x91(%rbp)
jmp 0x64ce
movb -0x91(%rbp), %al
andb $0x1, %al
movzbl %al, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x6503
movl 0xd0a7(%rip), %eax      # 0x1358c
addl $0x1, %eax
movl %eax, 0xd09e(%rip)      # 0x1358c
cmpl $0x5, 0xd097(%rip)      # 0x1358c
jl 0x6501
movl $0x0, 0xd08b(%rip)      # 0x1358c
jmp 0x64bb
jmp 0x6505
movl $0x0, -0x30(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x6820
addq $0xa0, %rsp
popq %rbp
retq
movq -0x28(%rbp), %rdi
callq 0x36b0
nopw (%rax,%rax)