#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct TYPE_3__ {
  size_t rm_eo;
  size_t rm_so;
};
typedef TYPE_1__ regmatch_t;

/* Variables and functions */
size_t get_char_width_esc(char const *const);
size_t count_substr_chars(const char line[], regmatch_t *match) {
  const size_t sub_len = match->rm_eo - match->rm_so;
  const char *const sub = line + match->rm_so;
  size_t count = 0;
  size_t i = 0;
  while (i < sub_len) {
    const size_t char_width_esc = get_char_width_esc(sub + i);
    i += char_width_esc;
    count++;
  }
  return count;
}


x86 assembly (clang, -O0):
<count_substr_chars>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x10(%rbp),%rcx
sub    0x8(%rcx),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
add    0x8(%rcx),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    118f <count_substr_chars+0x7f>
mov    -0x20(%rbp),%rdi
add    -0x30(%rbp),%rdi
call   1030 <get_char_width_esc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1153 <count_substr_chars+0x43>
mov    -0x28(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_substr_chars>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    (%rsi),%r15
mov    0x8(%rsi),%rax
sub    %rax,%r15
je     1147 <count_substr_chars+0x37>
mov    %rdi,%r14
add    %rax,%r14
xor    %r12d,%r12d
xor    %ebx,%ebx
nop
lea    (%r14,%rbx,1),%rdi
call   1030 <get_char_width_esc@plt>
add    %rax,%rbx
add    $0x1,%r12
cmp    %r15,%rbx
jb     1130 <count_substr_chars+0x20>
jmp    114a <count_substr_chars+0x3a>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i64, i64 }
define i64 @count_substr_chars(i8* %0, %struct.TYPE_3__* %1) {
%3 = alloca i8*
%4 = alloca %struct.TYPE_3__*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i8* %0, i8** %3
store %struct.TYPE_3__* %1, %struct.TYPE_3__** %4
%10 = load %struct.TYPE_3__*, %struct.TYPE_3__** %4
%11 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %10, i32 0, i32 0
%12 = load i64, i64* %11
%13 = load %struct.TYPE_3__*, %struct.TYPE_3__** %4
%14 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %13, i32 0, i32 1
%15 = load i64, i64* %14
%16 = sub i64 %12, %15
store i64 %16, i64* %5
%17 = load i8*, i8** %3
%18 = load %struct.TYPE_3__*, %struct.TYPE_3__** %4
%19 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %18, i32 0, i32 1
%20 = load i64, i64* %19
%21 = getelementptr inbounds i8, i8* %17, i64 %20
store i8* %21, i8** %6
store i64 0, i64* %7
store i64 0, i64* %8
br label %22
22: ; preds = %26, %2
%23 = load i64, i64* %8
%24 = load i64, i64* %5
%25 = icmp ult i64 %23, %24
br i1 %25, label %26, label %36
26: ; preds = %22
%27 = load i8*, i8** %6
%28 = load i64, i64* %8
%29 = getelementptr inbounds i8, i8* %27, i64 %28
%30 = call i64 @get_char_width_esc(i8* %29)
store i64 %30, i64* %9
%31 = load i64, i64* %9
%32 = load i64, i64* %8
%33 = add i64 %32, %31
store i64 %33, i64* %8
%34 = load i64, i64* %7
%35 = add i64 %34, 1
store i64 %35, i64* %7
br label %22
36: ; preds = %22
%37 = load i64, i64* %7
ret i64 %37
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i64, i64 }
define i64 @count_substr_chars(i8* %0, %struct.TYPE_3__* nocapture %1) {
%3 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 0
%4 = load i64, i64* %3
%5 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 1
%6 = load i64, i64* %5
%7 = sub i64 %4, %6
%8 = getelementptr inbounds i8, i8* %0, i64 %6
%9 = icmp eq i64 %7, 0
br i1 %9, label %18, label %10
10: ; preds = %2, %10
%11 = phi i64 [ %16, %10 ], [ 0, %2 ]
%12 = phi i64 [ %15, %10 ], [ 0, %2 ]
%13 = getelementptr inbounds i8, i8* %8, i64 %12
%14 = tail call i64 @get_char_width_esc(i8* %13)
%15 = add i64 %14, %12
%16 = add i64 %11, 1
%17 = icmp ult i64 %15, %7
br i1 %17, label %10, label %18
18: ; preds = %10, %2
%19 = phi i64 [ 0, %2 ], [ %16, %10 ]
ret i64 %19
}