int trydiraccess(filename)
char *filename;
{
  char pathname[6 * 14];
  register char *tp, *sp, *lp;
  lp = rindex(filename, '/');
  if (lp == 0) {
    if (access(".", 2) == 0)
      return 1;
    else {
      error("Current directory not writable");
      return 0;
    }
  }
  sp = filename;
  tp = pathname;
  do
    *tp++ = *sp++;
  while (sp <= lp);
  *tp = '\0';
  if (access(pathname, 2) == 0)
    return 1;
  else {
    error("Directory %s not writable", pathname);
    return 0;
  }
}


x86 assembly (clang, -O0):
<trydiraccess>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <rindex@plt>
mov    %rax,-0x88(%rbp)
cmpq   $0x0,-0x88(%rbp)
jne    11a4 <trydiraccess+0x74>
lea    0xe97(%rip),%rdi
mov    $0x2,%esi
mov    $0x0,%al
call   1040 <access@plt>
cmp    $0x0,%eax
jne    118a <trydiraccess+0x5a>
movl   $0x1,-0x4(%rbp)
jmp    122c <trydiraccess+0xfc>
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1030 <error@plt>
movl   $0x0,-0x4(%rbp)
jmp    122c <trydiraccess+0xfc>
mov    -0x10(%rbp),%rax
mov    %rax,-0x80(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x80(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x80(%rbp)
mov    (%rax),%cl
mov    -0x78(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x78(%rbp)
mov    %cl,(%rax)
mov    -0x80(%rbp),%rax
cmp    -0x88(%rbp),%rax
jbe    11b4 <trydiraccess+0x84>
mov    -0x78(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x70(%rbp),%rdi
mov    $0x2,%esi
mov    $0x0,%al
call   1040 <access@plt>
cmp    $0x0,%eax
jne    1213 <trydiraccess+0xe3>
movl   $0x1,-0x4(%rbp)
jmp    122c <trydiraccess+0xfc>
lea    -0x70(%rbp),%rsi
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1030 <error@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trydiraccess>:
push   %r15
push   %r14
push   %rbx
sub    $0x60,%rsp
mov    %rdi,%r14
mov    $0x2f,%esi
call   1060 <rindex@plt>
test   %rax,%rax
je     11ad <trydiraccess+0x6d>
mov    %rax,%rbx
lea    0x1(%r14),%rax
add    $0x1,%rbx
cmp    %rbx,%rax
cmova  %rax,%rbx
sub    %r14,%rbx
mov    %rsp,%r15
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <memcpy@plt>
movb   $0x0,(%rsp,%rbx,1)
xor    %ebx,%ebx
mov    %r15,%rdi
mov    $0x2,%esi
xor    %eax,%eax
call   1050 <access@plt>
test   %eax,%eax
je     11d6 <trydiraccess+0x96>
lea    0xe80(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1040 <error@plt>
jmp    11db <trydiraccess+0x9b>
lea    0xe4c(%rip),%rdi
xor    %ebx,%ebx
mov    $0x2,%esi
xor    %eax,%eax
call   1050 <access@plt>
test   %eax,%eax
je     11d6 <trydiraccess+0x96>
lea    0xe35(%rip),%rdi
xor    %eax,%eax
call   1040 <error@plt>
jmp    11db <trydiraccess+0x9b>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x60,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
@.str.1 = [31 x i8] c"Current directory not writable\00"
@.str.2 = [26 x i8] c"Directory %s not writable\00"
define i32 @trydiraccess(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [84 x i8]
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i8* @rindex(i8* %8, i32 47)
store i8* %9, i8** %7
%10 = load i8*, i8** %7
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %18
12: ; preds = %1
%13 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @access to i32 (i8*, i32, ...)*)(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 2)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %12
store i32 1, i32* %2
br label %40
16: ; preds = %12
%17 = call i32 (i8*, ...) bitcast (i32 (...)* @error to i32 (i8*, ...)*)(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %2
br label %40
18: ; preds = %1
%19 = load i8*, i8** %3
store i8* %19, i8** %6
%20 = getelementptr inbounds [84 x i8], [84 x i8]* %4, i64 0, i64 0
store i8* %20, i8** %5
br label %21
21: ; preds = %27, %18
%22 = load i8*, i8** %6
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %6
%24 = load i8, i8* %22
%25 = load i8*, i8** %5
%26 = getelementptr inbounds i8, i8* %25, i32 1
store i8* %26, i8** %5
store i8 %24, i8* %25
br label %27
27: ; preds = %21
%28 = load i8*, i8** %6
%29 = load i8*, i8** %7
%30 = icmp ule i8* %28, %29
br i1 %30, label %21, label %31
31: ; preds = %27
%32 = load i8*, i8** %5
store i8 0, i8* %32
%33 = getelementptr inbounds [84 x i8], [84 x i8]* %4, i64 0, i64 0
%34 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @access to i32 (i8*, i32, ...)*)(i8* %33, i32 2)
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %31
store i32 1, i32* %2
br label %40
37: ; preds = %31
%38 = getelementptr inbounds [84 x i8], [84 x i8]* %4, i64 0, i64 0
%39 = call i32 (i8*, i8*, ...) bitcast (i32 (...)* @error to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %38)
store i32 0, i32* %2
br label %40
40: ; preds = %37, %36, %16, %15
%41 = load i32, i32* %2
ret i32 %41
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
@.str.1 = [31 x i8] c"Current directory not writable\00"
@.str.2 = [26 x i8] c"Directory %s not writable\00"
define i32 @trydiraccess(i8* %0) {
%2 = ptrtoint i8* %0 to i64
%3 = alloca [84 x i8]
%4 = getelementptr inbounds [84 x i8], [84 x i8]* %3, i64 0, i64 0
%5 = tail call i8* @rindex(i8* %0, i32 47)
%6 = icmp eq i8* %5, null
br i1 %6, label %17, label %7
7: ; preds = %1
%8 = ptrtoint i8* %5 to i64
%9 = getelementptr inbounds [84 x i8], [84 x i8]* %3, i64 0, i64 0
%10 = add i64 %2, 1
%11 = add nuw i64 %8, 1
%12 = call i64 @llvm.umax.i64(i64 %10, i64 %11)
%13 = sub i64 %12, %2
%14 = getelementptr [84 x i8], [84 x i8]* %3, i64 0, i64 %13
store i8 0, i8* %14
%15 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @access to i32 (i8*, i32, ...)*)(i8* %4, i32 2)
%16 = icmp eq i32 %15, 0
br i1 %16, label %24, label %22
17: ; preds = %1
%18 = tail call i32 (i8*, i32, ...) bitcast (i32 (...)* @access to i32 (i8*, i32, ...)*)(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 2)
%19 = icmp eq i32 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %17
%21 = tail call i32 (i8*, ...) bitcast (i32 (...)* @error to i32 (i8*, ...)*)(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %24
22: ; preds = %7
%23 = call i32 (i8*, i8*, ...) bitcast (i32 (...)* @error to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %4)
br label %24
24: ; preds = %7, %17, %22, %20
%25 = phi i32 [ 0, %20 ], [ 0, %22 ], [ 1, %17 ], [ 1, %7 ]
ret i32 %25
}