#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 errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int set_gpio_mode_input(int index) {
  char fn[64];
  FILE *f;
  f = fopen("/sys/class/gpio/export", "w");
  if (!f)
    return -errno;
  sprintf(fn, "%d", index);
  fputs(fn, f);
  fclose(f);
  sprintf(fn, "/sys/class/gpio/gpio%d/direction", index);
  f = fopen(fn, "w");
  if (!f)
    return -errno;
  fputs("in", f);
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<set_gpio_mode_input>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
lea    0xeae(%rip),%rdi
lea    0xebe(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jne    1180 <set_gpio_mode_input+0x40>
xor    %eax,%eax
mov    0x2e6a(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    120e <set_gpio_mode_input+0xce>
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe8b(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rsi
call   1040 <fputs@plt>
mov    -0x58(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe65(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x50(%rbp),%rdi
lea    0xe50(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jne    11ee <set_gpio_mode_input+0xae>
xor    %eax,%eax
mov    0x2dfc(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    120e <set_gpio_mode_input+0xce>
mov    -0x58(%rbp),%rsi
lea    0xe44(%rip),%rdi
call   1040 <fputs@plt>
mov    -0x58(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_gpio_mode_input>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %edi,%ebp
lea    0xe9f(%rip),%rdi
lea    0xeaf(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11e9 <set_gpio_mode_input+0x99>
mov    %rax,%rbx
lea    0xe9d(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %ebp,%edx
call   1060 <sprintf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <fputs@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    0xe79(%rip),%rsi
mov    %r14,%rdi
mov    %ebp,%edx
call   1060 <sprintf@plt>
lea    0xe63(%rip),%rsi
mov    %r14,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     11e9 <set_gpio_mode_input+0x99>
mov    %rax,%rbx
lea    0xe72(%rip),%rdi
mov    $0x2,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    11f4 <set_gpio_mode_input+0xa4>
mov    0x2df0(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"/sys/class/gpio/export\00"
@.str.1 = [2 x i8] c"w\00"
@errno = global i32 0
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@.str.4 = [3 x i8] c"in\00"
define i32 @set_gpio_mode_input(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [64 x i8]
%5 = alloca i32*
store i32 %0, i32* %3
%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* @errno
%11 = sub nsw i32 0, %10
store i32 %11, i32* %2
br label %36
12: ; preds = %1
%13 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%14 = load i32, i32* %3
%15 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%16 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%17 = load i32*, i32** %5
%18 = call i32 @fputs(i8* %16, i32* %17)
%19 = load i32*, i32** %5
%20 = call i32 @fclose(i32* %19)
%21 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%22 = load i32, i32* %3
%23 = call i32 @sprintf(i8* %21, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %25, i32** %5
%26 = load i32*, i32** %5
%27 = icmp ne i32* %26, null
br i1 %27, label %31, label %28
28: ; preds = %12
%29 = load i32, i32* @errno
%30 = sub nsw i32 0, %29
store i32 %30, i32* %2
br label %36
31: ; preds = %12
%32 = load i32*, i32** %5
%33 = call i32 @fputs(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %32)
%34 = load i32*, i32** %5
%35 = call i32 @fclose(i32* %34)
store i32 0, i32* %2
br label %36
36: ; preds = %31, %28, %9
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
@.str = [23 x i8] c"/sys/class/gpio/export\00"
@.str.1 = [2 x i8] c"w\00"
@errno = global i32 0
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@.str.4 = [3 x i8] c"in\00"
define i32 @set_gpio_mode_input(i32 %0) {
%2 = alloca [64 x i8]
%3 = getelementptr inbounds [64 x i8], [64 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 %9
6: ; preds = %1
%7 = load i32, i32* @errno
%8 = sub nsw i32 0, %7
br label %22
9: ; preds = %1
%10 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %0)
%11 = call i32 @fputs(i8* %3, i32* %4)
%12 = tail call i32 @fclose(i32* %4)
%13 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %0)
%14 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %19
16: ; preds = %9
%17 = load i32, i32* @errno
%18 = sub nsw i32 0, %17
br label %22
19: ; preds = %9
%20 = tail call i64 @fwrite(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i64 2, i64 1, i32* %14)
%21 = tail call i32 @fclose(i32* %14)
br label %22
22: ; preds = %19, %16, %6
%23 = phi i32 [ 0, %19 ], [ %18, %16 ], [ %8, %6 ]
ret i32 %23
}