#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 uint16_t;
struct spi_ioc_transfer {
  unsigned long tx_buf;
  unsigned long rx_buf;
  int len;
  int speed_hz;
  int bits_per_word;
  int /*<<< orphan*/ delay_usecs;
};

/* Variables and functions */
int /*<<< orphan*/ SPI_IOC_MESSAGE(int);
scalar_t__ errno;
int /*<<< orphan*/ error(int /*<<< orphan*/, scalar_t__, char *, int);
int ioctl(int, int /*<<< orphan*/, struct spi_ioc_transfer *);
uint16_t spiTransfer(int fd, int port) {
  int result, i;
  uint16_t retVal;
  uint8_t tx[] = {
      0x01,
      0x00,
      0x00,
  };
  uint8_t rx[] = {
      0,
      0,
      0,
  };
  int txLen = 3;
  struct spi_ioc_transfer tr = {
      .tx_buf = (unsigned long)tx,
      .rx_buf = (unsigned long)rx,
      .len = txLen,
      .delay_usecs = (0),
      .speed_hz = (1000000),
      .bits_per_word = (8),
  };
  tx[1] = 0x80 | ((port & 0x7) << 4);
  errno = 0;
  result = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
  if (result < 1) {
    error(0, errno, "In spiTransfer, ioctl() returned 0x%x\n", result);
    retVal = 0;
  } else {
    retVal = (((rx[1] & 3) << 8) | rx[2]);
  }
  return (retVal);
}


