#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 */

/* Variables and functions */
scalar_t__ spi_rw(int, unsigned char *, int);
int mcp3008_read(int spi_fd, int adc_channel) {
  unsigned char data[3];
  unsigned int val = 0;
  data[0] = 1;
  data[1] = 0x80 | ((adc_channel & 7) << 4);
  data[2] = 0;
  if (spi_rw(spi_fd, data, 3) < 0)
    return -1;
  val = (data[1] << 8) & 0x300;
  val |= data[2] & 0xFF;
  return val;
}


x86 assembly (clang, -O0):
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
movb   $0x1,-0xf(%rbp)
mov    -0xc(%rbp),%eax
and    $0x7,%eax
shl    $0x4,%eax
or     $0x80,%eax
mov    %al,-0xe(%rbp)
movb   $0x0,-0xd(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xf(%rbp),%rsi
mov    $0x3,%edx
call   1030 <spi_rw@plt>
cmp    $0x0,%rax
jge    1165 <mcp3008_read+0x55>
movl   $0xffffffff,-0x4(%rbp)
jmp    1189 <mcp3008_read+0x79>
movzbl -0xe(%rbp),%eax
shl    $0x8,%eax
and    $0x300,%eax
mov    %eax,-0x14(%rbp)
movzbl -0xd(%rbp),%eax
and    $0xff,%eax
or     -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
push   %rax
movb   $0x1,0x5(%rsp)
shl    $0x4,%sil
or     $0x80,%sil
mov    %sil,0x6(%rsp)
movb   $0x0,0x7(%rsp)
lea    0x5(%rsp),%rsi
mov    $0x3,%edx
call   1030 <spi_rw@plt>
test   %rax,%rax
js     1150 <mcp3008_read+0x40>
movzbl 0x6(%rsp),%ecx
and    $0x3,%ecx
shl    $0x8,%ecx
movzbl 0x7(%rsp),%eax
or     %ecx,%eax
pop    %rcx
ret
mov    $0xffffffff,%eax
pop    %rcx
ret

LLVM IR (-O0):
define i32 @mcp3008_read(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [3 x i8]
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 0, i32* %7
%8 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 0
store i8 1, i8* %8
%9 = load i32, i32* %5
%10 = and i32 %9, 7
%11 = shl i32 %10, 4
%12 = or i32 128, %11
%13 = trunc i32 %12 to i8
%14 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 1
store i8 %13, i8* %14
%15 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 2
store i8 0, i8* %15
%16 = load i32, i32* %4
%17 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 0
%18 = call i64 @spi_rw(i32 %16, i8* %17, i32 3)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %2
store i32 -1, i32* %3
br label %34
21: ; preds = %2
%22 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 1
%23 = load i8, i8* %22
%24 = zext i8 %23 to i32
%25 = shl i32 %24, 8
%26 = and i32 %25, 768
store i32 %26, i32* %7
%27 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 2
%28 = load i8, i8* %27
%29 = zext i8 %28 to i32
%30 = and i32 %29, 255
%31 = load i32, i32* %7
%32 = or i32 %31, %30
store i32 %32, i32* %7
%33 = load i32, i32* %7
store i32 %33, i32* %3
br label %34
34: ; preds = %21, %20
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
define i32 @mcp3008_read(i32 %0, i32 %1) {
%3 = alloca [3 x i8]
%4 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
store i8 1, i8* %4
%5 = trunc i32 %1 to i8
%6 = shl i8 %5, 4
%7 = or i8 %6, -128
%8 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 1
store i8 %7, i8* %8
%9 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 2
store i8 0, i8* %9
%10 = call i64 @spi_rw(i32 %0, i8* %4, i32 3)
%11 = icmp slt i64 %10, 0
br i1 %11, label %20, label %12
12: ; preds = %2
%13 = load i8, i8* %8
%14 = zext i8 %13 to i32
%15 = shl nuw nsw i32 %14, 8
%16 = and i32 %15, 768
%17 = load i8, i8* %9
%18 = zext i8 %17 to i32
%19 = or i32 %16, %18
br label %20
20: ; preds = %2, %12
%21 = phi i32 [ %19, %12 ], [ -1, %2 ]
ret i32 %21
}