#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 int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ TextEncodingEncode(char *, unsigned short, int);
char *TextEncodingPrefix();
char *TextEncodingSuffix();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strlcat(char *, char *, int);
int /*<<< orphan*/ strlcpy(char *, int /*<<< orphan*/, int);
int strlen(char *);
int /*<<< orphan*/ wcs2str(unsigned short *);
char *encode_say(unsigned short *in) {
  static char buf[1024];
  unsigned short *p;
  char *out;
  memset(buf, 0, sizeof(buf));
  for (p = in; *p; p++)
    if (*p > 255)
      goto encode;
  strlcpy(buf, wcs2str(in), sizeof(buf));
  return buf;
encode:
  strlcpy(buf, wcs2str(in),
          ((p - in + 1) < (sizeof(buf)) ? (p - in + 1) : (sizeof(buf))));
  in = p;
  strlcat(buf, TextEncodingPrefix(), sizeof(buf));
  out = buf + strlen(buf);
  char *lastValidCharacter = &buf[1024 - strlen(TextEncodingSuffix()) - 1];
  while (*in && out <= lastValidCharacter) {
    out += TextEncodingEncode(out, *in, lastValidCharacter - out + 1);
    ++in;
  }
  strlcat(buf, TextEncodingSuffix(), sizeof(buf));
  return buf;
}


