#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 */
scalar_t__ isprint(int);
int /*<<< orphan*/ memmove(char *, char *, int);
char *strchr(char const *, char);
int strlen(char *);
void mag_dequote(char *ibuf) {
  char *esc = NULL;
  while ((esc = strchr(ibuf, 0x1b))) {
    int nremains = strlen(esc);
    if (nremains >= 3) {
      static const char hex[16] = "0123456789ABCDEF";
      char *c1 = strchr(hex, esc[1]);
      char *c2 = strchr(hex, esc[2]);
      if (c1 && c2) {
        int escv = (c1 - hex) * 16 + (c2 - hex);
        if (escv == 255) {
          char *tmp = esc + 1;
          while (*tmp == 'F')
            tmp++;
          memmove(esc, tmp, strlen(tmp) + 1);
        } else {
          *esc++ = (isprint(escv)) ? escv : '$';
          memmove(esc, esc + 2, nremains - 2);
        }
      }
    } else {
      *esc = '\0';
    }
  }
}


x86 assembly (clang, -O0):
<mag_dequote>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x1b,%esi
call   1040 <strchr@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     12c9 <mag_dequote+0x189>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x3,-0x14(%rbp)
jl     12bd <mag_dequote+0x17d>
mov    -0x10(%rbp),%rax
lea    0xe6f(%rip),%rdi
movsbl 0x1(%rax),%esi
call   1040 <strchr@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
lea    0xe57(%rip),%rdi
movsbl 0x2(%rax),%esi
call   1040 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12b8 <mag_dequote+0x178>
cmpq   $0x0,-0x28(%rbp)
je     12b8 <mag_dequote+0x178>
mov    -0x20(%rbp),%rax
lea    0xe29(%rip),%rcx
sub    %rcx,%rax
shl    $0x4,%rax
mov    -0x28(%rbp),%rcx
lea    0xe17(%rip),%rdx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %eax,-0x2c(%rbp)
cmpl   $0xff,-0x2c(%rbp)
jne    125c <mag_dequote+0x11c>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x46,%eax
jne    122c <mag_dequote+0xec>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    120b <mag_dequote+0xcb>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
call   1060 <memmove@plt>
jmp    12b3 <mag_dequote+0x173>
mov    -0x2c(%rbp),%edi
call   1050 <isprint@plt>
cmp    $0x0,%rax
je     1279 <mag_dequote+0x139>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4c(%rbp)
jmp    1286 <mag_dequote+0x146>
mov    $0x24,%eax
mov    %eax,-0x4c(%rbp)
jmp    1286 <mag_dequote+0x146>
mov    -0x4c(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x2,%rsi
mov    -0x14(%rbp),%edx
sub    $0x2,%edx
call   1060 <memmove@plt>
jmp    12b8 <mag_dequote+0x178>
jmp    12c4 <mag_dequote+0x184>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
jmp    1154 <mag_dequote+0x14>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mag_dequote>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbp
mov    $0x1b,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     1256 <mag_dequote+0x116>
mov    %rax,%rbx
mov    %rbp,(%rsp)
jmp    118c <mag_dequote+0x4c>
nopw   0x0(%rax,%rax,1)
movb   $0x0,(%rbx)
mov    %rbp,%rdi
mov    $0x1b,%esi
call   1040 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1256 <mag_dequote+0x116>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cmp    $0x3,%eax
jl     1170 <mag_dequote+0x30>
mov    %eax,%r12d
mov    0x1(%rbx),%r15b
movsbl %r15b,%esi
mov    %rbp,%r13
lea    0xe52(%rip),%rbp
mov    %rbp,%rdi
call   1040 <strchr@plt>
mov    %rax,%r14
movsbl 0x2(%rbx),%esi
mov    %rbp,%rdi
mov    %r13,%rbp
call   1040 <strchr@plt>
test   %r14,%r14
je     1173 <mag_dequote+0x33>
mov    %rax,%r13
test   %rax,%rax
je     1173 <mag_dequote+0x33>
lea    0x1(%rbx),%rbp
lea    0xe20(%rip),%rax
sub    %rax,%r14
shl    $0x4,%r14
sub    %rax,%r13
add    %r14,%r13
cmp    $0xff,%r13d
jne    1220 <mag_dequote+0xe0>
cmp    $0x46,%r15b
jne    120d <mag_dequote+0xcd>
lea    0x1(%rbx),%rax
lea    0x1(%rax),%rbp
cmpb   $0x46,0x1(%rax)
mov    %rbp,%rax
je     1200 <mag_dequote+0xc0>
mov    %rbp,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%edx
mov    %rbx,%rdi
mov    %rbp,%rsi
jmp    1248 <mag_dequote+0x108>
mov    %r13d,%edi
call   1050 <isprint@plt>
test   %rax,%rax
mov    $0x24,%eax
cmove  %eax,%r13d
mov    %r13b,(%rbx)
add    $0x3,%rbx
add    $0xfffffffe,%r12d
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1060 <memmove@plt>
mov    (%rsp),%rbp
jmp    1173 <mag_dequote+0x33>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@mag_dequote.hex = internal constant [16 x i8] c"0123456789ABCDEF"
define void @mag_dequote(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %2
store i8* null, i8** %3
br label %9
9: ; preds = %84, %1
%10 = load i8*, i8** %2
%11 = call i8* @strchr(i8* %10, i8 signext 27)
store i8* %11, i8** %3
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %85
13: ; preds = %9
%14 = load i8*, i8** %3
%15 = call i32 @strlen(i8* %14)
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = icmp sge i32 %16, 3
br i1 %17, label %18, label %82
18: ; preds = %13
%19 = load i8*, i8** %3
%20 = getelementptr inbounds i8, i8* %19, i64 1
%21 = load i8, i8* %20
%22 = call i8* @strchr(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @mag_dequote.hex, i64 0, i64 0), i8 signext %21)
store i8* %22, i8** %5
%23 = load i8*, i8** %3
%24 = getelementptr inbounds i8, i8* %23, i64 2
%25 = load i8, i8* %24
%26 = call i8* @strchr(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @mag_dequote.hex, i64 0, i64 0), i8 signext %25)
store i8* %26, i8** %6
%27 = load i8*, i8** %5
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %81
29: ; preds = %18
%30 = load i8*, i8** %6
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %81
32: ; preds = %29
%33 = load i8*, i8** %5
%34 = ptrtoint i8* %33 to i64
%35 = sub i64 %34, ptrtoint ([16 x i8]* @mag_dequote.hex to i64)
%36 = mul nsw i64 %35, 16
%37 = load i8*, i8** %6
%38 = ptrtoint i8* %37 to i64
%39 = sub i64 %38, ptrtoint ([16 x i8]* @mag_dequote.hex to i64)
%40 = add nsw i64 %36, %39
%41 = trunc i64 %40 to i32
store i32 %41, i32* %7
%42 = load i32, i32* %7
%43 = icmp eq i32 %42, 255
br i1 %43, label %44, label %62
44: ; preds = %32
%45 = load i8*, i8** %3
%46 = getelementptr inbounds i8, i8* %45, i64 1
store i8* %46, i8** %8
br label %47
47: ; preds = %52, %44
%48 = load i8*, i8** %8
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
%51 = icmp eq i32 %50, 70
br i1 %51, label %52, label %55
52: ; preds = %47
%53 = load i8*, i8** %8
%54 = getelementptr inbounds i8, i8* %53, i32 1
store i8* %54, i8** %8
br label %47
55: ; preds = %47
%56 = load i8*, i8** %3
%57 = load i8*, i8** %8
%58 = load i8*, i8** %8
%59 = call i32 @strlen(i8* %58)
%60 = add nsw i32 %59, 1
%61 = call i32 @memmove(i8* %56, i8* %57, i32 %60)
br label %80
62: ; preds = %32
%63 = load i32, i32* %7
%64 = call i64 @isprint(i32 %63)
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %62
%67 = load i32, i32* %7
br label %69
68: ; preds = %62
br label %69
69: ; preds = %68, %66
%70 = phi i32 [ %67, %66 ], [ 36, %68 ]
%71 = trunc i32 %70 to i8
%72 = load i8*, i8** %3
%73 = getelementptr inbounds i8, i8* %72, i32 1
store i8* %73, i8** %3
store i8 %71, i8* %72
%74 = load i8*, i8** %3
%75 = load i8*, i8** %3
%76 = getelementptr inbounds i8, i8* %75, i64 2
%77 = load i32, i32* %4
%78 = sub nsw i32 %77, 2
%79 = call i32 @memmove(i8* %74, i8* %76, i32 %78)
br label %80
80: ; preds = %69, %55
br label %81
81: ; preds = %80, %29, %18
br label %84
82: ; preds = %13
%83 = load i8*, i8** %3
store i8 0, i8* %83
br label %84
84: ; preds = %82, %81
br label %9
85: ; preds = %9
ret void
}

