#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int, char *, int);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ strcat(char *, char *);
char *strchr(char const *, char);
char *strdup(char const *);
int strlen(char const *);
int /*<<< orphan*/ strncat(char *, char const *, int);
void midnam_print_pgm_cb(int num, int pc, const char *name, void *arg) {
  FILE *fp = (FILE *)arg;
  if (strlen(name) == 0) {
    return;
  }
  int ent = 0;
  char *escaped;
  const char *tmp = name;
  while ((tmp = strchr(tmp, '&'))) {
    tmp++;
    ent++;
  }
  if (ent == 0) {
    escaped = strdup(name);
  } else {
    const char *t1, *t2;
    escaped = (char *)malloc((strlen(name) + ent * 4 + 1) * sizeof(char));
    escaped[0] = '\0';
    t1 = name;
    while ((t2 = strchr(t1, '&'))) {
      strncat(escaped, t1, t2 - t1);
      strcat(escaped, "&amp;");
      t1 = t2 + 1;
    }
    strncat(escaped, t1, strlen(name) - (t1 - name));
  }
  fprintf(fp,
          "      <Patch Number=\"%03d\" Name=\"%s\" ProgramChange=\"%d\"/>\n",
          num - 1, escaped, pc - 1);
  free(escaped);
}


x86 assembly (clang, -O0):
<midnam_print_pgm_cb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%eax
jne    11b5 <midnam_print_pgm_cb+0x35>
jmp    1304 <midnam_print_pgm_cb+0x184>
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    $0x26,%esi
call   1050 <strchr@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     11fa <midnam_print_pgm_cb+0x7a>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11c4 <midnam_print_pgm_cb+0x44>
cmpl   $0x0,-0x24(%rbp)
jne    1216 <midnam_print_pgm_cb+0x96>
mov    -0x10(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x30(%rbp)
jmp    12d9 <midnam_print_pgm_cb+0x159>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x24(%rbp),%ecx
shl    $0x2,%ecx
add    %ecx,%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    $0x26,%esi
call   1050 <strchr@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
je     12a1 <midnam_print_pgm_cb+0x121>
mov    -0x30(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x48(%rbp),%rax
mov    -0x40(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1060 <strncat@plt>
mov    -0x30(%rbp),%rdi
lea    0xd75(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
jmp    124a <midnam_print_pgm_cb+0xca>
mov    -0x30(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
cltq
mov    -0x40(%rbp),%rcx
mov    -0x10(%rbp),%rdx
sub    %rdx,%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1060 <strncat@plt>
mov    -0x20(%rbp),%rdi
mov    -0x4(%rbp),%edx
sub    $0x1,%edx
mov    -0x30(%rbp),%rcx
mov    -0x8(%rbp),%r8d
sub    $0x1,%r8d
lea    0xd10(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<midnam_print_pgm_cb>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r14
mov    %rdx,%r13
mov    %esi,%ebx
mov    %edi,%r12d
mov    %rdx,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
je     128e <midnam_print_pgm_cb+0x10e>
mov    %r13,%rdi
mov    $0x26,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     129d <midnam_print_pgm_cb+0x11d>
mov    %r14,0x10(%rsp)
mov    %ebx,0xc(%rsp)
mov    $0x1,%ebx
nopl   (%rax)
add    $0x1,%rax
mov    %rax,%rdi
mov    $0x26,%esi
call   1050 <strchr@plt>
add    $0x4,%ebx
test   %rax,%rax
jne    11d0 <midnam_print_pgm_cb+0x50>
mov    %r13,%rdi
call   1040 <strlen@plt>
add    %eax,%ebx
mov    %ebx,%edi
call   1080 <malloc@plt>
mov    %rax,%rbp
movb   $0x0,(%rax)
mov    %r13,%rdi
mov    $0x26,%esi
call   1050 <strchr@plt>
mov    %r13,%rbx
test   %rax,%rax
je     1267 <midnam_print_pgm_cb+0xe7>
mov    %rax,%r15
lea    0xde1(%rip),%r14
mov    %r13,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15d,%edx
sub    %ebx,%edx
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1060 <strncat@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
call   1090 <strcat@plt>
mov    %r15,%rbx
add    $0x1,%rbx
mov    %rbx,%rdi
mov    $0x26,%esi
call   1050 <strchr@plt>
mov    %rax,%r15
test   %rax,%rax
jne    1230 <midnam_print_pgm_cb+0xb0>
mov    %r13,%rdi
call   1040 <strlen@plt>
sub    %ebx,%r13d
add    %eax,%r13d
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r13d,%edx
call   1060 <strncat@plt>
mov    0xc(%rsp),%ebx
mov    0x10(%rsp),%r14
jmp    12a8 <midnam_print_pgm_cb+0x128>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r13,%rdi
call   10a0 <strdup@plt>
mov    %rax,%rbp
add    $0xffffffff,%r12d
add    $0xffffffff,%ebx
lea    0xd50(%rip),%rsi
mov    %r14,%rdi
mov    %r12d,%edx
mov    %rbp,%rcx
mov    %ebx,%r8d
call   1070 <fprintf@plt>
mov    %rbp,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"&amp;\00"
@.str.1 = [59 x i8] c" <Patch Number=\22%03d\22 Name=\22%s\22 ProgramChange=\22%d\22/>\0A\00"
define void @midnam_print_pgm_cb(i32 %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i8*
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i8* %3, i8** %8
%15 = load i8*, i8** %8
%16 = bitcast i8* %15 to i32*
store i32* %16, i32** %9
%17 = load i8*, i8** %7
%18 = call i32 @strlen(i8* %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %4
br label %95
21: ; preds = %4
store i32 0, i32* %10
%22 = load i8*, i8** %7
store i8* %22, i8** %12
br label %23
23: ; preds = %27, %21
%24 = load i8*, i8** %12
%25 = call i8* @strchr(i8* %24, i8 signext 38)
store i8* %25, i8** %12
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %32
27: ; preds = %23
%28 = load i8*, i8** %12
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %12
%30 = load i32, i32* %10
%31 = add nsw i32 %30, 1
store i32 %31, i32* %10
br label %23
32: ; preds = %23
%33 = load i32, i32* %10
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = load i8*, i8** %7
%37 = call i8* @strdup(i8* %36)
store i8* %37, i8** %11
br label %85
38: ; preds = %32
%39 = load i8*, i8** %7
%40 = call i32 @strlen(i8* %39)
%41 = load i32, i32* %10
%42 = mul nsw i32 %41, 4
%43 = add nsw i32 %40, %42
%44 = add nsw i32 %43, 1
%45 = sext i32 %44 to i64
%46 = mul i64 %45, 1
%47 = trunc i64 %46 to i32
%48 = call i64 @malloc(i32 %47)
%49 = inttoptr i64 %48 to i8*
store i8* %49, i8** %11
%50 = load i8*, i8** %11
%51 = getelementptr inbounds i8, i8* %50, i64 0
store i8 0, i8* %51
%52 = load i8*, i8** %7
store i8* %52, i8** %13
br label %53
53: ; preds = %57, %38
%54 = load i8*, i8** %13
%55 = call i8* @strchr(i8* %54, i8 signext 38)
store i8* %55, i8** %14
%56 = icmp ne i8* %55, null
br i1 %56, label %57, label %71
57: ; preds = %53
%58 = load i8*, i8** %11
%59 = load i8*, i8** %13
%60 = load i8*, i8** %14
%61 = load i8*, i8** %13
%62 = ptrtoint i8* %60 to i64
%63 = ptrtoint i8* %61 to i64
%64 = sub i64 %62, %63
%65 = trunc i64 %64 to i32
%66 = call i32 @strncat(i8* %58, i8* %59, i32 %65)
%67 = load i8*, i8** %11
%68 = call i32 @strcat(i8* %67, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%69 = load i8*, i8** %14
%70 = getelementptr inbounds i8, i8* %69, i64 1
store i8* %70, i8** %13
br label %53
71: ; preds = %53
%72 = load i8*, i8** %11
%73 = load i8*, i8** %13
%74 = load i8*, i8** %7
%75 = call i32 @strlen(i8* %74)
%76 = sext i32 %75 to i64
%77 = load i8*, i8** %13
%78 = load i8*, i8** %7
%79 = ptrtoint i8* %77 to i64
%80 = ptrtoint i8* %78 to i64
%81 = sub i64 %79, %80
%82 = sub nsw i64 %76, %81
%83 = trunc i64 %82 to i32
%84 = call i32 @strncat(i8* %72, i8* %73, i32 %83)
br label %85
85: ; preds = %71, %35
%86 = load i32*, i32** %9
%87 = load i32, i32* %5
%88 = sub nsw i32 %87, 1
%89 = load i8*, i8** %11
%90 = load i32, i32* %6
%91 = sub nsw i32 %90, 1
%92 = call i32 @fprintf(i32* %86, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.1, i64 0, i64 0), i32 %88, i8* %89, i32 %91)
%93 = load i8*, i8** %11
%94 = call i32 @free(i8* %93)
br label %95
95: ; preds = %85, %20
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"&amp;\00"
@.str.1 = [59 x i8] c" <Patch Number=\22%03d\22 Name=\22%s\22 ProgramChange=\22%d\22/>\0A\00"
define void @midnam_print_pgm_cb(i32 %0, i32 %1, i8* %2, i8* nocapture %3) {
%5 = bitcast i8* %3 to i32*
%6 = tail call i32 @strlen(i8* %2)
%7 = icmp eq i32 %6, 0
br i1 %7, label %56, label %8
8: ; preds = %4
%9 = tail call i8* @strchr(i8* dereferenceable(1) %2, i8 signext 38)
%10 = icmp eq i8* %9, null
br i1 %10, label %18, label %11
11: ; preds = %8, %11
%12 = phi i8* [ %16, %11 ], [ %9, %8 ]
%13 = phi i32 [ %15, %11 ], [ 0, %8 ]
%14 = getelementptr inbounds i8, i8* %12, i64 1
%15 = add nuw nsw i32 %13, 1
%16 = tail call i8* @strchr(i8* dereferenceable(1) %14, i8 signext 38)
%17 = icmp eq i8* %16, null
br i1 %17, label %20, label %11
18: ; preds = %8
%19 = tail call i8* @strdup(i8* %2)
br label %50
20: ; preds = %11
%21 = tail call i32 @strlen(i8* %2)
%22 = shl nsw i32 %15, 2
%23 = or i32 %22, 1
%24 = add i32 %23, %21
%25 = tail call i64 @malloc(i32 %24)
%26 = inttoptr i64 %25 to i8*
store i8 0, i8* %26
%27 = tail call i8* @strchr(i8* dereferenceable(1) %2, i8 signext 38)
%28 = icmp eq i8* %27, null
br i1 %28, label %41, label %29
29: ; preds = %20, %29
%30 = phi i8* [ %39, %29 ], [ %27, %20 ]
%31 = phi i8* [ %38, %29 ], [ %2, %20 ]
%32 = ptrtoint i8* %30 to i64
%33 = ptrtoint i8* %31 to i64
%34 = sub i64 %32, %33
%35 = trunc i64 %34 to i32
%36 = tail call i32 @strncat(i8* %26, i8* %31, i32 %35)
%37 = tail call i32 @strcat(i8* %26, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%38 = getelementptr inbounds i8, i8* %30, i64 1
%39 = tail call i8* @strchr(i8* dereferenceable(1) %38, i8 signext 38)
%40 = icmp eq i8* %39, null
br i1 %40, label %41, label %29
41: ; preds = %29, %20
%42 = phi i8* [ %2, %20 ], [ %38, %29 ]
%43 = tail call i32 @strlen(i8* %2)
%44 = ptrtoint i8* %42 to i64
%45 = ptrtoint i8* %2 to i64
%46 = sub i64 %45, %44
%47 = trunc i64 %46 to i32
%48 = add i32 %43, %47
%49 = tail call i32 @strncat(i8* %26, i8* %42, i32 %48)
br label %50
50: ; preds = %41, %18
%51 = phi i8* [ %19, %18 ], [ %26, %41 ]
%52 = add nsw i32 %0, -1
%53 = add nsw i32 %1, -1
%54 = tail call i32 @fprintf(i32* %5, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.1, i64 0, i64 0), i32 %52, i8* %51, i32 %53)
%55 = tail call i32 @free(i8* %51)
br label %56
56: ; preds = %4, %50
ret void
}