#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 uint32_t;
struct spi_ioc_transfer {
  uintptr_t tx_buf;
  uintptr_t rx_buf;
  int len;
  int bits_per_word;
  scalar_t__ cs_change;
  scalar_t__ delay_usecs;
  scalar_t__ speed_hz;
};

/* Variables and functions */
int /*<<< orphan*/ SPI_IOC_MESSAGE(int);
int SPI_getBitsPerWord(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct spi_ioc_transfer *);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
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;
  memset((void *)&transfer, 0, sizeof(struct spi_ioc_transfer));
  transfer.tx_buf = (uintptr_t)tx_buffer;
  transfer.rx_buf = (uintptr_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   1060 <SPI_getBitsPerWord@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1176 <SPI_transfer+0x36>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1253 <SPI_transfer+0x113>
mov    -0x20(%rbp),%eax
imul   -0x1c(%rbp),%eax
cvtsi2ss %eax,%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe7b(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  0xe67(%rip),%xmm1
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    11ba <SPI_transfer+0x7a>
movl   $0x0,-0x4(%rbp)
jmp    1253 <SPI_transfer+0x113>
cmpl   $0x1000,-0x24(%rbp)
jle    11ce <SPI_transfer+0x8e>
movl   $0x1000,-0x24(%rbp)
lea    -0x58(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1030 <memset@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x48(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x44(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    $0x1,%edi
call   1040 <SPI_IOC_MESSAGE@plt>
mov    -0x5c(%rbp),%edi
mov    %eax,%esi
lea    -0x58(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    1246 <SPI_transfer+0x106>
movl   $0xffffffff,-0x4(%rbp)
jmp    1253 <SPI_transfer+0x113>
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   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %ecx,%ebp
mov    %rdx,%r14
mov    %rsi,%r12
mov    %edi,%r15d
call   1060 <SPI_getBitsPerWord@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1204 <SPI_transfer+0xc4>
mov    %ebx,%eax
imul   %ebp,%eax
cvtsi2ss %eax,%xmm0
cvtss2sd %xmm0,%xmm0
mulsd  0xe83(%rip),%xmm0
addsd  0xe83(%rip),%xmm0
cvttsd2si %xmm0,%eax
test   %eax,%eax
je     11fb <SPI_transfer+0xbb>
cmp    $0x1000,%eax
mov    $0x1000,%ebp
cmovl  %eax,%ebp
lea    0x8(%rsp),%r13
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1030 <memset@plt>
mov    %r12,0x8(%rsp)
mov    %r14,0x10(%rsp)
mov    %ebp,0x18(%rsp)
xorpd  %xmm0,%xmm0
movupd %xmm0,0x28(%rsp)
mov    %ebx,0x1c(%rsp)
movq   $0x0,0x20(%rsp)
mov    $0x1,%edi
call   1040 <SPI_IOC_MESSAGE@plt>
mov    %r15d,%edi
mov    %eax,%esi
mov    %r13,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
js     11ff <SPI_transfer+0xbf>
shl    $0x3,%ebp
mov    %ebp,%eax
cltd
idiv   %ebx
mov    %eax,%ebx
jmp    1204 <SPI_transfer+0xc4>
xor    %ebx,%ebx
jmp    1204 <SPI_transfer+0xc4>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.spi_ioc_transfer = type { i64, i64, i32, i32, i64, i64, i64 }
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 %61
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 %61
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 = bitcast %struct.spi_ioc_transfer* %12 to i8*
%37 = call i32 @memset(i8* %36, i32 0, i32 48)
%38 = load i8*, i8** %7
%39 = ptrtoint i8* %38 to i64
%40 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %12, i32 0, i32 0
store i64 %39, i64* %40
%41 = load i8*, i8** %8
%42 = ptrtoint i8* %41 to i64
%43 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %12, i32 0, i32 1
store i64 %42, i64* %43
%44 = load i32, i32* %11
%45 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %12, i32 0, i32 2
store i32 %44, i32* %45
%46 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %12, i32 0, i32 6
store i64 0, i64* %46
%47 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %12, i32 0, i32 5
store i64 0, i64* %47
%48 = load i32, i32* %10
%49 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %12, i32 0, i32 3
store i32 %48, i32* %49
%50 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %12, i32 0, i32 4
store i64 0, i64* %50
%51 = load i32, i32* %6
%52 = call i32 @SPI_IOC_MESSAGE(i32 1)
%53 = call i64 @ioctl(i32 %51, i32 %52, %struct.spi_ioc_transfer* %12)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %35
store i32 -1, i32* %5
br label %61
56: ; preds = %35
%57 = load i32, i32* %11
%58 = shl i32 %57, 3
%59 = load i32, i32* %10
%60 = sdiv i32 %58, %59
store i32 %60, i32* %5
br label %61
61: ; preds = %56, %55, %30, %17
%62 = load i32, i32* %5
ret i32 %62
}

LLVM IR (-O2):
%struct.spi_ioc_transfer = type { i64, i64, i32, i32, i64, i64, i64 }
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 %36, 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 %36, label %17
17: ; preds = %9
%18 = icmp slt i32 %15, 4096
%19 = select i1 %18, i32 %15, i32 4096
%20 = call i32 @memset(i8* %6, i32 0, i32 48)
%21 = ptrtoint i8* %1 to i64
%22 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %5, i64 0, i32 0
store i64 %21, i64* %22
%23 = ptrtoint i8* %2 to i64
%24 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %5, i64 0, i32 1
store i64 %23, i64* %24
%25 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %5, i64 0, i32 2
store i32 %19, i32* %25
%26 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %5, i64 0, i32 5
%27 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %5, i64 0, i32 3
%28 = bitcast i64* %26 to i8*
store i32 %7, i32* %27
%29 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %5, i64 0, i32 4
store i64 0, i64* %29
%30 = call i32 @SPI_IOC_MESSAGE(i32 1)
%31 = call i64 @ioctl(i32 %0, i32 %30, %struct.spi_ioc_transfer* %5)
%32 = icmp slt i64 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %17
%34 = shl i32 %19, 3
%35 = sdiv i32 %34, %7
br label %36
36: ; preds = %17, %9, %4, %33
%37 = phi i32 [ %35, %33 ], [ %7, %4 ], [ 0, %9 ], [ -1, %17 ]
ret i32 %37
}