#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*/ sclk(int);
int /*<<< orphan*/ scmd(char);
int /*<<< orphan*/ sdata(char);
int /*<<< orphan*/ spi_delay();
void spi_write(char data, char cmd) {
  scmd(cmd);
  sclk(0);
  int i;
  for (i = 7; i >= 0; i--) {
    sdata((data >> i) & 1);
    sclk(1);
    spi_delay();
    sclk(0);
  }
}


x86 assembly (clang, -O0):
<spi_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %sil,%al
mov    %dil,%cl
mov    %cl,-0x1(%rbp)
mov    %al,-0x2(%rbp)
movsbl -0x2(%rbp),%edi
call   1040 <scmd@plt>
xor    %edi,%edi
call   1030 <sclk@plt>
movl   $0x7,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     11af <spi_write+0x6f>
movsbl -0x1(%rbp),%eax
mov    -0x8(%rbp),%ecx
sar    %cl,%eax
and    $0x1,%eax
movsbl %al,%edi
call   1060 <sdata@plt>
mov    $0x1,%edi
call   1030 <sclk@plt>
mov    $0x0,%al
call   1050 <spi_delay@plt>
xor    %edi,%edi
call   1030 <sclk@plt>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
jmp    116b <spi_write+0x2b>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spi_write>:
push   %rbx
mov    %edi,%ebx
mov    %esi,%edi
call   1040 <scmd@plt>
xor    %edi,%edi
call   1030 <sclk@plt>
mov    %ebx,%eax
shr    $0x7,%al
movzbl %al,%edi
call   1060 <sdata@plt>
mov    $0x1,%edi
call   1030 <sclk@plt>
xor    %eax,%eax
call   1050 <spi_delay@plt>
xor    %edi,%edi
call   1030 <sclk@plt>
mov    %ebx,%eax
shr    $0x6,%al
and    $0x1,%al
movzbl %al,%edi
call   1060 <sdata@plt>
mov    $0x1,%edi
call   1030 <sclk@plt>
xor    %eax,%eax
call   1050 <spi_delay@plt>
xor    %edi,%edi
call   1030 <sclk@plt>
mov    %ebx,%eax
shr    $0x5,%al
and    $0x1,%al
movzbl %al,%edi
call   1060 <sdata@plt>
mov    $0x1,%edi
call   1030 <sclk@plt>
xor    %eax,%eax
call   1050 <spi_delay@plt>
xor    %edi,%edi
call   1030 <sclk@plt>
mov    %ebx,%eax
shr    $0x4,%al
and    $0x1,%al
movzbl %al,%edi
call   1060 <sdata@plt>
mov    $0x1,%edi
call   1030 <sclk@plt>
xor    %eax,%eax
call   1050 <spi_delay@plt>
xor    %edi,%edi
call   1030 <sclk@plt>
mov    %ebx,%eax
shr    $0x3,%al
and    $0x1,%al
movzbl %al,%edi
call   1060 <sdata@plt>
mov    $0x1,%edi
call   1030 <sclk@plt>
xor    %eax,%eax
call   1050 <spi_delay@plt>
xor    %edi,%edi
call   1030 <sclk@plt>
mov    %ebx,%eax
shr    $0x2,%al
and    $0x1,%al
movzbl %al,%edi
call   1060 <sdata@plt>
mov    $0x1,%edi
call   1030 <sclk@plt>
xor    %eax,%eax
call   1050 <spi_delay@plt>
xor    %edi,%edi
call   1030 <sclk@plt>
mov    %ebx,%eax
shr    %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <sdata@plt>
mov    $0x1,%edi
call   1030 <sclk@plt>
xor    %eax,%eax
call   1050 <spi_delay@plt>
xor    %edi,%edi
call   1030 <sclk@plt>
and    $0x1,%ebx
mov    %ebx,%edi
call   1060 <sdata@plt>
mov    $0x1,%edi
call   1030 <sclk@plt>
xor    %eax,%eax
call   1050 <spi_delay@plt>
xor    %edi,%edi
pop    %rbx
jmp    1030 <sclk@plt>

