#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 */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
int write(int, char const *, int);
int GPIOWrite(int pin, int value) {
  static const char s_values_str[] = "01";
  char path[30];
  int fd;
  snprintf(path, 30, "/sys/class/gpio/gpio%d/value", pin);
  fd = open(path, O_WRONLY);
  if (-1 == fd) {
    fprintf(stderr, "Failed to open %s for writing!\n", path);
    return (-1);
  }
  if (1 != write(fd, &s_values_str[0 == value ? 0 : 1], 1)) {
    fprintf(stderr, "Failed to write %s\n", path);
    return (-1);
  }
  close(fd);
  return (0);
}


x86 assembly (clang, -O0):
<GPIOWrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x1e,%esi
lea    0xe92(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x34(%rbp)
mov    $0xffffffff,%eax
cmp    -0x34(%rbp),%eax
jne    11be <GPIOWrite+0x6e>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rdx
lea    0xe73(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1229 <GPIOWrite+0xd9>
mov    -0x34(%rbp),%edi
mov    -0xc(%rbp),%edx
xor    %ecx,%ecx
mov    $0x1,%eax
cmp    %edx,%ecx
cmove  %ecx,%eax
cltq
lea    0xe27(%rip),%rsi
add    %rax,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     121a <GPIOWrite+0xca>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rdx
lea    0xe37(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1229 <GPIOWrite+0xd9>
mov    -0x34(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GPIOWrite>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ebx
mov    %edi,%ecx
lea    0xea2(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x1e,%esi
call   1040 <snprintf@plt>
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11b3 <GPIOWrite+0x63>
mov    %eax,%ebp
lea    0xe70(%rip),%rsi
cmp    $0x1,%ebx
sbb    $0xffffffffffffffff,%rsi
mov    %eax,%edi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
jne    11c5 <GPIOWrite+0x75>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    11e2 <GPIOWrite+0x92>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
lea    0xe5d(%rip),%rsi
jmp    11d5 <GPIOWrite+0x85>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
lea    0xe6b(%rip),%rsi
mov    %rsp,%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@GPIOWrite.s_values_str = internal constant [3 x i8] c"01\00"
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str.1 = [32 x i8] c"Failed to open %s for writing!\0A\00"
@.str.2 = [20 x i8] c"Failed to write %s\0A\00"
define i32 @GPIOWrite(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [30 x i8]
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 0
%9 = load i32, i32* %4
%10 = call i32 @snprintf(i8* %8, i32 30, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 0
%12 = load i32, i32* @O_WRONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp eq i32 -1, %14
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 0
%19 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %18)
store i32 -1, i32* %3
br label %37
20: ; preds = %2
%21 = load i32, i32* %7
%22 = load i32, i32* %5
%23 = icmp eq i32 0, %22
%24 = zext i1 %23 to i64
%25 = select i1 %23, i32 0, i32 1
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [3 x i8], [3 x i8]* @GPIOWrite.s_values_str, i64 0, i64 %26
%28 = call i32 @write(i32 %21, i8* %27, i32 1)
%29 = icmp ne i32 1, %28
br i1 %29, label %30, label %34
30: ; preds = %20
%31 = load i32, i32* @stderr
%32 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 0
%33 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %32)
store i32 -1, i32* %3
br label %37
34: ; preds = %20
%35 = load i32, i32* %7
%36 = call i32 @close(i32 %35)
store i32 0, i32* %3
br label %37
37: ; preds = %34, %30, %16
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
@GPIOWrite.s_values_str = internal constant [3 x i8] c"01\00"
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str.1 = [32 x i8] c"Failed to open %s for writing!\0A\00"
@.str.2 = [20 x i8] c"Failed to write %s\0A\00"
define i32 @GPIOWrite(i32 %0, i32 %1) {
%3 = alloca [30 x i8]
%4 = getelementptr inbounds [30 x i8], [30 x i8]* %3, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 30, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = load i32, i32* @O_WRONLY
%7 = call i32 @open(i8* %4, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %4)
br label %23
12: ; preds = %2
%13 = icmp ne i32 %1, 0
%14 = zext i1 %13 to i64
%15 = getelementptr inbounds [3 x i8], [3 x i8]* @GPIOWrite.s_values_str, i64 0, i64 %14
%16 = call i32 @write(i32 %7, i8* %15, i32 1)
%17 = icmp eq i32 %16, 1
br i1 %17, label %21, label %18
18: ; preds = %12
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %4)
br label %23
21: ; preds = %12
%22 = call i32 @close(i32 %7)
br label %23
23: ; preds = %21, %18, %9
%24 = phi i32 [ -1, %9 ], [ -1, %18 ], [ 0, %21 ]
ret i32 %24
}