#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 char uint32_t;
typedef int /*<<< orphan*/ iconv_t;

/* Variables and functions */
int /*<<< orphan*/ errx(int, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ iconv_close(int /*<<< orphan*/);
int /*<<< orphan*/ sscanf(char *, char *, char *);
scalar_t__ string_do_convert(char const *, int /*<<< orphan*/);
char *string_do_convert_length(char *, int /*<<< orphan*/, size_t);
int strlen(char const *);
char *xcalloc(size_t, int);
int /*<<< orphan*/ xiconv_open(char *, char *);
char *string_unescape(const char *in) {
  size_t len = strlen(in) + 1;
  iconv_t ic = xiconv_open("UTF-32", "UTF-8");
  uint32_t *in_exp = (uint32_t *)string_do_convert(in, ic);
  uint32_t *out_exp = xcalloc(len + 1, sizeof(uint32_t));
  uint32_t *in_pos = in_exp;
  uint32_t *out_pos = out_exp;
  char tmp_str[5];
  char *ret;
  size_t i;
  iconv_close(ic);
  *out_pos = *in_pos;
  out_pos++;
  in_pos++;
  for (; *in_pos; out_pos++, in_pos++) {
    if (*in_pos != '\\') {
      *out_pos = *in_pos;
      continue;
    }
    in_pos++;
    switch (*in_pos) {
    case '\"':
    case '\\':
    case '/':
      *out_pos = *in_pos;
      break;
    case 'b':
      *out_pos = '\b';
      break;
    case 'f':
      *out_pos = '\f';
      break;
    case 'n':
      *out_pos = '\n';
      break;
    case 'r':
      *out_pos = '\r';
      break;
    case 't':
      *out_pos = '\t';
      break;
    case 'v':
      *out_pos = '\v';
      break;
    case 'u':
      for (i = 0; i < 4; i++)
        tmp_str[i] = (char)*(++in_pos);
      tmp_str[4] = '\0';
      if (sscanf(tmp_str, "%04x", out_pos) > 0)
        break;
    default:
      errx(1, "Unexpected escape sequence");
    }
  }
  *out_pos = 0;
  ic = xiconv_open("UTF-8", "UTF-32");
  ret = string_do_convert_length((char *)out_exp, ic,
                                 (len + 1) * sizeof(uint32_t));
  iconv_close(ic);
  free(out_exp);
  free(in_exp);
  return ret;
}


x86 assembly (clang, -O0):
<string_unescape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x10(%rbp)
lea    0xf9f(%rip),%rdi
lea    0xf9f(%rip),%rsi
call   1090 <xiconv_open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   10a0 <string_do_convert@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   10b0 <xcalloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
call   1080 <iconv_close@plt>
mov    -0x30(%rbp),%rax
mov    (%rax),%cl
mov    -0x38(%rbp),%rax
mov    %cl,(%rax)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmpb   $0x0,(%rax)
je     1382 <string_unescape+0x1f2>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5c,%eax
je     1254 <string_unescape+0xc4>
mov    -0x30(%rbp),%rax
mov    (%rax),%cl
mov    -0x38(%rbp),%rax
mov    %cl,(%rax)
jmp    1365 <string_unescape+0x1d5>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
add    $0xffffffde,%eax
mov    %eax,%ecx
mov    %rcx,-0x58(%rbp)
sub    $0x54,%eax
ja     134f <string_unescape+0x1bf>
mov    -0x58(%rbp),%rax
lea    0xd7c(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x30(%rbp),%rax
mov    (%rax),%cl
mov    -0x38(%rbp),%rax
mov    %cl,(%rax)
jmp    1360 <string_unescape+0x1d0>
mov    -0x38(%rbp),%rax
movb   $0x8,(%rax)
jmp    1360 <string_unescape+0x1d0>
mov    -0x38(%rbp),%rax
movb   $0xc,(%rax)
jmp    1360 <string_unescape+0x1d0>
mov    -0x38(%rbp),%rax
movb   $0xa,(%rax)
jmp    1360 <string_unescape+0x1d0>
mov    -0x38(%rbp),%rax
movb   $0xd,(%rax)
jmp    1360 <string_unescape+0x1d0>
mov    -0x38(%rbp),%rax
movb   $0x9,(%rax)
jmp    1360 <string_unescape+0x1d0>
mov    -0x38(%rbp),%rax
movb   $0xb,(%rax)
jmp    1360 <string_unescape+0x1d0>
movq   $0x0,-0x50(%rbp)
cmpq   $0x4,-0x50(%rbp)
jae    1324 <string_unescape+0x194>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
mov    0x1(%rax),%cl
mov    -0x50(%rbp),%rax
mov    %cl,-0x3d(%rbp,%rax,1)
mov    -0x50(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x50(%rbp)
jmp    12ee <string_unescape+0x15e>
movb   $0x0,-0x39(%rbp)
lea    -0x3d(%rbp),%rdi
mov    -0x38(%rbp),%rdx
lea    0xe2a(%rip),%rsi
call   1070 <sscanf@plt>
cmp    $0x0,%eax
jle    134a <string_unescape+0x1ba>
jmp    1360 <string_unescape+0x1d0>
jmp    134f <string_unescape+0x1bf>
mov    $0x1,%edi
lea    0xe0b(%rip),%rsi
call   1060 <errx@plt>
jmp    1365 <string_unescape+0x1d5>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1226 <string_unescape+0x96>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
lea    0xdcb(%rip),%rdi
lea    0xdbd(%rip),%rsi
call   1090 <xiconv_open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rdx
add    $0x1,%rdx
shl    $0x0,%rdx
call   1050 <string_do_convert_length@plt>
mov    %rax,-0x48(%rbp)
mov    -0x14(%rbp),%edi
call   1080 <iconv_close@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x48(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_unescape>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    %eax,%r15d
lea    0xfa4(%rip),%rdi
lea    0xfa4(%rip),%rsi
call   1090 <xiconv_open@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %eax,%esi
call   10a0 <string_do_convert@plt>
mov    %rax,%r13
movslq %r15d,%r12
add    $0x2,%r12
mov    %r12,%rdi
mov    $0x1,%esi
call   10b0 <xcalloc@plt>
mov    %rax,%r15
mov    %ebp,%edi
call   1080 <iconv_close@plt>
mov    0x0(%r13),%al
mov    %al,(%r15)
mov    %r15,%rbx
add    $0x1,%rbx
lea    0xe02(%rip),%rbp
mov    %r13,0x10(%rsp)
jmp    1227 <string_unescape+0x97>
mov    $0x1,%edi
lea    0xf55(%rip),%rsi
call   1060 <errx@plt>
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%rbx
mov    %r14,%r13
movzbl 0x1(%r13),%eax
cmp    $0x5c,%al
je     1250 <string_unescape+0xc0>
test   %al,%al
je     12e1 <string_unescape+0x151>
add    $0x1,%r13
mov    %al,(%rbx)
mov    %r13,%r14
jmp    1220 <string_unescape+0x90>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0x2(%r13),%r14
movsbl 0x2(%r13),%eax
lea    -0x22(%rax),%ecx
cmp    $0x54,%ecx
ja     1205 <string_unescape+0x75>
movslq 0x0(%rbp,%rcx,4),%rcx
add    %rbp,%rcx
jmp    *%rcx
mov    %al,(%rbx)
jmp    1220 <string_unescape+0x90>
movb   $0x8,(%rbx)
jmp    1220 <string_unescape+0x90>
movb   $0xc,(%rbx)
jmp    1220 <string_unescape+0x90>
movb   $0xa,(%rbx)
jmp    1220 <string_unescape+0x90>
movb   $0xd,(%rbx)
jmp    1220 <string_unescape+0x90>
movb   $0x9,(%rbx)
jmp    1220 <string_unescape+0x90>
movzbl 0x3(%r13),%eax
mov    %al,0xb(%rsp)
movzbl 0x4(%r13),%eax
mov    %al,0xc(%rsp)
movzbl 0x5(%r13),%eax
mov    %al,0xd(%rsp)
movzbl 0x6(%r13),%eax
add    $0x6,%r13
mov    %al,0xe(%rsp)
movb   $0x0,0xf(%rsp)
lea    0xb(%rsp),%rdi
lea    0xea0(%rip),%rsi
mov    %rbx,%rdx
call   1070 <sscanf@plt>
mov    %r13,%r14
test   %eax,%eax
jg     1220 <string_unescape+0x90>
jmp    1205 <string_unescape+0x75>
movb   $0xb,(%rbx)
jmp    1220 <string_unescape+0x90>
movb   $0x0,(%rbx)
lea    0xe70(%rip),%rdi
lea    0xe62(%rip),%rsi
call   1090 <xiconv_open@plt>
mov    %eax,%ebx
mov    %r15,%rdi
mov    %eax,%esi
mov    %r12,%rdx
call   1050 <string_do_convert_length@plt>
mov    %rax,%rbp
mov    %ebx,%edi
call   1080 <iconv_close@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    %rbp,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"UTF-32\00"
@.str.1 = [6 x i8] c"UTF-8\00"
@.str.2 = [5 x i8] c"%04x\00"
@.str.3 = [27 x i8] c"Unexpected escape sequence\00"
define i8* @string_unescape(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca [5 x i8]
%10 = alloca i8*
%11 = alloca i64
store i8* %0, i8** %2
%12 = load i8*, i8** %2
%13 = call i32 @strlen(i8* %12)
%14 = add nsw i32 %13, 1
%15 = sext i32 %14 to i64
store i64 %15, i64* %3
%16 = call i32 @xiconv_open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 %16, i32* %4
%17 = load i8*, i8** %2
%18 = load i32, i32* %4
%19 = call i64 @string_do_convert(i8* %17, i32 %18)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %5
%21 = load i64, i64* %3
%22 = add i64 %21, 1
%23 = call i8* @xcalloc(i64 %22, i32 1)
store i8* %23, i8** %6
%24 = load i8*, i8** %5
store i8* %24, i8** %7
%25 = load i8*, i8** %6
store i8* %25, i8** %8
%26 = load i32, i32* %4
%27 = call i32 @iconv_close(i32 %26)
%28 = load i8*, i8** %7
%29 = load i8, i8* %28
%30 = load i8*, i8** %8
store i8 %29, i8* %30
%31 = load i8*, i8** %8
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %8
%33 = load i8*, i8** %7
%34 = getelementptr inbounds i8, i8* %33, i32 1
store i8* %34, i8** %7
br label %35
35: ; preds = %94, %1
%36 = load i8*, i8** %7
%37 = load i8, i8* %36
%38 = icmp ne i8 %37, 0
br i1 %38, label %39, label %99
39: ; preds = %35
%40 = load i8*, i8** %7
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = icmp ne i32 %42, 92
br i1 %43, label %44, label %48
44: ; preds = %39
%45 = load i8*, i8** %7
%46 = load i8, i8* %45
%47 = load i8*, i8** %8
store i8 %46, i8* %47
br label %94
48: ; preds = %39
%49 = load i8*, i8** %7
%50 = getelementptr inbounds i8, i8* %49, i32 1
store i8* %50, i8** %7
%51 = load i8*, i8** %7
%52 = load i8, i8* %51
%53 = sext i8 %52 to i32
switch i32 %53, label %91 [
i32 34, label %54
i32 92, label %54
i32 47, label %54
i32 98, label %58
i32 102, label %60
i32 110, label %62
i32 114, label %64
i32 116, label %66
i32 118, label %68
i32 117, label %70
]
54: ; preds = %48, %48, %48
%55 = load i8*, i8** %7
%56 = load i8, i8* %55
%57 = load i8*, i8** %8
store i8 %56, i8* %57
br label %93
58: ; preds = %48
%59 = load i8*, i8** %8
store i8 8, i8* %59
br label %93
60: ; preds = %48
%61 = load i8*, i8** %8
store i8 12, i8* %61
br label %93
62: ; preds = %48
%63 = load i8*, i8** %8
store i8 10, i8* %63
br label %93
64: ; preds = %48
%65 = load i8*, i8** %8
store i8 13, i8* %65
br label %93
66: ; preds = %48
%67 = load i8*, i8** %8
store i8 9, i8* %67
br label %93
68: ; preds = %48
%69 = load i8*, i8** %8
store i8 11, i8* %69
br label %93
70: ; preds = %48
store i64 0, i64* %11
br label %71
71: ; preds = %80, %70
%72 = load i64, i64* %11
%73 = icmp ult i64 %72, 4
br i1 %73, label %74, label %83
74: ; preds = %71
%75 = load i8*, i8** %7
%76 = getelementptr inbounds i8, i8* %75, i32 1
store i8* %76, i8** %7
%77 = load i8, i8* %76
%78 = load i64, i64* %11
%79 = getelementptr inbounds [5 x i8], [5 x i8]* %9, i64 0, i64 %78
store i8 %77, i8* %79
br label %80
80: ; preds = %74
%81 = load i64, i64* %11
%82 = add i64 %81, 1
store i64 %82, i64* %11
br label %71
83: ; preds = %71
%84 = getelementptr inbounds [5 x i8], [5 x i8]* %9, i64 0, i64 4
store i8 0, i8* %84
%85 = getelementptr inbounds [5 x i8], [5 x i8]* %9, i64 0, i64 0
%86 = load i8*, i8** %8
%87 = call i32 @sscanf(i8* %85, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %86)
%88 = icmp sgt i32 %87, 0
br i1 %88, label %89, label %90
89: ; preds = %83
br label %93
90: ; preds = %83
br label %91
91: ; preds = %48, %90
%92 = call i32 @errx(i32 1, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %93
93: ; preds = %91, %89, %68, %66, %64, %62, %60, %58, %54
br label %94
94: ; preds = %93, %44
%95 = load i8*, i8** %8
%96 = getelementptr inbounds i8, i8* %95, i32 1
store i8* %96, i8** %8
%97 = load i8*, i8** %7
%98 = getelementptr inbounds i8, i8* %97, i32 1
store i8* %98, i8** %7
br label %35
99: ; preds = %35
%100 = load i8*, i8** %8
store i8 0, i8* %100
%101 = call i32 @xiconv_open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 %101, i32* %4
%102 = load i8*, i8** %6
%103 = load i32, i32* %4
%104 = load i64, i64* %3
%105 = add i64 %104, 1
%106 = mul i64 %105, 1
%107 = call i8* @string_do_convert_length(i8* %102, i32 %103, i64 %106)
store i8* %107, i8** %10
%108 = load i32, i32* %4
%109 = call i32 @iconv_close(i32 %108)
%110 = load i8*, i8** %6
%111 = call i32 @free(i8* %110)
%112 = load i8*, i8** %5
%113 = call i32 @free(i8* %112)
%114 = load i8*, i8** %10
ret i8* %114
}

LLVM IR (-O2):
@.str = [7 x i8] c"UTF-32\00"
@.str.1 = [6 x i8] c"UTF-8\00"
@.str.2 = [5 x i8] c"%04x\00"
@.str.3 = [27 x i8] c"Unexpected escape sequence\00"
define i8* @string_unescape(i8* %0) {
%2 = alloca [5 x i8]
%3 = tail call i32 @strlen(i8* %0)
%4 = tail call i32 @xiconv_open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%5 = tail call i64 @string_do_convert(i8* %0, i32 %4)
%6 = inttoptr i64 %5 to i8*
%7 = sext i32 %3 to i64
%8 = add nsw i64 %7, 2
%9 = tail call i8* @xcalloc(i64 %8, i32 1)
%10 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 0
%11 = tail call i32 @iconv_close(i32 %4)
%12 = load i8, i8* %6
store i8 %12, i8* %9
%13 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 4
%14 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 1
%15 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 2
%16 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 3
br label %17
17: ; preds = %40, %1
%18 = phi i8* [ %6, %1 ], [ %41, %40 ]
%19 = phi i8* [ %9, %1 ], [ %21, %40 ]
%20 = getelementptr inbounds i8, i8* %18, i64 1
%21 = getelementptr inbounds i8, i8* %19, i64 1
%22 = load i8, i8* %20
switch i8 %22, label %23 [
i8 0, label %51
i8 92, label %24
]
23: ; preds = %17
store i8 %22, i8* %21
br label %40
24: ; preds = %17
%25 = getelementptr inbounds i8, i8* %18, i64 2
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
switch i32 %27, label %48 [
i32 34, label %39
i32 92, label %39
i32 47, label %39
i32 98, label %42
i32 102, label %43
i32 110, label %44
i32 114, label %45
i32 116, label %46
i32 118, label %47
i32 117, label %28
]
28: ; preds = %24
%29 = getelementptr inbounds i8, i8* %18, i64 3
%30 = load i8, i8* %29
store i8 %30, i8* %10
%31 = getelementptr inbounds i8, i8* %18, i64 4
%32 = load i8, i8* %31
store i8 %32, i8* %14
%33 = getelementptr inbounds i8, i8* %18, i64 5
%34 = load i8, i8* %33
store i8 %34, i8* %15
%35 = getelementptr inbounds i8, i8* %18, i64 6
%36 = load i8, i8* %35
store i8 %36, i8* %16
store i8 0, i8* %13
%37 = call i32 @sscanf(i8* %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%38 = icmp sgt i32 %37, 0
br i1 %38, label %40, label %48
39: ; preds = %24, %24, %24
store i8 %26, i8* %21
br label %40
40: ; preds = %39, %42, %43, %44, %45, %46, %47, %48, %28, %23
%41 = phi i8* [ %20, %23 ], [ %49, %48 ], [ %35, %28 ], [ %25, %47 ], [ %25, %46 ], [ %25, %45 ], [ %25, %44 ], [ %25, %43 ], [ %25, %42 ], [ %25, %39 ]
br label %17
42: ; preds = %24
store i8 8, i8* %21
br label %40
43: ; preds = %24
store i8 12, i8* %21
br label %40
44: ; preds = %24
store i8 10, i8* %21
br label %40
45: ; preds = %24
store i8 13, i8* %21
br label %40
46: ; preds = %24
store i8 9, i8* %21
br label %40
47: ; preds = %24
store i8 11, i8* %21
br label %40
48: ; preds = %28, %24
%49 = phi i8* [ %25, %24 ], [ %35, %28 ]
%50 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %40
51: ; preds = %17
store i8 0, i8* %21
%52 = tail call i32 @xiconv_open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%53 = tail call i8* @string_do_convert_length(i8* %9, i32 %52, i64 %8)
%54 = tail call i32 @iconv_close(i32 %52)
%55 = tail call i32 @free(i8* %9)
%56 = tail call i32 @free(i8* %6)
ret i8* %53
}