// File: /workspace/github_cpp_makefile_star2_O3/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 $0xe0ff, %esi           # imm = 0xE0FF
jg 0x9bb1
cmpl $0x257, %esi            # imm = 0x257
jle 0x9bf3
cmpl $0x12bf, %esi           # imm = 0x12BF
jg 0x9c67
cmpl $0x707, %esi            # imm = 0x707
jg 0x9d5a
cmpl $0x258, %esi            # imm = 0x258
je 0x9df3
cmpl $0x4b0, %esi            # imm = 0x4B0
jne 0xa012
movl $0x9, %ebp
jmp 0x9e45
cmpl $0xf423f, %esi          # imm = 0xF423F
jle 0x9c2d
cmpl $0x26259f, %esi         # imm = 0x26259F
jg 0x9c95
cmpl $0x16e35f, %esi         # imm = 0x16E35F
jg 0x9d7c
cmpl $0xf4240, %esi          # imm = 0xF4240
je 0x9dfa
cmpl $0x119400, %esi         # imm = 0x119400
jne 0xa012
movl $0x1009, %ebp           # imm = 0x1009
jmp 0x9e45
cmpl $0x85, %esi
jle 0x9cc3
cmpl $0xc7, %esi
jg 0x9d16
cmpl $0x86, %esi
je 0x9dd7
cmpl $0x96, %esi
jne 0xa012
movl $0x5, %ebp
jmp 0x9e45
cmpl $0x707ff, %esi          # imm = 0x707FF
jle 0x9ce8
cmpl $0x8c9ff, %esi          # imm = 0x8C9FF
jg 0x9d38
cmpl $0x70800, %esi          # imm = 0x70800
je 0x9dde
cmpl $0x7a120, %esi          # imm = 0x7A120
jne 0xa012
movl $0x1005, %ebp           # imm = 0x1005
jmp 0x9e45
cmpl $0x4aff, %esi           # imm = 0x4AFF
jg 0x9d9e
cmpl $0x12c0, %esi           # imm = 0x12C0
je 0x9e01
cmpl $0x2580, %esi           # imm = 0x2580
jne 0xa012
movl $0xd, %ebp
jmp 0x9e45
cmpl $0x3567df, %esi         # imm = 0x3567DF
jg 0x9dbc
cmpl $0x2625a0, %esi         # imm = 0x2625A0
je 0x9e08
cmpl $0x2dc6c0, %esi         # imm = 0x2DC6C0
jne 0xa012
movl $0x100d, %ebp           # imm = 0x100D
jmp 0x9e45
cmpl $0x32, %esi
je 0x9e39
cmpl $0x4b, %esi
je 0x9e2b
cmpl $0x6e, %esi
jne 0xa012
movl $0x3, %ebp
jmp 0x9e45
cmpl $0xe100, %esi           # imm = 0xE100
je 0x9e40
cmpl $0x1c200, %esi          # imm = 0x1C200
je 0x9e32
cmpl $0x38400, %esi          # imm = 0x38400
jne 0xa012
movl $0x1003, %ebp           # imm = 0x1003
jmp 0x9e45
cmpl $0xc8, %esi
je 0x9de5
cmpl $0x12c, %esi            # imm = 0x12C
jne 0xa012
movl $0x7, %ebp
jmp 0x9e45
cmpl $0x8ca00, %esi          # imm = 0x8CA00
je 0x9dec
cmpl $0xe1000, %esi          # imm = 0xE1000
jne 0xa012
movl $0x1007, %ebp           # imm = 0x1007
jmp 0x9e45
cmpl $0x708, %esi            # imm = 0x708
je 0x9e0f
cmpl $0x960, %esi            # imm = 0x960
jne 0xa012
movl $0xb, %ebp
jmp 0x9e45
cmpl $0x16e360, %esi         # imm = 0x16E360
je 0x9e16
cmpl $0x1e8480, %esi         # imm = 0x1E8480
jne 0xa012
movl $0x100b, %ebp           # imm = 0x100B
jmp 0x9e45
cmpl $0x4b00, %esi           # imm = 0x4B00
je 0x9e1d
cmpl $0x9600, %esi           # imm = 0x9600
jne 0xa012
movl $0xf, %ebp
jmp 0x9e45
cmpl $0x3567e0, %esi         # imm = 0x3567E0
je 0x9e24
cmpl $0x3d0900, %esi         # imm = 0x3D0900
jne 0xa012
movl $0x100f, %ebp           # imm = 0x100F
jmp 0x9e45
movl $0x4, %ebp
jmp 0x9e45
movl $0x1004, %ebp           # imm = 0x1004
jmp 0x9e45
movl $0x6, %ebp
jmp 0x9e45
movl $0x1006, %ebp           # imm = 0x1006
jmp 0x9e45
movl $0x8, %ebp
jmp 0x9e45
movl $0x1008, %ebp           # imm = 0x1008
jmp 0x9e45
movl $0xc, %ebp
jmp 0x9e45
movl $0x100c, %ebp           # imm = 0x100C
jmp 0x9e45
movl $0xa, %ebp
jmp 0x9e45
movl $0x100a, %ebp           # imm = 0x100A
jmp 0x9e45
movl $0xe, %ebp
jmp 0x9e45
movl $0x100e, %ebp           # imm = 0x100E
jmp 0x9e45
movl $0x2, %ebp
jmp 0x9e45
movl $0x1002, %ebp           # imm = 0x1002
jmp 0x9e45
movl $0x1, %ebp
jmp 0x9e45
movl $0x1001, %ebp           # imm = 0x1001
movq %r15, %rdi
callq 0x3130
cmpq $0x3, %rax
jne 0x9eb2
movsbl (%r15), %esi
leal -0x35(%rsi), %r12d
cmpb $0x4, %r12b
jae 0x9ec8
movzbl 0x1(%r15), %eax
leal -0x45(%rax), %ecx
cmpl $0x2a, %ecx
ja 0x9feb
movabsq $0x100000001, %rdx      # imm = 0x100000001
btq %rcx, %rdx
jb 0x9edd
movabsq $0x20000000200, %rdx    # imm = 0x20000000200
btq %rcx, %rdx
jb 0x9ed4
movabsq $0x40000000400, %rdx    # imm = 0x40000000400
btq %rcx, %rdx
jae 0x9feb
movl $0x10, %ecx
movl $0x300, %ebx            # imm = 0x300
jmp 0x9ee7
leaq 0x6cc1(%rip), %rdi      # 0x10b7a
movq %r15, %rsi
xorl %eax, %eax
callq 0x3060
jmp 0x9ffc
leaq 0x6cbe(%rip), %rdi      # 0x10b8d
jmp 0x9ff5
movl $0x4, %ecx
xorl %ebx, %ebx
jmp 0x9ee7
movl $0x10, %ecx
movl $0x100, %ebx            # imm = 0x100
movzbl 0x2(%r15), %eax
cmpl $0x31, %eax
je 0x9f06
cmpl $0x32, %eax
jne 0x9fdf
movl %ecx, 0x4(%rsp)
movl $0x40, %r15d
jmp 0x9f0d
movl %ecx, 0x4(%rsp)
xorl %r15d, %r15d
movq %r14, %rdi
movl $0x902, %esi            # imm = 0x902
xorl %eax, %eax
callq 0x3200
movl %eax, (%r13)
cmpl $-0x1, %eax
je 0xa020
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)
xorl %eax, %eax
movl %eax, 0x4(%r14)
movl %eax, 0xc(%r14)
movb $0x0, 0x16(%r14)
movq %r14, %rdi
movl %ebp, %esi
callq 0x31a0
movq %r14, %rdi
movl %ebp, %esi
callq 0x3310
movl (%r13), %edi
xorl %esi, %esi
movq %r14, %rdx
callq 0x3390
movl %eax, 0xca1c(%rip)      # 0x169b0
movl (%r13), %edi
cmpl $-0x1, %eax
je 0xa029
xorl %ebp, %ebp
leaq 0x8(%rsp), %rdx
movl $0x5415, %esi           # imm = 0x5415
xorl %eax, %eax
callq 0x3420
cmpl $-0x1, %eax
je 0xa037
leaq 0x8(%rsp), %rdx
orb $0x6, (%rdx)
movl (%r13), %edi
movl $0x5418, %esi           # imm = 0x5418
xorl %eax, %eax
callq 0x3420
cmpl $-0x1, %eax
jne 0xa001
leaq 0x6c59(%rip), %rdi      # 0x10c36
jmp 0xa03e
movsbl %al, %esi
leaq 0x6bdb(%rip), %rdi      # 0x10bc4
jmp 0x9ff5
movsbl %al, %esi
leaq 0x6bba(%rip), %rdi      # 0x10baf
xorl %eax, %eax
callq 0x3060
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6c36(%rip), %rdi      # 0x10c4f
callq 0x3470
jmp 0x9ffc
leaq 0x6bbf(%rip), %rdi      # 0x10be6
jmp 0xa03e
callq 0x3570
leaq 0x6bc9(%rip), %rdi      # 0x10bfe
jmp 0xa03e
leaq 0x6bdf(%rip), %rdi      # 0x10c1d
callq 0x3230
jmp 0x9ffc