#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*/ FILE;

/* Variables and functions */
scalar_t__ EAGAIN;
int EXIT_FAILURE;
scalar_t__ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print_state_change(int, int);
int main(void) {
  FILE *f;
  uint8_t last_state = 0xf0;
  int ret;
  for (;;) {
    uint8_t cur_state;
    f = fopen("/sys/bus/platform/drivers/fpga_uinput/fpga_uinput", "r");
    if (f == NULL) {
      perror("fopen");
      return EXIT_FAILURE;
    }
    ret = fread(&cur_state, 1, 1, f);
    fclose(f);
    if (ret != 1) {
      if (errno == EAGAIN)
        continue;
      return EXIT_FAILURE;
    }
    print_state_change(cur_state, last_state);
    last_state = cur_state;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xf0,-0x14(%rbp)
lea    0xe93(%rip),%rdi
lea    0xebe(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a5 <main+0x55>
lea    0xea5(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1215 <main+0xc5>
mov    -0x10(%rbp),%rcx
lea    -0x1c(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x1,-0x18(%rbp)
je     11ff <main+0xaf>
mov    0x2e0a(%rip),%rax
mov    (%rax),%rax
mov    0x2de8(%rip),%rcx
cmp    (%rcx),%rax
jne    11ee <main+0x9e>
jmp    1166 <main+0x16>
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1215 <main+0xc5>
mov    -0x1c(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1050 <print_state_change@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    1166 <main+0x16>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe9e(%rip),%rdi
lea    0xec9(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1216 <main+0xc6>
mov    %rax,%rbx
mov    $0xf0,%r13d
lea    0x4(%rsp),%r14
lea    0xe74(%rip),%r15
lea    0xe9f(%rip),%r12
jmp    11c4 <main+0x74>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x4(%rsp),%ebx
mov    %ebx,%edi
mov    %r13d,%esi
call   1050 <print_state_change@plt>
mov    %ebx,%r13d
mov    %r15,%rdi
mov    %r12,%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1216 <main+0xc6>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <fclose@plt>
cmp    $0x1,%ebp
je     11a0 <main+0x50>
mov    0x2df1(%rip),%rax
mov    (%rax),%rax
mov    0x2dcf(%rip),%rcx
cmp    (%rcx),%rax
je     11b1 <main+0x61>
mov    0x2dd3(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe17(%rip),%rdi
call   1070 <perror@plt>
jmp    11fe <main+0xae>

LLVM IR (-O0):
@.str = [50 x i8] c"/sys/bus/platform/drivers/fpga_uinput/fpga_uinput\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"fopen\00"
@EXIT_FAILURE = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
store i32 240, i32* %3
br label %6
6: ; preds = %27, %24, %0
%7 = call i32* @fopen(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %2
%8 = load i32*, i32** %2
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %13
10: ; preds = %6
%11 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
store i32 %12, i32* %1
br label %32
13: ; preds = %6
%14 = load i32*, i32** %2
%15 = call i32 @fread(i32* %5, i32 1, i32 1, i32* %14)
store i32 %15, i32* %4
%16 = load i32*, i32** %2
%17 = call i32 @fclose(i32* %16)
%18 = load i32, i32* %4
%19 = icmp ne i32 %18, 1
br i1 %19, label %20, label %27
20: ; preds = %13
%21 = load i64, i64* @errno
%22 = load i64, i64* @EAGAIN
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %25
24: ; preds = %20
br label %6
25: ; preds = %20
%26 = load i32, i32* @EXIT_FAILURE
store i32 %26, i32* %1
br label %32
27: ; preds = %13
%28 = load i32, i32* %5
%29 = load i32, i32* %3
%30 = call i32 @print_state_change(i32 %28, i32 %29)
%31 = load i32, i32* %5
store i32 %31, i32* %3
br label %6
32: ; preds = %25, %10
%33 = load i32, i32* %1
ret i32 %33
}

LLVM IR (-O2):
@.str = [50 x i8] c"/sys/bus/platform/drivers/fpga_uinput/fpga_uinput\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"fopen\00"
@EXIT_FAILURE = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %20, %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %24
7: ; preds = %0, %20
%8 = phi i32* [ %22, %20 ], [ %3, %0 ]
%9 = phi i32 [ %21, %20 ], [ 240, %0 ]
%10 = call i32 @fread(i32* %1, i32 1, i32 1, i32* %8)
%11 = tail call i32 @fclose(i32* %8)
%12 = icmp eq i32 %10, 1
br i1 %12, label %17, label %13
13: ; preds = %7
%14 = load i64, i64* @errno
%15 = load i64, i64* @EAGAIN
%16 = icmp eq i64 %14, %15
br i1 %16, label %20, label %24
17: ; preds = %7
%18 = load i32, i32* %1
%19 = tail call i32 @print_state_change(i32 %18, i32 %9)
br label %20
20: ; preds = %13, %17
%21 = phi i32 [ %18, %17 ], [ %9, %13 ]
%22 = tail call i32* @fopen(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%23 = icmp eq i32* %22, null
br i1 %23, label %5, label %7
24: ; preds = %13, %5
%25 = load i32, i32* @EXIT_FAILURE
ret i32 %25
}