#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__ spi_comms(int *, int, int *, int);
int read_byte(uint8_t reg, uint8_t *value) {
  uint8_t tx_buf[2] = {0};
  uint8_t rx_buf[2] = {0};
  uint8_t shift_reg = reg | 0x80;
  tx_buf[0] = shift_reg;
  if (spi_comms(tx_buf, 2, rx_buf, 2) != 0) {
    return -1;
  }
  *value = rx_buf[1];
  return 0;
}


x86 assembly (clang, -O0):
<read_byte>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    -0x8(%rbp),%eax
or     $0x80,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x18(%rbp)
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rdx
mov    $0x2,%ecx
mov    %ecx,%esi
call   1040 <spi_comms@plt>
cmp    $0x0,%rax
je     118a <read_byte+0x6a>
movl   $0xffffffff,-0x4(%rbp)
jmp    119a <read_byte+0x7a>
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_byte>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
movq   $0x0,0x8(%rsp)
movq   $0x0,(%rsp)
or     $0x80,%edi
mov    %edi,0x8(%rsp)
lea    0x8(%rsp),%rdi
mov    %rsp,%rdx
mov    $0x2,%esi
mov    $0x2,%ecx
call   1030 <spi_comms@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    115f <read_byte+0x4f>
mov    0x4(%rsp),%eax
mov    %eax,(%rbx)
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define i32 @read_byte(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca [2 x i32]
%7 = alloca [2 x i32]
%8 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
%9 = bitcast [2 x i32]* %6 to i8*
%10 = bitcast [2 x i32]* %7 to i8*
%11 = load i32, i32* %4
%12 = or i32 %11, 128
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
store i32 %13, i32* %14
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%17 = call i64 @spi_comms(i32* %15, i32 2, i32* %16, i32 2)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %2
store i32 -1, i32* %3
br label %24
20: ; preds = %2
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%22 = load i32, i32* %21
%23 = load i32*, i32** %5
store i32 %22, i32* %23
store i32 0, i32* %3
br label %24
24: ; preds = %20, %19
%25 = load i32, i32* %3
ret i32 %25
}

LLVM IR (-O2):
define i32 @read_byte(i32 %0, i32* nocapture writeonly %1) {
%3 = alloca i64
%4 = alloca i64
%5 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%6 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%7 = or i32 %0, 128
%8 = bitcast i64* %3 to i32*
store i32 %7, i32* %8
%9 = bitcast i64* %4 to i32*
%10 = call i64 @spi_comms(i32* %8, i32 2, i32* %9, i32 2)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = bitcast i64* %4 to [2 x i32]*
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %13, i64 0, i64 1
%15 = load i32, i32* %14
store i32 %15, i32* %1
br label %16
16: ; preds = %2, %12
%17 = phi i32 [ 0, %12 ], [ -1, %2 ]
ret i32 %17
}