#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*/ xfer;
typedef int uint32_t;
struct spi_ioc_transfer {
  unsigned long tx_buf;
  unsigned long rx_buf;
  int len;
};

/* Variables and functions */
int /*<<< orphan*/ SPI_IOC_MESSAGE(int);
long ioctl(int, int /*<<< orphan*/, struct spi_ioc_transfer *);
int /*<<< orphan*/ memset(struct spi_ioc_transfer *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
uint32_t spidev_interface_transact(void *handle, const uint32_t data,
                                   const bool readback) {
  int *fd = (int *)handle;
  struct spi_ioc_transfer xfer[1];
  unsigned char txbuf[4];
  unsigned char rxbuf[4];
  memset(xfer, 0, sizeof(xfer));
  xfer[0].tx_buf = (unsigned long)txbuf;
  if (readback)
    xfer[0].rx_buf = (unsigned long)rxbuf;
  xfer[0].len = 4;
  txbuf[0] = (data >> 24);
  txbuf[1] = (data >> 16);
  txbuf[2] = (data >> 8);
  txbuf[3] = (data >> 0);
  long status = ioctl(*fd, SPI_IOC_MESSAGE(1), xfer);
  if (status < 0) {
    perror("SPI_IOC_MESSAGE");
  }
  return (((uint32_t)rxbuf[0]) << 24) | (((uint32_t)rxbuf[1]) << 16) |
         (((uint32_t)rxbuf[2]) << 8) | (((uint32_t)rxbuf[3]) << 0);
}


x86 assembly (clang, -O0):
<spidev_interface_transact>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
lea    -0x34(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     1184 <spidev_interface_transact+0x44>
lea    -0x38(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x4,-0x20(%rbp)
mov    -0xc(%rbp),%eax
sar    $0x18,%eax
mov    %al,-0x34(%rbp)
mov    -0xc(%rbp),%eax
sar    $0x10,%eax
mov    %al,-0x33(%rbp)
mov    -0xc(%rbp),%eax
sar    $0x8,%eax
mov    %al,-0x32(%rbp)
mov    -0xc(%rbp),%eax
sar    $0x0,%eax
mov    %al,-0x31(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    $0x1,%edi
call   1040 <SPI_IOC_MESSAGE@plt>
mov    -0x44(%rbp),%edi
mov    %eax,%esi
lea    -0x30(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jge    11eb <spidev_interface_transact+0xab>
lea    0xe1a(%rip),%rdi
call   1060 <perror@plt>
movzbl -0x38(%rbp),%eax
shl    $0x18,%eax
movzbl -0x37(%rbp),%ecx
shl    $0x10,%ecx
or     %ecx,%eax
movzbl -0x36(%rbp),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
movzbl -0x35(%rbp),%ecx
shl    $0x0,%ecx
or     %ecx,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spidev_interface_transact>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %edx,%ebp
mov    %esi,%ebx
mov    %rdi,%r14
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
lea    0x8(%rsp),%rax
mov    %rax,0x10(%rsp)
test   %ebp,%ebp
je     1178 <spidev_interface_transact+0x38>
lea    0xc(%rsp),%rax
mov    %rax,0x18(%rsp)
movl   $0x4,0x20(%rsp)
mov    %ebx,%eax
shr    $0x18,%eax
mov    %al,0x8(%rsp)
mov    %ebx,%eax
shr    $0x10,%eax
mov    %al,0x9(%rsp)
mov    %bh,0xa(%rsp)
mov    %bl,0xb(%rsp)
mov    (%r14),%ebx
mov    $0x1,%edi
call   1040 <SPI_IOC_MESSAGE@plt>
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
mov    %eax,%esi
call   1050 <ioctl@plt>
test   %rax,%rax
js     11c9 <spidev_interface_transact+0x89>
mov    0xc(%rsp),%eax
bswap  %eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe30(%rip),%rdi
call   1060 <perror@plt>
jmp    11ba <spidev_interface_transact+0x7a>

LLVM IR (-O0):
%struct.spi_ioc_transfer = type { i64, i64, i32 }
@.str = [16 x i8] c"SPI_IOC_MESSAGE\00"
define i32 @spidev_interface_transact(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca [1 x %struct.spi_ioc_transfer]
%9 = alloca [4 x i8]
%10 = alloca [4 x i8]
%11 = alloca i64
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%12 = load i8*, i8** %4
%13 = bitcast i8* %12 to i32*
store i32* %13, i32** %7
%14 = getelementptr inbounds [1 x %struct.spi_ioc_transfer], [1 x %struct.spi_ioc_transfer]* %8, i64 0, i64 0
%15 = call i32 @memset(%struct.spi_ioc_transfer* %14, i32 0, i32 24)
%16 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
%17 = ptrtoint i8* %16 to i64
%18 = getelementptr inbounds [1 x %struct.spi_ioc_transfer], [1 x %struct.spi_ioc_transfer]* %8, i64 0, i64 0
%19 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %18, i32 0, i32 0
store i64 %17, i64* %19
%20 = load i32, i32* %6
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %3
%23 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%24 = ptrtoint i8* %23 to i64
%25 = getelementptr inbounds [1 x %struct.spi_ioc_transfer], [1 x %struct.spi_ioc_transfer]* %8, i64 0, i64 0
%26 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %25, i32 0, i32 1
store i64 %24, i64* %26
br label %27
27: ; preds = %22, %3
%28 = getelementptr inbounds [1 x %struct.spi_ioc_transfer], [1 x %struct.spi_ioc_transfer]* %8, i64 0, i64 0
%29 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %28, i32 0, i32 2
store i32 4, i32* %29
%30 = load i32, i32* %5
%31 = ashr i32 %30, 24
%32 = trunc i32 %31 to i8
%33 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
store i8 %32, i8* %33
%34 = load i32, i32* %5
%35 = ashr i32 %34, 16
%36 = trunc i32 %35 to i8
%37 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 1
store i8 %36, i8* %37
%38 = load i32, i32* %5
%39 = ashr i32 %38, 8
%40 = trunc i32 %39 to i8
%41 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 2
store i8 %40, i8* %41
%42 = load i32, i32* %5
%43 = ashr i32 %42, 0
%44 = trunc i32 %43 to i8
%45 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 3
store i8 %44, i8* %45
%46 = load i32*, i32** %7
%47 = load i32, i32* %46
%48 = call i32 @SPI_IOC_MESSAGE(i32 1)
%49 = getelementptr inbounds [1 x %struct.spi_ioc_transfer], [1 x %struct.spi_ioc_transfer]* %8, i64 0, i64 0
%50 = call i64 @ioctl(i32 %47, i32 %48, %struct.spi_ioc_transfer* %49)
store i64 %50, i64* %11
%51 = load i64, i64* %11
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %27
%54 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %55
55: ; preds = %53, %27
%56 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%57 = load i8, i8* %56
%58 = zext i8 %57 to i32
%59 = shl i32 %58, 24
%60 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 1
%61 = load i8, i8* %60
%62 = zext i8 %61 to i32
%63 = shl i32 %62, 16
%64 = or i32 %59, %63
%65 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 2
%66 = load i8, i8* %65
%67 = zext i8 %66 to i32
%68 = shl i32 %67, 8
%69 = or i32 %64, %68
%70 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 3
%71 = load i8, i8* %70
%72 = zext i8 %71 to i32
%73 = shl i32 %72, 0
%74 = or i32 %69, %73
ret i32 %74
}

LLVM IR (-O2):
%struct.spi_ioc_transfer = type { i64, i64, i32 }
@.str = [16 x i8] c"SPI_IOC_MESSAGE\00"
define i32 @spidev_interface_transact(i8* nocapture %0, i32 %1, i32 %2) {
%4 = alloca [1 x %struct.spi_ioc_transfer]
%5 = alloca [4 x i8]
%6 = alloca [4 x i8]
%7 = bitcast i8* %0 to i32*
%8 = bitcast [1 x %struct.spi_ioc_transfer]* %4 to i8*
%9 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%10 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%11 = getelementptr inbounds [1 x %struct.spi_ioc_transfer], [1 x %struct.spi_ioc_transfer]* %4, i64 0, i64 0
%12 = call i32 @memset(%struct.spi_ioc_transfer* %11, i32 0, i32 24)
%13 = ptrtoint [4 x i8]* %5 to i64
%14 = getelementptr inbounds [1 x %struct.spi_ioc_transfer], [1 x %struct.spi_ioc_transfer]* %4, i64 0, i64 0, i32 0
store i64 %13, i64* %14
%15 = icmp eq i32 %2, 0
br i1 %15, label %19, label %16
16: ; preds = %3
%17 = ptrtoint [4 x i8]* %6 to i64
%18 = getelementptr inbounds [1 x %struct.spi_ioc_transfer], [1 x %struct.spi_ioc_transfer]* %4, i64 0, i64 0, i32 1
store i64 %17, i64* %18
br label %19
19: ; preds = %16, %3
%20 = getelementptr inbounds [1 x %struct.spi_ioc_transfer], [1 x %struct.spi_ioc_transfer]* %4, i64 0, i64 0, i32 2
store i32 4, i32* %20
%21 = lshr i32 %1, 24
%22 = trunc i32 %21 to i8
store i8 %22, i8* %9
%23 = lshr i32 %1, 16
%24 = trunc i32 %23 to i8
%25 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 1
store i8 %24, i8* %25
%26 = lshr i32 %1, 8
%27 = trunc i32 %26 to i8
%28 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 2
store i8 %27, i8* %28
%29 = trunc i32 %1 to i8
%30 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 3
store i8 %29, i8* %30
%31 = load i32, i32* %7
%32 = call i32 @SPI_IOC_MESSAGE(i32 1)
%33 = call i64 @ioctl(i32 %31, i32 %32, %struct.spi_ioc_transfer* %11)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %19
%36 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %37
37: ; preds = %35, %19
%38 = load i8, i8* %10
%39 = zext i8 %38 to i32
%40 = shl nuw i32 %39, 24
%41 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 1
%42 = load i8, i8* %41
%43 = zext i8 %42 to i32
%44 = shl nuw nsw i32 %43, 16
%45 = or i32 %44, %40
%46 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 2
%47 = load i8, i8* %46
%48 = zext i8 %47 to i32
%49 = shl nuw nsw i32 %48, 8
%50 = or i32 %45, %49
%51 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 3
%52 = load i8, i8* %51
%53 = zext i8 %52 to i32
%54 = or i32 %50, %53
ret i32 %54
}