#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 */
char *do_ucs4toutf8(char const *);
int /*<<< orphan*/ s_free(char *);
char *s_realloc(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int strlen(char *);
char *ucs4toutf8(const char *from) {
  if (!from) {
    return NULL;
  }
  char *out = NULL;
  int outlen = 0;
  const char *c;
  for (c = from; *c != '\0'; ++c) {
    char *s;
    if (*c == '\\' && *(c + 1) == 'u') {
      s = do_ucs4toutf8(c);
      out = s_realloc(out, outlen + strlen(s) + 1);
      snprintf(out + outlen, strlen(s) + 1, "%s", s);
      outlen = strlen(out);
      s_free(s);
      c += 5;
    } else {
      out = s_realloc(out, outlen + 2);
      out[outlen] = *c;
      out[outlen + 1] = '\0';
      outlen++;
      continue;
    }
  }
  return out;
}


x86 assembly (clang, -O0):
<ucs4toutf8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1174 <ucs4toutf8+0x24>
movq   $0x0,-0x8(%rbp)
jmp    12ad <ucs4toutf8+0x15d>
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12a5 <ucs4toutf8+0x155>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5c,%eax
jne    124c <ucs4toutf8+0xfc>
mov    -0x28(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x75,%eax
jne    124c <ucs4toutf8+0xfc>
mov    -0x28(%rbp),%rdi
call   1070 <do_ucs4toutf8@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x44(%rbp),%esi
mov    -0x40(%rbp),%rdi
add    %eax,%esi
add    $0x1,%esi
call   1040 <s_realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%esi
add    $0x1,%esi
mov    -0x30(%rbp),%rcx
lea    0xddf(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <s_free@plt>
mov    -0x28(%rbp),%rax
add    $0x5,%rax
mov    %rax,-0x28(%rbp)
jmp    128f <ucs4toutf8+0x13f>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
add    $0x2,%esi
call   1040 <s_realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%dl
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1294 <ucs4toutf8+0x144>
jmp    1294 <ucs4toutf8+0x144>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    118b <ucs4toutf8+0x3b>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ucs4toutf8>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
xor    %ebx,%ebx
test   %rdi,%rdi
je     1220 <ucs4toutf8+0xd0>
mov    %rdi,%r12
lea    0xe92(%rip),%r14
xor    %r15d,%r15d
jmp    11b0 <ucs4toutf8+0x60>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
test   %al,%al
je     1223 <ucs4toutf8+0xd3>
lea    0x2(%rbx),%esi
mov    %r15,%rdi
call   1040 <s_realloc@plt>
mov    %rax,%r15
movzbl (%r12),%eax
movslq %ebx,%rbx
mov    %al,(%r15,%rbx,1)
movb   $0x0,0x1(%r15,%rbx,1)
add    $0x1,%rbx
add    $0x1,%r12
movzbl (%r12),%eax
cmp    $0x5c,%al
jne    1180 <ucs4toutf8+0x30>
cmpb   $0x75,0x1(%r12)
jne    1188 <ucs4toutf8+0x38>
mov    %r12,%rdi
call   1070 <do_ucs4toutf8@plt>
mov    %rax,%r13
mov    %rax,%rdi
call   1050 <strlen@plt>
lea    (%rbx,%rax,1),%esi
add    $0x1,%esi
mov    %r15,%rdi
call   1040 <s_realloc@plt>
mov    %rax,%r15
movslq %ebx,%rbx
add    %rax,%rbx
mov    %r13,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%esi
mov    %rbx,%rdi
mov    %r14,%rdx
mov    %r13,%rcx
call   1060 <snprintf@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
mov    %r13,%rdi
call   1030 <s_free@plt>
add    $0x5,%r12
add    $0x1,%r12
jmp    11b0 <ucs4toutf8+0x60>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
define i8* @ucs4toutf8(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = icmp ne i8* %8, null
br i1 %9, label %11, label %10
10: ; preds = %1
store i8* null, i8** %2
br label %78
11: ; preds = %1
store i8* null, i8** %4
store i32 0, i32* %5
%12 = load i8*, i8** %3
store i8* %12, i8** %6
br label %13
13: ; preds = %73, %11
%14 = load i8*, i8** %6
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %76
18: ; preds = %13
%19 = load i8*, i8** %6
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 92
br i1 %22, label %23, label %54
23: ; preds = %18
%24 = load i8*, i8** %6
%25 = getelementptr inbounds i8, i8* %24, i64 1
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 117
br i1 %28, label %29, label %54
29: ; preds = %23
%30 = load i8*, i8** %6
%31 = call i8* @do_ucs4toutf8(i8* %30)
store i8* %31, i8** %7
%32 = load i8*, i8** %4
%33 = load i32, i32* %5
%34 = load i8*, i8** %7
%35 = call i32 @strlen(i8* %34)
%36 = add nsw i32 %33, %35
%37 = add nsw i32 %36, 1
%38 = call i8* @s_realloc(i8* %32, i32 %37)
store i8* %38, i8** %4
%39 = load i8*, i8** %4
%40 = load i32, i32* %5
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
%43 = load i8*, i8** %7
%44 = call i32 @strlen(i8* %43)
%45 = add nsw i32 %44, 1
%46 = load i8*, i8** %7
%47 = call i32 @snprintf(i8* %42, i32 %45, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %46)
%48 = load i8*, i8** %4
%49 = call i32 @strlen(i8* %48)
store i32 %49, i32* %5
%50 = load i8*, i8** %7
%51 = call i32 @s_free(i8* %50)
%52 = load i8*, i8** %6
%53 = getelementptr inbounds i8, i8* %52, i64 5
store i8* %53, i8** %6
br label %72
54: ; preds = %23, %18
%55 = load i8*, i8** %4
%56 = load i32, i32* %5
%57 = add nsw i32 %56, 2
%58 = call i8* @s_realloc(i8* %55, i32 %57)
store i8* %58, i8** %4
%59 = load i8*, i8** %6
%60 = load i8, i8* %59
%61 = load i8*, i8** %4
%62 = load i32, i32* %5
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %61, i64 %63
store i8 %60, i8* %64
%65 = load i8*, i8** %4
%66 = load i32, i32* %5
%67 = add nsw i32 %66, 1
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i8, i8* %65, i64 %68
store i8 0, i8* %69
%70 = load i32, i32* %5
%71 = add nsw i32 %70, 1
store i32 %71, i32* %5
br label %73
72: ; preds = %29
br label %73
73: ; preds = %72, %54
%74 = load i8*, i8** %6
%75 = getelementptr inbounds i8, i8* %74, i32 1
store i8* %75, i8** %6
br label %13
76: ; preds = %13
%77 = load i8*, i8** %4
store i8* %77, i8** %2
br label %78
78: ; preds = %76, %10
%79 = load i8*, i8** %2
ret i8* %79
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
define i8* @ucs4toutf8(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %40, label %3
3: ; preds = %1, %35
%4 = phi i8* [ %36, %35 ], [ null, %1 ]
%5 = phi i32 [ %37, %35 ], [ 0, %1 ]
%6 = phi i8* [ %39, %35 ], [ %0, %1 ]
%7 = load i8, i8* %6
switch i8 %7, label %26 [
i8 0, label %40
i8 92, label %8
]
8: ; preds = %3
%9 = getelementptr inbounds i8, i8* %6, i64 1
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 117
br i1 %11, label %12, label %26
12: ; preds = %8
%13 = tail call i8* @do_ucs4toutf8(i8* %6)
%14 = tail call i32 @strlen(i8* %13)
%15 = add i32 %5, 1
%16 = add i32 %15, %14
%17 = tail call i8* @s_realloc(i8* %4, i32 %16)
%18 = sext i32 %5 to i64
%19 = getelementptr inbounds i8, i8* %17, i64 %18
%20 = tail call i32 @strlen(i8* %13)
%21 = add nsw i32 %20, 1
%22 = tail call i32 @snprintf(i8* %19, i32 %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %13)
%23 = tail call i32 @strlen(i8* %17)
%24 = tail call i32 @s_free(i8* %13)
%25 = getelementptr inbounds i8, i8* %6, i64 5
br label %35
26: ; preds = %3, %8
%27 = add nsw i32 %5, 2
%28 = tail call i8* @s_realloc(i8* %4, i32 %27)
%29 = load i8, i8* %6
%30 = sext i32 %5 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
store i8 %29, i8* %31
%32 = add nsw i32 %5, 1
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %28, i64 %33
store i8 0, i8* %34
br label %35
35: ; preds = %12, %26
%36 = phi i8* [ %17, %12 ], [ %28, %26 ]
%37 = phi i32 [ %23, %12 ], [ %32, %26 ]
%38 = phi i8* [ %25, %12 ], [ %6, %26 ]
%39 = getelementptr inbounds i8, i8* %38, i64 1
br label %3
40: ; preds = %3, %1
%41 = phi i8* [ null, %1 ], [ %4, %3 ]
ret i8* %41
}