x86 assembly (clang, -O0):
<spiTransfer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    0xeab(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xea9(%rip),%eax
mov    %eax,-0x18(%rbp)
lea    -0x2c(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1030 <memset@plt>
movl   $0x3,-0x30(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x2c(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x40(%rbp)
movl   $0xf4240,-0x3c(%rbp)
movl   $0x8,-0x38(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x8(%rbp),%eax
and    $0x7,%eax
shl    $0x4,%eax
or     $0x80,%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e24(%rip),%rax
movq   $0x0,(%rax)
mov    -0x4(%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>
mov    %eax,-0xc(%rbp)
cmpl   $0x1,-0xc(%rbp)
jge    1215 <spiTransfer+0xd5>
mov    0x2deb(%rip),%rax
mov    (%rax),%rsi
mov    -0xc(%rbp),%ecx
xor    %edi,%edi
lea    0xe08(%rip),%rdx
call   1060 <error@plt>
movl   $0x0,-0x14(%rbp)
jmp    1224 <spiTransfer+0xe4>
mov    -0x28(%rbp),%eax
and    $0x3,%eax
shl    $0x8,%eax
or     -0x24(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spiTransfer>:
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %edi,%ebx
mov    0xed9(%rip),%eax
mov    %eax,0x20(%rsp)
mov    0xec6(%rip),%rax
mov    %rax,0x18(%rsp)
movl   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
lea    0x18(%rsp),%rax
mov    %rax,0x28(%rsp)
lea    0x8(%rsp),%rax
mov    %rax,0x30(%rsp)
movaps 0xe85(%rip),%xmm0
movups %xmm0,0x38(%rsp)
or     $0x8,%esi
shl    $0x4,%esi
movzbl %sil,%eax
mov    %eax,0x1c(%rsp)
mov    0x2e4b(%rip),%r14
movq   $0x0,(%r14)
mov    $0x1,%edi
call   1030 <SPI_IOC_MESSAGE@plt>
lea    0x28(%rsp),%rdx
mov    %ebx,%edi
mov    %eax,%esi
call   1040 <ioctl@plt>
test   %eax,%eax
jle    11c8 <spiTransfer+0x98>
mov    0xc(%rsp),%ebx
and    $0x3,%ebx
shl    $0x8,%ebx
or     0x10(%rsp),%ebx
jmp    11dd <spiTransfer+0xad>
mov    (%r14),%rsi
lea    0xe4a(%rip),%rdx
xor    %ebx,%ebx
xor    %edi,%edi
mov    %eax,%ecx
call   1050 <error@plt>
mov    %ebx,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.spi_ioc_transfer = type { i64, i64, i32, i32, i32, i32 }
@__const.spiTransfer.tx = [3 x i32] [i32 1, i32 0, i32 0]
@errno = global i64 0
@.str = [39 x i8] c"In spiTransfer, ioctl() returned 0x%x\0A\00"
define i32 @spiTransfer(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [3 x i32]
%9 = alloca [3 x i32]
%10 = alloca i32
%11 = alloca %struct.spi_ioc_transfer
store i32 %0, i32* %3
store i32 %1, i32* %4
%12 = bitcast [3 x i32]* %8 to i8*
%13 = bitcast [3 x i32]* %9 to i8*
store i32 3, i32* %10
%14 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %11, i32 0, i32 0
%15 = getelementptr inbounds [3 x i32], [3 x i32]* %8, i64 0, i64 0
%16 = ptrtoint i32* %15 to i64
store i64 %16, i64* %14
%17 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %11, i32 0, i32 1
%18 = getelementptr inbounds [3 x i32], [3 x i32]* %9, i64 0, i64 0
%19 = ptrtoint i32* %18 to i64
store i64 %19, i64* %17
%20 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %11, i32 0, i32 2
%21 = load i32, i32* %10
store i32 %21, i32* %20
%22 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %11, i32 0, i32 3
store i32 1000000, i32* %22
%23 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %11, i32 0, i32 4
store i32 8, i32* %23
%24 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %11, i32 0, i32 5
store i32 0, i32* %24
%25 = load i32, i32* %4
%26 = and i32 %25, 7
%27 = shl i32 %26, 4
%28 = or i32 128, %27
%29 = getelementptr inbounds [3 x i32], [3 x i32]* %8, i64 0, i64 1
store i32 %28, i32* %29
store i64 0, i64* @errno
%30 = load i32, i32* %3
%31 = call i32 @SPI_IOC_MESSAGE(i32 1)
%32 = call i32 @ioctl(i32 %30, i32 %31, %struct.spi_ioc_transfer* %11)
store i32 %32, i32* %5
%33 = load i32, i32* %5
%34 = icmp slt i32 %33, 1
br i1 %34, label %35, label %39
35: ; preds = %2
%36 = load i64, i64* @errno
%37 = load i32, i32* %5
%38 = call i32 @error(i32 0, i64 %36, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %37)
store i32 0, i32* %7
br label %47
39: ; preds = %2
%40 = getelementptr inbounds [3 x i32], [3 x i32]* %9, i64 0, i64 1
%41 = load i32, i32* %40
%42 = and i32 %41, 3
%43 = shl i32 %42, 8
%44 = getelementptr inbounds [3 x i32], [3 x i32]* %9, i64 0, i64 2
%45 = load i32, i32* %44
%46 = or i32 %43, %45
store i32 %46, i32* %7
br label %47
47: ; preds = %39, %35
%48 = load i32, i32* %7
ret i32 %48
}

LLVM IR (-O2):
%struct.spi_ioc_transfer = type { i64, i64, i32, i32, i32, i32 }
@__const.spiTransfer.tx = [3 x i32] [i32 1, i32 0, i32 0]
@errno = global i64 0
@.str = [39 x i8] c"In spiTransfer, ioctl() returned 0x%x\0A\00"
define i32 @spiTransfer(i32 %0, i32 %1) {
%3 = alloca [3 x i32]
%4 = alloca [3 x i32]
%5 = alloca %struct.spi_ioc_transfer
%6 = bitcast [3 x i32]* %3 to i8*
%7 = bitcast [3 x i32]* %4 to i8*
%8 = bitcast %struct.spi_ioc_transfer* %5 to i8*
%9 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %5, i64 0, i32 0
%10 = ptrtoint [3 x i32]* %3 to i64
store i64 %10, i64* %9
%11 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %5, i64 0, i32 1
%12 = ptrtoint [3 x i32]* %4 to i64
store i64 %12, i64* %11
%13 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %5, i64 0, i32 2
%14 = bitcast i32* %13 to <4 x i32>*
store <4 x i32> <i32 3, i32 1000000, i32 8, i32 0>, <4 x i32>* %14
%15 = shl i32 %1, 4
%16 = and i32 %15, 112
%17 = or i32 %16, 128
%18 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 1
store i32 %17, i32* %18
store i64 0, i64* @errno
%19 = call i32 @SPI_IOC_MESSAGE(i32 1)
%20 = call i32 @ioctl(i32 %0, i32 %19, %struct.spi_ioc_transfer* %5)
%21 = icmp slt i32 %20, 1
br i1 %21, label %22, label %25
22: ; preds = %2
%23 = load i64, i64* @errno
%24 = call i32 @error(i32 0, i64 %23, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %20)
br label %33
25: ; preds = %2
%26 = getelementptr inbounds [3 x i32], [3 x i32]* %4, i64 0, i64 1
%27 = load i32, i32* %26
%28 = shl i32 %27, 8
%29 = and i32 %28, 768
%30 = getelementptr inbounds [3 x i32], [3 x i32]* %4, i64 0, i64 2
%31 = load i32, i32* %30
%32 = or i32 %29, %31
br label %33
33: ; preds = %25, %22
%34 = phi i32 [ 0, %22 ], [ %32, %25 ]
ret i32 %34
}