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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ gpio_is_pin_valid(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
int gpio_open(int pin) {
  if (!gpio_is_pin_valid(pin)) {
    fprintf(stderr, "Invalid pin number %d!\n", pin);
    return -1;
  }
  char gpio[64];
  snprintf(gpio, sizeof(gpio), "/sys/class/gpio/gpio%d/value", pin);
  int fd = open(gpio, O_RDONLY);
  if (fd == -1) {
    fprintf(stderr, "Failed opening %s! Error %d.\n", gpio, errno);
  }
  return fd;
}


x86 assembly (clang, -O0):
<gpio_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <gpio_is_pin_valid@plt>
cmp    $0x0,%eax
jne    1182 <gpio_open+0x42>
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe91(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e3 <gpio_open+0xa3>
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x40,%esi
lea    0xe83(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x50(%rbp),%rdi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0xffffffff,-0x54(%rbp)
jne    11dd <gpio_open+0x9d>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
lea    -0x50(%rbp),%rdx
mov    0x2e03(%rip),%rax
mov    (%rax),%ecx
lea    0xe5f(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x54(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_open>:
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %edi,%ebx
call   1050 <gpio_is_pin_valid@plt>
test   %eax,%eax
je     11a8 <gpio_open+0x68>
lea    0xebf(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x40,%esi
mov    %ebx,%ecx
call   1030 <snprintf@plt>
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1060 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11c6 <gpio_open+0x86>
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
mov    0x2e3d(%rip),%rax
mov    (%rax),%ecx
lea    0xe99(%rip),%rsi
mov    %rsp,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    11c6 <gpio_open+0x86>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
lea    0xe48(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [24 x i8] c"Invalid pin number %d!\0A\00"
@.str.1 = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_RDONLY = global i32 0
@.str.2 = [30 x i8] c"Failed opening %s! Error %d.\0A\00"
@errno = global i32 0
define i32 @gpio_open(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [64 x i8]
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = call i32 @gpio_is_pin_valid(i32 %6)
%8 = icmp ne i32 %7, 0
br i1 %8, label %13, label %9
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = load i32, i32* %3
%12 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 -1, i32* %2
br label %29
13: ; preds = %1
%14 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%15 = load i32, i32* %3
%16 = call i32 @snprintf(i8* %14, i32 64, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %27
22: ; preds = %13
%23 = load i32, i32* @stderr
%24 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%25 = load i32, i32* @errno
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %24, i32 %25)
br label %27
27: ; preds = %22, %13
%28 = load i32, i32* %5
store i32 %28, i32* %2
br label %29
29: ; preds = %27, %9
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [24 x i8] c"Invalid pin number %d!\0A\00"
@.str.1 = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_RDONLY = global i32 0
@.str.2 = [30 x i8] c"Failed opening %s! Error %d.\0A\00"
@errno = global i32 0
define i32 @gpio_open(i32 %0) {
%2 = alloca [64 x i8]
%3 = tail call i32 @gpio_is_pin_valid(i32 %0)
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = load i32, i32* @stderr
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %19
8: ; preds = %1
%9 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%10 = call i32 @snprintf(i8* %9, i32 64, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %9, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %18
14: ; preds = %8
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %9, i32 %16)
br label %18
18: ; preds = %14, %8
br label %19
19: ; preds = %18, %5
%20 = phi i32 [ %12, %18 ], [ -1, %5 ]
ret i32 %20
}