#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 */
char *memchr(char *, int, char);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
char *strrchr(char *, char);
char *qfits_get_dir_name(const char *filename) {
  static char path[4096];
  char *last_slash;
  if (strlen(filename) > 4096)
    return NULL;
  strcpy(path, filename);
  last_slash = path != NULL ? strrchr(path, '/') : NULL;
  if (last_slash == path)
    ++last_slash;
  else if (last_slash != NULL && last_slash[1] == '\0')
    last_slash = memchr(path, last_slash - path, '/');
  if (last_slash != NULL)
    last_slash[0] = '\0';
  else
    strcpy(path, ".");
  return path;
}


x86 assembly (clang, -O0):
<qfits_get_dir_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x1000,%eax
jle    116d <qfits_get_dir_name+0x2d>
movq   $0x0,-0x8(%rbp)
jmp    122f <qfits_get_dir_name+0xef>
mov    -0x10(%rbp),%rsi
lea    0x2ed8(%rip),%rdi
call   1030 <strcpy@plt>
lea    0x2ecc(%rip),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x18(%rbp)
lea    0x2eb7(%rip),%rax
cmp    %rax,-0x18(%rbp)
jne    11b4 <qfits_get_dir_name+0x74>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11fa <qfits_get_dir_name+0xba>
cmpq   $0x0,-0x18(%rbp)
je     11f5 <qfits_get_dir_name+0xb5>
mov    -0x18(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
jne    11f5 <qfits_get_dir_name+0xb5>
mov    -0x18(%rbp),%rax
lea    0x2e75(%rip),%rcx
sub    %rcx,%rax
mov    %eax,%esi
lea    0x2e69(%rip),%rdi
mov    $0x2f,%edx
call   1060 <memchr@plt>
mov    %rax,-0x18(%rbp)
jmp    11fa <qfits_get_dir_name+0xba>
cmpq   $0x0,-0x18(%rbp)
je     1211 <qfits_get_dir_name+0xd1>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
jmp    1224 <qfits_get_dir_name+0xe4>
lea    0x2e38(%rip),%rdi
lea    0xde1(%rip),%rsi
call   1030 <strcpy@plt>
lea    0x2e25(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<qfits_get_dir_name>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1040 <strlen@plt>
cmp    $0x1000,%eax
jle    1158 <qfits_get_dir_name+0x18>
xor    %r14d,%r14d
jmp    11c1 <qfits_get_dir_name+0x81>
lea    0x2ef1(%rip),%r14
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %r14,%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
cmp    %r14,%rax
je     11a6 <qfits_get_dir_name+0x66>
test   %rax,%rax
je     11b2 <qfits_get_dir_name+0x72>
cmpb   $0x0,0x1(%rax)
jne    11a1 <qfits_get_dir_name+0x61>
lea    0x2ec2(%rip),%rdi
sub    %edi,%eax
mov    %eax,%esi
mov    $0x2f,%edx
call   1060 <memchr@plt>
test   %rax,%rax
je     11b2 <qfits_get_dir_name+0x72>
movb   $0x0,(%rax)
jmp    11c1 <qfits_get_dir_name+0x81>
lea    0x2ea4(%rip),%rax
movb   $0x0,(%rax)
jmp    11c1 <qfits_get_dir_name+0x81>
lea    0xe47(%rip),%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@qfits_get_dir_name.path = internal global [4096 x i8] zeroinitializer
@.str = [2 x i8] c".\00"
define i8* @qfits_get_dir_name(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i32 @strlen(i8* %5)
%7 = icmp sgt i32 %6, 4096
br i1 %7, label %8, label %9
8: ; preds = %1
store i8* null, i8** %2
br label %43
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i32 @strcpy(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @qfits_get_dir_name.path, i64 0, i64 0), i8* %10)
%12 = call i8* @strrchr(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @qfits_get_dir_name.path, i64 0, i64 0), i8 signext 47)
store i8* %12, i8** %4
%13 = load i8*, i8** %4
%14 = icmp eq i8* %13, getelementptr inbounds ([4096 x i8], [4096 x i8]* @qfits_get_dir_name.path, i64 0, i64 0)
br i1 %14, label %15, label %18
15: ; preds = %9
%16 = load i8*, i8** %4
%17 = getelementptr inbounds i8, i8* %16, i32 1
store i8* %17, i8** %4
br label %34
18: ; preds = %9
%19 = load i8*, i8** %4
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %33
21: ; preds = %18
%22 = load i8*, i8** %4
%23 = getelementptr inbounds i8, i8* %22, i64 1
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %21
%28 = load i8*, i8** %4
%29 = ptrtoint i8* %28 to i64
%30 = sub i64 %29, ptrtoint ([4096 x i8]* @qfits_get_dir_name.path to i64)
%31 = trunc i64 %30 to i32
%32 = call i8* @memchr(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @qfits_get_dir_name.path, i64 0, i64 0), i32 %31, i8 signext 47)
store i8* %32, i8** %4
br label %33
33: ; preds = %27, %21, %18
br label %34
34: ; preds = %33, %15
%35 = load i8*, i8** %4
%36 = icmp ne i8* %35, null
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = load i8*, i8** %4
%39 = getelementptr inbounds i8, i8* %38, i64 0
store i8 0, i8* %39
br label %42
40: ; preds = %34
%41 = call i32 @strcpy(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @qfits_get_dir_name.path, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %42
42: ; preds = %40, %37
store i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @qfits_get_dir_name.path, i64 0, i64 0), i8** %2
br label %43
43: ; preds = %42, %8
%44 = load i8*, i8** %2
ret i8* %44
}

