#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*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ write(int, char *, int);
int init_GPIO(int pin, int direction) {
  char pinDir[36];
  int fd;
  snprintf(pinDir, sizeof pinDir, "/sys/class/gpio/gpio%d/", pin);
  if (access(pinDir, F_OK) != -1) {
    printf("Warning: GPIO pin %d has already been exported\n", pin);
  } else {
    if ((fd = open("/sys/class/gpio/export", O_WRONLY | O_TRUNC)) == -1) {
      printf("Error opening gpio/export\n");
      exit(-1);
    }
    char pinb[2];
    sprintf(pinb, "%d", pin);
    if (write(fd, (char *)pinb, 2) < 0) {
      printf("Error exporting pin %d\n", pin);
      exit(-1);
    }
    close(fd);
  }
  char opt[64];
  strcpy(opt, pinDir);
  strcat(opt, "direction");
  if ((fd = open(opt, O_WRONLY | O_TRUNC)) == -1) {
    printf("Error opening gpio/export\n");
    exit(-1);
  }
  if (direction == 0) {
    if (write(fd, (char *)"out", 3) < 0) {
      printf("Error setting pin %d to OUT\n", pin);
      exit(-1);
    }
  } else if (direction == 1) {
    if (write(fd, (char *)"in", 2) < 0) {
      printf("Error setting pin %d to IN\n", pin);
      exit(-1);
    }
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<init_GPIO>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x24,%esi
lea    0xe3c(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
call   1090 <access@plt>
cmp    $0xffffffff,%eax
je     11fa <init_GPIO+0x5a>
mov    -0x4(%rbp),%esi
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    128d <init_GPIO+0xed>
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
mov    0x2dee(%rip),%rax
or     (%rax),%esi
lea    0xe35(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
jne    123c <init_GPIO+0x9c>
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
lea    -0x36(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe30(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x34(%rbp),%edi
lea    -0x36(%rbp),%rsi
mov    $0x2,%edx
call   1040 <write@plt>
cmp    $0x0,%rax
jge    1285 <init_GPIO+0xe5>
mov    -0x4(%rbp),%esi
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    -0x34(%rbp),%edi
call   1070 <close@plt>
lea    -0x80(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x80(%rbp),%rdi
lea    0xdf0(%rip),%rsi
call   10a0 <strcat@plt>
lea    -0x80(%rbp),%rdi
mov    0x2d1b(%rip),%rax
mov    (%rax),%esi
mov    0x2d3a(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
jne    12e9 <init_GPIO+0x149>
lea    0xd87(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    1331 <init_GPIO+0x191>
mov    -0x34(%rbp),%edi
lea    0xda2(%rip),%rsi
mov    $0x3,%edx
call   1040 <write@plt>
cmp    $0x0,%rax
jge    132c <init_GPIO+0x18c>
mov    -0x4(%rbp),%esi
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
jmp    137e <init_GPIO+0x1de>
cmpl   $0x1,-0x8(%rbp)
jne    1379 <init_GPIO+0x1d9>
mov    -0x34(%rbp),%edi
lea    0xd7b(%rip),%rsi
mov    $0x2,%edx
call   1040 <write@plt>
cmp    $0x0,%rax
jge    1374 <init_GPIO+0x1d4>
mov    -0x4(%rbp),%esi
lea    0xd60(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
jmp    1379 <init_GPIO+0x1d9>
jmp    137e <init_GPIO+0x1de>
mov    -0x34(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_GPIO>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x78,%rsp
mov    %esi,%ebp
mov    %edi,%r14d
lea    0xe3a(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x24,%esi
mov    %r14d,%ecx
call   1070 <snprintf@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10a0 <access@plt>
cmp    $0xffffffff,%eax
je     1202 <init_GPIO+0x52>
lea    0xe22(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1261 <init_GPIO+0xb1>
mov    0x2dc7(%rip),%rax
mov    0x2de8(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe2d(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12f7 <init_GPIO+0x147>
mov    %eax,%ebx
lea    0xe2d(%rip),%rsi
lea    0x30(%rsp),%r15
mov    %r15,%rdi
mov    %r14d,%edx
call   10c0 <sprintf@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x2,%edx
call   1050 <write@plt>
test   %rax,%rax
js     130d <init_GPIO+0x15d>
mov    %ebx,%edi
call   1080 <close@plt>
lea    0x30(%rsp),%rbx
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
lea    0xe02(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <strcat@plt>
mov    0x2d49(%rip),%rax
mov    0x2d6a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12f7 <init_GPIO+0x147>
mov    %eax,%ebx
cmp    $0x1,%ebp
je     12cb <init_GPIO+0x11b>
test   %ebp,%ebp
jne    12e3 <init_GPIO+0x133>
lea    0xdd3(%rip),%rsi
mov    %ebx,%edi
mov    $0x3,%edx
call   1050 <write@plt>
test   %rax,%rax
jns    12e3 <init_GPIO+0x133>
lea    0xdbf(%rip),%rdi
jmp    131d <init_GPIO+0x16d>
lea    0xdd3(%rip),%rsi
mov    %ebx,%edi
mov    $0x2,%edx
call   1050 <write@plt>
test   %rax,%rax
js     1316 <init_GPIO+0x166>
mov    %ebx,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc6(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
lea    0xd4e(%rip),%rdi
jmp    131d <init_GPIO+0x16d>
lea    0xd8b(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"/sys/class/gpio/gpio%d/\00"
@F_OK = global i32 0
@.str.1 = [48 x i8] c"Warning: GPIO pin %d has already been exported\0A\00"
@.str.2 = [23 x i8] c"/sys/class/gpio/export\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@.str.3 = [27 x i8] c"Error opening gpio/export\0A\00"
@.str.4 = [3 x i8] c"%d\00"
@.str.5 = [24 x i8] c"Error exporting pin %d\0A\00"
@.str.6 = [10 x i8] c"direction\00"
@.str.7 = [4 x i8] c"out\00"
@.str.8 = [29 x i8] c"Error setting pin %d to OUT\0A\00"
@.str.9 = [3 x i8] c"in\00"
@.str.10 = [28 x i8] c"Error setting pin %d to IN\0A\00"
define i32 @init_GPIO(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [36 x i8]
%6 = alloca i32
%7 = alloca [2 x i8]
%8 = alloca [64 x i8]
store i32 %0, i32* %3
store i32 %1, i32* %4
%9 = getelementptr inbounds [36 x i8], [36 x i8]* %5, i64 0, i64 0
%10 = load i32, i32* %3
%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]* %5, 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 %19
16: ; preds = %2
%17 = load i32, i32* %3
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i32 %17)
br label %43
19: ; preds = %2
%20 = load i32, i32* @O_WRONLY
%21 = load i32, i32* @O_TRUNC
%22 = or i32 %20, %21
%23 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %22)
store i32 %23, i32* %6
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %19
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%27 = call i32 @exit(i32 -1)
unreachable
28: ; preds = %19
%29 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 0
%30 = load i32, i32* %3
%31 = call i32 @sprintf(i8* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %30)
%32 = load i32, i32* %6
%33 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 0
%34 = call i64 @write(i32 %32, i8* %33, i32 2)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %28
%37 = load i32, i32* %3
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %37)
%39 = call i32 @exit(i32 -1)
unreachable
40: ; preds = %28
%41 = load i32, i32* %6
%42 = call i32 @close(i32 %41)
br label %43
43: ; preds = %40, %16
%44 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%45 = getelementptr inbounds [36 x i8], [36 x i8]* %5, i64 0, i64 0
%46 = call i32 @strcpy(i8* %44, i8* %45)
%47 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%48 = call i32 @strcat(i8* %47, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0))
%49 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%50 = load i32, i32* @O_WRONLY
%51 = load i32, i32* @O_TRUNC
%52 = or i32 %50, %51
%53 = call i32 @open(i8* %49, i32 %52)
store i32 %53, i32* %6
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %58
55: ; preds = %43
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%57 = call i32 @exit(i32 -1)
unreachable
58: ; preds = %43
%59 = load i32, i32* %4
%60 = icmp eq i32 %59, 0
br i1 %60, label %61, label %70
61: ; preds = %58
%62 = load i32, i32* %6
%63 = call i64 @write(i32 %62, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i32 3)
%64 = icmp slt i64 %63, 0
br i1 %64, label %65, label %69
65: ; preds = %61
%66 = load i32, i32* %3
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.8, i64 0, i64 0), i32 %66)
%68 = call i32 @exit(i32 -1)
unreachable
69: ; preds = %61
br label %83
70: ; preds = %58
%71 = load i32, i32* %4
%72 = icmp eq i32 %71, 1
br i1 %72, label %73, label %82
73: ; preds = %70
%74 = load i32, i32* %6
%75 = call i64 @write(i32 %74, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i64 0, i64 0), i32 2)
%76 = icmp slt i64 %75, 0
br i1 %76, label %77, label %81
77: ; preds = %73
%78 = load i32, i32* %3
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.10, i64 0, i64 0), i32 %78)
%80 = call i32 @exit(i32 -1)
unreachable
81: ; preds = %73
br label %82
82: ; preds = %81, %70
br label %83
83: ; preds = %82, %69
%84 = load i32, i32* %6
%85 = call i32 @close(i32 %84)
ret i32 0
}

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