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

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ errno;
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fputs(char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
scalar_t__ strcmp(char const *, char *);
int gpio_set_direction(int pin, const char *direction) {
  char path[1024];
  FILE *fp;
  int rc = -1;
  if (strcmp(direction, "in") != 0 && strcmp(direction, "out") != 0 &&
      strcmp(direction, "low") != 0 && strcmp(direction, "high") != 0) {
    errno = EINVAL;
    goto done;
  }
  snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/direction", pin);
  if (!(fp = fopen(path, "w")))
    goto done;
  if (fputs(direction, fp) < 0) {
    (void)fclose(fp);
    goto done;
  }
  if (fclose(fp) != 0)
    goto done;
  rc = 0;
done:
  return rc;
}


x86 assembly (clang, -O0):
<gpio_set_direction>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x41c(%rbp)
ff ff ff
mov    -0x10(%rbp),%rdi
lea    0xe89(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     11eb <gpio_set_direction+0x9b>
mov    -0x10(%rbp),%rdi
lea    0xe72(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     11eb <gpio_set_direction+0x9b>
mov    -0x10(%rbp),%rdi
lea    0xe5c(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     11eb <gpio_set_direction+0x9b>
mov    -0x10(%rbp),%rdi
lea    0xe46(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     11eb <gpio_set_direction+0x9b>
mov    0x2dfd(%rip),%rax
mov    (%rax),%ecx
mov    0x2dfc(%rip),%rax
mov    %ecx,(%rax)
jmp    127f <gpio_set_direction+0x12f>
lea    -0x410(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x400,%esi
lea    0xe0f(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x410(%rbp),%rdi
lea    0xdf5(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x418(%rbp)
cmp    $0x0,%rax
jne    122f <gpio_set_direction+0xdf>
jmp    127f <gpio_set_direction+0x12f>
mov    -0x10(%rbp),%rdi
mov    -0x418(%rbp),%rsi
call   1050 <fputs@plt>
cmp    $0x0,%rax
jge    125a <gpio_set_direction+0x10a>
mov    -0x418(%rbp),%rdi
call   1030 <fclose@plt>
jmp    127f <gpio_set_direction+0x12f>
mov    -0x418(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
je     1275 <gpio_set_direction+0x125>
jmp    127f <gpio_set_direction+0x12f>
movl   $0x0,-0x41c(%rbp)
mov    -0x41c(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_set_direction>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
lea    0xe99(%rip),%rsi
mov    %r14,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11c9 <gpio_set_direction+0x79>
lea    0xe88(%rip),%rsi
mov    %r14,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11c9 <gpio_set_direction+0x79>
lea    0xe78(%rip),%rsi
mov    %r14,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11c9 <gpio_set_direction+0x79>
lea    0xe68(%rip),%rsi
mov    %r14,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11c9 <gpio_set_direction+0x79>
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    0x2e20(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebp
jmp    121e <gpio_set_direction+0xce>
lea    0xe40(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %ebp,%ecx
call   1040 <snprintf@plt>
lea    0xe20(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     121e <gpio_set_direction+0xce>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %rax,%rsi
call   1050 <fputs@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %r14,%r14
js     121e <gpio_set_direction+0xce>
neg    %rax
sbb    %ebp,%ebp
mov    %ebp,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"in\00"
@.str.1 = [4 x i8] c"out\00"
@.str.2 = [4 x i8] c"low\00"
@.str.3 = [5 x i8] c"high\00"
@EINVAL = global i32 0
@errno = global i32 0
@.str.4 = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@.str.5 = [2 x i8] c"w\00"
define i32 @gpio_set_direction(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca [1024 x i8]
%6 = alloca i32*
%7 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
store i32 -1, i32* %7
%8 = load i8*, i8** %4
%9 = call i64 @strcmp(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %25
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = call i64 @strcmp(i8* %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %25
15: ; preds = %11
%16 = load i8*, i8** %4
%17 = call i64 @strcmp(i8* %16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %15
%20 = load i8*, i8** %4
%21 = call i64 @strcmp(i8* %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = load i32, i32* @EINVAL
store i32 %24, i32* @errno
br label %47
25: ; preds = %19, %15, %11, %2
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%27 = load i32, i32* %3
%28 = call i32 @snprintf(i8* %26, i32 1024, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %27)
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%30 = call i32* @fopen(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %30, i32** %6
%31 = icmp ne i32* %30, null
br i1 %31, label %33, label %32
32: ; preds = %25
br label %47
33: ; preds = %25
%34 = load i8*, i8** %4
%35 = load i32*, i32** %6
%36 = call i64 @fputs(i8* %34, i32* %35)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %33
%39 = load i32*, i32** %6
%40 = call i64 @fclose(i32* %39)
br label %47
41: ; preds = %33
%42 = load i32*, i32** %6
%43 = call i64 @fclose(i32* %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %41
br label %47
46: ; preds = %41
store i32 0, i32* %7
br label %47
47: ; preds = %46, %45, %38, %32, %23
%48 = load i32, i32* %7
ret i32 %48
}

LLVM IR (-O2):
@.str = [3 x i8] c"in\00"
@.str.1 = [4 x i8] c"out\00"
@.str.2 = [4 x i8] c"low\00"
@.str.3 = [5 x i8] c"high\00"
@EINVAL = global i32 0
@errno = global i32 0
@.str.4 = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@.str.5 = [2 x i8] c"w\00"
define i32 @gpio_set_direction(i32 %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i64 %5, 0
br i1 %6, label %18, label %7
7: ; preds = %2
%8 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i64 %8, 0
br i1 %9, label %18, label %10
10: ; preds = %7
%11 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%12 = icmp eq i64 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %10
%14 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = load i32, i32* @EINVAL
store i32 %17, i32* @errno
br label %29
18: ; preds = %13, %10, %7, %2
%19 = call i32 @snprintf(i8* %4, i32 1024, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %0)
%20 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%21 = icmp eq i32* %20, null
br i1 %21, label %29, label %22
22: ; preds = %18
%23 = call i64 @fputs(i8* %1, i32* %20)
%24 = icmp slt i64 %23, 0
%25 = call i64 @fclose(i32* %20)
br i1 %24, label %29, label %26
26: ; preds = %22
%27 = icmp ne i64 %25, 0
%28 = sext i1 %27 to i32
br label %29
29: ; preds = %22, %26, %18, %16
%30 = phi i32 [ -1, %16 ], [ -1, %18 ], [ %28, %26 ], [ -1, %22 ]
ret i32 %30
}