#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 R_OK;
int W_OK;
scalar_t__ access(char *, int);
char *getenv(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
scalar_t__ strlen(char *);
char *gpio_path(char *envvar) {
  char *name = getenv(envvar);
  if (!name) {
    printf("ERROR: Could not find environment variable: %s\n", envvar);
    return NULL;
  }
  char *fmt = "/sys/class/gpio/%s/value";
  char *res = malloc(strlen(name) + strlen(fmt) - 1);
  if (!res) {
    puts("ERROR: out of memory");
    return NULL;
  }
  sprintf(res, fmt, name);
  if (access(res, R_OK | W_OK)) {
    perror("ERROR");
    return NULL;
  }
  return res;
}


x86 assembly (clang, -O0):
<gpio_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c1 <gpio_path+0x41>
mov    -0x10(%rbp),%rsi
lea    0xe51(%rip),%rdi
call   1060 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1271 <gpio_path+0xf1>
lea    0xe68(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x30(%rbp),%rdi
add    %rax,%rdi
sub    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    121a <gpio_path+0x9a>
lea    0xe41(%rip),%rdi
call   1040 <puts@plt>
movq   $0x0,-0x8(%rbp)
jmp    1271 <gpio_path+0xf1>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   10a0 <sprintf@plt>
mov    -0x28(%rbp),%rdi
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
mov    0x2da9(%rip),%rax
or     (%rax),%esi
call   1080 <access@plt>
cmp    $0x0,%rax
je     1269 <gpio_path+0xe9>
lea    0xe07(%rip),%rdi
call   1090 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1271 <gpio_path+0xf1>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_path>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
call   1030 <getenv@plt>
test   %rax,%rax
je     11ed <gpio_path+0x6d>
mov    %rax,%r15
mov    %rax,%rdi
call   1050 <strlen@plt>
lea    0x17(%rax),%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     11fe <gpio_path+0x7e>
mov    %rax,%r14
lea    0xe7b(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
call   10a0 <sprintf@plt>
mov    0x2e11(%rip),%rax
mov    0x2e1a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r14,%rdi
call   1080 <access@plt>
test   %rax,%rax
je     120d <gpio_path+0x8d>
lea    0xe78(%rip),%rdi
call   1090 <perror@plt>
jmp    120a <gpio_path+0x8a>
lea    0xe0c(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
jmp    120a <gpio_path+0x8a>
lea    0xe44(%rip),%rdi
call   1040 <puts@plt>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [48 x i8] c"ERROR: Could not find environment variable: %s\0A\00"
@.str.1 = [25 x i8] c"/sys/class/gpio/%s/value\00"
@.str.2 = [21 x i8] c"ERROR: out of memory\00"
@R_OK = global i32 0
@W_OK = global i32 0
@.str.3 = [6 x i8] c"ERROR\00"
define i8* @gpio_path(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i8* @getenv(i8* %7)
store i8* %8, i8** %4
%9 = load i8*, i8** %4
%10 = icmp ne i8* %9, null
br i1 %10, label %14, label %11
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8* %12)
store i8* null, i8** %2
br label %41
14: ; preds = %1
store i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8** %5
%15 = load i8*, i8** %4
%16 = call i64 @strlen(i8* %15)
%17 = load i8*, i8** %5
%18 = call i64 @strlen(i8* %17)
%19 = add nsw i64 %16, %18
%20 = sub nsw i64 %19, 1
%21 = call i8* @malloc(i64 %20)
store i8* %21, i8** %6
%22 = load i8*, i8** %6
%23 = icmp ne i8* %22, null
br i1 %23, label %26, label %24
24: ; preds = %14
%25 = call i32 @puts(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %2
br label %41
26: ; preds = %14
%27 = load i8*, i8** %6
%28 = load i8*, i8** %5
%29 = load i8*, i8** %4
%30 = call i32 @sprintf(i8* %27, i8* %28, i8* %29)
%31 = load i8*, i8** %6
%32 = load i32, i32* @R_OK
%33 = load i32, i32* @W_OK
%34 = or i32 %32, %33
%35 = call i64 @access(i8* %31, i32 %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %26
%38 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i8* null, i8** %2
br label %41
39: ; preds = %26
%40 = load i8*, i8** %6
store i8* %40, i8** %2
br label %41
41: ; preds = %39, %37, %24, %11
%42 = load i8*, i8** %2
ret i8* %42
}

LLVM IR (-O2):
@.str = [48 x i8] c"ERROR: Could not find environment variable: %s\0A\00"
@.str.1 = [25 x i8] c"/sys/class/gpio/%s/value\00"
@.str.2 = [21 x i8] c"ERROR: out of memory\00"
@R_OK = global i32 0
@W_OK = global i32 0
@.str.3 = [6 x i8] c"ERROR\00"
define noalias i8* @gpio_path(i8* %0) {
%2 = tail call i8* @getenv(i8* %0)
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %22
6: ; preds = %1
%7 = tail call i64 @strlen(i8* %2)
%8 = add nsw i64 %7, 23
%9 = tail call i8* @malloc(i64 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %22
13: ; preds = %6
%14 = tail call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%15 = load i32, i32* @R_OK
%16 = load i32, i32* @W_OK
%17 = or i32 %16, %15
%18 = tail call i64 @access(i8* %9, i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %13
%21 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %22
22: ; preds = %11, %20, %13, %4
%23 = phi i8* [ null, %4 ], [ null, %20 ], [ null, %11 ], [ %9, %13 ]
ret i8* %23
}