#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*/ uint8_t;
typedef int /*<<< orphan*/ uint32_t;

/* 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 ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int open(char *, int /*<<< orphan*/);
int spi_init(uint32_t mode, uint8_t bits, uint32_t speed) {
  int dev;
  dev = open("/dev/spidev0.1", O_RDWR);
  if (dev < 0) {
    return -1;
  }
  if (ioctl(dev, SPI_IOC_WR_MODE, &mode) == -1) {
    return -1;
  }
  if (ioctl(dev, SPI_IOC_WR_BITS_PER_WORD, &bits) == -1) {
    return -1;
  }
  if (ioctl(dev, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == -1) {
    return -1;
  }
  return dev;
}


x86 assembly (clang, -O0):
<spi_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    0x2eb0(%rip),%rax
mov    (%rax),%esi
lea    0xebf(%rip),%rdi
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    115f <spi_init+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e3 <spi_init+0xc3>
mov    -0x14(%rbp),%edi
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1189 <spi_init+0x69>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e3 <spi_init+0xc3>
mov    -0x14(%rbp),%edi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
lea    -0xc(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    11b3 <spi_init+0x93>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e3 <spi_init+0xc3>
mov    -0x14(%rbp),%edi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    11dd <spi_init+0xbd>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e3 <spi_init+0xc3>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spi_init>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,0x14(%rsp)
mov    %esi,0x10(%rsp)
mov    %edx,0xc(%rsp)
mov    0x2eaf(%rip),%rax
mov    (%rax),%esi
lea    0xebe(%rip),%rdi
call   1040 <open@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     11a3 <spi_init+0x83>
mov    %eax,%ebx
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
lea    0x14(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11a3 <spi_init+0x83>
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11a3 <spi_init+0x83>
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebx
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"/dev/spidev0.1\00"
@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_init(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %8
%11 = load i32, i32* %8
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %3
store i32 -1, i32* %4
br label %34
14: ; preds = %3
%15 = load i32, i32* %8
%16 = load i32, i32* @SPI_IOC_WR_MODE
%17 = call i32 @ioctl(i32 %15, i32 %16, i32* %5)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %14
store i32 -1, i32* %4
br label %34
20: ; preds = %14
%21 = load i32, i32* %8
%22 = load i32, i32* @SPI_IOC_WR_BITS_PER_WORD
%23 = call i32 @ioctl(i32 %21, i32 %22, i32* %6)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %26
25: ; preds = %20
store i32 -1, i32* %4
br label %34
26: ; preds = %20
%27 = load i32, i32* %8
%28 = load i32, i32* @SPI_IOC_WR_MAX_SPEED_HZ
%29 = call i32 @ioctl(i32 %27, i32 %28, i32* %7)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %26
store i32 -1, i32* %4
br label %34
32: ; preds = %26
%33 = load i32, i32* %8
store i32 %33, i32* %4
br label %34
34: ; preds = %32, %31, %25, %19, %13
%35 = load i32, i32* %4
ret i32 %35
}

LLVM IR (-O2):
@.str = [15 x i8] c"/dev/spidev0.1\00"
@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_init(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%7 = load i32, i32* @O_RDWR
%8 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %23, label %10
10: ; preds = %3
%11 = load i32, i32* @SPI_IOC_WR_MODE
%12 = call i32 @ioctl(i32 %8, i32 %11, i32* %4)
%13 = icmp eq i32 %12, -1
br i1 %13, label %23, label %14
14: ; preds = %10
%15 = load i32, i32* @SPI_IOC_WR_BITS_PER_WORD
%16 = call i32 @ioctl(i32 %8, i32 %15, i32* %5)
%17 = icmp eq i32 %16, -1
br i1 %17, label %23, label %18
18: ; preds = %14
%19 = load i32, i32* @SPI_IOC_WR_MAX_SPEED_HZ
%20 = call i32 @ioctl(i32 %8, i32 %19, i32* %6)
%21 = icmp eq i32 %20, -1
%22 = select i1 %21, i32 -1, i32 %8
br label %23
23: ; preds = %18, %14, %10, %3
%24 = phi i32 [ -1, %3 ], [ -1, %10 ], [ -1, %14 ], [ %22, %18 ]
ret i32 %24
}