#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 PATH_MAX;
char *calloc(scalar_t__, int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ getcwd(char *, int);
int /*<<< orphan*/ strcat(char *, char *);
char *strdup(char *);
scalar_t__ strlen(char *);
char *strrchr(char *, char);
char *sh6_path_to_custom_programs(char *pgr_name) {
  char cwd[PATH_MAX + 1];
  char *dir_to_exec;
  char *last_slash;
  char *complete_path;
  getcwd(cwd, PATH_MAX + 1);
  dir_to_exec = strdup(pgr_name);
  last_slash = strrchr(dir_to_exec, '/');
  *last_slash = '\0';
  if ((complete_path =
           calloc(strlen(cwd) + strlen(dir_to_exec) + 6, sizeof(char))) == NULL)
    return NULL;
  complete_path[0] = '\0';
  strcat(complete_path, cwd);
  strcat(complete_path, "/");
  strcat(complete_path, dir_to_exec);
  strcat(complete_path, "/cmd");
  free(dir_to_exec);
  return complete_path;
}


x86 assembly (clang, -O0):
<sh6_path_to_custom_programs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x50(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
add    $0x1,%esi
call   1060 <getcwd@plt>
mov    -0x10(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    -0x50(%rbp),%rdi
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
call   1040 <strlen@plt>
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x48(%rbp),%rdi
add    %rax,%rdi
add    $0x6,%rdi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    122f <sh6_path_to_custom_programs+0xbf>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1288 <sh6_path_to_custom_programs+0x118>
mov    -0x50(%rbp),%rsi
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x38(%rbp),%rdi
call   1080 <strcat@plt>
mov    -0x38(%rbp),%rdi
lea    0xdb2(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x38(%rbp),%rdi
lea    0xd97(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sh6_path_to_custom_programs>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
add    $0x1,%esi
mov    %rsp,%r15
lea    0xf(%rsi),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    %r15,%rdi
call   1060 <getcwd@plt>
mov    %rbx,%rdi
call   1090 <strdup@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
movb   $0x0,(%rax)
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x6,%rdi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1229 <sh6_path_to_custom_programs+0xb9>
movb   $0x0,(%rbx)
mov    %rbx,%rdi
mov    %r15,%rsi
call   1080 <strcat@plt>
lea    0xe01(%rip),%rsi
mov    %rbx,%rdi
call   1080 <strcat@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <strcat@plt>
lea    0xde9(%rip),%rsi
mov    %rbx,%rdi
call   1080 <strcat@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [2 x i8] c"/\00"
@.str.1 = [5 x i8] c"/cmd\00"
define i8* @sh6_path_to_custom_programs(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %3
%10 = load i32, i32* @PATH_MAX
%11 = add nsw i32 %10, 1
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %4
%14 = alloca i8, i64 %12
store i64 %12, i64* %5
%15 = load i32, i32* @PATH_MAX
%16 = add nsw i32 %15, 1
%17 = call i32 @getcwd(i8* %14, i32 %16)
%18 = load i8*, i8** %3
%19 = call i8* @strdup(i8* %18)
store i8* %19, i8** %6
%20 = load i8*, i8** %6
%21 = call i8* @strrchr(i8* %20, i8 signext 47)
store i8* %21, i8** %7
%22 = load i8*, i8** %7
store i8 0, i8* %22
%23 = call i64 @strlen(i8* %14)
%24 = load i8*, i8** %6
%25 = call i64 @strlen(i8* %24)
%26 = add nsw i64 %23, %25
%27 = add nsw i64 %26, 6
%28 = call i8* @calloc(i64 %27, i32 1)
store i8* %28, i8** %8
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %31
30: ; preds = %1
store i8* null, i8** %2
store i32 1, i32* %9
br label %46
31: ; preds = %1
%32 = load i8*, i8** %8
%33 = getelementptr inbounds i8, i8* %32, i64 0
store i8 0, i8* %33
%34 = load i8*, i8** %8
%35 = call i32 @strcat(i8* %34, i8* %14)
%36 = load i8*, i8** %8
%37 = call i32 @strcat(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%38 = load i8*, i8** %8
%39 = load i8*, i8** %6
%40 = call i32 @strcat(i8* %38, i8* %39)
%41 = load i8*, i8** %8
%42 = call i32 @strcat(i8* %41, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%43 = load i8*, i8** %6
%44 = call i32 @free(i8* %43)
%45 = load i8*, i8** %8
store i8* %45, i8** %2
store i32 1, i32* %9
br label %46
46: ; preds = %31, %30
%47 = load i8*, i8** %4
%48 = load i8*, i8** %2
ret i8* %48
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [2 x i8] c"/\00"
@.str.1 = [5 x i8] c"/cmd\00"
define i8* @sh6_path_to_custom_programs(i8* nocapture %0) {
%2 = load i32, i32* @PATH_MAX
%3 = add nsw i32 %2, 1
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = call i32 @getcwd(i8* %5, i32 %3)
%7 = call i8* @strdup(i8* %0)
%8 = call i8* @strrchr(i8* dereferenceable(1) %7, i8 signext 47)
store i8 0, i8* %8
%9 = call i64 @strlen(i8* %5)
%10 = call i64 @strlen(i8* dereferenceable(1) %7)
%11 = add i64 %9, 6
%12 = add i64 %11, %10
%13 = call i8* @calloc(i64 %12, i32 1)
%14 = icmp eq i8* %13, null
br i1 %14, label %21, label %15
15: ; preds = %1
store i8 0, i8* %13
%16 = call i32 @strcat(i8* %13, i8* %5)
%17 = call i32 @strcat(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @strcat(i8* %13, i8* %7)
%19 = call i32 @strcat(i8* %13, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @free(i8* %7)
br label %21
21: ; preds = %1, %15
ret i8* %13
}