#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
int GPIO_Write(int pin, int value) {
  if ((value != 0) && (value != 1)) {
    fprintf(stderr, "ERROR: Invalid value!\nValue must be 0 or 1\n");
    return -1;
  }
  FILE *sysfs_handle = NULL;
  char str_value_file[64];
  snprintf(str_value_file, (64 * sizeof(char)), "/sys/class/gpio/gpio%d/value",
           pin);
  if ((sysfs_handle = fopen(str_value_file, "w")) == NULL) {
    fprintf(stderr,
            "ERROR: Cannot open value file for pin %d...\n(Has the pin been "
            "exported?)\n",
            pin);
    return 1;
  }
  char str_val[2];
  snprintf(str_val, (2 * sizeof(char)), "%d", value);
  if (fwrite(str_val, sizeof(char), 2, sysfs_handle) != 2) {
    fprintf(stderr, "ERROR: Cannot write value %d to GPIO pin %d\n", value,
            pin);
    return 2;
  }
  fclose(sysfs_handle);
  return 0;
}


x86 assembly (clang, -O0):
<GPIO_Write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1195 <GPIO_Write+0x45>
cmpl   $0x1,-0xc(%rbp)
je     1195 <GPIO_Write+0x45>
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
lea    0xe7e(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126a <GPIO_Write+0x11a>
movq   $0x0,-0x18(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x40,%esi
lea    0xe7c(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x60(%rbp),%rdi
lea    0xe89(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11f9 <GPIO_Write+0xa9>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe65(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    126a <GPIO_Write+0x11a>
lea    -0x62(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0x2,%esi
lea    0xe89(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x62(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x2,%edx
call   1070 <fwrite@plt>
cmp    $0x2,%eax
je     125a <GPIO_Write+0x10a>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
mov    -0x8(%rbp),%ecx
lea    0xe51(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    126a <GPIO_Write+0x11a>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GPIO_Write>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x50,%rsp
cmp    $0x2,%esi
jae    11df <GPIO_Write+0x8f>
mov    %esi,%ebp
mov    %edi,%r14d
lea    0xebf(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
mov    %r14d,%ecx
call   1040 <snprintf@plt>
lea    0xec0(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11fd <GPIO_Write+0xad>
mov    %rax,%r15
lea    0xef5(%rip),%rdx
lea    0xe(%rsp),%r12
mov    $0x2,%ebx
mov    %r12,%rdi
mov    $0x2,%esi
mov    %ebp,%ecx
call   1040 <snprintf@plt>
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x2,%edx
mov    %r15,%rcx
call   1070 <fwrite@plt>
cmp    $0x2,%eax
jne    121e <GPIO_Write+0xce>
mov    %r15,%rdi
call   1030 <fclose@plt>
xor    %ebx,%ebx
jmp    123a <GPIO_Write+0xea>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
lea    0xe11(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    123a <GPIO_Write+0xea>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
lea    0xe3e(%rip),%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%ebx
jmp    123a <GPIO_Write+0xea>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
lea    0xe6a(%rip),%rsi
mov    %ebp,%edx
mov    %r14d,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebx,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [44 x i8] c"ERROR: Invalid value!\0AValue must be 0 or 1\0A\00"
@.str.1 = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [74 x i8] c"ERROR: Cannot open value file for pin %d...\0A(Has the pin been exported?)\0A\00"
@.str.4 = [3 x i8] c"%d\00"
@.str.5 = [45 x i8] c"ERROR: Cannot write value %d to GPIO pin %d\0A\00"
define i32 @GPIO_Write(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca [64 x i8]
%8 = alloca [2 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = load i32, i32* %5
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = load i32, i32* %5
%13 = icmp ne i32 %12, 1
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %44
17: ; preds = %11, %2
store i32* null, i32** %6
%18 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%19 = load i32, i32* %4
%20 = call i32 @snprintf(i8* %18, i32 64, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %22, i32** %6
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %28
24: ; preds = %17
%25 = load i32, i32* @stderr
%26 = load i32, i32* %4
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str.3, i64 0, i64 0), i32 %26)
store i32 1, i32* %3
br label %44
28: ; preds = %17
%29 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 0
%30 = load i32, i32* %5
%31 = call i32 @snprintf(i8* %29, i32 2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %30)
%32 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 0
%33 = load i32*, i32** %6
%34 = call i32 @fwrite(i8* %32, i32 1, i32 2, i32* %33)
%35 = icmp ne i32 %34, 2
br i1 %35, label %36, label %41
36: ; preds = %28
%37 = load i32, i32* @stderr
%38 = load i32, i32* %5
%39 = load i32, i32* %4
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0), i32 %38, i32 %39)
store i32 2, i32* %3
br label %44
41: ; preds = %28
%42 = load i32*, i32** %6
%43 = call i32 @fclose(i32* %42)
store i32 0, i32* %3
br label %44
44: ; preds = %41, %36, %24, %14
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [44 x i8] c"ERROR: Invalid value!\0AValue must be 0 or 1\0A\00"
@.str.1 = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [74 x i8] c"ERROR: Cannot open value file for pin %d...\0A(Has the pin been exported?)\0A\00"
@.str.4 = [3 x i8] c"%d\00"
@.str.5 = [45 x i8] c"ERROR: Cannot write value %d to GPIO pin %d\0A\00"
define i32 @GPIO_Write(i32 %0, i32 %1) {
%3 = alloca [64 x i8]
%4 = alloca [2 x i8]
%5 = icmp ult i32 %1, 2
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
br label %31
9: ; preds = %2
%10 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%11 = call i32 @snprintf(i8* %10, i32 64, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%12 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str.3, i64 0, i64 0), i32 %0)
br label %29
17: ; preds = %9
%18 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%19 = call i32 @snprintf(i8* %18, i32 2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %1)
%20 = call i32 @fwrite(i8* %18, i32 1, i32 2, i32* %12)
%21 = icmp eq i32 %20, 2
br i1 %21, label %25, label %22
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0), i32 %1, i32 %0)
br label %27
25: ; preds = %17
%26 = call i32 @fclose(i32* %12)
br label %27
27: ; preds = %25, %22
%28 = phi i32 [ 2, %22 ], [ 0, %25 ]
br label %29
29: ; preds = %27, %14
%30 = phi i32 [ 1, %14 ], [ %28, %27 ]
br label %31
31: ; preds = %29, %6
%32 = phi i32 [ -1, %6 ], [ %30, %29 ]
ret i32 %32
}