#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*/ error(char *);
int /*<<< orphan*/ printf(char *, unsigned int, unsigned int, int);
int /*<<< orphan*/ small1_ping(int);
int spi_transfer_w32(unsigned int *, unsigned int *);
unsigned int small1_prog(unsigned int *buffer, unsigned int length,
                         int debugp) {
  small1_ping(0xefab);
  small1_ping(0xefab);
  unsigned int CMD = 99 | ((debugp & 1) << 16);
  unsigned int rbuf = 31;
  int ret = spi_transfer_w32(&CMD, &rbuf);
  if (ret)
    error("Cannot write");
  ret = spi_transfer_w32(&length, &rbuf);
  if (ret)
    error("Cannot write");
  int i;
  for (i = 0; i < length; i++) {
    ret = spi_transfer_w32(&buffer[i], &rbuf);
    if (i > 0 && rbuf != buffer[i - 1])
      printf("Poo poo shit %x vs %x at %x\n", rbuf, buffer[i - 1], i);
    if (ret)
      error("Cannot write");
  }
}


x86 assembly (clang, -O0):
<small1_prog>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    $0xefab,%edi
call   1030 <small1_ping@plt>
mov    $0xefab,%edi
call   1030 <small1_ping@plt>
mov    -0x18(%rbp),%eax
and    $0x1,%eax
shl    $0x10,%eax
or     $0x63,%eax
mov    %eax,-0x1c(%rbp)
movl   $0x1f,-0x20(%rbp)
lea    -0x1c(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <spi_transfer_w32@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11a2 <small1_prog+0x62>
lea    0xe63(%rip),%rdi
call   1050 <error@plt>
lea    -0x14(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <spi_transfer_w32@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11c8 <small1_prog+0x88>
lea    0xe3d(%rip),%rdi
call   1050 <error@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    1264 <small1_prog+0x124>
mov    -0x10(%rbp),%rdi
movslq -0x28(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
lea    -0x20(%rbp),%rsi
call   1060 <spi_transfer_w32@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x28(%rbp)
jle    123b <small1_prog+0xfb>
mov    -0x20(%rbp),%eax
mov    -0x10(%rbp),%rcx
mov    -0x28(%rbp),%edx
sub    $0x1,%edx
movslq %edx,%rdx
cmp    (%rcx,%rdx,4),%eax
je     123b <small1_prog+0xfb>
mov    -0x20(%rbp),%esi
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x28(%rbp),%ecx
lea    0xdd7(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x24(%rbp)
je     1251 <small1_prog+0x111>
lea    0xdb4(%rip),%rdi
call   1050 <error@plt>
jmp    1256 <small1_prog+0x116>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11cf <small1_prog+0x8f>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<small1_prog>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebx
mov    %rdi,%r14
mov    %esi,0x10(%rsp)
mov    $0xefab,%edi
call   1030 <small1_ping@plt>
mov    $0xefab,%edi
call   1030 <small1_ping@plt>
and    $0x1,%ebx
shl    $0x10,%ebx
or     $0x63,%ebx
mov    %ebx,0x14(%rsp)
movl   $0x1f,0xc(%rsp)
lea    0x14(%rsp),%rdi
lea    0xc(%rsp),%rsi
call   1060 <spi_transfer_w32@plt>
test   %eax,%eax
je     119f <small1_prog+0x5f>
lea    0xe66(%rip),%rdi
call   1050 <error@plt>
lea    0x10(%rsp),%rdi
lea    0xc(%rsp),%rsi
call   1060 <spi_transfer_w32@plt>
test   %eax,%eax
je     11be <small1_prog+0x7e>
lea    0xe47(%rip),%rdi
call   1050 <error@plt>
cmpl   $0x0,0x10(%rsp)
je     1252 <small1_prog+0x112>
lea    0xc(%rsp),%rsi
mov    %r14,%rdi
call   1060 <spi_transfer_w32@plt>
test   %eax,%eax
je     11e6 <small1_prog+0xa6>
lea    0xe1f(%rip),%rdi
call   1050 <error@plt>
cmpl   $0x2,0x10(%rsp)
jb     1252 <small1_prog+0x112>
add    $0x4,%r14
mov    $0x1,%ebx
lea    0xc(%rsp),%r15
lea    0xdfe(%rip),%r12
lea    0xe04(%rip),%r13
jmp    1221 <small1_prog+0xe1>
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbx
mov    0x10(%rsp),%eax
add    $0x4,%r14
cmp    %rax,%rbx
jae    1252 <small1_prog+0x112>
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <spi_transfer_w32@plt>
mov    %eax,%ebp
mov    0xc(%rsp),%esi
mov    -0x4(%r14),%edx
cmp    %edx,%esi
je     1244 <small1_prog+0x104>
mov    %r13,%rdi
mov    %ebx,%ecx
call   1040 <printf@plt>
test   %ebp,%ebp
je     1210 <small1_prog+0xd0>
mov    %r12,%rdi
call   1050 <error@plt>
jmp    1210 <small1_prog+0xd0>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"Cannot write\00"
@.str.1 = [29 x i8] c"Poo poo shit %x vs %x at %x\0A\00"
define i32 @small1_prog(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%12 = call i32 @small1_ping(i32 61355)
%13 = call i32 @small1_ping(i32 61355)
%14 = load i32, i32* %7
%15 = and i32 %14, 1
%16 = shl i32 %15, 16
%17 = or i32 99, %16
store i32 %17, i32* %8
store i32 31, i32* %9
%18 = call i32 @spi_transfer_w32(i32* %8, i32* %9)
store i32 %18, i32* %10
%19 = load i32, i32* %10
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %3
%22 = call i32 @error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %21, %3
%24 = call i32 @spi_transfer_w32(i32* %6, i32* %9)
store i32 %24, i32* %10
%25 = load i32, i32* %10
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 @error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %29
29: ; preds = %27, %23
store i32 0, i32* %11
br label %30
30: ; preds = %67, %29
%31 = load i32, i32* %11
%32 = load i32, i32* %6
%33 = icmp ult i32 %31, %32
br i1 %33, label %34, label %70
34: ; preds = %30
%35 = load i32*, i32** %5
%36 = load i32, i32* %11
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i32, i32* %35, i64 %37
%39 = call i32 @spi_transfer_w32(i32* %38, i32* %9)
store i32 %39, i32* %10
%40 = load i32, i32* %11
%41 = icmp sgt i32 %40, 0
br i1 %41, label %42, label %61
42: ; preds = %34
%43 = load i32, i32* %9
%44 = load i32*, i32** %5
%45 = load i32, i32* %11
%46 = sub nsw i32 %45, 1
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i32, i32* %44, i64 %47
%49 = load i32, i32* %48
%50 = icmp ne i32 %43, %49
br i1 %50, label %51, label %61
51: ; preds = %42
%52 = load i32, i32* %9
%53 = load i32*, i32** %5
%54 = load i32, i32* %11
%55 = sub nsw i32 %54, 1
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i32, i32* %53, i64 %56
%58 = load i32, i32* %57
%59 = load i32, i32* %11
%60 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %52, i32 %58, i32 %59)
br label %61
61: ; preds = %51, %42, %34
%62 = load i32, i32* %10
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %61
%65 = call i32 @error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %66
66: ; preds = %64, %61
br label %67
67: ; preds = %66
%68 = load i32, i32* %11
%69 = add nsw i32 %68, 1
store i32 %69, i32* %11
br label %30
70: ; preds = %30
%71 = load i32, i32* %4
ret i32 %71
}

LLVM IR (-O2):
@.str = [13 x i8] c"Cannot write\00"
@.str.1 = [29 x i8] c"Poo poo shit %x vs %x at %x\0A\00"
define i32 @small1_prog(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %1, i32* %4
%7 = tail call i32 @small1_ping(i32 61355)
%8 = tail call i32 @small1_ping(i32 61355)
%9 = bitcast i32* %5 to i8*
%10 = shl i32 %2, 16
%11 = and i32 %10, 65536
%12 = or i32 %11, 99
store i32 %12, i32* %5
%13 = bitcast i32* %6 to i8*
store i32 31, i32* %6
%14 = call i32 @spi_transfer_w32(i32* %5, i32* %6)
%15 = icmp eq i32 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %3
%17 = call i32 @error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %3
%19 = call i32 @spi_transfer_w32(i32* %4, i32* %6)
%20 = icmp eq i32 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = call i32 @error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %21, %18
%24 = load i32, i32* %4
%25 = icmp eq i32 %24, 0
br i1 %25, label %56, label %26
26: ; preds = %23
%27 = call i32 @spi_transfer_w32(i32* %0, i32* %6)
%28 = icmp eq i32 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %26
%30 = call i32 @error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %31
31: ; preds = %29, %26
%32 = load i32, i32* %4
%33 = icmp ugt i32 %32, 1
br i1 %33, label %34, label %56
34: ; preds = %31, %51
%35 = phi i64 [ %52, %51 ], [ 1, %31 ]
%36 = getelementptr inbounds i32, i32* %0, i64 %35
%37 = call i32 @spi_transfer_w32(i32* %36, i32* %6)
%38 = load i32, i32* %6
%39 = add nuw i64 %35, 4294967295
%40 = and i64 %39, 4294967295
%41 = getelementptr inbounds i32, i32* %0, i64 %40
%42 = load i32, i32* %41
%43 = icmp eq i32 %38, %42
br i1 %43, label %47, label %44
44: ; preds = %34
%45 = trunc i64 %35 to i32
%46 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %38, i32 %42, i32 %45)
br label %47
47: ; preds = %44, %34
%48 = icmp eq i32 %37, 0
br i1 %48, label %51, label %49
49: ; preds = %47
%50 = call i32 @error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %51
51: ; preds = %47, %49
%52 = add nuw nsw i64 %35, 1
%53 = load i32, i32* %4
%54 = zext i32 %53 to i64
%55 = icmp ult i64 %52, %54
br i1 %55, label %34, label %56
56: ; preds = %51, %31, %23
ret i32 undef
}