// File: /workspace/github_c_makefile_star2_O1/chipkin[P]Linux-HighSpeedSerialMonitor/linuxMSTPFunctions.c
// Function: serialConnect(char const*, unsigned short)
bool serialConnect(const char* path, uint16_t baudRate)
{
    struct termios portSettings;

    serailFD = open(path, O_RDWR | O_NOCTTY | O_NDELAY);
    if (serailFD == -1) {
        std::cerr << "Error: Could not open port. path=" << path << ", Error: " << strerror(errno) << std::endl;
        return false;
    }

    if (!isatty(serailFD)) {
        std::cerr << "Error: File descripter is not pointing at a TTY device. path=" << path << std::endl;
        return false;
    }

    // Set the port options
    tcgetattr(serailFD, &portSettings); /* save current port settings */
    switch (baudRate) {
        case 38400:
            if (cfsetispeed(&portSettings, B38400) < 0 || cfsetospeed(&portSettings, B38400) < 0) {
                std::cerr << "Error: Could not set the Baud rate. path=" << path << ", baudRate: " << baudRate << std::endl;
                return false;
            }
            break;

        default:
            std::cerr << "Error: Unknown baud rate. path=" << path << ", baudRate: " << baudRate << std::endl;
            return false;
    }

    //
    // Input flags - Turn off input processing
    //
    // convert break to null byte, no CR to NL translation,
    // no NL to CR translation, don't mark parity errors or breaks
    // no input parity check, don't strip high bit off,
    // no XON/XOFF software flow control
    //
    portSettings.c_iflag &= ~(IGNBRK | BRKINT | ICRNL | INLCR | PARMRK | INPCK | ISTRIP);
    // No software flow control
    portSettings.c_iflag &= ~(IXON | IXOFF | IXANY);

    //
    // Output flags - Turn off output processing
    //
    // no CR to NL translation, no NL to CR-NL translation,
    // no NL to CR translation, no column 0 CR suppression,
    // no Ctrl-D suppression, no fill characters, no case mapping,
    // no local output processing
    //
    // config.c_oflag &= ~(OCRNL | ONLCR | ONLRET |
    //                     ONOCR | ONOEOT| OFILL | OLCUC | OPOST);
    portSettings.c_oflag = 0;

    //
    // No line processing
    //
    // echo off, echo newline off, canonical mode off,
    // extended input processing off, signal chars off
    //
    portSettings.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);

    //
    // Turn off character processing
    //
    // clear current char size mask, no parity checking,
    // no output processing, force 8 bit input
    //
    portSettings.c_cflag &= ~(CSIZE | PARENB);
    portSettings.c_cflag |= CS8;

    //
    // One input byte is enough to return from read()
    // Inter-character timer off
    //
    portSettings.c_cc[VMIN] = 1;
    portSettings.c_cc[VTIME] = 0;

    // Enable reading
    // portSettings.c_cflag |= (CLOCAL | CREAD);

    if (tcsetattr(serailFD, TCSAFLUSH, &portSettings) < 0) {
        std::cerr << "Error: Could not set the port settings. path=" << path << ", baudRate: " << baudRate << std::endl;
        return false;
    }
    /*
    if (tcflush(serailFD, TCSAFLUSH, &portSettings) <= 0) {
        std::cerr << "Error: Could not inital flush the serial port. path=" << path << ", baudRate: " << baudRate << std::endl;
        return false;
    }
    */

    return true;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x40, %rsp
