#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*/ snprintf(char *, int, char *, unsigned int);
int write(int, char *, int);
int gpio_set_value(unsigned int gpio, unsigned int value) {
  int fd;
  int wr_err = 0;
  char buf[100];
  snprintf(buf, sizeof(buf),
           "/sys/class/gpio/"
           "gpio%d/value",
           gpio);
  fd = open(buf, O_WRONLY);
  if (fd < 0)
    return -1;
  if (value == 1)
    wr_err += write(fd, "1", 1) != 1;
  else
    wr_err += write(fd, "0", 1) != 1;
  close(fd);
  return -wr_err;
}


x86 assembly (clang, -O0):
<gpio_set_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x64,%esi
lea    0xe95(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x80(%rbp),%rdi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    119b <gpio_set_value+0x5b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1204 <gpio_set_value+0xc4>
cmpl   $0x1,-0xc(%rbp)
jne    11cf <gpio_set_value+0x8f>
mov    -0x10(%rbp),%edi
lea    0xe6e(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
setne  %al
and    $0x1,%al
movzbl %al,%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    11f4 <gpio_set_value+0xb4>
mov    -0x10(%rbp),%edi
lea    0xe46(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
setne  %al
and    $0x1,%al
movzbl %al,%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
sub    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_set_value>:
push   %rbp
push   %rbx
sub    $0x68,%rsp
mov    %esi,%ebp
mov    %edi,%ecx
lea    0xeaf(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1040 <snprintf@plt>
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11ad <gpio_set_value+0x6d>
mov    %eax,%ebx
cmp    $0x1,%ebp
lea    0xe9b(%rip),%rax
lea    0xe96(%rip),%rsi
cmove  %rax,%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <write@plt>
xor    %ebp,%ebp
cmp    $0x1,%eax
setne  %bpl
neg    %ebp
mov    %ebx,%edi
call   1050 <close@plt>
jmp    11b2 <gpio_set_value+0x72>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x68,%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 i32
%8 = alloca [100 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 0, i32* %7
%9 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%10 = load i32, i32* %4
%11 = call i32 @snprintf(i8* %9, i32 100, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%13 = load i32, i32* @O_WRONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %2
store i32 -1, i32* %3
br label %40
18: ; preds = %2
%19 = load i32, i32* %5
%20 = icmp eq i32 %19, 1
br i1 %20, label %21, label %28
21: ; preds = %18
%22 = load i32, i32* %6
%23 = call i32 @write(i32 %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%24 = icmp ne i32 %23, 1
%25 = zext i1 %24 to i32
%26 = load i32, i32* %7
%27 = add nsw i32 %26, %25
store i32 %27, i32* %7
br label %35
28: ; preds = %18
%29 = load i32, i32* %6
%30 = call i32 @write(i32 %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%31 = icmp ne i32 %30, 1
%32 = zext i1 %31 to i32
%33 = load i32, i32* %7
%34 = add nsw i32 %33, %32
store i32 %34, i32* %7
br label %35
35: ; preds = %28, %21
%36 = load i32, i32* %6
%37 = call i32 @close(i32 %36)
%38 = load i32, i32* %7
%39 = sub nsw i32 0, %38
store i32 %39, i32* %3
br label %40
40: ; preds = %35, %17
%41 = load i32, i32* %3
ret i32 %41
}

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 [100 x i8]
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 100, i8* 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 slt i32 %7, 0
br i1 %8, label %16, label %9
9: ; preds = %2
%10 = icmp eq i32 %1, 1
%11 = select i1 %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0)
%12 = call i32 @write(i32 %7, i8* %11, i32 1)
%13 = icmp ne i32 %12, 1
%14 = sext i1 %13 to i32
%15 = call i32 @close(i32 %7)
br label %16
16: ; preds = %2, %9
%17 = phi i32 [ %14, %9 ], [ -1, %2 ]
ret i32 %17
}