#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 uint8_t;
typedef int /*<<< orphan*/ b;

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ pwrite(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int cleanup(uint8_t pin) {
  int fd;
  char b[8];
  fd = open("/sys/class/gpio/unexport", O_WRONLY);
  if (fd < 0) {
    perror("/sys/class/gpio/unexport");
    return 1;
  }
  snprintf(b, sizeof(b), "%d", pin);
  if (pwrite(fd, b, strlen(b), 0) < 0) {
    fprintf(stderr, "Unable to unexport pin=%d: %s", pin, strerror(errno));
    return 2;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<cleanup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
lea    0xe65(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11c5 <cleanup+0x45>
lea    0xe4c(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    125e <cleanup+0xde>
lea    -0x14(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x8,%esi
lea    0xe41(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
lea    -0x14(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    -0x14(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <pwrite@plt>
cmp    $0x0,%rax
jge    124f <cleanup+0xcf>
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%edx
mov    %rax,%rcx
lea    0xdde(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    125e <cleanup+0xde>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cleanup>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
lea    0xe66(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1222 <cleanup+0xa2>
mov    %eax,%ebp
lea    0xe6d(%rip),%rdx
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x8,%esi
mov    %r14d,%ecx
call   1040 <snprintf@plt>
mov    %r15,%rdi
call   1030 <strlen@plt>
xor    %ebx,%ebx
mov    %ebp,%edi
mov    %r15,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <pwrite@plt>
test   %rax,%rax
js     11e5 <cleanup+0x65>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    1215 <cleanup+0x95>
mov    0x2df4(%rip),%rax
mov    (%rax),%ebx
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe19(%rip),%rsi
mov    %ebx,%edi
mov    %r14d,%edx
mov    %rax,%rcx
call   1060 <fprintf@plt>
mov    $0x2,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd7(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%ebx
jmp    1215 <cleanup+0x95>

LLVM IR (-O0):
@.str = [25 x i8] c"/sys/class/gpio/unexport\00"
@O_WRONLY = global i32 0
@.str.1 = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.2 = [30 x i8] c"Unable to unexport pin=%d: %s\00"
@errno = global i32 0
define i32 @cleanup(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [8 x i8]
store i32 %0, i32* %3
%6 = load i32, i32* @O_WRONLY
%7 = call i32 @open(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %2
br label %31
12: ; preds = %1
%13 = getelementptr inbounds [8 x i8], [8 x i8]* %5, i64 0, i64 0
%14 = load i32, i32* %3
%15 = call i32 @snprintf(i8* %13, i32 8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = load i32, i32* %4
%17 = getelementptr inbounds [8 x i8], [8 x i8]* %5, i64 0, i64 0
%18 = getelementptr inbounds [8 x i8], [8 x i8]* %5, i64 0, i64 0
%19 = call i32 @strlen(i8* %18)
%20 = call i64 @pwrite(i32 %16, i8* %17, i32 %19, i32 0)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %12
%23 = load i32, i32* @stderr
%24 = load i32, i32* %3
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %24, i8* %26)
store i32 2, i32* %2
br label %31
28: ; preds = %12
%29 = load i32, i32* %4
%30 = call i32 @close(i32 %29)
store i32 0, i32* %2
br label %31
31: ; preds = %28, %22, %10
%32 = load i32, i32* %2
ret i32 %32
}

LLVM IR (-O2):
@.str = [25 x i8] c"/sys/class/gpio/unexport\00"
@O_WRONLY = global i32 0
@.str.1 = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.2 = [30 x i8] c"Unable to unexport pin=%d: %s\00"
@errno = global i32 0
define i32 @cleanup(i32 %0) {
%2 = alloca [8 x i8]
%3 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @O_WRONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %21
9: ; preds = %1
%10 = call i32 @snprintf(i8* %3, i32 8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%11 = call i32 @strlen(i8* %3)
%12 = call i64 @pwrite(i32 %5, i8* %3, i32 %11, i32 0)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %9
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %0, i8* %17)
br label %21
19: ; preds = %9
%20 = call i32 @close(i32 %5)
br label %21
21: ; preds = %19, %14, %7
%22 = phi i32 [ 1, %7 ], [ 2, %14 ], [ 0, %19 ]
ret i32 %22
}