movl %esi, %ebx
movq %rdi, %r14
movl $0x902, %esi            # imm = 0x902
xorl %eax, %eax
callq 0x2120
movl %eax, 0x2cd0(%rip)      # 0x619c
cmpl $-0x1, %eax
je 0x3592
movl %eax, %edi
callq 0x22b0
testl %eax, %eax
je 0x35ce
movl 0x2cb2(%rip), %edi      # 0x619c
leaq 0x4(%rsp), %rsi
callq 0x21c0
movzwl %bx, %ebx
cmpl $0x9600, %ebx           # imm = 0x9600
jne 0x35f0
leaq 0x4(%rsp), %rdi
movl $0xf, %esi
callq 0x20f0
testl %eax, %eax
js 0x3689
leaq 0x4(%rsp), %rdi
movl $0xf, %esi
callq 0x21b0
testl %eax, %eax
js 0x3689
leaq 0x4(%rsp), %rdx
andl $0xffffe284, (%rdx)     # imm = 0xFFFFE284
movl $0x0, 0x4(%rdx)
andl $0xffff7fb4, 0xc(%rdx)  # imm = 0xFFFF7FB4
movl $0xfffffecf, %eax       # imm = 0xFFFFFECF
andl 0x8(%rdx), %eax
orl $0x30, %eax
movl %eax, 0x8(%rdx)
movw $0x100, 0x16(%rdx)      # imm = 0x100
movl 0x2c38(%rip), %edi      # 0x619c
movl $0x2, %esi
callq 0x21e0
movl %eax, %ecx
movb $0x1, %al
testl %ecx, %ecx
jns 0x372f
movq 0x2a6f(%rip), %r15      # 0x5ff0
leaq 0xe67(%rip), %rsi       # 0x43ef
movl $0x2d, %edx
jmp 0x369c
movq 0x2a57(%rip), %rbx      # 0x5ff0
leaq 0xd8e(%rip), %rsi       # 0x432e
movl $0x21, %edx
movq %rbx, %rdi
callq 0x21d0
testq %r14, %r14
je 0x3608
movq %r14, %rdi
callq 0x20b0
movq 0x2a2f(%rip), %rdi      # 0x5ff0
movq %r14, %rsi
movq %rax, %rdx
callq 0x21d0
jmp 0x361f
movq 0x2a1b(%rip), %rbx      # 0x5ff0
leaq 0xd7e(%rip), %rsi       # 0x435a
movl $0x3d, %edx
movq %rbx, %rdi
callq 0x21d0
testq %r14, %r14
jne 0x364e
jmp 0x3669
movq 0x29f9(%rip), %r15      # 0x5ff0
leaq 0xdd1(%rip), %rsi       # 0x43cf
movl $0x1f, %edx
jmp 0x369c
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x2290
movq 0x29ca(%rip), %rbx      # 0x5ff0
leaq 0xd23(%rip), %rsi       # 0x4350
movl $0x9, %edx
movq %rbx, %rdi
callq 0x21d0
callq 0x2040
movl (%rax), %edi
callq 0x2080
testq %rax, %rax
je 0x3669
movq %rax, %r14
movq %r14, %rdi
callq 0x20b0
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x21d0
jmp 0x3706
movq 0x2980(%rip), %rbx      # 0x5ff0
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x2290
jmp 0x3706
movq 0x2960(%rip), %r15      # 0x5ff0
leaq 0xd01(%rip), %rsi       # 0x4398
movl $0x29, %edx
movq %r15, %rdi
callq 0x21d0
testq %r14, %r14
je 0x36c5
movq %r14, %rdi
callq 0x20b0
movq 0x2938(%rip), %rdi      # 0x5ff0
movq %r14, %rsi
movq %rax, %rdx
callq 0x21d0
jmp 0x36dd
movq (%r15), %rax
movq -0x18(%rax), %rax
leaq (%r15,%rax), %rdi
movl 0x20(%r15,%rax), %esi
orl $0x1, %esi
callq 0x2290
movq 0x290c(%rip), %r14      # 0x5ff0
leaq 0xcd7(%rip), %rsi       # 0x43c2
movl $0xc, %edx
movq %r14, %rdi
callq 0x21d0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2100
movq %rax, %rbx
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2110
xorl %eax, %eax
addq $0x40, %rsp
popq %rbx
popq %r14
popq %r15
retq