#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*/ delayMicroseconds(int);
int gpio_get_value(int);
int /*<<< orphan*/ gpio_set_value(int, int);
int shiftIn(int bit_order, int n_bits) {
  int ret = 0;
  int i;
  gpio_set_value(40, 0);
  for (i = 0; i < n_bits; ++i) {
    gpio_set_value(40, 1);
    if (bit_order == 0) {
      ret |= gpio_get_value(38) << i;
    } else {
      ret |= gpio_get_value(38) << n_bits - i;
    }
    delayMicroseconds(20);
    gpio_set_value(40, 0);
  }
  return (ret);
}


x86 assembly (clang, -O0):
<shiftIn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    $0x28,%edi
xor    %esi,%esi
call   1050 <gpio_set_value@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11d3 <shiftIn+0xa3>
mov    $0x28,%edi
mov    $0x1,%esi
call   1050 <gpio_set_value@plt>
cmpl   $0x0,-0x4(%rbp)
jne    1197 <shiftIn+0x67>
mov    $0x26,%edi
call   1040 <gpio_get_value@plt>
mov    -0x10(%rbp),%ecx
shl    %cl,%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    11af <shiftIn+0x7f>
mov    $0x26,%edi
call   1040 <gpio_get_value@plt>
mov    -0x8(%rbp),%ecx
sub    -0x10(%rbp),%ecx
shl    %cl,%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    $0x14,%edi
call   1030 <delayMicroseconds@plt>
mov    $0x28,%edi
xor    %esi,%esi
call   1050 <gpio_set_value@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1158 <shiftIn+0x28>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shiftIn>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%ebx
mov    %edi,%r14d
xor    %r12d,%r12d
mov    $0x28,%edi
xor    %esi,%esi
call   1050 <gpio_set_value@plt>
test   %ebx,%ebx
jle    11a9 <shiftIn+0x79>
mov    %ebx,%r15d
neg    %r15d
xor    %ebp,%ebp
xor    %r12d,%r12d
nopl   0x0(%rax,%rax,1)
mov    $0x28,%edi
mov    $0x1,%esi
call   1050 <gpio_set_value@plt>
mov    $0x26,%edi
call   1040 <gpio_get_value@plt>
test   %r14d,%r14d
mov    %ebx,%ecx
cmove  %ebp,%ecx
shl    %cl,%eax
or     %eax,%r12d
mov    $0x14,%edi
call   1030 <delayMicroseconds@plt>
mov    $0x28,%edi
xor    %esi,%esi
call   1050 <gpio_set_value@plt>
add    $0x1,%ebp
add    $0xffffffff,%ebx
mov    %r15d,%eax
add    %ebp,%eax
jne    1160 <shiftIn+0x30>
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @shiftIn(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %5
%7 = call i32 @gpio_set_value(i32 40, i32 0)
store i32 0, i32* %6
br label %8
8: ; preds = %33, %2
%9 = load i32, i32* %6
%10 = load i32, i32* %4
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %36
12: ; preds = %8
%13 = call i32 @gpio_set_value(i32 40, i32 1)
%14 = load i32, i32* %3
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %12
%17 = call i32 @gpio_get_value(i32 38)
%18 = load i32, i32* %6
%19 = shl i32 %17, %18
%20 = load i32, i32* %5
%21 = or i32 %20, %19
store i32 %21, i32* %5
br label %30
22: ; preds = %12
%23 = call i32 @gpio_get_value(i32 38)
%24 = load i32, i32* %4
%25 = load i32, i32* %6
%26 = sub nsw i32 %24, %25
%27 = shl i32 %23, %26
%28 = load i32, i32* %5
%29 = or i32 %28, %27
store i32 %29, i32* %5
br label %30
30: ; preds = %22, %16
%31 = call i32 @delayMicroseconds(i32 20)
%32 = call i32 @gpio_set_value(i32 40, i32 0)
br label %33
33: ; preds = %30
%34 = load i32, i32* %6
%35 = add nsw i32 %34, 1
store i32 %35, i32* %6
br label %8
36: ; preds = %8
%37 = load i32, i32* %5
ret i32 %37
}

LLVM IR (-O2):
define i32 @shiftIn(i32 %0, i32 %1) {
%3 = tail call i32 @gpio_set_value(i32 40, i32 0)
%4 = icmp sgt i32 %1, 0
br i1 %4, label %5, label %20
5: ; preds = %2
%6 = icmp eq i32 %0, 0
br label %7
7: ; preds = %5, %7
%8 = phi i32 [ 0, %5 ], [ %18, %7 ]
%9 = phi i32 [ 0, %5 ], [ %15, %7 ]
%10 = tail call i32 @gpio_set_value(i32 40, i32 1)
%11 = tail call i32 @gpio_get_value(i32 38)
%12 = sub nsw i32 %1, %8
%13 = select i1 %6, i32 %8, i32 %12
%14 = shl i32 %11, %13
%15 = or i32 %14, %9
%16 = tail call i32 @delayMicroseconds(i32 20)
%17 = tail call i32 @gpio_set_value(i32 40, i32 0)
%18 = add nuw nsw i32 %8, 1
%19 = icmp eq i32 %18, %1
br i1 %19, label %20, label %7
20: ; preds = %7, %2
%21 = phi i32 [ 0, %2 ], [ %15, %7 ]
ret i32 %21
}