// File: /workspace/github_c_makefile_star2_O1/justy989[P]ce/ce_app.c
// Function: clang_format_string
ClangFormatResult_t _clang_format_string(char* clang_format_exe, char* string, int64_t string_len){
    ClangFormatResult_t result = {};
    CeSubprocess_t proc = {};
    bool use_shell = false;
    // TODO(jtardiff): move to config
    if(!ce_subprocess_open(&proc, clang_format_exe, CE_PROC_COMM_STDIN | CE_PROC_COMM_STDOUT, use_shell)){
        return result;
    }

    // Write our buffer to stdin, including the null terminator.
    int64_t bytes_to_write = string_len + 1;
    int64_t bytes_written = ce_subprocess_write_stdin(&proc, string, bytes_to_write);
    if(bytes_written != bytes_to_write){
        ce_log("Failed to write string to clang format process to format buffer\n");
        return result;
    }
    ce_subprocess_close_stdin(&proc);

    // Read bytes from stdout, clear our buffer and re-insert it.
    char* bytes = NULL;
    int64_t bytes_size = 0;
    int64_t total_bytes_read = 0;
    int64_t bytes_read = 0;
    do{
        int64_t new_bytes_size = bytes_size + BUFSIZ;
        bytes = realloc(bytes, new_bytes_size);
        if(bytes == NULL){
            ce_log("failed to realloc() %" PRId64 " bytes.\n", new_bytes_size);
            return result;
        }
        bytes_size = new_bytes_size;

        bytes_read = ce_subprocess_read_stdout(&proc, bytes + total_bytes_read, (BUFSIZ - 1));
        if(bytes_read < 0){
            ce_subprocess_close(&proc);
            return result;
        }
        bytes[total_bytes_read + bytes_read] = 0;
        total_bytes_read += bytes_read;
    }while(bytes_read > 0);
    int exit_code = ce_subprocess_close(&proc);

    if(total_bytes_read == 0){
        free(bytes);
        return result;
    }
    if(exit_code != 0){
        ce_log("clang format returned: %d\n", exit_code);
        return result;
    }

    // The last byte is null and the 2nd to last byte is a newline, just trim it.
    if(total_bytes_read > 2 &&
       bytes[total_bytes_read - 2] == CE_NEWLINE &&
       bytes[total_bytes_read - 1] == 0){
        bytes[total_bytes_read - 2] = 0;
    }
    result.bytes = bytes;
    result.success = true;
    return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rsi
xorps %xmm0, %xmm0
movq %rsp, %rdi
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, (%rdi)
xorl %ebx, %ebx
movl $0x3, %edx
xorl %ecx, %ecx
callq 0x1f5c2
testb %al, %al
je 0x14d8f
incq %r14
movq %rsp, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x1f8a6
cmpq %r14, %rax
jne 0x14d73
movq %rsp, %rdi
callq 0x1f752
xorl %ebx, %ebx
xorl %r15d, %r15d
xorl %ebp, %ebp
xorl %r12d, %r12d
leaq 0x2000(%r15), %r13
movq %rbx, %rdi
movq %r13, %rsi
callq 0xd520
movq %rax, %rbx
testq %rax, %rax
je 0x14d20
movq %rbx, %rsi
addq %rbp, %rsi
movl $0x1fff, %edx           # imm = 0x1FFF
movq %rsp, %r14
movq %r14, %rdi
callq 0x1f844
movq %rax, %r12
testq %rax, %rax
js 0x14d36
addq %r12, %rbp
movb $0x0, (%rbx,%rbp)
movb $0x1, %r14b
jmp 0x14d41
xorl %r14d, %r14d
leaq 0x1b092(%rip), %rdi     # 0x2fdbc
movq %r13, %rsi
xorl %eax, %eax
callq 0xd954
jmp 0x14d44
movq %r14, %rdi
callq 0x1f783
xorl %r14d, %r14d
movq %r13, %r15
testb %r14b, %r14b
je 0x14d8d
testq %r12, %r12
jg 0x14cdc
movq %rsp, %rdi
callq 0x1f783
testq %rbp, %rbp
je 0x14d85
testl %eax, %eax
je 0x14da3
leaq 0x1b076(%rip), %rdi     # 0x2fddc
xorl %ebx, %ebx
movl %eax, %esi
xorl %eax, %eax
callq 0xd954
jmp 0x14d8f
leaq 0x1b001(%rip), %rdi     # 0x2fd7b
xorl %ebx, %ebx
xorl %eax, %eax
callq 0xd954
jmp 0x14d8f
movq %rbx, %rdi
callq 0xd090
xorl %ebx, %ebx
xorl %eax, %eax
movq %rbx, %rdx
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, %al
cmpq $0x3, %rbp
jl 0x14d91
cmpb $0xa, -0x2(%rbx,%rbp)
jne 0x14d91
cmpb $0x0, -0x1(%rbx,%rbp)
jne 0x14d91
movb $0x0, -0x2(%rbx,%rbp)
jmp 0x14d91