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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/);
char *optarg;
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ strtoul(char *, char **, int /*<<< orphan*/);
int get_gpio_value(int gpio_base, int nchannel) {
  char gpio_val_file[128];
  int val_fd = 0;
  int gpio_max;
  char val_str[2];
  char *cptr;
  int value = 0;
  int c;
  gpio_max = gpio_base + nchannel;
  for (c = gpio_max - 1; c >= gpio_base; c--) {
    sprintf(gpio_val_file, "/sys/class/gpio/gpio%d/value", c);
    val_fd = open(gpio_val_file, O_RDWR);
    if (val_fd < 0) {
      fprintf(stderr, "Cannot open the value file of GPIO %d\n", c);
      return -1;
    }
    read(val_fd, val_str, sizeof(val_str));
    value <<= 1;
    value += (int)strtoul(val_str, &cptr, 0);
    if (cptr == optarg) {
      fprintf(stderr, "Failed to change %s into integer", val_str);
    }
    close(val_fd);
  }
  return value;
}


x86 assembly (clang, -O0):
<get_gpio_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x94(%rbp)
movl   $0x0,-0xac(%rbp)
mov    -0x8(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0x98(%rbp)
mov    -0x98(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0xb0(%rbp)
mov    -0xb0(%rbp),%eax
cmp    -0x8(%rbp),%eax
jl     12b4 <get_gpio_value+0x154>
lea    -0x90(%rbp),%rdi
mov    -0xb0(%rbp),%edx
lea    0xe3d(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x90(%rbp),%rdi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x94(%rbp)
cmpl   $0x0,-0x94(%rbp)
jge    1219 <get_gpio_value+0xb9>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
mov    -0xb0(%rbp),%edx
lea    0xe17(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bd <get_gpio_value+0x15d>
mov    -0x94(%rbp),%edi
lea    -0x9a(%rbp),%rsi
mov    $0x2,%edx
call   1040 <read@plt>
mov    -0xac(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0xac(%rbp)
lea    -0x9a(%rbp),%rdi
lea    -0xa8(%rbp),%rsi
xor    %edx,%edx
call   1070 <strtoul@plt>
add    -0xac(%rbp),%eax
mov    %eax,-0xac(%rbp)
mov    -0xa8(%rbp),%rax
mov    0x2d8a(%rip),%rcx
cmp    (%rcx),%rax
jne    1295 <get_gpio_value+0x135>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
lea    -0x9a(%rbp),%rdx
lea    0xdb6(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x94(%rbp),%edi
call   1030 <close@plt>
mov    -0xb0(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xb0(%rbp)
jmp    11a0 <get_gpio_value+0x40>
mov    -0xac(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_gpio_value>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
test   %esi,%esi
jle    1237 <get_gpio_value+0xd7>
mov    %esi,%r13d
mov    %edi,%r12d
add    %edi,%r13d
xor    %ebx,%ebx
jmp    11a8 <get_gpio_value+0x48>
cs nopw 0x0(%rax,%rax,1)
lea    0x0(%rbp,%rbx,2),%ebx
mov    %r15d,%edi
call   1030 <close@plt>
mov    %r14d,%r13d
cmp    %r12d,%r14d
jle    125e <get_gpio_value+0xfe>
lea    -0x1(%r13),%r14d
lea    0x10(%rsp),%r15
mov    %r15,%rdi
lea    0xe45(%rip),%rsi
mov    %r14d,%edx
call   1080 <sprintf@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     123b <get_gpio_value+0xdb>
mov    %eax,%r15d
mov    %eax,%edi
lea    0x6(%rsp),%rbp
mov    %rbp,%rsi
mov    $0x2,%edx
call   1040 <read@plt>
mov    %rbp,%rdi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1070 <strtoul@plt>
mov    %rax,%rbp
mov    0x8(%rsp),%rax
mov    0x2deb(%rip),%rcx
cmp    (%rcx),%rax
jne    1190 <get_gpio_value+0x30>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
lea    0xe1e(%rip),%rsi
lea    0x6(%rsp),%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1190 <get_gpio_value+0x30>
xor    %ebx,%ebx
jmp    125e <get_gpio_value+0xfe>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
add    $0xffffffff,%r13d
lea    0xdce(%rip),%rsi
mov    %r13d,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [39 x i8] c"Cannot open the value file of GPIO %d\0A\00"
@optarg = global i8* null
@.str.2 = [33 x i8] c"Failed to change %s into integer\00"
define i32 @get_gpio_value(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [128 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca [2 x i8]
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 0, i32* %7
store i32 0, i32* %11
%13 = load i32, i32* %4
%14 = load i32, i32* %5
%15 = add nsw i32 %13, %14
store i32 %15, i32* %8
%16 = load i32, i32* %8
%17 = sub nsw i32 %16, 1
store i32 %17, i32* %12
br label %18
18: ; preds = %56, %2
%19 = load i32, i32* %12
%20 = load i32, i32* %4
%21 = icmp sge i32 %19, %20
br i1 %21, label %22, label %59
22: ; preds = %18
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%24 = load i32, i32* %12
%25 = call i32 @sprintf(i8* %23, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %24)
%26 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%27 = load i32, i32* @O_RDWR
%28 = call i32 @open(i8* %26, i32 %27)
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %22
%32 = load i32, i32* @stderr
%33 = load i32, i32* %12
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %33)
store i32 -1, i32* %3
br label %61
35: ; preds = %22
%36 = load i32, i32* %7
%37 = getelementptr inbounds [2 x i8], [2 x i8]* %9, i64 0, i64 0
%38 = call i32 @read(i32 %36, i8* %37, i32 2)
%39 = load i32, i32* %11
%40 = shl i32 %39, 1
store i32 %40, i32* %11
%41 = getelementptr inbounds [2 x i8], [2 x i8]* %9, i64 0, i64 0
%42 = call i64 @strtoul(i8* %41, i8** %10, i32 0)
%43 = trunc i64 %42 to i32
%44 = load i32, i32* %11
%45 = add nsw i32 %44, %43
store i32 %45, i32* %11
%46 = load i8*, i8** %10
%47 = load i8*, i8** @optarg
%48 = icmp eq i8* %46, %47
br i1 %48, label %49, label %53
49: ; preds = %35
%50 = load i32, i32* @stderr
%51 = getelementptr inbounds [2 x i8], [2 x i8]* %9, i64 0, i64 0
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %51)
br label %53
53: ; preds = %49, %35
%54 = load i32, i32* %7
%55 = call i32 @close(i32 %54)
br label %56
56: ; preds = %53
%57 = load i32, i32* %12
%58 = add nsw i32 %57, -1
store i32 %58, i32* %12
br label %18
59: ; preds = %18
%60 = load i32, i32* %11
store i32 %60, i32* %3
br label %61
61: ; preds = %59, %31
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [39 x i8] c"Cannot open the value file of GPIO %d\0A\00"
@optarg = global i8* null
@.str.2 = [33 x i8] c"Failed to change %s into integer\00"
define i32 @get_gpio_value(i32 %0, i32 %1) {
%3 = alloca [128 x i8]
%4 = alloca [2 x i8]
%5 = alloca i8*
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%8 = bitcast i8** %5 to i8*
%9 = icmp sgt i32 %1, 0
br i1 %9, label %10, label %38
10: ; preds = %2
%11 = add nsw i32 %1, %0
br label %12
12: ; preds = %10, %35
%13 = phi i32 [ %15, %35 ], [ %11, %10 ]
%14 = phi i32 [ %28, %35 ], [ 0, %10 ]
%15 = add nsw i32 %13, -1
%16 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = load i32, i32* @O_RDWR
%18 = call i32 @open(i8* %6, i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %12
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %15)
br label %38
23: ; preds = %12
%24 = call i32 @read(i32 %18, i8* %7, i32 2)
%25 = shl i32 %14, 1
%26 = call i64 @strtoul(i8* %7, i8** %5, i32 0)
%27 = trunc i64 %26 to i32
%28 = add nsw i32 %25, %27
%29 = load i8*, i8** %5
%30 = load i8*, i8** @optarg
%31 = icmp eq i8* %29, %30
br i1 %31, label %32, label %35
32: ; preds = %23
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %7)
br label %35
35: ; preds = %32, %23
%36 = call i32 @close(i32 %18)
%37 = icmp sgt i32 %15, %0
br i1 %37, label %12, label %38
38: ; preds = %35, %2, %20
%39 = phi i32 [ -1, %20 ], [ 0, %2 ], [ %28, %35 ]
ret i32 %39
}