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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ strlen(char *);
scalar_t__ strtoul(char *, char **, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, scalar_t__);
int open_gpio_channel(int gpio_base) {
  char gpio_nchan_file[128];
  int gpio_nchan_fd;
  int gpio_max;
  int nchannel;
  char nchannel_str[5];
  char *cptr;
  int c;
  char channel_str[5];
  char *gpio_export_file = "/sys/class/gpio/export";
  int export_fd = 0;
  sprintf(gpio_nchan_file, "%s/gpiochip%d/ngpio", "/sys/class/gpio", gpio_base);
  gpio_nchan_fd = open(gpio_nchan_file, O_RDONLY);
  if (gpio_nchan_fd < 0) {
    fprintf(stderr, "Failed to open %s: %s\n", gpio_nchan_file,
            strerror(errno));
    return -1;
  }
  read(gpio_nchan_fd, nchannel_str, sizeof(nchannel_str));
  close(gpio_nchan_fd);
  nchannel = (int)strtoul(nchannel_str, &cptr, 0);
  if (cptr == nchannel_str) {
    fprintf(stderr, "Failed to change %s into GPIO channel number\n",
            nchannel_str);
    exit(1);
  }
  export_fd = open(gpio_export_file, O_WRONLY);
  if (export_fd < 0) {
    fprintf(stderr, "Cannot open GPIO to export %d\n", gpio_base);
    return -1;
  }
  gpio_max = gpio_base + nchannel;
  for (c = gpio_base; c < gpio_max; c++) {
    sprintf(channel_str, "%d", c);
    write(export_fd, channel_str, (strlen(channel_str) + 1));
  }
  close(export_fd);
  return nchannel;
}


