#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
char *malloc(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char const *);
int strlen(char const *);
char *strrchr(char const *, char);
void split_filename(const char *filename, char **dirname, char **basename) {
  char *lastslash = strrchr(filename, '/');
  if (lastslash == NULL) {
    *basename = strdup(filename);
    *dirname = malloc(2);
    strcpy(*dirname, ".");
  } else {
    if (lastslash == (filename + strlen(filename) - 1)) {
      fprintf(stderr, "input filename cannot be a directory (%s)\n", filename);
      exit(EXIT_FAILURE);
    }
    *basename = strdup(lastslash + 1);
    *dirname = strdup(filename);
    (*dirname)[lastslash - filename] = '\0';
  }
}


x86 assembly (clang, -O0):
<split_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e0 <split_filename+0x70>
mov    -0x8(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    $0x2,%edi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xe2a(%rip),%rsi
call   1030 <strcpy@plt>
jmp    127d <split_filename+0x10d>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rcx
mov    %eax,%edx
mov    -0x28(%rbp),%rax
movslq %edx,%rdx
add    %rdx,%rcx
add    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    123d <split_filename+0xcd>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdd8(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1090 <strdup@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x20(%rbp),%rcx
mov    -0x8(%rbp),%rdx
sub    %rdx,%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<split_filename>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
mov    $0x2f,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     11d3 <split_filename+0x63>
mov    %rax,%rbx
mov    %r12,%rdi
call   1040 <strlen@plt>
cltq
add    %r12,%rax
add    $0xffffffffffffffff,%rax
cmp    %rax,%rbx
je     1205 <split_filename+0x95>
lea    0x1(%rbx),%rdi
call   1090 <strdup@plt>
mov    %rax,(%r15)
mov    %r12,%rdi
call   1090 <strdup@plt>
mov    %rax,(%r14)
sub    %r12,%rbx
movb   $0x0,(%rax,%rbx,1)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %r12,%rdi
call   1090 <strdup@plt>
mov    %rax,(%r15)
mov    $0x2,%edi
call   1070 <malloc@plt>
mov    %rax,(%r14)
lea    0xe0e(%rip),%rsi
mov    %rax,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <strcpy@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
lea    0xded(%rip),%rsi
mov    %r12,%rdx
call   1060 <fprintf@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
@stderr = global i32 0
@.str.1 = [43 x i8] c"input filename cannot be a directory (%s)\0A\00"
@EXIT_FAILURE = global i32 0
define void @split_filename(i8* %0, i8** %1, i8** %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i8**
%7 = alloca i8*
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i8** %2, i8*** %6
%8 = load i8*, i8** %4
%9 = call i8* @strrchr(i8* %8, i8 signext 47)
store i8* %9, i8** %7
%10 = load i8*, i8** %7
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %21
12: ; preds = %3
%13 = load i8*, i8** %4
%14 = call i8* @strdup(i8* %13)
%15 = load i8**, i8*** %6
store i8* %14, i8** %15
%16 = call i8* @malloc(i32 2)
%17 = load i8**, i8*** %5
store i8* %16, i8** %17
%18 = load i8**, i8*** %5
%19 = load i8*, i8** %18
%20 = call i32 @strcpy(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %52
21: ; preds = %3
%22 = load i8*, i8** %7
%23 = load i8*, i8** %4
%24 = load i8*, i8** %4
%25 = call i32 @strlen(i8* %24)
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %23, i64 %26
%28 = getelementptr inbounds i8, i8* %27, i64 -1
%29 = icmp eq i8* %22, %28
br i1 %29, label %30, label %36
30: ; preds = %21
%31 = load i32, i32* @stderr
%32 = load i8*, i8** %4
%33 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i8* %32)
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %21
%37 = load i8*, i8** %7
%38 = getelementptr inbounds i8, i8* %37, i64 1
%39 = call i8* @strdup(i8* %38)
%40 = load i8**, i8*** %6
store i8* %39, i8** %40
%41 = load i8*, i8** %4
%42 = call i8* @strdup(i8* %41)
%43 = load i8**, i8*** %5
store i8* %42, i8** %43
%44 = load i8**, i8*** %5
%45 = load i8*, i8** %44
%46 = load i8*, i8** %7
%47 = load i8*, i8** %4
%48 = ptrtoint i8* %46 to i64
%49 = ptrtoint i8* %47 to i64
%50 = sub i64 %48, %49
%51 = getelementptr inbounds i8, i8* %45, i64 %50
store i8 0, i8* %51
br label %52
52: ; preds = %36, %12
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
@stderr = global i32 0
@.str.1 = [43 x i8] c"input filename cannot be a directory (%s)\0A\00"
@EXIT_FAILURE = global i32 0
define void @split_filename(i8* %0, i8** nocapture writeonly %1, i8** nocapture writeonly %2) {
%4 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %10
6: ; preds = %3
%7 = tail call i8* @strdup(i8* %0)
store i8* %7, i8** %2
%8 = tail call dereferenceable_or_null(2) i8* @malloc(i32 2)
store i8* %8, i8** %1
%9 = tail call i32 @strcpy(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %29
10: ; preds = %3
%11 = tail call i32 @strlen(i8* %0)
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %0, i64 %12
%14 = getelementptr inbounds i8, i8* %13, i64 -1
%15 = icmp eq i8* %4, %14
br i1 %15, label %16, label %21
16: ; preds = %10
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %10
%22 = getelementptr inbounds i8, i8* %4, i64 1
%23 = tail call i8* @strdup(i8* %22)
store i8* %23, i8** %2
%24 = tail call i8* @strdup(i8* %0)
store i8* %24, i8** %1
%25 = ptrtoint i8* %4 to i64
%26 = ptrtoint i8* %0 to i64
%27 = sub i64 %25, %26
%28 = getelementptr inbounds i8, i8* %24, i64 %27
store i8 0, i8* %28
br label %29
29: ; preds = %21, %6
ret void
}