// File: /workspace/github_c_makefile_star2_O3/justy989[P]ce/ce_app.c
// Function: run_shell_command_and_output_to_buffer
static void* run_shell_command_and_output_to_buffer(void* data){
     ShellCommandData_t* shell_command_data = (ShellCommandData_t*)(data);

     CeSubprocess_t subprocess;
     bool use_shell = true;
     if(!ce_subprocess_open(&subprocess, shell_command_data->command, CE_PROC_COMM_STDOUT, use_shell)){
          ce_log("failed to run shell command '%s': '%s'", shell_command_data->command, strerror(errno));
          pthread_exit(NULL);
     }

     *shell_command_data->should_scroll = true;

     RunShellCommandCleanup_t cleanup = {shell_command_data, &subprocess};
     int rc = 0;

     char bytes[BUFSIZ];
     snprintf(bytes, BUFSIZ, "pid %d started: '%s'\n\n", subprocess.pid, shell_command_data->command);
     ce_buffer_insert_string(shell_command_data->buffer, bytes, ce_buffer_end_point(shell_command_data->buffer));

     int stdout_fd = fileno(subprocess.stdout_file);
     int flags = fcntl(stdout_fd, F_GETFL, 0);
     fcntl(stdout_fd, F_SETFL, flags | O_NONBLOCK);

     while(true){
          if(g_shell_command_should_die){
               run_shell_command_cleanup(&cleanup);
               return NULL;
          }

          int64_t bytes_read = ce_subprocess_read_stdout(&subprocess, bytes, BUFSIZ - 1);
          if(bytes_read > 0){
               bytes[bytes_read] = 0;

               // sanitize bytes for non-printable characters
               for(int i = 0; i < bytes_read; i++){
                   if(bytes[i] < 32 && bytes[i] != '\n') bytes[i] = '?';
               }

               CePoint_t end = ce_buffer_advance_point(shell_command_data->buffer, ce_buffer_end_point(shell_command_data->buffer), 1);
               ce_buffer_insert_string(shell_command_data->buffer, bytes, end);
#if defined(DISPLAY_TERMINAL)
               do{
                    bytes_read = write(g_shell_command_ready_fds[1], "1", 2);
               }while(bytes_read == -1 && errno == EINTR);
#endif

               if(bytes_read < 0){
                    ce_log("%s() write() to terminal ready fd failed: %s", __FUNCTION__, strerror(errno));
                    run_shell_command_cleanup(&cleanup);
                    return NULL;
               }
          }else if(bytes_read < 0){
               if(errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR){
                    usleep(1000);
               }else{
                    break;
               }
          }else{
               break;
          }
     }

     if(ferror(subprocess.stdout_file)){
          ce_log("shell command: fgets() from pid %d failed\n", subprocess.pid);
          run_shell_command_cleanup(&cleanup);
          return NULL;
     }

     int status = ce_subprocess_close(&subprocess);

     if(WIFEXITED(status)){
          snprintf(bytes, BUFSIZ, "\npid %d exited with code %d", subprocess.pid, WEXITSTATUS(status));
     }else if(WIFSIGNALED(status)){
          snprintf(bytes, BUFSIZ, "\npid %d killed by signal %d", subprocess.pid, WTERMSIG(status));
     }else if(WIFSTOPPED(status)){
          snprintf(bytes, BUFSIZ, "\npid %d stopped by signal %d", subprocess.pid, WSTOPSIG(status));
     }else{
          snprintf(bytes, BUFSIZ, "\npid %d stopped with unexpected status %d", subprocess.pid, status);
     }

     CePoint_t end = ce_buffer_advance_point(shell_command_data->buffer, ce_buffer_end_point(shell_command_data->buffer), 1);
     ce_buffer_insert_string(shell_command_data->buffer, bytes, end);
     shell_command_data->buffer->status = CE_BUFFER_STATUS_READONLY;
#if defined(DISPLAY_TERMINAL)
     do{
          rc = write(g_shell_command_ready_fds[1], "1", 2);
     }while(rc == -1 && errno == EINTR);
#endif

     if(rc < 0){
          ce_log("%s() write() to terminal ready fd failed: %s", __FUNCTION__, strerror(errno));
          run_shell_command_cleanup(&cleanup);
          return NULL;
     }

     run_shell_command_cleanup(&cleanup);
     return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2038, %rsp           # imm = 0x2038
movq %rdi, %rbx
movq 0x8(%rdi), %rsi
movq %rsp, %r14
movq %r14, %rdi
movl $0x2, %edx
movl $0x1, %ecx
callq 0x1f552
testb %al, %al
je 0x1494d
movq 0x10(%rbx), %rax
movb $0x1, (%rax)
movq %rbx, 0x20(%rsp)
movq %r14, 0x28(%rsp)
movl (%rsp), %ecx
movq 0x8(%rbx), %r8
leaq 0x1a81e(%rip), %rdx     # 0x2eedc
leaq 0x30(%rsp), %r14
movl $0x2000, %esi           # imm = 0x2000
movq %r14, %rdi
xorl %eax, %eax
callq 0xd260
movq (%rbx), %r15
movq %r15, %rdi
callq 0xda40
movq %rdx, %rcx
movq %r15, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0xda6e
movq 0x8(%rsp), %rdi
callq 0xd460
movl %eax, %ebp
movl %eax, %edi
movl $0x3, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0xd1b0
orl $0x800, %eax            # imm = 0x800
movl %ebp, %edi
movl $0x4, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0xd1b0
cmpb $0x0, 0x283da(%rip)     # 0x3cb00
jne 0x1492f
movq %rsp, %r14
leaq 0x30(%rsp), %r15
leaq 0x1a7b8(%rip), %r12     # 0x2eef3
movl $0x1fff, %edx           # imm = 0x1FFF
movq %r14, %rdi
movq %r15, %rsi
callq 0x1f7d8
testq %rax, %rax
jle 0x147ce
movb $0x0, 0x30(%rsp,%rax)
xorl %ecx, %ecx
movb 0x30(%rsp,%rcx), %dl
cmpb $0x20, %dl
setge %sil
cmpb $0xa, %dl
sete %dl
orb %sil, %dl
jne 0x14772
movb $0x3f, 0x30(%rsp,%rcx)
incq %rcx
cmpq %rcx, %rax
jne 0x14757
movq (%rbx), %r13
movq %r13, %rdi
callq 0xda40
movl $0x1, %ecx
movq %r13, %rdi
movq %rax, %rsi
callq 0xee7a
movq %rdx, %rcx
movq (%rbx), %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0xda6e
movl 0x2835c(%rip), %edi     # 0x3cb08
movl $0x2, %edx
movq %r12, %rsi
callq 0xd1d0
cmpq $-0x1, %rax
jne 0x147ed
callq 0xd0a0
cmpl $0x4, (%rax)
je 0x147a6
jmp 0x14910
jns 0x14808
callq 0xd0a0
movl (%rax), %eax
cmpl $0xb, %eax
je 0x147e1
cmpl $0x4, %eax
jne 0x14808
movl $0x3e8, %edi            # imm = 0x3E8
callq 0xd740
jmp 0x147f6
testq %rax, %rax
js 0x1490b
cmpb $0x0, 0x28303(%rip)     # 0x3cb00
je 0x1473b
jmp 0x1492f
movq 0x8(%rsp), %rdi
callq 0xd130
testl %eax, %eax
je 0x1482c
movl (%rsp), %esi
leaq 0x1a729(%rip), %rdi     # 0x2ef49
xorl %eax, %eax
callq 0xd954
jmp 0x1492f
movq %rsp, %rdi
callq 0x1f70f
movl %eax, %r8d
andl $0x7f, %r8d
je 0x14867
movl %r8d, %ecx
shll $0x18, %ecx
addl $0x1000000, %ecx        # imm = 0x1000000
cmpl $0x2000000, %ecx        # imm = 0x2000000
jl 0x14876
movl (%rsp), %ecx
leaq 0x1a735(%rip), %rdx     # 0x2ef90
leaq 0x30(%rsp), %rdi
movl $0x2000, %esi           # imm = 0x2000
jmp 0x1489d
movl (%rsp), %ecx
movzbl %ah, %eax
leaq 0x1a700(%rip), %rdx     # 0x2ef74
jmp 0x14890
movl (%rsp), %ecx
cmpb $0x7f, %al
jne 0x14889
movzbl %ah, %eax
leaq 0x1a725(%rip), %rdx     # 0x2efac
jmp 0x14890
leaq 0x1a739(%rip), %rdx     # 0x2efc9
leaq 0x30(%rsp), %rdi
movl $0x2000, %esi           # imm = 0x2000
movl %eax, %r8d
xorl %eax, %eax
callq 0xd260
movq (%rbx), %r14
movq %r14, %rdi
callq 0xda40
movl $0x1, %ecx
movq %r14, %rdi
movq %rax, %rsi
callq 0xee7a
movq %rdx, %rcx
movq (%rbx), %rdi
leaq 0x30(%rsp), %rsi
movq %rax, %rdx
callq 0xda6e
movq (%rbx), %rax
movl $0x2, 0x18(%rax)
leaq 0x1a610(%rip), %rbx     # 0x2eef3
movl 0x2821f(%rip), %edi     # 0x3cb08
movl $0x2, %edx
movq %rbx, %rsi
callq 0xd1d0
cmpl $-0x1, %eax
jne 0x14907
callq 0xd0a0
cmpl $0x4, (%rax)
je 0x148e3
jmp 0x14910
testl %eax, %eax
jns 0x1492f
callq 0xd0a0
movl (%rax), %edi
callq 0xd650
leaq 0x1a5d7(%rip), %rdi     # 0x2eef5
leaq 0x1a5fd(%rip), %rsi     # 0x2ef22
movq %rax, %rdx
xorl %eax, %eax
callq 0xd954
leaq 0x20(%rsp), %rdi
callq 0x144ee
xorl %eax, %eax
addq $0x2038, %rsp           # imm = 0x2038
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rbx), %rbx
callq 0xd0a0
movl (%rax), %edi
callq 0xd650
leaq 0x1a551(%rip), %rdi     # 0x2eeb5
movq %rbx, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0xd954
xorl %edi, %edi
callq 0xd4b0