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

/* Variables and functions */
char *get_homedir();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char const *);
const char *prepare_dir(const char *directory) {
  if (*directory == '~') {
    static char buffer[512] = {0};
    memset(buffer, 0, sizeof(buffer));
    snprintf(buffer, sizeof(buffer) - 1, "%s%s", get_homedir(), directory + 1);
    return buffer;
  }
  return directory;
}


x86 assembly (clang, -O0):
<prepare_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
jne    1199 <prepare_dir+0x69>
lea    0x2efd(%rip),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1040 <memset@plt>
mov    $0x0,%al
call   1050 <get_homedir@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%r8
add    $0x1,%r8
lea    0x2ed8(%rip),%rdi
mov    $0x1ff,%esi
lea    0xe7c(%rip),%rdx
call   1030 <snprintf@plt>
lea    0x2ec0(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11a1 <prepare_dir+0x71>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prepare_dir>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
cmpb   $0x7e,(%rdi)
jne    117a <prepare_dir+0x4a>
lea    0x2f0d(%rip),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1040 <memset@plt>
xor    %eax,%eax
call   1050 <get_homedir@plt>
add    $0x1,%rbx
lea    0xe9c(%rip),%rdx
mov    %r14,%rdi
mov    $0x1ff,%esi
mov    %rax,%rcx
mov    %rbx,%r8
call   1030 <snprintf@plt>
mov    %r14,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@prepare_dir.buffer = internal global [512 x i8] zeroinitializer
@.str = [5 x i8] c"%s%s\00"
define i8* @prepare_dir(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
store i8* %0, i8** %3
%4 = load i8*, i8** %3
%5 = load i8, i8* %4
%6 = sext i8 %5 to i32
%7 = icmp eq i32 %6, 126
br i1 %7, label %8, label %14
8: ; preds = %1
%9 = call i32 @memset(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @prepare_dir.buffer, i64 0, i64 0), i32 0, i32 512)
%10 = call i8* (...) @get_homedir()
%11 = load i8*, i8** %3
%12 = getelementptr inbounds i8, i8* %11, i64 1
%13 = call i32 @snprintf(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @prepare_dir.buffer, i64 0, i64 0), i32 511, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %10, i8* %12)
store i8* getelementptr inbounds ([512 x i8], [512 x i8]* @prepare_dir.buffer, i64 0, i64 0), i8** %2
br label %16
14: ; preds = %1
%15 = load i8*, i8** %3
store i8* %15, i8** %2
br label %16
16: ; preds = %14, %8
%17 = load i8*, i8** %2
ret i8* %17
}

LLVM IR (-O2):
@prepare_dir.buffer = internal global [512 x i8] zeroinitializer
@.str = [5 x i8] c"%s%s\00"
define i8* @prepare_dir(i8* %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 126
br i1 %3, label %4, label %9
4: ; preds = %1
%5 = tail call i32 @memset(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @prepare_dir.buffer, i64 0, i64 0), i32 0, i32 512)
%6 = tail call i8* (...) @get_homedir()
%7 = getelementptr inbounds i8, i8* %0, i64 1
%8 = tail call i32 @snprintf(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @prepare_dir.buffer, i64 0, i64 0), i32 511, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %6, i8* %7)
br label %9
9: ; preds = %1, %4
%10 = phi i8* [ getelementptr inbounds ([512 x i8], [512 x i8]* @prepare_dir.buffer, i64 0, i64 0), %4 ], [ %0, %1 ]
ret i8* %10
}