#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*/ F_OK;
int O_TRUNC;
int O_WRONLY;
int access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ write(int, char *, int);
int set_GPIO(int pin, int val) {
  char pinDir[36];
  int fd;
  snprintf(pinDir, sizeof pinDir, "/sys/class/gpio/gpio%d/", pin);
  if (access(pinDir, F_OK) != -1) {
    char opt[64];
    strcpy(opt, pinDir);
    strcat(opt, "value");
    if ((fd = open(opt, O_WRONLY | O_TRUNC)) == -1) {
      printf("Error opening gpio/gpio%d/value\n", pin);
      exit(-1);
    }
    if (val == 1) {
      if (write(fd, (char *)"1", 3) < 0) {
        printf("Error setting pin %d to OUT\n", pin);
        exit(-1);
      }
    } else if (val == 0) {
      if (write(fd, (char *)"0", 3) < 0) {
        printf("Error setting pin %d to OUT\n", pin);
        exit(-1);
      }
    } else {
      printf("Error: value %d out of range\n", val);
      return (-1);
    }
  } else {
    printf("Error GPIO pin %d has not been initialized\n", pin);
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_GPIO>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x24,%esi
lea    0xe5c(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
cmp    $0xffffffff,%eax
je     12d2 <set_GPIO+0x152>
lea    -0x80(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x80(%rbp),%rdi
lea    0xe3c(%rip),%rsi
call   1090 <strcat@plt>
lea    -0x80(%rbp),%rdi
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
mov    0x2e03(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
jne    1221 <set_GPIO+0xa1>
mov    -0x8(%rbp),%esi
lea    0xe0c(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
cmpl   $0x1,-0xc(%rbp)
jne    1267 <set_GPIO+0xe7>
mov    -0x34(%rbp),%edi
lea    0xe0a(%rip),%rsi
mov    $0x3,%edx
call   1040 <write@plt>
cmp    $0x0,%rax
jge    1262 <set_GPIO+0xe2>
mov    -0x8(%rbp),%esi
lea    0xdee(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
jmp    12cd <set_GPIO+0x14d>
cmpl   $0x0,-0xc(%rbp)
jne    12ad <set_GPIO+0x12d>
mov    -0x34(%rbp),%edi
lea    0xde3(%rip),%rsi
mov    $0x3,%edx
call   1040 <write@plt>
cmp    $0x0,%rax
jge    12a8 <set_GPIO+0x128>
mov    -0x8(%rbp),%esi
lea    0xda8(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
jmp    12c8 <set_GPIO+0x148>
mov    -0xc(%rbp),%esi
lea    0xda9(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e8 <set_GPIO+0x168>
jmp    12cd <set_GPIO+0x14d>
jmp    12e1 <set_GPIO+0x161>
mov    -0x8(%rbp),%esi
lea    0xda2(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_GPIO>:
push   %rbp
push   %r14
push   %rbx
sub    $0x70,%rsp
mov    %esi,%ebp
mov    %edi,%r14d
lea    0xe6c(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x24,%esi
mov    %r14d,%ecx
call   1060 <snprintf@plt>
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <access@plt>
cmp    $0xffffffff,%eax
je     120d <set_GPIO+0x8d>
lea    0x30(%rsp),%rbx
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
lea    0xe44(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcat@plt>
mov    0x2ded(%rip),%rax
mov    0x2e0e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1256 <set_GPIO+0xd6>
test   %ebp,%ebp
je     121e <set_GPIO+0x9e>
cmp    $0x1,%ebp
jne    123a <set_GPIO+0xba>
lea    0xe34(%rip),%rsi
jmp    1225 <set_GPIO+0xa5>
lea    0xe6a(%rip),%rdi
mov    %r14d,%esi
call   1050 <printf@plt>
jmp    1236 <set_GPIO+0xb6>
lea    0xe39(%rip),%rsi
mov    %eax,%edi
mov    $0x3,%edx
call   1040 <write@plt>
test   %rax,%rax
js     125f <set_GPIO+0xdf>
xor    %eax,%eax
jmp    124d <set_GPIO+0xcd>
lea    0xe1f(%rip),%rdi
mov    %ebp,%esi
call   1050 <printf@plt>
mov    $0xffffffff,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdc1(%rip),%rdi
jmp    1266 <set_GPIO+0xe6>
lea    0xddb(%rip),%rdi
mov    %r14d,%esi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"/sys/class/gpio/gpio%d/\00"
@F_OK = global i32 0
@.str.1 = [6 x i8] c"value\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [33 x i8] c"Error opening gpio/gpio%d/value\0A\00"
@.str.3 = [2 x i8] c"1\00"
@.str.4 = [29 x i8] c"Error setting pin %d to OUT\0A\00"
@.str.5 = [2 x i8] c"0\00"
@.str.6 = [30 x i8] c"Error: value %d out of range\0A\00"
@.str.7 = [44 x i8] c"Error GPIO pin %d has not been initialized\0A\00"
define i32 @set_GPIO(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [36 x i8]
%7 = alloca i32
%8 = alloca [64 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = getelementptr inbounds [36 x i8], [36 x i8]* %6, i64 0, i64 0
%10 = load i32, i32* %4
%11 = call i32 @snprintf(i8* %9, i32 36, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [36 x i8], [36 x i8]* %6, i64 0, i64 0
%13 = load i32, i32* @F_OK
%14 = call i32 @access(i8* %12, i32 %13)
%15 = icmp ne i32 %14, -1
br i1 %15, label %16, label %61
16: ; preds = %2
%17 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%18 = getelementptr inbounds [36 x i8], [36 x i8]* %6, i64 0, i64 0
%19 = call i32 @strcpy(i8* %17, i8* %18)
%20 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%21 = call i32 @strcat(i8* %20, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%22 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%23 = load i32, i32* @O_WRONLY
%24 = load i32, i32* @O_TRUNC
%25 = or i32 %23, %24
%26 = call i32 @open(i8* %22, i32 %25)
store i32 %26, i32* %7
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %32
28: ; preds = %16
%29 = load i32, i32* %4
%30 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %29)
%31 = call i32 @exit(i32 -1)
unreachable
32: ; preds = %16
%33 = load i32, i32* %5
%34 = icmp eq i32 %33, 1
br i1 %34, label %35, label %44
35: ; preds = %32
%36 = load i32, i32* %7
%37 = call i64 @write(i32 %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 3)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %35
%40 = load i32, i32* %4
%41 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %40)
%42 = call i32 @exit(i32 -1)
unreachable
43: ; preds = %35
br label %60
44: ; preds = %32
%45 = load i32, i32* %5
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %56
47: ; preds = %44
%48 = load i32, i32* %7
%49 = call i64 @write(i32 %48, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 3)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %47
%52 = load i32, i32* %4
%53 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %52)
%54 = call i32 @exit(i32 -1)
unreachable
55: ; preds = %47
br label %59
56: ; preds = %44
%57 = load i32, i32* %5
%58 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0), i32 %57)
store i32 -1, i32* %3
br label %65
59: ; preds = %55
br label %60
60: ; preds = %59, %43
br label %64
61: ; preds = %2
%62 = load i32, i32* %4
%63 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.7, i64 0, i64 0), i32 %62)
br label %64
64: ; preds = %61, %60
store i32 0, i32* %3
br label %65
65: ; preds = %64, %56
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
@.str = [24 x i8] c"/sys/class/gpio/gpio%d/\00"
@F_OK = global i32 0
@.str.1 = [6 x i8] c"value\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [33 x i8] c"Error opening gpio/gpio%d/value\0A\00"
@.str.3 = [2 x i8] c"1\00"
@.str.4 = [29 x i8] c"Error setting pin %d to OUT\0A\00"
@.str.5 = [2 x i8] c"0\00"
@.str.6 = [30 x i8] c"Error: value %d out of range\0A\00"
@.str.7 = [44 x i8] c"Error GPIO pin %d has not been initialized\0A\00"
define i32 @set_GPIO(i32 %0, i32 %1) {
%3 = alloca [36 x i8]
%4 = alloca [64 x i8]
%5 = getelementptr inbounds [36 x i8], [36 x i8]* %3, i64 0, i64 0
%6 = call i32 @snprintf(i8* %5, i32 36, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = load i32, i32* @F_OK
%8 = call i32 @access(i8* %5, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %38, label %10
10: ; preds = %2
%11 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%12 = call i32 @strcpy(i8* %11, i8* %5)
%13 = call i32 @strcat(i8* %11, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* @O_WRONLY
%15 = load i32, i32* @O_TRUNC
%16 = or i32 %15, %14
%17 = call i32 @open(i8* %11, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %10
%20 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %0)
%21 = call i32 @exit(i32 -1)
unreachable
22: ; preds = %10
switch i32 %1, label %35 [
i32 1, label %23
i32 0, label %29
]
23: ; preds = %22
%24 = call i64 @write(i32 %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 3)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %37
26: ; preds = %23
%27 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %0)
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %22
%30 = call i64 @write(i32 %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 3)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %29
%33 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %0)
%34 = call i32 @exit(i32 -1)
unreachable
35: ; preds = %22
%36 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0), i32 %1)
br label %40
37: ; preds = %23, %29
br label %40
38: ; preds = %2
%39 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.7, i64 0, i64 0), i32 %0)
br label %40
40: ; preds = %38, %37, %35
%41 = phi i32 [ -1, %35 ], [ 0, %37 ], [ 0, %38 ]
ret i32 %41
}