// File: /workspace/github_cpp_makefile_star2_O3/jkingan[P]ladyheather/heathui.cpp
// Function: config_input_device(unsigned int, char*)
int config_input_device(unsigned port, char *arg)
{
int i;
char *comma;

   // __linux__  __MACH__  __FreeBSD__   ... WINDOWS can't do this

   // setup to use a com device using a com device name.
   // Returns 0 if successful,  non-zero if error.
   // Serial port baud rate info can follow the device number/name - separated by a comma

   #ifdef WINDOWS
      return 4;     // Windows does not support named input devices
   #endif

   if(arg == 0) return 1;
   if(port >= NUM_COM_PORTS) return 2;

   comma = strchr(arg, ',');
   if(comma) {     // baud rate info follows the '='
      *comma = 0;
//    port = (unsigned) set_serial_params(port, arg, 0);
      port = (unsigned) set_serial_params(port, arg, 1);
      if(port >= NUM_COM_PORTS) return 3;
   }

   com[port].com_port = 0;
   com[port].user_disabled_com = 0;
   com[port].usb_port = USE_IDEV_NAME;
   com[port].process_com = 1;

   if(arg[0]) {  // device name given
      strncpy(com[port].com_dev, &arg[0], sizeof(com[port].com_dev));

      for(i=strlen(com[port].com_dev)-1; i>=0; i++) {  // trim trailing scuzz
         if     (com[port].com_dev[i] == 0x0A) com[port].com_dev[i] = 0;
         else if(com[port].com_dev[i] == 0x0D) com[port].com_dev[i] = 0;
         else if(com[port].com_dev[i] == ' ')  com[port].com_dev[i] = 0;
         else if(com[port].com_dev[i] == '\t') com[port].com_dev[i] = 0;
         else if(com[port].com_dev[i]) break;
      }
   }
   else if(com[port].com_dev[0]) {   // use previously set device name
   }
   else {  // use default device name
      strncpy(com[port].com_dev, "/dev/heather", sizeof(com[port].com_dev));
   }

   if(com[port].com_dev[0] == 0) return 3;
   com[port].com_dev[sizeof(com[port].com_dev)-1] = 0;

   if(com[port].com_running) {
      com[port].process_com = 1;
      com[port].user_disabled_com = 0;
      init_com(port, 14);
   }

   com[port].last_com_time = this_msec - com[port].com_timeout;

   return 0;
}

// Assembly:
testq %rsi, %rsi
je 0x4d455
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edi, %ebx
movl $0x2, %eax
cmpl $0x12, %edi
ja 0x4d4df
movq %rsi, %r14
movq %rsi, %rdi
movl $0x2c, %esi
callq 0x9100
testq %rax, %rax
je 0x4d3ba
movb $0x0, (%rax)
movl %ebx, %edi
movq %r14, %rsi
movl $0x1, %edx
callq 0x262e7
movl %eax, %ebx
movl $0x3, %eax
cmpl $0x12, %ebx
ja 0x4d4df
movl %ebx, %eax
movq %rax, -0x30(%rbp)
imulq $0x2260, %rax, %r13     # imm = 0x2260
leaq 0x2c0f12(%rip), %rbx    # 0x30e2e0
leaq (%rbx,%r13), %r12
movabsq $0x270f00000000, %rax   # imm = 0x270F00000000
leaq (%rbx,%r13), %r15
addq $0xc, %r15
movq %rax, -0xc(%r15)
movl $0x0, 0x221c(%r15)
movl $0x1, 0x2228(%r15)
cmpb $0x0, (%r14)
je 0x4d45b
movl $0x101, %edx            # imm = 0x101
movq %r15, %rdi
movq %r14, %rsi
callq 0x9170
movq %r15, %rdi
callq 0x9120
decl %eax
js 0x4d475
movl %eax, %eax
addq %rax, %r13
leaq (%rbx,%r13), %rax
addq $0xc, %rax
movabsq $0x100002600, %rcx      # imm = 0x100002600
movzbl (%rax), %edx
cmpq $0x20, %rdx
ja 0x4d475
btq %rdx, %rcx
jae 0x4d44e
movb $0x0, (%rax)
incq %rax
jmp 0x4d437
testq %rdx, %rdx
je 0x4d449
jmp 0x4d475
movl $0x1, %eax
retq
cmpb $0x0, (%r15)
jne 0x4d475
leaq 0xbc5ed(%rip), %rsi     # 0x109a55
movl $0x101, %edx            # imm = 0x101
movq %r15, %rdi
callq 0x9170
cmpb $0x0, 0xc(%r12)
je 0x4d4da
movb $0x0, 0x10c(%r12)
cmpl $0x0, 0x2230(%r12)
je 0x4d4b7
movl $0x1, 0x2234(%r12)
movl $0x0, 0x2228(%r12)
movq -0x30(%rbp), %rdi
movl $0xe, %esi
callq 0xb49e
leaq 0x2c02aa(%rip), %rax    # 0x30d768
movsd (%rax), %xmm0
subsd 0x2238(%r12), %xmm0
movsd %xmm0, 0x2248(%r12)
xorl %eax, %eax
jmp 0x4d4df
movl $0x3, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq