// File: /workspace/github_c_makefile_star2_O0/lupyuen[P]lora-sx1262/src/sx126x-linux.c
// Function: transfer_spi
static int transfer_spi(const uint8_t *tx_buf, uint8_t *rx_buf, uint16_t len) {
    assert(spi > 0);
    assert(len > 0);
    assert(len <= 31);  //  CAUTION: CH341 SPI doesn't seem to support 32-byte SPI transfers 

    //  Prepare SPI Transfer
    struct spi_ioc_transfer spi_trans;
    memset(&spi_trans, 0, sizeof(spi_trans));
    spi_trans.tx_buf = (unsigned long) tx_buf;  //  Transmit Buffer
    spi_trans.rx_buf = (unsigned long) rx_buf;  //  Receive Buffer
    spi_trans.cs_change = true;   //  Set SPI Chip Select to Low
    spi_trans.len       = len;    //  How many bytes
    printf("spi tx: "); for (int i = 0; i < len; i++) { printf("%02x ", tx_buf[i]); } printf("\n");

    //  Transfer and receive the SPI buffers
    int rc = ioctl(spi, SPI_IOC_MESSAGE(1), &spi_trans);
    assert(rc >= 0);
    assert(rc == len);

    printf("spi rx: "); for (int i = 0; i < len; i++) { printf("%02x ", rx_buf[i]); } printf("\n");
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movw %dx, %ax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movw %ax, -0x12(%rbp)
cmpl $0x0, 0x7612(%rip)      # 0xecf0
jle 0x76e2
jmp 0x7701
leaq 0x24ab(%rip), %rdi      # 0x9b94
leaq 0x2043(%rip), %rsi      # 0x9733
movl $0x29f, %edx            # imm = 0x29F
leaq 0x24a0(%rip), %rcx      # 0x9b9c
callq 0x2130
movzwl -0x12(%rbp), %eax
cmpl $0x0, %eax
jle 0x770c
jmp 0x772b
leaq 0x23fd(%rip), %rdi      # 0x9b10
leaq 0x2019(%rip), %rsi      # 0x9733
movl $0x2a0, %edx            # imm = 0x2A0
leaq 0x2476(%rip), %rcx      # 0x9b9c
callq 0x2130
movzwl -0x12(%rbp), %eax
cmpl $0x1f, %eax
jg 0x7736
jmp 0x7755
leaq 0x2496(%rip), %rdi      # 0x9bd3
leaq 0x1fef(%rip), %rsi      # 0x9733
movl $0x2a1, %edx            # imm = 0x2A1
leaq 0x244c(%rip), %rcx      # 0x9b9c
callq 0x2130
leaq -0x38(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x2100
movq -0x8(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
movb $0x1, -0x1d(%rbp)
movzwl -0x12(%rbp), %eax
movl %eax, -0x28(%rbp)
leaq 0x2456(%rip), %rdi      # 0x9bdd
movb $0x0, %al
callq 0x2050
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movzwl -0x12(%rbp), %ecx
cmpl %ecx, %eax
jge 0x77c5
movq -0x8(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movzbl (%rax,%rcx), %esi
leaq 0x18df(%rip), %rdi      # 0x9092
movb $0x0, %al
callq 0x2050
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x7795
leaq 0x18c4(%rip), %rdi      # 0x9090
movb $0x0, %al
callq 0x2050
movl 0x7517(%rip), %edi      # 0xecf0
movl $0x40206b00, %esi       # imm = 0x40206B00
leaq -0x38(%rbp), %rdx
movb $0x0, %al
callq 0x2220
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
jl 0x77f4
jmp 0x7813
leaq 0x23eb(%rip), %rdi      # 0x9be6
leaq 0x1f31(%rip), %rsi      # 0x9733
movl $0x2ae, %edx            # imm = 0x2AE
leaq 0x238e(%rip), %rcx      # 0x9b9c
callq 0x2130
movl -0x40(%rbp), %eax
movzwl -0x12(%rbp), %ecx
cmpl %ecx, %eax
jne 0x7820
jmp 0x783f
leaq 0x23c7(%rip), %rdi      # 0x9bee
leaq 0x1f05(%rip), %rsi      # 0x9733
movl $0x2af, %edx            # imm = 0x2AF
leaq 0x2362(%rip), %rcx      # 0x9b9c
callq 0x2130
leaq 0x23b2(%rip), %rdi      # 0x9bf8
movb $0x0, %al
callq 0x2050
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movzwl -0x12(%rbp), %ecx
cmpl %ecx, %eax
jge 0x7884
movq -0x10(%rbp), %rax
movslq -0x44(%rbp), %rcx
movzbl (%rax,%rcx), %esi
leaq 0x1820(%rip), %rdi      # 0x9092
movb $0x0, %al
callq 0x2050
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x7854
leaq 0x1805(%rip), %rdi      # 0x9090
movb $0x0, %al
callq 0x2050
xorl %eax, %eax
addq $0x50, %rsp
popq %rbp
retq
nopw (%rax,%rax)