#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;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int snprintf(char *, int, char *, unsigned int);
int gpio_fd_open(unsigned int gpio) {
  int fd, len;
  char buf[64];
  len = snprintf(buf, sizeof(buf),
                 "/sys/class/gpio"
                 "/gpio%d/value",
                 gpio);
  FILE *f;
  f = fopen(buf, "r");
  fd = fileno(f);
  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)
lea    -0x50(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x40,%esi
lea    0xea2(%rip),%rdx
call   1030 <snprintf@plt>
mov    %eax,-0xc(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xeac(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rdi
call   1040 <fileno@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    119c <gpio_fd_open+0x5c>
lea    0xe88(%rip),%rdi
call   1060 <perror@plt>
mov    -0x8(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_fd_open>:
push   %rbx
sub    $0x40,%rsp
mov    %edi,%ecx
lea    0xeb2(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
call   1030 <snprintf@plt>
lea    0xeb8(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
mov    %rax,%rdi
call   1040 <fileno@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1183 <gpio_fd_open+0x43>
mov    %ebx,%eax
add    $0x40,%rsp
pop    %rbx
ret
lea    0xe95(%rip),%rdi
call   1060 <perror@plt>
jmp    117b <gpio_fd_open+0x3b>

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

LLVM IR (-O2):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [13 x i8] c"gpio/fd_open\00"
define i32 @gpio_fd_open(i32 %0) {
%2 = alloca [64 x i8]
%3 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 64, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = call i32 @fileno(i32* %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %10
10: ; preds = %8, %1
ret i32 %6
}