#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;
typedef int uint32_t;

/* Variables and functions */
int O_NONBLOCK;
int O_RDWR;
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 SPI_MODE_1;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int ioctl(int, int /*<<< orphan*/, int *);
int open(char const *, int);
int /*<<< orphan*/ stderr;
int open_spi(const char *dev, uint32_t speed) {
  int fd;
  int rc;
  uint8_t mode;
  uint8_t bits;
  mode = SPI_MODE_1;
  bits = 8;
  fd = open(dev, O_RDWR | O_NONBLOCK);
  if (fd < 0) {
    int err = errno;
    fprintf(stderr, "Failed to open SPI device %s, errno=%d\n", dev, err);
    errno = err;
    return -1;
  }
  rc = ioctl(fd, SPI_IOC_WR_MODE, &mode);
  if (rc == -1) {
    fprintf(stderr, "SPI_IOC_WR_MODE failed, errno=%d\n", errno);
    goto err_exit;
  }
  rc = ioctl(fd, SPI_IOC_RD_MODE, &mode);
  if (rc == -1) {
    fprintf(stderr, "SPI_IOC_RD_MODE failed, errno=%d\n", errno);
    goto err_exit;
  }
  rc = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
  if (rc == -1) {
    fprintf(stderr, "SPI_IOC_WR_BITS_PER_WORD failed, errno=%d\n", errno);
    goto err_exit;
  }
  rc = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
  if (rc == -1) {
    fprintf(stderr, "SPI_IOC_RD_BITS_PER_WORD failed, errno=%d\n", errno);
    goto err_exit;
  }
  rc = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
  if (rc == -1) {
    fprintf(stderr, "SPI_IOC_WR_MAX_SPEED failed, errno=%d\n", errno);
    goto err_exit;
  }
  rc = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
  if (rc == -1) {
    fprintf(stderr, "SPI_IOC_RD_MAX_SPEED failed, errno=%d\n", errno);
    goto err_exit;
  }
  return fd;
err_exit:
  close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<open_spi>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movl   $0x8,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
mov    0x2e6a(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11cc <open_spi+0x8c>
mov    0x2e07(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x28(%rbp),%ecx
lea    0xe53(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%ecx
mov    0x2dda(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1390 <open_spi+0x250>
mov    -0x18(%rbp),%edi
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1213 <open_spi+0xd3>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
mov    0x2d9a(%rip),%rax
mov    (%rax),%edx
lea    0xe21(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1381 <open_spi+0x241>
mov    -0x18(%rbp),%edi
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    125a <open_spi+0x11a>
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
mov    0x2d53(%rip),%rax
mov    (%rax),%edx
lea    0xdfc(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1381 <open_spi+0x241>
mov    -0x18(%rbp),%edi
mov    0x2d74(%rip),%rax
mov    (%rax),%esi
lea    -0x24(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    12a1 <open_spi+0x161>
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
mov    0x2d0c(%rip),%rax
mov    (%rax),%edx
lea    0xdd7(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1381 <open_spi+0x241>
mov    -0x18(%rbp),%edi
mov    0x2d1d(%rip),%rax
mov    (%rax),%esi
lea    -0x24(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    12e8 <open_spi+0x1a8>
mov    0x2cde(%rip),%rax
mov    (%rax),%edi
mov    0x2cc5(%rip),%rax
mov    (%rax),%edx
lea    0xdbb(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1381 <open_spi+0x241>
mov    -0x18(%rbp),%edi
mov    0x2c9e(%rip),%rax
mov    (%rax),%esi
lea    -0x14(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    132f <open_spi+0x1ef>
mov    0x2c97(%rip),%rax
mov    (%rax),%edi
mov    0x2c7e(%rip),%rax
mov    (%rax),%edx
lea    0xd9f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1381 <open_spi+0x241>
mov    -0x18(%rbp),%edi
mov    0x2c77(%rip),%rax
mov    (%rax),%esi
lea    -0x14(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1376 <open_spi+0x236>
mov    0x2c50(%rip),%rax
mov    (%rax),%edi
mov    0x2c37(%rip),%rax
mov    (%rax),%edx
lea    0xd7f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1381 <open_spi+0x241>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1390 <open_spi+0x250>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_spi>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    %esi,0xc(%rsp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
movl   $0x8,0x4(%rsp)
mov    0x2e7d(%rip),%rax
mov    0x2e6e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     123e <open_spi+0xfe>
mov    %eax,%ebp
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1269 <open_spi+0x129>
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1272 <open_spi+0x132>
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
mov    %ebp,%edi
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
je     127b <open_spi+0x13b>
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
mov    %ebp,%edi
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1284 <open_spi+0x144>
mov    0x2d8c(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %ebp,%edi
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
je     128d <open_spi+0x14d>
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %ebp,%edi
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    12b9 <open_spi+0x179>
lea    0xead(%rip),%rsi
jmp    1294 <open_spi+0x154>
mov    0x2d53(%rip),%r14
mov    (%r14),%ebp
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
lea    0xda8(%rip),%rsi
mov    %rbx,%rdx
mov    %ebp,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebp,(%r14)
jmp    12b4 <open_spi+0x174>
lea    0xdb8(%rip),%rsi
jmp    1294 <open_spi+0x154>
lea    0xdd1(%rip),%rsi
jmp    1294 <open_spi+0x154>
lea    0xdea(%rip),%rsi
jmp    1294 <open_spi+0x154>
lea    0xe0c(%rip),%rsi
jmp    1294 <open_spi+0x154>
lea    0xe2e(%rip),%rsi
mov    0x2d0d(%rip),%rax
mov    (%rax),%edi
mov    0x2cf4(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@SPI_MODE_1 = global i32 0
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i32 0
@stderr = global i32 0
@.str = [40 x i8] c"Failed to open SPI device %s, errno=%d\0A\00"
@SPI_IOC_WR_MODE = global i32 0
@.str.1 = [34 x i8] c"SPI_IOC_WR_MODE failed, errno=%d\0A\00"
@SPI_IOC_RD_MODE = global i32 0
@.str.2 = [34 x i8] c"SPI_IOC_RD_MODE failed, errno=%d\0A\00"
@SPI_IOC_WR_BITS_PER_WORD = global i32 0
@.str.3 = [43 x i8] c"SPI_IOC_WR_BITS_PER_WORD failed, errno=%d\0A\00"
@SPI_IOC_RD_BITS_PER_WORD = global i32 0
@.str.4 = [43 x i8] c"SPI_IOC_RD_BITS_PER_WORD failed, errno=%d\0A\00"
@SPI_IOC_WR_MAX_SPEED_HZ = global i32 0
@.str.5 = [39 x i8] c"SPI_IOC_WR_MAX_SPEED failed, errno=%d\0A\00"
@SPI_IOC_RD_MAX_SPEED_HZ = global i32 0
@.str.6 = [39 x i8] c"SPI_IOC_RD_MAX_SPEED failed, errno=%d\0A\00"
define i32 @open_spi(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%11 = load i32, i32* @SPI_MODE_1
store i32 %11, i32* %8
store i32 8, i32* %9
%12 = load i8*, i8** %4
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_NONBLOCK
%15 = or i32 %13, %14
%16 = call i32 @open(i8* %12, i32 %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %26
19: ; preds = %2
%20 = load i32, i32* @errno
store i32 %20, i32* %10
%21 = load i32, i32* @stderr
%22 = load i8*, i8** %4
%23 = load i32, i32* %10
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %22, i32 %23)
%25 = load i32, i32* %10
store i32 %25, i32* @errno
store i32 -1, i32* %3
br label %91
26: ; preds = %2
%27 = load i32, i32* %6
%28 = load i32, i32* @SPI_IOC_WR_MODE
%29 = call i32 @ioctl(i32 %27, i32 %28, i32* %8)
store i32 %29, i32* %7
%30 = load i32, i32* %7
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %36
32: ; preds = %26
%33 = load i32, i32* @stderr
%34 = load i32, i32* @errno
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %34)
br label %88
36: ; preds = %26
%37 = load i32, i32* %6
%38 = load i32, i32* @SPI_IOC_RD_MODE
%39 = call i32 @ioctl(i32 %37, i32 %38, i32* %8)
store i32 %39, i32* %7
%40 = load i32, i32* %7
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %46
42: ; preds = %36
%43 = load i32, i32* @stderr
%44 = load i32, i32* @errno
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %44)
br label %88
46: ; preds = %36
%47 = load i32, i32* %6
%48 = load i32, i32* @SPI_IOC_WR_BITS_PER_WORD
%49 = call i32 @ioctl(i32 %47, i32 %48, i32* %9)
store i32 %49, i32* %7
%50 = load i32, i32* %7
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %56
52: ; preds = %46
%53 = load i32, i32* @stderr
%54 = load i32, i32* @errno
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i32 %54)
br label %88
56: ; preds = %46
%57 = load i32, i32* %6
%58 = load i32, i32* @SPI_IOC_RD_BITS_PER_WORD
%59 = call i32 @ioctl(i32 %57, i32 %58, i32* %9)
store i32 %59, i32* %7
%60 = load i32, i32* %7
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %66
62: ; preds = %56
%63 = load i32, i32* @stderr
%64 = load i32, i32* @errno
%65 = call i32 (i32, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i32 %64)
br label %88
66: ; preds = %56
%67 = load i32, i32* %6
%68 = load i32, i32* @SPI_IOC_WR_MAX_SPEED_HZ
%69 = call i32 @ioctl(i32 %67, i32 %68, i32* %5)
store i32 %69, i32* %7
%70 = load i32, i32* %7
%71 = icmp eq i32 %70, -1
br i1 %71, label %72, label %76
72: ; preds = %66
%73 = load i32, i32* @stderr
%74 = load i32, i32* @errno
%75 = call i32 (i32, i8*, ...) @fprintf(i32 %73, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i32 %74)
br label %88
76: ; preds = %66
%77 = load i32, i32* %6
%78 = load i32, i32* @SPI_IOC_RD_MAX_SPEED_HZ
%79 = call i32 @ioctl(i32 %77, i32 %78, i32* %5)
store i32 %79, i32* %7
%80 = load i32, i32* %7
%81 = icmp eq i32 %80, -1
br i1 %81, label %82, label %86
82: ; preds = %76
%83 = load i32, i32* @stderr
%84 = load i32, i32* @errno
%85 = call i32 (i32, i8*, ...) @fprintf(i32 %83, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0), i32 %84)
br label %88
86: ; preds = %76
%87 = load i32, i32* %6
store i32 %87, i32* %3
br label %91
88: ; preds = %82, %72, %62, %52, %42, %32
%89 = load i32, i32* %6
%90 = call i32 @close(i32 %89)
store i32 -1, i32* %3
br label %91
91: ; preds = %88, %86, %19
%92 = load i32, i32* %3
ret i32 %92
}

LLVM IR (-O2):
@SPI_MODE_1 = global i32 0
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i32 0
@stderr = global i32 0
@.str = [40 x i8] c"Failed to open SPI device %s, errno=%d\0A\00"
@SPI_IOC_WR_MODE = global i32 0
@.str.1 = [34 x i8] c"SPI_IOC_WR_MODE failed, errno=%d\0A\00"
@SPI_IOC_RD_MODE = global i32 0
@.str.2 = [34 x i8] c"SPI_IOC_RD_MODE failed, errno=%d\0A\00"
@SPI_IOC_WR_BITS_PER_WORD = global i32 0
@.str.3 = [43 x i8] c"SPI_IOC_WR_BITS_PER_WORD failed, errno=%d\0A\00"
@SPI_IOC_RD_BITS_PER_WORD = global i32 0
@.str.4 = [43 x i8] c"SPI_IOC_RD_BITS_PER_WORD failed, errno=%d\0A\00"
@SPI_IOC_WR_MAX_SPEED_HZ = global i32 0
@.str.5 = [39 x i8] c"SPI_IOC_WR_MAX_SPEED failed, errno=%d\0A\00"
@SPI_IOC_RD_MAX_SPEED_HZ = global i32 0
@.str.6 = [39 x i8] c"SPI_IOC_RD_MAX_SPEED failed, errno=%d\0A\00"
define i32 @open_spi(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %1, i32* %3
%6 = bitcast i32* %4 to i8*
%7 = bitcast i32* %5 to i8*
%8 = load i32, i32* @SPI_MODE_1
store i32 %8, i32* %4
store i32 8, i32* %5
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_NONBLOCK
%11 = or i32 %10, %9
%12 = tail call i32 @open(i8* %0, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i32, i32* @errno
%16 = load i32, i32* @stderr
%17 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %15)
store i32 %15, i32* @errno
br label %48
18: ; preds = %2
%19 = load i32, i32* @SPI_IOC_WR_MODE
%20 = call i32 @ioctl(i32 %12, i32 %19, i32* %4)
%21 = icmp eq i32 %20, -1
br i1 %21, label %42, label %22
22: ; preds = %18
%23 = load i32, i32* @SPI_IOC_RD_MODE
%24 = call i32 @ioctl(i32 %12, i32 %23, i32* %4)
%25 = icmp eq i32 %24, -1
br i1 %25, label %42, label %26
26: ; preds = %22
%27 = load i32, i32* @SPI_IOC_WR_BITS_PER_WORD
%28 = call i32 @ioctl(i32 %12, i32 %27, i32* %5)
%29 = icmp eq i32 %28, -1
br i1 %29, label %42, label %30
30: ; preds = %26
%31 = load i32, i32* @SPI_IOC_RD_BITS_PER_WORD
%32 = call i32 @ioctl(i32 %12, i32 %31, i32* %5)
%33 = icmp eq i32 %32, -1
br i1 %33, label %42, label %34
34: ; preds = %30
%35 = load i32, i32* @SPI_IOC_WR_MAX_SPEED_HZ
%36 = call i32 @ioctl(i32 %12, i32 %35, i32* %3)
%37 = icmp eq i32 %36, -1
br i1 %37, label %42, label %38
38: ; preds = %34
%39 = load i32, i32* @SPI_IOC_RD_MAX_SPEED_HZ
%40 = call i32 @ioctl(i32 %12, i32 %39, i32* %3)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %48
42: ; preds = %38, %34, %30, %26, %22, %18
%43 = phi i8* [ getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), %18 ], [ getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), %22 ], [ getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), %26 ], [ getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), %30 ], [ getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), %34 ], [ getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0), %38 ]
%44 = load i32, i32* @stderr
%45 = load i32, i32* @errno
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* %43, i32 %45)
%47 = call i32 @close(i32 %12)
br label %48
48: ; preds = %38, %42, %14
%49 = phi i32 [ -1, %14 ], [ -1, %42 ], [ %12, %38 ]
ret i32 %49
}