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

/* Variables and functions */
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ read(int, char **, int);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
scalar_t__ strcmp(char *, char *);
int gpio_get_direction(unsigned int gpio, unsigned int *value) {
  int fd;
  char direction[4];
  char filename[34];
  snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/direction",
           gpio);
  if ((fd = open(filename, O_RDONLY | O_NONBLOCK)) < 0)
    return -1;
  lseek(fd, 0, SEEK_SET);
  read(fd, &direction, 3);
  if (strcmp(direction, "out") == 0) {
    *value = 1;
  } else {
    *value = 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<gpio_get_direction>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x22,%esi
lea    0xe8e(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
mov    0x2e5d(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11aa <gpio_get_direction+0x5a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1208 <gpio_get_direction+0xb8>
mov    -0x14(%rbp),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x3,%edx
call   1050 <read@plt>
lea    -0x18(%rbp),%rdi
lea    0xe48(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    11f7 <gpio_get_direction+0xa7>
mov    -0x10(%rbp),%rax
movl   $0x1,(%rax)
jmp    1201 <gpio_get_direction+0xb1>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_get_direction>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%r14
mov    %edi,%ecx
lea    0xe9a(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x22,%esi
call   1030 <snprintf@plt>
mov    0x2e61(%rip),%rax
mov    0x2e62(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11dc <gpio_get_direction+0x8c>
mov    %eax,%ebx
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
xor    %ebp,%ebp
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
lea    0xc(%rsp),%r15
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x3,%edx
call   1050 <read@plt>
lea    0xe5a(%rip),%rsi
mov    %r15,%rdi
call   1060 <strcmp@plt>
xor    %ecx,%ecx
test   %rax,%rax
sete   %cl
mov    %ecx,(%r14)
jmp    11e1 <gpio_get_direction+0x91>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [4 x i8] c"out\00"
define i32 @gpio_get_direction(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca [4 x i8]
%8 = alloca [34 x i8]
store i32 %0, i32* %4
store i32* %1, i32** %5
%9 = getelementptr inbounds [34 x i8], [34 x i8]* %8, i64 0, i64 0
%10 = load i32, i32* %4
%11 = call i32 @snprintf(i8* %9, i32 34, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [34 x i8], [34 x i8]* %8, 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* %6
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %2
store i32 -1, i32* %3
br label %34
19: ; preds = %2
%20 = load i32, i32* %6
%21 = load i32, i32* @SEEK_SET
%22 = call i32 @lseek(i32 %20, i32 0, i32 %21)
%23 = load i32, i32* %6
%24 = bitcast [4 x i8]* %7 to i8**
%25 = call i32 @read(i32 %23, i8** %24, i32 3)
%26 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%27 = call i64 @strcmp(i8* %26, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %19
%30 = load i32*, i32** %5
store i32 1, i32* %30
br label %33
31: ; preds = %19
%32 = load i32*, i32** %5
store i32 0, i32* %32
br label %33
33: ; preds = %31, %29
store i32 0, i32* %3
br label %34
34: ; preds = %33, %18
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@.str = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [4 x i8] c"out\00"
define i32 @gpio_get_direction(i32 %0, i32* nocapture writeonly %1) {
%3 = alloca [4 x i8]
%4 = alloca [34 x i8]
%5 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [34 x i8], [34 x i8]* %4, i64 0, i64 0
%7 = call i32 @snprintf(i8* %6, i32 34, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %0)
%8 = load i32, i32* @O_RDONLY
%9 = load i32, i32* @O_NONBLOCK
%10 = or i32 %9, %8
%11 = call i32 @open(i8* %6, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %21, label %13
13: ; preds = %2
%14 = load i32, i32* @SEEK_SET
%15 = call i32 @lseek(i32 %11, i32 0, i32 %14)
%16 = bitcast [4 x i8]* %3 to i8**
%17 = call i32 @read(i32 %11, i8** %16, i32 3)
%18 = call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
%20 = zext i1 %19 to i32
store i32 %20, i32* %1
br label %21
21: ; preds = %2, %13
%22 = phi i32 [ 0, %13 ], [ -1, %2 ]
ret i32 %22
}