#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 readlink(char *, char *, int);
int snprintf(char *, int, char *, char *);
void get_storage_path(char *out) {
  char buf[1024];
  int sz;
  sz = readlink("/proc/self/exe", buf, 1024);
  buf[sz] = '\0';
  if (snprintf(out, 1024, "%s_persist", buf) >= 1024)
    out[1024 - 1] = '\0';
}


x86 assembly (clang, -O0):
<get_storage_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x410(%rbp),%rsi
lea    0xec3(%rip),%rdi
mov    $0x400,%edx
call   1030 <readlink@plt>
mov    %eax,-0x414(%rbp)
movslq -0x414(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
mov    -0x8(%rbp),%rdi
lea    -0x410(%rbp),%rcx
mov    $0x400,%esi
lea    0xe9c(%rip),%rdx
call   1040 <snprintf@plt>
cmp    $0x400,%eax
jl     118e <get_storage_path+0x6e>
mov    -0x8(%rbp),%rax
movb   $0x0,0x3ff(%rax)
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_storage_path>:
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%rbx
lea    0xecc(%rip),%rdi
mov    %rsp,%r14
mov    %r14,%rsi
mov    $0x400,%edx
call   1030 <readlink@plt>
cltq
movb   $0x0,(%rsp,%rax,1)
lea    0xebe(%rip),%rdx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r14,%rcx
call   1040 <snprintf@plt>
cmp    $0x400,%eax
jl     116f <get_storage_path+0x4f>
movb   $0x0,0x3ff(%rbx)
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [15 x i8] c"/proc/self/exe\00"
@.str.1 = [11 x i8] c"%s_persist\00"
define void @get_storage_path(i8* %0) {
%2 = alloca i8*
%3 = alloca [1024 x i8]
%4 = alloca i32
store i8* %0, i8** %2
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %5, i32 1024)
store i32 %6, i32* %4
%7 = load i32, i32* %4
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %8
store i8 0, i8* %9
%10 = load i8*, i8** %2
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%12 = call i32 @snprintf(i8* %10, i32 1024, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%13 = icmp sge i32 %12, 1024
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = load i8*, i8** %2
%16 = getelementptr inbounds i8, i8* %15, i64 1023
store i8 0, i8* %16
br label %17
17: ; preds = %14, %1
ret void
}

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/self/exe\00"
@.str.1 = [11 x i8] c"%s_persist\00"
define void @get_storage_path(i8* %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %3, i32 1024)
%5 = sext i32 %4 to i64
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %5
store i8 0, i8* %6
%7 = call i32 @snprintf(i8* %0, i32 1024, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%8 = icmp sgt i32 %7, 1023
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = getelementptr inbounds i8, i8* %0, i64 1023
store i8 0, i8* %10
br label %11
11: ; preds = %9, %1
ret void
}