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

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int snprintf(char *, int, char *, unsigned int);
int /*<<< orphan*/ write(int, char *, int);
int gpio_value(unsigned gpio, unsigned value) {
  int fd, len;
  char buf[60];
  len = snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/value", gpio);
  if (len > 60)
    perror("\n***Filename too long in gpio_dir function\n");
  fd = open(buf, O_WRONLY);
  if (fd < 0) {
    perror("gpio/value");
    return fd;
  }
  if (value)
    write(fd, "1", 2);
  else
    write(fd, "0", 2);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<gpio_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x3c,%esi
lea    0xe8f(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x3c,-0x14(%rbp)
jle    118f <gpio_value+0x3f>
lea    0xe93(%rip),%rdi
call   1070 <perror@plt>
lea    -0x50(%rbp),%rdi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11c5 <gpio_value+0x75>
lea    0xe94(%rip),%rdi
call   1070 <perror@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120b <gpio_value+0xbb>
cmpl   $0x0,-0xc(%rbp)
je     11e8 <gpio_value+0x98>
mov    -0x10(%rbp),%edi
lea    0xe7b(%rip),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
jmp    11fc <gpio_value+0xac>
mov    -0x10(%rbp),%edi
lea    0xe64(%rip),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_value>:
push   %rbp
push   %rbx
sub    $0x48,%rsp
mov    %esi,%ebx
mov    %edi,%ecx
lea    0xe9f(%rip),%rdx
mov    %rsp,%rdi
mov    $0x3c,%esi
call   1040 <snprintf@plt>
cmp    $0x3d,%eax
jge    11bc <gpio_value+0x6c>
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1060 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11ca <gpio_value+0x7a>
test   %ebx,%ebx
lea    0xec3(%rip),%rax
lea    0xeba(%rip),%rsi
cmove  %rax,%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe5a(%rip),%rdi
call   1070 <perror@plt>
jmp    1173 <gpio_value+0x23>
lea    0xe78(%rip),%rdi
call   1070 <perror@plt>
jmp    11b3 <gpio_value+0x63>

LLVM IR (-O0):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@.str.1 = [44 x i8] c"\0A***Filename too long in gpio_dir function\0A\00"
@O_WRONLY = global i32 0
@.str.2 = [11 x i8] c"gpio/value\00"
@.str.3 = [2 x i8] c"1\00"
@.str.4 = [2 x i8] c"0\00"
define i32 @gpio_value(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [60 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = getelementptr inbounds [60 x i8], [60 x i8]* %8, i64 0, i64 0
%10 = load i32, i32* %4
%11 = call i32 @snprintf(i8* %9, i32 60, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp sgt i32 %12, 60
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %2
%17 = getelementptr inbounds [60 x i8], [60 x i8]* %8, i64 0, i64 0
%18 = load i32, i32* @O_WRONLY
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%24 = load i32, i32* %6
store i32 %24, i32* %3
br label %37
25: ; preds = %16
%26 = load i32, i32* %5
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i32, i32* %6
%30 = call i32 @write(i32 %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 2)
br label %34
31: ; preds = %25
%32 = load i32, i32* %6
%33 = call i32 @write(i32 %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 2)
br label %34
34: ; preds = %31, %28
%35 = load i32, i32* %6
%36 = call i32 @close(i32 %35)
store i32 0, i32* %3
br label %37
37: ; preds = %34, %22
%38 = load i32, i32* %3
ret i32 %38
}

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