#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*/ v_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);
int write(int, char *, int);
int gpio_set_value(int gpio_num, int value) {
  const char *gpio_value_path = "/sys/class/gpio/gpio%d/value";
  int fd_v = 0, g_err = -1;
  char pinvalue_buf[256];
  char v_buf[8];
  if (sprintf(pinvalue_buf, gpio_value_path, gpio_num) < 0) {
    printf("Can't create pin direction file path\n");
    return g_err;
  }
  fd_v = open(pinvalue_buf, O_WRONLY);
  if (fd_v < 0) {
    if (gpio_export(gpio_num) < 0) {
      return g_err;
    }
    fd_v = open(pinvalue_buf, O_WRONLY);
    if (fd_v <= 0) {
      printf("Couldn't get value File for pin %d\n", gpio_num);
      return g_err;
    }
  }
  sprintf(v_buf, "%d", value);
  if (write(fd_v, v_buf, sizeof(v_buf)) == g_err) {
    printf("Couldn't set value for pin %d\n", gpio_num);
    return g_err;
  }
  close(fd_v);
  return 0;
}


x86 assembly (clang, -O0):
<gpio_set_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    0xe88(%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
call   1080 <sprintf@plt>
cmp    $0x0,%rax
jge    11c0 <gpio_set_value+0x60>
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12a0 <gpio_set_value+0x140>
lea    -0x120(%rbp),%rdi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1242 <gpio_set_value+0xe2>
mov    -0x8(%rbp),%edi
call   1050 <gpio_export@plt>
cmp    $0x0,%rax
jge    11ff <gpio_set_value+0x9f>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12a0 <gpio_set_value+0x140>
lea    -0x120(%rbp),%rdi
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     123d <gpio_set_value+0xdd>
mov    -0x8(%rbp),%esi
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12a0 <gpio_set_value+0x140>
jmp    1242 <gpio_set_value+0xe2>
lea    -0x128(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe14(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x1c(%rbp),%edi
lea    -0x128(%rbp),%rsi
mov    $0x8,%edx
call   1030 <write@plt>
cmp    -0x20(%rbp),%eax
jne    1291 <gpio_set_value+0x131>
mov    -0x8(%rbp),%esi
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12a0 <gpio_set_value+0x140>
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_value>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x110,%rsp
mov    %esi,%r14d
mov    %edi,%r15d
lea    0xe74(%rip),%rsi
lea    0x10(%rsp),%rdi
mov    %r15d,%edx
call   1090 <sprintf@plt>
test   %rax,%rax
js     11ef <gpio_set_value+0x7f>
mov    0x2e33(%rip),%r12
mov    (%r12),%esi
lea    0x10(%rsp),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11fd <gpio_set_value+0x8d>
lea    0xe81(%rip),%rsi
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
mov    %r14d,%edx
call   1090 <sprintf@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x8,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     1236 <gpio_set_value+0xc6>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %ebp,%ebp
jmp    124c <gpio_set_value+0xdc>
lea    0xe6d(%rip),%rdi
call   1030 <puts@plt>
jmp    1247 <gpio_set_value+0xd7>
mov    %r15d,%edi
call   1060 <gpio_export@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
js     124c <gpio_set_value+0xdc>
mov    (%r12),%esi
lea    0x10(%rsp),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jg     11b9 <gpio_set_value+0x49>
lea    0xdf3(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    124c <gpio_set_value+0xdc>
lea    0xe07(%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 = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@.str.1 = [38 x i8] c"Can't create pin direction file path\0A\00"
@O_WRONLY = global i32 0
@.str.2 = [36 x i8] c"Couldn't get value File for pin %d\0A\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [31 x i8] c"Couldn't set value for pin %d\0A\00"
define i32 @gpio_set_value(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%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 i32 %1, i32* %5
store i8* getelementptr inbounds ([29 x i8], [29 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 @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 ([36 x i8], [36 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 i32, i32* %5
%45 = call i64 @sprintf(i8* %43, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %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 ([31 x i8], [31 x i8]* @.str.4, 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 = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_WRONLY = global i32 0
@.str.2 = [36 x i8] c"Couldn't get value File for pin %d\0A\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [31 x i8] c"Couldn't set value for pin %d\0A\00"
@str = [37 x i8] c"Can't create pin direction file path\00"
define i32 @gpio_set_value(i32 %0, i32 %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 @sprintf(i8* %5, i8* getelementptr inbounds ([29 x i8], [29 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 ([36 x i8], [36 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 @sprintf(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %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 ([31 x i8], [31 x i8]* @.str.4, 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
}