// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x18(%rbp)
movq %rsi, -0x20(%rbp)
movq %rdx, -0x28(%rbp)
leaq -0x10(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0xd290
leaq -0x48(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0xd290
movb $0x0, -0x49(%rbp)
movq -0x18(%rbp), %rsi
movb -0x49(%rbp), %al
leaq -0x48(%rbp), %rdi
movl $0x3, %edx
andb $0x1, %al
movzbl %al, %ecx
callq 0x2abf0
testb $0x1, %al
jne 0x1958e
jmp 0x19729
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rsi
movq -0x58(%rbp), %rdx
leaq -0x48(%rbp), %rdi
callq 0x2b0a0
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x58(%rbp), %rax
je 0x195cc
leaq 0x2a76e(%rip), %rdi     # 0x43d2e
movb $0x0, %al
callq 0xd8f0
jmp 0x19729
leaq -0x48(%rbp), %rdi
callq 0x2ae90
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x80(%rbp)
movq -0x70(%rbp), %rax
addq $0x2000, %rax           # imm = 0x2000
movq %rax, -0x88(%rbp)
movq -0x68(%rbp), %rdi
movq -0x88(%rbp), %rsi
callq 0xd500
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
jne 0x1963b
movq -0x88(%rbp), %rsi
leaq 0x2a740(%rip), %rdi     # 0x43d6f
movb $0x0, %al
callq 0xd8f0
jmp 0x19729
movq -0x88(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rsi
addq -0x78(%rbp), %rsi
leaq -0x48(%rbp), %rdi
movl $0x1fff, %edx           # imm = 0x1FFF
callq 0x2b000
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x80(%rbp)
jge 0x19675
leaq -0x48(%rbp), %rdi
callq 0x2af10
jmp 0x19729
movq -0x68(%rbp), %rax
movq -0x78(%rbp), %rcx
addq -0x80(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x80(%rbp), %rax
addq -0x78(%rbp), %rax
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x80(%rbp)
jg 0x195f5
leaq -0x48(%rbp), %rdi
callq 0x2af10
movl %eax, -0x8c(%rbp)
cmpq $0x0, -0x78(%rbp)
jne 0x196bd
movq -0x68(%rbp), %rdi
callq 0xd090
jmp 0x19729
cmpl $0x0, -0x8c(%rbp)
je 0x196dc
movl -0x8c(%rbp), %esi
leaq 0x2a6bc(%rip), %rdi     # 0x43d8f
movb $0x0, %al
callq 0xd8f0
jmp 0x19729
cmpq $0x2, -0x78(%rbp)
jle 0x1971d
movq -0x68(%rbp), %rax
movq -0x78(%rbp), %rcx
subq $0x2, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
jne 0x1971d
movq -0x68(%rbp), %rax
movq -0x78(%rbp), %rcx
subq $0x1, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x1971d
movq -0x68(%rbp), %rax
movq -0x78(%rbp), %rcx
subq $0x2, %rcx
movb $0x0, (%rax,%rcx)
movq -0x68(%rbp), %rax
movq %rax, -0x8(%rbp)
movb $0x1, -0x10(%rbp)
movb -0x10(%rbp), %al
movq -0x8(%rbp), %rdx
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax)