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

/* Variables and functions */
int EINVAL;
int ENODEV;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int strlen(char *);
int gpio_set(int gpio, int val) {
  FILE *fd;
  if (gpio < 0)
    return -EINVAL;
  char path[32] = "/sys/class/gpio/gpio";
  sprintf(path + strlen(path), "%d/value", gpio);
  fd = fopen(path, "w");
  if (fd < 0)
    return -ENODEV;
  fputc(val ? '1' : '0', fd);
  fclose(fd);
  return 0;
}


x86 assembly (clang, -O0):
<gpio_set>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    117b <gpio_set+0x2b>
xor    %eax,%eax
mov    0x2e6f(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1231 <gpio_set+0xe1>
mov    0xe7e(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe7b(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0xe78(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe75(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
lea    -0x40(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x48(%rbp),%rdi
cltq
add    %rax,%rdi
mov    -0x8(%rbp),%edx
lea    0xe55(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x40(%rbp),%rdi
lea    0xe4e(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jae    1202 <gpio_set+0xb2>
xor    %eax,%eax
mov    0x2de0(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1231 <gpio_set+0xe1>
mov    -0xc(%rbp),%edx
mov    $0x30,%eax
mov    $0x31,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    -0x18(%rbp),%rsi
movsbl %al,%edi
call   1050 <fputc@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_set>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
test   %edi,%edi
js     11c7 <gpio_set+0x77>
mov    %esi,%ebx
mov    %edi,%ebp
movaps 0xea9(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xe8d(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    %rsp,%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
cltq
lea    (%rsp,%rax,1),%rdi
lea    0xe91(%rip),%rsi
mov    %ebp,%edx
call   1070 <sprintf@plt>
lea    0xe8c(%rip),%rsi
mov    %r14,%rdi
call   1060 <fopen@plt>
mov    %rax,%rbp
cmp    $0x1,%ebx
mov    $0x31,%edi
sbb    $0x0,%edi
mov    %rax,%rsi
call   1050 <fputc@plt>
mov    %rbp,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    11d2 <gpio_set+0x82>
mov    0x2e12(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@__const.gpio_set.path = [32 x i8] c"/sys/class/gpio/gpio\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [9 x i8] c"%d/value\00"
@.str.1 = [2 x i8] c"w\00"
@ENODEV = global i32 0
define i32 @gpio_set(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca [32 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i32, i32* @EINVAL
%12 = sub nsw i32 0, %11
store i32 %12, i32* %3
br label %39
13: ; preds = %2
%14 = bitcast [32 x i8]* %7 to i8*
%15 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%16 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%17 = call i32 @strlen(i8* %16)
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %15, i64 %18
%20 = load i32, i32* %4
%21 = call i32 @sprintf(i8* %19, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %23, i32** %6
%24 = load i32*, i32** %6
%25 = icmp ult i32* %24, null
br i1 %25, label %26, label %29
26: ; preds = %13
%27 = load i32, i32* @ENODEV
%28 = sub nsw i32 0, %27
store i32 %28, i32* %3
br label %39
29: ; preds = %13
%30 = load i32, i32* %5
%31 = icmp ne i32 %30, 0
%32 = zext i1 %31 to i64
%33 = select i1 %31, i32 49, i32 48
%34 = trunc i32 %33 to i8
%35 = load i32*, i32** %6
%36 = call i32 @fputc(i8 signext %34, i32* %35)
%37 = load i32*, i32** %6
%38 = call i32 @fclose(i32* %37)
store i32 0, i32* %3
br label %39
39: ; preds = %29, %26, %10
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
@EINVAL = global i32 0
@__const.gpio_set.path = [32 x i8] c"/sys/class/gpio/gpio\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [9 x i8] c"%d/value\00"
@.str.1 = [2 x i8] c"w\00"
@ENODEV = global i32 0
define i32 @gpio_set(i32 %0, i32 %1) {
%3 = alloca [32 x i8]
%4 = icmp slt i32 %0, 0
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i32, i32* @EINVAL
%7 = sub nsw i32 0, %6
br label %19
8: ; preds = %2
%9 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%10 = call i32 @strlen(i8* %9)
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 %11
%13 = call i32 @sprintf(i8* %12, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %0)
%14 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i32 %1, 0
%16 = select i1 %15, i8 48, i8 49
%17 = call i32 @fputc(i8 signext %16, i32* %14)
%18 = call i32 @fclose(i32* %14)
br label %19
19: ; preds = %8, %5
%20 = phi i32 [ %7, %5 ], [ 0, %8 ]
ret i32 %20
}