#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 *calloc(int, int);
int dsh_prompt_add_element(char **, char *, int);
int /*<<< orphan*/ free(char *);
char *getcwd(int /*<<< orphan*/ *, int /*<<< orphan*/);
char *getlogin();
int /*<<< orphan*/ sprintf(char *, char *, int);
char *dsh_prompt_generate(char *config_char, int status) {
  int buffsize = 2;
  char *prompt = calloc(buffsize, sizeof(char)), *cwd = getcwd(NULL, 0),
       rstat[20] = {'\0'};
  if (!config_char) {
    buffsize = dsh_prompt_add_element(&prompt, "Hello ", buffsize);
    buffsize = dsh_prompt_add_element(&prompt, "\033[32m", buffsize);
    buffsize = dsh_prompt_add_element(&prompt, getlogin(), buffsize);
    buffsize = dsh_prompt_add_element(&prompt, "\033[0m", buffsize);
    buffsize = dsh_prompt_add_element(&prompt, " at ", buffsize);
    buffsize = dsh_prompt_add_element(&prompt, "\033[36m", buffsize);
    buffsize = dsh_prompt_add_element(&prompt, cwd, buffsize);
    buffsize = dsh_prompt_add_element(&prompt, "\033[0m", buffsize);
    if (status) {
      sprintf(rstat, "%d", status);
      buffsize = dsh_prompt_add_element(&prompt, "\n", buffsize);
      buffsize = dsh_prompt_add_element(&prompt, "\033[31m", buffsize);
      buffsize = dsh_prompt_add_element(&prompt, rstat, buffsize);
      buffsize = dsh_prompt_add_element(&prompt, "\033[0m", buffsize);
      buffsize = dsh_prompt_add_element(&prompt, " > ", buffsize);
    } else
      buffsize = dsh_prompt_add_element(&prompt, "\n> ", buffsize);
  }
  free(cwd);
  return prompt;
}


