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

/* Variables and functions */
int EINVAL;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ goio_direction(unsigned int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned int);
int gpio_read_state(unsigned offset) {
  int ret;
  int fd;
  char buf[8];
  char file[128];
  goio_direction(offset, 0);
  sprintf(file,
          "/sys/class/gpio/"
          "gpio%d/value",
          offset);
  fd = open(file, O_RDONLY);
  if (fd < 0) {
    printf("open file [%s] failed [%d]\n", file, errno);
    return -EINVAL;
  }
  ret = read(fd, buf, sizeof(buf));
  if (ret < 0) {
    printf("read file [%s] failed [%d]\n", file, errno);
  } else {
    printf("gpio state is %s\n", buf);
    ret = 0;
  }
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<gpio_read_state>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1070 <goio_direction@plt>
lea    -0xa0(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe77(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0xa0(%rbp),%rdi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11e1 <gpio_read_state+0x81>
lea    -0xa0(%rbp),%rsi
mov    0x2e1a(%rip),%rax
mov    (%rax),%edx
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
mov    0x2df9(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1249 <gpio_read_state+0xe9>
mov    -0x10(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1050 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1222 <gpio_read_state+0xc2>
lea    -0xa0(%rbp),%rsi
mov    0x2dcb(%rip),%rax
mov    (%rax),%edx
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    123b <gpio_read_state+0xdb>
lea    -0x18(%rbp),%rsi
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0xc(%rbp)
mov    -0x10(%rbp),%edi
call   1040 <close@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_read_state>:
push   %rbp
push   %r14
push   %rbx
sub    $0x90,%rsp
mov    %edi,%ebp
xor    %r14d,%r14d
xor    %esi,%esi
call   1070 <goio_direction@plt>
lea    0xe82(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %ebp,%edx
call   1080 <sprintf@plt>
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11d1 <gpio_read_state+0x71>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1050 <read@plt>
test   %eax,%eax
js     11f9 <gpio_read_state+0x99>
lea    0xe95(%rip),%rdi
xor    %r14d,%r14d
lea    0x8(%rsp),%rsi
xor    %eax,%eax
call   1030 <printf@plt>
jmp    1218 <gpio_read_state+0xb8>
mov    0x2e00(%rip),%rax
mov    (%rax),%edx
lea    0xe3c(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x2ddc(%rip),%rax
sub    (%rax),%r14d
jmp    121f <gpio_read_state+0xbf>
mov    %eax,%r14d
mov    0x2dd5(%rip),%rax
mov    (%rax),%edx
lea    0xe2d(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r14d,%eax
add    $0x90,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_RDONLY = global i32 0
@.str.1 = [28 x i8] c"open file [%s] failed [%d]\0A\00"
@errno = global i32 0
@EINVAL = global i32 0
@.str.2 = [28 x i8] c"read file [%s] failed [%d]\0A\00"
@.str.3 = [18 x i8] c"gpio state is %s\0A\00"
define i32 @gpio_read_state(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [8 x i8]
%7 = alloca [128 x i8]
store i32 %0, i32* %3
%8 = load i32, i32* %3
%9 = call i32 @goio_direction(i32 %8, i32 0)
%10 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%11 = load i32, i32* %3
%12 = call i32 @sprintf(i8* %10, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %5
%16 = load i32, i32* %5
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %1
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%20 = load i32, i32* @errno
%21 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %19, i32 %20)
%22 = load i32, i32* @EINVAL
%23 = sub nsw i32 0, %22
store i32 %23, i32* %2
br label %41
24: ; preds = %1
%25 = load i32, i32* %5
%26 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 0
%27 = call i32 @read(i32 %25, i8* %26, i32 8)
store i32 %27, i32* %4
%28 = load i32, i32* %4
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %24
%31 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%32 = load i32, i32* @errno
%33 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %31, i32 %32)
br label %37
34: ; preds = %24
%35 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 0
%36 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %35)
store i32 0, i32* %4
br label %37
37: ; preds = %34, %30
%38 = load i32, i32* %5
%39 = call i32 @close(i32 %38)
%40 = load i32, i32* %4
store i32 %40, i32* %2
br label %41
41: ; preds = %37, %18
%42 = load i32, i32* %2
ret i32 %42
}

LLVM IR (-O2):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_RDONLY = global i32 0
@.str.1 = [28 x i8] c"open file [%s] failed [%d]\0A\00"
@errno = global i32 0
@EINVAL = global i32 0
@.str.2 = [28 x i8] c"read file [%s] failed [%d]\0A\00"
@.str.3 = [18 x i8] c"gpio state is %s\0A\00"
define i32 @gpio_read_state(i32 %0) {
%2 = alloca [8 x i8]
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%6 = tail call i32 @goio_direction(i32 %0, i32 0)
%7 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %0)
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %5, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %1
%12 = load i32, i32* @errno
%13 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %5, i32 %12)
%14 = load i32, i32* @EINVAL
%15 = sub nsw i32 0, %14
br label %27
16: ; preds = %1
%17 = call i32 @read(i32 %9, i8* %4, i32 8)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i32, i32* @errno
%21 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %5, i32 %20)
br label %24
22: ; preds = %16
%23 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %4)
br label %24
24: ; preds = %22, %19
%25 = phi i32 [ %17, %19 ], [ 0, %22 ]
%26 = call i32 @close(i32 %9)
br label %27
27: ; preds = %24, %11
%28 = phi i32 [ %15, %11 ], [ %25, %24 ]
ret i32 %28
}