#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 */
struct stat {
  int dummy;
};
typedef int /*<<< orphan*/ gpio;

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ gpio_is_pin_valid(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int strlen(char *);
int write(int, char *, int const);
int gpio_unexport(int pin) {
  if (!gpio_is_pin_valid(pin)) {
    fprintf(stderr, "Invalid pin number %d!\n", pin);
    return -1;
  }
  char gpio[64];
  snprintf(gpio, sizeof(gpio), "/sys/class/gpio/gpio%d", pin);
  struct stat s;
  if (stat(gpio, &s) == 0) {
    int fd = open("/sys/class/gpio/unexport", O_WRONLY);
    if (fd == -1) {
      fprintf(stderr, "Failed top open /sys/class/gpio/unexport!\n");
      return -1;
    }
    char str_pin[4];
    snprintf(str_pin, 3, "%d", pin);
    str_pin[3] = '\0';
    const int n = strlen(str_pin) + 1;
    int result = write(fd, str_pin, n);
    if (result != n) {
      fprintf(stderr, "Failed writing to /sys/class/gpio/unexport! Error %d.\n",
              errno);
      close(fd);
      return -1;
    }
    result = close(fd);
    if (result != 0) {
      fprintf(stderr, "Failed closing /sys/class/gpio/unexport! Error %d.\n",
              errno);
      return -1;
    }
    return 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<gpio_unexport>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1090 <gpio_is_pin_valid@plt>
cmp    $0x0,%eax
jne    11c2 <gpio_unexport+0x42>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe51(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1307 <gpio_unexport+0x187>
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x40,%esi
lea    0xe43(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x50(%rbp),%rdi
lea    -0x58(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jne    1300 <gpio_unexport+0x180>
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
lea    0xe2e(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0xffffffff,-0x5c(%rbp)
jne    1236 <gpio_unexport+0xb6>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
lea    0xe25(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1307 <gpio_unexport+0x187>
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x3,%esi
lea    0xde3(%rip),%rdx
call   1050 <snprintf@plt>
movb   $0x0,-0x5d(%rbp)
lea    -0x60(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x64(%rbp)
mov    -0x5c(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    -0x64(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x68(%rbp)
mov    -0x68(%rbp),%eax
cmp    -0x64(%rbp),%eax
je     12b3 <gpio_unexport+0x133>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
mov    0x2d49(%rip),%rax
mov    (%rax),%edx
lea    0xddb(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x5c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1307 <gpio_unexport+0x187>
mov    -0x5c(%rbp),%edi
call   1060 <close@plt>
mov    %eax,-0x68(%rbp)
cmpl   $0x0,-0x68(%rbp)
je     12f4 <gpio_unexport+0x174>
mov    0x2d11(%rip),%rax
mov    (%rax),%edi
mov    0x2d00(%rip),%rax
mov    (%rax),%edx
lea    0xdc9(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1307 <gpio_unexport+0x187>
movl   $0x0,-0x4(%rbp)
jmp    1307 <gpio_unexport+0x187>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_unexport>:
push   %rbp
push   %r14
push   %rbx
sub    $0x50,%rsp
mov    %edi,%ebx
call   1090 <gpio_is_pin_valid@plt>
test   %eax,%eax
je     1254 <gpio_unexport+0xd4>
lea    0xe7a(%rip),%rdx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x40,%esi
mov    %ebx,%ecx
call   1050 <snprintf@plt>
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   1080 <stat@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    1272 <gpio_unexport+0xf2>
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
lea    0xe52(%rip),%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     127b <gpio_unexport+0xfb>
mov    %eax,%ebp
lea    0xe38(%rip),%rdx
lea    0x4(%rsp),%r14
mov    %r14,%rdi
mov    $0x3,%esi
mov    %ebx,%ecx
call   1050 <snprintf@plt>
movb   $0x0,0x7(%rsp)
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
add    $0x1,%ebx
mov    %ebp,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
jne    1294 <gpio_unexport+0x114>
mov    %ebp,%edi
call   1060 <close@plt>
test   %eax,%eax
je     12bd <gpio_unexport+0x13d>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
mov    0x2d8f(%rip),%rax
mov    (%rax),%edx
lea    0xe58(%rip),%rsi
jmp    1266 <gpio_unexport+0xe6>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
lea    0xd9c(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
lea    0xdbd(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    126d <gpio_unexport+0xed>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
mov    0x2d34(%rip),%rax
mov    (%rax),%edx
lea    0xdc6(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    126d <gpio_unexport+0xed>
xor    %eax,%eax
jmp    1272 <gpio_unexport+0xf2>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [24 x i8] c"Invalid pin number %d!\0A\00"
@.str.1 = [23 x i8] c"/sys/class/gpio/gpio%d\00"
@.str.2 = [25 x i8] c"/sys/class/gpio/unexport\00"
@O_WRONLY = global i32 0
@.str.3 = [43 x i8] c"Failed top open /sys/class/gpio/unexport!\0A\00"
@.str.4 = [3 x i8] c"%d\00"
@.str.5 = [55 x i8] c"Failed writing to /sys/class/gpio/unexport! Error %d.\0A\00"
@errno = global i32 0
@.str.6 = [52 x i8] c"Failed closing /sys/class/gpio/unexport! Error %d.\0A\00"
define i32 @gpio_unexport(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [64 x i8]
%5 = alloca %struct.stat
%6 = alloca i32
%7 = alloca [4 x i8]
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %3
%10 = load i32, i32* %3
%11 = call i32 @gpio_is_pin_valid(i32 %10)
%12 = icmp ne i32 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = load i32, i32* %3
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %15)
store i32 -1, i32* %2
br label %64
17: ; preds = %1
%18 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%19 = load i32, i32* %3
%20 = call i32 @snprintf(i8* %18, i32 64, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%22 = call i64 @stat(i8* %21, %struct.stat* %5)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %63
24: ; preds = %17
%25 = load i32, i32* @O_WRONLY
%26 = call i32 @open(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %25)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %64
32: ; preds = %24
%33 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%34 = load i32, i32* %3
%35 = call i32 @snprintf(i8* %33, i32 3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %34)
%36 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 3
store i8 0, i8* %36
%37 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%38 = call i32 @strlen(i8* %37)
%39 = add nsw i32 %38, 1
store i32 %39, i32* %8
%40 = load i32, i32* %6
%41 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%42 = load i32, i32* %8
%43 = call i32 @write(i32 %40, i8* %41, i32 %42)
store i32 %43, i32* %9
%44 = load i32, i32* %9
%45 = load i32, i32* %8
%46 = icmp ne i32 %44, %45
br i1 %46, label %47, label %53
47: ; preds = %32
%48 = load i32, i32* @stderr
%49 = load i32, i32* @errno
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.5, i64 0, i64 0), i32 %49)
%51 = load i32, i32* %6
%52 = call i32 @close(i32 %51)
store i32 -1, i32* %2
br label %64
53: ; preds = %32
%54 = load i32, i32* %6
%55 = call i32 @close(i32 %54)
store i32 %55, i32* %9
%56 = load i32, i32* %9
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %62
58: ; preds = %53
%59 = load i32, i32* @stderr
%60 = load i32, i32* @errno
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.6, i64 0, i64 0), i32 %60)
store i32 -1, i32* %2
br label %64
62: ; preds = %53
store i32 0, i32* %2
br label %64
63: ; preds = %17
store i32 0, i32* %2
br label %64
64: ; preds = %63, %62, %58, %47, %29, %13
%65 = load i32, i32* %2
ret i32 %65
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [24 x i8] c"Invalid pin number %d!\0A\00"
@.str.1 = [23 x i8] c"/sys/class/gpio/gpio%d\00"
@.str.2 = [25 x i8] c"/sys/class/gpio/unexport\00"
@O_WRONLY = global i32 0
@.str.3 = [43 x i8] c"Failed top open /sys/class/gpio/unexport!\0A\00"
@.str.4 = [3 x i8] c"%d\00"
@.str.5 = [55 x i8] c"Failed writing to /sys/class/gpio/unexport! Error %d.\0A\00"
@errno = global i32 0
@.str.6 = [52 x i8] c"Failed closing /sys/class/gpio/unexport! Error %d.\0A\00"
define i32 @gpio_unexport(i32 %0) {
%2 = alloca [64 x i8]
%3 = alloca %struct.stat
%4 = alloca [4 x i8]
%5 = tail call i32 @gpio_is_pin_valid(i32 %0)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %47
10: ; preds = %1
%11 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%12 = call i32 @snprintf(i8* %11, i32 64, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%13 = bitcast %struct.stat* %3 to i8*
%14 = call i64 @stat(i8* %11, %struct.stat* %3)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %45
16: ; preds = %10
%17 = load i32, i32* @O_WRONLY
%18 = call i32 @open(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0))
br label %45
23: ; preds = %16
%24 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%25 = call i32 @snprintf(i8* %24, i32 3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %0)
%26 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 3
store i8 0, i8* %26
%27 = call i32 @strlen(i8* %24)
%28 = add nsw i32 %27, 1
%29 = call i32 @write(i32 %18, i8* %24, i32 %28)
%30 = icmp eq i32 %29, %28
br i1 %30, label %36, label %31
31: ; preds = %23
%32 = load i32, i32* @stderr
%33 = load i32, i32* @errno
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.5, i64 0, i64 0), i32 %33)
%35 = call i32 @close(i32 %18)
br label %43
36: ; preds = %23
%37 = call i32 @close(i32 %18)
%38 = icmp eq i32 %37, 0
br i1 %38, label %43, label %39
39: ; preds = %36
%40 = load i32, i32* @stderr
%41 = load i32, i32* @errno
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.6, i64 0, i64 0), i32 %41)
br label %43
43: ; preds = %36, %39, %31
%44 = phi i32 [ -1, %31 ], [ -1, %39 ], [ 0, %36 ]
br label %45
45: ; preds = %10, %20, %43
%46 = phi i32 [ -1, %20 ], [ %44, %43 ], [ 0, %10 ]
br label %47
47: ; preds = %45, %7
%48 = phi i32 [ %46, %45 ], [ -1, %7 ]
ret i32 %48
}