#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*/ isCombiningChar(int);
scalar_t__ isWideChar(int);
size_t prevUtf8CharLen(char const *, size_t);
int /*<<< orphan*/ utf8BytesToCodePoint(char const *, size_t, int *);
size_t linenoiseUtf8PrevCharLen(const char *buf, size_t buf_len, size_t pos,
                                size_t *col_len) {
  (void)(buf_len);
  size_t end = pos;
  while (pos > 0) {
    size_t len = prevUtf8CharLen(buf, pos);
    pos -= len;
    int cp;
    utf8BytesToCodePoint(buf + pos, len, &cp);
    if (!isCombiningChar(cp)) {
      if (col_len != NULL)
        *col_len = isWideChar(cp) ? 2 : 1;
      return end - pos;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<linenoiseUtf8PrevCharLen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    11f8 <linenoiseUtf8PrevCharLen+0xb8>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <prevUtf8CharLen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
add    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   1030 <utf8BytesToCodePoint@plt>
mov    -0x3c(%rbp),%edi
call   1040 <isCombiningChar@plt>
cmp    $0x0,%eax
jne    11f3 <linenoiseUtf8PrevCharLen+0xb3>
cmpq   $0x0,-0x28(%rbp)
je     11e2 <linenoiseUtf8PrevCharLen+0xa2>
mov    -0x3c(%rbp),%edi
call   1050 <isWideChar@plt>
mov    %rax,%rdx
mov    $0x1,%eax
mov    $0x2,%ecx
cmp    $0x0,%rdx
cmovne %ecx,%eax
movslq %eax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
sub    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1200 <linenoiseUtf8PrevCharLen+0xc0>
jmp    1160 <linenoiseUtf8PrevCharLen+0x20>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<linenoiseUtf8PrevCharLen>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %rdx,%rdx
je     119f <linenoiseUtf8PrevCharLen+0x5f>
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rdi,%r12
lea    0xc(%rsp),%r13
mov    %rdx,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r12,%rdi
mov    %rbx,%rsi
call   1060 <prevUtf8CharLen@plt>
sub    %rax,%rbx
lea    (%r12,%rbx,1),%rdi
mov    %rax,%rsi
mov    %r13,%rdx
call   1030 <utf8BytesToCodePoint@plt>
mov    0xc(%rsp),%edi
call   1040 <isCombiningChar@plt>
test   %eax,%eax
je     11a4 <linenoiseUtf8PrevCharLen+0x64>
test   %rbx,%rbx
jne    1170 <linenoiseUtf8PrevCharLen+0x30>
xor    %r15d,%r15d
jmp    11c5 <linenoiseUtf8PrevCharLen+0x85>
test   %r14,%r14
je     11c2 <linenoiseUtf8PrevCharLen+0x82>
mov    0xc(%rsp),%edi
call   1050 <isWideChar@plt>
cmp    $0x1,%rax
mov    $0x2,%eax
sbb    $0x0,%rax
mov    %rax,(%r14)
sub    %rbx,%r15
mov    %r15,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @linenoiseUtf8PrevCharLen(i8* %0, i64 %1, i64 %2, i64* %3) {
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i32
store i8* %0, i8** %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i64* %3, i64** %9
%13 = load i64, i64* %7
%14 = load i64, i64* %8
store i64 %14, i64* %10
br label %15
15: ; preds = %48, %4
%16 = load i64, i64* %8
%17 = icmp ugt i64 %16, 0
br i1 %17, label %18, label %49
18: ; preds = %15
%19 = load i8*, i8** %6
%20 = load i64, i64* %8
%21 = call i64 @prevUtf8CharLen(i8* %19, i64 %20)
store i64 %21, i64* %11
%22 = load i64, i64* %11
%23 = load i64, i64* %8
%24 = sub i64 %23, %22
store i64 %24, i64* %8
%25 = load i8*, i8** %6
%26 = load i64, i64* %8
%27 = getelementptr inbounds i8, i8* %25, i64 %26
%28 = load i64, i64* %11
%29 = call i32 @utf8BytesToCodePoint(i8* %27, i64 %28, i32* %12)
%30 = load i32, i32* %12
%31 = call i32 @isCombiningChar(i32 %30)
%32 = icmp ne i32 %31, 0
br i1 %32, label %48, label %33
33: ; preds = %18
%34 = load i64*, i64** %9
%35 = icmp ne i64* %34, null
br i1 %35, label %36, label %44
36: ; preds = %33
%37 = load i32, i32* %12
%38 = call i64 @isWideChar(i32 %37)
%39 = icmp ne i64 %38, 0
%40 = zext i1 %39 to i64
%41 = select i1 %39, i32 2, i32 1
%42 = sext i32 %41 to i64
%43 = load i64*, i64** %9
store i64 %42, i64* %43
br label %44
44: ; preds = %36, %33
%45 = load i64, i64* %10
%46 = load i64, i64* %8
%47 = sub i64 %45, %46
store i64 %47, i64* %5
br label %50
48: ; preds = %18
br label %15
49: ; preds = %15
store i64 0, i64* %5
br label %50
50: ; preds = %49, %44
%51 = load i64, i64* %5
ret i64 %51
}

LLVM IR (-O2):
define i64 @linenoiseUtf8PrevCharLen(i8* %0, i64 %1, i64 %2, i64* writeonly %3) {
%5 = alloca i32
%6 = icmp eq i64 %2, 0
br i1 %6, label %29, label %7
7: ; preds = %4
%8 = bitcast i32* %5 to i8*
br label %9
9: ; preds = %7, %27
%10 = phi i64 [ %2, %7 ], [ %12, %27 ]
%11 = call i64 @prevUtf8CharLen(i8* %0, i64 %10)
%12 = sub i64 %10, %11
%13 = getelementptr inbounds i8, i8* %0, i64 %12
%14 = call i32 @utf8BytesToCodePoint(i8* %13, i64 %11, i32* %5)
%15 = load i32, i32* %5
%16 = call i32 @isCombiningChar(i32 %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %27
18: ; preds = %9
%19 = icmp eq i64* %3, null
br i1 %19, label %25, label %20
20: ; preds = %18
%21 = load i32, i32* %5
%22 = call i64 @isWideChar(i32 %21)
%23 = icmp eq i64 %22, 0
%24 = select i1 %23, i64 1, i64 2
store i64 %24, i64* %3
br label %25
25: ; preds = %18, %20
%26 = sub i64 %2, %12
br label %29
27: ; preds = %9
%28 = icmp eq i64 %12, 0
br i1 %28, label %29, label %9
29: ; preds = %27, %4, %25
%30 = phi i64 [ %26, %25 ], [ 0, %4 ], [ 0, %27 ]
ret i64 %30
}