#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(int);
scalar_t__ isalnum(int);
int /*<<< orphan*/ isalpha(int);
int /*<<< orphan*/ printf(char *, char *, ...);
int strlen(char *);
int toupper(int);
void make_legal_prodos_name(char *new_name, char *old_name) {
  char *ptr;
  int start_len, start_char;
  int len;
  int pos;
  int c;
  int j;
  for (j = 0; j < 16; j++) {
    new_name[j] = 0;
  }
  start_char = 0;
  start_len = strlen(old_name);
  len = 0;
  ptr = &old_name[start_len - 1];
  for (j = start_len - 1; j >= 0; j--) {
    if (*ptr == '/' || *ptr == ':') {
      break;
    }
    ptr--;
    len++;
  }
  ptr++;
  if (len <= 0) {
    printf("Filename: %s has len: %d!\n", old_name, len);
    exit(1);
  }
  printf("mid_name: %s, len:%d\n", ptr, len);
  pos = 0;
  for (j = 0; j < 15; j++) {
    c = ptr[pos];
    if (isalnum(c)) {
      c = toupper(c);
    } else if (c != 0) {
      c = '.';
    }
    if (j == 0 && !isalpha(c)) {
      c = 'A';
    }
    new_name[j] = c;
    pos++;
    if (pos == 7 && len > 15) {
      pos = len - 8;
    }
  }
  printf("new_name: %s\n", new_name);
}


