// File: /workspace/github_cpp_makefile_star2_O3/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 0x416a
movl %eax, %ebx
leaq 0x2995(%rip), %rdi      # 0x737f
leaq 0x2999(%rip), %rdx      # 0x738a
movl $0x1c200, %esi          # imm = 0x1C200
callq 0x5a07
movq %rax, %r14
xorl %edi, %edi
testq %rax, %rax
setne %dil
leaq 0x2980(%rip), %rsi      # 0x738e
leaq 0x2f4a(%rip), %rcx      # 0x795f
leaq 0x2992(%rip), %r8       # 0x73ae
movl $0xac, %edx
xorl %eax, %eax
callq 0x28df
testl %eax, %eax
je 0x4e3d
movl %ebx, 0x10(%rsp)
leaq 0x297e(%rip), %rdi      # 0x73b9
leaq 0x2948(%rip), %rdx      # 0x738a
movl $0x1c200, %esi          # imm = 0x1C200
callq 0x5a07
xorl %edi, %edi
movq %rax, 0x20(%rsp)
testq %rax, %rax
setne %dil
leaq 0x292d(%rip), %rsi      # 0x738e
leaq 0x2ef7(%rip), %rcx      # 0x795f
leaq 0x2955(%rip), %r8       # 0x73c4
movl $0xae, %edx
xorl %eax, %eax
callq 0x28df
testl %eax, %eax
je 0x4e3d
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 0x578a
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 0x28a9(%rip), %rsi      # 0x738e
leaq 0x2e73(%rip), %rcx      # 0x795f
leaq 0x2b80(%rip), %r8       # 0x7673
movl $0xb4, %edx
xorl %eax, %eax
callq 0x28df
testl %eax, %eax
je 0x4e3d
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 0x2845(%rip), %rsi      # 0x738e
leaq 0x2e0f(%rip), %rcx      # 0x795f
leaq 0x2b31(%rip), %r8       # 0x7688
movl $0xb5, %edx
xorl %eax, %eax
callq 0x28df
testl %eax, %eax
je 0x4e3d
leaq 0x16(%rsp), %rsi
movq 0x8(%rsp), %r15
movq %r15, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x578a
xorl %edi, %edi
testq %rax, %rax
sete %dil
leaq 0x27fd(%rip), %rsi      # 0x738e
leaq 0x2dc7(%rip), %rcx      # 0x795f
leaq 0x2b07(%rip), %r8       # 0x76a6
movl $0xb9, %edx
xorl %eax, %eax
callq 0x28df
testl %eax, %eax
je 0x4e3d
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 0x578a
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 0x277b(%rip), %rsi      # 0x738e
leaq 0x2d45(%rip), %rcx      # 0x795f
leaq 0x2a52(%rip), %r8       # 0x7673
movl $0xbf, %edx
xorl %eax, %eax
callq 0x28df
testl %eax, %eax
je 0x4e3d
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 0x271e(%rip), %rsi      # 0x738e
leaq 0x2ce8(%rip), %rcx      # 0x795f
leaq 0x2a34(%rip), %r8       # 0x76b2
movl $0xc0, %edx
xorl %eax, %eax
callq 0x28df
testl %eax, %eax
je 0x4e3d
xorl %edi, %edi
cmpq $0x1f4, %rbx            # imm = 0x1F4
setb %dil
leaq 0x26e8(%rip), %rsi      # 0x738e
leaq 0x2cb2(%rip), %rcx      # 0x795f
leaq 0x2a1d(%rip), %r8       # 0x76d1
movl $0xc1, %edx
xorl %eax, %eax
callq 0x28df
testl %eax, %eax
je 0x4e3d
leaq 0x2a20(%rip), %rsi      # 0x76ef
movl $0x4, %edx
movq 0x20(%rsp), %rdi
callq 0x58f7
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 0x578a
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 0x262f(%rip), %rsi      # 0x738e
leaq 0x2bf9(%rip), %rcx      # 0x795f
leaq 0x2987(%rip), %r8       # 0x76f4
movl $0xc9, %edx
xorl %eax, %eax
callq 0x28df
testl %eax, %eax
je 0x4e3d
xorl %edi, %edi
cmpl $0x74736574, 0x16(%rsp) # imm = 0x74736574
sete %dil
leaq 0x25f8(%rip), %rsi      # 0x738e
leaq 0x2bc2(%rip), %rcx      # 0x795f
leaq 0x295c(%rip), %r8       # 0x7700
movl $0xca, %edx
xorl %eax, %eax
callq 0x28df
testl %eax, %eax
je 0x4e3d
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 0x25ad(%rip), %rsi      # 0x738e
leaq 0x2b77(%rip), %rcx      # 0x795f
leaq 0x2899(%rip), %r8       # 0x7688
movl $0xcb, %edx
xorl %eax, %eax
callq 0x28df
testl %eax, %eax
je 0x4e3d
movq 0x8(%rsp), %rdi
callq 0x59cf
movq 0x20(%rsp), %rdi
callq 0x59cf
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 0x31c2