#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 uint8_t;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int spi_n(int, int *, int *, int);
int /*<<< orphan*/ stderr;
int send_cmd(int fd, uint8_t cmd, uint8_t size) {
  static uint8_t transaction_id = 0;
  int ret;
  uint8_t rx[3], tx[] = {cmd, transaction_id++, size};
  if ((ret = spi_n(fd, tx, rx, 3)) < 0)
    return ret;
  if (rx[0] != 0xBB) {
    fprintf(stderr, "SPI client wasn't expecting a command\n");
    return -1;
  }
  if (rx[1] > 10) {
    fprintf(stderr, "SPI client returned unexpected number of buffers\n");
    return -1;
  }
  if (rx[2] != tx[1]) {
    fprintf(stderr, "SPI client returned unexpected transaction id\n");
    return -1;
  }
  return 0xff & rx[1];
}


x86 assembly (clang, -O0):
<send_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2ef7(%rip),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,0x2eec(%rip)
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x2c(%rbp),%rsi
lea    -0x20(%rbp),%rdx
mov    $0x3,%ecx
call   1030 <spi_n@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    117d <send_cmd+0x5d>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120e <send_cmd+0xee>
cmpl   $0xbb,-0x20(%rbp)
je     11ab <send_cmd+0x8b>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
lea    0xe66(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120e <send_cmd+0xee>
cmpl   $0xa,-0x1c(%rbp)
jle    11d6 <send_cmd+0xb6>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
lea    0xe62(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120e <send_cmd+0xee>
mov    -0x18(%rbp),%eax
cmp    -0x28(%rbp),%eax
je     1203 <send_cmd+0xe3>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
lea    0xe67(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120e <send_cmd+0xee>
mov    -0x1c(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_cmd>:
sub    $0x18,%rsp
mov    %esi,(%rsp)
mov    0x2f07(%rip),%eax
lea    0x1(%rax),%ecx
mov    %ecx,0x2efe(%rip)
mov    %eax,0x4(%rsp)
mov    %edx,0x8(%rsp)
mov    %rsp,%rsi
lea    0xc(%rsp),%rdx
mov    $0x3,%ecx
call   1030 <spi_n@plt>
test   %eax,%eax
js     1198 <send_cmd+0x78>
cmpl   $0xbb,0xc(%rsp)
jne    1179 <send_cmd+0x59>
mov    0x10(%rsp),%eax
cmp    $0xb,%eax
jl     118b <send_cmd+0x6b>
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
lea    0xeb0(%rip),%rsi
jmp    11ad <send_cmd+0x8d>
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
lea    0xe77(%rip),%rsi
jmp    11ad <send_cmd+0x8d>
mov    0x14(%rsp),%ecx
cmp    0x4(%rsp),%ecx
jne    119d <send_cmd+0x7d>
movzbl %al,%eax
add    $0x18,%rsp
ret
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
lea    0xeac(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
@send_cmd.transaction_id = internal global i32 0
@stderr = global i32 0
@.str = [39 x i8] c"SPI client wasn't expecting a command\0A\00"
@.str.1 = [50 x i8] c"SPI client returned unexpected number of buffers\0A\00"
@.str.2 = [47 x i8] c"SPI client returned unexpected transaction id\0A\00"
define i32 @send_cmd(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [3 x i32]
%10 = alloca [3 x i32]
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%11 = getelementptr inbounds [3 x i32], [3 x i32]* %10, i64 0, i64 0
%12 = load i32, i32* %6
store i32 %12, i32* %11
%13 = getelementptr inbounds i32, i32* %11, i64 1
%14 = load i32, i32* @send_cmd.transaction_id
%15 = add nsw i32 %14, 1
store i32 %15, i32* @send_cmd.transaction_id
store i32 %14, i32* %13
%16 = getelementptr inbounds i32, i32* %13, i64 1
%17 = load i32, i32* %7
store i32 %17, i32* %16
%18 = load i32, i32* %5
%19 = getelementptr inbounds [3 x i32], [3 x i32]* %10, i64 0, i64 0
%20 = getelementptr inbounds [3 x i32], [3 x i32]* %9, i64 0, i64 0
%21 = call i32 @spi_n(i32 %18, i32* %19, i32* %20, i32 3)
store i32 %21, i32* %8
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %3
%24 = load i32, i32* %8
store i32 %24, i32* %4
br label %52
25: ; preds = %3
%26 = getelementptr inbounds [3 x i32], [3 x i32]* %9, i64 0, i64 0
%27 = load i32, i32* %26
%28 = icmp ne i32 %27, 187
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %52
32: ; preds = %25
%33 = getelementptr inbounds [3 x i32], [3 x i32]* %9, i64 0, i64 1
%34 = load i32, i32* %33
%35 = icmp sgt i32 %34, 10
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %52
39: ; preds = %32
%40 = getelementptr inbounds [3 x i32], [3 x i32]* %9, i64 0, i64 2
%41 = load i32, i32* %40
%42 = getelementptr inbounds [3 x i32], [3 x i32]* %10, i64 0, i64 1
%43 = load i32, i32* %42
%44 = icmp ne i32 %41, %43
br i1 %44, label %45, label %48
45: ; preds = %39
%46 = load i32, i32* @stderr
%47 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %52
48: ; preds = %39
%49 = getelementptr inbounds [3 x i32], [3 x i32]* %9, i64 0, i64 1
%50 = load i32, i32* %49
%51 = and i32 255, %50
store i32 %51, i32* %4
br label %52
52: ; preds = %48, %45, %36, %29, %23
%53 = load i32, i32* %4
ret i32 %53
}

LLVM IR (-O2):
@send_cmd.transaction_id = internal unnamed_addr global i32 0
@stderr = global i32 0
@.str = [39 x i8] c"SPI client wasn't expecting a command\0A\00"
@.str.1 = [50 x i8] c"SPI client returned unexpected number of buffers\0A\00"
@.str.2 = [47 x i8] c"SPI client returned unexpected transaction id\0A\00"
define i32 @send_cmd(i32 %0, i32 %1, i32 %2) {
%4 = alloca [3 x i32]
%5 = alloca [3 x i32]
%6 = bitcast [3 x i32]* %4 to i8*
%7 = bitcast [3 x i32]* %5 to i8*
%8 = getelementptr inbounds [3 x i32], [3 x i32]* %5, i64 0, i64 0
store i32 %1, i32* %8
%9 = getelementptr inbounds [3 x i32], [3 x i32]* %5, i64 0, i64 1
%10 = load i32, i32* @send_cmd.transaction_id
%11 = add nsw i32 %10, 1
store i32 %11, i32* @send_cmd.transaction_id
store i32 %10, i32* %9
%12 = getelementptr inbounds [3 x i32], [3 x i32]* %5, i64 0, i64 2
store i32 %2, i32* %12
%13 = getelementptr inbounds [3 x i32], [3 x i32]* %4, i64 0, i64 0
%14 = call i32 @spi_n(i32 %0, i32* %8, i32* %13, i32 3)
%15 = icmp slt i32 %14, 0
br i1 %15, label %39, label %16
16: ; preds = %3
%17 = load i32, i32* %13
%18 = icmp eq i32 %17, 187
br i1 %18, label %22, label %19
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
br label %39
22: ; preds = %16
%23 = getelementptr inbounds [3 x i32], [3 x i32]* %4, i64 0, i64 1
%24 = load i32, i32* %23
%25 = icmp sgt i32 %24, 10
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0))
br label %39
29: ; preds = %22
%30 = getelementptr inbounds [3 x i32], [3 x i32]* %4, i64 0, i64 2
%31 = load i32, i32* %30
%32 = load i32, i32* %9
%33 = icmp eq i32 %31, %32
br i1 %33, label %37, label %34
34: ; preds = %29
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
br label %39
37: ; preds = %29
%38 = and i32 %24, 255
br label %39
39: ; preds = %3, %37, %34, %26, %19
%40 = phi i32 [ -1, %19 ], [ -1, %26 ], [ -1, %34 ], [ %38, %37 ], [ %14, %3 ]
ret i32 %40
}