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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int gpio_get_val_fd(uint32_t gpio) {
  int ret, fd;
  char f_name[40] = {0};
  snprintf(f_name, sizeof(f_name), "/sys/class/gpio/gpio%d/value", gpio);
  fd = open(f_name, O_RDWR);
  if (fd < 0) {
    printf("%d: ", gpio);
    perror("failed to get gpio value control");
    ret = -1;
  } else
    ret = fd;
  return ret;
}


x86 assembly (clang, -O0):
<gpio_get_val_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
lea    -0x40(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x28,%esi
lea    0xe82(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11c9 <gpio_get_val_fd+0x79>
mov    -0x4(%rbp),%esi
lea    0xe71(%rip),%rdi
call   1030 <printf@plt>
lea    0xe6a(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x8(%rbp)
jmp    11cf <gpio_get_val_fd+0x7f>
mov    -0xc(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_get_val_fd>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebp
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
movq   $0x0,0x20(%rsp)
lea    0xe9c(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x28,%esi
mov    %ebp,%ecx
call   1040 <snprintf@plt>
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <open@plt>
test   %eax,%eax
js     1192 <gpio_get_val_fd+0x52>
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe84(%rip),%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
lea    0xe7b(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    118b <gpio_get_val_fd+0x4b>

LLVM IR (-O0):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"%d: \00"
@.str.2 = [33 x i8] c"failed to get gpio value control\00"
define i32 @gpio_get_val_fd(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [40 x i8]
store i32 %0, i32* %2
%6 = bitcast [40 x i8]* %5 to i8*
%7 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 0
%8 = load i32, i32* %2
%9 = call i32 @snprintf(i8* %7, i32 40, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 0
%11 = load i32, i32* @O_RDWR
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %1
%16 = load i32, i32* %2
%17 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %21
19: ; preds = %1
%20 = load i32, i32* %4
store i32 %20, i32* %3
br label %21
21: ; preds = %19, %15
%22 = load i32, i32* %3
ret i32 %22
}

LLVM IR (-O2):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"%d: \00"
@.str.2 = [33 x i8] c"failed to get gpio value control\00"
define i32 @gpio_get_val_fd(i32 %0) {
%2 = alloca [40 x i8]
%3 = getelementptr inbounds [40 x i8], [40 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 40, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = load i32, i32* @O_RDWR
%6 = call i32 @open(i8* %3, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%10 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %11
11: ; preds = %1, %8
%12 = phi i32 [ -1, %8 ], [ %6, %1 ]
ret i32 %12
}