#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 {
  unsigned long tx_buf;
  int len;
};

/* Variables and functions */
int /*<<< orphan*/ SPI_IOC_MESSAGE(int);
int ioctl(int, int /*<<< orphan*/, struct spi_ioc_transfer *);
int /*<<< orphan*/ perror(char *);
int spi_write(int fd, uint8_t *buf, int len) {
  int ret = 0;
  struct spi_ioc_transfer tr = {
      .tx_buf = (unsigned long)buf,
      .len = len,
  };
  ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
  if (ret < 0) {
    perror("spi write then read fail");
  }
  return ret;
}


x86 assembly (clang, -O0):
<spi_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    $0x1,%edi
call   1030 <SPI_IOC_MESSAGE@plt>
mov    -0x2c(%rbp),%edi
mov    %eax,%esi
lea    -0x28(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    118e <spi_write+0x5e>
lea    0xe77(%rip),%rdi
call   1050 <perror@plt>
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spi_write>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    %rsi,(%rsp)
mov    %edx,0x8(%rsp)
mov    $0x1,%edi
call   1030 <SPI_IOC_MESSAGE@plt>
mov    %rsp,%rdx
mov    %ebx,%edi
mov    %eax,%esi
call   1040 <ioctl@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1163 <spi_write+0x33>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe96(%rip),%rdi
call   1050 <perror@plt>
jmp    115b <spi_write+0x2b>

LLVM IR (-O0):
%struct.spi_ioc_transfer = type { i64, i32 }
@.str = [25 x i8] c"spi write then read fail\00"
define i32 @spi_write(i32 %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.spi_ioc_transfer
store i32 %0, i32* %4
store i32* %1, i32** %5
store i32 %2, i32* %6
store i32 0, i32* %7
%9 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %8, i32 0, i32 0
%10 = load i32*, i32** %5
%11 = ptrtoint i32* %10 to i64
store i64 %11, i64* %9
%12 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %8, i32 0, i32 1
%13 = load i32, i32* %6
store i32 %13, i32* %12
%14 = load i32, i32* %4
%15 = call i32 @SPI_IOC_MESSAGE(i32 1)
%16 = call i32 @ioctl(i32 %14, i32 %15, %struct.spi_ioc_transfer* %8)
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %3
%20 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %3
%22 = load i32, i32* %7
ret i32 %22
}

LLVM IR (-O2):
%struct.spi_ioc_transfer = type { i64, i32 }
@.str = [25 x i8] c"spi write then read fail\00"
define i32 @spi_write(i32 %0, i32* %1, i32 %2) {
%4 = alloca %struct.spi_ioc_transfer
%5 = bitcast %struct.spi_ioc_transfer* %4 to i8*
%6 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %4, i64 0, i32 0
%7 = ptrtoint i32* %1 to i64
store i64 %7, i64* %6
%8 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %4, i64 0, i32 1
store i32 %2, i32* %8
%9 = tail call i32 @SPI_IOC_MESSAGE(i32 1)
%10 = call i32 @ioctl(i32 %0, i32 %9, %struct.spi_ioc_transfer* %4)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %3
ret i32 %10
}