// File: AndreRenaud[P]simple_uart/simple_uart_test.c
// Function: test_logfile
void test_logfile(void)
{
    struct simple_uart *u1, *u2;
    char buffer[20];
    pid_t pid;
    const char *logfile = "/tmp/simple_uart_test.log";
    FILE *fp;
    char log_contents[100];

    /* Remove any existing log file */
    unlink(logfile);

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

    /* Set logfile on u1 */
    TEST_ASSERT(simple_uart_set_logfile(u1, logfile) == 0);

    /* Test error cases */
    TEST_ASSERT(simple_uart_set_logfile(NULL, logfile) == -EINVAL);
    TEST_ASSERT(simple_uart_set_logfile(u1, NULL) == -EINVAL);

    TEST_ASSERT(simple_uart_write(u1, "hello", 5) == 5);
    TEST_ASSERT(simple_uart_read(u2, buffer, sizeof(buffer)) == 5);
    TEST_ASSERT(simple_uart_write(u2, "world", 5) == 5);
    TEST_ASSERT(simple_uart_read(u1, buffer, sizeof(buffer)) == 5);

    /* Close connections to flush logs */
    simple_uart_close(u1);
    simple_uart_close(u2);
    shutdown_loopback(pid);

    /* Check that logfile was created and contains expected data */
    fp = fopen(logfile, "r");
    TEST_ASSERT(fp != NULL);

    size_t read_size = fread(log_contents, 1, sizeof(log_contents) - 1, fp);
    TEST_ASSERT(read_size > 0); // Ensure we read something
    log_contents[read_size] = '\0';
    fclose(fp);

    TEST_ASSERT(strcmp(log_contents, "helloworld") == 0);

    /* Clean up */
    unlink(logfile);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
leaq 0x3f9d(%rip), %rax      # 0x96bf
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
callq 0x2060
callq 0x48e0
movl %eax, -0x34(%rbp)
leaq 0x3be1(%rip), %rdi      # 0x931f
movl $0x1c200, %esi          # imm = 0x1C200
leaq 0x3be0(%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 0x3bc3(%rip), %rsi      # 0x932e
movl $0xe1, %edx
leaq 0x418d(%rip), %rcx      # 0x9904
leaq 0x3bd0(%rip), %r8       # 0x934e
movb $0x0, %al
callq 0x26e0
cmpl $0x0, %eax
jne 0x578f
callq 0x3290
jmp 0x5791
leaq 0x3bc1(%rip), %rdi      # 0x9359
movl $0x1c200, %esi          # imm = 0x1C200
leaq 0x3b86(%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 0x3b69(%rip), %rsi      # 0x932e
movl $0xe3, %edx
leaq 0x4133(%rip), %rcx      # 0x9904
leaq 0x3b8c(%rip), %r8       # 0x9364
movb $0x0, %al
callq 0x26e0
cmpl $0x0, %eax
jne 0x57e9
callq 0x3290
jmp 0x57eb
jmp 0x57ed
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
movb $0x0, %al
callq 0x7c70
cmpl $0x0, %eax
sete %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %edi
leaq 0x3b1c(%rip), %rsi      # 0x932e
movl $0xe6, %edx
leaq 0x40e6(%rip), %rcx      # 0x9904
leaq 0x3eb4(%rip), %r8       # 0x96d9
movb $0x0, %al
callq 0x26e0
cmpl $0x0, %eax
jne 0x5836
callq 0x3290
jmp 0x5838
jmp 0x583a
movq -0x40(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edi
movb $0x0, %al
callq 0x7c70
cmpl $-0x16, %eax
sete %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %edi
leaq 0x3acf(%rip), %rsi      # 0x932e
movl $0xe9, %edx
leaq 0x4099(%rip), %rcx      # 0x9904
leaq 0x3e91(%rip), %r8       # 0x9703
movb $0x0, %al
callq 0x26e0
cmpl $0x0, %eax
jne 0x5883
callq 0x3290
jmp 0x5885
jmp 0x5887
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movb $0x0, %al
callq 0x7c70
cmpl $-0x16, %eax
sete %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %edi
leaq 0x3a82(%rip), %rsi      # 0x932e
movl $0xea, %edx
leaq 0x404c(%rip), %rcx      # 0x9904
leaq 0x3e76(%rip), %r8       # 0x9735
movb $0x0, %al
callq 0x26e0
cmpl $0x0, %eax
jne 0x58d0
callq 0x3290
jmp 0x58d2
jmp 0x58d4
movq -0x8(%rbp), %rdi
leaq 0x3e83(%rip), %rsi      # 0x9762
movl $0x5, %edx
callq 0x68b0
cmpq $0x5, %rax
sete %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %edi
leaq 0x3a2e(%rip), %rsi      # 0x932e
movl $0xec, %edx
leaq 0x3ff8(%rip), %rcx      # 0x9904
leaq 0x3e55(%rip), %r8       # 0x9768
movb $0x0, %al
callq 0x26e0
cmpl $0x0, %eax
jne 0x5924
callq 0x3290
jmp 0x5926
jmp 0x5928
movq -0x10(%rbp), %rdi
leaq -0x30(%rbp), %rsi
movl $0x14, %edx
callq 0x6600
cmpq $0x5, %rax
sete %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %edi
leaq 0x39dd(%rip), %rsi      # 0x932e
movl $0xed, %edx
leaq 0x3fa7(%rip), %rcx      # 0x9904
leaq 0x3e2b(%rip), %r8       # 0x978f
movb $0x0, %al
callq 0x26e0
cmpl $0x0, %eax
jne 0x5975
callq 0x3290
jmp 0x5977
jmp 0x5979
movq -0x10(%rbp), %rdi
leaq 0x3e82(%rip), %rsi      # 0x9806
movl $0x5, %edx
callq 0x68b0
cmpq $0x5, %rax
sete %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %edi
leaq 0x3989(%rip), %rsi      # 0x932e
movl $0xee, %edx
leaq 0x3f53(%rip), %rcx      # 0x9904
leaq 0x3e09(%rip), %r8       # 0x97c1
movb $0x0, %al
callq 0x26e0
cmpl $0x0, %eax
jne 0x59c9
callq 0x3290
jmp 0x59cb
jmp 0x59cd
movq -0x8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
movl $0x14, %edx
callq 0x6600
cmpq $0x5, %rax
sete %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %edi
leaq 0x3938(%rip), %rsi      # 0x932e
movl $0xef, %edx
leaq 0x3f02(%rip), %rcx      # 0x9904
leaq 0x3a0c(%rip), %r8       # 0x9415
movb $0x0, %al
callq 0x26e0
cmpl $0x0, %eax
jne 0x5a1a
callq 0x3290
jmp 0x5a1c
movq -0x8(%rbp), %rdi
callq 0x6a20
movq -0x10(%rbp), %rdi
callq 0x6a20
movl -0x34(%rbp), %edi
callq 0x4980
movq -0x40(%rbp), %rdi
leaq 0x3f1b(%rip), %rsi      # 0x995c
callq 0x2310
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %edi
leaq 0x38cc(%rip), %rsi      # 0x932e
movl $0xf8, %edx
leaq 0x3e96(%rip), %rcx      # 0x9904
leaq 0x3d73(%rip), %r8       # 0x97e8
movb $0x0, %al
callq 0x26e0
cmpl $0x0, %eax
jne 0x5a86
callq 0x3290
jmp 0x5a88
leaq -0xb0(%rbp), %rdi
movq -0x48(%rbp), %rcx
movl $0x1, %esi
movl $0x63, %edx
callq 0x20a0
movq %rax, -0xb8(%rbp)
cmpq $0x0, -0xb8(%rbp)
seta %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %edi
leaq 0x386a(%rip), %rsi      # 0x932e
movl $0xfb, %edx
leaq 0x3e34(%rip), %rcx      # 0x9904
leaq 0x3d1c(%rip), %r8       # 0x97f3
movb $0x0, %al
callq 0x26e0
cmpl $0x0, %eax
jne 0x5ae8
callq 0x3290
jmp 0x5aea
movq -0xb8(%rbp), %rax
movb $0x0, -0xb0(%rbp,%rax)
movq -0x48(%rbp), %rdi
callq 0x2110
leaq -0xb0(%rbp), %rdi
leaq 0x3cf1(%rip), %rsi      # 0x9801
callq 0x2200
cmpl $0x0, %eax
sete %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %edi
leaq 0x3803(%rip), %rsi      # 0x932e
movl $0xff, %edx
leaq 0x3dcd(%rip), %rcx      # 0x9904
leaq 0x3cce(%rip), %r8       # 0x980c
movb $0x0, %al
callq 0x26e0
cmpl $0x0, %eax
jne 0x5b4f
callq 0x3290
jmp 0x5b51
movq -0x40(%rbp), %rdi
callq 0x2060
addq $0xc0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)