#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 *);
scalar_t__ malloc(int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *strstr(char *, char *);
void homeReplacer(char *cwd) {
  char *homeDir = getenv("HOME");
  int homeLength = strlen(homeDir);
  char *occurrence;
  occurrence = strstr(cwd, homeDir);
  if (occurrence != NULL) {
    char *newCwd = (char *)malloc(strlen(cwd) - homeLength + 1);
    strcpy(newCwd, "~");
    strcat(newCwd, &cwd[homeLength]);
    strcpy(cwd, newCwd);
  }
  return;
}


x86 assembly (clang, -O0):
<homeReplacer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe8d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11ef <homeReplacer+0x8f>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%edi
sub    -0x14(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe3c(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
call   1070 <strcat@plt>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <strcpy@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<homeReplacer>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
lea    0xe92(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1080 <strstr@plt>
test   %rax,%rax
je     11d3 <homeReplacer+0x73>
mov    %rbx,%rdi
call   1050 <strlen@plt>
sub    %r14d,%eax
lea    0x1(%rax),%edi
call   1060 <malloc@plt>
mov    %rax,%rbp
lea    0xe57(%rip),%rsi
mov    %rax,%rdi
call   1040 <strcpy@plt>
movslq %r14d,%rsi
add    %rbx,%rsi
mov    %rbp,%rdi
call   1070 <strcat@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <strcpy@plt>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [2 x i8] c"~\00"
define void @homeReplacer(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %2
%7 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %7, i8** %3
%8 = load i8*, i8** %3
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %4
%10 = load i8*, i8** %2
%11 = load i8*, i8** %3
%12 = call i8* @strstr(i8* %10, i8* %11)
store i8* %12, i8** %5
%13 = load i8*, i8** %5
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %34
15: ; preds = %1
%16 = load i8*, i8** %2
%17 = call i32 @strlen(i8* %16)
%18 = load i32, i32* %4
%19 = sub nsw i32 %17, %18
%20 = add nsw i32 %19, 1
%21 = call i64 @malloc(i32 %20)
%22 = inttoptr i64 %21 to i8*
store i8* %22, i8** %6
%23 = load i8*, i8** %6
%24 = call i32 @strcpy(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%25 = load i8*, i8** %6
%26 = load i8*, i8** %2
%27 = load i32, i32* %4
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
%30 = call i32 @strcat(i8* %25, i8* %29)
%31 = load i8*, i8** %2
%32 = load i8*, i8** %6
%33 = call i32 @strcpy(i8* %31, i8* %32)
br label %34
34: ; preds = %15, %1
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [2 x i8] c"~\00"
define void @homeReplacer(i8* %0) {
%2 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%3 = tail call i32 @strlen(i8* %2)
%4 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %2)
%5 = icmp eq i8* %4, null
br i1 %5, label %17, label %6
6: ; preds = %1
%7 = tail call i32 @strlen(i8* %0)
%8 = sub i32 1, %3
%9 = add i32 %8, %7
%10 = tail call i64 @malloc(i32 %9)
%11 = inttoptr i64 %10 to i8*
%12 = tail call i32 @strcpy(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = sext i32 %3 to i64
%14 = getelementptr inbounds i8, i8* %0, i64 %13
%15 = tail call i32 @strcat(i8* %11, i8* %14)
%16 = tail call i32 @strcpy(i8* %0, i8* %11)
br label %17
17: ; preds = %6, %1
ret void
}