// File: /workspace/github_cpp_makefile_star2_O1/AndreRenaud[P]simple_uart/simple_uart_term.c
// Function: main
int main(int argc, char *argv[])
{
    const char *port = NULL;
    int baudrate;
    const char *flags = "8N1";
    int opt;

    while ((opt = getopt(argc, argv, "p:b:f:lh")) != -1) {
        switch (opt) {
        case 'p': port = optarg; break;
        case 'b': baudrate = atoi(optarg); break;
        case 'f': flags = optarg; break;
        case 'l': {
            char **names;
            ssize_t nuarts = simple_uart_list(&names);
            printf("Found %zd ports\n", nuarts);
            for (ssize_t i = 0; i < nuarts; i++) {
                printf("Port %zd: %s\n", i, names[i]);
            }

            for (ssize_t i = 0; i < nuarts; i++) {
                if (names[i])
                    free(names[i]);
            }
            free(names);
            exit(0);
        }
        default:
        case 'h': print_usage(argv[0]); exit(1);
        }
    }

    if (!port) {
        print_usage(argv[0]);
        exit(1);
    }

    struct simple_uart *uart;

    uart = simple_uart_open(port, baudrate, flags);
    if (!uart) {
        fprintf(stderr, "Unable to open %s:%d:%s\n", port, baudrate, flags);
        exit(1);
    }

    /* Disable buffering on stdin */
    struct termios term;

    if (tcgetattr(STDIN_FILENO, &term)) {
        fprintf(stderr, "tcgetattr\n");
        exit(-1);
    }

    term.c_lflag &= ~(ICANON | ECHO);
    term.c_cc[VMIN] = 0;
    term.c_cc[VTIME] = 0;

    if (tcsetattr(STDIN_FILENO, TCSANOW, &term)) {
        fprintf(stderr, "tcsetattr\n");
        exit(-1);
    }

    while (1) {
        fd_set fds;
        int nfds = STDIN_FILENO;
        struct timeval tv = {1, 0};
        char buffer[128];

        /* Wait for bytes on either serial port or stdin */
        FD_ZERO(&fds);
        FD_SET(STDIN_FILENO, &fds);
        FD_SET(simple_uart_get_fd(uart), &fds);
        if (simple_uart_get_fd(uart) > nfds)
            nfds = simple_uart_get_fd(uart);

        select(nfds + 1, &fds, NULL, NULL, &tv);

        /* Move bytes from one to the other */
        if (FD_ISSET(STDIN_FILENO, &fds)) {
            int len = read(STDIN_FILENO, buffer, sizeof(buffer));
            if (len > 0)
                simple_uart_write(uart, buffer, len);
        }
        if (FD_ISSET(simple_uart_get_fd(uart), &fds)) {
            int len = simple_uart_read(uart, buffer, sizeof(buffer));
            if (len > 0)
                write(STDOUT_FILENO, buffer, len);
        }
    }

    simple_uart_close(uart);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq %rsi, %rbx
movl %edi, %r12d
leaq 0x1c0e(%rip), %rdx      # 0x4028
callq 0x2250
cmpl $-0x1, %eax
je 0x2490
leaq 0x1bf9(%rip), %rcx      # 0x4024
movq %rcx, (%rsp)
xorl %r15d, %r15d
leaq 0x1bcb(%rip), %r14      # 0x4004
movq 0x3b98(%rip), %rbp      # 0x5fd8
leaq 0x1be1(%rip), %r13      # 0x4028
addl $-0x62, %eax
rorl %eax
cmpl $0x7, %eax
ja 0x2654
movslq (%r14,%rax,4), %rax
addq %r14, %rax
jmpq *%rax
movq (%rbp), %rdi
callq 0x2260
movl %eax, 0xc(%rsp)
jmp 0x247b
movq (%rbp), %rax
movq %rax, (%rsp)
jmp 0x247b
movq (%rbp), %r15
movl %r12d, %edi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x2250
cmpl $-0x1, %eax
jne 0x2447
jmp 0x249e
leaq 0x1b8d(%rip), %rax      # 0x4024
movq %rax, (%rsp)
xorl %r15d, %r15d
testq %r15, %r15
je 0x2654
movq %r15, %rdi
movl 0xc(%rsp), %ebp
movl %ebp, %esi
movq (%rsp), %r14
movq %r14, %rdx
callq 0x29ed
testq %rax, %rax
je 0x26e5
movq %rax, %rbx
leaq 0xa4(%rsp), %rsi
xorl %edi, %edi
callq 0x2200
testl %eax, %eax
jne 0x270a
leaq 0xa4(%rsp), %rdx
andb $-0xb, 0xc(%rdx)
movw $0x0, 0x16(%rdx)
xorl %edi, %edi
xorl %esi, %esi
callq 0x2210
testl %eax, %eax
jne 0x271d
movl $0x1, %r13d
leaq 0x10(%rsp), %r14
leaq 0x90(%rsp), %r15
leaq 0xe0(%rsp), %r12
movups 0x1b8c(%rip), %xmm0     # 0x40b0
movaps %xmm0, 0x90(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x80(%rsp)
movq %r13, 0x10(%rsp)
movq %rbx, %rdi
callq 0x3968
leal 0x3f(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
andl $0xc0, %ecx
subl %ecx, %eax
movl $0x1, %ebp
movl %eax, %ecx
shlq %cl, %rbp
movq %rbx, %rdi
callq 0x3968
leal 0x3f(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
sarl $0x6, %ecx
movslq %ecx, %rax
orq %rbp, 0x10(%rsp,%rax,8)
movq %rbx, %rdi
callq 0x3968
movl $0x1, %edi
testl %eax, %eax
jle 0x25b9
movq %rbx, %rdi
callq 0x3968
movl %eax, %edi
incl %edi
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r15, %r8
callq 0x21d0
testb $0x1, 0x10(%rsp)
je 0x25f5
movl $0x80, %edx
xorl %edi, %edi
movq %r12, %rsi
callq 0x2160
testl %eax, %eax
jle 0x25f5
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rbx, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x28bf
movq %rbx, %rdi
callq 0x3968
leal 0x3f(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
sarl $0x6, %ecx
movslq %ecx, %rax
movq 0x10(%rsp,%rax,8), %rbp
movq %rbx, %rdi
callq 0x3968
btq %rax, %rbp
jae 0x251d
movl $0x80, %edx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2748
testl %eax, %eax
jle 0x251d
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl $0x1, %edi
movq %r12, %rsi
movq %rax, %rdx
callq 0x20b0
jmp 0x251d
movq (%rbx), %rdi
callq 0x22f0
movl $0x1, %edi
callq 0x2280
leaq 0x10(%rsp), %rdi
callq 0x308d
movq %rax, %rbx
leaq 0x19b7(%rip), %rdi      # 0x4031
movq %rax, %rsi
xorl %eax, %eax
callq 0x2120
testq %rbx, %rbx
jle 0x26b1
leaq 0x19b2(%rip), %r14      # 0x4042
xorl %r15d, %r15d
movq 0x10(%rsp), %rax
movq (%rax,%r15,8), %rdx
movq %r14, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x2120
incq %r15
cmpq %r15, %rbx
jne 0x2693
testq %rbx, %rbx
jle 0x26d4
xorl %r14d, %r14d
movq 0x10(%rsp), %rax
movq (%rax,%r14,8), %rdi
testq %rdi, %rdi
je 0x26cc
callq 0x2040
incq %r14
cmpq %r14, %rbx
jne 0x26b9
movq 0x10(%rsp), %rdi
callq 0x2040
xorl %edi, %edi
callq 0x2280
movq 0x390c(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x195a(%rip), %rsi      # 0x4050
movq %r15, %rdx
movl %ebp, %ecx
movq %r14, %r8
xorl %eax, %eax
callq 0x21a0
jmp 0x265c
movq 0x38e7(%rip), %rax      # 0x5ff8
movq (%rax), %rcx
leaq 0x194e(%rip), %rdi      # 0x4069
jmp 0x272e
movq 0x38d4(%rip), %rax      # 0x5ff8
movq (%rax), %rcx
leaq 0x1946(%rip), %rdi      # 0x4074
movl $0xa, %esi
movl $0x1, %edx
callq 0x2290
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x2280
nop