#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 */
scalar_t__ gpio_read(int);
int /*<<< orphan*/ gpio_write_high(int);
int /*<<< orphan*/ gpio_write_low(int);
int /*<<< orphan*/ spi_bitbang_delay();
unsigned char spi_bitbang_xfer(int sck_fd, int mosi_fd, int miso_fd,
                               unsigned char tx) {
  unsigned char rx = 0;
  int i = 7;
  gpio_write_low(sck_fd);
  while (i >= 0) {
    if (tx & (((unsigned int)1) << i))
      gpio_write_high(mosi_fd);
    else
      gpio_write_low(mosi_fd);
    spi_bitbang_delay();
    spi_bitbang_delay();
    gpio_write_high(sck_fd);
    spi_bitbang_delay();
    rx |= ((((unsigned char)gpio_read(miso_fd)) & (unsigned char)0x01) << i);
    spi_bitbang_delay();
    gpio_write_low(sck_fd);
    i--;
  }
  return rx;
}


x86 assembly (clang, -O0):
<spi_bitbang_xfer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %cl,%al
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %al,-0xd(%rbp)
movb   $0x0,-0xe(%rbp)
movl   $0x7,-0x14(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <gpio_write_low@plt>
cmpl   $0x0,-0x14(%rbp)
jl     11fb <spi_bitbang_xfer+0xbb>
movzbl -0xd(%rbp),%eax
mov    -0x14(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     119b <spi_bitbang_xfer+0x5b>
mov    -0x8(%rbp),%edi
call   1030 <gpio_write_high@plt>
jmp    11a3 <spi_bitbang_xfer+0x63>
mov    -0x8(%rbp),%edi
call   1040 <gpio_write_low@plt>
mov    $0x0,%al
call   1050 <spi_bitbang_delay@plt>
mov    $0x0,%al
call   1050 <spi_bitbang_delay@plt>
mov    -0x4(%rbp),%edi
call   1030 <gpio_write_high@plt>
mov    $0x0,%al
call   1050 <spi_bitbang_delay@plt>
mov    -0xc(%rbp),%edi
call   1060 <gpio_read@plt>
movzbl %al,%eax
and    $0x1,%eax
mov    -0x14(%rbp),%ecx
shl    %cl,%eax
mov    %eax,%ecx
movzbl -0xe(%rbp),%eax
or     %ecx,%eax
mov    %al,-0xe(%rbp)
mov    $0x0,%al
call   1050 <spi_bitbang_delay@plt>
mov    -0x4(%rbp),%edi
call   1040 <gpio_write_low@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1169 <spi_bitbang_xfer+0x29>
movzbl -0xe(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spi_bitbang_xfer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %esi,%r12d
mov    %edi,%r13d
call   1040 <gpio_write_low@plt>
mov    $0x7,%ebx
xor    %ebp,%ebp
jmp    11b8 <spi_bitbang_xfer+0x78>
cs nopw 0x0(%rax,%rax,1)
nop
call   1040 <gpio_write_low@plt>
xor    %eax,%eax
call   1050 <spi_bitbang_delay@plt>
xor    %eax,%eax
call   1050 <spi_bitbang_delay@plt>
mov    %r13d,%edi
call   1030 <gpio_write_high@plt>
xor    %eax,%eax
call   1050 <spi_bitbang_delay@plt>
mov    %r15d,%edi
call   1060 <gpio_read@plt>
and    $0x1,%eax
mov    %ebx,%ecx
shl    %cl,%eax
or     %al,%bpl
xor    %eax,%eax
call   1050 <spi_bitbang_delay@plt>
mov    %r13d,%edi
call   1040 <gpio_write_low@plt>
add    $0xffffffff,%ebx
jae    11c8 <spi_bitbang_xfer+0x88>
mov    %r12d,%edi
bt     %ebx,%r14d
jae    1170 <spi_bitbang_xfer+0x30>
call   1030 <gpio_write_high@plt>
jmp    1175 <spi_bitbang_xfer+0x35>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define zeroext i8 @spi_bitbang_xfer(i32 %0, i32 %1, i32 %2, i8 zeroext %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
%9 = alloca i8
%10 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8 %3, i8* %8
store i8 0, i8* %9
store i32 7, i32* %10
%11 = load i32, i32* %5
%12 = call i32 @gpio_write_low(i32 %11)
br label %13
13: ; preds = %29, %4
%14 = load i32, i32* %10
%15 = icmp sge i32 %14, 0
br i1 %15, label %16, label %51
16: ; preds = %13
%17 = load i8, i8* %8
%18 = zext i8 %17 to i32
%19 = load i32, i32* %10
%20 = shl i32 1, %19
%21 = and i32 %18, %20
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = load i32, i32* %6
%25 = call i32 @gpio_write_high(i32 %24)
br label %29
26: ; preds = %16
%27 = load i32, i32* %6
%28 = call i32 @gpio_write_low(i32 %27)
br label %29
29: ; preds = %26, %23
%30 = call i32 (...) @spi_bitbang_delay()
%31 = call i32 (...) @spi_bitbang_delay()
%32 = load i32, i32* %5
%33 = call i32 @gpio_write_high(i32 %32)
%34 = call i32 (...) @spi_bitbang_delay()
%35 = load i32, i32* %7
%36 = call i64 @gpio_read(i32 %35)
%37 = trunc i64 %36 to i8
%38 = zext i8 %37 to i32
%39 = and i32 %38, 1
%40 = load i32, i32* %10
%41 = shl i32 %39, %40
%42 = load i8, i8* %9
%43 = zext i8 %42 to i32
%44 = or i32 %43, %41
%45 = trunc i32 %44 to i8
store i8 %45, i8* %9
%46 = call i32 (...) @spi_bitbang_delay()
%47 = load i32, i32* %5
%48 = call i32 @gpio_write_low(i32 %47)
%49 = load i32, i32* %10
%50 = add nsw i32 %49, -1
store i32 %50, i32* %10
br label %13
51: ; preds = %13
%52 = load i8, i8* %9
ret i8 %52
}

LLVM IR (-O2):
define zeroext i8 @spi_bitbang_xfer(i32 %0, i32 %1, i32 %2, i8 zeroext %3) {
%5 = tail call i32 @gpio_write_low(i32 %0)
%6 = zext i8 %3 to i32
br label %7
7: ; preds = %4, %17
%8 = phi i32 [ 7, %4 ], [ %30, %17 ]
%9 = phi i8 [ 0, %4 ], [ %27, %17 ]
%10 = shl nuw i32 1, %8
%11 = and i32 %10, %6
%12 = icmp eq i32 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %7
%14 = tail call i32 @gpio_write_high(i32 %1)
br label %17
15: ; preds = %7
%16 = tail call i32 @gpio_write_low(i32 %1)
br label %17
17: ; preds = %15, %13
%18 = tail call i32 (...) @spi_bitbang_delay()
%19 = tail call i32 (...) @spi_bitbang_delay()
%20 = tail call i32 @gpio_write_high(i32 %0)
%21 = tail call i32 (...) @spi_bitbang_delay()
%22 = tail call i64 @gpio_read(i32 %2)
%23 = trunc i64 %22 to i32
%24 = and i32 %23, 1
%25 = shl nuw i32 %24, %8
%26 = trunc i32 %25 to i8
%27 = or i8 %9, %26
%28 = tail call i32 (...) @spi_bitbang_delay()
%29 = tail call i32 @gpio_write_low(i32 %0)
%30 = add nsw i32 %8, -1
%31 = icmp eq i32 %8, 0
br i1 %31, label %32, label %7
32: ; preds = %17
ret i8 %27
}