#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 */
scalar_t__ EINTR;
int /*<<< orphan*/ O_RDONLY;
int close(int);
scalar_t__ errno;
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
scalar_t__ snprintf(char *, int, char *, int);
int get(int gpio) {
  char fn[256];
  int rv = 0;
  if (snprintf(fn, 256, "/sys/class/gpio/gpio%i/value", gpio) < 0) {
    return -1;
  } else {
    int fd = open(fn, O_RDONLY);
    if (fd) {
      char buf[32];
      int r = read(fd, buf, 32);
      if (r < 1) {
        rv = -2;
      } else {
        rv = (buf[0] == '1');
      }
      do {
        r = close(fd);
      } while ((r < 0) && (errno == EINTR));
    }
  }
  return rv;
}


x86 assembly (clang, -O0):
<get>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0x114(%rbp)
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x100,%esi
lea    0xe92(%rip),%rdx
call   1030 <snprintf@plt>
cmp    $0x0,%rax
jge    1189 <get+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    126b <get+0x12b>
lea    -0x110(%rbp),%rdi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x118(%rbp)
cmpl   $0x0,-0x118(%rbp)
je     125d <get+0x11d>
mov    -0x118(%rbp),%edi
lea    -0x140(%rbp),%rsi
mov    $0x20,%edx
call   1050 <read@plt>
mov    %eax,-0x144(%rbp)
cmpl   $0x1,-0x144(%rbp)
jge    11ea <get+0xaa>
movl   $0xfffffffe,-0x114(%rbp)
ff ff ff
jmp    1202 <get+0xc2>
movsbl -0x140(%rbp),%eax
cmp    $0x31,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x114(%rbp)
jmp    1207 <get+0xc7>
mov    -0x118(%rbp),%edi
call   1040 <close@plt>
mov    %eax,-0x144(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x144(%rbp)
mov    %al,-0x145(%rbp)
jge    124a <get+0x10a>
mov    0x2da4(%rip),%rax
mov    (%rax),%rax
mov    0x2d92(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x145(%rbp)
mov    -0x145(%rbp),%al
test   $0x1,%al
jne    1207 <get+0xc7>
jmp    125d <get+0x11d>
jmp    1262 <get+0x122>
mov    -0x114(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x120,%rsp
mov    %edi,%ecx
lea    0xea8(%rip),%rdx
lea    0x20(%rsp),%rdi
mov    $0x100,%esi
call   1030 <snprintf@plt>
test   %rax,%rax
js     11d3 <get+0x93>
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
lea    0x20(%rsp),%rdi
call   1060 <open@plt>
test   %eax,%eax
je     11db <get+0x9b>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x20,%edx
call   1050 <read@plt>
xor    %r15d,%r15d
cmpb   $0x31,(%rsp)
mov    %eax,%r14d
sete   %r15b
mov    0x2e2f(%rip),%r12
mov    0x2e20(%rip),%rbp
mov    %ebx,%edi
call   1040 <close@plt>
test   %eax,%eax
jns    11c5 <get+0x85>
mov    0x0(%rbp),%rax
cmp    %rax,(%r12)
je     11b0 <get+0x70>
test   %r14d,%r14d
mov    $0xfffffffe,%eax
cmovle %eax,%r15d
jmp    11de <get+0x9e>
mov    $0xffffffff,%r15d
jmp    11de <get+0x9e>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x120,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"/sys/class/gpio/gpio%i/value\00"
@O_RDONLY = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @get(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [256 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca [32 x i8]
%8 = alloca i32
store i32 %0, i32* %3
store i32 0, i32* %5
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%10 = load i32, i32* %3
%11 = call i64 @snprintf(i8* %9, i32 256, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 -1, i32* %2
br label %50
14: ; preds = %1
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %47
20: ; preds = %14
%21 = load i32, i32* %6
%22 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%23 = call i32 @read(i32 %21, i8* %22, i32 32)
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = icmp slt i32 %24, 1
br i1 %25, label %26, label %27
26: ; preds = %20
store i32 -2, i32* %5
br label %33
27: ; preds = %20
%28 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 49
%32 = zext i1 %31 to i32
store i32 %32, i32* %5
br label %33
33: ; preds = %27, %26
br label %34
34: ; preds = %44, %33
%35 = load i32, i32* %6
%36 = call i32 @close(i32 %35)
store i32 %36, i32* %8
br label %37
37: ; preds = %34
%38 = load i32, i32* %8
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %37
%41 = load i64, i64* @errno
%42 = load i64, i64* @EINTR
%43 = icmp eq i64 %41, %42
br label %44
44: ; preds = %40, %37
%45 = phi i1 [ false, %37 ], [ %43, %40 ]
br i1 %45, label %34, label %46
46: ; preds = %44
br label %47
47: ; preds = %46, %14
br label %48
48: ; preds = %47
%49 = load i32, i32* %5
store i32 %49, i32* %2
br label %50
50: ; preds = %48, %13
%51 = load i32, i32* %2
ret i32 %51
}

LLVM IR (-O2):
@.str = [29 x i8] c"/sys/class/gpio/gpio%i/value\00"
@O_RDONLY = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @get(i32 %0) {
%2 = alloca [256 x i8]
%3 = alloca [32 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%5 = call i64 @snprintf(i8* %4, i32 256, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = icmp slt i64 %5, 0
br i1 %6, label %27, label %7
7: ; preds = %1
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %4, i32 %8)
%10 = icmp eq i32 %9, 0
br i1 %10, label %27, label %11
11: ; preds = %7
%12 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%13 = call i32 @read(i32 %9, i8* %12, i32 32)
%14 = load i8, i8* %12
%15 = icmp eq i8 %14, 49
%16 = zext i1 %15 to i32
br label %17
17: ; preds = %17, %11
%18 = call i32 @close(i32 %9)
%19 = icmp slt i32 %18, 0
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
%23 = select i1 %19, i1 %22, i1 false
br i1 %23, label %17, label %24
24: ; preds = %17
%25 = icmp slt i32 %13, 1
%26 = select i1 %25, i32 -2, i32 %16
br label %27
27: ; preds = %7, %24, %1
%28 = phi i32 [ -1, %1 ], [ %26, %24 ], [ 0, %7 ]
ret i32 %28
}