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

/* Variables and functions */
int /*<<< orphan*/ SPI_IOC_MESSAGE(int);
int ioctl(int, int /*<<< orphan*/, struct spi_ioc_transfer **);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int spi_write_then_read(int fd, uint8_t *wbuf, int wlen, uint8_t *rbuf,
                        int rlen) {
  struct spi_ioc_transfer tr[2];
  int ret = 0;
  memset((void *)tr, 0, sizeof(tr));
  tr[0].tx_buf = (unsigned long)wbuf;
  tr[0].len = wlen;
  tr[1].rx_buf = (unsigned long)rbuf;
  tr[1].len = rlen;
  ret = ioctl(fd, SPI_IOC_MESSAGE(2), &tr);
  if (ret < 0) {
    perror("spi write then read fail");
  }
  return ret;
}


x86 assembly (clang, -O0):
<spi_write_then_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
movl   $0x0,-0x64(%rbp)
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1030 <memset@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x58(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x68(%rbp)
mov    $0x2,%edi
call   1040 <SPI_IOC_MESSAGE@plt>
mov    -0x68(%rbp),%edi
mov    %eax,%esi
lea    -0x60(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0x0,-0x64(%rbp)
jge    11c4 <spi_write_then_read+0x84>
lea    0xe41(%rip),%rdi
call   1060 <perror@plt>
mov    -0x64(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spi_write_then_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r8d,%r14d
mov    %rcx,%r15
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r12d
mov    %rsp,%r13
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1030 <memset@plt>
mov    %rbx,(%rsp)
mov    %ebp,0x8(%rsp)
mov    %r15,0x28(%rsp)
mov    %r14d,0x20(%rsp)
mov    $0x2,%edi
call   1040 <SPI_IOC_MESSAGE@plt>
mov    %r12d,%edi
mov    %eax,%esi
mov    %r13,%rdx
call   1050 <ioctl@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11ae <spi_write_then_read+0x6e>
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe4b(%rip),%rdi
call   1060 <perror@plt>
jmp    119d <spi_write_then_read+0x5d>

LLVM IR (-O0):
%struct.spi_ioc_transfer = type { i64, i32, i64 }
@.str = [25 x i8] c"spi write then read fail\00"
define i32 @spi_write_then_read(i32 %0, i32* %1, i32 %2, i32* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca [2 x %struct.spi_ioc_transfer]
%12 = alloca i32
store i32 %0, i32* %6
store i32* %1, i32** %7
store i32 %2, i32* %8
store i32* %3, i32** %9
store i32 %4, i32* %10
store i32 0, i32* %12
%13 = getelementptr inbounds [2 x %struct.spi_ioc_transfer], [2 x %struct.spi_ioc_transfer]* %11, i64 0, i64 0
%14 = bitcast %struct.spi_ioc_transfer* %13 to i8*
%15 = call i32 @memset(i8* %14, i32 0, i32 48)
%16 = load i32*, i32** %7
%17 = ptrtoint i32* %16 to i64
%18 = getelementptr inbounds [2 x %struct.spi_ioc_transfer], [2 x %struct.spi_ioc_transfer]* %11, 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* %8
%21 = getelementptr inbounds [2 x %struct.spi_ioc_transfer], [2 x %struct.spi_ioc_transfer]* %11, i64 0, i64 0
%22 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %21, i32 0, i32 1
store i32 %20, i32* %22
%23 = load i32*, i32** %9
%24 = ptrtoint i32* %23 to i64
%25 = getelementptr inbounds [2 x %struct.spi_ioc_transfer], [2 x %struct.spi_ioc_transfer]* %11, i64 0, i64 1
%26 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %25, i32 0, i32 2
store i64 %24, i64* %26
%27 = load i32, i32* %10
%28 = getelementptr inbounds [2 x %struct.spi_ioc_transfer], [2 x %struct.spi_ioc_transfer]* %11, i64 0, i64 1
%29 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %28, i32 0, i32 1
store i32 %27, i32* %29
%30 = load i32, i32* %6
%31 = call i32 @SPI_IOC_MESSAGE(i32 2)
%32 = bitcast [2 x %struct.spi_ioc_transfer]* %11 to %struct.spi_ioc_transfer**
%33 = call i32 @ioctl(i32 %30, i32 %31, %struct.spi_ioc_transfer** %32)
store i32 %33, i32* %12
%34 = load i32, i32* %12
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %5
%37 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %38
38: ; preds = %36, %5
%39 = load i32, i32* %12
ret i32 %39
}

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