LLVM IR (-O2):
@qfits_get_dir_name.path = internal global [4096 x i8] zeroinitializer
@.str = [2 x i8] c".\00"
define i8* @qfits_get_dir_name(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp sgt i32 %2, 4096
br i1 %3, label %24, label %4
4: ; preds = %1
%5 = tail call i32 @strcpy(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @qfits_get_dir_name.path, i64 0, i64 0), i8* %0)
%6 = tail call i8* @strrchr(i8* dereferenceable(1) getelementptr inbounds ([4096 x i8], [4096 x i8]* @qfits_get_dir_name.path, i64 0, i64 0), i8 signext 47)
%7 = icmp eq i8* %6, getelementptr inbounds ([4096 x i8], [4096 x i8]* @qfits_get_dir_name.path, i64 0, i64 0)
br i1 %7, label %20, label %8
8: ; preds = %4
%9 = icmp eq i8* %6, null
br i1 %9, label %22, label %10
10: ; preds = %8
%11 = getelementptr inbounds i8, i8* %6, i64 1
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %10
%15 = ptrtoint i8* %6 to i64
%16 = trunc i64 %15 to i32
%17 = sub i32 %16, ptrtoint ([4096 x i8]* @qfits_get_dir_name.path to i32)
%18 = tail call i8* @memchr(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @qfits_get_dir_name.path, i64 0, i64 0), i32 %17, i8 signext 47)
%19 = icmp eq i8* %18, null
br i1 %19, label %22, label %20
20: ; preds = %4, %10, %14
%21 = phi i8* [ %18, %14 ], [ getelementptr inbounds ([4096 x i8], [4096 x i8]* @qfits_get_dir_name.path, i64 0, i64 1), %4 ], [ %6, %10 ]
store i8 0, i8* %21
br label %24
22: ; preds = %8, %14
%23 = tail call i32 @strcpy(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @qfits_get_dir_name.path, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %20, %22, %1
%25 = phi i8* [ null, %1 ], [ getelementptr inbounds ([4096 x i8], [4096 x i8]* @qfits_get_dir_name.path, i64 0, i64 0), %22 ], [ getelementptr inbounds ([4096 x i8], [4096 x i8]* @qfits_get_dir_name.path, i64 0, i64 0), %20 ]
ret i8* %25
}