#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*/ is_valid_utf8(unsigned char const *, int);
scalar_t__ utf8_to_eci(int, unsigned char const *, unsigned char *, int *);
__attribute__((visibility("hidden"))) int
get_best_eci(const unsigned char source[], int length) {
  int eci = 3;
  unsigned char local_source[length + 1];
  do {
    if (eci == 14) {
      eci = 15;
    } else if (eci == 19) {
      eci = 21;
    }
    if (utf8_to_eci(eci, source, local_source, &length) == 0) {
      return eci;
    }
    eci++;
  } while (eci < 25);
  if (!is_valid_utf8(source, length)) {
    return 0;
  }
  return 26;
}


x86 assembly (clang, -O0):
<get_best_eci>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x3,-0x18(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
cmpl   $0xe,-0x18(%rbp)
jne    1175 <get_best_eci+0x55>
movl   $0xf,-0x18(%rbp)
jmp    118b <get_best_eci+0x6b>
cmpl   $0x13,-0x18(%rbp)
jne    1186 <get_best_eci+0x66>
movl   $0x15,-0x18(%rbp)
jmp    118b <get_best_eci+0x6b>
mov    -0x38(%rbp),%rdx
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rcx
call   1030 <utf8_to_eci@plt>
cmp    $0x0,%rax
jne    11bb <get_best_eci+0x9b>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    1204 <get_best_eci+0xe4>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x19,-0x18(%rbp)
jl     115f <get_best_eci+0x3f>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <is_valid_utf8@plt>
cmp    $0x0,%eax
jne    11f6 <get_best_eci+0xd6>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    1204 <get_best_eci+0xe4>
movl   $0x1a,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_best_eci>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    %esi,-0x2c(%rbp)
add    $0x1,%esi
mov    %rsp,%r15
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
sub    %rsi,%r15
mov    %r15,%rsp
mov    $0x3,%eax
mov    $0xf,%r13d
lea    -0x2c(%rbp),%r12
nopw   0x0(%rax,%rax,1)
cmp    $0x13,%eax
mov    %eax,%ebx
mov    $0x15,%ecx
cmove  %ecx,%ebx
cmp    $0xe,%eax
cmove  %r13d,%ebx
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
mov    %r12,%rcx
call   1030 <utf8_to_eci@plt>
test   %rax,%rax
je     11a6 <get_best_eci+0x86>
lea    0x1(%rbx),%eax
cmp    $0x18,%ebx
jl     1160 <get_best_eci+0x40>
mov    -0x2c(%rbp),%esi
mov    %r14,%rdi
call   1040 <is_valid_utf8@plt>
test   %eax,%eax
mov    $0x1a,%ebx
cmove  %eax,%ebx
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define hidden i32 @get_best_eci(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 3, i32* %6
%10 = load i32, i32* %5
%11 = add nsw i32 %10, 1
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %7
%14 = alloca i8, i64 %12
store i64 %12, i64* %8
br label %15
15: ; preds = %34, %2
%16 = load i32, i32* %6
%17 = icmp eq i32 %16, 14
br i1 %17, label %18, label %19
18: ; preds = %15
store i32 15, i32* %6
br label %24
19: ; preds = %15
%20 = load i32, i32* %6
%21 = icmp eq i32 %20, 19
br i1 %21, label %22, label %23
22: ; preds = %19
store i32 21, i32* %6
br label %23
23: ; preds = %22, %19
br label %24
24: ; preds = %23, %18
%25 = load i32, i32* %6
%26 = load i8*, i8** %4
%27 = call i64 @utf8_to_eci(i32 %25, i8* %26, i8* %14, i32* %5)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = load i32, i32* %6
store i32 %30, i32* %3
store i32 1, i32* %9
br label %44
31: ; preds = %24
%32 = load i32, i32* %6
%33 = add nsw i32 %32, 1
store i32 %33, i32* %6
br label %34
34: ; preds = %31
%35 = load i32, i32* %6
%36 = icmp slt i32 %35, 25
br i1 %36, label %15, label %37
37: ; preds = %34
%38 = load i8*, i8** %4
%39 = load i32, i32* %5
%40 = call i32 @is_valid_utf8(i8* %38, i32 %39)
%41 = icmp ne i32 %40, 0
br i1 %41, label %43, label %42
42: ; preds = %37
store i32 0, i32* %3
store i32 1, i32* %9
br label %44
43: ; preds = %37
store i32 26, i32* %3
store i32 1, i32* %9
br label %44
44: ; preds = %43, %42, %29
%45 = load i8*, i8** %7
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
define hidden i32 @get_best_eci(i8* %0, i32 %1) {
%3 = alloca i32
store i32 %1, i32* %3
%4 = add nsw i32 %1, 1
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
br label %7
7: ; preds = %15, %2
%8 = phi i32 [ 3, %2 ], [ %16, %15 ]
%9 = icmp eq i32 %8, 14
%10 = icmp eq i32 %8, 19
%11 = select i1 %10, i32 21, i32 %8
%12 = select i1 %9, i32 15, i32 %11
%13 = call i64 @utf8_to_eci(i32 %12, i8* %0, i8* %6, i32* %3)
%14 = icmp eq i64 %13, 0
br i1 %14, label %23, label %15
15: ; preds = %7
%16 = add nsw i32 %12, 1
%17 = icmp slt i32 %12, 24
br i1 %17, label %7, label %18
18: ; preds = %15
%19 = load i32, i32* %3
%20 = call i32 @is_valid_utf8(i8* %0, i32 %19)
%21 = icmp eq i32 %20, 0
%22 = select i1 %21, i32 0, i32 26
br label %23
23: ; preds = %7, %18
%24 = phi i32 [ %22, %18 ], [ %12, %7 ]
ret i32 %24
}