// File: /workspace/github_c_makefile_star2_O1/heltilda[P]cicada/ciclib.c
// Function: cclib_save
void cclib_save()
{
    FILE *fileToWrite;
    window *fileNameWindow, *stringWindow;
    linkedlist *fileNameLL, *stringLL = NULL;
    ccInt bytesWritten, rtrn;
    char *fileString, closeFileRetrn;
    
    if (numBIF_args() != 2)  {  setError(wrong_argument_count_err, pcCodePtr-1);  return;  }
    
    
        // get arguments:  file name, string to write
    
    fileNameWindow = getBIFstringArg(1);
    if (errCode != passed)  return;
    stringWindow = getBIFstringArg(2);
    if (errCode != passed)  return;   
    
    fileNameLL = &(fileNameWindow->variable_ptr->mem.data);
    stringLL = &(stringWindow->variable_ptr->mem.data);
    
    
        // defragment the string; copy the file name into a separate buffer
    
    rtrn = defragmentLinkedList(stringLL);
    if (rtrn != passed)  {  setError(out_of_memory_err, pcCodePtr-1);  return;  }
    
    fileString = (char *) malloc(fileNameWindow->width+1);
    if (fileString == NULL)  {  setError(out_of_memory_err, pcCodePtr-1);  return;  }
    getElements(fileNameLL, fileNameWindow->offset+1, fileNameWindow->offset+fileNameWindow->width, fileString);
    fileString[fileNameWindow->width] = 0;
    
    
        // try to open the file; throw error if unsuccessful
    
    fileToWrite = fopen(fileString, "wb");
    if (fileToWrite == NULL)  {  setError(IO_error, pcCodePtr-1);  free((void *) fileString);  return;  }
    
    
        // write the string to the file
    
    if (stringLL->elementNum > 0)  {
        bytesWritten = (ccInt) fwrite(element(stringLL, stringWindow->offset+1), 1, stringWindow->width, fileToWrite);    }
    else  bytesWritten = 0;
    if (bytesWritten != stringWindow->width)  {  setError(IO_error, pcCodePtr-1);  }
    
    
        // close the file
    
    closeFileRetrn = fclose(fileToWrite);
    if ((errCode == passed) && (closeFileRetrn != 0))  {  setError(IO_error, pcCodePtr-1);  }
    
    free((void *) fileString);    // clean up
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
callq 0x1926a
cmpl $0x2, %eax
jne 0x17a7a
movl $0x1, %edi
callq 0x19452
leaq 0xdd0e(%rip), %rbx      # 0x25760
cmpl $0x0, 0x18(%rbx)
jne 0x17a6b
movq %rax, %r12
movl $0x2, %edi
callq 0x19452
cmpl $0x0, 0x18(%rbx)
je 0x17aa4
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xdcdf(%rip), %rax      # 0x25760
movq 0xb8(%rax), %rsi
addq $-0x4, %rsi
movl $0x2d, %edi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xd1a9
movq %rax, %r14
movq 0x10(%r12), %r13
movq 0x10(%rax), %r15
addq $0x10, %r15
movq %r15, %rdi
callq 0x8bb7
testl %eax, %eax
je 0x17ad2
movq 0xb8(%rbx), %rsi
addq $-0x4, %rsi
movl $0x1, %edi
jmp 0x17a91
movq %r13, (%rsp)
movslq 0x1c(%r12), %rbp
leaq 0x1(%rbp), %rdi
callq 0x81d0
testq %rax, %rax
je 0x17ac0
movq %rax, %r13
movq (%rsp), %rdi
addq $0x10, %rdi
movl 0x18(%r12), %eax
leal 0x1(%rax), %esi
addl %eax, %ebp
movl %ebp, %edx
movq %r13, %rcx
callq 0x8fee
movslq 0x1c(%r12), %rax
movb $0x0, (%r13,%rax)
leaq 0x42fd(%rip), %rsi      # 0x1be17
movq %r13, %rdi
callq 0x81b0
testq %rax, %rax
je 0x17b86
movq %rax, %r12
cmpl $0x0, (%r15)
jle 0x17b54
movl 0x18(%r14), %esi
incl %esi
movq %r15, %rdi
callq 0x911c
movslq 0x1c(%r14), %rdx
movl $0x1, %esi
movq %rax, %rdi
movq %r12, %rcx
callq 0x8270
jmp 0x17b56
xorl %eax, %eax
cmpl 0x1c(%r14), %eax
je 0x17b71
movq 0xb8(%rbx), %rsi
addq $-0x4, %rsi
movl $0x31, %edi
callq 0xd1a9
movq %r12, %rdi
callq 0x8170
cmpl $0x0, 0x18(%rbx)
jne 0x17b9b
movzbl %al, %eax
testl %eax, %eax
je 0x17b9b
movq 0xb8(%rbx), %rsi
addq $-0x4, %rsi
movl $0x31, %edi
callq 0xd1a9
movq %r13, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x81c0