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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int g_read(int pino) {
  int arquivo;
  char caminho[33];
  char retorno[3];
  snprintf(caminho, 33, "/sys/class/gpio/gpio%d/value", pino);
  arquivo = open(caminho, O_RDONLY);
  if (arquivo == -1)
    return -1;
  if (read(arquivo, retorno, 3) == -1) {
    close(arquivo);
    return -2;
  }
  close(arquivo);
  return ((int)retorno[0] - 48);
}


x86 assembly (clang, -O0):
<g_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x21,%esi
lea    0xea2(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    118e <g_read+0x4e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ce <g_read+0x8e>
mov    -0xc(%rbp),%edi
lea    -0x33(%rbp),%rsi
mov    $0x3,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
jne    11bc <g_read+0x7c>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11ce <g_read+0x8e>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
movsbl -0x33(%rbp),%eax
sub    $0x30,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<g_read>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
mov    %edi,%ecx
lea    0xeb1(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x21,%esi
call   1030 <snprintf@plt>
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11a2 <g_read+0x62>
mov    %eax,%ebx
lea    0xd(%rsp),%rsi
mov    %eax,%edi
mov    $0x3,%edx
call   1050 <read@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%ebp
je     11a9 <g_read+0x69>
movsbl 0xd(%rsp),%eax
add    $0xffffffd0,%eax
jmp    11ae <g_read+0x6e>
mov    $0xffffffff,%eax
jmp    11ae <g_read+0x6e>
mov    $0xfffffffe,%eax
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_RDONLY = global i32 0
define i32 @g_read(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [33 x i8]
%6 = alloca [3 x i8]
store i32 %0, i32* %3
%7 = getelementptr inbounds [33 x i8], [33 x i8]* %5, i64 0, i64 0
%8 = load i32, i32* %3
%9 = call i32 @snprintf(i8* %7, i32 33, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [33 x i8], [33 x i8]* %5, i64 0, i64 0
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %1
store i32 -1, i32* %2
br label %31
16: ; preds = %1
%17 = load i32, i32* %4
%18 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 0
%19 = call i32 @read(i32 %17, i8* %18, i32 3)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = load i32, i32* %4
%23 = call i32 @close(i32 %22)
store i32 -2, i32* %2
br label %31
24: ; preds = %16
%25 = load i32, i32* %4
%26 = call i32 @close(i32 %25)
%27 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 0
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = sub nsw i32 %29, 48
store i32 %30, i32* %2
br label %31
31: ; preds = %24, %21, %15
%32 = load i32, i32* %2
ret i32 %32
}

LLVM IR (-O2):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_RDONLY = global i32 0
define i32 @g_read(i32 %0) {
%2 = alloca [33 x i8]
%3 = alloca [3 x i8]
%4 = getelementptr inbounds [33 x i8], [33 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%6 = call i32 @snprintf(i8* %4, i32 33, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %4, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %18, label %10
10: ; preds = %1
%11 = call i32 @read(i32 %8, i8* %5, i32 3)
%12 = icmp eq i32 %11, -1
%13 = call i32 @close(i32 %8)
br i1 %12, label %18, label %14
14: ; preds = %10
%15 = load i8, i8* %5
%16 = sext i8 %15 to i32
%17 = add nsw i32 %16, -48
br label %18
18: ; preds = %10, %1, %14
%19 = phi i32 [ %17, %14 ], [ -1, %1 ], [ -2, %10 ]
ret i32 %19
}