#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*/ vstr;
typedef int /*<<< orphan*/ filename;

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int gpio_set_value(unsigned int gpio, unsigned int value) {
  int fd;
  char filename[40];
  char vstr[10];
  snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/value", gpio);
  if ((fd = open(filename, O_WRONLY)) < 0)
    return -1;
  if (value) {
    strncpy(vstr, "1", (sizeof(vstr) / sizeof(vstr[0])) - 1);
  } else {
    strncpy(vstr, "0", (sizeof(vstr) / sizeof(vstr[0])) - 1);
  }
  write(fd, vstr, strlen(vstr));
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<gpio_set_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x28,%esi
lea    0xe7f(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    11b0 <gpio_set_value+0x50>
movl   $0xffffffff,-0x4(%rbp)
jmp    121d <gpio_set_value+0xbd>
cmpl   $0x0,-0xc(%rbp)
je     11d4 <gpio_set_value+0x74>
lea    -0x4a(%rbp),%rdi
lea    0xe58(%rip),%rsi
mov    $0x9,%edx
call   1030 <strncpy@plt>
jmp    11e9 <gpio_set_value+0x89>
lea    -0x4a(%rbp),%rdi
lea    0xe40(%rip),%rsi
mov    $0x9,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x5c(%rbp)
lea    -0x4a(%rbp),%rax
mov    %rax,-0x58(%rbp)
lea    -0x4a(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_set_value>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
mov    %esi,%ebp
mov    %edi,%ecx
lea    0xe8f(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x28,%esi
call   1060 <snprintf@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11df <gpio_set_value+0x7f>
mov    %eax,%ebx
test   %ebp,%ebp
lea    0xe7c(%rip),%rax
lea    0xe73(%rip),%rsi
cmove  %rax,%rsi
lea    0x6(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x9,%edx
call   1030 <strncpy@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
jmp    11e4 <gpio_set_value+0x84>
mov    $0xffffffff,%eax
add    $0x38,%rsp
pop    %rbx
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 = [2 x i8] c"1\00"
@.str.2 = [2 x i8] c"0\00"
define i32 @gpio_set_value(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [40 x i8]
%8 = alloca [10 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = getelementptr inbounds [40 x i8], [40 x i8]* %7, i64 0, i64 0
%10 = load i32, i32* %4
%11 = call i32 @snprintf(i8* %9, i32 40, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [40 x i8], [40 x i8]* %7, i64 0, i64 0
%13 = load i32, i32* @O_WRONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 -1, i32* %3
br label %34
17: ; preds = %2
%18 = load i32, i32* %5
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%22 = call i32 @strncpy(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 9)
br label %26
23: ; preds = %17
%24 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%25 = call i32 @strncpy(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 9)
br label %26
26: ; preds = %23, %20
%27 = load i32, i32* %6
%28 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%29 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%30 = call i32 @strlen(i8* %29)
%31 = call i32 @write(i32 %27, i8* %28, i32 %30)
%32 = load i32, i32* %6
%33 = call i32 @close(i32 %32)
store i32 0, i32* %3
br label %34
34: ; preds = %26, %16
%35 = load i32, i32* %3
ret i32 %35
}

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