// File: /workspace/github_cpp_makefile_star2_O3/jkingan[P]ladyheather/heather.cpp
// Function: get_serial_char(unsigned int)
u08 get_serial_char(unsigned port)
{
int flag;
u08 c;

   // return the next byte from a com device
   if(port >= NUM_COM_PORTS) return FALSE;

   if((port == RCVR_PORT) && (rcvr_type == NO_RCVR) && (enable_terminal == 0)) { // alternate ready/not ready each check
      return 0;
   }
if((port == RCVR_PORT) && (rcvr_type == TIDE_RCVR) && (enable_terminal == 0)) {
   return 0;
}


   if((port == RCVR_PORT) && sim_file) {  // get char from a simulation file
      return get_sim_char(port);
   }

   if(com[port].process_com == 0) return 0;  //lfs
   if(com[port].com_fd < 0) return 0;
   if(com[port].com_error & RCV_ERR) return 0;

   if(com[port].next_rcv_byte < com[port].rcv_byte_count) {   // return byte previously fetched by check_incoming_data()
      c = com[port].rcv_buffer[com[port].next_rcv_byte++];
      if((c == 0x0D) || (c == 0x0A)) crlf_seen = 1;

      if((zoom_screen == 'O')  && (port == monitor_port)) {   // send char to "terminal" format monitor screen
         echo_term(monitor_port, c, monitor_hex, 0);
      }
      return c;
   }

   com[port].rcv_byte_count = 0;
   com[port].next_rcv_byte = 0;

   while(com[port].rcv_byte_count == 0) {  // wait until we have a character
      if(1 || (com[port].com_port != 0) || (com[port].usb_port != 0)) {  // zork COM port in use: read a byte from serial port
         flag = read(com[port].com_fd, &com[port].rcv_buffer[0], RCV_BUF_SIZE);
         if(flag < 0) {  // !!!! com error or no serial data available - we should check errno()
            flag = com[port].rcv_byte_count = 0;
         }
         else com[port].rcv_byte_count = flag;
      }

      update_pwm();   // if doing pwm temperature control
   }

   if(flag && com[port].rcv_byte_count) {  // succesful read
      c = com[port].rcv_buffer[com[port].next_rcv_byte++];
      if((c == 0x0D) || (c == 0x0A)) crlf_seen = 1;

      if((zoom_screen == 'O') && (port == monitor_port)) {   // send char to "terminal" format monitor screen
         echo_term(monitor_port, c, monitor_hex, 0);
      }
      return c;
   }

   if(com_error_exit) {
      error_exit(222, "Serial receive error");
   }
   else {
      com[port].com_error |= RCV_ERR;
      ++com_errors;
   }
   return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
cmpl $0x12, %edi
ja 0xbb50
movl %edi, %ebx
cmpl $0xa, %edi
sete %cl
movq 0x20cdb6(%rip), %rax    # 0x2188d0
cmpq $0x400, %rax            # imm = 0x400
sete %dl
andb %cl, %dl
movl 0x233405(%rip), %ecx    # 0x23ef30
testl %ecx, %ecx
sete %sil
testb %sil, %dl
jne 0xbb50
testl %ecx, %ecx
sete %cl
cmpl $0xa, %ebx
sete %dl
cmpq $0x800000, %rax         # imm = 0x800000
sete %al
andb %dl, %al
testb %cl, %al
je 0xbb5f
xorl %r14d, %r14d
movl %r14d, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
cmpl $0xa, %ebx
jne 0xbb80
cmpq $0x0, 0x232854(%rip)    # 0x23e3c0
je 0xbb80
movl $0xa, %edi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0xaa9f
movl %ebx, %eax
imulq $0x2260, %rax, %rax     # imm = 0x2260
leaq 0x302750(%rip), %r12    # 0x30e2e0
cmpl $0x0, 0x2234(%rax,%r12)
je 0xbb50
addq %rax, %r12
cmpl $0x0, 0x8(%r12)
js 0xbb50
testb $0x1, 0x2251(%r12)
jne 0xbb50
movslq 0x2224(%r12), %rax
cmpl 0x2220(%r12), %eax
jl 0xbc1d
movq $0x0, 0x2220(%r12)
leaq 0x1220(%r12), %r14
movl 0x8(%r12), %edi
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rsi
callq 0x9570
movq %rax, %r15
testl %r15d, %r15d
movl $0x0, %eax
cmovgl %r15d, %eax
movl %eax, 0x2220(%r12)
callq 0xd1efe
cmpl $0x0, 0x2220(%r12)
je 0xbbd7
testl %r15d, %r15d
jle 0xbc77
movslq 0x2224(%r12), %rax
leal 0x1(%rax), %ecx
movl %ecx, 0x2224(%r12)
movzbl 0x1220(%r12,%rax), %r14d
cmpl $0xd, %r14d
je 0xbc3d
cmpl $0xa, %r14d
jne 0xbc47
movl $0x1, 0x23334d(%rip)    # 0x23ef94
cmpb $0x4f, 0x300b5a(%rip)   # 0x30c7a8
jne 0xbb53
cmpl %ebx, 0x302676(%rip)    # 0x30e2d0
jne 0xbb53
movl 0x2327de(%rip), %edx    # 0x23e444
movl %ebx, %edi
movl %r14d, %esi
xorl %ecx, %ecx
callq 0xac18
jmp 0xbb53
cmpb $0x0, 0x20f5b5(%rip)    # 0x21b233
je 0xbc96
leaq 0xfde28(%rip), %rsi     # 0x109aaf
movl $0xde, %edi
callq 0xb424
jmp 0xbb50
orb $0x1, 0x2251(%r12)
incl 0x32b36f(%rip)          # 0x337014
jmp 0xbb50