#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*/ memset(struct spi_ioc_transfer *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int spi_write_twice(int fd, uint8_t *buf1, int len1, uint8_t *buf2, int len2) {
  int ret = 0;
  struct spi_ioc_transfer tr[2];
  memset(tr, 0, sizeof(struct spi_ioc_transfer) * 2);
  tr[0].tx_buf = (unsigned long)buf1;
  tr[0].len = len1;
  tr[1].tx_buf = (unsigned long)buf2;
  tr[1].len = len2;
  ret = ioctl(fd, SPI_IOC_MESSAGE(2), tr);
  if (ret < 0) {
    perror("spi write twice failed.");
    return -1;
  }
  return ret;
}


x86 assembly (clang, -O0):
<spi_write_twice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    $0x2,%edi
call   1040 <SPI_IOC_MESSAGE@plt>
mov    -0x54(%rbp),%edi
mov    %eax,%esi
lea    -0x50(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11d0 <spi_write_twice+0x90>
lea    0xe41(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d6 <spi_write_twice+0x96>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spi_write_twice>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%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    $0x20,%edx
call   1030 <memset@plt>
mov    %rbx,(%rsp)
mov    %ebp,0x8(%rsp)
mov    %r15,0x10(%rsp)
mov    %r14d,0x18(%rsp)
mov    $0x2,%edi
call   1040 <SPI_IOC_MESSAGE@plt>
mov    %r12d,%edi
mov    %eax,%esi
mov    %r13,%rdx
call   1050 <ioctl@plt>
test   %eax,%eax
js     11aa <spi_write_twice+0x6a>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe4f(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    119b <spi_write_twice+0x5b>

LLVM IR (-O0):
%struct.spi_ioc_transfer = type { i64, i32 }
@.str = [24 x i8] c"spi write twice failed.\00"
define i32 @spi_write_twice(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 i32
%12 = alloca i32
%13 = alloca [2 x %struct.spi_ioc_transfer]
store i32 %0, i32* %7
store i32* %1, i32** %8
store i32 %2, i32* %9
store i32* %3, i32** %10
store i32 %4, i32* %11
store i32 0, i32* %12
%14 = getelementptr inbounds [2 x %struct.spi_ioc_transfer], [2 x %struct.spi_ioc_transfer]* %13, i64 0, i64 0
%15 = call i32 @memset(%struct.spi_ioc_transfer* %14, i32 0, i32 32)
%16 = load i32*, i32** %8
%17 = ptrtoint i32* %16 to i64
%18 = getelementptr inbounds [2 x %struct.spi_ioc_transfer], [2 x %struct.spi_ioc_transfer]* %13, 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* %9
%21 = getelementptr inbounds [2 x %struct.spi_ioc_transfer], [2 x %struct.spi_ioc_transfer]* %13, 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** %10
%24 = ptrtoint i32* %23 to i64
%25 = getelementptr inbounds [2 x %struct.spi_ioc_transfer], [2 x %struct.spi_ioc_transfer]* %13, i64 0, i64 1
%26 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %25, i32 0, i32 0
store i64 %24, i64* %26
%27 = load i32, i32* %11
%28 = getelementptr inbounds [2 x %struct.spi_ioc_transfer], [2 x %struct.spi_ioc_transfer]* %13, 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* %7
%31 = call i32 @SPI_IOC_MESSAGE(i32 2)
%32 = getelementptr inbounds [2 x %struct.spi_ioc_transfer], [2 x %struct.spi_ioc_transfer]* %13, i64 0, i64 0
%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 ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %6
br label %40
38: ; preds = %5
%39 = load i32, i32* %12
store i32 %39, i32* %6
br label %40
40: ; preds = %38, %36
%41 = load i32, i32* %6
ret i32 %41
}

LLVM IR (-O2):
%struct.spi_ioc_transfer = type { i64, i32 }
@.str = [24 x i8] c"spi write twice failed.\00"
define i32 @spi_write_twice(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 = getelementptr inbounds [2 x %struct.spi_ioc_transfer], [2 x %struct.spi_ioc_transfer]* %6, i64 0, i64 0
%9 = call i32 @memset(%struct.spi_ioc_transfer* %8, i32 0, i32 32)
%10 = ptrtoint i32* %1 to i64
%11 = getelementptr inbounds [2 x %struct.spi_ioc_transfer], [2 x %struct.spi_ioc_transfer]* %6, i64 0, i64 0, i32 0
store i64 %10, i64* %11
%12 = 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* %12
%13 = ptrtoint i32* %3 to i64
%14 = getelementptr inbounds [2 x %struct.spi_ioc_transfer], [2 x %struct.spi_ioc_transfer]* %6, i64 0, i64 1, i32 0
store i64 %13, i64* %14
%15 = 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* %15
%16 = call i32 @SPI_IOC_MESSAGE(i32 2)
%17 = call i32 @ioctl(i32 %0, i32 %16, %struct.spi_ioc_transfer* %8)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %5
%20 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %5, %19
%22 = phi i32 [ -1, %19 ], [ %17, %5 ]
ret i32 %22
}