// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq $0x0, -0x20(%rbp)
callq 0x1fa40
movl %eax, -0x34(%rbp)
cmpl $0x2, -0x34(%rbp)
jl 0x1f084
cmpl $0x4, -0x34(%rbp)
jle 0x1f0a5
leaq 0xd6c5(%rip), %rax      # 0x2c750
movq 0xb8(%rax), %rsi
addq $-0x4, %rsi
movl $0x2d, %edi
callq 0xf740
jmp 0x1f3de
movl $0x1, %edi
callq 0x1fd70
movq %rax, -0x8(%rbp)
leaq 0xd696(%rip), %rax      # 0x2c750
cmpl $0x0, 0x18(%rax)
je 0x1f0c5
jmp 0x1f3de
movl $0x2, %edi
callq 0x1fd70
movq %rax, -0x10(%rbp)
leaq 0xd676(%rip), %rax      # 0x2c750
cmpl $0x0, 0x18(%rax)
je 0x1f0e5
jmp 0x1f3de
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
addq $0x10, %rax
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
addq $0x10, %rax
movq %rax, -0x20(%rbp)
cmpl $0x3, -0x34(%rbp)
jge 0x1f11b
movl $0x1, -0x44(%rbp)
movl $0x1, -0x38(%rbp)
jmp 0x1f18d
movl $0x3, %edi
movsd 0x2ef0(%rip), %xmm0     # 0x22018
movsd 0x2ed8(%rip), %xmm1     # 0x22008
callq 0x1fb80
cvttsd2si %xmm0, %eax
movl %eax, -0x44(%rbp)
cmpl $0x4, -0x34(%rbp)
jge 0x1f15d
cmpl $-0x1, -0x44(%rbp)
jne 0x1f154
movq -0x8(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, -0x38(%rbp)
jmp 0x1f15b
movl $0x1, -0x38(%rbp)
jmp 0x1f18b
movq -0x8(%rbp), %rax
movl 0x1c(%rax), %eax
movq -0x10(%rbp), %rcx
subl 0x1c(%rcx), %eax
addl $0x1, %eax
cvtsi2sd %eax, %xmm1
movl $0x4, %edi
movsd 0x2e89(%rip), %xmm0     # 0x22008
callq 0x1fb80
cvttsd2si %xmm0, %eax
movl %eax, -0x38(%rbp)
jmp 0x1f18d
leaq 0xd5bc(%rip), %rax      # 0x2c750
cmpl $0x0, 0x18(%rax)
je 0x1f19f
jmp 0x1f3de
movq -0x18(%rbp), %rdi
callq 0x8f70
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
je 0x1f1d2
leaq 0xd598(%rip), %rax      # 0x2c750
movq 0xb8(%rax), %rsi
addq $-0x4, %rsi
movl $0x1, %edi
callq 0xf740
jmp 0x1f3de
movq -0x20(%rbp), %rdi
callq 0x8f70
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
je 0x1f205
leaq 0xd565(%rip), %rax      # 0x2c750
movq 0xb8(%rax), %rsi
addq $-0x4, %rsi
movl $0x1, %edi
callq 0xf740
jmp 0x1f3de
movq -0x8(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
jne 0x1f222
leaq 0xd53a(%rip), %rax      # 0x2c750
movl $0x0, 0x6c(%rax)
jmp 0x1f3de
movq -0x10(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
jne 0x1f254
cmpl $0x0, -0x44(%rbp)
jne 0x1f242
leaq 0xd517(%rip), %rax      # 0x2c750
movl $0x0, 0x6c(%rax)
jmp 0x1f24f
movl -0x38(%rbp), %ecx
leaq 0xd504(%rip), %rax      # 0x2c750
movl %ecx, 0x6c(%rax)
jmp 0x1f3de
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x18(%rax), %esi
addl $0x1, %esi
callq 0x9700
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rdi
movq -0x8(%rbp), %rax
movl 0x18(%rax), %esi
addl $0x1, %esi
callq 0x9700
movq %rax, -0x28(%rbp)
movl $0x0, -0x3c(%rbp)
cmpl $-0x1, -0x44(%rbp)
jle 0x1f33c
movl -0x38(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
movq -0x8(%rbp), %rcx
movl 0x1c(%rcx), %ecx
movq -0x10(%rbp), %rdx
subl 0x1c(%rdx), %ecx
cmpl %ecx, %eax
jg 0x1f33a
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x1c(%rcx), %eax
jge 0x1f2f8
movq -0x28(%rbp), %rax
movl -0x48(%rbp), %ecx
addl -0x40(%rbp), %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
movq -0x30(%rbp), %rcx
movslq -0x40(%rbp), %rdx
movsbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
je 0x1f2eb
jmp 0x1f2f8
jmp 0x1f2ed
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x1f2bc
movl -0x40(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x1c(%rcx), %eax
jne 0x1f32a
cmpl $0x1, -0x44(%rbp)
jne 0x1f31f
movl -0x48(%rbp), %ecx
addl $0x1, %ecx
leaq 0xd439(%rip), %rax      # 0x2c750
movl %ecx, 0x6c(%rax)
jmp 0x1f3de
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1f32a
jmp 0x1f32c
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x1f29c
jmp 0x1f3bb
movl -0x38(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x48(%rbp)
jl 0x1f3b9
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x1c(%rcx), %eax
jge 0x1f38e
movq -0x28(%rbp), %rax
movl -0x48(%rbp), %ecx
addl -0x40(%rbp), %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
movq -0x30(%rbp), %rcx
movslq -0x40(%rbp), %rdx
movsbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
je 0x1f381
jmp 0x1f38e
jmp 0x1f383
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x1f352
movl -0x40(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x1c(%rcx), %eax
jne 0x1f3ac
movl -0x48(%rbp), %ecx
addl $0x1, %ecx
leaq 0xd3a9(%rip), %rax      # 0x2c750
movl %ecx, 0x6c(%rax)
jmp 0x1f3de
jmp 0x1f3ae
movl -0x48(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x1f345
jmp 0x1f3bb
cmpl $0x0, -0x44(%rbp)
jne 0x1f3d0
movl -0x3c(%rbp), %ecx
leaq 0xd385(%rip), %rax      # 0x2c750
movl %ecx, 0x6c(%rax)
jmp 0x1f3de
leaq 0xd379(%rip), %rax      # 0x2c750
movl $0x0, 0x6c(%rax)
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)