#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
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);
scalar_t__ ioctl(int, int /*<<< orphan*/, int *);
int open(char const *, int /*<<< orphan*/);
int spi_open(const char *devname, int mode, int speed, int bits_per_word) {
  int fd = open(devname, O_RDWR);
  if (fd < 0)
    return fd;
  if ((ioctl(fd, SPI_IOC_WR_MODE, &mode) == 0) &&
      (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits_per_word) == 0) &&
      (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == 0))
    return fd;
  close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<spi_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e98(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    116f <spi_open+0x3f>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e6 <spi_open+0xb6>
mov    -0x20(%rbp),%edi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
lea    -0x14(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jne    11d7 <spi_open+0xa7>
mov    -0x20(%rbp),%edi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
lea    -0x1c(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jne    11d7 <spi_open+0xa7>
mov    -0x20(%rbp),%edi
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jne    11d7 <spi_open+0xa7>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e6 <spi_open+0xb6>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spi_open>:
push   %rbx
sub    $0x10,%rsp
mov    %esi,0xc(%rsp)
mov    %edx,0x8(%rsp)
mov    %ecx,0x4(%rsp)
mov    0x2ea0(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11af <spi_open+0x7f>
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
jne    11a3 <spi_open+0x73>
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
jne    11a3 <spi_open+0x73>
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
je     11af <spi_open+0x7f>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@SPI_IOC_WR_MODE = global i32 0
@SPI_IOC_WR_BITS_PER_WORD = global i32 0
@SPI_IOC_WR_MAX_SPEED_HZ = global i32 0
define i32 @spi_open(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%11 = load i8*, i8** %6
%12 = load i32, i32* @O_RDWR
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %10
%14 = load i32, i32* %10
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %4
%17 = load i32, i32* %10
store i32 %17, i32* %5
br label %38
18: ; preds = %4
%19 = load i32, i32* %10
%20 = load i32, i32* @SPI_IOC_WR_MODE
%21 = call i64 @ioctl(i32 %19, i32 %20, i32* %7)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %35
23: ; preds = %18
%24 = load i32, i32* %10
%25 = load i32, i32* @SPI_IOC_WR_BITS_PER_WORD
%26 = call i64 @ioctl(i32 %24, i32 %25, i32* %9)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %35
28: ; preds = %23
%29 = load i32, i32* %10
%30 = load i32, i32* @SPI_IOC_WR_MAX_SPEED_HZ
%31 = call i64 @ioctl(i32 %29, i32 %30, i32* %8)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = load i32, i32* %10
store i32 %34, i32* %5
br label %38
35: ; preds = %28, %23, %18
%36 = load i32, i32* %10
%37 = call i32 @close(i32 %36)
store i32 -1, i32* %5
br label %38
38: ; preds = %35, %33, %16
%39 = load i32, i32* %5
ret i32 %39
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@SPI_IOC_WR_MODE = global i32 0
@SPI_IOC_WR_BITS_PER_WORD = global i32 0
@SPI_IOC_WR_MAX_SPEED_HZ = global i32 0
define i32 @spi_open(i8* nocapture %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 %3, i32* %7
%8 = load i32, i32* @O_RDWR
%9 = tail call i32 @open(i8* %0, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %25, label %11
11: ; preds = %4
%12 = load i32, i32* @SPI_IOC_WR_MODE
%13 = call i64 @ioctl(i32 %9, i32 %12, i32* %5)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %23
15: ; preds = %11
%16 = load i32, i32* @SPI_IOC_WR_BITS_PER_WORD
%17 = call i64 @ioctl(i32 %9, i32 %16, i32* %7)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = load i32, i32* @SPI_IOC_WR_MAX_SPEED_HZ
%21 = call i64 @ioctl(i32 %9, i32 %20, i32* %6)
%22 = icmp eq i64 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %19, %15, %11
%24 = call i32 @close(i32 %9)
br label %25
25: ; preds = %19, %4, %23
%26 = phi i32 [ -1, %23 ], [ %9, %4 ], [ %9, %19 ]
ret i32 %26
}