// File: /workspace/github_cpp_makefile_star2_O2/AndreRenaud[P]simple_uart/simple_uart_test.c
// Function: test_read_timeout
void test_read_timeout(void)
{
    struct simple_uart *u1, *u2;
    char buffer[10];
    pid_t pid;
    uint64_t start_time, end_time;

    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);

    /* Test timeout=0 (non-blocking) - should return immediately */
    start_time = mseconds();
    ssize_t result = simple_uart_read_timeout(u1, buffer, sizeof(buffer), 0);
    end_time = mseconds();
    TEST_ASSERT(result == -ETIMEDOUT);          // Nothing was available, so it timed out
    TEST_ASSERT((end_time - start_time) < 100); // Should return quickly

    /* Try reading nothing */
    result = simple_uart_read_timeout(u1, buffer, 0, 0);
    TEST_ASSERT(result == 0); // Should return 0 for reading nothing

    /* Test with actual timeout - should timeout after specified period */
    start_time = mseconds();
    result = simple_uart_read_timeout(u1, buffer, sizeof(buffer), 200); // 200ms timeout
    end_time = mseconds();
    TEST_ASSERT(result == -ETIMEDOUT);           // Should timeout
    TEST_ASSERT((end_time - start_time) >= 180); // Should wait at least ~180ms
    TEST_ASSERT((end_time - start_time) < 500);  // But not too long

    /* Test successful read within timeout */
    simple_uart_write(u2, "test", 4);
    usleep(10000); // Small delay to ensure data is available
    start_time = mseconds();
    result = simple_uart_read_timeout(u1, buffer, sizeof(buffer), 1000); // 1 second timeout
    end_time = mseconds();
    TEST_ASSERT(result == 4); // Should read 4 bytes
    TEST_ASSERT(memcmp(buffer, "test", 4) == 0);
    TEST_ASSERT((end_time - start_time) < 100); // Should return quickly when data is available

    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 $0x18, %rsp
