#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 strlen(char const *);
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, strlen(pin));
  close(exporter);
  char dirstr[80];
  strcpy(dirstr, "/sys/class/gpio/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/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    $0xf0,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11b9 <pin_read+0x49>
lea    0xe6f(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e3 <pin_read+0x173>
mov    -0x14(%rbp),%eax
mov    %eax,-0xe4(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0xe0(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0xe4(%rbp),%edi
mov    -0xe0(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
lea    -0x70(%rbp),%rdi
lea    0xe31(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x70(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <strcat@plt>
lea    -0x70(%rbp),%rdi
lea    0xe29(%rip),%rsi
call   1090 <strcat@plt>
lea    -0x70(%rbp),%rdi
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x74(%rbp)
jge    1256 <pin_read+0xe6>
lea    0xe09(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e3 <pin_read+0x173>
mov    -0x74(%rbp),%edi
lea    0xe06(%rip),%rsi
mov    $0x2,%edx
call   1040 <write@plt>
mov    -0x74(%rbp),%edi
call   1070 <close@plt>
lea    -0xd0(%rbp),%rdi
lea    0xdae(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0xd0(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <strcat@plt>
lea    -0xd0(%rbp),%rdi
lea    0xdc6(%rip),%rsi
call   1090 <strcat@plt>
lea    -0xd0(%rbp),%rdi
mov    0x2d32(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0xd4(%rbp)
cmpl   $0x0,-0xd4(%rbp)
jge    12e3 <pin_read+0x173>
lea    0xd99(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pin_read>:
push   %rbp
push   %r14
push   %rbx
sub    $0xa0,%rsp
mov    %rdi,%rbx
mov    0x2e4b(%rip),%r14
mov    (%r14),%esi
lea    0xe71(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     125c <pin_read+0xec>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    %ebp,%edi
call   1070 <close@plt>
lea    0xe57(%rip),%rsi
lea    0x50(%rsp),%rbp
mov    %rbp,%rdi
call   1030 <strcpy@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1090 <strcat@plt>
lea    0xe4d(%rip),%rsi
mov    %rbp,%rdi
call   1090 <strcat@plt>
mov    (%r14),%esi
mov    %rbp,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1265 <pin_read+0xf5>
mov    %eax,%ebp
lea    0xe38(%rip),%rsi
mov    %eax,%edi
mov    $0x2,%edx
call   1050 <write@plt>
mov    %ebp,%edi
call   1070 <close@plt>
lea    0xdfe(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1030 <strcpy@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1090 <strcat@plt>
lea    0xe04(%rip),%rsi
mov    %rbp,%rdi
call   1090 <strcat@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1080 <open@plt>
test   %eax,%eax
jns    1271 <pin_read+0x101>
lea    0xde7(%rip),%rdi
jmp    126c <pin_read+0xfc>
lea    0xe08(%rip),%rdi
jmp    126c <pin_read+0xfc>
lea    0xde8(%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 = [21 x i8] c"/sys/class/gpio/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 %56
15: ; preds = %1
%16 = load i32, i32* %4
%17 = load i8*, i8** %3
%18 = load i8*, i8** %3
%19 = call i32 @strlen(i8* %18)
%20 = call i32 @write(i32 %16, i8* %17, i32 %19)
%21 = load i32, i32* %4
%22 = call i32 @close(i32 %21)
%23 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%24 = call i32 @strcpy(i8* %23, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%25 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%26 = load i8*, i8** %3
%27 = call i32 @strcat(i8* %25, i8* %26)
%28 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%29 = call i32 @strcat(i8* %28, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%30 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%31 = load i32, i32* @O_WRONLY
%32 = call i32 @open(i8* %30, i32 %31)
store i32 %32, i32* %6
%33 = load i32, i32* %6
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %15
%36 = 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 %56
37: ; preds = %15
%38 = load i32, i32* %6
%39 = call i32 @write(i32 %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 2)
%40 = load i32, i32* %6
%41 = call i32 @close(i32 %40)
%42 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%43 = call i32 @strcpy(i8* %42, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%44 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%45 = load i8*, i8** %3
%46 = call i32 @strcat(i8* %44, i8* %45)
%47 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%48 = call i32 @strcat(i8* %47, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%49 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%50 = load i32, i32* @O_RDONLY
%51 = call i32 @open(i8* %49, i32 %50)
store i32 %51, i32* %8
%52 = load i32, i32* %8
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %37
%55 = 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 %56
56: ; preds = %13, %35, %54, %37
%57 = load i32, i32* %2
ret i32 %57
}

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