// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movw %si, %ax
movq %rdi, -0x10(%rbp)
movw %ax, -0x12(%rbp)
movq -0x10(%rbp), %rdi
movl $0x902, %esi            # imm = 0x902
movb $0x0, %al
callq 0x2120
movl %eax, 0x3d1b(%rip)      # 0x81d4
cmpl $-0x1, 0x3d14(%rip)     # 0x81d4
jne 0x4524
movq 0x3b27(%rip), %rdi      # 0x7ff0
leaq 0xde5(%rip), %rsi       # 0x52b5
callq 0x2170
movq %rax, %rdi
movq -0x10(%rbp), %rsi
callq 0x2170
movq %rax, %rdi
leaq 0xdec(%rip), %rsi       # 0x52d7
callq 0x2170
movq %rax, -0x58(%rbp)
callq 0x2030
movl (%rax), %edi
callq 0x2070
movq -0x58(%rbp), %rdi
movq %rax, %rsi
callq 0x2170
movq %rax, %rdi
movq 0x3aaa(%rip), %rsi      # 0x7fc0
callq 0x21a0
movb $0x0, -0x1(%rbp)
jmp 0x4702
movl 0x3caa(%rip), %edi      # 0x81d4
callq 0x22e0
cmpl $0x0, %eax
jne 0x456b
movq 0x3ab5(%rip), %rdi      # 0x7ff0
leaq 0xd9f(%rip), %rsi       # 0x52e1
callq 0x2170
movq %rax, %rdi
movq -0x10(%rbp), %rsi
callq 0x2170
movq %rax, %rdi
movq 0x3a63(%rip), %rsi      # 0x7fc0
callq 0x21a0
movb $0x0, -0x1(%rbp)
jmp 0x4702
movl 0x3c63(%rip), %edi      # 0x81d4
leaq -0x50(%rbp), %rsi
callq 0x21d0
movw -0x12(%rbp), %ax
subw $0x9600, %ax            # imm = 0x9600
jne 0x4600
jmp 0x4586
leaq -0x50(%rbp), %rdi
movl $0xf, %esi
callq 0x2100
cmpl $0x0, %eax
jl 0x45ac
leaq -0x50(%rbp), %rdi
movl $0xf, %esi
callq 0x21c0
cmpl $0x0, %eax
jge 0x45fe
movq 0x3a3d(%rip), %rdi      # 0x7ff0
leaq 0xd65(%rip), %rsi       # 0x531f
callq 0x2170
movq %rax, %rdi
movq -0x10(%rbp), %rsi
callq 0x2170
movq %rax, %rdi
leaq 0xd74(%rip), %rsi       # 0x5349
callq 0x2170
movq %rax, %rdi
movzwl -0x12(%rbp), %esi
callq 0x22c0
movq %rax, %rdi
movq 0x39d0(%rip), %rsi      # 0x7fc0
callq 0x21a0
movb $0x0, -0x1(%rbp)
jmp 0x4702
jmp 0x4652
movq 0x39e9(%rip), %rdi      # 0x7ff0
leaq 0xd48(%rip), %rsi       # 0x5356
callq 0x2170
movq %rax, %rdi
movq -0x10(%rbp), %rsi
callq 0x2170
movq %rax, %rdi
leaq 0xd20(%rip), %rsi       # 0x5349
callq 0x2170
movq %rax, %rdi
movzwl -0x12(%rbp), %esi
callq 0x22c0
movq %rax, %rdi
movq 0x397c(%rip), %rsi      # 0x7fc0
callq 0x21a0
movb $0x0, -0x1(%rbp)
jmp 0x4702
movl -0x50(%rbp), %eax
andl $0xfffffe84, %eax       # imm = 0xFFFFFE84
movl %eax, -0x50(%rbp)
movl -0x50(%rbp), %eax
andl $0xffffe3ff, %eax       # imm = 0xFFFFE3FF
movl %eax, -0x50(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x44(%rbp), %eax
andl $0xffff7fb4, %eax       # imm = 0xFFFF7FB4
movl %eax, -0x44(%rbp)
movl -0x48(%rbp), %eax
andl $0xfffffecf, %eax       # imm = 0xFFFFFECF
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
orl $0x30, %eax
movl %eax, -0x48(%rbp)
movb $0x1, -0x39(%rbp)
movb $0x0, -0x3a(%rbp)
movl 0x3b38(%rip), %edi      # 0x81d4
movl $0x2, %esi
leaq -0x50(%rbp), %rdx
callq 0x21f0
cmpl $0x0, %eax
jge 0x46fe
movq 0x393a(%rip), %rdi      # 0x7ff0
leaq 0xcb9(%rip), %rsi       # 0x5376
callq 0x2170
movq %rax, %rdi
movq -0x10(%rbp), %rsi
callq 0x2170
movq %rax, %rdi
leaq 0xc71(%rip), %rsi       # 0x5349
callq 0x2170
movq %rax, %rdi
movzwl -0x12(%rbp), %esi
callq 0x22c0
movq %rax, %rdi
movq 0x38cd(%rip), %rsi      # 0x7fc0
callq 0x21a0
movb $0x0, -0x1(%rbp)
jmp 0x4702
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)