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

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, unsigned int,
                            char const *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int gpio_write(unsigned int gpio, const char *filename, char *value) {
  int fd;
  char filepath[64];
  snprintf(filepath, sizeof(filepath), "%s/gpio%d/%s", "/sys/class/gpio", gpio,
           filename);
  if ((fd = open(filepath, O_WRONLY)) < 0)
    return -1;
  write(fd, value, strlen(value));
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<gpio_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%r8d
mov    -0x10(%rbp),%r9
mov    $0x40,%esi
lea    0xe85(%rip),%rdx
lea    0xe8b(%rip),%rcx
call   1050 <snprintf@plt>
lea    -0x60(%rbp),%rdi
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11b1 <gpio_write+0x61>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e5 <gpio_write+0x95>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x6c(%rbp),%edi
mov    -0x68(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_write>:
push   %rbp
push   %rbx
sub    $0x48,%rsp
mov    %rdx,%rbx
mov    %rsi,%r9
mov    %edi,%r8d
lea    0xe9a(%rip),%rdx
lea    0xea0(%rip),%rcx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x40,%esi
call   1050 <snprintf@plt>
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11b3 <gpio_write+0x63>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    11b8 <gpio_write+0x68>
mov    $0xffffffff,%eax
add    $0x48,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"%s/gpio%d/%s\00"
@.str.1 = [16 x i8] c"/sys/class/gpio\00"
@O_WRONLY = global i32 0
define i32 @gpio_write(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca [64 x i8]
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%10 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%11 = load i32, i32* %5
%12 = load i8*, i8** %6
%13 = call i32 @snprintf(i8* %10, i32 64, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %11, i8* %12)
%14 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%15 = load i32, i32* @O_WRONLY
%16 = call i32 @open(i8* %14, i32 %15)
store i32 %16, i32* %8
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %3
store i32 -1, i32* %4
br label %27
19: ; preds = %3
%20 = load i32, i32* %8
%21 = load i8*, i8** %7
%22 = load i8*, i8** %7
%23 = call i32 @strlen(i8* %22)
%24 = call i32 @write(i32 %20, i8* %21, i32 %23)
%25 = load i32, i32* %8
%26 = call i32 @close(i32 %25)
store i32 0, i32* %4
br label %27
27: ; preds = %19, %18
%28 = load i32, i32* %4
ret i32 %28
}

LLVM IR (-O2):
@.str = [13 x i8] c"%s/gpio%d/%s\00"
@.str.1 = [16 x i8] c"/sys/class/gpio\00"
@O_WRONLY = global i32 0
define i32 @gpio_write(i32 %0, i8* %1, i8* %2) {
%4 = alloca [64 x i8]
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%6 = call i32 @snprintf(i8* %5, i32 64, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %0, i8* %1)
%7 = load i32, i32* @O_WRONLY
%8 = call i32 @open(i8* %5, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %14, label %10
10: ; preds = %3
%11 = call i32 @strlen(i8* %2)
%12 = call i32 @write(i32 %8, i8* %2, i32 %11)
%13 = call i32 @close(i32 %8)
br label %14
14: ; preds = %3, %10
%15 = phi i32 [ 0, %10 ], [ -1, %3 ]
ret i32 %15
}