#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*/ getcwd(char *, int);
char *getenv(char *);
int /*<<< orphan*/ gethostname(char *, int);
int /*<<< orphan*/ printf(char *, char *, char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
void print_prompt() {
  char *home_dir = getenv("HOME");
  char current_dir[500];
  getcwd(current_dir, 500);
  if (strncmp(home_dir, current_dir, strlen(home_dir)) == 0) {
    strcpy(current_dir, current_dir + strlen(home_dir) - sizeof(char));
    current_dir[0] = '~';
  }
  char hostname[500];
  gethostname(hostname, 500);
  printf("\x1B[32m"
         "[MySh] %s@%s:%s$ "
         "\x1B[0m",
         getenv("LOGNAME"), hostname, current_dir);
}


x86 assembly (clang, -O0):
<print_prompt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
lea    0xe7e(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x8(%rbp)
lea    -0x200(%rbp),%rdi
mov    $0x1f4,%esi
call   1080 <getcwd@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x410(%rbp)
lea    -0x200(%rbp),%rax
mov    %rax,-0x408(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x410(%rbp),%rdi
mov    -0x408(%rbp),%rsi
mov    %eax,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1225 <print_prompt+0xb5>
lea    -0x200(%rbp),%rax
mov    %rax,-0x418(%rbp)
lea    -0x200(%rbp),%rax
mov    %rax,-0x420(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x420(%rbp),%rsi
mov    -0x418(%rbp),%rdi
cltq
add    %rax,%rsi
add    $0xffffffffffffffff,%rsi
call   1050 <strcpy@plt>
movb   $0x7e,-0x200(%rbp)
lea    -0x400(%rbp),%rdi
mov    $0x1f4,%esi
call   1090 <gethostname@plt>
lea    0xde3(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rsi
lea    -0x400(%rbp),%rdx
lea    -0x200(%rbp),%rcx
lea    0xdab(%rip),%rdi
call   1070 <printf@plt>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_prompt>:
push   %r14
push   %rbx
sub    $0x3f8,%rsp
lea    0xe7f(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x1f4,%esi
call   1080 <getcwd@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
jne    11d1 <print_prompt+0x61>
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
lea    (%rsp,%rax,1),%rsi
add    $0xffffffffffffffff,%rsi
mov    %rsp,%rdi
call   1050 <strcpy@plt>
movb   $0x7e,(%rsp)
lea    0x200(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x1f4,%esi
call   1090 <gethostname@plt>
lea    0xe33(%rip),%rdi
call   1030 <getenv@plt>
lea    0xe0c(%rip),%rdi
mov    %rsp,%rcx
mov    %rax,%rsi
mov    %rbx,%rdx
call   1070 <printf@plt>
add    $0x3f8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [27 x i8] c"\1B[32m[MySh] %s@%s:%s$ \1B[0m\00"
@.str.2 = [8 x i8] c"LOGNAME\00"
define void @print_prompt() {
%1 = alloca i8*
%2 = alloca [500 x i8]
%3 = alloca [500 x i8]
%4 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %4, i8** %1
%5 = getelementptr inbounds [500 x i8], [500 x i8]* %2, i64 0, i64 0
%6 = call i32 @getcwd(i8* %5, i32 500)
%7 = load i8*, i8** %1
%8 = getelementptr inbounds [500 x i8], [500 x i8]* %2, i64 0, i64 0
%9 = load i8*, i8** %1
%10 = call i32 @strlen(i8* %9)
%11 = call i64 @strncmp(i8* %7, i8* %8, i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %23
13: ; preds = %0
%14 = getelementptr inbounds [500 x i8], [500 x i8]* %2, i64 0, i64 0
%15 = getelementptr inbounds [500 x i8], [500 x i8]* %2, i64 0, i64 0
%16 = load i8*, i8** %1
%17 = call i32 @strlen(i8* %16)
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %15, i64 %18
%20 = getelementptr inbounds i8, i8* %19, i64 -1
%21 = call i32 @strcpy(i8* %14, i8* %20)
%22 = getelementptr inbounds [500 x i8], [500 x i8]* %2, i64 0, i64 0
store i8 126, i8* %22
br label %23
23: ; preds = %13, %0
%24 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 0
%25 = call i32 @gethostname(i8* %24, i32 500)
%26 = call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%27 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 0
%28 = getelementptr inbounds [500 x i8], [500 x i8]* %2, i64 0, i64 0
%29 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %26, i8* %27, i8* %28)
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [27 x i8] c"\1B[32m[MySh] %s@%s:%s$ \1B[0m\00"
@.str.2 = [8 x i8] c"LOGNAME\00"
define void @print_prompt() {
%1 = alloca [500 x i8]
%2 = alloca [500 x i8]
%3 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%4 = getelementptr inbounds [500 x i8], [500 x i8]* %1, i64 0, i64 0
%5 = call i32 @getcwd(i8* %4, i32 500)
%6 = call i32 @strlen(i8* %3)
%7 = call i64 @strncmp(i8* %3, i8* %4, i32 %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %15
9: ; preds = %0
%10 = call i32 @strlen(i8* %3)
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds [500 x i8], [500 x i8]* %1, i64 0, i64 %11
%13 = getelementptr inbounds i8, i8* %12, i64 -1
%14 = call i32 @strcpy(i8* %4, i8* %13)
store i8 126, i8* %4
br label %15
15: ; preds = %9, %0
%16 = getelementptr inbounds [500 x i8], [500 x i8]* %2, i64 0, i64 0
%17 = call i32 @gethostname(i8* %16, i32 500)
%18 = call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %18, i8* %16, i8* %4)
ret void
}