#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 /*<<< orphan*/ O_WRONLY;
int access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, unsigned int);
int /*<<< orphan*/ strlen(char const *);
int /*<<< orphan*/ unsetGpio(unsigned int);
int write(int, char const *, int /*<<< orphan*/);
int setGpio(unsigned int index, const char *direction) {
  int fd;
  char buf[50];
  sprintf(buf, "/sys/class/gpio/gpio%d/direction", index);
  if (access(buf, F_OK) != -1)
    unsetGpio(index);
  if ((fd = open("/sys/class/gpio/export", O_WRONLY)) == -1)
    return -1;
  sprintf(buf, "%d", index);
  if (write(fd, buf, strlen(buf)) == -1)
    return -1;
  close(fd);
  sprintf(buf, "/sys/class/gpio/gpio%d/direction", index);
  if ((fd = open(buf, O_WRONLY)) == -1)
    return -1;
  if (write(fd, direction, strlen(direction)) == -1)
    return -1;
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<setGpio>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe73(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x50(%rbp),%rdi
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
cmp    $0xffffffff,%eax
je     11b5 <setGpio+0x45>
mov    -0x8(%rbp),%edi
call   1050 <unsetGpio@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
lea    0xe5c(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11e2 <setGpio+0x72>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bd <setGpio+0x14d>
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe48(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x5c(%rbp)
lea    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
lea    -0x50(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    122f <setGpio+0xbf>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bd <setGpio+0x14d>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xdbb(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x50(%rbp),%rdi
mov    0x2d83(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1274 <setGpio+0x104>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bd <setGpio+0x14d>
mov    -0x14(%rbp),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x6c(%rbp),%edi
mov    -0x68(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    12ae <setGpio+0x13e>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bd <setGpio+0x14d>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setGpio>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
lea    0xe76(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %ebx,%edx
call   1090 <sprintf@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1080 <access@plt>
cmp    $0xffffffff,%eax
je     11b4 <setGpio+0x44>
mov    %ebx,%edi
call   1050 <unsetGpio@plt>
mov    0x2e1d(%rip),%r13
mov    0x0(%r13),%esi
lea    0xe5b(%rip),%rdi
call   1070 <open@plt>
mov    $0xffffffff,%r15d
cmp    $0xffffffff,%eax
je     125a <setGpio+0xea>
mov    %eax,%ebp
lea    0xe55(%rip),%rsi
mov    %rsp,%r12
mov    %r12,%rdi
mov    %ebx,%edx
call   1090 <sprintf@plt>
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     125a <setGpio+0xea>
mov    %ebp,%edi
call   1060 <close@plt>
lea    0xde9(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %ebx,%edx
call   1090 <sprintf@plt>
mov    0x0(%r13),%esi
mov    %rbp,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     125a <setGpio+0xea>
mov    %eax,%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     125a <setGpio+0xea>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@F_OK = global i32 0
@.str.1 = [23 x i8] c"/sys/class/gpio/export\00"
@O_WRONLY = global i32 0
@.str.2 = [3 x i8] c"%d\00"
define i32 @setGpio(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [50 x i8]
store i32 %0, i32* %4
store i8* %1, i8** %5
%8 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 0
%9 = load i32, i32* %4
%10 = call i32 @sprintf(i8* %8, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 0
%12 = load i32, i32* @F_OK
%13 = call i32 @access(i8* %11, i32 %12)
%14 = icmp ne i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = load i32, i32* %4
%17 = call i32 @unsetGpio(i32 %16)
br label %18
18: ; preds = %15, %2
%19 = load i32, i32* @O_WRONLY
%20 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %19)
store i32 %20, i32* %6
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %18
store i32 -1, i32* %3
br label %56
23: ; preds = %18
%24 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 0
%25 = load i32, i32* %4
%26 = call i32 @sprintf(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %25)
%27 = load i32, i32* %6
%28 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 0
%29 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 0
%30 = call i32 @strlen(i8* %29)
%31 = call i32 @write(i32 %27, i8* %28, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %34
33: ; preds = %23
store i32 -1, i32* %3
br label %56
34: ; preds = %23
%35 = load i32, i32* %6
%36 = call i32 @close(i32 %35)
%37 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 0
%38 = load i32, i32* %4
%39 = call i32 @sprintf(i8* %37, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %38)
%40 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 0
%41 = load i32, i32* @O_WRONLY
%42 = call i32 @open(i8* %40, i32 %41)
store i32 %42, i32* %6
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %45
44: ; preds = %34
store i32 -1, i32* %3
br label %56
45: ; preds = %34
%46 = load i32, i32* %6
%47 = load i8*, i8** %5
%48 = load i8*, i8** %5
%49 = call i32 @strlen(i8* %48)
%50 = call i32 @write(i32 %46, i8* %47, i32 %49)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %53
52: ; preds = %45
store i32 -1, i32* %3
br label %56
53: ; preds = %45
%54 = load i32, i32* %6
%55 = call i32 @close(i32 %54)
store i32 0, i32* %3
br label %56
56: ; preds = %53, %52, %44, %33, %22
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
@.str = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@F_OK = global i32 0
@.str.1 = [23 x i8] c"/sys/class/gpio/export\00"
@O_WRONLY = global i32 0
@.str.2 = [3 x i8] c"%d\00"
define i32 @setGpio(i32 %0, i8* %1) {
%3 = alloca [50 x i8]
%4 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%5 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = load i32, i32* @F_OK
%7 = call i32 @access(i8* %4, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %11, label %9
9: ; preds = %2
%10 = tail call i32 @unsetGpio(i32 %0)
br label %11
11: ; preds = %9, %2
%12 = load i32, i32* @O_WRONLY
%13 = tail call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %32, label %15
15: ; preds = %11
%16 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %0)
%17 = call i32 @strlen(i8* %4)
%18 = call i32 @write(i32 %13, i8* %4, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %32, label %20
20: ; preds = %15
%21 = call i32 @close(i32 %13)
%22 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %0)
%23 = load i32, i32* @O_WRONLY
%24 = call i32 @open(i8* %4, i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %32, label %26
26: ; preds = %20
%27 = call i32 @strlen(i8* %1)
%28 = call i32 @write(i32 %24, i8* %1, i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %32, label %30
30: ; preds = %26
%31 = call i32 @close(i32 %24)
br label %32
32: ; preds = %26, %20, %15, %11, %30
%33 = phi i32 [ 0, %30 ], [ -1, %11 ], [ -1, %15 ], [ -1, %20 ], [ -1, %26 ]
ret i32 %33
}