#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 */
char *get_homedir();
int /*<<< orphan*/ strcpy(char *, char const *);
size_t strlen(char const *);
char *xrealloc(char *, size_t);
char *xstrdup(char const *);
char *expand_tilde(const char *filename) {
  char *new_filename;
  size_t homedirlen;
  if (filename[0] == '~') {
    new_filename = get_homedir();
    homedirlen = strlen(new_filename);
    new_filename =
        xrealloc(new_filename, (homedirlen + strlen(filename)) * sizeof(char));
    strcpy(new_filename + homedirlen, filename + 1);
    return new_filename;
  } else {
    return xstrdup(filename);
  }
}


x86 assembly (clang, -O0):
<expand_tilde>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
jne    11d7 <expand_tilde+0x87>
mov    $0x0,%al
call   1070 <get_homedir@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdi
add    %rax,%rsi
shl    $0x0,%rsi
call   1050 <xrealloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x1,%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11e4 <expand_tilde+0x94>
mov    -0x10(%rbp),%rdi
call   1060 <xstrdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<expand_tilde>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
cmpb   $0x7e,(%rdi)
jne    11a4 <expand_tilde+0x54>
xor    %eax,%eax
call   1070 <get_homedir@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    (%rax,%r15,1),%rsi
mov    %r14,%rdi
call   1050 <xrealloc@plt>
mov    %rax,%r14
add    %rax,%r15
add    $0x1,%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1060 <xstrdup@plt>

LLVM IR (-O0):
define i8* @expand_tilde(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = getelementptr inbounds i8, i8* %6, i64 0
%8 = load i8, i8* %7
%9 = sext i8 %8 to i32
%10 = icmp eq i32 %9, 126
br i1 %10, label %11, label %29
11: ; preds = %1
%12 = call i8* (...) @get_homedir()
store i8* %12, i8** %4
%13 = load i8*, i8** %4
%14 = call i64 @strlen(i8* %13)
store i64 %14, i64* %5
%15 = load i8*, i8** %4
%16 = load i64, i64* %5
%17 = load i8*, i8** %3
%18 = call i64 @strlen(i8* %17)
%19 = add i64 %16, %18
%20 = mul i64 %19, 1
%21 = call i8* @xrealloc(i8* %15, i64 %20)
store i8* %21, i8** %4
%22 = load i8*, i8** %4
%23 = load i64, i64* %5
%24 = getelementptr inbounds i8, i8* %22, i64 %23
%25 = load i8*, i8** %3
%26 = getelementptr inbounds i8, i8* %25, i64 1
%27 = call i32 @strcpy(i8* %24, i8* %26)
%28 = load i8*, i8** %4
store i8* %28, i8** %2
br label %32
29: ; preds = %1
%30 = load i8*, i8** %3
%31 = call i8* @xstrdup(i8* %30)
store i8* %31, i8** %2
br label %32
32: ; preds = %29, %11
%33 = load i8*, i8** %2
ret i8* %33
}

LLVM IR (-O2):
define i8* @expand_tilde(i8* %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 126
br i1 %3, label %4, label %13
4: ; preds = %1
%5 = tail call i8* (...) @get_homedir()
%6 = tail call i64 @strlen(i8* dereferenceable(1) %5)
%7 = tail call i64 @strlen(i8* %0)
%8 = add i64 %7, %6
%9 = tail call i8* @xrealloc(i8* %5, i64 %8)
%10 = getelementptr inbounds i8, i8* %9, i64 %6
%11 = getelementptr inbounds i8, i8* %0, i64 1
%12 = tail call i32 @strcpy(i8* %10, i8* %11)
br label %15
13: ; preds = %1
%14 = tail call i8* @xstrdup(i8* %0)
br label %15
15: ; preds = %13, %4
%16 = phi i8* [ %9, %4 ], [ %14, %13 ]
ret i8* %16
}