#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;
typedef int /*<<< orphan*/ btns;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(int *, int, int, int /*<<< orphan*/ *);
uint8_t pifacedigital_read_reg(uint8_t reg, uint8_t hw_addr) {
  uint8_t btns;
  FILE *fp;
  fp = fopen("buttons.dat", "r");
  if (fp) {
    fread(&btns, sizeof(btns), 1, fp);
    fclose(fp);
    return btns;
  }
  return 0xff;
}


x86 assembly (clang, -O0):
<pifacedigital_read_reg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    0xebb(%rip),%rdi
lea    0xec0(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     118b <pifacedigital_read_reg+0x5b>
mov    -0x18(%rbp),%rcx
lea    -0x10(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1192 <pifacedigital_read_reg+0x62>
movl   $0xff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pifacedigital_read_reg>:
push   %rbx
sub    $0x10,%rsp
lea    0xec4(%rip),%rdi
lea    0xec9(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1175 <pifacedigital_read_reg+0x45>
mov    %rax,%rbx
lea    0xc(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0xc(%rsp),%eax
jmp    117a <pifacedigital_read_reg+0x4a>
mov    $0xff,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [12 x i8] c"buttons.dat\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @pifacedigital_read_reg(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %7
%9 = load i32*, i32** %7
%10 = icmp ne i32* %9, null
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = load i32*, i32** %7
%13 = call i32 @fread(i32* %6, i32 4, i32 1, i32* %12)
%14 = load i32*, i32** %7
%15 = call i32 @fclose(i32* %14)
%16 = load i32, i32* %6
store i32 %16, i32* %3
br label %18
17: ; preds = %2
store i32 255, i32* %3
br label %18
18: ; preds = %17, %11
%19 = load i32, i32* %3
ret i32 %19
}

LLVM IR (-O2):
@.str = [12 x i8] c"buttons.dat\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @pifacedigital_read_reg(i32 %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %11, label %7
7: ; preds = %2
%8 = call i32 @fread(i32* %3, i32 4, i32 1, i32* %5)
%9 = tail call i32 @fclose(i32* %5)
%10 = load i32, i32* %3
br label %11
11: ; preds = %2, %7
%12 = phi i32 [ %10, %7 ], [ 255, %2 ]
ret i32 %12
}