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

/* Variables and functions */
int /*<<< orphan*/ SPI_IOC_MESSAGE(int);
int ioctl(int, int /*<<< orphan*/, struct spi_ioc_transfer *);
int /*<<< orphan*/ perror(char *);
int spi_n(int fd, uint8_t *tx, uint8_t *rx, int len) {
  int ret;
  struct spi_ioc_transfer tr = {
      .tx_buf = (__u32)tx,
      .rx_buf = (__u32)rx,
      .len = len,
      .delay_usecs = 0,
      .speed_hz = 1000000,
      .bits_per_word = 8,
  };
  if ((ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr)) < 0) {
    perror("SPI transfer error");
    return ret;
  }
  return 0;
}


x86 assembly (clang, -O0):
<spi_n>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x38(%rbp)
movl   $0xf4240,-0x34(%rbp)
movl   $0x8,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    $0x1,%edi
call   1030 <SPI_IOC_MESSAGE@plt>
mov    -0x3c(%rbp),%edi
mov    %eax,%esi
lea    -0x38(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    11b0 <spi_n+0x80>
lea    0xe60(%rip),%rdi
call   1050 <perror@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b7 <spi_n+0x87>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spi_n>:
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
mov    %ecx,0x8(%rsp)
movabs $0x8000f4240,%rax
mov    %rax,0xc(%rsp)
movl   $0x0,0x14(%rsp)
mov    %edx,0x18(%rsp)
mov    %esi,0x1c(%rsp)
mov    $0x1,%edi
call   1030 <SPI_IOC_MESSAGE@plt>
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
mov    %eax,%esi
call   1040 <ioctl@plt>
mov    %eax,%ebx
xor    %eax,%eax
test   %ebx,%ebx
js     1180 <spi_n+0x50>
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe79(%rip),%rdi
call   1050 <perror@plt>
mov    %ebx,%eax
jmp    117a <spi_n+0x4a>

LLVM IR (-O0):
%struct.spi_ioc_transfer = type { i32, i32, i32, i32, i32, i32 }
@.str = [19 x i8] c"SPI transfer error\00"
define i32 @spi_n(i32 %0, i32* %1, i32* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.spi_ioc_transfer
store i32 %0, i32* %6
store i32* %1, i32** %7
store i32* %2, i32** %8
store i32 %3, i32* %9
%12 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %11, i32 0, i32 0
%13 = load i32, i32* %9
store i32 %13, i32* %12
%14 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %11, i32 0, i32 1
store i32 1000000, i32* %14
%15 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %11, i32 0, i32 2
store i32 8, i32* %15
%16 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %11, i32 0, i32 3
store i32 0, i32* %16
%17 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %11, i32 0, i32 4
%18 = load i32*, i32** %8
%19 = ptrtoint i32* %18 to i32
store i32 %19, i32* %17
%20 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %11, i32 0, i32 5
%21 = load i32*, i32** %7
%22 = ptrtoint i32* %21 to i32
store i32 %22, i32* %20
%23 = load i32, i32* %6
%24 = call i32 @SPI_IOC_MESSAGE(i32 1)
%25 = call i32 @ioctl(i32 %23, i32 %24, %struct.spi_ioc_transfer* %11)
store i32 %25, i32* %10
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %4
%28 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%29 = load i32, i32* %10
store i32 %29, i32* %5
br label %31
30: ; preds = %4
store i32 0, i32* %5
br label %31
31: ; preds = %30, %27
%32 = load i32, i32* %5
ret i32 %32
}

LLVM IR (-O2):
%struct.spi_ioc_transfer = type { i32, i32, i32, i32, i32, i32 }
@.str = [19 x i8] c"SPI transfer error\00"
define i32 @spi_n(i32 %0, i32* %1, i32* %2, i32 %3) {
%5 = alloca %struct.spi_ioc_transfer
%6 = bitcast %struct.spi_ioc_transfer* %5 to i8*
%7 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %5, i64 0, i32 0
store i32 %3, i32* %7
%8 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %5, i64 0, i32 1
store i32 1000000, i32* %8
%9 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %5, i64 0, i32 2
store i32 8, i32* %9
%10 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %5, i64 0, i32 3
store i32 0, i32* %10
%11 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %5, i64 0, i32 4
%12 = ptrtoint i32* %2 to i64
%13 = trunc i64 %12 to i32
store i32 %13, i32* %11
%14 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %5, i64 0, i32 5
%15 = ptrtoint i32* %1 to i64
%16 = trunc i64 %15 to i32
store i32 %16, i32* %14
%17 = tail call i32 @SPI_IOC_MESSAGE(i32 1)
%18 = call i32 @ioctl(i32 %0, i32 %17, %struct.spi_ioc_transfer* %5)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %4
%21 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %4, %20
%23 = phi i32 [ %18, %20 ], [ 0, %4 ]
ret i32 %23
}