#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 *malloc(int);
char *strdup(char const *);
int strlen(char const *);
int /*<<< orphan*/ strncat(char *, char const *, int);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *redirect_writable_path(const char *pathname, const char *basepath) {
  char *redirected_pathname;
  int chop = 0;
  if (pathname[0] == 0) {
    return strdup(basepath);
  }
  redirected_pathname = malloc(PATH_MAX);
  if (basepath[strlen(basepath) - 1] == '/') {
    chop = 1;
  }
  strncpy(redirected_pathname, basepath, PATH_MAX - 1 - chop);
  strncat(redirected_pathname, pathname,
          PATH_MAX - 1 - strlen(redirected_pathname));
  return redirected_pathname;
}


x86 assembly (clang, -O0):
<redirect_writable_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1189 <redirect_writable_path+0x39>
mov    -0x18(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    122a <redirect_writable_path+0xda>
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    11cc <redirect_writable_path+0x7c>
movl   $0x1,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2e15(%rip),%rax
mov    (%rax),%edx
sub    $0x1,%edx
sub    -0x24(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
sub    $0x1,%eax
mov    %eax,-0x44(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x44(%rbp),%edx
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
sub    %eax,%edx
call   1050 <strncat@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<redirect_writable_path>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
cmpb   $0x0,(%rdi)
je     11c7 <redirect_writable_path+0x77>
mov    %rdi,%r14
mov    0x2e86(%rip),%r12
mov    (%r12),%edi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
xor    %edx,%edx
cmpb   $0x2f,-0x1(%rax,%rbx,1)
sete   %dl
not    %edx
add    (%r12),%edx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    (%r12),%ebx
mov    %r15,%rdi
call   1040 <strlen@plt>
not    %eax
add    %eax,%ebx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1050 <strncat@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1070 <strdup@plt>

LLVM IR (-O0):
@PATH_MAX = global i32 0
define i8* @redirect_writable_path(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %7
%8 = load i8*, i8** %4
%9 = getelementptr inbounds i8, i8* %8, i64 0
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i8*, i8** %5
%15 = call i8* @strdup(i8* %14)
store i8* %15, i8** %3
br label %46
16: ; preds = %2
%17 = load i32, i32* @PATH_MAX
%18 = call i8* @malloc(i32 %17)
store i8* %18, i8** %6
%19 = load i8*, i8** %5
%20 = load i8*, i8** %5
%21 = call i32 @strlen(i8* %20)
%22 = sub nsw i32 %21, 1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %19, i64 %23
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 47
br i1 %27, label %28, label %29
28: ; preds = %16
store i32 1, i32* %7
br label %29
29: ; preds = %28, %16
%30 = load i8*, i8** %6
%31 = load i8*, i8** %5
%32 = load i32, i32* @PATH_MAX
%33 = sub nsw i32 %32, 1
%34 = load i32, i32* %7
%35 = sub nsw i32 %33, %34
%36 = call i32 @strncpy(i8* %30, i8* %31, i32 %35)
%37 = load i8*, i8** %6
%38 = load i8*, i8** %4
%39 = load i32, i32* @PATH_MAX
%40 = sub nsw i32 %39, 1
%41 = load i8*, i8** %6
%42 = call i32 @strlen(i8* %41)
%43 = sub nsw i32 %40, %42
%44 = call i32 @strncat(i8* %37, i8* %38, i32 %43)
%45 = load i8*, i8** %6
store i8* %45, i8** %3
br label %46
46: ; preds = %29, %13
%47 = load i8*, i8** %3
ret i8* %47
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
define i8* @redirect_writable_path(i8* %0, i8* %1) {
%3 = load i8, i8* %0
%4 = icmp eq i8 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i8* @strdup(i8* %1)
br label %26
7: ; preds = %2
%8 = load i32, i32* @PATH_MAX
%9 = tail call i8* @malloc(i32 %8)
%10 = tail call i32 @strlen(i8* %1)
%11 = add nsw i32 %10, -1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %1, i64 %12
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 47
%16 = sext i1 %15 to i32
%17 = load i32, i32* @PATH_MAX
%18 = add nsw i32 %17, -1
%19 = add i32 %18, %16
%20 = tail call i32 @strncpy(i8* %9, i8* %1, i32 %19)
%21 = load i32, i32* @PATH_MAX
%22 = tail call i32 @strlen(i8* %9)
%23 = xor i32 %22, -1
%24 = add i32 %21, %23
%25 = tail call i32 @strncat(i8* %9, i8* %0, i32 %24)
br label %26
26: ; preds = %7, %5
%27 = phi i8* [ %6, %5 ], [ %9, %7 ]
ret i8* %27
}