#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 uint8_t;
typedef int /*<<< orphan*/ uint64_t;
typedef int uint32_t;
struct spi_ioc_transfer {
  int len;
  int bits_per_word;
  scalar_t__ cs_change;
  scalar_t__ delay_usecs;
  scalar_t__ speed_hz;
  int /*<<< orphan*/ *rx_buf;
  int /*<<< orphan*/ *tx_buf;
};

/* Variables and functions */
int /*<<< orphan*/ SPI_IOC_MESSAGE(int);
int SPI_getBitsPerWord(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct spi_ioc_transfer *);
int SPI_transfer(int spidev_fd, void *tx_buffer, void *rx_buffer, int n_words) {
  uint8_t bits_per_word;
  uint32_t n_bytes;
  struct spi_ioc_transfer transfer;
  bits_per_word = SPI_getBitsPerWord(spidev_fd);
  if (bits_per_word < 0)
    return bits_per_word;
  n_bytes = (uint32_t)(((float)(bits_per_word * n_words)) / 8.0 + 0.5);
  if (!n_bytes)
    return 0;
  if (n_bytes > 4096)
    n_bytes = 4096;
  transfer.tx_buf = (uint64_t *)tx_buffer;
  transfer.rx_buf = (uint64_t *)rx_buffer;
  transfer.len = n_bytes;
  transfer.speed_hz = 0;
  transfer.delay_usecs = 0;
  transfer.bits_per_word = bits_per_word;
  transfer.cs_change = 0;
  if (ioctl(spidev_fd, SPI_IOC_MESSAGE(1), &transfer) < 0)
    return -1;
  return (n_bytes << 3) / bits_per_word;
}