x86 assembly (clang, -O0):
<encode_say>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
lea    0x2edd(%rip),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpw   $0x0,(%rax)
je     11e2 <encode_say+0x62>
mov    -0x18(%rbp),%rax
movzwl (%rax),%eax
cmp    $0xff,%eax
jle    11cc <encode_say+0x4c>
jmp    120e <encode_say+0x8e>
jmp    11d1 <encode_say+0x51>
mov    -0x18(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x18(%rbp)
jmp    11a7 <encode_say+0x27>
mov    -0x10(%rbp),%rdi
call   10a0 <wcs2str@plt>
mov    %eax,%esi
lea    0x2e7c(%rip),%rdi
mov    $0x400,%edx
call   1080 <strlcpy@plt>
lea    0x2e6b(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1382 <encode_say+0x202>
mov    -0x10(%rbp),%rdi
call   10a0 <wcs2str@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
sar    $0x1,%rax
add    $0x1,%rax
cmp    $0x400,%rax
jae    1255 <encode_say+0xd5>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
sar    $0x1,%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    1263 <encode_say+0xe3>
mov    $0x400,%eax
mov    %rax,-0x38(%rbp)
jmp    1263 <encode_say+0xe3>
mov    -0x2c(%rbp),%esi
mov    -0x38(%rbp),%rax
mov    %eax,%edx
lea    0x2dfd(%rip),%rdi
call   1080 <strlcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1030 <TextEncodingPrefix@plt>
mov    %rax,%rsi
lea    0x2ddf(%rip),%rdi
mov    $0x400,%edx
call   1040 <strlcat@plt>
lea    0x2dce(%rip),%rdi
call   1050 <strlen@plt>
movslq %eax,%rcx
lea    0x2dbf(%rip),%rax
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1070 <TextEncodingSuffix@plt>
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    $0x400,%eax
sub    %ecx,%eax
sub    $0x1,%eax
movslq %eax,%rcx
lea    0x2d93(%rip),%rax
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movzwl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x39(%rbp)
je     1307 <encode_say+0x187>
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
setbe  %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    1317 <encode_say+0x197>
jmp    135c <encode_say+0x1dc>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%ax
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rdx
sub    %rdx,%rcx
add    $0x1,%rcx
mov    %ecx,%edx
movzwl %ax,%esi
call   1090 <TextEncodingEncode@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x10(%rbp)
jmp    12e4 <encode_say+0x164>
mov    $0x0,%al
call   1070 <TextEncodingSuffix@plt>
mov    %rax,%rsi
lea    0x2d03(%rip),%rdi
mov    $0x400,%edx
call   1040 <strlcat@plt>
lea    0x2cf2(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encode_say>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
lea    0x2ede(%rip),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
movzwl (%r15),%eax
test   %ax,%ax
je     11cc <encode_say+0x4c>
lea    0x2(%r15),%r12
xor    %ebx,%ebx
movzwl %ax,%eax
cmp    $0xff,%eax
ja     11ec <encode_say+0x6c>
movzwl (%r12),%eax
add    $0x2,%r12
add    $0x2,%rbx
test   %ax,%ax
jne    11b0 <encode_say+0x30>
mov    %r15,%rdi
call   10a0 <wcs2str@plt>
lea    0x2e95(%rip),%rdi
mov    %eax,%esi
mov    $0x400,%edx
call   1080 <strlcpy@plt>
jmp    12b6 <encode_say+0x136>
mov    %r15,%rdi
call   10a0 <wcs2str@plt>
sar    %rbx
add    $0x1,%rbx
cmp    $0x400,%rbx
mov    $0x400,%ecx
cmovae %ecx,%ebx
mov    %r14,%rdi
mov    %eax,%esi
mov    %ebx,%edx
call   1080 <strlcpy@plt>
xor    %eax,%eax
call   1030 <TextEncodingPrefix@plt>
mov    %r14,%rdi
mov    %rax,%rsi
mov    $0x400,%edx
call   1040 <strlcat@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1070 <TextEncodingSuffix@plt>
mov    %rax,%rdi
call   1050 <strlen@plt>
movzwl -0x2(%r12),%ecx
test   %cx,%cx
je     129b <encode_say+0x11b>
mov    $0x3ff,%edx
sub    %eax,%edx
cmp    %edx,%ebx
jg     129b <encode_say+0x11b>
movslq %edx,%r15
add    %r14,%r15
movslq %ebx,%rbx
add    %r14,%rbx
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edx
sub    %ebx,%edx
add    $0x1,%edx
movzwl %cx,%esi
mov    %rbx,%rdi
call   1090 <TextEncodingEncode@plt>
movzwl (%r12),%ecx
test   %cx,%cx
je     129b <encode_say+0x11b>
cltq
add    %rax,%rbx
add    $0x2,%r12
cmp    %r15,%rbx
jbe    1270 <encode_say+0xf0>
xor    %eax,%eax
call   1070 <TextEncodingSuffix@plt>
lea    0x2dc7(%rip),%rdi
mov    %rax,%rsi
mov    $0x400,%edx
call   1040 <strlcat@plt>
lea    0x2db3(%rip),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@encode_say.buf = internal global [1024 x i8] zeroinitializer
define i8* @encode_say(i16* %0) {
%2 = alloca i8*
%3 = alloca i16*
%4 = alloca i16*
%5 = alloca i8*
%6 = alloca i8*
store i16* %0, i16** %3
%7 = call i32 @memset(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @encode_say.buf, i64 0, i64 0), i32 0, i32 1024)
%8 = load i16*, i16** %3
store i16* %8, i16** %4
br label %9
9: ; preds = %20, %1
%10 = load i16*, i16** %4
%11 = load i16, i16* %10
%12 = icmp ne i16 %11, 0
br i1 %12, label %13, label %23
13: ; preds = %9
%14 = load i16*, i16** %4
%15 = load i16, i16* %14
%16 = zext i16 %15 to i32
%17 = icmp sgt i32 %16, 255
br i1 %17, label %18, label %19
18: ; preds = %13
br label %27
19: ; preds = %13
br label %20
20: ; preds = %19
%21 = load i16*, i16** %4
%22 = getelementptr inbounds i16, i16* %21, i32 1
store i16* %22, i16** %4
br label %9
23: ; preds = %9
%24 = load i16*, i16** %3
%25 = call i32 @wcs2str(i16* %24)
%26 = call i32 @strlcpy(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @encode_say.buf, i64 0, i64 0), i32 %25, i32 1024)
store i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @encode_say.buf, i64 0, i64 0), i8** %2
br label %94
27: ; preds = %18
%28 = load i16*, i16** %3
%29 = call i32 @wcs2str(i16* %28)
%30 = load i16*, i16** %4
%31 = load i16*, i16** %3
%32 = ptrtoint i16* %30 to i64
%33 = ptrtoint i16* %31 to i64
%34 = sub i64 %32, %33
%35 = sdiv exact i64 %34, 2
%36 = add nsw i64 %35, 1
%37 = icmp ult i64 %36, 1024
br i1 %37, label %38, label %46
38: ; preds = %27
%39 = load i16*, i16** %4
%40 = load i16*, i16** %3
%41 = ptrtoint i16* %39 to i64
%42 = ptrtoint i16* %40 to i64
%43 = sub i64 %41, %42
%44 = sdiv exact i64 %43, 2
%45 = add nsw i64 %44, 1
br label %47
46: ; preds = %27
br label %47
47: ; preds = %46, %38
%48 = phi i64 [ %45, %38 ], [ 1024, %46 ]
%49 = trunc i64 %48 to i32
%50 = call i32 @strlcpy(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @encode_say.buf, i64 0, i64 0), i32 %29, i32 %49)
%51 = load i16*, i16** %4
store i16* %51, i16** %3
%52 = call i8* (...) @TextEncodingPrefix()
%53 = call i32 @strlcat(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @encode_say.buf, i64 0, i64 0), i8* %52, i32 1024)
%54 = call i32 @strlen(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @encode_say.buf, i64 0, i64 0))
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @encode_say.buf, i64 0, i64 0), i64 %55
store i8* %56, i8** %5
%57 = call i8* (...) @TextEncodingSuffix()
%58 = call i32 @strlen(i8* %57)
%59 = sub nsw i32 1024, %58
%60 = sub nsw i32 %59, 1
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds [1024 x i8], [1024 x i8]* @encode_say.buf, i64 0, i64 %61
store i8* %62, i8** %6
br label %63
63: ; preds = %74, %47
%64 = load i16*, i16** %3
%65 = load i16, i16* %64
%66 = zext i16 %65 to i32
%67 = icmp ne i32 %66, 0
br i1 %67, label %68, label %72
68: ; preds = %63
%69 = load i8*, i8** %5
%70 = load i8*, i8** %6
%71 = icmp ule i8* %69, %70
br label %72
72: ; preds = %68, %63
%73 = phi i1 [ false, %63 ], [ %71, %68 ]
br i1 %73, label %74, label %91
74: ; preds = %72
%75 = load i8*, i8** %5
%76 = load i16*, i16** %3
%77 = load i16, i16* %76
%78 = load i8*, i8** %6
%79 = load i8*, i8** %5
%80 = ptrtoint i8* %78 to i64
%81 = ptrtoint i8* %79 to i64
%82 = sub i64 %80, %81
%83 = add nsw i64 %82, 1
%84 = trunc i64 %83 to i32
%85 = call i32 @TextEncodingEncode(i8* %75, i16 zeroext %77, i32 %84)
%86 = load i8*, i8** %5
%87 = sext i32 %85 to i64
%88 = getelementptr inbounds i8, i8* %86, i64 %87
store i8* %88, i8** %5
%89 = load i16*, i16** %3
%90 = getelementptr inbounds i16, i16* %89, i32 1
store i16* %90, i16** %3
br label %63
91: ; preds = %72
%92 = call i8* (...) @TextEncodingSuffix()
%93 = call i32 @strlcat(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @encode_say.buf, i64 0, i64 0), i8* %92, i32 1024)
store i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @encode_say.buf, i64 0, i64 0), i8** %2
br label %94
94: ; preds = %91, %23
%95 = load i8*, i8** %2
ret i8* %95
}

