// File: 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
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
callq 0x48e0
movl %eax, -0x14(%rbp)
leaq 0x4665(%rip), %rdi      # 0x931f
movl $0x1c200, %esi          # imm = 0x1C200
leaq 0x4664(%rip), %rdx      # 0x932a
callq 0x6a90
movq %rax, -0x8(%rbp)
cmpq $0x0, -0x8(%rbp)
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %edi
leaq 0x4647(%rip), %rsi      # 0x932e
movl $0x6b, %edx
leaq 0x4c11(%rip), %rcx      # 0x9904
leaq 0x4654(%rip), %r8       # 0x934e
movb $0x0, %al
callq 0x26e0
cmpl $0x0, %eax
jne 0x4d0b
callq 0x3290
jmp 0x4d0d
leaq 0x4645(%rip), %rdi      # 0x9359
movl $0x1c200, %esi          # imm = 0x1C200
leaq 0x460a(%rip), %rdx      # 0x932a
callq 0x6a90
movq %rax, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %edi
leaq 0x45ed(%rip), %rsi      # 0x932e
movl $0x6d, %edx
leaq 0x4bb7(%rip), %rcx      # 0x9904
leaq 0x4610(%rip), %r8       # 0x9364
movb $0x0, %al
callq 0x26e0
cmpl $0x0, %eax
jne 0x4d65
callq 0x3290
jmp 0x4d67
movl $0x0, -0x18(%rbp)
cmpl $0x64, -0x18(%rbp)
jge 0x4f53
callq 0x23c0
cltq
movl $0x63, %ecx
xorl %edx, %edx
divq %rcx
addq $0x1, %rdx
movl %edx, %eax
movl %eax, -0x154(%rbp)
movl $0x0, -0x158(%rbp)
movl -0x158(%rbp), %eax
cmpl -0x154(%rbp), %eax
jge 0x4dd8
callq 0x23c0
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
movb %dl, %cl
movslq -0x158(%rbp), %rax
movb %cl, -0x80(%rbp,%rax)
movl -0x158(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x158(%rbp)
jmp 0x4d9f
leaq -0x150(%rbp), %rdi
xorl %esi, %esi
movl $0xc8, %edx
callq 0x2190
movq -0x8(%rbp), %rdi
leaq -0x80(%rbp), %rsi
movslq -0x154(%rbp), %rdx
callq 0x68b0
movslq -0x154(%rbp), %rcx
cmpq %rcx, %rax
sete %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %edi
leaq 0x4512(%rip), %rsi      # 0x932e
movl $0x76, %edx
leaq 0x4adc(%rip), %rcx      # 0x9904
leaq 0x4638(%rip), %r8       # 0x9467
movb $0x0, %al
callq 0x26e0
cmpl $0x0, %eax
jne 0x4e40
callq 0x3290
jmp 0x4e42
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x23e0
movq -0x10(%rbp), %rdi
callq 0x7330
cmpl -0x154(%rbp), %eax
sete %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %edi
leaq 0x44c0(%rip), %rsi      # 0x932e
movl $0x78, %edx
leaq 0x4a8a(%rip), %rcx      # 0x9904
leaq 0x4612(%rip), %r8       # 0x9493
movb $0x0, %al
callq 0x26e0
cmpl $0x0, %eax
jne 0x4e92
callq 0x3290
jmp 0x4e94
jmp 0x4e96
movq -0x10(%rbp), %rdi
leaq -0x150(%rbp), %rsi
movl $0xc8, %edx
callq 0x6600
movslq -0x154(%rbp), %rcx
cmpq %rcx, %rax
sete %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %edi
leaq 0x4466(%rip), %rsi      # 0x932e
movl $0x79, %edx
leaq 0x4a30(%rip), %rcx      # 0x9904
leaq 0x45db(%rip), %r8       # 0x94b6
movb $0x0, %al
callq 0x26e0
cmpl $0x0, %eax
jne 0x4eec
callq 0x3290
jmp 0x4eee
jmp 0x4ef0
leaq -0x150(%rbp), %rdi
leaq -0x80(%rbp), %rsi
movslq -0x154(%rbp), %rdx
callq 0x21d0
cmpl $0x0, %eax
sete %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %edi
leaq 0x4411(%rip), %rsi      # 0x932e
movl $0x7a, %edx
leaq 0x49db(%rip), %rcx      # 0x9904
leaq 0x45b5(%rip), %r8       # 0x94e5
movb $0x0, %al
callq 0x26e0
cmpl $0x0, %eax
jne 0x4f41
callq 0x3290
jmp 0x4f43
jmp 0x4f45
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x4d6e
movq -0x8(%rbp), %rdi
callq 0x6a20
movq -0x10(%rbp), %rdi
callq 0x6a20
movl -0x14(%rbp), %edi
callq 0x4980
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
nopw %cs:(%rax,%rax)