#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*/ UText;
typedef int /*<<< orphan*/ UErrorCode;
typedef int /*<<< orphan*/ UBreakIterator;

/* Variables and functions */
int /*<<< orphan*/ UBRK_CHARACTER;
int UBRK_DONE;
int /*<<< orphan*/ U_SUCCESS(int /*<<< orphan*/);
int /*<<< orphan*/ U_ZERO_ERROR;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ ubrk_close(int /*<<< orphan*/ *);
int ubrk_following(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ *ubrk_open(int /*<<< orphan*/, int /*<<< orphan*/ *,
                              int /*<<< orphan*/ *, int /*<<< orphan*/,
                              int /*<<< orphan*/ *);
int /*<<< orphan*/ ubrk_setUText(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                 int /*<<< orphan*/ *);
int /*<<< orphan*/ utext_close(int /*<<< orphan*/ *);
int /*<<< orphan*/ *utext_openUTF8(int /*<<< orphan*/ *, char *, int,
                                   int /*<<< orphan*/ *);
size_t next_char(char *buffer, size_t position) {
  size_t result = position;
  UErrorCode status = U_ZERO_ERROR;
  UText *ut = utext_openUTF8(NULL, buffer, -1, &status);
  UBreakIterator *it = ubrk_open(UBRK_CHARACTER, NULL, NULL, 0, &status);
  ubrk_setUText(it, ut, &status);
  int boundary = ubrk_following(it, (int)position);
  if (boundary != UBRK_DONE) {
    result = (size_t)boundary;
  }
  ubrk_close(it);
  utext_close(ut);
  assert(U_SUCCESS(status));
  return result;
}


x86 assembly (clang, -O0):
<next_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
mov    $0xffffffff,%edx
lea    -0x1c(%rbp),%rcx
call   1040 <utext_openUTF8@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%edx
xor    %ecx,%ecx
lea    -0x1c(%rbp),%r8
mov    %rdx,%rsi
call   1090 <ubrk_open@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1030 <ubrk_setUText@plt>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    %eax,%esi
call   10a0 <ubrk_following@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    0x2de6(%rip),%rcx
cmp    (%rcx),%eax
je     121a <next_char+0x9a>
movslq -0x34(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x30(%rbp),%rdi
call   1070 <ubrk_close@plt>
mov    -0x28(%rbp),%rdi
call   1050 <utext_close@plt>
mov    -0x1c(%rbp),%edi
call   1080 <U_SUCCESS@plt>
mov    %eax,%edi
call   1060 <assert@plt>
mov    -0x18(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<next_char>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rsi
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
lea    0x4(%rsp),%r15
xor    %edi,%edi
mov    $0xffffffff,%edx
mov    %r15,%rcx
call   1040 <utext_openUTF8@plt>
mov    %rax,%r12
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1090 <ubrk_open@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
call   1030 <ubrk_setUText@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   10a0 <ubrk_following@plt>
mov    0x2e04(%rip),%rcx
cmp    (%rcx),%eax
movslq %eax,%r15
cmove  %r14,%r15
mov    %rbx,%rdi
call   1070 <ubrk_close@plt>
mov    %r12,%rdi
call   1050 <utext_close@plt>
mov    0x4(%rsp),%edi
call   1080 <U_SUCCESS@plt>
mov    %eax,%edi
call   1060 <assert@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@U_ZERO_ERROR = global i32 0
@UBRK_CHARACTER = global i32 0
@UBRK_DONE = global i32 0
define i64 @next_char(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
%10 = load i64, i64* %4
store i64 %10, i64* %5
%11 = load i32, i32* @U_ZERO_ERROR
store i32 %11, i32* %6
%12 = load i8*, i8** %3
%13 = call i32* @utext_openUTF8(i32* null, i8* %12, i32 -1, i32* %6)
store i32* %13, i32** %7
%14 = load i32, i32* @UBRK_CHARACTER
%15 = call i32* @ubrk_open(i32 %14, i32* null, i32* null, i32 0, i32* %6)
store i32* %15, i32** %8
%16 = load i32*, i32** %8
%17 = load i32*, i32** %7
%18 = call i32 @ubrk_setUText(i32* %16, i32* %17, i32* %6)
%19 = load i32*, i32** %8
%20 = load i64, i64* %4
%21 = trunc i64 %20 to i32
%22 = call i32 @ubrk_following(i32* %19, i32 %21)
store i32 %22, i32* %9
%23 = load i32, i32* %9
%24 = load i32, i32* @UBRK_DONE
%25 = icmp ne i32 %23, %24
br i1 %25, label %26, label %29
26: ; preds = %2
%27 = load i32, i32* %9
%28 = sext i32 %27 to i64
store i64 %28, i64* %5
br label %29
29: ; preds = %26, %2
%30 = load i32*, i32** %8
%31 = call i32 @ubrk_close(i32* %30)
%32 = load i32*, i32** %7
%33 = call i32 @utext_close(i32* %32)
%34 = load i32, i32* %6
%35 = call i32 @U_SUCCESS(i32 %34)
%36 = call i32 @assert(i32 %35)
%37 = load i64, i64* %5
ret i64 %37
}

LLVM IR (-O2):
@U_ZERO_ERROR = global i32 0
@UBRK_CHARACTER = global i32 0
@UBRK_DONE = global i32 0
define i64 @next_char(i8* %0, i64 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = load i32, i32* @U_ZERO_ERROR
store i32 %5, i32* %3
%6 = call i32* @utext_openUTF8(i32* null, i8* %0, i32 -1, i32* %3)
%7 = load i32, i32* @UBRK_CHARACTER
%8 = call i32* @ubrk_open(i32 %7, i32* null, i32* null, i32 0, i32* %3)
%9 = call i32 @ubrk_setUText(i32* %8, i32* %6, i32* %3)
%10 = trunc i64 %1 to i32
%11 = call i32 @ubrk_following(i32* %8, i32 %10)
%12 = load i32, i32* @UBRK_DONE
%13 = icmp eq i32 %11, %12
%14 = sext i32 %11 to i64
%15 = select i1 %13, i64 %1, i64 %14
%16 = call i32 @ubrk_close(i32* %8)
%17 = call i32 @utext_close(i32* %6)
%18 = load i32, i32* %3
%19 = call i32 @U_SUCCESS(i32 %18)
%20 = call i32 @assert(i32 %19)
ret i64 %15
}