// File: /workspace/github_cpp_makefile_star2_O1/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 0x4256
movl %eax, %ebx
leaq 0x2dd3(%rip), %rdi      # 0x7347
leaq 0x2dd7(%rip), %rdx      # 0x7352
movl $0x1c200, %esi          # imm = 0x1C200
callq 0x5b0d
xorl %edi, %edi
movq %rax, 0x10(%rsp)
testq %rax, %rax
setne %dil
leaq 0x2dbc(%rip), %rsi      # 0x7356
leaq 0x3389(%rip), %rcx      # 0x792a
leaq 0x2dce(%rip), %r8       # 0x7376
movl $0x6b, %edx
xorl %eax, %eax
callq 0x28d6
testl %eax, %eax
je 0x47d8
movl %ebx, 0xc(%rsp)
leaq 0x2dba(%rip), %rdi      # 0x7381
leaq 0x2d84(%rip), %rdx      # 0x7352
movl $0x1c200, %esi          # imm = 0x1C200
callq 0x5b0d
movq %rax, %r15
xorl %edi, %edi
testq %rax, %rax
setne %dil
leaq 0x2d6b(%rip), %rsi      # 0x7356
leaq 0x3338(%rip), %rcx      # 0x792a
leaq 0x2d93(%rip), %r8       # 0x738c
movl $0x6d, %edx
xorl %eax, %eax
callq 0x28d6
testl %eax, %eax
je 0x47d8
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 0x4663
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 0x59df
xorl %edi, %edi
cmpq %r14, %rax
sete %dil
leaq 0x2ca9(%rip), %rsi      # 0x7356
movl $0x76, %edx
leaq 0x3271(%rip), %rbp      # 0x792a
movq %rbp, %rcx
leaq 0x2dcc(%rip), %r8       # 0x748f
xorl %eax, %eax
callq 0x28d6
testl %eax, %eax
je 0x47d8
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x2410
movq 0x18(%rsp), %r15
movq %r15, %rdi
callq 0x6162
xorl %edi, %edi
cmpl %r14d, %eax
sete %dil
leaq 0x2c5d(%rip), %rsi      # 0x7356
movl $0x78, %edx
movq %rbp, %rcx
leaq 0x2db3(%rip), %r8       # 0x74bb
xorl %eax, %eax
callq 0x28d6
testl %eax, %eax
je 0x47d8
movl $0xc8, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x5868
xorl %edi, %edi
cmpq %r14, %rax
sete %dil
leaq 0x2c1f(%rip), %rsi      # 0x7356
movl $0x79, %edx
movq %rbp, %rcx
leaq 0x2d98(%rip), %r8       # 0x74de
xorl %eax, %eax
callq 0x28d6
testl %eax, %eax
je 0x47d8
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x22c0
xorl %edi, %edi
testl %eax, %eax
sete %dil
leaq 0x2be4(%rip), %rsi      # 0x7356
movl $0x7a, %edx
movq %rbp, %rcx
leaq 0x2d8c(%rip), %r8       # 0x750d
xorl %eax, %eax
callq 0x28d6
testl %eax, %eax
je 0x47d8
incl %r13d
cmpl $0x64, %r13d
jne 0x4622
movq 0x10(%rsp), %rdi
callq 0x5ad5
movq %r15, %rdi
callq 0x5ad5
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 0x31a2