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

/* Variables and functions */
int O_NONBLOCK;
int O_RDONLY;
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int snprintf(char *, int, char *, unsigned int, char *);
int gpio_fd_open(unsigned int gpio, char *ident) {
  int fd, len;
  char buf[64];
  len = snprintf(buf, sizeof(buf),
                 "/sys/class/gpio"
                 "/gpio%d_%s/value",
                 gpio, ident);
  fd = open(buf, O_RDONLY | O_NONBLOCK);
  if (fd < 0) {
    perror("gpio/fd_open");
  }
  return fd;
}


x86 assembly (clang, -O0):
<gpio_fd_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    -0x10(%rbp),%r8
mov    $0x40,%esi
lea    0xeaa(%rip),%rdx
call   1030 <snprintf@plt>
mov    %eax,-0x18(%rbp)
lea    -0x60(%rbp),%rdi
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
mov    0x2e76(%rip),%rax
or     (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1192 <gpio_fd_open+0x62>
lea    0xe93(%rip),%rdi
call   1050 <perror@plt>
mov    -0x14(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_fd_open>:
push   %rbx
sub    $0x40,%rsp
mov    %rsi,%r8
mov    %edi,%ecx
lea    0xebf(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
call   1030 <snprintf@plt>
mov    0x2e88(%rip),%rax
mov    0x2e89(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1040 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1179 <gpio_fd_open+0x49>
mov    %ebx,%eax
add    $0x40,%rsp
pop    %rbx
ret
lea    0xea0(%rip),%rdi
call   1050 <perror@plt>
jmp    1171 <gpio_fd_open+0x41>

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

LLVM IR (-O2):
@.str = [32 x i8] c"/sys/class/gpio/gpio%d_%s/value\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [13 x i8] c"gpio/fd_open\00"
define i32 @gpio_fd_open(i32 %0, i8* %1) {
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 64, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %0, i8* %1)
%6 = load i32, i32* @O_RDONLY
%7 = load i32, i32* @O_NONBLOCK
%8 = or i32 %7, %6
%9 = call i32 @open(i8* %4, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
ret i32 %9
}