#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*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ free(unsigned char *);
unsigned char *load_sysex(char *);
int /*<<< orphan*/ save_sysex(unsigned char *, char *);
int /*<<< orphan*/ stderr;
int strlen(char *);
int do_rename(char *inputfilename, char *outputfilename, char *newname) {
  unsigned char *data;
  int i, j;
  data = load_sysex(inputfilename);
  if (data != NULL) {
    if (strlen(newname) > 12) {
      fputs("do_rename: new name must be shorter than 12 characters\n", stderr);
      return 0;
    }
    i = 0;
    for (j = 0; j < 14; j++) {
      if (j % 8 == 0)
        continue;
      if (i < strlen(newname)) {
        if (newname[i] & 0x80) {
          fputs("do_rename: illegal char in new name\n", stderr);
          return 0;
        } else
          data[5 + j] = newname[i++];
      } else
        data[5 + j] = ' ';
    }
    if (!save_sysex(data, outputfilename)) {
      free(data);
      return 0;
    } else {
      free(data);
      return 1;
    }
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<do_rename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <load_sysex@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12c5 <do_rename+0x175>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0xc,%eax
jle    11af <do_rename+0x5f>
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
lea    0xe62(%rip),%rdi
call   1060 <fputs@plt>
movl   $0x0,-0x4(%rbp)
jmp    12cc <do_rename+0x17c>
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
cmpl   $0xe,-0x30(%rbp)
jge    1285 <do_rename+0x135>
mov    -0x30(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    11e0 <do_rename+0x90>
jmp    1277 <do_rename+0x127>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
cmp    %ecx,%eax
jge    1261 <do_rename+0x111>
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
and    $0x80,%eax
cmp    $0x0,%eax
je     1237 <do_rename+0xe7>
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
lea    0xe12(%rip),%rdi
call   1060 <fputs@plt>
movl   $0x0,-0x4(%rbp)
jmp    12cc <do_rename+0x17c>
mov    -0x20(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x2c(%rbp)
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%ecx
add    $0x5,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1272 <do_rename+0x122>
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%ecx
add    $0x5,%ecx
movslq %ecx,%rcx
movb   $0x20,(%rax,%rcx,1)
jmp    1277 <do_rename+0x127>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11bd <do_rename+0x6d>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <save_sysex@plt>
cmp    $0x0,%eax
jne    12b0 <do_rename+0x160>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    12cc <do_rename+0x17c>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    12cc <do_rename+0x17c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_rename>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r12
mov    %rsi,%r14
call   1040 <load_sysex@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     1208 <do_rename+0xb8>
mov    %rax,%r15
mov    %r12,%rdi
call   1050 <strlen@plt>
cmp    $0xc,%eax
jle    1195 <do_rename+0x45>
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
lea    0xe72(%rip),%rdi
call   1060 <fputs@plt>
jmp    1208 <do_rename+0xb8>
xor    %ebp,%ebp
xor    %ebx,%ebx
jmp    11af <do_rename+0x5f>
nopl   0x0(%rax,%rax,1)
mov    %cl,0x5(%r15,%rbp,1)
add    $0x1,%rbp
cmp    $0xe,%rbp
je     11d4 <do_rename+0x84>
test   $0x7,%bpl
je     11a5 <do_rename+0x55>
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    $0x20,%cl
cmp    %eax,%ebx
jge    11a0 <do_rename+0x50>
movslq %ebx,%rax
movzbl (%r12,%rax,1),%ecx
test   %cl,%cl
js     11f1 <do_rename+0xa1>
add    $0x1,%ebx
jmp    11a0 <do_rename+0x50>
mov    %r15,%rdi
mov    %r14,%rsi
call   1070 <save_sysex@plt>
xor    %ebp,%ebp
test   %eax,%eax
setne  %bpl
mov    %r15,%rdi
call   1030 <free@plt>
jmp    1208 <do_rename+0xb8>
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
lea    0xe37(%rip),%rdi
call   1060 <fputs@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [56 x i8] c"do_rename: new name must be shorter than 12 characters\0A\00"
@stderr = global i32 0
@.str.1 = [37 x i8] c"do_rename: illegal char in new name\0A\00"
define i32 @do_rename(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%11 = load i8*, i8** %5
%12 = call i8* @load_sysex(i8* %11)
store i8* %12, i8** %8
%13 = load i8*, i8** %8
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %82
15: ; preds = %3
%16 = load i8*, i8** %7
%17 = call i32 @strlen(i8* %16)
%18 = icmp sgt i32 %17, 12
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* @stderr
%21 = call i32 @fputs(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i32 %20)
store i32 0, i32* %4
br label %83
22: ; preds = %15
store i32 0, i32* %9
store i32 0, i32* %10
br label %23
23: ; preds = %68, %22
%24 = load i32, i32* %10
%25 = icmp slt i32 %24, 14
br i1 %25, label %26, label %71
26: ; preds = %23
%27 = load i32, i32* %10
%28 = srem i32 %27, 8
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %26
br label %68
31: ; preds = %26
%32 = load i32, i32* %9
%33 = load i8*, i8** %7
%34 = call i32 @strlen(i8* %33)
%35 = icmp slt i32 %32, %34
br i1 %35, label %36, label %61
36: ; preds = %31
%37 = load i8*, i8** %7
%38 = load i32, i32* %9
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = and i32 %42, 128
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %36
%46 = load i32, i32* @stderr
%47 = call i32 @fputs(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %46)
store i32 0, i32* %4
br label %83
48: ; preds = %36
%49 = load i8*, i8** %7
%50 = load i32, i32* %9
%51 = add nsw i32 %50, 1
store i32 %51, i32* %9
%52 = sext i32 %50 to i64
%53 = getelementptr inbounds i8, i8* %49, i64 %52
%54 = load i8, i8* %53
%55 = load i8*, i8** %8
%56 = load i32, i32* %10
%57 = add nsw i32 5, %56
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %55, i64 %58
store i8 %54, i8* %59
br label %60
60: ; preds = %48
br label %67
61: ; preds = %31
%62 = load i8*, i8** %8
%63 = load i32, i32* %10
%64 = add nsw i32 5, %63
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8, i8* %62, i64 %65
store i8 32, i8* %66
br label %67
67: ; preds = %61, %60
br label %68
68: ; preds = %67, %30
%69 = load i32, i32* %10
%70 = add nsw i32 %69, 1
store i32 %70, i32* %10
br label %23
71: ; preds = %23
%72 = load i8*, i8** %8
%73 = load i8*, i8** %6
%74 = call i32 @save_sysex(i8* %72, i8* %73)
%75 = icmp ne i32 %74, 0
br i1 %75, label %79, label %76
76: ; preds = %71
%77 = load i8*, i8** %8
%78 = call i32 @free(i8* %77)
store i32 0, i32* %4
br label %83
79: ; preds = %71
%80 = load i8*, i8** %8
%81 = call i32 @free(i8* %80)
store i32 1, i32* %4
br label %83
82: ; preds = %3
store i32 0, i32* %4
br label %83
83: ; preds = %82, %79, %76, %45, %19
%84 = load i32, i32* %4
ret i32 %84
}

LLVM IR (-O2):
@.str = [56 x i8] c"do_rename: new name must be shorter than 12 characters\0A\00"
@stderr = global i32 0
@.str.1 = [37 x i8] c"do_rename: illegal char in new name\0A\00"
define i32 @do_rename(i8* %0, i8* %1, i8* %2) {
%4 = tail call i8* @load_sysex(i8* %0)
%5 = icmp eq i8* %4, null
br i1 %5, label %44, label %6
6: ; preds = %3
%7 = tail call i32 @strlen(i8* %2)
%8 = icmp sgt i32 %7, 12
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = load i32, i32* @stderr
%11 = tail call i32 @fputs(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i32 %10)
br label %44
12: ; preds = %6, %35
%13 = phi i64 [ %37, %35 ], [ 0, %6 ]
%14 = phi i32 [ %36, %35 ], [ 0, %6 ]
%15 = and i64 %13, 7
%16 = icmp eq i64 %15, 0
br i1 %16, label %35, label %17
17: ; preds = %12
%18 = tail call i32 @strlen(i8* %2)
%19 = icmp slt i32 %14, %18
br i1 %19, label %20, label %30
20: ; preds = %17
%21 = sext i32 %14 to i64
%22 = getelementptr inbounds i8, i8* %2, i64 %21
%23 = load i8, i8* %22
%24 = icmp sgt i8 %23, -1
br i1 %24, label %28, label %25
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = tail call i32 @fputs(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %26)
br label %44
28: ; preds = %20
%29 = add nsw i32 %14, 1
br label %30
30: ; preds = %17, %28
%31 = phi i8 [ %23, %28 ], [ 32, %17 ]
%32 = phi i32 [ %29, %28 ], [ %14, %17 ]
%33 = add nuw nsw i64 %13, 5
%34 = getelementptr inbounds i8, i8* %4, i64 %33
store i8 %31, i8* %34
br label %35
35: ; preds = %30, %12
%36 = phi i32 [ %14, %12 ], [ %32, %30 ]
%37 = add nuw nsw i64 %13, 1
%38 = icmp eq i64 %37, 14
br i1 %38, label %39, label %12
39: ; preds = %35
%40 = tail call i32 @save_sysex(i8* %4, i8* %1)
%41 = icmp ne i32 %40, 0
%42 = tail call i32 @free(i8* %4)
%43 = zext i1 %41 to i32
br label %44
44: ; preds = %39, %3, %25, %9
%45 = phi i32 [ 0, %9 ], [ 0, %25 ], [ 0, %3 ], [ %43, %39 ]
ret i32 %45
}