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

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int eim_set_value(int, int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int);
int write(int, char *, int);
int gpio_set_value(int gpio, int value) {
  char gpio_path[256];
  int fd;
  int ret;
  if (gpio & (0x80000000))
    return eim_set_value(gpio & (~(0x80000000)), value);
  snprintf(gpio_path, sizeof(gpio_path) - 1,
           "/sys/class/gpio"
           "/gpio%d/value",
           gpio);
  fd = open(gpio_path, O_WRONLY);
  if (fd == -1) {
    fprintf(stderr, "Value file: [%s]\n", gpio_path);
    perror("Couldn't open value file for gpio");
    return -errno;
  }
  if (value)
    ret = write(fd, "1", 2);
  else
    ret = write(fd, "0", 2);
  if (ret == -1) {
    fprintf(stderr, "Couldn't set GPIO %d output value: %s\n", gpio,
            strerror(errno));
    close(fd);
    return -errno;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<gpio_set_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%eax
and    $0x80000000,%eax
cmp    $0x0,%eax
je     11bb <gpio_set_value+0x3b>
mov    -0x8(%rbp),%edi
and    $0x7fffffff,%edi
mov    -0xc(%rbp),%esi
call   1090 <eim_set_value@plt>
mov    %eax,-0x4(%rbp)
jmp    1304 <gpio_set_value+0x184>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0xff,%esi
lea    0xe2f(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x110(%rbp),%rdi
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0xffffffff,-0x114(%rbp)
jne    123b <gpio_set_value+0xbb>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
lea    -0x110(%rbp),%rdx
lea    0xe08(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
lea    0xe0c(%rip),%rdi
call   1080 <perror@plt>
xor    %eax,%eax
mov    0x2da7(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1304 <gpio_set_value+0x184>
cmpl   $0x0,-0xc(%rbp)
je     1267 <gpio_set_value+0xe7>
mov    -0x114(%rbp),%edi
lea    0xdff(%rip),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    %eax,-0x118(%rbp)
jmp    1284 <gpio_set_value+0x104>
mov    -0x114(%rbp),%edi
lea    0xddf(%rip),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    %eax,-0x118(%rbp)
cmpl   $0xffffffff,-0x118(%rbp)
jne    12f2 <gpio_set_value+0x172>
mov    0x2d48(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x120(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x11c(%rbp)
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x120(%rbp),%edi
mov    -0x11c(%rbp),%edx
mov    %rax,%rcx
lea    0xd88(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x114(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
mov    0x2cf0(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1304 <gpio_set_value+0x184>
mov    -0x114(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_set_value>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x100,%rsp
mov    %esi,%r14d
mov    %edi,%ebx
test   %edi,%edi
js     11ff <gpio_set_value+0x7f>
lea    0xe61(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0xff,%esi
mov    %ebx,%ecx
call   1040 <snprintf@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     126f <gpio_set_value+0xef>
mov    %eax,%ebp
test   %r14d,%r14d
lea    0xe7c(%rip),%rax
lea    0xe73(%rip),%rsi
cmove  %rax,%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     121e <gpio_set_value+0x9e>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %r14d,%r14d
jmp    125c <gpio_set_value+0xdc>
and    $0x7fffffff,%ebx
mov    %ebx,%edi
mov    %r14d,%esi
add    $0x100,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <eim_set_value@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%r15d
mov    0x2da9(%rip),%r12
mov    (%r12),%edi
call   10a0 <strerror@plt>
lea    0xe16(%rip),%rsi
xor    %r14d,%r14d
mov    %r15d,%edi
mov    %ebx,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebp,%edi
call   1050 <close@plt>
sub    (%r12),%r14d
mov    %r14d,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
lea    0xd9e(%rip),%rsi
xor    %r14d,%r14d
mov    %rsp,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
lea    0xd9c(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d39(%rip),%rax
sub    (%rax),%r14d
jmp    125c <gpio_set_value+0xdc>

LLVM IR (-O0):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str.1 = [18 x i8] c"Value file: [%s]\0A\00"
@.str.2 = [34 x i8] c"Couldn't open value file for gpio\00"
@errno = global i32 0
@.str.3 = [2 x i8] c"1\00"
@.str.4 = [2 x i8] c"0\00"
@.str.5 = [39 x i8] c"Couldn't set GPIO %d output value: %s\0A\00"
define i32 @gpio_set_value(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [256 x i8]
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = load i32, i32* %4
%10 = and i32 %9, -2147483648
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i32, i32* %4
%14 = and i32 %13, 2147483647
%15 = load i32, i32* %5
%16 = call i32 @eim_set_value(i32 %14, i32 %15)
store i32 %16, i32* %3
br label %58
17: ; preds = %2
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%19 = load i32, i32* %4
%20 = call i32 @snprintf(i8* %18, i32 255, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%22 = load i32, i32* @O_WRONLY
%23 = call i32 @open(i8* %21, i32 %22)
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %33
26: ; preds = %17
%27 = load i32, i32* @stderr
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
%31 = load i32, i32* @errno
%32 = sub nsw i32 0, %31
store i32 %32, i32* %3
br label %58
33: ; preds = %17
%34 = load i32, i32* %5
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = load i32, i32* %7
%38 = call i32 @write(i32 %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 2)
store i32 %38, i32* %8
br label %42
39: ; preds = %33
%40 = load i32, i32* %7
%41 = call i32 @write(i32 %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 2)
store i32 %41, i32* %8
br label %42
42: ; preds = %39, %36
%43 = load i32, i32* %8
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %55
45: ; preds = %42
%46 = load i32, i32* @stderr
%47 = load i32, i32* %4
%48 = load i32, i32* @errno
%49 = call i8* @strerror(i32 %48)
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i32 %47, i8* %49)
%51 = load i32, i32* %7
%52 = call i32 @close(i32 %51)
%53 = load i32, i32* @errno
%54 = sub nsw i32 0, %53
store i32 %54, i32* %3
br label %58
55: ; preds = %42
%56 = load i32, i32* %7
%57 = call i32 @close(i32 %56)
store i32 0, i32* %3
br label %58
58: ; preds = %55, %45, %26, %12
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str.1 = [18 x i8] c"Value file: [%s]\0A\00"
@.str.2 = [34 x i8] c"Couldn't open value file for gpio\00"
@errno = global i32 0
@.str.3 = [2 x i8] c"1\00"
@.str.4 = [2 x i8] c"0\00"
@.str.5 = [39 x i8] c"Couldn't set GPIO %d output value: %s\0A\00"
define i32 @gpio_set_value(i32 %0, i32 %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = icmp sgt i32 %0, -1
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = and i32 %0, 2147483647
%8 = tail call i32 @eim_set_value(i32 %7, i32 %1)
br label %35
9: ; preds = %2
%10 = call i32 @snprintf(i8* %4, i32 255, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %0)
%11 = load i32, i32* @O_WRONLY
%12 = call i32 @open(i8* %4, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %20
14: ; preds = %9
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%17 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
%18 = load i32, i32* @errno
%19 = sub nsw i32 0, %18
br label %35
20: ; preds = %9
%21 = icmp eq i32 %1, 0
%22 = select i1 %21, 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)
%23 = call i32 @write(i32 %12, i8* %22, i32 2)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %33
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i32 %0, i8* %28)
%30 = call i32 @close(i32 %12)
%31 = load i32, i32* @errno
%32 = sub nsw i32 0, %31
br label %35
33: ; preds = %20
%34 = call i32 @close(i32 %12)
br label %35
35: ; preds = %33, %25, %14, %6
%36 = phi i32 [ %8, %6 ], [ %19, %14 ], [ %32, %25 ], [ 0, %33 ]
ret i32 %36
}