#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*/ memcpy(char *, char *, int);
char *skip(char *, char);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
scalar_t__ strtol(char *, int /*<<< orphan*/ *, int);
void replace_xml_escapes(char *buf) {
  char *start = buf;
  char *ptr;
  char new[strlen(buf) + 1];
  char esc[2];
  new[0] = '\0';
  while ((ptr = skip(start, '&')) && *ptr != '\0') {
    strcat(new, start);
    start = skip(ptr, ';');
    esc[0] = (char)strtol(++ptr, NULL, 10);
    esc[1] = '\0';
    strcat(new, esc);
  }
  strcat(new, start);
  memcpy(buf, new, strlen(new) + 1);
}


x86 assembly (clang, -O0):
<replace_xml_escapes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %eax,%ecx
mov    %rsp,%rax
mov    %rax,-0x20(%rbp)
mov    %rcx,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rax
sub    %rdx,%rax
mov    %rax,-0x38(%rbp)
mov    %rax,%rsp
mov    %rcx,-0x28(%rbp)
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    $0x26,%esi
call   1030 <skip@plt>
mov    %rax,%rcx
mov    %rcx,-0x18(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x39(%rbp)
je     11cc <replace_xml_escapes+0x7c>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    11dc <replace_xml_escapes+0x8c>
jmp    122e <replace_xml_escapes+0xde>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strcat@plt>
mov    -0x18(%rbp),%rdi
mov    $0x3b,%esi
call   1030 <skip@plt>
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
mov    %rdi,-0x18(%rbp)
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    -0x38(%rbp),%rdi
mov    %al,-0x2a(%rbp)
movb   $0x0,-0x29(%rbp)
lea    -0x2a(%rbp),%rsi
call   1070 <strcat@plt>
jmp    1198 <replace_xml_escapes+0x48>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strcat@plt>
mov    -0x38(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
call   1040 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
call   1060 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_xml_escapes>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %rsp,%rcx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
mov    %rcx,%r12
sub    %rax,%r12
mov    %r12,%rsp
neg    %rax
movb   $0x0,(%rcx,%rax,1)
mov    %r14,%rdi
mov    $0x26,%esi
call   1030 <skip@plt>
mov    %r14,%r13
test   %rax,%rax
je     120a <replace_xml_escapes+0xba>
mov    %rax,%rbx
lea    -0x2a(%rbp),%r15
mov    %r14,%r13
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmpb   $0x0,(%rbx)
je     120a <replace_xml_escapes+0xba>
mov    %r12,%rdi
mov    %r13,%rsi
call   1070 <strcat@plt>
mov    %rbx,%rdi
mov    $0x3b,%esi
call   1030 <skip@plt>
mov    %rax,%r13
add    $0x1,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %al,-0x2a(%rbp)
movb   $0x0,-0x29(%rbp)
mov    %r12,%rdi
mov    %r15,%rsi
call   1070 <strcat@plt>
mov    %r13,%rdi
mov    $0x26,%esi
call   1030 <skip@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11b0 <replace_xml_escapes+0x60>
mov    %r12,%rdi
mov    %r13,%rsi
call   1070 <strcat@plt>
mov    %r12,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%edx
mov    %r14,%rdi
mov    %r12,%rsi
call   1060 <memcpy@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @replace_xml_escapes(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca [2 x i8]
store i8* %0, i8** %2
%8 = load i8*, i8** %2
store i8* %8, i8** %3
%9 = load i8*, i8** %2
%10 = call i32 @strlen(i8* %9)
%11 = add nsw i32 %10, 1
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %5
%14 = alloca i8, i64 %12
store i64 %12, i64* %6
%15 = getelementptr inbounds i8, i8* %14, i64 0
store i8 0, i8* %15
br label %16
16: ; preds = %27, %1
%17 = load i8*, i8** %3
%18 = call i8* @skip(i8* %17, i8 signext 38)
store i8* %18, i8** %4
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %25
20: ; preds = %16
%21 = load i8*, i8** %4
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp ne i32 %23, 0
br label %25
25: ; preds = %20, %16
%26 = phi i1 [ false, %16 ], [ %24, %20 ]
br i1 %26, label %27, label %40
27: ; preds = %25
%28 = load i8*, i8** %3
%29 = call i32 @strcat(i8* %14, i8* %28)
%30 = load i8*, i8** %4
%31 = call i8* @skip(i8* %30, i8 signext 59)
store i8* %31, i8** %3
%32 = load i8*, i8** %4
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %4
%34 = call i64 @strtol(i8* %33, i32* null, i32 10)
%35 = trunc i64 %34 to i8
%36 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 0
store i8 %35, i8* %36
%37 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 1
store i8 0, i8* %37
%38 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 0
%39 = call i32 @strcat(i8* %14, i8* %38)
br label %16
40: ; preds = %25
%41 = load i8*, i8** %3
%42 = call i32 @strcat(i8* %14, i8* %41)
%43 = load i8*, i8** %2
%44 = call i32 @strlen(i8* %14)
%45 = add nsw i32 %44, 1
%46 = call i32 @memcpy(i8* %43, i8* %14, i32 %45)
%47 = load i8*, i8** %5
ret void
}

LLVM IR (-O2):
define void @replace_xml_escapes(i8* %0) {
%2 = alloca [2 x i8]
%3 = tail call i32 @strlen(i8* %0)
%4 = add nsw i32 %3, 1
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 0
store i8 0, i8* %6
%8 = call i8* @skip(i8* %0, i8 signext 38)
%9 = icmp eq i8* %8, null
br i1 %9, label %26, label %10
10: ; preds = %1
%11 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 1
br label %12
12: ; preds = %10, %17
%13 = phi i8* [ %8, %10 ], [ %24, %17 ]
%14 = phi i8* [ %0, %10 ], [ %19, %17 ]
%15 = load i8, i8* %13
%16 = icmp eq i8 %15, 0
br i1 %16, label %26, label %17
17: ; preds = %12
%18 = call i32 @strcat(i8* %6, i8* %14)
%19 = call i8* @skip(i8* %13, i8 signext 59)
%20 = getelementptr inbounds i8, i8* %13, i64 1
%21 = call i64 @strtol(i8* nocapture %20, i32* null, i32 10)
%22 = trunc i64 %21 to i8
store i8 %22, i8* %7
store i8 0, i8* %11
%23 = call i32 @strcat(i8* %6, i8* %7)
%24 = call i8* @skip(i8* %19, i8 signext 38)
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %12
26: ; preds = %12, %17, %1
%27 = phi i8* [ %0, %1 ], [ %19, %17 ], [ %14, %12 ]
%28 = call i32 @strcat(i8* %6, i8* %27)
%29 = call i32 @strlen(i8* %6)
%30 = add nsw i32 %29, 1
%31 = call i32 @memcpy(i8* %0, i8* %6, i32 %30)
ret void
}