// File: /workspace/github_cpp_makefile_star2_O3/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, %ebp
leaq 0x1c0f(%rip), %rdx      # 0x4028
callq 0x2250
cmpl $-0x1, %eax
je 0x2647
leaq 0x1bf6(%rip), %rcx      # 0x4024
movq %rcx, 0x8(%rsp)
xorl %r15d, %r15d
leaq 0x1bc7(%rip), %r14      # 0x4004
movq 0x3b94(%rip), %r12      # 0x5fd8
leaq 0x1bdd(%rip), %r13      # 0x4028
addl $-0x62, %eax
rorl %eax
cmpl $0x7, %eax
ja 0x2647
movslq (%r14,%rax,4), %rax
addq %r14, %rax
jmpq *%rax
movq (%r12), %rdi
callq 0x2260
movl %eax, 0x4(%rsp)
jmp 0x2480
movq (%r12), %rax
movq %rax, 0x8(%rsp)
jmp 0x2480
movq (%r12), %r15
movl %ebp, %edi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x2250
cmpl $-0x1, %eax
jne 0x244b
testq %r15, %r15
je 0x2647
movq %r15, %rdi
movl 0x4(%rsp), %ebp
movl %ebp, %esi
movq 0x8(%rsp), %r14
movq %r14, %rdx
callq 0x29c3
testq %rax, %rax
je 0x26d8
movq %rax, %rbx
leaq 0xa4(%rsp), %rsi
xorl %edi, %edi
callq 0x2200
testl %eax, %eax
jne 0x26fd
leaq 0xa4(%rsp), %rdx
andb $-0xb, 0xc(%rdx)
movw $0x0, 0x16(%rdx)
xorl %edi, %edi
xorl %esi, %esi
callq 0x2210
testl %eax, %eax
jne 0x2710
leaq 0x28(%rsp), %r13
leaq 0x20(%rsp), %r14
leaq 0x10(%rsp), %r15
leaq 0xe0(%rsp), %r12
movups 0x1b9b(%rip), %xmm0     # 0x40b0
movaps %xmm0, 0x10(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x60(%r13)
movups %xmm0, 0x50(%r13)
movups %xmm0, 0x40(%r13)
movups %xmm0, 0x30(%r13)
movups %xmm0, 0x20(%r13)
movups %xmm0, 0x10(%r13)
movups %xmm0, (%r13)
movq $0x0, 0x70(%r13)
movl $0x1, %eax
movq %rax, 0x20(%rsp)
movq %rbx, %rdi
callq 0x384b
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 0x384b
leal 0x3f(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
sarl $0x6, %ecx
movslq %ecx, %rax
orq %rbp, 0x20(%rsp,%rax,8)
movq %rbx, %rdi
callq 0x384b
movl $0x1, %edi
testl %eax, %eax
jle 0x25ac
movq %rbx, %rdi
callq 0x384b
movl %eax, %edi
incl %edi
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r15, %r8
callq 0x21d0
testb $0x1, 0x20(%rsp)
je 0x25e8
movl $0x80, %edx
xorl %edi, %edi
movq %r12, %rsi
callq 0x2160
testl %eax, %eax
jle 0x25e8
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rbx, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x28b3
movq %rbx, %rdi
callq 0x384b
leal 0x3f(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
sarl $0x6, %ecx
movslq %ecx, %rax
movq 0x20(%rsp,%rax,8), %rbp
movq %rbx, %rdi
callq 0x384b
btq %rax, %rbp
jae 0x250e
movl $0x80, %edx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x273c
testl %eax, %eax
jle 0x250e
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl $0x1, %edi
movq %r12, %rsi
movq %rax, %rdx
callq 0x20b0
jmp 0x250e
movq (%rbx), %rdi
callq 0x22f0
movl $0x1, %edi
callq 0x2280
leaq 0x20(%rsp), %rdi
callq 0x303a
movq %rax, %rbx
leaq 0x19c4(%rip), %rdi      # 0x4031
movq %rax, %rsi
xorl %eax, %eax
callq 0x2120
testq %rbx, %rbx
jle 0x26c7
leaq 0x19bf(%rip), %r14      # 0x4042
xorl %r15d, %r15d
movq 0x20(%rsp), %rax
movq (%rax,%r15,8), %rdx
movq %r14, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x2120
incq %r15
cmpq %r15, %rbx
jne 0x2686
testq %rbx, %rbx
jle 0x26c7
xorl %r14d, %r14d
movq 0x20(%rsp), %rax
movq (%rax,%r14,8), %rdi
testq %rdi, %rdi
je 0x26bf
callq 0x2040
incq %r14
cmpq %r14, %rbx
jne 0x26ac
movq 0x20(%rsp), %rdi
callq 0x2040
xorl %edi, %edi
callq 0x2280
movq 0x3919(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x1967(%rip), %rsi      # 0x4050
movq %r15, %rdx
movl %ebp, %ecx
movq %r14, %r8
xorl %eax, %eax
callq 0x21a0
jmp 0x264f
movq 0x38f4(%rip), %rax      # 0x5ff8
movq (%rax), %rcx
leaq 0x195b(%rip), %rdi      # 0x4069
jmp 0x2721
movq 0x38e1(%rip), %rax      # 0x5ff8
movq (%rax), %rcx
leaq 0x1953(%rip), %rdi      # 0x4074
movl $0xa, %esi
movl $0x1, %edx
callq 0x2290
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x2280
nop