// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x2110, %rsp           # imm = 0x2110
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x18(%rbp)
movb $0x1, -0x39(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
movb -0x39(%rbp), %al
leaq -0x38(%rbp), %rdi
movl $0x2, %edx
andb $0x1, %al
movzbl %al, %ecx
callq 0x2abf0
testb $0x1, %al
jne 0x18fe9
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20d8(%rbp)
callq 0xd0a0
movl (%rax), %edi
callq 0xd630
movq -0x20d8(%rbp), %rsi
movq %rax, %rdx
leaq 0x2ae93(%rip), %rdi     # 0x43e6c
movb $0x0, %al
callq 0xd8f0
xorl %eax, %eax
movl %eax, %edi
callq 0xd490
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movb $0x1, (%rax)
movq -0x18(%rbp), %rax
movq %rax, -0x50(%rbp)
leaq -0x38(%rbp), %rax
movq %rax, -0x48(%rbp)
movl $0x0, -0x54(%rbp)
leaq -0x2060(%rbp), %rdi
movl -0x38(%rbp), %ecx
movq -0x18(%rbp), %rax
movq 0x8(%rax), %r8
movl $0x2000, %esi           # imm = 0x2000
leaq 0x2ae6a(%rip), %rdx     # 0x43e93
movb $0x0, %al
callq 0xd250
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x20e8(%rbp)
leaq -0x2060(%rbp), %rax
movq %rax, -0x20e0(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
callq 0xda10
movq -0x20e8(%rbp), %rdi
movq -0x20e0(%rbp), %rsi
movq %rax, -0x2070(%rbp)
movq %rdx, -0x2068(%rbp)
movq -0x2070(%rbp), %rdx
movq -0x2068(%rbp), %rcx
callq 0xda70
movq -0x30(%rbp), %rdi
callq 0xd440
movl %eax, -0x2074(%rbp)
movl -0x2074(%rbp), %edi
movl $0x3, %esi
xorl %edx, %edx
movb $0x0, %al
callq 0xd1a0
movl %eax, -0x2078(%rbp)
movl -0x2074(%rbp), %edi
movl -0x2078(%rbp), %edx
orl $0x800, %edx            # imm = 0x800
movl $0x4, %esi
movb $0x0, %al
callq 0xd1a0
testb $0x1, 0x37adb(%rip)     # 0x50bb0
je 0x190ed
leaq -0x50(%rbp), %rdi
callq 0x18d00
movq $0x0, -0x8(%rbp)
jmp 0x19516
leaq -0x2060(%rbp), %rsi
leaq -0x38(%rbp), %rdi
movl $0x1fff, %edx           # imm = 0x1FFF
callq 0x2b000
movq %rax, -0x2080(%rbp)
cmpq $0x0, -0x2080(%rbp)
jle 0x19291
movq -0x2080(%rbp), %rax
movb $0x0, -0x2060(%rbp,%rax)
movl $0x0, -0x2084(%rbp)
movslq -0x2084(%rbp), %rax
cmpq -0x2080(%rbp), %rax
jge 0x1918a
movslq -0x2084(%rbp), %rax
movsbl -0x2060(%rbp,%rax), %eax
cmpl $0x20, %eax
jge 0x19177
movslq -0x2084(%rbp), %rax
movsbl -0x2060(%rbp,%rax), %eax
cmpl $0xa, %eax
je 0x19177
movslq -0x2084(%rbp), %rax
movb $0x3f, -0x2060(%rbp,%rax)
jmp 0x19179
movl -0x2084(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2084(%rbp)
jmp 0x19130
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x20f0(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
callq 0xda10
movq -0x20f0(%rbp), %rdi
movq %rax, -0x20a8(%rbp)
movq %rdx, -0x20a0(%rbp)
movq -0x20a8(%rbp), %rsi
movq -0x20a0(%rbp), %rdx
movl $0x1, %ecx
callq 0xfaf0
movq %rax, -0x2098(%rbp)
movq %rdx, -0x2090(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
leaq -0x2060(%rbp), %rsi
movq -0x2098(%rbp), %rdx
movq -0x2090(%rbp), %rcx
callq 0xda70
movl 0x379b2(%rip), %edi     # 0x50bb8
leaq 0x2ac9d(%rip), %rsi     # 0x43eaa
movl $0x2, %edx
callq 0xd1c0
movq %rax, -0x2080(%rbp)
xorl %eax, %eax
cmpq $-0x1, -0x2080(%rbp)
movb %al, -0x20f1(%rbp)
jne 0x19241
callq 0xd0a0
cmpl $0x4, (%rax)
sete %al
movb %al, -0x20f1(%rbp)
movb -0x20f1(%rbp), %al
testb $0x1, %al
jne 0x19200
cmpq $0x0, -0x2080(%rbp)
jge 0x1928f
callq 0xd0a0
movl (%rax), %edi
callq 0xd630
movq %rax, %rdx
leaq 0x2ac41(%rip), %rdi     # 0x43eac
leaq 0x2ac67(%rip), %rsi     # 0x43ed9
movb $0x0, %al
callq 0xd8f0
leaq -0x50(%rbp), %rdi
callq 0x18d00
movq $0x0, -0x8(%rbp)
jmp 0x19516
jmp 0x192cd
cmpq $0x0, -0x2080(%rbp)
jge 0x192c9
callq 0xd0a0
cmpl $0xb, (%rax)
je 0x192b9
callq 0xd0a0
cmpl $0xb, (%rax)
je 0x192b9
callq 0xd0a0
cmpl $0x4, (%rax)
jne 0x192c5
movl $0x3e8, %edi            # imm = 0x3E8
callq 0xd720
jmp 0x192c7
jmp 0x192d2
jmp 0x192cb
jmp 0x192d2
jmp 0x192cd
jmp 0x190ce
movq -0x30(%rbp), %rdi
callq 0xd120
cmpl $0x0, %eax
je 0x19307
movl -0x38(%rbp), %esi
leaq 0x2ac16(%rip), %rdi     # 0x43f00
movb $0x0, %al
callq 0xd8f0
leaq -0x50(%rbp), %rdi
callq 0x18d00
movq $0x0, -0x8(%rbp)
jmp 0x19516
leaq -0x38(%rbp), %rdi
callq 0x2af10
movl %eax, -0x20ac(%rbp)
movl -0x20ac(%rbp), %eax
andl $0x7f, %eax
cmpl $0x0, %eax
jne 0x19358
leaq -0x2060(%rbp), %rdi
movl -0x38(%rbp), %ecx
movl -0x20ac(%rbp), %r8d
andl $0xff00, %r8d           # imm = 0xFF00
sarl $0x8, %r8d
movl $0x2000, %esi           # imm = 0x2000
leaq 0x2abdf(%rip), %rdx     # 0x43f2b
movb $0x0, %al
callq 0xd250
jmp 0x19401
movl -0x20ac(%rbp), %eax
andl $0x7f, %eax
addl $0x1, %eax
movsbl %al, %eax
sarl %eax
cmpl $0x0, %eax
jle 0x19398
leaq -0x2060(%rbp), %rdi
movl -0x38(%rbp), %ecx
movl -0x20ac(%rbp), %r8d
andl $0x7f, %r8d
movl $0x2000, %esi           # imm = 0x2000
leaq 0x2abb8(%rip), %rdx     # 0x43f47
movb $0x0, %al
callq 0xd250
jmp 0x193ff
movl -0x20ac(%rbp), %eax
andl $0xff, %eax
cmpl $0x7f, %eax
jne 0x193d9
leaq -0x2060(%rbp), %rdi
movl -0x38(%rbp), %ecx
movl -0x20ac(%rbp), %r8d
andl $0xff00, %r8d           # imm = 0xFF00
sarl $0x8, %r8d
movl $0x2000, %esi           # imm = 0x2000
leaq 0x2ab93(%rip), %rdx     # 0x43f63
movb $0x0, %al
callq 0xd250
jmp 0x193fd
leaq -0x2060(%rbp), %rdi
movl -0x38(%rbp), %ecx
movl -0x20ac(%rbp), %r8d
movl $0x2000, %esi           # imm = 0x2000
leaq 0x2ab8a(%rip), %rdx     # 0x43f80
movb $0x0, %al
callq 0xd250
jmp 0x193ff
jmp 0x19401
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x2100(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
callq 0xda10
movq -0x2100(%rbp), %rdi
movq %rax, -0x20d0(%rbp)
movq %rdx, -0x20c8(%rbp)
movq -0x20d0(%rbp), %rsi
movq -0x20c8(%rbp), %rdx
movl $0x1, %ecx
callq 0xfaf0
movq %rax, -0x20c0(%rbp)
movq %rdx, -0x20b8(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
leaq -0x2060(%rbp), %rsi
movq -0x20c0(%rbp), %rdx
movq -0x20b8(%rbp), %rcx
callq 0xda70
movq -0x18(%rbp), %rax
movq (%rax), %rax
movl $0x2, 0x18(%rax)
movl 0x3772d(%rip), %edi     # 0x50bb8
leaq 0x2aa18(%rip), %rsi     # 0x43eaa
movl $0x2, %edx
callq 0xd1c0
movl %eax, -0x54(%rbp)
xorl %eax, %eax
cmpl $-0x1, -0x54(%rbp)
movb %al, -0x2101(%rbp)
jne 0x194be
callq 0xd0a0
cmpl $0x4, (%rax)
sete %al
movb %al, -0x2101(%rbp)
movb -0x2101(%rbp), %al
testb $0x1, %al
jne 0x19485
cmpl $0x0, -0x54(%rbp)
jge 0x19505
callq 0xd0a0
movl (%rax), %edi
callq 0xd630
movq %rax, %rdx
leaq 0x2a9c8(%rip), %rdi     # 0x43eac
leaq 0x2a9ee(%rip), %rsi     # 0x43ed9
movb $0x0, %al
callq 0xd8f0
leaq -0x50(%rbp), %rdi
callq 0x18d00
movq $0x0, -0x8(%rbp)
jmp 0x19516
leaq -0x50(%rbp), %rdi
callq 0x18d00
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x2110, %rsp           # imm = 0x2110
popq %rbp
retq
nopw %cs:(%rax,%rax)