#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 close(int);
scalar_t__ errno;
int open(char *, int /*<<< orphan*/);
scalar_t__ snprintf(char *, int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int set(int gpio, char state) {
  char fn[256];
  int rv = 0;
  if (snprintf(fn, 256, "/sys/class/gpio/gpio%i/value", gpio) < 0) {
    return -1;
  } else {
    int fd = open(fn, O_WRONLY);
    if (fd) {
      (void)write(fd, state ? "1\n" : "0\n", 2);
      do {
        rv = close(fd);
      } while ((rv < 0) && (errno == EINTR));
    }
  }
  return rv;
}


x86 assembly (clang, -O0):
<set>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %sil,%al
mov    %edi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
movl   $0x0,-0x114(%rbp)
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x100,%esi
lea    0xe8c(%rip),%rdx
call   1040 <snprintf@plt>
cmp    $0x0,%rax
jge    118f <set+0x4f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1244 <set+0x104>
lea    -0x110(%rbp),%rdi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x118(%rbp)
cmpl   $0x0,-0x118(%rbp)
je     1236 <set+0xf6>
mov    -0x118(%rbp),%edi
movsbl -0x9(%rbp),%ecx
lea    0xe58(%rip),%rsi
lea    0xe4e(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    -0x118(%rbp),%edi
call   1050 <close@plt>
mov    %eax,-0x114(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x114(%rbp)
mov    %al,-0x119(%rbp)
jge    1223 <set+0xe3>
mov    0x2dd3(%rip),%rax
mov    (%rax),%rax
mov    0x2dc1(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x119(%rbp)
mov    -0x119(%rbp),%al
test   $0x1,%al
jne    11e0 <set+0xa0>
jmp    1236 <set+0xf6>
jmp    123b <set+0xfb>
mov    -0x114(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %esi,%ebp
mov    %edi,%ecx
lea    0xeaa(%rip),%rdx
mov    %rsp,%rdi
mov    $0x100,%esi
call   1040 <snprintf@plt>
test   %rax,%rax
js     11c6 <set+0x86>
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1060 <open@plt>
test   %eax,%eax
je     11cd <set+0x8d>
mov    %eax,%ebx
test   %bpl,%bpl
lea    0xe97(%rip),%rax
lea    0xe8d(%rip),%rsi
cmove  %rax,%rsi
mov    %ebx,%edi
mov    $0x2,%edx
call   1030 <write@plt>
mov    0x2e39(%rip),%r14
mov    0x2e2a(%rip),%rbp
xchg   %ax,%ax
mov    %ebx,%edi
call   1050 <close@plt>
test   %eax,%eax
jns    11cf <set+0x8f>
mov    0x0(%rbp),%rcx
cmp    %rcx,(%r14)
je     11b0 <set+0x70>
jmp    11cf <set+0x8f>
mov    $0xffffffff,%eax
jmp    11cf <set+0x8f>
xor    %eax,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"/sys/class/gpio/gpio%i/value\00"
@O_WRONLY = global i32 0
@.str.1 = [3 x i8] c"1\0A\00"
@.str.2 = [3 x i8] c"0\0A\00"
@errno = global i64 0
@EINTR = global i64 0
define i32 @set(i32 %0, i8 signext %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
%6 = alloca [256 x i8]
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8 %1, i8* %5
store i32 0, i32* %7
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%10 = load i32, i32* %4
%11 = call i64 @snprintf(i8* %9, i32 256, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %44
14: ; preds = %2
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%16 = load i32, i32* @O_WRONLY
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %41
20: ; preds = %14
%21 = load i32, i32* %8
%22 = load i8, i8* %5
%23 = sext i8 %22 to i32
%24 = icmp ne i32 %23, 0
%25 = zext i1 %24 to i64
%26 = select i1 %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0)
%27 = call i32 @write(i32 %21, i8* %26, i32 2)
br label %28
28: ; preds = %38, %20
%29 = load i32, i32* %8
%30 = call i32 @close(i32 %29)
store i32 %30, i32* %7
br label %31
31: ; preds = %28
%32 = load i32, i32* %7
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %31
%35 = load i64, i64* @errno
%36 = load i64, i64* @EINTR
%37 = icmp eq i64 %35, %36
br label %38
38: ; preds = %34, %31
%39 = phi i1 [ false, %31 ], [ %37, %34 ]
br i1 %39, label %28, label %40
40: ; preds = %38
br label %41
41: ; preds = %40, %14
br label %42
42: ; preds = %41
%43 = load i32, i32* %7
store i32 %43, i32* %3
br label %44
44: ; preds = %42, %13
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
@.str = [29 x i8] c"/sys/class/gpio/gpio%i/value\00"
@O_WRONLY = global i32 0
@.str.1 = [3 x i8] c"1\0A\00"
@.str.2 = [3 x i8] c"0\0A\00"
@errno = global i64 0
@EINTR = global i64 0
define i32 @set(i32 %0, i8 signext %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = call i64 @snprintf(i8* %4, i32 256, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = icmp slt i64 %5, 0
br i1 %6, label %22, label %7
7: ; preds = %2
%8 = load i32, i32* @O_WRONLY
%9 = call i32 @open(i8* %4, i32 %8)
%10 = icmp eq i32 %9, 0
br i1 %10, label %22, label %11
11: ; preds = %7
%12 = icmp eq i8 %1, 0
%13 = select i1 %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0)
%14 = call i32 @write(i32 %9, i8* %13, i32 2)
br label %15
15: ; preds = %15, %11
%16 = call i32 @close(i32 %9)
%17 = icmp slt i32 %16, 0
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINTR
%20 = icmp eq i64 %18, %19
%21 = select i1 %17, i1 %20, i1 false
br i1 %21, label %15, label %22
22: ; preds = %15, %7, %2
%23 = phi i32 [ -1, %2 ], [ 0, %7 ], [ %16, %15 ]
ret i32 %23
}