// File: jkingan[P]ladyheather/heather.cpp
// Function: check_incoming_data(unsigned int)
int check_incoming_data(unsigned port)
{
int flag;

   // return true if we have a serial port byte available to read
   if(port >= NUM_COM_PORTS) return 0;

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

   if((port == RCVR_PORT) && sim_file) {  // using a simulation file
      return sim_kbd_check();
   }

   if(com[port].com_fd < 0) return 0;

   if(com[port].com_error & RCV_ERR) {
//printf("com port %d avail err:%02X\n", port, com[port].com_error);
      return -100; // !!!! return 0;
   }
   if(com[port].next_rcv_byte < com[port].rcv_byte_count) return 2000|com[port].next_rcv_byte;  // we have already read and buffered the char

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

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

   return (0x8000 | com[port].rcv_byte_count);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x10, %rsp
movl %edi, -0x8(%rbp)
cmpl $0x13, -0x8(%rbp)
jb 0xcaed
movl $0x0, -0x4(%rbp)
jmp 0xcd4f
cmpl $0xa, -0x8(%rbp)
jne 0xcb23
cmpq $0x400, 0x26afb2(%rip) # imm = 0x400
jne 0xcb23
cmpl $0x0, 0x291609(%rip)    # 0x29e110
jne 0xcb23
movl 0x3925cd(%rip), %eax    # 0x39f0dc
addl $0x1, %eax
movl %eax, 0x3925c4(%rip)    # 0x39f0dc
andl $0x1, %eax
movl %eax, -0x4(%rbp)
jmp 0xcd4f
cmpl $0xa, -0x8(%rbp)
jne 0xcb59
cmpq $0x800000, 0x26af7c(%rip) # imm = 0x800000
jne 0xcb59
cmpl $0x0, 0x2915d3(%rip)    # 0x29e110
jne 0xcb59
movl 0x392597(%rip), %eax    # 0x39f0dc
addl $0x1, %eax
movl %eax, 0x39258e(%rip)    # 0x39f0dc
andl $0x1, %eax
movl %eax, -0x4(%rbp)
jmp 0xcd4f
cmpl $0xa, -0x8(%rbp)
jne 0xcb76
cmpq $0x0, 0x290a39(%rip)    # 0x29d5a0
je 0xcb76
callq 0xaf80
movl %eax, -0x4(%rbp)
jmp 0xcd4f
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x36093e(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
cmpl $0x0, 0x8(%rax)
jge 0xcb9e
movl $0x0, -0x4(%rbp)
jmp 0xcd4f
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x360916(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movzbl 0x2251(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0xcbcf
movl $0xffffff9c, -0x4(%rbp) # imm = 0xFFFFFF9C
jmp 0xcd4f
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x3608e5(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl 0x2224(%rax), %eax
movl -0x8(%rbp), %ecx
movl %ecx, %edx
leaq 0x3608c9(%rip), %rcx    # 0x36d4c0
imulq $0x2260, %rdx, %rdx     # imm = 0x2260
addq %rdx, %rcx
cmpl 0x2220(%rcx), %eax
jge 0xcc32
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x3608ab(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl 0x2224(%rax), %eax
orl $0x7d0, %eax            # imm = 0x7D0
movl %eax, -0x4(%rbp)
jmp 0xcd4f
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x360882(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl $0x0, 0x2220(%rax)
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x360862(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl $0x0, 0x2224(%rax)
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x360842(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl 0x8(%rax), %edi
movl -0x8(%rbp), %eax
leaq 0x36082b(%rip), %rsi    # 0x36d4c0
imulq $0x2260, %rax, %rax     # imm = 0x2260
addq %rax, %rsi
addq $0x1220, %rsi           # imm = 0x1220
movl $0x1000, %edx           # imm = 0x1000
callq 0x9560
movl %eax, -0xc(%rbp)
cmpl $0x0, -0xc(%rbp)
jge 0xcce2
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x3607fb(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl $0x0, 0x2220(%rax)
movl $0x0, -0x4(%rbp)
jmp 0xcd4f
movl -0xc(%rbp), %ecx
movl -0x8(%rbp), %eax
movl %eax, %edx
leaq 0x3607cf(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rdx, %rdx     # imm = 0x2260
addq %rdx, %rax
movl %ecx, 0x2220(%rax)
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x3607b3(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
cmpl $0x0, 0x2220(%rax)
jne 0xcd29
movl $0x0, -0x4(%rbp)
jmp 0xcd4f
jmp 0xcd2b
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x360789(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl 0x2220(%rax), %eax
orl $0x8000, %eax           # imm = 0x8000
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x10, %rsp
popq %rbp
retq
nopl (%rax,%rax)