callq 0x3e43
movl %eax, %ebx
leaq 0x2cdc(%rip), %rdi      # 0x7347
leaq 0x2ce0(%rip), %rdx      # 0x7352
movl $0x1c200, %esi          # imm = 0x1C200
callq 0x54e4
movq %rax, %r14
xorl %edi, %edi
testq %rax, %rax
setne %dil
leaq 0x2cc7(%rip), %rsi      # 0x7356
leaq 0x3291(%rip), %rcx      # 0x7927
leaq 0x2cd9(%rip), %r8       # 0x7376
movl $0xac, %edx
xorl %eax, %eax
callq 0x269a
testl %eax, %eax
je 0x499b
leaq 0x2cc9(%rip), %rdi      # 0x7381
leaq 0x2c93(%rip), %rdx      # 0x7352
movl $0x1c200, %esi          # imm = 0x1C200
callq 0x54e4
movq %rax, %r15
xorl %edi, %edi
testq %rax, %rax
setne %dil
leaq 0x2c7a(%rip), %rsi      # 0x7356
leaq 0x3244(%rip), %rcx      # 0x7927
leaq 0x2ca2(%rip), %r8       # 0x738c
movl $0xae, %edx
xorl %eax, %eax
callq 0x269a
testl %eax, %eax
je 0x499b
callq 0x49a0
movq %rax, %r12
leaq 0xe(%rsp), %rsi
pushq $0xa
popq %rdx
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x52ac
movq %rax, %rbp
callq 0x49a0
movq %rax, %r13
xorl %edi, %edi
cmpq $-0x6e, %rbp
sete %dil
leaq 0x2c22(%rip), %rsi      # 0x7356
leaq 0x31ec(%rip), %rcx      # 0x7927
leaq 0x2ef9(%rip), %r8       # 0x763b
movl $0xb4, %edx
xorl %eax, %eax
callq 0x269a
testl %eax, %eax
je 0x499b
subq %r12, %r13
xorl %edi, %edi
cmpq $0x64, %r13
setb %dil
leaq 0x2bec(%rip), %rsi      # 0x7356
leaq 0x31b6(%rip), %rcx      # 0x7927
leaq 0x2ed8(%rip), %r8       # 0x7650
movl $0xb5, %edx
xorl %eax, %eax
callq 0x269a
testl %eax, %eax
je 0x499b
leaq 0xe(%rsp), %rsi
movq %r14, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x52ac
xorl %edi, %edi
testq %rax, %rax
sete %dil
leaq 0x2ba9(%rip), %rsi      # 0x7356
leaq 0x3173(%rip), %rcx      # 0x7927
leaq 0x2eb3(%rip), %r8       # 0x766e
movl $0xb9, %edx
xorl %eax, %eax
callq 0x269a
testl %eax, %eax
je 0x499b
callq 0x49a0
movq %rax, %r13
leaq 0xe(%rsp), %rsi
pushq $0xa
popq %rdx
movl $0xc8, %ecx
movq %r14, %rdi
callq 0x52ac
movq %rax, %rbp
callq 0x49a0
movq %rax, %r12
xorl %edi, %edi
cmpq $-0x6e, %rbp
sete %dil
leaq 0x2b4e(%rip), %rsi      # 0x7356
leaq 0x3118(%rip), %rcx      # 0x7927
leaq 0x2e25(%rip), %r8       # 0x763b
movl $0xbf, %edx
xorl %eax, %eax
callq 0x269a
testl %eax, %eax
je 0x499b
subq %r13, %r12
xorl %edi, %edi
cmpq $0xb4, %r12
setae %dil
leaq 0x2b15(%rip), %rsi      # 0x7356
leaq 0x30df(%rip), %rcx      # 0x7927
leaq 0x2e2b(%rip), %r8       # 0x767a
movl $0xc0, %edx
xorl %eax, %eax
callq 0x269a
testl %eax, %eax
je 0x499b
xorl %edi, %edi
cmpq $0x1f4, %r12            # imm = 0x1F4
setb %dil
leaq 0x2adf(%rip), %rsi      # 0x7356
leaq 0x30a9(%rip), %rcx      # 0x7927
leaq 0x2e14(%rip), %r8       # 0x7699
movl $0xc1, %edx
xorl %eax, %eax
callq 0x269a
testl %eax, %eax
je 0x499b
leaq 0x2e17(%rip), %rsi      # 0x76b7
pushq $0x4
popq %rdx
movq %r15, %rdi
callq 0x53e0
movl $0x2710, %edi           # imm = 0x2710
callq 0x2410
callq 0x49a0
movq %rax, %r12
leaq 0xe(%rsp), %rsi
pushq $0xa
popq %rdx
movl $0x3e8, %ecx            # imm = 0x3E8
movq %r14, %rdi
callq 0x52ac
movq %rax, %rbp
callq 0x49a0
movq %rax, %r13
xorl %edi, %edi
cmpq $0x4, %rbp
sete %dil
leaq 0x2a68(%rip), %rsi      # 0x7356
leaq 0x3032(%rip), %rcx      # 0x7927
leaq 0x2dc0(%rip), %r8       # 0x76bc
movl $0xc9, %edx
xorl %eax, %eax
callq 0x269a
testl %eax, %eax
je 0x499b
xorl %edi, %edi
cmpl $0x74736574, 0xe(%rsp)  # imm = 0x74736574
sete %dil
leaq 0x2a31(%rip), %rsi      # 0x7356
leaq 0x2ffb(%rip), %rcx      # 0x7927
leaq 0x2d95(%rip), %r8       # 0x76c8
movl $0xca, %edx
xorl %eax, %eax
callq 0x269a
testl %eax, %eax
je 0x499b
subq %r12, %r13
xorl %edi, %edi
cmpq $0x64, %r13
setb %dil
leaq 0x29ff(%rip), %rsi      # 0x7356
leaq 0x2fc9(%rip), %rcx      # 0x7927
leaq 0x2ceb(%rip), %r8       # 0x7650
movl $0xcb, %edx
xorl %eax, %eax
callq 0x269a
testl %eax, %eax
je 0x499b
movq %r14, %rdi
callq 0x54b1
movq %r15, %rdi
callq 0x54b1
movl %ebx, %edi
callq 0x3eba
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2f01