#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 O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int init_gpio(int gpio) {
  FILE *tmpf = fopen("/sys/class/gpio/export", "w");
  char path[42];
  fprintf(tmpf, "%d\n", gpio);
  fclose(tmpf);
  sprintf(path, "/sys/class/gpio/gpio%d/direction", gpio);
  tmpf = fopen(path, "w");
  fprintf(tmpf, "%s\n", "in");
  fclose(tmpf);
  sprintf(path, "/sys/class/gpio/gpio%d/edge", gpio);
  tmpf = fopen(path, "w");
  fprintf(tmpf, "%s\n", "falling");
  fclose(tmpf);
  sprintf(path, "/sys/class/gpio/gpio%d/value", gpio);
  int fd = open(path, O_RDWR | O_NONBLOCK);
  if (fd <= 0) {
    fprintf(stderr, "open of gpio %d returned %d: %s\n", gpio, fd,
            strerror(errno));
  }
  return fd;
}


x86 assembly (clang, -O0):
<init_gpio>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
lea    0xe8e(%rip),%rdi
lea    0xe9e(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,%edi
mov    -0x4(%rbp),%edx
lea    0xe87(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x40(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe6d(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x40(%rbp),%rdi
lea    0xe57(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,%edi
lea    0xec9(%rip),%rsi
lea    0xe61(%rip),%rdx
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x40(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe46(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x40(%rbp),%rdi
lea    0xe0c(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,%edi
lea    0xe7e(%rip),%rsi
lea    0xe35(%rip),%rdx
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x40(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe1f(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
mov    0x2d81(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jg     12b3 <init_gpio+0x153>
mov    0x2d56(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x44(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    0x2d36(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x50(%rbp),%edi
mov    -0x4c(%rbp),%edx
mov    -0x48(%rbp),%ecx
mov    %rax,%r8
lea    0xdd6(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x44(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_gpio>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %edi,%r15d
lea    0xe8a(%rip),%rdi
lea    0xe9a(%rip),%r14
mov    %r14,%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
lea    0xe8a(%rip),%rsi
mov    %ebx,%edi
mov    %r15d,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    0xe73(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r15d,%edx
call   1070 <sprintf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <fopen@plt>
mov    %rax,%rbp
lea    0xed2(%rip),%r12
lea    0xe6a(%rip),%rdx
mov    %ebp,%edi
mov    %r12,%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %rbp,%rdi
call   1030 <fclose@plt>
lea    0xe52(%rip),%rsi
mov    %rbx,%rdi
mov    %r15d,%edx
call   1070 <sprintf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <fopen@plt>
mov    %rax,%rbp
lea    0xe4e(%rip),%rdx
mov    %ebp,%edi
mov    %r12,%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %rbp,%rdi
call   1030 <fclose@plt>
lea    0xe3b(%rip),%rsi
mov    %rbx,%rdi
mov    %r15d,%edx
call   1070 <sprintf@plt>
mov    0x2dac(%rip),%rax
mov    0x2d9d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1050 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jg     1284 <init_gpio+0x124>
mov    0x2d74(%rip),%rax
mov    (%rax),%ebp
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe0f(%rip),%rsi
mov    %ebp,%edi
mov    %r15d,%edx
mov    %ebx,%ecx
mov    %rax,%r8
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %ebx,%eax
add    $0x30,%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 = [4 x i8] c"%d\0A\00"
@.str.3 = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@.str.4 = [4 x i8] c"%s\0A\00"
@.str.5 = [3 x i8] c"in\00"
@.str.6 = [28 x i8] c"/sys/class/gpio/gpio%d/edge\00"
@.str.7 = [8 x i8] c"falling\00"
@.str.8 = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@stderr = global i32 0
@.str.9 = [33 x i8] c"open of gpio %d returned %d: %s\0A\00"
@errno = global i32 0
define i32 @init_gpio(i32 %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca [42 x i8]
%5 = alloca i32
store i32 %0, i32* %2
%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** %3
%7 = load i32*, i32** %3
%8 = ptrtoint i32* %7 to i32
%9 = load i32, i32* %2
%10 = call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%11 = load i32*, i32** %3
%12 = call i32 @fclose(i32* %11)
%13 = getelementptr inbounds [42 x i8], [42 x i8]* %4, i64 0, i64 0
%14 = load i32, i32* %2
%15 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %14)
%16 = getelementptr inbounds [42 x i8], [42 x i8]* %4, i64 0, i64 0
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %3
%18 = load i32*, i32** %3
%19 = ptrtoint i32* %18 to i32
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%21 = load i32*, i32** %3
%22 = call i32 @fclose(i32* %21)
%23 = getelementptr inbounds [42 x i8], [42 x i8]* %4, i64 0, i64 0
%24 = load i32, i32* %2
%25 = call i32 @sprintf(i8* %23, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i32 %24)
%26 = getelementptr inbounds [42 x i8], [42 x i8]* %4, i64 0, i64 0
%27 = call i32* @fopen(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %27, i32** %3
%28 = load i32*, i32** %3
%29 = ptrtoint i32* %28 to i32
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0))
%31 = load i32*, i32** %3
%32 = call i32 @fclose(i32* %31)
%33 = getelementptr inbounds [42 x i8], [42 x i8]* %4, i64 0, i64 0
%34 = load i32, i32* %2
%35 = call i32 @sprintf(i8* %33, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.8, i64 0, i64 0), i32 %34)
%36 = getelementptr inbounds [42 x i8], [42 x i8]* %4, i64 0, i64 0
%37 = load i32, i32* @O_RDWR
%38 = load i32, i32* @O_NONBLOCK
%39 = or i32 %37, %38
%40 = call i32 @open(i8* %36, i32 %39)
store i32 %40, i32* %5
%41 = load i32, i32* %5
%42 = icmp sle i32 %41, 0
br i1 %42, label %43, label %50
43: ; preds = %1
%44 = load i32, i32* @stderr
%45 = load i32, i32* %2
%46 = load i32, i32* %5
%47 = load i32, i32* @errno
%48 = call i8* @strerror(i32 %47)
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.9, i64 0, i64 0), i32 %45, i32 %46, i8* %48)
br label %50
50: ; preds = %43, %1
%51 = load i32, i32* %5
ret i32 %51
}

LLVM IR (-O2):
@.str = [23 x i8] c"/sys/class/gpio/export\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@.str.4 = [4 x i8] c"%s\0A\00"
@.str.5 = [3 x i8] c"in\00"
@.str.6 = [28 x i8] c"/sys/class/gpio/gpio%d/edge\00"
@.str.7 = [8 x i8] c"falling\00"
@.str.8 = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@stderr = global i32 0
@.str.9 = [33 x i8] c"open of gpio %d returned %d: %s\0A\00"
@errno = global i32 0
define i32 @init_gpio(i32 %0) {
%2 = alloca [42 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 = getelementptr inbounds [42 x i8], [42 x i8]* %2, i64 0, i64 0
%5 = ptrtoint i32* %3 to i64
%6 = trunc i64 %5 to i32
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %0)
%8 = tail call i32 @fclose(i32* %3)
%9 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %0)
%10 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = ptrtoint i32* %10 to i64
%12 = trunc i64 %11 to i32
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%14 = tail call i32 @fclose(i32* %10)
%15 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i32 %0)
%16 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = ptrtoint i32* %16 to i64
%18 = trunc i64 %17 to i32
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0))
%20 = tail call i32 @fclose(i32* %16)
%21 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.8, i64 0, i64 0), i32 %0)
%22 = load i32, i32* @O_RDWR
%23 = load i32, i32* @O_NONBLOCK
%24 = or i32 %23, %22
%25 = call i32 @open(i8* %4, i32 %24)
%26 = icmp slt i32 %25, 1
br i1 %26, label %27, label %32
27: ; preds = %1
%28 = load i32, i32* @stderr
%29 = load i32, i32* @errno
%30 = tail call i8* @strerror(i32 %29)
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.9, i64 0, i64 0), i32 %0, i32 %25, i8* %30)
br label %32
32: ; preds = %27, %1
ret i32 %25
}