// File: /workspace/github_c_makefile_star2_O2/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 0x170b2
cmpl $0x2, %eax
jne 0x15b64
pushq $0x1
popq %rdi
callq 0x17280
leaq 0xec22(%rip), %rbx      # 0x24760
cmpl $0x0, 0x18(%rbx)
jne 0x15b55
movq %rax, %r12
pushq $0x2
popq %rdi
callq 0x17280
cmpl $0x0, 0x18(%rbx)
je 0x15b8c
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xebf5(%rip), %rax      # 0x24760
movq 0xb8(%rax), %rsi
addq $-0x4, %rsi
pushq $0x2d
popq %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xc7c2
movq %rax, %r14
movq 0x10(%r12), %r13
movq 0x10(%rax), %r15
addq $0x10, %r15
movq %r15, %rdi
callq 0x8b50
testl %eax, %eax
je 0x15bb7
movq 0xb8(%rbx), %rsi
addq $-0x4, %rsi
pushq $0x1
jmp 0x15b78
movq %r13, (%rsp)
movslq 0x1c(%r12), %rbp
leaq 0x1(%rbp), %rdi
callq 0x81d0
testq %rax, %rax
je 0x15ba8
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 0x8f8b
movslq 0x1c(%r12), %rax
movb $0x0, (%r13,%rax)
leaq 0x41b8(%rip), %rsi      # 0x19db7
movq %r13, %rdi
callq 0x81b0
testq %rax, %rax
je 0x15c67
movq %rax, %r12
cmpl $0x0, (%r15)
jle 0x15c37
movl 0x18(%r14), %esi
incl %esi
movq %r15, %rdi
callq 0x9088
movslq 0x1c(%r14), %rdx
pushq $0x1
popq %rsi
movq %rax, %rdi
movq %r12, %rcx
callq 0x8270
jmp 0x15c39
xorl %eax, %eax
cmpl 0x1c(%r14), %eax
je 0x15c52
movq 0xb8(%rbx), %rsi
addq $-0x4, %rsi
pushq $0x31
popq %rdi
callq 0xc7c2
movq %r12, %rdi
callq 0x8170
cmpl $0x0, 0x18(%rbx)
jne 0x15c7a
movzbl %al, %eax
testl %eax, %eax
je 0x15c7a
movq 0xb8(%rbx), %rsi
addq $-0x4, %rsi
pushq $0x31
popq %rdi
callq 0xc7c2
movq %r13, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x81c0