#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 /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ getenv(char *);
int /*<<< orphan*/ stderr;
char *strcat(char *, char *);
char *expand_filename(char *str) {
  if (str[0] != '~')
    return str;
  str++;
  if ((int)getenv("HOME") != -1) {
    char *home;
    home = (char *)getenv("HOME");
    str = strcat(home, str);
  } else {
    (void)fprintf(stderr, "Error: environment variable HOME is not set.\n");
    exit(-1);
  }
  return str;
}


x86 assembly (clang, -O0):
<expand_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
je     1169 <expand_filename+0x29>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d7 <expand_filename+0x97>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
lea    0xe84(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0xffffffff,%eax
je     11b0 <expand_filename+0x70>
lea    0xe6f(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strcat@plt>
mov    %rax,-0x10(%rbp)
jmp    11cf <expand_filename+0x8f>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
lea    0xe45(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<expand_filename>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
cmpb   $0x7e,(%rdi)
jne    1173 <expand_filename+0x33>
lea    0xead(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r14
cmp    $0xffffffff,%r14d
je     117e <expand_filename+0x3e>
add    $0x1,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <strcat@plt>
mov    %r14,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
lea    0xe77(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"HOME\00"
@stderr = global i32 0
@.str.1 = [46 x i8] c"Error: environment variable HOME is not set.\0A\00"
define i8* @expand_filename(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = getelementptr inbounds i8, i8* %5, i64 0
%7 = load i8, i8* %6
%8 = sext i8 %7 to i32
%9 = icmp ne i32 %8, 126
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = load i8*, i8** %3
store i8* %11, i8** %2
br label %30
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = getelementptr inbounds i8, i8* %13, i32 1
store i8* %14, i8** %3
%15 = call i64 @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%16 = trunc i64 %15 to i32
%17 = icmp ne i32 %16, -1
br i1 %17, label %18, label %24
18: ; preds = %12
%19 = call i64 @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %4
%21 = load i8*, i8** %4
%22 = load i8*, i8** %3
%23 = call i8* @strcat(i8* %21, i8* %22)
store i8* %23, i8** %3
br label %28
24: ; preds = %12
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 -1)
unreachable
28: ; preds = %18
%29 = load i8*, i8** %3
store i8* %29, i8** %2
br label %30
30: ; preds = %28, %10
%31 = load i8*, i8** %2
ret i8* %31
}

LLVM IR (-O2):
@.str = [5 x i8] c"HOME\00"
@stderr = global i32 0
@.str.1 = [46 x i8] c"Error: environment variable HOME is not set.\0A\00"
define i8* @expand_filename(i8* %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 126
br i1 %3, label %4, label %16
4: ; preds = %1
%5 = tail call i64 @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%6 = trunc i64 %5 to i32
%7 = icmp eq i32 %6, -1
br i1 %7, label %12, label %8
8: ; preds = %4
%9 = getelementptr inbounds i8, i8* %0, i64 1
%10 = inttoptr i64 %5 to i8*
%11 = tail call i8* @strcat(i8* dereferenceable(1) %10, i8* %9)
br label %16
12: ; preds = %4
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32 @exit(i32 -1)
unreachable
16: ; preds = %1, %8
%17 = phi i8* [ %10, %8 ], [ %0, %1 ]
ret i8* %17
}