LLVM IR (-O2):
@mag_dequote.hex = internal constant [16 x i8] c"0123456789ABCDEF"
define void @mag_dequote(i8* %0) {
%2 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 27)
%3 = icmp eq i8* %2, null
br i1 %3, label %53, label %4
4: ; preds = %1, %50
%5 = phi i8* [ %51, %50 ], [ %2, %1 ]
%6 = tail call i32 @strlen(i8* %5)
%7 = icmp sgt i32 %6, 2
br i1 %7, label %8, label %49
8: ; preds = %4
%9 = getelementptr inbounds i8, i8* %5, i64 1
%10 = load i8, i8* %9
%11 = tail call i8* @strchr(i8* dereferenceable(17) getelementptr inbounds ([16 x i8], [16 x i8]* @mag_dequote.hex, i64 0, i64 0), i8 signext %10)
%12 = getelementptr inbounds i8, i8* %5, i64 2
%13 = load i8, i8* %12
%14 = tail call i8* @strchr(i8* dereferenceable(17) getelementptr inbounds ([16 x i8], [16 x i8]* @mag_dequote.hex, i64 0, i64 0), i8 signext %13)
%15 = icmp ne i8* %11, null
%16 = icmp ne i8* %14, null
%17 = select i1 %15, i1 %16, i1 false
br i1 %17, label %18, label %50
18: ; preds = %8
%19 = ptrtoint i8* %11 to i64
%20 = sub i64 %19, ptrtoint ([16 x i8]* @mag_dequote.hex to i64)
%21 = shl nsw i64 %20, 4
%22 = ptrtoint i8* %14 to i64
%23 = sub i64 %22, ptrtoint ([16 x i8]* @mag_dequote.hex to i64)
%24 = add nsw i64 %23, %21
%25 = trunc i64 %24 to i32
%26 = icmp eq i32 %25, 255
br i1 %26, label %27, label %41
27: ; preds = %18
%28 = icmp eq i8 %10, 70
br i1 %28, label %29, label %36
29: ; preds = %27
%30 = getelementptr inbounds i8, i8* %5, i64 2
br label %31
31: ; preds = %29, %31
%32 = phi i8* [ %30, %29 ], [ %35, %31 ]
%33 = load i8, i8* %32
%34 = icmp eq i8 %33, 70
%35 = getelementptr inbounds i8, i8* %32, i64 1
br i1 %34, label %31, label %36
36: ; preds = %31, %27
%37 = phi i8* [ %9, %27 ], [ %32, %31 ]
%38 = tail call i32 @strlen(i8* %37)
%39 = add nsw i32 %38, 1
%40 = tail call i32 @memmove(i8* %5, i8* %37, i32 %39)
br label %50
41: ; preds = %18
%42 = tail call i64 @isprint(i32 %25)
%43 = icmp eq i64 %42, 0
%44 = trunc i64 %24 to i8
%45 = select i1 %43, i8 36, i8 %44
store i8 %45, i8* %5
%46 = getelementptr inbounds i8, i8* %5, i64 3
%47 = add nsw i32 %6, -2
%48 = tail call i32 @memmove(i8* %9, i8* %46, i32 %47)
br label %50
49: ; preds = %4
store i8 0, i8* %5
br label %50
50: ; preds = %8, %41, %36, %49
%51 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 27)
%52 = icmp eq i8* %51, null
br i1 %52, label %53, label %4
53: ; preds = %50, %1
ret void
}