// File: /workspace/github_c_makefile_star2_O1/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 0x1f5c2
testb %al, %al
je 0x14c4b
movq 0x10(%rbx), %rax
movb $0x1, (%rax)
leaq 0x20(%rsp), %rax
movq %rbx, (%rax)
movq %r14, 0x8(%rax)
movl (%rsp), %ecx
movq 0x8(%rbx), %r8
leaq 0x1b55e(%rip), %rdx     # 0x2fee0
leaq 0x30(%rsp), %r12
movl $0x2000, %esi           # imm = 0x2000
movq %r12, %rdi
xorl %eax, %eax
callq 0xd260
movq (%rbx), %r13
movq %r13, %rdi
callq 0xda40
movq %rdx, %rcx
movq %r13, %rdi
movq %r12, %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
leaq 0x1b50d(%rip), %r13     # 0x2fef7
cmpb $0x0, 0x2910f(%rip)     # 0x3db00
jne 0x14c2d
movl $0x1fff, %edx           # imm = 0x1FFF
movq %r14, %rdi
movq %r12, %rsi
callq 0x1f844
testq %rax, %rax
jle 0x14aca
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 0x14a32
movb $0x3f, 0x30(%rsp,%rcx)
incq %rcx
cmpq %rcx, %rax
jne 0x14a17
movq (%rbx), %rbp
movq %rbp, %rdi
callq 0xda40
movl $0x1, %ecx
movq %rbp, %rdi
movq %rax, %rsi
callq 0xeffa
movq %rdx, %rcx
movq (%rbx), %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0xda6e
movl 0x2909c(%rip), %edi     # 0x3db08
movl $0x2, %edx
movq %r13, %rsi
callq 0xd1d0
movq %rax, %rbp
cmpq $-0x1, %rax
jne 0x14a8c
callq 0xd0a0
cmpl $0x4, (%rax)
je 0x14a66
xorl %r15d, %r15d
testq %rbp, %rbp
jns 0x14af0
callq 0xd0a0
movl (%rax), %edi
callq 0xd650
leaq 0x1b452(%rip), %rdi     # 0x2fef9
leaq 0x1b478(%rip), %rsi     # 0x2ff26
movq %rax, %rdx
xorl %eax, %eax
callq 0xd954
leaq 0x20(%rsp), %rdi
callq 0x147ac
movl $0x1, %r15d
jmp 0x14af0
movl $0x3, %r15d
jns 0x14af0
callq 0xd0a0
movl (%rax), %eax
cmpl $0xb, %eax
je 0x14ae3
cmpl $0x4, %eax
jne 0x14af0
movl $0x3e8, %edi            # imm = 0x3E8
callq 0xd740
xorl %r15d, %r15d
testl %r15d, %r15d
je 0x149ea
cmpl $0x3, %r15d
jne 0x14c37
movq 0x8(%rsp), %rdi
callq 0xd130
testl %eax, %eax
je 0x14b27
movl (%rsp), %esi
leaq 0x1b432(%rip), %rdi     # 0x2ff4d
xorl %eax, %eax
callq 0xd954
jmp 0x14c2d
movq %rsp, %rdi
callq 0x1f783
movl %eax, %r8d
andl $0x7f, %r8d
je 0x14b62
movl %r8d, %ecx
shll $0x18, %ecx
addl $0x1000000, %ecx        # imm = 0x1000000
cmpl $0x2000000, %ecx        # imm = 0x2000000
jl 0x14b71
movl (%rsp), %ecx
leaq 0x1b43e(%rip), %rdx     # 0x2ff94
leaq 0x30(%rsp), %rdi
movl $0x2000, %esi           # imm = 0x2000
jmp 0x14b98
movl (%rsp), %ecx
movzbl %ah, %eax
leaq 0x1b409(%rip), %rdx     # 0x2ff78
jmp 0x14b8b
movl (%rsp), %ecx
cmpb $0x7f, %al
jne 0x14b84
movzbl %ah, %eax
leaq 0x1b42e(%rip), %rdx     # 0x2ffb0
jmp 0x14b8b
leaq 0x1b442(%rip), %rdx     # 0x2ffcd
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 0xeffa
movq %rdx, %rcx
movq (%rbx), %rdi
leaq 0x30(%rsp), %rsi
movq %rax, %rdx
callq 0xda6e
movq (%rbx), %rax
movl $0x2, 0x18(%rax)
leaq 0x1b319(%rip), %rbx     # 0x2fef7
movl 0x28f24(%rip), %edi     # 0x3db08
movl $0x2, %edx
movq %rbx, %rsi
callq 0xd1d0
movq %rax, %r14
cmpl $-0x1, %r14d
jne 0x14c04
callq 0xd0a0
cmpl $0x4, (%rax)
je 0x14bde
testl %r14d, %r14d
jns 0x14c2d
callq 0xd0a0
movl (%rax), %edi
callq 0xd650
leaq 0x1b2dd(%rip), %rdi     # 0x2fef9
leaq 0x1b303(%rip), %rsi     # 0x2ff26
movq %rax, %rdx
xorl %eax, %eax
callq 0xd954
leaq 0x20(%rsp), %rdi
callq 0x147ac
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 0x1b257(%rip), %rdi     # 0x2feb9
movq %rbx, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0xd954
xorl %edi, %edi
callq 0xd4b0