LLVM IR (-O2):
@encode_say.buf = internal global [1024 x i8] zeroinitializer
define i8* @encode_say(i16* %0) {
%2 = tail call i32 @memset(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @encode_say.buf, i64 0, i64 0), i32 0, i32 1024)
%3 = load i16, i16* %0
%4 = icmp eq i16 %3, 0
br i1 %4, label %13, label %5
5: ; preds = %1, %9
%6 = phi i16 [ %11, %9 ], [ %3, %1 ]
%7 = phi i16* [ %10, %9 ], [ %0, %1 ]
%8 = icmp ugt i16 %6, 255
br i1 %8, label %16, label %9
9: ; preds = %5
%10 = getelementptr inbounds i16, i16* %7, i64 1
%11 = load i16, i16* %10
%12 = icmp eq i16 %11, 0
br i1 %12, label %13, label %5
13: ; preds = %9, %1
%14 = tail call i32 @wcs2str(i16* %0)
%15 = tail call i32 @strlcpy(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @encode_say.buf, i64 0, i64 0), i32 %14, i32 1024)
br label %62
16: ; preds = %5
%17 = tail call i32 @wcs2str(i16* %0)
%18 = ptrtoint i16* %7 to i64
%19 = ptrtoint i16* %0 to i64
%20 = sub i64 %18, %19
%21 = ashr exact i64 %20, 1
%22 = add nsw i64 %21, 1
%23 = icmp ult i64 %22, 1024
%24 = select i1 %23, i64 %22, i64 1024
%25 = trunc i64 %24 to i32
%26 = tail call i32 @strlcpy(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @encode_say.buf, i64 0, i64 0), i32 %17, i32 %25)
%27 = tail call i8* (...) @TextEncodingPrefix()
%28 = tail call i32 @strlcat(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @encode_say.buf, i64 0, i64 0), i8* %27, i32 1024)
%29 = tail call i32 @strlen(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @encode_say.buf, i64 0, i64 0))
%30 = tail call i8* (...) @TextEncodingSuffix()
%31 = tail call i32 @strlen(i8* %30)
%32 = sub i32 1023, %31
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* @encode_say.buf, i64 0, i64 %33
%35 = load i16, i16* %7
%36 = icmp ne i16 %35, 0
%37 = icmp sle i32 %29, %32
%38 = select i1 %36, i1 %37, i1 false
br i1 %38, label %39, label %59
39: ; preds = %16
%40 = sext i32 %29 to i64
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* @encode_say.buf, i64 0, i64 %40
%42 = ptrtoint i8* %34 to i64
br label %43
43: ; preds = %39, %43
%44 = phi i16 [ %35, %39 ], [ %55, %43 ]
%45 = phi i8* [ %41, %39 ], [ %53, %43 ]
%46 = phi i16* [ %7, %39 ], [ %54, %43 ]
%47 = ptrtoint i8* %45 to i64
%48 = sub i64 %42, %47
%49 = trunc i64 %48 to i32
%50 = add i32 %49, 1
%51 = tail call i32 @TextEncodingEncode(i8* %45, i16 zeroext %44, i32 %50)
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %45, i64 %52
%54 = getelementptr inbounds i16, i16* %46, i64 1
%55 = load i16, i16* %54
%56 = icmp ne i16 %55, 0
%57 = icmp ule i8* %53, %34
%58 = select i1 %56, i1 %57, i1 false
br i1 %58, label %43, label %59
59: ; preds = %43, %16
%60 = tail call i8* (...) @TextEncodingSuffix()
%61 = tail call i32 @strlcat(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @encode_say.buf, i64 0, i64 0), i8* %60, i32 1024)
br label %62
62: ; preds = %59, %13
ret i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @encode_say.buf, i64 0, i64 0)
}