#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 rx_buf;
  int len;
  int bits_per_word;
  scalar_t__ cs_change;
  scalar_t__ delay_usecs;
  scalar_t__ speed_hz;
  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 /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
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;
  memset((void *)&transfer, 0, sizeof(struct spi_ioc_transfer));
  transfer.tx_buf = 0;
  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_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   1060 <SPI_getBitsPerWord@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1172 <SPI_read+0x32>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    124f <SPI_read+0x10f>
mov    -0x18(%rbp),%eax
imul   -0x14(%rbp),%eax
cvtsi2ss %eax,%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe7f(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  0xe6b(%rip),%xmm1
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11b6 <SPI_read+0x76>
movl   $0x0,-0x4(%rbp)
jmp    124f <SPI_read+0x10f>
cmpl   $0x1000,-0x1c(%rbp)
jle    11ca <SPI_read+0x8a>
movl   $0x1000,-0x1c(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1030 <memset@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x48(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x44(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    $0x1,%edi
call   1040 <SPI_IOC_MESSAGE@plt>
mov    -0x54(%rbp),%edi
mov    %eax,%esi
lea    -0x50(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    1242 <SPI_read+0x102>
movl   $0xffffffff,-0x4(%rbp)
jmp    124f <SPI_read+0x10f>
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   %r12
push   %rbx
sub    $0x30,%rsp
mov    %edx,%ebp
mov    %rsi,%r15
mov    %edi,%r14d
call   1060 <SPI_getBitsPerWord@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1200 <SPI_read+0xc0>
mov    %ebx,%eax
imul   %ebp,%eax
cvtsi2ss %eax,%xmm0
cvtss2sd %xmm0,%xmm0
mulsd  0xe88(%rip),%xmm0
addsd  0xe88(%rip),%xmm0
cvttsd2si %xmm0,%eax
test   %eax,%eax
je     11f7 <SPI_read+0xb7>
cmp    $0x1000,%eax
mov    $0x1000,%ebp
cmovl  %eax,%ebp
mov    %rsp,%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1030 <memset@plt>
movq   $0x0,0x28(%rsp)
mov    %r15,(%rsp)
mov    %ebp,0x8(%rsp)
xorpd  %xmm0,%xmm0
movupd %xmm0,0x18(%rsp)
mov    %ebx,0xc(%rsp)
movq   $0x0,0x10(%rsp)
mov    $0x1,%edi
call   1040 <SPI_IOC_MESSAGE@plt>
mov    %r14d,%edi
mov    %eax,%esi
mov    %r12,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
js     11fb <SPI_read+0xbb>
shl    $0x3,%ebp
mov    %ebp,%eax
cltd
idiv   %ebx
mov    %eax,%ebx
jmp    1200 <SPI_read+0xc0>
xor    %ebx,%ebx
jmp    1200 <SPI_read+0xc0>
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 { i64, i32, i32, i64, i64, i64, 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 %57
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 %57
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 = bitcast %struct.spi_ioc_transfer* %10 to i8*
%35 = call i32 @memset(i8* %34, i32 0, i32 48)
%36 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %10, i32 0, i32 6
store i64 0, i64* %36
%37 = load i8*, i8** %6
%38 = ptrtoint i8* %37 to i64
%39 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %10, i32 0, i32 0
store i64 %38, i64* %39
%40 = load i32, i32* %9
%41 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %10, i32 0, i32 1
store i32 %40, i32* %41
%42 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %10, i32 0, i32 5
store i64 0, i64* %42
%43 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %10, i32 0, i32 4
store i64 0, i64* %43
%44 = load i32, i32* %8
%45 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %10, i32 0, i32 2
store i32 %44, i32* %45
%46 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %10, i32 0, i32 3
store i64 0, i64* %46
%47 = load i32, i32* %5
%48 = call i32 @SPI_IOC_MESSAGE(i32 1)
%49 = call i64 @ioctl(i32 %47, i32 %48, %struct.spi_ioc_transfer* %10)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %33
store i32 -1, i32* %4
br label %57
52: ; preds = %33
%53 = load i32, i32* %9
%54 = shl i32 %53, 3
%55 = load i32, i32* %8
%56 = sdiv i32 %54, %55
store i32 %56, i32* %4
br label %57
57: ; preds = %52, %51, %28, %15
%58 = load i32, i32* %4
ret i32 %58
}

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