// File: /workspace/github_c_makefile_star2_O2/justy989[P]ce/ce.c
// Function: ce_buffer_load_file
bool ce_buffer_load_file(CeBuffer_t* buffer, const char* filename){
     struct stat statbuf;
     if(stat(filename, &statbuf) != 0) return false;
     bool is_dir = false;

#if defined(PLATFORM_WINDOWS)
     is_dir = (statbuf.st_mode & _S_IFDIR);
#else
     is_dir = S_ISDIR(statbuf.st_mode);
#endif
     if(is_dir){
          errno = EPERM;
          return false;
     }

#if defined(PLATFORM_WINDOWS)
     bool writeable = (_access(filename, 2) == 0);
     bool readable = (_access(filename, 4) == 0);

     if (!writeable && readable) {
          buffer->status = CE_BUFFER_STATUS_READONLY;
     }else if(writeable && readable){
          buffer->status = CE_BUFFER_STATUS_NONE;
     }else{
          ce_log("File %s not read-able or write-able %d", filename, errno);
          return false;
     }
#else
     if(access(filename, W_OK) != 0){
          buffer->status = CE_BUFFER_STATUS_READONLY;
     }else{
          buffer->status = CE_BUFFER_STATUS_NONE;
     }
#endif

     buffer->file_modified_time = statbuf.st_mtime;

     // read the entire file
     size_t content_size;
     char* contents = NULL;

     FILE* file = fopen(filename, "rb");
     if(!file){
          ce_log("%s() fopen('%s', 'rb') failed: '%s'\n", __FUNCTION__, filename, strerror(errno));
          return false;
     }

     fseek(file, 0, SEEK_END);
     content_size = ftell(file);
     fseek(file, 0, SEEK_SET);

     contents = (char*)malloc(content_size + 1);
     fread(contents, content_size, 1, file);
     contents[content_size] = 0;

     if(strlen(contents) != content_size){
          ce_log("%s() file '%s' has early null terminator\n", __FUNCTION__, filename);
          errno = ENOPROTOOPT;
          return false;
     }

     // strip the ending '\n'
     if(contents[content_size - 1] == CE_NEWLINE) contents[content_size - 1] = 0;

     if(!ce_buffer_load_string(buffer, contents, filename)){
          return false;
     }

     fclose(file);

     free(contents);
     ce_log("%s() loaded '%s'\n", __FUNCTION__, filename);
     return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0xd3f0
testl %eax, %eax
jne 0xdf08
movl $0xf000, %eax           # imm = 0xF000
andl 0x20(%rsp), %eax
cmpl $0x4000, %eax           # imm = 0x4000
jne 0xdf1e
callq 0xd0a0
movl $0x1, (%rax)
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x2
popq %rsi
movq %rbx, %rdi
callq 0xd5a0
xorl %ecx, %ecx
testl %eax, %eax
setne %cl
addl %ecx, %ecx
movl %ecx, 0x18(%r14)
movq 0x60(%rsp), %rax
movq %rax, 0x68(%r14)
leaq 0x1c603(%rip), %rsi     # 0x2a549
movq %rbx, %rdi
callq 0xd5b0
testq %rax, %rax
je 0xe005
movq %rax, %r15
pushq $0x2
popq %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0xd510
movq %r15, %rdi
callq 0xd3d0
movq %rax, %r13
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xd510
leaq 0x1(%r13), %rdi
callq 0xd4c0
movq %rax, %r12
pushq $0x1
popq %rdx
movq %rax, %rdi
movq %r13, %rsi
movq %r15, %rcx
callq 0xd160
movb $0x0, (%r12,%r13)
movq %r12, %rdi
callq 0xd210
cmpq %r13, %rax
jne 0xe033
cmpb $0xa, -0x1(%r12,%r13)
jne 0xdfbf
movb $0x0, -0x1(%r12,%r13)
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0xe05d
testb %al, %al
je 0xdf08
movq %r15, %rdi
callq 0xd1f0
movq %r12, %rdi
callq 0xd090
leaq 0x1c5c3(%rip), %rdi     # 0x2a5af
leaq 0x1c57e(%rip), %rsi     # 0x2a571
movq %rbx, %rdx
xorl %eax, %eax
callq 0xd8e5
movb $0x1, %bpl
jmp 0xdf0a
callq 0xd0a0
movl (%rax), %edi
callq 0xd650
leaq 0x1c534(%rip), %rdi     # 0x2a54c
leaq 0x1c552(%rip), %rsi     # 0x2a571
xorl %ebp, %ebp
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xd8e5
jmp 0xdf0a
leaq 0x1c54b(%rip), %rdi     # 0x2a585
leaq 0x1c530(%rip), %rsi     # 0x2a571
xorl %ebp, %ebp
movq %rbx, %rdx
xorl %eax, %eax
callq 0xd8e5
callq 0xd0a0
movl $0x5c, (%rax)
jmp 0xdf0a