#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 fatfs_lfn_entries_required(char *);
int /*<<< orphan*/ memset(unsigned char *, int, int);
scalar_t__ strlen(char *);
void fatfs_filename_to_lfn(char *filename, unsigned char *buffer, int entry,
                           unsigned char sfnChk) {
  int i;
  int nameIndexes[13] = {1,    3,    5,    7,    9,    0x0E, 0x10,
                         0x12, 0x14, 0x16, 0x18, 0x1C, 0x1E};
  int length = (int)strlen(filename);
  int entriesRequired = fatfs_lfn_entries_required(filename);
  int start = entry * 13;
  memset(buffer, 0x00, 32);
  buffer[0] =
      (unsigned char)(((entriesRequired - 1) == entry) ? (0x40 | (entry + 1))
                                                       : (entry + 1));
  buffer[11] = 0x0F;
  buffer[13] = sfnChk;
  for (i = 0; i < 13; i++) {
    if ((start + i) < length)
      buffer[nameIndexes[i]] = filename[start + i];
    else if ((start + i) == length)
      buffer[nameIndexes[i]] = 0x00;
    else {
      buffer[nameIndexes[i]] = 0xFF;
      buffer[nameIndexes[i] + 1] = 0xFF;
    }
  }
}


x86 assembly (clang, -O0):
<fatfs_filename_to_lfn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %cl,%al
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %al,-0x15(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xe9d(%rip),%rsi
mov    $0x34,%edx
call   1050 <memcpy@plt>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x54(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <fatfs_lfn_entries_required@plt>
mov    %eax,-0x58(%rbp)
imul   $0xd,-0x14(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    -0x58(%rbp),%eax
sub    $0x1,%eax
cmp    -0x14(%rbp),%eax
jne    11bc <fatfs_filename_to_lfn+0x7c>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
or     $0x40,%eax
mov    %eax,-0x60(%rbp)
jmp    11c5 <fatfs_filename_to_lfn+0x85>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x60(%rbp)
mov    -0x60(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
movb   $0xf,0xb(%rax)
mov    -0x15(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %cl,0xd(%rax)
movl   $0x0,-0x1c(%rbp)
cmpl   $0xd,-0x1c(%rbp)
jge    128b <fatfs_filename_to_lfn+0x14b>
mov    -0x5c(%rbp),%eax
add    -0x1c(%rbp),%eax
cmp    -0x54(%rbp),%eax
jge    1227 <fatfs_filename_to_lfn+0xe7>
mov    -0x8(%rbp),%rax
mov    -0x5c(%rbp),%ecx
add    -0x1c(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movslq -0x50(%rbp,%rcx,4),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1278 <fatfs_filename_to_lfn+0x138>
mov    -0x5c(%rbp),%eax
add    -0x1c(%rbp),%eax
cmp    -0x54(%rbp),%eax
jne    124c <fatfs_filename_to_lfn+0x10c>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movslq -0x50(%rbp,%rcx,4),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1273 <fatfs_filename_to_lfn+0x133>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movslq -0x50(%rbp,%rcx,4),%rcx
movb   $0xff,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    -0x50(%rbp,%rcx,4),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0xff,(%rax,%rcx,1)
jmp    1278 <fatfs_filename_to_lfn+0x138>
jmp    127d <fatfs_filename_to_lfn+0x13d>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11e9 <fatfs_filename_to_lfn+0xa9>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fatfs_filename_to_lfn>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,0xc(%rsp)
mov    %edx,%r13d
mov    %rsi,%r12
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %rax,0x10(%rsp)
mov    %r14,%rdi
call   1050 <fatfs_lfn_entries_required@plt>
mov    %eax,%ebp
lea    0x0(%r13,%r13,2),%eax
lea    0x0(%r13,%rax,4),%ebx
xor    %r15d,%r15d
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
add    $0xffffffff,%ebp
lea    0x1(%r13),%eax
movzbl %al,%ecx
or     $0x40,%al
cmp    %r13d,%ebp
movzbl %al,%eax
cmovne %ecx,%eax
mov    %al,(%r12)
movb   $0xf,0xb(%r12)
mov    0xc(%rsp),%eax
mov    %al,0xd(%r12)
movslq %ebx,%rax
movslq 0x10(%rsp),%rcx
mov    %ecx,%edx
sub    %eax,%edx
lea    0xe4a(%rip),%rsi
jmp    11de <fatfs_filename_to_lfn+0xae>
nopl   0x0(%rax,%rax,1)
movzbl (%r14,%rax,1),%ebx
movslq (%r15,%rsi,1),%rdi
mov    %bl,(%r12,%rdi,1)
add    $0xffffffff,%edx
add    $0x1,%rax
add    $0x4,%r15
cmp    $0x34,%r15
je     11ff <fatfs_filename_to_lfn+0xcf>
cmp    %rcx,%rax
jl     11c0 <fatfs_filename_to_lfn+0x90>
movslq (%r15,%rsi,1),%rdi
test   %edx,%edx
je     11f8 <fatfs_filename_to_lfn+0xc8>
movb   $0xff,(%r12,%rdi,1)
movb   $0xff,0x1(%rdi,%r12,1)
jmp    11cd <fatfs_filename_to_lfn+0x9d>
movb   $0x0,(%r12,%rdi,1)
jmp    11cd <fatfs_filename_to_lfn+0x9d>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.fatfs_filename_to_lfn.nameIndexes = [13 x i32] [i32 1, i32 3, i32 5, i32 7, i32 9, i32 14, i32 16, i32 18, i32 20, i32 22, i32 24, i32 28, i32 30]
define void @fatfs_filename_to_lfn(i8* %0, i8* %1, i32 %2, i8 zeroext %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8
%9 = alloca i32
%10 = alloca [13 x i32]
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i8 %3, i8* %8
%14 = bitcast [13 x i32]* %10 to i8*
%15 = load i8*, i8** %5
%16 = call i64 @strlen(i8* %15)
%17 = trunc i64 %16 to i32
store i32 %17, i32* %11
%18 = load i8*, i8** %5
%19 = call i32 @fatfs_lfn_entries_required(i8* %18)
store i32 %19, i32* %12
%20 = load i32, i32* %7
%21 = mul nsw i32 %20, 13
store i32 %21, i32* %13
%22 = load i8*, i8** %6
%23 = call i32 @memset(i8* %22, i32 0, i32 32)
%24 = load i32, i32* %12
%25 = sub nsw i32 %24, 1
%26 = load i32, i32* %7
%27 = icmp eq i32 %25, %26
br i1 %27, label %28, label %32
28: ; preds = %4
%29 = load i32, i32* %7
%30 = add nsw i32 %29, 1
%31 = or i32 64, %30
br label %35
32: ; preds = %4
%33 = load i32, i32* %7
%34 = add nsw i32 %33, 1
br label %35
35: ; preds = %32, %28
%36 = phi i32 [ %31, %28 ], [ %34, %32 ]
%37 = trunc i32 %36 to i8
%38 = load i8*, i8** %6
%39 = getelementptr inbounds i8, i8* %38, i64 0
store i8 %37, i8* %39
%40 = load i8*, i8** %6
%41 = getelementptr inbounds i8, i8* %40, i64 11
store i8 15, i8* %41
%42 = load i8, i8* %8
%43 = load i8*, i8** %6
%44 = getelementptr inbounds i8, i8* %43, i64 13
store i8 %42, i8* %44
store i32 0, i32* %9
br label %45
45: ; preds = %101, %35
%46 = load i32, i32* %9
%47 = icmp slt i32 %46, 13
br i1 %47, label %48, label %104
48: ; preds = %45
%49 = load i32, i32* %13
%50 = load i32, i32* %9
%51 = add nsw i32 %49, %50
%52 = load i32, i32* %11
%53 = icmp slt i32 %51, %52
br i1 %53, label %54, label %69
54: ; preds = %48
%55 = load i8*, i8** %5
%56 = load i32, i32* %13
%57 = load i32, i32* %9
%58 = add nsw i32 %56, %57
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %55, i64 %59
%61 = load i8, i8* %60
%62 = load i8*, i8** %6
%63 = load i32, i32* %9
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds [13 x i32], [13 x i32]* %10, i64 0, i64 %64
%66 = load i32, i32* %65
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds i8, i8* %62, i64 %67
store i8 %61, i8* %68
br label %100
69: ; preds = %48
%70 = load i32, i32* %13
%71 = load i32, i32* %9
%72 = add nsw i32 %70, %71
%73 = load i32, i32* %11
%74 = icmp eq i32 %72, %73
br i1 %74, label %75, label %83
75: ; preds = %69
%76 = load i8*, i8** %6
%77 = load i32, i32* %9
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds [13 x i32], [13 x i32]* %10, i64 0, i64 %78
%80 = load i32, i32* %79
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds i8, i8* %76, i64 %81
store i8 0, i8* %82
br label %99
83: ; preds = %69
%84 = load i8*, i8** %6
%85 = load i32, i32* %9
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds [13 x i32], [13 x i32]* %10, i64 0, i64 %86
%88 = load i32, i32* %87
%89 = sext i32 %88 to i64
%90 = getelementptr inbounds i8, i8* %84, i64 %89
store i8 -1, i8* %90
%91 = load i8*, i8** %6
%92 = load i32, i32* %9
%93 = sext i32 %92 to i64
%94 = getelementptr inbounds [13 x i32], [13 x i32]* %10, i64 0, i64 %93
%95 = load i32, i32* %94
%96 = add nsw i32 %95, 1
%97 = sext i32 %96 to i64
%98 = getelementptr inbounds i8, i8* %91, i64 %97
store i8 -1, i8* %98
br label %99
99: ; preds = %83, %75
br label %100
100: ; preds = %99, %54
br label %101
101: ; preds = %100
%102 = load i32, i32* %9
%103 = add nsw i32 %102, 1
store i32 %103, i32* %9
br label %45
104: ; preds = %45
ret void
}

LLVM IR (-O2):
@__const.fatfs_filename_to_lfn.nameIndexes = [13 x i32] [i32 1, i32 3, i32 5, i32 7, i32 9, i32 14, i32 16, i32 18, i32 20, i32 22, i32 24, i32 28, i32 30]
define void @fatfs_filename_to_lfn(i8* %0, i8* %1, i32 %2, i8 zeroext %3) {
%5 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%6 = trunc i64 %5 to i32
%7 = tail call i32 @fatfs_lfn_entries_required(i8* %0)
%8 = mul nsw i32 %2, 13
%9 = tail call i32 @memset(i8* %1, i32 0, i32 32)
%10 = add nsw i32 %7, -1
%11 = icmp eq i32 %10, %2
%12 = add nsw i32 %2, 1
%13 = or i32 %12, 64
%14 = select i1 %11, i32 %13, i32 %12
%15 = trunc i32 %14 to i8
store i8 %15, i8* %1
%16 = getelementptr inbounds i8, i8* %1, i64 11
store i8 15, i8* %16
%17 = getelementptr inbounds i8, i8* %1, i64 13
store i8 %3, i8* %17
%18 = sext i32 %8 to i64
%19 = shl i64 %5, 32
%20 = ashr exact i64 %19, 32
br label %21
21: ; preds = %4, %44
%22 = phi i64 [ 0, %4 ], [ %45, %44 ]
%23 = add nsw i64 %22, %18
%24 = icmp slt i64 %23, %20
br i1 %24, label %25, label %32
25: ; preds = %21
%26 = getelementptr inbounds i8, i8* %0, i64 %23
%27 = load i8, i8* %26
%28 = getelementptr inbounds [13 x i32], [13 x i32]* @__const.fatfs_filename_to_lfn.nameIndexes, i64 0, i64 %22
%29 = load i32, i32* %28
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %1, i64 %30
store i8 %27, i8* %31
br label %44
32: ; preds = %21
%33 = trunc i64 %23 to i32
%34 = icmp eq i32 %33, %6
%35 = getelementptr inbounds [13 x i32], [13 x i32]* @__const.fatfs_filename_to_lfn.nameIndexes, i64 0, i64 %22
%36 = load i32, i32* %35
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %1, i64 %37
br i1 %34, label %39, label %40
39: ; preds = %32
store i8 0, i8* %38
br label %44
40: ; preds = %32
store i8 -1, i8* %38
%41 = add nsw i32 %36, 1
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %1, i64 %42
store i8 -1, i8* %43
br label %44
44: ; preds = %25, %40, %39
%45 = add nuw nsw i64 %22, 1
%46 = icmp eq i64 %45, 13
br i1 %46, label %47, label %21
47: ; preds = %44
ret void
}