#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*/ CHECK(int, int, int, int, int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *, ...);
int spi_close(int, int);
int spi_open(int, int, int, int /*<<< orphan*/);
int spi_xfer(int, int, char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *);
int /*<<< orphan*/ time_sleep(double);
void tc(int pi) {
  int h, x, b, e;
  char buf[128];
  printf("SPI tests.");
  h = spi_open(pi, 1, 50000, 0);
  CHECK(12, 1, h, 0, 0, "spi open");
  for (x = 0; x < 5; x++) {
    sprintf(buf, "\x01\x80");
    b = spi_xfer(pi, h, buf, buf, 3);
    CHECK(12, 2, b, 3, 0, "spi xfer");
    if (b == 3) {
      time_sleep(1.0);
      printf("%d ", ((buf[1] & 0x0F) * 256) | buf[2]);
    }
  }
  e = spi_close(pi, h);
  CHECK(12, 99, e, 0, 0, "spi close");
}


x86 assembly (clang, -O0):
<tc>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %edi,-0x4(%rbp)
lea    0xe83(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
mov    $0x1,%esi
mov    $0xc350,%edx
xor    %ecx,%ecx
call   1060 <spi_open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edx
mov    $0xc,%edi
mov    $0x1,%esi
xor    %r8d,%r8d
lea    0xe59(%rip),%r9
mov    %r8d,%ecx
call   1030 <CHECK@plt>
movl   $0x0,-0xc(%rbp)
cmpl   $0x5,-0xc(%rbp)
jge    1277 <tc+0x107>
lea    -0xa0(%rbp),%rdi
lea    0xe3b(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
lea    -0xa0(%rbp),%rdx
lea    -0xa0(%rbp),%rcx
mov    $0x3,%r8d
call   1050 <spi_xfer@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edx
mov    $0xc,%edi
mov    $0x2,%esi
mov    $0x3,%ecx
xor    %r8d,%r8d
lea    0xdfb(%rip),%r9
call   1030 <CHECK@plt>
cmpl   $0x3,-0x10(%rbp)
jne    1264 <tc+0xf4>
movsd  0xdc5(%rip),%xmm0
call   1090 <time_sleep@plt>
movsbl -0x9f(%rbp),%esi
and    $0xf,%esi
shl    $0x8,%esi
movsbl -0x9e(%rbp),%eax
or     %eax,%esi
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1269 <tc+0xf9>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11c9 <tc+0x59>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1080 <spi_close@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edx
mov    $0xc,%edi
mov    $0x63,%esi
xor    %r8d,%r8d
lea    0xd90(%rip),%r9
mov    %r8d,%ecx
call   1030 <CHECK@plt>
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    %edi,%r14d
lea    0xe8d(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14d,%edi
mov    $0x1,%esi
mov    $0xc350,%edx
xor    %ecx,%ecx
call   1060 <spi_open@plt>
mov    %eax,%r13d
lea    0xe73(%rip),%r9
mov    $0xc,%edi
mov    $0x1,%esi
mov    %eax,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1030 <CHECK@plt>
mov    $0x5,%r12d
mov    %rsp,%rbx
lea    0xe59(%rip),%r15
jmp    11d6 <tc+0x76>
nopl   0x0(%rax,%rax,1)
add    $0xffffffff,%r12d
je     124a <tc+0xea>
movb   $0x0,0x2(%rsp)
movw   $0x8001,(%rsp)
mov    %r14d,%edi
mov    %r13d,%esi
mov    %rbx,%rdx
mov    %rbx,%rcx
mov    $0x3,%r8d
call   1050 <spi_xfer@plt>
mov    %eax,%ebp
mov    $0xc,%edi
mov    $0x2,%esi
mov    %eax,%edx
mov    $0x3,%ecx
xor    %r8d,%r8d
mov    %r15,%r9
call   1030 <CHECK@plt>
cmp    $0x3,%ebp
jne    11d0 <tc+0x70>
movsd  0xddd(%rip),%xmm0
call   1080 <time_sleep@plt>
movzbl 0x1(%rsp),%eax
and    $0xf,%eax
shl    $0x8,%eax
movsbl 0x2(%rsp),%esi
or     %eax,%esi
lea    0xde7(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11d0 <tc+0x70>
mov    %r14d,%edi
mov    %r13d,%esi
call   1070 <spi_close@plt>
lea    0xdd0(%rip),%r9
mov    $0xc,%edi
mov    $0x63,%esi
mov    %eax,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1030 <CHECK@plt>
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"SPI tests.\00"
@.str.1 = [9 x i8] c"spi open\00"
@.str.2 = [3 x i8] c"\01\80\00"
@.str.3 = [9 x i8] c"spi xfer\00"
@.str.4 = [4 x i8] c"%d \00"
@.str.5 = [10 x i8] c"spi close\00"
define void @tc(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [128 x i8]
store i32 %0, i32* %2
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* %2
%10 = call i32 @spi_open(i32 %9, i32 1, i32 50000, i32 0)
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = call i32 @CHECK(i32 12, i32 1, i32 %11, i32 0, i32 0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %4
br label %13
13: ; preds = %41, %1
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 5
br i1 %15, label %16, label %44
16: ; preds = %13
%17 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%18 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%19 = load i32, i32* %2
%20 = load i32, i32* %3
%21 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%22 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%23 = call i32 @spi_xfer(i32 %19, i32 %20, i8* %21, i8* %22, i32 3)
store i32 %23, i32* %5
%24 = load i32, i32* %5
%25 = call i32 @CHECK(i32 12, i32 2, i32 %24, i32 3, i32 0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%26 = load i32, i32* %5
%27 = icmp eq i32 %26, 3
br i1 %27, label %28, label %40
28: ; preds = %16
%29 = call i32 @time_sleep(double 1.000000e+00)
%30 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 1
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = and i32 %32, 15
%34 = mul nsw i32 %33, 256
%35 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 2
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = or i32 %34, %37
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %38)
br label %40
40: ; preds = %28, %16
br label %41
41: ; preds = %40
%42 = load i32, i32* %4
%43 = add nsw i32 %42, 1
store i32 %43, i32* %4
br label %13
44: ; preds = %13
%45 = load i32, i32* %2
%46 = load i32, i32* %3
%47 = call i32 @spi_close(i32 %45, i32 %46)
store i32 %47, i32* %6
%48 = load i32, i32* %6
%49 = call i32 @CHECK(i32 12, i32 99, i32 %48, i32 0, i32 0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"SPI tests.\00"
@.str.1 = [9 x i8] c"spi open\00"
@.str.2 = [3 x i8] c"\01\80\00"
@.str.3 = [9 x i8] c"spi xfer\00"
@.str.4 = [4 x i8] c"%d \00"
@.str.5 = [10 x i8] c"spi close\00"
define void @tc(i32 %0) {
%2 = alloca [128 x i8]
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @spi_open(i32 %0, i32 1, i32 50000, i32 0)
%6 = tail call i32 @CHECK(i32 12, i32 1, i32 %5, i32 0, i32 0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 1
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 2
br label %9
9: ; preds = %1, %24
%10 = phi i32 [ 0, %1 ], [ %25, %24 ]
%11 = call i32 @spi_xfer(i32 %0, i32 %5, i8* %3, i8* %3, i32 3)
%12 = call i32 @CHECK(i32 12, i32 2, i32 %11, i32 3, i32 0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%13 = icmp eq i32 %11, 3
br i1 %13, label %14, label %24
14: ; preds = %9
%15 = call i32 @time_sleep(double 1.000000e+00)
%16 = load i8, i8* %7
%17 = and i8 %16, 15
%18 = zext i8 %17 to i32
%19 = shl nuw nsw i32 %18, 8
%20 = load i8, i8* %8
%21 = sext i8 %20 to i32
%22 = or i32 %19, %21
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %22)
br label %24
24: ; preds = %9, %14
%25 = add nuw nsw i32 %10, 1
%26 = icmp eq i32 %25, 5
br i1 %26, label %27, label %9
27: ; preds = %24
%28 = call i32 @spi_close(i32 %0, i32 %5)
%29 = call i32 @CHECK(i32 12, i32 99, i32 %28, i32 0, i32 0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
ret void
}