#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*/ tr;
struct timespec {
  int dummy;
};
struct spi_ioc_transfer {
  uintptr_t tx_buf;
  uintptr_t rx_buf;
  int len;
  int delay_usecs;
  int speed_hz;
  int bits_per_word;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SPI_IOC_MESSAGE(int);
int /*<<< orphan*/ SPI_IOC_RD_BITS_PER_WORD;
int /*<<< orphan*/ SPI_IOC_RD_MAX_SPEED_HZ;
int /*<<< orphan*/ SPI_IOC_RD_MODE;
int /*<<< orphan*/ SPI_IOC_WR_BITS_PER_WORD;
int /*<<< orphan*/ SPI_IOC_WR_MAX_SPEED_HZ;
int /*<<< orphan*/ SPI_IOC_WR_MODE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ memset(struct spi_ioc_transfer **, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ spi_reset(int);
int spi_txrx(const char *wrbuf, char *rdbuf, int bufsz) {
  int fd;
  int mode, bits, speed, rv, i, j, maxspeed;
  struct timespec tv;
  struct spi_ioc_transfer tr[16];
  memset(&tr, 0, sizeof(tr));
  mode = 0;
  bits = 8;
  speed = 625000, maxspeed = 1000000;
  spi_reset(1234);
  fd = open("/dev/spidev0.0", O_RDWR);
  if (fd < 0) {
    perror("Unable to open SPI device");
    exit(1);
  }
  if (ioctl(fd, SPI_IOC_WR_MODE, &mode) < 0) {
    perror("Unable to set WR MODE");
    close(fd);
    return -1;
  }
  if (ioctl(fd, SPI_IOC_RD_MODE, &mode) < 0) {
    perror("Unable to set RD MODE");
    close(fd);
    return -1;
  }
  if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) < 0) {
    perror("Unable to set WR_BITS_PER_WORD");
    close(fd);
    return -1;
  }
  if (ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits) < 0) {
    perror("Unable to set RD_BITS_PER_WORD");
    close(fd);
    return -1;
  }
  if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &maxspeed) < 0) {
    perror("Unable to set WR_MAX_SPEED_HZ");
    close(fd);
    return -1;
  }
  if (ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &maxspeed) < 0) {
    perror("Unable to set RD_MAX_SPEED_HZ");
    close(fd);
    return -1;
  }
  rv = 0;
  while (bufsz >= 4096) {
    tr[rv].tx_buf = (uintptr_t)wrbuf;
    tr[rv].rx_buf = (uintptr_t)rdbuf;
    tr[rv].len = 4096;
    tr[rv].delay_usecs = 1;
    tr[rv].speed_hz = speed;
    tr[rv].bits_per_word = bits;
    bufsz -= 4096;
    wrbuf += 4096;
    rdbuf += 4096;
    rv++;
  }
  if (bufsz > 0) {
    tr[rv].tx_buf = (uintptr_t)wrbuf;
    tr[rv].rx_buf = (uintptr_t)rdbuf;
    tr[rv].len = (unsigned)bufsz;
    tr[rv].delay_usecs = 1;
    tr[rv].speed_hz = speed;
    tr[rv].bits_per_word = bits;
    rv++;
  }
  i = rv;
  for (j = 0; j < i; j++) {
    rv = (int)ioctl(fd, SPI_IOC_MESSAGE(1), (intptr_t)&tr[j]);
    if (rv < 0) {
      perror("WTF!");
      close(fd);
      return -1;
    }
  }
  close(fd);
  spi_reset(4321);
  return 0;
}


