#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 SPI_CPHA;
int SPI_CPOL;
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 /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int ioctl(int, int /*<<< orphan*/, int *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ send_file(int, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  uint8_t mode = (SPI_CPHA | SPI_CPOL);
  uint8_t bits = 8;
  uint32_t speed = 1000000;
  int ret = 0;
  int fd;
  if ((fd = open("/dev/spidev1.0", O_RDWR)) < 0) {
    perror("can't open device");
    exit(-1);
  }
  if ((ret = ioctl(fd, SPI_IOC_WR_MODE, &mode)) < 0) {
    perror("can't set spi mode");
    close(fd);
    exit(-1);
  }
  if ((ret = ioctl(fd, SPI_IOC_RD_MODE, &mode)) < 0) {
    perror("can't get spi mode");
    close(fd);
    exit(-1);
  }
  if (bits) {
    if ((ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits)) < 0) {
      perror("can't set bits per word");
      close(fd);
      exit(-1);
    }
  }
  if ((ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits)) < 0) {
    perror("can't get bits per word");
    close(fd);
    exit(-1);
  }
  if (speed) {
    if ((ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed)) < 0) {
      perror("can't set max speed hz");
      close(fd);
      exit(-1);
    }
  }
  if ((ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed)) < 0) {
    perror("can't get max speed hz");
    close(fd);
    exit(-1);
  }
  send_file(fd, 0);
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
mov    0x2e22(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x14(%rbp)
movl   $0x8,-0x18(%rbp)
movl   $0xf4240,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
lea    0xe50(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11d7 <main+0x77>
lea    0xe47(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x24(%rbp),%edi
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
lea    -0x14(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    1216 <main+0xb6>
lea    0xe22(%rip),%rdi
call   1060 <perror@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x24(%rbp),%edi
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
lea    -0x14(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    1255 <main+0xf5>
lea    0xdf6(%rip),%rdi
call   1060 <perror@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x18(%rbp)
je     12a3 <main+0x143>
mov    -0x24(%rbp),%edi
mov    0x2d77(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    129e <main+0x13e>
lea    0xdc0(%rip),%rdi
call   1060 <perror@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
jmp    12a3 <main+0x143>
mov    -0x24(%rbp),%edi
mov    0x2d23(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    12e2 <main+0x182>
lea    0xd94(%rip),%rdi
call   1060 <perror@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
je     1330 <main+0x1d0>
mov    -0x24(%rbp),%edi
mov    0x2caa(%rip),%rax
mov    (%rax),%esi
lea    -0x1c(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    132b <main+0x1cb>
lea    0xd63(%rip),%rdi
call   1060 <perror@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
jmp    1330 <main+0x1d0>
mov    -0x24(%rbp),%edi
mov    0x2c7e(%rip),%rax
mov    (%rax),%esi
lea    -0x1c(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    136f <main+0x20f>
lea    0xd36(%rip),%rdi
call   1060 <perror@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x24(%rbp),%edi
xor    %esi,%esi
call   1070 <send_file@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    0x2e5b(%rip),%rax
mov    0x2e34(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x14(%rsp)
movl   $0x8,0x10(%rsp)
movl   $0xf4240,0xc(%rsp)
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
lea    0xe64(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     1268 <main+0x108>
mov    %eax,%ebx
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
lea    0x14(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %eax,%eax
js     127e <main+0x11e>
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
lea    0x14(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %eax,%eax
js     1287 <main+0x127>
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %eax,%eax
js     1290 <main+0x130>
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %eax,%eax
js     1299 <main+0x139>
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %eax,%eax
js     12a2 <main+0x142>
mov    0x2d7d(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %eax,%eax
js     12ab <main+0x14b>
mov    %eax,%ebp
mov    %ebx,%edi
xor    %esi,%esi
call   1070 <send_file@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xda0(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
lea    0xd9c(%rip),%rdi
jmp    12b2 <main+0x152>
lea    0xda6(%rip),%rdi
jmp    12b2 <main+0x152>
lea    0xdb0(%rip),%rdi
jmp    12b2 <main+0x152>
lea    0xdbf(%rip),%rdi
jmp    12b2 <main+0x152>
lea    0xdce(%rip),%rdi
jmp    12b2 <main+0x152>
lea    0xddc(%rip),%rdi
call   1060 <perror@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@SPI_CPHA = global i32 0
@SPI_CPOL = global i32 0
@.str = [15 x i8] c"/dev/spidev1.0\00"
@O_RDWR = global i32 0
@.str.1 = [18 x i8] c"can't open device\00"
@SPI_IOC_WR_MODE = global i32 0
@.str.2 = [19 x i8] c"can't set spi mode\00"
@SPI_IOC_RD_MODE = global i32 0
@.str.3 = [19 x i8] c"can't get spi mode\00"
@SPI_IOC_WR_BITS_PER_WORD = global i32 0
@.str.4 = [24 x i8] c"can't set bits per word\00"
@SPI_IOC_RD_BITS_PER_WORD = global i32 0
@.str.5 = [24 x i8] c"can't get bits per word\00"
@SPI_IOC_WR_MAX_SPEED_HZ = global i32 0
@.str.6 = [23 x i8] c"can't set max speed hz\00"
@SPI_IOC_RD_MAX_SPEED_HZ = global i32 0
@.str.7 = [23 x i8] c"can't get max speed hz\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @SPI_CPHA
%12 = load i32, i32* @SPI_CPOL
%13 = or i32 %11, %12
store i32 %13, i32* %6
store i32 8, i32* %7
store i32 1000000, i32* %8
store i32 0, i32* %9
%14 = load i32, i32* @O_RDWR
%15 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 %15, i32* %10
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %2
%21 = load i32, i32* %10
%22 = load i32, i32* @SPI_IOC_WR_MODE
%23 = call i32 @ioctl(i32 %21, i32 %22, i32* %6)
store i32 %23, i32* %9
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %20
%26 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%27 = load i32, i32* %10
%28 = call i32 @close(i32 %27)
%29 = call i32 @exit(i32 -1)
unreachable
30: ; preds = %20
%31 = load i32, i32* %10
%32 = load i32, i32* @SPI_IOC_RD_MODE
%33 = call i32 @ioctl(i32 %31, i32 %32, i32* %6)
store i32 %33, i32* %9
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %30
%36 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%37 = load i32, i32* %10
%38 = call i32 @close(i32 %37)
%39 = call i32 @exit(i32 -1)
unreachable
40: ; preds = %30
%41 = load i32, i32* %7
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %54
43: ; preds = %40
%44 = load i32, i32* %10
%45 = load i32, i32* @SPI_IOC_WR_BITS_PER_WORD
%46 = call i32 @ioctl(i32 %44, i32 %45, i32* %7)
store i32 %46, i32* %9
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %53
48: ; preds = %43
%49 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
%50 = load i32, i32* %10
%51 = call i32 @close(i32 %50)
%52 = call i32 @exit(i32 -1)
unreachable
53: ; preds = %43
br label %54
54: ; preds = %53, %40
%55 = load i32, i32* %10
%56 = load i32, i32* @SPI_IOC_RD_BITS_PER_WORD
%57 = call i32 @ioctl(i32 %55, i32 %56, i32* %7)
store i32 %57, i32* %9
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %64
59: ; preds = %54
%60 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
%61 = load i32, i32* %10
%62 = call i32 @close(i32 %61)
%63 = call i32 @exit(i32 -1)
unreachable
64: ; preds = %54
%65 = load i32, i32* %8
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %78
67: ; preds = %64
%68 = load i32, i32* %10
%69 = load i32, i32* @SPI_IOC_WR_MAX_SPEED_HZ
%70 = call i32 @ioctl(i32 %68, i32 %69, i32* %8)
store i32 %70, i32* %9
%71 = icmp slt i32 %70, 0
br i1 %71, label %72, label %77
72: ; preds = %67
%73 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
%74 = load i32, i32* %10
%75 = call i32 @close(i32 %74)
%76 = call i32 @exit(i32 -1)
unreachable
77: ; preds = %67
br label %78
78: ; preds = %77, %64
%79 = load i32, i32* %10
%80 = load i32, i32* @SPI_IOC_RD_MAX_SPEED_HZ
%81 = call i32 @ioctl(i32 %79, i32 %80, i32* %8)
store i32 %81, i32* %9
%82 = icmp slt i32 %81, 0
br i1 %82, label %83, label %88
83: ; preds = %78
%84 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0))
%85 = load i32, i32* %10
%86 = call i32 @close(i32 %85)
%87 = call i32 @exit(i32 -1)
unreachable
88: ; preds = %78
%89 = load i32, i32* %10
%90 = call i32 @send_file(i32 %89, i32 0)
%91 = load i32, i32* %10
%92 = call i32 @close(i32 %91)
%93 = load i32, i32* %9
ret i32 %93
}

LLVM IR (-O2):
@SPI_CPHA = global i32 0
@SPI_CPOL = global i32 0
@.str = [15 x i8] c"/dev/spidev1.0\00"
@O_RDWR = global i32 0
@.str.1 = [18 x i8] c"can't open device\00"
@SPI_IOC_WR_MODE = global i32 0
@.str.2 = [19 x i8] c"can't set spi mode\00"
@SPI_IOC_RD_MODE = global i32 0
@.str.3 = [19 x i8] c"can't get spi mode\00"
@SPI_IOC_WR_BITS_PER_WORD = global i32 0
@.str.4 = [24 x i8] c"can't set bits per word\00"
@SPI_IOC_RD_BITS_PER_WORD = global i32 0
@.str.5 = [24 x i8] c"can't get bits per word\00"
@SPI_IOC_WR_MAX_SPEED_HZ = global i32 0
@.str.6 = [23 x i8] c"can't set max speed hz\00"
@SPI_IOC_RD_MAX_SPEED_HZ = global i32 0
@.str.7 = [23 x i8] c"can't get max speed hz\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %3 to i8*
%7 = load i32, i32* @SPI_CPHA
%8 = load i32, i32* @SPI_CPOL
%9 = or i32 %8, %7
store i32 %9, i32* %3
%10 = bitcast i32* %4 to i8*
store i32 8, i32* %4
%11 = bitcast i32* %5 to i8*
store i32 1000000, i32* %5
%12 = load i32, i32* @O_RDWR
%13 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i32 @exit(i32 -1)
unreachable
18: ; preds = %2
%19 = load i32, i32* @SPI_IOC_WR_MODE
%20 = call i32 @ioctl(i32 %13, i32 %19, i32* %3)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @close(i32 %13)
%25 = call i32 @exit(i32 -1)
unreachable
26: ; preds = %18
%27 = load i32, i32* @SPI_IOC_RD_MODE
%28 = call i32 @ioctl(i32 %13, i32 %27, i32* %3)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %26
%31 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 @close(i32 %13)
%33 = call i32 @exit(i32 -1)
unreachable
34: ; preds = %26
%35 = load i32, i32* @SPI_IOC_WR_BITS_PER_WORD
%36 = call i32 @ioctl(i32 %13, i32 %35, i32* %4)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %34
%39 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
%40 = call i32 @close(i32 %13)
%41 = call i32 @exit(i32 -1)
unreachable
42: ; preds = %34
%43 = load i32, i32* @SPI_IOC_RD_BITS_PER_WORD
%44 = call i32 @ioctl(i32 %13, i32 %43, i32* %4)
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %42
%47 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
%48 = call i32 @close(i32 %13)
%49 = call i32 @exit(i32 -1)
unreachable
50: ; preds = %42
%51 = load i32, i32* @SPI_IOC_WR_MAX_SPEED_HZ
%52 = call i32 @ioctl(i32 %13, i32 %51, i32* %5)
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %58
54: ; preds = %50
%55 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
%56 = call i32 @close(i32 %13)
%57 = call i32 @exit(i32 -1)
unreachable
58: ; preds = %50
%59 = load i32, i32* @SPI_IOC_RD_MAX_SPEED_HZ
%60 = call i32 @ioctl(i32 %13, i32 %59, i32* %5)
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %66
62: ; preds = %58
%63 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0))
%64 = call i32 @close(i32 %13)
%65 = call i32 @exit(i32 -1)
unreachable
66: ; preds = %58
%67 = call i32 @send_file(i32 %13, i32 0)
%68 = call i32 @close(i32 %13)
ret i32 %60
}