#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 */
struct timespec {
  int dummy;
};
typedef int /*<<< orphan*/ data;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
scalar_t__ spiWriteRead(unsigned char *, int);
int get_mcp3008_channels(int a2dVal[], int nchannels, struct timespec *ptp) {
  unsigned char a2dChannel = 0;
  unsigned char data[3];
  unsigned char byte0, byte2, chbyte;
  byte0 = 1;
  byte2 = 0;
  int gooddata = 0;
  int badtransfers = 0;
  for (a2dChannel = 0; a2dChannel < nchannels; a2dChannel++) {
    chbyte = 0b10000000 | (((a2dChannel & 7) << 4));
    gooddata = 0;
    while (!gooddata) {
      data[0] = byte0;
      data[1] = chbyte;
      data[2] = byte2;
      if (spiWriteRead(data, sizeof(data)) > 0)
        gooddata = 1;
      else
        badtransfers++;
    }
    a2dVal[a2dChannel] = 0;
    a2dVal[a2dChannel] = (data[1] << 8) & 0b1100000000;
    a2dVal[a2dChannel] |= (data[2] & 0xff);
  }
  clock_gettime(CLOCK_REALTIME, ptp);
  return badtransfers;
}


x86 assembly (clang, -O0):
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movb   $0x0,-0x19(%rbp)
movb   $0x1,-0x1d(%rbp)
movb   $0x0,-0x1e(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movb   $0x0,-0x19(%rbp)
movzbl -0x19(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    121d <get_mcp3008_channels+0xfd>
movzbl -0x19(%rbp),%eax
and    $0x7,%eax
shl    $0x4,%eax
or     $0x80,%eax
mov    %al,-0x1f(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    118d <get_mcp3008_channels+0x6d>
jmp    11d1 <get_mcp3008_channels+0xb1>
mov    -0x1d(%rbp),%al
mov    %al,-0x1c(%rbp)
mov    -0x1f(%rbp),%al
mov    %al,-0x1b(%rbp)
mov    -0x1e(%rbp),%al
mov    %al,-0x1a(%rbp)
lea    -0x1c(%rbp),%rdi
mov    $0x3,%esi
call   1030 <spiWriteRead@plt>
cmp    $0x0,%rax
jle    11c3 <get_mcp3008_channels+0xa3>
movl   $0x1,-0x24(%rbp)
jmp    11cc <get_mcp3008_channels+0xac>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1177 <get_mcp3008_channels+0x57>
mov    -0x8(%rbp),%rax
movzbl -0x19(%rbp),%ecx
movl   $0x0,(%rax,%rcx,4)
movzbl -0x1b(%rbp),%edx
shl    $0x8,%edx
and    $0x300,%edx
mov    -0x8(%rbp),%rax
movzbl -0x19(%rbp),%ecx
mov    %edx,(%rax,%rcx,4)
movzbl -0x1a(%rbp),%edx
and    $0xff,%edx
mov    -0x8(%rbp),%rax
movzbl -0x19(%rbp),%ecx
or     (%rax,%rcx,4),%edx
mov    %edx,(%rax,%rcx,4)
mov    -0x19(%rbp),%al
add    $0x1,%al
mov    %al,-0x19(%rbp)
jmp    1151 <get_mcp3008_channels+0x31>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
call   1040 <clock_gettime@plt>
mov    -0x28(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %esi,%esi
jle    11d0 <get_mcp3008_channels+0xb0>
mov    %esi,%r12d
mov    %rdi,%r15
mov    %rdx,0x10(%rsp)
xor    %ecx,%ecx
lea    0xd(%rsp),%rbx
xor    %r13d,%r13d
nopl   0x0(%rax,%rax,1)
mov    %r13d,%r14d
shl    $0x4,%r14b
or     $0x80,%r14b
add    $0xffffffff,%ecx
mov    %ecx,%ebp
movb   $0x1,0xd(%rsp)
mov    %r14b,0xe(%rsp)
movb   $0x0,0xf(%rsp)
mov    %rbx,%rdi
mov    $0x3,%esi
call   1030 <spiWriteRead@plt>
add    $0x1,%ebp
test   %rax,%rax
jle    1160 <get_mcp3008_channels+0x40>
setg   %cl
movzbl %r13b,%edx
movzbl 0xe(%rsp),%esi
and    $0x3,%esi
shl    $0x8,%esi
movzbl 0xf(%rsp),%edi
or     %esi,%edi
mov    %edi,(%r15,%rdx,4)
add    $0x1,%r13b
movzbl %r13b,%edx
add    $0x1,%cl
movzbl %cl,%ecx
and    $0x1,%ecx
add    %ebp,%ecx
cmp    %r12d,%edx
jl     1150 <get_mcp3008_channels+0x30>
test   %rax,%rax
setg   %al
add    $0x1,%al
movzbl %al,%ebx
and    $0x1,%ebx
add    %ebp,%ebx
mov    0x10(%rsp),%rdx
jmp    11d2 <get_mcp3008_channels+0xb2>
xor    %ebx,%ebx
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
mov    %rdx,%rsi
call   1040 <clock_gettime@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
@CLOCK_REALTIME = global i32 0
define i32 @get_mcp3008_channels(i32* %0, i32 %1, %struct.timespec* %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca %struct.timespec*
%7 = alloca i8
%8 = alloca [3 x i8]
%9 = alloca i8
%10 = alloca i8
%11 = alloca i8
%12 = alloca i32
%13 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store %struct.timespec* %2, %struct.timespec** %6
store i8 0, i8* %7
store i8 1, i8* %9
store i8 0, i8* %10
store i32 0, i32* %12
store i32 0, i32* %13
store i8 0, i8* %7
br label %14
14: ; preds = %69, %3
%15 = load i8, i8* %7
%16 = zext i8 %15 to i32
%17 = load i32, i32* %5
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %72
19: ; preds = %14
%20 = load i8, i8* %7
%21 = zext i8 %20 to i32
%22 = and i32 %21, 7
%23 = shl i32 %22, 4
%24 = or i32 128, %23
%25 = trunc i32 %24 to i8
store i8 %25, i8* %11
store i32 0, i32* %12
br label %26
26: ; preds = %44, %19
%27 = load i32, i32* %12
%28 = icmp ne i32 %27, 0
%29 = xor i1 %28, true
br i1 %29, label %30, label %45
30: ; preds = %26
%31 = load i8, i8* %9
%32 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 0
store i8 %31, i8* %32
%33 = load i8, i8* %11
%34 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 1
store i8 %33, i8* %34
%35 = load i8, i8* %10
%36 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 2
store i8 %35, i8* %36
%37 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 0
%38 = call i64 @spiWriteRead(i8* %37, i32 3)
%39 = icmp sgt i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %30
store i32 1, i32* %12
br label %44
41: ; preds = %30
%42 = load i32, i32* %13
%43 = add nsw i32 %42, 1
store i32 %43, i32* %13
br label %44
44: ; preds = %41, %40
br label %26
45: ; preds = %26
%46 = load i32*, i32** %4
%47 = load i8, i8* %7
%48 = zext i8 %47 to i64
%49 = getelementptr inbounds i32, i32* %46, i64 %48
store i32 0, i32* %49
%50 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 1
%51 = load i8, i8* %50
%52 = zext i8 %51 to i32
%53 = shl i32 %52, 8
%54 = and i32 %53, 768
%55 = load i32*, i32** %4
%56 = load i8, i8* %7
%57 = zext i8 %56 to i64
%58 = getelementptr inbounds i32, i32* %55, i64 %57
store i32 %54, i32* %58
%59 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 2
%60 = load i8, i8* %59
%61 = zext i8 %60 to i32
%62 = and i32 %61, 255
%63 = load i32*, i32** %4
%64 = load i8, i8* %7
%65 = zext i8 %64 to i64
%66 = getelementptr inbounds i32, i32* %63, i64 %65
%67 = load i32, i32* %66
%68 = or i32 %67, %62
store i32 %68, i32* %66
br label %69
69: ; preds = %45
%70 = load i8, i8* %7
%71 = add i8 %70, 1
store i8 %71, i8* %7
br label %14
72: ; preds = %14
%73 = load i32, i32* @CLOCK_REALTIME
%74 = load %struct.timespec*, %struct.timespec** %6
%75 = call i32 @clock_gettime(i32 %73, %struct.timespec* %74)
%76 = load i32, i32* %13
ret i32 %76
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@CLOCK_REALTIME = global i32 0
define i32 @get_mcp3008_channels(i32* nocapture writeonly %0, i32 %1, %struct.timespec* %2) {
%4 = alloca [3 x i8]
%5 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
%6 = icmp sgt i32 %1, 0
br i1 %6, label %7, label %35
7: ; preds = %3
%8 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 1
%9 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 2
br label %10
10: ; preds = %7, %22
%11 = phi i32 [ 0, %7 ], [ %21, %22 ]
%12 = phi i8 [ 0, %7 ], [ %32, %22 ]
%13 = shl i8 %12, 4
%14 = or i8 %13, -128
br label %15
15: ; preds = %10, %15
%16 = phi i32 [ %11, %10 ], [ %21, %15 ]
store i8 1, i8* %5
store i8 %14, i8* %8
store i8 0, i8* %9
%17 = call i64 @spiWriteRead(i8* %5, i32 3)
%18 = icmp sgt i64 %17, 0
%19 = xor i1 %18, true
%20 = zext i1 %19 to i32
%21 = add nsw i32 %16, %20
br i1 %18, label %22, label %15
22: ; preds = %15
%23 = zext i8 %12 to i64
%24 = getelementptr inbounds i32, i32* %0, i64 %23
%25 = load i8, i8* %8
%26 = zext i8 %25 to i32
%27 = shl nuw nsw i32 %26, 8
%28 = and i32 %27, 768
%29 = load i8, i8* %9
%30 = zext i8 %29 to i32
%31 = or i32 %28, %30
store i32 %31, i32* %24
%32 = add i8 %12, 1
%33 = zext i8 %32 to i32
%34 = icmp slt i32 %33, %1
br i1 %34, label %10, label %35
35: ; preds = %22, %3
%36 = phi i32 [ 0, %3 ], [ %21, %22 ]
%37 = load i32, i32* @CLOCK_REALTIME
%38 = call i32 @clock_gettime(i32 %37, %struct.timespec* %2)
ret i32 %36
}