#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 */
scalar_t__ fatfs_get_substring(char *, int, char *, int);
int fatfs_total_path_levels(char *);
int /*<<< orphan*/ memcpy(char *, char *, int);
scalar_t__ strlen(char *);
int fatfs_split_path(char *full_path, char *path, int max_path, char *filename,
                     int max_filename) {
  int strindex;
  int levels = fatfs_total_path_levels(full_path);
  if (levels == -1)
    return -1;
  if (fatfs_get_substring(full_path, levels, filename, max_filename) != 0)
    return -1;
  if (levels == 0)
    path[0] = '\0';
  else {
    strindex = (int)strlen(full_path) - (int)strlen(filename);
    if (strindex > max_path)
      strindex = max_path;
    memcpy(path, full_path, strindex);
    path[strindex - 1] = '\0';
  }
  return 0;
}


x86 assembly (clang, -O0):
<fatfs_split_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fatfs_total_path_levels@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    117d <fatfs_split_path+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1216 <fatfs_split_path+0xd6>
mov    -0x10(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    -0x28(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
call   1060 <fatfs_get_substring@plt>
cmp    $0x0,%rax
je     11a6 <fatfs_split_path+0x66>
movl   $0xffffffff,-0x4(%rbp)
jmp    1216 <fatfs_split_path+0xd6>
cmpl   $0x0,-0x34(%rbp)
jne    11bc <fatfs_split_path+0x7c>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
jmp    120f <fatfs_split_path+0xcf>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jle    11ee <fatfs_split_path+0xae>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fatfs_split_path>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%r13d
mov    %rcx,%r12
mov    %edx,0x4(%rsp)
mov    %rsi,%r14
mov    %rdi,%rbx
call   1040 <fatfs_total_path_levels@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     11c2 <fatfs_split_path+0x82>
mov    %eax,%r15d
mov    %rbx,%rdi
mov    %eax,%esi
mov    %r12,%rdx
mov    %r13d,%ecx
call   1060 <fatfs_get_substring@plt>
test   %rax,%rax
jne    11c2 <fatfs_split_path+0x82>
test   %r15d,%r15d
je     11bc <fatfs_split_path+0x7c>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbp
mov    %r12,%rdi
call   1030 <strlen@plt>
sub    %eax,%ebp
mov    0x4(%rsp),%eax
cmp    %eax,%ebp
cmovg  %eax,%ebp
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <memcpy@plt>
movslq %ebp,%rax
add    %rax,%r14
add    $0xffffffffffffffff,%r14
movb   $0x0,(%r14)
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @fatfs_split_path(i8* %0, i8* %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i32 %4, i32* %11
%14 = load i8*, i8** %7
%15 = call i32 @fatfs_total_path_levels(i8* %14)
store i32 %15, i32* %13
%16 = load i32, i32* %13
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %5
store i32 -1, i32* %6
br label %57
19: ; preds = %5
%20 = load i8*, i8** %7
%21 = load i32, i32* %13
%22 = load i8*, i8** %10
%23 = load i32, i32* %11
%24 = call i64 @fatfs_get_substring(i8* %20, i32 %21, i8* %22, i32 %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %19
store i32 -1, i32* %6
br label %57
27: ; preds = %19
%28 = load i32, i32* %13
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = load i8*, i8** %8
%32 = getelementptr inbounds i8, i8* %31, i64 0
store i8 0, i8* %32
br label %56
33: ; preds = %27
%34 = load i8*, i8** %7
%35 = call i64 @strlen(i8* %34)
%36 = trunc i64 %35 to i32
%37 = load i8*, i8** %10
%38 = call i64 @strlen(i8* %37)
%39 = trunc i64 %38 to i32
%40 = sub nsw i32 %36, %39
store i32 %40, i32* %12
%41 = load i32, i32* %12
%42 = load i32, i32* %9
%43 = icmp sgt i32 %41, %42
br i1 %43, label %44, label %46
44: ; preds = %33
%45 = load i32, i32* %9
store i32 %45, i32* %12
br label %46
46: ; preds = %44, %33
%47 = load i8*, i8** %8
%48 = load i8*, i8** %7
%49 = load i32, i32* %12
%50 = call i32 @memcpy(i8* %47, i8* %48, i32 %49)
%51 = load i8*, i8** %8
%52 = load i32, i32* %12
%53 = sub nsw i32 %52, 1
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %51, i64 %54
store i8 0, i8* %55
br label %56
56: ; preds = %46, %30
store i32 0, i32* %6
br label %57
57: ; preds = %56, %26, %18
%58 = load i32, i32* %6
ret i32 %58
}

LLVM IR (-O2):
define i32 @fatfs_split_path(i8* %0, i8* %1, i32 %2, i8* %3, i32 %4) {
%6 = tail call i32 @fatfs_total_path_levels(i8* %0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %27, label %8
8: ; preds = %5
%9 = tail call i64 @fatfs_get_substring(i8* %0, i32 %6, i8* %3, i32 %4)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %27
11: ; preds = %8
%12 = icmp eq i32 %6, 0
br i1 %12, label %25, label %13
13: ; preds = %11
%14 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%15 = trunc i64 %14 to i32
%16 = tail call i64 @strlen(i8* dereferenceable(1) %3)
%17 = trunc i64 %16 to i32
%18 = sub nsw i32 %15, %17
%19 = icmp sgt i32 %18, %2
%20 = select i1 %19, i32 %2, i32 %18
%21 = tail call i32 @memcpy(i8* %1, i8* %0, i32 %20)
%22 = add nsw i32 %20, -1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %1, i64 %23
br label %25
25: ; preds = %11, %13
%26 = phi i8* [ %24, %13 ], [ %1, %11 ]
store i8 0, i8* %26
br label %27
27: ; preds = %25, %8, %5
%28 = phi i32 [ -1, %5 ], [ -1, %8 ], [ 0, %25 ]
ret i32 %28
}