// File: /workspace/github_cpp_makefile_star2_O1/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 $0x38, %rsp
callq 0x4256
movl %eax, %ebx
leaq 0x2875(%rip), %rdi      # 0x7347
leaq 0x2879(%rip), %rdx      # 0x7352
movl $0x1c200, %esi          # imm = 0x1C200
callq 0x5b0d
movq %rax, %r14
xorl %edi, %edi
testq %rax, %rax
setne %dil
leaq 0x2860(%rip), %rsi      # 0x7356
leaq 0x2e2d(%rip), %rcx      # 0x792a
leaq 0x2872(%rip), %r8       # 0x7376
movl $0xac, %edx
xorl %eax, %eax
callq 0x28d6
testl %eax, %eax
je 0x4f25
movl %ebx, 0x10(%rsp)
leaq 0x285e(%rip), %rdi      # 0x7381
leaq 0x2828(%rip), %rdx      # 0x7352
movl $0x1c200, %esi          # imm = 0x1C200
callq 0x5b0d
xorl %edi, %edi
movq %rax, 0x20(%rsp)
testq %rax, %rax
setne %dil
leaq 0x280d(%rip), %rsi      # 0x7356
leaq 0x2dda(%rip), %rcx      # 0x792a
leaq 0x2835(%rip), %r8       # 0x738c
movl $0xae, %edx
xorl %eax, %eax
callq 0x28d6
testl %eax, %eax
je 0x4f25
leaq 0x28(%rsp), %r12
movl $0x1, %edi
movq %r12, %rsi
callq 0x2100
movq (%r12), %r15
movq 0x8(%r12), %r12
leaq 0x16(%rsp), %rsi
movl $0xa, %edx
movq %r14, 0x8(%rsp)
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x5872
movq %rax, %rbp
leaq 0x28(%rsp), %r13
movl $0x1, %edi
movq %r13, %rsi
callq 0x2100
movq (%r13), %r14
movq 0x8(%r13), %r13
xorl %edi, %edi
cmpq $-0x6e, %rbp
sete %dil
leaq 0x2789(%rip), %rsi      # 0x7356
leaq 0x2d56(%rip), %rcx      # 0x792a
leaq 0x2a60(%rip), %r8       # 0x763b
movl $0xb4, %edx
xorl %eax, %eax
callq 0x28d6
testl %eax, %eax
je 0x4f25
movabsq $0x431bde82d7b634db, %rbx # imm = 0x431BDE82D7B634DB
movq %r13, %rax
mulq %rbx
movq %rdx, %rcx
shrq $0x12, %rcx
movq %r12, %rax
mulq %rbx
shrq $0x12, %rdx
subq %rdx, %rcx
subq %r15, %r14
imulq $0x3e8, %r14, %rax      # imm = 0x3E8
addq %rcx, %rax
xorl %edi, %edi
cmpq $0x64, %rax
setb %dil
leaq 0x2725(%rip), %rsi      # 0x7356
leaq 0x2cf2(%rip), %rcx      # 0x792a
leaq 0x2a11(%rip), %r8       # 0x7650
movl $0xb5, %edx
xorl %eax, %eax
callq 0x28d6
testl %eax, %eax
je 0x4f25
leaq 0x16(%rsp), %rsi
movq 0x8(%rsp), %r15
movq %r15, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x5872
xorl %edi, %edi
testq %rax, %rax
sete %dil
leaq 0x26dd(%rip), %rsi      # 0x7356
leaq 0x2caa(%rip), %rcx      # 0x792a
leaq 0x29e7(%rip), %r8       # 0x766e
movl $0xb9, %edx
xorl %eax, %eax
callq 0x28d6
testl %eax, %eax
je 0x4f25
leaq 0x28(%rsp), %r12
movl $0x1, %edi
movq %r12, %rsi
callq 0x2100
movq (%r12), %r14
movq 0x8(%r12), %r12
leaq 0x16(%rsp), %rsi
movl $0xa, %edx
movl $0xc8, %ecx
movq %r15, %rdi
callq 0x5872
movq %rax, %rbp
leaq 0x28(%rsp), %r13
movl $0x1, %edi
movq %r13, %rsi
callq 0x2100
movq (%r13), %r15
movq 0x8(%r13), %r13
xorl %edi, %edi
cmpq $-0x6e, %rbp
sete %dil
leaq 0x265b(%rip), %rsi      # 0x7356
leaq 0x2c28(%rip), %rcx      # 0x792a
leaq 0x2932(%rip), %r8       # 0x763b
movl $0xbf, %edx
xorl %eax, %eax
callq 0x28d6
testl %eax, %eax
je 0x4f25
movq %r13, %rax
mulq %rbx
movq %rdx, %rcx
shrq $0x12, %rcx
movq %r12, %rax
mulq %rbx
shrq $0x12, %rdx
subq %rdx, %rcx
subq %r14, %r15
imulq $0x3e8, %r15, %rbx      # imm = 0x3E8
addq %rcx, %rbx
xorl %edi, %edi
cmpq $0xb4, %rbx
setae %dil
leaq 0x25fe(%rip), %rsi      # 0x7356
leaq 0x2bcb(%rip), %rcx      # 0x792a
leaq 0x2914(%rip), %r8       # 0x767a
movl $0xc0, %edx
xorl %eax, %eax
callq 0x28d6
testl %eax, %eax
je 0x4f25
xorl %edi, %edi
cmpq $0x1f4, %rbx            # imm = 0x1F4
setb %dil
leaq 0x25c8(%rip), %rsi      # 0x7356
leaq 0x2b95(%rip), %rcx      # 0x792a
leaq 0x28fd(%rip), %r8       # 0x7699
movl $0xc1, %edx
xorl %eax, %eax
callq 0x28d6
testl %eax, %eax
je 0x4f25
leaq 0x2900(%rip), %rsi      # 0x76b7
movl $0x4, %edx
movq 0x20(%rsp), %rdi
callq 0x59df
movl $0x2710, %edi           # imm = 0x2710
callq 0x2410
leaq 0x28(%rsp), %r12
movl $0x1, %edi
movq %r12, %rsi
callq 0x2100
movq (%r12), %r15
movabsq $0x431bde82d7b634db, %r13 # imm = 0x431BDE82D7B634DB
movq %r13, %rax
mulq 0x8(%r12)
movq %rdx, %r12
leaq 0x16(%rsp), %rsi
movl $0xa, %edx
movl $0x3e8, %ecx            # imm = 0x3E8
movq 0x8(%rsp), %rdi
callq 0x5872
movq %rax, %rbp
leaq 0x28(%rsp), %rbx
movl $0x1, %edi
movq %rbx, %rsi
callq 0x2100
movq (%rbx), %r14
movq %r13, %rax
mulq 0x8(%rbx)
movq %rdx, %r13
xorl %edi, %edi
cmpq $0x4, %rbp
sete %dil
leaq 0x250f(%rip), %rsi      # 0x7356
leaq 0x2adc(%rip), %rcx      # 0x792a
leaq 0x2867(%rip), %r8       # 0x76bc
movl $0xc9, %edx
xorl %eax, %eax
callq 0x28d6
testl %eax, %eax
je 0x4f25
xorl %edi, %edi
cmpl $0x74736574, 0x16(%rsp) # imm = 0x74736574
sete %dil
leaq 0x24d8(%rip), %rsi      # 0x7356
leaq 0x2aa5(%rip), %rcx      # 0x792a
leaq 0x283c(%rip), %r8       # 0x76c8
movl $0xca, %edx
xorl %eax, %eax
callq 0x28d6
testl %eax, %eax
je 0x4f25
shrq $0x12, %r12
shrq $0x12, %r13
subq %r12, %r13
subq %r15, %r14
imulq $0x3e8, %r14, %rax      # imm = 0x3E8
addq %r13, %rax
xorl %edi, %edi
cmpq $0x64, %rax
setb %dil
leaq 0x248d(%rip), %rsi      # 0x7356
leaq 0x2a5a(%rip), %rcx      # 0x792a
leaq 0x2779(%rip), %r8       # 0x7650
movl $0xcb, %edx
xorl %eax, %eax
callq 0x28d6
testl %eax, %eax
je 0x4f25
movq 0x8(%rsp), %rdi
callq 0x5ad5
movq 0x20(%rsp), %rdi
callq 0x5ad5
movl 0x10(%rsp), %ebx
movl %ebx, %edi
movl $0x1, %esi
callq 0x2270
movl %ebx, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x22f0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x31a2