#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 */
int /*<<< orphan*/ pinHigh(int);
int /*<<< orphan*/ pinLow(int);
scalar_t__ pinRead(int);
int mcp3008_read(int *result, int id, int mode, int sclk, int cs, int mosi,
                 int miso) {
  if (id > 7 || id < 0) {
    return 0;
  }
  pinHigh(cs);
  pinLow(sclk);
  pinLow(cs);
  int cmd = id;
  cmd |= mode;
  cmd <<= 3;
  for (int i = 0; i < 5; i++) {
    if (cmd & 0x80) {
      pinHigh(mosi);
    } else {
      pinLow(mosi);
    }
    cmd <<= 1;
    pinHigh(sclk);
    pinLow(sclk);
  }
  int out = 0;
  for (int i = 0; i < 12; i++) {
    pinHigh(sclk);
    pinLow(sclk);
    out <<= 1;
    if (pinRead(miso)) {
      out |= 0x1;
    }
  }
  pinHigh(cs);
  out >>= 1;
  *result = out;
  return 1;
}


x86 assembly (clang, -O0):
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x10(%rbp),%eax
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    %r9d,-0x24(%rbp)
cmpl   $0x7,-0x14(%rbp)
jg     1164 <mcp3008_read+0x34>
cmpl   $0x0,-0x14(%rbp)
jge    1170 <mcp3008_read+0x40>
movl   $0x0,-0x4(%rbp)
jmp    127e <mcp3008_read+0x14e>
mov    -0x20(%rbp),%edi
call   1030 <pinHigh@plt>
mov    -0x1c(%rbp),%edi
call   1040 <pinLow@plt>
mov    -0x20(%rbp),%edi
call   1040 <pinLow@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
shl    $0x3,%eax
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x5,-0x2c(%rbp)
jge    11fe <mcp3008_read+0xce>
mov    -0x28(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
je     11cf <mcp3008_read+0x9f>
mov    -0x24(%rbp),%edi
call   1030 <pinHigh@plt>
jmp    11d7 <mcp3008_read+0xa7>
mov    -0x24(%rbp),%edi
call   1040 <pinLow@plt>
mov    -0x28(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <pinHigh@plt>
mov    -0x1c(%rbp),%edi
call   1040 <pinLow@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11a7 <mcp3008_read+0x77>
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
cmpl   $0xc,-0x34(%rbp)
jge    125d <mcp3008_read+0x12d>
mov    -0x1c(%rbp),%edi
call   1030 <pinHigh@plt>
mov    -0x1c(%rbp),%edi
call   1040 <pinLow@plt>
mov    -0x30(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x30(%rbp)
mov    0x10(%rbp),%edi
call   1050 <pinRead@plt>
cmp    $0x0,%rax
je     124a <mcp3008_read+0x11a>
mov    -0x30(%rbp),%eax
or     $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    124f <mcp3008_read+0x11f>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    120c <mcp3008_read+0xdc>
mov    -0x20(%rbp),%edi
call   1030 <pinHigh@plt>
mov    -0x30(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
xor    %eax,%eax
cmp    $0x7,%esi
ja     1403 <mcp3008_read+0x2d3>
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9d,%r13d
mov    %r8d,%ebx
mov    %ecx,%r15d
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,0x10(%rsp)
mov    %r8d,%edi
call   1030 <pinHigh@plt>
mov    %r15d,%edi
call   1040 <pinLow@plt>
mov    %ebx,%edi
call   1040 <pinLow@plt>
or     %ebp,%r14d
mov    %r13d,%edi
test   $0x10,%r14b
jne    1186 <mcp3008_read+0x56>
call   1040 <pinLow@plt>
jmp    118b <mcp3008_read+0x5b>
call   1030 <pinHigh@plt>
mov    %r15d,%edi
call   1030 <pinHigh@plt>
mov    %r15d,%edi
call   1040 <pinLow@plt>
mov    %r13d,%edi
test   $0x8,%r14b
jne    11ab <mcp3008_read+0x7b>
call   1040 <pinLow@plt>
jmp    11b0 <mcp3008_read+0x80>
call   1030 <pinHigh@plt>
mov    %r15d,%edi
call   1030 <pinHigh@plt>
mov    %r15d,%edi
call   1040 <pinLow@plt>
mov    %r13d,%edi
test   $0x4,%r14b
jne    11d0 <mcp3008_read+0xa0>
call   1040 <pinLow@plt>
jmp    11d5 <mcp3008_read+0xa5>
call   1030 <pinHigh@plt>
mov    %r15d,%edi
call   1030 <pinHigh@plt>
mov    %r15d,%edi
call   1040 <pinLow@plt>
mov    %r13d,%edi
test   $0x2,%r14b
jne    11f5 <mcp3008_read+0xc5>
call   1040 <pinLow@plt>
jmp    11fa <mcp3008_read+0xca>
call   1030 <pinHigh@plt>
mov    0x50(%rsp),%r12d
mov    %r15d,%edi
call   1030 <pinHigh@plt>
mov    %r15d,%edi
call   1040 <pinLow@plt>
test   $0x1,%r14b
mov    %ebx,0xc(%rsp)
mov    %r13d,%edi
jne    1223 <mcp3008_read+0xf3>
call   1040 <pinLow@plt>
jmp    1228 <mcp3008_read+0xf8>
call   1030 <pinHigh@plt>
mov    %r15d,%edi
call   1030 <pinHigh@plt>
mov    %r15d,%edi
call   1040 <pinLow@plt>
mov    %r15d,%edi
call   1030 <pinHigh@plt>
mov    %r15d,%edi
call   1040 <pinLow@plt>
mov    %r12d,%edi
call   1050 <pinRead@plt>
xor    %ebp,%ebp
test   %rax,%rax
setne  %bpl
mov    %r15d,%edi
call   1030 <pinHigh@plt>
mov    %r15d,%edi
call   1040 <pinLow@plt>
mov    %r12d,%edi
call   1050 <pinRead@plt>
xor    %ecx,%ecx
test   %rax,%rax
setne  %cl
lea    (%rcx,%rbp,2),%r14d
mov    %r15d,%edi
call   1030 <pinHigh@plt>
mov    %r15d,%edi
call   1040 <pinLow@plt>
mov    %r12d,%edi
call   1050 <pinRead@plt>
xor    %ebp,%ebp
test   %rax,%rax
setne  %bpl
mov    %r15d,%edi
call   1030 <pinHigh@plt>
mov    %r15d,%edi
call   1040 <pinLow@plt>
add    %ebp,%ebp
lea    0x0(%rbp,%r14,4),%r14d
mov    %r12d,%edi
call   1050 <pinRead@plt>
xor    %ebp,%ebp
test   %rax,%rax
setne  %bpl
or     %r14d,%ebp
mov    %r15d,%edi
call   1030 <pinHigh@plt>
mov    %r15d,%edi
call   1040 <pinLow@plt>
mov    %r12d,%edi
call   1050 <pinRead@plt>
xor    %ebx,%ebx
test   %rax,%rax
setne  %bl
mov    %r15d,%edi
call   1030 <pinHigh@plt>
mov    %r15d,%edi
call   1040 <pinLow@plt>
add    %ebx,%ebx
lea    (%rbx,%rbp,4),%ebx
mov    %r12d,%edi
call   1050 <pinRead@plt>
xor    %ebp,%ebp
test   %rax,%rax
setne  %bpl
or     %ebx,%ebp
mov    %r15d,%edi
call   1030 <pinHigh@plt>
mov    %r15d,%edi
call   1040 <pinLow@plt>
mov    %r12d,%edi
call   1050 <pinRead@plt>
xor    %ebx,%ebx
test   %rax,%rax
setne  %bl
mov    %r15d,%edi
call   1030 <pinHigh@plt>
mov    %r15d,%edi
call   1040 <pinLow@plt>
add    %ebx,%ebx
lea    (%rbx,%rbp,4),%ebx
mov    %r12d,%edi
call   1050 <pinRead@plt>
xor    %ebp,%ebp
test   %rax,%rax
setne  %bpl
or     %ebx,%ebp
mov    %r15d,%edi
call   1030 <pinHigh@plt>
mov    %r15d,%edi
call   1040 <pinLow@plt>
mov    %r12d,%edi
call   1050 <pinRead@plt>
xor    %ebx,%ebx
test   %rax,%rax
setne  %bl
mov    %r15d,%edi
call   1030 <pinHigh@plt>
mov    %r15d,%edi
call   1040 <pinLow@plt>
add    %ebx,%ebx
lea    (%rbx,%rbp,4),%ebx
mov    %r12d,%edi
call   1050 <pinRead@plt>
xor    %ebp,%ebp
test   %rax,%rax
setne  %bpl
or     %ebx,%ebp
mov    %r15d,%edi
call   1030 <pinHigh@plt>
mov    %r15d,%edi
call   1040 <pinLow@plt>
mov    %r12d,%edi
call   1050 <pinRead@plt>
xor    %ebx,%ebx
test   %rax,%rax
setne  %bl
mov    %r15d,%edi
call   1030 <pinHigh@plt>
mov    %r15d,%edi
call   1040 <pinLow@plt>
add    %ebx,%ebx
lea    (%rbx,%rbp,4),%ebx
mov    %r12d,%edi
call   1050 <pinRead@plt>
mov    0xc(%rsp),%edi
call   1030 <pinHigh@plt>
sar    %ebx
mov    0x10(%rsp),%rax
mov    %ebx,(%rax)
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @mcp3008_read(i32* %0, i32 %1, i32 %2, i32 %3, i32 %4, i32 %5, i32 %6) {
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
store i32* %0, i32** %9
store i32 %1, i32* %10
store i32 %2, i32* %11
store i32 %3, i32* %12
store i32 %4, i32* %13
store i32 %5, i32* %14
store i32 %6, i32* %15
%20 = load i32, i32* %10
%21 = icmp sgt i32 %20, 7
br i1 %21, label %25, label %22
22: ; preds = %7
%23 = load i32, i32* %10
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %22, %7
store i32 0, i32* %8
br label %90
26: ; preds = %22
%27 = load i32, i32* %13
%28 = call i32 @pinHigh(i32 %27)
%29 = load i32, i32* %12
%30 = call i32 @pinLow(i32 %29)
%31 = load i32, i32* %13
%32 = call i32 @pinLow(i32 %31)
%33 = load i32, i32* %10
store i32 %33, i32* %16
%34 = load i32, i32* %11
%35 = load i32, i32* %16
%36 = or i32 %35, %34
store i32 %36, i32* %16
%37 = load i32, i32* %16
%38 = shl i32 %37, 3
store i32 %38, i32* %16
store i32 0, i32* %17
br label %39
39: ; preds = %59, %26
%40 = load i32, i32* %17
%41 = icmp slt i32 %40, 5
br i1 %41, label %42, label %62
42: ; preds = %39
%43 = load i32, i32* %16
%44 = and i32 %43, 128
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = load i32, i32* %14
%48 = call i32 @pinHigh(i32 %47)
br label %52
49: ; preds = %42
%50 = load i32, i32* %14
%51 = call i32 @pinLow(i32 %50)
br label %52
52: ; preds = %49, %46
%53 = load i32, i32* %16
%54 = shl i32 %53, 1
store i32 %54, i32* %16
%55 = load i32, i32* %12
%56 = call i32 @pinHigh(i32 %55)
%57 = load i32, i32* %12
%58 = call i32 @pinLow(i32 %57)
br label %59
59: ; preds = %52
%60 = load i32, i32* %17
%61 = add nsw i32 %60, 1
store i32 %61, i32* %17
br label %39
62: ; preds = %39
store i32 0, i32* %18
store i32 0, i32* %19
br label %63
63: ; preds = %80, %62
%64 = load i32, i32* %19
%65 = icmp slt i32 %64, 12
br i1 %65, label %66, label %83
66: ; preds = %63
%67 = load i32, i32* %12
%68 = call i32 @pinHigh(i32 %67)
%69 = load i32, i32* %12
%70 = call i32 @pinLow(i32 %69)
%71 = load i32, i32* %18
%72 = shl i32 %71, 1
store i32 %72, i32* %18
%73 = load i32, i32* %15
%74 = call i64 @pinRead(i32 %73)
%75 = icmp ne i64 %74, 0
br i1 %75, label %76, label %79
76: ; preds = %66
%77 = load i32, i32* %18
%78 = or i32 %77, 1
store i32 %78, i32* %18
br label %79
79: ; preds = %76, %66
br label %80
80: ; preds = %79
%81 = load i32, i32* %19
%82 = add nsw i32 %81, 1
store i32 %82, i32* %19
br label %63
83: ; preds = %63
%84 = load i32, i32* %13
%85 = call i32 @pinHigh(i32 %84)
%86 = load i32, i32* %18
%87 = ashr i32 %86, 1
store i32 %87, i32* %18
%88 = load i32, i32* %18
%89 = load i32*, i32** %9
store i32 %88, i32* %89
store i32 1, i32* %8
br label %90
90: ; preds = %83, %25
%91 = load i32, i32* %8
ret i32 %91
}

LLVM IR (-O2):
define i32 @mcp3008_read(i32* nocapture writeonly %0, i32 %1, i32 %2, i32 %3, i32 %4, i32 %5, i32 %6) {
%8 = icmp ugt i32 %1, 7
br i1 %8, label %134, label %9
9: ; preds = %7
%10 = tail call i32 @pinHigh(i32 %4)
%11 = tail call i32 @pinLow(i32 %3)
%12 = tail call i32 @pinLow(i32 %4)
%13 = or i32 %2, %1
%14 = and i32 %13, 16
%15 = icmp eq i32 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %9
%17 = tail call i32 @pinHigh(i32 %5)
br label %20
18: ; preds = %9
%19 = tail call i32 @pinLow(i32 %5)
br label %20
20: ; preds = %18, %16
%21 = tail call i32 @pinHigh(i32 %3)
%22 = tail call i32 @pinLow(i32 %3)
%23 = and i32 %13, 8
%24 = icmp eq i32 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %20
%26 = tail call i32 @pinHigh(i32 %5)
br label %29
27: ; preds = %20
%28 = tail call i32 @pinLow(i32 %5)
br label %29
29: ; preds = %27, %25
%30 = tail call i32 @pinHigh(i32 %3)
%31 = tail call i32 @pinLow(i32 %3)
%32 = and i32 %13, 4
%33 = icmp eq i32 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %29
%35 = tail call i32 @pinHigh(i32 %5)
br label %38
36: ; preds = %29
%37 = tail call i32 @pinLow(i32 %5)
br label %38
38: ; preds = %36, %34
%39 = tail call i32 @pinHigh(i32 %3)
%40 = tail call i32 @pinLow(i32 %3)
%41 = and i32 %13, 2
%42 = icmp eq i32 %41, 0
br i1 %42, label %45, label %43
43: ; preds = %38
%44 = tail call i32 @pinHigh(i32 %5)
br label %47
45: ; preds = %38
%46 = tail call i32 @pinLow(i32 %5)
br label %47
47: ; preds = %45, %43
%48 = tail call i32 @pinHigh(i32 %3)
%49 = tail call i32 @pinLow(i32 %3)
%50 = and i32 %13, 1
%51 = icmp eq i32 %50, 0
br i1 %51, label %54, label %52
52: ; preds = %47
%53 = tail call i32 @pinHigh(i32 %5)
br label %56
54: ; preds = %47
%55 = tail call i32 @pinLow(i32 %5)
br label %56
56: ; preds = %54, %52
%57 = tail call i32 @pinHigh(i32 %3)
%58 = tail call i32 @pinLow(i32 %3)
%59 = tail call i32 @pinHigh(i32 %3)
%60 = tail call i32 @pinLow(i32 %3)
%61 = tail call i64 @pinRead(i32 %6)
%62 = icmp eq i64 %61, 0
%63 = tail call i32 @pinHigh(i32 %3)
%64 = tail call i32 @pinLow(i32 %3)
%65 = select i1 %62, i32 0, i32 2
%66 = tail call i64 @pinRead(i32 %6)
%67 = icmp ne i64 %66, 0
%68 = zext i1 %67 to i32
%69 = or i32 %65, %68
%70 = tail call i32 @pinHigh(i32 %3)
%71 = tail call i32 @pinLow(i32 %3)
%72 = tail call i64 @pinRead(i32 %6)
%73 = icmp eq i64 %72, 0
%74 = tail call i32 @pinHigh(i32 %3)
%75 = tail call i32 @pinLow(i32 %3)
%76 = shl nuw nsw i32 %69, 2
%77 = select i1 %73, i32 0, i32 2
%78 = or i32 %76, %77
%79 = tail call i64 @pinRead(i32 %6)
%80 = icmp ne i64 %79, 0
%81 = zext i1 %80 to i32
%82 = or i32 %78, %81
%83 = tail call i32 @pinHigh(i32 %3)
%84 = tail call i32 @pinLow(i32 %3)
%85 = tail call i64 @pinRead(i32 %6)
%86 = icmp eq i64 %85, 0
%87 = tail call i32 @pinHigh(i32 %3)
%88 = tail call i32 @pinLow(i32 %3)
%89 = shl nuw nsw i32 %82, 2
%90 = select i1 %86, i32 0, i32 2
%91 = or i32 %89, %90
%92 = tail call i64 @pinRead(i32 %6)
%93 = icmp ne i64 %92, 0
%94 = zext i1 %93 to i32
%95 = or i32 %91, %94
%96 = tail call i32 @pinHigh(i32 %3)
%97 = tail call i32 @pinLow(i32 %3)
%98 = tail call i64 @pinRead(i32 %6)
%99 = icmp eq i64 %98, 0
%100 = tail call i32 @pinHigh(i32 %3)
%101 = tail call i32 @pinLow(i32 %3)
%102 = shl i32 %95, 2
%103 = select i1 %99, i32 0, i32 2
%104 = or i32 %102, %103
%105 = tail call i64 @pinRead(i32 %6)
%106 = icmp ne i64 %105, 0
%107 = zext i1 %106 to i32
%108 = or i32 %104, %107
%109 = tail call i32 @pinHigh(i32 %3)
%110 = tail call i32 @pinLow(i32 %3)
%111 = tail call i64 @pinRead(i32 %6)
%112 = icmp eq i64 %111, 0
%113 = tail call i32 @pinHigh(i32 %3)
%114 = tail call i32 @pinLow(i32 %3)
%115 = shl i32 %108, 2
%116 = select i1 %112, i32 0, i32 2
%117 = or i32 %115, %116
%118 = tail call i64 @pinRead(i32 %6)
%119 = icmp ne i64 %118, 0
%120 = zext i1 %119 to i32
%121 = or i32 %117, %120
%122 = tail call i32 @pinHigh(i32 %3)
%123 = tail call i32 @pinLow(i32 %3)
%124 = tail call i64 @pinRead(i32 %6)
%125 = icmp eq i64 %124, 0
%126 = tail call i32 @pinHigh(i32 %3)
%127 = tail call i32 @pinLow(i32 %3)
%128 = shl i32 %121, 2
%129 = select i1 %125, i32 0, i32 2
%130 = or i32 %128, %129
%131 = tail call i64 @pinRead(i32 %6)
%132 = tail call i32 @pinHigh(i32 %4)
%133 = ashr exact i32 %130, 1
store i32 %133, i32* %0
br label %134
134: ; preds = %7, %56
%135 = phi i32 [ 1, %56 ], [ 0, %7 ]
ret i32 %135
}