#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 /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ *stderr;
int gpio_open(int gpio) {
  FILE *fd;
  fd = fopen("/sys/class/gpio/export", "w");
  if (fd == NULL) {
    fprintf(stderr, "Could not open export\n");
    return -1;
  }
  fprintf(fd, "%d", gpio);
  fclose(fd);
  char file[50];
  sprintf(file, "/sys/class/gpio/gpio%d/direction", gpio);
  fd = fopen(file, "w");
  if (fd == NULL) {
    fprintf(stderr, "Could not open direction\n");
    return -1;
  }
  fprintf(fd, "low");
  fclose(fd);
  return 0;
}


x86 assembly (clang, -O0):
<gpio_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
lea    0xeae(%rip),%rdi
lea    0xf15(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1191 <gpio_open+0x51>
mov    0x2e6c(%rip),%rax
mov    (%rax),%rdi
lea    0xe99(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1227 <gpio_open+0xe7>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe8f(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe74(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x50(%rbp),%rdi
lea    0xea1(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1205 <gpio_open+0xc5>
mov    0x2df8(%rip),%rax
mov    (%rax),%rdi
lea    0xe60(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1227 <gpio_open+0xe7>
mov    -0x10(%rbp),%rdi
lea    0xe5c(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x10(%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_open>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %edi,%ebp
lea    0xe9f(%rip),%rdi
lea    0xf06(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11e1 <gpio_open+0x91>
mov    %rax,%rbx
lea    0xeb2(%rip),%rsi
xor    %r14d,%r14d
mov    %rax,%rdi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    0xe97(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %ebp,%edx
call   1060 <sprintf@plt>
lea    0xec0(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     11f9 <gpio_open+0xa9>
mov    %rax,%rbx
lea    0xea7(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    121f <gpio_open+0xcf>
mov    0x2df8(%rip),%rax
mov    (%rax),%rcx
lea    0xe25(%rip),%rdi
mov    $0x16,%esi
jmp    120f <gpio_open+0xbf>
mov    0x2de0(%rip),%rax
mov    (%rax),%rcx
lea    0xe48(%rip),%rdi
mov    $0x19,%esi
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%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"
@stderr = global i32* null
@.str.2 = [23 x i8] c"Could not open export\0A\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@.str.5 = [26 x i8] c"Could not open direction\0A\00"
@.str.6 = [4 x i8] c"low\00"
define i32 @gpio_open(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca [50 x i8]
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** %4
%7 = load i32*, i32** %4
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i32*, i32** @stderr
%11 = call i32 (i32*, i8*, ...) @fprintf(i32* %10, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %33
12: ; preds = %1
%13 = load i32*, i32** %4
%14 = load i32, i32* %3
%15 = call i32 (i32*, i8*, ...) @fprintf(i32* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %14)
%16 = load i32*, i32** %4
%17 = call i32 @fclose(i32* %16)
%18 = getelementptr inbounds [50 x i8], [50 x i8]* %5, i64 0, i64 0
%19 = load i32, i32* %3
%20 = call i32 @sprintf(i8* %18, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %19)
%21 = getelementptr inbounds [50 x i8], [50 x i8]* %5, i64 0, i64 0
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %22, i32** %4
%23 = load i32*, i32** %4
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %28
25: ; preds = %12
%26 = load i32*, i32** @stderr
%27 = call i32 (i32*, i8*, ...) @fprintf(i32* %26, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %2
br label %33
28: ; preds = %12
%29 = load i32*, i32** %4
%30 = call i32 (i32*, i8*, ...) @fprintf(i32* %29, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
%31 = load i32*, i32** %4
%32 = call i32 @fclose(i32* %31)
store i32 0, i32* %2
br label %33
33: ; preds = %28, %25, %9
%34 = load i32, i32* %2
ret i32 %34
}

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