x86 assembly (clang, -O0):
<make_legal_prodos_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x30(%rbp)
cmpl   $0x10,-0x30(%rbp)
jge    119b <make_legal_prodos_name+0x3b>
mov    -0x8(%rbp),%rax
movslq -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1177 <make_legal_prodos_name+0x17>
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jl     1224 <make_legal_prodos_name+0xc4>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     11fc <make_legal_prodos_name+0x9c>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x3a,%eax
jne    1201 <make_legal_prodos_name+0xa1>
jmp    1224 <make_legal_prodos_name+0xc4>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x30(%rbp)
jmp    11d2 <make_legal_prodos_name+0x72>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x0,-0x24(%rbp)
jg     1259 <make_legal_prodos_name+0xf9>
mov    -0x10(%rbp),%rsi
mov    -0x24(%rbp),%edx
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%edx
lea    0xdb4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x30(%rbp)
cmpl   $0xf,-0x30(%rbp)
jge    1338 <make_legal_prodos_name+0x1d8>
mov    -0x18(%rbp),%rax
movslq -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   1060 <isalnum@plt>
cmp    $0x0,%rax
je     12b7 <make_legal_prodos_name+0x157>
mov    -0x2c(%rbp),%edi
call   1030 <toupper@plt>
mov    %eax,-0x2c(%rbp)
jmp    12cd <make_legal_prodos_name+0x16d>
cmpl   $0x0,-0x2c(%rbp)
je     12c8 <make_legal_prodos_name+0x168>
movl   $0x2e,-0x2c(%rbp)
jmp    12cd <make_legal_prodos_name+0x16d>
cmpl   $0x0,-0x30(%rbp)
jne    12ef <make_legal_prodos_name+0x18f>
mov    -0x2c(%rbp),%edi
call   1070 <isalpha@plt>
cmp    $0x0,%eax
jne    12ef <make_legal_prodos_name+0x18f>
movl   $0x41,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %al,%dl
mov    -0x8(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x7,-0x28(%rbp)
jne    1325 <make_legal_prodos_name+0x1c5>
cmpl   $0xf,-0x24(%rbp)
jle    1325 <make_legal_prodos_name+0x1c5>
mov    -0x24(%rbp),%eax
sub    $0x8,%eax
mov    %eax,-0x28(%rbp)
jmp    132a <make_legal_prodos_name+0x1ca>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    127c <make_legal_prodos_name+0x11c>
mov    -0x8(%rbp),%rsi
lea    0xcee(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_legal_prodos_name>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
xorps  %xmm0,%xmm0
mov    %rdi,(%rsp)
movups %xmm0,(%rdi)
mov    %rsi,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    12a2 <make_legal_prodos_name+0x142>
movslq %eax,%rcx
lea    (%rcx,%r12,1),%r15
add    $0xffffffffffffffff,%r15
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
movzbl (%r15),%ecx
cmp    $0x2f,%cl
je     11be <make_legal_prodos_name+0x5e>
cmp    $0x3a,%cl
je     11be <make_legal_prodos_name+0x5e>
add    $0xffffffffffffffff,%r15
add    $0x1,%r13d
cmp    %r13d,%eax
jne    11a0 <make_legal_prodos_name+0x40>
mov    %eax,%r13d
test   %r13d,%r13d
je     12a2 <make_legal_prodos_name+0x142>
add    $0x1,%r15
lea    0xe49(%rip),%rdi
xor    %ebx,%ebx
mov    %r15,%rsi
mov    %r13d,%edx
xor    %eax,%eax
call   1050 <printf@plt>
movsbl (%r15),%ebp
mov    %ebp,%edi
call   1060 <isalnum@plt>
test   %rax,%rax
je     11fc <make_legal_prodos_name+0x9c>
mov    %ebp,%edi
call   1030 <toupper@plt>
mov    %eax,%ebp
jmp    1207 <make_legal_prodos_name+0xa7>
test   %bpl,%bpl
mov    $0x2e,%ebp
cmove  %ebx,%ebp
lea    -0x8(%r13),%r12d
mov    %ebp,%edi
call   1070 <isalpha@plt>
test   %eax,%eax
mov    $0x41,%eax
cmovne %ebp,%eax
mov    (%rsp),%rcx
mov    %al,(%rcx)
mov    $0x1,%ebx
mov    $0x1,%r14d
jmp    1265 <make_legal_prodos_name+0x105>
nop
test   %bpl,%bpl
mov    $0x2e,%eax
mov    $0x0,%ecx
cmove  %ecx,%eax
add    $0x1,%ebx
cmp    $0x10,%r13d
mov    (%rsp),%rcx
mov    %al,(%rcx,%r14,1)
mov    %ebx,%eax
cmovae %r12d,%eax
cmp    $0x7,%ebx
cmove  %eax,%ebx
add    $0x1,%r14
cmp    $0xf,%r14
je     1282 <make_legal_prodos_name+0x122>
movslq %ebx,%rax
movsbl (%r15,%rax,1),%ebp
mov    %ebp,%edi
call   1060 <isalnum@plt>
test   %rax,%rax
je     1230 <make_legal_prodos_name+0xd0>
mov    %ebp,%edi
call   1030 <toupper@plt>
jmp    1240 <make_legal_prodos_name+0xe0>
lea    0xda8(%rip),%rdi
mov    (%rsp),%rsi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <printf@plt>
lea    0xd57(%rip),%rdi
mov    %r12,%rsi
xor    %edx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [27 x i8] c"Filename: %s has len: %d!\0A\00"
@.str.1 = [22 x i8] c"mid_name: %s, len:%d\0A\00"
@.str.2 = [14 x i8] c"new_name: %s\0A\00"
define void @make_legal_prodos_name(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %11
br label %12
12: ; preds = %20, %2
%13 = load i32, i32* %11
%14 = icmp slt i32 %13, 16
br i1 %14, label %15, label %23
15: ; preds = %12
%16 = load i8*, i8** %3
%17 = load i32, i32* %11
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
store i8 0, i8* %19
br label %20
20: ; preds = %15
%21 = load i32, i32* %11
%22 = add nsw i32 %21, 1
store i32 %22, i32* %11
br label %12
23: ; preds = %12
store i32 0, i32* %7
%24 = load i8*, i8** %4
%25 = call i32 @strlen(i8* %24)
store i32 %25, i32* %6
store i32 0, i32* %8
%26 = load i8*, i8** %4
%27 = load i32, i32* %6
%28 = sub nsw i32 %27, 1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %26, i64 %29
store i8* %30, i8** %5
%31 = load i32, i32* %6
%32 = sub nsw i32 %31, 1
store i32 %32, i32* %11
br label %33
33: ; preds = %52, %23
%34 = load i32, i32* %11
%35 = icmp sge i32 %34, 0
br i1 %35, label %36, label %55
36: ; preds = %33
%37 = load i8*, i8** %5
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 47
br i1 %40, label %46, label %41
41: ; preds = %36
%42 = load i8*, i8** %5
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp eq i32 %44, 58
br i1 %45, label %46, label %47
46: ; preds = %41, %36
br label %55
47: ; preds = %41
%48 = load i8*, i8** %5
%49 = getelementptr inbounds i8, i8* %48, i32 -1
store i8* %49, i8** %5
%50 = load i32, i32* %8
%51 = add nsw i32 %50, 1
store i32 %51, i32* %8
br label %52
52: ; preds = %47
%53 = load i32, i32* %11
%54 = add nsw i32 %53, -1
store i32 %54, i32* %11
br label %33
55: ; preds = %46, %33
%56 = load i8*, i8** %5
%57 = getelementptr inbounds i8, i8* %56, i32 1
store i8* %57, i8** %5
%58 = load i32, i32* %8
%59 = icmp sle i32 %58, 0
br i1 %59, label %60, label %65
60: ; preds = %55
%61 = load i8*, i8** %4
%62 = load i32, i32* %8
%63 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %61, i32 %62)
%64 = call i32 @exit(i32 1)
unreachable
65: ; preds = %55
%66 = load i8*, i8** %5
%67 = load i32, i32* %8
%68 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %66, i32 %67)
store i32 0, i32* %9
store i32 0, i32* %11
br label %69
69: ; preds = %116, %65
%70 = load i32, i32* %11
%71 = icmp slt i32 %70, 15
br i1 %71, label %72, label %119
72: ; preds = %69
%73 = load i8*, i8** %5
%74 = load i32, i32* %9
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i8, i8* %73, i64 %75
%77 = load i8, i8* %76
%78 = sext i8 %77 to i32
store i32 %78, i32* %10
%79 = load i32, i32* %10
%80 = call i64 @isalnum(i32 %79)
%81 = icmp ne i64 %80, 0
br i1 %81, label %82, label %85
82: ; preds = %72
%83 = load i32, i32* %10
%84 = call i32 @toupper(i32 %83)
store i32 %84, i32* %10
br label %90
85: ; preds = %72
%86 = load i32, i32* %10
%87 = icmp ne i32 %86, 0
br i1 %87, label %88, label %89
88: ; preds = %85
store i32 46, i32* %10
br label %89
89: ; preds = %88, %85
br label %90
90: ; preds = %89, %82
%91 = load i32, i32* %11
%92 = icmp eq i32 %91, 0
br i1 %92, label %93, label %98
93: ; preds = %90
%94 = load i32, i32* %10
%95 = call i32 @isalpha(i32 %94)
%96 = icmp ne i32 %95, 0
br i1 %96, label %98, label %97
97: ; preds = %93
store i32 65, i32* %10
br label %98
98: ; preds = %97, %93, %90
%99 = load i32, i32* %10
%100 = trunc i32 %99 to i8
%101 = load i8*, i8** %3
%102 = load i32, i32* %11
%103 = sext i32 %102 to i64
%104 = getelementptr inbounds i8, i8* %101, i64 %103
store i8 %100, i8* %104
%105 = load i32, i32* %9
%106 = add nsw i32 %105, 1
store i32 %106, i32* %9
%107 = load i32, i32* %9
%108 = icmp eq i32 %107, 7
br i1 %108, label %109, label %115
109: ; preds = %98
%110 = load i32, i32* %8
%111 = icmp sgt i32 %110, 15
br i1 %111, label %112, label %115
112: ; preds = %109
%113 = load i32, i32* %8
%114 = sub nsw i32 %113, 8
store i32 %114, i32* %9
br label %115
115: ; preds = %112, %109, %98
br label %116
116: ; preds = %115
%117 = load i32, i32* %11
%118 = add nsw i32 %117, 1
store i32 %118, i32* %11
br label %69
119: ; preds = %69
%120 = load i8*, i8** %3
%121 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %120)
ret void
}

