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


x86 assembly (clang, -O0):
<tc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
lea    0xe86(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
mov    $0xc350,%esi
xor    %edx,%edx
call   1060 <spi_open@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%edx
mov    $0xc,%edi
mov    $0x1,%esi
xor    %r8d,%r8d
lea    0xe5f(%rip),%r9
mov    %r8d,%ecx
call   1030 <CHECK@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x5,-0x8(%rbp)
jge    126d <tc+0xfd>
lea    -0x90(%rbp),%rdi
lea    0xe41(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x4(%rbp),%edi
lea    -0x90(%rbp),%rsi
lea    -0x90(%rbp),%rdx
mov    $0x3,%ecx
call   1050 <spi_xfer@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edx
mov    $0xc,%edi
mov    $0x2,%esi
mov    $0x3,%ecx
xor    %r8d,%r8d
lea    0xe05(%rip),%r9
call   1030 <CHECK@plt>
cmpl   $0x3,-0xc(%rbp)
jne    125a <tc+0xea>
movsd  0xdcf(%rip),%xmm0
call   1090 <time_sleep@plt>
movsbl -0x8f(%rbp),%esi
and    $0xf,%esi
shl    $0x8,%esi
movsbl -0x8e(%rbp),%eax
or     %eax,%esi
lea    0xdd5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    125f <tc+0xef>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11c3 <tc+0x53>
mov    -0x4(%rbp),%edi
call   1080 <spi_close@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edx
mov    $0xc,%edi
mov    $0x63,%esi
xor    %r8d,%r8d
lea    0xd9d(%rip),%r9
mov    %r8d,%ecx
call   1030 <CHECK@plt>
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
lea    0xe90(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
mov    $0xc350,%esi
xor    %edx,%edx
call   1060 <spi_open@plt>
mov    %eax,%r12d
lea    0xe79(%rip),%r9
mov    $0xc,%edi
mov    $0x1,%esi
mov    %eax,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1030 <CHECK@plt>
mov    $0x5,%ebx
mov    %rsp,%r13
lea    0xe60(%rip),%r14
lea    0xe62(%rip),%r15
jmp    11d5 <tc+0x75>
nopl   0x0(%rax,%rax,1)
add    $0xffffffff,%ebx
je     1241 <tc+0xe1>
movb   $0x0,0x2(%rsp)
movw   $0x8001,(%rsp)
mov    %r12d,%edi
mov    %r13,%rsi
mov    %r13,%rdx
mov    $0x3,%ecx
call   1050 <spi_xfer@plt>
mov    %eax,%ebp
mov    $0xc,%edi
mov    $0x2,%esi
mov    %eax,%edx
mov    $0x3,%ecx
xor    %r8d,%r8d
mov    %r14,%r9
call   1030 <CHECK@plt>
cmp    $0x3,%ebp
jne    11d0 <tc+0x70>
movsd  0xde2(%rip),%xmm0
call   1080 <time_sleep@plt>
movzbl 0x1(%rsp),%eax
and    $0xf,%eax
shl    $0x8,%eax
movsbl 0x2(%rsp),%esi
or     %eax,%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11d0 <tc+0x70>
mov    %r12d,%edi
call   1070 <spi_close@plt>
lea    0xddc(%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() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [128 x i8]
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @spi_open(i32 1, i32 50000, i32 0)
store i32 %7, i32* %1
%8 = load i32, i32* %1
%9 = call i32 @CHECK(i32 12, i32 1, i32 %8, i32 0, i32 0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %2
br label %10
10: ; preds = %37, %0
%11 = load i32, i32* %2
%12 = icmp slt i32 %11, 5
br i1 %12, label %13, label %40
13: ; preds = %10
%14 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%15 = call i32 @sprintf(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%16 = load i32, i32* %1
%17 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%19 = call i32 @spi_xfer(i32 %16, i8* %17, i8* %18, i32 3)
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = call i32 @CHECK(i32 12, i32 2, i32 %20, i32 3, i32 0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%22 = load i32, i32* %3
%23 = icmp eq i32 %22, 3
br i1 %23, label %24, label %36
24: ; preds = %13
%25 = call i32 @time_sleep(double 1.000000e+00)
%26 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 1
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = and i32 %28, 15
%30 = mul nsw i32 %29, 256
%31 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 2
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = or i32 %30, %33
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %24, %13
br label %37
37: ; preds = %36
%38 = load i32, i32* %2
%39 = add nsw i32 %38, 1
store i32 %39, i32* %2
br label %10
40: ; preds = %10
%41 = load i32, i32* %1
%42 = call i32 @spi_close(i32 %41)
store i32 %42, i32* %4
%43 = load i32, i32* %4
%44 = call i32 @CHECK(i32 12, i32 99, i32 %43, 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() {
%1 = alloca [128 x i8]
%2 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%4 = tail call i32 @spi_open(i32 1, i32 50000, i32 0)
%5 = tail call i32 @CHECK(i32 12, i32 1, i32 %4, i32 0, i32 0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 1
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 2
br label %8
8: ; preds = %0, %23
%9 = phi i32 [ 0, %0 ], [ %24, %23 ]
%10 = call i32 @spi_xfer(i32 %4, i8* %2, i8* %2, i32 3)
%11 = call i32 @CHECK(i32 12, i32 2, i32 %10, i32 3, i32 0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%12 = icmp eq i32 %10, 3
br i1 %12, label %13, label %23
13: ; preds = %8
%14 = call i32 @time_sleep(double 1.000000e+00)
%15 = load i8, i8* %6
%16 = and i8 %15, 15
%17 = zext i8 %16 to i32
%18 = shl nuw nsw i32 %17, 8
%19 = load i8, i8* %7
%20 = sext i8 %19 to i32
%21 = or i32 %18, %20
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %8, %13
%24 = add nuw nsw i32 %9, 1
%25 = icmp eq i32 %24, 5
br i1 %25, label %26, label %8
26: ; preds = %23
%27 = call i32 @spi_close(i32 %4)
%28 = call i32 @CHECK(i32 12, i32 99, i32 %27, i32 0, i32 0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
ret void
}