#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");
  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    124b <string_expand_home+0xfb>
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    124b <string_expand_home+0xfb>
lea    0xe34(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
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    1224 <string_expand_home+0xd4>
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    124b <string_expand_home+0xfb>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%r8
add    $0x1,%r8
lea    0xdc7(%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     11d2 <string_expand_home+0x82>
mov    %rdi,%rbx
cmpb   $0x7e,(%rdi)
jne    11c2 <string_expand_home+0x72>
mov    0x1(%rbx),%al
test   %al,%al
je     1170 <string_expand_home+0x20>
cmp    $0x2f,%al
jne    11c2 <string_expand_home+0x72>
lea    0x1(%rbx),%r14
lea    0xe85(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1040 <strlen@plt>
add    %eax,%ebp
add    $0x1,%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11c2 <string_expand_home+0x72>
mov    %rax,%r12
lea    0xe55(%rip),%rdx
mov    %rax,%rdi
mov    %ebp,%esi
mov    %r15,%rcx
mov    %r14,%r8
call   1050 <snprintf@plt>
jmp    11d5 <string_expand_home+0x85>
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 %60
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 %60
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 = call i32 @strlen(i8* %38)
%40 = load i8*, i8** %3
%41 = getelementptr inbounds i8, i8* %40, i64 1
%42 = call i32 @strlen(i8* %41)
%43 = add nsw i32 %39, %42
%44 = add nsw i32 %43, 1
store i32 %44, i32* %6
%45 = load i32, i32* %6
%46 = call i8* @malloc(i32 %45)
store i8* %46, i8** %5
%47 = load i8*, i8** %5
%48 = icmp ne i8* %47, null
br i1 %48, label %52, label %49
49: ; preds = %36
%50 = load i8*, i8** %3
%51 = call i8* @strdup(i8* %50)
store i8* %51, i8** %2
br label %60
52: ; preds = %36
%53 = load i8*, i8** %5
%54 = load i32, i32* %6
%55 = load i8*, i8** %4
%56 = load i8*, i8** %3
%57 = getelementptr inbounds i8, i8* %56, i64 1
%58 = call i32 @snprintf(i8* %53, i32 %54, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %55, i8* %57)
%59 = load i8*, i8** %5
store i8* %59, i8** %2
br label %60
60: ; preds = %52, %49, %33, %9
%61 = load i8*, i8** %2
ret i8* %61
}

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 %23, 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 %23
11: ; preds = %6, %6
%12 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @strlen(i8* %12)
%14 = tail call i32 @strlen(i8* %7)
%15 = add i32 %13, 1
%16 = add i32 %15, %14
%17 = tail call i8* @malloc(i32 %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %21
19: ; preds = %11
%20 = tail call i8* @strdup(i8* %0)
br label %23
21: ; preds = %11
%22 = tail call i32 @snprintf(i8* %17, i32 %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %12, i8* %7)
br label %23
23: ; preds = %1, %21, %19, %9
%24 = phi i8* [ %10, %9 ], [ %17, %21 ], [ %20, %19 ], [ null, %1 ]
ret i8* %24
}