#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 *getenv(char *);
char *malloc(int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char const *);
char *strdup(char const *);
int strlen(char const *);
char *string_expand_home(const char *path) {
  char *ptr_home, *str;
  int length;
  if (!path)
    return NULL;
  if (!path[0] || (path[0] != '~') || ((path[1] && path[1] != '/'))) {
    return strdup(path);
  }
  ptr_home = getenv("HOME");
  if (!ptr_home)
    return NULL;
  length = strlen(ptr_home) + strlen(path + 1) + 1;
  str = malloc(length);
  if (!str)
    return strdup(path);
  snprintf(str, length, "%s%s", ptr_home, path + 1);
  return str;
}


x86 assembly (clang, -O0):
<string_expand_home>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1174 <string_expand_home+0x24>
movq   $0x0,-0x8(%rbp)
jmp    1263 <string_expand_home+0x113>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     11b3 <string_expand_home+0x63>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
jne    11b3 <string_expand_home+0x63>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
je     11c5 <string_expand_home+0x75>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x2f,%eax
je     11c5 <string_expand_home+0x75>
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1263 <string_expand_home+0x113>
lea    0xe34(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ed <string_expand_home+0x9d>
movq   $0x0,-0x8(%rbp)
jmp    1263 <string_expand_home+0x113>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    123c <string_expand_home+0xec>
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1263 <string_expand_home+0x113>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%r8
add    $0x1,%r8
lea    0xdaf(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_expand_home>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdi,%rdi
je     11d7 <string_expand_home+0x87>
mov    %rdi,%rbx
cmpb   $0x7e,(%rdi)
jne    11c7 <string_expand_home+0x77>
mov    0x1(%rbx),%al
test   %al,%al
je     1170 <string_expand_home+0x20>
cmp    $0x2f,%al
jne    11c7 <string_expand_home+0x77>
lea    0xe89(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11d7 <string_expand_home+0x87>
mov    %rax,%r14
lea    0x1(%rbx),%r15
mov    %rax,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r15,%rdi
call   1040 <strlen@plt>
add    %eax,%ebp
add    $0x1,%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11c7 <string_expand_home+0x77>
mov    %rax,%r12
lea    0xe50(%rip),%rdx
mov    %rax,%rdi
mov    %ebp,%esi
mov    %r14,%rcx
mov    %r15,%r8
call   1050 <snprintf@plt>
jmp    11da <string_expand_home+0x8a>
mov    %rbx,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <strdup@plt>
xor    %r12d,%r12d
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [5 x i8] c"%s%s\00"
define i8* @string_expand_home(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = icmp ne i8* %7, null
br i1 %8, label %10, label %9
9: ; preds = %1
store i8* null, i8** %2
br label %64
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = getelementptr inbounds i8, i8* %11, i64 0
%13 = load i8, i8* %12
%14 = icmp ne i8 %13, 0
br i1 %14, label %15, label %33
15: ; preds = %10
%16 = load i8*, i8** %3
%17 = getelementptr inbounds i8, i8* %16, i64 0
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp ne i32 %19, 126
br i1 %20, label %33, label %21
21: ; preds = %15
%22 = load i8*, i8** %3
%23 = getelementptr inbounds i8, i8* %22, i64 1
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %36
27: ; preds = %21
%28 = load i8*, i8** %3
%29 = getelementptr inbounds i8, i8* %28, i64 1
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp ne i32 %31, 47
br i1 %32, label %33, label %36
33: ; preds = %27, %15, %10
%34 = load i8*, i8** %3
%35 = call i8* @strdup(i8* %34)
store i8* %35, i8** %2
br label %64
36: ; preds = %27, %21
%37 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %37, i8** %4
%38 = load i8*, i8** %4
%39 = icmp ne i8* %38, null
br i1 %39, label %41, label %40
40: ; preds = %36
store i8* null, i8** %2
br label %64
41: ; preds = %36
%42 = load i8*, i8** %4
%43 = call i32 @strlen(i8* %42)
%44 = load i8*, i8** %3
%45 = getelementptr inbounds i8, i8* %44, i64 1
%46 = call i32 @strlen(i8* %45)
%47 = add nsw i32 %43, %46
%48 = add nsw i32 %47, 1
store i32 %48, i32* %6
%49 = load i32, i32* %6
%50 = call i8* @malloc(i32 %49)
store i8* %50, i8** %5
%51 = load i8*, i8** %5
%52 = icmp ne i8* %51, null
br i1 %52, label %56, label %53
53: ; preds = %41
%54 = load i8*, i8** %3
%55 = call i8* @strdup(i8* %54)
store i8* %55, i8** %2
br label %64
56: ; preds = %41
%57 = load i8*, i8** %5
%58 = load i32, i32* %6
%59 = load i8*, i8** %4
%60 = load i8*, i8** %3
%61 = getelementptr inbounds i8, i8* %60, i64 1
%62 = call i32 @snprintf(i8* %57, i32 %58, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %59, i8* %61)
%63 = load i8*, i8** %5
store i8* %63, i8** %2
br label %64
64: ; preds = %56, %53, %40, %33, %9
%65 = load i8*, i8** %2
ret i8* %65
}

LLVM IR (-O2):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [5 x i8] c"%s%s\00"
define i8* @string_expand_home(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %25, label %3
3: ; preds = %1
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 126
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = getelementptr inbounds i8, i8* %0, i64 1
%8 = load i8, i8* %7
switch i8 %8, label %9 [
i8 0, label %11
i8 47, label %11
]
9: ; preds = %3, %6
%10 = tail call i8* @strdup(i8* %0)
br label %25
11: ; preds = %6, %6
%12 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i8* %12, null
br i1 %13, label %25, label %14
14: ; preds = %11
%15 = tail call i32 @strlen(i8* %12)
%16 = tail call i32 @strlen(i8* %7)
%17 = add i32 %15, 1
%18 = add i32 %17, %16
%19 = tail call i8* @malloc(i32 %18)
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = tail call i8* @strdup(i8* %0)
br label %25
23: ; preds = %14
%24 = tail call i32 @snprintf(i8* %19, i32 %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %12, i8* %7)
br label %25
25: ; preds = %11, %1, %23, %21, %9
%26 = phi i8* [ %10, %9 ], [ %19, %23 ], [ %22, %21 ], [ null, %1 ], [ null, %11 ]
ret i8* %26
}