#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 /*<<< orphan*/ usleep(int);
int write(int, char *, int const);
int gpio_export(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/export", O_WRONLY);
    if (fd == -1) {
      fprintf(stderr, "Failed top open /sys/class/gpio/export!\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/export! Error %d.\n",
              errno);
      close(fd);
      return -1;
    }
    result = close(fd);
    if (result != 0) {
      fprintf(stderr, "Failed closing /sys/class/gpio/export! Error %d.\n",
              errno);
      return -1;
    }
    usleep(100000);
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<gpio_export>:
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    11d2 <gpio_export+0x42>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe41(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1321 <gpio_export+0x191>
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x40,%esi
lea    0xe33(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x50(%rbp),%rdi
lea    -0x58(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
je     131a <gpio_export+0x18a>
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
lea    0xe1e(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0xffffffff,-0x5c(%rbp)
jne    1246 <gpio_export+0xb6>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
lea    0xe13(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1321 <gpio_export+0x191>
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x3,%esi
lea    0xdd3(%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     12c3 <gpio_export+0x133>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
mov    0x2d39(%rip),%rax
mov    (%rax),%edx
lea    0xdc7(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x5c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1321 <gpio_export+0x191>
mov    -0x5c(%rbp),%edi
call   1060 <close@plt>
mov    %eax,-0x68(%rbp)
cmpl   $0x0,-0x68(%rbp)
je     1304 <gpio_export+0x174>
mov    0x2d01(%rip),%rax
mov    (%rax),%edi
mov    0x2cf0(%rip),%rax
mov    (%rax),%edx
lea    0xdb3(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1321 <gpio_export+0x191>
mov    $0x186a0,%edi
call   10b0 <usleep@plt>
movl   $0x1,-0x4(%rbp)
jmp    1321 <gpio_export+0x191>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_export>:
push   %rbp
push   %r14
push   %rbx
sub    $0x50,%rsp
mov    %edi,%ebx
call   1090 <gpio_is_pin_valid@plt>
test   %eax,%eax
je     125f <gpio_export+0xcf>
lea    0xe6a(%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>
test   %rax,%rax
je     127f <gpio_export+0xef>
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
lea    0xe47(%rip),%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     1283 <gpio_export+0xf3>
mov    %eax,%ebp
lea    0xe2d(%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    12a1 <gpio_export+0x111>
mov    %ebp,%edi
call   1060 <close@plt>
test   %eax,%eax
je     12cf <gpio_export+0x13f>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
mov    0x2d84(%rip),%rax
mov    (%rax),%edx
lea    0xe47(%rip),%rsi
jmp    1271 <gpio_export+0xe1>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
lea    0xd91(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    12de <gpio_export+0x14e>
xor    %eax,%eax
jmp    12de <gpio_export+0x14e>
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
lea    0xdb3(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    12de <gpio_export+0x14e>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
mov    0x2d27(%rip),%rax
mov    (%rax),%edx
lea    0xdb5(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0xffffffff,%eax
jmp    12de <gpio_export+0x14e>
mov    $0x186a0,%edi
call   10b0 <usleep@plt>
mov    $0x1,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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 = [23 x i8] c"/sys/class/gpio/export\00"
@O_WRONLY = global i32 0
@.str.3 = [41 x i8] c"Failed top open /sys/class/gpio/export!\0A\00"
@.str.4 = [3 x i8] c"%d\00"
@.str.5 = [53 x i8] c"Failed writing to /sys/class/gpio/export! Error %d.\0A\00"
@errno = global i32 0
@.str.6 = [50 x i8] c"Failed closing /sys/class/gpio/export! Error %d.\0A\00"
define i32 @gpio_export(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 %65
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 ne i64 %22, 0
br i1 %23, label %24, label %64
24: ; preds = %17
%25 = load i32, i32* @O_WRONLY
%26 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 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 ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %65
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 ([53 x i8], [53 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 %65
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 ([50 x i8], [50 x i8]* @.str.6, i64 0, i64 0), i32 %60)
store i32 -1, i32* %2
br label %65
62: ; preds = %53
%63 = call i32 @usleep(i32 100000)
store i32 1, i32* %2
br label %65
64: ; preds = %17
store i32 0, i32* %2
br label %65
65: ; preds = %64, %62, %58, %47, %29, %13
%66 = load i32, i32* %2
ret i32 %66
}

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 = [23 x i8] c"/sys/class/gpio/export\00"
@O_WRONLY = global i32 0
@.str.3 = [41 x i8] c"Failed top open /sys/class/gpio/export!\0A\00"
@.str.4 = [3 x i8] c"%d\00"
@.str.5 = [53 x i8] c"Failed writing to /sys/class/gpio/export! Error %d.\0A\00"
@errno = global i32 0
@.str.6 = [50 x i8] c"Failed closing /sys/class/gpio/export! Error %d.\0A\00"
define i32 @gpio_export(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 %49
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 %47, label %16
16: ; preds = %10
%17 = load i32, i32* @O_WRONLY
%18 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 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 ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
br label %47
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 ([53 x i8], [53 x i8]* @.str.5, i64 0, i64 0), i32 %33)
%35 = call i32 @close(i32 %18)
br label %45
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 ([50 x i8], [50 x i8]* @.str.6, i64 0, i64 0), i32 %41)
br label %45
43: ; preds = %36
%44 = call i32 @usleep(i32 100000)
br label %45
45: ; preds = %43, %39, %31
%46 = phi i32 [ -1, %31 ], [ -1, %39 ], [ 1, %43 ]
br label %47
47: ; preds = %10, %20, %45
%48 = phi i32 [ -1, %20 ], [ %46, %45 ], [ 0, %10 ]
br label %49
49: ; preds = %47, %7
%50 = phi i32 [ %48, %47 ], [ -1, %7 ]
ret i32 %50
}