LLVM IR (-O0):
define void @spi_write(i8 signext %0, i8 signext %1) {
%3 = alloca i8
%4 = alloca i8
%5 = alloca i32
store i8 %0, i8* %3
store i8 %1, i8* %4
%6 = load i8, i8* %4
%7 = call i32 @scmd(i8 signext %6)
%8 = call i32 @sclk(i32 0)
store i32 7, i32* %5
br label %9
9: ; preds = %23, %2
%10 = load i32, i32* %5
%11 = icmp sge i32 %10, 0
br i1 %11, label %12, label %26
12: ; preds = %9
%13 = load i8, i8* %3
%14 = sext i8 %13 to i32
%15 = load i32, i32* %5
%16 = ashr i32 %14, %15
%17 = and i32 %16, 1
%18 = trunc i32 %17 to i8
%19 = call i32 @sdata(i8 signext %18)
%20 = call i32 @sclk(i32 1)
%21 = call i32 (...) @spi_delay()
%22 = call i32 @sclk(i32 0)
br label %23
23: ; preds = %12
%24 = load i32, i32* %5
%25 = add nsw i32 %24, -1
store i32 %25, i32* %5
br label %9
26: ; preds = %9
ret void
}

LLVM IR (-O2):
define void @spi_write(i8 signext %0, i8 signext %1) {
%3 = tail call i32 @scmd(i8 signext %1)
%4 = tail call i32 @sclk(i32 0)
%5 = lshr i8 %0, 7
%6 = tail call i32 @sdata(i8 signext %5)
%7 = tail call i32 @sclk(i32 1)
%8 = tail call i32 (...) @spi_delay()
%9 = tail call i32 @sclk(i32 0)
%10 = lshr i8 %0, 6
%11 = and i8 %10, 1
%12 = tail call i32 @sdata(i8 signext %11)
%13 = tail call i32 @sclk(i32 1)
%14 = tail call i32 (...) @spi_delay()
%15 = tail call i32 @sclk(i32 0)
%16 = lshr i8 %0, 5
%17 = and i8 %16, 1
%18 = tail call i32 @sdata(i8 signext %17)
%19 = tail call i32 @sclk(i32 1)
%20 = tail call i32 (...) @spi_delay()
%21 = tail call i32 @sclk(i32 0)
%22 = lshr i8 %0, 4
%23 = and i8 %22, 1
%24 = tail call i32 @sdata(i8 signext %23)
%25 = tail call i32 @sclk(i32 1)
%26 = tail call i32 (...) @spi_delay()
%27 = tail call i32 @sclk(i32 0)
%28 = lshr i8 %0, 3
%29 = and i8 %28, 1
%30 = tail call i32 @sdata(i8 signext %29)
%31 = tail call i32 @sclk(i32 1)
%32 = tail call i32 (...) @spi_delay()
%33 = tail call i32 @sclk(i32 0)
%34 = lshr i8 %0, 2
%35 = and i8 %34, 1
%36 = tail call i32 @sdata(i8 signext %35)
%37 = tail call i32 @sclk(i32 1)
%38 = tail call i32 (...) @spi_delay()
%39 = tail call i32 @sclk(i32 0)
%40 = lshr i8 %0, 1
%41 = and i8 %40, 1
%42 = tail call i32 @sdata(i8 signext %41)
%43 = tail call i32 @sclk(i32 1)
%44 = tail call i32 (...) @spi_delay()
%45 = tail call i32 @sclk(i32 0)
%46 = and i8 %0, 1
%47 = tail call i32 @sdata(i8 signext %46)
%48 = tail call i32 @sclk(i32 1)
%49 = tail call i32 (...) @spi_delay()
%50 = tail call i32 @sclk(i32 0)
ret void
}