LLVM IR (-O2):
@.str = [27 x i8] c"Filename: %s has len: %d!\0A\00"
@.str.1 = [22 x i8] c"mid_name: %s, len:%d\0A\00"
@.str.2 = [14 x i8] c"new_name: %s\0A\00"
define void @make_legal_prodos_name(i8* %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = icmp sgt i32 %3, 0
br i1 %4, label %5, label %22
5: ; preds = %2
%6 = add nsw i32 %3, -1
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds i8, i8* %1, i64 %7
br label %9
9: ; preds = %5, %13
%10 = phi i32 [ %15, %13 ], [ 0, %5 ]
%11 = phi i8* [ %14, %13 ], [ %8, %5 ]
%12 = load i8, i8* %11
switch i8 %12, label %13 [
i8 47, label %17
i8 58, label %17
]
13: ; preds = %9
%14 = getelementptr inbounds i8, i8* %11, i64 -1
%15 = add nuw i32 %10, 1
%16 = icmp eq i32 %15, %3
br i1 %16, label %17, label %9
17: ; preds = %13, %9, %9
%18 = phi i8* [ %14, %13 ], [ %11, %9 ], [ %11, %9 ]
%19 = phi i32 [ %3, %13 ], [ %10, %9 ], [ %10, %9 ]
%20 = getelementptr inbounds i8, i8* %18, i64 1
%21 = icmp eq i32 %19, 0
br i1 %21, label %22, label %25
22: ; preds = %2, %17
%23 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %1, i32 0)
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %17
%26 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %20, i32 %19)
%27 = icmp ugt i32 %19, 15
%28 = add nsw i32 %19, -8
%29 = load i8, i8* %20
%30 = sext i8 %29 to i32
%31 = tail call i64 @isalnum(i32 %30)
%32 = icmp eq i64 %31, 0
br i1 %32, label %35, label %33
33: ; preds = %25
%34 = tail call i32 @toupper(i32 %30)
br label %38
35: ; preds = %25
%36 = icmp eq i8 %29, 0
%37 = select i1 %36, i32 0, i32 46
br label %38
38: ; preds = %33, %35
%39 = phi i32 [ %34, %33 ], [ %37, %35 ]
%40 = tail call i32 @isalpha(i32 %39)
%41 = icmp eq i32 %40, 0
%42 = trunc i32 %39 to i8
%43 = select i1 %41, i8 65, i8 %42
store i8 %43, i8* %0
br label %44
44: ; preds = %38, %58
%45 = phi i64 [ 1, %38 ], [ %66, %58 ]
%46 = phi i32 [ 1, %38 ], [ %65, %58 ]
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %20, i64 %47
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
%51 = tail call i64 @isalnum(i32 %50)
%52 = icmp eq i64 %51, 0
br i1 %52, label %55, label %53
53: ; preds = %44
%54 = tail call i32 @toupper(i32 %50)
br label %58
55: ; preds = %44
%56 = icmp eq i8 %49, 0
%57 = select i1 %56, i32 0, i32 46
br label %58
58: ; preds = %53, %55
%59 = phi i32 [ %54, %53 ], [ %57, %55 ]
%60 = trunc i32 %59 to i8
%61 = getelementptr inbounds i8, i8* %0, i64 %45
store i8 %60, i8* %61
%62 = add nsw i32 %46, 1
%63 = icmp eq i32 %62, 7
%64 = select i1 %63, i1 %27, i1 false
%65 = select i1 %64, i32 %28, i32 %62
%66 = add nuw nsw i64 %45, 1
%67 = icmp eq i64 %66, 15
br i1 %67, label %68, label %44
68: ; preds = %58
%69 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %0)
ret void
}