#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ uint8_t;
typedef int /*<<< orphan*/ uint16_t;
struct spi_ioc_transfer {
  unsigned long tx_buf;
  unsigned long rx_buf;
  int cs_change;
  scalar_t__ delay_usecs;
  int /*<<< orphan*/ len;
};
typedef scalar_t__ int32_t;

/* Variables and functions */
int /*<<< orphan*/ SPI_IOC_MESSAGE(int);
scalar_t__ ioctl(scalar_t__ const, int /*<<< orphan*/,
                 struct spi_ioc_transfer *);
int32_t nrf24_spi_transfer(const int32_t handle, uint8_t *tx, uint8_t *rx,
                           const uint16_t len) {
  int32_t result = 0;
  struct spi_ioc_transfer tr = {
      .tx_buf = 0, .rx_buf = 0, .len = 0, .delay_usecs = 0, .cs_change = 0};
  if (handle < 0) {
    return -101;
  }
  if (tx == 0 || rx == 0) {
    return -100;
  }
  tr.tx_buf = (unsigned long)tx;
  tr.rx_buf = (unsigned long)rx;
  tr.len = len;
  tr.delay_usecs = 0;
  tr.cs_change = 1;
  result = ioctl(handle, SPI_IOC_MESSAGE(1), &tr);
  if (result < 1) {
    result = -104;
  }
  return result;
}


x86 assembly (clang, -O0):
<nrf24_spi_transfer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0x58(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
cmpq   $0x0,-0x10(%rbp)
jge    1177 <nrf24_spi_transfer+0x47>
movq   $0xffffffffffffff9b,-0x8(%rbp)
ff
jmp    11ff <nrf24_spi_transfer+0xcf>
cmpq   $0x0,-0x18(%rbp)
je     118d <nrf24_spi_transfer+0x5d>
cmpq   $0x0,-0x20(%rbp)
jne    119a <nrf24_spi_transfer+0x6a>
movq   $0xffffffffffffff9c,-0x8(%rbp)
ff
jmp    11ff <nrf24_spi_transfer+0xcf>
mov    -0x18(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movl   $0x1,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    $0x1,%edi
call   1040 <SPI_IOC_MESSAGE@plt>
mov    -0x60(%rbp),%rdi
mov    %eax,%esi
lea    -0x58(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x1,-0x30(%rbp)
jge    11f7 <nrf24_spi_transfer+0xc7>
movq   $0xffffffffffffff98,-0x30(%rbp)
ff
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nrf24_spi_transfer>:
push   %rbx
sub    $0x30,%rsp
xorps  %xmm0,%xmm0
movups %xmm0,0x18(%rsp)
movq   $0x0,0x28(%rsp)
test   %rdi,%rdi
js     1191 <nrf24_spi_transfer+0x71>
mov    $0xffffffffffffff9c,%rax
test   %rsi,%rsi
je     1198 <nrf24_spi_transfer+0x78>
test   %rdx,%rdx
je     1198 <nrf24_spi_transfer+0x78>
mov    %rdi,%rbx
mov    %rsi,0x8(%rsp)
mov    %rdx,0x10(%rsp)
mov    %ecx,0x28(%rsp)
movl   $0x1,0x18(%rsp)
mov    $0x1,%edi
call   1030 <SPI_IOC_MESSAGE@plt>
lea    0x8(%rsp),%rdx
mov    %rbx,%rdi
mov    %eax,%esi
call   1040 <ioctl@plt>
mov    %rax,%rcx
test   %rax,%rax
mov    $0xffffffffffffff98,%rax
cmovg  %rcx,%rax
jmp    1198 <nrf24_spi_transfer+0x78>
mov    $0xffffffffffffff9b,%rax
add    $0x30,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.spi_ioc_transfer = type { i64, i64, i32, i64, i32 }
define i64 @nrf24_spi_transfer(i64 %0, i32* %1, i32* %2, i32 %3) {
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i64
%11 = alloca %struct.spi_ioc_transfer
store i64 %0, i64* %6
store i32* %1, i32** %7
store i32* %2, i32** %8
store i32 %3, i32* %9
store i64 0, i64* %10
%12 = bitcast %struct.spi_ioc_transfer* %11 to i8*
%13 = load i64, i64* %6
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %4
store i64 -101, i64* %5
br label %42
16: ; preds = %4
%17 = load i32*, i32** %7
%18 = icmp eq i32* %17, null
br i1 %18, label %22, label %19
19: ; preds = %16
%20 = load i32*, i32** %8
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %23
22: ; preds = %19, %16
store i64 -100, i64* %5
br label %42
23: ; preds = %19
%24 = load i32*, i32** %7
%25 = ptrtoint i32* %24 to i64
%26 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %11, i32 0, i32 0
store i64 %25, i64* %26
%27 = load i32*, i32** %8
%28 = ptrtoint i32* %27 to i64
%29 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %11, i32 0, i32 1
store i64 %28, i64* %29
%30 = load i32, i32* %9
%31 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %11, i32 0, i32 4
store i32 %30, i32* %31
%32 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %11, i32 0, i32 3
store i64 0, i64* %32
%33 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %11, i32 0, i32 2
store i32 1, i32* %33
%34 = load i64, i64* %6
%35 = call i32 @SPI_IOC_MESSAGE(i32 1)
%36 = call i64 @ioctl(i64 %34, i32 %35, %struct.spi_ioc_transfer* %11)
store i64 %36, i64* %10
%37 = load i64, i64* %10
%38 = icmp slt i64 %37, 1
br i1 %38, label %39, label %40
39: ; preds = %23
store i64 -104, i64* %10
br label %40
40: ; preds = %39, %23
%41 = load i64, i64* %10
store i64 %41, i64* %5
br label %42
42: ; preds = %40, %22, %15
%43 = load i64, i64* %5
ret i64 %43
}

LLVM IR (-O2):
%struct.spi_ioc_transfer = type { i64, i64, i32, i64, i32 }
define i64 @nrf24_spi_transfer(i64 %0, i32* %1, i32* %2, i32 %3) {
%5 = alloca %struct.spi_ioc_transfer
%6 = bitcast %struct.spi_ioc_transfer* %5 to i8*
%7 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %5, i64 0, i32 2
%8 = bitcast i32* %7 to i8*
%9 = icmp slt i64 %0, 0
br i1 %9, label %25, label %10
10: ; preds = %4
%11 = icmp eq i32* %1, null
%12 = icmp eq i32* %2, null
%13 = or i1 %11, %12
br i1 %13, label %25, label %14
14: ; preds = %10
%15 = ptrtoint i32* %1 to i64
%16 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %5, i64 0, i32 0
store i64 %15, i64* %16
%17 = ptrtoint i32* %2 to i64
%18 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %5, i64 0, i32 1
store i64 %17, i64* %18
%19 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %5, i64 0, i32 4
store i32 %3, i32* %19
%20 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %5, i64 0, i32 2
store i32 1, i32* %20
%21 = tail call i32 @SPI_IOC_MESSAGE(i32 1)
%22 = call i64 @ioctl(i64 %0, i32 %21, %struct.spi_ioc_transfer* %5)
%23 = icmp slt i64 %22, 1
%24 = select i1 %23, i64 -104, i64 %22
br label %25
25: ; preds = %10, %4, %14
%26 = phi i64 [ %24, %14 ], [ -101, %4 ], [ -100, %10 ]
ret i64 %26
}