#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 int16_t;
typedef int /*<<< orphan*/ data;

/* Variables and functions */
int EINVAL;
scalar_t__ __send_receive_bytes(int, unsigned char *, int);
int errno;
int mcp3008_read(int16_t *tenbitreading, int spi_fd, int mcp3008_channel,
                 int single) {
  unsigned char data[3];
  if (single < 0 || single > 1) {
    errno = EINVAL;
    return -1;
  }
  data[0] = 0b00000001;
  data[1] = ((single) << 7) | ((mcp3008_channel) << 4);
  data[2] = 0b00000000;
  if (__send_receive_bytes(spi_fd, data, sizeof(data)) < 0) {
    return errno;
  }
  *tenbitreading = (int16_t)(0b00000011 & data[1]) << 8 | data[2];
  return 0;
}


x86 assembly (clang, -O0):
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     1139 <mcp3008_read+0x29>
cmpl   $0x1,-0x1c(%rbp)
jle    1157 <mcp3008_read+0x47>
mov    0x2e98(%rip),%rax
mov    (%rax),%ecx
mov    0x2e97(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11b9 <mcp3008_read+0xa9>
movb   $0x1,-0x1f(%rbp)
mov    -0x1c(%rbp),%eax
shl    $0x7,%eax
mov    -0x18(%rbp),%ecx
shl    $0x4,%ecx
or     %ecx,%eax
mov    %al,-0x1e(%rbp)
movb   $0x0,-0x1d(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x1f(%rbp),%rsi
mov    $0x3,%edx
call   1030 <__send_receive_bytes@plt>
cmp    $0x0,%rax
jge    119c <mcp3008_read+0x8c>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11b9 <mcp3008_read+0xa9>
movzbl -0x1e(%rbp),%ecx
and    $0x3,%ecx
shl    $0x8,%ecx
movzbl -0x1d(%rbp),%eax
or     %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
push   %rbx
sub    $0x10,%rsp
cmp    $0x2,%ecx
jb     1133 <mcp3008_read+0x23>
mov    0x2eb7(%rip),%rax
mov    (%rax),%eax
mov    0x2eb6(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
jmp    1186 <mcp3008_read+0x76>
mov    %rdi,%rbx
movb   $0x1,0xd(%rsp)
shl    $0x7,%ecx
shl    $0x4,%edx
or     %ecx,%edx
mov    %dl,0xe(%rsp)
movb   $0x0,0xf(%rsp)
lea    0xd(%rsp),%rax
mov    %esi,%edi
mov    %rax,%rsi
mov    $0x3,%edx
call   1030 <__send_receive_bytes@plt>
test   %rax,%rax
js     117d <mcp3008_read+0x6d>
movzbl 0xe(%rsp),%eax
and    $0x3,%eax
shl    $0x8,%eax
movzbl 0xf(%rsp),%ecx
or     %eax,%ecx
mov    %ecx,(%rbx)
xor    %eax,%eax
jmp    1186 <mcp3008_read+0x76>
mov    0x2e5c(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
define i32 @mcp3008_read(i32* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [3 x i8]
store i32* %0, i32** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%11 = load i32, i32* %9
%12 = icmp slt i32 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %4
%14 = load i32, i32* %9
%15 = icmp sgt i32 %14, 1
br i1 %15, label %16, label %18
16: ; preds = %13, %4
%17 = load i32, i32* @EINVAL
store i32 %17, i32* @errno
store i32 -1, i32* %5
br label %45
18: ; preds = %13
%19 = getelementptr inbounds [3 x i8], [3 x i8]* %10, i64 0, i64 0
store i8 1, i8* %19
%20 = load i32, i32* %9
%21 = shl i32 %20, 7
%22 = load i32, i32* %8
%23 = shl i32 %22, 4
%24 = or i32 %21, %23
%25 = trunc i32 %24 to i8
%26 = getelementptr inbounds [3 x i8], [3 x i8]* %10, i64 0, i64 1
store i8 %25, i8* %26
%27 = getelementptr inbounds [3 x i8], [3 x i8]* %10, i64 0, i64 2
store i8 0, i8* %27
%28 = load i32, i32* %7
%29 = getelementptr inbounds [3 x i8], [3 x i8]* %10, i64 0, i64 0
%30 = call i64 @__send_receive_bytes(i32 %28, i8* %29, i32 3)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %18
%33 = load i32, i32* @errno
store i32 %33, i32* %5
br label %45
34: ; preds = %18
%35 = getelementptr inbounds [3 x i8], [3 x i8]* %10, i64 0, i64 1
%36 = load i8, i8* %35
%37 = zext i8 %36 to i32
%38 = and i32 3, %37
%39 = shl i32 %38, 8
%40 = getelementptr inbounds [3 x i8], [3 x i8]* %10, i64 0, i64 2
%41 = load i8, i8* %40
%42 = zext i8 %41 to i32
%43 = or i32 %39, %42
%44 = load i32*, i32** %6
store i32 %43, i32* %44
store i32 0, i32* %5
br label %45
45: ; preds = %34, %32, %16
%46 = load i32, i32* %5
ret i32 %46
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
define i32 @mcp3008_read(i32* nocapture writeonly %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca [3 x i8]
%6 = getelementptr inbounds [3 x i8], [3 x i8]* %5, i64 0, i64 0
%7 = icmp ugt i32 %3, 1
br i1 %7, label %8, label %10
8: ; preds = %4
%9 = load i32, i32* @EINVAL
store i32 %9, i32* @errno
br label %29
10: ; preds = %4
store i8 1, i8* %6
%11 = shl nuw nsw i32 %3, 7
%12 = shl i32 %2, 4
%13 = or i32 %11, %12
%14 = trunc i32 %13 to i8
%15 = getelementptr inbounds [3 x i8], [3 x i8]* %5, i64 0, i64 1
store i8 %14, i8* %15
%16 = getelementptr inbounds [3 x i8], [3 x i8]* %5, i64 0, i64 2
store i8 0, i8* %16
%17 = call i64 @__send_receive_bytes(i32 %1, i8* %6, i32 3)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %10
%20 = load i32, i32* @errno
br label %29
21: ; preds = %10
%22 = load i8, i8* %15
%23 = and i8 %22, 3
%24 = zext i8 %23 to i32
%25 = shl nuw nsw i32 %24, 8
%26 = load i8, i8* %16
%27 = zext i8 %26 to i32
%28 = or i32 %25, %27
store i32 %28, i32* %0
br label %29
29: ; preds = %21, %19, %8
%30 = phi i32 [ -1, %8 ], [ %20, %19 ], [ 0, %21 ]
ret i32 %30
}