// File: /workspace/github_cpp_makefile_star2_O3/AndreRenaud[P]simple_uart/simple_uart_test.c
// Function: test_loopback_random
void test_loopback_random(void)
{
    struct simple_uart *u1, *u2;
    pid_t pid;

    pid = setup_loopback();

    u1 = simple_uart_open(UART_LOOPBACK_1, 115200, "8N1");
    TEST_ASSERT(u1 != NULL);
    u2 = simple_uart_open(UART_LOOPBACK_2, 115200, "8N1");
    TEST_ASSERT(u2 != NULL);

    for (int i = 0; i < 100; i++) {
        unsigned char tx[100];
        unsigned char rx[sizeof(tx) * 2];
        int tx_len = 1 + (rand() % (sizeof(tx) - 1));
        for (int j = 0; j < tx_len; j++)
            tx[j] = rand() % 256;
        memset(rx, 0, sizeof(rx));
        TEST_ASSERT(simple_uart_write(u1, tx, tx_len) == tx_len);
        usleep(1000); // Give it time to get to the other end
        TEST_ASSERT(simple_uart_has_data(u2) == tx_len);
        TEST_ASSERT(simple_uart_read(u2, rx, sizeof(rx)) == tx_len);
        TEST_ASSERT(memcmp(rx, tx, tx_len) == 0);
    }

    simple_uart_close(u1);
    simple_uart_close(u2);
    shutdown_loopback(pid);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
callq 0x416a
movl %eax, %ebx
leaq 0x2ef3(%rip), %rdi      # 0x737f
leaq 0x2ef7(%rip), %rdx      # 0x738a
movl $0x1c200, %esi          # imm = 0x1C200
callq 0x5a07
xorl %edi, %edi
movq %rax, 0x10(%rsp)
testq %rax, %rax
setne %dil
leaq 0x2edc(%rip), %rsi      # 0x738e
leaq 0x34a6(%rip), %rcx      # 0x795f
leaq 0x2eee(%rip), %r8       # 0x73ae
movl $0x6b, %edx
xorl %eax, %eax
callq 0x28df
testl %eax, %eax
je 0x46f0
movl %ebx, 0xc(%rsp)
leaq 0x2eda(%rip), %rdi      # 0x73b9
leaq 0x2ea4(%rip), %rdx      # 0x738a
movl $0x1c200, %esi          # imm = 0x1C200
callq 0x5a07
movq %rax, %r15
xorl %edi, %edi
testq %rax, %rax
setne %dil
leaq 0x2e8b(%rip), %rsi      # 0x738e
leaq 0x3455(%rip), %rcx      # 0x795f
leaq 0x2eb3(%rip), %r8       # 0x73c4
movl $0x6d, %edx
xorl %eax, %eax
callq 0x28df
testl %eax, %eax
je 0x46f0
leaq 0x90(%rsp), %r12
leaq 0x20(%rsp), %rbx
xorl %r13d, %r13d
movq %r15, 0x18(%rsp)
movq %rbx, %rbp
movq %r12, %rbx
callq 0x23e0
movslq %eax, %r14
movq %r14, %rax
movabsq $0x4afd6a052bf5a815, %rcx # imm = 0x4AFD6A052BF5A815
mulq %rcx
movq %r14, %rax
subq %rdx, %rax
shrq %rax
addq %rdx, %rax
shrq $0x6, %rax
imull $0x63, %eax, %ecx
imull $-0x63, %eax, %eax
leal (%r14,%rax), %r12d
incl %r12d
subl %ecx, %r14d
xorl %r15d, %r15d
callq 0x23e0
movb %al, 0x20(%rsp,%r15)
incq %r15
cmpq %r15, %r12
jne 0x457b
incl %r14d
movl $0xc8, %edx
movq %rbx, %r12
movq %rbx, %rdi
xorl %esi, %esi
callq 0x21b0
movq 0x10(%rsp), %rdi
movq %rbp, %rbx
movq %rbp, %rsi
movq %r14, %rdx
callq 0x58f7
xorl %edi, %edi
cmpq %r14, %rax
sete %dil
leaq 0x2dc9(%rip), %rsi      # 0x738e
movl $0x76, %edx
leaq 0x338e(%rip), %rbp      # 0x795f
movq %rbp, %rcx
leaq 0x2eec(%rip), %r8       # 0x74c7
xorl %eax, %eax
callq 0x28df
testl %eax, %eax
je 0x46f0
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x2410
movq 0x18(%rsp), %r15
movq %r15, %rdi
callq 0x6033
xorl %edi, %edi
cmpl %r14d, %eax
sete %dil
leaq 0x2d7d(%rip), %rsi      # 0x738e
movl $0x78, %edx
movq %rbp, %rcx
leaq 0x2ed3(%rip), %r8       # 0x74f3
xorl %eax, %eax
callq 0x28df
testl %eax, %eax
je 0x46f0
movl $0xc8, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x5780
xorl %edi, %edi
cmpq %r14, %rax
sete %dil
leaq 0x2d3f(%rip), %rsi      # 0x738e
movl $0x79, %edx
movq %rbp, %rcx
leaq 0x2eb8(%rip), %r8       # 0x7516
xorl %eax, %eax
callq 0x28df
testl %eax, %eax
je 0x46f0
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x22c0
xorl %edi, %edi
testl %eax, %eax
sete %dil
leaq 0x2d04(%rip), %rsi      # 0x738e
movl $0x7a, %edx
movq %rbp, %rcx
leaq 0x2eac(%rip), %r8       # 0x7545
xorl %eax, %eax
callq 0x28df
testl %eax, %eax
je 0x46f0
incl %r13d
cmpl $0x64, %r13d
jne 0x453a
movq 0x10(%rsp), %rdi
callq 0x59cf
movq %r15, %rdi
callq 0x59cf
movl 0xc(%rsp), %ebx
movl %ebx, %edi
movl $0x1, %esi
callq 0x2270
movl %ebx, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x22f0
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x31c2