#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 *);
scalar_t__ fwrite(char *, int, scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
scalar_t__ strlen(char *);
int gpio_export(int pin) {
  char buffer[3];
  FILE *fp = NULL;
  fp = fopen("/sys/class/gpio/export", "w");
  if (!fp) {
    printf("Failed to open export %d!\n", pin);
    return 1;
  }
  snprintf(buffer, 3, "%d", pin);
  if (fwrite(buffer, 1, strlen(buffer), fp) != strlen(buffer)) {
    printf("Failed to write export %d!\n", pin);
    fclose(fp);
    return 1;
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<gpio_export>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
movq   $0x0,-0x18(%rbp)
lea    0xe86(%rip),%rdi
lea    0xe96(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b0 <gpio_export+0x50>
mov    -0x8(%rbp),%esi
lea    0xe7a(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    123f <gpio_export+0xdf>
lea    -0xb(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x3,%esi
lea    0xe71(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0xb(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    -0xb(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x28(%rbp),%rdi
mov    %rax,%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1080 <fwrite@plt>
mov    %rax,-0x20(%rbp)
lea    -0xb(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
cmp    %rcx,%rax
je     122f <gpio_export+0xcf>
mov    -0x8(%rbp),%esi
lea    0xe22(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    123f <gpio_export+0xdf>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_export>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
lea    0xe8a(%rip),%rdi
lea    0xe9a(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11e4 <gpio_export+0x84>
mov    %rax,%rbx
lea    0xea3(%rip),%rdx
lea    0xd(%rsp),%r15
mov    %r15,%rdi
mov    $0x3,%esi
mov    %r14d,%ecx
call   1060 <snprintf@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    $0x1,%r12d
mov    %r15,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
mov    %rbx,%rcx
call   1080 <fwrite@plt>
mov    %rax,%rbp
mov    %r15,%rdi
call   1040 <strlen@plt>
cmp    %rax,%rbp
jne    11fb <gpio_export+0x9b>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %r12d,%r12d
jmp    1212 <gpio_export+0xb2>
lea    0xe2e(%rip),%rdi
mov    %r14d,%esi
call   1050 <printf@plt>
mov    $0x1,%r12d
jmp    1212 <gpio_export+0xb2>
lea    0xe35(%rip),%rdi
mov    %r14d,%esi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %r12d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"/sys/class/gpio/export\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [27 x i8] c"Failed to open export %d!\0A\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [28 x i8] c"Failed to write export %d!\0A\00"
define i32 @gpio_export(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [3 x i8]
%5 = alloca i32*
store i32 %0, i32* %3
store i32* null, i32** %5
%6 = call i32* @fopen(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %5
%7 = load i32*, i32** %5
%8 = icmp ne i32* %7, null
br i1 %8, label %12, label %9
9: ; preds = %1
%10 = load i32, i32* %3
%11 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %10)
store i32 1, i32* %2
br label %32
12: ; preds = %1
%13 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
%14 = load i32, i32* %3
%15 = call i32 @snprintf(i8* %13, i32 3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %14)
%16 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
%17 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
%18 = call i64 @strlen(i8* %17)
%19 = load i32*, i32** %5
%20 = call i64 @fwrite(i8* %16, i32 1, i64 %18, i32* %19)
%21 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
%22 = call i64 @strlen(i8* %21)
%23 = icmp ne i64 %20, %22
br i1 %23, label %24, label %29
24: ; preds = %12
%25 = load i32, i32* %3
%26 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %25)
%27 = load i32*, i32** %5
%28 = call i32 @fclose(i32* %27)
store i32 1, i32* %2
br label %32
29: ; preds = %12
%30 = load i32*, i32** %5
%31 = call i32 @fclose(i32* %30)
store i32 0, i32* %2
br label %32
32: ; preds = %29, %24, %9
%33 = load i32, i32* %2
ret i32 %33
}

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