#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*/ execlogp(char *, int /*<<< orphan*/ *, char **);
int /*<<< orphan*/ fileno(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, ...);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
int /*<<< orphan*/ isatty(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
scalar_t__ strlen(char const *);
scalar_t__ strncmp(char *, char *, int);
int /*<<< orphan*/ strreplace(char **, char *, char *, int);
void xterm_reset_title(const char *default_xterm_title) {
  char *prompt_command;
  char *title;
  if (getenv("TERM") == NULL || strncmp(getenv("TERM"), "xterm", 5) != 0 ||
      !isatty(fileno(stdout)))
    return;
  title = NULL;
  if ((prompt_command = getenv("PROMPT_COMMAND")) != NULL) {
    execlogp(prompt_command, NULL, &title);
    if (strlen(title) == 0) {
      free(title);
      title = NULL;
    }
  }
  if (title != NULL)
    fprintf(stderr, "\x1b]0;%s\x07", title);
  else if (strlen(default_xterm_title) == 0) {
    char *pwd = getenv("PWD");
    strreplace(&pwd, getenv("HOME"), "~", 1);
    fprintf(stderr, "\x1b]0;%s@%s:%s\x07", getenv("LOGNAME"),
            getenv("HOSTNAME"), pwd);
  } else
    fprintf(stderr, "\x1b]0;%s\x07", default_xterm_title);
  free(title);
}


x86 assembly (clang, -O0):
<xterm_reset_title>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe5d(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     11fa <xterm_reset_title+0x6a>
lea    0xe47(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
lea    0xe3d(%rip),%rsi
mov    $0x5,%edx
call   1050 <strncmp@plt>
cmp    $0x0,%rax
jne    11fa <xterm_reset_title+0x6a>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   10b0 <fileno@plt>
mov    %eax,%edi
call   1060 <isatty@plt>
cmp    $0x0,%eax
jne    11ff <xterm_reset_title+0x6f>
jmp    133f <xterm_reset_title+0x1af>
movq   $0x0,-0x18(%rbp)
lea    0xdfd(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     125b <xterm_reset_title+0xcb>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x18(%rbp),%rdx
call   1070 <execlogp@plt>
mov    -0x18(%rbp),%rdi
call   1080 <strlen@plt>
cmp    $0x0,%rax
jne    1256 <xterm_reset_title+0xc6>
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
movq   $0x0,-0x18(%rbp)
jmp    125b <xterm_reset_title+0xcb>
cmpq   $0x0,-0x18(%rbp)
je     1286 <xterm_reset_title+0xf6>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xda0(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
jmp    1336 <xterm_reset_title+0x1a6>
mov    -0x8(%rbp),%rdi
call   1080 <strlen@plt>
cmp    $0x0,%rax
jne    1316 <xterm_reset_title+0x186>
lea    0xd82(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
lea    0xd76(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rsi
lea    -0x20(%rbp),%rdi
lea    0xd68(%rip),%rdx
mov    $0x1,%ecx
call   1090 <strreplace@plt>
mov    0x2d0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
lea    0xd5b(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
lea    0xd53(%rip),%rdi
call   1030 <getenv@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %rax,%rcx
mov    -0x20(%rbp),%r8
lea    0xd23(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
jmp    1331 <xterm_reset_title+0x1a1>
mov    0x2cc3(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xcf0(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
jmp    1336 <xterm_reset_title+0x1a6>
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xterm_reset_title>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
lea    0xe70(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     12ce <xterm_reset_title+0x14e>
lea    0xe60(%rip),%rsi
mov    %rax,%rdi
mov    $0x5,%edx
call   1050 <strncmp@plt>
test   %rax,%rax
jne    12ce <xterm_reset_title+0x14e>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
call   10a0 <fileno@plt>
mov    %eax,%edi
call   1060 <isatty@plt>
test   %eax,%eax
je     12ce <xterm_reset_title+0x14e>
movq   $0x0,0x8(%rsp)
lea    0xe23(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     122e <xterm_reset_title+0xae>
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
xor    %esi,%esi
call   1070 <execlogp@plt>
mov    0x8(%rsp),%rdx
cmpb   $0x0,(%rdx)
je     121d <xterm_reset_title+0x9d>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
lea    0xdff(%rip),%rsi
jmp    1246 <xterm_reset_title+0xc6>
mov    %rdx,%rdi
call   1040 <free@plt>
movq   $0x0,0x8(%rsp)
cmpb   $0x0,(%rbx)
je     124f <xterm_reset_title+0xcf>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
lea    0xdd7(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    12c4 <xterm_reset_title+0x144>
lea    0xdcc(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,0x10(%rsp)
lea    0xdbf(%rip),%rdi
call   1030 <getenv@plt>
lea    0xdb8(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    %rax,%rsi
mov    $0x1,%ecx
call   1080 <strreplace@plt>
mov    0x2d54(%rip),%rax
mov    (%rax),%ebp
lea    0xda6(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
lea    0xd9f(%rip),%rdi
call   1030 <getenv@plt>
mov    0x10(%rsp),%r8
lea    0xd78(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    0x8(%rsp),%rdi
call   1040 <free@plt>
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"TERM\00"
@.str.1 = [6 x i8] c"xterm\00"
@stdout = global i32 0
@.str.2 = [15 x i8] c"PROMPT_COMMAND\00"
@stderr = global i32 0
@.str.3 = [8 x i8] c"\1B]0;%s\07\00"
@.str.4 = [4 x i8] c"PWD\00"
@.str.5 = [5 x i8] c"HOME\00"
@.str.6 = [2 x i8] c"~\00"
@.str.7 = [14 x i8] c"\1B]0;%s@%s:%s\07\00"
@.str.8 = [8 x i8] c"LOGNAME\00"
@.str.9 = [9 x i8] c"HOSTNAME\00"
define void @xterm_reset_title(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %2
%6 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i8* %6, null
br i1 %7, label %17, label %8
8: ; preds = %1
%9 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%10 = call i64 @strncmp(i8* %9, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 5)
%11 = icmp ne i64 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %8
%13 = load i32, i32* @stdout
%14 = call i32 @fileno(i32 %13)
%15 = call i32 @isatty(i32 %14)
%16 = icmp ne i32 %15, 0
br i1 %16, label %18, label %17
17: ; preds = %12, %8, %1
br label %59
18: ; preds = %12
store i8* null, i8** %4
%19 = call i8* @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
store i8* %19, i8** %3
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %31
21: ; preds = %18
%22 = load i8*, i8** %3
%23 = call i32 @execlogp(i8* %22, i32* null, i8** %4)
%24 = load i8*, i8** %4
%25 = call i64 @strlen(i8* %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = load i8*, i8** %4
%29 = call i32 @free(i8* %28)
store i8* null, i8** %4
br label %30
30: ; preds = %27, %21
br label %31
31: ; preds = %30, %18
%32 = load i8*, i8** %4
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %38
34: ; preds = %31
%35 = load i32, i32* @stderr
%36 = load i8*, i8** %4
%37 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %36)
br label %56
38: ; preds = %31
%39 = load i8*, i8** %2
%40 = call i64 @strlen(i8* %39)
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %51
42: ; preds = %38
%43 = call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
store i8* %43, i8** %5
%44 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%45 = call i32 @strreplace(i8** %5, i8* %44, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32 1)
%46 = load i32, i32* @stderr
%47 = call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0))
%48 = call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0))
%49 = load i8*, i8** %5
%50 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0), i8* %47, i8* %48, i8* %49)
br label %55
51: ; preds = %38
%52 = load i32, i32* @stderr
%53 = load i8*, i8** %2
%54 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %53)
br label %55
55: ; preds = %51, %42
br label %56
56: ; preds = %55, %34
%57 = load i8*, i8** %4
%58 = call i32 @free(i8* %57)
br label %59
59: ; preds = %56, %17
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"TERM\00"
@.str.1 = [6 x i8] c"xterm\00"
@stdout = global i32 0
@.str.2 = [15 x i8] c"PROMPT_COMMAND\00"
@stderr = global i32 0
@.str.3 = [8 x i8] c"\1B]0;%s\07\00"
@.str.4 = [4 x i8] c"PWD\00"
@.str.5 = [5 x i8] c"HOME\00"
@.str.6 = [2 x i8] c"~\00"
@.str.7 = [14 x i8] c"\1B]0;%s@%s:%s\07\00"
@.str.8 = [8 x i8] c"LOGNAME\00"
@.str.9 = [9 x i8] c"HOSTNAME\00"
define void @xterm_reset_title(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = bitcast i8** %2 to i8*
%5 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i8* %5, null
br i1 %6, label %47, label %7
7: ; preds = %1
%8 = tail call i64 @strncmp(i8* %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 5)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %47
10: ; preds = %7
%11 = load i32, i32* @stdout
%12 = tail call i32 @fileno(i32 %11)
%13 = tail call i32 @isatty(i32 %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %47, label %15
15: ; preds = %10
store i8* null, i8** %2
%16 = tail call i8* @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i8* %16, null
br i1 %17, label %28, label %18
18: ; preds = %15
%19 = call i32 @execlogp(i8* %16, i32* null, i8** %2)
%20 = load i8*, i8** %2
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = call i32 @free(i8* %20)
store i8* null, i8** %2
br label %28
25: ; preds = %18
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %20)
br label %44
28: ; preds = %23, %15
%29 = load i8, i8* %0
%30 = icmp eq i8 %29, 0
br i1 %30, label %31, label %41
31: ; preds = %28
%32 = bitcast i8** %3 to i8*
%33 = call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
store i8* %33, i8** %3
%34 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%35 = call i32 @strreplace(i8** %3, i8* %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32 1)
%36 = load i32, i32* @stderr
%37 = call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0))
%38 = call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0))
%39 = load i8*, i8** %3
%40 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0), i8* %37, i8* %38, i8* %39)
br label %44
41: ; preds = %28
%42 = load i32, i32* @stderr
%43 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %44
44: ; preds = %31, %41, %25
%45 = load i8*, i8** %2
%46 = call i32 @free(i8* %45)
br label %47
47: ; preds = %1, %7, %10, %44
ret void
}