#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);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
int gpio_check(unsigned int gpio) {
  int fd;
  char filename[23];
  snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d", gpio);
  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)
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x17,%esi
lea    0xeb2(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    117d <gpio_check+0x4d>
movl   $0xffffffff,-0x4(%rbp)
jmp    118c <gpio_check+0x5c>
mov    -0xc(%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
mov    %edi,%ecx
lea    0xec2(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x17,%esi
call   1030 <snprintf@plt>
mov    0x2e93(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <open@plt>
test   %eax,%eax
js     116e <gpio_check+0x3e>
mov    %eax,%edi
call   1040 <close@plt>
xor    %eax,%eax
jmp    1173 <gpio_check+0x43>
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 [23 x i8]
store i32 %0, i32* %3
%6 = getelementptr inbounds [23 x i8], [23 x i8]* %5, i64 0, i64 0
%7 = load i32, i32* %3
%8 = call i32 @snprintf(i8* %6, i32 23, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = getelementptr inbounds [23 x i8], [23 x i8]* %5, i64 0, i64 0
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %4
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 -1, i32* %2
br label %17
14: ; preds = %1
%15 = load i32, i32* %4
%16 = call i32 @close(i32 %15)
store i32 0, i32* %2
br label %17
17: ; preds = %14, %13
%18 = load i32, i32* %2
ret i32 %18
}

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 = getelementptr inbounds [23 x i8], [23 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 23, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* %3, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %1
%9 = call i32 @close(i32 %6)
br label %10
10: ; preds = %1, %8
%11 = phi i32 [ 0, %8 ], [ -1, %1 ]
ret i32 %11
}