#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;
  scalar_t__ 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_read(int spidev_fd, 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 = 0;
  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_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <SPI_getBitsPerWord@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1162 <SPI_read+0x32>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    122f <SPI_read+0xff>
mov    -0x18(%rbp),%eax
imul   -0x14(%rbp),%eax
cvtsi2ss %eax,%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe8f(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  0xe7b(%rip),%xmm1
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11a6 <SPI_read+0x76>
movl   $0x0,-0x4(%rbp)
jmp    122f <SPI_read+0xff>
cmpl   $0x1000,-0x1c(%rbp)
jle    11ba <SPI_read+0x8a>
movl   $0x1000,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x50(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4c(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    $0x1,%edi
call   1030 <SPI_IOC_MESSAGE@plt>
mov    -0x54(%rbp),%edi
mov    %eax,%esi
lea    -0x50(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    1222 <SPI_read+0xf2>
movl   $0xffffffff,-0x4(%rbp)
jmp    122f <SPI_read+0xff>
mov    -0x1c(%rbp),%eax
shl    $0x3,%eax
cltd
idivl  -0x18(%rbp)
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SPI_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %edx,%ebp
mov    %rsi,%r15
mov    %edi,%r14d
call   1050 <SPI_getBitsPerWord@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11df <SPI_read+0xaf>
mov    %ebx,%eax
imul   %ebp,%eax
cvtsi2ss %eax,%xmm0
cvtss2sd %xmm0,%xmm0
mulsd  0xe9a(%rip),%xmm0
addsd  0xe9a(%rip),%xmm0
cvttsd2si %xmm0,%eax
test   %eax,%eax
je     11d6 <SPI_read+0xa6>
cmp    $0x1000,%eax
mov    $0x1000,%ebp
cmovl  %eax,%ebp
movq   $0x0,0x30(%rsp)
mov    %r15,0x28(%rsp)
mov    %ebp,0x8(%rsp)
xorpd  %xmm0,%xmm0
movupd %xmm0,0x18(%rsp)
mov    %ebx,0xc(%rsp)
movq   $0x0,0x10(%rsp)
mov    $0x1,%edi
call   1030 <SPI_IOC_MESSAGE@plt>
lea    0x8(%rsp),%rdx
mov    %r14d,%edi
mov    %eax,%esi
call   1040 <ioctl@plt>
test   %rax,%rax
js     11da <SPI_read+0xaa>
shl    $0x3,%ebp
mov    %ebp,%eax
cltd
idiv   %ebx
mov    %eax,%ebx
jmp    11df <SPI_read+0xaf>
xor    %ebx,%ebx
jmp    11df <SPI_read+0xaf>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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