#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);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write(int, char *, int);
int set_gpio_value(int gpio_base, int nchannel, int value) {
  char gpio_val_file[128];
  int val_fd = 0;
  int gpio_max;
  char val_str[2];
  int c;
  gpio_max = gpio_base + nchannel;
  for (c = gpio_base; c < gpio_max; 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;
    }
    sprintf(val_str, "%d", (value & 1));
    write(val_fd, val_str, sizeof(val_str));
    close(val_fd);
    value >>= 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_gpio_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x94(%rbp)
mov    -0x8(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0x98(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0xa0(%rbp)
mov    -0xa0(%rbp),%eax
cmp    -0x98(%rbp),%eax
jge    1255 <set_gpio_value+0x105>
lea    -0x90(%rbp),%rdi
mov    -0xa0(%rbp),%edx
lea    0xe57(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x90(%rbp),%rdi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x94(%rbp)
cmpl   $0x0,-0x94(%rbp)
jge    11fd <set_gpio_value+0xad>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
mov    -0xa0(%rbp),%edx
lea    0xe31(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125c <set_gpio_value+0x10c>
lea    -0x9a(%rbp),%rdi
mov    -0x10(%rbp),%edx
and    $0x1,%edx
lea    0xe33(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x94(%rbp),%edi
lea    -0x9a(%rbp),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    -0x94(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0xa0(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xa0(%rbp)
jmp    1183 <set_gpio_value+0x33>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_gpio_value>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
xor    %eax,%eax
test   %esi,%esi
jle    11ff <set_gpio_value+0xaf>
mov    %edx,%ebx
mov    %esi,%r15d
mov    %edi,%ebp
add    %edi,%r15d
lea    0x10(%rsp),%r13
lea    0xe(%rsp),%r12
nop
mov    %r13,%rdi
lea    0xe76(%rip),%rsi
mov    %ebp,%edx
call   1070 <sprintf@plt>
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
mov    %r13,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11e3 <set_gpio_value+0x93>
mov    %eax,%r14d
mov    %ebx,%edx
and    $0x1,%edx
mov    %r12,%rdi
lea    0xe8c(%rip),%rsi
call   1070 <sprintf@plt>
mov    %r14d,%edi
mov    %r12,%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    %r14d,%edi
call   1040 <close@plt>
sar    %ebx
add    $0x1,%ebp
cmp    %r15d,%ebp
jl     1180 <set_gpio_value+0x30>
xor    %eax,%eax
jmp    11ff <set_gpio_value+0xaf>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
lea    0xe2a(%rip),%rsi
mov    %ebp,%edx
call   1050 <fprintf@plt>
mov    $0xffffffff,%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"
@.str.2 = [3 x i8] c"%d\00"
define i32 @set_gpio_value(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [128 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca [2 x i8]
%12 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 0, i32* %9
%13 = load i32, i32* %5
%14 = load i32, i32* %6
%15 = add nsw i32 %13, %14
store i32 %15, i32* %10
%16 = load i32, i32* %5
store i32 %16, i32* %12
br label %17
17: ; preds = %46, %3
%18 = load i32, i32* %12
%19 = load i32, i32* %10
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %49
21: ; preds = %17
%22 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%23 = load i32, i32* %12
%24 = call i32 @sprintf(i8* %22, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%26 = load i32, i32* @O_RDWR
%27 = call i32 @open(i8* %25, i32 %26)
store i32 %27, i32* %9
%28 = load i32, i32* %9
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %21
%31 = load i32, i32* @stderr
%32 = load i32, i32* %12
%33 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %32)
store i32 -1, i32* %4
br label %50
34: ; preds = %21
%35 = getelementptr inbounds [2 x i8], [2 x i8]* %11, i64 0, i64 0
%36 = load i32, i32* %7
%37 = and i32 %36, 1
%38 = call i32 @sprintf(i8* %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %37)
%39 = load i32, i32* %9
%40 = getelementptr inbounds [2 x i8], [2 x i8]* %11, i64 0, i64 0
%41 = call i32 @write(i32 %39, i8* %40, i32 2)
%42 = load i32, i32* %9
%43 = call i32 @close(i32 %42)
%44 = load i32, i32* %7
%45 = ashr i32 %44, 1
store i32 %45, i32* %7
br label %46
46: ; preds = %34
%47 = load i32, i32* %12
%48 = add nsw i32 %47, 1
store i32 %48, i32* %12
br label %17
49: ; preds = %17
store i32 0, i32* %4
br label %50
50: ; preds = %49, %30
%51 = load i32, i32* %4
ret i32 %51
}

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"
@.str.2 = [3 x i8] c"%d\00"
define i32 @set_gpio_value(i32 %0, i32 %1, i32 %2) {
%4 = alloca [128 x i8]
%5 = alloca [2 x i8]
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%8 = add nsw i32 %1, %0
%9 = icmp sgt i32 %1, 0
br i1 %9, label %10, label %28
10: ; preds = %3, %20
%11 = phi i32 [ %26, %20 ], [ %0, %3 ]
%12 = phi i32 [ %25, %20 ], [ %2, %3 ]
%13 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %11)
%14 = load i32, i32* @O_RDWR
%15 = call i32 @open(i8* %6, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %10
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %11)
br label %28
20: ; preds = %10
%21 = and i32 %12, 1
%22 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %21)
%23 = call i32 @write(i32 %15, i8* %7, i32 2)
%24 = tail call i32 @close(i32 %15)
%25 = ashr i32 %12, 1
%26 = add nsw i32 %11, 1
%27 = icmp slt i32 %26, %8
br i1 %27, label %10, label %28
28: ; preds = %20, %3, %17
%29 = phi i32 [ -1, %17 ], [ 0, %3 ], [ 0, %20 ]
ret i32 %29
}