#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_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;
scalar_t__ strlen(char *);
int /*<<< orphan*/ write(int, char *, scalar_t__);
int set_gpio_direction(int gpio_base, int nchannel, char *direction) {
  char gpio_dir_file[128];
  int direction_fd = 0;
  int gpio_max;
  int c;
  gpio_max = gpio_base + nchannel;
  for (c = gpio_base; c < gpio_max; c++) {
    sprintf(gpio_dir_file, "/sys/class/gpio/gpio%d/direction", c);
    direction_fd = open(gpio_dir_file, O_RDWR);
    if (direction_fd < 0) {
      fprintf(stderr, "Cannot open the direction file for GPIO %d\n", c);
      return 1;
    }
    write(direction_fd, direction, (strlen(direction) + 1));
    close(direction_fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_gpio_direction>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0xa4(%rbp)
mov    -0x8(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xa8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0xac(%rbp)
mov    -0xac(%rbp),%eax
cmp    -0xa8(%rbp),%eax
jge    1266 <set_gpio_direction+0x106>
lea    -0xa0(%rbp),%rdi
mov    -0xac(%rbp),%edx
lea    0xe46(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0xa0(%rbp),%rdi
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0xa4(%rbp)
cmpl   $0x0,-0xa4(%rbp)
jge    120e <set_gpio_direction+0xae>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
mov    -0xac(%rbp),%edx
lea    0xe24(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    126d <set_gpio_direction+0x10d>
mov    -0xa4(%rbp),%eax
mov    %eax,-0xbc(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0xb8(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xbc(%rbp),%edi
mov    -0xb8(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <write@plt>
mov    -0xa4(%rbp),%edi
call   1050 <close@plt>
mov    -0xac(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xac(%rbp)
jmp    1194 <set_gpio_direction+0x34>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_gpio_direction>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
xor    %eax,%eax
test   %esi,%esi
jle    11fb <set_gpio_direction+0x9b>
mov    %rdx,%rbx
mov    %esi,%r15d
mov    %edi,%ebp
add    %edi,%r15d
mov    %rsp,%r13
mov    0x2e58(%rip),%r12
mov    %r13,%rdi
lea    0xe66(%rip),%rsi
mov    %ebp,%edx
call   1080 <sprintf@plt>
mov    (%r12),%esi
mov    %r13,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11df <set_gpio_direction+0x7f>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1030 <write@plt>
mov    %r14d,%edi
call   1050 <close@plt>
add    $0x1,%ebp
cmp    %r15d,%ebp
jl     1190 <set_gpio_direction+0x30>
xor    %eax,%eax
jmp    11fb <set_gpio_direction+0x9b>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
lea    0xe32(%rip),%rsi
mov    %ebp,%edx
call   1060 <fprintf@plt>
mov    $0x1,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [44 x i8] c"Cannot open the direction file for GPIO %d\0A\00"
define i32 @set_gpio_direction(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca [128 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 0, i32* %9
%12 = load i32, i32* %5
%13 = load i32, i32* %6
%14 = add nsw i32 %12, %13
store i32 %14, i32* %10
%15 = load i32, i32* %5
store i32 %15, i32* %11
br label %16
16: ; preds = %42, %3
%17 = load i32, i32* %11
%18 = load i32, i32* %10
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %45
20: ; preds = %16
%21 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%22 = load i32, i32* %11
%23 = call i32 @sprintf(i8* %21, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %22)
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%25 = load i32, i32* @O_RDWR
%26 = call i32 @open(i8* %24, i32 %25)
store i32 %26, i32* %9
%27 = load i32, i32* %9
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %20
%30 = load i32, i32* @stderr
%31 = load i32, i32* %11
%32 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %31)
store i32 1, i32* %4
br label %46
33: ; preds = %20
%34 = load i32, i32* %9
%35 = load i8*, i8** %7
%36 = load i8*, i8** %7
%37 = call i64 @strlen(i8* %36)
%38 = add nsw i64 %37, 1
%39 = call i32 @write(i32 %34, i8* %35, i64 %38)
%40 = load i32, i32* %9
%41 = call i32 @close(i32 %40)
br label %42
42: ; preds = %33
%43 = load i32, i32* %11
%44 = add nsw i32 %43, 1
store i32 %44, i32* %11
br label %16
45: ; preds = %16
store i32 0, i32* %4
br label %46
46: ; preds = %45, %29
%47 = load i32, i32* %4
ret i32 %47
}

LLVM IR (-O2):
@.str = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [44 x i8] c"Cannot open the direction file for GPIO %d\0A\00"
define i32 @set_gpio_direction(i32 %0, i32 %1, i8* nocapture %2) {
%4 = alloca [128 x i8]
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%6 = add nsw i32 %1, %0
%7 = icmp sgt i32 %1, 0
br i1 %7, label %8, label %24
8: ; preds = %3, %17
%9 = phi i32 [ %22, %17 ], [ %0, %3 ]
%10 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = load i32, i32* @O_RDWR
%12 = call i32 @open(i8* %5, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %8
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %9)
br label %24
17: ; preds = %8
%18 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%19 = add nsw i64 %18, 1
%20 = tail call i32 @write(i32 %12, i8* %2, i64 %19)
%21 = tail call i32 @close(i32 %12)
%22 = add nsw i32 %9, 1
%23 = icmp slt i32 %22, %6
br i1 %23, label %8, label %24
24: ; preds = %17, %3, %14
%25 = phi i32 [ 1, %14 ], [ 0, %3 ], [ 0, %17 ]
ret i32 %25
}