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

/* 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] = {0};
  char filename[40];
  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, sizeof(direction) - 1);
  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    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
lea    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x28,%esi
lea    0xe6e(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
mov    0x2e3d(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11ca <gpio_get_direction+0x6a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1228 <gpio_get_direction+0xc8>
mov    -0x14(%rbp),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x3,%edx
call   1060 <read@plt>
lea    -0x18(%rbp),%rdi
lea    0xe28(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1217 <gpio_get_direction+0xb7>
mov    -0x10(%rbp),%rax
movl   $0x1,(%rax)
jmp    1221 <gpio_get_direction+0xc1>
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
movl   $0x0,0xc(%rsp)
lea    0xe92(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x28,%esi
call   1030 <snprintf@plt>
mov    0x2e59(%rip),%rax
mov    0x2e5a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11e4 <gpio_get_direction+0x94>
mov    %eax,%ebx
mov    0x2e29(%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    0xe52(%rip),%rsi
mov    %r15,%rdi
call   1060 <strcmp@plt>
xor    %ecx,%ecx
test   %rax,%rax
sete   %cl
mov    %ecx,(%r14)
jmp    11e9 <gpio_get_direction+0x99>
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 [40 x i8]
store i32 %0, i32* %4
store i32* %1, i32** %5
%9 = bitcast [4 x i8]* %7 to i8*
%10 = getelementptr inbounds [40 x i8], [40 x i8]* %8, i64 0, i64 0
%11 = load i32, i32* %4
%12 = call i32 @snprintf(i8* %10, i32 40, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds [40 x i8], [40 x i8]* %8, i64 0, i64 0
%14 = load i32, i32* @O_RDONLY
%15 = load i32, i32* @O_NONBLOCK
%16 = or i32 %14, %15
%17 = call i32 @open(i8* %13, i32 %16)
store i32 %17, i32* %6
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %2
store i32 -1, i32* %3
br label %35
20: ; preds = %2
%21 = load i32, i32* %6
%22 = load i32, i32* @SEEK_SET
%23 = call i32 @lseek(i32 %21, i32 0, i32 %22)
%24 = load i32, i32* %6
%25 = bitcast [4 x i8]* %7 to i8**
%26 = call i32 @read(i32 %24, i8** %25, i32 3)
%27 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%28 = call i64 @strcmp(i8* %27, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %20
%31 = load i32*, i32** %5
store i32 1, i32* %31
br label %34
32: ; preds = %20
%33 = load i32*, i32** %5
store i32 0, i32* %33
br label %34
34: ; preds = %32, %30
store i32 0, i32* %3
br label %35
35: ; preds = %34, %19
%36 = load i32, i32* %3
ret i32 %36
}

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 i32
%4 = alloca [40 x i8]
%5 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%6 = getelementptr inbounds [40 x i8], [40 x i8]* %4, i64 0, i64 0
%7 = call i32 @snprintf(i8* %6, i32 40, 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 i32* %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
}