#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 */

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int open(char *, int /*<<< orphan*/);
int snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int gpio_pin_state(int pin, unsigned char state) {
  char str[3];
  int fd;
open_it:
  if (state == 1)
    fd = open("/sys/class/gpio/export", O_WRONLY);
  else
    fd = open("/sys/class/gpio/unexport", O_WRONLY);
  if (fd == -1) {
    if (errno == EINTR)
      goto open_it;
    goto error;
  }
  int n = snprintf(str, 3, "%d", pin);
  if (n < 0)
    goto error;
  int err = write(fd, str, n);
  if (err == -1)
    goto error;
  close(fd);
  return 0;
error:
  fprintf(stderr, "ERROR: could not active PIN -> %d\n", pin);
  return -1;
}


x86 assembly (clang, -O0):
<gpio_pin_state>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %edi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
movzbl -0x9(%rbp),%eax
cmp    $0x1,%eax
jne    118b <gpio_pin_state+0x3b>
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
lea    0xe82(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x10(%rbp)
jmp    11a3 <gpio_pin_state+0x53>
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
lea    0xe7c(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11d1 <gpio_pin_state+0x81>
mov    0x2e24(%rip),%rax
mov    (%rax),%rax
mov    0x2e12(%rip),%rcx
cmp    (%rcx),%rax
jne    11cc <gpio_pin_state+0x7c>
jmp    1161 <gpio_pin_state+0x11>
jmp    1230 <gpio_pin_state+0xe0>
lea    -0xc(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x3,%esi
lea    0xe4c(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11fb <gpio_pin_state+0xab>
jmp    1230 <gpio_pin_state+0xe0>
mov    -0x10(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    121c <gpio_pin_state+0xcc>
jmp    1230 <gpio_pin_state+0xe0>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    124f <gpio_pin_state+0xff>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xdf0(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_pin_state>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x1,%sil
lea    0xe9a(%rip),%rax
lea    0xeaa(%rip),%rbx
cmove  %rax,%rbx
mov    %edi,%r14d
mov    0x2e45(%rip),%r15
mov    0x2e56(%rip),%r12
mov    0x2e47(%rip),%r13
nopl   0x0(%rax)
mov    (%r15),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
jne    11ac <gpio_pin_state+0x5c>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1190 <gpio_pin_state+0x40>
jmp    11e9 <gpio_pin_state+0x99>
mov    %eax,%ebp
lea    0xe7b(%rip),%rdx
lea    0x5(%rsp),%rdi
mov    $0x3,%esi
mov    %r14d,%ecx
call   1040 <snprintf@plt>
test   %eax,%eax
js     11e9 <gpio_pin_state+0x99>
lea    0x5(%rsp),%rsi
mov    %ebp,%edi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     11e9 <gpio_pin_state+0x99>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    1206 <gpio_pin_state+0xb6>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
lea    0xe3a(%rip),%rsi
mov    %r14d,%edx
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"/sys/class/gpio/export\00"
@O_WRONLY = global i32 0
@.str.1 = [25 x i8] c"/sys/class/gpio/unexport\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.2 = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.3 = [35 x i8] c"ERROR: could not active PIN -> %d\0A\00"
define i32 @gpio_pin_state(i32 %0, i8 zeroext %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
%6 = alloca [3 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i8 %1, i8* %5
br label %10
10: ; preds = %27, %2
%11 = load i8, i8* %5
%12 = zext i8 %11 to i32
%13 = icmp eq i32 %12, 1
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = load i32, i32* @O_WRONLY
%16 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %15)
store i32 %16, i32* %7
br label %20
17: ; preds = %10
%18 = load i32, i32* @O_WRONLY
%19 = call i32 @open(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %18)
store i32 %19, i32* %7
br label %20
20: ; preds = %17, %14
%21 = load i32, i32* %7
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %29
23: ; preds = %20
%24 = load i64, i64* @errno
%25 = load i64, i64* @EINTR
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %23
br label %10
28: ; preds = %23
br label %47
29: ; preds = %20
%30 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 0
%31 = load i32, i32* %4
%32 = call i32 @snprintf(i8* %30, i32 3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %31)
store i32 %32, i32* %8
%33 = load i32, i32* %8
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %29
br label %47
36: ; preds = %29
%37 = load i32, i32* %7
%38 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 0
%39 = load i32, i32* %8
%40 = call i32 @write(i32 %37, i8* %38, i32 %39)
store i32 %40, i32* %9
%41 = load i32, i32* %9
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %44
43: ; preds = %36
br label %47
44: ; preds = %36
%45 = load i32, i32* %7
%46 = call i32 @close(i32 %45)
store i32 0, i32* %3
br label %51
47: ; preds = %43, %35, %28
%48 = load i32, i32* @stderr
%49 = load i32, i32* %4
%50 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %49)
store i32 -1, i32* %3
br label %51
51: ; preds = %47, %44
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
@.str = [23 x i8] c"/sys/class/gpio/export\00"
@O_WRONLY = global i32 0
@.str.1 = [25 x i8] c"/sys/class/gpio/unexport\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.2 = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.3 = [35 x i8] c"ERROR: could not active PIN -> %d\0A\00"
define i32 @gpio_pin_state(i32 %0, i8 zeroext %1) {
%3 = alloca [3 x i8]
%4 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%5 = icmp eq i8 %1, 1
%6 = select i1 %5, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0)
br label %7
7: ; preds = %11, %2
%8 = load i32, i32* @O_WRONLY
%9 = tail call i32 @open(i8* %6, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %7
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
br i1 %14, label %7, label %23
15: ; preds = %7
%16 = call i32 @snprintf(i8* %4, i32 3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %0)
%17 = icmp slt i32 %16, 0
br i1 %17, label %23, label %18
18: ; preds = %15
%19 = call i32 @write(i32 %9, i8* %4, i32 %16)
%20 = icmp eq i32 %19, -1
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = call i32 @close(i32 %9)
br label %26
23: ; preds = %11, %18, %15
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %0)
br label %26
26: ; preds = %23, %21
%27 = phi i32 [ -1, %23 ], [ 0, %21 ]
ret i32 %27
}