#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 atoi(char *);
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int gpio_read(int pin) {
  char path[60];
  char value_str[3];
  int fd;
  snprintf(path, 60, "/sys/class/gpio/gpio%d/value", pin);
  fd = open(path, O_RDONLY);
  if (fd < 0) {
    ;
    return -1;
  }
  if (read(fd, value_str, 3) < 0) {
    ;
    return -1;
  }
  close(fd);
  return (atoi(value_str));
}


x86 assembly (clang, -O0):
<gpio_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x3c,%esi
lea    0xe92(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x50(%rbp),%rdi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x58(%rbp)
cmpl   $0x0,-0x58(%rbp)
jge    119e <gpio_read+0x4e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d9 <gpio_read+0x89>
mov    -0x58(%rbp),%edi
lea    -0x53(%rbp),%rsi
mov    $0x3,%edx
call   1050 <read@plt>
cmp    $0x0,%rax
jge    11c5 <gpio_read+0x75>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d9 <gpio_read+0x89>
mov    -0x58(%rbp),%edi
call   1040 <close@plt>
lea    -0x53(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_read>:
push   %rbp
push   %rbx
sub    $0x58,%rsp
mov    %edi,%ecx
lea    0xea1(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x3c,%esi
call   1030 <snprintf@plt>
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     11b6 <gpio_read+0x66>
mov    %eax,%ebp
lea    0xd(%rsp),%rsi
mov    %eax,%edi
mov    $0x3,%edx
call   1050 <read@plt>
test   %rax,%rax
js     11b6 <gpio_read+0x66>
mov    %ebp,%edi
call   1040 <close@plt>
lea    0xd(%rsp),%rdi
call   1070 <atoi@plt>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x58,%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 @gpio_read(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [60 x i8]
%5 = alloca [3 x i8]
%6 = alloca i32
store i32 %0, i32* %3
%7 = getelementptr inbounds [60 x i8], [60 x i8]* %4, i64 0, i64 0
%8 = load i32, i32* %3
%9 = call i32 @snprintf(i8* %7, i32 60, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [60 x i8], [60 x i8]* %4, i64 0, i64 0
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %1
store i32 -1, i32* %2
br label %27
16: ; preds = %1
%17 = load i32, i32* %6
%18 = getelementptr inbounds [3 x i8], [3 x i8]* %5, i64 0, i64 0
%19 = call i64 @read(i32 %17, i8* %18, i32 3)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %16
store i32 -1, i32* %2
br label %27
22: ; preds = %16
%23 = load i32, i32* %6
%24 = call i32 @close(i32 %23)
%25 = getelementptr inbounds [3 x i8], [3 x i8]* %5, i64 0, i64 0
%26 = call i32 @atoi(i8* %25)
store i32 %26, i32* %2
br label %27
27: ; preds = %22, %21, %15
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_RDONLY = global i32 0
define i32 @gpio_read(i32 %0) {
%2 = alloca [60 x i8]
%3 = alloca [3 x i8]
%4 = getelementptr inbounds [60 x i8], [60 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 60, 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 slt i32 %8, 0
br i1 %9, label %16, label %10
10: ; preds = %1
%11 = call i64 @read(i32 %8, i8* %5, i32 3)
%12 = icmp slt i64 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = call i32 @close(i32 %8)
%15 = call i32 @atoi(i8* %5)
br label %16
16: ; preds = %10, %1, %13
%17 = phi i32 [ %15, %13 ], [ -1, %1 ], [ -1, %10 ]
ret i32 %17
}