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

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
scalar_t__ pwrite(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int strlen(char *);
int do_export(uint16_t pin) {
  int fd;
  char b[64];
  DIR *dir;
  snprintf(b, sizeof(b), "%s%d/",
           "/sys/class/gpio/"
           "gpio",
           pin);
  dir = opendir(b);
  if (dir) {
    closedir(dir);
    return -2;
  } else {
    fd = open("/sys/class/gpio/"
              "export",
              O_WRONLY);
    if (fd < 0) {
      perror("/sys/class/gpio/"
             "export");
      return -1;
    }
    snprintf(b, sizeof(b), "%d", pin);
    if (pwrite(fd, b, strlen(b), 0) < 0) {
      fprintf(stderr, "Unable to export pin=%d (already in use?): %s\n", pin,
              strerror(errno));
      return -1;
    }
    close(fd);
    snprintf(b, sizeof(b), "%s%d/direction",
             "/sys/class/gpio/"
             "gpio",
             pin);
    fd = open(b, O_WRONLY);
    if (fd < 0) {
      fprintf(stderr, "Open %s: %s\n", b, strerror(errno));
      return -1;
    }
    if (pwrite(fd, "out", 4, 0) < 0) {
      fprintf(stderr, "Unable to pwrite to gpio direction for pin %d: %s\n",
              pin, strerror(errno));
      close(fd);
      return -1;
    }
    close(fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<do_export>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%r8d
mov    $0x40,%esi
lea    0xe3e(%rip),%rdx
lea    0xe3d(%rip),%rcx
mov    $0x0,%al
call   1050 <snprintf@plt>
lea    -0x50(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     11fd <do_export+0x5d>
mov    -0x58(%rbp),%rdi
call   1070 <closedir@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    13d4 <do_export+0x234>
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
lea    0xe0e(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1237 <do_export+0x97>
lea    0xdf5(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d4 <do_export+0x234>
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x40,%esi
lea    0xde8(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x64(%rbp)
lea    -0x50(%rbp),%rax
mov    %rax,-0x60(%rbp)
lea    -0x50(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1090 <pwrite@plt>
cmp    $0x0,%rax
jge    12c5 <do_export+0x125>
mov    0x2d57(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x68(%rbp)
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x6c(%rbp),%edi
mov    -0x68(%rbp),%edx
mov    %rax,%rcx
lea    0xd83(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d4 <do_export+0x234>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%r8d
mov    $0x40,%esi
lea    0xd83(%rip),%rdx
lea    0xd1e(%rip),%rcx
mov    $0x0,%al
call   1050 <snprintf@plt>
lea    -0x50(%rbp),%rdi
mov    0x2cce(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1354 <do_export+0x1b4>
mov    0x2ccb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x7c(%rbp)
lea    -0x50(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    0x2caf(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x7c(%rbp),%edi
mov    -0x78(%rbp),%rdx
mov    %rax,%rcx
lea    0xd32(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d4 <do_export+0x234>
mov    -0xc(%rbp),%edi
lea    0xd22(%rip),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1090 <pwrite@plt>
cmp    $0x0,%rax
jge    13c5 <do_export+0x225>
mov    0x2c65(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x84(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x80(%rbp)
mov    0x2c48(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x84(%rbp),%edi
mov    -0x80(%rbp),%edx
mov    %rax,%rcx
lea    0xcda(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d4 <do_export+0x234>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_export>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %edi,%r15d
lea    0xe4c(%rip),%rdx
lea    0xe4b(%rip),%rcx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x40,%esi
mov    %r15d,%r8d
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    %rbp,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     11f7 <do_export+0x57>
mov    %rax,%rdi
call   1070 <closedir@plt>
mov    $0xfffffffe,%ebp
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dca(%rip),%r14
mov    (%r14),%esi
lea    0xe13(%rip),%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     1360 <do_export+0x1c0>
mov    %eax,%ebp
lea    0xe14(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
mov    %r15d,%ecx
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1090 <pwrite@plt>
test   %rax,%rax
js     12b3 <do_export+0x113>
mov    %ebp,%edi
call   1060 <close@plt>
lea    0xe08(%rip),%rdx
lea    0xda3(%rip),%rcx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
mov    %r15d,%r8d
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    (%r14),%esi
mov    %rbx,%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     12ea <do_export+0x14a>
mov    %eax,%r14d
lea    0xdef(%rip),%rsi
xor    %ebp,%ebp
mov    %eax,%edi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1090 <pwrite@plt>
test   %rax,%rax
js     1321 <do_export+0x181>
mov    %r14d,%edi
call   1060 <close@plt>
jmp    11ea <do_export+0x4a>
mov    0x2d26(%rip),%rax
mov    (%rax),%ebx
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd64(%rip),%rsi
mov    %ebx,%edi
mov    %r15d,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    11ea <do_export+0x4a>
mov    0x2cef(%rip),%rax
mov    (%rax),%ebx
mov    0x2cde(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd6b(%rip),%rsi
mov    %rsp,%rdx
mov    %ebx,%edi
mov    %rax,%rcx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    11ea <do_export+0x4a>
mov    0x2cb8(%rip),%rax
mov    (%rax),%ebx
mov    0x2ca7(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd45(%rip),%rsi
mov    %ebx,%edi
mov    %r15d,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %r14d,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebp
jmp    11ea <do_export+0x4a>
lea    0xcb4(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%ebp
jmp    11ea <do_export+0x4a>

LLVM IR (-O0):
@.str = [6 x i8] c"%s%d/\00"
@.str.1 = [21 x i8] c"/sys/class/gpio/gpio\00"
@.str.2 = [23 x i8] c"/sys/class/gpio/export\00"
@O_WRONLY = global i32 0
@.str.3 = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.4 = [47 x i8] c"Unable to export pin=%d (already in use?): %s\0A\00"
@errno = global i32 0
@.str.5 = [15 x i8] c"%s%d/direction\00"
@.str.6 = [13 x i8] c"Open %s: %s\0A\00"
@.str.7 = [4 x i8] c"out\00"
@.str.8 = [51 x i8] c"Unable to pwrite to gpio direction for pin %d: %s\0A\00"
define i32 @do_export(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [64 x i8]
%6 = alloca i32*
store i32 %0, i32* %3
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%8 = load i32, i32* %3
%9 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %7, i32 64, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%11 = call i32* @opendir(i8* %10)
store i32* %11, i32** %6
%12 = load i32*, i32** %6
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = load i32*, i32** %6
%16 = call i32 @closedir(i32* %15)
store i32 -2, i32* %2
br label %73
17: ; preds = %1
%18 = load i32, i32* @O_WRONLY
%19 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %18)
store i32 %19, i32* %4
%20 = load i32, i32* %4
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %73
24: ; preds = %17
%25 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%26 = load i32, i32* %3
%27 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %25, i32 64, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %26)
%28 = load i32, i32* %4
%29 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%30 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%31 = call i32 @strlen(i8* %30)
%32 = call i64 @pwrite(i32 %28, i8* %29, i32 %31, i32 0)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %24
%35 = load i32, i32* @stderr
%36 = load i32, i32* %3
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i32 %36, i8* %38)
store i32 -1, i32* %2
br label %73
40: ; preds = %24
%41 = load i32, i32* %4
%42 = call i32 @close(i32 %41)
%43 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%44 = load i32, i32* %3
%45 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %43, i32 64, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %44)
%46 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%47 = load i32, i32* @O_WRONLY
%48 = call i32 @open(i8* %46, i32 %47)
store i32 %48, i32* %4
%49 = load i32, i32* %4
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %57
51: ; preds = %40
%52 = load i32, i32* @stderr
%53 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%54 = load i32, i32* @errno
%55 = call i8* @strerror(i32 %54)
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i8* %53, i8* %55)
store i32 -1, i32* %2
br label %73
57: ; preds = %40
%58 = load i32, i32* %4
%59 = call i64 @pwrite(i32 %58, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i32 4, i32 0)
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %69
61: ; preds = %57
%62 = load i32, i32* @stderr
%63 = load i32, i32* %3
%64 = load i32, i32* @errno
%65 = call i8* @strerror(i32 %64)
%66 = call i32 (i32, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.8, i64 0, i64 0), i32 %63, i8* %65)
%67 = load i32, i32* %4
%68 = call i32 @close(i32 %67)
store i32 -1, i32* %2
br label %73
69: ; preds = %57
%70 = load i32, i32* %4
%71 = call i32 @close(i32 %70)
br label %72
72: ; preds = %69
store i32 0, i32* %2
br label %73
73: ; preds = %72, %61, %51, %34, %22, %14
%74 = load i32, i32* %2
ret i32 %74
}

LLVM IR (-O2):
@.str = [6 x i8] c"%s%d/\00"
@.str.1 = [21 x i8] c"/sys/class/gpio/gpio\00"
@.str.2 = [23 x i8] c"/sys/class/gpio/export\00"
@O_WRONLY = global i32 0
@.str.3 = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.4 = [47 x i8] c"Unable to export pin=%d (already in use?): %s\0A\00"
@errno = global i32 0
@.str.5 = [15 x i8] c"%s%d/direction\00"
@.str.6 = [13 x i8] c"Open %s: %s\0A\00"
@.str.7 = [4 x i8] c"out\00"
@.str.8 = [51 x i8] c"Unable to pwrite to gpio direction for pin %d: %s\0A\00"
define i32 @do_export(i32 %0) {
%2 = alloca [64 x i8]
%3 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%4 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %3, i32 64, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%5 = call i32* @opendir(i8* %3)
%6 = icmp eq i32* %5, null
br i1 %6, label %9, label %7
7: ; preds = %1
%8 = call i32 @closedir(i32* %5)
br label %47
9: ; preds = %1
%10 = load i32, i32* @O_WRONLY
%11 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %47
15: ; preds = %9
%16 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %3, i32 64, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %0)
%17 = call i32 @strlen(i8* %3)
%18 = call i64 @pwrite(i32 %11, i8* %3, i32 %17, i32 0)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i32 %0, i8* %23)
br label %47
25: ; preds = %15
%26 = call i32 @close(i32 %11)
%27 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %3, i32 64, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%28 = load i32, i32* @O_WRONLY
%29 = call i32 @open(i8* %3, i32 %28)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %25
%32 = load i32, i32* @stderr
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i8* %3, i8* %34)
br label %47
36: ; preds = %25
%37 = call i64 @pwrite(i32 %29, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i32 4, i32 0)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %45
39: ; preds = %36
%40 = load i32, i32* @stderr
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.8, i64 0, i64 0), i32 %0, i8* %42)
%44 = call i32 @close(i32 %29)
br label %47
45: ; preds = %36
%46 = call i32 @close(i32 %29)
br label %47
47: ; preds = %45, %39, %31, %20, %13, %7
%48 = phi i32 [ -2, %7 ], [ -1, %13 ], [ -1, %20 ], [ -1, %31 ], [ -1, %39 ], [ 0, %45 ]
ret i32 %48
}