#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 PATH_MAX;
int readlink(char *, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
char *strchr(char *, char);
void get_storage_path(char *out) {
  char buf[PATH_MAX];
  int sz;
  char *current;
  sz = readlink("/proc/self/exe", buf, PATH_MAX - 1);
  buf[sz] = '\0';
  current = strchr(buf, '/');
  while (current) {
    *current = '_';
    current = strchr(current, '/');
  }
  snprintf(out, PATH_MAX - 1, "/dev/shm/EC_persist_%s", buf);
  out[PATH_MAX - 1] = '\0';
}


x86 assembly (clang, -O0):
<get_storage_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2ead(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x30(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x18(%rbp)
mov    0x2e81(%rip),%rax
mov    (%rax),%edx
sub    $0x1,%edx
lea    0xe85(%rip),%rdi
call   1030 <readlink@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
movb   $0x0,(%rdi,%rax,1)
mov    $0x2f,%esi
call   1040 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11c6 <get_storage_path+0x96>
mov    -0x28(%rbp),%rax
movb   $0x5f,(%rax)
mov    -0x28(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
mov    %rax,-0x28(%rbp)
jmp    119d <get_storage_path+0x6d>
mov    -0x30(%rbp),%rcx
mov    -0x8(%rbp),%rdi
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
sub    $0x1,%esi
lea    0xe2e(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x8(%rbp),%rax
mov    0x2dff(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_storage_path>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2eac(%rip),%r15
mov    (%r15),%eax
mov    %rsp,%rbx
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rbx
mov    %rbx,%rsp
lea    -0x1(%rax),%edx
lea    0xe9b(%rip),%rdi
mov    %rbx,%rsi
call   1030 <readlink@plt>
cltq
movb   $0x0,(%rax,%rbx,1)
mov    %rbx,%rdi
cs nopw 0x0(%rax,%rax,1)
mov    $0x2f,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     1197 <get_storage_path+0x67>
movb   $0x5f,(%rax)
mov    %rax,%rdi
jmp    1180 <get_storage_path+0x50>
mov    (%r15),%esi
add    $0xffffffff,%esi
lea    0xe6b(%rip),%rdx
mov    %r14,%rdi
mov    %rbx,%rcx
call   1050 <snprintf@plt>
movslq (%r15),%rax
movb   $0x0,-0x1(%rax,%r14,1)
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [15 x i8] c"/proc/self/exe\00"
@.str.1 = [23 x i8] c"/dev/shm/EC_persist_%s\00"
define void @get_storage_path(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %2
%7 = load i32, i32* @PATH_MAX
%8 = zext i32 %7 to i64
%9 = call i8* @llvm.stacksave()
store i8* %9, i8** %3
%10 = alloca i8, i64 %8
store i64 %8, i64* %4
%11 = load i32, i32* @PATH_MAX
%12 = sub nsw i32 %11, 1
%13 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %10, i32 %12)
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %10, i64 %15
store i8 0, i8* %16
%17 = call i8* @strchr(i8* %10, i8 signext 47)
store i8* %17, i8** %6
br label %18
18: ; preds = %21, %1
%19 = load i8*, i8** %6
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = load i8*, i8** %6
store i8 95, i8* %22
%23 = load i8*, i8** %6
%24 = call i8* @strchr(i8* %23, i8 signext 47)
store i8* %24, i8** %6
br label %18
25: ; preds = %18
%26 = load i8*, i8** %2
%27 = load i32, i32* @PATH_MAX
%28 = sub nsw i32 %27, 1
%29 = call i32 @snprintf(i8* %26, i32 %28, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%30 = load i8*, i8** %2
%31 = load i32, i32* @PATH_MAX
%32 = sub nsw i32 %31, 1
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %30, i64 %33
store i8 0, i8* %34
%35 = load i8*, i8** %3
ret void
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [15 x i8] c"/proc/self/exe\00"
@.str.1 = [23 x i8] c"/dev/shm/EC_persist_%s\00"
define void @get_storage_path(i8* %0) {
%2 = load i32, i32* @PATH_MAX
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = add nsw i32 %2, -1
%6 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %4, i32 %5)
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds i8, i8* %4, i64 %7
store i8 0, i8* %8
%9 = call i8* @strchr(i8* %4, i8 signext 47)
%10 = icmp eq i8* %9, null
br i1 %10, label %15, label %11
11: ; preds = %1, %11
%12 = phi i8* [ %13, %11 ], [ %9, %1 ]
store i8 95, i8* %12
%13 = call i8* @strchr(i8* %12, i8 signext 47)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %11
15: ; preds = %11, %1
%16 = load i32, i32* @PATH_MAX
%17 = add nsw i32 %16, -1
%18 = call i32 @snprintf(i8* %0, i32 %17, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%19 = load i32, i32* @PATH_MAX
%20 = add nsw i32 %19, -1
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %0, i64 %21
store i8 0, i8* %22
ret void
}