x86 assembly (clang, -O0):
<spi_txrx>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
lea    -0x240(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1040 <memset@plt>
movl   $0x0,-0x24(%rbp)
movl   $0x8,-0x28(%rbp)
movl   $0x98968,-0x2c(%rbp)
movl   $0xf4240,-0x3c(%rbp)
mov    $0x4d2,%edi
call   1030 <spi_reset@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
lea    0xe21(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1207 <spi_txrx+0x87>
lea    0xe17(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%edi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
lea    -0x24(%rbp),%rdx
mov    $0x0,%al
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    1248 <spi_txrx+0xc8>
lea    0xdfa(%rip),%rdi
call   1090 <perror@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    15b8 <spi_txrx+0x438>
mov    -0x20(%rbp),%edi
mov    0x2d86(%rip),%rax
mov    (%rax),%esi
lea    -0x24(%rbp),%rdx
mov    $0x0,%al
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    1289 <spi_txrx+0x109>
lea    0xdcf(%rip),%rdi
call   1090 <perror@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    15b8 <spi_txrx+0x438>
mov    -0x20(%rbp),%edi
mov    0x2d4d(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
mov    $0x0,%al
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    12ca <spi_txrx+0x14a>
lea    0xda4(%rip),%rdi
call   1090 <perror@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    15b8 <spi_txrx+0x438>
mov    -0x20(%rbp),%edi
mov    0x2cfc(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
mov    $0x0,%al
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    130b <spi_txrx+0x18b>
lea    0xd82(%rip),%rdi
call   1090 <perror@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    15b8 <spi_txrx+0x438>
mov    -0x20(%rbp),%edi
mov    0x2c9b(%rip),%rax
mov    (%rax),%esi
lea    -0x3c(%rbp),%rdx
mov    $0x0,%al
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    134c <spi_txrx+0x1cc>
lea    0xd60(%rip),%rdi
call   1090 <perror@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    15b8 <spi_txrx+0x438>
mov    -0x20(%rbp),%edi
mov    0x2c6a(%rip),%rax
mov    (%rax),%esi
lea    -0x3c(%rbp),%rdx
mov    $0x0,%al
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    138d <spi_txrx+0x20d>
lea    0xd3d(%rip),%rdi
call   1090 <perror@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    15b8 <spi_txrx+0x438>
movl   $0x0,-0x30(%rbp)
cmpl   $0x1000,-0x1c(%rbp)
jl     146b <spi_txrx+0x2eb>
mov    -0x10(%rbp),%rcx
movslq -0x30(%rbp),%rdx
lea    -0x240(%rbp),%rax
shl    $0x5,%rdx
add    %rdx,%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rcx
movslq -0x30(%rbp),%rdx
lea    -0x240(%rbp),%rax
shl    $0x5,%rdx
add    %rdx,%rax
mov    %rcx,0x8(%rax)
movslq -0x30(%rbp),%rcx
lea    -0x240(%rbp),%rax
shl    $0x5,%rcx
add    %rcx,%rax
movl   $0x1000,0x10(%rax)
movslq -0x30(%rbp),%rcx
lea    -0x240(%rbp),%rax
shl    $0x5,%rcx
add    %rcx,%rax
movl   $0x1,0x14(%rax)
mov    -0x2c(%rbp),%ecx
movslq -0x30(%rbp),%rdx
lea    -0x240(%rbp),%rax
shl    $0x5,%rdx
add    %rdx,%rax
mov    %ecx,0x18(%rax)
mov    -0x28(%rbp),%ecx
movslq -0x30(%rbp),%rdx
lea    -0x240(%rbp),%rax
shl    $0x5,%rdx
add    %rdx,%rax
mov    %ecx,0x1c(%rax)
mov    -0x1c(%rbp),%eax
sub    $0x1000,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1000,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1000,%rax
mov    %rax,-0x18(%rbp)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1394 <spi_txrx+0x214>
cmpl   $0x0,-0x1c(%rbp)
jle    1512 <spi_txrx+0x392>
mov    -0x10(%rbp),%rcx
movslq -0x30(%rbp),%rdx
lea    -0x240(%rbp),%rax
shl    $0x5,%rdx
add    %rdx,%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rcx
movslq -0x30(%rbp),%rdx
lea    -0x240(%rbp),%rax
shl    $0x5,%rdx
add    %rdx,%rax
mov    %rcx,0x8(%rax)
mov    -0x1c(%rbp),%ecx
movslq -0x30(%rbp),%rdx
lea    -0x240(%rbp),%rax
shl    $0x5,%rdx
add    %rdx,%rax
mov    %ecx,0x10(%rax)
movslq -0x30(%rbp),%rcx
lea    -0x240(%rbp),%rax
shl    $0x5,%rcx
add    %rcx,%rax
movl   $0x1,0x14(%rax)
mov    -0x2c(%rbp),%ecx
movslq -0x30(%rbp),%rdx
lea    -0x240(%rbp),%rax
shl    $0x5,%rdx
add    %rdx,%rax
mov    %ecx,0x18(%rax)
mov    -0x28(%rbp),%ecx
movslq -0x30(%rbp),%rdx
lea    -0x240(%rbp),%rax
shl    $0x5,%rdx
add    %rdx,%rax
mov    %ecx,0x1c(%rax)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    159f <spi_txrx+0x41f>
mov    -0x20(%rbp),%eax
mov    %eax,-0x244(%rbp)
mov    $0x1,%edi
call   1050 <SPI_IOC_MESSAGE@plt>
mov    -0x244(%rbp),%edi
mov    %eax,%esi
movslq -0x38(%rbp),%rax
lea    -0x240(%rbp),%rdx
shl    $0x5,%rax
add    %rax,%rdx
mov    $0x0,%al
call   1060 <ioctl@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    158c <spi_txrx+0x40c>
lea    0xb5c(%rip),%rdi
call   1090 <perror@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    15b8 <spi_txrx+0x438>
jmp    1591 <spi_txrx+0x411>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    151f <spi_txrx+0x39f>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
mov    $0x10e1,%edi
call   1030 <spi_reset@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spi_txrx>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%r12
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1040 <memset@plt>
movl   $0x0,0xc(%rsp)
movl   $0x8,0x4(%rsp)
movl   $0xf4240,0x8(%rsp)
mov    $0x4d2,%edi
call   1030 <spi_reset@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
lea    0xe20(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1411 <spi_txrx+0x291>
mov    %eax,%r13d
mov    0x2dc1(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %r13d,%edi
xor    %eax,%eax
call   1060 <ioctl@plt>
test   %rax,%rax
js     13b0 <spi_txrx+0x230>
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %r13d,%edi
xor    %eax,%eax
call   1060 <ioctl@plt>
test   %rax,%rax
js     13b9 <spi_txrx+0x239>
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
mov    %r13d,%edi
xor    %eax,%eax
call   1060 <ioctl@plt>
test   %rax,%rax
js     13c2 <spi_txrx+0x242>
mov    0x2d76(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
mov    %r13d,%edi
xor    %eax,%eax
call   1060 <ioctl@plt>
test   %rax,%rax
js     13cb <spi_txrx+0x24b>
mov    0x2d35(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %r13d,%edi
xor    %eax,%eax
call   1060 <ioctl@plt>
test   %rax,%rax
js     13d4 <spi_txrx+0x254>
mov    0x2d24(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
lea    0x8(%rsp),%rdx
mov    %r13d,%edi
xor    %eax,%eax
call   1060 <ioctl@plt>
test   %rax,%rax
js     13dd <spi_txrx+0x25d>
cmp    $0x1000,%r14d
jl     1336 <spi_txrx+0x1b6>
mov    0x4(%rsp),%r9d
lea    0x2c(%rsp),%rdx
lea    0x1000(%r14),%r10d
xor    %eax,%eax
movabs $0x100001000,%r8
mov    %r15,%rdi
mov    %r12,%rcx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
mov    %rcx,-0x1c(%rdx)
mov    %rdi,-0x14(%rdx)
mov    %r8,-0xc(%rdx)
movl   $0x98968,-0x4(%rdx)
mov    %r9d,(%rdx)
add    $0x20,%rdx
add    $0x1,%ebp
add    $0x1000,%rcx
add    $0xfffffffffffff000,%rax
add    $0x1000,%rdi
lea    (%r10,%rax,1),%esi
cmp    $0x1fff,%esi
ja     12f0 <spi_txrx+0x170>
sub    %rax,%r12
sub    %rax,%r15
add    %eax,%r14d
test   %r14d,%r14d
jle    136a <spi_txrx+0x1ea>
mov    %ebp,%eax
shl    $0x5,%rax
mov    %r12,0x10(%rsp,%rax,1)
mov    %r15,0x18(%rsp,%rax,1)
mov    %r14d,0x20(%rsp,%rax,1)
movabs $0x9896800000001,%rcx
mov    %rcx,0x24(%rsp,%rax,1)
mov    0x4(%rsp),%ecx
mov    %ecx,0x2c(%rsp,%rax,1)
add    $0x1,%ebp
test   %ebp,%ebp
jle    139a <spi_txrx+0x21a>
mov    %ebp,%ebp
mov    $0x1,%edi
call   1050 <SPI_IOC_MESSAGE@plt>
mov    %r13d,%edi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <ioctl@plt>
test   $0x80000000,%eax
jne    13e6 <spi_txrx+0x266>
add    $0x20,%rbx
add    $0xffffffffffffffff,%rbp
jne    1370 <spi_txrx+0x1f0>
mov    %r13d,%edi
call   1070 <close@plt>
mov    $0x10e1,%edi
call   1030 <spi_reset@plt>
xor    %eax,%eax
jmp    13ff <spi_txrx+0x27f>
lea    0xc72(%rip),%rdi
jmp    13ed <spi_txrx+0x26d>
lea    0xc7f(%rip),%rdi
jmp    13ed <spi_txrx+0x26d>
lea    0xc8c(%rip),%rdi
jmp    13ed <spi_txrx+0x26d>
lea    0xca2(%rip),%rdi
jmp    13ed <spi_txrx+0x26d>
lea    0xcb8(%rip),%rdi
jmp    13ed <spi_txrx+0x26d>
lea    0xccd(%rip),%rdi
jmp    13ed <spi_txrx+0x26d>
lea    0xce2(%rip),%rdi
call   1090 <perror@plt>
mov    %r13d,%edi
call   1070 <close@plt>
mov    $0xffffffff,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xbf7(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.timespec = type { i32 }
%struct.spi_ioc_transfer = type { i64, i64, i32, i32, i32, i32 }
@.str = [15 x i8] c"/dev/spidev0.0\00"
@O_RDWR = global i32 0
@.str.1 = [26 x i8] c"Unable to open SPI device\00"
@SPI_IOC_WR_MODE = global i32 0
@.str.2 = [22 x i8] c"Unable to set WR MODE\00"
@SPI_IOC_RD_MODE = global i32 0
@.str.3 = [22 x i8] c"Unable to set RD MODE\00"
@SPI_IOC_WR_BITS_PER_WORD = global i32 0
@.str.4 = [31 x i8] c"Unable to set WR_BITS_PER_WORD\00"
@SPI_IOC_RD_BITS_PER_WORD = global i32 0
@.str.5 = [31 x i8] c"Unable to set RD_BITS_PER_WORD\00"
@SPI_IOC_WR_MAX_SPEED_HZ = global i32 0
@.str.6 = [30 x i8] c"Unable to set WR_MAX_SPEED_HZ\00"
@SPI_IOC_RD_MAX_SPEED_HZ = global i32 0
@.str.7 = [30 x i8] c"Unable to set RD_MAX_SPEED_HZ\00"
@.str.8 = [5 x i8] c"WTF!\00"
define i32 @spi_txrx(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca %struct.timespec
%17 = alloca [16 x %struct.spi_ioc_transfer]
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%18 = bitcast [16 x %struct.spi_ioc_transfer]* %17 to %struct.spi_ioc_transfer**
%19 = call i32 @memset(%struct.spi_ioc_transfer** %18, i32 0, i32 512)
store i32 0, i32* %9
store i32 8, i32* %10
store i32 625000, i32* %11
store i32 1000000, i32* %15
%20 = call i32 @spi_reset(i32 1234)
%21 = load i32, i32* @O_RDWR
%22 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %21)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %3
%26 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %3
%29 = load i32, i32* %8
%30 = load i32, i32* @SPI_IOC_WR_MODE
%31 = call i64 (i32, i32, ...) @ioctl(i32 %29, i32 %30, i32* %9)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %28
%34 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%35 = load i32, i32* %8
%36 = call i32 @close(i32 %35)
store i32 -1, i32* %4
br label %191
37: ; preds = %28
%38 = load i32, i32* %8
%39 = load i32, i32* @SPI_IOC_RD_MODE
%40 = call i64 (i32, i32, ...) @ioctl(i32 %38, i32 %39, i32* %9)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %37
%43 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%44 = load i32, i32* %8
%45 = call i32 @close(i32 %44)
store i32 -1, i32* %4
br label %191
46: ; preds = %37
%47 = load i32, i32* %8
%48 = load i32, i32* @SPI_IOC_WR_BITS_PER_WORD
%49 = call i64 (i32, i32, ...) @ioctl(i32 %47, i32 %48, i32* %10)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %46
%52 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
%53 = load i32, i32* %8
%54 = call i32 @close(i32 %53)
store i32 -1, i32* %4
br label %191
55: ; preds = %46
%56 = load i32, i32* %8
%57 = load i32, i32* @SPI_IOC_RD_BITS_PER_WORD
%58 = call i64 (i32, i32, ...) @ioctl(i32 %56, i32 %57, i32* %10)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %64
60: ; preds = %55
%61 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
%62 = load i32, i32* %8
%63 = call i32 @close(i32 %62)
store i32 -1, i32* %4
br label %191
64: ; preds = %55
%65 = load i32, i32* %8
%66 = load i32, i32* @SPI_IOC_WR_MAX_SPEED_HZ
%67 = call i64 (i32, i32, ...) @ioctl(i32 %65, i32 %66, i32* %15)
%68 = icmp slt i64 %67, 0
br i1 %68, label %69, label %73
69: ; preds = %64
%70 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0))
%71 = load i32, i32* %8
%72 = call i32 @close(i32 %71)
store i32 -1, i32* %4
br label %191
73: ; preds = %64
%74 = load i32, i32* %8
%75 = load i32, i32* @SPI_IOC_RD_MAX_SPEED_HZ
%76 = call i64 (i32, i32, ...) @ioctl(i32 %74, i32 %75, i32* %15)
%77 = icmp slt i64 %76, 0
br i1 %77, label %78, label %82
78: ; preds = %73
%79 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.7, i64 0, i64 0))
%80 = load i32, i32* %8
%81 = call i32 @close(i32 %80)
store i32 -1, i32* %4
br label %191
82: ; preds = %73
store i32 0, i32* %12
br label %83
83: ; preds = %86, %82
%84 = load i32, i32* %7
%85 = icmp sge i32 %84, 4096
br i1 %85, label %86, label %125
86: ; preds = %83
%87 = load i8*, i8** %5
%88 = ptrtoint i8* %87 to i64
%89 = load i32, i32* %12
%90 = sext i32 %89 to i64
%91 = getelementptr inbounds [16 x %struct.spi_ioc_transfer], [16 x %struct.spi_ioc_transfer]* %17, i64 0, i64 %90
%92 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %91, i32 0, i32 0
store i64 %88, i64* %92
%93 = load i8*, i8** %6
%94 = ptrtoint i8* %93 to i64
%95 = load i32, i32* %12
%96 = sext i32 %95 to i64
%97 = getelementptr inbounds [16 x %struct.spi_ioc_transfer], [16 x %struct.spi_ioc_transfer]* %17, i64 0, i64 %96
%98 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %97, i32 0, i32 1
store i64 %94, i64* %98
%99 = load i32, i32* %12
%100 = sext i32 %99 to i64
%101 = getelementptr inbounds [16 x %struct.spi_ioc_transfer], [16 x %struct.spi_ioc_transfer]* %17, i64 0, i64 %100
%102 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %101, i32 0, i32 2
store i32 4096, i32* %102
%103 = load i32, i32* %12
%104 = sext i32 %103 to i64
%105 = getelementptr inbounds [16 x %struct.spi_ioc_transfer], [16 x %struct.spi_ioc_transfer]* %17, i64 0, i64 %104
%106 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %105, i32 0, i32 3
store i32 1, i32* %106
%107 = load i32, i32* %11
%108 = load i32, i32* %12
%109 = sext i32 %108 to i64
%110 = getelementptr inbounds [16 x %struct.spi_ioc_transfer], [16 x %struct.spi_ioc_transfer]* %17, i64 0, i64 %109
%111 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %110, i32 0, i32 4
store i32 %107, i32* %111
%112 = load i32, i32* %10
%113 = load i32, i32* %12
%114 = sext i32 %113 to i64
%115 = getelementptr inbounds [16 x %struct.spi_ioc_transfer], [16 x %struct.spi_ioc_transfer]* %17, i64 0, i64 %114
%116 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %115, i32 0, i32 5
store i32 %112, i32* %116
%117 = load i32, i32* %7
%118 = sub nsw i32 %117, 4096
store i32 %118, i32* %7
%119 = load i8*, i8** %5
%120 = getelementptr inbounds i8, i8* %119, i64 4096
store i8* %120, i8** %5
%121 = load i8*, i8** %6
%122 = getelementptr inbounds i8, i8* %121, i64 4096
store i8* %122, i8** %6
%123 = load i32, i32* %12
%124 = add nsw i32 %123, 1
store i32 %124, i32* %12
br label %83
125: ; preds = %83
%126 = load i32, i32* %7
%127 = icmp sgt i32 %126, 0
br i1 %127, label %128, label %162
128: ; preds = %125
%129 = load i8*, i8** %5
%130 = ptrtoint i8* %129 to i64
%131 = load i32, i32* %12
%132 = sext i32 %131 to i64
%133 = getelementptr inbounds [16 x %struct.spi_ioc_transfer], [16 x %struct.spi_ioc_transfer]* %17, i64 0, i64 %132
%134 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %133, i32 0, i32 0
store i64 %130, i64* %134
%135 = load i8*, i8** %6
%136 = ptrtoint i8* %135 to i64
%137 = load i32, i32* %12
%138 = sext i32 %137 to i64
%139 = getelementptr inbounds [16 x %struct.spi_ioc_transfer], [16 x %struct.spi_ioc_transfer]* %17, i64 0, i64 %138
%140 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %139, i32 0, i32 1
store i64 %136, i64* %140
%141 = load i32, i32* %7
%142 = load i32, i32* %12
%143 = sext i32 %142 to i64
%144 = getelementptr inbounds [16 x %struct.spi_ioc_transfer], [16 x %struct.spi_ioc_transfer]* %17, i64 0, i64 %143
%145 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %144, i32 0, i32 2
store i32 %141, i32* %145
%146 = load i32, i32* %12
%147 = sext i32 %146 to i64
%148 = getelementptr inbounds [16 x %struct.spi_ioc_transfer], [16 x %struct.spi_ioc_transfer]* %17, i64 0, i64 %147
%149 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %148, i32 0, i32 3
store i32 1, i32* %149
%150 = load i32, i32* %11
%151 = load i32, i32* %12
%152 = sext i32 %151 to i64
%153 = getelementptr inbounds [16 x %struct.spi_ioc_transfer], [16 x %struct.spi_ioc_transfer]* %17, i64 0, i64 %152
%154 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %153, i32 0, i32 4
store i32 %150, i32* %154
%155 = load i32, i32* %10
%156 = load i32, i32* %12
%157 = sext i32 %156 to i64
%158 = getelementptr inbounds [16 x %struct.spi_ioc_transfer], [16 x %struct.spi_ioc_transfer]* %17, i64 0, i64 %157
%159 = getelementptr inbounds %struct.spi_ioc_transfer, %struct.spi_ioc_transfer* %158, i32 0, i32 5
store i32 %155, i32* %159
%160 = load i32, i32* %12
%161 = add nsw i32 %160, 1
store i32 %161, i32* %12
br label %162
162: ; preds = %128, %125
%163 = load i32, i32* %12
store i32 %163, i32* %13
store i32 0, i32* %14
br label %164
164: ; preds = %184, %162
%165 = load i32, i32* %14
%166 = load i32, i32* %13
%167 = icmp slt i32 %165, %166
br i1 %167, label %168, label %187
168: ; preds = %164
%169 = load i32, i32* %8
%170 = call i32 @SPI_IOC_MESSAGE(i32 1)
%171 = load i32, i32* %14
%172 = sext i32 %171 to i64
%173 = getelementptr inbounds [16 x %struct.spi_ioc_transfer], [16 x %struct.spi_ioc_transfer]* %17, i64 0, i64 %172
%174 = ptrtoint %struct.spi_ioc_transfer* %173 to i64
%175 = call i64 (i32, i32, ...) @ioctl(i32 %169, i32 %170, i64 %174)
%176 = trunc i64 %175 to i32
store i32 %176, i32* %12
%177 = load i32, i32* %12
%178 = icmp slt i32 %177, 0
br i1 %178, label %179, label %183
179: ; preds = %168
%180 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
%181 = load i32, i32* %8
%182 = call i32 @close(i32 %181)
store i32 -1, i32* %4
br label %191
183: ; preds = %168
br label %184
184: ; preds = %183
%185 = load i32, i32* %14
%186 = add nsw i32 %185, 1
store i32 %186, i32* %14
br label %164
187: ; preds = %164
%188 = load i32, i32* %8
%189 = call i32 @close(i32 %188)
%190 = call i32 @spi_reset(i32 4321)
store i32 0, i32* %4
br label %191
191: ; preds = %187, %179, %78, %69, %60, %51, %42, %33
%192 = load i32, i32* %4
ret i32 %192
}

LLVM IR (-O2):
%struct.spi_ioc_transfer = type { i64, i64, i32, i32, i32, i32 }
@.str = [15 x i8] c"/dev/spidev0.0\00"
@O_RDWR = global i32 0
@.str.1 = [26 x i8] c"Unable to open SPI device\00"
@SPI_IOC_WR_MODE = global i32 0
@.str.2 = [22 x i8] c"Unable to set WR MODE\00"
@SPI_IOC_RD_MODE = global i32 0
@.str.3 = [22 x i8] c"Unable to set RD MODE\00"
@SPI_IOC_WR_BITS_PER_WORD = global i32 0
@.str.4 = [31 x i8] c"Unable to set WR_BITS_PER_WORD\00"
@SPI_IOC_RD_BITS_PER_WORD = global i32 0
@.str.5 = [31 x i8] c"Unable to set RD_BITS_PER_WORD\00"
@SPI_IOC_WR_MAX_SPEED_HZ = global i32 0
@.str.6 = [30 x i8] c"Unable to set WR_MAX_SPEED_HZ\00"
@SPI_IOC_RD_MAX_SPEED_HZ = global i32 0
@.str.7 = [30 x i8] c"Unable to set RD_MAX_SPEED_HZ\00"
@.str.8 = [5 x i8] c"WTF!\00"
define i32 @spi_txrx(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [16 x %struct.spi_ioc_transfer]
%8 = bitcast i32* %4 to i8*
%9 = bitcast i32* %5 to i8*
%10 = bitcast i32* %6 to i8*
%11 = bitcast [16 x %struct.spi_ioc_transfer]* %7 to i8*
%12 = bitcast [16 x %struct.spi_ioc_transfer]* %7 to %struct.spi_ioc_transfer**
%13 = call i32 @memset(%struct.spi_ioc_transfer** %12, i32 0, i32 512)
store i32 0, i32* %4
store i32 8, i32* %5
store i32 1000000, i32* %6
%14 = call i32 @spi_reset(i32 1234)
%15 = load i32, i32* @O_RDWR
%16 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %3
%19 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %3
%22 = load i32, i32* @SPI_IOC_WR_MODE
%23 = call i64 (i32, i32, ...) @ioctl(i32 %16, i32 %22, i32* %4)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 @close(i32 %16)
br label %127
28: ; preds = %21
%29 = load i32, i32* @SPI_IOC_RD_MODE
%30 = call i64 (i32, i32, ...) @ioctl(i32 %16, i32 %29, i32* %4)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%34 = call i32 @close(i32 %16)
br label %127
35: ; preds = %28
%36 = load i32, i32* @SPI_IOC_WR_BITS_PER_WORD
%37 = call i64 (i32, i32, ...) @ioctl(i32 %16, i32 %36, i32* %5)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
%41 = call i32 @close(i32 %16)
br label %127
42: ; preds = %35
%43 = load i32, i32* @SPI_IOC_RD_BITS_PER_WORD
%44 = call i64 (i32, i32, ...) @ioctl(i32 %16, i32 %43, i32* %5)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
%48 = call i32 @close(i32 %16)
br label %127
49: ; preds = %42
%50 = load i32, i32* @SPI_IOC_WR_MAX_SPEED_HZ
%51 = call i64 (i32, i32, ...) @ioctl(i32 %16, i32 %50, i32* %6)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %49
%54 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0))
%55 = call i32 @close(i32 %16)
br label %127
56: ; preds = %49
%57 = load i32, i32* @SPI_IOC_RD_MAX_SPEED_HZ
%58 = call i64 (i32, i32, ...) @ioctl(i32 %16, i32 %57, i32* %6)
%59 = icmp slt i64 %58, 0
br i1 %59, label %64, label %60
60: ; preds = %56
%61 = icmp sgt i32 %2, 4095
br i1 %61, label %62, label %87
62: ; preds = %60
%63 = load i32, i32* %5
br label %67
64: ; preds = %56
%65 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.7, i64 0, i64 0))
%66 = call i32 @close(i32 %16)
br label %127
67: ; preds = %62, %67
%68 = phi i64 [ 0, %62 ], [ %83, %67 ]
%69 = phi i8* [ %0, %62 ], [ %81, %67 ]
%70 = phi i8* [ %1, %62 ], [ %82, %67 ]
%71 = phi i32 [ %2, %62 ], [ %80, %67 ]
%72 = ptrtoint i8* %69 to i64
%73 = getelementptr inbounds [16 x %struct.spi_ioc_transfer], [16 x %struct.spi_ioc_transfer]* %7, i64 0, i64 %68, i32 0
store i64 %72, i64* %73
%74 = ptrtoint i8* %70 to i64
%75 = getelementptr inbounds [16 x %struct.spi_ioc_transfer], [16 x %struct.spi_ioc_transfer]* %7, i64 0, i64 %68, i32 1
store i64 %74, i64* %75
%76 = getelementptr inbounds [16 x %struct.spi_ioc_transfer], [16 x %struct.spi_ioc_transfer]* %7, i64 0, i64 %68, i32 2
store i32 4096, i32* %76
%77 = getelementptr inbounds [16 x %struct.spi_ioc_transfer], [16 x %struct.spi_ioc_transfer]* %7, i64 0, i64 %68, i32 3
store i32 1, i32* %77
%78 = getelementptr inbounds [16 x %struct.spi_ioc_transfer], [16 x %struct.spi_ioc_transfer]* %7, i64 0, i64 %68, i32 4
store i32 625000, i32* %78
%79 = getelementptr inbounds [16 x %struct.spi_ioc_transfer], [16 x %struct.spi_ioc_transfer]* %7, i64 0, i64 %68, i32 5
store i32 %63, i32* %79
%80 = add nsw i32 %71, -4096
%81 = getelementptr inbounds i8, i8* %69, i64 4096
%82 = getelementptr inbounds i8, i8* %70, i64 4096
%83 = add nuw nsw i64 %68, 1
%84 = icmp ugt i32 %71, 8191
br i1 %84, label %67, label %85
85: ; preds = %67
%86 = trunc i64 %83 to i32
br label %87
87: ; preds = %85, %60
%88 = phi i32 [ %2, %60 ], [ %80, %85 ]
%89 = phi i8* [ %1, %60 ], [ %82, %85 ]
%90 = phi i32 [ 0, %60 ], [ %86, %85 ]
%91 = phi i8* [ %0, %60 ], [ %81, %85 ]
%92 = icmp sgt i32 %88, 0
br i1 %92, label %93, label %105
93: ; preds = %87
%94 = ptrtoint i8* %91 to i64
%95 = zext i32 %90 to i64
%96 = getelementptr inbounds [16 x %struct.spi_ioc_transfer], [16 x %struct.spi_ioc_transfer]* %7, i64 0, i64 %95, i32 0
store i64 %94, i64* %96
%97 = ptrtoint i8* %89 to i64
%98 = getelementptr inbounds [16 x %struct.spi_ioc_transfer], [16 x %struct.spi_ioc_transfer]* %7, i64 0, i64 %95, i32 1
store i64 %97, i64* %98
%99 = getelementptr inbounds [16 x %struct.spi_ioc_transfer], [16 x %struct.spi_ioc_transfer]* %7, i64 0, i64 %95, i32 2
store i32 %88, i32* %99
%100 = getelementptr inbounds [16 x %struct.spi_ioc_transfer], [16 x %struct.spi_ioc_transfer]* %7, i64 0, i64 %95, i32 3
store i32 1, i32* %100
%101 = getelementptr inbounds [16 x %struct.spi_ioc_transfer], [16 x %struct.spi_ioc_transfer]* %7, i64 0, i64 %95, i32 4
store i32 625000, i32* %101
%102 = load i32, i32* %5
%103 = getelementptr inbounds [16 x %struct.spi_ioc_transfer], [16 x %struct.spi_ioc_transfer]* %7, i64 0, i64 %95, i32 5
store i32 %102, i32* %103
%104 = add nuw nsw i32 %90, 1
br label %105
105: ; preds = %93, %87
%106 = phi i32 [ %104, %93 ], [ %90, %87 ]
%107 = icmp sgt i32 %106, 0
br i1 %107, label %108, label %124
108: ; preds = %105
%109 = zext i32 %106 to i64
br label %112
110: ; preds = %112
%111 = icmp eq i64 %120, %109
br i1 %111, label %124, label %112
112: ; preds = %108, %110
%113 = phi i64 [ 0, %108 ], [ %120, %110 ]
%114 = call i32 @SPI_IOC_MESSAGE(i32 1)
%115 = getelementptr inbounds [16 x %struct.spi_ioc_transfer], [16 x %struct.spi_ioc_transfer]* %7, i64 0, i64 %113
%116 = ptrtoint %struct.spi_ioc_transfer* %115 to i64
%117 = call i64 (i32, i32, ...) @ioctl(i32 %16, i32 %114, i64 %116)
%118 = and i64 %117, 2147483648
%119 = icmp eq i64 %118, 0
%120 = add nuw nsw i64 %113, 1
br i1 %119, label %110, label %121
121: ; preds = %112
%122 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
%123 = call i32 @close(i32 %16)
br label %127
124: ; preds = %110, %105
%125 = call i32 @close(i32 %16)
%126 = call i32 @spi_reset(i32 4321)
br label %127
127: ; preds = %124, %121, %64, %53, %46, %39, %32, %25
%128 = phi i32 [ -1, %25 ], [ -1, %32 ], [ -1, %39 ], [ -1, %46 ], [ -1, %53 ], [ -1, %64 ], [ -1, %121 ], [ 0, %124 ]
ret i32 %128
}