#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 */
typedef int uint8_t;

/* Variables and functions */
scalar_t__ GPIO_read_MISO();
int /*<<< orphan*/ GPIO_write_MOSI(int);
int /*<<< orphan*/ GPIO_write_SCLK(int);
int /*<<< orphan*/ s_SPI_delay(int);
uint8_t SPI_transfer_byte(uint8_t byte_out) {
  uint8_t byte_in;
  uint8_t bit;
  for (bit = 0; bit < 8; bit++) {
    if (byte_out & 0x80) {
      (GPIO_write_MOSI(1));
    } else {
      (GPIO_write_MOSI(0));
    }
    byte_out <<= 1;
    s_SPI_delay(1);
    (GPIO_write_SCLK(1));
    byte_in <<= 1;
    if (GPIO_read_MISO()) {
      byte_in |= 0x01;
    }
    s_SPI_delay(1);
    (GPIO_write_SCLK(0));
  }
  s_SPI_delay(1);
  return byte_in;
}


x86 assembly (clang, -O0):
<SPI_transfer_byte>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x8,-0xc(%rbp)
jge    11e2 <SPI_transfer_byte+0xa2>
mov    -0x4(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
je     117c <SPI_transfer_byte+0x3c>
mov    $0x1,%edi
call   1040 <GPIO_write_MOSI@plt>
jmp    1183 <SPI_transfer_byte+0x43>
xor    %edi,%edi
call   1040 <GPIO_write_MOSI@plt>
mov    -0x4(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    $0x1,%edi
call   1060 <s_SPI_delay@plt>
mov    $0x1,%edi
call   1030 <GPIO_write_SCLK@plt>
mov    -0x8(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1050 <GPIO_read_MISO@plt>
cmp    $0x0,%rax
je     11c3 <SPI_transfer_byte+0x83>
mov    -0x8(%rbp),%eax
or     $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    $0x1,%edi
call   1060 <s_SPI_delay@plt>
xor    %edi,%edi
call   1030 <GPIO_write_SCLK@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1152 <SPI_transfer_byte+0x12>
mov    $0x1,%edi
call   1060 <s_SPI_delay@plt>
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SPI_transfer_byte>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebp
mov    $0x8,%ebx
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
shr    $0x7,%edi
and    $0x1,%edi
call   1040 <GPIO_write_MOSI@plt>
add    %ebp,%ebp
mov    $0x1,%edi
call   1060 <s_SPI_delay@plt>
mov    $0x1,%edi
call   1030 <GPIO_write_SCLK@plt>
xor    %eax,%eax
call   1050 <GPIO_read_MISO@plt>
xor    %ecx,%ecx
test   %rax,%rax
setne  %cl
lea    (%rcx,%r14,2),%r14d
mov    $0x1,%edi
call   1060 <s_SPI_delay@plt>
xor    %edi,%edi
call   1030 <GPIO_write_SCLK@plt>
add    $0xffffffff,%ebx
jne    1150 <SPI_transfer_byte+0x10>
mov    $0x1,%edi
call   1060 <s_SPI_delay@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @SPI_transfer_byte(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %4
br label %5
5: ; preds = %31, %1
%6 = load i32, i32* %4
%7 = icmp slt i32 %6, 8
br i1 %7, label %8, label %34
8: ; preds = %5
%9 = load i32, i32* %2
%10 = and i32 %9, 128
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = call i32 @GPIO_write_MOSI(i32 1)
br label %16
14: ; preds = %8
%15 = call i32 @GPIO_write_MOSI(i32 0)
br label %16
16: ; preds = %14, %12
%17 = load i32, i32* %2
%18 = shl i32 %17, 1
store i32 %18, i32* %2
%19 = call i32 @s_SPI_delay(i32 1)
%20 = call i32 @GPIO_write_SCLK(i32 1)
%21 = load i32, i32* %3
%22 = shl i32 %21, 1
store i32 %22, i32* %3
%23 = call i64 (...) @GPIO_read_MISO()
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %16
%26 = load i32, i32* %3
%27 = or i32 %26, 1
store i32 %27, i32* %3
br label %28
28: ; preds = %25, %16
%29 = call i32 @s_SPI_delay(i32 1)
%30 = call i32 @GPIO_write_SCLK(i32 0)
br label %31
31: ; preds = %28
%32 = load i32, i32* %4
%33 = add nsw i32 %32, 1
store i32 %33, i32* %4
br label %5
34: ; preds = %5
%35 = call i32 @s_SPI_delay(i32 1)
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
define i32 @SPI_transfer_byte(i32 %0) {
br label %2
2: ; preds = %1, %2
%3 = phi i32 [ 0, %1 ], [ %19, %2 ]
%4 = phi i32 [ undef, %1 ], [ %16, %2 ]
%5 = phi i32 [ %0, %1 ], [ %9, %2 ]
%6 = lshr i32 %5, 7
%7 = and i32 %6, 1
%8 = tail call i32 @GPIO_write_MOSI(i32 %7)
%9 = shl i32 %5, 1
%10 = tail call i32 @s_SPI_delay(i32 1)
%11 = tail call i32 @GPIO_write_SCLK(i32 1)
%12 = shl i32 %4, 1
%13 = tail call i64 (...) @GPIO_read_MISO()
%14 = icmp ne i64 %13, 0
%15 = zext i1 %14 to i32
%16 = or i32 %12, %15
%17 = tail call i32 @s_SPI_delay(i32 1)
%18 = tail call i32 @GPIO_write_SCLK(i32 0)
%19 = add nuw nsw i32 %3, 1
%20 = icmp eq i32 %19, 8
br i1 %20, label %21, label %2
21: ; preds = %2
%22 = tail call i32 @s_SPI_delay(i32 1)
ret i32 %16
}