x86 assembly (clang, -O0):
<dsh_prompt_generate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x2,-0x10(%rbp)
mov    -0x10(%rbp),%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   1050 <getcwd@plt>
mov    %rax,-0x20(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1040 <memset@plt>
cmpq   $0x0,-0x8(%rbp)
jne    1319 <dsh_prompt_generate+0x1a9>
mov    -0x10(%rbp),%edx
lea    -0x18(%rbp),%rdi
lea    0xe31(%rip),%rsi
call   1070 <dsh_prompt_add_element@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edx
lea    -0x18(%rbp),%rdi
lea    0xe22(%rip),%rsi
call   1070 <dsh_prompt_add_element@plt>
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1080 <getlogin@plt>
mov    %rax,%rsi
mov    -0x10(%rbp),%edx
lea    -0x18(%rbp),%rdi
call   1070 <dsh_prompt_add_element@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edx
lea    -0x18(%rbp),%rdi
lea    0xdf9(%rip),%rsi
call   1070 <dsh_prompt_add_element@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edx
lea    -0x18(%rbp),%rdi
lea    0xde8(%rip),%rsi
call   1070 <dsh_prompt_add_element@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edx
lea    -0x18(%rbp),%rdi
lea    0xdd7(%rip),%rsi
call   1070 <dsh_prompt_add_element@plt>
mov    %eax,-0x10(%rbp)
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%edx
lea    -0x18(%rbp),%rdi
call   1070 <dsh_prompt_add_element@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edx
lea    -0x18(%rbp),%rdi
lea    0xda4(%rip),%rsi
call   1070 <dsh_prompt_add_element@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     12fe <dsh_prompt_generate+0x18e>
lea    -0x40(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xd94(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x10(%rbp),%edx
lea    -0x18(%rbp),%rdi
lea    0xd84(%rip),%rsi
call   1070 <dsh_prompt_add_element@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edx
lea    -0x18(%rbp),%rdi
lea    0xd70(%rip),%rsi
call   1070 <dsh_prompt_add_element@plt>
mov    %eax,-0x10(%rbp)
lea    -0x40(%rbp),%rsi
mov    -0x10(%rbp),%edx
lea    -0x18(%rbp),%rdi
call   1070 <dsh_prompt_add_element@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edx
lea    -0x18(%rbp),%rdi
lea    0xd32(%rip),%rsi
call   1070 <dsh_prompt_add_element@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edx
lea    -0x18(%rbp),%rdi
lea    0xd37(%rip),%rsi
call   1070 <dsh_prompt_add_element@plt>
mov    %eax,-0x10(%rbp)
jmp    1314 <dsh_prompt_generate+0x1a4>
mov    -0x10(%rbp),%edx
lea    -0x18(%rbp),%rdi
lea    0xd20(%rip),%rsi
call   1070 <dsh_prompt_add_element@plt>
mov    %eax,-0x10(%rbp)
jmp    1319 <dsh_prompt_generate+0x1a9>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dsh_prompt_generate>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
mov    $0x2,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,0x8(%rsp)
xor    %edi,%edi
xor    %esi,%esi
call   1040 <getcwd@plt>
mov    %rax,%r12
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x0,0x20(%rsp)
test   %rbp,%rbp
jne    12bf <dsh_prompt_generate+0x15f>
lea    0xe4e(%rip),%rsi
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x2,%edx
call   1060 <dsh_prompt_add_element@plt>
lea    0xe3c(%rip),%rsi
mov    %rbp,%rdi
mov    %eax,%edx
call   1060 <dsh_prompt_add_element@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1070 <getlogin@plt>
mov    %rbp,%rdi
mov    %rax,%rsi
mov    %ebx,%edx
call   1060 <dsh_prompt_add_element@plt>
lea    0xe1b(%rip),%r15
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1060 <dsh_prompt_add_element@plt>
lea    0xe0c(%rip),%rsi
mov    %rbp,%rdi
mov    %eax,%edx
call   1060 <dsh_prompt_add_element@plt>
lea    0xe00(%rip),%rsi
mov    %rbp,%rdi
mov    %eax,%edx
call   1060 <dsh_prompt_add_element@plt>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1060 <dsh_prompt_add_element@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1060 <dsh_prompt_add_element@plt>
mov    %eax,%ebp
test   %r14d,%r14d
je     12ac <dsh_prompt_generate+0x14c>
lea    0xdd4(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %r14d,%edx
call   1080 <sprintf@plt>
lea    0xdc0(%rip),%rsi
lea    0x8(%rsp),%r14
mov    %r14,%rdi
mov    %ebp,%edx
call   1060 <dsh_prompt_add_element@plt>
lea    0xdac(%rip),%rsi
mov    %r14,%rdi
mov    %eax,%edx
call   1060 <dsh_prompt_add_element@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1060 <dsh_prompt_add_element@plt>
lea    0xd79(%rip),%rsi
mov    %r14,%rdi
mov    %eax,%edx
call   1060 <dsh_prompt_add_element@plt>
lea    0xd83(%rip),%rsi
mov    %r14,%rdi
mov    %eax,%edx
jmp    12ba <dsh_prompt_generate+0x15a>
lea    0xd79(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    %ebp,%edx
call   1060 <dsh_prompt_add_element@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    0x8(%rsp),%rax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"Hello \00"
@.str.1 = [6 x i8] c"\1B[32m\00"
@.str.2 = [5 x i8] c"\1B[0m\00"
@.str.3 = [5 x i8] c" at \00"
@.str.4 = [6 x i8] c"\1B[36m\00"
@.str.5 = [3 x i8] c"%d\00"
@.str.6 = [2 x i8] c"\0A\00"
@.str.7 = [6 x i8] c"\1B[31m\00"
@.str.8 = [4 x i8] c" > \00"
@.str.9 = [4 x i8] c"\0A> \00"
define i8* @dsh_prompt_generate(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca [20 x i8]
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 2, i32* %5
%9 = load i32, i32* %5
%10 = call i8* @calloc(i32 %9, i32 1)
store i8* %10, i8** %6
%11 = call i8* @getcwd(i32* null, i32 0)
store i8* %11, i8** %7
%12 = bitcast [20 x i8]* %8 to i8*
%13 = load i8*, i8** %3
%14 = icmp ne i8* %13, null
br i1 %14, label %55, label %15
15: ; preds = %2
%16 = load i32, i32* %5
%17 = call i32 @dsh_prompt_add_element(i8** %6, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %16)
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = call i32 @dsh_prompt_add_element(i8** %6, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %18)
store i32 %19, i32* %5
%20 = call i8* (...) @getlogin()
%21 = load i32, i32* %5
%22 = call i32 @dsh_prompt_add_element(i8** %6, i8* %20, i32 %21)
store i32 %22, i32* %5
%23 = load i32, i32* %5
%24 = call i32 @dsh_prompt_add_element(i8** %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %23)
store i32 %24, i32* %5
%25 = load i32, i32* %5
%26 = call i32 @dsh_prompt_add_element(i8** %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %25)
store i32 %26, i32* %5
%27 = load i32, i32* %5
%28 = call i32 @dsh_prompt_add_element(i8** %6, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %27)
store i32 %28, i32* %5
%29 = load i8*, i8** %7
%30 = load i32, i32* %5
%31 = call i32 @dsh_prompt_add_element(i8** %6, i8* %29, i32 %30)
store i32 %31, i32* %5
%32 = load i32, i32* %5
%33 = call i32 @dsh_prompt_add_element(i8** %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %32)
store i32 %33, i32* %5
%34 = load i32, i32* %4
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %51
36: ; preds = %15
%37 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%38 = load i32, i32* %4
%39 = call i32 @sprintf(i8* %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %38)
%40 = load i32, i32* %5
%41 = call i32 @dsh_prompt_add_element(i8** %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32 %40)
store i32 %41, i32* %5
%42 = load i32, i32* %5
%43 = call i32 @dsh_prompt_add_element(i8** %6, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i32 %42)
store i32 %43, i32* %5
%44 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%45 = load i32, i32* %5
%46 = call i32 @dsh_prompt_add_element(i8** %6, i8* %44, i32 %45)
store i32 %46, i32* %5
%47 = load i32, i32* %5
%48 = call i32 @dsh_prompt_add_element(i8** %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %47)
store i32 %48, i32* %5
%49 = load i32, i32* %5
%50 = call i32 @dsh_prompt_add_element(i8** %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0), i32 %49)
store i32 %50, i32* %5
br label %54
51: ; preds = %15
%52 = load i32, i32* %5
%53 = call i32 @dsh_prompt_add_element(i8** %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.9, i64 0, i64 0), i32 %52)
store i32 %53, i32* %5
br label %54
54: ; preds = %51, %36
br label %55
55: ; preds = %54, %2
%56 = load i8*, i8** %7
%57 = call i32 @free(i8* %56)
%58 = load i8*, i8** %6
ret i8* %58
}

LLVM IR (-O2):
@.str = [7 x i8] c"Hello \00"
@.str.1 = [6 x i8] c"\1B[32m\00"
@.str.2 = [5 x i8] c"\1B[0m\00"
@.str.3 = [5 x i8] c" at \00"
@.str.4 = [6 x i8] c"\1B[36m\00"
@.str.5 = [3 x i8] c"%d\00"
@.str.6 = [2 x i8] c"\0A\00"
@.str.7 = [6 x i8] c"\1B[31m\00"
@.str.8 = [4 x i8] c" > \00"
@.str.9 = [4 x i8] c"\0A> \00"
define i8* @dsh_prompt_generate(i8* readnone %0, i32 %1) {
%3 = alloca i8*
%4 = alloca [20 x i8]
%5 = bitcast i8** %3 to i8*
%6 = tail call dereferenceable_or_null(2) i8* @calloc(i32 2, i32 1)
store i8* %6, i8** %3
%7 = tail call i8* @getcwd(i32* null, i32 0)
%8 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%9 = icmp eq i8* %0, null
br i1 %9, label %10, label %30
10: ; preds = %2
%11 = call i32 @dsh_prompt_add_element(i8** %3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 2)
%12 = call i32 @dsh_prompt_add_element(i8** %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = call i8* (...) @getlogin()
%14 = call i32 @dsh_prompt_add_element(i8** %3, i8* %13, i32 %12)
%15 = call i32 @dsh_prompt_add_element(i8** %3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%16 = call i32 @dsh_prompt_add_element(i8** %3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %15)
%17 = call i32 @dsh_prompt_add_element(i8** %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %16)
%18 = call i32 @dsh_prompt_add_element(i8** %3, i8* %7, i32 %17)
%19 = call i32 @dsh_prompt_add_element(i8** %3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%20 = icmp eq i32 %1, 0
br i1 %20, label %28, label %21
21: ; preds = %10
%22 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %1)
%23 = call i32 @dsh_prompt_add_element(i8** %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32 %19)
%24 = call i32 @dsh_prompt_add_element(i8** %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i32 %23)
%25 = call i32 @dsh_prompt_add_element(i8** %3, i8* %8, i32 %24)
%26 = call i32 @dsh_prompt_add_element(i8** %3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %25)
%27 = call i32 @dsh_prompt_add_element(i8** %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0), i32 %26)
br label %30
28: ; preds = %10
%29 = call i32 @dsh_prompt_add_element(i8** %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.9, i64 0, i64 0), i32 %19)
br label %30
30: ; preds = %21, %28, %2
%31 = call i32 @free(i8* %7)
%32 = load i8*, i8** %3
ret i8* %32
}