// File: /workspace/github_c_makefile_star2_O2/heltilda[P]cicada/ciclib.c
// Function: cclib_find
void cclib_find()
{
    window *stringToSearchWindow, *soughtStringWindow;
    linkedlist *stringToSearch, *soughtString = NULL;
    char *strPtrToSearch, *soughtStrPtr;
    ccInt numArgs, startingPosition, numMatches, charCounter, mode, matchStart, rtrn;
    
    numArgs = numBIF_args();
    if ((numArgs < 2) || (numArgs > 4))
        {  setError(wrong_argument_count_err, pcCodePtr-1);  return;  }
    
    
        // load arguments:  the string to search within, then substring to look for
    
    stringToSearchWindow = getBIFstringArg(1);
    if (errCode != passed)  return;
    soughtStringWindow = getBIFstringArg(2);
    if (errCode != passed)  return;   
    
    stringToSearch = &(stringToSearchWindow->variable_ptr->mem.data);
    soughtString = &(soughtStringWindow->variable_ptr->mem.data);
    
    
        // if argument 3 is present, load it -- it is the 'mode' (search forwards from beginning, backwards from end, or count)
    
    if (numArgs < 3)  {  mode = 1;  startingPosition = 1;  }
    else  {
        
        mode = (ccInt) getBIFnumArg(3, -1., 1.);
        
        
            // if argument 4 is present, load it -- that is the point in the big string to start looking/counting (defaults to beginning/end)
        
        if (numArgs < 4)  {
            if (mode == -1)  startingPosition = stringToSearchWindow->width;
            else  startingPosition = 1;         }
        else  {
            startingPosition = (ccInt) getBIFnumArg(4, 1., (ccFloat) (stringToSearchWindow->width-soughtStringWindow->width+1));
    }   }
    
    if (errCode != passed)  return;
    
    
        // prepare the return variable
    
    rtrn = defragmentLinkedList(stringToSearch);
    if (rtrn != passed)  {  setError(out_of_memory_err, pcCodePtr-1);  return;  }
    rtrn = defragmentLinkedList(soughtString);
    if (rtrn != passed)  {  setError(out_of_memory_err, pcCodePtr-1);  return;  }
    
    
        // get pointers to our two strings
    
    if (stringToSearchWindow->width == 0)  {  intRegister = 0;  return;  }
    if (soughtStringWindow->width == 0)  {
        if (mode == 0)  intRegister = 0;
        else  intRegister = startingPosition;
        return;     }
    soughtStrPtr = (char *) element(soughtString, soughtStringWindow->offset+1);
    strPtrToSearch = (char *) element(stringToSearch, stringToSearchWindow->offset+1);
    
    numMatches = 0;
    
    if (mode > -1)  {               // search forwards
    for (matchStart = startingPosition-1; matchStart <= stringToSearchWindow->width-soughtStringWindow->width; matchStart++)  {
        
        for (charCounter = 0; charCounter < soughtStringWindow->width; charCounter++)  {
        if (strPtrToSearch[matchStart+charCounter] != soughtStrPtr[charCounter])   {
            break;
        }}
        
        if (charCounter == soughtStringWindow->width)  {
            if (mode == 1)   {  intRegister = matchStart+1;  return;   }
            else  numMatches++;     // count
    }}  }
    
    else    {                       // search backwards
    for (matchStart = startingPosition-1; matchStart >= 0; matchStart--)  {
        
        for (charCounter = 0; charCounter < soughtStringWindow->width; charCounter++)  {
        if (strPtrToSearch[matchStart+charCounter] != soughtStrPtr[charCounter])   {
            break;
        }}
        
        if (charCounter == soughtStringWindow->width)  {  intRegister = matchStart+1;  return;   }
    }}
    
    if (mode == 0)  intRegister = numMatches;
    else  intRegister = 0;     // if we were searching, rather than counting, then if we made it this far we found nothing
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
callq 0x170b2
movl %eax, %r13d
leal -0x5(%r13), %eax
cmpl $-0x4, %eax
ja 0x16a41
leaq 0xdd40(%rip), %rax      # 0x24760
movq 0xb8(%rax), %rsi
addq $-0x4, %rsi
pushq $0x2d
popq %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xc7c2
pushq $0x1
popq %rbp
movl %ebp, %edi
callq 0x17280
leaq 0xdd0e(%rip), %r12      # 0x24760
cmpl $0x0, 0x18(%r12)
jne 0x16ae8
movq %rax, %r15
pushq $0x2
popq %rdi
callq 0x17280
cmpl $0x0, 0x18(%r12)
jne 0x16ae8
movq %rax, %rbx
movq 0x10(%r15), %r14
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
cmpl $0x3, %r13d
jae 0x16a8b
movl %ebp, %ecx
jmp 0x16ae0
pushq $0x3
popq %rdi
movsd 0x32a2(%rip), %xmm0     # 0x19d38
movsd 0x256a(%rip), %xmm1     # 0x19008
callq 0x1715f
cvttsd2si %xmm0, %ecx
cmpl $0x3, %r13d
jne 0x16abb
cmpl $-0x1, %ecx
jne 0x16ae0
movl 0x1c(%r15), %ebp
pushq $-0x1
popq %rcx
jmp 0x16ae0
movl 0x1c(%r15), %eax
subl 0x1c(%rbx), %eax
incl %eax
cvtsi2sd %eax, %xmm1
pushq $0x4
popq %rdi
movsd 0x2535(%rip), %xmm0     # 0x19008
movl %ecx, %ebp
callq 0x1715f
movl %ebp, %ecx
cvttsd2si %xmm0, %ebp
cmpl $0x0, 0x18(%r12)
je 0x16af7
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ecx, 0x4(%rsp)
addq $0x10, %r14
movq %r14, %rdi
callq 0x8b50
testl %eax, %eax
jne 0x16b25
movq %r14, 0x8(%rsp)
movq 0x10(%rsp), %r14
addq $0x10, %r14
movq %r14, %rdi
callq 0x8b50
testl %eax, %eax
je 0x16b38
movq 0xb8(%r12), %rsi
addq $-0x4, %rsi
pushq $0x1
jmp 0x16a2d
cmpl $0x0, 0x1c(%r15)
je 0x16bd4
cmpl $0x0, 0x1c(%rbx)
je 0x16bdf
movl 0x18(%rbx), %esi
incl %esi
movq %r14, %rdi
callq 0x9088
movq %rax, %r14
movl 0x18(%r15), %esi
incl %esi
movq 0x8(%rsp), %rdi
callq 0x9088
movl 0x4(%rsp), %r11d
testl %r11d, %r11d
js 0x16bf5
decl %ebp
movslq 0x1c(%r15), %rdx
movslq 0x1c(%rbx), %rsi
subq %rsi, %rdx
xorl %ecx, %ecx
testl %esi, %esi
movl $0x0, %r8d
cmovgl %esi, %r8d
movslq %ebp, %rdi
addq %rdi, %rax
cmpq %rdx, %rdi
jg 0x16c46
xorl %r9d, %r9d
cmpq %r9, %r8
je 0x16bb8
movb (%rax,%r9), %r10b
cmpb (%r14,%r9), %r10b
jne 0x16bbb
incq %r9
jmp 0x16ba4
movl %r8d, %r9d
cmpl %esi, %r9d
jne 0x16bcc
cmpl $0x1, %r11d
je 0x16c58
incl %ecx
incq %rdi
incq %rax
jmp 0x16b98
andl $0x0, 0x6c(%r12)
jmp 0x16ae8
movl 0x4(%rsp), %r13d
testl %r13d, %r13d
cmovel %r13d, %ebp
movl %ebp, 0x6c(%r12)
jmp 0x16ae8
movslq %ebp, %rsi
addq %rsi, %rax
decq %rax
xorl %ecx, %ecx
testq %rsi, %rsi
jle 0x16c46
movq %rsi, %rdx
decq %rsi
movl 0x1c(%rbx), %edi
testl %edi, %edi
movl $0x0, %r8d
cmovgl %edi, %r8d
xorl %r9d, %r9d
cmpq %r9, %r8
je 0x16c34
movb (%rax,%r9), %r10b
cmpb (%r14,%r9), %r10b
jne 0x16c31
incq %r9
jmp 0x16c1d
movl %r9d, %r8d
decq %rax
cmpl %edi, %r8d
jne 0x16c00
movl %edx, 0x6c(%r12)
jmp 0x16ae8
xorl %eax, %eax
testl %r11d, %r11d
cmovel %ecx, %eax
movl %eax, 0x6c(%r12)
jmp 0x16ae8
incl %edi
movl %edi, 0x6c(%r12)
jmp 0x16ae8