x86 assembly (clang, -O0):
<SPI_transfer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <SPI_getBitsPerWord@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1166 <SPI_transfer+0x36>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1233 <SPI_transfer+0x103>
mov    -0x20(%rbp),%eax
imul   -0x1c(%rbp),%eax
cvtsi2ss %eax,%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe8b(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  0xe77(%rip),%xmm1
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    11aa <SPI_transfer+0x7a>
movl   $0x0,-0x4(%rbp)
jmp    1233 <SPI_transfer+0x103>
cmpl   $0x1000,-0x24(%rbp)
jle    11be <SPI_transfer+0x8e>
movl   $0x1000,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x58(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x54(%rbp)
movq   $0x0,-0x50(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    $0x1,%edi
call   1030 <SPI_IOC_MESSAGE@plt>
mov    -0x5c(%rbp),%edi
mov    %eax,%esi
lea    -0x58(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    1226 <SPI_transfer+0xf6>
movl   $0xffffffff,-0x4(%rbp)
jmp    1233 <SPI_transfer+0x103>
mov    -0x24(%rbp),%eax
shl    $0x3,%eax
cltd
idivl  -0x20(%rbp)
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SPI_transfer>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %ecx,%ebp
mov    %rdx,%r15
mov    %rsi,%r12
mov    %edi,%r14d
call   1050 <SPI_getBitsPerWord@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11dd <SPI_transfer+0xad>
mov    %ebx,%eax
imul   %ebp,%eax
cvtsi2ss %eax,%xmm0
cvtss2sd %xmm0,%xmm0
mulsd  0xe95(%rip),%xmm0
addsd  0xe95(%rip),%xmm0
cvttsd2si %xmm0,%eax
test   %eax,%eax
je     11d4 <SPI_transfer+0xa4>
cmp    $0x1000,%eax
mov    $0x1000,%ebp
cmovl  %eax,%ebp
mov    %r12,0x28(%rsp)
mov    %r15,0x20(%rsp)
mov    %ebp,(%rsp)
xorpd  %xmm0,%xmm0
movupd %xmm0,0x10(%rsp)
mov    %ebx,0x4(%rsp)
movq   $0x0,0x8(%rsp)
mov    $0x1,%edi
call   1030 <SPI_IOC_MESSAGE@plt>
mov    %rsp,%rdx
mov    %r14d,%edi
mov    %eax,%esi
call   1040 <ioctl@plt>
test   %rax,%rax
js     11d8 <SPI_transfer+0xa8>
shl    $0x3,%ebp
mov    %ebp,%eax
cltd
idiv   %ebx
mov    %eax,%ebx
jmp    11dd <SPI_transfer+0xad>
xor    %ebx,%ebx
jmp    11dd <SPI_transfer+0xad>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.spi_ioc_transfer = type { i32, i32, i64, i64, i64, i32*, i32* }
define i32 @SPI_transfer(i32 %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.spi_ioc_transfer
store i32 %0, i32* %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%13 = load i32, i32* %6
%14 = call i32 @SPI_getBitsPerWord(i32 %13)
store i32 %14, i32* %10
%15 = load i32, i32* %10
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %4
%18 = load i32, i32* %10
store i32 %18, i32* %5
br label %59
19: ; preds = %4
%20 = load i32, i32* %10
%21 = load i32, i32* %9
%22 = mul nsw i32 %20, %21
%23 = sitofp i32 %22 to float
%24 = fpext float %23 to double
%25 = fdiv double %24, 8.000000e+00
%26 = fadd double %25, 5.000000e-01
%27 = fptosi double %26 to i32
store i32 %27, i32* %11
%28 = load i32, i32* %11
%29 = icmp ne i32 %28, 0
br i1 %29, label %31, label %30
30: ; preds = %19
store i32 0, i32* %5
br label %59
31: ; preds = %19
%32 = load i32, i32* %11
%33 = icmp sgt i32 %32, 4096
br i1 %33, label %34, label %35
34: ; preds = %31
store i32 4096, i32* %11
br label %35
35: ; preds = %34, %31
%36 = load i8*, i8** %7
%37 = bitcast i8* %36 to i32*
%38 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %12, i32 0, i32 6
store i32* %37, i32** %38
%39 = load i8*, i8** %8
%40 = bitcast i8* %39 to i32*
%41 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %12, i32 0, i32 5
store i32* %40, i32** %41
%42 = load i32, i32* %11
%43 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %12, i32 0, i32 0
store i32 %42, i32* %43
%44 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %12, i32 0, i32 4
store i64 0, i64* %44
%45 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %12, i32 0, i32 3
store i64 0, i64* %45
%46 = load i32, i32* %10
%47 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %12, i32 0, i32 1
store i32 %46, i32* %47
%48 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %12, i32 0, i32 2
store i64 0, i64* %48
%49 = load i32, i32* %6
%50 = call i32 @SPI_IOC_MESSAGE(i32 1)
%51 = call i64 @ioctl(i32 %49, i32 %50, %struct.spi_ioc_transfer* %12)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %35
store i32 -1, i32* %5
br label %59
54: ; preds = %35
%55 = load i32, i32* %11
%56 = shl i32 %55, 3
%57 = load i32, i32* %10
%58 = sdiv i32 %56, %57
store i32 %58, i32* %5
br label %59
59: ; preds = %54, %53, %30, %17
%60 = load i32, i32* %5
ret i32 %60
}

LLVM IR (-O2):
%struct.spi_ioc_transfer = type { i32, i32, i64, i64, i64, i32*, i32* }
define i32 @SPI_transfer(i32 %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca %struct.spi_ioc_transfer
%6 = bitcast %struct.spi_ioc_transfer* %5 to i8*
%7 = tail call i32 @SPI_getBitsPerWord(i32 %0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %35, label %9
9: ; preds = %4
%10 = mul nsw i32 %7, %3
%11 = sitofp i32 %10 to float
%12 = fpext float %11 to double
%13 = fmul double %12, 1.250000e-01
%14 = fadd double %13, 5.000000e-01
%15 = fptosi double %14 to i32
%16 = icmp eq i32 %15, 0
br i1 %16, label %35, label %17
17: ; preds = %9
%18 = icmp slt i32 %15, 4096
%19 = select i1 %18, i32 %15, i32 4096
%20 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %5, i64 0, i32 6
%21 = bitcast i32** %20 to i8**
store i8* %1, i8** %21
%22 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %5, i64 0, i32 5
%23 = bitcast i32** %22 to i8**
store i8* %2, i8** %23
%24 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %5, i64 0, i32 0
store i32 %19, i32* %24
%25 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %5, i64 0, i32 3
%26 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %5, i64 0, i32 1
%27 = bitcast i64* %25 to i8*
store i32 %7, i32* %26
%28 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %5, i64 0, i32 2
store i64 0, i64* %28
%29 = tail call i32 @SPI_IOC_MESSAGE(i32 1)
%30 = call i64 @ioctl(i32 %0, i32 %29, %struct.spi_ioc_transfer* %5)
%31 = icmp slt i64 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %17
%33 = shl i32 %19, 3
%34 = sdiv i32 %33, %7
br label %35
35: ; preds = %17, %9, %4, %32
%36 = phi i32 [ %34, %32 ], [ %7, %4 ], [ 0, %9 ], [ -1, %17 ]
ret i32 %36
}