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


x86 assembly (clang, -O0):
<writeGpio>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe94(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x50(%rbp),%rdi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
jne    119b <writeGpio+0x4b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f7 <writeGpio+0xa7>
lea    -0x50(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe74(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x10(%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    11e8 <writeGpio+0x98>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f7 <writeGpio+0xa7>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeGpio>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %esi,%r14d
mov    %edi,%edx
lea    0xe9a(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1070 <sprintf@plt>
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     11c5 <writeGpio+0x75>
mov    %eax,%ebp
lea    0xe88(%rip),%rsi
mov    %rsp,%r15
mov    %r15,%rdi
mov    %r14d,%edx
call   1070 <sprintf@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     11c5 <writeGpio+0x75>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_WRONLY = global i32 0
@.str.1 = [3 x i8] c"%d\00"
define i32 @writeGpio(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [50 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %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 ([29 x i8], [29 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* @O_WRONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %6
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 -1, i32* %3
br label %30
16: ; preds = %2
%17 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 0
%18 = load i32, i32* %5
%19 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = load i32, i32* %6
%21 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 0
%22 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 0
%23 = call i32 @strlen(i8* %22)
%24 = call i32 @write(i32 %20, i8* %21, i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %27
26: ; preds = %16
store i32 -1, i32* %3
br label %30
27: ; preds = %16
%28 = load i32, i32* %6
%29 = call i32 @close(i32 %28)
store i32 0, i32* %3
br label %30
30: ; preds = %27, %26, %15
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_WRONLY = global i32 0
@.str.1 = [3 x i8] c"%d\00"
define i32 @writeGpio(i32 %0, i32 %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 ([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 %16, label %9
9: ; preds = %2
%10 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %1)
%11 = call i32 @strlen(i8* %4)
%12 = call i32 @write(i32 %7, i8* %4, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %16, label %14
14: ; preds = %9
%15 = call i32 @close(i32 %7)
br label %16
16: ; preds = %9, %2, %14
%17 = phi i32 [ 0, %14 ], [ -1, %2 ], [ -1, %9 ]
ret i32 %17
}