#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*/ fprintf(int /*<<< orphan*/, char *);
char *getenv(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char const *);
int /*<<< orphan*/ strncat(char *, char const *, size_t);
void expand_home(const char *from, char *to, size_t maxlen) {
  char *home = getenv("HOME");
  size_t len;
  size_t space;
  if (!home) {
    fprintf(stderr, "Warning: Can't expand $HOME\n");
    to[0] = '\0';
    space = maxlen - 1;
  } else {
    len = strlen(home);
    if (len >= maxlen) {
      fprintf(stderr, "Warning: Truncating in ~-expansion\n");
      to[0] = '\0';
      strncat(to, from, maxlen - 1);
      return;
    } else {
      strcpy(to, home);
      from++;
      space = maxlen - len - 1;
    }
  }
  len = strlen(from);
  if (len > space) {
    fprintf(stderr, "Warning: Truncating in ~-expansion\n");
    strncat(to, from, space);
  } else {
    strcat(to, from);
  }
}


x86 assembly (clang, -O0):
<expand_home>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    0xe85(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11bc <expand_home+0x5c>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
lea    0xe66(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    123b <expand_home+0xdb>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     120d <expand_home+0xad>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
lea    0xe3b(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
call   1060 <strncat@plt>
jmp    128e <expand_home+0x12e>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x18(%rbp),%rax
sub    -0x28(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    123b <expand_home+0xdb>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jbe    1281 <expand_home+0x121>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
lea    0xdbc(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1060 <strncat@plt>
jmp    128e <expand_home+0x12e>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1080 <strcat@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<expand_home>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xe87(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11ad <expand_home+0x4d>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <strlen@plt>
cmp    %r12,%rax
jae    11ee <expand_home+0x8e>
mov    %rax,%r13
mov    %r15,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
add    $0x1,%r14
not    %r13
add    %r13,%r12
jmp    11ca <expand_home+0x6a>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
lea    0xe48(%rip),%rsi
call   1070 <fprintf@plt>
movb   $0x0,(%r15)
add    $0xffffffffffffffff,%r12
mov    %r14,%rdi
call   1050 <strlen@plt>
cmp    %r12,%rax
jbe    1222 <expand_home+0xc2>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
lea    0xe3b(%rip),%rsi
call   1070 <fprintf@plt>
jmp    120b <expand_home+0xab>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
lea    0xe24(%rip),%rsi
call   1070 <fprintf@plt>
movb   $0x0,(%r15)
add    $0xffffffffffffffff,%r12
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1060 <strncat@plt>
mov    %r15,%rdi
mov    %r14,%rsi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1080 <strcat@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"HOME\00"
@stderr = global i32 0
@.str.1 = [29 x i8] c"Warning: Can't expand $HOME\0A\00"
@.str.2 = [36 x i8] c"Warning: Truncating in ~-expansion\0A\00"
define void @expand_home(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%10 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %10, i8** %7
%11 = load i8*, i8** %7
%12 = icmp ne i8* %11, null
br i1 %12, label %20, label %13
13: ; preds = %3
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%16 = load i8*, i8** %5
%17 = getelementptr inbounds i8, i8* %16, i64 0
store i8 0, i8* %17
%18 = load i64, i64* %6
%19 = sub i64 %18, 1
store i64 %19, i64* %9
br label %47
20: ; preds = %3
%21 = load i8*, i8** %7
%22 = call i64 @strlen(i8* %21)
store i64 %22, i64* %8
%23 = load i64, i64* %8
%24 = load i64, i64* %6
%25 = icmp uge i64 %23, %24
br i1 %25, label %26, label %36
26: ; preds = %20
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%29 = load i8*, i8** %5
%30 = getelementptr inbounds i8, i8* %29, i64 0
store i8 0, i8* %30
%31 = load i8*, i8** %5
%32 = load i8*, i8** %4
%33 = load i64, i64* %6
%34 = sub i64 %33, 1
%35 = call i32 @strncat(i8* %31, i8* %32, i64 %34)
br label %64
36: ; preds = %20
%37 = load i8*, i8** %5
%38 = load i8*, i8** %7
%39 = call i32 @strcpy(i8* %37, i8* %38)
%40 = load i8*, i8** %4
%41 = getelementptr inbounds i8, i8* %40, i32 1
store i8* %41, i8** %4
%42 = load i64, i64* %6
%43 = load i64, i64* %8
%44 = sub i64 %42, %43
%45 = sub i64 %44, 1
store i64 %45, i64* %9
br label %46
46: ; preds = %36
br label %47
47: ; preds = %46, %13
%48 = load i8*, i8** %4
%49 = call i64 @strlen(i8* %48)
store i64 %49, i64* %8
%50 = load i64, i64* %8
%51 = load i64, i64* %9
%52 = icmp ugt i64 %50, %51
br i1 %52, label %53, label %60
53: ; preds = %47
%54 = load i32, i32* @stderr
%55 = call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%56 = load i8*, i8** %5
%57 = load i8*, i8** %4
%58 = load i64, i64* %9
%59 = call i32 @strncat(i8* %56, i8* %57, i64 %58)
br label %64
60: ; preds = %47
%61 = load i8*, i8** %5
%62 = load i8*, i8** %4
%63 = call i32 @strcat(i8* %61, i8* %62)
br label %64
64: ; preds = %26, %60, %53
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"HOME\00"
@stderr = global i32 0
@.str.1 = [29 x i8] c"Warning: Can't expand $HOME\0A\00"
@.str.2 = [36 x i8] c"Warning: Truncating in ~-expansion\0A\00"
define void @expand_home(i8* %0, i8* %1, i64 %2) {
%4 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %10
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
store i8 0, i8* %1
%9 = add i64 %2, -1
br label %23
10: ; preds = %3
%11 = tail call i64 @strlen(i8* %4)
%12 = icmp ult i64 %11, %2
br i1 %12, label %18, label %13
13: ; preds = %10
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
store i8 0, i8* %1
%16 = add i64 %2, -1
%17 = tail call i32 @strncat(i8* %1, i8* %0, i64 %16)
br label %34
18: ; preds = %10
%19 = tail call i32 @strcpy(i8* %1, i8* %4)
%20 = getelementptr inbounds i8, i8* %0, i64 1
%21 = xor i64 %11, -1
%22 = add i64 %21, %2
br label %23
23: ; preds = %18, %6
%24 = phi i8* [ %20, %18 ], [ %0, %6 ]
%25 = phi i64 [ %22, %18 ], [ %9, %6 ]
%26 = tail call i64 @strlen(i8* dereferenceable(1) %24)
%27 = icmp ugt i64 %26, %25
br i1 %27, label %28, label %32
28: ; preds = %23
%29 = load i32, i32* @stderr
%30 = tail call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%31 = tail call i32 @strncat(i8* %1, i8* %24, i64 %25)
br label %34
32: ; preds = %23
%33 = tail call i32 @strcat(i8* %1, i8* %24)
br label %34
34: ; preds = %28, %32, %13
ret void
}