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


x86 assembly (clang, -O0):
<gpio_set_direction>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x4a(%rbp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1070 <memset@plt>
lea    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x28,%esi
lea    0xe5f(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    11d0 <gpio_set_direction+0x60>
movl   $0xffffffff,-0x4(%rbp)
jmp    123d <gpio_set_direction+0xcd>
cmpl   $0x0,-0xc(%rbp)
je     11f4 <gpio_set_direction+0x84>
lea    -0x4a(%rbp),%rdi
lea    0xe3c(%rip),%rsi
mov    $0x9,%edx
call   1030 <strncpy@plt>
jmp    1209 <gpio_set_direction+0x99>
lea    -0x4a(%rbp),%rdi
lea    0xe26(%rip),%rsi
mov    $0x9,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x5c(%rbp)
lea    -0x4a(%rbp),%rax
mov    %rax,-0x58(%rbp)
lea    -0x4a(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    -0x10(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_set_direction>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
mov    %esi,%ebp
mov    %edi,%ecx
movw   $0x0,0x8(%rsp)
movq   $0x0,(%rsp)
lea    0xe80(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x28,%esi
call   1060 <snprintf@plt>
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11ec <gpio_set_direction+0x8c>
mov    %eax,%ebx
test   %ebp,%ebp
lea    0xe73(%rip),%rax
lea    0xe68(%rip),%rsi
cmove  %rax,%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x9,%edx
call   1030 <strncpy@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
jmp    11f1 <gpio_set_direction+0x91>
mov    $0xffffffff,%eax
add    $0x38,%rsp
pop    %rbx
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 = [4 x i8] c"out\00"
@.str.2 = [3 x i8] c"in\00"
define i32 @gpio_set_direction(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [40 x i8]
%8 = alloca [10 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = bitcast [10 x i8]* %8 to i8*
%10 = getelementptr inbounds [40 x i8], [40 x i8]* %7, 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]* %7, i64 0, i64 0
%14 = load i32, i32* @O_WRONLY
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %2
store i32 -1, i32* %3
br label %35
18: ; preds = %2
%19 = load i32, i32* %5
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%23 = call i32 @strncpy(i8* %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 9)
br label %27
24: ; preds = %18
%25 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%26 = call i32 @strncpy(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 9)
br label %27
27: ; preds = %24, %21
%28 = load i32, i32* %6
%29 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%30 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%31 = call i32 @strlen(i8* %30)
%32 = call i32 @write(i32 %28, i8* %29, i32 %31)
%33 = load i32, i32* %6
%34 = call i32 @close(i32 %33)
store i32 0, i32* %3
br label %35
35: ; preds = %27, %17
%36 = load i32, i32* %3
ret i32 %36
}

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