#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 /*<<< orphan*/ SPI_IOC_WR_BITS_PER_WORD;
int /*<<< orphan*/ SPI_IOC_WR_MAX_SPEED_HZ;
int /*<<< orphan*/ SPI_IOC_WR_MODE;
int SPI_MODE_0;
int ioctl(int, int /*<<< orphan*/, int const *);
int spi_init(int filedes) {
  int ret;
  const uint8_t mode = SPI_MODE_0;
  const uint8_t bits = 16;
  const uint32_t speed = 2000000;
  ret = ioctl(filedes, SPI_IOC_WR_MODE, &mode);
  if (ret == -1) {
    return -1;
  }
  ret = ioctl(filedes, SPI_IOC_WR_BITS_PER_WORD, &bits);
  if (ret == -1) {
    return -1;
  }
  ret = ioctl(filedes, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
  if (ret == -1) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<spi_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2eae(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movl   $0x10,-0x14(%rbp)
movl   $0x1e8480,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1163 <spi_init+0x53>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c6 <spi_init+0xb6>
mov    -0x8(%rbp),%edi
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
lea    -0x14(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1191 <spi_init+0x81>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c6 <spi_init+0xb6>
mov    -0x8(%rbp),%edi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11bf <spi_init+0xaf>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c6 <spi_init+0xb6>
movl   $0x0,-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,%ebx
mov    0x2eb1(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
movl   $0x10,0x10(%rsp)
movl   $0x1e8480,0xc(%rsp)
mov    0x2e9c(%rip),%rax
mov    (%rax),%esi
lea    0x14(%rsp),%rdx
call   1030 <ioctl@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     118c <spi_init+0x7c>
mov    0x2e8f(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
je     118c <spi_init+0x7c>
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
xor    %ebp,%ebp
cmp    $0xffffffff,%eax
sete   %bpl
neg    %ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@SPI_MODE_0 = 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) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
%8 = load i32, i32* @SPI_MODE_0
store i32 %8, i32* %5
store i32 16, i32* %6
store i32 2000000, i32* %7
%9 = load i32, i32* %3
%10 = load i32, i32* @SPI_IOC_WR_MODE
%11 = call i32 @ioctl(i32 %9, i32 %10, i32* %5)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %1
store i32 -1, i32* %2
br label %30
15: ; preds = %1
%16 = load i32, i32* %3
%17 = load i32, i32* @SPI_IOC_WR_BITS_PER_WORD
%18 = call i32 @ioctl(i32 %16, i32 %17, i32* %6)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %22
21: ; preds = %15
store i32 -1, i32* %2
br label %30
22: ; preds = %15
%23 = load i32, i32* %3
%24 = load i32, i32* @SPI_IOC_WR_MAX_SPEED_HZ
%25 = call i32 @ioctl(i32 %23, i32 %24, i32* %7)
store i32 %25, i32* %4
%26 = load i32, i32* %4
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %29
28: ; preds = %22
store i32 -1, i32* %2
br label %30
29: ; preds = %22
store i32 0, i32* %2
br label %30
30: ; preds = %29, %28, %21, %14
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
@SPI_MODE_0 = 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) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %2 to i8*
%6 = load i32, i32* @SPI_MODE_0
store i32 %6, i32* %2
%7 = bitcast i32* %3 to i8*
store i32 16, i32* %3
%8 = bitcast i32* %4 to i8*
store i32 2000000, i32* %4
%9 = load i32, i32* @SPI_IOC_WR_MODE
%10 = call i32 @ioctl(i32 %0, i32 %9, i32* %2)
%11 = icmp eq i32 %10, -1
br i1 %11, label %21, label %12
12: ; preds = %1
%13 = load i32, i32* @SPI_IOC_WR_BITS_PER_WORD
%14 = call i32 @ioctl(i32 %0, i32 %13, i32* %3)
%15 = icmp eq i32 %14, -1
br i1 %15, label %21, label %16
16: ; preds = %12
%17 = load i32, i32* @SPI_IOC_WR_MAX_SPEED_HZ
%18 = call i32 @ioctl(i32 %0, i32 %17, i32* %4)
%19 = icmp eq i32 %18, -1
%20 = sext i1 %19 to i32
br label %21
21: ; preds = %16, %12, %1
%22 = phi i32 [ -1, %1 ], [ -1, %12 ], [ %20, %16 ]
ret i32 %22
}