// File: /workspace/github_cpp_makefile_star2_O2/pelrun[P]aft/rs232.c
// Function: RS232_OpenComport
int RS232_OpenComport(const char* comport, int baudrate, const char *mode, HANDLE *handle)
{
  int baudr,
      status;

  struct termios new_port_settings;

  switch(baudrate)
  {
    case      50 : baudr = B50;
                   break;
    case      75 : baudr = B75;
                   break;
    case     110 : baudr = B110;
                   break;
    case     134 : baudr = B134;
                   break;
    case     150 : baudr = B150;
                   break;
    case     200 : baudr = B200;
                   break;
    case     300 : baudr = B300;
                   break;
    case     600 : baudr = B600;
                   break;
    case    1200 : baudr = B1200;
                   break;
    case    1800 : baudr = B1800;
                   break;
    case    2400 : baudr = B2400;
                   break;
    case    4800 : baudr = B4800;
                   break;
    case    9600 : baudr = B9600;
                   break;
    case   19200 : baudr = B19200;
                   break;
    case   38400 : baudr = B38400;
                   break;
    case   57600 : baudr = B57600;
                   break;
    case  115200 : baudr = B115200;
                   break;
    case  230400 : baudr = B230400;
                   break;
    case  460800 : baudr = B460800;
                   break;
    case  500000 : baudr = B500000;
                   break;
    case  576000 : baudr = B576000;
                   break;
    case  921600 : baudr = B921600;
                   break;
    case 1000000 : baudr = B1000000;
                   break;
    case 1152000 : baudr = B1152000;
                   break;
    case 1500000 : baudr = B1500000;
                   break;
    case 2000000 : baudr = B2000000;
                   break;
    case 2500000 : baudr = B2500000;
                   break;
    case 3000000 : baudr = B3000000;
                   break;
    case 3500000 : baudr = B3500000;
                   break;
    case 4000000 : baudr = B4000000;
                   break;
    default      : printf("invalid baudrate\n");
                   return -1;
                   break;
  }

  int cbits=CS8,
      cpar=0,
      ipar=IGNPAR,
      bstop=0;

  if(strlen(mode) != 3)
  {
    printf("invalid mode \"%s\"\n", mode);
    return -1;
  }

  switch(mode[0])
  {
    case '8': cbits = CS8;
              break;
    case '7': cbits = CS7;
              break;
    case '6': cbits = CS6;
              break;
    case '5': cbits = CS5;
              break;
    default : printf("invalid number of data-bits '%c'\n", mode[0]);
              return -1;
              break;
  }

  switch(mode[1])
  {
    case 'N':
    case 'n': cpar = 0;
              ipar = IGNPAR;
              break;
    case 'E':
    case 'e': cpar = PARENB;
              ipar = INPCK;
              break;
    case 'O':
    case 'o': cpar = (PARENB | PARODD);
              ipar = INPCK;
              break;
    default : printf("invalid parity '%c'\n", mode[1]);
              return -1;
              break;
  }

  switch(mode[2])
  {
    case '1': bstop = 0;
              break;
    case '2': bstop = CSTOPB;
              break;
    default : printf("invalid number of stop bits '%c'\n", mode[2]);
              return -1;
              break;
  }

/*
http://pubs.opengroup.org/onlinepubs/7908799/xsh/termios.h.html

http://man7.org/linux/man-pages/man3/termios.3.html
*/

  *handle = open(comport, O_RDWR | O_NOCTTY | O_NDELAY);
  if(*handle==-1)
  {
    perror("unable to open comport ");
    return -1;
  }

/*  error = tcgetattr(handle, old_port_settings + comport_number);
  if(error==-1)
  {
    close(handle);
    perror("unable to read portsettings ");
    return -1;
  }
  */
  memset(&new_port_settings, 0, sizeof(new_port_settings));  /* clear the new struct */

  new_port_settings.c_cflag = cbits | cpar | bstop | CLOCAL | CREAD;
  new_port_settings.c_iflag = ipar;
  new_port_settings.c_oflag = 0;
  new_port_settings.c_lflag = 0;
  new_port_settings.c_cc[VMIN] = 0;      /* block untill n bytes are received */
  new_port_settings.c_cc[VTIME] = 0;     /* block untill a timer expires (n * 100 mSec.) */

  cfsetispeed(&new_port_settings, baudr);
  cfsetospeed(&new_port_settings, baudr);

  error = tcsetattr(*handle, TCSANOW, &new_port_settings);
  if(error==-1)
  {
    close(*handle);
    perror("unable to adjust portsettings ");
    return -1;
  }

  if(ioctl(*handle, TIOCMGET, &status) == -1)
  {
    perror("unable to get portstatus");
    return -1;
  }

  status |= TIOCM_DTR;    /* turn on DTR */
  status |= TIOCM_RTS;    /* turn on RTS */

  if(ioctl(*handle, TIOCMSET, &status) == -1)
  {
    perror("unable to set portstatus");
    return -1;
  }

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, %r13
movq %rdx, %r15
movq %rdi, %r14
cmpl $0x32, %esi
je 0x8868
cmpl $0x3d0900, %esi         # imm = 0x3D0900
je 0x887c
cmpl $0x6e, %esi
je 0x8878
cmpl $0x86, %esi
je 0x87bd
cmpl $0x96, %esi
je 0x8883
cmpl $0xc8, %esi
je 0x87fd
cmpl $0x12c, %esi            # imm = 0x12C
je 0x8804
cmpl $0x258, %esi            # imm = 0x258
je 0x8874
cmpl $0x4b0, %esi            # imm = 0x4B0
je 0x886c
cmpl $0x708, %esi            # imm = 0x708
je 0x8844
cmpl $0x960, %esi            # imm = 0x960
je 0x8839
cmpl $0x12c0, %esi           # imm = 0x12C0
je 0x8870
cmpl $0x2580, %esi           # imm = 0x2580
je 0x8856
cmpl $0x4b00, %esi           # imm = 0x4B00
je 0x8835
cmpl $0x9600, %esi           # imm = 0x9600
je 0x87c4
cmpl $0xe100, %esi           # imm = 0xE100
je 0x880f
cmpl $0x1c200, %esi          # imm = 0x1C200
je 0x87cb
cmpl $0x38400, %esi          # imm = 0x38400
je 0x8848
cmpl $0x70800, %esi          # imm = 0x70800
je 0x87e9
cmpl $0x7a120, %esi          # imm = 0x7A120
je 0x87d5
cmpl $0x8ca00, %esi          # imm = 0x8CA00
je 0x885a
cmpl $0xe1000, %esi          # imm = 0xE1000
je 0x87f3
cmpl $0xf4240, %esi          # imm = 0xF4240
je 0x8861
cmpl $0x119400, %esi         # imm = 0x119400
je 0x881d
cmpl $0x16e360, %esi         # imm = 0x16E360
je 0x884f
cmpl $0x1e8480, %esi         # imm = 0x1E8480
je 0x883d
cmpl $0x2625a0, %esi         # imm = 0x2625A0
je 0x87df
cmpl $0x2dc6c0, %esi         # imm = 0x2DC6C0
je 0x8816
cmpl $0x3567e0, %esi         # imm = 0x3567E0
je 0x8808
cmpl $0x4b, %esi
jne 0x8824
pushq $0x2
jmp 0x8885
pushq $0x4
jmp 0x8885
pushq $0xf
jmp 0x8885
movl $0x1002, %ebp           # imm = 0x1002
jmp 0x8886
movl $0x1005, %ebp           # imm = 0x1005
jmp 0x8886
movl $0x100c, %ebp           # imm = 0x100C
jmp 0x8886
movl $0x1004, %ebp           # imm = 0x1004
jmp 0x8886
movl $0x1007, %ebp           # imm = 0x1007
jmp 0x8886
pushq $0x6
jmp 0x8885
pushq $0x7
jmp 0x8885
movl $0x100e, %ebp           # imm = 0x100E
jmp 0x8886
movl $0x1001, %ebp           # imm = 0x1001
jmp 0x8886
movl $0x100d, %ebp           # imm = 0x100D
jmp 0x8886
movl $0x1009, %ebp           # imm = 0x1009
jmp 0x8886
leaq 0x6401(%rip), %rdi      # 0xec2c
callq 0x3460
jmp 0x8a31
pushq $0xe
jmp 0x8885
pushq $0xb
jmp 0x8885
movl $0x100b, %ebp           # imm = 0x100B
jmp 0x8886
pushq $0xa
jmp 0x8885
movl $0x1003, %ebp           # imm = 0x1003
jmp 0x8886
movl $0x100a, %ebp           # imm = 0x100A
jmp 0x8886
pushq $0xd
jmp 0x8885
movl $0x1006, %ebp           # imm = 0x1006
jmp 0x8886
movl $0x1008, %ebp           # imm = 0x1008
jmp 0x8886
pushq $0x1
jmp 0x8885
pushq $0x9
jmp 0x8885
pushq $0xc
jmp 0x8885
pushq $0x8
jmp 0x8885
pushq $0x3
jmp 0x8885
movl $0x100f, %ebp           # imm = 0x100F
jmp 0x8886
pushq $0x5
popq %rbp
movq %r15, %rdi
callq 0x3120
cmpq $0x3, %rax
jne 0x88f1
movsbl (%r15), %esi
leal -0x35(%rsi), %r12d
cmpb $0x4, %r12b
jae 0x8907
movzbl 0x1(%r15), %eax
leal -0x45(%rax), %ecx
cmpl $0x2a, %ecx
ja 0x8a20
movabsq $0x100000001, %rdx      # imm = 0x100000001
btq %rcx, %rdx
jb 0x891a
movabsq $0x20000000200, %rdx    # imm = 0x20000000200
btq %rcx, %rdx
jb 0x8913
movabsq $0x40000000400, %rdx    # imm = 0x40000000400
btq %rcx, %rdx
jae 0x8a20
pushq $0x10
popq %rcx
movl $0x300, %ebx            # imm = 0x300
jmp 0x8922
leaq 0x625f(%rip), %rdi      # 0xeb57
movq %r15, %rsi
xorl %eax, %eax
callq 0x3050
jmp 0x8a31
leaq 0x625c(%rip), %rdi      # 0xeb6a
jmp 0x8a2a
pushq $0x4
popq %rcx
xorl %ebx, %ebx
jmp 0x8922
pushq $0x10
popq %rcx
movl $0x100, %ebx            # imm = 0x100
movzbl 0x2(%r15), %eax
cmpl $0x31, %eax
je 0x893f
cmpl $0x32, %eax
jne 0x8a14
movl %ecx, 0x4(%rsp)
pushq $0x40
popq %r15
jmp 0x8946
movl %ecx, 0x4(%rsp)
xorl %r15d, %r15d
movq %r14, %rdi
movl $0x902, %esi            # imm = 0x902
xorl %eax, %eax
callq 0x31d0
movl %eax, (%r13)
cmpl $-0x1, %eax
je 0x8a45
shlb $0x4, %r12b
movzbl %r12b, %eax
orl %eax, %ebx
xorps %xmm0, %xmm0
leaq 0xc(%rsp), %r14
movups %xmm0, 0x10(%r14)
movups %xmm0, 0x2c(%r14)
movups %xmm0, 0x20(%r14)
leal 0x880(%r15,%rbx), %eax
movl %eax, 0x8(%r14)
movl 0x4(%rsp), %eax
movl %eax, (%r14)
andl $0x0, 0x4(%r14)
andl $0x0, 0xc(%r14)
movb $0x0, 0x16(%r14)
movq %r14, %rdi
movl %ebp, %esi
callq 0x3190
movq %r14, %rdi
movl %ebp, %esi
callq 0x32e0
movl (%r13), %edi
xorl %esi, %esi
movq %r14, %rdx
callq 0x3360
movl %eax, 0xbfe3(%rip)      # 0x149b0
movl (%r13), %edi
cmpl $-0x1, %eax
je 0x8a4e
xorl %ebp, %ebp
leaq 0x8(%rsp), %rdx
movl $0x5415, %esi           # imm = 0x5415
xorl %eax, %eax
callq 0x3410
cmpl $-0x1, %eax
je 0x8a5c
leaq 0x8(%rsp), %rdx
orb $0x6, (%rdx)
movl (%r13), %edi
movl $0x5418, %esi           # imm = 0x5418
xorl %eax, %eax
callq 0x3410
cmpl $-0x1, %eax
jne 0x8a34
leaq 0x6201(%rip), %rdi      # 0xec13
jmp 0x8a63
movsbl %al, %esi
leaq 0x6183(%rip), %rdi      # 0xeba1
jmp 0x8a2a
movsbl %al, %esi
leaq 0x6162(%rip), %rdi      # 0xeb8c
xorl %eax, %eax
callq 0x3050
pushq $-0x1
popq %rbp
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6177(%rip), %rdi      # 0xebc3
jmp 0x8a63
callq 0x3560
leaq 0x6181(%rip), %rdi      # 0xebdb
jmp 0x8a63
leaq 0x6197(%rip), %rdi      # 0xebfa
callq 0x3210
jmp 0x8a31