#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*/ snprintf(char *, int, char *, unsigned short);
int fteEncoder(char *out, unsigned short in, int maxCharacters) {
  if (in <= 0x7F) {
    out[0] = in;
    return 1;
  }
  if (maxCharacters < sizeof(unsigned short) * 2 + 1) {
    out[0] = '?';
    return 1;
  }
  snprintf(out, maxCharacters + 1, "^U%04x", in);
  return 6;
}


x86 assembly (clang, -O0):
<fteEncoder>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
mov    %edx,-0x18(%rbp)
movzwl -0x12(%rbp),%eax
cmp    $0x7f,%eax
jg     114b <fteEncoder+0x3b>
mov    -0x12(%rbp),%ax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    118d <fteEncoder+0x7d>
movslq -0x18(%rbp),%rax
cmp    $0x5,%rax
jae    116c <fteEncoder+0x5c>
mov    -0x10(%rbp),%rax
movb   $0x3f,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    118d <fteEncoder+0x7d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
add    $0x1,%esi
lea    0xe83(%rip),%rdx
movzwl -0x12(%rbp),%ecx
call   1030 <snprintf@plt>
movl   $0x6,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fteEncoder>:
push   %rax
cmp    $0x7f,%si
ja     111c <fteEncoder+0xc>
mov    %sil,(%rdi)
jmp    1124 <fteEncoder+0x14>
cmp    $0x4,%edx
ja     112b <fteEncoder+0x1b>
movb   $0x3f,(%rdi)
mov    $0x1,%eax
pop    %rcx
ret
add    $0x1,%edx
movzwl %si,%ecx
lea    0xec8(%rip),%rax
mov    %edx,%esi
mov    %rax,%rdx
call   1030 <snprintf@plt>
mov    $0x6,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [7 x i8] c"^U%04x\00"
define i32 @fteEncoder(i8* %0, i16 zeroext %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i16
%7 = alloca i32
store i8* %0, i8** %5
store i16 %1, i16* %6
store i32 %2, i32* %7
%8 = load i16, i16* %6
%9 = zext i16 %8 to i32
%10 = icmp sle i32 %9, 127
br i1 %10, label %11, label %16
11: ; preds = %3
%12 = load i16, i16* %6
%13 = trunc i16 %12 to i8
%14 = load i8*, i8** %5
%15 = getelementptr inbounds i8, i8* %14, i64 0
store i8 %13, i8* %15
store i32 1, i32* %4
br label %29
16: ; preds = %3
%17 = load i32, i32* %7
%18 = sext i32 %17 to i64
%19 = icmp ult i64 %18, 5
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i8*, i8** %5
%22 = getelementptr inbounds i8, i8* %21, i64 0
store i8 63, i8* %22
store i32 1, i32* %4
br label %29
23: ; preds = %16
%24 = load i8*, i8** %5
%25 = load i32, i32* %7
%26 = add nsw i32 %25, 1
%27 = load i16, i16* %6
%28 = call i32 @snprintf(i8* %24, i32 %26, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i16 zeroext %27)
store i32 6, i32* %4
br label %29
29: ; preds = %23, %20, %11
%30 = load i32, i32* %4
ret i32 %30
}

LLVM IR (-O2):
@.str = [7 x i8] c"^U%04x\00"
define i32 @fteEncoder(i8* %0, i16 zeroext %1, i32 %2) {
%4 = icmp ult i16 %1, 128
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = trunc i16 %1 to i8
store i8 %6, i8* %0
br label %13
7: ; preds = %3
%8 = icmp ult i32 %2, 5
br i1 %8, label %9, label %10
9: ; preds = %7
store i8 63, i8* %0
br label %13
10: ; preds = %7
%11 = add nsw i32 %2, 1
%12 = tail call i32 @snprintf(i8* %0, i32 %11, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i16 zeroext %1)
br label %13
13: ; preds = %10, %9, %5
%14 = phi i32 [ 1, %5 ], [ 1, %9 ], [ 6, %10 ]
ret i32 %14
}