// File: /workspace/github_c_makefile_star2_O1/chipkin[P]Linux-HighSpeedSerialMonitor/main.cpp
// Function: main
int main(int argc, char** argv)
{
    std::cout << "FYI: High speed serail monitor. Version: " << APPLICATION_VERSION << "." << CI_PIPELINE_IID << std::endl;

    // Set up the output stream.
    ComposeStream outputStream;
    outputStream.linkStream(std::cout);

    // Open a log file
    std::ofstream outfile(CONFIGURATION_LOG_FILE, std::ios_base::app);
    outputStream.linkStream(outfile);

    // Check for command line parmeters
    uint32_t baudRate = DEFAULT_SERIAL_BAUDRATE;
    static const uint32_t MAX_COMPORT_STRING_LENGTH = 100;
    char comport[MAX_COMPORT_STRING_LENGTH];
    strncpy(comport, DEFAULT_SERIAL_PORT, MAX_COMPORT_STRING_LENGTH);

    // Check for help flag.
    for (uint32_t i = 1; i < argc; i++) {
        if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-help") == 0) {
            printHelp();
            return EXIT_SUCCESS;
        }
    }

    // Extract the command line paramters.
    if (argc == 1) {
        outputStream << "FYI: No command line parameters set. Using defaults. " << std::endl;
    }
    if (argc >= 2) {
        strncpy(comport, argv[1], 100);
    }
    if (argc >= 3) {
        baudRate = atoi(argv[2]);
    }

    // Open the serial port
    outputStream
        << "FYI: Connecting to serial port. SerialPort=" << comport << ", BaudRate=" << baudRate << std::endl;
    if (!serialConnect(comport, baudRate)) {
        outputStream << "Error: Could not connect to the serial port" << std::endl;
        return EXIT_FAILURE;
    }
    outputStream << "FYI: Connected to serial port" << std::endl
                 << std::endl;

    const uint32_t PRINT_WIDTH_TIME = 12;
    const uint32_t PRINT_WIDTH_CHAR = 10;
    const uint32_t PRINT_WIDTH_HEX = 10;

    // Print the headers
    outputStream << "|" << std::setfill(' ') << std::setw(PRINT_WIDTH_TIME) << " Time (ms) ";
    outputStream << "|" << std::setfill(' ') << std::setw(PRINT_WIDTH_CHAR) << " CHAR ";
    outputStream << "|" << std::setfill(' ') << std::setw(PRINT_WIDTH_HEX) << " HEX ";
    outputStream << "|" << std::endl;
    outputStream << "* " << std::setfill('-') << std::setw(PRINT_WIDTH_TIME + 1);
    outputStream << " * " << std::setfill('-') << std::setw(PRINT_WIDTH_CHAR + 1);
    outputStream << " * " << std::setfill('-') << std::setw(PRINT_WIDTH_HEX + 1);
    outputStream << " * " << std::endl;

    mstp_TimerReset();
    uint32_t microsecond;
    uint8_t buffer;
    for (;;) {
        if (mstp_RecvByte(&buffer)) {
            microsecond = mstp_TimerDifference();
            mstp_TimerReset();

            outputStream << "|" << std::setfill(' ') << std::setw(PRINT_WIDTH_TIME - 1) << std::fixed << std::setprecision(3) << (float)microsecond / 1000;
            outputStream << " |" << std::setfill(' ') << std::setw(PRINT_WIDTH_CHAR - 1) << buffer;
            outputStream << " |" << std::setfill(' ') << std::setw(PRINT_WIDTH_HEX - 3) << " " << std::hex << std::setw(2) << std::setfill('0') << std::uppercase << buffer;
            outputStream << " |" << std::endl;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3e8, %rsp            # imm = 0x3E8
movq %rsi, 0x10(%rsp)
movl %edi, %ebp
movq 0x390f(%rip), %r14      # 0x5fd0
leaq 0x1a9b(%rip), %rsi      # 0x4163
movl $0x29, %edx
movq %r14, %rdi
callq 0x21d0
leaq 0x1b94(%rip), %rsi      # 0x4270
movl $0x5, %edx
movq %r14, %rdi
callq 0x21d0
leaq 0x1a9d(%rip), %rsi      # 0x418d
movl $0x1, %edx
movq %r14, %rdi
callq 0x21d0
movq %r14, %rdi
xorl %esi, %esi
callq 0x22a0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq %r15, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2110
leaq 0x18(%rsp), %rdi
callq 0x2fb0
movq %r14, %rdi
callq 0x2110
leaq 0x20(%rsp), %r15
movq (%r14), %rax
movq -0x18(%rax), %rax
movq 0xe8(%r14,%rax), %rsi
movq %r15, %rdi
callq 0x3346
leaq 0x1b2a(%rip), %rsi      # 0x4290
leaq 0x1e8(%rsp), %rdi
movl $0x1, %edx
callq 0x21a0
leaq 0x1e8(%rsp), %rdi
callq 0x2110
movq 0x1e8(%rsp), %rax
movq -0x18(%rax), %rax
movq 0x2d0(%rsp,%rax), %rsi
movq %r15, %rdi
callq 0x3346
leaq 0x1b21(%rip), %rsi      # 0x42c9
leaq 0x180(%rsp), %rdi
movl $0x64, %edx
callq 0x2140
movl %ebp, %ebx
cmpl $0x2, %ebp
jb 0x280e
movl %ebx, %r13d
movl $0x1, %ebp
leaq 0x19bf(%rip), %r14      # 0x418f
leaq 0x19bb(%rip), %r15      # 0x4192
movq 0x10(%rsp), %rax
movq (%rax,%rbp,8), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x2200
testl %eax, %eax
je 0x297a
movq %r12, %rdi
movq %r15, %rsi
callq 0x2200
testl %eax, %eax
je 0x297a
incq %rbp
cmpq %rbp, %r13
jne 0x27d7
cmpl $0x1, %ebx
jne 0x2857
leaq 0x197e(%rip), %rsi      # 0x4198
leaq 0x18(%rsp), %r14
movl $0x35, %edx
movq %r14, %rdi
callq 0x21d0
movq 0x18(%rsp), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
leaq 0x18(%rsp), %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2110
cmpl $0x2, %ebx
jl 0x2877
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rsi
leaq 0x180(%rsp), %rdi
movl $0x64, %edx
callq 0x20d0
movl $0x9600, %r14d          # imm = 0x9600
cmpl $0x3, %ebx
jl 0x2893
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x2250
movl %eax, %r14d
leaq 0x1934(%rip), %rsi      # 0x41ce
leaq 0x18(%rsp), %rdi
movl $0x2b, %edx
callq 0x21d0
leaq 0x180(%rsp), %rbx
movq %rbx, %rdi
callq 0x20b0
leaq 0x18(%rsp), %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x21d0
leaq 0x192a(%rip), %rsi      # 0x41fa
leaq 0x18(%rsp), %rdi
movl $0xb, %edx
callq 0x21d0
movl %r14d, %esi
leaq 0x18(%rsp), %rdi
callq 0x2100
movq %rax, %rbx
movq (%rax), %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
movzwl %r14w, %esi
leaq 0x180(%rsp), %rdi
callq 0x34ac
testb %al, %al
jne 0x2a29
leaq 0x18d0(%rip), %rsi      # 0x4206
leaq 0x18(%rsp), %rbx
movl $0x2b, %edx
movq %rbx, %rdi
callq 0x21d0
movq 0x18(%rsp), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
leaq 0x18(%rsp), %rdi
callq 0x2030
movl $0x1, %ebx
movq %rax, %rdi
callq 0x2110
jmp 0x2981
xorl %ebx, %ebx
callq 0x244c
movq 0x3640(%rip), %rax      # 0x5fc8
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq 0x1f0(%rsp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, 0x1e8(%rsp,%rcx)
callq 0x2150
leaq 0x2e0(%rsp), %rdi
callq 0x2070
leaq 0x32a8(%rip), %rax      # 0x5c68
leaq 0x78(%rsp), %r14
movq %rax, -0x60(%r14)
leaq 0x32c0(%rip), %rax      # 0x5c90
movq %rax, (%r14)
leaq 0x333e(%rip), %rax      # 0x5d18
movq %rax, -0x58(%r14)
movq -0x18(%r14), %rdi
testq %rdi, %rdi
je 0x29f4
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x2190
movq 0x35bd(%rip), %rax      # 0x5fb8
addq $0x10, %rax
leaq 0x58(%rsp), %rdi
movq %rax, -0x38(%rdi)
callq 0x2210
movq %r14, %rdi
callq 0x2070
movl %ebx, %eax
addq $0x3e8, %rsp            # imm = 0x3E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1802(%rip), %rsi      # 0x4232
leaq 0x18(%rsp), %rdi
movl $0x1d, %edx
callq 0x21d0
leaq 0x18(%rsp), %rdi
callq 0x20c0
movq %rax, %rdi
callq 0x20c0
leaq 0x1816(%rip), %rsi      # 0x426e
leaq 0x18(%rsp), %rdi
movl $0x1, %edx
callq 0x21d0
movq 0x18(%rsp), %rax
movq -0x18(%rax), %rax
leaq (%rsp,%rax), %rbx
addq $0x18, %rbx
cmpb $0x0, 0xe1(%rbx)
jne 0x2a9b
movq %rbx, %rdi
movl $0x20, %esi
callq 0x2170
movb %al, 0xe0(%rbx)
movb $0x1, 0xe1(%rbx)
movb $0x20, 0xe0(%rbx)
leaq 0x18(%rsp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0xc, 0x28(%rsp,%rax)
leaq 0x1792(%rip), %rsi      # 0x4250
movl $0xb, %edx
callq 0x21d0
leaq 0x179f(%rip), %rsi      # 0x426e
leaq 0x18(%rsp), %rdi
movl $0x1, %edx
callq 0x21d0
movq 0x18(%rsp), %rax
movq -0x18(%rax), %rax
leaq (%rsp,%rax), %rbx
addq $0x18, %rbx
cmpb $0x0, 0xe1(%rbx)
jne 0x2b12
movq %rbx, %rdi
movl $0x20, %esi
callq 0x2170
movb %al, 0xe0(%rbx)
movb $0x1, 0xe1(%rbx)
movb $0x20, 0xe0(%rbx)
leaq 0x18(%rsp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0xa, 0x28(%rsp,%rax)
leaq 0x1727(%rip), %rsi      # 0x425c
movl $0x6, %edx
callq 0x21d0
leaq 0x1728(%rip), %rsi      # 0x426e
leaq 0x18(%rsp), %rdi
movl $0x1, %edx
callq 0x21d0
movq 0x18(%rsp), %rax
movq -0x18(%rax), %rax
leaq (%rsp,%rax), %rbx
addq $0x18, %rbx
cmpb $0x0, 0xe1(%rbx)
jne 0x2b89
movq %rbx, %rdi
movl $0x20, %esi
callq 0x2170
movb %al, 0xe0(%rbx)
movb $0x1, 0xe1(%rbx)
movb $0x20, 0xe0(%rbx)
leaq 0x18(%rsp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0xa, 0x28(%rsp,%rax)
leaq 0x16b7(%rip), %rsi      # 0x4263
movl $0x5, %edx
callq 0x21d0
leaq 0x16b1(%rip), %rsi      # 0x426e
leaq 0x18(%rsp), %rdi
movl $0x1, %edx
callq 0x21d0
leaq 0x18(%rsp), %rdi
callq 0x20c0
leaq 0x168d(%rip), %rsi      # 0x426a
leaq 0x18(%rsp), %rdi
movl $0x2, %edx
callq 0x21d0
movq 0x18(%rsp), %rax
movq -0x18(%rax), %rax
leaq (%rsp,%rax), %rbx
addq $0x18, %rbx
cmpb $0x0, 0xe1(%rbx)
jne 0x2c20
movq %rbx, %rdi
movl $0x20, %esi
callq 0x2170
movb %al, 0xe0(%rbx)
movb $0x1, 0xe1(%rbx)
movb $0x2d, 0xe0(%rbx)
leaq 0x18(%rsp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0xd, 0x28(%rsp,%rax)
leaq 0x1626(%rip), %rsi      # 0x4269
movl $0x3, %edx
callq 0x21d0
movq 0x18(%rsp), %rax
movq -0x18(%rax), %rax
leaq (%rsp,%rax), %rbx
addq $0x18, %rbx
cmpb $0x0, 0xe1(%rbx)
jne 0x2c81
movq %rbx, %rdi
movl $0x20, %esi
callq 0x2170
movb %al, 0xe0(%rbx)
movb $0x1, 0xe1(%rbx)
movb $0x2d, 0xe0(%rbx)
leaq 0x18(%rsp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0xb, 0x28(%rsp,%rax)
leaq 0x15c5(%rip), %rsi      # 0x4269
movl $0x3, %edx
callq 0x21d0
movq 0x18(%rsp), %rax
movq -0x18(%rax), %rax
leaq (%rsp,%rax), %rbx
addq $0x18, %rbx
cmpb $0x0, 0xe1(%rbx)
jne 0x2ce2
movq %rbx, %rdi
movl $0x20, %esi
callq 0x2170
movb %al, 0xe0(%rbx)
movb $0x1, 0xe1(%rbx)
movb $0x2d, 0xe0(%rbx)
leaq 0x18(%rsp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0xb, 0x28(%rsp,%rax)
leaq 0x1564(%rip), %rsi      # 0x4269
movl $0x3, %edx
callq 0x21d0
leaq 0x18(%rsp), %rdi
callq 0x20c0
callq 0x3864
leaq 0xe(%rsp), %rbx
leaq 0x18(%rsp), %r15
leaq 0x153e(%rip), %r12      # 0x426d
leaq 0xf(%rsp), %r13
movq %rbx, %rdi
callq 0x3739
testb %al, %al
je 0x2d34
callq 0x3872
movl %eax, %r14d
callq 0x3864
movl $0x1, %edx
movq %r15, %rdi
leaq 0x1512(%rip), %rsi      # 0x426e
callq 0x21d0
movq 0x18(%rsp), %rax
movq -0x18(%rax), %rax
leaq (%rsp,%rax), %rbp
addq $0x18, %rbp
cmpb $0x0, 0xe1(%rbp)
jne 0x2d95
movq %rbp, %rdi
movl $0x20, %esi
callq 0x2170
movb %al, 0xe0(%rbp)
movb $0x1, 0xe1(%rbp)
movb $0x20, 0xe0(%rbp)
movq 0x18(%rsp), %rax
movq -0x18(%rax), %rcx
movq $0xb, 0x28(%rsp,%rcx)
movq -0x18(%rax), %rcx
movl 0x30(%rsp,%rcx), %edx
movl $0xfffffefb, %esi       # imm = 0xFFFFFEFB
andl %esi, %edx
orl $0x4, %edx
movl %edx, 0x30(%rsp,%rcx)
movl %r14d, %ecx
cvtsi2ss %rcx, %xmm0
movq -0x18(%rax), %rax
movq $0x3, 0x20(%rsp,%rax)
divss 0x1223(%rip), %xmm0     # 0x4004
cvtss2sd %xmm0, %xmm0
movq %r15, %rdi
callq 0x2260
movl $0x2, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x21d0
movq 0x18(%rsp), %rax
movq -0x18(%rax), %rax
leaq (%rsp,%rax), %r14
addq $0x18, %r14
cmpb $0x0, 0xe1(%r14)
jne 0x2e34
movq %r14, %rdi
movl $0x20, %esi
callq 0x2170
movb %al, 0xe0(%r14)
movb $0x1, 0xe1(%r14)
movb $0x20, 0xe0(%r14)
movq 0x18(%rsp), %rax
movq -0x18(%rax), %rax
movq $0x9, 0x28(%rsp,%rax)
movb 0xe(%rsp), %al
movb %al, 0xf(%rsp)
movl $0x1, %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0x21d0
movl $0x2, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x21d0
movq 0x18(%rsp), %rax
movq -0x18(%rax), %rax
leaq (%rsp,%rax), %r14
addq $0x18, %r14
cmpb $0x0, 0xe1(%r14)
jne 0x2ead
movq %r14, %rdi
movl $0x20, %esi
callq 0x2170
movb %al, 0xe0(%r14)
movb $0x1, 0xe1(%r14)
movb $0x20, 0xe0(%r14)
movq 0x18(%rsp), %rax
movq -0x18(%rax), %rax
movq $0x7, 0x28(%rsp,%rax)
movl $0x1, %edx
movq %r15, %rdi
leaq 0x1384(%rip), %rsi      # 0x425a
callq 0x21d0
movq 0x18(%rsp), %rax
movq -0x18(%rax), %rcx
movl 0x30(%rsp,%rcx), %edx
andl $-0x4b, %edx
orl $0x8, %edx
movl %edx, 0x30(%rsp,%rcx)
movq -0x18(%rax), %rcx
movq $0x2, 0x28(%rsp,%rcx)
movq -0x18(%rax), %rax
leaq (%rsp,%rax), %r14
addq $0x18, %r14
cmpb $0x0, 0xe1(%r14)
jne 0x2f31
movq %r14, %rdi
movl $0x20, %esi
callq 0x2170
movb %al, 0xe0(%r14)
movb $0x1, 0xe1(%r14)
movb $0x30, 0xe0(%r14)
movq 0x18(%rsp), %rax
movq -0x18(%rax), %rax
orl $0x4000, 0x30(%rsp,%rax) # imm = 0x4000
movb 0xe(%rsp), %al
movb %al, 0xf(%rsp)
movl $0x1, %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0x21d0
movl $0x2, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x21d0
movq %r15, %rdi
callq 0x20c0
jmp 0x2d34
jmp 0x2f8e
jmp 0x2f8e
jmp 0x2f8e
jmp 0x2f89
jmp 0x2f8e
movq %rax, %rbx
jmp 0x2f9e
movq %rax, %rbx
leaq 0x1e8(%rsp), %rdi
callq 0x2230
leaq 0x18(%rsp), %rdi
callq 0x30a2
movq %rbx, %rdi
callq 0x22c0