#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ write(int, char const *, int);
int pin_read(const char *pin) {
  int exporter = open("/sys/class/gpio/export", O_WRONLY);
  if (exporter < 0) {
    printf("fail to open exporter\n");
    return -1;
  }
  write(exporter, pin, 2);
  close(exporter);
  char dirstr[80];
  strcpy(dirstr, "/sys/class/gpio");
  strcat(dirstr, pin);
  strcat(dirstr, "/direction");
  int direction = open(dirstr, O_WRONLY);
  if (direction < 0) {
    printf("fail to open direction\n");
    return -1;
  }
  write(direction, "in", 2);
  close(direction);
  char valstr[80];
  strcpy(valstr, "/sys/class/gpio");
  strcat(valstr, pin);
  strcat(valstr, "/value");
  int value = open(valstr, O_RDONLY);
  if (value < 0) {
    printf("fail to open value\n");
    return -1;
  }
}


x86 assembly (clang, -O0):
<pin_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11a9 <pin_read+0x49>
lea    0xe7f(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b3 <pin_read+0x153>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x2,%edx
call   1040 <write@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
lea    -0x70(%rbp),%rdi
lea    0xe61(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x70(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strcat@plt>
lea    -0x70(%rbp),%rdi
lea    0xe54(%rip),%rsi
call   1080 <strcat@plt>
lea    -0x70(%rbp),%rdi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x74(%rbp)
jge    1226 <pin_read+0xc6>
lea    0xe34(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b3 <pin_read+0x153>
mov    -0x74(%rbp),%edi
lea    0xe31(%rip),%rsi
mov    $0x2,%edx
call   1040 <write@plt>
mov    -0x74(%rbp),%edi
call   1060 <close@plt>
lea    -0xd0(%rbp),%rdi
lea    0xdde(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0xd0(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strcat@plt>
lea    -0xd0(%rbp),%rdi
lea    0xdf1(%rip),%rsi
call   1080 <strcat@plt>
lea    -0xd0(%rbp),%rdi
mov    0x2d62(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0xd4(%rbp)
cmpl   $0x0,-0xd4(%rbp)
jge    12b3 <pin_read+0x153>
lea    0xdc4(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pin_read>:
push   %rbp
push   %r14
push   %rbx
sub    $0xa0,%rsp
mov    %rdi,%rbx
mov    0x2e5b(%rip),%r14
mov    (%r14),%esi
lea    0xe81(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1247 <pin_read+0xe7>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x2,%edx
call   1050 <write@plt>
mov    %ebp,%edi
call   1060 <close@plt>
lea    0xe6c(%rip),%rsi
lea    0x50(%rsp),%rbp
mov    %rbp,%rdi
call   1030 <strcpy@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1080 <strcat@plt>
lea    0xe5d(%rip),%rsi
mov    %rbp,%rdi
call   1080 <strcat@plt>
mov    (%r14),%esi
mov    %rbp,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1250 <pin_read+0xf0>
mov    %eax,%ebp
lea    0xe48(%rip),%rsi
mov    %eax,%edi
mov    $0x2,%edx
call   1050 <write@plt>
mov    %ebp,%edi
call   1060 <close@plt>
lea    0xe13(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1030 <strcpy@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1080 <strcat@plt>
lea    0xe14(%rip),%rsi
mov    %rbp,%rdi
call   1080 <strcat@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1070 <open@plt>
test   %eax,%eax
jns    125c <pin_read+0xfc>
lea    0xdf7(%rip),%rdi
jmp    1257 <pin_read+0xf7>
lea    0xe18(%rip),%rdi
jmp    1257 <pin_read+0xf7>
lea    0xdf8(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
add    $0xa0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"/sys/class/gpio/export\00"
@O_WRONLY = global i32 0
@.str.1 = [23 x i8] c"fail to open exporter\0A\00"
@.str.2 = [16 x i8] c"/sys/class/gpio\00"
@.str.3 = [11 x i8] c"/direction\00"
@.str.4 = [24 x i8] c"fail to open direction\0A\00"
@.str.5 = [3 x i8] c"in\00"
@.str.6 = [7 x i8] c"/value\00"
@O_RDONLY = global i32 0
@.str.7 = [20 x i8] c"fail to open value\0A\00"
define i32 @pin_read(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [80 x i8]
%6 = alloca i32
%7 = alloca [80 x i8]
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i32, i32* @O_WRONLY
%10 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %54
15: ; preds = %1
%16 = load i32, i32* %4
%17 = load i8*, i8** %3
%18 = call i32 @write(i32 %16, i8* %17, i32 2)
%19 = load i32, i32* %4
%20 = call i32 @close(i32 %19)
%21 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%22 = call i32 @strcpy(i8* %21, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%23 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%24 = load i8*, i8** %3
%25 = call i32 @strcat(i8* %23, i8* %24)
%26 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%27 = call i32 @strcat(i8* %26, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%28 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%29 = load i32, i32* @O_WRONLY
%30 = call i32 @open(i8* %28, i32 %29)
store i32 %30, i32* %6
%31 = load i32, i32* %6
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %15
%34 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %2
br label %54
35: ; preds = %15
%36 = load i32, i32* %6
%37 = call i32 @write(i32 %36, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 2)
%38 = load i32, i32* %6
%39 = call i32 @close(i32 %38)
%40 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%41 = call i32 @strcpy(i8* %40, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%42 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%43 = load i8*, i8** %3
%44 = call i32 @strcat(i8* %42, i8* %43)
%45 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%46 = call i32 @strcat(i8* %45, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%47 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%48 = load i32, i32* @O_RDONLY
%49 = call i32 @open(i8* %47, i32 %48)
store i32 %49, i32* %8
%50 = load i32, i32* %8
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %35
%53 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %2
br label %54
54: ; preds = %13, %33, %52, %35
%55 = load i32, i32* %2
ret i32 %55
}

LLVM IR (-O2):
@.str = [23 x i8] c"/sys/class/gpio/export\00"
@O_WRONLY = global i32 0
@.str.2 = [16 x i8] c"/sys/class/gpio\00"
@.str.3 = [11 x i8] c"/direction\00"
@.str.5 = [3 x i8] c"in\00"
@.str.6 = [7 x i8] c"/value\00"
@O_RDONLY = global i32 0
@str = [19 x i8] c"fail to open value\00"
@str.8 = [23 x i8] c"fail to open direction\00"
@str.9 = [22 x i8] c"fail to open exporter\00"
define i32 @pin_read(i8* %0) {
%2 = alloca [80 x i8]
%3 = alloca [80 x i8]
%4 = load i32, i32* @O_WRONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.9, i64 0, i64 0))
br label %35
9: ; preds = %1
%10 = tail call i32 @write(i32 %5, i8* %0, i32 2)
%11 = tail call i32 @close(i32 %5)
%12 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%13 = call i32 @strcpy(i8* %12, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%14 = call i32 @strcat(i8* %12, i8* %0)
%15 = call i32 @strcat(i8* %12, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%16 = load i32, i32* @O_WRONLY
%17 = call i32 @open(i8* %12, i32 %16)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %9
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.8, i64 0, i64 0))
br label %34
21: ; preds = %9
%22 = call i32 @write(i32 %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 2)
%23 = call i32 @close(i32 %17)
%24 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%25 = call i32 @strcpy(i8* %24, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @strcat(i8* %24, i8* %0)
%27 = call i32 @strcat(i8* %24, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%28 = load i32, i32* @O_RDONLY
%29 = call i32 @open(i8* %24, i32 %28)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %21
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %33
33: ; preds = %21, %31
br label %34
34: ; preds = %33, %19
br label %35
35: ; preds = %34, %7
ret i32 -1
}