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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int strtol(char const *, char **, int);
int init_gpio(const char *arg) {
  int gpio;
  int fd;
  char b[256];
  char *end;
  gpio = strtol(arg, &end, 10);
  if (*end)
    return -1;
  snprintf(b, sizeof(b), "%s%d/value", "/sys/class/gpio/gpio", gpio);
  fd = open(b, O_RDWR);
  if (fd < 0) {
    fprintf(stderr, "Open %s: %s\n", b, strerror(errno));
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<init_gpio>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x128(%rbp),%rsi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %eax,-0x14(%rbp)
mov    -0x128(%rbp),%rax
cmpb   $0x0,(%rax)
je     1193 <init_gpio+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    1231 <init_gpio+0xe1>
lea    -0x120(%rbp),%rdi
mov    -0x14(%rbp),%r8d
mov    $0x100,%esi
lea    0xe56(%rip),%rdx
lea    0xe5a(%rip),%rcx
call   1030 <snprintf@plt>
lea    -0x120(%rbp),%rdi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    122b <init_gpio+0xdb>
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x134(%rbp)
lea    -0x120(%rbp),%rax
mov    %rax,-0x130(%rbp)
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x134(%rbp),%edi
mov    -0x130(%rbp),%rdx
mov    %rax,%rcx
lea    0xe06(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1231 <init_gpio+0xe1>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_gpio>:
push   %rbx
sub    $0x110,%rsp
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    0x8(%rsp),%rdx
mov    $0xffffffff,%ecx
cmpb   $0x0,(%rdx)
jne    11e2 <init_gpio+0x92>
lea    0xe83(%rip),%rdx
lea    0xe87(%rip),%rcx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %eax,%r8d
call   1030 <snprintf@plt>
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
mov    %eax,%ecx
test   %eax,%eax
jns    11e2 <init_gpio+0x92>
mov    0x2e21(%rip),%rax
mov    (%rax),%ebx
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe52(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
mov    %rax,%rcx
call   1040 <fprintf@plt>
mov    $0xffffffff,%ecx
mov    %ecx,%eax
add    $0x110,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [11 x i8] c"%s%d/value\00"
@.str.1 = [21 x i8] c"/sys/class/gpio/gpio\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.2 = [13 x i8] c"Open %s: %s\0A\00"
@errno = global i32 0
define i32 @init_gpio(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [256 x i8]
%7 = alloca i8*
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32 @strtol(i8* %8, i8** %7, i32 10)
store i32 %9, i32* %4
%10 = load i8*, i8** %7
%11 = load i8, i8* %10
%12 = icmp ne i8 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 -1, i32* %2
br label %31
14: ; preds = %1
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%16 = load i32, i32* %4
%17 = call i32 @snprintf(i8* %15, i32 256, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%19 = load i32, i32* @O_RDWR
%20 = call i32 @open(i8* %18, i32 %19)
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %14
%24 = load i32, i32* @stderr
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %25, i8* %27)
store i32 -1, i32* %2
br label %31
29: ; preds = %14
%30 = load i32, i32* %5
store i32 %30, i32* %2
br label %31
31: ; preds = %29, %23, %13
%32 = load i32, i32* %2
ret i32 %32
}

LLVM IR (-O2):
@.str = [11 x i8] c"%s%d/value\00"
@.str.1 = [21 x i8] c"/sys/class/gpio/gpio\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.2 = [13 x i8] c"Open %s: %s\0A\00"
@errno = global i32 0
define i32 @init_gpio(i8* %0) {
%2 = alloca [256 x i8]
%3 = alloca i8*
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%5 = bitcast i8** %3 to i8*
%6 = call i32 @strtol(i8* %0, i8** %3, i32 10)
%7 = load i8*, i8** %3
%8 = load i8, i8* %7
%9 = icmp eq i8 %8, 0
br i1 %9, label %10, label %20
10: ; preds = %1
%11 = call i32 @snprintf(i8* %4, i32 256, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%12 = load i32, i32* @O_RDWR
%13 = call i32 @open(i8* %4, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %10
%16 = load i32, i32* @stderr
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %4, i8* %18)
br label %20
20: ; preds = %10, %1, %15
%21 = phi i32 [ -1, %15 ], [ -1, %1 ], [ %13, %10 ]
ret i32 %21
}