#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*/ O_RDWR;
int /*<<< orphan*/ SPI_IOC_WR_BITS_PER_WORD;
int /*<<< orphan*/ SPI_IOC_WR_LSB_FIRST;
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 /*<<< orphan*/ print_errno(char *, ...);
int init_spidev(void) {
  const char path[] = "/dev/spidev0.0";
  int fd;
  uint8_t b;
  uint32_t speed;
  fd = open(path, O_RDWR);
  if (fd < 0) {
    print_errno("opening \"%s\"", path);
    goto err;
  }
  b = 0;
  if (ioctl(fd, SPI_IOC_WR_MODE, &b) < 0) {
    print_errno("Setting SPI mode");
    goto err_close;
  }
  b = 0;
  if (ioctl(fd, SPI_IOC_WR_LSB_FIRST, &b) < 0) {
    print_errno("Setting SPI LSB-first");
    goto err_close;
  }
  b = 0;
  if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &b) < 0) {
    print_errno("Setting SPI bits-per-word");
    goto err_close;
  }
  speed = 200000;
  if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0) {
    print_errno("Setting SPI speed");
    goto err_close;
  }
  return fd;
err_close:
  close(fd);
err:
  return -1;
}


x86 assembly (clang, -O0):
<init_spidev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0xeb1(%rip),%rax
mov    %rax,-0x13(%rbp)
mov    0xeaf(%rip),%eax
mov    %eax,-0xb(%rbp)
mov    0xea9(%rip),%ax
mov    %ax,-0x7(%rbp)
mov    0xea1(%rip),%al
mov    %al,-0x5(%rbp)
lea    -0x13(%rbp),%rdi
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11a6 <init_spidev+0x66>
lea    -0x13(%rbp),%rsi
lea    0xe75(%rip),%rdi
mov    $0x0,%al
call   1030 <print_errno@plt>
jmp    129d <init_spidev+0x15d>
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
lea    -0x1c(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11df <init_spidev+0x9f>
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1030 <print_errno@plt>
jmp    1295 <init_spidev+0x155>
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
lea    -0x1c(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    1218 <init_spidev+0xd8>
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1030 <print_errno@plt>
jmp    1295 <init_spidev+0x155>
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
lea    -0x1c(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    1251 <init_spidev+0x111>
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1030 <print_errno@plt>
jmp    1295 <init_spidev+0x155>
movl   $0x30d40,-0x20(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2d5e(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    128a <init_spidev+0x14a>
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1030 <print_errno@plt>
jmp    1295 <init_spidev+0x155>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12a4 <init_spidev+0x164>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_spidev>:
push   %rbx
sub    $0x20,%rsp
movabs $0x302e3076656469,%rax
mov    %rax,0x17(%rsp)
movabs $0x6970732f7665642f,%rax
73 70 69
mov    %rax,0x10(%rsp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     1215 <init_spidev+0xd5>
mov    %eax,%ebx
movl   $0x0,0x8(%rsp)
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     122a <init_spidev+0xea>
movl   $0x0,0x8(%rsp)
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     1233 <init_spidev+0xf3>
movl   $0x0,0x8(%rsp)
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     123c <init_spidev+0xfc>
movl   $0x30d40,0xc(%rsp)
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
jns    1256 <init_spidev+0x116>
lea    0xe4a(%rip),%rdi
jmp    1243 <init_spidev+0x103>
lea    0xdf3(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1030 <print_errno@plt>
jmp    1251 <init_spidev+0x111>
lea    0xdeb(%rip),%rdi
jmp    1243 <init_spidev+0x103>
lea    0xdf3(%rip),%rdi
jmp    1243 <init_spidev+0x103>
lea    0xe00(%rip),%rdi
xor    %eax,%eax
call   1030 <print_errno@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@__const.init_spidev.path = [15 x i8] c"/dev/spidev0.0\00"
@O_RDWR = global i32 0
@.str = [13 x i8] c"opening \22%s\22\00"
@SPI_IOC_WR_MODE = global i32 0
@.str.1 = [17 x i8] c"Setting SPI mode\00"
@SPI_IOC_WR_LSB_FIRST = global i32 0
@.str.2 = [22 x i8] c"Setting SPI LSB-first\00"
@SPI_IOC_WR_BITS_PER_WORD = global i32 0
@.str.3 = [26 x i8] c"Setting SPI bits-per-word\00"
@SPI_IOC_WR_MAX_SPEED_HZ = global i32 0
@.str.4 = [18 x i8] c"Setting SPI speed\00"
define i32 @init_spidev() {
%1 = alloca i32
%2 = alloca [15 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = bitcast [15 x i8]* %2 to i8*
%7 = getelementptr inbounds [15 x i8], [15 x i8]* %2, i64 0, i64 0
%8 = load i32, i32* @O_RDWR
%9 = call i32 @open(i8* %7, i32 %8)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = getelementptr inbounds [15 x i8], [15 x i8]* %2, i64 0, i64 0
%14 = call i32 (i8*, ...) @print_errno(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %13)
br label %48
15: ; preds = %0
store i32 0, i32* %4
%16 = load i32, i32* %3
%17 = load i32, i32* @SPI_IOC_WR_MODE
%18 = call i64 @ioctl(i32 %16, i32 %17, i32* %4)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 (i8*, ...) @print_errno(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %45
22: ; preds = %15
store i32 0, i32* %4
%23 = load i32, i32* %3
%24 = load i32, i32* @SPI_IOC_WR_LSB_FIRST
%25 = call i64 @ioctl(i32 %23, i32 %24, i32* %4)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = call i32 (i8*, ...) @print_errno(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %45
29: ; preds = %22
store i32 0, i32* %4
%30 = load i32, i32* %3
%31 = load i32, i32* @SPI_IOC_WR_BITS_PER_WORD
%32 = call i64 @ioctl(i32 %30, i32 %31, i32* %4)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %29
%35 = call i32 (i8*, ...) @print_errno(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %45
36: ; preds = %29
store i32 200000, i32* %5
%37 = load i32, i32* %3
%38 = load i32, i32* @SPI_IOC_WR_MAX_SPEED_HZ
%39 = call i64 @ioctl(i32 %37, i32 %38, i32* %5)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %36
%42 = call i32 (i8*, ...) @print_errno(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %45
43: ; preds = %36
%44 = load i32, i32* %3
store i32 %44, i32* %1
br label %49
45: ; preds = %41, %34, %27, %20
%46 = load i32, i32* %3
%47 = call i32 @close(i32 %46)
br label %48
48: ; preds = %45, %12
store i32 -1, i32* %1
br label %49
49: ; preds = %48, %43
%50 = load i32, i32* %1
ret i32 %50
}

LLVM IR (-O2):
@__const.init_spidev.path = [15 x i8] c"/dev/spidev0.0\00"
@O_RDWR = global i32 0
@.str = [13 x i8] c"opening \22%s\22\00"
@SPI_IOC_WR_MODE = global i32 0
@.str.1 = [17 x i8] c"Setting SPI mode\00"
@SPI_IOC_WR_LSB_FIRST = global i32 0
@.str.2 = [22 x i8] c"Setting SPI LSB-first\00"
@SPI_IOC_WR_BITS_PER_WORD = global i32 0
@.str.3 = [26 x i8] c"Setting SPI bits-per-word\00"
@SPI_IOC_WR_MAX_SPEED_HZ = global i32 0
@.str.4 = [18 x i8] c"Setting SPI speed\00"
define i32 @init_spidev() {
%1 = alloca [15 x i8]
%2 = alloca i32
%3 = alloca i32
%4 = getelementptr inbounds [15 x i8], [15 x i8]* %1, i64 0, i64 0
%5 = bitcast i32* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* %4, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 (i8*, ...) @print_errno(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %4)
br label %32
12: ; preds = %0
store i32 0, i32* %2
%13 = load i32, i32* @SPI_IOC_WR_MODE
%14 = call i64 @ioctl(i32 %8, i32 %13, i32* %2)
%15 = icmp slt i64 %14, 0
br i1 %15, label %28, label %16
16: ; preds = %12
store i32 0, i32* %2
%17 = load i32, i32* @SPI_IOC_WR_LSB_FIRST
%18 = call i64 @ioctl(i32 %8, i32 %17, i32* %2)
%19 = icmp slt i64 %18, 0
br i1 %19, label %28, label %20
20: ; preds = %16
store i32 0, i32* %2
%21 = load i32, i32* @SPI_IOC_WR_BITS_PER_WORD
%22 = call i64 @ioctl(i32 %8, i32 %21, i32* %2)
%23 = icmp slt i64 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %20
store i32 200000, i32* %3
%25 = load i32, i32* @SPI_IOC_WR_MAX_SPEED_HZ
%26 = call i64 @ioctl(i32 %8, i32 %25, i32* %3)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %24, %20, %16, %12
%29 = phi i8* [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), %12 ], [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), %16 ], [ getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), %20 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), %24 ]
%30 = call i32 (i8*, ...) @print_errno(i8* %29)
%31 = call i32 @close(i32 %8)
br label %32
32: ; preds = %10, %28, %24
%33 = phi i32 [ %8, %24 ], [ -1, %28 ], [ -1, %10 ]
ret i32 %33
}