#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*/ EIO;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ errno;
scalar_t__ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int read(int, char *, int);
int gpio_read(int fd, int *val) {
  char c;
  int n;
  int rc = -1;
  if (lseek(fd, 0, SEEK_SET) < 0)
    goto done;
  n = read(fd, &c, 1);
  if (n < 0)
    goto done;
  if (n == 0) {
    errno = EIO;
    goto done;
  }
  *val = (c == '0' ? 0 : 1);
  rc = 0;
done:
  return rc;
}


x86 assembly (clang, -O0):
<gpio_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e90(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
cmp    $0x0,%rax
jge    1158 <gpio_read+0x38>
jmp    11ba <gpio_read+0x9a>
mov    -0x4(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    117b <gpio_read+0x5b>
jmp    11ba <gpio_read+0x9a>
cmpl   $0x0,-0x18(%rbp)
jne    119c <gpio_read+0x7c>
mov    0x2e54(%rip),%rax
mov    (%rax),%ecx
mov    0x2e43(%rip),%rax
mov    %ecx,(%rax)
jmp    11ba <gpio_read+0x9a>
movsbl -0x11(%rbp),%edx
mov    $0x1,%ecx
xor    %eax,%eax
cmp    $0x30,%edx
cmove  %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_read>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
mov    0x2e9c(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
js     1181 <gpio_read+0x61>
lea    0xf(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
js     1181 <gpio_read+0x61>
je     116f <gpio_read+0x4f>
xor    %eax,%eax
cmpb   $0x30,0xf(%rsp)
setne  %al
mov    %eax,(%r14)
xor    %ebp,%ebp
jmp    1181 <gpio_read+0x61>
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    0x2e59(%rip),%rcx
mov    %eax,(%rcx)
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
@EIO = global i32 0
@errno = global i32 0
define i32 @gpio_read(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i32* %1, i32** %4
store i32 -1, i32* %7
%8 = load i32, i32* %3
%9 = load i32, i32* @SEEK_SET
%10 = call i64 @lseek(i32 %8, i32 0, i32 %9)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %2
br label %31
13: ; preds = %2
%14 = load i32, i32* %3
%15 = call i32 @read(i32 %14, i8* %5, i32 1)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %13
br label %31
19: ; preds = %13
%20 = load i32, i32* %6
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = load i32, i32* @EIO
store i32 %23, i32* @errno
br label %31
24: ; preds = %19
%25 = load i8, i8* %5
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 48
%28 = zext i1 %27 to i64
%29 = select i1 %27, i32 0, i32 1
%30 = load i32*, i32** %4
store i32 %29, i32* %30
store i32 0, i32* %7
br label %31
31: ; preds = %24, %22, %18, %12
%32 = load i32, i32* %7
ret i32 %32
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@EIO = global i32 0
@errno = global i32 0
define i32 @gpio_read(i32 %0, i32* nocapture writeonly %1) {
%3 = alloca i8
%4 = load i32, i32* @SEEK_SET
%5 = tail call i64 @lseek(i32 %0, i32 0, i32 %4)
%6 = icmp slt i64 %5, 0
br i1 %6, label %18, label %7
7: ; preds = %2
%8 = call i32 @read(i32 %0, i8* %3, i32 1)
%9 = icmp slt i32 %8, 0
br i1 %9, label %18, label %10
10: ; preds = %7
%11 = icmp eq i32 %8, 0
br i1 %11, label %12, label %14
12: ; preds = %10
%13 = load i32, i32* @EIO
store i32 %13, i32* @errno
br label %18
14: ; preds = %10
%15 = load i8, i8* %3
%16 = icmp ne i8 %15, 48
%17 = zext i1 %16 to i32
store i32 %17, i32* %1
br label %18
18: ; preds = %7, %2, %14, %12
%19 = phi i32 [ -1, %2 ], [ -1, %7 ], [ -1, %12 ], [ 0, %14 ]
ret i32 %19
}