#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 /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
scalar_t__ write(int, char *, int);
int gpio_set_direction(unsigned int gpio, unsigned int in_flag) {
  int fd;
  char filename[128];
  snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/direction",
           gpio);
  ;
  if ((fd = open(filename, O_WRONLY)) < 0) {
    return -1;
  }
  if (in_flag) {
    if (write(fd, "in", 3) < 0)
      return -1;
  } else {
    if (write(fd, "out", 4) < 0)
      return -1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<gpio_set_direction>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x80,%esi
lea    0xe99(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x90(%rbp),%rdi
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    1199 <gpio_set_direction+0x59>
movl   $0xffffffff,-0x4(%rbp)
jmp    1210 <gpio_set_direction+0xd0>
cmpl   $0x0,-0xc(%rbp)
je     11d2 <gpio_set_direction+0x92>
mov    -0x10(%rbp),%edi
lea    0xe74(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    11cd <gpio_set_direction+0x8d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1210 <gpio_set_direction+0xd0>
jmp    1201 <gpio_set_direction+0xc1>
mov    -0x10(%rbp),%edi
lea    0xe48(%rip),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    11fc <gpio_set_direction+0xbc>
movl   $0xffffffff,-0x4(%rbp)
jmp    1210 <gpio_set_direction+0xd0>
jmp    1201 <gpio_set_direction+0xc1>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_set_direction>:
push   %rbp
push   %r14
push   %rbx
sub    $0x80,%rsp
mov    %esi,%r14d
mov    %edi,%ecx
lea    0xea9(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x80,%esi
call   1040 <snprintf@plt>
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     11b9 <gpio_set_direction+0x79>
mov    %eax,%ebp
test   %r14d,%r14d
je     1198 <gpio_set_direction+0x58>
lea    0xe92(%rip),%rsi
mov    %ebp,%edi
mov    $0x3,%edx
jmp    11a6 <gpio_set_direction+0x66>
lea    0xe85(%rip),%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   1030 <write@plt>
test   %rax,%rax
js     11b9 <gpio_set_direction+0x79>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@O_WRONLY = global i32 0
@.str.1 = [3 x i8] c"in\00"
@.str.2 = [4 x i8] c"out\00"
define i32 @gpio_set_direction(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [128 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%9 = load i32, i32* %4
%10 = call i32 @snprintf(i8* %8, i32 128, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%12 = load i32, i32* @O_WRONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 -1, i32* %3
br label %34
16: ; preds = %2
%17 = load i32, i32* %5
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %16
%20 = load i32, i32* %6
%21 = call i64 @write(i32 %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 3)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %19
store i32 -1, i32* %3
br label %34
24: ; preds = %19
br label %31
25: ; preds = %16
%26 = load i32, i32* %6
%27 = call i64 @write(i32 %26, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 4)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %25
store i32 -1, i32* %3
br label %34
30: ; preds = %25
br label %31
31: ; preds = %30, %24
%32 = load i32, i32* %6
%33 = call i32 @close(i32 %32)
store i32 0, i32* %3
br label %34
34: ; preds = %31, %29, %23, %15
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@.str = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@O_WRONLY = global i32 0
@.str.1 = [3 x i8] c"in\00"
@.str.2 = [4 x i8] c"out\00"
define i32 @gpio_set_direction(i32 %0, i32 %1) {
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 128, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = load i32, i32* @O_WRONLY
%7 = call i32 @open(i8* %4, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %19, label %9
9: ; preds = %2
%10 = icmp eq i32 %1, 0
br i1 %10, label %14, label %11
11: ; preds = %9
%12 = call i64 @write(i32 %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 3)
%13 = icmp slt i64 %12, 0
br i1 %13, label %19, label %17
14: ; preds = %9
%15 = call i64 @write(i32 %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 4)
%16 = icmp slt i64 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %14, %11
%18 = call i32 @close(i32 %7)
br label %19
19: ; preds = %14, %11, %2, %17
%20 = phi i32 [ 0, %17 ], [ -1, %2 ], [ -1, %11 ], [ -1, %14 ]
ret i32 %20
}