#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*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *strtok(char *, char *);
char FAT_filename_to_fat_name(char *filename, char *FAT_name) {
  char *name;
  unsigned int i;
  char is_longname = 0;
  char *temp = (char *)malloc(sizeof(char) * (strlen(filename) + 1));
  strcpy(temp, filename);
  for (i = 0; i < 11; i++) {
    FAT_name[i] = ' ';
  }
  name = strtok(temp, ".");
  if (strlen(name) <= 8) {
    memcpy(FAT_name, name, strlen(name));
  } else {
    memcpy(FAT_name, name, 7);
    FAT_name[7] = '~';
    is_longname = 1;
  }
  if ((name = strtok(NULL, ".")) != NULL) {
    if (strlen(name) <= 3) {
      memcpy(FAT_name + 8, name, strlen(name));
    } else {
      FAT_name[8] = name[0];
      FAT_name[9] = name[1];
      FAT_name[10] = '~';
      is_longname = 1;
    }
  }
  free(temp);
  return is_longname;
}


x86 assembly (clang, -O0):
<FAT_filename_to_fat_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movb   $0x0,-0x1d(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0xb,-0x1c(%rbp)
jae    11c8 <FAT_filename_to_fat_name+0x68>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
movb   $0x20,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11a5 <FAT_filename_to_fat_name+0x45>
mov    -0x28(%rbp),%rdi
lea    0xe2d(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x8,%eax
jg     121b <FAT_filename_to_fat_name+0xbb>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1060 <memcpy@plt>
jmp    1239 <FAT_filename_to_fat_name+0xd9>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x7,%edx
call   1060 <memcpy@plt>
mov    -0x10(%rbp),%rax
movb   $0x7e,0x7(%rax)
movb   $0x1,-0x1d(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xdbc(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     12c6 <FAT_filename_to_fat_name+0x166>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x3,%eax
jg     129a <FAT_filename_to_fat_name+0x13a>
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
call   1060 <memcpy@plt>
jmp    12c1 <FAT_filename_to_fat_name+0x161>
mov    -0x18(%rbp),%rax
mov    (%rax),%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x8(%rax)
mov    -0x18(%rbp),%rax
mov    0x1(%rax),%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x9(%rax)
mov    -0x10(%rbp),%rax
movb   $0x7e,0xa(%rax)
movb   $0x1,-0x1d(%rbp)
jmp    12c6 <FAT_filename_to_fat_name+0x166>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movsbl -0x1d(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<FAT_filename_to_fat_name>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%rbp
call   1050 <strlen@plt>
lea    0x1(%rax),%edi
call   1070 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbp,%rsi
call   1040 <strcpy@plt>
movabs $0x2020202020202020,%rax
20 20 20
mov    %rax,(%rbx)
movl   $0x20202020,0x7(%rbx)
lea    0xe5d(%rip),%rsi
mov    %r14,%rdi
call   1080 <strtok@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1050 <strlen@plt>
cmp    $0x8,%eax
jg     11d5 <FAT_filename_to_fat_name+0x75>
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %eax,%edx
call   1060 <memcpy@plt>
xor    %r15d,%r15d
jmp    11ec <FAT_filename_to_fat_name+0x8c>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x7,%edx
call   1060 <memcpy@plt>
movb   $0x7e,0x7(%rbx)
mov    $0x1,%r15b
lea    0xe0d(%rip),%rsi
xor    %edi,%edi
call   1080 <strtok@plt>
test   %rax,%rax
je     123d <FAT_filename_to_fat_name+0xdd>
mov    %rax,%rbp
mov    %rax,%rdi
call   1050 <strlen@plt>
cmp    $0x3,%eax
jg     122a <FAT_filename_to_fat_name+0xca>
add    $0x8,%rbx
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %eax,%edx
call   1060 <memcpy@plt>
jmp    123d <FAT_filename_to_fat_name+0xdd>
mov    0x0(%rbp),%al
mov    %al,0x8(%rbx)
mov    0x1(%rbp),%al
mov    %al,0x9(%rbx)
movb   $0x7e,0xa(%rbx)
mov    $0x1,%r15b
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
define signext i8 @FAT_filename_to_fat_name(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8
%8 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
store i8 0, i8* %7
%9 = load i8*, i8** %3
%10 = call i32 @strlen(i8* %9)
%11 = add nsw i32 %10, 1
%12 = sext i32 %11 to i64
%13 = mul i64 1, %12
%14 = trunc i64 %13 to i32
%15 = call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %8
%17 = load i8*, i8** %8
%18 = load i8*, i8** %3
%19 = call i32 @strcpy(i8* %17, i8* %18)
store i32 0, i32* %6
br label %20
20: ; preds = %28, %2
%21 = load i32, i32* %6
%22 = icmp ult i32 %21, 11
br i1 %22, label %23, label %31
23: ; preds = %20
%24 = load i8*, i8** %4
%25 = load i32, i32* %6
%26 = zext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
store i8 32, i8* %27
br label %28
28: ; preds = %23
%29 = load i32, i32* %6
%30 = add i32 %29, 1
store i32 %30, i32* %6
br label %20
31: ; preds = %20
%32 = load i8*, i8** %8
%33 = call i8* @strtok(i8* %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %33, i8** %5
%34 = load i8*, i8** %5
%35 = call i32 @strlen(i8* %34)
%36 = icmp sle i32 %35, 8
br i1 %36, label %37, label %43
37: ; preds = %31
%38 = load i8*, i8** %4
%39 = load i8*, i8** %5
%40 = load i8*, i8** %5
%41 = call i32 @strlen(i8* %40)
%42 = call i32 @memcpy(i8* %38, i8* %39, i32 %41)
br label %49
43: ; preds = %31
%44 = load i8*, i8** %4
%45 = load i8*, i8** %5
%46 = call i32 @memcpy(i8* %44, i8* %45, i32 7)
%47 = load i8*, i8** %4
%48 = getelementptr inbounds i8, i8* %47, i64 7
store i8 126, i8* %48
store i8 1, i8* %7
br label %49
49: ; preds = %43, %37
%50 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %50, i8** %5
%51 = icmp ne i8* %50, null
br i1 %51, label %52, label %77
52: ; preds = %49
%53 = load i8*, i8** %5
%54 = call i32 @strlen(i8* %53)
%55 = icmp sle i32 %54, 3
br i1 %55, label %56, label %63
56: ; preds = %52
%57 = load i8*, i8** %4
%58 = getelementptr inbounds i8, i8* %57, i64 8
%59 = load i8*, i8** %5
%60 = load i8*, i8** %5
%61 = call i32 @strlen(i8* %60)
%62 = call i32 @memcpy(i8* %58, i8* %59, i32 %61)
br label %76
63: ; preds = %52
%64 = load i8*, i8** %5
%65 = getelementptr inbounds i8, i8* %64, i64 0
%66 = load i8, i8* %65
%67 = load i8*, i8** %4
%68 = getelementptr inbounds i8, i8* %67, i64 8
store i8 %66, i8* %68
%69 = load i8*, i8** %5
%70 = getelementptr inbounds i8, i8* %69, i64 1
%71 = load i8, i8* %70
%72 = load i8*, i8** %4
%73 = getelementptr inbounds i8, i8* %72, i64 9
store i8 %71, i8* %73
%74 = load i8*, i8** %4
%75 = getelementptr inbounds i8, i8* %74, i64 10
store i8 126, i8* %75
store i8 1, i8* %7
br label %76
76: ; preds = %63, %56
br label %77
77: ; preds = %76, %49
%78 = load i8*, i8** %8
%79 = call i32 @free(i8* %78)
%80 = load i8, i8* %7
ret i8 %80
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
define signext i8 @FAT_filename_to_fat_name(i8* %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = add nsw i32 %3, 1
%5 = tail call i64 @malloc(i32 %4)
%6 = inttoptr i64 %5 to i8*
%7 = tail call i32 @strcpy(i8* %6, i8* %0)
%8 = tail call i8* @strtok(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @strlen(i8* %8)
%10 = icmp slt i32 %9, 9
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = tail call i32 @strlen(i8* %8)
%13 = tail call i32 @memcpy(i8* %1, i8* %8, i32 %12)
br label %17
14: ; preds = %2
%15 = tail call i32 @memcpy(i8* %1, i8* %8, i32 7)
%16 = getelementptr inbounds i8, i8* %1, i64 7
store i8 126, i8* %16
br label %17
17: ; preds = %14, %11
%18 = phi i8 [ 0, %11 ], [ 1, %14 ]
%19 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%20 = icmp eq i8* %19, null
br i1 %20, label %35, label %21
21: ; preds = %17
%22 = tail call i32 @strlen(i8* %19)
%23 = icmp slt i32 %22, 4
br i1 %23, label %24, label %28
24: ; preds = %21
%25 = getelementptr inbounds i8, i8* %1, i64 8
%26 = tail call i32 @strlen(i8* %19)
%27 = tail call i32 @memcpy(i8* %25, i8* %19, i32 %26)
br label %35
28: ; preds = %21
%29 = load i8, i8* %19
%30 = getelementptr inbounds i8, i8* %1, i64 8
store i8 %29, i8* %30
%31 = getelementptr inbounds i8, i8* %19, i64 1
%32 = load i8, i8* %31
%33 = getelementptr inbounds i8, i8* %1, i64 9
store i8 %32, i8* %33
%34 = getelementptr inbounds i8, i8* %1, i64 10
store i8 126, i8* %34
br label %35
35: ; preds = %24, %28, %17
%36 = phi i8 [ %18, %24 ], [ 1, %28 ], [ %18, %17 ]
%37 = tail call i32 @free(i8* %6)
ret i8 %36
}