// File: /workspace/github_c_makefile_star2_O2/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)
pushq $0x3
popq %rdx
xorl %ebx, %ebx
xorl %ecx, %ecx
callq 0x1d791
testb %al, %al
je 0x1411e
incq %r14
movq %rsp, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x1da39
cmpq %r14, %rax
jne 0x140e3
movq %rsp, %r15
movq %r15, %rdi
callq 0x1d8fe
movl $0x2000, %r14d          # imm = 0x2000
xorl %ebx, %ebx
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r14, %rsi
callq 0xd520
testq %rax, %rax
je 0x140f5
movq %rax, %rbx
leaq (%rax,%r12), %rsi
movl $0x1fff, %edx           # imm = 0x1FFF
movq %r15, %rdi
callq 0x1d9db
testq %rax, %rax
js 0x1410a
addq %rax, %r12
movb $0x0, (%rbx,%r12)
addq $0x2000, %r14           # imm = 0x2000
testq %rax, %rax
jne 0x14081
movq %rsp, %rdi
callq 0x1d934
testq %r12, %r12
je 0x14114
testl %eax, %eax
je 0x1412f
leaq 0x17d06(%rip), %rdi     # 0x2bddc
xorl %ebx, %ebx
movl %eax, %esi
xorl %eax, %eax
callq 0xd8e5
jmp 0x1411e
leaq 0x17c91(%rip), %rdi     # 0x2bd7b
xorl %ebx, %ebx
xorl %eax, %eax
callq 0xd8e5
jmp 0x1411e
leaq 0x17cc0(%rip), %rdi     # 0x2bdbc
xorl %ebx, %ebx
movq %r14, %rsi
xorl %eax, %eax
callq 0xd8e5
jmp 0x1411e
movq %rsp, %rdi
callq 0x1d934
jmp 0x1411c
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 0x14120
cmpb $0xa, -0x2(%rbx,%r12)
jne 0x14120
cmpb $0x0, -0x1(%rbx,%r12)
jne 0x14120
movb $0x0, -0x2(%rbx,%r12)
jmp 0x14120