#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int gpio_write(int pin, int val) {
  char path[40];
  char ch;
  FILE *fp = NULL;
  snprintf(path, 40, "/sys/class/gpio/gpio%d/value", pin);
  fp = fopen(path, "r+");
  if (!fp) {
    printf("Failed to open gpio value %d!\n", pin);
    return 1;
  }
  ch = '0' + (val & 1);
  if (fwrite(&ch, 1, 1, fp) != 1) {
    printf("Failed to write value %d!\n", pin);
    fclose(fp);
    return 1;
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<gpio_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movq   $0x0,-0x50(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x28,%esi
lea    0xe87(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x40(%rbp),%rdi
lea    0xe94(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    11b8 <gpio_write+0x68>
mov    -0x8(%rbp),%esi
lea    0xe79(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1215 <gpio_write+0xc5>
mov    -0xc(%rbp),%eax
and    $0x1,%eax
add    $0x30,%eax
mov    %al,-0x41(%rbp)
mov    -0x50(%rbp),%rcx
lea    -0x41(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1070 <fwrite@plt>
cmp    $0x1,%eax
je     1205 <gpio_write+0xb5>
mov    -0x8(%rbp),%esi
lea    0xe54(%rip),%rdi
call   1040 <printf@plt>
mov    -0x50(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    1215 <gpio_write+0xc5>
mov    -0x50(%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_write>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %esi,%ebp
mov    %edi,%r14d
lea    0xe9c(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x28,%esi
mov    %r14d,%ecx
call   1050 <snprintf@plt>
lea    0xe9d(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11ca <gpio_write+0x7a>
mov    %rax,%rbx
and    $0x1,%bpl
or     $0x30,%bpl
mov    %bpl,0xf(%rsp)
lea    0xf(%rsp),%rdi
mov    $0x1,%ebp
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
cmp    $0x1,%eax
jne    11e0 <gpio_write+0x90>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %ebp,%ebp
jmp    11f7 <gpio_write+0xa7>
lea    0xe4f(%rip),%rdi
mov    %r14d,%esi
call   1040 <printf@plt>
mov    $0x1,%ebp
jmp    11f7 <gpio_write+0xa7>
lea    0xe58(%rip),%rdi
mov    %r14d,%esi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [31 x i8] c"Failed to open gpio value %d!\0A\00"
@.str.3 = [27 x i8] c"Failed to write value %d!\0A\00"
define i32 @gpio_write(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [40 x i8]
%7 = alloca i8
%8 = alloca i32*
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32* null, i32** %8
%9 = getelementptr inbounds [40 x i8], [40 x i8]* %6, 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]* %6, i64 0, i64 0
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %8
%14 = load i32*, i32** %8
%15 = icmp ne i32* %14, null
br i1 %15, label %19, label %16
16: ; preds = %2
%17 = load i32, i32* %4
%18 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %17)
store i32 1, i32* %3
br label %35
19: ; preds = %2
%20 = load i32, i32* %5
%21 = and i32 %20, 1
%22 = add nsw i32 48, %21
%23 = trunc i32 %22 to i8
store i8 %23, i8* %7
%24 = load i32*, i32** %8
%25 = call i32 @fwrite(i8* %7, i32 1, i32 1, i32* %24)
%26 = icmp ne i32 %25, 1
br i1 %26, label %27, label %32
27: ; preds = %19
%28 = load i32, i32* %4
%29 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 %28)
%30 = load i32*, i32** %8
%31 = call i32 @fclose(i32* %30)
store i32 1, i32* %3
br label %35
32: ; preds = %19
%33 = load i32*, i32** %8
%34 = call i32 @fclose(i32* %33)
store i32 0, i32* %3
br label %35
35: ; preds = %32, %27, %16
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [31 x i8] c"Failed to open gpio value %d!\0A\00"
@.str.3 = [27 x i8] c"Failed to write value %d!\0A\00"
define i32 @gpio_write(i32 %0, i32 %1) {
%3 = alloca [40 x i8]
%4 = alloca i8
%5 = getelementptr inbounds [40 x i8], [40 x i8]* %3, i64 0, i64 0
%6 = call i32 @snprintf(i8* %5, i32 40, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %0)
br label %22
11: ; preds = %2
%12 = trunc i32 %1 to i8
%13 = and i8 %12, 1
%14 = or i8 %13, 48
store i8 %14, i8* %4
%15 = call i32 @fwrite(i8* %4, i32 1, i32 1, i32* %7)
%16 = icmp eq i32 %15, 1
br i1 %16, label %20, label %17
17: ; preds = %11
%18 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 %0)
%19 = call i32 @fclose(i32* %7)
br label %22
20: ; preds = %11
%21 = call i32 @fclose(i32* %7)
br label %22
22: ; preds = %20, %17, %9
%23 = phi i32 [ 1, %17 ], [ 0, %20 ], [ 1, %9 ]
ret i32 %23
}