#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*/ isxdigit(char);
int /*<<< orphan*/ strncpy(char *, char *, int);
char strtoul(char *, int /*<<< orphan*/ *, int);
int FormatDeescapeString(char *string) {
  char *srcPtr = string;
  char *dstPtr = string;
  int charCount = 0;
  while (*srcPtr) {
    char srcChar = *srcPtr;
    srcPtr++;
    if (srcChar == '\\') {
      char escapeChar = *srcPtr;
      srcPtr++;
      switch (escapeChar) {
      case 'x':
        if (isxdigit(srcPtr[0]) && isxdigit(srcPtr[1])) {
          char hex[3];
          hex[2] = '\0';
          strncpy(hex, srcPtr, 2);
          srcPtr += 2;
          srcChar = strtoul(hex, NULL, 16);
        } else
          srcChar = '?';
        break;
      case '\\':
        srcChar = '\\';
        break;
      case 'r':
        srcChar = '\r';
        break;
      case 'n':
        srcChar = '\n';
        break;
      default:
        srcChar = '?';
        break;
      }
    }
    *dstPtr = srcChar;
    dstPtr++;
    charCount++;
  }
  *dstPtr = '\0';
  return charCount;
}


x86 assembly (clang, -O0):
<FormatDeescapeString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     1278 <FormatDeescapeString+0x148>
mov    -0x10(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x1d(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
movsbl -0x1d(%rbp),%eax
cmp    $0x5c,%eax
jne    1255 <FormatDeescapeString+0x125>
mov    -0x10(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x1e(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
movsbl -0x1e(%rbp),%eax
add    $0xffffffa4,%eax
mov    %eax,%ecx
mov    %rcx,-0x30(%rbp)
sub    $0x1c,%eax
ja     124c <FormatDeescapeString+0x11c>
mov    -0x30(%rbp),%rax
lea    0xe48(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1050 <isxdigit@plt>
cmp    $0x0,%eax
je     1228 <FormatDeescapeString+0xf8>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%edi
call   1050 <isxdigit@plt>
cmp    $0x0,%eax
je     1228 <FormatDeescapeString+0xf8>
movb   $0x0,-0x1f(%rbp)
lea    -0x21(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x2,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x10(%rbp)
lea    -0x21(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1040 <strtoul@plt>
mov    %al,-0x1d(%rbp)
jmp    122c <FormatDeescapeString+0xfc>
movb   $0x3f,-0x1d(%rbp)
jmp    1250 <FormatDeescapeString+0x120>
movb   $0x5c,-0x1d(%rbp)
jmp    1250 <FormatDeescapeString+0x120>
movb   $0xd,-0x1d(%rbp)
jmp    1250 <FormatDeescapeString+0x120>
movb   $0xa,-0x1d(%rbp)
jmp    1250 <FormatDeescapeString+0x120>
movb   $0x3f,-0x1d(%rbp)
jmp    1255 <FormatDeescapeString+0x125>
mov    -0x1d(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1153 <FormatDeescapeString+0x23>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<FormatDeescapeString>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    (%rdi),%bl
xor    %r12d,%r12d
test   %bl,%bl
je     11fe <FormatDeescapeString+0xce>
lea    0xeae(%rip),%rbp
mov    %r14,%r13
jmp    1177 <FormatDeescapeString+0x47>
mov    $0xa,%bl
nopl   0x0(%rax)
mov    %bl,(%r14,%r12,1)
movzbl (%r15),%ebx
add    $0x1,%r12
mov    %r15,%r13
test   %bl,%bl
je     11fb <FormatDeescapeString+0xcb>
cmp    $0x5c,%bl
jne    11a0 <FormatDeescapeString+0x70>
movsbl 0x1(%r13),%eax
lea    0x2(%r13),%r15
add    $0xffffffa4,%eax
cmp    $0x1c,%eax
ja     11a9 <FormatDeescapeString+0x79>
mov    $0x5c,%bl
movslq 0x0(%rbp,%rax,4),%rax
add    %rbp,%rax
jmp    *%rax
mov    $0xd,%bl
jmp    1160 <FormatDeescapeString+0x30>
nopl   (%rax)
add    $0x1,%r13
mov    %r13,%r15
jmp    1160 <FormatDeescapeString+0x30>
mov    $0x3f,%bl
jmp    1160 <FormatDeescapeString+0x30>
movsbl (%r15),%edi
call   1050 <isxdigit@plt>
mov    $0x3f,%bl
test   %eax,%eax
je     1160 <FormatDeescapeString+0x30>
movsbl 0x3(%r13),%edi
call   1050 <isxdigit@plt>
test   %eax,%eax
je     1160 <FormatDeescapeString+0x30>
movb   $0x0,0x7(%rsp)
lea    0x5(%rsp),%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x2,%edx
call   1030 <strncpy@plt>
add    $0x4,%r13
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <strtoul@plt>
mov    %eax,%ebx
jmp    11a4 <FormatDeescapeString+0x74>
add    %r12,%r14
movb   $0x0,(%r14)
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @FormatDeescapeString(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8
%7 = alloca i8
%8 = alloca [3 x i8]
store i8* %0, i8** %2
%9 = load i8*, i8** %2
store i8* %9, i8** %3
%10 = load i8*, i8** %2
store i8* %10, i8** %4
store i32 0, i32* %5
br label %11
11: ; preds = %58, %1
%12 = load i8*, i8** %3
%13 = load i8, i8* %12
%14 = icmp ne i8 %13, 0
br i1 %14, label %15, label %65
15: ; preds = %11
%16 = load i8*, i8** %3
%17 = load i8, i8* %16
store i8 %17, i8* %6
%18 = load i8*, i8** %3
%19 = getelementptr inbounds i8, i8* %18, i32 1
store i8* %19, i8** %3
%20 = load i8, i8* %6
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 92
br i1 %22, label %23, label %58
23: ; preds = %15
%24 = load i8*, i8** %3
%25 = load i8, i8* %24
store i8 %25, i8* %7
%26 = load i8*, i8** %3
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %3
%28 = load i8, i8* %7
%29 = sext i8 %28 to i32
switch i32 %29, label %56 [
i32 120, label %30
i32 92, label %53
i32 114, label %54
i32 110, label %55
]
30: ; preds = %23
%31 = load i8*, i8** %3
%32 = getelementptr inbounds i8, i8* %31, i64 0
%33 = load i8, i8* %32
%34 = call i32 @isxdigit(i8 signext %33)
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %51
36: ; preds = %30
%37 = load i8*, i8** %3
%38 = getelementptr inbounds i8, i8* %37, i64 1
%39 = load i8, i8* %38
%40 = call i32 @isxdigit(i8 signext %39)
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %51
42: ; preds = %36
%43 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 2
store i8 0, i8* %43
%44 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 0
%45 = load i8*, i8** %3
%46 = call i32 @strncpy(i8* %44, i8* %45, i32 2)
%47 = load i8*, i8** %3
%48 = getelementptr inbounds i8, i8* %47, i64 2
store i8* %48, i8** %3
%49 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 0
%50 = call signext i8 @strtoul(i8* %49, i32* null, i32 16)
store i8 %50, i8* %6
br label %52
51: ; preds = %36, %30
store i8 63, i8* %6
br label %52
52: ; preds = %51, %42
br label %57
53: ; preds = %23
store i8 92, i8* %6
br label %57
54: ; preds = %23
store i8 13, i8* %6
br label %57
55: ; preds = %23
store i8 10, i8* %6
br label %57
56: ; preds = %23
store i8 63, i8* %6
br label %57
57: ; preds = %56, %55, %54, %53, %52
br label %58
58: ; preds = %57, %15
%59 = load i8, i8* %6
%60 = load i8*, i8** %4
store i8 %59, i8* %60
%61 = load i8*, i8** %4
%62 = getelementptr inbounds i8, i8* %61, i32 1
store i8* %62, i8** %4
%63 = load i32, i32* %5
%64 = add nsw i32 %63, 1
store i32 %64, i32* %5
br label %11
65: ; preds = %11
%66 = load i8*, i8** %4
store i8 0, i8* %66
%67 = load i32, i32* %5
ret i32 %67
}

LLVM IR (-O2):
define i32 @FormatDeescapeString(i8* %0) {
%2 = alloca [3 x i8]
%3 = load i8, i8* %0
%4 = icmp eq i8 %3, 0
br i1 %4, label %42, label %5
5: ; preds = %1
%6 = getelementptr inbounds [3 x i8], [3 x i8]* %2, i64 0, i64 0
%7 = getelementptr inbounds [3 x i8], [3 x i8]* %2, i64 0, i64 2
br label %8
8: ; preds = %5, %35
%9 = phi i8 [ %3, %5 ], [ %40, %35 ]
%10 = phi i8* [ %0, %5 ], [ %37, %35 ]
%11 = phi i32 [ 0, %5 ], [ %39, %35 ]
%12 = phi i8* [ %0, %5 ], [ %38, %35 ]
%13 = getelementptr inbounds i8, i8* %10, i64 1
%14 = icmp eq i8 %9, 92
br i1 %14, label %15, label %35
15: ; preds = %8
%16 = load i8, i8* %13
%17 = getelementptr inbounds i8, i8* %10, i64 2
%18 = sext i8 %16 to i32
switch i32 %18, label %34 [
i32 120, label %19
i32 92, label %35
i32 114, label %32
i32 110, label %33
]
19: ; preds = %15
%20 = load i8, i8* %17
%21 = call i32 @isxdigit(i8 signext %20)
%22 = icmp eq i32 %21, 0
br i1 %22, label %35, label %23
23: ; preds = %19
%24 = getelementptr inbounds i8, i8* %10, i64 3
%25 = load i8, i8* %24
%26 = call i32 @isxdigit(i8 signext %25)
%27 = icmp eq i32 %26, 0
br i1 %27, label %35, label %28
28: ; preds = %23
store i8 0, i8* %7
%29 = call i32 @strncpy(i8* %6, i8* %17, i32 2)
%30 = getelementptr inbounds i8, i8* %10, i64 4
%31 = call signext i8 @strtoul(i8* nocapture %6, i32* null, i32 16)
br label %35
32: ; preds = %15
br label %35
33: ; preds = %15
br label %35
34: ; preds = %15
br label %35
35: ; preds = %32, %33, %34, %28, %23, %19, %15, %8
%36 = phi i8 [ %9, %8 ], [ 63, %34 ], [ 10, %33 ], [ 13, %32 ], [ %31, %28 ], [ 63, %23 ], [ 63, %19 ], [ 92, %15 ]
%37 = phi i8* [ %13, %8 ], [ %17, %34 ], [ %17, %33 ], [ %17, %32 ], [ %30, %28 ], [ %17, %23 ], [ %17, %19 ], [ %17, %15 ]
store i8 %36, i8* %12
%38 = getelementptr inbounds i8, i8* %12, i64 1
%39 = add nuw nsw i32 %11, 1
%40 = load i8, i8* %37
%41 = icmp eq i8 %40, 0
br i1 %41, label %42, label %8
42: ; preds = %35, %1
%43 = phi i8* [ %0, %1 ], [ %38, %35 ]
%44 = phi i32 [ 0, %1 ], [ %39, %35 ]
store i8 0, i8* %43
ret i32 %44
}