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

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
scalar_t__ gpio_export(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ sprintf(char *, char const *, ...);
int write(int, char *, int);
int gpio_set_mode(int gpio_num, const char *mode) {
  const char *gpio_direction_path = "/sys/class/gpio/gpio%d/direction";
  int fd_d = 0, g_err = -1;
  char pindirection_buf[256];
  char d_buf[8];
  if (sprintf(pindirection_buf, gpio_direction_path, gpio_num) < 0) {
    printf("Can't create pin direction file path\n");
    return g_err;
  }
  fd_d = open(pindirection_buf, O_WRONLY);
  if (fd_d < 0) {
    if (gpio_export(gpio_num) < 0) {
      return g_err;
    }
    fd_d = open(pindirection_buf, O_WRONLY);
    if (fd_d <= 0) {
      printf("Couldn't get direction File for pin %d\n", gpio_num);
      return g_err;
    }
  }
  sprintf(d_buf, mode);
  if (write(fd_d, d_buf, sizeof(d_buf)) == g_err) {
    printf("Couldn't set direction for pin %d\n", gpio_num);
    return g_err;
  }
  close(fd_d);
  return 0;
}


x86 assembly (clang, -O0):
<gpio_set_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe87(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0xffffffff,-0x20(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x8(%rbp),%edx
mov    $0x0,%al
call   1080 <sprintf@plt>
cmp    $0x0,%rax
jge    11c3 <gpio_set_mode+0x63>
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    129f <gpio_set_mode+0x13f>
lea    -0x120(%rbp),%rdi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1245 <gpio_set_mode+0xe5>
mov    -0x8(%rbp),%edi
call   1050 <gpio_export@plt>
cmp    $0x0,%rax
jge    1202 <gpio_set_mode+0xa2>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    129f <gpio_set_mode+0x13f>
lea    -0x120(%rbp),%rdi
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     1240 <gpio_set_mode+0xe0>
mov    -0x8(%rbp),%esi
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    129f <gpio_set_mode+0x13f>
jmp    1245 <gpio_set_mode+0xe5>
lea    -0x128(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
mov    -0x1c(%rbp),%edi
lea    -0x128(%rbp),%rsi
mov    $0x8,%edx
call   1030 <write@plt>
cmp    -0x20(%rbp),%eax
jne    1290 <gpio_set_mode+0x130>
mov    -0x8(%rbp),%esi
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    129f <gpio_set_mode+0x13f>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_set_mode>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x110,%rsp
mov    %rsi,%r14
mov    %edi,%r15d
lea    0xe74(%rip),%rsi
lea    0x10(%rsp),%rdi
mov    %r15d,%edx
xor    %eax,%eax
call   1090 <sprintf@plt>
test   %rax,%rax
js     11ec <gpio_set_mode+0x7c>
mov    0x2e31(%rip),%r12
mov    (%r12),%esi
lea    0x10(%rsp),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11fa <gpio_set_mode+0x8a>
xor    %ebp,%ebp
lea    0x8(%rsp),%r12
mov    %r12,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1090 <sprintf@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x8,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     1233 <gpio_set_mode+0xc3>
mov    %ebx,%edi
call   1070 <close@plt>
jmp    1249 <gpio_set_mode+0xd9>
lea    0xe79(%rip),%rdi
call   1030 <puts@plt>
jmp    1244 <gpio_set_mode+0xd4>
mov    %r15d,%edi
call   1060 <gpio_export@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
js     1249 <gpio_set_mode+0xd9>
mov    (%r12),%esi
lea    0x10(%rsp),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jg     11bb <gpio_set_mode+0x4b>
lea    0xdfa(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1249 <gpio_set_mode+0xd9>
lea    0xe0f(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x110,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@.str.1 = [38 x i8] c"Can't create pin direction file path\0A\00"
@O_WRONLY = global i32 0
@.str.2 = [40 x i8] c"Couldn't get direction File for pin %d\0A\00"
@.str.3 = [35 x i8] c"Couldn't set direction for pin %d\0A\00"
define i32 @gpio_set_mode(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca [256 x i8]
%10 = alloca [8 x i8]
store i32 %0, i32* %4
store i8* %1, i8** %5
store i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8** %6
store i32 0, i32* %7
store i32 -1, i32* %8
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%12 = load i8*, i8** %6
%13 = load i32, i32* %4
%14 = call i64 (i8*, i8*, ...) @sprintf(i8* %11, i8* %12, i32 %13)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* %8
store i32 %18, i32* %3
br label %58
19: ; preds = %2
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%21 = load i32, i32* @O_WRONLY
%22 = call i32 @open(i8* %20, i32 %21)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %42
25: ; preds = %19
%26 = load i32, i32* %4
%27 = call i64 @gpio_export(i32 %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = load i32, i32* %8
store i32 %30, i32* %3
br label %58
31: ; preds = %25
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%33 = load i32, i32* @O_WRONLY
%34 = call i32 @open(i8* %32, i32 %33)
store i32 %34, i32* %7
%35 = load i32, i32* %7
%36 = icmp sle i32 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %31
%38 = load i32, i32* %4
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %38)
%40 = load i32, i32* %8
store i32 %40, i32* %3
br label %58
41: ; preds = %31
br label %42
42: ; preds = %41, %19
%43 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%44 = load i8*, i8** %5
%45 = call i64 (i8*, i8*, ...) @sprintf(i8* %43, i8* %44)
%46 = load i32, i32* %7
%47 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%48 = call i32 @write(i32 %46, i8* %47, i32 8)
%49 = load i32, i32* %8
%50 = icmp eq i32 %48, %49
br i1 %50, label %51, label %55
51: ; preds = %42
%52 = load i32, i32* %4
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %52)
%54 = load i32, i32* %8
store i32 %54, i32* %3
br label %58
55: ; preds = %42
%56 = load i32, i32* %7
%57 = call i32 @close(i32 %56)
store i32 0, i32* %3
br label %58
58: ; preds = %55, %51, %37, %29, %16
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
@.str = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@O_WRONLY = global i32 0
@.str.2 = [40 x i8] c"Couldn't get direction File for pin %d\0A\00"
@.str.3 = [35 x i8] c"Couldn't set direction for pin %d\0A\00"
@str = [37 x i8] c"Can't create pin direction file path\00"
define i32 @gpio_set_mode(i32 %0, i8* %1) {
%3 = alloca [256 x i8]
%4 = alloca [8 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 0
%7 = call i64 (i8*, i8*, ...) @sprintf(i8* %5, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %0)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str, i64 0, i64 0))
br label %33
11: ; preds = %2
%12 = load i32, i32* @O_WRONLY
%13 = call i32 @open(i8* %5, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %24
15: ; preds = %11
%16 = call i64 @gpio_export(i32 %0)
%17 = icmp slt i64 %16, 0
br i1 %17, label %33, label %18
18: ; preds = %15
%19 = load i32, i32* @O_WRONLY
%20 = call i32 @open(i8* %5, i32 %19)
%21 = icmp slt i32 %20, 1
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %0)
br label %33
24: ; preds = %18, %11
%25 = phi i32 [ %20, %18 ], [ %13, %11 ]
%26 = call i64 (i8*, i8*, ...) @sprintf(i8* %6, i8* %1)
%27 = call i32 @write(i32 %25, i8* %6, i32 8)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %0)
br label %33
31: ; preds = %24
%32 = call i32 @close(i32 %25)
br label %33
33: ; preds = %15, %31, %29, %22, %9
%34 = phi i32 [ -1, %9 ], [ -1, %22 ], [ -1, %29 ], [ 0, %31 ], [ -1, %15 ]
ret i32 %34
}