#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 */
struct passwd {
  char *pw_name;
};

/* Variables and functions */
scalar_t__ HOST_NAME_MAX;
char *basename(char *);
int /*<<< orphan*/ free(char *);
char *getcwd(char *, size_t);
scalar_t__ gethostname(char *, scalar_t__);
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ getuid();
char *malloc(scalar_t__);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
void psh_print_prompt(void) {
  struct passwd *pw;
  char *host;
  char *dir = NULL;
  char *base;
  size_t size = 0;
  pw = getpwuid(getuid());
  if (!pw) {
    printf("%s", "psh> ");
    return;
  }
  printf("%s@", pw->pw_name);
  host = malloc(HOST_NAME_MAX + 1);
  if (gethostname(host, HOST_NAME_MAX + 1)) {
    perror("psh");
    printf("%s", "psh> ");
    return;
  }
  printf("%s ", host);
  free(host);
  dir = getcwd(dir, size);
  if (!dir) {
    perror("psh");
    printf("%s", "psh> ");
    return;
  }
  base = basename(dir);
  printf("%s $ ", base);
  free(dir);
}


x86 assembly (clang, -O0):
<psh_print_prompt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    $0x0,%al
call   1050 <getuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11dd <psh_print_prompt+0x4d>
lea    0xe34(%rip),%rdi
lea    0xe30(%rip),%rsi
call   1060 <printf@plt>
jmp    12cb <psh_print_prompt+0x13b>
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
lea    0xe1e(%rip),%rdi
call   1060 <printf@plt>
mov    0x2df1(%rip),%rax
mov    (%rax),%rdi
add    $0x1,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2dd6(%rip),%rax
mov    (%rax),%rsi
add    $0x1,%rsi
call   10b0 <gethostname@plt>
cmp    $0x0,%rax
je     124c <psh_print_prompt+0xbc>
lea    0xdde(%rip),%rdi
call   10a0 <perror@plt>
lea    0xdc5(%rip),%rdi
lea    0xdc1(%rip),%rsi
call   1060 <printf@plt>
jmp    12cb <psh_print_prompt+0x13b>
mov    -0x10(%rbp),%rsi
lea    0xdba(%rip),%rdi
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1070 <getcwd@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    12a5 <psh_print_prompt+0x115>
lea    0xd85(%rip),%rdi
call   10a0 <perror@plt>
lea    0xd6c(%rip),%rdi
lea    0xd68(%rip),%rsi
call   1060 <printf@plt>
jmp    12cb <psh_print_prompt+0x13b>
mov    -0x18(%rbp),%rdi
call   1080 <basename@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xd58(%rip),%rdi
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<psh_print_prompt>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1050 <getuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
test   %rax,%rax
je     123d <psh_print_prompt+0xad>
mov    (%rax),%rsi
lea    0xe54(%rip),%rdi
call   1060 <printf@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%rbx
add    $0x1,%rbx
mov    %rbx,%rdi
call   1090 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   10b0 <gethostname@plt>
test   %rax,%rax
jne    1231 <psh_print_prompt+0xa1>
lea    0xe27(%rip),%rdi
mov    %r14,%rsi
call   1060 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %edi,%edi
xor    %esi,%esi
call   1070 <getcwd@plt>
test   %rax,%rax
je     1231 <psh_print_prompt+0xa1>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <basename@plt>
lea    0xdfb(%rip),%rdi
mov    %rax,%rsi
call   1060 <printf@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <free@plt>
lea    0xdd5(%rip),%rdi
call   10a0 <perror@plt>
lea    0xdbc(%rip),%rdi
lea    0xdb8(%rip),%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <printf@plt>

LLVM IR (-O0):
%struct.passwd = type { i8* }
@.str = [3 x i8] c"%s\00"
@.str.1 = [6 x i8] c"psh> \00"
@.str.2 = [4 x i8] c"%s@\00"
@HOST_NAME_MAX = global i64 0
@.str.3 = [4 x i8] c"psh\00"
@.str.4 = [4 x i8] c"%s \00"
@.str.5 = [6 x i8] c"%s $ \00"
define void @psh_print_prompt() {
%1 = alloca %struct.passwd*
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
store i8* null, i8** %3
store i64 0, i64* %5
%6 = call i32 (...) @getuid()
%7 = call %struct.passwd* @getpwuid(i32 %6)
store %struct.passwd* %7, %struct.passwd** %1
%8 = load %struct.passwd*, %struct.passwd** %1
%9 = icmp ne %struct.passwd* %8, null
br i1 %9, label %12, label %10
10: ; preds = %0
%11 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %48
12: ; preds = %0
%13 = load %struct.passwd*, %struct.passwd** %1
%14 = getelementptr inbounds %struct.passwd, %struct.passwd* %13, i32 0, i32 0
%15 = load i8*, i8** %14
%16 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %15)
%17 = load i64, i64* @HOST_NAME_MAX
%18 = add nsw i64 %17, 1
%19 = call i8* @malloc(i64 %18)
store i8* %19, i8** %2
%20 = load i8*, i8** %2
%21 = load i64, i64* @HOST_NAME_MAX
%22 = add nsw i64 %21, 1
%23 = call i64 @gethostname(i8* %20, i64 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %12
%26 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%27 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %48
28: ; preds = %12
%29 = load i8*, i8** %2
%30 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %29)
%31 = load i8*, i8** %2
%32 = call i32 @free(i8* %31)
%33 = load i8*, i8** %3
%34 = load i64, i64* %5
%35 = call i8* @getcwd(i8* %33, i64 %34)
store i8* %35, i8** %3
%36 = load i8*, i8** %3
%37 = icmp ne i8* %36, null
br i1 %37, label %41, label %38
38: ; preds = %28
%39 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%40 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %48
41: ; preds = %28
%42 = load i8*, i8** %3
%43 = call i8* @basename(i8* %42)
store i8* %43, i8** %4
%44 = load i8*, i8** %4
%45 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* %44)
%46 = load i8*, i8** %3
%47 = call i32 @free(i8* %46)
br label %48
48: ; preds = %41, %38, %25, %10
ret void
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@.str = [3 x i8] c"%s\00"
@.str.1 = [6 x i8] c"psh> \00"
@.str.2 = [4 x i8] c"%s@\00"
@HOST_NAME_MAX = global i64 0
@.str.3 = [4 x i8] c"psh\00"
@.str.4 = [4 x i8] c"%s \00"
@.str.5 = [6 x i8] c"%s $ \00"
define void @psh_print_prompt() {
%1 = tail call i32 (...) @getuid()
%2 = tail call %struct.passwd* @getpwuid(i32 %1)
%3 = icmp eq %struct.passwd* %2, null
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %30
6: ; preds = %0
%7 = getelementptr inbounds %struct.passwd, %struct.passwd* %2, i64 0, i32 0
%8 = load i8*, i8** %7
%9 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %8)
%10 = load i64, i64* @HOST_NAME_MAX
%11 = add nsw i64 %10, 1
%12 = tail call i8* @malloc(i64 %11)
%13 = tail call i64 @gethostname(i8* %12, i64 %11)
%14 = icmp eq i64 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %6
%16 = tail call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%17 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %30
18: ; preds = %6
%19 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %12)
%20 = tail call i32 @free(i8* %12)
%21 = tail call i8* @getcwd(i8* null, i64 0)
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = tail call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%25 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %30
26: ; preds = %18
%27 = tail call i8* @basename(i8* %21)
%28 = tail call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* %27)
%29 = tail call i32 @free(i8* %21)
br label %30
30: ; preds = %26, %23, %15, %4
ret void
}