#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 /*<<< orphan*/ cfatal(char *, char *);
int /*<<< orphan*/ memcpy(char *, char *, int);
char *strrchr(char *, char);
int hasdirectory(char *source, char *result, int max) {
  char *tp;
  if ((tp = strrchr(source, '/')) == NULL)
    return (0);
  else {
    int len = (int)(tp - source);
    if (len < max) {
      memcpy(result, source, len);
      result[len] = 0;
    } else {
      cfatal("Filename work buffer overflow", ((char *)NULL));
    }
    return (1);
  }
}


x86 assembly (clang, -O0):
<hasdirectory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1030 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    116b <hasdirectory+0x3b>
movl   $0x0,-0x4(%rbp)
jmp    11bd <hasdirectory+0x8d>
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11a6 <hasdirectory+0x76>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11b6 <hasdirectory+0x86>
lea    0xe53(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <cfatal@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hasdirectory>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0x2f,%esi
call   1030 <strrchr@plt>
test   %rax,%rax
je     116f <hasdirectory+0x3f>
mov    %rax,%rbx
sub    %r15,%rbx
cmp    %ebp,%ebx
jge    1173 <hasdirectory+0x43>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1040 <memcpy@plt>
movslq %ebx,%rax
movb   $0x0,(%r14,%rax,1)
jmp    1181 <hasdirectory+0x51>
xor    %eax,%eax
jmp    1186 <hasdirectory+0x56>
lea    0xe86(%rip),%rdi
xor    %esi,%esi
call   1050 <cfatal@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [30 x i8] c"Filename work buffer overflow\00"
define i32 @hasdirectory(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = call i8* @strrchr(i8* %10, i8 signext 47)
store i8* %11, i8** %8
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %14
13: ; preds = %3
store i32 0, i32* %4
br label %36
14: ; preds = %3
%15 = load i8*, i8** %8
%16 = load i8*, i8** %5
%17 = ptrtoint i8* %15 to i64
%18 = ptrtoint i8* %16 to i64
%19 = sub i64 %17, %18
%20 = trunc i64 %19 to i32
store i32 %20, i32* %9
%21 = load i32, i32* %9
%22 = load i32, i32* %7
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %33
24: ; preds = %14
%25 = load i8*, i8** %6
%26 = load i8*, i8** %5
%27 = load i32, i32* %9
%28 = call i32 @memcpy(i8* %25, i8* %26, i32 %27)
%29 = load i8*, i8** %6
%30 = load i32, i32* %9
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8 0, i8* %32
br label %35
33: ; preds = %14
%34 = call i32 @cfatal(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* null)
br label %35
35: ; preds = %33, %24
store i32 1, i32* %4
br label %36
36: ; preds = %35, %13
%37 = load i32, i32* %4
ret i32 %37
}

LLVM IR (-O2):
@.str = [30 x i8] c"Filename work buffer overflow\00"
define i32 @hasdirectory(i8* %0, i8* %1, i32 %2) {
%4 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%5 = icmp eq i8* %4, null
br i1 %5, label %19, label %6
6: ; preds = %3
%7 = ptrtoint i8* %4 to i64
%8 = ptrtoint i8* %0 to i64
%9 = sub i64 %7, %8
%10 = trunc i64 %9 to i32
%11 = icmp slt i32 %10, %2
br i1 %11, label %12, label %17
12: ; preds = %6
%13 = tail call i32 @memcpy(i8* %1, i8* %0, i32 %10)
%14 = shl i64 %9, 32
%15 = ashr exact i64 %14, 32
%16 = getelementptr inbounds i8, i8* %1, i64 %15
store i8 0, i8* %16
br label %19
17: ; preds = %6
%18 = tail call i32 @cfatal(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* null)
br label %19
19: ; preds = %12, %17, %3
%20 = phi i32 [ 0, %3 ], [ 1, %17 ], [ 1, %12 ]
ret i32 %20
}