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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
unsigned int gpioToSysPin(unsigned int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
int gpio_check(unsigned int gpio) {
  unsigned int gpioSys = gpioToSysPin(gpio);
  int fd;
  char filename[23];
  snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d", gpioSys);
  if ((fd = open(filename, O_RDONLY)) < 0)
    return -1;
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<gpio_check>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <gpioToSysPin@plt>
mov    %eax,-0xc(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0x17,%esi
lea    0xe97(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    1198 <gpio_check+0x58>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a7 <gpio_check+0x67>
mov    -0x10(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_check>:
push   %rbx
sub    $0x20,%rsp
call   1050 <gpioToSysPin@plt>
lea    0xeaf(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x17,%esi
mov    %eax,%ecx
call   1030 <snprintf@plt>
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     1183 <gpio_check+0x43>
mov    %eax,%edi
call   1040 <close@plt>
xor    %eax,%eax
jmp    1188 <gpio_check+0x48>
mov    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [23 x i8] c"/sys/class/gpio/gpio%d\00"
@O_RDONLY = global i32 0
define i32 @gpio_check(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [23 x i8]
store i32 %0, i32* %3
%7 = load i32, i32* %3
%8 = call i32 @gpioToSysPin(i32 %7)
store i32 %8, i32* %4
%9 = getelementptr inbounds [23 x i8], [23 x i8]* %6, i64 0, i64 0
%10 = load i32, i32* %4
%11 = call i32 @snprintf(i8* %9, i32 23, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [23 x i8], [23 x i8]* %6, i64 0, i64 0
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %5
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %1
store i32 -1, i32* %2
br label %20
17: ; preds = %1
%18 = load i32, i32* %5
%19 = call i32 @close(i32 %18)
store i32 0, i32* %2
br label %20
20: ; preds = %17, %16
%21 = load i32, i32* %2
ret i32 %21
}

LLVM IR (-O2):
@.str = [23 x i8] c"/sys/class/gpio/gpio%d\00"
@O_RDONLY = global i32 0
define i32 @gpio_check(i32 %0) {
%2 = alloca [23 x i8]
%3 = tail call i32 @gpioToSysPin(i32 %0)
%4 = getelementptr inbounds [23 x i8], [23 x i8]* %2, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 23, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %3)
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* %4, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %1
%10 = call i32 @close(i32 %7)
br label %11
11: ; preds = %1, %9
%12 = phi i32 [ 0, %9 ], [ -1, %1 ]
ret i32 %12
}