// File: /workspace/github_c_makefile_star2_O3/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 %r15
pushq %r14
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 0x1f552
testb %al, %al
je 0x14a78
incq %r14
movq %rsp, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x1f83a
cmpq %r14, %rax
jne 0x14a3d
movq %rsp, %r15
movq %r15, %rdi
callq 0x1f6da
movl $0x2000, %r14d          # imm = 0x2000
xorl %ebx, %ebx
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r14, %rsi
callq 0xd520
testq %rax, %rax
je 0x14a4f
movq %rax, %rbx
leaq (%rax,%r12), %rsi
movl $0x1fff, %edx           # imm = 0x1FFF
movq %r15, %rdi
callq 0x1f7d8
testq %rax, %rax
js 0x14a64
addq %rax, %r12
movb $0x0, (%rbx,%r12)
addq $0x2000, %r14           # imm = 0x2000
testq %rax, %rax
jne 0x149db
movq %rsp, %rdi
callq 0x1f70f
testq %r12, %r12
je 0x14a6e
testl %eax, %eax
je 0x14a89
leaq 0x1a3ac(%rip), %rdi     # 0x2eddc
xorl %ebx, %ebx
movl %eax, %esi
xorl %eax, %eax
callq 0xd954
jmp 0x14a78
leaq 0x1a337(%rip), %rdi     # 0x2ed7b
xorl %ebx, %ebx
xorl %eax, %eax
callq 0xd954
jmp 0x14a78
leaq 0x1a366(%rip), %rdi     # 0x2edbc
xorl %ebx, %ebx
movq %r14, %rsi
xorl %eax, %eax
callq 0xd954
jmp 0x14a78
movq %rsp, %rdi
callq 0x1f70f
jmp 0x14a76
movq %rbx, %rdi
callq 0xd090
xorl %ebx, %ebx
xorl %eax, %eax
movq %rbx, %rdx
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movb $0x1, %al
cmpq $0x3, %r12
jl 0x14a7a
cmpb $0xa, -0x2(%rbx,%r12)
jne 0x14a7a
cmpb $0x0, -0x1(%rbx,%r12)
jne 0x14a7a
movb $0x0, -0x2(%rbx,%r12)
jmp 0x14a7a