#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 */
struct spi_slave {
  int dummy;
};

/* Variables and functions */
int BIT(int);
int spi_xfer(struct spi_slave *, int, unsigned char *, unsigned char *, int);
int spi_w8r8(struct spi_slave *slave, unsigned char byte) {
  unsigned char dout[2];
  unsigned char din[2];
  int ret;
  dout[0] = byte;
  dout[1] = 0;
  ret = spi_xfer(slave, 16, dout, din, BIT(0) | BIT(1));
  return ret < 0 ? ret : din[1];
}


x86 assembly (clang, -O0):
<spi_w8r8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    -0x9(%rbp),%al
mov    %al,-0xb(%rbp)
movb   $0x0,-0xa(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0xb(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    -0xd(%rbp),%rax
mov    %rax,-0x20(%rbp)
xor    %edi,%edi
call   1030 <BIT@plt>
mov    %eax,-0x34(%rbp)
mov    $0x1,%edi
call   1030 <BIT@plt>
mov    -0x34(%rbp),%r8d
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
or     %eax,%r8d
mov    $0x10,%esi
call   1040 <spi_xfer@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    119d <spi_w8r8+0x7d>
mov    -0x14(%rbp),%eax
mov    %eax,-0x38(%rbp)
jmp    11a4 <spi_w8r8+0x84>
movzbl -0xc(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spi_w8r8>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    %sil,0x4(%rsp)
movb   $0x0,0x5(%rsp)
xor    %edi,%edi
call   1030 <BIT@plt>
mov    %eax,%ebp
mov    $0x1,%edi
call   1030 <BIT@plt>
or     %eax,%ebp
lea    0x4(%rsp),%rdx
lea    0x6(%rsp),%rcx
mov    %rbx,%rdi
mov    $0x10,%esi
mov    %ebp,%r8d
call   1040 <spi_xfer@plt>
test   %eax,%eax
movzbl 0x7(%rsp),%ecx
cmovns %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.spi_slave = type { i32 }
define i32 @spi_w8r8(%struct.spi_slave* %0, i8 zeroext %1) {
%3 = alloca %struct.spi_slave*
%4 = alloca i8
%5 = alloca [2 x i8]
%6 = alloca [2 x i8]
%7 = alloca i32
store %struct.spi_slave* %0, %struct.spi_slave** %3
store i8 %1, i8* %4
%8 = load i8, i8* %4
%9 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
store i8 %8, i8* %9
%10 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 1
store i8 0, i8* %10
%11 = load %struct.spi_slave*, %struct.spi_slave** %3
%12 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%13 = getelementptr inbounds [2 x i8], [2 x i8]* %6, i64 0, i64 0
%14 = call i32 @BIT(i32 0)
%15 = call i32 @BIT(i32 1)
%16 = or i32 %14, %15
%17 = call i32 @spi_xfer(%struct.spi_slave* %11, i32 16, i8* %12, i8* %13, i32 %16)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %2
%21 = load i32, i32* %7
br label %26
22: ; preds = %2
%23 = getelementptr inbounds [2 x i8], [2 x i8]* %6, i64 0, i64 1
%24 = load i8, i8* %23
%25 = zext i8 %24 to i32
br label %26
26: ; preds = %22, %20
%27 = phi i32 [ %21, %20 ], [ %25, %22 ]
ret i32 %27
}

LLVM IR (-O2):
%struct.spi_slave = type { i32 }
define i32 @spi_w8r8(%struct.spi_slave* %0, i8 zeroext %1) {
%3 = alloca [2 x i8]
%4 = alloca [2 x i8]
%5 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
store i8 %1, i8* %5
%7 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 1
store i8 0, i8* %7
%8 = tail call i32 @BIT(i32 0)
%9 = tail call i32 @BIT(i32 1)
%10 = or i32 %9, %8
%11 = call i32 @spi_xfer(%struct.spi_slave* %0, i32 16, i8* %5, i8* %6, i32 %10)
%12 = icmp slt i32 %11, 0
%13 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 1
%14 = load i8, i8* %13
%15 = zext i8 %14 to i32
%16 = select i1 %12, i32 %11, i32 %15
ret i32 %16
}