x86 assembly (clang, -O0):
<open_gpio_channel>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
mov    %edi,-0x8(%rbp)
lea    0xe4b(%rip),%rax
mov    %rax,-0xc8(%rbp)
movl   $0x0,-0xcc(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x8(%rbp),%ecx
lea    0xe40(%rip),%rsi
lea    0xe4d(%rip),%rdx
mov    $0x0,%al
call   10a0 <sprintf@plt>
lea    -0x90(%rbp),%rdi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x94(%rbp)
cmpl   $0x0,-0x94(%rbp)
jge    1262 <open_gpio_channel+0xc2>
mov    0x2dc4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xdc(%rbp)
lea    -0x90(%rbp),%rax
mov    %rax,-0xd8(%rbp)
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0xdc(%rbp),%edi
mov    -0xd8(%rbp),%rdx
mov    %rax,%rcx
lea    0xdec(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d8 <open_gpio_channel+0x238>
mov    -0x94(%rbp),%edi
lea    -0xa1(%rbp),%rsi
mov    $0x5,%edx
call   1060 <read@plt>
mov    -0x94(%rbp),%edi
call   1050 <close@plt>
lea    -0xa1(%rbp),%rdi
lea    -0xb0(%rbp),%rsi
xor    %edx,%edx
call   1090 <strtoul@plt>
mov    %eax,-0x9c(%rbp)
mov    -0xb0(%rbp),%rax
lea    -0xa1(%rbp),%rcx
cmp    %rcx,%rax
jne    12de <open_gpio_channel+0x13e>
mov    0x2d1b(%rip),%rax
mov    (%rax),%edi
lea    -0xa1(%rbp),%rdx
lea    0xd85(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0xc8(%rbp),%rdi
mov    0x2cd4(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0xcc(%rbp)
cmpl   $0x0,-0xcc(%rbp)
jge    132c <open_gpio_channel+0x18c>
mov    0x2ccb(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xd67(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d8 <open_gpio_channel+0x238>
mov    -0x8(%rbp),%eax
add    -0x9c(%rbp),%eax
mov    %eax,-0x98(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0xb4(%rbp)
mov    -0xb4(%rbp),%eax
cmp    -0x98(%rbp),%eax
jge    13c4 <open_gpio_channel+0x224>
lea    -0xb9(%rbp),%rdi
mov    -0xb4(%rbp),%edx
lea    0xd35(%rip),%rsi
mov    $0x0,%al
call   10a0 <sprintf@plt>
mov    -0xcc(%rbp),%eax
mov    %eax,-0xec(%rbp)
lea    -0xb9(%rbp),%rax
mov    %rax,-0xe8(%rbp)
lea    -0xb9(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xec(%rbp),%edi
mov    -0xe8(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <write@plt>
mov    -0xb4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xb4(%rbp)
jmp    1344 <open_gpio_channel+0x1a4>
mov    -0xcc(%rbp),%edi
call   1050 <close@plt>
mov    -0x9c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_gpio_channel>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xa8,%rsp
mov    %edi,%ebx
lea    0xe5d(%rip),%rsi
lea    0xe6a(%rip),%rdx
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
mov    %ebx,%ecx
xor    %eax,%eax
call   10a0 <sprintf@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1298 <open_gpio_channel+0xf8>
mov    %eax,%ebp
lea    0xb(%rsp),%r15
mov    %eax,%edi
mov    %r15,%rsi
mov    $0x5,%edx
call   1060 <read@plt>
mov    %ebp,%edi
call   1050 <close@plt>
lea    0x10(%rsp),%rsi
mov    %r15,%rdi
xor    %edx,%edx
call   1090 <strtoul@plt>
cmp    %r15,0x10(%rsp)
je     12fd <open_gpio_channel+0x15d>
mov    %rax,%r14
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
lea    0xdcb(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     12c9 <open_gpio_channel+0x129>
mov    %eax,%r12d
test   %r14d,%r14d
jle    128e <open_gpio_channel+0xee>
lea    (%r14,%rbx,1),%r13d
lea    0xe4a(%rip),%r15
lea    0x1b(%rsp),%rbp
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   10a0 <sprintf@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %r12d,%edi
mov    %rbp,%rsi
call   1030 <write@plt>
add    $0x1,%ebx
cmp    %r13d,%ebx
jl     1260 <open_gpio_channel+0xc0>
mov    %r12d,%edi
call   1050 <close@plt>
jmp    12e8 <open_gpio_channel+0x148>
mov    0x2d39(%rip),%rax
mov    (%rax),%ebx
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd85(%rip),%rsi
lea    0x20(%rsp),%rdx
mov    %ebx,%edi
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    12e2 <open_gpio_channel+0x142>
mov    0x2d08(%rip),%rax
mov    (%rax),%edi
lea    0xda7(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0xa8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cd4(%rip),%rax
mov    (%rax),%edi
lea    0xd45(%rip),%rsi
lea    0xb(%rsp),%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [23 x i8] c"/sys/class/gpio/export\00"
@.str.1 = [20 x i8] c"%s/gpiochip%d/ngpio\00"
@.str.2 = [16 x i8] c"/sys/class/gpio\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.3 = [23 x i8] c"Failed to open %s: %s\0A\00"
@errno = global i32 0
@.str.4 = [46 x i8] c"Failed to change %s into GPIO channel number\0A\00"
@O_WRONLY = global i32 0
@.str.5 = [31 x i8] c"Cannot open GPIO to export %d\0A\00"
@.str.6 = [3 x i8] c"%d\00"
define i32 @open_gpio_channel(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [128 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [5 x i8]
%9 = alloca i8*
%10 = alloca i32
%11 = alloca [5 x i8]
%12 = alloca i8*
%13 = alloca i32
store i32 %0, i32* %3
store i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8** %12
store i32 0, i32* %13
%14 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%15 = load i32, i32* %3
%16 = call i32 (i8*, i8*, ...) @sprintf(i8* %14, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %1
%23 = load i32, i32* @stderr
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %24, i8* %26)
store i32 -1, i32* %2
br label %81
28: ; preds = %1
%29 = load i32, i32* %5
%30 = getelementptr inbounds [5 x i8], [5 x i8]* %8, i64 0, i64 0
%31 = call i32 @read(i32 %29, i8* %30, i32 5)
%32 = load i32, i32* %5
%33 = call i32 @close(i32 %32)
%34 = getelementptr inbounds [5 x i8], [5 x i8]* %8, i64 0, i64 0
%35 = call i64 @strtoul(i8* %34, i8** %9, i32 0)
%36 = trunc i64 %35 to i32
store i32 %36, i32* %7
%37 = load i8*, i8** %9
%38 = getelementptr inbounds [5 x i8], [5 x i8]* %8, i64 0, i64 0
%39 = icmp eq i8* %37, %38
br i1 %39, label %40, label %45
40: ; preds = %28
%41 = load i32, i32* @stderr
%42 = getelementptr inbounds [5 x i8], [5 x i8]* %8, i64 0, i64 0
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0), i8* %42)
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %28
%46 = load i8*, i8** %12
%47 = load i32, i32* @O_WRONLY
%48 = call i32 @open(i8* %46, i32 %47)
store i32 %48, i32* %13
%49 = load i32, i32* %13
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %45
%52 = load i32, i32* @stderr
%53 = load i32, i32* %3
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 %53)
store i32 -1, i32* %2
br label %81
55: ; preds = %45
%56 = load i32, i32* %3
%57 = load i32, i32* %7
%58 = add nsw i32 %56, %57
store i32 %58, i32* %6
%59 = load i32, i32* %3
store i32 %59, i32* %10
br label %60
60: ; preds = %74, %55
%61 = load i32, i32* %10
%62 = load i32, i32* %6
%63 = icmp slt i32 %61, %62
br i1 %63, label %64, label %77
64: ; preds = %60
%65 = getelementptr inbounds [5 x i8], [5 x i8]* %11, i64 0, i64 0
%66 = load i32, i32* %10
%67 = call i32 (i8*, i8*, ...) @sprintf(i8* %65, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32 %66)
%68 = load i32, i32* %13
%69 = getelementptr inbounds [5 x i8], [5 x i8]* %11, i64 0, i64 0
%70 = getelementptr inbounds [5 x i8], [5 x i8]* %11, i64 0, i64 0
%71 = call i64 @strlen(i8* %70)
%72 = add nsw i64 %71, 1
%73 = call i32 @write(i32 %68, i8* %69, i64 %72)
br label %74
74: ; preds = %64
%75 = load i32, i32* %10
%76 = add nsw i32 %75, 1
store i32 %76, i32* %10
br label %60
77: ; preds = %60
%78 = load i32, i32* %13
%79 = call i32 @close(i32 %78)
%80 = load i32, i32* %7
store i32 %80, i32* %2
br label %81
81: ; preds = %77, %51, %22
%82 = load i32, i32* %2
ret i32 %82
}

LLVM IR (-O2):
@.str = [23 x i8] c"/sys/class/gpio/export\00"
@.str.1 = [20 x i8] c"%s/gpiochip%d/ngpio\00"
@.str.2 = [16 x i8] c"/sys/class/gpio\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.3 = [23 x i8] c"Failed to open %s: %s\0A\00"
@errno = global i32 0
@.str.4 = [46 x i8] c"Failed to change %s into GPIO channel number\0A\00"
@O_WRONLY = global i32 0
@.str.5 = [31 x i8] c"Cannot open GPIO to export %d\0A\00"
@.str.6 = [3 x i8] c"%d\00"
define i32 @open_gpio_channel(i32 %0) {
%2 = alloca [128 x i8]
%3 = alloca [5 x i8]
%4 = alloca i8*
%5 = alloca [5 x i8]
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%7 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%8 = bitcast i8** %4 to i8*
%9 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 0
%10 = call i32 (i8*, i8*, ...) @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %0)
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %6, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %1
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = tail call i8* @strerror(i32 %16)
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %6, i8* %17)
br label %50
19: ; preds = %1
%20 = call i32 @read(i32 %12, i8* %7, i32 5)
%21 = tail call i32 @close(i32 %12)
%22 = call i64 @strtoul(i8* %7, i8** %4, i32 0)
%23 = trunc i64 %22 to i32
%24 = load i8*, i8** %4
%25 = icmp eq i8* %24, %7
br i1 %25, label %26, label %30
26: ; preds = %19
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0), i8* %7)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %19
%31 = load i32, i32* @O_WRONLY
%32 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %31)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 %0)
br label %50
37: ; preds = %30
%38 = add nsw i32 %23, %0
%39 = icmp sgt i32 %23, 0
br i1 %39, label %40, label %48
40: ; preds = %37, %40
%41 = phi i32 [ %46, %40 ], [ %0, %37 ]
%42 = call i32 (i8*, i8*, ...) @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32 %41)
%43 = call i64 @strlen(i8* %9)
%44 = add nsw i64 %43, 1
%45 = call i32 @write(i32 %32, i8* %9, i64 %44)
%46 = add nsw i32 %41, 1
%47 = icmp slt i32 %46, %38
br i1 %47, label %40, label %48
48: ; preds = %40, %37
%49 = call i32 @close(i32 %32)
br label %50
50: ; preds = %48, %34, %14
%51 = phi i32 [ -1, %14 ], [ -1, %34 ], [ %23, %48 ]
ret i32 %51
}