#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 {
  uintptr_t tx_buf;
  uintptr_t rx_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*/ print_err(char *);
int /*<<< orphan*/ print_errno(char *);
int do_instruction(int fd, uint8_t tx[4], uint8_t rx[4]) {
  struct spi_ioc_transfer xfer;
  int res;
  memset(&xfer, 0, sizeof xfer);
  xfer.tx_buf = (uintptr_t)tx;
  xfer.rx_buf = (uintptr_t)rx;
  xfer.len = 4;
  res = ioctl(fd, SPI_IOC_MESSAGE(1), &xfer);
  if (res == 4)
    return 1;
  if (res < 0)
    print_errno("SPI_IOC_MESSAGE");
  else
    print_err("short response from SPI_IOC_MESSAGE");
  return 0;
}


x86 assembly (clang, -O0):
<do_instruction>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x4,-0x20(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    $0x1,%edi
call   1060 <SPI_IOC_MESSAGE@plt>
mov    -0x38(%rbp),%edi
mov    %eax,%esi
lea    -0x30(%rbp),%rdx
call   1070 <ioctl@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x4,-0x34(%rbp)
jne    11c1 <do_instruction+0x71>
movl   $0x1,-0x4(%rbp)
jmp    11ef <do_instruction+0x9f>
cmpl   $0x0,-0x34(%rbp)
jge    11dc <do_instruction+0x8c>
lea    0xe42(%rip),%rdi
call   1030 <print_errno@plt>
jmp    11e8 <do_instruction+0x98>
lea    0xe1d(%rip),%rdi
call   1040 <print_err@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_instruction>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    %rbx,(%rsp)
mov    %r14,0x8(%rsp)
movl   $0x4,0x10(%rsp)
mov    $0x1,%ebx
mov    $0x1,%edi
call   1060 <SPI_IOC_MESSAGE@plt>
mov    %ebp,%edi
mov    %eax,%esi
mov    %r15,%rdx
call   1070 <ioctl@plt>
cmp    $0x4,%eax
je     11c5 <do_instruction+0x75>
test   %eax,%eax
js     11b7 <do_instruction+0x67>
lea    0xe50(%rip),%rdi
call   1040 <print_err@plt>
jmp    11c3 <do_instruction+0x73>
lea    0xe56(%rip),%rdi
call   1030 <print_errno@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.spi_ioc_transfer = type { i64, i64, i32 }
@.str = [16 x i8] c"SPI_IOC_MESSAGE\00"
@.str.1 = [36 x i8] c"short response from SPI_IOC_MESSAGE\00"
define i32 @do_instruction(i32 %0, i32* %1, i32* %2) {
%4 = alloca %struct.spi_ioc_transfer
%5 = bitcast %struct.spi_ioc_transfer* %4 to i8*
%6 = call i32 @memset(%struct.spi_ioc_transfer* %4, i32 0, i32 24)
%7 = ptrtoint i32* %1 to i64
%8 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %4, i64 0, i32 0
store i64 %7, i64* %8
%9 = ptrtoint i32* %2 to i64
%10 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %4, i64 0, i32 1
store i64 %9, i64* %10
%11 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %4, i64 0, i32 2
store i32 4, i32* %11
%12 = call i32 @SPI_IOC_MESSAGE(i32 1)
%13 = call i32 @ioctl(i32 %0, i32 %12, %struct.spi_ioc_transfer* %4)
%14 = icmp eq i32 %13, 4
br i1 %14, label %21, label %15
15: ; preds = %3
%16 = icmp slt i32 %13, 0
br i1 %16, label %17, label %19
17: ; preds = %15
%18 = call i32 @print_errno(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %21
19: ; preds = %15
%20 = call i32 @print_err(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %17, %19, %3
%22 = phi i32 [ 1, %3 ], [ 0, %19 ], [ 0, %17 ]
ret i32 %22
}